blob: 0f1e33d679b92cb59f35326e41d024fa8a003555 [file] [log] [blame]
Naveen Joye4168932022-10-04 14:22:05 -07001#!/usr/bin/env python3
2import unittest
3from ipaddress import ip_address, ip_interface
4from vpp_qemu_utils import (
5 create_namespace,
6 delete_namespace,
7 create_host_interface,
8 delete_host_interfaces,
9 set_interface_mtu,
10 disable_interface_gso,
11 add_namespace_route,
Naveen Joy70335e82023-07-28 16:33:30 -070012 libmemif_test_app,
Naveen Joye4168932022-10-04 14:22:05 -070013)
14from vpp_iperf import start_iperf, stop_iperf
Dave Wallace8800f732023-08-31 00:47:44 -040015from framework import VppTestCase
16from asfframework import VppTestRunner, tag_fixme_debian11, is_distro_debian11
Naveen Joye4168932022-10-04 14:22:05 -070017from config import config
18from vpp_papi import VppEnum
19import time
20import sys
21from vm_test_config import test_config
22
23#
24# Tests for:
Naveen Joy70335e82023-07-28 16:33:30 -070025# - tapv2, tunv2, af_packet_v2/v3 & memif interfaces.
Naveen Joye4168932022-10-04 14:22:05 -070026# - reads test config from the file vm_test_config.py
27# - Uses iPerf to send TCP/IP streams to VPP
28# - VPP ingress interface runs the iperf client
29# - VPP egress interface runs the iperf server
30# - Runs tests specified in the vm_test_config module and verifies that:
31# - TCP over IPv4 and IPv6 is enabled correctly for Bridged and Routed topologies.
32# sending jumbo frames (9000/9001 MTUs) with GSO/GRO is enabled correctly.
33# sending VPP buffer-sized frames(2048 MTU) with GSO/GRO is enabled correctly.
34# sending standard frames (1500 MTU) with GSO/GRO is enabled correctly.
35# sending smaller frames (512 MTU) with GSO/GRO is enabled correctly for IPv4
36# sending odd sized frames (9001, 2049 MTU) with GSO/GRO is enabled correctly.
37#
38
39
Naveen Joy0215ef12024-01-31 08:46:18 -080040class TestSelector:
41 """Selects specified test(s) from vm_test_config to run
42
43 The selected_test field specifies a comma separated or range(s) of
44 tests to run (default='' i.e all_tests) e.g. setting the selected_tests
45 attribute to "1,3-4,19-23" runs tests with ID's 1, 3, 4, 19, 20, 21,
46 22 & 23 from the spec file vm_test_config
47 """
48
49 def __init__(self, selected_tests="") -> None:
50 self.selected_tests = selected_tests
51
52 def filter_tests(self, test):
53 """Works with the filter fn. to include only selected tests."""
54
55 if self.selected_tests:
56 selection = self.selected_tests
57 else:
58 selection = test_config["tests_to_run"]
59
60 if not selection or selection == " ":
61 return True
62
Naveen Joye4168932022-10-04 14:22:05 -070063 test_ids_to_run = []
64 for test_id in selection.split(","):
65 if "-" in test_id.strip():
66 start, end = map(int, test_id.split("-"))
67 test_ids_to_run.extend(list(range(start, end + 1)))
68 elif test_id.strip():
69 test_ids_to_run.append(int(test_id))
70 return test["id"] in test_ids_to_run
71
72
73# Test Config variables
Naveen Joye4168932022-10-04 14:22:05 -070074af_packet_config = test_config["af_packet"]
75layer2 = test_config["L2"]
76layer3 = test_config["L3"]
77
78
79def create_test(test_name, test, ip_version, mtu):
80 """Create and return a unittest method for a test."""
81
Andrew Yourtchenko9ba6dcf2023-06-20 14:52:08 +000082 @unittest.skipIf(
Dave Wallace8800f732023-08-31 00:47:44 -040083 is_distro_debian11, "FIXME intermittent test failures on debian11 distro"
84 )
85 @unittest.skipIf(
Andrew Yourtchenko9ba6dcf2023-06-20 14:52:08 +000086 config.skip_netns_tests, "netns not available or disabled from cli"
87 )
Naveen Joye4168932022-10-04 14:22:05 -070088 def test_func(self):
89 self.logger.debug(f"Starting unittest:{test_name}")
90 self.setUpTestToplogy(test=test, ip_version=ip_version)
91 result = self.set_interfaces_mtu(
92 mtu=mtu,
93 ip_version=ip_version,
94 vpp_interfaces=self.vpp_interfaces,
95 linux_interfaces=self.linux_interfaces,
96 )
Naveen Joy70335e82023-07-28 16:33:30 -070097 if "memif" in self.if_types:
98 self.logger.debug("Starting libmemif test_app for memif test")
99 self.memif_process = libmemif_test_app(
100 memif_sock_path=self.get_memif_sock_path(), logger=self.logger
101 )
Naveen Joye4168932022-10-04 14:22:05 -0700102 if result is True:
Naveen Joy0215ef12024-01-31 08:46:18 -0800103 # Start an instance of an iperf server using
104 # a unique port. Save the iperf cmdline for
105 # terminating the iperf_server process after the test.
106 self.iperf_cmd = start_iperf(
107 ip_version=6,
108 client_ns=self.client_namespace,
109 server_ns=self.server_namespace,
110 server_only=True,
111 server_args=f"-p {self.iperf_port}",
112 logger=self.logger,
113 )
114 # Send traffic between iperf client & server
Naveen Joye4168932022-10-04 14:22:05 -0700115 self.assertTrue(
116 start_iperf(
117 ip_version=ip_version,
Naveen Joy0215ef12024-01-31 08:46:18 -0800118 client_ns=self.client_namespace,
119 server_ns=self.server_namespace,
Naveen Joye4168932022-10-04 14:22:05 -0700120 server_ipv4_address=self.server_ip4_address,
121 server_ipv6_address=self.server_ip6_address,
Naveen Joy0215ef12024-01-31 08:46:18 -0800122 client_args=f"-p {self.iperf_port}",
Naveen Joye4168932022-10-04 14:22:05 -0700123 client_only=True,
124 duration=2,
125 logger=self.logger,
126 )
127 )
128 else:
Klement Sekeraceed1e32024-03-08 10:24:46 +0100129 return unittest.skip(
Naveen Joye4168932022-10-04 14:22:05 -0700130 f"Skipping test:{test_name} as mtu:{mtu} is "
131 f"invalid for TCP/IPv{ip_version}"
132 )
133
134 test_func.__name__ = test_name
135 return test_func
136
137
Naveen Joy0215ef12024-01-31 08:46:18 -0800138def generate_vpp_interface_tests(tests, test_class):
139 """Generate unittests for testing vpp interfaces
140
141 Generates unittests from test spec. and sets them as attributes
142 to the test_class.
143 Args:
144 tests : list of test specs from vm_test_config['tests']
145 test_class : the name of the test class to which the
146 generated tests are set as attributes.
147 """
148
Naveen Joye4168932022-10-04 14:22:05 -0700149 for test in tests:
150 for ip_version in test_config["ip_versions"]:
151 for mtu in test_config["mtus"]:
152 test_name = (
153 f"test_id_{test['id']}_"
154 + f"client_{test['client_if_type']}"
155 + f"_v{test['client_if_version']}_"
156 + f"gso_{test.get('client_if_gso', 0)}_"
157 + f"gro_{test.get('client_if_gro', 0)}_"
Naveen Joyae916b32023-06-27 14:28:57 -0700158 + f"checksum_{test.get('client_if_checksum_offload', 0)}_"
Naveen Joye4168932022-10-04 14:22:05 -0700159 + f"to_server_{test['server_if_type']}"
160 + f"_v{test['server_if_version']}_"
161 + f"gso_{test.get('server_if_gso', 0)}_"
162 + f"gro_{test.get('server_if_gro', 0)}_"
Naveen Joyae916b32023-06-27 14:28:57 -0700163 + f"checksum_{test.get('server_if_checksum_offload', 0)}_"
Naveen Joye4168932022-10-04 14:22:05 -0700164 + f"mtu_{mtu}_mode_{test['x_connect_mode']}_"
165 + f"tcp_ipv{ip_version}"
166 )
167 test_func = create_test(
168 test_name=test_name, test=test, ip_version=ip_version, mtu=mtu
169 )
Naveen Joy0215ef12024-01-31 08:46:18 -0800170 setattr(test_class, test_name, test_func)
Naveen Joye4168932022-10-04 14:22:05 -0700171
172
Dave Wallace8800f732023-08-31 00:47:44 -0400173@tag_fixme_debian11
Naveen Joy0215ef12024-01-31 08:46:18 -0800174class TestVPPInterfacesQemu:
Naveen Joye4168932022-10-04 14:22:05 -0700175 """Test VPP interfaces inside a QEMU VM for IPv4/v6.
176
177 Test Setup:
178 Linux_ns1--iperfClient--host-int1--vpp-af_packet-int1--VPP-BD
179 --vppaf_packet_int2--host-int2--iperfServer--Linux_ns2
180 """
181
Naveen Joye4168932022-10-04 14:22:05 -0700182 def setUpTestToplogy(self, test, ip_version):
183 """Setup the test topology.
184
185 1. Create Linux Namespaces for iPerf Client & Server.
186 2. Create VPP iPerf client and server virtual interfaces.
187 3. Enable desired vif features such as GSO & GRO.
188 3. Cross-Connect interfaces in VPP using L2 or L3.
189 """
Naveen Joy0215ef12024-01-31 08:46:18 -0800190
Naveen Joy70335e82023-07-28 16:33:30 -0700191 # Need to support multiple interface types as the memif interface
192 # in VPP is connected to the iPerf client & server by x-connecting
193 # to a tap interface in their respective namespaces.
194 client_if_types = test["client_if_type"].split(",")
195 server_if_types = test["server_if_type"].split(",")
Naveen Joye4168932022-10-04 14:22:05 -0700196 client_if_version = test["client_if_version"]
197 server_if_version = test["server_if_version"]
198 x_connect_mode = test["x_connect_mode"]
199 # server ip4/ip6 addresses required by iperf client
200 server_ip4_prefix = (
201 layer2["server_ip4_prefix"]
202 if x_connect_mode == "L2"
203 else layer3["server_ip4_prefix"]
204 )
205 server_ip6_prefix = (
206 layer2["server_ip6_prefix"]
207 if x_connect_mode == "L2"
208 else layer3["server_ip6_prefix"]
209 )
210 self.server_ip4_address = str(ip_interface(server_ip4_prefix).ip)
211 self.server_ip6_address = str(ip_interface(server_ip6_prefix).ip)
212 # next-hop IP address on VPP for routing from client & server namespaces
213 vpp_client_prefix = (
214 layer3["vpp_client_ip4_prefix"]
215 if ip_version == 4
216 else layer3["vpp_client_ip6_prefix"]
217 )
218 vpp_client_nexthop = str(ip_interface(vpp_client_prefix).ip)
219 vpp_server_prefix = (
220 layer3["vpp_server_ip4_prefix"]
221 if ip_version == 4
222 else layer3["vpp_server_ip6_prefix"]
223 )
224 vpp_server_nexthop = str(ip_interface(vpp_server_prefix).ip)
Naveen Joy0215ef12024-01-31 08:46:18 -0800225 # Create unique namespaces for iperf client & iperf server to
226 # prevent conflicts when TEST_JOBS > 1
227 self.client_namespace = test_config["client_namespace"] + str(test["id"])
228 self.server_namespace = test_config["server_namespace"] + str(test["id"])
229 create_namespace([self.client_namespace, self.server_namespace])
230 # Set a unique iPerf port for parallel server and client runs
231 self.iperf_port = 5000 + test["id"]
Naveen Joy70335e82023-07-28 16:33:30 -0700232 # IPerf client & server ingress/egress interface indexes in VPP
233 self.tap_interfaces = []
234 self.memif_interfaces = []
235 self.ingress_if_idxes = []
236 self.egress_if_idxes = []
Naveen Joye4168932022-10-04 14:22:05 -0700237 self.vpp_interfaces = []
238 self.linux_interfaces = []
239 enable_client_if_gso = test.get("client_if_gso", 0)
240 enable_server_if_gso = test.get("server_if_gso", 0)
241 enable_client_if_gro = test.get("client_if_gro", 0)
242 enable_server_if_gro = test.get("server_if_gro", 0)
Naveen Joyae916b32023-06-27 14:28:57 -0700243 enable_client_if_checksum_offload = test.get("client_if_checksum_offload", 0)
244 enable_server_if_checksum_offload = test.get("server_if_checksum_offload", 0)
Naveen Joy0215ef12024-01-31 08:46:18 -0800245
246 # Create unique host interfaces in Linux and VPP for connecting to iperf
247 # client & iperf server to prevent conflicts when TEST_JOBS > 1
248 self.iprf_client_host_interface_on_linux = af_packet_config[
249 "iprf_client_interface_on_linux"
250 ] + str(test["id"])
251 self.iprf_client_host_interface_on_vpp = af_packet_config[
252 "iprf_client_interface_on_vpp"
253 ] + str(test["id"])
254 self.iprf_server_host_interface_on_linux = af_packet_config[
255 "iprf_server_interface_on_linux"
256 ] + str(test["id"])
257 self.iprf_server_host_interface_on_vpp = af_packet_config[
258 "iprf_server_interface_on_vpp"
259 ] + str(test["id"])
260 # Handle client interface types
Naveen Joy70335e82023-07-28 16:33:30 -0700261 for client_if_type in client_if_types:
262 if client_if_type == "af_packet":
263 create_host_interface(
Naveen Joy0215ef12024-01-31 08:46:18 -0800264 self.iprf_client_host_interface_on_linux,
265 self.iprf_client_host_interface_on_vpp,
266 self.client_namespace,
Dave Wallace940a70f2024-04-03 12:31:42 -0400267 (
268 layer2["client_ip4_prefix"]
269 if x_connect_mode == "L2"
270 else layer3["client_ip4_prefix"]
271 ),
272 (
273 layer2["client_ip6_prefix"]
274 if x_connect_mode == "L2"
275 else layer3["client_ip6_prefix"]
276 ),
Naveen Joye4168932022-10-04 14:22:05 -0700277 )
Naveen Joy70335e82023-07-28 16:33:30 -0700278 self.ingress_if_idx = self.create_af_packet(
279 version=client_if_version,
Naveen Joy0215ef12024-01-31 08:46:18 -0800280 host_if_name=self.iprf_client_host_interface_on_vpp,
Naveen Joy70335e82023-07-28 16:33:30 -0700281 enable_gso=enable_client_if_gso,
Naveen Joye4168932022-10-04 14:22:05 -0700282 )
Naveen Joy70335e82023-07-28 16:33:30 -0700283 self.ingress_if_idxes.append(self.ingress_if_idx)
284 self.vpp_interfaces.append(self.ingress_if_idx)
285 self.linux_interfaces.append(
Naveen Joy0215ef12024-01-31 08:46:18 -0800286 ["", self.iprf_client_host_interface_on_vpp]
Naveen Joye4168932022-10-04 14:22:05 -0700287 )
Naveen Joy70335e82023-07-28 16:33:30 -0700288 self.linux_interfaces.append(
289 [
Naveen Joy0215ef12024-01-31 08:46:18 -0800290 self.client_namespace,
291 self.iprf_client_host_interface_on_linux,
Naveen Joy70335e82023-07-28 16:33:30 -0700292 ]
Naveen Joye4168932022-10-04 14:22:05 -0700293 )
Naveen Joy70335e82023-07-28 16:33:30 -0700294 if enable_client_if_gso == 0:
Naveen Joy0215ef12024-01-31 08:46:18 -0800295 disable_interface_gso("", self.iprf_client_host_interface_on_vpp)
Naveen Joy70335e82023-07-28 16:33:30 -0700296 disable_interface_gso(
Naveen Joy0215ef12024-01-31 08:46:18 -0800297 self.client_namespace,
298 self.iprf_client_host_interface_on_linux,
Naveen Joy70335e82023-07-28 16:33:30 -0700299 )
300 elif client_if_type == "tap" or client_if_type == "tun":
301 self.ingress_if_idx = self.create_tap_tun(
302 id=101,
Naveen Joy0215ef12024-01-31 08:46:18 -0800303 host_namespace=self.client_namespace,
Naveen Joy70335e82023-07-28 16:33:30 -0700304 ip_version=ip_version,
Dave Wallace940a70f2024-04-03 12:31:42 -0400305 host_ip4_prefix=(
306 layer2["client_ip4_prefix"]
307 if x_connect_mode == "L2"
308 else layer3["client_ip4_prefix"]
309 ),
310 host_ip6_prefix=(
311 layer2["client_ip6_prefix"]
312 if x_connect_mode == "L2"
313 else layer3["client_ip6_prefix"]
314 ),
315 host_ip4_gw=(
316 vpp_client_nexthop
317 if x_connect_mode == "L3" and ip_version == 4
318 else None
319 ),
320 host_ip6_gw=(
321 vpp_client_nexthop
322 if x_connect_mode == "L3" and ip_version == 6
323 else None
324 ),
Naveen Joy70335e82023-07-28 16:33:30 -0700325 int_type=client_if_type,
326 host_if_name=f"{client_if_type}0",
327 enable_gso=enable_client_if_gso,
328 enable_gro=enable_client_if_gro,
329 enable_checksum_offload=enable_client_if_checksum_offload,
330 )
331 self.tap_interfaces.append(self.ingress_if_idx)
332 self.ingress_if_idxes.append(self.ingress_if_idx)
333 self.vpp_interfaces.append(self.ingress_if_idx)
Naveen Joy0215ef12024-01-31 08:46:18 -0800334 self.linux_interfaces.append(
335 [self.client_namespace, f"{client_if_type}0"]
336 )
Naveen Joy70335e82023-07-28 16:33:30 -0700337 # Seeing TCP timeouts if tx=on & rx=on Linux tap & tun interfaces
Naveen Joy0215ef12024-01-31 08:46:18 -0800338 disable_interface_gso(self.client_namespace, f"{client_if_type}0")
Naveen Joy70335e82023-07-28 16:33:30 -0700339 elif client_if_type == "memif":
340 self.ingress_if_idx = self.create_memif(
341 memif_id=0, mode=0 if x_connect_mode == "L2" else 1
342 )
343 self.memif_interfaces.append(self.ingress_if_idx)
344 self.ingress_if_idxes.append(self.ingress_if_idx)
345 self.vpp_interfaces.append(self.ingress_if_idx)
Naveen Joye4168932022-10-04 14:22:05 -0700346 else:
Naveen Joy70335e82023-07-28 16:33:30 -0700347 print(
348 f"Unsupported client interface type: {client_if_type} "
349 f"for test - ID={test['id']}"
350 )
351 sys.exit(1)
352 for server_if_type in server_if_types:
353 if server_if_type == "af_packet":
354 create_host_interface(
Naveen Joy0215ef12024-01-31 08:46:18 -0800355 self.iprf_server_host_interface_on_linux,
356 self.iprf_server_host_interface_on_vpp,
357 self.server_namespace,
Naveen Joy70335e82023-07-28 16:33:30 -0700358 server_ip4_prefix,
359 server_ip6_prefix,
360 )
361 self.egress_if_idx = self.create_af_packet(
362 version=server_if_version,
Naveen Joy0215ef12024-01-31 08:46:18 -0800363 host_if_name=self.iprf_server_host_interface_on_vpp,
Naveen Joy70335e82023-07-28 16:33:30 -0700364 enable_gso=enable_server_if_gso,
365 )
366 self.egress_if_idxes.append(self.egress_if_idx)
367 self.vpp_interfaces.append(self.egress_if_idx)
368 self.linux_interfaces.append(
Naveen Joy0215ef12024-01-31 08:46:18 -0800369 ["", self.iprf_server_host_interface_on_vpp]
Naveen Joy70335e82023-07-28 16:33:30 -0700370 )
371 self.linux_interfaces.append(
372 [
Naveen Joy0215ef12024-01-31 08:46:18 -0800373 self.server_namespace,
374 self.iprf_server_host_interface_on_linux,
Naveen Joy70335e82023-07-28 16:33:30 -0700375 ]
376 )
377 if enable_server_if_gso == 0:
Naveen Joy0215ef12024-01-31 08:46:18 -0800378 disable_interface_gso("", self.iprf_server_host_interface_on_vpp)
Naveen Joy70335e82023-07-28 16:33:30 -0700379 disable_interface_gso(
Naveen Joy0215ef12024-01-31 08:46:18 -0800380 self.server_namespace,
381 self.iprf_server_host_interface_on_linux,
Naveen Joy70335e82023-07-28 16:33:30 -0700382 )
383 elif server_if_type == "tap" or server_if_type == "tun":
384 self.egress_if_idx = self.create_tap_tun(
385 id=102,
Naveen Joy0215ef12024-01-31 08:46:18 -0800386 host_namespace=self.server_namespace,
Naveen Joy70335e82023-07-28 16:33:30 -0700387 ip_version=ip_version,
Dave Wallace940a70f2024-04-03 12:31:42 -0400388 host_ip4_prefix=(
389 layer2["server_ip4_prefix"]
390 if x_connect_mode == "L2"
391 else layer3["server_ip4_prefix"]
392 ),
393 host_ip6_prefix=(
394 layer2["server_ip6_prefix"]
395 if x_connect_mode == "L2"
396 else layer3["server_ip6_prefix"]
397 ),
Naveen Joy70335e82023-07-28 16:33:30 -0700398 int_type=server_if_type,
399 host_if_name=f"{server_if_type}0",
400 enable_gso=enable_server_if_gso,
401 enable_gro=enable_server_if_gro,
402 enable_checksum_offload=enable_server_if_checksum_offload,
403 )
404 self.tap_interfaces.append(self.egress_if_idx)
405 self.egress_if_idxes.append(self.egress_if_idx)
406 self.vpp_interfaces.append(self.egress_if_idx)
Naveen Joy0215ef12024-01-31 08:46:18 -0800407 self.linux_interfaces.append(
408 [self.server_namespace, f"{server_if_type}0"]
409 )
Naveen Joy70335e82023-07-28 16:33:30 -0700410 # Seeing TCP timeouts if tx=on & rx=on Linux tap & tun interfaces
Naveen Joy0215ef12024-01-31 08:46:18 -0800411 disable_interface_gso(self.server_namespace, f"{server_if_type}0")
Naveen Joy70335e82023-07-28 16:33:30 -0700412 elif server_if_type == "memif":
413 self.egress_if_idx = self.create_memif(
414 memif_id=1, mode=0 if x_connect_mode == "L2" else 1
415 )
416 self.memif_interfaces.append(self.egress_if_idx)
417 self.egress_if_idxes.append(self.egress_if_idx)
418 self.vpp_interfaces.append(self.egress_if_idx)
419 else:
420 print(
421 f"Unsupported server interface type: {server_if_type} "
422 f"for test - ID={test['id']}"
423 )
424 sys.exit(1)
425 self.if_types = set(client_if_types).union(set(server_if_types))
426 # for memif testing: tapv2, memif & libmemif_app are connected
427 if "memif" not in self.if_types:
428 if x_connect_mode == "L2":
429 self.l2_connect_interfaces(1, self.ingress_if_idx, self.egress_if_idx)
430 elif x_connect_mode == "L3":
431 # L3 connect client & server side
432 vrf_id = layer3["ip4_vrf"] if ip_version == 4 else layer3["ip6_vrf"]
433 self.l3_connect_interfaces(
434 ip_version,
435 vrf_id,
436 (self.ingress_if_idx, vpp_client_prefix),
437 (self.egress_if_idx, vpp_server_prefix),
438 )
439 # Setup namespace routing
440 if ip_version == 4:
441 add_namespace_route(
Naveen Joy0215ef12024-01-31 08:46:18 -0800442 self.client_namespace, "0.0.0.0/0", vpp_client_nexthop
Naveen Joy70335e82023-07-28 16:33:30 -0700443 )
444 add_namespace_route(
Naveen Joy0215ef12024-01-31 08:46:18 -0800445 self.server_namespace, "0.0.0.0/0", vpp_server_nexthop
Naveen Joy70335e82023-07-28 16:33:30 -0700446 )
447 else:
Naveen Joy0215ef12024-01-31 08:46:18 -0800448 add_namespace_route(
449 self.client_namespace, "::/0", vpp_client_nexthop
450 )
451 add_namespace_route(
452 self.server_namespace, "::/0", vpp_server_nexthop
453 )
Naveen Joy70335e82023-07-28 16:33:30 -0700454 else:
455 # connect: ingress tap & memif & egress tap and memif interfaces
456 if x_connect_mode == "L2":
457 self.l2_connect_interfaces(1, *self.ingress_if_idxes)
458 self.l2_connect_interfaces(2, *self.egress_if_idxes)
Naveen Joye4168932022-10-04 14:22:05 -0700459 # Wait for Linux IPv6 stack to become ready
460 if ip_version == 6:
461 time.sleep(2)
462
463 def tearDown(self):
464 try:
Naveen Joy70335e82023-07-28 16:33:30 -0700465 for interface_if_idx in self.tap_interfaces:
466 self.vapi.tap_delete_v2(sw_if_index=interface_if_idx)
Naveen Joye4168932022-10-04 14:22:05 -0700467 except Exception:
468 pass
469 try:
Naveen Joy70335e82023-07-28 16:33:30 -0700470 for interface_if_idx in self.memif_interfaces:
471 self.vapi.memif_delete(sw_if_index=interface_if_idx)
Naveen Joye4168932022-10-04 14:22:05 -0700472 except Exception:
473 pass
474 try:
475 for interface in self.vapi.af_packet_dump():
Naveen Joy0215ef12024-01-31 08:46:18 -0800476 if interface.host_if_name == self.iprf_client_host_interface_on_vpp:
477 self.vapi.af_packet_delete(self.iprf_client_host_interface_on_vpp)
478 elif interface.host_if_name == self.iprf_server_host_interface_on_vpp:
479 self.vapi.af_packet_delete(self.iprf_server_host_interface_on_vpp)
Naveen Joye4168932022-10-04 14:22:05 -0700480 except Exception:
481 pass
482 try:
483 delete_host_interfaces(
Naveen Joy0215ef12024-01-31 08:46:18 -0800484 self.iprf_client_host_interface_on_linux,
485 self.iprf_server_host_interface_on_linux,
486 self.iprf_client_host_interface_on_vpp,
487 self.iprf_server_host_interface_on_vpp,
Naveen Joye4168932022-10-04 14:22:05 -0700488 )
489 except Exception:
490 pass
491 try:
BenoƮt Ganneff570d32024-04-16 09:36:05 +0200492 self.vapi.ip_table_add_del_v2(
493 is_add=0, table={"table_id": layer3["ip4_vrf"]}
494 )
Naveen Joye4168932022-10-04 14:22:05 -0700495 except Exception:
496 pass
497 try:
BenoƮt Ganneff570d32024-04-16 09:36:05 +0200498 self.vapi.ip_table_add_del_v2(
499 is_add=0, table={"table_id": layer3["ip6_vrf"]}
500 )
Naveen Joye4168932022-10-04 14:22:05 -0700501 except Exception:
502 pass
503 try:
Naveen Joy70335e82023-07-28 16:33:30 -0700504 self.vapi.bridge_domain_add_del_v2(bd_id=1, is_add=0)
505 self.vapi.bridge_domain_add_del_v2(bd_id=2, is_add=0)
506 except Exception:
507 pass
508 try:
Naveen Joye4168932022-10-04 14:22:05 -0700509 delete_namespace(
510 [
Naveen Joy0215ef12024-01-31 08:46:18 -0800511 self.client_namespace,
512 self.server_namespace,
Naveen Joye4168932022-10-04 14:22:05 -0700513 ]
514 )
515 except Exception:
516 pass
517 try:
Naveen Joy0215ef12024-01-31 08:46:18 -0800518 if hasattr(self, "iperf_cmd"):
519 stop_iperf(" ".join(self.iperf_cmd))
Naveen Joye4168932022-10-04 14:22:05 -0700520 except Exception:
521 pass
Naveen Joy70335e82023-07-28 16:33:30 -0700522 try:
523 if self.memif_process:
524 self.memif_process.terminate()
525 self.memif_process.join()
526 except Exception:
527 pass
Naveen Joye4168932022-10-04 14:22:05 -0700528
529 def create_af_packet(self, version, host_if_name, enable_gso=0):
530 """Create an af_packetv3 interface in VPP.
531
532 Parameters:
533 version -- 2 for af_packet_create_v2
534 -- 3 for af_packet_create_v3
535 host_if_name -- host interface name
536 enable_gso -- Enable GSO on the interface when True
537 """
538 af_packet_mode = VppEnum.vl_api_af_packet_mode_t
539 af_packet_interface_mode = af_packet_mode.AF_PACKET_API_MODE_ETHERNET
540 af_packet_flags = VppEnum.vl_api_af_packet_flags_t
541 af_packet_interface_flags = af_packet_flags.AF_PACKET_API_FLAG_QDISC_BYPASS
542 if enable_gso:
543 af_packet_interface_flags = (
544 af_packet_interface_flags | af_packet_flags.AF_PACKET_API_FLAG_CKSUM_GSO
545 )
546 if version == 2:
547 af_packet_interface_flags = (
548 af_packet_interface_flags | af_packet_flags.AF_PACKET_API_FLAG_VERSION_2
549 )
550 api_args = {
551 "use_random_hw_addr": True,
552 "host_if_name": host_if_name,
553 "flags": af_packet_interface_flags,
554 }
555 api_args["mode"] = af_packet_interface_mode
556 result = self.vapi.af_packet_create_v3(**api_args)
557 sw_if_index = result.sw_if_index
558 # Enable software GSO chunking when interface doesn't support GSO offload
559 if enable_gso == 0:
560 self.vapi.feature_gso_enable_disable(
561 sw_if_index=sw_if_index, enable_disable=1
562 )
563 else:
564 self.vapi.feature_gso_enable_disable(
565 sw_if_index=sw_if_index, enable_disable=0
566 )
567 self.vapi.sw_interface_set_flags(sw_if_index=sw_if_index, flags=1)
568 return sw_if_index
569
570 def create_tap_tun(
571 self,
572 id,
573 host_namespace,
574 ip_version,
575 host_ip4_prefix=None,
576 host_ip6_prefix=None,
577 host_ip4_gw=None,
578 host_ip6_gw=None,
579 int_type="tap",
580 host_if_name=None,
581 enable_gso=0,
582 enable_gro=0,
Naveen Joyae916b32023-06-27 14:28:57 -0700583 enable_checksum_offload=0,
Naveen Joye4168932022-10-04 14:22:05 -0700584 ):
585 """Create a tapv2 or tunv2 interface in VPP and attach to host.
586
587 Parameters:
588 id -- interface ID
589 host_namespace -- host namespace to attach the tap/tun interface to
590 ip_version -- 4 or 6
591 host_ip4_prefix -- ipv4 host interface address in CIDR notation
592 if ip_version=4
593 host_ip6_prefix -- ipv6 host interface address in CIDR notation
594 if ip_version=6
595 host_ip4_gw -- host IPv4 default gateway IP Address
596 host_ip6_gw -- host IPv6 default gateway IP address
597 int_type -- "tap" for tapv2 & "tun" for tunv2 interface
598 host_if_name -- host side interface name
599 enable_gso -- enable GSO
600 enable_gro -- enable GSO/GRO-Coalesce
Naveen Joyae916b32023-06-27 14:28:57 -0700601 enable_checksum_offload -- enable checksum offload without gso
Naveen Joye4168932022-10-04 14:22:05 -0700602 """
603 TapFlags = VppEnum.vl_api_tap_flags_t
604 tap_flags = 0
605 if int_type == "tun":
606 tap_flags = TapFlags.TAP_API_FLAG_TUN
607 if enable_gro:
608 tap_flags = tap_flags | (
609 TapFlags.TAP_API_FLAG_GSO | TapFlags.TAP_API_FLAG_GRO_COALESCE
610 )
611 elif enable_gso:
612 tap_flags = tap_flags | TapFlags.TAP_API_FLAG_GSO
Naveen Joyae916b32023-06-27 14:28:57 -0700613 elif enable_checksum_offload:
614 tap_flags = tap_flags | TapFlags.TAP_API_FLAG_CSUM_OFFLOAD
Naveen Joye4168932022-10-04 14:22:05 -0700615 elif int_type == "tap":
616 if enable_gro:
617 tap_flags = (
618 TapFlags.TAP_API_FLAG_GSO | TapFlags.TAP_API_FLAG_GRO_COALESCE
619 )
620 elif enable_gso:
621 tap_flags = TapFlags.TAP_API_FLAG_GSO
Naveen Joyae916b32023-06-27 14:28:57 -0700622 elif enable_checksum_offload:
623 tap_flags = tap_flags | TapFlags.TAP_API_FLAG_CSUM_OFFLOAD
Naveen Joye4168932022-10-04 14:22:05 -0700624
625 api_args = {
626 "id": id,
627 "host_namespace_set": True,
628 "host_namespace": host_namespace,
629 "host_if_name_set": False,
630 "host_bridge_set": False,
631 "host_mac_addr_set": False,
632 }
633 if tap_flags != 0:
634 api_args["tap_flags"] = tap_flags
635 if ip_version == 4:
636 api_args["host_ip4_prefix"] = ip_interface(host_ip4_prefix)
637 api_args["host_ip4_prefix_set"] = True
638 if host_ip4_gw:
639 api_args["host_ip4_gw"] = ip_address(host_ip4_gw)
640 api_args["host_ip4_gw_set"] = True
641 if ip_version == 6:
642 api_args["host_ip6_prefix"] = ip_interface(host_ip6_prefix)
643 api_args["host_ip6_prefix_set"] = True
644 if host_ip6_gw:
645 api_args["host_ip6_gw"] = ip_address(host_ip6_gw)
646 api_args["host_ip6_gw_set"] = True
647 if host_if_name:
648 api_args["host_if_name"] = host_if_name
649 api_args["host_if_name_set"] = True
650
651 result = self.vapi.tap_create_v2(**api_args)
652 sw_if_index = result.sw_if_index
653 # Enable software GSO chunking when interface doesn't support GSO offload and
654 # GRO coalesce
655 if enable_gso == 0 and enable_gro == 0:
656 self.vapi.feature_gso_enable_disable(
657 sw_if_index=sw_if_index, enable_disable=1
658 )
659 else:
660 self.vapi.feature_gso_enable_disable(
661 sw_if_index=sw_if_index, enable_disable=0
662 )
663 # Admin up
664 self.vapi.sw_interface_set_flags(sw_if_index=sw_if_index, flags=1)
665 return sw_if_index
666
Naveen Joy70335e82023-07-28 16:33:30 -0700667 def create_memif(self, memif_id, mode):
668 """Create memif interface in VPP.
669
670 Parameters:
671 memif_id: A unique ID for the memif interface
672 mode: 0 = ethernet, 1 = ip, 2 = punt/inject
673 """
674 # create memif interface with role=0 (i.e. master)
675 result = self.vapi.memif_create_v2(
676 role=0, mode=mode, id=memif_id, buffer_size=9216
677 )
678 sw_if_index = result.sw_if_index
679 self.vapi.sw_interface_set_flags(sw_if_index=sw_if_index, flags=1)
680 return sw_if_index
681
Naveen Joye4168932022-10-04 14:22:05 -0700682 def dump_bridge_domain_details(self, bd_id):
683 return self.vapi.bridge_domain_dump(bd_id=bd_id)
684
685 def l2_connect_interfaces(self, bridge_id, *sw_if_idxs):
686 for if_idx in sw_if_idxs:
687 self.vapi.sw_interface_set_l2_bridge(
688 rx_sw_if_index=if_idx, bd_id=bridge_id, shg=0, port_type=0, enable=True
689 )
690
691 def l3_connect_interfaces(self, ip_version, vrf_id, *if_idx_ip_prefixes):
692 """Setup routing for (if_idx, ip_prefix) inside VPP.
693
694 arguments:
695 if_idx_ip_prefixes -- sequence of (if_idx, ip_prefix) tuples
696 ip_version -- 4 or 6
697 vrf_id -- vrf_id
698 """
699 is_ipv6 = 0 if ip_version == 4 else 1
BenoƮt Ganneff570d32024-04-16 09:36:05 +0200700 self.vapi.ip_table_add_del_v2(
Naveen Joye4168932022-10-04 14:22:05 -0700701 is_add=1, table={"table_id": vrf_id, "is_ip6": is_ipv6}
702 )
703 for sw_if_index, ip_prefix in if_idx_ip_prefixes:
704 self.vapi.sw_interface_set_table(
705 sw_if_index=sw_if_index, is_ipv6=is_ipv6, vrf_id=vrf_id
706 )
707 self.vapi.sw_interface_add_del_address(
708 sw_if_index=sw_if_index, is_add=1, prefix=ip_interface(ip_prefix)
709 )
710
711 def set_interfaces_mtu(self, mtu, ip_version, **kwargs):
712 """Set MTUs on VPP and Linux interfaces.
713
714 arguments --
715 mtu -- mtu value
716 ip_version - 4 or 6
717 kwargs['vpp_interfaces'] -- list of vpp interface if indexes
718 kwargs['linux_interfaces'] -- list of tuples (namespace, interface_names)
719 return True if mtu is set, else False
720 """
721 vpp_interfaces = kwargs.get("vpp_interfaces")
722 linux_interfaces = kwargs.get("linux_interfaces")
723 # IPv6 on Linux requires an MTU value >=1280
724 if (ip_version == 6 and mtu >= 1280) or ip_version == 4:
725 for sw_if_idx in vpp_interfaces:
726 self.vapi.sw_interface_set_mtu(
727 sw_if_index=sw_if_idx, mtu=[mtu, 0, 0, 0]
728 )
729 for namespace, interface_name in linux_interfaces:
730 set_interface_mtu(
731 namespace=namespace,
732 interface=interface_name,
733 mtu=mtu,
734 logger=self.logger,
735 )
736 return True
737 else:
738 return False
739
740
Naveen Joye4168932022-10-04 14:22:05 -0700741if __name__ == "__main__":
742 unittest.main(testRunner=VppTestRunner)