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): |
| 49 | if utils.class_is_list(self.base): |
| 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': |
| 58 | v = 'const.OFPFW_ALL' |
| 59 | elif self.base in ['of_octets_t', 'of_port_name_t', 'of_table_name_t', |
| 60 | 'of_desc_str_t', 'of_serial_num_t']: |
| 61 | v = '""' |
| 62 | elif self.base == 'of_match_t': |
| 63 | v = 'common.match()' |
| 64 | elif self.base == 'of_port_desc_t': |
| 65 | v = 'common.port_desc()' |
| 66 | else: |
| 67 | v = "None" |
| 68 | |
| 69 | if self.is_array: |
| 70 | return "[" + ','.join([v] * self.array_length) + "]" |
| 71 | else: |
| 72 | return v |
| 73 | |
| 74 | def gen_pack_expr(self, expr_expr): |
| 75 | pack_fmt = self._pack_fmt() |
| 76 | if pack_fmt and not self.is_array: |
| 77 | return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr) |
| 78 | elif pack_fmt and self.is_array: |
| 79 | return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr) |
| 80 | elif self.base == 'of_octets_t': |
| 81 | return expr_expr |
| 82 | elif utils.class_is_list(self.base): |
| 83 | return '"".join([x.pack() for x in %s])' % expr_expr |
| 84 | elif self.base == 'of_mac_addr_t': |
| 85 | return 'struct.pack("!6B", *%s)' % expr_expr |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 86 | elif self.base == 'of_ipv6_t': |
| 87 | return 'struct.pack("!16s", %s)' % expr_expr |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 88 | elif self.base in ['of_match_t', 'of_port_desc_t']: |
| 89 | return '%s.pack()' % expr_expr |
| 90 | elif self.base == 'of_port_name_t': |
| 91 | return self._gen_string_pack_expr(16, expr_expr) |
| 92 | elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t': |
| 93 | return self._gen_string_pack_expr(32, expr_expr) |
| 94 | elif self.base == 'of_desc_str_t': |
| 95 | return self._gen_string_pack_expr(256, expr_expr) |
| 96 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 97 | return "loxi.unimplemented('pack %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 98 | |
| 99 | def _gen_string_pack_expr(self, length, expr_expr): |
| 100 | return 'struct.pack("!%ds", %s)' % (length, expr_expr) |
| 101 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 102 | def gen_unpack_expr(self, reader_expr): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 103 | pack_fmt = self._pack_fmt() |
| 104 | if pack_fmt and not self.is_array: |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 105 | return "%s.read('!%s')[0]" % (reader_expr, pack_fmt) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 106 | elif pack_fmt and self.is_array: |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 107 | return "list(%s.read('!%d%s'))" % (self.array_length, pack_fmt) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 108 | elif self.base == 'of_octets_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 109 | return "str(%s.read_all())" % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | elif self.base == 'of_mac_addr_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 111 | return "list(%s.read('!6B'))" % (reader_expr) |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 112 | elif self.base == 'of_ipv6_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 113 | return "%s.read('!16s')[0]" % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 114 | elif self.base == 'of_match_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 115 | return 'common.match.unpack(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 116 | elif self.base == 'of_port_desc_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 117 | return 'common.port_desc.unpack(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 118 | elif self.base == 'of_list_action_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 119 | return 'action.unpack_list(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 120 | elif self.base == 'of_list_flow_stats_entry_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 121 | return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 122 | elif self.base == 'of_list_queue_prop_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 123 | return 'common.unpack_list_queue_prop(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 124 | elif self.base == 'of_list_packet_queue_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 125 | return 'common.unpack_list_packet_queue(%s)' % (reader_expr) |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 126 | elif self.base == 'of_list_hello_elem_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 127 | return 'common.unpack_list_hello_elem(%s)' % (reader_expr) |
| 128 | elif self.base == 'of_list_oxm_t': |
Rich Lane | 21fd011 | 2013-05-01 12:50:04 -0700 | [diff] [blame] | 129 | # HACK need the match_v3 length field |
| 130 | return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr) |
Rich Lane | 8e27ec7 | 2013-05-02 11:04:31 -0700 | [diff] [blame] | 131 | elif self.base == 'of_list_bucket_t': |
| 132 | return 'common.unpack_list_bucket(%s)' % (reader_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 133 | elif self.base == 'of_port_name_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 134 | return self._gen_string_unpack_expr(reader_expr, 16) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | 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] | 136 | return self._gen_string_unpack_expr(reader_expr, 32) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 137 | elif self.base == 'of_desc_str_t': |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 138 | return self._gen_string_unpack_expr(reader_expr, 256) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 139 | elif utils.class_is_list(self.base): |
| 140 | element_cls = utils.list_to_entry_type(self.base)[:-2] |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame^] | 141 | if ((element_cls, self.version) in of_g.is_fixed_length) \ |
| 142 | and not element_cls in loxi_front_end.type_maps.inheritance_map: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 143 | klass_name = self.base[8:-2] |
| 144 | element_size, = of_g.base_length[(element_cls, self.version)], |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 145 | 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] | 146 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 147 | return "loxi.unimplemented('unpack list %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 148 | else: |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 149 | return "loxi.unimplemented('unpack %s')" % self.base |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 150 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 151 | def _gen_string_unpack_expr(self, reader_expr, length): |
| 152 | return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 153 | |
| 154 | def _pack_fmt(self): |
| 155 | if self.base == "char": |
| 156 | return "B" |
| 157 | if self.base == "uint8_t": |
| 158 | return "B" |
| 159 | if self.base == "uint16_t": |
| 160 | return "H" |
| 161 | if self.base == "uint32_t": |
| 162 | return "L" |
| 163 | if self.base == "uint64_t": |
| 164 | return "Q" |
| 165 | if self.base == "of_port_no_t": |
| 166 | if self.version == of_g.VERSION_1_0: |
| 167 | return "H" |
| 168 | else: |
| 169 | return "L" |
| 170 | if self.base == "of_fm_cmd_t": |
| 171 | if self.version == of_g.VERSION_1_0: |
| 172 | return "H" |
| 173 | else: |
| 174 | return "B" |
| 175 | if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]: |
| 176 | if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]: |
| 177 | return "L" |
| 178 | else: |
| 179 | return "Q" |
| 180 | return None |
| 181 | |
| 182 | class TestOFType(unittest.TestCase): |
| 183 | def test_init(self): |
| 184 | from oftype import OFType |
| 185 | self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr()) |
| 186 | self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr()) |
| 187 | |
| 188 | def test_pack(self): |
| 189 | self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"')) |
| 190 | |
| 191 | def test_unpack(self): |
| 192 | self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8)) |
| 193 | |
| 194 | if __name__ == '__main__': |
| 195 | unittest.main() |