blob: a23597d1e8d52ff8ccd410e74cc48858c9ca402f [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
15
16def pretty_mac(mac):
17 return ':'.join(["%02x" % x for x in mac])
18
19def pretty_ipv4(v):
20 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
21
22def pretty_flags(v, flag_names):
23 set_flags = []
24 for flag_name in flag_names:
25 flag_value = getattr(const, flag_name)
26 if v & flag_value == flag_value:
27 set_flags.append(flag_name)
28 elif v & flag_value:
29 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
30 v &= ~flag_value
31 if v:
32 set_flags.append("%#x" % v)
33 return '|'.join(set_flags) or '0'
34
35
36def pretty_port(v):
37 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
38 for (k, v2) in named_ports:
39 if v == v2:
40 return k
41 return v
42
43def pack_port_no(value):
44 return struct.pack("!L", value)
45
46def unpack_port_no(reader):
47 return reader.read("!L")[0]
48
49def pack_fm_cmd(value):
50 return struct.pack("!B", value)
51
52def unpack_fm_cmd(reader):
53 return reader.read("!B")[0]
54
55def init_wc_bmap():
56 return 0
57
58def pack_wc_bmap(value):
59 return struct.pack("!Q", value)
60
61def unpack_wc_bmap(reader):
62 return reader.read("!Q")[0]
63
64def init_match_bmap():
65 return 0
66
67def pack_match_bmap(value):
68 return struct.pack("!Q", value)
69
70def unpack_match_bmap(reader):
71 return reader.read("!Q")[0]
72
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
92
93def pack_bitmap_512(value):
94 words = [0] * 8
95 for v in value:
96 assert v < 512
97 words[7-v/64] |= 1 << (v % 64)
98 return struct.pack("!8Q", *words)
99
100def unpack_bitmap_512(reader):
101 words = reader.read("!8Q")
102 x = 0l
103 for word in words:
104 x <<= 64
105 x |= word
106 i = 0
107 value = set()
108 while x != 0:
109 if x & 1 == 1:
110 value.add(i)
111 i += 1
112 x >>= 1
113 return value
114
115def pack_checksum_128(value):
116 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
117
118def unpack_checksum_128(reader):
119 hi, lo = reader.read("!QQ")
120 return (hi << 64) | lo