blob: 3735787ef6be06b0d8f33f8fddd0b954969b020c [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
13import instruction
14import oxm
15import action_id
16import instruction_id
17import meter_band
18
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
45
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
76MASK64 = (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
95
96def pack_bitmap_512(value):
97 words = [0] * 8
98 for v in value:
99 assert v < 512
100 words[7-v/64] |= 1 << (v % 64)
101 return struct.pack("!8Q", *words)
102
103def unpack_bitmap_512(reader):
104 words = reader.read("!8Q")
105 x = 0l
106 for word in words:
107 x <<= 64
108 x |= word
109 i = 0
110 value = set()
111 while x != 0:
112 if x & 1 == 1:
113 value.add(i)
114 i += 1
115 x >>= 1
116 return value
117
118def pack_checksum_128(value):
119 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
120
121def unpack_checksum_128(reader):
122 hi, lo = reader.read("!QQ")
123 return (hi << 64) | lo
sathishgbbb90992017-06-23 09:52:29 +0530124
125def bitmap_to_version(bitmaps):
126 versions = [i * 32 + shift
127 for i, bitmap in enumerate(bitmaps)
128 for shift in range(31) if bitmap & (1 << shift)]
129 return versions
130
131def verify_version_support(msg,version_list):
132 version_list_sup = bitmap_to_version([msg.elements[0].bitmaps[0].value])
133 return any(i in version_list_sup for i in version_list)