blob: d50185497d2eff3134afe471b1c1add75d2039bf [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
39import util
Rich Lane15cbe842013-04-26 16:04:11 -070040import loxi.generic_util
Rich Lanea06d0c32013-03-25 08:52:03 -070041
42class Message(object):
43 version = const.OFP_VERSION
44 type = None # override in subclass
45 xid = None
46
47:: for ofclass in ofclasses:
Rich Lane8ca3b772013-04-30 13:36:55 -070048:: from py_gen.codegen import Member, LengthMember, TypeMember
49:: normal_members = [m for m in ofclass.members if type(m) == Member and not m.skip]
50:: type_members = [m for m in ofclass.members if type(m) == TypeMember]
Rich Lanea06d0c32013-03-25 08:52:03 -070051class ${ofclass.pyname}(Message):
Rich Lane8ca3b772013-04-30 13:36:55 -070052:: for m in type_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070053 ${m.name} = ${m.value}
54:: #endfor
55
Rich Lane8ca3b772013-04-30 13:36:55 -070056 def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}):
Rich Lanea06d0c32013-03-25 08:52:03 -070057 self.xid = xid
Rich Lane8ca3b772013-04-30 13:36:55 -070058:: for m in [x for x in normal_members if x.name != 'xid']:
Rich Lanea06d0c32013-03-25 08:52:03 -070059 if ${m.name} != None:
60 self.${m.name} = ${m.name}
61 else:
62 self.${m.name} = ${m.oftype.gen_init_expr()}
63:: #endfor
64
65 def pack(self):
66 packed = []
67:: if ofclass.name == 'of_packet_out':
68:: include('_pack_packet_out.py', ofclass=ofclass)
69:: else:
70:: include('_pack.py', ofclass=ofclass)
71:: #endif
72 return ''.join(packed)
73
74 @staticmethod
75 def unpack(buf):
76 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
77 obj = ${ofclass.pyname}()
78:: if ofclass.name == 'of_packet_out':
79:: include('_unpack_packet_out.py', ofclass=ofclass)
80:: else:
81:: include('_unpack.py', ofclass=ofclass)
82:: #endif
83 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)
116 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
117 raise loxi.ProtocolError("wrong OpenFlow version")
118 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
125:: # TODO fix for OF 1.1+
126def parse_flow_mod(buf):
127 if len(buf) < 56 + 2:
128 raise loxi.ProtocolError("message too short")
129 cmd, = struct.unpack_from("!H", buf, 56)
130 if cmd in flow_mod_parsers:
131 return flow_mod_parsers[cmd](buf)
132 else:
133 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
134
Rich Lane3f075972013-03-15 22:56:29 -0700135:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700136def parse_stats_reply(buf):
137 if len(buf) < 8 + 2:
138 raise loxi.ProtocolError("message too short")
139 stats_type, = struct.unpack_from("!H", buf, 8)
140 if stats_type in stats_reply_parsers:
141 return stats_reply_parsers[stats_type](buf)
142 else:
143 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
144
145def parse_stats_request(buf):
146 if len(buf) < 8 + 2:
147 raise loxi.ProtocolError("message too short")
148 stats_type, = struct.unpack_from("!H", buf, 8)
149 if stats_type in stats_request_parsers:
150 return stats_request_parsers[stats_type](buf)
151 else:
152 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700153:: else:
154def parse_multipart_reply(buf):
155 if len(buf) < 8 + 2:
156 raise loxi.ProtocolError("message too short")
157 multipart_type, = struct.unpack_from("!H", buf, 8)
158 if multipart_type in multipart_reply_parsers:
159 return multipart_reply_parsers[multipart_type](buf)
160 else:
161 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700162
Rich Lane3f075972013-03-15 22:56:29 -0700163def parse_multipart_request(buf):
164 if len(buf) < 8 + 2:
165 raise loxi.ProtocolError("message too short")
166 multipart_type, = struct.unpack_from("!H", buf, 8)
167 if multipart_type in multipart_request_parsers:
168 return multipart_request_parsers[multipart_type](buf)
169 else:
170 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
171:: #endif
172
173:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700174def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700175:: else:
176def parse_experimenter(buf):
177:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700178 if len(buf) < 16:
179 raise loxi.ProtocolError("experimenter message too short")
180
181 experimenter, = struct.unpack_from("!L", buf, 8)
182 if experimenter == 0x005c16c7: # Big Switch Networks
183 subtype, = struct.unpack_from("!L", buf, 12)
184 elif experimenter == 0x00002320: # Nicira
185 subtype, = struct.unpack_from("!L", buf, 12)
186 else:
187 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
188
189 if subtype in experimenter_parsers[experimenter]:
190 return experimenter_parsers[experimenter][subtype](buf)
191 else:
192 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
193
194parsers = {
195:: sort_key = lambda x: x.type_members[1].value
196:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
197:: for (k, v) in msgtype_groups:
198:: 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 Lanea06d0c32013-03-25 08:52:03 -0700228}
229
230stats_request_parsers = {
231 const.OFPST_DESC : desc_stats_request.unpack,
232 const.OFPST_FLOW : flow_stats_request.unpack,
233 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
234 const.OFPST_TABLE : table_stats_request.unpack,
235 const.OFPST_PORT : port_stats_request.unpack,
236 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700237:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700238 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700239:: else:
240 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
241:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700242}
Rich Lane3f075972013-03-15 22:56:29 -0700243:: else:
244
245:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700246
247:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
248:: sort_key = lambda x: x.type_members[2].value
249:: experimenter_ofclasses.sort(key=sort_key)
250:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
251experimenter_parsers = {
252:: for (experimenter, v) in grouped:
253 ${experimenter} : {
254:: for ofclass in v:
255 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
256:: #endfor
257 },
258:: #endfor
259}