blob: 8e2f8611309b088b0352caed9a803a2c531ff55f [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 Lanea06d0c32013-03-25 08:52:03 -070030:: include('_copyright.py')
31
32:: include('_autogen.py')
33
34import struct
35import loxi
36import const
37import common
38import action # for unpack_list
Rich Laneed4f9062013-05-02 17:05:03 -070039:: if version >= 2:
40import instruction # for unpack_list
41:: #endif
Rich Laned82c0a62013-05-02 15:40:35 -070042:: if version >= 4:
43import meter_band # for unpack_list
44:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070045import util
Rich Lane15cbe842013-04-26 16:04:11 -070046import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070047
48class Message(object):
49 version = const.OFP_VERSION
50 type = None # override in subclass
51 xid = None
52
53:: for ofclass in ofclasses:
Rich Lane8ca3b772013-04-30 13:36:55 -070054:: from py_gen.codegen import Member, LengthMember, TypeMember
Rich Lane5de3bb22013-05-01 13:38:39 -070055:: normal_members = [m for m in ofclass.members if type(m) == Member]
Rich Lane8ca3b772013-04-30 13:36:55 -070056:: type_members = [m for m in ofclass.members if type(m) == TypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070057class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070058:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070059 ${m.name} = ${m.value}
60:: #endfor
61
Rich Lane8ca3b772013-04-30 13:36:55 -070062 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070063 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070064:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070065 if ${m.name} != None:
66 self.${m.name} = ${m.name}
67 else:
68 self.${m.name} = ${m.oftype.gen_init_expr()}
69:: #endfor
70
71 def pack(self):
72 packed = []
Rich Lanea06d0c32013-03-25 08:52:03 -070073:: include('_pack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070074 return ''.join(packed)
75
76 @staticmethod
77 def unpack(buf):
78 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
79 obj = ${ofclass.pyname}()
Rich Lanea06d0c32013-03-25 08:52:03 -070080:: include('_unpack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070081 return obj
82
83 def __eq__(self, other):
84 if type(self) != type(other): return False
85 if self.version != other.version: return False
86 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070087:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070088 if self.${m.name} != other.${m.name}: return False
89:: #endfor
90 return True
91
92 def __ne__(self, other):
93 return not self.__eq__(other)
94
95 def __str__(self):
96 return self.show()
97
98 def show(self):
99 import loxi.pp
100 return loxi.pp.pp(self)
101
102 def pretty_print(self, q):
103:: include('_pretty_print.py', ofclass=ofclass)
104
105:: #endfor
106
107def parse_header(buf):
108 if len(buf) < 8:
109 raise loxi.ProtocolError("too short to be an OpenFlow message")
110 return struct.unpack_from("!BBHL", buf)
111
112def parse_message(buf):
113 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
114 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
115 raise loxi.ProtocolError("wrong OpenFlow version")
116 if len(buf) != msg_len:
117 raise loxi.ProtocolError("incorrect message size")
118 if msg_type in parsers:
119 return parsers[msg_type](buf)
120 else:
121 raise loxi.ProtocolError("unexpected message type")
122
123:: # TODO fix for OF 1.1+
124def parse_flow_mod(buf):
125 if len(buf) < 56 + 2:
126 raise loxi.ProtocolError("message too short")
127 cmd, = struct.unpack_from("!H", buf, 56)
128 if cmd in flow_mod_parsers:
129 return flow_mod_parsers[cmd](buf)
130 else:
131 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
132
Rich Lane3f075972013-03-15 22:56:29 -0700133:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700134def parse_stats_reply(buf):
135 if len(buf) < 8 + 2:
136 raise loxi.ProtocolError("message too short")
137 stats_type, = struct.unpack_from("!H", buf, 8)
138 if stats_type in stats_reply_parsers:
139 return stats_reply_parsers[stats_type](buf)
140 else:
141 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
142
143def parse_stats_request(buf):
144 if len(buf) < 8 + 2:
145 raise loxi.ProtocolError("message too short")
146 stats_type, = struct.unpack_from("!H", buf, 8)
147 if stats_type in stats_request_parsers:
148 return stats_request_parsers[stats_type](buf)
149 else:
150 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700151:: else:
152def parse_multipart_reply(buf):
153 if len(buf) < 8 + 2:
154 raise loxi.ProtocolError("message too short")
155 multipart_type, = struct.unpack_from("!H", buf, 8)
156 if multipart_type in multipart_reply_parsers:
157 return multipart_reply_parsers[multipart_type](buf)
158 else:
159 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700160
Rich Lane3f075972013-03-15 22:56:29 -0700161def parse_multipart_request(buf):
162 if len(buf) < 8 + 2:
163 raise loxi.ProtocolError("message too short")
164 multipart_type, = struct.unpack_from("!H", buf, 8)
165 if multipart_type in multipart_request_parsers:
166 return multipart_request_parsers[multipart_type](buf)
167 else:
168 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
169:: #endif
170
171:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700172def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700173:: else:
174def parse_experimenter(buf):
175:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700176 if len(buf) < 16:
177 raise loxi.ProtocolError("experimenter message too short")
178
179 experimenter, = struct.unpack_from("!L", buf, 8)
180 if experimenter == 0x005c16c7: # Big Switch Networks
181 subtype, = struct.unpack_from("!L", buf, 12)
182 elif experimenter == 0x00002320: # Nicira
183 subtype, = struct.unpack_from("!L", buf, 12)
184 else:
185 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
186
187 if subtype in experimenter_parsers[experimenter]:
188 return experimenter_parsers[experimenter][subtype](buf)
189 else:
190 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
191
192parsers = {
193:: sort_key = lambda x: x.type_members[1].value
194:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
195:: for (k, v) in msgtype_groups:
196:: v = list(v)
197:: if len(v) == 1:
198 ${k} : ${v[0].pyname}.unpack,
199:: else:
200 ${k} : parse_${k[11:].lower()},
201:: #endif
202:: #endfor
203}
204
205flow_mod_parsers = {
206 const.OFPFC_ADD : flow_add.unpack,
207 const.OFPFC_MODIFY : flow_modify.unpack,
208 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
209 const.OFPFC_DELETE : flow_delete.unpack,
210 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
211}
212
Rich Lane3f075972013-03-15 22:56:29 -0700213:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700214stats_reply_parsers = {
215 const.OFPST_DESC : desc_stats_reply.unpack,
216 const.OFPST_FLOW : flow_stats_reply.unpack,
217 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
218 const.OFPST_TABLE : table_stats_reply.unpack,
219 const.OFPST_PORT : port_stats_reply.unpack,
220 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700221:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700222 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700223:: else:
224 const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack,
225:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700226:: if version >= of_g.VERSION_1_1:
227 const.OFPST_GROUP : group_stats_reply.unpack,
228 const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack,
229:: #endif
230:: if version >= of_g.VERSION_1_2:
231 const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack,
232:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700233}
234
235stats_request_parsers = {
236 const.OFPST_DESC : desc_stats_request.unpack,
237 const.OFPST_FLOW : flow_stats_request.unpack,
238 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
239 const.OFPST_TABLE : table_stats_request.unpack,
240 const.OFPST_PORT : port_stats_request.unpack,
241 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700242:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700243 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700244:: else:
245 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
246:: #endif
Rich Lane5edb5552013-05-07 18:49:29 -0700247:: if version >= of_g.VERSION_1_1:
248 const.OFPST_GROUP : group_stats_request.unpack,
249 const.OFPST_GROUP_DESC : group_desc_stats_request.unpack,
250:: #endif
251:: if version >= of_g.VERSION_1_2:
252 const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack,
253:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700254}
Rich Lane3f075972013-03-15 22:56:29 -0700255:: else:
Rich Lane5edb5552013-05-07 18:49:29 -0700256# TODO OF 1.3 multipart messages
Rich Lane3f075972013-03-15 22:56:29 -0700257:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700258
259:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
260:: sort_key = lambda x: x.type_members[2].value
261:: experimenter_ofclasses.sort(key=sort_key)
262:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
263experimenter_parsers = {
264:: for (experimenter, v) in grouped:
265 ${experimenter} : {
266:: for ofclass in v:
267 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
268:: #endfor
269 },
270:: #endfor
271}