Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Ericsson AB
3 : : */
4 : :
5 : : /*
6 : : * Basic test of memif PMD functionality.
7 : : * Based off of the TAP PMD test (test_pmd_tap.c).
8 : : *
9 : : * memif connects two endpoints over shared memory. Unlike a NIC or TAP
10 : : * device, a memif port is only usable once its peer connects, so the test
11 : : * creates a server and a client memif port in this one process, connected
12 : : * over an abstract-namespace socket (the loopback setup documented in
13 : : * doc/guides/nics/memif.rst). The control-channel handshake is asynchronous,
14 : : * so the test waits for both ports to report link up before exercising the
15 : : * data path.
16 : : */
17 : :
18 : : #include "test.h"
19 : :
20 : : #include <stdio.h>
21 : :
22 : : #ifndef RTE_EXEC_ENV_LINUX
23 : :
24 : : /* memif PMD is only available on Linux */
25 : : static int
26 : : test_pmd_memif(void)
27 : : {
28 : : printf("memif PMD not supported on this platform, skipping test\n");
29 : : return TEST_SKIPPED;
30 : : }
31 : :
32 : : #else /* RTE_EXEC_ENV_LINUX */
33 : :
34 : : #include <string.h>
35 : : #include <unistd.h>
36 : :
37 : : #include <rte_ethdev.h>
38 : : #include <rte_bus_vdev.h>
39 : : #include <rte_mbuf.h>
40 : : #include <rte_ether.h>
41 : :
42 : : #define SOCKET0 0
43 : : #define RING_SIZE 256
44 : : #define NB_MBUF 4096
45 : : #define MAX_PKT_BURST 32
46 : : #define PKT_LEN 64
47 : :
48 : : /* memif ignores nb_tx_desc; the ring size is set via the rsize arg. */
49 : : #define MEMIF_LOG2_RING 8
50 : :
51 : : /*
52 : : * Same socket name and id on both ports makes them a connected pair.
53 : : * The small-bsize pair forces chained descriptors; the default-bsize pair
54 : : * exercises the single-pool Tx fast path.
55 : : */
56 : : #define MEMIF_SRV_NAME "net_memif_test_srv"
57 : : #define MEMIF_CLI_NAME "net_memif_test_cli"
58 : : #define MEMIF_SRV_ARGS "role=server,id=0,bsize=512,rsize=8,socket-abstract=yes,socket=memif-autotest"
59 : : #define MEMIF_CLI_ARGS "role=client,id=0,bsize=512,rsize=8,socket-abstract=yes,socket=memif-autotest"
60 : : #define MEMIF_FP_SRV_NAME "net_memif_test_fsrv"
61 : : #define MEMIF_FP_CLI_NAME "net_memif_test_fcli"
62 : : #define MEMIF_FP_SRV_ARGS "role=server,id=2,socket-abstract=yes,socket=memif-autotest-fp"
63 : : #define MEMIF_FP_CLI_ARGS "role=client,id=2,socket-abstract=yes,socket=memif-autotest-fp"
64 : :
65 : : /* Time to wait for the asynchronous control-channel handshake. */
66 : : #define CONNECT_TRIES 50
67 : : #define CONNECT_DELAY_US 100000
68 : : /* Shorter window for a negative check (peer must NOT connect). */
69 : : #define REJECT_TRIES 10
70 : :
71 : : static struct rte_mempool *mp;
72 : : static int memif_srv_port = -1;
73 : : static int memif_cli_port = -1;
74 : : static int memif_fp_srv_port = -1;
75 : : static int memif_fp_cli_port = -1;
76 : :
77 : : static int
78 : 0 : find_port(const char *frag)
79 : : {
80 : : uint16_t port_id;
81 : : char name[RTE_ETH_NAME_MAX_LEN];
82 : :
83 [ # # ]: 0 : RTE_ETH_FOREACH_DEV(port_id) {
84 [ # # ]: 0 : if (rte_eth_dev_get_name_by_port(port_id, name, sizeof(name)) == 0 &&
85 [ # # ]: 0 : strstr(name, frag) != NULL)
86 : 0 : return port_id;
87 : : }
88 : : return -1;
89 : : }
90 : :
91 : : static int
92 : 0 : configure_port(int port)
93 : : {
94 : : struct rte_eth_conf port_conf;
95 : : int ret;
96 : :
97 : : memset(&port_conf, 0, sizeof(port_conf));
98 : :
99 : 0 : ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
100 [ # # ]: 0 : if (ret < 0) {
101 : 0 : printf("Configure failed for port %d: %s\n",
102 : : port, rte_strerror(-ret));
103 : 0 : return -1;
104 : : }
105 : :
106 : 0 : ret = rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL);
107 [ # # ]: 0 : if (ret < 0) {
108 : 0 : printf("TX queue setup failed for port %d: %s\n",
109 : : port, rte_strerror(-ret));
110 : 0 : return -1;
111 : : }
112 : :
113 : 0 : ret = rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL, mp);
114 [ # # ]: 0 : if (ret < 0) {
115 : 0 : printf("RX queue setup failed for port %d: %s\n",
116 : : port, rte_strerror(-ret));
117 : 0 : return -1;
118 : : }
119 : :
120 : 0 : ret = rte_eth_dev_start(port);
121 [ # # ]: 0 : if (ret < 0) {
122 : 0 : printf("Error starting port %d: %s\n",
123 : : port, rte_strerror(-ret));
124 : 0 : return -1;
125 : : }
126 : :
127 : : return 0;
128 : : }
129 : :
130 : : /* Wait until both ports report link up, i.e. the peers have connected. */
131 : : static int
132 : 0 : wait_for_link_pair_tries(int p0, int p1, int tries)
133 : : {
134 : : struct rte_eth_link l0, l1;
135 : : int i;
136 : :
137 [ # # ]: 0 : for (i = 0; i < tries; i++) {
138 [ # # # # ]: 0 : if (rte_eth_link_get_nowait(p0, &l0) == 0 &&
139 : 0 : rte_eth_link_get_nowait(p1, &l1) == 0 &&
140 [ # # ]: 0 : l0.link_status == RTE_ETH_LINK_UP &&
141 [ # # ]: 0 : l1.link_status == RTE_ETH_LINK_UP)
142 : : return 0;
143 : 0 : usleep(CONNECT_DELAY_US);
144 : : }
145 : : return -1;
146 : : }
147 : :
148 : : static int
149 : : wait_for_link_pair(int p0, int p1)
150 : : {
151 : 0 : return wait_for_link_pair_tries(p0, p1, CONNECT_TRIES);
152 : : }
153 : :
154 : : static int
155 : : wait_for_link(void)
156 : : {
157 : 0 : return wait_for_link_pair(memif_srv_port, memif_cli_port);
158 : : }
159 : :
160 : : static struct rte_mbuf *
161 : 0 : create_test_packet(uint16_t pkt_len, uint8_t seed)
162 : : {
163 : : struct rte_mbuf *mbuf;
164 : : struct rte_ether_hdr *eth_hdr;
165 : : uint8_t *payload;
166 : : uint16_t i;
167 : :
168 : 0 : mbuf = rte_pktmbuf_alloc(mp);
169 [ # # ]: 0 : if (mbuf == NULL)
170 : : return NULL;
171 : :
172 : : if (pkt_len < RTE_ETHER_MIN_LEN)
173 : : pkt_len = RTE_ETHER_MIN_LEN;
174 : :
175 : : eth_hdr = (struct rte_ether_hdr *)rte_pktmbuf_append(mbuf, pkt_len);
176 [ # # ]: 0 : if (eth_hdr == NULL) {
177 : 0 : rte_pktmbuf_free(mbuf);
178 : 0 : return NULL;
179 : : }
180 : :
181 : 0 : memset(ð_hdr->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
182 : 0 : memset(ð_hdr->src_addr, 0x02, RTE_ETHER_ADDR_LEN);
183 : 0 : eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
184 : :
185 : : payload = (uint8_t *)(eth_hdr + 1);
186 [ # # ]: 0 : for (i = 0; i < pkt_len - sizeof(*eth_hdr); i++)
187 : 0 : payload[i] = (uint8_t)(seed + i);
188 : :
189 : : return mbuf;
190 : : }
191 : :
192 : : static int
193 : 0 : test_memif_dev_info(void)
194 : : {
195 : : struct rte_eth_dev_info dev_info;
196 : : int ret;
197 : :
198 : 0 : ret = rte_eth_dev_info_get(memif_srv_port, &dev_info);
199 [ # # ]: 0 : TEST_ASSERT(ret == 0, "dev_info_get failed: %s", rte_strerror(-ret));
200 [ # # ]: 0 : TEST_ASSERT(strcmp(dev_info.driver_name, "net_memif") == 0,
201 : : "unexpected driver name: %s", dev_info.driver_name);
202 : : return TEST_SUCCESS;
203 : : }
204 : :
205 : : static int
206 : 0 : test_memif_link_up(void)
207 : : {
208 : : /* The pair must have connected during setup. */
209 [ # # ]: 0 : TEST_ASSERT(wait_for_link() == 0,
210 : : "memif server/client did not reach link up");
211 : : return TEST_SUCCESS;
212 : : }
213 : :
214 : : static int
215 : 0 : test_memif_stats_get(void)
216 : : {
217 : : struct rte_eth_stats stats;
218 : :
219 [ # # ]: 0 : TEST_ASSERT(rte_eth_stats_get(memif_srv_port, &stats) == 0,
220 : : "stats_get failed for server port");
221 [ # # ]: 0 : TEST_ASSERT(rte_eth_stats_get(memif_cli_port, &stats) == 0,
222 : : "stats_get failed for client port");
223 : : return TEST_SUCCESS;
224 : : }
225 : :
226 : : static int
227 : 0 : test_memif_stats_reset(void)
228 : : {
229 : : struct rte_eth_stats stats;
230 : :
231 [ # # ]: 0 : TEST_ASSERT(rte_eth_stats_reset(memif_srv_port) == 0,
232 : : "stats_reset failed");
233 [ # # ]: 0 : TEST_ASSERT(rte_eth_stats_get(memif_srv_port, &stats) == 0,
234 : : "stats_get after reset failed");
235 [ # # # # : 0 : TEST_ASSERT(stats.ipackets == 0 && stats.opackets == 0 &&
# # # # ]
236 : : stats.ibytes == 0 && stats.obytes == 0,
237 : : "stats not zero after reset");
238 : : return TEST_SUCCESS;
239 : : }
240 : :
241 : : /*
242 : : * Send a burst from the client and receive it on the server, verifying the
243 : : * payload is delivered intact. This exercises the Rx/Tx data path over the
244 : : * connected memif pair.
245 : : */
246 : : static int
247 : 0 : test_memif_send_receive(void)
248 : : {
249 : : struct rte_mbuf *tx[MAX_PKT_BURST];
250 : : struct rte_mbuf *rx[MAX_PKT_BURST];
251 : : const uint16_t nb = MAX_PKT_BURST / 2;
252 : : uint16_t nb_tx, nb_rx, got;
253 : : int i, tries;
254 : :
255 [ # # ]: 0 : TEST_ASSERT(wait_for_link() == 0, "link not up before send/receive");
256 : :
257 [ # # ]: 0 : for (i = 0; i < nb; i++) {
258 : 0 : tx[i] = create_test_packet(PKT_LEN, (uint8_t)i);
259 [ # # ]: 0 : if (tx[i] == NULL) {
260 [ # # ]: 0 : while (--i >= 0)
261 : 0 : rte_pktmbuf_free(tx[i]);
262 : 0 : TEST_ASSERT(0, "failed to build test packet");
263 : : }
264 : : }
265 : :
266 : 0 : nb_tx = rte_eth_tx_burst(memif_cli_port, 0, tx, nb);
267 [ # # ]: 0 : for (i = nb_tx; i < nb; i++)
268 : 0 : rte_pktmbuf_free(tx[i]);
269 [ # # ]: 0 : TEST_ASSERT(nb_tx > 0, "client transmitted no packets");
270 : :
271 : : /* Receive may take a moment; poll a few times. */
272 : : got = 0;
273 [ # # ]: 0 : for (tries = 0; tries < 10 && got < nb_tx; tries++) {
274 : 0 : nb_rx = rte_eth_rx_burst(memif_srv_port, 0, rx, MAX_PKT_BURST);
275 [ # # ]: 0 : for (i = 0; i < nb_rx; i++) {
276 : : /* Payload must match the position-dependent pattern. */
277 : 0 : uint8_t *p = rte_pktmbuf_mtod_offset(rx[i], uint8_t *,
278 : : sizeof(struct rte_ether_hdr));
279 : : uint16_t plen = PKT_LEN -
280 : : sizeof(struct rte_ether_hdr);
281 : : uint16_t k;
282 : 0 : int bad = rte_pktmbuf_pkt_len(rx[i]) != PKT_LEN;
283 : :
284 [ # # ]: 0 : for (k = 0; !bad && k < plen; k++)
285 [ # # ]: 0 : if (p[k] != (uint8_t)(got + k))
286 : : bad = 1;
287 [ # # ]: 0 : if (bad) {
288 [ # # ]: 0 : for (k = i; k < nb_rx; k++)
289 : 0 : rte_pktmbuf_free(rx[k]);
290 : 0 : TEST_ASSERT(0,
291 : : "packet %u payload/length mismatch",
292 : : got);
293 : : }
294 : 0 : rte_pktmbuf_free(rx[i]);
295 : 0 : got++;
296 : : }
297 [ # # ]: 0 : if (got < nb_tx)
298 : 0 : usleep(10000);
299 : : }
300 : :
301 [ # # ]: 0 : TEST_ASSERT(got == nb_tx,
302 : : "received %u packets, expected %u", got, nb_tx);
303 : : return TEST_SUCCESS;
304 : : }
305 : :
306 : : /*
307 : : * Single-segment loopback on the default-bsize pair, where bsize exceeds
308 : : * the mbuf data room so eth_memif_tx() takes the single-pool fast path.
309 : : */
310 : : static int
311 : 0 : test_memif_fastpath(void)
312 : : {
313 : : struct rte_mbuf *tx[MAX_PKT_BURST];
314 : : struct rte_mbuf *rx[MAX_PKT_BURST];
315 : : const uint16_t nb = MAX_PKT_BURST / 2;
316 : : uint16_t nb_tx, got;
317 : : int i, tries;
318 : :
319 [ # # ]: 0 : TEST_ASSERT(wait_for_link_pair(memif_fp_srv_port, memif_fp_cli_port)
320 : : == 0, "fast-path pair not up");
321 : :
322 [ # # ]: 0 : for (i = 0; i < nb; i++) {
323 : 0 : tx[i] = create_test_packet(PKT_LEN, (uint8_t)i);
324 [ # # ]: 0 : if (tx[i] == NULL) {
325 [ # # ]: 0 : while (--i >= 0)
326 : 0 : rte_pktmbuf_free(tx[i]);
327 : 0 : TEST_ASSERT(0, "failed to build test packet");
328 : : }
329 : : }
330 : :
331 : 0 : nb_tx = rte_eth_tx_burst(memif_fp_cli_port, 0, tx, nb);
332 [ # # ]: 0 : for (i = nb_tx; i < nb; i++)
333 : 0 : rte_pktmbuf_free(tx[i]);
334 [ # # ]: 0 : TEST_ASSERT(nb_tx > 0, "fast-path client transmitted no packets");
335 : :
336 : : got = 0;
337 [ # # ]: 0 : for (tries = 0; tries < 10 && got < nb_tx; tries++) {
338 : 0 : uint16_t nb_rx = rte_eth_rx_burst(memif_fp_srv_port, 0, rx,
339 : : MAX_PKT_BURST);
340 [ # # ]: 0 : for (i = 0; i < nb_rx; i++) {
341 : 0 : uint8_t *p = rte_pktmbuf_mtod_offset(rx[i], uint8_t *,
342 : : sizeof(struct rte_ether_hdr));
343 : : uint16_t plen = PKT_LEN -
344 : : sizeof(struct rte_ether_hdr);
345 : : uint16_t k;
346 : 0 : int bad = rte_pktmbuf_pkt_len(rx[i]) != PKT_LEN;
347 : :
348 [ # # ]: 0 : for (k = 0; !bad && k < plen; k++)
349 [ # # ]: 0 : if (p[k] != (uint8_t)(got + k))
350 : : bad = 1;
351 [ # # ]: 0 : if (bad) {
352 [ # # ]: 0 : for (k = i; k < nb_rx; k++)
353 : 0 : rte_pktmbuf_free(rx[k]);
354 : 0 : TEST_ASSERT(0,
355 : : "fast-path packet %u payload/length mismatch",
356 : : got);
357 : : }
358 : 0 : rte_pktmbuf_free(rx[i]);
359 : 0 : got++;
360 : : }
361 [ # # ]: 0 : if (got < nb_tx)
362 : 0 : usleep(10000);
363 : : }
364 : :
365 [ # # ]: 0 : TEST_ASSERT(got == nb_tx, "fast-path received %u expected %u",
366 : : got, nb_tx);
367 : : return TEST_SUCCESS;
368 : : }
369 : :
370 : : static int
371 : 0 : test_memif_stop_start(void)
372 : : {
373 [ # # ]: 0 : TEST_ASSERT(rte_eth_dev_stop(memif_cli_port) == 0,
374 : : "stop client failed");
375 [ # # ]: 0 : TEST_ASSERT(rte_eth_dev_start(memif_cli_port) == 0,
376 : : "restart client failed");
377 : : /* After a restart the pair must reconnect. */
378 [ # # ]: 0 : TEST_ASSERT(wait_for_link() == 0, "link did not recover after restart");
379 : : return TEST_SUCCESS;
380 : : }
381 : :
382 : : /*
383 : : * A zero-length Tx burst must be accepted and return 0 without touching
384 : : * the (empty) mbuf array. Regression test for the empty-burst guard in
385 : : * eth_memif_tx().
386 : : */
387 : : static int
388 : 0 : test_memif_tx_empty_burst(void)
389 : : {
390 : 0 : struct rte_mbuf *dummy = NULL;
391 : :
392 [ # # ]: 0 : TEST_ASSERT(wait_for_link() == 0, "link not up before empty burst");
393 [ # # ]: 0 : TEST_ASSERT(rte_eth_tx_burst(memif_cli_port, 0, &dummy, 0) == 0,
394 : : "zero-length Tx burst did not return 0");
395 : : return TEST_SUCCESS;
396 : : }
397 : :
398 : : /*
399 : : * Close a configured but never-started port. The queues were never
400 : : * allocated, so memif_disconnect() must skip the NULL queue entries
401 : : * rather than dereference them. Regression test for the NULL-queue guard
402 : : * in the disconnect-on-close fix. Uses a throwaway device so the suite
403 : : * pair stays intact.
404 : : */
405 : : static int
406 : 0 : test_memif_close_without_stop(void)
407 : : {
408 : : int sport, ret;
409 : : struct rte_eth_conf port_conf;
410 : :
411 : 0 : ret = rte_vdev_init("net_memif_test_tmps",
412 : : "role=server,id=1,socket-abstract=yes,socket=memif-autotest-tmp");
413 [ # # ]: 0 : if (ret < 0) {
414 : 0 : printf("could not create throwaway server: %s\n",
415 : : rte_strerror(-ret));
416 : 0 : return TEST_FAILED;
417 : : }
418 : :
419 : 0 : sport = find_port("net_memif_test_tmps");
420 [ # # ]: 0 : if (sport < 0) {
421 : 0 : rte_vdev_uninit("net_memif_test_tmps");
422 : 0 : TEST_ASSERT(0, "throwaway server port not found");
423 : : }
424 : :
425 : : /* Configure but do not start: close is rejected on a started port. */
426 : : memset(&port_conf, 0, sizeof(port_conf));
427 : 0 : ret = rte_eth_dev_configure(sport, 1, 1, &port_conf);
428 [ # # ]: 0 : if (ret < 0) {
429 : 0 : rte_vdev_uninit("net_memif_test_tmps");
430 : 0 : TEST_ASSERT(0, "configure of throwaway server failed: %s",
431 : : rte_strerror(-ret));
432 : : }
433 : :
434 : : /* Close without ever starting; must not crash on the NULL queues. */
435 : 0 : ret = rte_eth_dev_close(sport);
436 : 0 : rte_vdev_uninit("net_memif_test_tmps");
437 [ # # ]: 0 : TEST_ASSERT(ret == 0, "close without stop failed: %s",
438 : : rte_strerror(-ret));
439 : :
440 : : return TEST_SUCCESS;
441 : : }
442 : :
443 : : /*
444 : : * A server that has been started and then stopped must refuse peers: a
445 : : * connection accepted while stopped would leave a live control channel on
446 : : * a stopped port. Regression test for the refuse-connection-on-stopped-
447 : : * device fix. Without it the stopped server reports link up here.
448 : : */
449 : : static int
450 : 0 : test_memif_stopped_server_rejects_peer(void)
451 : : {
452 : : int sport, cport, ret;
453 : :
454 : 0 : ret = rte_vdev_init("net_memif_test_tmp2s",
455 : : "role=server,id=3,socket-abstract=yes,socket=memif-autotest-tmp2");
456 [ # # ]: 0 : if (ret < 0) {
457 : 0 : printf("could not create throwaway server: %s\n",
458 : : rte_strerror(-ret));
459 : 0 : return TEST_FAILED;
460 : : }
461 : 0 : sport = find_port("net_memif_test_tmp2s");
462 [ # # ]: 0 : if (sport < 0) {
463 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
464 : 0 : TEST_ASSERT(0, "throwaway server port not found");
465 : : }
466 [ # # ]: 0 : if (configure_port(sport) < 0) {
467 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
468 : 0 : TEST_ASSERT(0, "throwaway server start failed");
469 : : }
470 [ # # ]: 0 : if (rte_eth_dev_stop(sport) < 0) {
471 : 0 : rte_eth_dev_close(sport);
472 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
473 : 0 : TEST_ASSERT(0, "throwaway server stop failed");
474 : : }
475 : :
476 : 0 : ret = rte_vdev_init("net_memif_test_tmp2c",
477 : : "role=client,id=3,socket-abstract=yes,socket=memif-autotest-tmp2");
478 [ # # ]: 0 : if (ret < 0) {
479 : 0 : rte_eth_dev_close(sport);
480 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
481 : 0 : TEST_ASSERT(0, "could not create throwaway client: %s",
482 : : rte_strerror(-ret));
483 : : }
484 : 0 : cport = find_port("net_memif_test_tmp2c");
485 [ # # # # ]: 0 : if (cport < 0 || configure_port(cport) < 0) {
486 : 0 : rte_eth_dev_close(sport);
487 : 0 : rte_vdev_uninit("net_memif_test_tmp2c");
488 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
489 : 0 : TEST_ASSERT(0, "throwaway client setup failed");
490 : : }
491 : :
492 : : /* The stopped server must not come up (short window; it never will). */
493 : 0 : ret = wait_for_link_pair_tries(sport, cport, REJECT_TRIES);
494 : :
495 : 0 : rte_eth_dev_stop(cport);
496 : 0 : rte_eth_dev_close(cport);
497 : 0 : rte_eth_dev_close(sport);
498 : 0 : rte_vdev_uninit("net_memif_test_tmp2c");
499 : 0 : rte_vdev_uninit("net_memif_test_tmp2s");
500 : :
501 [ # # ]: 0 : TEST_ASSERT(ret != 0, "stopped server accepted a peer");
502 : : return TEST_SUCCESS;
503 : : }
504 : :
505 : : #define BIG_PKT_LEN 4000 /* > bsize (512) => chained descriptors */
506 : : #define BIG_SEG_LEN 1000 /* each mbuf segment carries this many bytes */
507 : : /* ~8 descriptors per packet; enough to wrap the 2^MEMIF_LOG2_RING ring */
508 : : #define WRAP_PKTS ((1 << MEMIF_LOG2_RING) / 8 + 8)
509 : :
510 : : /*
511 : : * Build a multi-segment mbuf of pkt_len bytes, chaining as many pool mbufs
512 : : * as needed (BIG_SEG_LEN bytes each) with a position-dependent pattern.
513 : : */
514 : : static struct rte_mbuf *
515 : 0 : create_big_packet(uint16_t pkt_len, uint8_t seed)
516 : : {
517 : : struct rte_mbuf *head = NULL, *tail = NULL;
518 : : uint16_t done = 0;
519 : :
520 [ # # ]: 0 : while (done < pkt_len) {
521 : 0 : struct rte_mbuf *seg = rte_pktmbuf_alloc(mp);
522 : 0 : uint16_t seg_len = RTE_MIN((uint16_t)BIG_SEG_LEN,
523 : : (uint16_t)(pkt_len - done));
524 : : uint8_t *data;
525 : : uint16_t i;
526 : :
527 [ # # ]: 0 : if (seg == NULL) {
528 : 0 : rte_pktmbuf_free(head);
529 : 0 : return NULL;
530 : : }
531 : : data = (uint8_t *)rte_pktmbuf_append(seg, seg_len);
532 [ # # ]: 0 : if (data == NULL) {
533 : 0 : rte_pktmbuf_free(seg);
534 : 0 : rte_pktmbuf_free(head);
535 : 0 : return NULL;
536 : : }
537 [ # # ]: 0 : for (i = 0; i < seg_len; i++)
538 : 0 : data[i] = (uint8_t)(seed + done + i);
539 : :
540 [ # # ]: 0 : if (head == NULL) {
541 : : head = seg;
542 : : tail = seg;
543 : : } else {
544 : 0 : tail->next = seg;
545 : : tail = seg;
546 : 0 : head->nb_segs++;
547 : 0 : head->pkt_len += seg_len;
548 : : }
549 : 0 : done += seg_len;
550 : : }
551 : : return head;
552 : : }
553 : :
554 : : /* Verify a received packet's payload, walking its segment chain. */
555 : : static int
556 : 0 : check_big_packet(struct rte_mbuf *m, uint16_t pkt_len, uint8_t seed)
557 : : {
558 : : uint16_t off = 0;
559 : : struct rte_mbuf *seg;
560 : :
561 [ # # ]: 0 : if (rte_pktmbuf_pkt_len(m) != pkt_len)
562 : : return -1;
563 [ # # ]: 0 : for (seg = m; seg != NULL; seg = seg->next) {
564 : 0 : const uint8_t *data = rte_pktmbuf_mtod(seg, const uint8_t *);
565 : : uint16_t i;
566 : :
567 [ # # ]: 0 : for (i = 0; i < rte_pktmbuf_data_len(seg); i++, off++)
568 [ # # ]: 0 : if (data[i] != (uint8_t)(seed + off))
569 : : return -1;
570 : : }
571 [ # # ]: 0 : return off == pkt_len ? 0 : -1;
572 : : }
573 : :
574 : : /*
575 : : * Send multi-segment packets larger than the memif buffer size, repeated
576 : : * enough to wrap the ring, and verify each is delivered intact. Exercises
577 : : * the chained-descriptor Rx and Tx paths across a ring wrap.
578 : : */
579 : : static int
580 : 0 : test_memif_multiseg(void)
581 : : {
582 : : unsigned int sent = 0, recv = 0;
583 : : unsigned int iter;
584 : :
585 [ # # ]: 0 : TEST_ASSERT(wait_for_link() == 0, "link not up before multiseg test");
586 : :
587 [ # # ]: 0 : for (iter = 0; iter < WRAP_PKTS; iter++) {
588 : : struct rte_mbuf *tx, *rx[MAX_PKT_BURST];
589 : : uint16_t n, j;
590 : : int tries;
591 : :
592 : 0 : tx = create_big_packet(BIG_PKT_LEN, (uint8_t)iter);
593 [ # # ]: 0 : TEST_ASSERT(tx != NULL, "failed to build big packet %u", iter);
594 [ # # ]: 0 : if (tx->nb_segs <= 1) {
595 : 0 : rte_pktmbuf_free(tx);
596 : 0 : TEST_ASSERT(0, "test packet is not multi-segment");
597 : : }
598 : :
599 [ # # ]: 0 : if (rte_eth_tx_burst(memif_cli_port, 0, &tx, 1) != 1) {
600 : 0 : rte_pktmbuf_free(tx);
601 : 0 : continue;
602 : : }
603 : 0 : sent++;
604 : :
605 [ # # ]: 0 : for (tries = 0; tries < 10; tries++) {
606 : 0 : n = rte_eth_rx_burst(memif_srv_port, 0, rx,
607 : : MAX_PKT_BURST);
608 [ # # ]: 0 : for (j = 0; j < n; j++) {
609 [ # # ]: 0 : if (check_big_packet(rx[j], BIG_PKT_LEN,
610 : : (uint8_t)recv) != 0) {
611 : : uint16_t k;
612 [ # # ]: 0 : for (k = j; k < n; k++)
613 : 0 : rte_pktmbuf_free(rx[k]);
614 : 0 : TEST_ASSERT(0,
615 : : "multi-seg payload corrupted at pkt %u",
616 : : recv);
617 : : }
618 : 0 : rte_pktmbuf_free(rx[j]);
619 : 0 : recv++;
620 : : }
621 [ # # ]: 0 : if (recv >= sent)
622 : : break;
623 : 0 : usleep(2000);
624 : : }
625 : : }
626 : :
627 [ # # ]: 0 : TEST_ASSERT(recv == sent,
628 : : "multi-seg: sent %u received %u", sent, recv);
629 : : return TEST_SUCCESS;
630 : : }
631 : :
632 : : static void
633 : 0 : test_memif_cleanup(void)
634 : : {
635 [ # # ]: 0 : if (memif_srv_port >= 0) {
636 : 0 : rte_eth_dev_stop(memif_srv_port);
637 : 0 : rte_eth_dev_close(memif_srv_port);
638 : : }
639 [ # # ]: 0 : if (memif_cli_port >= 0) {
640 : 0 : rte_eth_dev_stop(memif_cli_port);
641 : 0 : rte_eth_dev_close(memif_cli_port);
642 : : }
643 [ # # ]: 0 : if (memif_fp_srv_port >= 0) {
644 : 0 : rte_eth_dev_stop(memif_fp_srv_port);
645 : 0 : rte_eth_dev_close(memif_fp_srv_port);
646 : : }
647 [ # # ]: 0 : if (memif_fp_cli_port >= 0) {
648 : 0 : rte_eth_dev_stop(memif_fp_cli_port);
649 : 0 : rte_eth_dev_close(memif_fp_cli_port);
650 : : }
651 : 0 : rte_vdev_uninit(MEMIF_SRV_NAME);
652 : 0 : rte_vdev_uninit(MEMIF_CLI_NAME);
653 : 0 : rte_vdev_uninit(MEMIF_FP_SRV_NAME);
654 : 0 : rte_vdev_uninit(MEMIF_FP_CLI_NAME);
655 [ # # ]: 0 : if (mp != NULL) {
656 : 0 : rte_mempool_free(mp);
657 : 0 : mp = NULL;
658 : : }
659 : 0 : memif_srv_port = -1;
660 : 0 : memif_cli_port = -1;
661 : 0 : memif_fp_srv_port = -1;
662 : 0 : memif_fp_cli_port = -1;
663 : 0 : }
664 : :
665 : : static int
666 : 0 : test_memif_setup(void)
667 : : {
668 : : int ret;
669 : :
670 : 0 : mp = rte_pktmbuf_pool_create("memif_test_pool", NB_MBUF, 32, 0,
671 : 0 : RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
672 [ # # ]: 0 : if (mp == NULL) {
673 : 0 : printf("Error: failed to create mempool: %s\n",
674 : : rte_strerror(rte_errno));
675 : 0 : return -1;
676 : : }
677 : :
678 : 0 : ret = rte_vdev_init(MEMIF_SRV_NAME, MEMIF_SRV_ARGS);
679 [ # # ]: 0 : if (ret < 0) {
680 : 0 : printf("Error: failed to create memif server: %s\n",
681 : : rte_strerror(-ret));
682 : 0 : rte_mempool_free(mp);
683 : 0 : mp = NULL;
684 : 0 : return TEST_FAILED;
685 : : }
686 : :
687 : 0 : ret = rte_vdev_init(MEMIF_CLI_NAME, MEMIF_CLI_ARGS);
688 [ # # ]: 0 : if (ret < 0) {
689 : 0 : printf("Error: failed to create memif client: %s\n",
690 : : rte_strerror(-ret));
691 : 0 : rte_vdev_uninit(MEMIF_SRV_NAME);
692 : 0 : rte_mempool_free(mp);
693 : 0 : mp = NULL;
694 : 0 : return -1;
695 : : }
696 : :
697 : 0 : memif_srv_port = find_port(MEMIF_SRV_NAME);
698 : 0 : memif_cli_port = find_port(MEMIF_CLI_NAME);
699 [ # # # # ]: 0 : if (memif_srv_port < 0 || memif_cli_port < 0) {
700 : : printf("Error: failed to find memif ports\n");
701 : 0 : test_memif_cleanup();
702 : 0 : return -1;
703 : : }
704 : :
705 [ # # # # ]: 0 : if (configure_port(memif_srv_port) < 0 ||
706 : 0 : configure_port(memif_cli_port) < 0) {
707 : 0 : test_memif_cleanup();
708 : 0 : return -1;
709 : : }
710 : :
711 : : /* Second pair with default bsize to exercise the Tx fast path. */
712 [ # # # # ]: 0 : if (rte_vdev_init(MEMIF_FP_SRV_NAME, MEMIF_FP_SRV_ARGS) < 0 ||
713 : 0 : rte_vdev_init(MEMIF_FP_CLI_NAME, MEMIF_FP_CLI_ARGS) < 0) {
714 : : printf("Error: failed to create fast-path memif pair\n");
715 : 0 : test_memif_cleanup();
716 : 0 : return TEST_FAILED;
717 : : }
718 : :
719 : 0 : memif_fp_srv_port = find_port(MEMIF_FP_SRV_NAME);
720 : 0 : memif_fp_cli_port = find_port(MEMIF_FP_CLI_NAME);
721 [ # # # # ]: 0 : if (memif_fp_srv_port < 0 || memif_fp_cli_port < 0) {
722 : : printf("Error: failed to find fast-path memif ports\n");
723 : 0 : test_memif_cleanup();
724 : 0 : return TEST_FAILED;
725 : : }
726 : :
727 [ # # # # ]: 0 : if (configure_port(memif_fp_srv_port) < 0 ||
728 : 0 : configure_port(memif_fp_cli_port) < 0) {
729 : 0 : test_memif_cleanup();
730 : 0 : return -1;
731 : : }
732 : :
733 : : return 0;
734 : : }
735 : :
736 : : static struct unit_test_suite test_pmd_memif_suite = {
737 : : .setup = test_memif_setup,
738 : : .teardown = test_memif_cleanup,
739 : : .suite_name = "memif PMD Unit Test Suite",
740 : : .unit_test_cases = {
741 : : TEST_CASE(test_memif_dev_info),
742 : : TEST_CASE(test_memif_link_up),
743 : : TEST_CASE(test_memif_stats_get),
744 : : TEST_CASE(test_memif_send_receive),
745 : : TEST_CASE(test_memif_fastpath),
746 : : TEST_CASE(test_memif_multiseg),
747 : : TEST_CASE(test_memif_stats_reset),
748 : : TEST_CASE(test_memif_stop_start),
749 : : TEST_CASE(test_memif_tx_empty_burst),
750 : : TEST_CASE(test_memif_close_without_stop),
751 : : TEST_CASE(test_memif_stopped_server_rejects_peer),
752 : : TEST_CASES_END()
753 : : }
754 : : };
755 : :
756 : : static int
757 : 0 : test_pmd_memif(void)
758 : : {
759 : 0 : return unit_test_suite_runner(&test_pmd_memif_suite);
760 : : }
761 : :
762 : : #endif /* RTE_EXEC_ENV_LINUX */
763 : :
764 : : /* registering as fast test deferred until driver is fixed. */
765 : 306 : REGISTER_ATTIC_TEST(memif_pmd_autotest, test_pmd_memif);
|