blob: ffad6cface7bbee264a7389568aaa7aa06fc974c [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 Lanea06d0c32013-03-25 08:52:03 -070031:: include('_copyright.py')
32
33:: include('_autogen.py')
34
35import struct
36import loxi
37import const
38import common
39import action # for unpack_list
Rich Laneed4f9062013-05-02 17:05:03 -070040:: if version >= 2:
41import instruction # for unpack_list
42:: #endif
Rich Laned82c0a62013-05-02 15:40:35 -070043:: if version >= 4:
44import meter_band # for unpack_list
45:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070046import util
Rich Lane15cbe842013-04-26 16:04:11 -070047import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070048
49class Message(object):
50 version = const.OFP_VERSION
51 type = None # override in subclass
52 xid = None
53
54:: for ofclass in ofclasses:
Rich Lane002e70c2013-05-09 17:08:16 -070055:: from loxi_ir import *
56:: normal_members = [m for m in ofclass.members if type(m) == OFDataMember]
57:: type_members = [m for m in ofclass.members if type(m) == OFTypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070058class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070059:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070060 ${m.name} = ${m.value}
61:: #endfor
62
Rich Lane8ca3b772013-04-30 13:36:55 -070063 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070064 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070065:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070066 if ${m.name} != None:
67 self.${m.name} = ${m.name}
68 else:
69 self.${m.name} = ${m.oftype.gen_init_expr()}
70:: #endfor
71
72 def pack(self):
73 packed = []
Rich Lanea06d0c32013-03-25 08:52:03 -070074:: include('_pack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070075 return ''.join(packed)
76
77 @staticmethod
78 def unpack(buf):
79 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
80 obj = ${ofclass.pyname}()
Rich Lanea06d0c32013-03-25 08:52:03 -070081:: include('_unpack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070082 return obj
83
84 def __eq__(self, other):
85 if type(self) != type(other): return False
86 if self.version != other.version: return False
87 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070088:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070089 if self.${m.name} != other.${m.name}: return False
90:: #endfor
91 return True
92
93 def __ne__(self, other):
94 return not self.__eq__(other)
95
96 def __str__(self):
97 return self.show()
98
99 def show(self):
100 import loxi.pp
101 return loxi.pp.pp(self)
102
103 def pretty_print(self, q):
104:: include('_pretty_print.py', ofclass=ofclass)
105
106:: #endfor
107
108def parse_header(buf):
109 if len(buf) < 8:
110 raise loxi.ProtocolError("too short to be an OpenFlow message")
111 return struct.unpack_from("!BBHL", buf)
112
113def parse_message(buf):
114 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
115 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
116 raise loxi.ProtocolError("wrong OpenFlow version")
117 if len(buf) != msg_len:
118 raise loxi.ProtocolError("incorrect message size")
119 if msg_type in parsers:
120 return parsers[msg_type](buf)
121 else:
122 raise loxi.ProtocolError("unexpected message type")
123
124:: # TODO fix for OF 1.1+
125def parse_flow_mod(buf):
126 if len(buf) < 56 + 2:
127 raise loxi.ProtocolError("message too short")
128 cmd, = struct.unpack_from("!H", buf, 56)
129 if cmd in flow_mod_parsers:
130 return flow_mod_parsers[cmd](buf)
131 else:
132 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
133
Rich Lane3f075972013-03-15 22:56:29 -0700134:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700135def parse_stats_reply(buf):
136 if len(buf) < 8 + 2:
137 raise loxi.ProtocolError("message too short")
138 stats_type, = struct.unpack_from("!H", buf, 8)
139 if stats_type in stats_reply_parsers:
140 return stats_reply_parsers[stats_type](buf)
141 else:
142 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
143
144def parse_stats_request(buf):
145 if len(buf) < 8 + 2:
146 raise loxi.ProtocolError("message too short")
147 stats_type, = struct.unpack_from("!H", buf, 8)
148 if stats_type in stats_request_parsers:
149 return stats_request_parsers[stats_type](buf)
150 else:
151 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700152:: else:
153def parse_multipart_reply(buf):
154 if len(buf) < 8 + 2:
155 raise loxi.ProtocolError("message too short")
156 multipart_type, = struct.unpack_from("!H", buf, 8)
157 if multipart_type in multipart_reply_parsers:
158 return multipart_reply_parsers[multipart_type](buf)
159 else:
160 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700161
Rich Lane3f075972013-03-15 22:56:29 -0700162def parse_multipart_request(buf):
163 if len(buf) < 8 + 2:
164 raise loxi.ProtocolError("message too short")
165 multipart_type, = struct.unpack_from("!H", buf, 8)
166 if multipart_type in multipart_request_parsers:
167 return multipart_request_parsers[multipart_type](buf)
168 else:
169 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
170:: #endif
171
172:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700173def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700174:: else:
175def parse_experimenter(buf):
176:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700177 if len(buf) < 16:
178 raise loxi.ProtocolError("experimenter message too short")
179
180 experimenter, = struct.unpack_from("!L", buf, 8)
181 if experimenter == 0x005c16c7: # Big Switch Networks
182 subtype, = struct.unpack_from("!L", buf, 12)
183 elif experimenter == 0x00002320: # Nicira
184 subtype, = struct.unpack_from("!L", buf, 12)
185 else:
186 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
187
188 if subtype in experimenter_parsers[experimenter]:
189 return experimenter_parsers[experimenter][subtype](buf)
190 else:
191 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
192
193parsers = {
194:: sort_key = lambda x: x.type_members[1].value
195:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
196:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700197:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700198:: v = list(v)
199:: if len(v) == 1:
200 ${k} : ${v[0].pyname}.unpack,
201:: else:
202 ${k} : parse_${k[11:].lower()},
203:: #endif
204:: #endfor
205}
206
207flow_mod_parsers = {
208 const.OFPFC_ADD : flow_add.unpack,
209 const.OFPFC_MODIFY : flow_modify.unpack,
210 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
211 const.OFPFC_DELETE : flow_delete.unpack,
212 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
213}
214
Rich Lane3f075972013-03-15 22:56:29 -0700215:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700216stats_reply_parsers = {
217 const.OFPST_DESC : desc_stats_reply.unpack,
218 const.OFPST_FLOW : flow_stats_reply.unpack,
219 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
220 const.OFPST_TABLE : table_stats_reply.unpack,
221 const.OFPST_PORT : port_stats_reply.unpack,
222 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700223:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700224 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700225:: else:
226 const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack,
227:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700228:: if version >= of_g.VERSION_1_1:
229 const.OFPST_GROUP : group_stats_reply.unpack,
230 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
231:: #endif
232:: if version >= of_g.VERSION_1_2:
233 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
234:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700235}
236
237stats_request_parsers = {
238 const.OFPST_DESC : desc_stats_request.unpack,
239 const.OFPST_FLOW : flow_stats_request.unpack,
240 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
241 const.OFPST_TABLE : table_stats_request.unpack,
242 const.OFPST_PORT : port_stats_request.unpack,
243 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700244:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700245 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700246:: else:
247 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
248:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700249:: if version >= of_g.VERSION_1_1:
250 const.OFPST_GROUP : group_stats_request.unpack,
251 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
252:: #endif
253:: if version >= of_g.VERSION_1_2:
254 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
255:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700256}
Rich Lane3f075972013-03-15 22:56:29 -0700257:: else:
Rich Lane5edb5552013-05-07 18:49:29 -0700258# TODO OF 1.3 multipart messages
Rich Lane3f075972013-03-15 22:56:29 -0700259:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700260
Rich Lane002e70c2013-05-09 17:08:16 -0700261:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4]
Rich Lanea06d0c32013-03-25 08:52:03 -0700262:: sort_key = lambda x: x.type_members[2].value
263:: experimenter_ofclasses.sort(key=sort_key)
264:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
265experimenter_parsers = {
266:: for (experimenter, v) in grouped:
267 ${experimenter} : {
268:: for ofclass in v:
269 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
270:: #endfor
271 },
272:: #endfor
273}