blob: ee5040cf46e23def019ce867f9ba7cbe8296eb38 [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]'
54 elif self.base == 'of_wc_bmap_t':
55 v = 'const.OFPFW_ALL'
56 elif self.base in ['of_octets_t', 'of_port_name_t', 'of_table_name_t',
57 'of_desc_str_t', 'of_serial_num_t']:
58 v = '""'
59 elif self.base == 'of_match_t':
60 v = 'common.match()'
61 elif self.base == 'of_port_desc_t':
62 v = 'common.port_desc()'
63 else:
64 v = "None"
65
66 if self.is_array:
67 return "[" + ','.join([v] * self.array_length) + "]"
68 else:
69 return v
70
71 def gen_pack_expr(self, expr_expr):
72 pack_fmt = self._pack_fmt()
73 if pack_fmt and not self.is_array:
74 return 'struct.pack("!%s", %s)' % (pack_fmt, expr_expr)
75 elif pack_fmt and self.is_array:
76 return 'struct.pack("!%s%s", *%s)' % (self.array_length, pack_fmt, expr_expr)
77 elif self.base == 'of_octets_t':
78 return expr_expr
79 elif utils.class_is_list(self.base):
80 return '"".join([x.pack() for x in %s])' % expr_expr
81 elif self.base == 'of_mac_addr_t':
82 return 'struct.pack("!6B", *%s)' % expr_expr
83 elif self.base in ['of_match_t', 'of_port_desc_t']:
84 return '%s.pack()' % expr_expr
85 elif self.base == 'of_port_name_t':
86 return self._gen_string_pack_expr(16, expr_expr)
87 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
88 return self._gen_string_pack_expr(32, expr_expr)
89 elif self.base == 'of_desc_str_t':
90 return self._gen_string_pack_expr(256, expr_expr)
91 else:
92 return "'TODO pack %s'" % self.base
93
94 def _gen_string_pack_expr(self, length, expr_expr):
95 return 'struct.pack("!%ds", %s)' % (length, expr_expr)
96
97 def gen_unpack_expr(self, buf_expr, offset_expr):
98 pack_fmt = self._pack_fmt()
99 if pack_fmt and not self.is_array:
100 return "struct.unpack_from('!%s', %s, %s)[0]" % (pack_fmt, buf_expr, offset_expr)
101 elif pack_fmt and self.is_array:
102 return "list(struct.unpack_from('!%d%s', %s, %s))" % (self.array_length, pack_fmt, buf_expr, offset_expr)
103 elif self.base == 'of_octets_t':
104 return "%s[%s:]" % (buf_expr, offset_expr)
105 elif self.base == 'of_mac_addr_t':
106 return "list(struct.unpack_from('!6B', %s, %s))" % (buf_expr, offset_expr)
107 elif self.base == 'of_match_t':
108 return 'common.match.unpack(buffer(%s, %s))' % (buf_expr, offset_expr)
109 elif self.base == 'of_port_desc_t':
110 return 'common.port_desc.unpack(buffer(%s, %s))' % (buf_expr, offset_expr)
111 elif self.base == 'of_list_action_t':
112 return 'action.unpack_list(buffer(%s, %s))' % (buf_expr, offset_expr)
113 elif self.base == 'of_list_flow_stats_entry_t':
114 return 'common.unpack_list_flow_stats_entry(buffer(%s, %s))' % (buf_expr, offset_expr)
115 elif self.base == 'of_list_queue_prop_t':
116 return 'common.unpack_list_queue_prop(buffer(%s, %s))' % (buf_expr, offset_expr)
117 elif self.base == 'of_list_packet_queue_t':
118 return 'common.unpack_list_packet_queue(buffer(%s, %s))' % (buf_expr, offset_expr)
119 elif self.base == 'of_port_name_t':
120 return self._gen_string_unpack_expr(16, buf_expr, offset_expr)
121 elif self.base == 'of_table_name_t' or self.base == 'of_serial_num_t':
122 return self._gen_string_unpack_expr(32, buf_expr, offset_expr)
123 elif self.base == 'of_desc_str_t':
124 return self._gen_string_unpack_expr(256, buf_expr, offset_expr)
125 elif utils.class_is_list(self.base):
126 element_cls = utils.list_to_entry_type(self.base)[:-2]
127 if ((element_cls, self.version) in of_g.is_fixed_length):
128 klass_name = self.base[8:-2]
129 element_size, = of_g.base_length[(element_cls, self.version)],
130 return 'util.unpack_array(common.%s.unpack, %d, buffer(%s, %s))' % (klass_name, element_size, buf_expr, offset_expr)
131 else:
132 return "None # TODO unpack list %s" % self.base
133 else:
134 return "None # TODO unpack %s" % self.base
135
136 def _gen_string_unpack_expr(self, length, buf_expr, offset_expr):
137 return 'str(buffer(%s, %s, %d)).rstrip("\\x00")' % (buf_expr, offset_expr, length)
138
139 def _pack_fmt(self):
140 if self.base == "char":
141 return "B"
142 if self.base == "uint8_t":
143 return "B"
144 if self.base == "uint16_t":
145 return "H"
146 if self.base == "uint32_t":
147 return "L"
148 if self.base == "uint64_t":
149 return "Q"
150 if self.base == "of_port_no_t":
151 if self.version == of_g.VERSION_1_0:
152 return "H"
153 else:
154 return "L"
155 if self.base == "of_fm_cmd_t":
156 if self.version == of_g.VERSION_1_0:
157 return "H"
158 else:
159 return "B"
160 if self.base in ["of_wc_bmap_t", "of_match_bmap_t"]:
161 if self.version in [of_g.VERSION_1_0, of_g.VERSION_1_1]:
162 return "L"
163 else:
164 return "Q"
165 return None
166
167class TestOFType(unittest.TestCase):
168 def test_init(self):
169 from oftype import OFType
170 self.assertEquals("None", OFType("of_list_action_t", 1).gen_init_expr())
171 self.assertEquals("[0,0,0]", OFType("uint32_t[3]", 1).gen_init_expr())
172
173 def test_pack(self):
174 self.assertEquals('struct.pack("!16s", "foo")', OFType("of_port_name_t", 1).gen_pack_expr('"foo"'))
175
176 def test_unpack(self):
177 self.assertEquals('str(buffer(buf, 8, 16)).rstrip("\\x00")', OFType("of_port_name_t", 1).gen_unpack_expr('buf', 8))
178
179if __name__ == '__main__':
180 unittest.main()