1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
9 #include <rte_eth_ring.h>
10 #include <rte_ethdev.h>
11 #include <rte_bus_vdev.h>
18 static struct rte_mempool *mp;
19 struct rte_ring *rxtx[NUM_RINGS];
20 static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;
23 test_ethdev_configure_port(int port)
25 struct rte_eth_conf null_conf;
26 struct rte_eth_link link;
29 memset(&null_conf, 0, sizeof(struct rte_eth_conf));
31 if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
32 printf("Configure failed for port %d\n", port);
36 /* Test queue release */
37 if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
38 printf("Configure failed for port %d\n", port);
42 if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
43 printf("TX queue setup failed port %d\n", port);
47 if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
49 printf("RX queue setup failed port %d\n", port);
53 if (rte_eth_dev_start(port) < 0) {
54 printf("Error starting port %d\n", port);
58 ret = rte_eth_link_get(port, &link);
60 printf("Link get failed for port %u: %s",
61 port, rte_strerror(-ret));
69 test_send_basic_packets(void)
71 struct rte_mbuf bufs[RING_SIZE];
72 struct rte_mbuf *pbufs[RING_SIZE];
75 printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");
77 for (i = 0; i < RING_SIZE/2; i++)
80 if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
81 printf("Failed to transmit packet burst port %d\n", tx_porta);
85 if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
86 printf("Failed to receive packet burst on port %d\n", rx_portb);
90 for (i = 0; i < RING_SIZE/2; i++)
91 if (pbufs[i] != &bufs[i]) {
92 printf("Error: received data does not match that transmitted\n");
100 test_send_basic_packets_port(int port)
102 struct rte_mbuf bufs[RING_SIZE];
103 struct rte_mbuf *pbufs[RING_SIZE];
106 printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");
108 for (i = 0; i < RING_SIZE/2; i++)
111 if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
112 printf("Failed to transmit packet burst port %d\n", port);
116 if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
117 printf("Failed to receive packet burst on port %d\n", port);
121 for (i = 0; i < RING_SIZE/2; i++)
122 if (pbufs[i] != &bufs[i]) {
123 printf("Error: received data does not match that transmitted\n");
132 test_get_stats(int port)
134 struct rte_eth_stats stats;
135 struct rte_mbuf buf, *pbuf = &buf;
137 printf("Testing ring PMD stats_get port %d\n", port);
139 /* check stats of RXTX port, should all be zero */
141 rte_eth_stats_get(port, &stats);
142 if (stats.ipackets != 0 || stats.opackets != 0 ||
143 stats.ibytes != 0 || stats.obytes != 0 ||
144 stats.ierrors != 0 || stats.oerrors != 0) {
145 printf("Error: port %d stats are not zero\n", port);
149 /* send and receive 1 packet and check for stats update */
150 if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
151 printf("Error sending packet to port %d\n", port);
155 if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
156 printf("Error receiving packet from port %d\n", port);
160 rte_eth_stats_get(port, &stats);
161 if (stats.ipackets != 1 || stats.opackets != 1 ||
162 stats.ibytes != 0 || stats.obytes != 0 ||
163 stats.ierrors != 0 || stats.oerrors != 0) {
164 printf("Error: port %d stats are not as expected\n", port);
171 test_stats_reset(int port)
173 struct rte_eth_stats stats;
174 struct rte_mbuf buf, *pbuf = &buf;
176 printf("Testing ring PMD stats_reset port %d\n", port);
178 rte_eth_stats_reset(port);
180 /* check stats of RXTX port, should all be zero */
181 rte_eth_stats_get(port, &stats);
182 if (stats.ipackets != 0 || stats.opackets != 0 ||
183 stats.ibytes != 0 || stats.obytes != 0 ||
184 stats.ierrors != 0 || stats.oerrors != 0) {
185 printf("Error: port %d stats are not zero\n", port);
189 /* send and receive 1 packet and check for stats update */
190 if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
191 printf("Error sending packet to port %d\n", port);
195 if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
196 printf("Error receiving packet from port %d\n", port);
200 rte_eth_stats_get(port, &stats);
201 if (stats.ipackets != 1 || stats.opackets != 1 ||
202 stats.ibytes != 0 || stats.obytes != 0 ||
203 stats.ierrors != 0 || stats.oerrors != 0) {
204 printf("Error: port %d stats are not as expected\n", port);
208 rte_eth_stats_reset(port);
210 /* check stats of RXTX port, should all be zero */
211 rte_eth_stats_get(port, &stats);
212 if (stats.ipackets != 0 || stats.opackets != 0 ||
213 stats.ibytes != 0 || stats.obytes != 0 ||
214 stats.ierrors != 0 || stats.oerrors != 0) {
215 printf("Error: port %d stats are not zero\n", port);
223 test_pmd_ring_pair_create_attach(void)
225 struct rte_eth_stats stats, stats2;
226 struct rte_mbuf buf, *pbuf = &buf;
227 struct rte_eth_conf null_conf;
230 memset(&null_conf, 0, sizeof(struct rte_eth_conf));
232 if ((rte_eth_dev_configure(rxtx_portd, 1, 1, &null_conf) < 0)
233 || (rte_eth_dev_configure(rxtx_porte, 1, 1,
235 printf("Configure failed for port\n");
239 if ((rte_eth_tx_queue_setup(rxtx_portd, 0, RING_SIZE,
241 || (rte_eth_tx_queue_setup(rxtx_porte, 0, RING_SIZE,
242 SOCKET0, NULL) < 0)) {
243 printf("TX queue setup failed\n");
247 if ((rte_eth_rx_queue_setup(rxtx_portd, 0, RING_SIZE,
248 SOCKET0, NULL, mp) < 0)
249 || (rte_eth_rx_queue_setup(rxtx_porte, 0, RING_SIZE,
250 SOCKET0, NULL, mp) < 0)) {
251 printf("RX queue setup failed\n");
255 if ((rte_eth_dev_start(rxtx_portd) < 0)
256 || (rte_eth_dev_start(rxtx_porte) < 0)) {
257 printf("Error starting port\n");
261 rte_eth_stats_reset(rxtx_portd);
262 /* check stats of port, should all be zero */
263 rte_eth_stats_get(rxtx_portd, &stats);
264 if (stats.ipackets != 0 || stats.opackets != 0 ||
265 stats.ibytes != 0 || stats.obytes != 0 ||
266 stats.ierrors != 0 || stats.oerrors != 0) {
267 printf("Error: port %d stats are not zero\n", rxtx_portd);
271 rte_eth_stats_reset(rxtx_porte);
272 /* check stats of port, should all be zero */
273 rte_eth_stats_get(rxtx_porte, &stats2);
274 if (stats2.ipackets != 0 || stats2.opackets != 0 ||
275 stats2.ibytes != 0 || stats2.obytes != 0 ||
276 stats2.ierrors != 0 || stats2.oerrors != 0) {
277 printf("Error: port %d stats are not zero\n", rxtx_porte);
282 * send and receive 1 packet (rxtx_portd -> rxtx_porte)
283 * and check for stats update
285 printf("Testing send and receive 1 packet (rxtx_portd -> rxtx_porte)\n");
286 if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
287 printf("Error sending packet to port %d\n", rxtx_portd);
291 if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
292 printf("Error receiving packet from port %d\n", rxtx_porte);
296 rte_eth_stats_get(rxtx_portd, &stats);
297 rte_eth_stats_get(rxtx_porte, &stats2);
298 if (stats.ipackets != 0 || stats.opackets != 1 ||
299 stats.ibytes != 0 || stats.obytes != 0 ||
300 stats.ierrors != 0 || stats.oerrors != 0) {
301 printf("Error: port %d stats are not as expected\n",
306 if (stats2.ipackets != 1 || stats2.opackets != 0 ||
307 stats2.ibytes != 0 || stats2.obytes != 0 ||
308 stats2.ierrors != 0 || stats2.oerrors != 0) {
309 printf("Error: port %d stats are not as expected\n",
315 * send and receive 1 packet (rxtx_porte -> rxtx_portd)
316 * and check for stats update
318 printf("Testing send and receive 1 packet "
319 "(rxtx_porte -> rxtx_portd)\n");
320 if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
321 printf("Error sending packet to port %d\n", rxtx_porte);
325 if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
326 printf("Error receiving packet from port %d\n", rxtx_portd);
330 rte_eth_stats_get(rxtx_portd, &stats);
331 rte_eth_stats_get(rxtx_porte, &stats2);
332 if (stats.ipackets != 1 || stats.opackets != 1 ||
333 stats.ibytes != 0 || stats.obytes != 0 ||
334 stats.ierrors != 0 || stats.oerrors != 0) {
335 printf("Error: port %d stats are not as expected\n",
340 if (stats2.ipackets != 1 || stats2.opackets != 1 ||
341 stats2.ibytes != 0 || stats2.obytes != 0 ||
342 stats2.ierrors != 0 || stats2.oerrors != 0) {
343 printf("Error: port %d stats are not as expected\n",
349 * send and receive 1 packet (rxtx_portd -> rxtx_portd)
350 * and check for stats update
352 printf("Testing send and receive 1 packet "
353 "(rxtx_portd -> rxtx_portd)\n");
354 if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
355 printf("Error sending packet to port %d\n", rxtx_portd);
359 if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
360 printf("Error receiving packet from port %d\n", rxtx_porte);
364 rte_eth_stats_get(rxtx_portd, &stats);
365 rte_eth_stats_get(rxtx_porte, &stats2);
366 if (stats.ipackets != 2 || stats.opackets != 2 ||
367 stats.ibytes != 0 || stats.obytes != 0 ||
368 stats.ierrors != 0 || stats.oerrors != 0) {
369 printf("Error: port %d stats are not as expected\n",
374 if (stats2.ipackets != 1 || stats2.opackets != 1 ||
375 stats2.ibytes != 0 || stats2.obytes != 0 ||
376 stats2.ierrors != 0 || stats2.oerrors != 0) {
377 printf("Error: port %d stats are not as expected\n",
383 * send and receive 1 packet (rxtx_porte -> rxtx_porte)
384 * and check for stats update
386 printf("Testing send and receive 1 packet "
387 "(rxtx_porte -> rxtx_porte)\n");
388 if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
389 printf("Error sending packet to port %d\n", rxtx_porte);
393 if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
394 printf("Error receiving packet from port %d\n", rxtx_porte);
398 rte_eth_stats_get(rxtx_portd, &stats);
399 rte_eth_stats_get(rxtx_porte, &stats2);
400 if (stats.ipackets != 2 || stats.opackets != 2 ||
401 stats.ibytes != 0 || stats.obytes != 0 ||
402 stats.ierrors != 0 || stats.oerrors != 0) {
403 printf("Error: port %d stats are not as expected\n",
408 if (stats2.ipackets != 2 || stats2.opackets != 2 ||
409 stats2.ibytes != 0 || stats2.obytes != 0 ||
410 stats2.ierrors != 0 || stats2.oerrors != 0) {
411 printf("Error: port %d stats are not as expected\n",
416 ret = rte_eth_dev_stop(rxtx_portd);
418 printf("Error: failed to stop port %u: %s\n",
419 rxtx_portd, rte_strerror(-ret));
420 ret = rte_eth_dev_stop(rxtx_porte);
422 printf("Error: failed to stop port %u: %s\n",
423 rxtx_porte, rte_strerror(-ret));
429 test_cleanup_resources(void)
432 for (itr = 0; itr < NUM_RINGS; itr++)
433 rte_ring_free(rxtx[itr]);
435 ret = rte_eth_dev_stop(tx_porta);
437 printf("Error: failed to stop port %u: %s\n",
438 tx_porta, rte_strerror(-ret));
439 ret = rte_eth_dev_stop(rx_portb);
441 printf("Error: failed to stop port %u: %s\n",
442 rx_portb, rte_strerror(-ret));
443 ret = rte_eth_dev_stop(rxtx_portc);
445 printf("Error: failed to stop port %u: %s\n",
446 rxtx_portc, rte_strerror(-ret));
448 rte_mempool_free(mp);
449 rte_vdev_uninit("net_ring_net_ringa");
450 rte_vdev_uninit("net_ring_net_ringb");
451 rte_vdev_uninit("net_ring_net_ringc");
452 rte_vdev_uninit("net_ring_net_ringd");
453 rte_vdev_uninit("net_ring_net_ringe");
457 test_pmd_ringcreate_setup(void)
461 nb_ports = rte_eth_dev_count_avail();
462 printf("nb_ports=%d\n", (int)nb_ports);
464 /* create the rings and eth_rings in the test code.
465 * This does not test the rte_pmd_ring_devinit function.
467 * Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
469 rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
470 if (rxtx[0] == NULL) {
471 printf("rte_ring_create R0 failed");
475 rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
476 if (rxtx[1] == NULL) {
477 printf("rte_ring_create R1 failed");
481 tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
482 rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
483 rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
484 rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
485 rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
487 printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
488 tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
490 if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
491 || (rxtx_portd == -1) || (rxtx_porte == -1)) {
492 printf("rte_eth_from rings failed\n");
496 mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
497 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
501 if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
502 || (rxtx_portc >= RTE_MAX_ETHPORTS)
503 || (rxtx_portd >= RTE_MAX_ETHPORTS)
504 || (rxtx_porte >= RTE_MAX_ETHPORTS)) {
505 printf(" port exceed max eth ports\n");
512 test_command_line_ring_port(void)
514 int port, cmdl_port0 = -1;
517 /* find a port created with the --vdev=net_ring0 command line option */
518 RTE_ETH_FOREACH_DEV(port) {
519 struct rte_eth_dev_info dev_info;
521 ret = rte_eth_dev_info_get(port, &dev_info);
522 TEST_ASSERT((ret == 0),
523 "Error during getting device (port %d) info: %s\n",
524 port, strerror(-ret));
526 if (!strcmp(dev_info.driver_name, "Rings PMD")) {
527 printf("found a command line ring port=%d\n", port);
532 if (cmdl_port0 != -1) {
533 TEST_ASSERT((test_ethdev_configure_port(cmdl_port0) < 0),
534 "test ethdev configure port cmdl_port0 is failed");
535 TEST_ASSERT((test_send_basic_packets_port(cmdl_port0) < 0),
536 "test send basic packets port cmdl_port0 is failed");
537 TEST_ASSERT((test_stats_reset(cmdl_port0) < 0),
538 "test stats reset cmdl_port0 is failed");
539 TEST_ASSERT((test_get_stats(cmdl_port0) < 0),
540 "test get stats cmdl_port0 is failed");
541 TEST_ASSERT((rte_eth_dev_stop(cmdl_port0) == 0),
542 "test stop cmdl_port0 is failed");
548 test_ethdev_configure_ports(void)
550 TEST_ASSERT((test_ethdev_configure_port(tx_porta) == 0),
551 "test ethdev configure ports tx_porta is failed");
552 TEST_ASSERT((test_ethdev_configure_port(rx_portb) == 0),
553 "test ethdev configure ports rx_portb is failed");
554 TEST_ASSERT((test_ethdev_configure_port(rxtx_portc) == 0),
555 "test ethdev configure ports rxtx_portc is failed");
561 test_get_stats_for_port(void)
563 TEST_ASSERT(test_get_stats(rxtx_portc) == 0, "test get stats failed");
568 test_stats_reset_for_port(void)
570 TEST_ASSERT(test_stats_reset(rxtx_portc) == 0, "test stats reset failed");
575 unit_test_suite test_pmd_ring_suite = {
576 .setup = test_pmd_ringcreate_setup,
577 .teardown = test_cleanup_resources,
578 .suite_name = "Test Pmd Ring Unit Test Suite",
580 TEST_CASE(test_ethdev_configure_ports),
581 TEST_CASE(test_send_basic_packets),
582 TEST_CASE(test_get_stats_for_port),
583 TEST_CASE(test_stats_reset_for_port),
584 TEST_CASE(test_pmd_ring_pair_create_attach),
585 TEST_CASE(test_command_line_ring_port),
593 return unit_test_suite_runner(&test_pmd_ring_suite);
596 REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);