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 | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 30 | :: import py_gen.util as util |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 31 | :: include('_copyright.py') |
| 32 | |
| 33 | :: include('_autogen.py') |
| 34 | |
| 35 | import struct |
| 36 | import loxi |
| 37 | import const |
| 38 | import common |
| 39 | import action # for unpack_list |
Rich Lane | ed4f906 | 2013-05-02 17:05:03 -0700 | [diff] [blame] | 40 | :: if version >= 2: |
| 41 | import instruction # for unpack_list |
| 42 | :: #endif |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 43 | :: if version >= 4: |
| 44 | import meter_band # for unpack_list |
| 45 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 46 | import util |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 47 | import loxi.generic_util |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 48 | |
| 49 | class Message(object): |
| 50 | version = const.OFP_VERSION |
| 51 | type = None # override in subclass |
| 52 | xid = None |
| 53 | |
| 54 | :: for ofclass in ofclasses: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 55 | :: from loxi_ir import * |
| 56 | :: normal_members = [m for m in ofclass.members if type(m) == OFDataMember] |
| 57 | :: type_members = [m for m in ofclass.members if type(m) == OFTypeMember] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | class ${ofclass.pyname}(Message): |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 59 | :: for m in type_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 60 | ${m.name} = ${m.value} |
| 61 | :: #endfor |
| 62 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 63 | 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] | 64 | self.xid = xid |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 65 | :: 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] | 66 | if ${m.name} != None: |
| 67 | self.${m.name} = ${m.name} |
| 68 | else: |
| 69 | self.${m.name} = ${m.oftype.gen_init_expr()} |
| 70 | :: #endfor |
| 71 | |
| 72 | def pack(self): |
| 73 | packed = [] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 74 | :: include('_pack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 75 | return ''.join(packed) |
| 76 | |
| 77 | @staticmethod |
| 78 | def unpack(buf): |
| 79 | if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message") |
| 80 | obj = ${ofclass.pyname}() |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | :: include('_unpack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 82 | return obj |
| 83 | |
| 84 | def __eq__(self, other): |
| 85 | if type(self) != type(other): return False |
| 86 | if self.version != other.version: return False |
| 87 | if self.type != other.type: return False |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 88 | :: for m in normal_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 89 | if self.${m.name} != other.${m.name}: return False |
| 90 | :: #endfor |
| 91 | return True |
| 92 | |
| 93 | def __ne__(self, other): |
| 94 | return not self.__eq__(other) |
| 95 | |
| 96 | def __str__(self): |
| 97 | return self.show() |
| 98 | |
| 99 | def show(self): |
| 100 | import loxi.pp |
| 101 | return loxi.pp.pp(self) |
| 102 | |
| 103 | def pretty_print(self, q): |
| 104 | :: include('_pretty_print.py', ofclass=ofclass) |
| 105 | |
| 106 | :: #endfor |
| 107 | |
| 108 | def parse_header(buf): |
| 109 | if len(buf) < 8: |
| 110 | raise loxi.ProtocolError("too short to be an OpenFlow message") |
| 111 | return struct.unpack_from("!BBHL", buf) |
| 112 | |
| 113 | def parse_message(buf): |
| 114 | msg_ver, msg_type, msg_len, msg_xid = parse_header(buf) |
| 115 | if msg_ver != const.OFP_VERSION and msg_type != ofp.OFPT_HELLO: |
| 116 | raise loxi.ProtocolError("wrong OpenFlow version") |
| 117 | if len(buf) != msg_len: |
| 118 | raise loxi.ProtocolError("incorrect message size") |
| 119 | if msg_type in parsers: |
| 120 | return parsers[msg_type](buf) |
| 121 | else: |
| 122 | raise loxi.ProtocolError("unexpected message type") |
| 123 | |
| 124 | :: # TODO fix for OF 1.1+ |
| 125 | def parse_flow_mod(buf): |
| 126 | if len(buf) < 56 + 2: |
| 127 | raise loxi.ProtocolError("message too short") |
| 128 | cmd, = struct.unpack_from("!H", buf, 56) |
| 129 | if cmd in flow_mod_parsers: |
| 130 | return flow_mod_parsers[cmd](buf) |
| 131 | else: |
| 132 | raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd) |
| 133 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 134 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | def parse_stats_reply(buf): |
| 136 | if len(buf) < 8 + 2: |
| 137 | raise loxi.ProtocolError("message too short") |
| 138 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 139 | if stats_type in stats_reply_parsers: |
| 140 | return stats_reply_parsers[stats_type](buf) |
| 141 | else: |
| 142 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
| 143 | |
| 144 | def parse_stats_request(buf): |
| 145 | if len(buf) < 8 + 2: |
| 146 | raise loxi.ProtocolError("message too short") |
| 147 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 148 | if stats_type in stats_request_parsers: |
| 149 | return stats_request_parsers[stats_type](buf) |
| 150 | else: |
| 151 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 152 | :: else: |
| 153 | def parse_multipart_reply(buf): |
| 154 | if len(buf) < 8 + 2: |
| 155 | raise loxi.ProtocolError("message too short") |
| 156 | multipart_type, = struct.unpack_from("!H", buf, 8) |
| 157 | if multipart_type in multipart_reply_parsers: |
| 158 | return multipart_reply_parsers[multipart_type](buf) |
| 159 | else: |
| 160 | raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 161 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 162 | def parse_multipart_request(buf): |
| 163 | if len(buf) < 8 + 2: |
| 164 | raise loxi.ProtocolError("message too short") |
| 165 | multipart_type, = struct.unpack_from("!H", buf, 8) |
| 166 | if multipart_type in multipart_request_parsers: |
| 167 | return multipart_request_parsers[multipart_type](buf) |
| 168 | else: |
| 169 | raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type) |
| 170 | :: #endif |
| 171 | |
| 172 | :: if version == of_g.VERSION_1_0: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 173 | def parse_vendor(buf): |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 174 | :: else: |
| 175 | def parse_experimenter(buf): |
| 176 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 177 | if len(buf) < 16: |
| 178 | raise loxi.ProtocolError("experimenter message too short") |
| 179 | |
| 180 | experimenter, = struct.unpack_from("!L", buf, 8) |
| 181 | if experimenter == 0x005c16c7: # Big Switch Networks |
| 182 | subtype, = struct.unpack_from("!L", buf, 12) |
| 183 | elif experimenter == 0x00002320: # Nicira |
| 184 | subtype, = struct.unpack_from("!L", buf, 12) |
| 185 | else: |
| 186 | raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter) |
| 187 | |
| 188 | if subtype in experimenter_parsers[experimenter]: |
| 189 | return experimenter_parsers[experimenter][subtype](buf) |
| 190 | else: |
| 191 | raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype)) |
| 192 | |
| 193 | parsers = { |
| 194 | :: sort_key = lambda x: x.type_members[1].value |
| 195 | :: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key) |
| 196 | :: for (k, v) in msgtype_groups: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 197 | :: k = util.constant_for_value(version, "ofp_type", k) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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 | |
| 207 | flow_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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 215 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 216 | stats_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 Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 223 | :: if version < of_g.VERSION_1_1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 224 | const.OFPST_VENDOR : experimenter_stats_reply.unpack, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 225 | :: else: |
| 226 | const.OFPST_EXPERIMENTER : experimenter_stats_reply.unpack, |
| 227 | :: #endif |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 228 | :: if version >= of_g.VERSION_1_1: |
| 229 | const.OFPST_GROUP : group_stats_reply.unpack, |
| 230 | const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack, |
| 231 | :: #endif |
| 232 | :: if version >= of_g.VERSION_1_2: |
| 233 | const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack, |
| 234 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | stats_request_parsers = { |
| 238 | const.OFPST_DESC : desc_stats_request.unpack, |
| 239 | const.OFPST_FLOW : flow_stats_request.unpack, |
| 240 | const.OFPST_AGGREGATE : aggregate_stats_request.unpack, |
| 241 | const.OFPST_TABLE : table_stats_request.unpack, |
| 242 | const.OFPST_PORT : port_stats_request.unpack, |
| 243 | const.OFPST_QUEUE : queue_stats_request.unpack, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 244 | :: if version < of_g.VERSION_1_1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 245 | const.OFPST_VENDOR : experimenter_stats_request.unpack, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 246 | :: else: |
| 247 | const.OFPST_EXPERIMENTER : experimenter_stats_request.unpack, |
| 248 | :: #endif |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 249 | :: if version >= of_g.VERSION_1_1: |
| 250 | const.OFPST_GROUP : group_stats_request.unpack, |
| 251 | const.OFPST_GROUP_DESC : group_desc_stats_request.unpack, |
| 252 | :: #endif |
| 253 | :: if version >= of_g.VERSION_1_2: |
| 254 | const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack, |
| 255 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 256 | } |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 257 | :: else: |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 258 | # TODO OF 1.3 multipart messages |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 259 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 260 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 261 | :: experimenter_ofclasses = [x for x in ofclasses if x.type_members[1].value == 4] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 262 | :: sort_key = lambda x: x.type_members[2].value |
| 263 | :: experimenter_ofclasses.sort(key=sort_key) |
| 264 | :: grouped = itertools.groupby(experimenter_ofclasses, sort_key) |
| 265 | experimenter_parsers = { |
| 266 | :: for (experimenter, v) in grouped: |
| 267 | ${experimenter} : { |
| 268 | :: for ofclass in v: |
| 269 | ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack, |
| 270 | :: #endfor |
| 271 | }, |
| 272 | :: #endfor |
| 273 | } |