blob: 7d902c5a1a8238f7ff5d7bc002dcfa41da347c18 [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,
12)
13from vpp_iperf import start_iperf, stop_iperf
14from framework import VppTestCase, VppTestRunner
15from config import config
16from vpp_papi import VppEnum
17import time
18import sys
19from vm_test_config import test_config
20
21#
22# Tests for:
23# - tapv2, tunv2 & af_packet_v2 & v3 interfaces.
24# - reads test config from the file vm_test_config.py
25# - Uses iPerf to send TCP/IP streams to VPP
26# - VPP ingress interface runs the iperf client
27# - VPP egress interface runs the iperf server
28# - Runs tests specified in the vm_test_config module and verifies that:
29# - TCP over IPv4 and IPv6 is enabled correctly for Bridged and Routed topologies.
30# sending jumbo frames (9000/9001 MTUs) with GSO/GRO is enabled correctly.
31# sending VPP buffer-sized frames(2048 MTU) with GSO/GRO is enabled correctly.
32# sending standard frames (1500 MTU) with GSO/GRO is enabled correctly.
33# sending smaller frames (512 MTU) with GSO/GRO is enabled correctly for IPv4
34# sending odd sized frames (9001, 2049 MTU) with GSO/GRO is enabled correctly.
35#
36
37
38def filter_tests(test):
39 """Filter test IDs to include only those selected to run."""
40 selection = test_config["tests_to_run"]
41 if not selection or selection == " ":
42 return True
43 else:
44 test_ids_to_run = []
45 for test_id in selection.split(","):
46 if "-" in test_id.strip():
47 start, end = map(int, test_id.split("-"))
48 test_ids_to_run.extend(list(range(start, end + 1)))
49 elif test_id.strip():
50 test_ids_to_run.append(int(test_id))
51 return test["id"] in test_ids_to_run
52
53
54# Test Config variables
55client_namespace = test_config["client_namespace"]
56server_namespace = test_config["server_namespace"]
57tests = filter(filter_tests, test_config["tests"])
58af_packet_config = test_config["af_packet"]
59layer2 = test_config["L2"]
60layer3 = test_config["L3"]
61
62
63def create_test(test_name, test, ip_version, mtu):
64 """Create and return a unittest method for a test."""
65
66 @unittest.skipUnless(config.extended, "part of extended tests")
67 def test_func(self):
68 self.logger.debug(f"Starting unittest:{test_name}")
69 self.setUpTestToplogy(test=test, ip_version=ip_version)
70 result = self.set_interfaces_mtu(
71 mtu=mtu,
72 ip_version=ip_version,
73 vpp_interfaces=self.vpp_interfaces,
74 linux_interfaces=self.linux_interfaces,
75 )
76 # Start the Iperf server in dual stack mode & run iperf client
77 if result is True:
78 start_iperf(ip_version=6, server_only=True, logger=self.logger)
79 self.assertTrue(
80 start_iperf(
81 ip_version=ip_version,
82 server_ipv4_address=self.server_ip4_address,
83 server_ipv6_address=self.server_ip6_address,
84 client_only=True,
85 duration=2,
86 logger=self.logger,
87 )
88 )
89 else:
90 print(
91 f"Skipping test:{test_name} as mtu:{mtu} is "
92 f"invalid for TCP/IPv{ip_version}"
93 )
94
95 test_func.__name__ = test_name
96 return test_func
97
98
99def generate_vpp_interface_tests():
100 """Generate unittests for testing vpp interfaces."""
101 for test in tests:
102 for ip_version in test_config["ip_versions"]:
103 for mtu in test_config["mtus"]:
104 test_name = (
105 f"test_id_{test['id']}_"
106 + f"client_{test['client_if_type']}"
107 + f"_v{test['client_if_version']}_"
108 + f"gso_{test.get('client_if_gso', 0)}_"
109 + f"gro_{test.get('client_if_gro', 0)}_"
110 + f"to_server_{test['server_if_type']}"
111 + f"_v{test['server_if_version']}_"
112 + f"gso_{test.get('server_if_gso', 0)}_"
113 + f"gro_{test.get('server_if_gro', 0)}_"
114 + f"mtu_{mtu}_mode_{test['x_connect_mode']}_"
115 + f"tcp_ipv{ip_version}"
116 )
117 test_func = create_test(
118 test_name=test_name, test=test, ip_version=ip_version, mtu=mtu
119 )
120 setattr(TestVPPInterfacesQemu, test_name, test_func)
121
122
123class TestVPPInterfacesQemu(VppTestCase):
124 """Test VPP interfaces inside a QEMU VM for IPv4/v6.
125
126 Test Setup:
127 Linux_ns1--iperfClient--host-int1--vpp-af_packet-int1--VPP-BD
128 --vppaf_packet_int2--host-int2--iperfServer--Linux_ns2
129 """
130
131 @classmethod
132 def setUpClass(cls):
133 super(TestVPPInterfacesQemu, cls).setUpClass()
134
135 @classmethod
136 def tearDownClass(cls):
137 super(TestVPPInterfacesQemu, cls).tearDownClass()
138
139 def setUpTestToplogy(self, test, ip_version):
140 """Setup the test topology.
141
142 1. Create Linux Namespaces for iPerf Client & Server.
143 2. Create VPP iPerf client and server virtual interfaces.
144 3. Enable desired vif features such as GSO & GRO.
145 3. Cross-Connect interfaces in VPP using L2 or L3.
146 """
147 super(TestVPPInterfacesQemu, self).setUp()
148 client_if_type = test["client_if_type"]
149 server_if_type = test["server_if_type"]
150 client_if_version = test["client_if_version"]
151 server_if_version = test["server_if_version"]
152 x_connect_mode = test["x_connect_mode"]
153 # server ip4/ip6 addresses required by iperf client
154 server_ip4_prefix = (
155 layer2["server_ip4_prefix"]
156 if x_connect_mode == "L2"
157 else layer3["server_ip4_prefix"]
158 )
159 server_ip6_prefix = (
160 layer2["server_ip6_prefix"]
161 if x_connect_mode == "L2"
162 else layer3["server_ip6_prefix"]
163 )
164 self.server_ip4_address = str(ip_interface(server_ip4_prefix).ip)
165 self.server_ip6_address = str(ip_interface(server_ip6_prefix).ip)
166 # next-hop IP address on VPP for routing from client & server namespaces
167 vpp_client_prefix = (
168 layer3["vpp_client_ip4_prefix"]
169 if ip_version == 4
170 else layer3["vpp_client_ip6_prefix"]
171 )
172 vpp_client_nexthop = str(ip_interface(vpp_client_prefix).ip)
173 vpp_server_prefix = (
174 layer3["vpp_server_ip4_prefix"]
175 if ip_version == 4
176 else layer3["vpp_server_ip6_prefix"]
177 )
178 vpp_server_nexthop = str(ip_interface(vpp_server_prefix).ip)
179 create_namespace([client_namespace, server_namespace])
180 self.vpp_interfaces = []
181 self.linux_interfaces = []
182 enable_client_if_gso = test.get("client_if_gso", 0)
183 enable_server_if_gso = test.get("server_if_gso", 0)
184 enable_client_if_gro = test.get("client_if_gro", 0)
185 enable_server_if_gro = test.get("server_if_gro", 0)
186 ## Handle client interface types
187 if client_if_type == "af_packet":
188 create_host_interface(
189 af_packet_config["iprf_client_interface_on_linux"],
190 af_packet_config["iprf_client_interface_on_vpp"],
191 client_namespace,
192 layer2["client_ip4_prefix"]
193 if x_connect_mode == "L2"
194 else layer3["client_ip4_prefix"],
195 layer2["client_ip6_prefix"]
196 if x_connect_mode == "L2"
197 else layer3["client_ip6_prefix"],
198 )
199 self.ingress_if_idx = self.create_af_packet(
200 version=client_if_version,
201 host_if_name=af_packet_config["iprf_client_interface_on_vpp"],
202 enable_gso=enable_client_if_gso,
203 )
204 self.vpp_interfaces.append(self.ingress_if_idx)
205 self.linux_interfaces.append(
206 ["", af_packet_config["iprf_client_interface_on_vpp"]]
207 )
208 self.linux_interfaces.append(
209 [client_namespace, af_packet_config["iprf_client_interface_on_linux"]]
210 )
211 if enable_client_if_gso == 0:
212 disable_interface_gso(
213 "", af_packet_config["iprf_client_interface_on_vpp"]
214 )
215 disable_interface_gso(
216 client_namespace, af_packet_config["iprf_client_interface_on_linux"]
217 )
218 elif client_if_type == "tap" or client_if_type == "tun":
219 self.ingress_if_idx = self.create_tap_tun(
220 id=101,
221 host_namespace=client_namespace,
222 ip_version=ip_version,
223 host_ip4_prefix=layer2["client_ip4_prefix"]
224 if x_connect_mode == "L2"
225 else layer3["client_ip4_prefix"],
226 host_ip6_prefix=layer2["client_ip6_prefix"]
227 if x_connect_mode == "L2"
228 else layer3["client_ip6_prefix"],
229 host_ip4_gw=vpp_client_nexthop
230 if x_connect_mode == "L3" and ip_version == 4
231 else None,
232 host_ip6_gw=vpp_client_nexthop
233 if x_connect_mode == "L3" and ip_version == 6
234 else None,
235 int_type=client_if_type,
236 host_if_name=f"{client_if_type}0",
237 enable_gso=enable_client_if_gso,
238 enable_gro=enable_client_if_gro,
239 )
240 self.vpp_interfaces.append(self.ingress_if_idx)
241 self.linux_interfaces.append([client_namespace, f"{client_if_type}0"])
242 # Seeing TCP timeouts if tx=on & rx=on Linux tap & tun interfaces
243 disable_interface_gso(client_namespace, f"{client_if_type}0")
244 else:
245 print(
246 f"Unsupported client interface type: {client_if_type} "
247 f"for test - ID={test['id']}"
248 )
249 sys.exit(1)
250
251 if server_if_type == "af_packet":
252 create_host_interface(
253 af_packet_config["iprf_server_interface_on_linux"],
254 af_packet_config["iprf_server_interface_on_vpp"],
255 server_namespace,
256 server_ip4_prefix,
257 server_ip6_prefix,
258 )
259 self.egress_if_idx = self.create_af_packet(
260 version=server_if_version,
261 host_if_name=af_packet_config["iprf_server_interface_on_vpp"],
262 enable_gso=enable_server_if_gso,
263 )
264 self.vpp_interfaces.append(self.egress_if_idx)
265 self.linux_interfaces.append(
266 ["", af_packet_config["iprf_server_interface_on_vpp"]]
267 )
268 self.linux_interfaces.append(
269 [server_namespace, af_packet_config["iprf_server_interface_on_linux"]]
270 )
271 if enable_server_if_gso == 0:
272 disable_interface_gso(
273 "", af_packet_config["iprf_server_interface_on_vpp"]
274 )
275 disable_interface_gso(
276 server_namespace, af_packet_config["iprf_server_interface_on_linux"]
277 )
278 elif server_if_type == "tap" or server_if_type == "tun":
279 self.egress_if_idx = self.create_tap_tun(
280 id=102,
281 host_namespace=server_namespace,
282 ip_version=ip_version,
283 host_ip4_prefix=layer2["server_ip4_prefix"]
284 if x_connect_mode == "L2"
285 else layer3["server_ip4_prefix"],
286 host_ip6_prefix=layer2["server_ip6_prefix"]
287 if x_connect_mode == "L2"
288 else layer3["server_ip6_prefix"],
289 int_type=server_if_type,
290 host_if_name=f"{server_if_type}0",
291 enable_gso=enable_server_if_gso,
292 enable_gro=enable_server_if_gro,
293 )
294 self.vpp_interfaces.append(self.egress_if_idx)
295 self.linux_interfaces.append([server_namespace, f"{server_if_type}0"])
296 # Seeing TCP timeouts if tx=on & rx=on Linux tap & tun interfaces
297 disable_interface_gso(server_namespace, f"{server_if_type}0")
298 else:
299 print(
300 f"Unsupported server interface type: {server_if_type} "
301 f"for test - ID={test['id']}"
302 )
303 sys.exit(1)
304
305 if x_connect_mode == "L2":
306 self.l2_connect_interfaces(1, self.ingress_if_idx, self.egress_if_idx)
307 elif x_connect_mode == "L3":
308 # L3 connect client & server side
309 vrf_id = layer3["ip4_vrf"] if ip_version == 4 else layer3["ip6_vrf"]
310 self.l3_connect_interfaces(
311 ip_version,
312 vrf_id,
313 (self.ingress_if_idx, vpp_client_prefix),
314 (self.egress_if_idx, vpp_server_prefix),
315 )
316 # Setup namespace routing
317 if ip_version == 4:
318 add_namespace_route(client_namespace, "0.0.0.0/0", vpp_client_nexthop)
319 add_namespace_route(server_namespace, "0.0.0.0/0", vpp_server_nexthop)
320 else:
321 add_namespace_route(client_namespace, "::/0", vpp_client_nexthop)
322 add_namespace_route(server_namespace, "::/0", vpp_server_nexthop)
323 # Wait for Linux IPv6 stack to become ready
324 if ip_version == 6:
325 time.sleep(2)
326
327 def tearDown(self):
328 try:
329 self.vapi.tap_delete_v2(self.ingress_if_idx)
330 except Exception:
331 pass
332 try:
333 self.vapi.tap_delete_v2(self.egress_if_idx)
334 except Exception:
335 pass
336 try:
337 for interface in self.vapi.af_packet_dump():
338 if (
339 interface.host_if_name
340 == af_packet_config["iprf_client_interface_on_vpp"]
341 ):
342 self.vapi.af_packet_delete(
343 af_packet_config["iprf_client_interface_on_vpp"]
344 )
345 elif (
346 interface.host_if_name
347 == af_packet_config["iprf_server_interface_on_vpp"]
348 ):
349 self.vapi.af_packet_delete(
350 af_packet_config["iprf_server_interface_on_vpp"]
351 )
352 except Exception:
353 pass
354 try:
355 delete_host_interfaces(
356 af_packet_config["iprf_client_interface_on_linux"],
357 af_packet_config["iprf_server_interface_on_linux"],
358 af_packet_config["iprf_client_interface_on_vpp"],
359 af_packet_config["iprf_server_interface_on_vpp"],
360 )
361 except Exception:
362 pass
363 try:
364 self.vapi.ip_table_add_del(is_add=0, table={"table_id": layer3["ip4_vrf"]})
365 except Exception:
366 pass
367 try:
368 self.vapi.ip_table_add_del(is_add=0, table={"table_id": layer3["ip6_vrf"]})
369 except Exception:
370 pass
371 try:
372 delete_namespace(
373 [
374 client_namespace,
375 server_namespace,
376 ]
377 )
378 except Exception:
379 pass
380 try:
381 stop_iperf()
382 except Exception:
383 pass
384
385 def create_af_packet(self, version, host_if_name, enable_gso=0):
386 """Create an af_packetv3 interface in VPP.
387
388 Parameters:
389 version -- 2 for af_packet_create_v2
390 -- 3 for af_packet_create_v3
391 host_if_name -- host interface name
392 enable_gso -- Enable GSO on the interface when True
393 """
394 af_packet_mode = VppEnum.vl_api_af_packet_mode_t
395 af_packet_interface_mode = af_packet_mode.AF_PACKET_API_MODE_ETHERNET
396 af_packet_flags = VppEnum.vl_api_af_packet_flags_t
397 af_packet_interface_flags = af_packet_flags.AF_PACKET_API_FLAG_QDISC_BYPASS
398 if enable_gso:
399 af_packet_interface_flags = (
400 af_packet_interface_flags | af_packet_flags.AF_PACKET_API_FLAG_CKSUM_GSO
401 )
402 if version == 2:
403 af_packet_interface_flags = (
404 af_packet_interface_flags | af_packet_flags.AF_PACKET_API_FLAG_VERSION_2
405 )
406 api_args = {
407 "use_random_hw_addr": True,
408 "host_if_name": host_if_name,
409 "flags": af_packet_interface_flags,
410 }
411 api_args["mode"] = af_packet_interface_mode
412 result = self.vapi.af_packet_create_v3(**api_args)
413 sw_if_index = result.sw_if_index
414 # Enable software GSO chunking when interface doesn't support GSO offload
415 if enable_gso == 0:
416 self.vapi.feature_gso_enable_disable(
417 sw_if_index=sw_if_index, enable_disable=1
418 )
419 else:
420 self.vapi.feature_gso_enable_disable(
421 sw_if_index=sw_if_index, enable_disable=0
422 )
423 self.vapi.sw_interface_set_flags(sw_if_index=sw_if_index, flags=1)
424 return sw_if_index
425
426 def create_tap_tun(
427 self,
428 id,
429 host_namespace,
430 ip_version,
431 host_ip4_prefix=None,
432 host_ip6_prefix=None,
433 host_ip4_gw=None,
434 host_ip6_gw=None,
435 int_type="tap",
436 host_if_name=None,
437 enable_gso=0,
438 enable_gro=0,
439 ):
440 """Create a tapv2 or tunv2 interface in VPP and attach to host.
441
442 Parameters:
443 id -- interface ID
444 host_namespace -- host namespace to attach the tap/tun interface to
445 ip_version -- 4 or 6
446 host_ip4_prefix -- ipv4 host interface address in CIDR notation
447 if ip_version=4
448 host_ip6_prefix -- ipv6 host interface address in CIDR notation
449 if ip_version=6
450 host_ip4_gw -- host IPv4 default gateway IP Address
451 host_ip6_gw -- host IPv6 default gateway IP address
452 int_type -- "tap" for tapv2 & "tun" for tunv2 interface
453 host_if_name -- host side interface name
454 enable_gso -- enable GSO
455 enable_gro -- enable GSO/GRO-Coalesce
456 """
457 TapFlags = VppEnum.vl_api_tap_flags_t
458 tap_flags = 0
459 if int_type == "tun":
460 tap_flags = TapFlags.TAP_API_FLAG_TUN
461 if enable_gro:
462 tap_flags = tap_flags | (
463 TapFlags.TAP_API_FLAG_GSO | TapFlags.TAP_API_FLAG_GRO_COALESCE
464 )
465 elif enable_gso:
466 tap_flags = tap_flags | TapFlags.TAP_API_FLAG_GSO
467 elif int_type == "tap":
468 if enable_gro:
469 tap_flags = (
470 TapFlags.TAP_API_FLAG_GSO | TapFlags.TAP_API_FLAG_GRO_COALESCE
471 )
472 elif enable_gso:
473 tap_flags = TapFlags.TAP_API_FLAG_GSO
474
475 api_args = {
476 "id": id,
477 "host_namespace_set": True,
478 "host_namespace": host_namespace,
479 "host_if_name_set": False,
480 "host_bridge_set": False,
481 "host_mac_addr_set": False,
482 }
483 if tap_flags != 0:
484 api_args["tap_flags"] = tap_flags
485 if ip_version == 4:
486 api_args["host_ip4_prefix"] = ip_interface(host_ip4_prefix)
487 api_args["host_ip4_prefix_set"] = True
488 if host_ip4_gw:
489 api_args["host_ip4_gw"] = ip_address(host_ip4_gw)
490 api_args["host_ip4_gw_set"] = True
491 if ip_version == 6:
492 api_args["host_ip6_prefix"] = ip_interface(host_ip6_prefix)
493 api_args["host_ip6_prefix_set"] = True
494 if host_ip6_gw:
495 api_args["host_ip6_gw"] = ip_address(host_ip6_gw)
496 api_args["host_ip6_gw_set"] = True
497 if host_if_name:
498 api_args["host_if_name"] = host_if_name
499 api_args["host_if_name_set"] = True
500
501 result = self.vapi.tap_create_v2(**api_args)
502 sw_if_index = result.sw_if_index
503 # Enable software GSO chunking when interface doesn't support GSO offload and
504 # GRO coalesce
505 if enable_gso == 0 and enable_gro == 0:
506 self.vapi.feature_gso_enable_disable(
507 sw_if_index=sw_if_index, enable_disable=1
508 )
509 else:
510 self.vapi.feature_gso_enable_disable(
511 sw_if_index=sw_if_index, enable_disable=0
512 )
513 # Admin up
514 self.vapi.sw_interface_set_flags(sw_if_index=sw_if_index, flags=1)
515 return sw_if_index
516
517 def dump_bridge_domain_details(self, bd_id):
518 return self.vapi.bridge_domain_dump(bd_id=bd_id)
519
520 def l2_connect_interfaces(self, bridge_id, *sw_if_idxs):
521 for if_idx in sw_if_idxs:
522 self.vapi.sw_interface_set_l2_bridge(
523 rx_sw_if_index=if_idx, bd_id=bridge_id, shg=0, port_type=0, enable=True
524 )
525
526 def l3_connect_interfaces(self, ip_version, vrf_id, *if_idx_ip_prefixes):
527 """Setup routing for (if_idx, ip_prefix) inside VPP.
528
529 arguments:
530 if_idx_ip_prefixes -- sequence of (if_idx, ip_prefix) tuples
531 ip_version -- 4 or 6
532 vrf_id -- vrf_id
533 """
534 is_ipv6 = 0 if ip_version == 4 else 1
535 self.vapi.ip_table_add_del(
536 is_add=1, table={"table_id": vrf_id, "is_ip6": is_ipv6}
537 )
538 for sw_if_index, ip_prefix in if_idx_ip_prefixes:
539 self.vapi.sw_interface_set_table(
540 sw_if_index=sw_if_index, is_ipv6=is_ipv6, vrf_id=vrf_id
541 )
542 self.vapi.sw_interface_add_del_address(
543 sw_if_index=sw_if_index, is_add=1, prefix=ip_interface(ip_prefix)
544 )
545
546 def set_interfaces_mtu(self, mtu, ip_version, **kwargs):
547 """Set MTUs on VPP and Linux interfaces.
548
549 arguments --
550 mtu -- mtu value
551 ip_version - 4 or 6
552 kwargs['vpp_interfaces'] -- list of vpp interface if indexes
553 kwargs['linux_interfaces'] -- list of tuples (namespace, interface_names)
554 return True if mtu is set, else False
555 """
556 vpp_interfaces = kwargs.get("vpp_interfaces")
557 linux_interfaces = kwargs.get("linux_interfaces")
558 # IPv6 on Linux requires an MTU value >=1280
559 if (ip_version == 6 and mtu >= 1280) or ip_version == 4:
560 for sw_if_idx in vpp_interfaces:
561 self.vapi.sw_interface_set_mtu(
562 sw_if_index=sw_if_idx, mtu=[mtu, 0, 0, 0]
563 )
564 for namespace, interface_name in linux_interfaces:
565 set_interface_mtu(
566 namespace=namespace,
567 interface=interface_name,
568 mtu=mtu,
569 logger=self.logger,
570 )
571 return True
572 else:
573 return False
574
575
576generate_vpp_interface_tests()
577
578if __name__ == "__main__":
579 unittest.main(testRunner=VppTestRunner)