Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Stephen Hemminger
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <fcntl.h>
7 : : #include <inttypes.h>
8 : : #include <sys/ioctl.h>
9 : : #include <sys/socket.h>
10 : : #include <linux/if.h>
11 : : #include <linux/if_tun.h>
12 : : #include <linux/sockios.h>
13 : : #include <pthread.h>
14 : : #include <stdbool.h>
15 : : #include <stdio.h>
16 : : #include <string.h>
17 : : #include <time.h>
18 : : #include <unistd.h>
19 : :
20 : : #include <rte_bus_vdev.h>
21 : : #include <rte_config.h>
22 : : #include <rte_cycles.h>
23 : : #include <rte_ethdev.h>
24 : : #include <rte_ether.h>
25 : : #include <rte_lcore.h>
26 : : #include <rte_mbuf.h>
27 : : #include <rte_mempool.h>
28 : : #include <rte_stdatomic.h>
29 : :
30 : : #include "test.h"
31 : :
32 : : #ifndef RTE_EXEC_ENV_LINUX
33 : : static int
34 : : test_pmd_af_packet(void)
35 : : {
36 : : printf("af_packet only supported on Linux, skipping test\n");
37 : : return TEST_SKIPPED;
38 : : }
39 : :
40 : : #else
41 : :
42 : : #define NUM_MBUFS 512
43 : : #define MBUF_CACHE_SIZE 32
44 : : #define BURST_SIZE 32
45 : : #define RING_SIZE 256
46 : :
47 : : /* Length of Ethernet frames built by the tests */
48 : : #define TEST_FRAME_LEN (sizeof(struct rte_ether_hdr) + 46)
49 : :
50 : : /* Source MAC address used for frames sent by the TAP device */
51 : : static const struct rte_ether_addr DEV_MAC_TAG = {
52 : : .addr_bytes = { 'P', 'A', 'C', 'K', 'E', 'T' },
53 : : };
54 : :
55 : : /* Source MAC address used for frames sent by the TAP file descriptor */
56 : : static const struct rte_ether_addr FD_MAC_TAG = {
57 : : .addr_bytes = { 'T', 'E', 'K', 'C', 'A', 'P' },
58 : : };
59 : :
60 : : /* Count of frames with each tag */
61 : : struct tag_count {
62 : : unsigned int n_dev_tagged;
63 : : unsigned int n_fd_tagged;
64 : : };
65 : :
66 : : /* Polling and timeout constants */
67 : : #define STATS_POLL_INTERVAL_US 100
68 : : #define STATS_POLL_TIMEOUT_US 100000 /* 100ms overall timeout */
69 : : #define LOOPBACK_TIMEOUT_US 500000 /* 500ms for loopback test */
70 : : #define RX_FLUSH_TIMEOUT_US 100000 /* 100ms to drain queued frames */
71 : :
72 : : /* Multi-threaded test constants */
73 : : #define MT_NUM_PACKETS 100
74 : : #define MT_TEST_DURATION_SEC 2
75 : :
76 : : /* Test device names */
77 : : #define AF_PACKET_DEV_NAME "net_af_packet_test"
78 : : #define TAP_DEV_NAME "dpdkafptest"
79 : :
80 : : static struct rte_mempool *mp;
81 : : static uint16_t port_id = RTE_MAX_ETHPORTS;
82 : : static int tap_fd = -1;
83 : : static bool tap_created;
84 : : static bool port_created;
85 : : static bool port_started;
86 : :
87 : : /*
88 : : * Create a TAP interface for testing.
89 : : * Returns fd on success, -1 on failure.
90 : : */
91 : : static int
92 : 1 : create_tap_interface(const char *name)
93 : : {
94 : : struct ifreq ifr;
95 : : int fd, ret;
96 : :
97 : : fd = open("/dev/net/tun", O_RDWR);
98 [ - + ]: 1 : if (fd < 0) {
99 : 0 : printf("Cannot open /dev/net/tun: %s\n", strerror(errno));
100 : : printf("(Are you running as root?)\n");
101 : 0 : return -1;
102 : : }
103 : :
104 : : memset(&ifr, 0, sizeof(ifr));
105 : 1 : ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
106 : : snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
107 : :
108 : 1 : ret = ioctl(fd, TUNSETIFF, &ifr);
109 [ - + ]: 1 : if (ret < 0) {
110 : 0 : printf("Cannot create TAP interface '%s': %s\n",
111 : 0 : name, strerror(errno));
112 : 0 : close(fd);
113 : 0 : return -1;
114 : : }
115 : :
116 : : /* Bring the interface up */
117 : 1 : int sock = socket(AF_INET, SOCK_DGRAM, 0);
118 [ + - ]: 1 : if (sock >= 0) {
119 : : memset(&ifr, 0, sizeof(ifr));
120 : : snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
121 : :
122 : : /* Get current flags */
123 [ + - ]: 1 : if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
124 : 1 : ifr.ifr_flags |= IFF_UP;
125 : 1 : ioctl(sock, SIOCSIFFLAGS, &ifr);
126 : : }
127 : 1 : close(sock);
128 : : }
129 : :
130 : : printf("Created TAP interface '%s'\n", name);
131 : 1 : return fd;
132 : : }
133 : :
134 : : static void
135 : : destroy_tap_interface(int fd)
136 : : {
137 : 1 : if (fd >= 0)
138 : 1 : close(fd);
139 : : }
140 : :
141 : : static int
142 : 9 : create_af_packet_port(const char *name, const char *args, uint16_t *out_port_id)
143 : : {
144 : : int ret;
145 : :
146 : 9 : ret = rte_vdev_init(name, args);
147 [ - + ]: 9 : if (ret != 0) {
148 : : printf("Failed to create af_packet device '%s': %d\n", name, ret);
149 : 0 : return ret;
150 : : }
151 : :
152 : 9 : ret = rte_eth_dev_get_port_by_name(name, out_port_id);
153 [ - + ]: 9 : if (ret != 0) {
154 : : printf("Failed to get port id for '%s': %d\n", name, ret);
155 : 0 : rte_vdev_uninit(name);
156 : 0 : return ret;
157 : : }
158 : :
159 : : return 0;
160 : : }
161 : :
162 : : static int
163 : 9 : configure_af_packet_port(uint16_t pid, uint16_t nb_rx_q, uint16_t nb_tx_q)
164 : : {
165 : 9 : struct rte_eth_conf port_conf = {0};
166 : : struct rte_eth_dev_info dev_info;
167 : : int ret;
168 : : uint16_t q;
169 : :
170 : 9 : ret = rte_eth_dev_info_get(pid, &dev_info);
171 [ - + ]: 9 : if (ret != 0) {
172 : : printf("Failed to get device info for port %u: %d\n", pid, ret);
173 : 0 : return ret;
174 : : }
175 : :
176 : 9 : ret = rte_eth_dev_configure(pid, nb_rx_q, nb_tx_q, &port_conf);
177 [ - + ]: 9 : if (ret != 0) {
178 : : printf("Failed to configure port %u: %d\n", pid, ret);
179 : 0 : return ret;
180 : : }
181 : :
182 [ + + ]: 20 : for (q = 0; q < nb_rx_q; q++) {
183 : 11 : ret = rte_eth_rx_queue_setup(pid, q, RING_SIZE,
184 : 11 : rte_eth_dev_socket_id(pid),
185 : : NULL, mp);
186 [ - + ]: 11 : if (ret != 0) {
187 : : printf("Failed to setup RX queue %u for port %u: %d\n",
188 : : q, pid, ret);
189 : 0 : return ret;
190 : : }
191 : : }
192 : :
193 [ + + ]: 20 : for (q = 0; q < nb_tx_q; q++) {
194 : 11 : ret = rte_eth_tx_queue_setup(pid, q, RING_SIZE,
195 : 11 : rte_eth_dev_socket_id(pid),
196 : : NULL);
197 [ - + ]: 11 : if (ret != 0) {
198 : : printf("Failed to setup TX queue %u for port %u: %d\n",
199 : : q, pid, ret);
200 : 0 : return ret;
201 : : }
202 : : }
203 : :
204 : 9 : ret = rte_eth_dev_start(pid);
205 [ - + ]: 9 : if (ret != 0) {
206 : : printf("Failed to start port %u: %d\n", pid, ret);
207 : 0 : return ret;
208 : : }
209 : :
210 : : return 0;
211 : : }
212 : :
213 : : /*
214 : : * Helper: Fill a minimal Ethernet frame with a broadcast destination and
215 : : * the given source MAC.
216 : : * Returns 0 on success, -1 otherwise.
217 : : */
218 : : static int
219 : 236 : init_frame(void *frame, size_t len, const struct rte_ether_addr *src)
220 : : {
221 : : struct rte_ether_hdr *eth = frame;
222 : :
223 [ + - ]: 236 : if (len < TEST_FRAME_LEN)
224 : : return -1;
225 : :
226 : : /* Set broadcast destination and source MAC */
227 : : memset(frame, 0, TEST_FRAME_LEN);
228 : 236 : memset(ð->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
229 : : rte_ether_addr_copy(src, ð->src_addr);
230 : 236 : eth->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
231 : :
232 : 236 : return 0;
233 : : }
234 : :
235 : : /*
236 : : * Helper: Allocate and prepare a burst of TX mbufs with minimal Ethernet frames.
237 : : * Returns the number of successfully allocated mbufs.
238 : : */
239 : : static unsigned int
240 : 22 : alloc_tx_mbufs(struct rte_mbuf **bufs, unsigned int count)
241 : : {
242 : : unsigned int i;
243 : : int ret;
244 : :
245 : 22 : ret = rte_pktmbuf_alloc_bulk(mp, bufs, count);
246 [ + - ]: 22 : if (ret != 0)
247 : : return 0;
248 : :
249 [ + + ]: 254 : for (i = 0; i < count; i++) {
250 : : void *hdr;
251 : :
252 : 232 : hdr = rte_pktmbuf_append(bufs[i], TEST_FRAME_LEN);
253 [ + - - + ]: 464 : if (hdr == NULL ||
254 : 232 : init_frame(hdr, TEST_FRAME_LEN, &DEV_MAC_TAG) != 0) {
255 : : /* Free all allocated mbufs on failure */
256 : 0 : rte_pktmbuf_free_bulk(bufs, count);
257 : 0 : return 0;
258 : : }
259 : : }
260 : :
261 : : return count;
262 : : }
263 : :
264 : : /*
265 : : * Helper: Indicates whether the mbuf source MAC matches tag.
266 : : */
267 : : static bool
268 : : is_tagged_mbuf(struct rte_mbuf *buf, const struct rte_ether_addr *tag)
269 : : {
270 : : struct rte_ether_hdr *eth;
271 : :
272 [ + - ]: 17 : if (rte_pktmbuf_data_len(buf) < sizeof(*eth))
273 : : return false;
274 : :
275 [ + + + + ]: 74 : eth = rte_pktmbuf_mtod(buf, struct rte_ether_hdr *);
276 : : return rte_is_same_ether_addr(ð->src_addr, tag);
277 : : }
278 : :
279 : : /*
280 : : * Helper: Transmit packets and free unsent mbufs.
281 : : * Returns the number of packets successfully transmitted.
282 : : */
283 : : static uint16_t
284 : 22 : do_tx_burst(uint16_t pid, uint16_t queue_id, struct rte_mbuf **bufs,
285 : : unsigned int count)
286 : : {
287 : : uint16_t nb_tx;
288 : :
289 : 22 : nb_tx = rte_eth_tx_burst(pid, queue_id, bufs, count);
290 : :
291 : : /* Free any unsent mbufs */
292 : 22 : rte_pktmbuf_free_bulk(&bufs[nb_tx], count - nb_tx);
293 : :
294 : 22 : return nb_tx;
295 : : }
296 : :
297 : : /*
298 : : * Helper: Receive packets and free received mbufs.
299 : : * If tag_count is non-NULL, count tags in it.
300 : : * Returns the number of packets received.
301 : : */
302 : : static uint16_t
303 : 15036 : do_rx_burst(uint16_t pid, uint16_t queue_id, struct rte_mbuf **bufs,
304 : : unsigned int count, struct tag_count *tag_count)
305 : : {
306 : : uint16_t i, nb_rx;
307 : :
308 : 15036 : nb_rx = rte_eth_rx_burst(pid, queue_id, bufs, count);
309 [ + - ]: 15036 : if (tag_count != NULL) {
310 [ + + ]: 15093 : for (i = 0; i < nb_rx; i++) {
311 [ + - + + ]: 57 : if (is_tagged_mbuf(bufs[i], &DEV_MAC_TAG))
312 : 40 : tag_count->n_dev_tagged++;
313 [ + + ]: 17 : else if (is_tagged_mbuf(bufs[i], &FD_MAC_TAG))
314 : 16 : tag_count->n_fd_tagged++;
315 : : }
316 : : }
317 : 15045 : rte_pktmbuf_free_bulk(bufs, nb_rx);
318 : :
319 : 15036 : return nb_rx;
320 : : }
321 : :
322 : : /*
323 : : * Helper: Discard frames already queued.
324 : : */
325 : : static void
326 : 4 : flush_rx_port(uint16_t pid, uint16_t nb_queues)
327 : : {
328 : : struct rte_mbuf *bufs[BURST_SIZE];
329 : : uint64_t flush_deadline;
330 : : uint16_t q;
331 : :
332 : 4 : flush_deadline = rte_get_timer_cycles() +
333 : 4 : (rte_get_timer_hz() * RX_FLUSH_TIMEOUT_US) / US_PER_S;
334 : :
335 [ + + ]: 10 : for (q = 0; q < nb_queues; q++)
336 [ - + - - ]: 12 : while (do_rx_burst(pid, q, bufs, BURST_SIZE, NULL) > 0 &&
337 : : rte_get_timer_cycles() < flush_deadline)
338 : : ;
339 : 4 : }
340 : :
341 : : /*
342 : : * Helper: Write FD_MAC_TAG frames to the TAP fd so they appear as
343 : : * incoming packets on the netdev.
344 : : */
345 : : static int
346 : 4 : do_tap_fd_tx_burst(int fd, unsigned int n)
347 : : {
348 : : unsigned int i;
349 : : uint8_t frame[TEST_FRAME_LEN];
350 : :
351 [ + - ]: 4 : if (init_frame(frame, sizeof(frame), &FD_MAC_TAG) != 0)
352 : : return -1;
353 : :
354 [ + + ]: 20 : for (i = 0; i < n; i++) {
355 [ + - ]: 16 : if (write(fd, frame, sizeof(frame)) != (ssize_t)sizeof(frame))
356 : : return -1;
357 : : }
358 : :
359 : : return 0;
360 : : }
361 : :
362 : : /*
363 : : * Helper: Wait for stats to update by polling at intervals.
364 : : * Returns 0 on success (stats updated), -1 on timeout.
365 : : */
366 : : static int
367 : 2 : wait_for_stats_update(uint16_t pid, uint64_t expected_opackets,
368 : : uint64_t timeout_us)
369 : : {
370 : : struct rte_eth_stats stats;
371 : : uint64_t elapsed = 0;
372 : :
373 [ + - ]: 2 : while (elapsed < timeout_us) {
374 [ + - ]: 2 : if (rte_eth_stats_get(pid, &stats) == 0) {
375 [ - + ]: 2 : if (stats.opackets >= expected_opackets)
376 : : return 0;
377 : : }
378 : 0 : rte_delay_us_block(STATS_POLL_INTERVAL_US);
379 : 0 : elapsed += STATS_POLL_INTERVAL_US;
380 : : }
381 : :
382 : : return -1;
383 : : }
384 : :
385 : : static int
386 : 1 : test_af_packet_setup(void)
387 : : {
388 : : char devargs[256];
389 : : int ret;
390 : :
391 : : /* Create mempool for mbufs */
392 : 1 : mp = rte_pktmbuf_pool_create("af_packet_test_pool", NUM_MBUFS,
393 : : MBUF_CACHE_SIZE, 0,
394 : : RTE_MBUF_DEFAULT_BUF_SIZE,
395 : 1 : rte_socket_id());
396 [ - + ]: 1 : if (mp == NULL) {
397 : : printf("Failed to create mempool\n");
398 : 0 : return -1;
399 : : }
400 : :
401 : : /* Create TAP interface for testing */
402 : 1 : tap_fd = create_tap_interface(TAP_DEV_NAME);
403 [ + - ]: 1 : if (tap_fd >= 0)
404 : 1 : tap_created = true;
405 : : else {
406 : : printf("TAP interface creation failed - tests will be skipped\n");
407 : 0 : return 0; /* Return success to allow skipped tests */
408 : : }
409 : :
410 : : /* Create and configure af_packet port */
411 : : snprintf(devargs, sizeof(devargs), "iface=%s", TAP_DEV_NAME);
412 : 1 : ret = create_af_packet_port(AF_PACKET_DEV_NAME, devargs, &port_id);
413 [ - + ]: 1 : if (ret != 0) {
414 : : printf("Failed to create af_packet port\n");
415 : 0 : return -1;
416 : : }
417 : 1 : port_created = true;
418 : :
419 : 1 : ret = configure_af_packet_port(port_id, 1, 1);
420 [ - + ]: 1 : if (ret != 0) {
421 : : printf("Failed to configure af_packet port\n");
422 : 0 : return -1;
423 : : }
424 : 1 : port_started = true;
425 : :
426 : 1 : return 0;
427 : : }
428 : :
429 : : static void
430 : 1 : test_af_packet_teardown(void)
431 : : {
432 : : /* Stop and close test port */
433 [ + - ]: 1 : if (port_started) {
434 : 1 : rte_eth_dev_stop(port_id);
435 : 1 : port_started = false;
436 : : }
437 : :
438 [ + - ]: 1 : if (port_created) {
439 : 1 : rte_eth_dev_close(port_id);
440 : 1 : rte_vdev_uninit(AF_PACKET_DEV_NAME);
441 : 1 : port_id = RTE_MAX_ETHPORTS;
442 : 1 : port_created = false;
443 : : }
444 : :
445 : : /* Destroy TAP interface */
446 [ + - ]: 1 : if (tap_created) {
447 [ + - ]: 1 : destroy_tap_interface(tap_fd);
448 : 1 : tap_fd = -1;
449 : 1 : tap_created = false;
450 : : }
451 : :
452 [ + - ]: 1 : if (mp != NULL) {
453 : 1 : rte_mempool_free(mp);
454 : 1 : mp = NULL;
455 : : }
456 : 1 : }
457 : :
458 : : /*
459 : : * Test: Device info verification
460 : : */
461 : : static int
462 : 1 : test_af_packet_dev_info(void)
463 : : {
464 : : struct rte_eth_dev_info dev_info;
465 : : int ret;
466 : :
467 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
468 : : printf("SKIPPED: Port not available (need root)\n");
469 : 0 : return TEST_SKIPPED;
470 : : }
471 : :
472 : 1 : ret = rte_eth_dev_info_get(port_id, &dev_info);
473 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get device info");
474 : :
475 : : /* Verify expected device info values */
476 [ - + ]: 1 : TEST_ASSERT(dev_info.max_mac_addrs == 1,
477 : : "Expected max_mac_addrs=1, got %u", dev_info.max_mac_addrs);
478 : :
479 : : /* The driver has default internal frame size of 2K */
480 [ - + ]: 1 : TEST_ASSERT(dev_info.max_rx_pktlen >= RTE_ETHER_MAX_LEN,
481 : : "max_rx_pktlen %u < %u",
482 : : dev_info.max_rx_pktlen, RTE_ETHER_MAX_LEN);
483 [ - + ]: 1 : TEST_ASSERT(dev_info.max_mtu >= RTE_ETHER_MTU,
484 : : "max_mtu %u < %u", dev_info.max_mtu, RTE_ETHER_MTU);
485 [ - + ]: 1 : TEST_ASSERT(dev_info.min_rx_bufsize == 0,
486 : : "Expected min_rx_bufsize=0, got %u", dev_info.min_rx_bufsize);
487 [ - + ]: 1 : TEST_ASSERT(dev_info.max_rx_queues >= 1, "No RX queues available");
488 [ - + ]: 1 : TEST_ASSERT(dev_info.max_tx_queues >= 1, "No TX queues available");
489 [ - + ]: 1 : TEST_ASSERT(dev_info.if_index > 0, "Invalid interface index");
490 : :
491 : : /* Check TX offload capabilities */
492 [ - + ]: 1 : TEST_ASSERT(dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MULTI_SEGS,
493 : : "Expected MULTI_SEGS TX offload capability");
494 [ - + ]: 1 : TEST_ASSERT(dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_VLAN_INSERT,
495 : : "Expected VLAN_INSERT TX offload capability");
496 : :
497 : : /* Check RX offload capabilities */
498 [ - + ]: 1 : TEST_ASSERT(dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_VLAN_STRIP,
499 : : "Expected VLAN_STRIP RX offload capability");
500 [ - + ]: 1 : TEST_ASSERT(dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_TIMESTAMP,
501 : : "Expected TIMESTAMP RX offload capability");
502 : :
503 : : return TEST_SUCCESS;
504 : : }
505 : :
506 : : /*
507 : : * Test: Link status
508 : : * Note: af_packet PMD link status reflects the underlying interface state,
509 : : * not the DPDK device start/stop state.
510 : : */
511 : : static int
512 : 1 : test_af_packet_link_status(void)
513 : : {
514 : : struct rte_eth_link link;
515 : : int ret;
516 : :
517 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
518 : : printf("SKIPPED: Port not available (need root)\n");
519 : 0 : return TEST_SKIPPED;
520 : : }
521 : :
522 : 1 : ret = rte_eth_link_get_nowait(port_id, &link);
523 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get link status");
524 : :
525 : : /* TAP interface was brought up during setup, so link should be UP */
526 [ - + ]: 1 : TEST_ASSERT(link.link_status == RTE_ETH_LINK_UP,
527 : : "Expected link UP (TAP interface is up)");
528 [ - + ]: 1 : TEST_ASSERT(link.link_speed == RTE_ETH_SPEED_NUM_10G,
529 : : "Expected 10G link speed");
530 [ - + ]: 1 : TEST_ASSERT(link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX,
531 : : "Expected full duplex");
532 : :
533 : : return TEST_SUCCESS;
534 : : }
535 : :
536 : : /*
537 : : * Test: Statistics initial state
538 : : */
539 : : static int
540 : 1 : test_af_packet_stats_init(void)
541 : : {
542 : : struct rte_eth_stats stats;
543 : : int ret;
544 : :
545 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
546 : : printf("SKIPPED: Port not available (need root)\n");
547 : 0 : return TEST_SKIPPED;
548 : : }
549 : :
550 : : /* Reset stats */
551 : 1 : ret = rte_eth_stats_reset(port_id);
552 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to reset stats");
553 : :
554 : : /* Get initial stats */
555 : 1 : ret = rte_eth_stats_get(port_id, &stats);
556 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get stats");
557 : :
558 : : /* After reset, stats should be zero */
559 [ - + ]: 1 : TEST_ASSERT(stats.ipackets == 0,
560 : : "Expected ipackets=0 after reset, got %"PRIu64,
561 : : stats.ipackets);
562 [ - + ]: 1 : TEST_ASSERT(stats.opackets == 0,
563 : : "Expected opackets=0 after reset, got %"PRIu64,
564 : : stats.opackets);
565 [ - + ]: 1 : TEST_ASSERT(stats.ibytes == 0,
566 : : "Expected ibytes=0 after reset, got %"PRIu64,
567 : : stats.ibytes);
568 [ - + ]: 1 : TEST_ASSERT(stats.obytes == 0,
569 : : "Expected obytes=0 after reset, got %"PRIu64,
570 : : stats.obytes);
571 : :
572 : : return TEST_SUCCESS;
573 : : }
574 : :
575 : : /*
576 : : * Test: TX packets (packets will be sent to TAP interface)
577 : : */
578 : : static int
579 : 1 : test_af_packet_tx(void)
580 : : {
581 : : struct rte_mbuf *bufs[BURST_SIZE];
582 : : struct rte_eth_stats stats;
583 : : uint16_t nb_tx;
584 : : unsigned int allocated;
585 : : int ret;
586 : :
587 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
588 : : printf("SKIPPED: Port not available (need root)\n");
589 : 0 : return TEST_SKIPPED;
590 : : }
591 : :
592 : : /* Reset stats */
593 : 1 : ret = rte_eth_stats_reset(port_id);
594 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to reset stats");
595 : :
596 : : /* Allocate and prepare mbufs for TX using helper */
597 : 1 : allocated = alloc_tx_mbufs(bufs, BURST_SIZE);
598 [ - + ]: 1 : TEST_ASSERT(allocated == BURST_SIZE, "Failed to allocate all mbufs");
599 : :
600 : : /* TX burst using helper */
601 : 1 : nb_tx = do_tx_burst(port_id, 0, bufs, BURST_SIZE);
602 : :
603 : : /* Poll for stats update instead of fixed delay */
604 [ + - ]: 1 : if (nb_tx > 0) {
605 : 1 : ret = wait_for_stats_update(port_id, nb_tx, STATS_POLL_TIMEOUT_US);
606 [ + - ]: 1 : if (ret == 0) {
607 : 1 : ret = rte_eth_stats_get(port_id, &stats);
608 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get stats");
609 [ - + ]: 1 : TEST_ASSERT(stats.opackets > 0,
610 : : "Expected opackets > 0 after TX");
611 : : }
612 : : /* Timeout is acceptable - stats may not update immediately */
613 : : }
614 : :
615 : : return TEST_SUCCESS;
616 : : }
617 : :
618 : : /*
619 : : * Test: RX packets (non-blocking, may not receive anything)
620 : : */
621 : : static int
622 : 1 : test_af_packet_rx(void)
623 : : {
624 : : struct rte_mbuf *bufs[BURST_SIZE];
625 : :
626 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
627 : : printf("SKIPPED: Port not available (need root)\n");
628 : 0 : return TEST_SKIPPED;
629 : : }
630 : :
631 : : /* Try to receive packets (non-blocking) using helper */
632 : 1 : do_rx_burst(port_id, 0, bufs, BURST_SIZE, NULL);
633 : :
634 : : /* RX from tap interface without external traffic will return 0 */
635 : : /* This test just verifies the RX path doesn't crash */
636 : :
637 : 1 : return TEST_SUCCESS;
638 : : }
639 : :
640 : : /*
641 : : * Test: Loopback - TX packets and receive them back via TAP interface.
642 : : * This exercises both the TX and RX paths in a round-trip.
643 : : */
644 : : static int
645 : 1 : test_af_packet_loopback(void)
646 : : {
647 : : struct rte_mbuf *tx_bufs[BURST_SIZE];
648 : : struct rte_mbuf *rx_bufs[BURST_SIZE];
649 : : uint16_t nb_tx;
650 : 1 : struct tag_count tag_count = {0};
651 : : uint64_t elapsed = 0;
652 : : unsigned int allocated;
653 : : char tap_buf[2048];
654 : :
655 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
656 : : printf("SKIPPED: Port not available (need root)\n");
657 : 0 : return TEST_SKIPPED;
658 : : }
659 : :
660 : : /* Set TAP fd to non-blocking for reading */
661 : 1 : int flags = fcntl(tap_fd, F_GETFL, 0);
662 [ + - - + ]: 1 : if (flags < 0 || fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK) < 0) {
663 : 0 : printf("fcntl failed: %s\n", strerror(errno));
664 : 0 : return TEST_FAILED;
665 : : }
666 : :
667 : : /* Allocate and send packets */
668 : 1 : allocated = alloc_tx_mbufs(tx_bufs, BURST_SIZE);
669 [ - + ]: 1 : if (allocated == 0) {
670 : : printf("Could not allocate mbufs\n");
671 : 0 : return TEST_FAILED;
672 : : }
673 : :
674 : 1 : nb_tx = do_tx_burst(port_id, 0, tx_bufs, allocated);
675 [ - + ]: 1 : if (nb_tx == 0) {
676 : : printf("No packets transmitted\n");
677 : 0 : return TEST_FAILED;
678 : : }
679 : :
680 : : /*
681 : : * Read packets from TAP interface (they were sent there by af_packet).
682 : : * Then write them back to TAP so af_packet can receive them.
683 : : */
684 [ + - ]: 33 : while (elapsed < LOOPBACK_TIMEOUT_US) {
685 : : ssize_t bytes_read, bytes_written;
686 : :
687 : : /* Read from TAP (what af_packet sent) */
688 : 33 : bytes_read = read(tap_fd, tap_buf, sizeof(tap_buf));
689 [ + - ]: 33 : if (bytes_read > 0) {
690 : : /* Write back to TAP for af_packet to receive */
691 : 33 : bytes_written = write(tap_fd, tap_buf, bytes_read);
692 [ - + ]: 33 : TEST_ASSERT(bytes_read == bytes_written, "TAP write %zd != %zd",
693 : : bytes_read, bytes_written);
694 : : }
695 : :
696 : : /* Try to receive via af_packet RX */
697 : 33 : do_rx_burst(port_id, 0, rx_bufs, BURST_SIZE, &tag_count);
698 : :
699 : : /* Check if we've received enough */
700 [ + + ]: 33 : if (tag_count.n_dev_tagged >= nb_tx)
701 : : break;
702 : :
703 : 32 : rte_delay_us_block(STATS_POLL_INTERVAL_US);
704 : 32 : elapsed += STATS_POLL_INTERVAL_US;
705 : : }
706 : :
707 : : /* Restore TAP fd flags */
708 : 1 : fcntl(tap_fd, F_SETFL, flags);
709 : :
710 : : /*
711 : : * Note: We may not receive all packets due to timing, but receiving
712 : : * any packets proves the loopback path works.
713 : : */
714 : 1 : printf("Loopback: TX=%u, RX=%u\n", nb_tx, tag_count.n_dev_tagged);
715 : :
716 : 1 : return TEST_SUCCESS;
717 : : }
718 : :
719 : : /*
720 : : * Test: Promiscuous mode
721 : : */
722 : : static int
723 : 1 : test_af_packet_promiscuous(void)
724 : : {
725 : : int ret;
726 : :
727 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
728 : : printf("SKIPPED: Port not available (need root)\n");
729 : 0 : return TEST_SKIPPED;
730 : : }
731 : :
732 : : /* Enable promiscuous mode */
733 : 1 : ret = rte_eth_promiscuous_enable(port_id);
734 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to enable promiscuous mode");
735 : :
736 : 1 : ret = rte_eth_promiscuous_get(port_id);
737 [ - + ]: 1 : TEST_ASSERT(ret == 1, "Expected promiscuous mode enabled");
738 : :
739 : : /* Disable promiscuous mode */
740 : 1 : ret = rte_eth_promiscuous_disable(port_id);
741 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to disable promiscuous mode");
742 : :
743 : 1 : ret = rte_eth_promiscuous_get(port_id);
744 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Expected promiscuous mode disabled");
745 : :
746 : : return TEST_SUCCESS;
747 : : }
748 : :
749 : : /*
750 : : * Test: MAC address operations
751 : : */
752 : : static int
753 : 1 : test_af_packet_mac_addr(void)
754 : : {
755 : : struct rte_ether_addr mac_addr;
756 : 1 : struct rte_ether_addr new_mac = {
757 : : .addr_bytes = {0x02, 0x11, 0x22, 0x33, 0x44, 0x55}
758 : : };
759 : : int ret;
760 : :
761 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
762 : : printf("SKIPPED: Port not available (need root)\n");
763 : 0 : return TEST_SKIPPED;
764 : : }
765 : :
766 : : /* Get current MAC address */
767 : 1 : ret = rte_eth_macaddr_get(port_id, &mac_addr);
768 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get MAC address");
769 : :
770 : : /* Set new MAC address (use locally administered address) */
771 : 1 : ret = rte_eth_dev_default_mac_addr_set(port_id, &new_mac);
772 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to set MAC address");
773 : :
774 : : /* Verify MAC was set */
775 : 1 : ret = rte_eth_macaddr_get(port_id, &mac_addr);
776 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get MAC address after set");
777 : :
778 [ - + ]: 1 : TEST_ASSERT(memcmp(&mac_addr, &new_mac, sizeof(mac_addr)) == 0,
779 : : "MAC address mismatch after set");
780 : :
781 : : return TEST_SUCCESS;
782 : : }
783 : :
784 : : /*
785 : : * Test: MTU operations
786 : : * Get min/max supported MTU from device info and verify setting works.
787 : : */
788 : : static int
789 : 1 : test_af_packet_mtu(void)
790 : : {
791 : : struct rte_eth_dev_info dev_info;
792 : : uint16_t mtu, original_mtu;
793 : : int ret;
794 : :
795 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
796 : : printf("SKIPPED: Port not available (need root)\n");
797 : 0 : return TEST_SKIPPED;
798 : : }
799 : :
800 : : /* Get device info for min/max MTU */
801 : 1 : ret = rte_eth_dev_info_get(port_id, &dev_info);
802 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get device info");
803 : :
804 : : /* Get current MTU */
805 : 1 : ret = rte_eth_dev_get_mtu(port_id, &original_mtu);
806 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get MTU");
807 : :
808 : 1 : printf("MTU: current=%u, min=%u, max=%u\n",
809 : 1 : original_mtu, dev_info.min_mtu, dev_info.max_mtu);
810 : :
811 : : /* Test setting minimum MTU if supported and different from current */
812 [ + - + - ]: 1 : if (dev_info.min_mtu > 0 && dev_info.min_mtu != original_mtu) {
813 : 1 : ret = rte_eth_dev_set_mtu(port_id, dev_info.min_mtu);
814 [ - + ]: 1 : if (ret == 0) {
815 : 0 : ret = rte_eth_dev_get_mtu(port_id, &mtu);
816 [ # # ]: 0 : TEST_ASSERT(ret == 0, "Failed to get MTU after set to min");
817 [ # # ]: 0 : TEST_ASSERT(mtu == dev_info.min_mtu,
818 : : "MTU not set to min: expected %u, got %u",
819 : : dev_info.min_mtu, mtu);
820 : : }
821 : : /* MTU set may fail depending on interface, that's acceptable */
822 : : }
823 : :
824 : : /* Test setting maximum MTU if supported and different from current */
825 [ + - + - ]: 1 : if (dev_info.max_mtu > 0 && dev_info.max_mtu != original_mtu &&
826 [ + - ]: 1 : dev_info.max_mtu != dev_info.min_mtu) {
827 : 1 : ret = rte_eth_dev_set_mtu(port_id, dev_info.max_mtu);
828 [ + - ]: 1 : if (ret == 0) {
829 : 1 : ret = rte_eth_dev_get_mtu(port_id, &mtu);
830 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get MTU after set to max");
831 [ - + ]: 1 : TEST_ASSERT(mtu == dev_info.max_mtu,
832 : : "MTU not set to max: expected %u, got %u",
833 : : dev_info.max_mtu, mtu);
834 : : }
835 : : /* MTU set may fail depending on interface capabilities */
836 : : }
837 : :
838 : : /* Restore original MTU */
839 : 1 : rte_eth_dev_set_mtu(port_id, original_mtu);
840 : :
841 : 1 : return TEST_SUCCESS;
842 : : }
843 : :
844 : : /*
845 : : * Test: Stats reset verification
846 : : */
847 : : static int
848 : 1 : test_af_packet_stats_reset(void)
849 : : {
850 : : struct rte_eth_stats stats;
851 : : struct rte_mbuf *bufs[BURST_SIZE / 2];
852 : : uint16_t nb_tx;
853 : : unsigned int allocated;
854 : : int ret;
855 : :
856 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
857 : : printf("SKIPPED: Port not available (need root)\n");
858 : 0 : return TEST_SKIPPED;
859 : : }
860 : :
861 : : /* Generate some TX traffic using helpers */
862 : 1 : allocated = alloc_tx_mbufs(bufs, BURST_SIZE / 2);
863 : 1 : nb_tx = do_tx_burst(port_id, 0, bufs, allocated);
864 : :
865 : : /* Poll for stats to update */
866 [ + - ]: 1 : if (nb_tx > 0)
867 : 1 : wait_for_stats_update(port_id, nb_tx, STATS_POLL_TIMEOUT_US);
868 : :
869 : : /* Reset stats */
870 : 1 : ret = rte_eth_stats_reset(port_id);
871 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to reset stats");
872 : :
873 : : /* Verify stats are zero */
874 : 1 : ret = rte_eth_stats_get(port_id, &stats);
875 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get stats after reset");
876 : :
877 [ - + ]: 1 : TEST_ASSERT(stats.ipackets == 0,
878 : : "Expected ipackets=0, got %"PRIu64, stats.ipackets);
879 [ - + ]: 1 : TEST_ASSERT(stats.opackets == 0,
880 : : "Expected opackets=0, got %"PRIu64, stats.opackets);
881 [ - + ]: 1 : TEST_ASSERT(stats.ibytes == 0,
882 : : "Expected ibytes=0, got %"PRIu64, stats.ibytes);
883 [ - + ]: 1 : TEST_ASSERT(stats.obytes == 0,
884 : : "Expected obytes=0, got %"PRIu64, stats.obytes);
885 : :
886 : : return TEST_SUCCESS;
887 : : }
888 : :
889 : : /*
890 : : * Test: Invalid configuration handling - missing iface
891 : : */
892 : : static int
893 : 1 : test_af_packet_invalid_no_iface(void)
894 : : {
895 : : int ret;
896 : :
897 : : /* Test without iface argument (should fail) */
898 : 1 : ret = rte_vdev_init("net_af_packet_invalid1", "");
899 [ - + ]: 1 : TEST_ASSERT(ret != 0, "Expected failure without iface argument");
900 : :
901 : : return TEST_SUCCESS;
902 : : }
903 : :
904 : : /*
905 : : * Test: Invalid configuration handling - non-existent interface
906 : : */
907 : : static int
908 : 1 : test_af_packet_invalid_bad_iface(void)
909 : : {
910 : : int ret;
911 : :
912 : : /* Test with non-existent interface (should fail) */
913 : 1 : ret = rte_vdev_init("net_af_packet_invalid2",
914 : : "iface=nonexistent_iface_12345");
915 [ - + ]: 1 : TEST_ASSERT(ret != 0, "Expected failure with non-existent interface");
916 : :
917 : : return TEST_SUCCESS;
918 : : }
919 : :
920 : : /*
921 : : * Test: Invalid configuration handling - invalid qpairs
922 : : */
923 : : static int
924 : 1 : test_af_packet_invalid_qpairs(void)
925 : : {
926 : : int ret;
927 : :
928 [ - + ]: 1 : if (!tap_created) {
929 : : printf("SKIPPED: TAP interface not available (need root)\n");
930 : 0 : return TEST_SKIPPED;
931 : : }
932 : :
933 : : /* Test with invalid qpairs (should fail) */
934 : 1 : ret = rte_vdev_init("net_af_packet_invalid3",
935 : : "iface=" TAP_DEV_NAME ",qpairs=0");
936 [ - + ]: 1 : TEST_ASSERT(ret != 0, "Expected failure with qpairs=0");
937 : :
938 : : return TEST_SUCCESS;
939 : : }
940 : :
941 : : /*
942 : : * Test: Custom frame size configuration
943 : : */
944 : : static int
945 : 1 : test_af_packet_frame_config(void)
946 : : {
947 : : struct rte_eth_dev_info dev_info;
948 : : uint16_t test_port;
949 : : char devargs[256];
950 : : int ret;
951 : :
952 [ - + ]: 1 : if (!tap_created) {
953 : : printf("SKIPPED: TAP interface not available (need root)\n");
954 : 0 : return TEST_SKIPPED;
955 : : }
956 : :
957 : : /* Create with custom frame parameters */
958 : : snprintf(devargs, sizeof(devargs),
959 : : "iface=%s,blocksz=4096,framesz=2048,framecnt=256",
960 : : TAP_DEV_NAME);
961 : :
962 : 1 : ret = rte_vdev_init("net_af_packet_frame_test", devargs);
963 [ - + ]: 1 : if (ret != 0) {
964 : : printf("SKIPPED: Could not create port with custom frame config\n");
965 : 0 : return TEST_SKIPPED;
966 : : }
967 : :
968 : 1 : ret = rte_eth_dev_get_port_by_name("net_af_packet_frame_test",
969 : : &test_port);
970 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get port id");
971 : :
972 : 1 : ret = rte_eth_dev_info_get(test_port, &dev_info);
973 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get device info");
974 : :
975 : : /* Cleanup */
976 : 1 : rte_eth_dev_close(test_port);
977 : 1 : rte_vdev_uninit("net_af_packet_frame_test");
978 : :
979 : 1 : return TEST_SUCCESS;
980 : : }
981 : :
982 : : /*
983 : : * Test: Qdisc bypass configuration
984 : : */
985 : : static int
986 : 1 : test_af_packet_qdisc_bypass(void)
987 : : {
988 : : uint16_t test_port;
989 : : char devargs[256];
990 : : int ret;
991 : :
992 [ - + ]: 1 : if (!tap_created) {
993 : : printf("SKIPPED: TAP interface not available (need root)\n");
994 : 0 : return TEST_SKIPPED;
995 : : }
996 : :
997 : : /* Create with qdisc_bypass enabled */
998 : : snprintf(devargs, sizeof(devargs), "iface=%s,qdisc_bypass=1",
999 : : TAP_DEV_NAME);
1000 : :
1001 : 1 : ret = rte_vdev_init("net_af_packet_qdisc_test", devargs);
1002 [ - + ]: 1 : if (ret != 0) {
1003 : : printf("SKIPPED: qdisc_bypass may not be supported\n");
1004 : 0 : return TEST_SKIPPED;
1005 : : }
1006 : :
1007 : 1 : ret = rte_eth_dev_get_port_by_name("net_af_packet_qdisc_test",
1008 : : &test_port);
1009 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get port id");
1010 : :
1011 : : /* Cleanup */
1012 : 1 : rte_eth_dev_close(test_port);
1013 : 1 : rte_vdev_uninit("net_af_packet_qdisc_test");
1014 : :
1015 : 1 : return TEST_SUCCESS;
1016 : : }
1017 : :
1018 : : /*
1019 : : * Test: Multiple queue pairs
1020 : : */
1021 : : static int
1022 : 1 : test_af_packet_multi_queue(void)
1023 : : {
1024 : : struct rte_eth_dev_info dev_info;
1025 : 1 : struct rte_eth_conf port_conf = {0};
1026 : : struct rte_mbuf *bufs[BURST_SIZE];
1027 : : uint16_t test_port;
1028 : : char devargs[256];
1029 : : const uint16_t num_queues = 2;
1030 : : unsigned int allocated, q;
1031 : : int ret;
1032 : :
1033 [ - + ]: 1 : if (!tap_created) {
1034 : : printf("SKIPPED: TAP interface not available (need root)\n");
1035 : 0 : return TEST_SKIPPED;
1036 : : }
1037 : :
1038 : : snprintf(devargs, sizeof(devargs), "iface=%s,qpairs=%u",
1039 : : TAP_DEV_NAME, num_queues);
1040 : 1 : ret = rte_vdev_init("net_af_packet_multi_q", devargs);
1041 [ - + ]: 1 : if (ret != 0) {
1042 : : printf("SKIPPED: Could not create multi-queue port\n");
1043 : 0 : return TEST_SKIPPED;
1044 : : }
1045 : :
1046 : 1 : ret = rte_eth_dev_get_port_by_name("net_af_packet_multi_q", &test_port);
1047 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get port id for multi-queue port");
1048 : :
1049 : 1 : ret = rte_eth_dev_info_get(test_port, &dev_info);
1050 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to get device info");
1051 : :
1052 [ - + ]: 1 : TEST_ASSERT(dev_info.max_rx_queues >= num_queues,
1053 : : "Expected at least %u RX queues, got %u",
1054 : : num_queues, dev_info.max_rx_queues);
1055 [ - + ]: 1 : TEST_ASSERT(dev_info.max_tx_queues >= num_queues,
1056 : : "Expected at least %u TX queues, got %u",
1057 : : num_queues, dev_info.max_tx_queues);
1058 : :
1059 : : /* Configure the port */
1060 : 1 : ret = rte_eth_dev_configure(test_port, num_queues, num_queues, &port_conf);
1061 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to configure multi-queue port");
1062 : :
1063 [ + + ]: 3 : for (q = 0; q < num_queues; q++) {
1064 : 2 : ret = rte_eth_rx_queue_setup(test_port, q, RING_SIZE,
1065 : 2 : rte_eth_dev_socket_id(test_port),
1066 : : NULL, mp);
1067 [ - + ]: 2 : TEST_ASSERT(ret == 0, "Failed to setup RX queue %u", q);
1068 : :
1069 : 2 : ret = rte_eth_tx_queue_setup(test_port, q, RING_SIZE,
1070 : 2 : rte_eth_dev_socket_id(test_port),
1071 : : NULL);
1072 [ - + ]: 2 : TEST_ASSERT(ret == 0, "Failed to setup TX queue %u", q);
1073 : : }
1074 : :
1075 : 1 : ret = rte_eth_dev_start(test_port);
1076 [ - + ]: 1 : TEST_ASSERT(ret == 0, "Failed to start multi-queue port");
1077 : :
1078 : : /* Test TX on different queues using shared helpers */
1079 [ + + ]: 3 : for (q = 0; q < num_queues; q++) {
1080 : 2 : allocated = alloc_tx_mbufs(bufs, BURST_SIZE / 2);
1081 [ + - ]: 2 : if (allocated > 0)
1082 : 2 : do_tx_burst(test_port, q, bufs, allocated);
1083 : : }
1084 : :
1085 : : /* Test RX on different queues */
1086 [ + + ]: 3 : for (q = 0; q < num_queues; q++)
1087 : 2 : do_rx_burst(test_port, q, bufs, BURST_SIZE, NULL);
1088 : :
1089 : : /* Cleanup */
1090 : 1 : rte_eth_dev_stop(test_port);
1091 : 1 : rte_eth_dev_close(test_port);
1092 : 1 : rte_vdev_uninit("net_af_packet_multi_q");
1093 : :
1094 : 1 : return TEST_SUCCESS;
1095 : : }
1096 : :
1097 : : /*
1098 : : * Test: Fanout modes
1099 : : */
1100 : : static int
1101 : 1 : test_af_packet_fanout_modes(void)
1102 : : {
1103 : : static const char * const modes[] = {
1104 : : "hash", "lb", "cpu", "rollover", "rnd", "qm",
1105 : : };
1106 : : uint16_t test_port;
1107 : : char devargs[256];
1108 : : unsigned int i;
1109 : : int ret;
1110 : :
1111 [ - + ]: 1 : if (!tap_created) {
1112 : : printf("SKIPPED: TAP interface not available (need root)\n");
1113 : 0 : return TEST_SKIPPED;
1114 : : }
1115 : :
1116 [ + + ]: 7 : for (i = 0; i < RTE_DIM(modes); i++) {
1117 : : snprintf(devargs, sizeof(devargs), "iface=%s,qpairs=2,fanout_mode=%s",
1118 : 6 : TAP_DEV_NAME, modes[i]);
1119 : :
1120 : 6 : ret = rte_vdev_init("net_af_packet_fanout", devargs);
1121 [ - + ]: 6 : TEST_ASSERT(ret == 0,
1122 : : "Failed to create af_packet sockets with fanout_mode=%s: %d",
1123 : : modes[i], ret);
1124 : :
1125 : 6 : ret = rte_eth_dev_get_port_by_name("net_af_packet_fanout", &test_port);
1126 [ + - ]: 6 : if (ret == 0)
1127 : 6 : rte_eth_dev_close(test_port);
1128 : 6 : rte_vdev_uninit("net_af_packet_fanout");
1129 [ - + ]: 6 : TEST_ASSERT(ret == 0,
1130 : : "af_packet port with fanout_mode=%s has no port id: %d",
1131 : : modes[i], ret);
1132 : : }
1133 : :
1134 : : return TEST_SUCCESS;
1135 : : }
1136 : :
1137 : : /*
1138 : : * Multi-threaded test context
1139 : : */
1140 : : struct mt_test_ctx {
1141 : : uint16_t port_id;
1142 : : RTE_ATOMIC(uint32_t) stop;
1143 : : uint32_t rx_count;
1144 : : };
1145 : :
1146 : : /*
1147 : : * Reader thread function for multi-threaded test
1148 : : */
1149 : : static void *
1150 : 1 : mt_reader_thread(void *arg)
1151 : : {
1152 : : struct mt_test_ctx *ctx = arg;
1153 : : struct rte_mbuf *bufs[BURST_SIZE];
1154 : : uint16_t nb_rx;
1155 : :
1156 [ - + ]: 1 : while (!rte_atomic_load_explicit(&ctx->stop, rte_memory_order_relaxed)) {
1157 : 0 : nb_rx = rte_eth_rx_burst(ctx->port_id, 0, bufs, BURST_SIZE);
1158 [ # # ]: 0 : if (nb_rx > 0) {
1159 : 0 : ctx->rx_count += nb_rx;
1160 : 0 : rte_pktmbuf_free_bulk(bufs, nb_rx);
1161 : : }
1162 : :
1163 : 0 : rte_delay_us_block(50);
1164 : : }
1165 : :
1166 : 1 : return NULL;
1167 : : }
1168 : :
1169 : : /*
1170 : : * Test: Multi-threaded concurrent TX and RX
1171 : : * Main thread sends packets while a reader thread receives,
1172 : : * exercising concurrent access to the af_packet PMD.
1173 : : */
1174 : : static int
1175 : 1 : test_af_packet_multithread(void)
1176 : : {
1177 : 1 : struct mt_test_ctx ctx = {0};
1178 : : struct rte_mbuf *bufs[BURST_SIZE];
1179 : : pthread_t reader_tid;
1180 : : struct timespec start, now;
1181 : : uint32_t tx_count = 0;
1182 : : unsigned int allocated;
1183 : : uint16_t nb_tx;
1184 : : int ret;
1185 : :
1186 [ + - - + ]: 1 : if (!tap_created || port_id >= RTE_MAX_ETHPORTS) {
1187 : : printf("SKIPPED: Port not available (need root)\n");
1188 : 0 : return TEST_SKIPPED;
1189 : : }
1190 : :
1191 : 1 : ctx.port_id = port_id;
1192 : :
1193 : : /* Start reader thread */
1194 : 1 : ret = pthread_create(&reader_tid, NULL, mt_reader_thread, &ctx);
1195 [ - + ]: 1 : if (ret != 0) {
1196 : : printf("SKIPPED: Failed to create reader thread: %d\n", ret);
1197 : 0 : return TEST_SKIPPED;
1198 : : }
1199 : :
1200 : : /* Main thread sends packets for the configured duration */
1201 : 1 : clock_gettime(CLOCK_MONOTONIC, &start);
1202 : : do {
1203 : 13 : allocated = alloc_tx_mbufs(bufs, BURST_SIZE / 4);
1204 [ + - ]: 13 : if (allocated == 0)
1205 : : break;
1206 : :
1207 : 13 : nb_tx = do_tx_burst(port_id, 0, bufs, allocated);
1208 : 13 : tx_count += nb_tx;
1209 : :
1210 : 13 : rte_delay_us_block(100);
1211 : 13 : clock_gettime(CLOCK_MONOTONIC, &now);
1212 [ + + ]: 13 : } while (tx_count < MT_NUM_PACKETS &&
1213 [ + - ]: 12 : (now.tv_sec - start.tv_sec) < MT_TEST_DURATION_SEC);
1214 : :
1215 : : /* Signal reader to stop */
1216 : 1 : rte_atomic_store_explicit(&ctx.stop, 1, rte_memory_order_relaxed);
1217 : 1 : pthread_join(reader_tid, NULL);
1218 : :
1219 : 1 : printf("Multi-threaded test: TX=%u, RX=%u (duration=%ds)\n",
1220 : : tx_count, ctx.rx_count, MT_TEST_DURATION_SEC);
1221 : :
1222 [ - + ]: 1 : TEST_ASSERT(tx_count > 0,
1223 : : "Expected some packets to be transmitted");
1224 : :
1225 : : return TEST_SUCCESS;
1226 : : }
1227 : :
1228 : : /*
1229 : : * Test: Ignore outgoing configuration
1230 : : *
1231 : : * By default an AF_PACKET socket sees frames sent by other sockets on
1232 : : * the same interface. ignore_outgoing filters those out. This test
1233 : : * uses a second vdev to generate outgoing traffic. It also writes to
1234 : : * the TAP fd to generate incoming traffic, so that a dead RX port can
1235 : : * be distinguished from one which filtered the outgoing frames.
1236 : : */
1237 : : static int
1238 : 4 : test_af_packet_ignore_outgoing(uint16_t nb_queues, bool ignore_outgoing)
1239 : : {
1240 : : struct rte_mbuf *bufs[BURST_SIZE];
1241 : : uint16_t tx_port, rx_port, nb_tx, q;
1242 : 4 : struct tag_count tag_count = {0};
1243 : : unsigned int allocated;
1244 : : const unsigned int n_inject = 4;
1245 : : uint64_t elapsed = 0;
1246 : : const char *err = NULL;
1247 : : bool have_rx = false;
1248 : : bool have_tx = false;
1249 : : char args[128];
1250 : : int ret;
1251 : :
1252 [ - + ]: 4 : if (!tap_created) {
1253 : : printf("SKIPPED: TAP interface not available (need root)\n");
1254 : 0 : return TEST_SKIPPED;
1255 : : }
1256 : :
1257 : 4 : snprintf(args, sizeof(args),
1258 : : "iface=%s,qpairs=%u,ignore_outgoing=%u",
1259 : : TAP_DEV_NAME, nb_queues, ignore_outgoing ? 1 : 0);
1260 : 4 : ret = create_af_packet_port("net_af_packet_ign_rx", args, &rx_port);
1261 [ - + ]: 4 : if (ret != 0) {
1262 : : err = "Failed to create RX af_packet port";
1263 : 0 : goto out;
1264 : : }
1265 : : have_rx = true;
1266 : :
1267 : : /* qdisc_bypass=0 so other AF_PACKET sockets see this TX */
1268 : 4 : ret = create_af_packet_port("net_af_packet_ign_tx",
1269 : : "iface=" TAP_DEV_NAME ",qdisc_bypass=0", &tx_port);
1270 [ - + ]: 4 : if (ret != 0) {
1271 : : err = "Failed to create TX af_packet port";
1272 : 0 : goto out;
1273 : : }
1274 : : have_tx = true;
1275 : :
1276 [ + - - + ]: 8 : if (configure_af_packet_port(rx_port, nb_queues, nb_queues) != 0 ||
1277 : 4 : configure_af_packet_port(tx_port, 1, 1) != 0) {
1278 : : err = "Failed to configure ports";
1279 : 0 : goto out;
1280 : : }
1281 : :
1282 : 4 : flush_rx_port(rx_port, nb_queues);
1283 : :
1284 : : /* send outgoing packets from the TX af_packet port */
1285 : 4 : allocated = alloc_tx_mbufs(bufs, n_inject);
1286 : 4 : nb_tx = do_tx_burst(tx_port, 0, bufs, allocated);
1287 [ - + ]: 4 : if (allocated == 0 || nb_tx == 0) {
1288 : : err = "TX setup failed";
1289 : 0 : goto out;
1290 : : }
1291 : :
1292 : : /* inject incoming packets on the TAP */
1293 [ - + ]: 4 : if (do_tap_fd_tx_burst(tap_fd, n_inject) != 0) {
1294 : : err = "TAP inject failed";
1295 : 0 : goto out;
1296 : : }
1297 : :
1298 : : /* wait long enough for frames to arrive */
1299 [ + + ]: 10004 : while (elapsed < LOOPBACK_TIMEOUT_US) {
1300 [ + + ]: 25005 : for (q = 0; q < nb_queues; q++)
1301 : 15003 : do_rx_burst(rx_port, q, bufs, BURST_SIZE, &tag_count);
1302 [ + + ]: 10002 : if (!ignore_outgoing &&
1303 [ + - ]: 2 : tag_count.n_dev_tagged >= nb_tx &&
1304 [ + - ]: 2 : tag_count.n_fd_tagged > 0)
1305 : : break;
1306 : 10000 : rte_delay_us_block(STATS_POLL_INTERVAL_US);
1307 : 10000 : elapsed += STATS_POLL_INTERVAL_US;
1308 : : }
1309 : :
1310 : 4 : out:
1311 [ + - ]: 4 : if (have_tx) {
1312 : 4 : rte_eth_dev_stop(tx_port);
1313 : 4 : rte_eth_dev_close(tx_port);
1314 : 4 : rte_vdev_uninit("net_af_packet_ign_tx");
1315 : : }
1316 [ + - ]: 4 : if (have_rx) {
1317 : 4 : rte_eth_dev_stop(rx_port);
1318 : 4 : rte_eth_dev_close(rx_port);
1319 : 4 : rte_vdev_uninit("net_af_packet_ign_rx");
1320 : : }
1321 : :
1322 [ - + ]: 4 : TEST_ASSERT(err == NULL, "%s", err);
1323 [ - + ]: 4 : TEST_ASSERT(tag_count.n_fd_tagged > 0,
1324 : : "Expected frames written to the TAP fd");
1325 [ + + ]: 4 : if (ignore_outgoing)
1326 [ - + ]: 2 : TEST_ASSERT(tag_count.n_dev_tagged == 0,
1327 : : "Expected no outgoing frames with ignore_outgoing");
1328 : : else
1329 [ - + ]: 2 : TEST_ASSERT(tag_count.n_dev_tagged > 0,
1330 : : "Expected outgoing frames with ignore_outgoing=0");
1331 : :
1332 : : return TEST_SUCCESS;
1333 : : }
1334 : :
1335 : : /*
1336 : : * Test: invalid ignore_outgoing value
1337 : : */
1338 : : static int
1339 : 1 : test_af_packet_invalid_ignore_outgoing(void)
1340 : : {
1341 : : int ret;
1342 : :
1343 [ - + ]: 1 : if (!tap_created) {
1344 : : printf("SKIPPED: TAP interface not available (need root)\n");
1345 : 0 : return TEST_SKIPPED;
1346 : : }
1347 : :
1348 : 1 : ret = rte_vdev_init("net_af_packet_ign_bad",
1349 : : "iface=" TAP_DEV_NAME ",ignore_outgoing=2");
1350 [ - + ]: 1 : TEST_ASSERT(ret != 0, "Expected failure with ignore_outgoing=2");
1351 : :
1352 : : return TEST_SUCCESS;
1353 : : }
1354 : :
1355 : : /*
1356 : : * Test: Ignore outgoing for one queue
1357 : : */
1358 : : static int
1359 : 1 : test_af_packet_ignore_outgoing_single_queue(void)
1360 : : {
1361 : : int ret;
1362 : :
1363 : 1 : ret = test_af_packet_ignore_outgoing(1, true);
1364 [ + - ]: 1 : if (ret != TEST_SUCCESS)
1365 : : return ret;
1366 : :
1367 : 1 : return test_af_packet_ignore_outgoing(1, false);
1368 : : }
1369 : :
1370 : : /*
1371 : : * Test: Ignore outgoing for multiple queues
1372 : : */
1373 : : static int
1374 : 1 : test_af_packet_ignore_outgoing_multi_queue(void)
1375 : : {
1376 : : int ret;
1377 : :
1378 : 1 : ret = test_af_packet_ignore_outgoing(2, true);
1379 [ + - ]: 1 : if (ret != TEST_SUCCESS)
1380 : : return ret;
1381 : :
1382 : 1 : return test_af_packet_ignore_outgoing(2, false);
1383 : : }
1384 : :
1385 : : static struct unit_test_suite af_packet_test_suite = {
1386 : : .suite_name = "AF_PACKET PMD Unit Test Suite",
1387 : : .setup = test_af_packet_setup,
1388 : : .teardown = test_af_packet_teardown,
1389 : : .unit_test_cases = {
1390 : : /* Tests that don't modify device state */
1391 : : TEST_CASE(test_af_packet_dev_info),
1392 : : TEST_CASE(test_af_packet_link_status),
1393 : : TEST_CASE(test_af_packet_stats_init),
1394 : : TEST_CASE(test_af_packet_tx),
1395 : : TEST_CASE(test_af_packet_rx),
1396 : : TEST_CASE(test_af_packet_loopback),
1397 : : TEST_CASE(test_af_packet_promiscuous),
1398 : : TEST_CASE(test_af_packet_mac_addr),
1399 : : TEST_CASE(test_af_packet_mtu),
1400 : : TEST_CASE(test_af_packet_stats_reset),
1401 : : TEST_CASE(test_af_packet_multithread),
1402 : :
1403 : : /* Tests that create their own devices */
1404 : : TEST_CASE(test_af_packet_invalid_no_iface),
1405 : : TEST_CASE(test_af_packet_invalid_bad_iface),
1406 : : TEST_CASE(test_af_packet_invalid_qpairs),
1407 : : TEST_CASE(test_af_packet_frame_config),
1408 : : TEST_CASE(test_af_packet_qdisc_bypass),
1409 : : TEST_CASE(test_af_packet_multi_queue),
1410 : : TEST_CASE(test_af_packet_fanout_modes),
1411 : : TEST_CASE(test_af_packet_invalid_ignore_outgoing),
1412 : : TEST_CASE(test_af_packet_ignore_outgoing_single_queue),
1413 : : TEST_CASE(test_af_packet_ignore_outgoing_multi_queue),
1414 : :
1415 : : TEST_CASES_END() /**< NULL terminate unit test array */
1416 : : }
1417 : : };
1418 : :
1419 : : static int
1420 : 1 : test_pmd_af_packet(void)
1421 : : {
1422 : 1 : return unit_test_suite_runner(&af_packet_test_suite);
1423 : : }
1424 : : #endif
1425 : :
1426 : 306 : REGISTER_FAST_TEST(af_packet_pmd_autotest, NOHUGE_OK, ASAN_OK, test_pmd_af_packet);
|