blob: 5d42058f4de088fe651076e6fc84a0666672da42 [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
30import unittest
31
32class 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 Lane41805642013-03-19 15:00:26 -070054 elif self.base == 'of_ipv6_t':
55 v = repr('\x00' * 16)
Rich Lanea06d0c32013-03-25 08:52:03 -070056 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 Lane41805642013-03-19 15:00:26 -070085 elif self.base == 'of_ipv6_t':
86 return 'struct.pack("!16s", %s)' % expr_expr
Rich Lanea06d0c32013-03-25 08:52:03 -070087 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:
Rich Lanea0186052013-05-01 14:18:39 -070096 return "loxi.unimplemented('pack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -070097
98 def _gen_string_pack_expr(self, length, expr_expr):
99 return 'struct.pack("!%ds", %s)' % (length, expr_expr)
100
Rich Lane57026dc2013-05-01 10:13:16 -0700101 def gen_unpack_expr(self, reader_expr):
Rich Lanea06d0c32013-03-25 08:52:03 -0700102 pack_fmt = self._pack_fmt()
103 if pack_fmt and not self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700104 return "%s.read('!%s')[0]" % (reader_expr, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700105 elif pack_fmt and self.is_array:
Rich Lane57026dc2013-05-01 10:13:16 -0700106 return "list(%s.read('!%d%s'))" % (self.array_length, pack_fmt)
Rich Lanea06d0c32013-03-25 08:52:03 -0700107 elif self.base == 'of_octets_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700108 return "str(%s.read_all())" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700109 elif self.base == 'of_mac_addr_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700110 return "list(%s.read('!6B'))" % (reader_expr)
Rich Lane41805642013-03-19 15:00:26 -0700111 elif self.base == 'of_ipv6_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700112 return "%s.read('!16s')[0]" % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700113 elif self.base == 'of_match_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700114 return 'common.match.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700115 elif self.base == 'of_port_desc_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700116 return 'common.port_desc.unpack(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700117 elif self.base == 'of_list_action_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700118 return 'action.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700119 elif self.base == 'of_list_flow_stats_entry_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700120 return 'common.unpack_list_flow_stats_entry(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700121 elif self.base == 'of_list_queue_prop_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700122 return 'common.unpack_list_queue_prop(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700123 elif self.base == 'of_list_packet_queue_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700124 return 'common.unpack_list_packet_queue(%s)' % (reader_expr)
Rich Lanee90685c2013-04-05 17:27:41 -0700125 elif self.base == 'of_list_hello_elem_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700126 return 'common.unpack_list_hello_elem(%s)' % (reader_expr)
127 elif self.base == 'of_list_oxm_t':
Rich Lane21fd0112013-05-01 12:50:04 -0700128 # HACK need the match_v3 length field
129 return 'oxm.unpack_list(%s.slice(_length-4))' % (reader_expr)
Rich Lane8e27ec72013-05-02 11:04:31 -0700130 elif self.base == 'of_list_bucket_t':
131 return 'common.unpack_list_bucket(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700132 elif self.base == 'of_port_name_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700133 return self._gen_string_unpack_expr(reader_expr, 16)
Rich Lanea06d0c32013-03-25 08:52:03 -0700134 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700135 return self._gen_string_unpack_expr(reader_expr, 32)
Rich Lanea06d0c32013-03-25 08:52:03 -0700136 elif self.base == 'of_desc_str_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700137 return self._gen_string_unpack_expr(reader_expr, 256)
Rich Lanea06d0c32013-03-25 08:52:03 -0700138 elif utils.class_is_list(self.base):
139 element_cls = utils.list_to_entry_type(self.base)[:-2]
140 if ((element_cls, self.version) in of_g.is_fixed_length):
141 klass_name = self.base[8:-2]
142 element_size, = of_g.base_length[(element_cls, self.version)],
Rich Lane57026dc2013-05-01 10:13:16 -0700143 return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700144 else:
Rich Lanea0186052013-05-01 14:18:39 -0700145 return "loxi.unimplemented('unpack list %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700146 else:
Rich Lanea0186052013-05-01 14:18:39 -0700147 return "loxi.unimplemented('unpack %s')" % self.base
Rich Lanea06d0c32013-03-25 08:52:03 -0700148
Rich Lane57026dc2013-05-01 10:13:16 -0700149 def _gen_string_unpack_expr(self, reader_expr, length):
150 return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700151
152 def _pack_fmt(self):
153 if self.base == "char":
154 return "B"
155 if self.base == "uint8_t":
156 return "B"
157 if self.base == "uint16_t":
158 return "H"
159 if self.base == "uint32_t":
160 return "L"
161 if self.base == "uint64_t":
162 return "Q"
163 if self.base == "of_port_no_t":
164 if self.version == of_g.VERSION_1_0:
165 return "H"
166 else:
167 return "L"
168 if self.base == "of_fm_cmd_t":
169 if self.version == of_g.VERSION_1_0:
170 return "H"
171 else:
172 return "B"
173 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
174 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
175 return "L"
176 else:
177 return "Q"
178 return None
179
180class TestOFType(unittest.TestCase):
181 def test_init(self):
182 from oftype import OFType
183 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
184 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
185
186 def test_pack(self):
187 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
188
189 def test_unpack(self):
190 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
191
192if __name__ == '__main__':
193 unittest.main()