Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | :: # 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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 29 | :: import of_g |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 30 | :: include('_copyright.py') |
| 31 | |
| 32 | :: include('_autogen.py') |
| 33 | |
| 34 | import struct |
| 35 | import loxi |
| 36 | import const |
| 37 | import common |
| 38 | import action # for unpack_list |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 39 | :: if version >= 4: |
| 40 | import meter_band # for unpack_list |
| 41 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 42 | import util |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 43 | import loxi.generic_util |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 44 | |
| 45 | class Message(object): |
| 46 | version = const.OFP_VERSION |
| 47 | type = None # override in subclass |
| 48 | xid = None |
| 49 | |
| 50 | :: for ofclass in ofclasses: |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 51 | :: from py_gen.codegen import Member, LengthMember, TypeMember |
Rich Lane | 5de3bb2 | 2013-05-01 13:38:39 -0700 | [diff] [blame] | 52 | :: normal_members = [m for m in ofclass.members if type(m) == Member] |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 53 | :: type_members = [m for m in ofclass.members if type(m) == TypeMember] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 54 | class ${ofclass.pyname}(Message): |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 55 | :: for m in type_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | ${m.name} = ${m.value} |
| 57 | :: #endfor |
| 58 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 59 | def __init__(self, ${', '.join(["%s=None" % m.name for m in normal_members])}): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 60 | self.xid = xid |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 61 | :: for m in [x for x in normal_members if x.name != 'xid']: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 62 | 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 Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 70 | :: include('_pack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 71 | 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 Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 77 | :: include('_unpack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 78 | 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 Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 84 | :: for m in normal_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 85 | 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 | |
| 104 | def 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 | |
| 109 | def 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+ |
| 121 | def 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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 130 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 131 | def 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 | |
| 140 | def 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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 148 | :: else: |
| 149 | def 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 Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 157 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 158 | def 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 Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 169 | def parse_vendor(buf): |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 170 | :: else: |
| 171 | def parse_experimenter(buf): |
| 172 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 173 | 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 | |
| 189 | parsers = { |
| 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 | |
| 202 | flow_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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 210 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 211 | stats_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 Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 218 | :: if version < of_g.VERSION_1_1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 219 | const.OFPST_VENDOR : experimenter_stats_reply.unpack, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 220 | :: else: |
| 221 | const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack, |
| 222 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | stats_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 Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 232 | :: if version < of_g.VERSION_1_1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 233 | const.OFPST_VENDOR : experimenter_stats_request.unpack, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 234 | :: else: |
| 235 | const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack, |
| 236 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 237 | } |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 238 | :: else: |
| 239 | |
| 240 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 241 | |
| 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) |
| 246 | experimenter_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 | } |