blob: fead78fd2ae16bf61f9a500f01ffde4f51a0f8c0 [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)
Rich Lane3c26eb62013-04-04 17:04:41 -070055 n = len(buf)
56 while offset < n:
Rich Laned4e08692013-02-20 17:40:33 -080057 if offset + length_struct.size > len(buf): raise loxi.ProtocolError("entry header overruns list length")
58 length, = length_struct.unpack_from(buf, offset)
59 if length < length_struct.size: raise loxi.ProtocolError("entry length is less than the header length")
60 if offset + length > len(buf): raise loxi.ProtocolError("entry length overruns list length")
61 entries.append(deserializer(buffer(buf, offset, length)))
62 offset += length
63 return entries
64
Rich Lanea06d0c32013-03-25 08:52:03 -070065def pretty_mac(mac):
66 return ':'.join(["%02x" % x for x in mac])
67
68def pretty_ipv4(v):
69 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
70
71def pretty_flags(v, flag_names):
72 set_flags = []
73 for flag_name in flag_names:
74 flag_value = getattr(const, flag_name)
75 if v & flag_value == flag_value:
76 set_flags.append(flag_name)
77 elif v & flag_value:
78 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
79 v &= ~flag_value
80 if v:
81 set_flags.append("%#x" % v)
82 return '|'.join(set_flags) or '0'
83
84def pretty_wildcards(v):
85 if v == const.OFPFW_ALL:
86 return 'OFPFW_ALL'
87 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
88 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
89 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
90 'OFPFW_NW_TOS']
91 return pretty_flags(v, flag_names)
92
93def pretty_port(v):
94 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
95 for (k, v2) in named_ports:
96 if v == v2:
97 return k
98 return v