blob: d81d5b7cb9b0db17f7eda9a9a11403c7780a196a [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
Rich Lane5de3bb22013-05-01 13:38:39 -070049:: normal_members = [m for m in ofclass.members if type(m) == Member]
Rich Lane8ca3b772013-04-30 13:36:55 -070050:: 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 = []
Rich Lanea06d0c32013-03-25 08:52:03 -070067:: include('_pack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070068 return ''.join(packed)
69
70 @staticmethod
71 def unpack(buf):
72 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
73 obj = ${ofclass.pyname}()
Rich Lanea06d0c32013-03-25 08:52:03 -070074:: include('_unpack.py', ofclass=ofclass)
Rich Lanea06d0c32013-03-25 08:52:03 -070075 return obj
76
77 def __eq__(self, other):
78 if type(self) != type(other): return False
79 if self.version != other.version: return False
80 if self.type != other.type: return False
Rich Lane8ca3b772013-04-30 13:36:55 -070081:: for m in normal_members:
Rich Lanea06d0c32013-03-25 08:52:03 -070082 if self.${m.name} != other.${m.name}: return False
83:: #endfor
84 return True
85
86 def __ne__(self, other):
87 return not self.__eq__(other)
88
89 def __str__(self):
90 return self.show()
91
92 def show(self):
93 import loxi.pp
94 return loxi.pp.pp(self)
95
96 def pretty_print(self, q):
97:: include('_pretty_print.py', ofclass=ofclass)
98
99:: #endfor
100
101def parse_header(buf):
102 if len(buf) < 8:
103 raise loxi.ProtocolError("too short to be an OpenFlow message")
104 return struct.unpack_from("!BBHL", buf)
105
106def parse_message(buf):
107 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
108 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
109 raise loxi.ProtocolError("wrong OpenFlow version")
110 if len(buf) != msg_len:
111 raise loxi.ProtocolError("incorrect message size")
112 if msg_type in parsers:
113 return parsers[msg_type](buf)
114 else:
115 raise loxi.ProtocolError("unexpected message type")
116
117:: # TODO fix for OF 1.1+
118def parse_flow_mod(buf):
119 if len(buf) < 56 + 2:
120 raise loxi.ProtocolError("message too short")
121 cmd, = struct.unpack_from("!H", buf, 56)
122 if cmd in flow_mod_parsers:
123 return flow_mod_parsers[cmd](buf)
124 else:
125 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
126
Rich Lane3f075972013-03-15 22:56:29 -0700127:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700128def parse_stats_reply(buf):
129 if len(buf) < 8 + 2:
130 raise loxi.ProtocolError("message too short")
131 stats_type, = struct.unpack_from("!H", buf, 8)
132 if stats_type in stats_reply_parsers:
133 return stats_reply_parsers[stats_type](buf)
134 else:
135 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
136
137def parse_stats_request(buf):
138 if len(buf) < 8 + 2:
139 raise loxi.ProtocolError("message too short")
140 stats_type, = struct.unpack_from("!H", buf, 8)
141 if stats_type in stats_request_parsers:
142 return stats_request_parsers[stats_type](buf)
143 else:
144 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700145:: else:
146def parse_multipart_reply(buf):
147 if len(buf) < 8 + 2:
148 raise loxi.ProtocolError("message too short")
149 multipart_type, = struct.unpack_from("!H", buf, 8)
150 if multipart_type in multipart_reply_parsers:
151 return multipart_reply_parsers[multipart_type](buf)
152 else:
153 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700154
Rich Lane3f075972013-03-15 22:56:29 -0700155def parse_multipart_request(buf):
156 if len(buf) < 8 + 2:
157 raise loxi.ProtocolError("message too short")
158 multipart_type, = struct.unpack_from("!H", buf, 8)
159 if multipart_type in multipart_request_parsers:
160 return multipart_request_parsers[multipart_type](buf)
161 else:
162 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
163:: #endif
164
165:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700166def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700167:: else:
168def parse_experimenter(buf):
169:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700170 if len(buf) < 16:
171 raise loxi.ProtocolError("experimenter message too short")
172
173 experimenter, = struct.unpack_from("!L", buf, 8)
174 if experimenter == 0x005c16c7: # Big Switch Networks
175 subtype, = struct.unpack_from("!L", buf, 12)
176 elif experimenter == 0x00002320: # Nicira
177 subtype, = struct.unpack_from("!L", buf, 12)
178 else:
179 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
180
181 if subtype in experimenter_parsers[experimenter]:
182 return experimenter_parsers[experimenter][subtype](buf)
183 else:
184 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
185
186parsers = {
187:: sort_key = lambda x: x.type_members[1].value
188:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
189:: for (k, v) in msgtype_groups:
190:: 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
199flow_mod_parsers = {
200 const.OFPFC_ADD : flow_add.unpack,
201 const.OFPFC_MODIFY : flow_modify.unpack,
202 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
203 const.OFPFC_DELETE : flow_delete.unpack,
204 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
205}
206
Rich Lane3f075972013-03-15 22:56:29 -0700207:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700208stats_reply_parsers = {
209 const.OFPST_DESC : desc_stats_reply.unpack,
210 const.OFPST_FLOW : flow_stats_reply.unpack,
211 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
212 const.OFPST_TABLE : table_stats_reply.unpack,
213 const.OFPST_PORT : port_stats_reply.unpack,
214 const.OFPST_QUEUE : queue_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700215:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700216 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700217:: else:
218 const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack,
219:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700220}
221
222stats_request_parsers = {
223 const.OFPST_DESC : desc_stats_request.unpack,
224 const.OFPST_FLOW : flow_stats_request.unpack,
225 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
226 const.OFPST_TABLE : table_stats_request.unpack,
227 const.OFPST_PORT : port_stats_request.unpack,
228 const.OFPST_QUEUE : queue_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700229:: if version < of_g.VERSION_1_1:
Rich Lanea06d0c32013-03-25 08:52:03 -0700230 const.OFPST_VENDOR : experimenter_stats_request.unpack,
Rich Lanea22233e2013-04-25 13:18:41 -0700231:: else:
232 const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack,
233:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700234}
Rich Lane3f075972013-03-15 22:56:29 -0700235:: else:
236
237:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700238
239:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
240:: sort_key = lambda x: x.type_members[2].value
241:: experimenter_ofclasses.sort(key=sort_key)
242:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
243experimenter_parsers = {
244:: for (experimenter, v) in grouped:
245 ${experimenter} : {
246:: for ofclass in v:
247 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
248:: #endfor
249 },
250:: #endfor
251}