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