blob: bf8be59ed2c22b155acdd806aa87621136956cf4 [file] [log] [blame]
Rich Lanec2ee4b82013-04-24 17:12:38 -07001# 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 Talaycof6202252013-07-02 01:00:29 -07004# See the file LICENSE.pyloxi which should have been included in the source distribution
Rich Lanec2ee4b82013-04-24 17:12:38 -07005# Automatically generated by LOXI from template util.py
6# Do not modify
7
Rich Lane7dcdf022013-12-11 14:45:27 -08008import struct
Rich Lanec2ee4b82013-04-24 17:12:38 -07009import loxi
10import const
Rich Lane7dcdf022013-12-11 14:45:27 -080011import common
12import action
13import instruction
14import oxm
15import action_id
16import instruction_id
17import meter_band
Rich Lanec2ee4b82013-04-24 17:12:38 -070018
19def pretty_mac(mac):
20 return ':'.join(["%02x" % x for x in mac])
21
22def pretty_ipv4(v):
23 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
24
25def 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
39def 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 Talaycof6202252013-07-02 01:00:29 -070045
46def pack_port_no(value):
47 return struct.pack("!L", value)
48
49def unpack_port_no(reader):
50 return reader.read("!L")[0]
51
52def pack_fm_cmd(value):
53 return struct.pack("!B", value)
54
55def unpack_fm_cmd(reader):
56 return reader.read("!B")[0]
57
58def init_wc_bmap():
59 return 0
60
61def pack_wc_bmap(value):
62 return struct.pack("!Q", value)
63
64def unpack_wc_bmap(reader):
65 return reader.read("!Q")[0]
66
67def init_match_bmap():
68 return 0
69
70def pack_match_bmap(value):
71 return struct.pack("!Q", value)
72
73def unpack_match_bmap(reader):
74 return reader.read("!Q")[0]
75
Rich Lane6f4978c2013-10-20 21:33:52 -070076MASK64 = (1 << 64) - 1
77
78def 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
84def 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 Lane7dcdf022013-12-11 14:45:27 -080095
96def 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]
Rich Lane1a820412014-01-06 14:15:07 -0800105
106def pack_checksum_128(value):
107 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
108
109def unpack_checksum_128(reader):
110 hi, lo = reader.read("!QQ")
111 return (hi << 64) | lo