1 # Copyright (c) 2022 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.
15 This module exists to start l3fwd on topology nodes.
18 from robot.libraries.BuiltIn import BuiltIn
19 from resources.libraries.python.Constants import Constants
20 from resources.libraries.python.CpuUtils import CpuUtils
21 from resources.libraries.python.DpdkUtil import DpdkUtil
22 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
23 from resources.libraries.python.topology import NodeType, Topology
29 """This class start l3fwd on topology nodes and check if properly started.
33 def start_l3fwd_on_all_duts(
34 nodes, topology_info, phy_cores, rx_queues=None, jumbo_frames=False,
37 Execute the l3fwd on all dut nodes.
39 :param nodes: All the nodes info from the topology file.
40 :param topology_info: All the info from the topology file.
41 :param phy_cores: Number of physical cores to use.
42 :param rx_queues: Number of RX queues.
43 :param jumbo_frames: Jumbo frames on/off.
44 :param rxd: Number of RX descriptors.
45 :param txd: Number of TX descriptors.
48 :type topology_info: dict
51 :type jumbo_frames: bool
54 :raises RuntimeError: If bash return code is not 0.
56 cpu_count_int = dp_count_int = int(phy_cores)
57 dp_cores = cpu_count_int+1
58 tg_flip = topology_info[f"tg_if1_pci"] > topology_info[f"tg_if2_pci"]
61 compute_resource_info = CpuUtils.get_affinity_vswitch(
62 nodes, node, phy_cores, rx_queues=rx_queues,
66 BuiltIn().set_tags('MTHREAD')
68 BuiltIn().set_tags('STHREAD')
70 f"{dp_count_int}T{cpu_count_int}C"
73 cpu_dp = compute_resource_info[u"cpu_dp"]
74 rxq_count_int = compute_resource_info[u"rxq_count_int"]
75 if1 = topology_info[f"{node}_pf1"][0]
76 if2 = topology_info[f"{node}_pf2"][0]
77 L3fwdTest.start_l3fwd(
78 nodes, nodes[node], if1=if1, if2=if2, lcores_list=cpu_dp,
79 nb_cores=dp_count_int, queue_nums=rxq_count_int,
80 jumbo_frames=jumbo_frames, tg_flip=tg_flip
86 L3fwdTest.check_l3fwd(nodes[node])
89 L3fwdTest.start_l3fwd(
90 nodes, nodes[node], if1=if1, if2=if2,
91 lcores_list=cpu_dp, nb_cores=dp_count_int,
92 queue_nums=rxq_count_int, jumbo_frames=jumbo_frames,
96 message = f"Failed to start l3fwd at node {node}"
97 raise RuntimeError(message)
101 nodes, node, if1, if2, lcores_list, nb_cores, queue_nums,
102 jumbo_frames, tg_flip):
104 Execute the l3fwd on the dut_node.
106 L3fwd uses default IP forwarding table, but sorts ports by API address.
107 When that does not match the traffic profile (depends on topology),
108 the only way to fix is is to latch and recompile l3fwd app.
110 :param nodes: All the nodes info in the topology file.
111 :param node: DUT node.
112 :param if1: The test link interface 1.
113 :param if2: The test link interface 2.
114 :param lcores_list: The lcore list string for the l3fwd routing
115 :param nb_cores: The cores number for the forwarding
116 :param queue_nums: The queues number for the NIC
117 :param jumbo_frames: Indication if the jumbo frames are used (True) or
119 :param tg_flip: Whether TG ports are reordered.
124 :type lcores_list: str
126 :type queue_nums: str
127 :type jumbo_frames: bool
130 if node[u"type"] == NodeType.DUT:
131 adj_mac0, adj_mac1, if_pci0, if_pci1 = L3fwdTest.get_adj_mac(
132 nodes, node, if1, if2, tg_flip
135 lcores = [int(item) for item in lcores_list.split(u",")]
137 # prepare the port config param
138 nb_cores = int(nb_cores)
141 for port in range(0, NB_PORTS):
142 for queue in range(0, int(queue_nums)):
143 index = 0 if nb_cores == 1 else index
145 f"({port}, {queue}, {lcores[index % NB_PORTS]}),"
149 l3fwd_args = DpdkUtil.get_l3fwd_args(
150 eal_corelist=f"1,{lcores_list}",
152 eal_pci_whitelist0=if_pci0,
153 eal_pci_whitelist1=if_pci1,
155 pmd_config=f"\\\"{port_config.rstrip(u',')}\\\"",
156 pmd_eth_dest_0=f"\\\"0,{adj_mac0}\\\"",
157 pmd_eth_dest_1=f"\\\"1,{adj_mac1}\\\"",
158 pmd_parse_ptype=True,
159 pmd_max_pkt_len=jumbo_frames
162 l3fwd_args = DpdkUtil.get_l3fwd_args(
163 eal_corelist=f"1,{lcores_list}",
165 eal_pci_whitelist0=if_pci0,
166 eal_pci_whitelist1=if_pci1,
168 pmd_config=f"\\\"{port_config.rstrip(u',')}\\\"",
169 pmd_eth_dest_0=f"\\\"0,{adj_mac0}\\\"",
170 pmd_eth_dest_1=f"\\\"1,{adj_mac1}\\\"",
174 command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}"\
175 f"/entry/run_l3fwd.sh \"{l3fwd_args} -P -L -p 0x3\""
176 message = f"Failed to execute l3fwd test at node {node['host']}"
177 exec_cmd_no_error(node, command, timeout=1800, message=message)
180 def check_l3fwd(node):
182 Execute the l3fwd check on the DUT node.
184 :param node: DUT node.
186 :raises RuntimeError: If the script "check_l3fwd.sh" fails.
188 if node[u"type"] == NodeType.DUT:
189 command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}"\
190 f"/entry/check_l3fwd.sh"
191 message = "L3fwd not started properly"
192 exec_cmd_no_error(node, command, timeout=1800, message=message)
195 def get_adj_mac(nodes, node, if1, if2, tg_flip):
197 Get adjacency MAC addresses of the DUT node.
199 Interfaces are re-ordered according to PCI address,
200 but the need to patch and recompile also depends on TG port order.
201 "tg_flip" signals whether TG ports are reordered.
203 :param nodes: All the nodes info in the topology file.
204 :param node: DUT node.
205 :param if1: The test link interface 1.
206 :param if2: The test link interface 2.
207 :param tg_flip: Whether tg ports are reordered.
213 :returns: Returns MAC addresses of adjacency DUT nodes and PCI
219 if_pci0 = Topology.get_interface_pci_addr(node, if_key0)
220 if_pci1 = Topology.get_interface_pci_addr(node, if_key1)
222 # Flipping routes logic:
223 # If TG and DUT ports are reordered -> flip
224 # If TG reordered and DUT not reordered -> don't flip
225 # If DUT reordered and TG not reordered -> don't flip
226 # If DUT and TG not reordered -> flip
228 # Detect which is the port 0.
229 dut_flip = if_pci0 > if_pci1
231 if_key0, if_key1 = if_key1, if_key0
233 L3fwdTest.patch_l3fwd(node, u"patch_l3fwd_flip_routes")
235 L3fwdTest.patch_l3fwd(node, u"patch_l3fwd_flip_routes")
237 adj_node0, adj_if_key0 = Topology.get_adjacent_node_and_interface(
240 adj_node1, adj_if_key1 = Topology.get_adjacent_node_and_interface(
243 if_pci0 = Topology.get_interface_pci_addr(node, if_key0)
244 if_pci1 = Topology.get_interface_pci_addr(node, if_key1)
245 adj_mac0 = Topology.get_interface_mac(adj_node0, adj_if_key0)
246 adj_mac1 = Topology.get_interface_mac(adj_node1, adj_if_key1)
248 return adj_mac0, adj_mac1, if_pci0, if_pci1
251 def patch_l3fwd(node, patch):
253 Patch l3fwd application and recompile.
255 :param node: DUT node.
256 :param patch: Patch to apply.
259 :raises RuntimeError: Patching of l3fwd failed.
261 command = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}"\
262 f"/entry/patch_l3fwd.sh " \
263 f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}"\
265 message = f"Failed to patch l3fwd at node {node['host']}"
266 ret_code, stdout, _ = exec_cmd(node, command, timeout=1800)
267 if ret_code != 0 and u"Skipping patch." not in stdout:
268 raise RuntimeError(message)