Rich Lane | 2e079da | 2014-10-29 15:30:24 -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. |
| 4 | # See the file LICENSE.pyloxi which should have been included in the source distribution |
| 5 | # Automatically generated by LOXI from template util.py |
| 6 | # Do not modify |
| 7 | |
| 8 | import struct |
| 9 | import loxi |
| 10 | import const |
| 11 | import common |
| 12 | import action |
| 13 | import instruction |
| 14 | import oxm |
| 15 | import action_id |
| 16 | import instruction_id |
| 17 | import meter_band |
| 18 | |
| 19 | def pretty_mac(mac): |
| 20 | return ':'.join(["%02x" % x for x in mac]) |
| 21 | |
| 22 | def pretty_ipv4(v): |
| 23 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 24 | |
| 25 | def pretty_flags(v, flag_names): |
| 26 | set_flags = [] |
| 27 | for flag_name in flag_names: |
| 28 | flag_value = getattr(const, flag_name) |
| 29 | if v & flag_value == flag_value: |
| 30 | set_flags.append(flag_name) |
| 31 | elif v & flag_value: |
| 32 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 33 | v &= ~flag_value |
| 34 | if v: |
| 35 | set_flags.append("%#x" % v) |
| 36 | return '|'.join(set_flags) or '0' |
| 37 | |
| 38 | |
| 39 | def pretty_port(v): |
| 40 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 41 | for (k, v2) in named_ports: |
| 42 | if v == v2: |
| 43 | return k |
| 44 | return v |
| 45 | |
| 46 | def pack_port_no(value): |
| 47 | return struct.pack("!L", value) |
| 48 | |
| 49 | def unpack_port_no(reader): |
| 50 | return reader.read("!L")[0] |
| 51 | |
| 52 | def pack_fm_cmd(value): |
| 53 | return struct.pack("!B", value) |
| 54 | |
| 55 | def unpack_fm_cmd(reader): |
| 56 | return reader.read("!B")[0] |
| 57 | |
| 58 | def init_wc_bmap(): |
| 59 | return 0 |
| 60 | |
| 61 | def pack_wc_bmap(value): |
| 62 | return struct.pack("!Q", value) |
| 63 | |
| 64 | def unpack_wc_bmap(reader): |
| 65 | return reader.read("!Q")[0] |
| 66 | |
| 67 | def init_match_bmap(): |
| 68 | return 0 |
| 69 | |
| 70 | def pack_match_bmap(value): |
| 71 | return struct.pack("!Q", value) |
| 72 | |
| 73 | def unpack_match_bmap(reader): |
| 74 | return reader.read("!Q")[0] |
| 75 | |
| 76 | MASK64 = (1 << 64) - 1 |
| 77 | |
| 78 | def pack_bitmap_128(value): |
| 79 | x = 0l |
| 80 | for y in value: |
| 81 | x |= 1 << y |
| 82 | return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64) |
| 83 | |
| 84 | def unpack_bitmap_128(reader): |
| 85 | hi, lo = reader.read("!QQ") |
| 86 | x = (hi << 64) | lo |
| 87 | i = 0 |
| 88 | value = set() |
| 89 | while x != 0: |
| 90 | if x & 1 == 1: |
| 91 | value.add(i) |
| 92 | i += 1 |
| 93 | x >>= 1 |
| 94 | return value |
| 95 | |
Rich Lane | 474324f | 2015-01-14 15:22:56 -0800 | [diff] [blame] | 96 | def pack_bitmap_512(value): |
| 97 | words = [0] * 8 |
| 98 | for v in value: |
| 99 | assert v < 512 |
| 100 | words[7-v/64] |= 1 << (v % 64) |
| 101 | return struct.pack("!8Q", *words) |
| 102 | |
| 103 | def unpack_bitmap_512(reader): |
| 104 | words = reader.read("!8Q") |
| 105 | x = 0l |
| 106 | for word in words: |
| 107 | x <<= 64 |
| 108 | x |= word |
| 109 | i = 0 |
| 110 | value = set() |
| 111 | while x != 0: |
| 112 | if x & 1 == 1: |
| 113 | value.add(i) |
| 114 | i += 1 |
| 115 | x >>= 1 |
| 116 | return value |
| 117 | |
Rich Lane | 2e079da | 2014-10-29 15:30:24 -0700 | [diff] [blame] | 118 | def pack_checksum_128(value): |
| 119 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 120 | |
| 121 | def unpack_checksum_128(reader): |
| 122 | hi, lo = reader.read("!QQ") |
| 123 | return (hi << 64) | lo |