blob: 7997e38c7ef13c4cddbcc3202e35823aa42b4f8f [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 Laneb658ddd2013-03-12 10:15:10 -07005# Automatically generated by LOXI from template util.py
6# Do not modify
7
8import loxi
9import const
Rich Lanec2ee4b82013-04-24 17:12:38 -070010import struct
Rich Laneb658ddd2013-03-12 10:15:10 -070011
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
31def pretty_wildcards(v):
32 if v == const.OFPFW_ALL:
33 return 'OFPFW_ALL'
34 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
35 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
36 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
37 'OFPFW_NW_TOS']
38 return pretty_flags(v, flag_names)
39
40def pretty_port(v):
41 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
42 for (k, v2) in named_ports:
43 if v == v2:
44 return k
45 return v
Dan Talaycof6202252013-07-02 01:00:29 -070046
47def pack_port_no(value):
48 return struct.pack("!H", value)
49
50def unpack_port_no(reader):
51 return reader.read("!H")[0]
52
53def pack_fm_cmd(value):
54 return struct.pack("!H", value)
55
56def unpack_fm_cmd(reader):
57 return reader.read("!H")[0]
58
59def init_wc_bmap():
60 return const.OFPFW_ALL
61
62def pack_wc_bmap(value):
63 return struct.pack("!L", value)
64
65def unpack_wc_bmap(reader):
66 return reader.read("!L")[0]
67
68def init_match_bmap():
69 return const.OFPFW_ALL
70
71def pack_match_bmap(value):
72 return struct.pack("!L", value)
73
74def unpack_match_bmap(reader):
75 return reader.read("!L")[0]
76
77def pack_list(values):
78 return "".join([x.pack() for x in values])
Rich Lane6f4978c2013-10-20 21:33:52 -070079
80MASK64 = (1 << 64) - 1
81
82def pack_bitmap_128(value):
83 x = 0l
84 for y in value:
85 x |= 1 << y
86 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
87
88def unpack_bitmap_128(reader):
89 hi, lo = reader.read("!QQ")
90 x = (hi << 64) | lo
91 i = 0
92 value = set()
93 while x != 0:
94 if x & 1 == 1:
95 value.add(i)
96 i += 1
97 x >>= 1
98 return value