blob: 71a2871dd1c7d445fc4c829529c0703d09a7d72d [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
29:: include('_copyright.py')
30
31:: include('_autogen.py')
32
33import struct
34import loxi
35import const
36import common
37import action # for unpack_list
38import util
39
40class Message(object):
41 version = const.OFP_VERSION
42 type = None # override in subclass
43 xid = None
44
45:: for ofclass in ofclasses:
46:: nonskip_members = [m for m in ofclass.members if not m.skip]
47class ${ofclass.pyname}(Message):
48:: for m in ofclass.type_members:
49 ${m.name} = ${m.value}
50:: #endfor
51
52 def __init__(self, ${', '.join(["%s=None" % m.name for m in nonskip_members])}):
53 self.xid = xid
54:: for m in [x for x in nonskip_members if x.name != 'xid']:
55 if ${m.name} != None:
56 self.${m.name} = ${m.name}
57 else:
58 self.${m.name} = ${m.oftype.gen_init_expr()}
59:: #endfor
60
61 def pack(self):
62 packed = []
63:: if ofclass.name == 'of_packet_out':
64:: include('_pack_packet_out.py', ofclass=ofclass)
65:: else:
66:: include('_pack.py', ofclass=ofclass)
67:: #endif
68 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}()
74:: if ofclass.name == 'of_packet_out':
75:: include('_unpack_packet_out.py', ofclass=ofclass)
76:: else:
77:: include('_unpack.py', ofclass=ofclass)
78:: #endif
79 return obj
80
81 def __eq__(self, other):
82 if type(self) != type(other): return False
83 if self.version != other.version: return False
84 if self.type != other.type: return False
85:: for m in nonskip_members:
86 if self.${m.name} != other.${m.name}: return False
87:: #endfor
88 return True
89
90 def __ne__(self, other):
91 return not self.__eq__(other)
92
93 def __str__(self):
94 return self.show()
95
96 def show(self):
97 import loxi.pp
98 return loxi.pp.pp(self)
99
100 def pretty_print(self, q):
101:: include('_pretty_print.py', ofclass=ofclass)
102
103:: #endfor
104
105def parse_header(buf):
106 if len(buf) < 8:
107 raise loxi.ProtocolError("too short to be an OpenFlow message")
108 return struct.unpack_from("!BBHL", buf)
109
110def parse_message(buf):
111 msg_ver, msg_type, msg_len, msg_xid = parse_header(buf)
112 if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO:
113 raise loxi.ProtocolError("wrong OpenFlow version")
114 if len(buf) != msg_len:
115 raise loxi.ProtocolError("incorrect message size")
116 if msg_type in parsers:
117 return parsers[msg_type](buf)
118 else:
119 raise loxi.ProtocolError("unexpected message type")
120
121:: # TODO fix for OF 1.1+
122def parse_flow_mod(buf):
123 if len(buf) < 56 + 2:
124 raise loxi.ProtocolError("message too short")
125 cmd, = struct.unpack_from("!H", buf, 56)
126 if cmd in flow_mod_parsers:
127 return flow_mod_parsers[cmd](buf)
128 else:
129 raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd)
130
131def 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)
148
149def parse_vendor(buf):
150 if len(buf) < 16:
151 raise loxi.ProtocolError("experimenter message too short")
152
153 experimenter, = struct.unpack_from("!L", buf, 8)
154 if experimenter == 0x005c16c7: # Big Switch Networks
155 subtype, = struct.unpack_from("!L", buf, 12)
156 elif experimenter == 0x00002320: # Nicira
157 subtype, = struct.unpack_from("!L", buf, 12)
158 else:
159 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
160
161 if subtype in experimenter_parsers[experimenter]:
162 return experimenter_parsers[experimenter][subtype](buf)
163 else:
164 raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype))
165
166parsers = {
167:: sort_key = lambda x: x.type_members[1].value
168:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
169:: for (k, v) in msgtype_groups:
170:: v = list(v)
171:: if len(v) == 1:
172 ${k} : ${v[0].pyname}.unpack,
173:: else:
174 ${k} : parse_${k[11:].lower()},
175:: #endif
176:: #endfor
177}
178
179flow_mod_parsers = {
180 const.OFPFC_ADD : flow_add.unpack,
181 const.OFPFC_MODIFY : flow_modify.unpack,
182 const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack,
183 const.OFPFC_DELETE : flow_delete.unpack,
184 const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack,
185}
186
187stats_reply_parsers = {
188 const.OFPST_DESC : desc_stats_reply.unpack,
189 const.OFPST_FLOW : flow_stats_reply.unpack,
190 const.OFPST_AGGREGATE : aggregate_stats_reply.unpack,
191 const.OFPST_TABLE : table_stats_reply.unpack,
192 const.OFPST_PORT : port_stats_reply.unpack,
193 const.OFPST_QUEUE : queue_stats_reply.unpack,
194 const.OFPST_VENDOR : experimenter_stats_reply.unpack,
195}
196
197stats_request_parsers = {
198 const.OFPST_DESC : desc_stats_request.unpack,
199 const.OFPST_FLOW : flow_stats_request.unpack,
200 const.OFPST_AGGREGATE : aggregate_stats_request.unpack,
201 const.OFPST_TABLE : table_stats_request.unpack,
202 const.OFPST_PORT : port_stats_request.unpack,
203 const.OFPST_QUEUE : queue_stats_request.unpack,
204 const.OFPST_VENDOR : experimenter_stats_request.unpack,
205}
206
207:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 'const.OFPT_VENDOR']
208:: sort_key = lambda x: x.type_members[2].value
209:: experimenter_ofclasses.sort(key=sort_key)
210:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
211experimenter_parsers = {
212:: for (experimenter, v) in grouped:
213 ${experimenter} : {
214:: for ofclass in v:
215 ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack,
216:: #endfor
217 },
218:: #endfor
219}