Tolerate failures when setting MTU
[csit.git] / resources / test_data / softwire / map_utils.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Utils for MAP feature."""
15
16
17 def map_port_ranges(psid, length, offset=6):
18     """Return list of port ranges for given PSID in tuple <min, max>.
19
20     :param psid: PSID.
21     :param length: PSID length.
22     :param offset: PSID offset.
23     :type psid: int
24     :type length: int
25     :type offset: int
26     :return: List of (min, max) port range tuples inclusive.
27     :rtype: list
28
29                       0                   1
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 |
36     """
37
38     port_field_len = 16
39     port_field_min = int('0x0000', 16)
40     port_field_max = int('0xffff', 16)
41
42     a = offset
43     k = length
44     m = port_field_len - offset - length
45     km = k + m
46     j_max = port_field_max >> a + k
47
48     port_ranges = []
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))
52
53     return port_ranges