blob: 5eadb1018801d9cd8fcaab2e7d67cab9af39e78b [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
Rich Lane7dcdf022013-12-11 14:45:27 -08008import struct
Rich Lanec2ee4b82013-04-24 17:12:38 -07009import loxi
10import const
Rich Lane7dcdf022013-12-11 14:45:27 -080011import common
12import action
13import instruction
14import oxm
15import action_id
16import instruction_id
17import meter_band
Rich Lanec2ee4b82013-04-24 17:12:38 -070018
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
Dan Talaycof6202252013-07-02 01:00:29 -070045
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
Rich Lane6f4978c2013-10-20 21:33:52 -070076MASK64 = (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
Rich Lane7dcdf022013-12-11 14:45:27 -080095
Rich Lane474324f2015-01-14 15:22:56 -080096def 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
Rich Lane5454b682014-01-14 17:07:36 -0800118def 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