blob: 9307676d913e4ebfdc9a59e94aefbbba7e0b6cdf [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::
28:: import itertools
Andreas Wundsam5630c422013-11-15 13:43:11 -080029:: from loxi_globals import OFVersions
30:: import loxi_globals
Rich Lane002e70c2013-05-09 17:08:16 -070031:: import py_gen.util as util
Rich Laneda11f8b2013-06-19 15:53:25 -070032:: import py_gen.oftype
Rich Lanea06d0c32013-03-25 08:52:03 -070033:: include('_copyright.py')
34
35:: include('_autogen.py')
36
37import struct
38import loxi
39import const
40import common
41import action # for unpack_list
Andreas Wundsam5630c422013-11-15 13:43:11 -080042:: if version >= OFVersions.VERSION_1_1:
Rich Laneed4f9062013-05-02 17:05:03 -070043import instruction # for unpack_list
44:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -080045:: if version >= OFVersions.VERSION_1_3:
Rich Laned82c0a62013-05-02 15:40:35 -070046import meter_band # for unpack_list
47:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070048import util
Rich Lane15cbe842013-04-26 16:04:11 -070049import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070050
Rich Lanea06d0c32013-03-25 08:52:03 -070051:: for ofclass in ofclasses:
Rich Lane9d98adf2013-11-29 18:37:24 -080052:: if ofclass.virtual:
53:: include('_virtual_ofclass.py', ofclass=ofclass)
54:: else:
55:: include('_ofclass.py', ofclass=ofclass)
56:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070057
58:: #endfor
59
60def parse_header(buf):
61 if len(buf) < 8:
62 raise loxi.ProtocolError("too short to be an OpenFlow message")
63 return struct.unpack_from("!BBHL", buf)
64
65def parse_message(buf):
66 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
Rich Lane1c5db112013-06-14 00:00:54 -070067 if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO:
68 raise loxi.ProtocolError("wrong OpenFlow version (expected %d, got %d)" % (const.OFP_VERSION, msg_ver))
Rich Lanea06d0c32013-03-25 08:52:03 -070069 if len(buf) != msg_len:
70 raise loxi.ProtocolError("incorrect message size")
71 if msg_type in parsers:
72 return parsers[msg_type](buf)
73 else:
74 raise loxi.ProtocolError("unexpected message type")
75
Rob Vaterlausfeee3712013-09-30 11:24:19 -070076def parse_error(buf):
77 if len(buf) < 8 + 2:
78 raise loxi.ProtocolError("message too short")
79 err_type, = struct.unpack_from("!H", buf, 8)
80 if err_type in error_msg_parsers:
81 return error_msg_parsers[err_type](buf)
82 else:
Rob Vaterlausb5e8c832013-10-04 12:50:23 -070083 raise loxi.ProtocolError("unexpected error type %u" % err_type)
Rob Vaterlausfeee3712013-09-30 11:24:19 -070084
Rich Lanea06d0c32013-03-25 08:52:03 -070085def parse_flow_mod(buf):
Andreas Wundsam5630c422013-11-15 13:43:11 -080086:: if version == OFVersions.VERSION_1_0:
Rich Laneefa54002013-06-14 07:26:27 -070087:: offset = 57
Andreas Wundsam5630c422013-11-15 13:43:11 -080088:: elif version >= OFVersions.VERSION_1_1:
Rich Laneefa54002013-06-14 07:26:27 -070089:: offset = 25
90:: #endif
91 if len(buf) < ${offset} + 1:
Rich Lanea06d0c32013-03-25 08:52:03 -070092 raise loxi.ProtocolError("message too short")
Rich Laneefa54002013-06-14 07:26:27 -070093 # Technically uint16_t for OF 1.0
94 cmd, = struct.unpack_from("!B", buf, ${offset})
Rich Lanea06d0c32013-03-25 08:52:03 -070095 if cmd in flow_mod_parsers:
96 return flow_mod_parsers[cmd](buf)
97 else:
98 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
99
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800100:: if version >= OFVersions.VERSION_1_0:
101def parse_group_mod(buf):
102:: offset = 8
103 if len(buf) < ${offset} + 2:
104 raise loxi.ProtocolError("message too short")
105 cmd, = struct.unpack_from("!H", buf, ${offset})
106 if cmd in flow_mod_parsers:
107 return group_mod_parsers[cmd](buf)
108 else:
109 raise loxi.ProtocolError("unexpected group mod cmd %u" % cmd)
110:: #endif
111
Rich Lanea06d0c32013-03-25 08:52:03 -0700112def parse_stats_reply(buf):
113 if len(buf) < 8 + 2:
114 raise loxi.ProtocolError("message too short")
115 stats_type, = struct.unpack_from("!H", buf, 8)
116 if stats_type in stats_reply_parsers:
117 return stats_reply_parsers[stats_type](buf)
118 else:
119 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
120
121def parse_stats_request(buf):
122 if len(buf) < 8 + 2:
123 raise loxi.ProtocolError("message too short")
124 stats_type, = struct.unpack_from("!H", buf, 8)
125 if stats_type in stats_request_parsers:
126 return stats_request_parsers[stats_type](buf)
127 else:
128 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
129
Rich Laned089bfa2013-11-13 10:40:40 -0800130def parse_experimenter_stats_request(buf):
131 if len(buf) < 24:
132 raise loxi.ProtocolError("experimenter stats request message too short")
133
134 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
135
136 if experimenter in experimenter_stats_request_parsers and \
137 exp_type in experimenter_stats_request_parsers[experimenter]:
138 return experimenter_stats_request_parsers[experimenter][exp_type](buf)
139 else:
140 raise loxi.ProtocolError("unexpected stats request experimenter %#x exp_type %#x" % (experimenter, exp_type))
141
142def parse_experimenter_stats_reply(buf):
143 if len(buf) < 24:
144 raise loxi.ProtocolError("experimenter stats reply message too short")
145
146 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
147
148 if experimenter in experimenter_stats_reply_parsers and \
149 exp_type in experimenter_stats_reply_parsers[experimenter]:
150 return experimenter_stats_reply_parsers[experimenter][exp_type](buf)
151 else:
152 raise loxi.ProtocolError("unexpected stats reply experimenter %#x exp_type %#x" % (experimenter, exp_type))
153
Rich Lane3f075972013-03-15 22:56:29 -0700154def parse_experimenter(buf):
Rich Lanea06d0c32013-03-25 08:52:03 -0700155 if len(buf) < 16:
156 raise loxi.ProtocolError("experimenter message too short")
157
158 experimenter, = struct.unpack_from("!L", buf, 8)
159 if experimenter == 0x005c16c7: # Big Switch Networks
160 subtype, = struct.unpack_from("!L", buf, 12)
161 elif experimenter == 0x00002320: # Nicira
162 subtype, = struct.unpack_from("!L", buf, 12)
163 else:
164 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
165
166 if subtype in experimenter_parsers[experimenter]:
167 return experimenter_parsers[experimenter][subtype](buf)
168 else:
169 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
170
171parsers = {
Rich Lane9d98adf2013-11-29 18:37:24 -0800172:: concrete_ofclasses = [x for x in ofclasses if not x.virtual]
Rich Lanea2de2192013-11-29 17:58:32 -0800173:: sort_key = lambda x: x.member_by_name('type').value
Rich Lane9d98adf2013-11-29 18:37:24 -0800174:: msgtype_groups = itertools.groupby(sorted(concrete_ofclasses, key=sort_key), sort_key)
Rich Lanea06d0c32013-03-25 08:52:03 -0700175:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700176:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700177:: v = list(v)
178:: if len(v) == 1:
179 ${k} : ${v[0].pyname}.unpack,
180:: else:
181 ${k} : parse_${k[11:].lower()},
182:: #endif
183:: #endfor
184}
185
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700186error_msg_parsers = {
187 const.OFPET_HELLO_FAILED : hello_failed_error_msg.unpack,
188 const.OFPET_BAD_REQUEST : bad_request_error_msg.unpack,
189 const.OFPET_BAD_ACTION : bad_action_error_msg.unpack,
190 const.OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg.unpack,
191 const.OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg.unpack,
192 const.OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg.unpack,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800193:: if version >= OFVersions.VERSION_1_1:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700194 const.OFPET_BAD_INSTRUCTION : bad_instruction_error_msg.unpack,
195 const.OFPET_BAD_MATCH : bad_match_error_msg.unpack,
196 const.OFPET_GROUP_MOD_FAILED : group_mod_failed_error_msg.unpack,
197 const.OFPET_TABLE_MOD_FAILED : table_mod_failed_error_msg.unpack,
198 const.OFPET_SWITCH_CONFIG_FAILED : switch_config_failed_error_msg.unpack,
199:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800200:: if version >= OFVersions.VERSION_1_2:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700201 const.OFPET_ROLE_REQUEST_FAILED : role_request_failed_error_msg.unpack,
202 const.OFPET_EXPERIMENTER : experimenter_error_msg.unpack,
203:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800204:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700205 const.OFPET_METER_MOD_FAILED : meter_mod_failed_error_msg.unpack,
206 const.OFPET_TABLE_FEATURES_FAILED : table_features_failed_error_msg.unpack,
207:: #endif
208}
209
Rich Lanea06d0c32013-03-25 08:52:03 -0700210flow_mod_parsers = {
211 const.OFPFC_ADD : flow_add.unpack,
212 const.OFPFC_MODIFY : flow_modify.unpack,
213 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
214 const.OFPFC_DELETE : flow_delete.unpack,
215 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
216}
217
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800218:: if version >= OFVersions.VERSION_1_1:
219group_mod_parsers = {
220 const.OFPGC_ADD : group_add.unpack,
221 const.OFPGC_MODIFY : group_modify.unpack,
222 const.OFPGC_DELETE : group_delete.unpack,
223}
224:: #endif
225
Rich Lanea06d0c32013-03-25 08:52:03 -0700226stats_reply_parsers = {
227 const.OFPST_DESC : desc_stats_reply.unpack,
228 const.OFPST_FLOW : flow_stats_reply.unpack,
229 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
230 const.OFPST_TABLE : table_stats_reply.unpack,
231 const.OFPST_PORT : port_stats_reply.unpack,
232 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800233 const.OFPST_EXPERIMENTER : parse_experimenter_stats_reply,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800234:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700235 const.OFPST_GROUP : group_stats_reply.unpack,
236 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
237:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800238:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700239 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
240:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800241:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700242 const.OFPST_METER : meter_stats_reply.unpack,
243 const.OFPST_METER_CONFIG : meter_config_stats_reply.unpack,
244 const.OFPST_METER_FEATURES : meter_features_stats_reply.unpack,
245 const.OFPST_TABLE_FEATURES : table_features_stats_reply.unpack,
246 const.OFPST_PORT_DESC : port_desc_stats_reply.unpack,
247:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700248}
249
250stats_request_parsers = {
251 const.OFPST_DESC : desc_stats_request.unpack,
252 const.OFPST_FLOW : flow_stats_request.unpack,
253 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
254 const.OFPST_TABLE : table_stats_request.unpack,
255 const.OFPST_PORT : port_stats_request.unpack,
256 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800257 const.OFPST_EXPERIMENTER : parse_experimenter_stats_request,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800258:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700259 const.OFPST_GROUP : group_stats_request.unpack,
260 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
261:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800262:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700263 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
264:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800265:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700266 const.OFPST_METER : meter_stats_request.unpack,
267 const.OFPST_METER_CONFIG : meter_config_stats_request.unpack,
268 const.OFPST_METER_FEATURES : meter_features_stats_request.unpack,
269 const.OFPST_TABLE_FEATURES : table_features_stats_request.unpack,
270 const.OFPST_PORT_DESC : port_desc_stats_request.unpack,
Rich Lane3f075972013-03-15 22:56:29 -0700271:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700272}
Rich Lanea06d0c32013-03-25 08:52:03 -0700273
Rich Lane9d98adf2013-11-29 18:37:24 -0800274:: experimenter_ofclasses = [x for x in concrete_ofclasses if x.member_by_name('type').value == 4]
Rich Lanea2de2192013-11-29 17:58:32 -0800275:: sort_key = lambda x: x.member_by_name('experimenter').value
Rich Lanea06d0c32013-03-25 08:52:03 -0700276:: experimenter_ofclasses.sort(key=sort_key)
277:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
278experimenter_parsers = {
279:: for (experimenter, v) in grouped:
280 ${experimenter} : {
281:: for ofclass in v:
Rich Lanea2de2192013-11-29 17:58:32 -0800282 ${ofclass.member_by_name('subtype').value}: ${ofclass.pyname}.unpack,
Rich Lanea06d0c32013-03-25 08:52:03 -0700283:: #endfor
284 },
285:: #endfor
286}
Rich Laned089bfa2013-11-13 10:40:40 -0800287
288experimenter_stats_request_parsers = {
289 0x005c16c7: {
Andreas Wundsam2c0a2d72013-11-15 15:16:36 -0800290:: if version >= OFVersions.VERSION_1_3:
Rich Laned089bfa2013-11-13 10:40:40 -0800291 1: bsn_lacp_stats_request.unpack,
292:: #endif
293 },
294}
295
296experimenter_stats_reply_parsers = {
297 0x005c16c7: {
Andreas Wundsam2c0a2d72013-11-15 15:16:36 -0800298:: if version >= OFVersions.VERSION_1_3:
Rich Laned089bfa2013-11-13 10:40:40 -0800299 1: bsn_lacp_stats_reply.unpack,
300:: #endif
301 },
302}