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 | |
| 6 | # Automatically generated by LOXI from template util.py |
| 7 | # Do not modify |
| 8 | |
| 9 | import loxi |
| 10 | import const |
| 11 | import struct |
| 12 | |
| 13 | def pretty_mac(mac): |
| 14 | return ':'.join(["%02x" % x for x in mac]) |
| 15 | |
| 16 | def pretty_ipv4(v): |
| 17 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 18 | |
| 19 | def pretty_flags(v, flag_names): |
| 20 | set_flags = [] |
| 21 | for flag_name in flag_names: |
| 22 | flag_value = getattr(const, flag_name) |
| 23 | if v & flag_value == flag_value: |
| 24 | set_flags.append(flag_name) |
| 25 | elif v & flag_value: |
| 26 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 27 | v &= ~flag_value |
| 28 | if v: |
| 29 | set_flags.append("%#x" % v) |
| 30 | return '|'.join(set_flags) or '0' |
| 31 | |
| 32 | |
| 33 | def pretty_port(v): |
| 34 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 35 | for (k, v2) in named_ports: |
| 36 | if v == v2: |
| 37 | return k |
| 38 | return v |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 39 | |
| 40 | def pack_port_no(value): |
| 41 | return struct.pack("!L", value) |
| 42 | |
| 43 | def unpack_port_no(reader): |
| 44 | return reader.read("!L")[0] |
| 45 | |
| 46 | def pack_fm_cmd(value): |
| 47 | return struct.pack("!B", value) |
| 48 | |
| 49 | def unpack_fm_cmd(reader): |
| 50 | return reader.read("!B")[0] |
| 51 | |
| 52 | def init_wc_bmap(): |
| 53 | return 0 |
| 54 | |
| 55 | def pack_wc_bmap(value): |
| 56 | return struct.pack("!Q", value) |
| 57 | |
| 58 | def unpack_wc_bmap(reader): |
| 59 | return reader.read("!Q")[0] |
| 60 | |
| 61 | def init_match_bmap(): |
| 62 | return 0 |
| 63 | |
| 64 | def pack_match_bmap(value): |
| 65 | return struct.pack("!Q", value) |
| 66 | |
| 67 | def unpack_match_bmap(reader): |
| 68 | return reader.read("!Q")[0] |
| 69 | |
| 70 | def pack_list(values): |
| 71 | return "".join([x.pack() for x in values]) |
Rich Lane | 6f4978c | 2013-10-20 21:33:52 -0700 | [diff] [blame] | 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 |