blob: 27eb6fb1866612da574020919b0ad5be0783a4bc [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
6# Automatically generated by LOXI from template util.py
7# Do not modify
8
9import loxi
10import const
11import struct
12
13def pretty_mac(mac):
14 return ':'.join(["%02x" % x for x in mac])
15
16def pretty_ipv4(v):
17 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
18
19def 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
33def 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 Talaycof6202252013-07-02 01:00:29 -070039
40def pack_port_no(value):
41 return struct.pack("!L", value)
42
43def unpack_port_no(reader):
44 return reader.read("!L")[0]
45
46def pack_fm_cmd(value):
47 return struct.pack("!B", value)
48
49def unpack_fm_cmd(reader):
50 return reader.read("!B")[0]
51
52def init_wc_bmap():
53 return 0
54
55def pack_wc_bmap(value):
56 return struct.pack("!Q", value)
57
58def unpack_wc_bmap(reader):
59 return reader.read("!Q")[0]
60
61def init_match_bmap():
62 return 0
63
64def pack_match_bmap(value):
65 return struct.pack("!Q", value)
66
67def unpack_match_bmap(reader):
68 return reader.read("!Q")[0]
69
70def pack_list(values):
71 return "".join([x.pack() for x in values])
Rich Lane6f4978c2013-10-20 21:33:52 -070072
73MASK64 = (1 << 64) - 1
74
75def 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
81def 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