blob: 6754b94158f1dc02853dacb1fc010a58c609eb1e [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:
Rich Laneda11f8b2013-06-19 15:53:25 -070070 self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype)}
Rich Lanea06d0c32013-03-25 08:52:03 -070071:: #endfor
72
73 def pack(self):
74 packed = []
Rich Lanea06d0c32013-03-25 08:52:03 -070075:: include('_pack.py', ofclass=ofclass)
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}()
Rich Lanea06d0c32013-03-25 08:52:03 -070082:: include('_unpack.py', ofclass=ofclass)
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
Rich Lanea06d0c32013-03-25 08:52:03 -0700125def parse_flow_mod(buf):
Rich Laneefa54002013-06-14 07:26:27 -0700126:: if version == 1:
127:: offset = 57
128:: elif version >= 2:
129:: offset = 25
130:: #endif
131 if len(buf) < ${offset} + 1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700132 raise loxi.ProtocolError("message too short")
Rich Laneefa54002013-06-14 07:26:27 -0700133 # Technically uint16_t for OF 1.0
134 cmd, = struct.unpack_from("!B", buf, ${offset})
Rich Lanea06d0c32013-03-25 08:52:03 -0700135 if cmd in flow_mod_parsers:
136 return flow_mod_parsers[cmd](buf)
137 else:
138 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
139
Rich Lane3f075972013-03-15 22:56:29 -0700140:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700141def parse_stats_reply(buf):
142 if len(buf) < 8 + 2:
143 raise loxi.ProtocolError("message too short")
144 stats_type, = struct.unpack_from("!H", buf, 8)
145 if stats_type in stats_reply_parsers:
146 return stats_reply_parsers[stats_type](buf)
147 else:
148 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
149
150def parse_stats_request(buf):
151 if len(buf) < 8 + 2:
152 raise loxi.ProtocolError("message too short")
153 stats_type, = struct.unpack_from("!H", buf, 8)
154 if stats_type in stats_request_parsers:
155 return stats_request_parsers[stats_type](buf)
156 else:
157 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700158:: else:
159def parse_multipart_reply(buf):
160 if len(buf) < 8 + 2:
161 raise loxi.ProtocolError("message too short")
162 multipart_type, = struct.unpack_from("!H", buf, 8)
163 if multipart_type in multipart_reply_parsers:
164 return multipart_reply_parsers[multipart_type](buf)
165 else:
166 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700167
Rich Lane3f075972013-03-15 22:56:29 -0700168def parse_multipart_request(buf):
169 if len(buf) < 8 + 2:
170 raise loxi.ProtocolError("message too short")
171 multipart_type, = struct.unpack_from("!H", buf, 8)
172 if multipart_type in multipart_request_parsers:
173 return multipart_request_parsers[multipart_type](buf)
174 else:
175 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
176:: #endif
177
178:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700179def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700180:: else:
181def parse_experimenter(buf):
182:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700183 if len(buf) < 16:
184 raise loxi.ProtocolError("experimenter message too short")
185
186 experimenter, = struct.unpack_from("!L", buf, 8)
187 if experimenter == 0x005c16c7: # Big Switch Networks
188 subtype, = struct.unpack_from("!L", buf, 12)
189 elif experimenter == 0x00002320: # Nicira
190 subtype, = struct.unpack_from("!L", buf, 12)
191 else:
192 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
193
194 if subtype in experimenter_parsers[experimenter]:
195 return experimenter_parsers[experimenter][subtype](buf)
196 else:
197 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
198
199parsers = {
200:: sort_key = lambda x: x.type_members[1].value
201:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
202:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700203:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700204:: v = list(v)
205:: if len(v) == 1:
206 ${k} : ${v[0].pyname}.unpack,
207:: else:
208 ${k} : parse_${k[11:].lower()},
209:: #endif
210:: #endfor
211}
212
213flow_mod_parsers = {
214 const.OFPFC_ADD : flow_add.unpack,
215 const.OFPFC_MODIFY : flow_modify.unpack,
216 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
217 const.OFPFC_DELETE : flow_delete.unpack,
218 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
219}
220
Rich Lane3f075972013-03-15 22:56:29 -0700221:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700222stats_reply_parsers = {
223 const.OFPST_DESC : desc_stats_reply.unpack,
224 const.OFPST_FLOW : flow_stats_reply.unpack,
225 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
226 const.OFPST_TABLE : table_stats_reply.unpack,
227 const.OFPST_PORT : port_stats_reply.unpack,
228 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700229:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700230 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700231:: else:
232 const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack,
233:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700234:: if version >= of_g.VERSION_1_1:
235 const.OFPST_GROUP : group_stats_reply.unpack,
236 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
237:: #endif
238:: if version >= of_g.VERSION_1_2:
239 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
240:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700241}
242
243stats_request_parsers = {
244 const.OFPST_DESC : desc_stats_request.unpack,
245 const.OFPST_FLOW : flow_stats_request.unpack,
246 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
247 const.OFPST_TABLE : table_stats_request.unpack,
248 const.OFPST_PORT : port_stats_request.unpack,
249 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700250:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700251 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700252:: else:
253 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
254:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700255:: if version >= of_g.VERSION_1_1:
256 const.OFPST_GROUP : group_stats_request.unpack,
257 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
258:: #endif
259:: if version >= of_g.VERSION_1_2:
260 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
261:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700262}
Rich Lane3f075972013-03-15 22:56:29 -0700263:: else:
Rich Lane7b9c9492013-06-14 07:43:22 -0700264multipart_reply_parsers = {
265 const.OFPMP_DESC : desc_stats_reply.unpack,
266 const.OFPMP_FLOW : flow_stats_reply.unpack,
267 const.OFPMP_AGGREGATE : aggregate_stats_reply.unpack,
268 const.OFPMP_TABLE : table_stats_reply.unpack,
269 const.OFPMP_PORT_STATS : port_stats_reply.unpack,
270 const.OFPMP_QUEUE : queue_stats_reply.unpack,
271 const.OFPMP_GROUP : group_stats_reply.unpack,
272 const.OFPMP_GROUP_DESC : group_desc_stats_reply.unpack,
273 const.OFPMP_GROUP_FEATURES : group_features_stats_reply.unpack,
274 const.OFPMP_METER : meter_stats_reply.unpack,
275 const.OFPMP_METER_CONFIG : meter_config_stats_reply.unpack,
276 const.OFPMP_METER_FEATURES : meter_features_stats_reply.unpack,
277 const.OFPMP_TABLE_FEATURES : table_features_stats_reply.unpack,
278 const.OFPMP_PORT_DESC : port_desc_stats_reply.unpack,
279}
280
281multipart_request_parsers = {
282 const.OFPMP_DESC : desc_stats_request.unpack,
283 const.OFPMP_FLOW : flow_stats_request.unpack,
284 const.OFPMP_AGGREGATE : aggregate_stats_request.unpack,
285 const.OFPMP_TABLE : table_stats_request.unpack,
286 const.OFPMP_PORT_STATS : port_stats_request.unpack,
287 const.OFPMP_QUEUE : queue_stats_request.unpack,
288 const.OFPMP_GROUP : group_stats_request.unpack,
289 const.OFPMP_GROUP_DESC : group_desc_stats_request.unpack,
290 const.OFPMP_GROUP_FEATURES : group_features_stats_request.unpack,
291 const.OFPMP_METER : meter_stats_request.unpack,
292 const.OFPMP_METER_CONFIG : meter_config_stats_request.unpack,
293 const.OFPMP_METER_FEATURES : meter_features_stats_request.unpack,
294 const.OFPMP_TABLE_FEATURES : table_features_stats_request.unpack,
295 const.OFPMP_PORT_DESC : port_desc_stats_request.unpack,
296}
Rich Lane3f075972013-03-15 22:56:29 -0700297:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700298
Rich Lane002e70c2013-05-09 17:08:16 -0700299:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4]
Rich Lanea06d0c32013-03-25 08:52:03 -0700300:: sort_key = lambda x: x.type_members[2].value
301:: experimenter_ofclasses.sort(key=sort_key)
302:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
303experimenter_parsers = {
304:: for (experimenter, v) in grouped:
305 ${experimenter} : {
306:: for ofclass in v:
307 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
308:: #endfor
309 },
310:: #endfor
311}