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