blob: 20d4ef278e8163108825c5636ec3ef9a6b5ee71e [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:
Rich Lane2c9938e2013-12-09 17:20:12 -080044import action_id
45import instruction_id
Rich Laned50d9712013-11-29 19:40:28 -080046import meter_band
47:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070048
Rich Lanea06d0c32013-03-25 08:52:03 -070049def pretty_mac(mac):
50 return ':'.join(["%02x" % x for x in mac])
51
52def pretty_ipv4(v):
53 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
54
Murat Parlakisikf95672c2016-12-05 00:53:17 -080055def pretty_ipv6(v):
56 return ":".join(["%0.2x%0.2x" % (ord(v[i]), ord(v[i+1])) for i in range(0, len(v), 2)])
57
Rich Lanea06d0c32013-03-25 08:52:03 -070058def pretty_flags(v, flag_names):
59 set_flags = []
60 for flag_name in flag_names:
61 flag_value = getattr(const, flag_name)
62 if v & flag_value == flag_value:
63 set_flags.append(flag_name)
64 elif v & flag_value:
65 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
66 v &= ~flag_value
67 if v:
68 set_flags.append("%#x" % v)
69 return '|'.join(set_flags) or '0'
70
Andreas Wundsam5630c422013-11-15 13:43:11 -080071:: if version in (OFVersions.VERSION_1_0, OFVersions.VERSION_1_1):
Rich Lanea06d0c32013-03-25 08:52:03 -070072def pretty_wildcards(v):
73 if v == const.OFPFW_ALL:
74 return 'OFPFW_ALL'
75 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
76 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
77 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
78 'OFPFW_NW_TOS']
79 return pretty_flags(v, flag_names)
Rich Laneadb79832013-05-02 17:14:33 -070080:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070081
82def pretty_port(v):
83 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
84 for (k, v2) in named_ports:
85 if v == v2:
86 return k
87 return v
Rich Lane8a22cda2013-06-19 13:20:07 -070088
89def pack_port_no(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080090:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070091 return struct.pack("!H", value)
92:: else:
93 return struct.pack("!L", value)
94:: #endif
95
96def unpack_port_no(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -080097:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070098 return reader.read("!H")[0]
99:: else:
100 return reader.read("!L")[0]
101:: #endif
102
103def pack_fm_cmd(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800104:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700105 return struct.pack("!H", value)
106:: else:
107 return struct.pack("!B", value)
108:: #endif
109
110def unpack_fm_cmd(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800111:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700112 return reader.read("!H")[0]
113:: else:
114 return reader.read("!B")[0]
115:: #endif
116
117def init_wc_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800118:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700119 return const.OFPFW_ALL
120:: else:
121 return 0
122:: #endif
123
124def pack_wc_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800125:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700126 return struct.pack("!L", value)
127:: else:
128 return struct.pack("!Q", value)
129:: #endif
130
131def unpack_wc_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800132:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700133 return reader.read("!L")[0]
134:: else:
135 return reader.read("!Q")[0]
136:: #endif
137
138def init_match_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800139:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700140 return const.OFPFW_ALL
141:: else:
142 return 0
143:: #endif
144
145def pack_match_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800146:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700147 return struct.pack("!L", value)
148:: else:
149 return struct.pack("!Q", value)
150:: #endif
151
152def unpack_match_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800153:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700154 return reader.read("!L")[0]
155:: else:
156 return reader.read("!Q")[0]
157:: #endif
158
Rich Lane3b2fd832013-09-24 13:44:08 -0700159MASK64 = (1 << 64) - 1
160
161def pack_bitmap_128(value):
162 x = 0l
163 for y in value:
164 x |= 1 << y
165 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
166
167def unpack_bitmap_128(reader):
168 hi, lo = reader.read("!QQ")
169 x = (hi << 64) | lo
170 i = 0
171 value = set()
172 while x != 0:
173 if x & 1 == 1:
174 value.add(i)
175 i += 1
176 x >>= 1
177 return value
Rich Laned50d9712013-11-29 19:40:28 -0800178
Rich Lane3009c1a2014-11-13 15:43:09 -0800179def pack_bitmap_512(value):
180 words = [0] * 8
181 for v in value:
182 assert v < 512
183 words[7-v/64] |= 1 << (v % 64)
184 return struct.pack("!8Q", *words)
185
186def unpack_bitmap_512(reader):
187 words = reader.read("!8Q")
188 x = 0l
189 for word in words:
190 x <<= 64
191 x |= word
192 i = 0
193 value = set()
194 while x != 0:
195 if x & 1 == 1:
196 value.add(i)
197 i += 1
198 x >>= 1
199 return value
200
Rich Lane1adf4212013-12-03 12:59:21 -0800201def pack_checksum_128(value):
202 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
203
204def unpack_checksum_128(reader):
205 hi, lo = reader.read("!QQ")
206 return (hi << 64) | lo