blob: 605d5beccc5d09b30317e0a147eb1082554f90f3 [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
Rich Lane8a22cda2013-06-19 13:20:07 -070028from collections import namedtuple
29
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070030import loxi_utils.loxi_utils as loxi_utils
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070031
Rich Lane8a22cda2013-06-19 13:20:07 -070032OFTypeData = namedtuple("OFTypeData", ["init", "pack", "unpack"])
33
Rich Lanec854bae2013-06-19 14:39:51 -070034# Map from LOXI type name to an object with templates for init, pack, and unpack
35# Most types are defined using the convenience code below. This dict should
36# only be used directly for special cases such as primitive types.
Rich Laneda11f8b2013-06-19 15:53:25 -070037type_data_map = {
Rich Lane8a22cda2013-06-19 13:20:07 -070038 'char': OFTypeData(
39 init='0',
40 pack='struct.pack("!B", %s)',
41 unpack='%s.read("!B")[0]'),
42
43 'uint8_t': OFTypeData(
44 init='0',
45 pack='struct.pack("!B", %s)',
46 unpack='%s.read("!B")[0]'),
47
48 'uint16_t': OFTypeData(
49 init='0',
50 pack='struct.pack("!H", %s)',
51 unpack='%s.read("!H")[0]'),
52
53 'uint32_t': OFTypeData(
54 init='0',
55 pack='struct.pack("!L", %s)',
56 unpack='%s.read("!L")[0]'),
57
58 'uint64_t': OFTypeData(
59 init='0',
60 pack='struct.pack("!Q", %s)',
61 unpack='%s.read("!Q")[0]'),
62
63 'of_port_no_t': OFTypeData(
64 init='0',
65 pack='util.pack_port_no(%s)',
66 unpack='util.unpack_port_no(%s)'),
67
68 'of_fm_cmd_t': OFTypeData(
69 init='0',
70 pack='util.pack_fm_cmd(%s)',
71 unpack='util.unpack_fm_cmd(%s)'),
72
73 'of_wc_bmap_t': OFTypeData(
74 init='util.init_wc_bmap()',
75 pack='util.pack_wc_bmap(%s)',
76 unpack='util.unpack_wc_bmap(%s)'),
77
78 'of_match_bmap_t': OFTypeData(
79 init='util.init_match_bmap()',
80 pack='util.pack_match_bmap(%s)',
81 unpack='util.unpack_match_bmap(%s)'),
82
Andreas Wundsamb566a162013-07-18 19:30:23 -070083 'of_ipv4_t': OFTypeData(
84 init='0',
85 pack='struct.pack("!L", %s)',
86 unpack='%s.read("!L")[0]'),
87
Rich Lane8a22cda2013-06-19 13:20:07 -070088 'of_ipv6_t': OFTypeData(
89 init="'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'",
90 pack='struct.pack("!16s", %s)',
91 unpack="%s.read('!16s')[0]"),
92
93 'of_mac_addr_t': OFTypeData(
94 init='[0,0,0,0,0,0]',
95 pack='struct.pack("!6B", *%s)',
96 unpack="list(%s.read('!6B'))"),
97
98 'of_octets_t': OFTypeData(
99 init="''",
100 pack='%s',
101 unpack='str(%s.read_all())'),
102
Rich Lane3b2fd832013-09-24 13:44:08 -0700103 'of_bitmap_128_t': OFTypeData(
104 init='set()',
105 pack='util.pack_bitmap_128(%s)',
106 unpack="util.unpack_bitmap_128(%s)"),
107
Rich Lane8a22cda2013-06-19 13:20:07 -0700108 # HACK need the match_v3 length field
109 'list(of_oxm_t)': OFTypeData(
110 init='[]',
111 pack='util.pack_list(%s)',
112 unpack='oxm.unpack_list(%s.slice(_length-4))'),
113
Rich Lanebe90eae2013-07-22 16:44:26 -0700114 'of_oxm_t': OFTypeData(
115 init='None',
116 pack='%s.pack()',
117 unpack='oxm.unpack(%s)'),
118
Rich Lanec854bae2013-06-19 14:39:51 -0700119 # TODO implement unpack
120 'list(of_table_features_t)': OFTypeData(
Rich Lane8a22cda2013-06-19 13:20:07 -0700121 init='[]',
122 pack='util.pack_list(%s)',
Rich Lanec854bae2013-06-19 14:39:51 -0700123 unpack=None),
Rich Lane8a22cda2013-06-19 13:20:07 -0700124
Rich Lanec854bae2013-06-19 14:39:51 -0700125 # TODO implement unpack
Rich Lane8a22cda2013-06-19 13:20:07 -0700126 'list(of_action_id_t)': OFTypeData(
127 init='[]',
128 pack='util.pack_list(%s)',
129 unpack=None),
130
Rich Lanec854bae2013-06-19 14:39:51 -0700131 # TODO implement unpack
Rich Lane8a22cda2013-06-19 13:20:07 -0700132 'list(of_table_feature_prop_t)': OFTypeData(
133 init='[]',
134 pack='util.pack_list(%s)',
135 unpack=None),
Rich Lane8a22cda2013-06-19 13:20:07 -0700136}
Rich Lanea06d0c32013-03-25 08:52:03 -0700137
Rich Lanec854bae2013-06-19 14:39:51 -0700138## Fixed length strings
139
140# Map from class name to length
141fixed_length_strings = {
142 'of_port_name_t': 16,
143 'of_table_name_t': 32,
144 'of_serial_num_t': 32,
145 'of_desc_str_t': 256,
146}
147
148for (cls, length) in fixed_length_strings.items():
Rich Laneda11f8b2013-06-19 15:53:25 -0700149 type_data_map[cls] = OFTypeData(
Rich Lanec854bae2013-06-19 14:39:51 -0700150 init='""',
151 pack='struct.pack("!%ds", %%s)' % length,
152 unpack='%%s.read("!%ds")[0].rstrip("\\x00")' % length)
153
154## Embedded structs
155
156# Map from class name to Python class name
157embedded_structs = {
158 'of_match_t': 'common.match',
159 'of_port_desc_t': 'common.port_desc',
160 'of_meter_features_t': 'common.meter_features',
161 'of_bsn_vport_q_in_q_t': 'common.bsn_vport_q_in_q',
162}
163
164for (cls, pyclass) in embedded_structs.items():
Rich Laneda11f8b2013-06-19 15:53:25 -0700165 type_data_map[cls] = OFTypeData(
Rich Lanec854bae2013-06-19 14:39:51 -0700166 init='%s()' % pyclass,
167 pack='%s.pack()',
168 unpack='%s.unpack(%%s)' % pyclass)
169
170## Variable element length lists
171
172# Map from list class name to list deserializer
173variable_elem_len_lists = {
174 'list(of_action_t)': 'action.unpack_list',
175 'list(of_bucket_t)': 'common.unpack_list_bucket',
176 'list(of_flow_stats_entry_t)': 'common.unpack_list_flow_stats_entry',
177 'list(of_group_desc_stats_entry_t)': 'common.unpack_list_group_desc_stats_entry',
178 'list(of_group_stats_entry_t)': 'common.unpack_list_group_stats_entry',
179 'list(of_hello_elem_t)': 'common.unpack_list_hello_elem',
180 'list(of_instruction_t)': 'instruction.unpack_list',
181 'list(of_meter_band_t)': 'meter_band.unpack_list',
182 'list(of_meter_stats_t)': 'common.unpack_list_meter_stats',
183 'list(of_packet_queue_t)': 'common.unpack_list_packet_queue',
184 'list(of_queue_prop_t)': 'common.unpack_list_queue_prop',
185}
186
187for (cls, deserializer) in variable_elem_len_lists.items():
Rich Laneda11f8b2013-06-19 15:53:25 -0700188 type_data_map[cls] = OFTypeData(
Rich Lanec854bae2013-06-19 14:39:51 -0700189 init='[]',
190 pack='util.pack_list(%s)',
191 unpack='%s(%%s)' % deserializer)
192
193## Fixed element length lists
194
195# Map from list class name to list element deserializer
196fixed_elem_len_lists = {
197 'list(of_bsn_interface_t)': 'common.bsn_interface.unpack',
198 'list(of_bucket_counter_t)': 'common.bucket_counter.unpack',
199 'list(of_meter_band_stats_t)': 'common.meter_band_stats.unpack',
200 'list(of_port_desc_t)': 'common.port_desc.unpack',
201 'list(of_port_stats_entry_t)': 'common.port_stats_entry.unpack',
202 'list(of_queue_stats_entry_t)': 'common.queue_stats_entry.unpack',
203 'list(of_table_stats_entry_t)': 'common.table_stats_entry.unpack',
204 'list(of_uint32_t)': 'common.uint32.unpack',
205 'list(of_uint8_t)': 'common.uint8.unpack',
Rich Laned089bfa2013-11-13 10:40:40 -0800206 'list(of_bsn_lacp_stats_entry_t)': 'common.bsn_lacp_stats_entry.unpack',
Rich Lanec854bae2013-06-19 14:39:51 -0700207}
208
209for (cls, element_deserializer) in fixed_elem_len_lists.items():
Rich Laneda11f8b2013-06-19 15:53:25 -0700210 type_data_map[cls] = OFTypeData(
Rich Lanec854bae2013-06-19 14:39:51 -0700211 init='[]',
212 pack='util.pack_list(%s)',
213 unpack='loxi.generic_util.unpack_list(%%s, %s)' % element_deserializer)
214
Rich Laneda11f8b2013-06-19 15:53:25 -0700215## Public interface
Rich Lanec854bae2013-06-19 14:39:51 -0700216
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -0700217def lookup_type_data(oftype, version):
218 return type_data_map.get(loxi_utils.lookup_ir_wiretype(oftype, version))
219
Rich Laneda11f8b2013-06-19 15:53:25 -0700220# Return an initializer expression for the given oftype
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -0700221def gen_init_expr(oftype, version):
222 type_data = lookup_type_data(oftype, version)
Rich Laneda11f8b2013-06-19 15:53:25 -0700223 if type_data and type_data.init:
224 return type_data.init
225 else:
226 return "loxi.unimplemented('init %s')" % oftype
Rich Lanea06d0c32013-03-25 08:52:03 -0700227
Rich Laneda11f8b2013-06-19 15:53:25 -0700228# Return a pack expression for the given oftype
229#
230# 'value_expr' is a string of Python code which will evaluate to
231# the value to be packed.
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -0700232def gen_pack_expr(oftype, value_expr, version):
233 type_data = lookup_type_data(oftype, version)
Rich Laneda11f8b2013-06-19 15:53:25 -0700234 if type_data and type_data.pack:
235 return type_data.pack % value_expr
236 else:
237 return "loxi.unimplemented('pack %s')" % oftype
Rich Lanea06d0c32013-03-25 08:52:03 -0700238
Rich Laneda11f8b2013-06-19 15:53:25 -0700239# Return an unpack expression for the given oftype
240#
241# 'reader_expr' is a string of Python code which will evaluate to
242# the OFReader instance used for deserialization.
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -0700243def gen_unpack_expr(oftype, reader_expr, version):
244 type_data = lookup_type_data(oftype, version)
Rich Laneda11f8b2013-06-19 15:53:25 -0700245 if type_data and type_data.unpack:
246 return type_data.unpack % reader_expr
247 else:
248 return "loxi.unimplemented('unpack %s')" % oftype