blob: 7af98be42f34eaecc3f1f416788404b80eb4db1e [file] [log] [blame]
Nathan Knuth418fdc82016-09-16 22:51:15 -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.
4# See the file LICENSE.pyloxi which should have been included in the source distribution
5# Automatically generated by LOXI from template util.py
6# Do not modify
7
8import struct
9import loxi
10import const
11import common
12import action
13
14def pretty_mac(mac):
15 return ':'.join(["%02x" % x for x in mac])
16
17def pretty_ipv4(v):
18 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
19
20def pretty_flags(v, flag_names):
21 set_flags = []
22 for flag_name in flag_names:
23 flag_value = getattr(const, flag_name)
24 if v & flag_value == flag_value:
25 set_flags.append(flag_name)
26 elif v & flag_value:
27 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
28 v &= ~flag_value
29 if v:
30 set_flags.append("%#x" % v)
31 return '|'.join(set_flags) or '0'
32
33def pretty_wildcards(v):
34 if v == const.OFPFW_ALL:
35 return 'OFPFW_ALL'
36 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
37 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
38 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
39 'OFPFW_NW_TOS']
40 return pretty_flags(v, flag_names)
41
42def pretty_port(v):
43 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
44 for (k, v2) in named_ports:
45 if v == v2:
46 return k
47 return v
48
49def pack_port_no(value):
50 return struct.pack("!H", value)
51
52def unpack_port_no(reader):
53 return reader.read("!H")[0]
54
55def pack_fm_cmd(value):
56 return struct.pack("!H", value)
57
58def unpack_fm_cmd(reader):
59 return reader.read("!H")[0]
60
61def init_wc_bmap():
62 return const.OFPFW_ALL
63
64def pack_wc_bmap(value):
65 return struct.pack("!L", value)
66
67def unpack_wc_bmap(reader):
68 return reader.read("!L")[0]
69
70def init_match_bmap():
71 return const.OFPFW_ALL
72
73def pack_match_bmap(value):
74 return struct.pack("!L", value)
75
76def unpack_match_bmap(reader):
77 return reader.read("!L")[0]
78
79MASK64 = (1 << 64) - 1
80
81def pack_bitmap_128(value):
82 x = 0l
83 for y in value:
84 x |= 1 << y
85 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
86
87def unpack_bitmap_128(reader):
88 hi, lo = reader.read("!QQ")
89 x = (hi << 64) | lo
90 i = 0
91 value = set()
92 while x != 0:
93 if x & 1 == 1:
94 value.add(i)
95 i += 1
96 x >>= 1
97 return value
98
99def pack_bitmap_512(value):
100 words = [0] * 8
101 for v in value:
102 assert v < 512
103 words[7-v/64] |= 1 << (v % 64)
104 return struct.pack("!8Q", *words)
105
106def unpack_bitmap_512(reader):
107 words = reader.read("!8Q")
108 x = 0l
109 for word in words:
110 x <<= 64
111 x |= word
112 i = 0
113 value = set()
114 while x != 0:
115 if x & 1 == 1:
116 value.add(i)
117 i += 1
118 x >>= 1
119 return value
120
121def pack_checksum_128(value):
122 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
123
124def unpack_checksum_128(reader):
125 hi, lo = reader.read("!QQ")
126 return (hi << 64) | lo