blob: ca3106e9aa23b00bef8249a8c396f475b9ed78d7 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001# 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
28import of_g
29import loxi_utils.loxi_utils as utils
Rich Lane8692ecd2013-05-02 11:33:53 -070030import loxi_front_end.type_maps
Rich Lanea06d0c32013-03-25 08:52:03 -070031import unittest
32
33class 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 Lane41805642013-03-19 15:00:26 -070055 elif self.base == 'of_ipv6_t':
56 v = repr('\x00' * 16)
Rich Lanea06d0c32013-03-25 08:52:03 -070057 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 Lane41805642013-03-19 15:00:26 -070086 elif self.base == 'of_ipv6_t':
87 return 'struct.pack("!16s", %s)' % expr_expr
Rich Lanea06d0c32013-03-25 08:52:03 -070088 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 Lanea0186052013-05-01 14:18:39 -070097 return "loxi.unimplemented('pack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -070098
99 def _gen_string_pack_expr(self, length, expr_expr):
100 return 'struct.pack("!%ds", %s)' % (length, expr_expr)
101
Rich Lane57026dc2013-05-01 10:13:16 -0700102 def gen_unpack_expr(self, reader_expr):
Rich Lanea06d0c32013-03-25 08:52:03 -0700103 pack_fmt = self._pack_fmt()
104 if pack_fmt and not self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700105 return "%s.read('!%s')[0]" % (reader_expr, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700106 elif pack_fmt and self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700107 return "list(%s.read('!%d%s'))" % (self.array_length, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700108 elif self.base == 'of_octets_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700109 return "str(%s.read_all())" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700110 elif self.base == 'of_mac_addr_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700111 return "list(%s.read('!6B'))" % (reader_expr)
Rich Lane41805642013-03-19 15:00:26 -0700112 elif self.base == 'of_ipv6_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700113 return "%s.read('!16s')[0]" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700114 elif self.base == 'of_match_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700115 return 'common.match.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700116 elif self.base == 'of_port_desc_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700117 return 'common.port_desc.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700118 elif self.base == 'of_list_action_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700119 return 'action.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700120 elif self.base == 'of_list_flow_stats_entry_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700121 return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700122 elif self.base == 'of_list_queue_prop_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700123 return 'common.unpack_list_queue_prop(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700124 elif self.base == 'of_list_packet_queue_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700125 return 'common.unpack_list_packet_queue(%s)' % (reader_expr)
Rich Lanee90685c2013-04-05 17:27:41 -0700126 elif self.base == 'of_list_hello_elem_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700127 return 'common.unpack_list_hello_elem(%s)' % (reader_expr)
128 elif self.base == 'of_list_oxm_t':
Rich Lane21fd0112013-05-01 12:50:04 -0700129 # HACK need the match_v3 length field
130 return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr)
Rich Lane8e27ec72013-05-02 11:04:31 -0700131 elif self.base == 'of_list_bucket_t':
132 return 'common.unpack_list_bucket(%s)' % (reader_expr)
Rich Lane9b38d112013-05-02 14:35:40 -0700133 elif self.base == 'of_list_group_desc_stats_entry_t':
134 return 'common.unpack_list_group_desc_stats_entry(%s)' % (reader_expr)
Rich Lane42bf98c2013-05-02 14:48:32 -0700135 elif self.base == 'of_list_group_stats_entry_t':
136 return 'common.unpack_list_group_stats_entry(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700137 elif self.base == 'of_port_name_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700138 return self._gen_string_unpack_expr(reader_expr, 16)
Rich Lanea06d0c32013-03-25 08:52:03 -0700139 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700140 return self._gen_string_unpack_expr(reader_expr, 32)
Rich Lanea06d0c32013-03-25 08:52:03 -0700141 elif self.base == 'of_desc_str_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700142 return self._gen_string_unpack_expr(reader_expr, 256)
Rich Lanea06d0c32013-03-25 08:52:03 -0700143 elif utils.class_is_list(self.base):
144 element_cls = utils.list_to_entry_type(self.base)[:-2]
Rich Lane8692ecd2013-05-02 11:33:53 -0700145 if ((element_cls, self.version) in of_g.is_fixed_length) \
146 and not element_cls in loxi_front_end.type_maps.inheritance_map:
Rich Lanea06d0c32013-03-25 08:52:03 -0700147 klass_name = self.base[8:-2]
148 element_size, = of_g.base_length[(element_cls, self.version)],
Rich Lane57026dc2013-05-01 10:13:16 -0700149 return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700150 else:
Rich Lanea0186052013-05-01 14:18:39 -0700151 return "loxi.unimplemented('unpack list %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700152 else:
Rich Lanea0186052013-05-01 14:18:39 -0700153 return "loxi.unimplemented('unpack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700154
Rich Lane57026dc2013-05-01 10:13:16 -0700155 def _gen_string_unpack_expr(self, reader_expr, length):
156 return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700157
158 def _pack_fmt(self):
159 if self.base == "char":
160 return "B"
161 if self.base == "uint8_t":
162 return "B"
163 if self.base == "uint16_t":
164 return "H"
165 if self.base == "uint32_t":
166 return "L"
167 if self.base == "uint64_t":
168 return "Q"
169 if self.base == "of_port_no_t":
170 if self.version == of_g.VERSION_1_0:
171 return "H"
172 else:
173 return "L"
174 if self.base == "of_fm_cmd_t":
175 if self.version == of_g.VERSION_1_0:
176 return "H"
177 else:
178 return "B"
179 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
180 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
181 return "L"
182 else:
183 return "Q"
184 return None
185
186class TestOFType(unittest.TestCase):
187 def test_init(self):
188 from oftype import OFType
189 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
190 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
191
192 def test_pack(self):
193 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
194
195 def test_unpack(self):
196 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
197
198if __name__ == '__main__':
199 unittest.main()