Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # Copyright 2013, Big Switch Networks, Inc. |
| 2 | # |
| 3 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | # the following special exception: |
| 5 | # |
| 6 | # LOXI Exception |
| 7 | # |
| 8 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | # |
| 15 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | # |
| 17 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | # a copy of the EPL at: |
| 19 | # |
| 20 | # http://www.eclipse.org/legal/epl-v10.html |
| 21 | # |
| 22 | # Unless required by applicable law or agreed to in writing, software |
| 23 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | # EPL for the specific language governing permissions and limitations |
| 26 | # under the EPL. |
| 27 | |
| 28 | import of_g |
| 29 | import loxi_utils.loxi_utils as utils |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 30 | import loxi_front_end.type_maps |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 31 | import unittest |
| 32 | |
| 33 | class OFType(object): |
| 34 | """ |
| 35 | Encapsulates knowledge about the OpenFlow type system. |
| 36 | """ |
| 37 | |
| 38 | version = None |
| 39 | base = None |
| 40 | is_array = False |
| 41 | array_length = None |
| 42 | |
| 43 | def __init__(self, string, version): |
| 44 | self.version = version |
| 45 | self.array_length, self.base = utils.type_dec_to_count_base(string) |
| 46 | self.is_array = self.array_length != 1 |
| 47 | |
| 48 | def gen_init_expr(self): |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 49 | if self.base.startswith('list('): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 50 | v = "[]" |
| 51 | elif self.base.find("uint") == 0 or self.base in ["char", "of_port_no_t"]: |
| 52 | v = "0" |
| 53 | elif self.base == 'of_mac_addr_t': |
| 54 | v = '[0,0,0,0,0,0]' |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 55 | elif self.base == 'of_ipv6_t': |
| 56 | v = repr('\x00' * 16) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 57 | elif self.base == 'of_wc_bmap_t': |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 58 | if self.version in [1,2]: |
| 59 | v = 'const.OFPFW_ALL' |
| 60 | else: |
| 61 | v = 0 |
| 62 | elif self.base == "of_match_bmap_t": |
| 63 | if self.version in [1,2]: |
| 64 | v = 'const.OFPFW_ALL' |
| 65 | else: |
| 66 | v = 0 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 67 | elif self.base in ['of_octets_t', 'of_port_name_t', 'of_table_name_t', |
| 68 | 'of_desc_str_t', 'of_serial_num_t']: |
| 69 | v = '""' |
| 70 | elif self.base == 'of_match_t': |
| 71 | v = 'common.match()' |
| 72 | elif self.base == 'of_port_desc_t': |
| 73 | v = 'common.port_desc()' |
Rich Lane | d367a24 | 2013-05-02 16:14:23 -0700 | [diff] [blame] | 74 | elif self.base == 'of_meter_features_t': |
| 75 | v = 'common.meter_features()' |
Rich Lane | eb9d3f0 | 2013-05-31 10:24:27 -0700 | [diff] [blame] | 76 | elif self.base == 'of_bsn_vport_q_in_q_t': |
| 77 | v = 'common.bsn_vport_q_in_q()' |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 78 | else: |
| 79 | v = "None" |
| 80 | |
| 81 | if self.is_array: |
| 82 | return "[" + ','.join([v] * self.array_length) + "]" |
| 83 | else: |
| 84 | return v |
| 85 | |
| 86 | def gen_pack_expr(self, expr_expr): |
| 87 | pack_fmt = self._pack_fmt() |
| 88 | if pack_fmt and not self.is_array: |
| 89 | return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr) |
| 90 | elif pack_fmt and self.is_array: |
| 91 | return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr) |
| 92 | elif self.base == 'of_octets_t': |
| 93 | return expr_expr |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 94 | elif self.base.startswith('list('): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 95 | return '"".join([x.pack() for x in %s])' % expr_expr |
| 96 | elif self.base == 'of_mac_addr_t': |
| 97 | return 'struct.pack("!6B", *%s)' % expr_expr |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 98 | elif self.base == 'of_ipv6_t': |
| 99 | return 'struct.pack("!16s", %s)' % expr_expr |
Rich Lane | eb9d3f0 | 2013-05-31 10:24:27 -0700 | [diff] [blame] | 100 | elif self.base in ['of_match_t', 'of_port_desc_t', 'of_meter_features_t', 'of_bsn_vport_q_in_q_t']: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 101 | return '%s.pack()' % expr_expr |
| 102 | elif self.base == 'of_port_name_t': |
| 103 | return self._gen_string_pack_expr(16, expr_expr) |
| 104 | elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t': |
| 105 | return self._gen_string_pack_expr(32, expr_expr) |
| 106 | elif self.base == 'of_desc_str_t': |
| 107 | return self._gen_string_pack_expr(256, expr_expr) |
| 108 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 109 | return "loxi.unimplemented('pack %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | |
| 111 | def _gen_string_pack_expr(self, length, expr_expr): |
| 112 | return 'struct.pack("!%ds", %s)' % (length, expr_expr) |
| 113 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 114 | def gen_unpack_expr(self, reader_expr): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 115 | pack_fmt = self._pack_fmt() |
| 116 | if pack_fmt and not self.is_array: |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 117 | return "%s.read('!%s')[0]" % (reader_expr, pack_fmt) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 118 | elif pack_fmt and self.is_array: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 119 | return "list(%s.read('!%d%s'))" % (reader_expr, self.array_length, pack_fmt) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 120 | elif self.base == 'of_octets_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 121 | return "str(%s.read_all())" % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 122 | elif self.base == 'of_mac_addr_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 123 | return "list(%s.read('!6B'))" % (reader_expr) |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 124 | elif self.base == 'of_ipv6_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 125 | return "%s.read('!16s')[0]" % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 126 | elif self.base == 'of_match_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 127 | return 'common.match.unpack(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 128 | elif self.base == 'of_port_desc_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 129 | return 'common.port_desc.unpack(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 130 | elif self.base == 'list(of_action_t)': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 131 | return 'action.unpack_list(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 132 | elif self.base == 'list(of_flow_stats_entry_t)': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 133 | return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 134 | elif self.base == 'list(of_queue_prop_t)': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 135 | return 'common.unpack_list_queue_prop(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 136 | elif self.base == 'list(of_packet_queue_t)': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 137 | return 'common.unpack_list_packet_queue(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 138 | elif self.base == 'list(of_hello_elem_t)': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 139 | return 'common.unpack_list_hello_elem(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 140 | elif self.base == 'list(of_oxm_t)': |
Rich Lane | 21fd011 | 2013-05-01 12:50:04 -0700 | [diff] [blame] | 141 | # HACK need the match_v3 length field |
| 142 | return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 143 | elif self.base == 'list(of_bucket_t)': |
Rich Lane | 8e27ec7 | 2013-05-02 11:04:31 -0700 | [diff] [blame] | 144 | return 'common.unpack_list_bucket(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 145 | elif self.base == 'list(of_group_desc_stats_entry_t)': |
Rich Lane | 9b38d11 | 2013-05-02 14:35:40 -0700 | [diff] [blame] | 146 | return 'common.unpack_list_group_desc_stats_entry(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 147 | elif self.base == 'list(of_group_stats_entry_t)': |
Rich Lane | 42bf98c | 2013-05-02 14:48:32 -0700 | [diff] [blame] | 148 | return 'common.unpack_list_group_stats_entry(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 149 | elif self.base == 'list(of_meter_band_t)': |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 150 | return 'meter_band.unpack_list(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 151 | elif self.base == 'list(of_meter_stats_t)': |
Rich Lane | 6c3acb2 | 2013-05-02 15:59:05 -0700 | [diff] [blame] | 152 | return 'common.unpack_list_meter_stats(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 153 | elif self.base == 'of_port_name_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 154 | return self._gen_string_unpack_expr(reader_expr, 16) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 155 | elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 156 | return self._gen_string_unpack_expr(reader_expr, 32) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 157 | elif self.base == 'of_desc_str_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 158 | return self._gen_string_unpack_expr(reader_expr, 256) |
Rich Lane | d367a24 | 2013-05-02 16:14:23 -0700 | [diff] [blame] | 159 | elif self.base == 'of_meter_features_t': |
| 160 | return 'common.meter_features.unpack(%s)' % (reader_expr) |
Rich Lane | eb9d3f0 | 2013-05-31 10:24:27 -0700 | [diff] [blame] | 161 | elif self.base == 'of_bsn_vport_q_in_q_t': |
| 162 | return 'common.bsn_vport_q_in_q.unpack(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 163 | elif self.base == 'list(of_instruction_t)': |
Rich Lane | ed4f906 | 2013-05-02 17:05:03 -0700 | [diff] [blame] | 164 | return 'instruction.unpack_list(%s)' % (reader_expr) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 165 | elif self.base.startswith('list('): |
| 166 | element_cls = self.base[5:-3] |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 167 | if ((element_cls, self.version) in of_g.is_fixed_length) \ |
| 168 | and not element_cls in loxi_front_end.type_maps.inheritance_map: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 169 | klass_name = self.base[8:-3] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 170 | element_size, = of_g.base_length[(element_cls, self.version)], |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 171 | return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 172 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 173 | return "loxi.unimplemented('unpack list %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 174 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 175 | return "loxi.unimplemented('unpack %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 176 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 177 | def _gen_string_unpack_expr(self, reader_expr, length): |
| 178 | return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 179 | |
| 180 | def _pack_fmt(self): |
| 181 | if self.base == "char": |
| 182 | return "B" |
| 183 | if self.base == "uint8_t": |
| 184 | return "B" |
| 185 | if self.base == "uint16_t": |
| 186 | return "H" |
| 187 | if self.base == "uint32_t": |
| 188 | return "L" |
| 189 | if self.base == "uint64_t": |
| 190 | return "Q" |
| 191 | if self.base == "of_port_no_t": |
| 192 | if self.version == of_g.VERSION_1_0: |
| 193 | return "H" |
| 194 | else: |
| 195 | return "L" |
| 196 | if self.base == "of_fm_cmd_t": |
| 197 | if self.version == of_g.VERSION_1_0: |
| 198 | return "H" |
| 199 | else: |
| 200 | return "B" |
| 201 | if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]: |
| 202 | if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]: |
| 203 | return "L" |
| 204 | else: |
| 205 | return "Q" |
| 206 | return None |
| 207 | |
| 208 | class TestOFType(unittest.TestCase): |
| 209 | def test_init(self): |
| 210 | from oftype import OFType |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 211 | self.assertEquals("None", OFType("list(of_action_t)", 1).gen_init_expr()) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 212 | self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr()) |
| 213 | |
| 214 | def test_pack(self): |
| 215 | self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"')) |
| 216 | |
| 217 | def test_unpack(self): |
| 218 | self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8)) |
| 219 | |
| 220 | if __name__ == '__main__': |
| 221 | unittest.main() |