blob: b908451674525107db711666c58a2dc6656dbffe [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
8import loxi
9import const
10import struct
11
12def pretty_mac(mac):
13 return ':'.join(["%02x" % x for x in mac])
14
15def pretty_ipv4(v):
16 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
17
18def pretty_flags(v, flag_names):
19 set_flags = []
20 for flag_name in flag_names:
21 flag_value = getattr(const, flag_name)
22 if v & flag_value == flag_value:
23 set_flags.append(flag_name)
24 elif v & flag_value:
25 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
26 v &= ~flag_value
27 if v:
28 set_flags.append("%#x" % v)
29 return '|'.join(set_flags) or '0'
30
31
32def pretty_port(v):
33 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
34 for (k, v2) in named_ports:
35 if v == v2:
36 return k
37 return v
Dan Talaycof6202252013-07-02 01:00:29 -070038
39def pack_port_no(value):
40 return struct.pack("!L", value)
41
42def unpack_port_no(reader):
43 return reader.read("!L")[0]
44
45def pack_fm_cmd(value):
46 return struct.pack("!B", value)
47
48def unpack_fm_cmd(reader):
49 return reader.read("!B")[0]
50
51def init_wc_bmap():
52 return 0
53
54def pack_wc_bmap(value):
55 return struct.pack("!Q", value)
56
57def unpack_wc_bmap(reader):
58 return reader.read("!Q")[0]
59
60def init_match_bmap():
61 return 0
62
63def pack_match_bmap(value):
64 return struct.pack("!Q", value)
65
66def unpack_match_bmap(reader):
67 return reader.read("!Q")[0]
68
69def pack_list(values):
70 return "".join([x.pack() for x in values])
Rich Lane6f4978c2013-10-20 21:33:52 -070071
72MASK64 = (1 << 64) - 1
73
74def pack_bitmap_128(value):
75 x = 0l
76 for y in value:
77 x |= 1 << y
78 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
79
80def unpack_bitmap_128(reader):
81 hi, lo = reader.read("!QQ")
82 x = (hi << 64) | lo
83 i = 0
84 value = set()
85 while x != 0:
86 if x & 1 == 1:
87 value.add(i)
88 i += 1
89 x >>= 1
90 return value