blob: e8218ad869a2f8aa1b3867eada596fbe36588a17 [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
Rich Lanec2ee4b82013-04-24 17:12:38 -070015
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
Dan Talaycof6202252013-07-02 01:00:29 -070042
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
Rich Lane6f4978c2013-10-20 21:33:52 -070073MASK64 = (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
Rich Lane7dcdf022013-12-11 14:45:27 -080092
Rich Lane5454b682014-01-14 17:07:36 -080093def pack_checksum_128(value):
94 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
95
96def unpack_checksum_128(reader):
97 hi, lo = reader.read("!QQ")
98 return (hi << 64) | lo