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