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