blob: 39e9722c5b05a0139baa6f24e4aaf2860ccac55b [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 Lane002e70c2013-05-09 17:08:16 -070057:: from loxi_ir import *
58:: normal_members = [m for m in ofclass.members if type(m) == OFDataMember]
59:: type_members = [m for m in ofclass.members if type(m) == OFTypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070060class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070061:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070062 ${m.name} = ${m.value}
63:: #endfor
64
Rich Lane8ca3b772013-04-30 13:36:55 -070065 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070066 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070067:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070068 if ${m.name} != None:
69 self.${m.name} = ${m.name}
70 else:
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070071 self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype, version=version)}
Rich Lanea06d0c32013-03-25 08:52:03 -070072:: #endfor
73
74 def pack(self):
75 packed = []
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070076:: include('_pack.py', ofclass=ofclass, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070077 return ''.join(packed)
78
79 @staticmethod
80 def unpack(buf):
81 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
82 obj = ${ofclass.pyname}()
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070083:: include('_unpack.py', ofclass=ofclass, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070084 return obj
85
86 def __eq__(self, other):
87 if type(self) != type(other): return False
88 if self.version != other.version: return False
89 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070090:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070091 if self.${m.name} != other.${m.name}: return False
92:: #endfor
93 return True
94
95 def __ne__(self, other):
96 return not self.__eq__(other)
97
98 def __str__(self):
99 return self.show()
100
101 def show(self):
102 import loxi.pp
103 return loxi.pp.pp(self)
104
105 def pretty_print(self, q):
106:: include('_pretty_print.py', ofclass=ofclass)
107
108:: #endfor
109
110def parse_header(buf):
111 if len(buf) < 8:
112 raise loxi.ProtocolError("too short to be an OpenFlow message")
113 return struct.unpack_from("!BBHL", buf)
114
115def parse_message(buf):
116 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
Rich Lane1c5db112013-06-14 00:00:54 -0700117 if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO:
118 raise loxi.ProtocolError("wrong OpenFlow version (expected %d, got %d)" % (const.OFP_VERSION, msg_ver))
Rich Lanea06d0c32013-03-25 08:52:03 -0700119 if len(buf) != msg_len:
120 raise loxi.ProtocolError("incorrect message size")
121 if msg_type in parsers:
122 return parsers[msg_type](buf)
123 else:
124 raise loxi.ProtocolError("unexpected message type")
125
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700126def parse_error(buf):
127 if len(buf) < 8 + 2:
128 raise loxi.ProtocolError("message too short")
129 err_type, = struct.unpack_from("!H", buf, 8)
130 if err_type in error_msg_parsers:
131 return error_msg_parsers[err_type](buf)
132 else:
Rob Vaterlausb5e8c832013-10-04 12:50:23 -0700133 raise loxi.ProtocolError("unexpected error type %u" % err_type)
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700134
Rich Lanea06d0c32013-03-25 08:52:03 -0700135def parse_flow_mod(buf):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800136:: if version == OFVersions.VERSION_1_0:
Rich Laneefa54002013-06-14 07:26:27 -0700137:: offset = 57
Andreas Wundsam5630c422013-11-15 13:43:11 -0800138:: elif version >= OFVersions.VERSION_1_1:
Rich Laneefa54002013-06-14 07:26:27 -0700139:: offset = 25
140:: #endif
141 if len(buf) < ${offset} + 1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700142 raise loxi.ProtocolError("message too short")
Rich Laneefa54002013-06-14 07:26:27 -0700143 # Technically uint16_t for OF 1.0
144 cmd, = struct.unpack_from("!B", buf, ${offset})
Rich Lanea06d0c32013-03-25 08:52:03 -0700145 if cmd in flow_mod_parsers:
146 return flow_mod_parsers[cmd](buf)
147 else:
148 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
149
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800150:: if version >= OFVersions.VERSION_1_0:
151def parse_group_mod(buf):
152:: offset = 8
153 if len(buf) < ${offset} + 2:
154 raise loxi.ProtocolError("message too short")
155 cmd, = struct.unpack_from("!H", buf, ${offset})
156 if cmd in flow_mod_parsers:
157 return group_mod_parsers[cmd](buf)
158 else:
159 raise loxi.ProtocolError("unexpected group mod cmd %u" % cmd)
160:: #endif
161
Rich Lanea06d0c32013-03-25 08:52:03 -0700162def parse_stats_reply(buf):
163 if len(buf) < 8 + 2:
164 raise loxi.ProtocolError("message too short")
165 stats_type, = struct.unpack_from("!H", buf, 8)
166 if stats_type in stats_reply_parsers:
167 return stats_reply_parsers[stats_type](buf)
168 else:
169 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
170
171def parse_stats_request(buf):
172 if len(buf) < 8 + 2:
173 raise loxi.ProtocolError("message too short")
174 stats_type, = struct.unpack_from("!H", buf, 8)
175 if stats_type in stats_request_parsers:
176 return stats_request_parsers[stats_type](buf)
177 else:
178 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
179
Rich Laned089bfa2013-11-13 10:40:40 -0800180def parse_experimenter_stats_request(buf):
181 if len(buf) < 24:
182 raise loxi.ProtocolError("experimenter stats request message too short")
183
184 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
185
186 if experimenter in experimenter_stats_request_parsers and \
187 exp_type in experimenter_stats_request_parsers[experimenter]:
188 return experimenter_stats_request_parsers[experimenter][exp_type](buf)
189 else:
190 raise loxi.ProtocolError("unexpected stats request experimenter %#x exp_type %#x" % (experimenter, exp_type))
191
192def parse_experimenter_stats_reply(buf):
193 if len(buf) < 24:
194 raise loxi.ProtocolError("experimenter stats reply message too short")
195
196 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
197
198 if experimenter in experimenter_stats_reply_parsers and \
199 exp_type in experimenter_stats_reply_parsers[experimenter]:
200 return experimenter_stats_reply_parsers[experimenter][exp_type](buf)
201 else:
202 raise loxi.ProtocolError("unexpected stats reply experimenter %#x exp_type %#x" % (experimenter, exp_type))
203
Rich Lane3f075972013-03-15 22:56:29 -0700204def parse_experimenter(buf):
Rich Lanea06d0c32013-03-25 08:52:03 -0700205 if len(buf) < 16:
206 raise loxi.ProtocolError("experimenter message too short")
207
208 experimenter, = struct.unpack_from("!L", buf, 8)
209 if experimenter == 0x005c16c7: # Big Switch Networks
210 subtype, = struct.unpack_from("!L", buf, 12)
211 elif experimenter == 0x00002320: # Nicira
212 subtype, = struct.unpack_from("!L", buf, 12)
213 else:
214 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
215
216 if subtype in experimenter_parsers[experimenter]:
217 return experimenter_parsers[experimenter][subtype](buf)
218 else:
219 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
220
221parsers = {
222:: sort_key = lambda x: x.type_members[1].value
223:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
224:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700225:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700226:: v = list(v)
227:: if len(v) == 1:
228 ${k} : ${v[0].pyname}.unpack,
229:: else:
230 ${k} : parse_${k[11:].lower()},
231:: #endif
232:: #endfor
233}
234
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700235error_msg_parsers = {
236 const.OFPET_HELLO_FAILED : hello_failed_error_msg.unpack,
237 const.OFPET_BAD_REQUEST : bad_request_error_msg.unpack,
238 const.OFPET_BAD_ACTION : bad_action_error_msg.unpack,
239 const.OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg.unpack,
240 const.OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg.unpack,
241 const.OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg.unpack,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800242:: if version >= OFVersions.VERSION_1_1:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700243 const.OFPET_BAD_INSTRUCTION : bad_instruction_error_msg.unpack,
244 const.OFPET_BAD_MATCH : bad_match_error_msg.unpack,
245 const.OFPET_GROUP_MOD_FAILED : group_mod_failed_error_msg.unpack,
246 const.OFPET_TABLE_MOD_FAILED : table_mod_failed_error_msg.unpack,
247 const.OFPET_SWITCH_CONFIG_FAILED : switch_config_failed_error_msg.unpack,
248:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800249:: if version >= OFVersions.VERSION_1_2:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700250 const.OFPET_ROLE_REQUEST_FAILED : role_request_failed_error_msg.unpack,
251 const.OFPET_EXPERIMENTER : experimenter_error_msg.unpack,
252:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800253:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700254 const.OFPET_METER_MOD_FAILED : meter_mod_failed_error_msg.unpack,
255 const.OFPET_TABLE_FEATURES_FAILED : table_features_failed_error_msg.unpack,
256:: #endif
257}
258
Rich Lanea06d0c32013-03-25 08:52:03 -0700259flow_mod_parsers = {
260 const.OFPFC_ADD : flow_add.unpack,
261 const.OFPFC_MODIFY : flow_modify.unpack,
262 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
263 const.OFPFC_DELETE : flow_delete.unpack,
264 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
265}
266
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800267:: if version >= OFVersions.VERSION_1_1:
268group_mod_parsers = {
269 const.OFPGC_ADD : group_add.unpack,
270 const.OFPGC_MODIFY : group_modify.unpack,
271 const.OFPGC_DELETE : group_delete.unpack,
272}
273:: #endif
274
Rich Lanea06d0c32013-03-25 08:52:03 -0700275stats_reply_parsers = {
276 const.OFPST_DESC : desc_stats_reply.unpack,
277 const.OFPST_FLOW : flow_stats_reply.unpack,
278 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
279 const.OFPST_TABLE : table_stats_reply.unpack,
280 const.OFPST_PORT : port_stats_reply.unpack,
281 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800282 const.OFPST_EXPERIMENTER : parse_experimenter_stats_reply,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800283:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700284 const.OFPST_GROUP : group_stats_reply.unpack,
285 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
286:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800287:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700288 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
289:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800290:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700291 const.OFPST_METER : meter_stats_reply.unpack,
292 const.OFPST_METER_CONFIG : meter_config_stats_reply.unpack,
293 const.OFPST_METER_FEATURES : meter_features_stats_reply.unpack,
294 const.OFPST_TABLE_FEATURES : table_features_stats_reply.unpack,
295 const.OFPST_PORT_DESC : port_desc_stats_reply.unpack,
296:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700297}
298
299stats_request_parsers = {
300 const.OFPST_DESC : desc_stats_request.unpack,
301 const.OFPST_FLOW : flow_stats_request.unpack,
302 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
303 const.OFPST_TABLE : table_stats_request.unpack,
304 const.OFPST_PORT : port_stats_request.unpack,
305 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800306 const.OFPST_EXPERIMENTER : parse_experimenter_stats_request,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800307:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700308 const.OFPST_GROUP : group_stats_request.unpack,
309 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
310:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800311:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700312 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
313:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800314:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700315 const.OFPST_METER : meter_stats_request.unpack,
316 const.OFPST_METER_CONFIG : meter_config_stats_request.unpack,
317 const.OFPST_METER_FEATURES : meter_features_stats_request.unpack,
318 const.OFPST_TABLE_FEATURES : table_features_stats_request.unpack,
319 const.OFPST_PORT_DESC : port_desc_stats_request.unpack,
Rich Lane3f075972013-03-15 22:56:29 -0700320:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700321}
Rich Lanea06d0c32013-03-25 08:52:03 -0700322
Rich Lane002e70c2013-05-09 17:08:16 -0700323:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4]
Rich Lanea06d0c32013-03-25 08:52:03 -0700324:: sort_key = lambda x: x.type_members[2].value
325:: experimenter_ofclasses.sort(key=sort_key)
326:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
327experimenter_parsers = {
328:: for (experimenter, v) in grouped:
329 ${experimenter} : {
330:: for ofclass in v:
331 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
332:: #endfor
333 },
334:: #endfor
335}
Rich Laned089bfa2013-11-13 10:40:40 -0800336
337experimenter_stats_request_parsers = {
338 0x005c16c7: {
Andreas Wundsam2c0a2d72013-11-15 15:16:36 -0800339:: if version >= OFVersions.VERSION_1_3:
Rich Laned089bfa2013-11-13 10:40:40 -0800340 1: bsn_lacp_stats_request.unpack,
341:: #endif
342 },
343}
344
345experimenter_stats_reply_parsers = {
346 0x005c16c7: {
Andreas Wundsam2c0a2d72013-11-15 15:16:36 -0800347:: if version >= OFVersions.VERSION_1_3:
Rich Laned089bfa2013-11-13 10:40:40 -0800348 1: bsn_lacp_stats_reply.unpack,
349:: #endif
350 },
351}