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 |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 29 | :: from loxi_globals import OFVersions |
| 30 | :: import loxi_globals |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 31 | :: import py_gen.util as util |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 32 | :: import py_gen.oftype |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 33 | :: include('_copyright.py') |
| 34 | |
| 35 | :: include('_autogen.py') |
| 36 | |
| 37 | import struct |
| 38 | import loxi |
| 39 | import const |
| 40 | import common |
| 41 | import action # for unpack_list |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 42 | :: if version >= OFVersions.VERSION_1_1: |
Rich Lane | ed4f906 | 2013-05-02 17:05:03 -0700 | [diff] [blame] | 43 | import instruction # for unpack_list |
| 44 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 45 | :: if version >= OFVersions.VERSION_1_3: |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 46 | import meter_band # for unpack_list |
| 47 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 48 | import util |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 49 | import loxi.generic_util |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 50 | |
| 51 | class Message(object): |
| 52 | version = const.OFP_VERSION |
| 53 | type = None # override in subclass |
| 54 | xid = None |
| 55 | |
| 56 | :: for ofclass in ofclasses: |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 57 | :: include('_ofclass.py', ofclass=ofclass, superclass="Message") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | |
| 59 | :: #endfor |
| 60 | |
| 61 | def parse_header(buf): |
| 62 | if len(buf) < 8: |
| 63 | raise loxi.ProtocolError("too short to be an OpenFlow message") |
| 64 | return struct.unpack_from("!BBHL", buf) |
| 65 | |
| 66 | def parse_message(buf): |
| 67 | msg_ver, msg_type, msg_len, msg_xid = parse_header(buf) |
Rich Lane | 1c5db11 | 2013-06-14 00:00:54 -0700 | [diff] [blame] | 68 | if msg_ver != const.OFP_VERSION and msg_type != const.OFPT_HELLO: |
| 69 | 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] | 70 | if len(buf) != msg_len: |
| 71 | raise loxi.ProtocolError("incorrect message size") |
| 72 | if msg_type in parsers: |
| 73 | return parsers[msg_type](buf) |
| 74 | else: |
| 75 | raise loxi.ProtocolError("unexpected message type") |
| 76 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 77 | def parse_error(buf): |
| 78 | if len(buf) < 8 + 2: |
| 79 | raise loxi.ProtocolError("message too short") |
| 80 | err_type, = struct.unpack_from("!H", buf, 8) |
| 81 | if err_type in error_msg_parsers: |
| 82 | return error_msg_parsers[err_type](buf) |
| 83 | else: |
Rob Vaterlaus | b5e8c83 | 2013-10-04 12:50:23 -0700 | [diff] [blame] | 84 | raise loxi.ProtocolError("unexpected error type %u" % err_type) |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 85 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 86 | def parse_flow_mod(buf): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 87 | :: if version == OFVersions.VERSION_1_0: |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 88 | :: offset = 57 |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 89 | :: elif version >= OFVersions.VERSION_1_1: |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 90 | :: offset = 25 |
| 91 | :: #endif |
| 92 | if len(buf) < ${offset} + 1: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 93 | raise loxi.ProtocolError("message too short") |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 94 | # Technically uint16_t for OF 1.0 |
| 95 | cmd, = struct.unpack_from("!B", buf, ${offset}) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 96 | if cmd in flow_mod_parsers: |
| 97 | return flow_mod_parsers[cmd](buf) |
| 98 | else: |
| 99 | raise loxi.ProtocolError("unexpected flow mod cmd %u" % cmd) |
| 100 | |
Andreas Wundsam | 5812cf3 | 2013-11-15 13:51:24 -0800 | [diff] [blame] | 101 | :: if version >= OFVersions.VERSION_1_0: |
| 102 | def parse_group_mod(buf): |
| 103 | :: offset = 8 |
| 104 | if len(buf) < ${offset} + 2: |
| 105 | raise loxi.ProtocolError("message too short") |
| 106 | cmd, = struct.unpack_from("!H", buf, ${offset}) |
| 107 | if cmd in flow_mod_parsers: |
| 108 | return group_mod_parsers[cmd](buf) |
| 109 | else: |
| 110 | raise loxi.ProtocolError("unexpected group mod cmd %u" % cmd) |
| 111 | :: #endif |
| 112 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 113 | def parse_stats_reply(buf): |
| 114 | if len(buf) < 8 + 2: |
| 115 | raise loxi.ProtocolError("message too short") |
| 116 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 117 | if stats_type in stats_reply_parsers: |
| 118 | return stats_reply_parsers[stats_type](buf) |
| 119 | else: |
| 120 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
| 121 | |
| 122 | def parse_stats_request(buf): |
| 123 | if len(buf) < 8 + 2: |
| 124 | raise loxi.ProtocolError("message too short") |
| 125 | stats_type, = struct.unpack_from("!H", buf, 8) |
| 126 | if stats_type in stats_request_parsers: |
| 127 | return stats_request_parsers[stats_type](buf) |
| 128 | else: |
| 129 | raise loxi.ProtocolError("unexpected stats type %u" % stats_type) |
| 130 | |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 131 | def parse_experimenter_stats_request(buf): |
| 132 | if len(buf) < 24: |
| 133 | raise loxi.ProtocolError("experimenter stats request message too short") |
| 134 | |
| 135 | experimenter, exp_type = struct.unpack_from("!LL", buf, 16) |
| 136 | |
| 137 | if experimenter in experimenter_stats_request_parsers and \ |
| 138 | exp_type in experimenter_stats_request_parsers[experimenter]: |
| 139 | return experimenter_stats_request_parsers[experimenter][exp_type](buf) |
| 140 | else: |
| 141 | raise loxi.ProtocolError("unexpected stats request experimenter %#x exp_type %#x" % (experimenter, exp_type)) |
| 142 | |
| 143 | def parse_experimenter_stats_reply(buf): |
| 144 | if len(buf) < 24: |
| 145 | raise loxi.ProtocolError("experimenter stats reply message too short") |
| 146 | |
| 147 | experimenter, exp_type = struct.unpack_from("!LL", buf, 16) |
| 148 | |
| 149 | if experimenter in experimenter_stats_reply_parsers and \ |
| 150 | exp_type in experimenter_stats_reply_parsers[experimenter]: |
| 151 | return experimenter_stats_reply_parsers[experimenter][exp_type](buf) |
| 152 | else: |
| 153 | raise loxi.ProtocolError("unexpected stats reply experimenter %#x exp_type %#x" % (experimenter, exp_type)) |
| 154 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 155 | def parse_experimenter(buf): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 156 | if len(buf) < 16: |
| 157 | raise loxi.ProtocolError("experimenter message too short") |
| 158 | |
| 159 | experimenter, = struct.unpack_from("!L", buf, 8) |
| 160 | if experimenter == 0x005c16c7: # Big Switch Networks |
| 161 | subtype, = struct.unpack_from("!L", buf, 12) |
| 162 | elif experimenter == 0x00002320: # Nicira |
| 163 | subtype, = struct.unpack_from("!L", buf, 12) |
| 164 | else: |
| 165 | raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter) |
| 166 | |
| 167 | if subtype in experimenter_parsers[experimenter]: |
| 168 | return experimenter_parsers[experimenter][subtype](buf) |
| 169 | else: |
| 170 | raise loxi.ProtocolError("unexpected experimenter %#x subtype %#x" % (experimenter, subtype)) |
| 171 | |
| 172 | parsers = { |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame^] | 173 | :: sort_key = lambda x: x.member_by_name('type').value |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 174 | :: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key) |
| 175 | :: for (k, v) in msgtype_groups: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 176 | :: k = util.constant_for_value(version, "ofp_type", k) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 177 | :: v = list(v) |
| 178 | :: if len(v) == 1: |
| 179 | ${k} : ${v[0].pyname}.unpack, |
| 180 | :: else: |
| 181 | ${k} : parse_${k[11:].lower()}, |
| 182 | :: #endif |
| 183 | :: #endfor |
| 184 | } |
| 185 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 186 | error_msg_parsers = { |
| 187 | const.OFPET_HELLO_FAILED : hello_failed_error_msg.unpack, |
| 188 | const.OFPET_BAD_REQUEST : bad_request_error_msg.unpack, |
| 189 | const.OFPET_BAD_ACTION : bad_action_error_msg.unpack, |
| 190 | const.OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg.unpack, |
| 191 | const.OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg.unpack, |
| 192 | const.OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg.unpack, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 193 | :: if version >= OFVersions.VERSION_1_1: |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 194 | const.OFPET_BAD_INSTRUCTION : bad_instruction_error_msg.unpack, |
| 195 | const.OFPET_BAD_MATCH : bad_match_error_msg.unpack, |
| 196 | const.OFPET_GROUP_MOD_FAILED : group_mod_failed_error_msg.unpack, |
| 197 | const.OFPET_TABLE_MOD_FAILED : table_mod_failed_error_msg.unpack, |
| 198 | const.OFPET_SWITCH_CONFIG_FAILED : switch_config_failed_error_msg.unpack, |
| 199 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 200 | :: if version >= OFVersions.VERSION_1_2: |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 201 | const.OFPET_ROLE_REQUEST_FAILED : role_request_failed_error_msg.unpack, |
| 202 | const.OFPET_EXPERIMENTER : experimenter_error_msg.unpack, |
| 203 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 204 | :: if version >= OFVersions.VERSION_1_3: |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 205 | const.OFPET_METER_MOD_FAILED : meter_mod_failed_error_msg.unpack, |
| 206 | const.OFPET_TABLE_FEATURES_FAILED : table_features_failed_error_msg.unpack, |
| 207 | :: #endif |
| 208 | } |
| 209 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 210 | flow_mod_parsers = { |
| 211 | const.OFPFC_ADD : flow_add.unpack, |
| 212 | const.OFPFC_MODIFY : flow_modify.unpack, |
| 213 | const.OFPFC_MODIFY_STRICT : flow_modify_strict.unpack, |
| 214 | const.OFPFC_DELETE : flow_delete.unpack, |
| 215 | const.OFPFC_DELETE_STRICT : flow_delete_strict.unpack, |
| 216 | } |
| 217 | |
Andreas Wundsam | 5812cf3 | 2013-11-15 13:51:24 -0800 | [diff] [blame] | 218 | :: if version >= OFVersions.VERSION_1_1: |
| 219 | group_mod_parsers = { |
| 220 | const.OFPGC_ADD : group_add.unpack, |
| 221 | const.OFPGC_MODIFY : group_modify.unpack, |
| 222 | const.OFPGC_DELETE : group_delete.unpack, |
| 223 | } |
| 224 | :: #endif |
| 225 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 226 | stats_reply_parsers = { |
| 227 | const.OFPST_DESC : desc_stats_reply.unpack, |
| 228 | const.OFPST_FLOW : flow_stats_reply.unpack, |
| 229 | const.OFPST_AGGREGATE : aggregate_stats_reply.unpack, |
| 230 | const.OFPST_TABLE : table_stats_reply.unpack, |
| 231 | const.OFPST_PORT : port_stats_reply.unpack, |
| 232 | const.OFPST_QUEUE : queue_stats_reply.unpack, |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 233 | const.OFPST_EXPERIMENTER : parse_experimenter_stats_reply, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 234 | :: if version >= OFVersions.VERSION_1_1: |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 235 | const.OFPST_GROUP : group_stats_reply.unpack, |
| 236 | const.OFPST_GROUP_DESC : group_desc_stats_reply.unpack, |
| 237 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 238 | :: if version >= OFVersions.VERSION_1_2: |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 239 | const.OFPST_GROUP_FEATURES : group_features_stats_reply.unpack, |
| 240 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 241 | :: if version >= OFVersions.VERSION_1_3: |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 242 | const.OFPST_METER : meter_stats_reply.unpack, |
| 243 | const.OFPST_METER_CONFIG : meter_config_stats_reply.unpack, |
| 244 | const.OFPST_METER_FEATURES : meter_features_stats_reply.unpack, |
| 245 | const.OFPST_TABLE_FEATURES : table_features_stats_reply.unpack, |
| 246 | const.OFPST_PORT_DESC : port_desc_stats_reply.unpack, |
| 247 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | stats_request_parsers = { |
| 251 | const.OFPST_DESC : desc_stats_request.unpack, |
| 252 | const.OFPST_FLOW : flow_stats_request.unpack, |
| 253 | const.OFPST_AGGREGATE : aggregate_stats_request.unpack, |
| 254 | const.OFPST_TABLE : table_stats_request.unpack, |
| 255 | const.OFPST_PORT : port_stats_request.unpack, |
| 256 | const.OFPST_QUEUE : queue_stats_request.unpack, |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 257 | const.OFPST_EXPERIMENTER : parse_experimenter_stats_request, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 258 | :: if version >= OFVersions.VERSION_1_1: |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 259 | const.OFPST_GROUP : group_stats_request.unpack, |
| 260 | const.OFPST_GROUP_DESC : group_desc_stats_request.unpack, |
| 261 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 262 | :: if version >= OFVersions.VERSION_1_2: |
Rich Lane | 5edb555 | 2013-05-07 18:49:29 -0700 | [diff] [blame] | 263 | const.OFPST_GROUP_FEATURES : group_features_stats_request.unpack, |
| 264 | :: #endif |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 265 | :: if version >= OFVersions.VERSION_1_3: |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 266 | const.OFPST_METER : meter_stats_request.unpack, |
| 267 | const.OFPST_METER_CONFIG : meter_config_stats_request.unpack, |
| 268 | const.OFPST_METER_FEATURES : meter_features_stats_request.unpack, |
| 269 | const.OFPST_TABLE_FEATURES : table_features_stats_request.unpack, |
| 270 | const.OFPST_PORT_DESC : port_desc_stats_request.unpack, |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 271 | :: #endif |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 272 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 273 | |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame^] | 274 | :: experimenter_ofclasses = [x for x in ofclasses if x.member_by_name('type').value == 4] |
| 275 | :: sort_key = lambda x: x.member_by_name('experimenter').value |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 276 | :: experimenter_ofclasses.sort(key=sort_key) |
| 277 | :: grouped = itertools.groupby(experimenter_ofclasses, sort_key) |
| 278 | experimenter_parsers = { |
| 279 | :: for (experimenter, v) in grouped: |
| 280 | ${experimenter} : { |
| 281 | :: for ofclass in v: |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame^] | 282 | ${ofclass.member_by_name('subtype').value}: ${ofclass.pyname}.unpack, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 283 | :: #endfor |
| 284 | }, |
| 285 | :: #endfor |
| 286 | } |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 287 | |
| 288 | experimenter_stats_request_parsers = { |
| 289 | 0x005c16c7: { |
Andreas Wundsam | 2c0a2d7 | 2013-11-15 15:16:36 -0800 | [diff] [blame] | 290 | :: if version >= OFVersions.VERSION_1_3: |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 291 | 1: bsn_lacp_stats_request.unpack, |
| 292 | :: #endif |
| 293 | }, |
| 294 | } |
| 295 | |
| 296 | experimenter_stats_reply_parsers = { |
| 297 | 0x005c16c7: { |
Andreas Wundsam | 2c0a2d7 | 2013-11-15 15:16:36 -0800 | [diff] [blame] | 298 | :: if version >= OFVersions.VERSION_1_3: |
Rich Lane | d089bfa | 2013-11-13 10:40:40 -0800 | [diff] [blame] | 299 | 1: bsn_lacp_stats_reply.unpack, |
| 300 | :: #endif |
| 301 | }, |
| 302 | } |