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 |
| 30 | import unittest |
| 31 | |
| 32 | class OFType(object): |
| 33 | """ |
| 34 | Encapsulates knowledge about the OpenFlow type system. |
| 35 | """ |
| 36 | |
| 37 | version = None |
| 38 | base = None |
| 39 | is_array = False |
| 40 | array_length = None |
| 41 | |
| 42 | def __init__(self, string, version): |
| 43 | self.version = version |
| 44 | self.array_length, self.base = utils.type_dec_to_count_base(string) |
| 45 | self.is_array = self.array_length != 1 |
| 46 | |
| 47 | def gen_init_expr(self): |
| 48 | if utils.class_is_list(self.base): |
| 49 | v = "[]" |
| 50 | elif self.base.find("uint") == 0 or self.base in ["char", "of_port_no_t"]: |
| 51 | v = "0" |
| 52 | elif self.base == 'of_mac_addr_t': |
| 53 | v = '[0,0,0,0,0,0]' |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 54 | elif self.base == 'of_ipv6_t': |
| 55 | v = repr('\x00' * 16) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | elif self.base == 'of_wc_bmap_t': |
| 57 | v = 'const.OFPFW_ALL' |
| 58 | elif self.base in ['of_octets_t', 'of_port_name_t', 'of_table_name_t', |
| 59 | 'of_desc_str_t', 'of_serial_num_t']: |
| 60 | v = '""' |
| 61 | elif self.base == 'of_match_t': |
| 62 | v = 'common.match()' |
| 63 | elif self.base == 'of_port_desc_t': |
| 64 | v = 'common.port_desc()' |
| 65 | else: |
| 66 | v = "None" |
| 67 | |
| 68 | if self.is_array: |
| 69 | return "[" + ','.join([v] * self.array_length) + "]" |
| 70 | else: |
| 71 | return v |
| 72 | |
| 73 | def gen_pack_expr(self, expr_expr): |
| 74 | pack_fmt = self._pack_fmt() |
| 75 | if pack_fmt and not self.is_array: |
| 76 | return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr) |
| 77 | elif pack_fmt and self.is_array: |
| 78 | return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr) |
| 79 | elif self.base == 'of_octets_t': |
| 80 | return expr_expr |
| 81 | elif utils.class_is_list(self.base): |
| 82 | return '"".join([x.pack() for x in %s])' % expr_expr |
| 83 | elif self.base == 'of_mac_addr_t': |
| 84 | return 'struct.pack("!6B", *%s)' % expr_expr |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 85 | elif self.base == 'of_ipv6_t': |
| 86 | return 'struct.pack("!16s", %s)' % expr_expr |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 87 | elif self.base in ['of_match_t', 'of_port_desc_t']: |
| 88 | return '%s.pack()' % expr_expr |
| 89 | elif self.base == 'of_port_name_t': |
| 90 | return self._gen_string_pack_expr(16, expr_expr) |
| 91 | elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t': |
| 92 | return self._gen_string_pack_expr(32, expr_expr) |
| 93 | elif self.base == 'of_desc_str_t': |
| 94 | return self._gen_string_pack_expr(256, expr_expr) |
| 95 | else: |
| 96 | return "'TODO pack %s'" % self.base |
| 97 | |
| 98 | def _gen_string_pack_expr(self, length, expr_expr): |
| 99 | return 'struct.pack("!%ds", %s)' % (length, expr_expr) |
| 100 | |
| 101 | def gen_unpack_expr(self, buf_expr, offset_expr): |
| 102 | pack_fmt = self._pack_fmt() |
| 103 | if pack_fmt and not self.is_array: |
| 104 | return "struct.unpack_from('!%s', %s, %s)[0]" % (pack_fmt, buf_expr, offset_expr) |
| 105 | elif pack_fmt and self.is_array: |
| 106 | return "list(struct.unpack_from('!%d%s', %s, %s))" % (self.array_length, pack_fmt, buf_expr, offset_expr) |
| 107 | elif self.base == 'of_octets_t': |
| 108 | return "%s[%s:]" % (buf_expr, offset_expr) |
| 109 | elif self.base == 'of_mac_addr_t': |
| 110 | return "list(struct.unpack_from('!6B', %s, %s))" % (buf_expr, offset_expr) |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 111 | elif self.base == 'of_ipv6_t': |
| 112 | return "struct.unpack_from('!16s', %s, %s)[0]" % (buf_expr, offset_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 113 | elif self.base == 'of_match_t': |
| 114 | return 'common.match.unpack(buffer(%s, %s))' % (buf_expr, offset_expr) |
| 115 | elif self.base == 'of_port_desc_t': |
| 116 | return 'common.port_desc.unpack(buffer(%s, %s))' % (buf_expr, offset_expr) |
| 117 | elif self.base == 'of_list_action_t': |
| 118 | return 'action.unpack_list(buffer(%s, %s))' % (buf_expr, offset_expr) |
| 119 | elif self.base == 'of_list_flow_stats_entry_t': |
| 120 | return 'common.unpack_list_flow_stats_entry(buffer(%s, %s))' % (buf_expr, offset_expr) |
| 121 | elif self.base == 'of_list_queue_prop_t': |
| 122 | return 'common.unpack_list_queue_prop(buffer(%s, %s))' % (buf_expr, offset_expr) |
| 123 | elif self.base == 'of_list_packet_queue_t': |
| 124 | return 'common.unpack_list_packet_queue(buffer(%s, %s))' % (buf_expr, offset_expr) |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 125 | elif self.base == 'of_list_hello_elem_t': |
| 126 | return 'common.unpack_list_hello_elem(buffer(%s, %s))' % (buf_expr, offset_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 127 | elif self.base == 'of_port_name_t': |
| 128 | return self._gen_string_unpack_expr(16, buf_expr, offset_expr) |
| 129 | elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t': |
| 130 | return self._gen_string_unpack_expr(32, buf_expr, offset_expr) |
| 131 | elif self.base == 'of_desc_str_t': |
| 132 | return self._gen_string_unpack_expr(256, buf_expr, offset_expr) |
| 133 | elif utils.class_is_list(self.base): |
| 134 | element_cls = utils.list_to_entry_type(self.base)[:-2] |
| 135 | if ((element_cls, self.version) in of_g.is_fixed_length): |
| 136 | klass_name = self.base[8:-2] |
| 137 | element_size, = of_g.base_length[(element_cls, self.version)], |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame^] | 138 | return 'loxi.generic_util.unpack_array(common.%s.unpack, %d, buffer(%s, %s))' % (klass_name, element_size, buf_expr, offset_expr) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 139 | else: |
| 140 | return "None # TODO unpack list %s" % self.base |
| 141 | else: |
| 142 | return "None # TODO unpack %s" % self.base |
| 143 | |
| 144 | def _gen_string_unpack_expr(self, length, buf_expr, offset_expr): |
| 145 | return 'str(buffer(%s, %s, %d)).rstrip("\\x00")' % (buf_expr, offset_expr, length) |
| 146 | |
| 147 | def _pack_fmt(self): |
| 148 | if self.base == "char": |
| 149 | return "B" |
| 150 | if self.base == "uint8_t": |
| 151 | return "B" |
| 152 | if self.base == "uint16_t": |
| 153 | return "H" |
| 154 | if self.base == "uint32_t": |
| 155 | return "L" |
| 156 | if self.base == "uint64_t": |
| 157 | return "Q" |
| 158 | if self.base == "of_port_no_t": |
| 159 | if self.version == of_g.VERSION_1_0: |
| 160 | return "H" |
| 161 | else: |
| 162 | return "L" |
| 163 | if self.base == "of_fm_cmd_t": |
| 164 | if self.version == of_g.VERSION_1_0: |
| 165 | return "H" |
| 166 | else: |
| 167 | return "B" |
| 168 | if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]: |
| 169 | if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]: |
| 170 | return "L" |
| 171 | else: |
| 172 | return "Q" |
| 173 | return None |
| 174 | |
| 175 | class TestOFType(unittest.TestCase): |
| 176 | def test_init(self): |
| 177 | from oftype import OFType |
| 178 | self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr()) |
| 179 | self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr()) |
| 180 | |
| 181 | def test_pack(self): |
| 182 | self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"')) |
| 183 | |
| 184 | def test_unpack(self): |
| 185 | self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8)) |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | unittest.main() |