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