blob: 3717036de0fdcdf067b01e3e847b18fbf480c5da [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()'
Rich Laned367a242013-05-02 16:14:23 -070066 elif self.base == 'of_meter_features_t':
67 v = 'common.meter_features()'
Rich Lanea06d0c32013-03-25 08:52:03 -070068 else:
69 v = "None"
70
71 if self.is_array:
72 return "[" + ','.join([v] * self.array_length) + "]"
73 else:
74 return v
75
76 def gen_pack_expr(self, expr_expr):
77 pack_fmt = self._pack_fmt()
78 if pack_fmt and not self.is_array:
79 return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr)
80 elif pack_fmt and self.is_array:
81 return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr)
82 elif self.base == 'of_octets_t':
83 return expr_expr
84 elif utils.class_is_list(self.base):
85 return '"".join([x.pack() for x in %s])' % expr_expr
86 elif self.base == 'of_mac_addr_t':
87 return 'struct.pack("!6B", *%s)' % expr_expr
Rich Lane41805642013-03-19 15:00:26 -070088 elif self.base == 'of_ipv6_t':
89 return 'struct.pack("!16s", %s)' % expr_expr
Rich Laned367a242013-05-02 16:14:23 -070090 elif self.base in ['of_match_t', 'of_port_desc_t', 'of_meter_features_t']:
Rich Lanea06d0c32013-03-25 08:52:03 -070091 return '%s.pack()' % expr_expr
92 elif self.base == 'of_port_name_t':
93 return self._gen_string_pack_expr(16, expr_expr)
94 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
95 return self._gen_string_pack_expr(32, expr_expr)
96 elif self.base == 'of_desc_str_t':
97 return self._gen_string_pack_expr(256, expr_expr)
98 else:
Rich Lanea0186052013-05-01 14:18:39 -070099 return "loxi.unimplemented('pack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700100
101 def _gen_string_pack_expr(self, length, expr_expr):
102 return 'struct.pack("!%ds", %s)' % (length, expr_expr)
103
Rich Lane57026dc2013-05-01 10:13:16 -0700104 def gen_unpack_expr(self, reader_expr):
Rich Lanea06d0c32013-03-25 08:52:03 -0700105 pack_fmt = self._pack_fmt()
106 if pack_fmt and not self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700107 return "%s.read('!%s')[0]" % (reader_expr, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700108 elif pack_fmt and self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700109 return "list(%s.read('!%d%s'))" % (self.array_length, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700110 elif self.base == 'of_octets_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700111 return "str(%s.read_all())" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700112 elif self.base == 'of_mac_addr_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700113 return "list(%s.read('!6B'))" % (reader_expr)
Rich Lane41805642013-03-19 15:00:26 -0700114 elif self.base == 'of_ipv6_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700115 return "%s.read('!16s')[0]" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700116 elif self.base == 'of_match_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700117 return 'common.match.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700118 elif self.base == 'of_port_desc_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700119 return 'common.port_desc.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700120 elif self.base == 'of_list_action_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700121 return 'action.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700122 elif self.base == 'of_list_flow_stats_entry_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700123 return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700124 elif self.base == 'of_list_queue_prop_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700125 return 'common.unpack_list_queue_prop(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700126 elif self.base == 'of_list_packet_queue_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700127 return 'common.unpack_list_packet_queue(%s)' % (reader_expr)
Rich Lanee90685c2013-04-05 17:27:41 -0700128 elif self.base == 'of_list_hello_elem_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700129 return 'common.unpack_list_hello_elem(%s)' % (reader_expr)
130 elif self.base == 'of_list_oxm_t':
Rich Lane21fd0112013-05-01 12:50:04 -0700131 # HACK need the match_v3 length field
132 return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr)
Rich Lane8e27ec72013-05-02 11:04:31 -0700133 elif self.base == 'of_list_bucket_t':
134 return 'common.unpack_list_bucket(%s)' % (reader_expr)
Rich Lane9b38d112013-05-02 14:35:40 -0700135 elif self.base == 'of_list_group_desc_stats_entry_t':
136 return 'common.unpack_list_group_desc_stats_entry(%s)' % (reader_expr)
Rich Lane42bf98c2013-05-02 14:48:32 -0700137 elif self.base == 'of_list_group_stats_entry_t':
138 return 'common.unpack_list_group_stats_entry(%s)' % (reader_expr)
Rich Laned82c0a62013-05-02 15:40:35 -0700139 elif self.base == 'of_list_meter_band_t':
140 return 'meter_band.unpack_list(%s)' % (reader_expr)
Rich Lane6c3acb22013-05-02 15:59:05 -0700141 elif self.base == 'of_list_meter_stats_t':
142 return 'common.unpack_list_meter_stats(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700143 elif self.base == 'of_port_name_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700144 return self._gen_string_unpack_expr(reader_expr, 16)
Rich Lanea06d0c32013-03-25 08:52:03 -0700145 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700146 return self._gen_string_unpack_expr(reader_expr, 32)
Rich Lanea06d0c32013-03-25 08:52:03 -0700147 elif self.base == 'of_desc_str_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700148 return self._gen_string_unpack_expr(reader_expr, 256)
Rich Laned367a242013-05-02 16:14:23 -0700149 elif self.base == 'of_meter_features_t':
150 return 'common.meter_features.unpack(%s)' % (reader_expr)
Rich Laneed4f9062013-05-02 17:05:03 -0700151 elif self.base == 'of_list_instruction_t':
152 return 'instruction.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700153 elif utils.class_is_list(self.base):
154 element_cls = utils.list_to_entry_type(self.base)[:-2]
Rich Lane8692ecd2013-05-02 11:33:53 -0700155 if ((element_cls, self.version) in of_g.is_fixed_length) \
156 and not element_cls in loxi_front_end.type_maps.inheritance_map:
Rich Lanea06d0c32013-03-25 08:52:03 -0700157 klass_name = self.base[8:-2]
158 element_size, = of_g.base_length[(element_cls, self.version)],
Rich Lane57026dc2013-05-01 10:13:16 -0700159 return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700160 else:
Rich Lanea0186052013-05-01 14:18:39 -0700161 return "loxi.unimplemented('unpack list %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700162 else:
Rich Lanea0186052013-05-01 14:18:39 -0700163 return "loxi.unimplemented('unpack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700164
Rich Lane57026dc2013-05-01 10:13:16 -0700165 def _gen_string_unpack_expr(self, reader_expr, length):
166 return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700167
168 def _pack_fmt(self):
169 if self.base == "char":
170 return "B"
171 if self.base == "uint8_t":
172 return "B"
173 if self.base == "uint16_t":
174 return "H"
175 if self.base == "uint32_t":
176 return "L"
177 if self.base == "uint64_t":
178 return "Q"
179 if self.base == "of_port_no_t":
180 if self.version == of_g.VERSION_1_0:
181 return "H"
182 else:
183 return "L"
184 if self.base == "of_fm_cmd_t":
185 if self.version == of_g.VERSION_1_0:
186 return "H"
187 else:
188 return "B"
189 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
190 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
191 return "L"
192 else:
193 return "Q"
194 return None
195
196class TestOFType(unittest.TestCase):
197 def test_init(self):
198 from oftype import OFType
199 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
200 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
201
202 def test_pack(self):
203 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
204
205 def test_unpack(self):
206 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
207
208if __name__ == '__main__':
209 unittest.main()