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 |
| 14 | import oxm |
| 15 | import action_id |
| 16 | import instruction_id |
| 17 | import meter_band |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 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 |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 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 | |
Rich Lane | 6f4978c | 2013-10-20 21:33:52 -0700 | [diff] [blame] | 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 |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 95 | |
| 96 | def unpack_list_hello_elem(reader): |
| 97 | def deserializer(reader): |
| 98 | typ, length, = reader.peek('!HH') |
| 99 | reader = reader.slice(length) |
| 100 | try: |
| 101 | return common.hello_elem.unpack(reader) |
| 102 | except loxi.ProtocolError: |
| 103 | return None |
| 104 | return [x for x in loxi.generic_util.unpack_list(reader, deserializer) if x != None] |