blob: 85181dc33873a6624a078e90ed12e81bcb236f74 [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
55def pretty_flags(v, flag_names):
56 set_flags = []
57 for flag_name in flag_names:
58 flag_value = getattr(const, flag_name)
59 if v & flag_value == flag_value:
60 set_flags.append(flag_name)
61 elif v & flag_value:
62 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
63 v &= ~flag_value
64 if v:
65 set_flags.append("%#x" % v)
66 return '|'.join(set_flags) or '0'
67
Andreas Wundsam5630c422013-11-15 13:43:11 -080068:: if version in (OFVersions.VERSION_1_0, OFVersions.VERSION_1_1):
Rich Lanea06d0c32013-03-25 08:52:03 -070069def pretty_wildcards(v):
70 if v == const.OFPFW_ALL:
71 return 'OFPFW_ALL'
72 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
73 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
74 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
75 'OFPFW_NW_TOS']
76 return pretty_flags(v, flag_names)
Rich Laneadb79832013-05-02 17:14:33 -070077:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070078
79def pretty_port(v):
80 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
81 for (k, v2) in named_ports:
82 if v == v2:
83 return k
84 return v
Rich Lane8a22cda2013-06-19 13:20:07 -070085
86def pack_port_no(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080087:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070088 return struct.pack("!H", value)
89:: else:
90 return struct.pack("!L", value)
91:: #endif
92
93def unpack_port_no(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -080094:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070095 return reader.read("!H")[0]
96:: else:
97 return reader.read("!L")[0]
98:: #endif
99
100def pack_fm_cmd(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800101:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700102 return struct.pack("!H", value)
103:: else:
104 return struct.pack("!B", value)
105:: #endif
106
107def unpack_fm_cmd(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800108:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -0700109 return reader.read("!H")[0]
110:: else:
111 return reader.read("!B")[0]
112:: #endif
113
114def init_wc_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800115:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700116 return const.OFPFW_ALL
117:: else:
118 return 0
119:: #endif
120
121def pack_wc_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800122:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700123 return struct.pack("!L", value)
124:: else:
125 return struct.pack("!Q", value)
126:: #endif
127
128def unpack_wc_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800129:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700130 return reader.read("!L")[0]
131:: else:
132 return reader.read("!Q")[0]
133:: #endif
134
135def init_match_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800136:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700137 return const.OFPFW_ALL
138:: else:
139 return 0
140:: #endif
141
142def pack_match_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800143:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700144 return struct.pack("!L", value)
145:: else:
146 return struct.pack("!Q", value)
147:: #endif
148
149def unpack_match_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800150:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700151 return reader.read("!L")[0]
152:: else:
153 return reader.read("!Q")[0]
154:: #endif
155
Rich Lane3b2fd832013-09-24 13:44:08 -0700156MASK64 = (1 << 64) - 1
157
158def pack_bitmap_128(value):
159 x = 0l
160 for y in value:
161 x |= 1 << y
162 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
163
164def unpack_bitmap_128(reader):
165 hi, lo = reader.read("!QQ")
166 x = (hi << 64) | lo
167 i = 0
168 value = set()
169 while x != 0:
170 if x & 1 == 1:
171 value.add(i)
172 i += 1
173 x >>= 1
174 return value
Rich Laned50d9712013-11-29 19:40:28 -0800175
Rich Lane1adf4212013-12-03 12:59:21 -0800176def pack_checksum_128(value):
177 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
178
179def unpack_checksum_128(reader):
180 hi, lo = reader.read("!QQ")
181 return (hi << 64) | lo