blob: 228d2d06b3f11b4aafbb5cd13691b9225c61f0a6 [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 Laned82c0a62013-05-02 15:40:35 -070039:: if version >= 4:
40import meter_band # for unpack_list
41:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070042import util
Rich Lane15cbe842013-04-26 16:04:11 -070043import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070044
45class Message(object):
46 version = const.OFP_VERSION
47 type = None # override in subclass
48 xid = None
49
50:: for ofclass in ofclasses:
Rich Lane8ca3b772013-04-30 13:36:55 -070051:: from py_gen.codegen import Member, LengthMember, TypeMember
Rich Lane5de3bb22013-05-01 13:38:39 -070052:: normal_members = [m for m in ofclass.members if type(m) == Member]
Rich Lane8ca3b772013-04-30 13:36:55 -070053:: type_members = [m for m in ofclass.members if type(m) == TypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070054class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070055:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070056 ${m.name} = ${m.value}
57:: #endfor
58
Rich Lane8ca3b772013-04-30 13:36:55 -070059 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070060 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070061:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070062 if ${m.name} != None:
63 self.${m.name} = ${m.name}
64 else:
65 self.${m.name} = ${m.oftype.gen_init_expr()}
66:: #endfor
67
68 def pack(self):
69 packed = []
Rich Lanea06d0c32013-03-25 08:52:03 -070070:: include('_pack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070071 return ''.join(packed)
72
73 @staticmethod
74 def unpack(buf):
75 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
76 obj = ${ofclass.pyname}()
Rich Lanea06d0c32013-03-25 08:52:03 -070077:: include('_unpack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070078 return obj
79
80 def __eq__(self, other):
81 if type(self) != type(other): return False
82 if self.version != other.version: return False
83 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070084:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070085 if self.${m.name} != other.${m.name}: return False
86:: #endfor
87 return True
88
89 def __ne__(self, other):
90 return not self.__eq__(other)
91
92 def __str__(self):
93 return self.show()
94
95 def show(self):
96 import loxi.pp
97 return loxi.pp.pp(self)
98
99 def pretty_print(self, q):
100:: include('_pretty_print.py', ofclass=ofclass)
101
102:: #endfor
103
104def parse_header(buf):
105 if len(buf) < 8:
106 raise loxi.ProtocolError("too short to be an OpenFlow message")
107 return struct.unpack_from("!BBHL", buf)
108
109def parse_message(buf):
110 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
111 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
112 raise loxi.ProtocolError("wrong OpenFlow version")
113 if len(buf) != msg_len:
114 raise loxi.ProtocolError("incorrect message size")
115 if msg_type in parsers:
116 return parsers[msg_type](buf)
117 else:
118 raise loxi.ProtocolError("unexpected message type")
119
120:: # TODO fix for OF 1.1+
121def parse_flow_mod(buf):
122 if len(buf) < 56 + 2:
123 raise loxi.ProtocolError("message too short")
124 cmd, = struct.unpack_from("!H", buf, 56)
125 if cmd in flow_mod_parsers:
126 return flow_mod_parsers[cmd](buf)
127 else:
128 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
129
Rich Lane3f075972013-03-15 22:56:29 -0700130:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700131def parse_stats_reply(buf):
132 if len(buf) < 8 + 2:
133 raise loxi.ProtocolError("message too short")
134 stats_type, = struct.unpack_from("!H", buf, 8)
135 if stats_type in stats_reply_parsers:
136 return stats_reply_parsers[stats_type](buf)
137 else:
138 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
139
140def parse_stats_request(buf):
141 if len(buf) < 8 + 2:
142 raise loxi.ProtocolError("message too short")
143 stats_type, = struct.unpack_from("!H", buf, 8)
144 if stats_type in stats_request_parsers:
145 return stats_request_parsers[stats_type](buf)
146 else:
147 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700148:: else:
149def parse_multipart_reply(buf):
150 if len(buf) < 8 + 2:
151 raise loxi.ProtocolError("message too short")
152 multipart_type, = struct.unpack_from("!H", buf, 8)
153 if multipart_type in multipart_reply_parsers:
154 return multipart_reply_parsers[multipart_type](buf)
155 else:
156 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700157
Rich Lane3f075972013-03-15 22:56:29 -0700158def parse_multipart_request(buf):
159 if len(buf) < 8 + 2:
160 raise loxi.ProtocolError("message too short")
161 multipart_type, = struct.unpack_from("!H", buf, 8)
162 if multipart_type in multipart_request_parsers:
163 return multipart_request_parsers[multipart_type](buf)
164 else:
165 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
166:: #endif
167
168:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700169def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700170:: else:
171def parse_experimenter(buf):
172:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700173 if len(buf) < 16:
174 raise loxi.ProtocolError("experimenter message too short")
175
176 experimenter, = struct.unpack_from("!L", buf, 8)
177 if experimenter == 0x005c16c7: # Big Switch Networks
178 subtype, = struct.unpack_from("!L", buf, 12)
179 elif experimenter == 0x00002320: # Nicira
180 subtype, = struct.unpack_from("!L", buf, 12)
181 else:
182 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
183
184 if subtype in experimenter_parsers[experimenter]:
185 return experimenter_parsers[experimenter][subtype](buf)
186 else:
187 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
188
189parsers = {
190:: sort_key = lambda x: x.type_members[1].value
191:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
192:: for (k, v) in msgtype_groups:
193:: v = list(v)
194:: if len(v) == 1:
195 ${k} : ${v[0].pyname}.unpack,
196:: else:
197 ${k} : parse_${k[11:].lower()},
198:: #endif
199:: #endfor
200}
201
202flow_mod_parsers = {
203 const.OFPFC_ADD : flow_add.unpack,
204 const.OFPFC_MODIFY : flow_modify.unpack,
205 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
206 const.OFPFC_DELETE : flow_delete.unpack,
207 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
208}
209
Rich Lane3f075972013-03-15 22:56:29 -0700210:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700211stats_reply_parsers = {
212 const.OFPST_DESC : desc_stats_reply.unpack,
213 const.OFPST_FLOW : flow_stats_reply.unpack,
214 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
215 const.OFPST_TABLE : table_stats_reply.unpack,
216 const.OFPST_PORT : port_stats_reply.unpack,
217 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700218:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700219 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700220:: else:
221 const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack,
222:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700223}
224
225stats_request_parsers = {
226 const.OFPST_DESC : desc_stats_request.unpack,
227 const.OFPST_FLOW : flow_stats_request.unpack,
228 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
229 const.OFPST_TABLE : table_stats_request.unpack,
230 const.OFPST_PORT : port_stats_request.unpack,
231 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700232:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700233 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700234:: else:
235 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
236:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700237}
Rich Lane3f075972013-03-15 22:56:29 -0700238:: else:
239
240:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700241
242:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
243:: sort_key = lambda x: x.type_members[2].value
244:: experimenter_ofclasses.sort(key=sort_key)
245:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
246experimenter_parsers = {
247:: for (experimenter, v) in grouped:
248 ${experimenter} : {
249:: for ofclass in v:
250 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
251:: #endfor
252 },
253:: #endfor
254}