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