feat(tests): 6p3nic ip4 tests
[csit.git] / resources / libraries / python / IPTopology.py
1 # Copyright (c) 2023 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 """IP Topology Library."""
15
16 from robot.libraries.BuiltIn import BuiltIn
17
18 from resources.libraries.python.IPUtil import IPUtil
19
20
21 class IPTopology:
22     """IP Topology Library."""
23
24     @staticmethod
25     def initialize_ipv4_forwarding(count=1, pfs=2):
26         """
27         Custom setup of IPv4 forwarding with scalability of IP routes on all
28         DUT nodes in 2-node / 3-node circular topology.
29
30         :param count: Number of routes to configure.
31         :param pfs: Number of physical interfaces to configure.
32         :type count: int
33         :type pfs: int
34         """
35         topology = BuiltIn().get_variable_value("&{topology_info}")
36         dut = topology["duts"][-1]
37         ifl = BuiltIn().get_variable_value("${int}")
38
39         for l, i in zip(range(pfs // 2), range(1, pfs, 2)):
40             dut1_int1 = BuiltIn().get_variable_value(f"${{DUT1_{ifl}{i}}}[0]")
41             dut1_int2 = BuiltIn().get_variable_value(f"${{DUT1_{ifl}{i+1}}}[0]")
42             dut_int1 = BuiltIn().get_variable_value(f"${{{dut}_{ifl}{i}}}[0]")
43             dut_int2 = BuiltIn().get_variable_value(f"${{{dut}_{ifl}{i+1}}}[0]")
44
45             IPUtil.vpp_add_ip_neighbor(
46                 topology["DUT1"], dut1_int1, f"1.{l}.1.1",
47                 topology[f"TG_pf{i}_mac"][0]
48             )
49             if dut == "DUT2":
50                 dut_mac1 = BuiltIn().get_variable_value(
51                     f"${{{dut}_{ifl}{i}_mac}}[0]"
52                 )
53                 IPUtil.vpp_add_ip_neighbor(
54                     topology["DUT1"], dut1_int2, f"3.{l}.3.2", dut_mac1
55                 )
56                 dut_mac2 = BuiltIn().get_variable_value(
57                     f"${{DUT1_{ifl}{i+1}_mac}}[0]"
58                 )
59                 IPUtil.vpp_add_ip_neighbor(
60                     topology["DUT2"], dut_int1, f"3.{l}.3.1", dut_mac2
61                 )
62             IPUtil.vpp_add_ip_neighbor(
63                 topology[dut], dut_int2, f"2.{l}.2.1",
64                 topology[f"TG_pf{i+1}_mac"][0]
65             )
66
67             IPUtil.vpp_interface_set_ip_address(
68                 topology["DUT1"], dut1_int1, f"1.{l}.1.2", 30
69             )
70             if dut == "DUT2":
71                 IPUtil.vpp_interface_set_ip_address(
72                     topology["DUT1"], dut1_int2, f"3.{l}.3.1", 30
73                 )
74                 IPUtil.vpp_interface_set_ip_address(
75                     topology["DUT2"], dut_int1, f"3.{l}.3.2", 30
76                 )
77             IPUtil.vpp_interface_set_ip_address(
78                 topology[dut], dut_int2, f"2.{l}.2.2", 30
79             )
80
81             IPUtil.vpp_route_add(
82                 topology["DUT1"], f"{i}0.0.0.0", 32, gateway=f"1.{l}.1.1",
83                 interface=dut1_int1, count=count
84             )
85             if dut == "DUT2":
86                 IPUtil.vpp_route_add(
87                     topology["DUT1"], f"{i+1}0.0.0.0", 32, gateway=f"3.{l}.3.2",
88                     interface=dut1_int2, count=count
89                 )
90                 IPUtil.vpp_route_add(
91                     topology["DUT2"], f"{i}0.0.0.0", 32, gateway=f"3.{l}.3.1",
92                     interface=dut_int1, count=count
93                 )
94             IPUtil.vpp_route_add(
95                 topology[dut], f"{i+1}0.0.0.0", 32, gateway=f"2.{l}.2.1",
96                 interface=dut_int2, count=count
97             )