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 | :: include('_copyright.py') |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 29 | :: from loxi_globals import OFVersions |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 30 | :: include('_autogen.py') |
| 31 | |
Rich Lane | d50d971 | 2013-11-29 19:40:28 -0800 | [diff] [blame] | 32 | import struct |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 33 | import loxi |
| 34 | import const |
Rich Lane | d50d971 | 2013-11-29 19:40:28 -0800 | [diff] [blame] | 35 | import common |
| 36 | import action |
| 37 | :: if version >= OFVersions.VERSION_1_1: |
| 38 | import instruction |
| 39 | :: #endif |
| 40 | :: if version >= OFVersions.VERSION_1_2: |
| 41 | import oxm |
| 42 | :: #endif |
| 43 | :: if version >= OFVersions.VERSION_1_3: |
Rich Lane | 2c9938e | 2013-12-09 17:20:12 -0800 | [diff] [blame] | 44 | import action_id |
| 45 | import instruction_id |
Rich Lane | d50d971 | 2013-11-29 19:40:28 -0800 | [diff] [blame] | 46 | import meter_band |
| 47 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 48 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 49 | def pretty_mac(mac): |
| 50 | return ':'.join(["%02x" % x for x in mac]) |
| 51 | |
| 52 | def pretty_ipv4(v): |
| 53 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 54 | |
| 55 | def pretty_flags(v, flag_names): |
| 56 | set_flags = [] |
| 57 | for flag_name in flag_names: |
| 58 | flag_value = getattr(const, flag_name) |
| 59 | if v & flag_value == flag_value: |
| 60 | set_flags.append(flag_name) |
| 61 | elif v & flag_value: |
| 62 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 63 | v &= ~flag_value |
| 64 | if v: |
| 65 | set_flags.append("%#x" % v) |
| 66 | return '|'.join(set_flags) or '0' |
| 67 | |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 68 | :: if version in (OFVersions.VERSION_1_0, OFVersions.VERSION_1_1): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 69 | def pretty_wildcards(v): |
| 70 | if v == const.OFPFW_ALL: |
| 71 | return 'OFPFW_ALL' |
| 72 | flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 73 | 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 74 | 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 75 | 'OFPFW_NW_TOS'] |
| 76 | return pretty_flags(v, flag_names) |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 77 | :: #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 78 | |
| 79 | def pretty_port(v): |
| 80 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 81 | for (k, v2) in named_ports: |
| 82 | if v == v2: |
| 83 | return k |
| 84 | return v |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 85 | |
| 86 | def pack_port_no(value): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 87 | :: if version == OFVersions.VERSION_1_0: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 88 | return struct.pack("!H", value) |
| 89 | :: else: |
| 90 | return struct.pack("!L", value) |
| 91 | :: #endif |
| 92 | |
| 93 | def unpack_port_no(reader): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 94 | :: if version == OFVersions.VERSION_1_0: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 95 | return reader.read("!H")[0] |
| 96 | :: else: |
| 97 | return reader.read("!L")[0] |
| 98 | :: #endif |
| 99 | |
| 100 | def pack_fm_cmd(value): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 101 | :: if version == OFVersions.VERSION_1_0: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 102 | return struct.pack("!H", value) |
| 103 | :: else: |
| 104 | return struct.pack("!B", value) |
| 105 | :: #endif |
| 106 | |
| 107 | def unpack_fm_cmd(reader): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 108 | :: if version == OFVersions.VERSION_1_0: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 109 | return reader.read("!H")[0] |
| 110 | :: else: |
| 111 | return reader.read("!B")[0] |
| 112 | :: #endif |
| 113 | |
| 114 | def init_wc_bmap(): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 115 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 116 | return const.OFPFW_ALL |
| 117 | :: else: |
| 118 | return 0 |
| 119 | :: #endif |
| 120 | |
| 121 | def pack_wc_bmap(value): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 122 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 123 | return struct.pack("!L", value) |
| 124 | :: else: |
| 125 | return struct.pack("!Q", value) |
| 126 | :: #endif |
| 127 | |
| 128 | def unpack_wc_bmap(reader): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 129 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 130 | return reader.read("!L")[0] |
| 131 | :: else: |
| 132 | return reader.read("!Q")[0] |
| 133 | :: #endif |
| 134 | |
| 135 | def init_match_bmap(): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 136 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 137 | return const.OFPFW_ALL |
| 138 | :: else: |
| 139 | return 0 |
| 140 | :: #endif |
| 141 | |
| 142 | def pack_match_bmap(value): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 143 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 144 | return struct.pack("!L", value) |
| 145 | :: else: |
| 146 | return struct.pack("!Q", value) |
| 147 | :: #endif |
| 148 | |
| 149 | def unpack_match_bmap(reader): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 150 | :: if version <= OFVersions.VERSION_1_1: |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 151 | return reader.read("!L")[0] |
| 152 | :: else: |
| 153 | return reader.read("!Q")[0] |
| 154 | :: #endif |
| 155 | |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 156 | MASK64 = (1 << 64) - 1 |
| 157 | |
| 158 | def pack_bitmap_128(value): |
| 159 | x = 0l |
| 160 | for y in value: |
| 161 | x |= 1 << y |
| 162 | return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64) |
| 163 | |
| 164 | def unpack_bitmap_128(reader): |
| 165 | hi, lo = reader.read("!QQ") |
| 166 | x = (hi << 64) | lo |
| 167 | i = 0 |
| 168 | value = set() |
| 169 | while x != 0: |
| 170 | if x & 1 == 1: |
| 171 | value.add(i) |
| 172 | i += 1 |
| 173 | x >>= 1 |
| 174 | return value |
Rich Lane | d50d971 | 2013-11-29 19:40:28 -0800 | [diff] [blame] | 175 | |
Rich Lane | 1adf421 | 2013-12-03 12:59:21 -0800 | [diff] [blame] | 176 | def pack_checksum_128(value): |
| 177 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 178 | |
| 179 | def unpack_checksum_128(reader): |
| 180 | hi, lo = reader.read("!QQ") |
| 181 | return (hi << 64) | lo |