blob: bfe11d059bca4e92f141f21966c36eb4352f7b1b [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')
29
30:: 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
36def unpack_array(deserializer, element_size, buf):
37 """
38 Deserialize an array of fixed length elements.
39 The deserializer function should take a buffer and return the new object.
40 """
41 if len(buf) % element_size != 0: raise loxi.ProtocolError("invalid array length")
42 n = len(buf) / element_size
43 return [deserializer(buffer(buf, i*element_size, element_size)) for i in range(n)]
44
Rich Laned4e08692013-02-20 17:40:33 -080045def unpack_list(deserializer, length_fmt, buf):
46 """
47 Deserialize a list of variable-length entries.
48 'length_fmt' is a struct format string with exactly one non-padding format
49 character that returns the length of the given element.
50 The deserializer function should take a buffer and return the new object.
51 """
52 entries = []
53 offset = 0
54 length_struct = struct.Struct(length_fmt)
55 while offset < len(buf):
56 if offset + length_struct.size > len(buf): raise loxi.ProtocolError("entry header overruns list length")
57 length, = length_struct.unpack_from(buf, offset)
58 if length < length_struct.size: raise loxi.ProtocolError("entry length is less than the header length")
59 if offset + length > len(buf): raise loxi.ProtocolError("entry length overruns list length")
60 entries.append(deserializer(buffer(buf, offset, length)))
61 offset += length
62 return entries
63
Rich Lanea06d0c32013-03-25 08:52:03 -070064def pretty_mac(mac):
65 return ':'.join(["%02x" % x for x in mac])
66
67def pretty_ipv4(v):
68 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
69
70def pretty_flags(v, flag_names):
71 set_flags = []
72 for flag_name in flag_names:
73 flag_value = getattr(const, flag_name)
74 if v & flag_value == flag_value:
75 set_flags.append(flag_name)
76 elif v & flag_value:
77 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
78 v &= ~flag_value
79 if v:
80 set_flags.append("%#x" % v)
81 return '|'.join(set_flags) or '0'
82
83def pretty_wildcards(v):
84 if v == const.OFPFW_ALL:
85 return 'OFPFW_ALL'
86 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
87 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
88 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
89 'OFPFW_NW_TOS']
90 return pretty_flags(v, flag_names)
91
92def pretty_port(v):
93 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
94 for (k, v2) in named_ports:
95 if v == v2:
96 return k
97 return v