Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 1 | # Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University |
| 2 | # Copyright (c) 2011, 2012 Open Networking Foundation |
| 3 | # Copyright (c) 2012, 2013 Big Switch Networks, Inc. |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 4 | # 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] | 5 | # Automatically generated by LOXI from template util.py |
| 6 | # Do not modify |
| 7 | |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 8 | import struct |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 9 | import loxi |
| 10 | import const |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 11 | import common |
| 12 | import action |
| 13 | import instruction |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 14 | |
| 15 | def pretty_mac(mac): |
| 16 | return ':'.join(["%02x" % x for x in mac]) |
| 17 | |
| 18 | def pretty_ipv4(v): |
| 19 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 20 | |
| 21 | def pretty_flags(v, flag_names): |
| 22 | set_flags = [] |
| 23 | for flag_name in flag_names: |
| 24 | flag_value = getattr(const, flag_name) |
| 25 | if v & flag_value == flag_value: |
| 26 | set_flags.append(flag_name) |
| 27 | elif v & flag_value: |
| 28 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 29 | v &= ~flag_value |
| 30 | if v: |
| 31 | set_flags.append("%#x" % v) |
| 32 | return '|'.join(set_flags) or '0' |
| 33 | |
| 34 | def pretty_wildcards(v): |
| 35 | if v == const.OFPFW_ALL: |
| 36 | return 'OFPFW_ALL' |
| 37 | flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 38 | 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 39 | 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 40 | 'OFPFW_NW_TOS'] |
| 41 | return pretty_flags(v, flag_names) |
| 42 | |
| 43 | def pretty_port(v): |
| 44 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 45 | for (k, v2) in named_ports: |
| 46 | if v == v2: |
| 47 | return k |
| 48 | return v |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 49 | |
| 50 | def pack_port_no(value): |
| 51 | return struct.pack("!L", value) |
| 52 | |
| 53 | def unpack_port_no(reader): |
| 54 | return reader.read("!L")[0] |
| 55 | |
| 56 | def pack_fm_cmd(value): |
| 57 | return struct.pack("!B", value) |
| 58 | |
| 59 | def unpack_fm_cmd(reader): |
| 60 | return reader.read("!B")[0] |
| 61 | |
| 62 | def init_wc_bmap(): |
| 63 | return const.OFPFW_ALL |
| 64 | |
| 65 | def pack_wc_bmap(value): |
| 66 | return struct.pack("!L", value) |
| 67 | |
| 68 | def unpack_wc_bmap(reader): |
| 69 | return reader.read("!L")[0] |
| 70 | |
| 71 | def init_match_bmap(): |
| 72 | return const.OFPFW_ALL |
| 73 | |
| 74 | def pack_match_bmap(value): |
| 75 | return struct.pack("!L", value) |
| 76 | |
| 77 | def unpack_match_bmap(reader): |
| 78 | return reader.read("!L")[0] |
| 79 | |
Rich Lane | 6f4978c | 2013-10-20 21:33:52 -0700 | [diff] [blame] | 80 | MASK64 = (1 << 64) - 1 |
| 81 | |
| 82 | def pack_bitmap_128(value): |
| 83 | x = 0l |
| 84 | for y in value: |
| 85 | x |= 1 << y |
| 86 | return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64) |
| 87 | |
| 88 | def unpack_bitmap_128(reader): |
| 89 | hi, lo = reader.read("!QQ") |
| 90 | x = (hi << 64) | lo |
| 91 | i = 0 |
| 92 | value = set() |
| 93 | while x != 0: |
| 94 | if x & 1 == 1: |
| 95 | value.add(i) |
| 96 | i += 1 |
| 97 | x >>= 1 |
| 98 | return value |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 99 | |
Rich Lane | 474324f | 2015-01-14 15:22:56 -0800 | [diff] [blame^] | 100 | def pack_bitmap_512(value): |
| 101 | words = [0] * 8 |
| 102 | for v in value: |
| 103 | assert v < 512 |
| 104 | words[7-v/64] |= 1 << (v % 64) |
| 105 | return struct.pack("!8Q", *words) |
| 106 | |
| 107 | def unpack_bitmap_512(reader): |
| 108 | words = reader.read("!8Q") |
| 109 | x = 0l |
| 110 | for word in words: |
| 111 | x <<= 64 |
| 112 | x |= word |
| 113 | i = 0 |
| 114 | value = set() |
| 115 | while x != 0: |
| 116 | if x & 1 == 1: |
| 117 | value.add(i) |
| 118 | i += 1 |
| 119 | x >>= 1 |
| 120 | return value |
| 121 | |
Rich Lane | 5454b68 | 2014-01-14 17:07:36 -0800 | [diff] [blame] | 122 | def pack_checksum_128(value): |
| 123 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 124 | |
| 125 | def unpack_checksum_128(reader): |
| 126 | hi, lo = reader.read("!QQ") |
| 127 | return (hi << 64) | lo |