Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 17 | # Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University |
| 18 | # Copyright (c) 2011, 2012 Open Networking Foundation |
| 19 | # Copyright (c) 2012, 2013 Big Switch Networks, Inc. |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 20 | # See the file LICENSE.pyloxi which should have been included in the source distribution |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 21 | # Automatically generated by LOXI from template util.py |
| 22 | # Do not modify |
| 23 | |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 24 | import struct |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 25 | import loxi |
| 26 | import const |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 27 | import common |
| 28 | import action |
| 29 | import instruction |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 30 | |
| 31 | def pretty_mac(mac): |
| 32 | return ':'.join(["%02x" % x for x in mac]) |
| 33 | |
| 34 | def pretty_ipv4(v): |
| 35 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 36 | |
| 37 | def pretty_flags(v, flag_names): |
| 38 | set_flags = [] |
| 39 | for flag_name in flag_names: |
| 40 | flag_value = getattr(const, flag_name) |
| 41 | if v & flag_value == flag_value: |
| 42 | set_flags.append(flag_name) |
| 43 | elif v & flag_value: |
| 44 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 45 | v &= ~flag_value |
| 46 | if v: |
| 47 | set_flags.append("%#x" % v) |
| 48 | return '|'.join(set_flags) or '0' |
| 49 | |
| 50 | def pretty_wildcards(v): |
| 51 | if v == const.OFPFW_ALL: |
| 52 | return 'OFPFW_ALL' |
| 53 | flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 54 | 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 55 | 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 56 | 'OFPFW_NW_TOS'] |
| 57 | return pretty_flags(v, flag_names) |
| 58 | |
| 59 | def pretty_port(v): |
| 60 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 61 | for (k, v2) in named_ports: |
| 62 | if v == v2: |
| 63 | return k |
| 64 | return v |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 65 | |
| 66 | def pack_port_no(value): |
| 67 | return struct.pack("!L", value) |
| 68 | |
| 69 | def unpack_port_no(reader): |
| 70 | return reader.read("!L")[0] |
| 71 | |
| 72 | def pack_fm_cmd(value): |
| 73 | return struct.pack("!B", value) |
| 74 | |
| 75 | def unpack_fm_cmd(reader): |
| 76 | return reader.read("!B")[0] |
| 77 | |
| 78 | def init_wc_bmap(): |
| 79 | return const.OFPFW_ALL |
| 80 | |
| 81 | def pack_wc_bmap(value): |
| 82 | return struct.pack("!L", value) |
| 83 | |
| 84 | def unpack_wc_bmap(reader): |
| 85 | return reader.read("!L")[0] |
| 86 | |
| 87 | def init_match_bmap(): |
| 88 | return const.OFPFW_ALL |
| 89 | |
| 90 | def pack_match_bmap(value): |
| 91 | return struct.pack("!L", value) |
| 92 | |
| 93 | def unpack_match_bmap(reader): |
| 94 | return reader.read("!L")[0] |
| 95 | |
Rich Lane | 6f4978c | 2013-10-20 21:33:52 -0700 | [diff] [blame] | 96 | MASK64 = (1 << 64) - 1 |
| 97 | |
| 98 | def pack_bitmap_128(value): |
| 99 | x = 0l |
| 100 | for y in value: |
| 101 | x |= 1 << y |
| 102 | return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64) |
| 103 | |
| 104 | def unpack_bitmap_128(reader): |
| 105 | hi, lo = reader.read("!QQ") |
| 106 | x = (hi << 64) | lo |
| 107 | i = 0 |
| 108 | value = set() |
| 109 | while x != 0: |
| 110 | if x & 1 == 1: |
| 111 | value.add(i) |
| 112 | i += 1 |
| 113 | x >>= 1 |
| 114 | return value |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 115 | |
Rich Lane | 474324f | 2015-01-14 15:22:56 -0800 | [diff] [blame] | 116 | def pack_bitmap_512(value): |
| 117 | words = [0] * 8 |
| 118 | for v in value: |
| 119 | assert v < 512 |
| 120 | words[7-v/64] |= 1 << (v % 64) |
| 121 | return struct.pack("!8Q", *words) |
| 122 | |
| 123 | def unpack_bitmap_512(reader): |
| 124 | words = reader.read("!8Q") |
| 125 | x = 0l |
| 126 | for word in words: |
| 127 | x <<= 64 |
| 128 | x |= word |
| 129 | i = 0 |
| 130 | value = set() |
| 131 | while x != 0: |
| 132 | if x & 1 == 1: |
| 133 | value.add(i) |
| 134 | i += 1 |
| 135 | x >>= 1 |
| 136 | return value |
| 137 | |
Rich Lane | 5454b68 | 2014-01-14 17:07:36 -0800 | [diff] [blame] | 138 | def pack_checksum_128(value): |
| 139 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 140 | |
| 141 | def unpack_checksum_128(reader): |
| 142 | hi, lo = reader.read("!QQ") |
| 143 | return (hi << 64) | lo |