blob: 96b68ccf62094c8785ae23ada7569dc5382a7c78 [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
150def parse_stats_reply(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_reply_parsers:
155 return stats_reply_parsers[stats_type](buf)
156 else:
157 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
158
159def parse_stats_request(buf):
160 if len(buf) < 8 + 2:
161 raise loxi.ProtocolError("message too short")
162 stats_type, = struct.unpack_from("!H", buf, 8)
163 if stats_type in stats_request_parsers:
164 return stats_request_parsers[stats_type](buf)
165 else:
166 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
167
Rich Lane3f075972013-03-15 22:56:29 -0700168def parse_experimenter(buf):
Rich Lanea06d0c32013-03-25 08:52:03 -0700169 if len(buf) < 16:
170 raise loxi.ProtocolError("experimenter message too short")
171
172 experimenter, = struct.unpack_from("!L", buf, 8)
173 if experimenter == 0x005c16c7: # Big Switch Networks
174 subtype, = struct.unpack_from("!L", buf, 12)
175 elif experimenter == 0x00002320: # Nicira
176 subtype, = struct.unpack_from("!L", buf, 12)
177 else:
178 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
179
180 if subtype in experimenter_parsers[experimenter]:
181 return experimenter_parsers[experimenter][subtype](buf)
182 else:
183 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
184
185parsers = {
186:: sort_key = lambda x: x.type_members[1].value
187:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
188:: for (k, v) in msgtype_groups:
Rich Lane002e70c2013-05-09 17:08:16 -0700189:: k = util.constant_for_value(version, "ofp_type", k)
Rich Lanea06d0c32013-03-25 08:52:03 -0700190:: v = list(v)
191:: if len(v) == 1:
192 ${k} : ${v[0].pyname}.unpack,
193:: else:
194 ${k} : parse_${k[11:].lower()},
195:: #endif
196:: #endfor
197}
198
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700199error_msg_parsers = {
200 const.OFPET_HELLO_FAILED : hello_failed_error_msg.unpack,
201 const.OFPET_BAD_REQUEST : bad_request_error_msg.unpack,
202 const.OFPET_BAD_ACTION : bad_action_error_msg.unpack,
203 const.OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg.unpack,
204 const.OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg.unpack,
205 const.OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg.unpack,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800206:: if version >= OFVersions.VERSION_1_1:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700207 const.OFPET_BAD_INSTRUCTION : bad_instruction_error_msg.unpack,
208 const.OFPET_BAD_MATCH : bad_match_error_msg.unpack,
209 const.OFPET_GROUP_MOD_FAILED : group_mod_failed_error_msg.unpack,
210 const.OFPET_TABLE_MOD_FAILED : table_mod_failed_error_msg.unpack,
211 const.OFPET_SWITCH_CONFIG_FAILED : switch_config_failed_error_msg.unpack,
212:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800213:: if version >= OFVersions.VERSION_1_2:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700214 const.OFPET_ROLE_REQUEST_FAILED : role_request_failed_error_msg.unpack,
215 const.OFPET_EXPERIMENTER : experimenter_error_msg.unpack,
216:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800217:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700218 const.OFPET_METER_MOD_FAILED : meter_mod_failed_error_msg.unpack,
219 const.OFPET_TABLE_FEATURES_FAILED : table_features_failed_error_msg.unpack,
220:: #endif
221}
222
Rich Lanea06d0c32013-03-25 08:52:03 -0700223flow_mod_parsers = {
224 const.OFPFC_ADD : flow_add.unpack,
225 const.OFPFC_MODIFY : flow_modify.unpack,
226 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
227 const.OFPFC_DELETE : flow_delete.unpack,
228 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
229}
230
231stats_reply_parsers = {
232 const.OFPST_DESC : desc_stats_reply.unpack,
233 const.OFPST_FLOW : flow_stats_reply.unpack,
234 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
235 const.OFPST_TABLE : table_stats_reply.unpack,
236 const.OFPST_PORT : port_stats_reply.unpack,
237 const.OFPST_QUEUE : queue_stats_reply.unpack,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800238:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700239 const.OFPST_GROUP : group_stats_reply.unpack,
240 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
241:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800242:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700243 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
244:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800245:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700246 const.OFPST_METER : meter_stats_reply.unpack,
247 const.OFPST_METER_CONFIG : meter_config_stats_reply.unpack,
248 const.OFPST_METER_FEATURES : meter_features_stats_reply.unpack,
249 const.OFPST_TABLE_FEATURES : table_features_stats_reply.unpack,
250 const.OFPST_PORT_DESC : port_desc_stats_reply.unpack,
251:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700252}
253
254stats_request_parsers = {
255 const.OFPST_DESC : desc_stats_request.unpack,
256 const.OFPST_FLOW : flow_stats_request.unpack,
257 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
258 const.OFPST_TABLE : table_stats_request.unpack,
259 const.OFPST_PORT : port_stats_request.unpack,
260 const.OFPST_QUEUE : queue_stats_request.unpack,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800261:: if version >= OFVersions.VERSION_1_1:
Rich Lane5edb5552013-05-07 18:49:29 -0700262 const.OFPST_GROUP : group_stats_request.unpack,
263 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
264:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800265:: if version >= OFVersions.VERSION_1_2:
Rich Lane5edb5552013-05-07 18:49:29 -0700266 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
267:: #endif
Andreas Wundsam5630c422013-11-15 13:43:11 -0800268:: if version >= OFVersions.VERSION_1_3:
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700269 const.OFPST_METER : meter_stats_request.unpack,
270 const.OFPST_METER_CONFIG : meter_config_stats_request.unpack,
271 const.OFPST_METER_FEATURES : meter_features_stats_request.unpack,
272 const.OFPST_TABLE_FEATURES : table_features_stats_request.unpack,
273 const.OFPST_PORT_DESC : port_desc_stats_request.unpack,
Rich Lane3f075972013-03-15 22:56:29 -0700274:: #endif
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700275}
Rich Lanea06d0c32013-03-25 08:52:03 -0700276
Rich Lane002e70c2013-05-09 17:08:16 -0700277:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4]
Rich Lanea06d0c32013-03-25 08:52:03 -0700278:: sort_key = lambda x: x.type_members[2].value
279:: experimenter_ofclasses.sort(key=sort_key)
280:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
281experimenter_parsers = {
282:: for (experimenter, v) in grouped:
283 ${experimenter} : {
284:: for ofclass in v:
285 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
286:: #endfor
287 },
288:: #endfor
289}