blob: d5bbe74eeb3bd3264ff0eb2d7d5690d7922324e2 [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
Rich Lanec2ee4b82013-04-24 17:12:38 -070014
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
Dan Talaycof6202252013-07-02 01:00:29 -070049
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
Rich Lane6f4978c2013-10-20 21:33:52 -070080MASK64 = (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
Rich Lane7dcdf022013-12-11 14:45:27 -080099
Rich Lane5454b682014-01-14 17:07:36 -0800100def pack_checksum_128(value):
101 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
102
103def unpack_checksum_128(reader):
104 hi, lo = reader.read("!QQ")
105 return (hi << 64) | lo