blob: e4c2c7b7bfabc052361914194e3634f57e4d438c [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
Rich Lane3f075972013-03-15 22:56:29 -070029:: import of_g
Rich Lane002e70c2013-05-09 17:08:16 -070030:: import py_gen.util as util
Rich Laneda11f8b2013-06-19 15:53:25 -070031:: import py_gen.oftype
Rich Lanea06d0c32013-03-25 08:52:03 -070032:: include('_copyright.py')
33
34:: include('_autogen.py')
35
36import struct
37import loxi
38import const
39import common
40import action # for unpack_list
Rich Laneed4f9062013-05-02 17:05:03 -070041:: if version >= 2:
42import instruction # for unpack_list
43:: #endif
Rich Laned82c0a62013-05-02 15:40:35 -070044:: if version >= 4:
45import meter_band # for unpack_list
46:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070047import util
Rich Lane15cbe842013-04-26 16:04:11 -070048import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070049
50class Message(object):
51 version = const.OFP_VERSION
52 type = None # override in subclass
53 xid = None
54
55:: for ofclass in ofclasses:
Rich Lane002e70c2013-05-09 17:08:16 -070056:: from loxi_ir import *
57:: normal_members = [m for m in ofclass.members if type(m) == OFDataMember]
58:: type_members = [m for m in ofclass.members if type(m) == OFTypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070059class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070060:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070061 ${m.name} = ${m.value}
62:: #endfor
63
Rich Lane8ca3b772013-04-30 13:36:55 -070064 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070065 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070066:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070067 if ${m.name} != None:
68 self.${m.name} = ${m.name}
69 else:
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070070 self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype, version=version)}
Rich Lanea06d0c32013-03-25 08:52:03 -070071:: #endfor
72
73 def pack(self):
74 packed = []
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070075:: include('_pack.py', ofclass=ofclass, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070076 return ''.join(packed)
77
78 @staticmethod
79 def unpack(buf):
80 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
81 obj = ${ofclass.pyname}()
Andreas Wundsam69ecfdc2013-09-17 13:50:12 -070082:: include('_unpack.py', ofclass=ofclass, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070083 return obj
84
85 def __eq__(self, other):
86 if type(self) != type(other): return False
87 if self.version != other.version: return False
88 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070089:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070090 if self.${m.name} != other.${m.name}: return False
91:: #endfor
92 return True
93
94 def __ne__(self, other):
95 return not self.__eq__(other)
96
97 def __str__(self):
98 return self.show()
99
100 def show(self):
101 import loxi.pp
102 return loxi.pp.pp(self)
103
104 def pretty_print(self, q):
105:: include('_pretty_print.py', ofclass=ofclass)
106
107:: #endfor
108
109def parse_header(buf):
110 if len(buf) < 8:
111 raise loxi.ProtocolError("too short to be an OpenFlow message")
112 return struct.unpack_from("!BBHL", buf)
113
114def parse_message(buf):
115 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
Rich Lane1c5db112013-06-14 00:00:54 -0700116 if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO:
117 raise loxi.ProtocolError("wrong OpenFlow version (expected %d, got %d)" % (const.OFP_VERSION, msg_ver))
Rich Lanea06d0c32013-03-25 08:52:03 -0700118 if len(buf) != msg_len:
119 raise loxi.ProtocolError("incorrect message size")
120 if msg_type in parsers:
121 return parsers[msg_type](buf)
122 else:
123 raise loxi.ProtocolError("unexpected message type")
124
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700125def parse_error(buf):
126 if len(buf) < 8 + 2:
127 raise loxi.ProtocolError("message too short")
128 err_type, = struct.unpack_from("!H", buf, 8)
129 if err_type in error_msg_parsers:
130 return error_msg_parsers[err_type](buf)
131 else:
Rob Vaterlausb5e8c832013-10-04 12:50:23 -0700132 raise loxi.ProtocolError("unexpected error type %u" % err_type)
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700133
Rich Lanea06d0c32013-03-25 08:52:03 -0700134def parse_flow_mod(buf):
Rich Laneefa54002013-06-14 07:26:27 -0700135:: if version == 1:
136:: offset = 57
137:: elif version >= 2:
138:: offset = 25
139:: #endif
140 if len(buf) < ${offset} + 1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700141 raise loxi.ProtocolError("message too short")
Rich Laneefa54002013-06-14 07:26:27 -0700142 # Technically uint16_t for OF 1.0
143 cmd, = struct.unpack_from("!B", buf, ${offset})
Rich Lanea06d0c32013-03-25 08:52:03 -0700144 if cmd in flow_mod_parsers:
145 return flow_mod_parsers[cmd](buf)
146 else:
147 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
148
149def parse_stats_reply(buf):
150 if len(buf) < 8 + 2:
151 raise loxi.ProtocolError("message too short")
152 stats_type, = struct.unpack_from("!H", buf, 8)
153 if stats_type in stats_reply_parsers:
154 return stats_reply_parsers[stats_type](buf)
155 else:
156 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
157
158def parse_stats_request(buf):
159 if len(buf) < 8 + 2:
160 raise loxi.ProtocolError("message too short")
161 stats_type, = struct.unpack_from("!H", buf, 8)
162 if stats_type in stats_request_parsers:
163 return stats_request_parsers[stats_type](buf)
164 else:
165 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
166
Rich Laned089bfa2013-11-13 10:40:40 -0800167def parse_experimenter_stats_request(buf):
168 if len(buf) < 24:
169 raise loxi.ProtocolError("experimenter stats request message too short")
170
171 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
172
173 if experimenter in experimenter_stats_request_parsers and \
174 exp_type in experimenter_stats_request_parsers[experimenter]:
175 return experimenter_stats_request_parsers[experimenter][exp_type](buf)
176 else:
177 raise loxi.ProtocolError("unexpected stats request experimenter %#x exp_type %#x" % (experimenter, exp_type))
178
179def parse_experimenter_stats_reply(buf):
180 if len(buf) < 24:
181 raise loxi.ProtocolError("experimenter stats reply message too short")
182
183 experimenter, exp_type = struct.unpack_from("!LL", buf, 16)
184
185 if experimenter in experimenter_stats_reply_parsers and \
186 exp_type in experimenter_stats_reply_parsers[experimenter]:
187 return experimenter_stats_reply_parsers[experimenter][exp_type](buf)
188 else:
189 raise loxi.ProtocolError("unexpected stats reply experimenter %#x exp_type %#x" % (experimenter, exp_type))
190
Rich Lane3f075972013-03-15 22:56:29 -0700191def parse_experimenter(buf):
Rich Lanea06d0c32013-03-25 08:52:03 -0700192 if len(buf) < 16:
193 raise loxi.ProtocolError("experimenter message too short")
194
195 experimenter, = struct.unpack_from("!L", buf, 8)
196 if experimenter == 0x005c16c7: # Big Switch Networks
197 subtype, = struct.unpack_from("!L", buf, 12)
198 elif experimenter == 0x00002320: # Nicira
199 subtype, = struct.unpack_from("!L", buf, 12)
200 else:
201 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
202
203 if subtype in experimenter_parsers[experimenter]:
204 return experimenter_parsers[experimenter][subtype](buf)
205 else:
206 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
207
208parsers = {
209:: sort_key = lambda x: x.type_members[1].value
210:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
211:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700212:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700213:: v = list(v)
214:: if len(v) == 1:
215 ${k} : ${v[0].pyname}.unpack,
216:: else:
217 ${k} : parse_${k[11:].lower()},
218:: #endif
219:: #endfor
220}
221
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700222error_msg_parsers = {
223 const.OFPET_HELLO_FAILED : hello_failed_error_msg.unpack,
224 const.OFPET_BAD_REQUEST : bad_request_error_msg.unpack,
225 const.OFPET_BAD_ACTION : bad_action_error_msg.unpack,
226 const.OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg.unpack,
227 const.OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg.unpack,
228 const.OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg.unpack,
229:: if version >= of_g.VERSION_1_1:
230 const.OFPET_BAD_INSTRUCTION : bad_instruction_error_msg.unpack,
231 const.OFPET_BAD_MATCH : bad_match_error_msg.unpack,
232 const.OFPET_GROUP_MOD_FAILED : group_mod_failed_error_msg.unpack,
233 const.OFPET_TABLE_MOD_FAILED : table_mod_failed_error_msg.unpack,
234 const.OFPET_SWITCH_CONFIG_FAILED : switch_config_failed_error_msg.unpack,
235:: #endif
236:: if version >= of_g.VERSION_1_2:
237 const.OFPET_ROLE_REQUEST_FAILED : role_request_failed_error_msg.unpack,
238 const.OFPET_EXPERIMENTER : experimenter_error_msg.unpack,
239:: #endif
240:: if version >= of_g.VERSION_1_3:
241 const.OFPET_METER_MOD_FAILED : meter_mod_failed_error_msg.unpack,
242 const.OFPET_TABLE_FEATURES_FAILED : table_features_failed_error_msg.unpack,
243:: #endif
244}
245
Rich Lanea06d0c32013-03-25 08:52:03 -0700246flow_mod_parsers = {
247 const.OFPFC_ADD : flow_add.unpack,
248 const.OFPFC_MODIFY : flow_modify.unpack,
249 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
250 const.OFPFC_DELETE : flow_delete.unpack,
251 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
252}
253
254stats_reply_parsers = {
255 const.OFPST_DESC : desc_stats_reply.unpack,
256 const.OFPST_FLOW : flow_stats_reply.unpack,
257 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
258 const.OFPST_TABLE : table_stats_reply.unpack,
259 const.OFPST_PORT : port_stats_reply.unpack,
260 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800261 const.OFPST_EXPERIMENTER : parse_experimenter_stats_reply,
Rich Lane5edb5552013-05-07 18:49:29 -0700262:: if version >= of_g.VERSION_1_1:
263 const.OFPST_GROUP : group_stats_reply.unpack,
264 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
265:: #endif
266:: if version >= of_g.VERSION_1_2:
267 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
268:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700269:: if version >= of_g.VERSION_1_3:
270 const.OFPST_METER : meter_stats_reply.unpack,
271 const.OFPST_METER_CONFIG : meter_config_stats_reply.unpack,
272 const.OFPST_METER_FEATURES : meter_features_stats_reply.unpack,
273 const.OFPST_TABLE_FEATURES : table_features_stats_reply.unpack,
274 const.OFPST_PORT_DESC : port_desc_stats_reply.unpack,
275:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700276}
277
278stats_request_parsers = {
279 const.OFPST_DESC : desc_stats_request.unpack,
280 const.OFPST_FLOW : flow_stats_request.unpack,
281 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
282 const.OFPST_TABLE : table_stats_request.unpack,
283 const.OFPST_PORT : port_stats_request.unpack,
284 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Laned089bfa2013-11-13 10:40:40 -0800285 const.OFPST_EXPERIMENTER : parse_experimenter_stats_request,
Rich Lane5edb5552013-05-07 18:49:29 -0700286:: if version >= of_g.VERSION_1_1:
287 const.OFPST_GROUP : group_stats_request.unpack,
288 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
289:: #endif
290:: if version >= of_g.VERSION_1_2:
291 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
292:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700293:: if version >= of_g.VERSION_1_3:
294 const.OFPST_METER : meter_stats_request.unpack,
295 const.OFPST_METER_CONFIG : meter_config_stats_request.unpack,
296 const.OFPST_METER_FEATURES : meter_features_stats_request.unpack,
297 const.OFPST_TABLE_FEATURES : table_features_stats_request.unpack,
298 const.OFPST_PORT_DESC : port_desc_stats_request.unpack,
Rich Lane3f075972013-03-15 22:56:29 -0700299:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700300}
Rich Lanea06d0c32013-03-25 08:52:03 -0700301
Rich Lane002e70c2013-05-09 17:08:16 -0700302:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4]
Rich Lanea06d0c32013-03-25 08:52:03 -0700303:: sort_key = lambda x: x.type_members[2].value
304:: experimenter_ofclasses.sort(key=sort_key)
305:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
306experimenter_parsers = {
307:: for (experimenter, v) in grouped:
308 ${experimenter} : {
309:: for ofclass in v:
310 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
311:: #endfor
312 },
313:: #endfor
314}
Rich Laned089bfa2013-11-13 10:40:40 -0800315
316experimenter_stats_request_parsers = {
317 0x005c16c7: {
318:: if version >= of_g.VERSION_1_3:
319 1: bsn_lacp_stats_request.unpack,
320:: #endif
321 },
322}
323
324experimenter_stats_reply_parsers = {
325 0x005c16c7: {
326:: if version >= of_g.VERSION_1_3:
327 1: bsn_lacp_stats_reply.unpack,
328:: #endif
329 },
330}