Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame^] | 1 | |
| 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 Lane | 2e079da | 2014-10-29 15:30:24 -0700 | [diff] [blame] | 17 | # 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. |
| 20 | # See the file LICENSE.pyloxi which should have been included in the source distribution |
| 21 | # Automatically generated by LOXI from template util.py |
| 22 | # Do not modify |
| 23 | |
| 24 | import struct |
| 25 | import loxi |
| 26 | import const |
| 27 | import common |
| 28 | import action |
| 29 | import instruction |
| 30 | import oxm |
| 31 | import action_id |
| 32 | import instruction_id |
| 33 | import meter_band |
| 34 | |
| 35 | def pretty_mac(mac): |
| 36 | return ':'.join(["%02x" % x for x in mac]) |
| 37 | |
| 38 | def pretty_ipv4(v): |
| 39 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 40 | |
| 41 | def pretty_flags(v, flag_names): |
| 42 | set_flags = [] |
| 43 | for flag_name in flag_names: |
| 44 | flag_value = getattr(const, flag_name) |
| 45 | if v & flag_value == flag_value: |
| 46 | set_flags.append(flag_name) |
| 47 | elif v & flag_value: |
| 48 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 49 | v &= ~flag_value |
| 50 | if v: |
| 51 | set_flags.append("%#x" % v) |
| 52 | return '|'.join(set_flags) or '0' |
| 53 | |
| 54 | |
| 55 | def 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 | |
| 62 | def pack_port_no(value): |
| 63 | return struct.pack("!L", value) |
| 64 | |
| 65 | def unpack_port_no(reader): |
| 66 | return reader.read("!L")[0] |
| 67 | |
| 68 | def pack_fm_cmd(value): |
| 69 | return struct.pack("!B", value) |
| 70 | |
| 71 | def unpack_fm_cmd(reader): |
| 72 | return reader.read("!B")[0] |
| 73 | |
| 74 | def init_wc_bmap(): |
| 75 | return 0 |
| 76 | |
| 77 | def pack_wc_bmap(value): |
| 78 | return struct.pack("!Q", value) |
| 79 | |
| 80 | def unpack_wc_bmap(reader): |
| 81 | return reader.read("!Q")[0] |
| 82 | |
| 83 | def init_match_bmap(): |
| 84 | return 0 |
| 85 | |
| 86 | def pack_match_bmap(value): |
| 87 | return struct.pack("!Q", value) |
| 88 | |
| 89 | def unpack_match_bmap(reader): |
| 90 | return reader.read("!Q")[0] |
| 91 | |
| 92 | MASK64 = (1 << 64) - 1 |
| 93 | |
| 94 | def 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 | |
| 100 | def 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 | |
Rich Lane | 474324f | 2015-01-14 15:22:56 -0800 | [diff] [blame] | 112 | def 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 | |
| 119 | def 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 | |
Rich Lane | 2e079da | 2014-10-29 15:30:24 -0700 | [diff] [blame] | 134 | def pack_checksum_128(value): |
| 135 | return struct.pack("!QQ", (value >> 64) & MASK64, value & MASK64) |
| 136 | |
| 137 | def unpack_checksum_128(reader): |
| 138 | hi, lo = reader.read("!QQ") |
| 139 | return (hi << 64) | lo |