blob: 2106ce77c94209adf3996a9a75b9069cd794e5c3 [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
51class Message(object):
52 version = const.OFP_VERSION
53 type = None # override in subclass
54 xid = None
55
56:: for ofclass in ofclasses:
Rich Lane0f7f9d52013-11-29 17:00:49 -080057:: include('_ofclass.py', ofclass=ofclass, superclass="Message")
Rich Lanea06d0c32013-03-25 08:52:03 -070058
59:: #endfor
60
61def parse_header(buf):
62 if len(buf) < 8:
63 raise loxi.ProtocolError("too short to be an OpenFlow message")
64 return struct.unpack_from("!BBHL", buf)
65
66def parse_message(buf):
67 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
Rich Lane1c5db112013-06-14 00:00:54 -070068 if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO:
69 raise loxi.ProtocolError("wrong OpenFlow version (expected %d, got %d)" % (const.OFP_VERSION, msg_ver))
Rich Lanea06d0c32013-03-25 08:52:03 -070070 if len(buf) != msg_len:
71 raise loxi.ProtocolError("incorrect message size")
72 if msg_type in parsers:
73 return parsers[msg_type](buf)
74 else:
75 raise loxi.ProtocolError("unexpected message type")
76
Rob Vaterlausfeee3712013-09-30 11:24:19 -070077def parse_error(buf):
78 if len(buf) < 8 + 2:
79 raise loxi.ProtocolError("message too short")
80 err_type, = struct.unpack_from("!H", buf, 8)
81 if err_type in error_msg_parsers:
82 return error_msg_parsers[err_type](buf)
83 else:
Rob Vaterlausb5e8c832013-10-04 12:50:23 -070084 raise loxi.ProtocolError("unexpected error type %u" % err_type)
Rob Vaterlausfeee3712013-09-30 11:24:19 -070085
Rich Lanea06d0c32013-03-25 08:52:03 -070086def parse_flow_mod(buf):
Andreas Wundsam5630c422013-11-15 13:43:11 -080087:: if version == OFVersions.VERSION_1_0:
Rich Laneefa54002013-06-14 07:26:27 -070088:: offset = 57
Andreas Wundsam5630c422013-11-15 13:43:11 -080089:: elif version >= OFVersions.VERSION_1_1:
Rich Laneefa54002013-06-14 07:26:27 -070090:: offset = 25
91:: #endif
92 if len(buf) < ${offset} + 1:
Rich Lanea06d0c32013-03-25 08:52:03 -070093 raise loxi.ProtocolError("message too short")
Rich Laneefa54002013-06-14 07:26:27 -070094 # Technically uint16_t for OF 1.0
95 cmd, = struct.unpack_from("!B", buf, ${offset})
Rich Lanea06d0c32013-03-25 08:52:03 -070096 if cmd in flow_mod_parsers:
97 return flow_mod_parsers[cmd](buf)
98 else:
99 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
100
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800101:: if version >= OFVersions.VERSION_1_0:
102def parse_group_mod(buf):
103:: offset = 8
104 if len(buf) < ${offset} + 2:
105 raise loxi.ProtocolError("message too short")
106 cmd, = struct.unpack_from("!H", buf, ${offset})
107 if cmd in flow_mod_parsers:
108 return group_mod_parsers[cmd](buf)
109 else:
110 raise loxi.ProtocolError("unexpected group mod cmd %u" % cmd)
111:: #endif
112
Rich Lanea06d0c32013-03-25 08:52:03 -0700113def parse_stats_reply(buf):
114 if len(buf) < 8 + 2:
115 raise loxi.ProtocolError("message too short")
116 stats_type, = struct.unpack_from("!H", buf, 8)
117 if stats_type in stats_reply_parsers:
118 return stats_reply_parsers[stats_type](buf)
119 else:
120 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
121
122def parse_stats_request(buf):
123 if len(buf) < 8 + 2:
124 raise loxi.ProtocolError("message too short")
125 stats_type, = struct.unpack_from("!H", buf, 8)
126 if stats_type in stats_request_parsers:
127 return stats_request_parsers[stats_type](buf)
128 else:
129 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
130
Rich Laned089bfa2013-11-13 10:40:40 -0800131def parse_experimenter_stats_request(buf):
132 if len(buf) < 24:
133 raise loxi.ProtocolError("experimenter stats request message too short")
134
135 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
136
137 if experimenter in experimenter_stats_request_parsers and \
138 exp_type in experimenter_stats_request_parsers[experimenter]:
139 return experimenter_stats_request_parsers[experimenter][exp_type](buf)
140 else:
141 raise loxi.ProtocolError("unexpected stats request experimenter %#x exp_type %#x" % (experimenter, exp_type))
142
143def parse_experimenter_stats_reply(buf):
144 if len(buf) < 24:
145 raise loxi.ProtocolError("experimenter stats reply message too short")
146
147 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
148
149 if experimenter in experimenter_stats_reply_parsers and \
150 exp_type in experimenter_stats_reply_parsers[experimenter]:
151 return experimenter_stats_reply_parsers[experimenter][exp_type](buf)
152 else:
153 raise loxi.ProtocolError("unexpected stats reply experimenter %#x exp_type %#x" % (experimenter, exp_type))
154
Rich Lane3f075972013-03-15 22:56:29 -0700155def parse_experimenter(buf):
Rich Lanea06d0c32013-03-25 08:52:03 -0700156 if len(buf) < 16:
157 raise loxi.ProtocolError("experimenter message too short")
158
159 experimenter, = struct.unpack_from("!L", buf, 8)
160 if experimenter == 0x005c16c7: # Big Switch Networks
161 subtype, = struct.unpack_from("!L", buf, 12)
162 elif experimenter == 0x00002320: # Nicira
163 subtype, = struct.unpack_from("!L", buf, 12)
164 else:
165 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
166
167 if subtype in experimenter_parsers[experimenter]:
168 return experimenter_parsers[experimenter][subtype](buf)
169 else:
170 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
171
172parsers = {
Rich Lanea2de2192013-11-29 17:58:32 -0800173:: sort_key = lambda x: x.member_by_name('type').value
Rich Lanea06d0c32013-03-25 08:52:03 -0700174:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
175:: 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 Lanea2de2192013-11-29 17:58:32 -0800274:: experimenter_ofclasses = [x for x in ofclasses if x.member_by_name('type').value == 4]
275:: 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}