1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Utils for MAP feature."""
17 def map_port_ranges(psid, length, offset=6):
18 """Return list of port ranges for given PSID in tuple <min, max>.
21 :param length: PSID length.
22 :param offset: PSID offset.
26 :return: List of (min, max) port range tuples inclusive.
30 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
31 +-----------+-----------+-------+
32 Ports in | A | PSID | j |
33 the CE port set | > 0 | | |
34 +-----------+-----------+-------+
35 | a bits | k bits |m bits |
39 port_field_min = int('0x0000', 16)
40 port_field_max = int('0xffff', 16)
44 m = port_field_len - offset - length
46 j_max = port_field_max >> a + k
49 for A in range(1, (port_field_max >> km) + 1):
50 port_ranges.append((((A << k) | psid) << m,
51 ((A << k) | psid) << m | j_max))