Nathan Knuth | 418fdc8 | 2016-09-16 22:51:15 -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 | |
| 16 | def pretty_mac(mac): |
| 17 | return ':'.join(["%02x" % x for x in mac]) |
| 18 | |
| 19 | def pretty_ipv4(v): |
| 20 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 21 | |
| 22 | def pretty_flags(v, flag_names): |
| 23 | set_flags = [] |
| 24 | for flag_name in flag_names: |
| 25 | flag_value = getattr(const, flag_name) |
| 26 | if v & flag_value == flag_value: |
| 27 | set_flags.append(flag_name) |
| 28 | elif v & flag_value: |
| 29 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 30 | v &= ~flag_value |
| 31 | if v: |
| 32 | set_flags.append("%#x" % v) |
| 33 | return '|'.join(set_flags) or '0' |
| 34 | |
| 35 | |
| 36 | def pretty_port(v): |
| 37 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 38 | for (k, v2) in named_ports: |
| 39 | if v == v2: |
| 40 | return k |
| 41 | return v |
| 42 | |
| 43 | def pack_port_no(value): |
| 44 | return struct.pack("!L", value) |
| 45 | |
| 46 | def unpack_port_no(reader): |
| 47 | return reader.read("!L")[0] |
| 48 | |
| 49 | def pack_fm_cmd(value): |
| 50 | return struct.pack("!B", value) |
| 51 | |
| 52 | def unpack_fm_cmd(reader): |
| 53 | return reader.read("!B")[0] |
| 54 | |
| 55 | def init_wc_bmap(): |
| 56 | return 0 |
| 57 | |
| 58 | def pack_wc_bmap(value): |
| 59 | return struct.pack("!Q", value) |
| 60 | |
| 61 | def unpack_wc_bmap(reader): |
| 62 | return reader.read("!Q")[0] |
| 63 | |
| 64 | def init_match_bmap(): |
| 65 | return 0 |
| 66 | |
| 67 | def pack_match_bmap(value): |
| 68 | return struct.pack("!Q", value) |
| 69 | |
| 70 | def unpack_match_bmap(reader): |
| 71 | return reader.read("!Q")[0] |
| 72 | |
| 73 | MASK64 = (1 << 64) - 1 |
| 74 | |
| 75 | def pack_bitmap_128(value): |
| 76 | x = 0l |
| 77 | for y in value: |
| 78 | x |= 1 << y |
| 79 | return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64) |
| 80 | |
| 81 | def unpack_bitmap_128(reader): |
| 82 | hi, lo = reader.read("!QQ") |
| 83 | x = (hi << 64) | lo |
| 84 | i = 0 |
| 85 | value = set() |
| 86 | while x != 0: |
| 87 | if x & 1 == 1: |
| 88 | value.add(i) |
| 89 | i += 1 |
| 90 | x >>= 1 |
| 91 | return value |
| 92 | |
| 93 | def pack_bitmap_512(value): |
| 94 | words = [0] * 8 |
| 95 | for v in value: |
| 96 | assert v < 512 |
| 97 | words[7-v/64] |= 1 << (v % 64) |
| 98 | return struct.pack("!8Q", *words) |
| 99 | |
| 100 | def unpack_bitmap_512(reader): |
| 101 | words = reader.read("!8Q") |
| 102 | x = 0l |
| 103 | for word in words: |
| 104 | x <<= 64 |
| 105 | x |= word |
| 106 | i = 0 |
| 107 | value = set() |
| 108 | while x != 0: |
| 109 | if x & 1 == 1: |
| 110 | value.add(i) |
| 111 | i += 1 |
| 112 | x >>= 1 |
| 113 | return value |
| 114 | |
| 115 | def pack_checksum_128(value): |
| 116 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 117 | |
| 118 | def unpack_checksum_128(reader): |
| 119 | hi, lo = reader.read("!QQ") |
| 120 | return (hi << 64) | lo |