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 | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 31 | :: import py_gen.oftype |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 32 | :: include('_copyright.py') |
| 33 | |
| 34 | :: include('_autogen.py') |
| 35 | |
| 36 | import struct |
| 37 | import loxi |
| 38 | import const |
| 39 | import common |
| 40 | import action # for unpack_list |
Rich Lane | ed4f906 | 2013-05-02 17:05:03 -0700 | [diff] [blame] | 41 | :: if version >= 2: |
| 42 | import instruction # for unpack_list |
| 43 | :: #endif |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 44 | :: if version >= 4: |
| 45 | import meter_band # for unpack_list |
| 46 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 47 | import util |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 48 | import loxi.generic_util |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 49 | |
| 50 | class Message(object): |
| 51 | version = const.OFP_VERSION |
| 52 | type = None # override in subclass |
| 53 | xid = None |
| 54 | |
| 55 | :: for ofclass in ofclasses: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 56 | :: from loxi_ir import * |
| 57 | :: normal_members = [m for m in ofclass.members if type(m) == OFDataMember] |
| 58 | :: type_members = [m for m in ofclass.members if type(m) == OFTypeMember] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 59 | class ${ofclass.pyname}(Message): |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 60 | :: for m in type_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 61 | ${m.name} = ${m.value} |
| 62 | :: #endfor |
| 63 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 64 | 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] | 65 | self.xid = xid |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 66 | :: 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] | 67 | if ${m.name} != None: |
| 68 | self.${m.name} = ${m.name} |
| 69 | else: |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 70 | self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype)} |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 71 | :: #endfor |
| 72 | |
| 73 | def pack(self): |
| 74 | packed = [] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 75 | :: include('_pack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 76 | return ''.join(packed) |
| 77 | |
| 78 | @staticmethod |
| 79 | def unpack(buf): |
| 80 | if len(buf) < 8: raise loxi.ProtocolError("buffer too short to contain an OpenFlow message") |
| 81 | obj = ${ofclass.pyname}() |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 82 | :: include('_unpack.py', ofclass=ofclass) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 83 | return obj |
| 84 | |
| 85 | def __eq__(self, other): |
| 86 | if type(self) != type(other): return False |
| 87 | if self.version != other.version: return False |
| 88 | if self.type != other.type: return False |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 89 | :: for m in normal_members: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 90 | if self.${m.name} != other.${m.name}: return False |
| 91 | :: #endfor |
| 92 | return True |
| 93 | |
| 94 | def __ne__(self, other): |
| 95 | return not self.__eq__(other) |
| 96 | |
| 97 | def __str__(self): |
| 98 | return self.show() |
| 99 | |
| 100 | def show(self): |
| 101 | import loxi.pp |
| 102 | return loxi.pp.pp(self) |
| 103 | |
| 104 | def pretty_print(self, q): |
| 105 | :: include('_pretty_print.py', ofclass=ofclass) |
| 106 | |
| 107 | :: #endfor |
| 108 | |
| 109 | def parse_header(buf): |
| 110 | if len(buf) < 8: |
| 111 | raise loxi.ProtocolError("too short to be an OpenFlow message") |
| 112 | return struct.unpack_from("!BBHL", buf) |
| 113 | |
| 114 | def parse_message(buf): |
| 115 | msg_ver, msg_type, msg_len, msg_xid = parse_header(buf) |
Rich Lane | 1c5db11 | 2013-06-14 00:00:54 -0700 | [diff] [blame] | 116 | if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO: |
| 117 | raise loxi.ProtocolError("wrong OpenFlow version (expected %d, got %d)" % (const.OFP_VERSION, msg_ver)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 118 | if len(buf) != msg_len: |
| 119 | raise loxi.ProtocolError("incorrect message size") |
| 120 | if msg_type in parsers: |
| 121 | return parsers[msg_type](buf) |
| 122 | else: |
| 123 | raise loxi.ProtocolError("unexpected message type") |
| 124 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 125 | def parse_flow_mod(buf): |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 126 | :: if version == 1: |
| 127 | :: offset = 57 |
| 128 | :: elif version >= 2: |
| 129 | :: offset = 25 |
| 130 | :: #endif |
| 131 | if len(buf) < ${offset} + 1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 132 | raise loxi.ProtocolError("message too short") |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 133 | # Technically uint16_t for OF 1.0 |
| 134 | cmd, = struct.unpack_from("!B", buf, ${offset}) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | if cmd in flow_mod_parsers: |
| 136 | return flow_mod_parsers[cmd](buf) |
| 137 | else: |
| 138 | raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd) |
| 139 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 140 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 141 | def parse_stats_reply(buf): |
| 142 | if len(buf) < 8 + 2: |
| 143 | raise loxi.ProtocolError("message too short") |
| 144 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 145 | if stats_type in stats_reply_parsers: |
| 146 | return stats_reply_parsers[stats_type](buf) |
| 147 | else: |
| 148 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
| 149 | |
| 150 | def parse_stats_request(buf): |
| 151 | if len(buf) < 8 + 2: |
| 152 | raise loxi.ProtocolError("message too short") |
| 153 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 154 | if stats_type in stats_request_parsers: |
| 155 | return stats_request_parsers[stats_type](buf) |
| 156 | else: |
| 157 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 158 | :: else: |
| 159 | def parse_multipart_reply(buf): |
| 160 | if len(buf) < 8 + 2: |
| 161 | raise loxi.ProtocolError("message too short") |
| 162 | multipart_type, = struct.unpack_from("!H", buf, 8) |
| 163 | if multipart_type in multipart_reply_parsers: |
| 164 | return multipart_reply_parsers[multipart_type](buf) |
| 165 | else: |
| 166 | raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 167 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 168 | def parse_multipart_request(buf): |
| 169 | if len(buf) < 8 + 2: |
| 170 | raise loxi.ProtocolError("message too short") |
| 171 | multipart_type, = struct.unpack_from("!H", buf, 8) |
| 172 | if multipart_type in multipart_request_parsers: |
| 173 | return multipart_request_parsers[multipart_type](buf) |
| 174 | else: |
| 175 | raise loxi.ProtocolError("unexpected multipart type %u" % multipart_type) |
| 176 | :: #endif |
| 177 | |
| 178 | :: if version == of_g.VERSION_1_0: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 179 | def parse_vendor(buf): |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 180 | :: else: |
| 181 | def parse_experimenter(buf): |
| 182 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 183 | if len(buf) < 16: |
| 184 | raise loxi.ProtocolError("experimenter message too short") |
| 185 | |
| 186 | experimenter, = struct.unpack_from("!L", buf, 8) |
| 187 | if experimenter == 0x005c16c7: # Big Switch Networks |
| 188 | subtype, = struct.unpack_from("!L", buf, 12) |
| 189 | elif experimenter == 0x00002320: # Nicira |
| 190 | subtype, = struct.unpack_from("!L", buf, 12) |
| 191 | else: |
| 192 | raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter) |
| 193 | |
| 194 | if subtype in experimenter_parsers[experimenter]: |
| 195 | return experimenter_parsers[experimenter][subtype](buf) |
| 196 | else: |
| 197 | raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype)) |
| 198 | |
| 199 | parsers = { |
| 200 | :: sort_key = lambda x: x.type_members[1].value |
| 201 | :: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key) |
| 202 | :: for (k, v) in msgtype_groups: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 203 | :: k = util.constant_for_value(version, "ofp_type", k) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 204 | :: v = list(v) |
| 205 | :: if len(v) == 1: |
| 206 | ${k} : ${v[0].pyname}.unpack, |
| 207 | :: else: |
| 208 | ${k} : parse_${k[11:].lower()}, |
| 209 | :: #endif |
| 210 | :: #endfor |
| 211 | } |
| 212 | |
| 213 | flow_mod_parsers = { |
| 214 | const.OFPFC_ADD : flow_add.unpack, |
| 215 | const.OFPFC_MODIFY : flow_modify.unpack, |
| 216 | const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack, |
| 217 | const.OFPFC_DELETE : flow_delete.unpack, |
| 218 | const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack, |
| 219 | } |
| 220 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 221 | :: if version < of_g.VERSION_1_3: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 222 | stats_reply_parsers = { |
| 223 | const.OFPST_DESC : desc_stats_reply.unpack, |
| 224 | const.OFPST_FLOW : flow_stats_reply.unpack, |
| 225 | const.OFPST_AGGREGATE : aggregate_stats_reply.unpack, |
| 226 | const.OFPST_TABLE : table_stats_reply.unpack, |
| 227 | const.OFPST_PORT : port_stats_reply.unpack, |
| 228 | const.OFPST_QUEUE : queue_stats_reply.unpack, |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 229 | :: if version >= of_g.VERSION_1_1: |
| 230 | const.OFPST_GROUP : group_stats_reply.unpack, |
| 231 | const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack, |
| 232 | :: #endif |
| 233 | :: if version >= of_g.VERSION_1_2: |
| 234 | const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack, |
| 235 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | stats_request_parsers = { |
| 239 | const.OFPST_DESC : desc_stats_request.unpack, |
| 240 | const.OFPST_FLOW : flow_stats_request.unpack, |
| 241 | const.OFPST_AGGREGATE : aggregate_stats_request.unpack, |
| 242 | const.OFPST_TABLE : table_stats_request.unpack, |
| 243 | const.OFPST_PORT : port_stats_request.unpack, |
| 244 | const.OFPST_QUEUE : queue_stats_request.unpack, |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 245 | :: if version >= of_g.VERSION_1_1: |
| 246 | const.OFPST_GROUP : group_stats_request.unpack, |
| 247 | const.OFPST_GROUP_DESC : group_desc_stats_request.unpack, |
| 248 | :: #endif |
| 249 | :: if version >= of_g.VERSION_1_2: |
| 250 | const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack, |
| 251 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 252 | } |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 253 | :: else: |
Rich Lane | 7b9c949 | 2013-06-14 07:43:22 -0700 | [diff] [blame] | 254 | multipart_reply_parsers = { |
| 255 | const.OFPMP_DESC : desc_stats_reply.unpack, |
| 256 | const.OFPMP_FLOW : flow_stats_reply.unpack, |
| 257 | const.OFPMP_AGGREGATE : aggregate_stats_reply.unpack, |
| 258 | const.OFPMP_TABLE : table_stats_reply.unpack, |
| 259 | const.OFPMP_PORT_STATS : port_stats_reply.unpack, |
| 260 | const.OFPMP_QUEUE : queue_stats_reply.unpack, |
| 261 | const.OFPMP_GROUP : group_stats_reply.unpack, |
| 262 | const.OFPMP_GROUP_DESC : group_desc_stats_reply.unpack, |
| 263 | const.OFPMP_GROUP_FEATURES : group_features_stats_reply.unpack, |
| 264 | const.OFPMP_METER : meter_stats_reply.unpack, |
| 265 | const.OFPMP_METER_CONFIG : meter_config_stats_reply.unpack, |
| 266 | const.OFPMP_METER_FEATURES : meter_features_stats_reply.unpack, |
| 267 | const.OFPMP_TABLE_FEATURES : table_features_stats_reply.unpack, |
| 268 | const.OFPMP_PORT_DESC : port_desc_stats_reply.unpack, |
| 269 | } |
| 270 | |
| 271 | multipart_request_parsers = { |
| 272 | const.OFPMP_DESC : desc_stats_request.unpack, |
| 273 | const.OFPMP_FLOW : flow_stats_request.unpack, |
| 274 | const.OFPMP_AGGREGATE : aggregate_stats_request.unpack, |
| 275 | const.OFPMP_TABLE : table_stats_request.unpack, |
| 276 | const.OFPMP_PORT_STATS : port_stats_request.unpack, |
| 277 | const.OFPMP_QUEUE : queue_stats_request.unpack, |
| 278 | const.OFPMP_GROUP : group_stats_request.unpack, |
| 279 | const.OFPMP_GROUP_DESC : group_desc_stats_request.unpack, |
| 280 | const.OFPMP_GROUP_FEATURES : group_features_stats_request.unpack, |
| 281 | const.OFPMP_METER : meter_stats_request.unpack, |
| 282 | const.OFPMP_METER_CONFIG : meter_config_stats_request.unpack, |
| 283 | const.OFPMP_METER_FEATURES : meter_features_stats_request.unpack, |
| 284 | const.OFPMP_TABLE_FEATURES : table_features_stats_request.unpack, |
| 285 | const.OFPMP_PORT_DESC : port_desc_stats_request.unpack, |
| 286 | } |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 287 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 288 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 289 | :: 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] | 290 | :: sort_key = lambda x: x.type_members[2].value |
| 291 | :: experimenter_ofclasses.sort(key=sort_key) |
| 292 | :: grouped = itertools.groupby(experimenter_ofclasses, sort_key) |
| 293 | experimenter_parsers = { |
| 294 | :: for (experimenter, v) in grouped: |
| 295 | ${experimenter} : { |
| 296 | :: for ofclass in v: |
| 297 | ${ofclass.type_members[3].value}: ${ofclass.pyname}.unpack, |
| 298 | :: #endfor |
| 299 | }, |
| 300 | :: #endfor |
| 301 | } |