blob: 3f37f5737bf36f6f0726c0756b5a34df7fcd5135 [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':
Rich Laneadb79832013-05-02 17:14:33 -070058 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 Lanea06d0c32013-03-25 08:52:03 -070067 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 Laned367a242013-05-02 16:14:23 -070074 elif self.base == 'of_meter_features_t':
75 v = 'common.meter_features()'
Rich Lanea06d0c32013-03-25 08:52:03 -070076 else:
77 v = "None"
78
79 if self.is_array:
80 return "[" + ','.join([v] * self.array_length) + "]"
81 else:
82 return v
83
84 def gen_pack_expr(self, expr_expr):
85 pack_fmt = self._pack_fmt()
86 if pack_fmt and not self.is_array:
87 return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr)
88 elif pack_fmt and self.is_array:
89 return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr)
90 elif self.base == 'of_octets_t':
91 return expr_expr
92 elif utils.class_is_list(self.base):
93 return '"".join([x.pack() for x in %s])' % expr_expr
94 elif self.base == 'of_mac_addr_t':
95 return 'struct.pack("!6B", *%s)' % expr_expr
Rich Lane41805642013-03-19 15:00:26 -070096 elif self.base == 'of_ipv6_t':
97 return 'struct.pack("!16s", %s)' % expr_expr
Rich Laned367a242013-05-02 16:14:23 -070098 elif self.base in ['of_match_t', 'of_port_desc_t', 'of_meter_features_t']:
Rich Lanea06d0c32013-03-25 08:52:03 -070099 return '%s.pack()' % expr_expr
100 elif self.base == 'of_port_name_t':
101 return self._gen_string_pack_expr(16, expr_expr)
102 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
103 return self._gen_string_pack_expr(32, expr_expr)
104 elif self.base == 'of_desc_str_t':
105 return self._gen_string_pack_expr(256, expr_expr)
106 else:
Rich Lanea0186052013-05-01 14:18:39 -0700107 return "loxi.unimplemented('pack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700108
109 def _gen_string_pack_expr(self, length, expr_expr):
110 return 'struct.pack("!%ds", %s)' % (length, expr_expr)
111
Rich Lane57026dc2013-05-01 10:13:16 -0700112 def gen_unpack_expr(self, reader_expr):
Rich Lanea06d0c32013-03-25 08:52:03 -0700113 pack_fmt = self._pack_fmt()
114 if pack_fmt and not self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700115 return "%s.read('!%s')[0]" % (reader_expr, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700116 elif pack_fmt and self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700117 return "list(%s.read('!%d%s'))" % (self.array_length, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700118 elif self.base == 'of_octets_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700119 return "str(%s.read_all())" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700120 elif self.base == 'of_mac_addr_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700121 return "list(%s.read('!6B'))" % (reader_expr)
Rich Lane41805642013-03-19 15:00:26 -0700122 elif self.base == 'of_ipv6_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700123 return "%s.read('!16s')[0]" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700124 elif self.base == 'of_match_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700125 return 'common.match.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700126 elif self.base == 'of_port_desc_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700127 return 'common.port_desc.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700128 elif self.base == 'of_list_action_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700129 return 'action.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700130 elif self.base == 'of_list_flow_stats_entry_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700131 return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700132 elif self.base == 'of_list_queue_prop_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700133 return 'common.unpack_list_queue_prop(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700134 elif self.base == 'of_list_packet_queue_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700135 return 'common.unpack_list_packet_queue(%s)' % (reader_expr)
Rich Lanee90685c2013-04-05 17:27:41 -0700136 elif self.base == 'of_list_hello_elem_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700137 return 'common.unpack_list_hello_elem(%s)' % (reader_expr)
138 elif self.base == 'of_list_oxm_t':
Rich Lane21fd0112013-05-01 12:50:04 -0700139 # HACK need the match_v3 length field
140 return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr)
Rich Lane8e27ec72013-05-02 11:04:31 -0700141 elif self.base == 'of_list_bucket_t':
142 return 'common.unpack_list_bucket(%s)' % (reader_expr)
Rich Lane9b38d112013-05-02 14:35:40 -0700143 elif self.base == 'of_list_group_desc_stats_entry_t':
144 return 'common.unpack_list_group_desc_stats_entry(%s)' % (reader_expr)
Rich Lane42bf98c2013-05-02 14:48:32 -0700145 elif self.base == 'of_list_group_stats_entry_t':
146 return 'common.unpack_list_group_stats_entry(%s)' % (reader_expr)
Rich Laned82c0a62013-05-02 15:40:35 -0700147 elif self.base == 'of_list_meter_band_t':
148 return 'meter_band.unpack_list(%s)' % (reader_expr)
Rich Lane6c3acb22013-05-02 15:59:05 -0700149 elif self.base == 'of_list_meter_stats_t':
150 return 'common.unpack_list_meter_stats(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700151 elif self.base == 'of_port_name_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700152 return self._gen_string_unpack_expr(reader_expr, 16)
Rich Lanea06d0c32013-03-25 08:52:03 -0700153 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700154 return self._gen_string_unpack_expr(reader_expr, 32)
Rich Lanea06d0c32013-03-25 08:52:03 -0700155 elif self.base == 'of_desc_str_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700156 return self._gen_string_unpack_expr(reader_expr, 256)
Rich Laned367a242013-05-02 16:14:23 -0700157 elif self.base == 'of_meter_features_t':
158 return 'common.meter_features.unpack(%s)' % (reader_expr)
Rich Laneed4f9062013-05-02 17:05:03 -0700159 elif self.base == 'of_list_instruction_t':
160 return 'instruction.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700161 elif utils.class_is_list(self.base):
162 element_cls = utils.list_to_entry_type(self.base)[:-2]
Rich Lane8692ecd2013-05-02 11:33:53 -0700163 if ((element_cls, self.version) in of_g.is_fixed_length) \
164 and not element_cls in loxi_front_end.type_maps.inheritance_map:
Rich Lanea06d0c32013-03-25 08:52:03 -0700165 klass_name = self.base[8:-2]
166 element_size, = of_g.base_length[(element_cls, self.version)],
Rich Lane57026dc2013-05-01 10:13:16 -0700167 return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700168 else:
Rich Lanea0186052013-05-01 14:18:39 -0700169 return "loxi.unimplemented('unpack list %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700170 else:
Rich Lanea0186052013-05-01 14:18:39 -0700171 return "loxi.unimplemented('unpack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700172
Rich Lane57026dc2013-05-01 10:13:16 -0700173 def _gen_string_unpack_expr(self, reader_expr, length):
174 return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700175
176 def _pack_fmt(self):
177 if self.base == "char":
178 return "B"
179 if self.base == "uint8_t":
180 return "B"
181 if self.base == "uint16_t":
182 return "H"
183 if self.base == "uint32_t":
184 return "L"
185 if self.base == "uint64_t":
186 return "Q"
187 if self.base == "of_port_no_t":
188 if self.version == of_g.VERSION_1_0:
189 return "H"
190 else:
191 return "L"
192 if self.base == "of_fm_cmd_t":
193 if self.version == of_g.VERSION_1_0:
194 return "H"
195 else:
196 return "B"
197 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
198 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
199 return "L"
200 else:
201 return "Q"
202 return None
203
204class TestOFType(unittest.TestCase):
205 def test_init(self):
206 from oftype import OFType
207 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
208 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
209
210 def test_pack(self):
211 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
212
213 def test_unpack(self):
214 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
215
216if __name__ == '__main__':
217 unittest.main()