blob: 69c3fe1c1b5c5146d29ef91cd82eb44671c45434 [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
40
41class Message(object):
42 version = const.OFP_VERSION
43 type = None # override in subclass
44 xid = None
45
46:: for ofclass in ofclasses:
47:: nonskip_members = [m for m in ofclass.members if not m.skip]
48class ${ofclass.pyname}(Message):
49:: for m in ofclass.type_members:
50 ${m.name} = ${m.value}
51:: #endfor
52
53 def __init__(self, ${', '.join(["%s=None" % m.name for m in nonskip_members])}):
54 self.xid = xid
55:: for m in [x for x in nonskip_members if x.name != 'xid']:
56 if ${m.name} != None:
57 self.${m.name} = ${m.name}
58 else:
59 self.${m.name} = ${m.oftype.gen_init_expr()}
60:: #endfor
61
62 def pack(self):
63 packed = []
64:: if ofclass.name == 'of_packet_out':
65:: include('_pack_packet_out.py', ofclass=ofclass)
66:: else:
67:: include('_pack.py', ofclass=ofclass)
68:: #endif
69 return ''.join(packed)
70
71 @staticmethod
72 def unpack(buf):
73 if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message")
74 obj = ${ofclass.pyname}()
75:: if ofclass.name == 'of_packet_out':
76:: include('_unpack_packet_out.py', ofclass=ofclass)
77:: else:
78:: include('_unpack.py', ofclass=ofclass)
79:: #endif
80 return obj
81
82 def __eq__(self, other):
83 if type(self) != type(other): return False
84 if self.version != other.version: return False
85 if self.type != other.type: return False
86:: for m in nonskip_members:
87 if self.${m.name} != other.${m.name}: return False
88:: #endfor
89 return True
90
91 def __ne__(self, other):
92 return not self.__eq__(other)
93
94 def __str__(self):
95 return self.show()
96
97 def show(self):
98 import loxi.pp
99 return loxi.pp.pp(self)
100
101 def pretty_print(self, q):
102:: include('_pretty_print.py', ofclass=ofclass)
103
104:: #endfor
105
106def parse_header(buf):
107 if len(buf) < 8:
108 raise loxi.ProtocolError("too short to be an OpenFlow message")
109 return struct.unpack_from("!BBHL", buf)
110
111def parse_message(buf):
112 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
113 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
114 raise loxi.ProtocolError("wrong OpenFlow version")
115 if len(buf) != msg_len:
116 raise loxi.ProtocolError("incorrect message size")
117 if msg_type in parsers:
118 return parsers[msg_type](buf)
119 else:
120 raise loxi.ProtocolError("unexpected message type")
121
122:: # TODO fix for OF 1.1+
123def parse_flow_mod(buf):
124 if len(buf) < 56 + 2:
125 raise loxi.ProtocolError("message too short")
126 cmd, = struct.unpack_from("!H", buf, 56)
127 if cmd in flow_mod_parsers:
128 return flow_mod_parsers[cmd](buf)
129 else:
130 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
131
Rich Lane3f075972013-03-15 22:56:29 -0700132:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700133def parse_stats_reply(buf):
134 if len(buf) < 8 + 2:
135 raise loxi.ProtocolError("message too short")
136 stats_type, = struct.unpack_from("!H", buf, 8)
137 if stats_type in stats_reply_parsers:
138 return stats_reply_parsers[stats_type](buf)
139 else:
140 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
141
142def parse_stats_request(buf):
143 if len(buf) < 8 + 2:
144 raise loxi.ProtocolError("message too short")
145 stats_type, = struct.unpack_from("!H", buf, 8)
146 if stats_type in stats_request_parsers:
147 return stats_request_parsers[stats_type](buf)
148 else:
149 raise loxi.ProtocolError("unexpected stats type %u" % stats_type)
Rich Lane3f075972013-03-15 22:56:29 -0700150:: else:
151def parse_multipart_reply(buf):
152 if len(buf) < 8 + 2:
153 raise loxi.ProtocolError("message too short")
154 multipart_type, = struct.unpack_from("!H", buf, 8)
155 if multipart_type in multipart_reply_parsers:
156 return multipart_reply_parsers[multipart_type](buf)
157 else:
158 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700159
Rich Lane3f075972013-03-15 22:56:29 -0700160def parse_multipart_request(buf):
161 if len(buf) < 8 + 2:
162 raise loxi.ProtocolError("message too short")
163 multipart_type, = struct.unpack_from("!H", buf, 8)
164 if multipart_type in multipart_request_parsers:
165 return multipart_request_parsers[multipart_type](buf)
166 else:
167 raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type)
168:: #endif
169
170:: if version == of_g.VERSION_1_0:
Rich Lanea06d0c32013-03-25 08:52:03 -0700171def parse_vendor(buf):
Rich Lane3f075972013-03-15 22:56:29 -0700172:: else:
173def parse_experimenter(buf):
174:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700175 if len(buf) < 16:
176 raise loxi.ProtocolError("experimenter message too short")
177
178 experimenter, = struct.unpack_from("!L", buf, 8)
179 if experimenter == 0x005c16c7: # Big Switch Networks
180 subtype, = struct.unpack_from("!L", buf, 12)
181 elif experimenter == 0x00002320: # Nicira
182 subtype, = struct.unpack_from("!L", buf, 12)
183 else:
184 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
185
186 if subtype in experimenter_parsers[experimenter]:
187 return experimenter_parsers[experimenter][subtype](buf)
188 else:
189 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
190
191parsers = {
192:: sort_key = lambda x: x.type_members[1].value
193:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
194:: for (k, v) in msgtype_groups:
195:: v = list(v)
196:: if len(v) == 1:
197 ${k} : ${v[0].pyname}.unpack,
198:: else:
199 ${k} : parse_${k[11:].lower()},
200:: #endif
201:: #endfor
202}
203
204flow_mod_parsers = {
205 const.OFPFC_ADD : flow_add.unpack,
206 const.OFPFC_MODIFY : flow_modify.unpack,
207 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
208 const.OFPFC_DELETE : flow_delete.unpack,
209 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
210}
211
Rich Lane3f075972013-03-15 22:56:29 -0700212:: if version < of_g.VERSION_1_3:
Rich Lanea06d0c32013-03-25 08:52:03 -0700213stats_reply_parsers = {
214 const.OFPST_DESC : desc_stats_reply.unpack,
215 const.OFPST_FLOW : flow_stats_reply.unpack,
216 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
217 const.OFPST_TABLE : table_stats_reply.unpack,
218 const.OFPST_PORT : port_stats_reply.unpack,
219 const.OFPST_QUEUE : queue_stats_reply.unpack,
220 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
221}
222
223stats_request_parsers = {
224 const.OFPST_DESC : desc_stats_request.unpack,
225 const.OFPST_FLOW : flow_stats_request.unpack,
226 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
227 const.OFPST_TABLE : table_stats_request.unpack,
228 const.OFPST_PORT : port_stats_request.unpack,
229 const.OFPST_QUEUE : queue_stats_request.unpack,
230 const.OFPST_VENDOR : experimenter_stats_request.unpack,
231}
Rich Lane3f075972013-03-15 22:56:29 -0700232:: else:
233
234:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700235
236:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
237:: sort_key = lambda x: x.type_members[2].value
238:: experimenter_ofclasses.sort(key=sort_key)
239:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
240experimenter_parsers = {
241:: for (experimenter, v) in grouped:
242 ${experimenter} : {
243:: for ofclass in v:
244 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
245:: #endfor
246 },
247:: #endfor
248}