blob: a0e253fbd1f2fe0885faa5df3ac7267ad0dc481b [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 Laneb658ddd2013-03-12 10:15:10 -07005# Automatically generated by LOXI from template util.py
6# Do not modify
7
Rich Lane7dcdf022013-12-11 14:45:27 -08008import struct
Rich Laneb658ddd2013-03-12 10:15:10 -07009import loxi
10import const
Rich Lane7dcdf022013-12-11 14:45:27 -080011import common
12import action
Rich Laneb658ddd2013-03-12 10:15:10 -070013
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
Dan Talaycof6202252013-07-02 01:00:29 -070048
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
Rich Lane6f4978c2013-10-20 21:33:52 -070079MASK64 = (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
Rich Lane7dcdf022013-12-11 14:45:27 -080098
99def unpack_list_hello_elem(reader):
100 def deserializer(reader):
101 typ, length, = reader.peek('!HH')
102 reader = reader.slice(length)
103 try:
104 return common.hello_elem.unpack(reader)
105 except loxi.ProtocolError:
106 return None
107 return [x for x in loxi.generic_util.unpack_list(reader, deserializer) if x != None]
Rich Lane1a820412014-01-06 14:15:07 -0800108
109def pack_checksum_128(value):
110 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
111
112def unpack_checksum_128(reader):
113 hi, lo = reader.read("!QQ")
114 return (hi << 64) | lo