blob: 8ad246ae879faf350ecd0fda72ec30af1ab1e654 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
28:: include('_copyright.py')
Andreas Wundsam5630c422013-11-15 13:43:11 -080029:: from loxi_globals import OFVersions
Rich Lanea06d0c32013-03-25 08:52:03 -070030:: include('_autogen.py')
31
Rich Laned50d9712013-11-29 19:40:28 -080032import struct
Rich Lanea06d0c32013-03-25 08:52:03 -070033import loxi
34import const
Rich Laned50d9712013-11-29 19:40:28 -080035import common
36import action
37:: if version >= OFVersions.VERSION_1_1:
38import instruction
39:: #endif
40:: if version >= OFVersions.VERSION_1_2:
41import oxm
42:: #endif
43:: if version >= OFVersions.VERSION_1_3:
44import meter_band
45:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070046
Rich Lanea06d0c32013-03-25 08:52:03 -070047def pretty_mac(mac):
48 return ':'.join(["%02x" % x for x in mac])
49
50def pretty_ipv4(v):
51 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
52
53def pretty_flags(v, flag_names):
54 set_flags = []
55 for flag_name in flag_names:
56 flag_value = getattr(const, flag_name)
57 if v & flag_value == flag_value:
58 set_flags.append(flag_name)
59 elif v & flag_value:
60 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
61 v &= ~flag_value
62 if v:
63 set_flags.append("%#x" % v)
64 return '|'.join(set_flags) or '0'
65
Andreas Wundsam5630c422013-11-15 13:43:11 -080066:: if version in (OFVersions.VERSION_1_0, OFVersions.VERSION_1_1):
Rich Lanea06d0c32013-03-25 08:52:03 -070067def pretty_wildcards(v):
68 if v == const.OFPFW_ALL:
69 return 'OFPFW_ALL'
70 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
71 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
72 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
73 'OFPFW_NW_TOS']
74 return pretty_flags(v, flag_names)
Rich Laneadb79832013-05-02 17:14:33 -070075:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070076
77def pretty_port(v):
78 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
79 for (k, v2) in named_ports:
80 if v == v2:
81 return k
82 return v
Rich Lane8a22cda2013-06-19 13:20:07 -070083
84def pack_port_no(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080085:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070086 return struct.pack("!H", value)
87:: else:
88 return struct.pack("!L", value)
89:: #endif
90
91def unpack_port_no(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -080092:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070093 return reader.read("!H")[0]
94:: else:
95 return reader.read("!L")[0]
96:: #endif
97
98def pack_fm_cmd(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080099:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700100 return struct.pack("!H", value)
101:: else:
102 return struct.pack("!B", value)
103:: #endif
104
105def unpack_fm_cmd(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800106:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700107 return reader.read("!H")[0]
108:: else:
109 return reader.read("!B")[0]
110:: #endif
111
112def init_wc_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800113:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700114 return const.OFPFW_ALL
115:: else:
116 return 0
117:: #endif
118
119def pack_wc_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800120:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700121 return struct.pack("!L", value)
122:: else:
123 return struct.pack("!Q", value)
124:: #endif
125
126def unpack_wc_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800127:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700128 return reader.read("!L")[0]
129:: else:
130 return reader.read("!Q")[0]
131:: #endif
132
133def init_match_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800134:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700135 return const.OFPFW_ALL
136:: else:
137 return 0
138:: #endif
139
140def pack_match_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800141:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700142 return struct.pack("!L", value)
143:: else:
144 return struct.pack("!Q", value)
145:: #endif
146
147def unpack_match_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800148:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700149 return reader.read("!L")[0]
150:: else:
151 return reader.read("!Q")[0]
152:: #endif
153
Rich Lane3b2fd832013-09-24 13:44:08 -0700154MASK64 = (1 << 64) - 1
155
156def pack_bitmap_128(value):
157 x = 0l
158 for y in value:
159 x |= 1 << y
160 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
161
162def unpack_bitmap_128(reader):
163 hi, lo = reader.read("!QQ")
164 x = (hi << 64) | lo
165 i = 0
166 value = set()
167 while x != 0:
168 if x & 1 == 1:
169 value.add(i)
170 i += 1
171 x >>= 1
172 return value
Rich Laned50d9712013-11-29 19:40:28 -0800173
Rich Laned50d9712013-11-29 19:40:28 -0800174def unpack_list_hello_elem(reader):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800175 def deserializer(reader):
176 typ, length, = reader.peek('!HH')
177 reader = reader.slice(length)
Rich Laned50d9712013-11-29 19:40:28 -0800178 try:
179 return common.hello_elem.unpack(reader)
180 except loxi.ProtocolError:
181 return None
Rich Lane5bcc7c72013-12-01 15:49:49 -0800182 return [x for x in loxi.generic_util.unpack_list(reader, deserializer) if x != None]