blob: f5dd74365e9fc3ce41333ca892f137f0d8478404 [file] [log] [blame]
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001# 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.
14# 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
26import instruction
27
28def pretty_mac(mac):
29 return ':'.join(["%02x" % x for x in mac])
30
31def pretty_ipv4(v):
32 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
33
34def pretty_flags(v, flag_names):
35 set_flags = []
36 for flag_name in flag_names:
37 flag_value = getattr(const, flag_name)
38 if v & flag_value == flag_value:
39 set_flags.append(flag_name)
40 elif v & flag_value:
41 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
42 v &= ~flag_value
43 if v:
44 set_flags.append("%#x" % v)
45 return '|'.join(set_flags) or '0'
46
47def pretty_wildcards(v):
48 if v == const.OFPFW_ALL:
49 return 'OFPFW_ALL'
50 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
51 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
52 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
53 'OFPFW_NW_TOS']
54 return pretty_flags(v, flag_names)
55
56def pretty_port(v):
57 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
58 for (k, v2) in named_ports:
59 if v == v2:
60 return k
61 return v
62
63def pack_port_no(value):
64 return struct.pack("!L", value)
65
66def unpack_port_no(reader):
67 return reader.read("!L")[0]
68
69def pack_fm_cmd(value):
70 return struct.pack("!B", value)
71
72def unpack_fm_cmd(reader):
73 return reader.read("!B")[0]
74
75def init_wc_bmap():
76 return const.OFPFW_ALL
77
78def pack_wc_bmap(value):
79 return struct.pack("!L", value)
80
81def unpack_wc_bmap(reader):
82 return reader.read("!L")[0]
83
84def init_match_bmap():
85 return const.OFPFW_ALL
86
87def pack_match_bmap(value):
88 return struct.pack("!L", value)
89
90def unpack_match_bmap(reader):
91 return reader.read("!L")[0]
92
93MASK64 = (1 << 64) - 1
94
95def pack_bitmap_128(value):
96 x = 0l
97 for y in value:
98 x |= 1 << y
99 return struct.pack("!QQ", (x >> 64) & MASK64, x & MASK64)
100
101def unpack_bitmap_128(reader):
102 hi, lo = reader.read("!QQ")
103 x = (hi << 64) | lo
104 i = 0
105 value = set()
106 while x != 0:
107 if x & 1 == 1:
108 value.add(i)
109 i += 1
110 x >>= 1
111 return value
112
113def pack_bitmap_512(value):
114 words = [0] * 8
115 for v in value:
116 assert v < 512
117 words[7-v/64] |= 1 << (v % 64)
118 return struct.pack("!8Q", *words)
119
120def unpack_bitmap_512(reader):
121 words = reader.read("!8Q")
122 x = 0l
123 for word in words:
124 x <<= 64
125 x |= word
126 i = 0
127 value = set()
128 while x != 0:
129 if x & 1 == 1:
130 value.add(i)
131 i += 1
132 x >>= 1
133 return value
134
135def pack_checksum_128(value):
136 return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64)
137
138def unpack_checksum_128(reader):
139 hi, lo = reader.read("!QQ")
140 return (hi << 64) | lo