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