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;
229 memset(&null_conf, 0, sizeof(struct rte_eth_conf));
231 if ((rte_eth_dev_configure(rxtx_portd, 1, 1, &null_conf) < 0)
232 || (rte_eth_dev_configure(rxtx_porte, 1, 1,
234 printf("Configure failed for port\n");
238 if ((rte_eth_tx_queue_setup(rxtx_portd, 0, RING_SIZE,
240 || (rte_eth_tx_queue_setup(rxtx_porte, 0, RING_SIZE,
241 SOCKET0, NULL) < 0)) {
242 printf("TX queue setup failed\n");
246 if ((rte_eth_rx_queue_setup(rxtx_portd, 0, RING_SIZE,
247 SOCKET0, NULL, mp) < 0)
248 || (rte_eth_rx_queue_setup(rxtx_porte, 0, RING_SIZE,
249 SOCKET0, NULL, mp) < 0)) {
250 printf("RX queue setup failed\n");
254 if ((rte_eth_dev_start(rxtx_portd) < 0)
255 || (rte_eth_dev_start(rxtx_porte) < 0)) {
256 printf("Error starting port\n");
260 rte_eth_stats_reset(rxtx_portd);
261 /* check stats of port, should all be zero */
262 rte_eth_stats_get(rxtx_portd, &stats);
263 if (stats.ipackets != 0 || stats.opackets != 0 ||
264 stats.ibytes != 0 || stats.obytes != 0 ||
265 stats.ierrors != 0 || stats.oerrors != 0) {
266 printf("Error: port %d stats are not zero\n", rxtx_portd);
270 rte_eth_stats_reset(rxtx_porte);
271 /* check stats of port, should all be zero */
272 rte_eth_stats_get(rxtx_porte, &stats2);
273 if (stats2.ipackets != 0 || stats2.opackets != 0 ||
274 stats2.ibytes != 0 || stats2.obytes != 0 ||
275 stats2.ierrors != 0 || stats2.oerrors != 0) {
276 printf("Error: port %d stats are not zero\n", rxtx_porte);
281 * send and receive 1 packet (rxtx_portd -> rxtx_porte)
282 * and check for stats update
284 printf("Testing send and receive 1 packet (rxtx_portd -> rxtx_porte)\n");
285 if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
286 printf("Error sending packet to port %d\n", rxtx_portd);
290 if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
291 printf("Error receiving packet from port %d\n", rxtx_porte);
295 rte_eth_stats_get(rxtx_portd, &stats);
296 rte_eth_stats_get(rxtx_porte, &stats2);
297 if (stats.ipackets != 0 || stats.opackets != 1 ||
298 stats.ibytes != 0 || stats.obytes != 0 ||
299 stats.ierrors != 0 || stats.oerrors != 0) {
300 printf("Error: port %d stats are not as expected\n",
305 if (stats2.ipackets != 1 || stats2.opackets != 0 ||
306 stats2.ibytes != 0 || stats2.obytes != 0 ||
307 stats2.ierrors != 0 || stats2.oerrors != 0) {
308 printf("Error: port %d stats are not as expected\n",
314 * send and receive 1 packet (rxtx_porte -> rxtx_portd)
315 * and check for stats update
317 printf("Testing send and receive 1 packet "
318 "(rxtx_porte -> rxtx_portd)\n");
319 if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
320 printf("Error sending packet to port %d\n", rxtx_porte);
324 if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
325 printf("Error receiving packet from port %d\n", rxtx_portd);
329 rte_eth_stats_get(rxtx_portd, &stats);
330 rte_eth_stats_get(rxtx_porte, &stats2);
331 if (stats.ipackets != 1 || stats.opackets != 1 ||
332 stats.ibytes != 0 || stats.obytes != 0 ||
333 stats.ierrors != 0 || stats.oerrors != 0) {
334 printf("Error: port %d stats are not as expected\n",
339 if (stats2.ipackets != 1 || stats2.opackets != 1 ||
340 stats2.ibytes != 0 || stats2.obytes != 0 ||
341 stats2.ierrors != 0 || stats2.oerrors != 0) {
342 printf("Error: port %d stats are not as expected\n",
348 * send and receive 1 packet (rxtx_portd -> rxtx_portd)
349 * and check for stats update
351 printf("Testing send and receive 1 packet "
352 "(rxtx_portd -> rxtx_portd)\n");
353 if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
354 printf("Error sending packet to port %d\n", rxtx_portd);
358 if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
359 printf("Error receiving packet from port %d\n", rxtx_porte);
363 rte_eth_stats_get(rxtx_portd, &stats);
364 rte_eth_stats_get(rxtx_porte, &stats2);
365 if (stats.ipackets != 2 || stats.opackets != 2 ||
366 stats.ibytes != 0 || stats.obytes != 0 ||
367 stats.ierrors != 0 || stats.oerrors != 0) {
368 printf("Error: port %d stats are not as expected\n",
373 if (stats2.ipackets != 1 || stats2.opackets != 1 ||
374 stats2.ibytes != 0 || stats2.obytes != 0 ||
375 stats2.ierrors != 0 || stats2.oerrors != 0) {
376 printf("Error: port %d stats are not as expected\n",
382 * send and receive 1 packet (rxtx_porte -> rxtx_porte)
383 * and check for stats update
385 printf("Testing send and receive 1 packet "
386 "(rxtx_porte -> rxtx_porte)\n");
387 if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
388 printf("Error sending packet to port %d\n", rxtx_porte);
392 if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
393 printf("Error receiving packet from port %d\n", rxtx_porte);
397 rte_eth_stats_get(rxtx_portd, &stats);
398 rte_eth_stats_get(rxtx_porte, &stats2);
399 if (stats.ipackets != 2 || stats.opackets != 2 ||
400 stats.ibytes != 0 || stats.obytes != 0 ||
401 stats.ierrors != 0 || stats.oerrors != 0) {
402 printf("Error: port %d stats are not as expected\n",
407 if (stats2.ipackets != 2 || stats2.opackets != 2 ||
408 stats2.ibytes != 0 || stats2.obytes != 0 ||
409 stats2.ierrors != 0 || stats2.oerrors != 0) {
410 printf("Error: port %d stats are not as expected\n",
415 rte_eth_dev_stop(rxtx_portd);
416 rte_eth_dev_stop(rxtx_porte);
422 test_cleanup_resources(void)
425 for (itr = 0; itr < NUM_RINGS; itr++)
426 rte_ring_free(rxtx[itr]);
428 rte_eth_dev_stop(tx_porta);
429 rte_eth_dev_stop(rx_portb);
430 rte_eth_dev_stop(rxtx_portc);
432 rte_mempool_free(mp);
433 rte_vdev_uninit("net_ring_net_ringa");
434 rte_vdev_uninit("net_ring_net_ringb");
435 rte_vdev_uninit("net_ring_net_ringc");
436 rte_vdev_uninit("net_ring_net_ringd");
437 rte_vdev_uninit("net_ring_net_ringe");
441 test_pmd_ringcreate_setup(void)
445 nb_ports = rte_eth_dev_count_avail();
446 printf("nb_ports=%d\n", (int)nb_ports);
448 /* create the rings and eth_rings in the test code.
449 * This does not test the rte_pmd_ring_devinit function.
451 * Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
453 rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
454 if (rxtx[0] == NULL) {
455 printf("rte_ring_create R0 failed");
459 rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
460 if (rxtx[1] == NULL) {
461 printf("rte_ring_create R1 failed");
465 tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
466 rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
467 rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
468 rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
469 rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
471 printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
472 tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
474 if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
475 || (rxtx_portd == -1) || (rxtx_porte == -1)) {
476 printf("rte_eth_from rings failed\n");
480 mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
481 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
485 if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
486 || (rxtx_portc >= RTE_MAX_ETHPORTS)
487 || (rxtx_portd >= RTE_MAX_ETHPORTS)
488 || (rxtx_porte >= RTE_MAX_ETHPORTS)) {
489 printf(" port exceed max eth ports\n");
496 test_command_line_ring_port(void)
498 int port, cmdl_port0 = -1;
501 /* find a port created with the --vdev=net_ring0 command line option */
502 RTE_ETH_FOREACH_DEV(port) {
503 struct rte_eth_dev_info dev_info;
505 ret = rte_eth_dev_info_get(port, &dev_info);
506 TEST_ASSERT((ret == 0),
507 "Error during getting device (port %d) info: %s\n",
508 port, strerror(-ret));
510 if (!strcmp(dev_info.driver_name, "Rings PMD")) {
511 printf("found a command line ring port=%d\n", port);
516 if (cmdl_port0 != -1) {
517 TEST_ASSERT((test_ethdev_configure_port(cmdl_port0) < 0),
518 "test ethdev configure port cmdl_port0 is failed");
519 TEST_ASSERT((test_send_basic_packets_port(cmdl_port0) < 0),
520 "test send basic packets port cmdl_port0 is failed");
521 TEST_ASSERT((test_stats_reset(cmdl_port0) < 0),
522 "test stats reset cmdl_port0 is failed");
523 TEST_ASSERT((test_get_stats(cmdl_port0) < 0),
524 "test get stats cmdl_port0 is failed");
525 rte_eth_dev_stop(cmdl_port0);
531 test_ethdev_configure_ports(void)
533 TEST_ASSERT((test_ethdev_configure_port(tx_porta) == 0),
534 "test ethdev configure ports tx_porta is failed");
535 TEST_ASSERT((test_ethdev_configure_port(rx_portb) == 0),
536 "test ethdev configure ports rx_portb is failed");
537 TEST_ASSERT((test_ethdev_configure_port(rxtx_portc) == 0),
538 "test ethdev configure ports rxtx_portc is failed");
544 test_get_stats_for_port(void)
546 TEST_ASSERT(test_get_stats(rxtx_portc) == 0, "test get stats failed");
551 test_stats_reset_for_port(void)
553 TEST_ASSERT(test_stats_reset(rxtx_portc) == 0, "test stats reset failed");
558 unit_test_suite test_pmd_ring_suite = {
559 .setup = test_pmd_ringcreate_setup,
560 .teardown = test_cleanup_resources,
561 .suite_name = "Test Pmd Ring Unit Test Suite",
563 TEST_CASE(test_ethdev_configure_ports),
564 TEST_CASE(test_send_basic_packets),
565 TEST_CASE(test_get_stats_for_port),
566 TEST_CASE(test_stats_reset_for_port),
567 TEST_CASE(test_pmd_ring_pair_create_attach),
568 TEST_CASE(test_command_line_ring_port),
576 return unit_test_suite_runner(&test_pmd_ring_suite);
579 REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);