blob: 28ffba1a5f7f153d16309ade171a6fe851dbf576 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Nathan Knuth418fdc82016-09-16 22:51:15 -070014# Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University
15# Copyright (c) 2011, 2012 Open Networking Foundation
16# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
17# See the file LICENSE.pyloxi which should have been included in the source distribution
18# Automatically generated by LOXI from template util.py
19# Do not modify
20
21import struct
22import loxi
23import const
24import common
25import action
26
27def pretty_mac(mac):
28 return ':'.join(["%02x" % x for x in mac])
29
30def pretty_ipv4(v):
31 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
32
33def pretty_flags(v, flag_names):
34 set_flags = []
35 for flag_name in flag_names:
36 flag_value = getattr(const, flag_name)
37 if v & flag_value == flag_value:
38 set_flags.append(flag_name)
39 elif v & flag_value:
40 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
41 v &= ~flag_value
42 if v:
43 set_flags.append("%#x" % v)
44 return '|'.join(set_flags) or '0'
45
46def pretty_wildcards(v):
47 if v == const.OFPFW_ALL:
48 return 'OFPFW_ALL'
49 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
50 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
51 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
52 'OFPFW_NW_TOS']
53 return pretty_flags(v, flag_names)
54
55def pretty_port(v):
56 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
57 for (k, v2) in named_ports:
58 if v == v2:
59 return k
60 return v
61
62def pack_port_no(value):
63 return struct.pack("!H", value)
64
65def unpack_port_no(reader):
66 return reader.read("!H")[0]
67
68def pack_fm_cmd(value):
69 return struct.pack("!H", value)
70
71def unpack_fm_cmd(reader):
72 return reader.read("!H")[0]
73
74def init_wc_bmap():
75 return const.OFPFW_ALL
76
77def pack_wc_bmap(value):
78 return struct.pack("!L", value)
79
80def unpack_wc_bmap(reader):
81 return reader.read("!L")[0]
82
83def init_match_bmap():
84 return const.OFPFW_ALL
85
86def pack_match_bmap(value):
87 return struct.pack("!L", value)
88
89def unpack_match_bmap(reader):
90 return reader.read("!L")[0]
91
92MASK64 = (1 << 64) - 1
93
94def pack_bitmap_128(value):
95 x = 0l
96 for y in value:
97 x |= 1 << y
98 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
99
100def unpack_bitmap_128(reader):
101 hi, lo = reader.read("!QQ")
102 x = (hi << 64) | lo
103 i = 0
104 value = set()
105 while x != 0:
106 if x & 1 == 1:
107 value.add(i)
108 i += 1
109 x >>= 1
110 return value
111
112def pack_bitmap_512(value):
113 words = [0] * 8
114 for v in value:
115 assert v < 512
116 words[7-v/64] |= 1 << (v % 64)
117 return struct.pack("!8Q", *words)
118
119def unpack_bitmap_512(reader):
120 words = reader.read("!8Q")
121 x = 0l
122 for word in words:
123 x <<= 64
124 x |= word
125 i = 0
126 value = set()
127 while x != 0:
128 if x & 1 == 1:
129 value.add(i)
130 i += 1
131 x >>= 1
132 return value
133
134def pack_checksum_128(value):
135 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
136
137def unpack_checksum_128(reader):
138 hi, lo = reader.read("!QQ")
139 return (hi << 64) | lo