blob: 50a64dbd4236c1480293e468e2edb92de72ef6fa [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
32import loxi
33import const
Rich Laned4e08692013-02-20 17:40:33 -080034import struct
Rich Lanea06d0c32013-03-25 08:52:03 -070035
Rich Lanea06d0c32013-03-25 08:52:03 -070036def pretty_mac(mac):
37 return ':'.join(["%02x" % x for x in mac])
38
39def pretty_ipv4(v):
40 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
41
42def pretty_flags(v, flag_names):
43 set_flags = []
44 for flag_name in flag_names:
45 flag_value = getattr(const, flag_name)
46 if v & flag_value == flag_value:
47 set_flags.append(flag_name)
48 elif v & flag_value:
49 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
50 v &= ~flag_value
51 if v:
52 set_flags.append("%#x" % v)
53 return '|'.join(set_flags) or '0'
54
Andreas Wundsam5630c422013-11-15 13:43:11 -080055:: if version in (OFVersions.VERSION_1_0, OFVersions.VERSION_1_1):
Rich Lanea06d0c32013-03-25 08:52:03 -070056def pretty_wildcards(v):
57 if v == const.OFPFW_ALL:
58 return 'OFPFW_ALL'
59 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
60 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
61 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
62 'OFPFW_NW_TOS']
63 return pretty_flags(v, flag_names)
Rich Laneadb79832013-05-02 17:14:33 -070064:: #endif
Rich Lanea06d0c32013-03-25 08:52:03 -070065
66def pretty_port(v):
67 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
68 for (k, v2) in named_ports:
69 if v == v2:
70 return k
71 return v
Rich Lane8a22cda2013-06-19 13:20:07 -070072
73def pack_port_no(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080074:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070075 return struct.pack("!H", value)
76:: else:
77 return struct.pack("!L", value)
78:: #endif
79
80def unpack_port_no(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -080081:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070082 return reader.read("!H")[0]
83:: else:
84 return reader.read("!L")[0]
85:: #endif
86
87def pack_fm_cmd(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -080088:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070089 return struct.pack("!H", value)
90:: else:
91 return struct.pack("!B", value)
92:: #endif
93
94def unpack_fm_cmd(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -080095:: if version == OFVersions.VERSION_1_0:
Rich Lane8a22cda2013-06-19 13:20:07 -070096 return reader.read("!H")[0]
97:: else:
98 return reader.read("!B")[0]
99:: #endif
100
101def init_wc_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800102:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700103 return const.OFPFW_ALL
104:: else:
105 return 0
106:: #endif
107
108def pack_wc_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800109:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700110 return struct.pack("!L", value)
111:: else:
112 return struct.pack("!Q", value)
113:: #endif
114
115def unpack_wc_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800116:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700117 return reader.read("!L")[0]
118:: else:
119 return reader.read("!Q")[0]
120:: #endif
121
122def init_match_bmap():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800123:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700124 return const.OFPFW_ALL
125:: else:
126 return 0
127:: #endif
128
129def pack_match_bmap(value):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800130:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700131 return struct.pack("!L", value)
132:: else:
133 return struct.pack("!Q", value)
134:: #endif
135
136def unpack_match_bmap(reader):
Andreas Wundsam5630c422013-11-15 13:43:11 -0800137:: if version <= OFVersions.VERSION_1_1:
Rich Lane8a22cda2013-06-19 13:20:07 -0700138 return reader.read("!L")[0]
139:: else:
140 return reader.read("!Q")[0]
141:: #endif
142
143def pack_list(values):
144 return "".join([x.pack() for x in values])
Rich Lane3b2fd832013-09-24 13:44:08 -0700145
146MASK64 = (1 << 64) - 1
147
148def pack_bitmap_128(value):
149 x = 0l
150 for y in value:
151 x |= 1 << y
152 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
153
154def unpack_bitmap_128(reader):
155 hi, lo = reader.read("!QQ")
156 x = (hi << 64) | lo
157 i = 0
158 value = set()
159 while x != 0:
160 if x & 1 == 1:
161 value.add(i)
162 i += 1
163 x >>= 1
164 return value