blob: 3beb8e39ec08a9b825c551240ccc8e9128f4b113 [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:
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
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':
128 # XXX need the match_v3 length field
129 return 'oxm.unpack_list(%s)' % (reader_expr)
Rich Lanea06d0c32013-03-25 08:52:03 -0700130 elif self.base == 'of_port_name_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700131 return self._gen_string_unpack_expr(reader_expr, 16)
Rich Lanea06d0c32013-03-25 08:52:03 -0700132 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700133 return self._gen_string_unpack_expr(reader_expr, 32)
Rich Lanea06d0c32013-03-25 08:52:03 -0700134 elif self.base == 'of_desc_str_t':
Rich Lane57026dc2013-05-01 10:13:16 -0700135 return self._gen_string_unpack_expr(reader_expr, 256)
Rich Lanea06d0c32013-03-25 08:52:03 -0700136 elif utils.class_is_list(self.base):
137 element_cls = utils.list_to_entry_type(self.base)[:-2]
138 if ((element_cls, self.version) in of_g.is_fixed_length):
139 klass_name = self.base[8:-2]
140 element_size, = of_g.base_length[(element_cls, self.version)],
Rich Lane57026dc2013-05-01 10:13:16 -0700141 return 'loxi.generic_util.unpack_list(%s, common.%s.unpack)' % (reader_expr, klass_name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700142 else:
143 return "None # TODO unpack list %s" % self.base
144 else:
145 return "None # TODO unpack %s" % self.base
146
Rich Lane57026dc2013-05-01 10:13:16 -0700147 def _gen_string_unpack_expr(self, reader_expr, length):
148 return '%s.read("!%ds")[0].rstrip("\\x00")' % (reader_expr, length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700149
150 def _pack_fmt(self):
151 if self.base == "char":
152 return "B"
153 if self.base == "uint8_t":
154 return "B"
155 if self.base == "uint16_t":
156 return "H"
157 if self.base == "uint32_t":
158 return "L"
159 if self.base == "uint64_t":
160 return "Q"
161 if self.base == "of_port_no_t":
162 if self.version == of_g.VERSION_1_0:
163 return "H"
164 else:
165 return "L"
166 if self.base == "of_fm_cmd_t":
167 if self.version == of_g.VERSION_1_0:
168 return "H"
169 else:
170 return "B"
171 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
172 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
173 return "L"
174 else:
175 return "Q"
176 return None
177
178class TestOFType(unittest.TestCase):
179 def test_init(self):
180 from oftype import OFType
181 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
182 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
183
184 def test_pack(self):
185 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
186
187 def test_unpack(self):
188 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
189
190if __name__ == '__main__':
191 unittest.main()