xref: /f-stack/dpdk/app/test/test_pmd_ring.c (revision 2d9fd380)
14418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
24418919fSjohnjiang  * Copyright(c) 2010-2015 Intel Corporation
34418919fSjohnjiang  */
44418919fSjohnjiang #include "test.h"
54418919fSjohnjiang #include <string.h>
64418919fSjohnjiang 
74418919fSjohnjiang #include <stdio.h>
84418919fSjohnjiang 
94418919fSjohnjiang #include <rte_eth_ring.h>
104418919fSjohnjiang #include <rte_ethdev.h>
114418919fSjohnjiang #include <rte_bus_vdev.h>
124418919fSjohnjiang 
134418919fSjohnjiang #define SOCKET0 0
144418919fSjohnjiang #define RING_SIZE 256
154418919fSjohnjiang #define NUM_RINGS 2
164418919fSjohnjiang #define NB_MBUF 512
174418919fSjohnjiang 
184418919fSjohnjiang static struct rte_mempool *mp;
194418919fSjohnjiang struct rte_ring *rxtx[NUM_RINGS];
204418919fSjohnjiang static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;
214418919fSjohnjiang 
224418919fSjohnjiang static int
test_ethdev_configure_port(int port)234418919fSjohnjiang test_ethdev_configure_port(int port)
244418919fSjohnjiang {
254418919fSjohnjiang 	struct rte_eth_conf null_conf;
264418919fSjohnjiang 	struct rte_eth_link link;
274418919fSjohnjiang 	int ret;
284418919fSjohnjiang 
294418919fSjohnjiang 	memset(&null_conf, 0, sizeof(struct rte_eth_conf));
304418919fSjohnjiang 
314418919fSjohnjiang 	if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
324418919fSjohnjiang 		printf("Configure failed for port %d\n", port);
334418919fSjohnjiang 		return -1;
344418919fSjohnjiang 	}
354418919fSjohnjiang 
364418919fSjohnjiang 	/* Test queue release */
374418919fSjohnjiang 	if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
384418919fSjohnjiang 		printf("Configure failed for port %d\n", port);
394418919fSjohnjiang 		return -1;
404418919fSjohnjiang 	}
414418919fSjohnjiang 
424418919fSjohnjiang 	if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
434418919fSjohnjiang 		printf("TX queue setup failed port %d\n", port);
444418919fSjohnjiang 		return -1;
454418919fSjohnjiang 	}
464418919fSjohnjiang 
474418919fSjohnjiang 	if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
484418919fSjohnjiang 				NULL, mp) < 0) {
494418919fSjohnjiang 		printf("RX queue setup failed port %d\n", port);
504418919fSjohnjiang 		return -1;
514418919fSjohnjiang 	}
524418919fSjohnjiang 
534418919fSjohnjiang 	if (rte_eth_dev_start(port) < 0) {
544418919fSjohnjiang 		printf("Error starting port %d\n", port);
554418919fSjohnjiang 		return -1;
564418919fSjohnjiang 	}
574418919fSjohnjiang 
584418919fSjohnjiang 	ret = rte_eth_link_get(port, &link);
594418919fSjohnjiang 	if (ret < 0) {
604418919fSjohnjiang 		printf("Link get failed for port %u: %s",
614418919fSjohnjiang 		       port, rte_strerror(-ret));
624418919fSjohnjiang 		return -1;
634418919fSjohnjiang 	}
644418919fSjohnjiang 
654418919fSjohnjiang 	return 0;
664418919fSjohnjiang }
674418919fSjohnjiang 
684418919fSjohnjiang static int
test_send_basic_packets(void)694418919fSjohnjiang test_send_basic_packets(void)
704418919fSjohnjiang {
714418919fSjohnjiang 	struct rte_mbuf  bufs[RING_SIZE];
724418919fSjohnjiang 	struct rte_mbuf *pbufs[RING_SIZE];
734418919fSjohnjiang 	int i;
744418919fSjohnjiang 
754418919fSjohnjiang 	printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");
764418919fSjohnjiang 
774418919fSjohnjiang 	for (i = 0; i < RING_SIZE/2; i++)
784418919fSjohnjiang 		pbufs[i] = &bufs[i];
794418919fSjohnjiang 
804418919fSjohnjiang 	if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
814418919fSjohnjiang 		printf("Failed to transmit packet burst port %d\n", tx_porta);
824418919fSjohnjiang 		return TEST_FAILED;
834418919fSjohnjiang 	}
844418919fSjohnjiang 
854418919fSjohnjiang 	if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
864418919fSjohnjiang 		printf("Failed to receive packet burst on port %d\n", rx_portb);
874418919fSjohnjiang 		return TEST_FAILED;
884418919fSjohnjiang 	}
894418919fSjohnjiang 
904418919fSjohnjiang 	for (i = 0; i < RING_SIZE/2; i++)
914418919fSjohnjiang 		if (pbufs[i] != &bufs[i]) {
924418919fSjohnjiang 			printf("Error: received data does not match that transmitted\n");
934418919fSjohnjiang 			return TEST_FAILED;
944418919fSjohnjiang 		}
954418919fSjohnjiang 
964418919fSjohnjiang 	return TEST_SUCCESS;
974418919fSjohnjiang }
984418919fSjohnjiang 
994418919fSjohnjiang static int
test_send_basic_packets_port(int port)1004418919fSjohnjiang test_send_basic_packets_port(int port)
1014418919fSjohnjiang {
1024418919fSjohnjiang 	struct rte_mbuf  bufs[RING_SIZE];
1034418919fSjohnjiang 	struct rte_mbuf *pbufs[RING_SIZE];
1044418919fSjohnjiang 	int i;
1054418919fSjohnjiang 
1064418919fSjohnjiang 	printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");
1074418919fSjohnjiang 
1084418919fSjohnjiang 	for (i = 0; i < RING_SIZE/2; i++)
1094418919fSjohnjiang 		pbufs[i] = &bufs[i];
1104418919fSjohnjiang 
1114418919fSjohnjiang 	if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
1124418919fSjohnjiang 		printf("Failed to transmit packet burst port %d\n", port);
1134418919fSjohnjiang 		return -1;
1144418919fSjohnjiang 	}
1154418919fSjohnjiang 
1164418919fSjohnjiang 	if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
1174418919fSjohnjiang 		printf("Failed to receive packet burst on port %d\n", port);
1184418919fSjohnjiang 		return -1;
1194418919fSjohnjiang 	}
1204418919fSjohnjiang 
1214418919fSjohnjiang 	for (i = 0; i < RING_SIZE/2; i++)
1224418919fSjohnjiang 		if (pbufs[i] != &bufs[i]) {
1234418919fSjohnjiang 			printf("Error: received data does not match that transmitted\n");
1244418919fSjohnjiang 			return -1;
1254418919fSjohnjiang 		}
1264418919fSjohnjiang 
1274418919fSjohnjiang 	return 0;
1284418919fSjohnjiang }
1294418919fSjohnjiang 
1304418919fSjohnjiang 
1314418919fSjohnjiang static int
test_get_stats(int port)1324418919fSjohnjiang test_get_stats(int port)
1334418919fSjohnjiang {
1344418919fSjohnjiang 	struct rte_eth_stats stats;
1354418919fSjohnjiang 	struct rte_mbuf buf, *pbuf = &buf;
1364418919fSjohnjiang 
1374418919fSjohnjiang 	printf("Testing ring PMD stats_get port %d\n", port);
1384418919fSjohnjiang 
1394418919fSjohnjiang 	/* check stats of RXTX port, should all be zero */
1404418919fSjohnjiang 
1414418919fSjohnjiang 	rte_eth_stats_get(port, &stats);
1424418919fSjohnjiang 	if (stats.ipackets != 0 || stats.opackets != 0 ||
1434418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
1444418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
1454418919fSjohnjiang 		printf("Error: port %d stats are not zero\n", port);
1464418919fSjohnjiang 		return -1;
1474418919fSjohnjiang 	}
1484418919fSjohnjiang 
1494418919fSjohnjiang 	/* send and receive 1 packet and check for stats update */
1504418919fSjohnjiang 	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
1514418919fSjohnjiang 		printf("Error sending packet to port %d\n", port);
1524418919fSjohnjiang 		return -1;
1534418919fSjohnjiang 	}
1544418919fSjohnjiang 
1554418919fSjohnjiang 	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
1564418919fSjohnjiang 		printf("Error receiving packet from port %d\n", port);
1574418919fSjohnjiang 		return -1;
1584418919fSjohnjiang 	}
1594418919fSjohnjiang 
1604418919fSjohnjiang 	rte_eth_stats_get(port, &stats);
1614418919fSjohnjiang 	if (stats.ipackets != 1 || stats.opackets != 1 ||
1624418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
1634418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
1644418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n", port);
1654418919fSjohnjiang 		return -1;
1664418919fSjohnjiang 	}
1674418919fSjohnjiang 	return 0;
1684418919fSjohnjiang }
1694418919fSjohnjiang 
1704418919fSjohnjiang static int
test_stats_reset(int port)1714418919fSjohnjiang test_stats_reset(int port)
1724418919fSjohnjiang {
1734418919fSjohnjiang 	struct rte_eth_stats stats;
1744418919fSjohnjiang 	struct rte_mbuf buf, *pbuf = &buf;
1754418919fSjohnjiang 
1764418919fSjohnjiang 	printf("Testing ring PMD stats_reset port %d\n", port);
1774418919fSjohnjiang 
1784418919fSjohnjiang 	rte_eth_stats_reset(port);
1794418919fSjohnjiang 
1804418919fSjohnjiang 	/* check stats of RXTX port, should all be zero */
1814418919fSjohnjiang 	rte_eth_stats_get(port, &stats);
1824418919fSjohnjiang 	if (stats.ipackets != 0 || stats.opackets != 0 ||
1834418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
1844418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
1854418919fSjohnjiang 		printf("Error: port %d stats are not zero\n", port);
1864418919fSjohnjiang 		return -1;
1874418919fSjohnjiang 	}
1884418919fSjohnjiang 
1894418919fSjohnjiang 	/* send and receive 1 packet and check for stats update */
1904418919fSjohnjiang 	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
1914418919fSjohnjiang 		printf("Error sending packet to port %d\n", port);
1924418919fSjohnjiang 		return -1;
1934418919fSjohnjiang 	}
1944418919fSjohnjiang 
1954418919fSjohnjiang 	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
1964418919fSjohnjiang 		printf("Error receiving packet from port %d\n", port);
1974418919fSjohnjiang 		return -1;
1984418919fSjohnjiang 	}
1994418919fSjohnjiang 
2004418919fSjohnjiang 	rte_eth_stats_get(port, &stats);
2014418919fSjohnjiang 	if (stats.ipackets != 1 || stats.opackets != 1 ||
2024418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
2034418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
2044418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n", port);
2054418919fSjohnjiang 		return -1;
2064418919fSjohnjiang 	}
2074418919fSjohnjiang 
2084418919fSjohnjiang 	rte_eth_stats_reset(port);
2094418919fSjohnjiang 
2104418919fSjohnjiang 	/* check stats of RXTX port, should all be zero */
2114418919fSjohnjiang 	rte_eth_stats_get(port, &stats);
2124418919fSjohnjiang 	if (stats.ipackets != 0 || stats.opackets != 0 ||
2134418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
2144418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
2154418919fSjohnjiang 		printf("Error: port %d stats are not zero\n", port);
2164418919fSjohnjiang 		return -1;
2174418919fSjohnjiang 	}
2184418919fSjohnjiang 
2194418919fSjohnjiang 	return 0;
2204418919fSjohnjiang }
2214418919fSjohnjiang 
2224418919fSjohnjiang static int
test_pmd_ring_pair_create_attach(void)2234418919fSjohnjiang test_pmd_ring_pair_create_attach(void)
2244418919fSjohnjiang {
2254418919fSjohnjiang 	struct rte_eth_stats stats, stats2;
2264418919fSjohnjiang 	struct rte_mbuf buf, *pbuf = &buf;
2274418919fSjohnjiang 	struct rte_eth_conf null_conf;
228*2d9fd380Sjfb8856606 	int ret;
2294418919fSjohnjiang 
2304418919fSjohnjiang 	memset(&null_conf, 0, sizeof(struct rte_eth_conf));
2314418919fSjohnjiang 
2324418919fSjohnjiang 	if ((rte_eth_dev_configure(rxtx_portd, 1, 1, &null_conf) < 0)
2334418919fSjohnjiang 			|| (rte_eth_dev_configure(rxtx_porte, 1, 1,
2344418919fSjohnjiang 					&null_conf) < 0)) {
2354418919fSjohnjiang 		printf("Configure failed for port\n");
2364418919fSjohnjiang 		return TEST_FAILED;
2374418919fSjohnjiang 	}
2384418919fSjohnjiang 
2394418919fSjohnjiang 	if ((rte_eth_tx_queue_setup(rxtx_portd, 0, RING_SIZE,
2404418919fSjohnjiang 					SOCKET0, NULL) < 0)
2414418919fSjohnjiang 			|| (rte_eth_tx_queue_setup(rxtx_porte, 0, RING_SIZE,
2424418919fSjohnjiang 					SOCKET0, NULL) < 0)) {
2434418919fSjohnjiang 		printf("TX queue setup failed\n");
2444418919fSjohnjiang 		return TEST_FAILED;
2454418919fSjohnjiang 	}
2464418919fSjohnjiang 
2474418919fSjohnjiang 	if ((rte_eth_rx_queue_setup(rxtx_portd, 0, RING_SIZE,
2484418919fSjohnjiang 					SOCKET0, NULL, mp) < 0)
2494418919fSjohnjiang 			|| (rte_eth_rx_queue_setup(rxtx_porte, 0, RING_SIZE,
2504418919fSjohnjiang 					SOCKET0, NULL, mp) < 0)) {
2514418919fSjohnjiang 		printf("RX queue setup failed\n");
2524418919fSjohnjiang 		return TEST_FAILED;
2534418919fSjohnjiang 	}
2544418919fSjohnjiang 
2554418919fSjohnjiang 	if ((rte_eth_dev_start(rxtx_portd) < 0)
2564418919fSjohnjiang 			|| (rte_eth_dev_start(rxtx_porte) < 0)) {
2574418919fSjohnjiang 		printf("Error starting port\n");
2584418919fSjohnjiang 		return TEST_FAILED;
2594418919fSjohnjiang 	}
2604418919fSjohnjiang 
2614418919fSjohnjiang 	rte_eth_stats_reset(rxtx_portd);
2624418919fSjohnjiang 	/* check stats of port, should all be zero */
2634418919fSjohnjiang 	rte_eth_stats_get(rxtx_portd, &stats);
2644418919fSjohnjiang 	if (stats.ipackets != 0 || stats.opackets != 0 ||
2654418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
2664418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
2674418919fSjohnjiang 		printf("Error: port %d stats are not zero\n", rxtx_portd);
2684418919fSjohnjiang 		return TEST_FAILED;
2694418919fSjohnjiang 	}
2704418919fSjohnjiang 
2714418919fSjohnjiang 	rte_eth_stats_reset(rxtx_porte);
2724418919fSjohnjiang 	/* check stats of port, should all be zero */
2734418919fSjohnjiang 	rte_eth_stats_get(rxtx_porte, &stats2);
2744418919fSjohnjiang 	if (stats2.ipackets != 0 || stats2.opackets != 0 ||
2754418919fSjohnjiang 			stats2.ibytes != 0 || stats2.obytes != 0 ||
2764418919fSjohnjiang 			stats2.ierrors != 0 || stats2.oerrors != 0) {
2774418919fSjohnjiang 		printf("Error: port %d stats are not zero\n", rxtx_porte);
2784418919fSjohnjiang 		return TEST_FAILED;
2794418919fSjohnjiang 	}
2804418919fSjohnjiang 
2814418919fSjohnjiang 	/*
2824418919fSjohnjiang 	 * send and receive 1 packet (rxtx_portd -> rxtx_porte)
2834418919fSjohnjiang 	 * and check for stats update
2844418919fSjohnjiang 	 */
2854418919fSjohnjiang 	printf("Testing send and receive 1 packet (rxtx_portd -> rxtx_porte)\n");
2864418919fSjohnjiang 	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
2874418919fSjohnjiang 		printf("Error sending packet to port %d\n", rxtx_portd);
2884418919fSjohnjiang 		return TEST_FAILED;
2894418919fSjohnjiang 	}
2904418919fSjohnjiang 
2914418919fSjohnjiang 	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
2924418919fSjohnjiang 		printf("Error receiving packet from port %d\n", rxtx_porte);
2934418919fSjohnjiang 		return TEST_FAILED;
2944418919fSjohnjiang 	}
2954418919fSjohnjiang 
2964418919fSjohnjiang 	rte_eth_stats_get(rxtx_portd, &stats);
2974418919fSjohnjiang 	rte_eth_stats_get(rxtx_porte, &stats2);
2984418919fSjohnjiang 	if (stats.ipackets != 0 || stats.opackets != 1 ||
2994418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
3004418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
3014418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3024418919fSjohnjiang 				rxtx_portd);
3034418919fSjohnjiang 		return TEST_FAILED;
3044418919fSjohnjiang 	}
3054418919fSjohnjiang 
3064418919fSjohnjiang 	if (stats2.ipackets != 1 || stats2.opackets != 0 ||
3074418919fSjohnjiang 			stats2.ibytes != 0 || stats2.obytes != 0 ||
3084418919fSjohnjiang 			stats2.ierrors != 0 || stats2.oerrors != 0) {
3094418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3104418919fSjohnjiang 				rxtx_porte);
3114418919fSjohnjiang 		return TEST_FAILED;
3124418919fSjohnjiang 	}
3134418919fSjohnjiang 
3144418919fSjohnjiang 	/*
3154418919fSjohnjiang 	 * send and receive 1 packet (rxtx_porte -> rxtx_portd)
3164418919fSjohnjiang 	 * and check for stats update
3174418919fSjohnjiang 	 */
3184418919fSjohnjiang 	printf("Testing send and receive 1 packet "
3194418919fSjohnjiang 			"(rxtx_porte -> rxtx_portd)\n");
3204418919fSjohnjiang 	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
3214418919fSjohnjiang 		printf("Error sending packet to port %d\n", rxtx_porte);
3224418919fSjohnjiang 		return TEST_FAILED;
3234418919fSjohnjiang 	}
3244418919fSjohnjiang 
3254418919fSjohnjiang 	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
3264418919fSjohnjiang 		printf("Error receiving packet from port %d\n", rxtx_portd);
3274418919fSjohnjiang 		return TEST_FAILED;
3284418919fSjohnjiang 	}
3294418919fSjohnjiang 
3304418919fSjohnjiang 	rte_eth_stats_get(rxtx_portd, &stats);
3314418919fSjohnjiang 	rte_eth_stats_get(rxtx_porte, &stats2);
3324418919fSjohnjiang 	if (stats.ipackets != 1 || stats.opackets != 1 ||
3334418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
3344418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
3354418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3364418919fSjohnjiang 				rxtx_portd);
3374418919fSjohnjiang 		return TEST_FAILED;
3384418919fSjohnjiang 	}
3394418919fSjohnjiang 
3404418919fSjohnjiang 	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
3414418919fSjohnjiang 			stats2.ibytes != 0 || stats2.obytes != 0 ||
3424418919fSjohnjiang 			stats2.ierrors != 0 || stats2.oerrors != 0) {
3434418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3444418919fSjohnjiang 				rxtx_porte);
3454418919fSjohnjiang 		return TEST_FAILED;
3464418919fSjohnjiang 	}
3474418919fSjohnjiang 
3484418919fSjohnjiang 	/*
3494418919fSjohnjiang 	 * send and receive 1 packet (rxtx_portd -> rxtx_portd)
3504418919fSjohnjiang 	 * and check for stats update
3514418919fSjohnjiang 	 */
3524418919fSjohnjiang 	printf("Testing send and receive 1 packet "
3534418919fSjohnjiang 			"(rxtx_portd -> rxtx_portd)\n");
3544418919fSjohnjiang 	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
3554418919fSjohnjiang 		printf("Error sending packet to port %d\n", rxtx_portd);
3564418919fSjohnjiang 		return TEST_FAILED;
3574418919fSjohnjiang 	}
3584418919fSjohnjiang 
3594418919fSjohnjiang 	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
3604418919fSjohnjiang 		printf("Error receiving packet from port %d\n", rxtx_porte);
3614418919fSjohnjiang 		return TEST_FAILED;
3624418919fSjohnjiang 	}
3634418919fSjohnjiang 
3644418919fSjohnjiang 	rte_eth_stats_get(rxtx_portd, &stats);
3654418919fSjohnjiang 	rte_eth_stats_get(rxtx_porte, &stats2);
3664418919fSjohnjiang 	if (stats.ipackets != 2 || stats.opackets != 2 ||
3674418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
3684418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
3694418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3704418919fSjohnjiang 				rxtx_portd);
3714418919fSjohnjiang 		return TEST_FAILED;
3724418919fSjohnjiang 	}
3734418919fSjohnjiang 
3744418919fSjohnjiang 	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
3754418919fSjohnjiang 			stats2.ibytes != 0 || stats2.obytes != 0 ||
3764418919fSjohnjiang 			stats2.ierrors != 0 || stats2.oerrors != 0) {
3774418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
3784418919fSjohnjiang 				rxtx_porte);
3794418919fSjohnjiang 		return TEST_FAILED;
3804418919fSjohnjiang 	}
3814418919fSjohnjiang 
3824418919fSjohnjiang 	/*
3834418919fSjohnjiang 	 * send and receive 1 packet (rxtx_porte -> rxtx_porte)
3844418919fSjohnjiang 	 * and check for stats update
3854418919fSjohnjiang 	 */
3864418919fSjohnjiang 	printf("Testing send and receive 1 packet "
3874418919fSjohnjiang 			"(rxtx_porte -> rxtx_porte)\n");
3884418919fSjohnjiang 	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
3894418919fSjohnjiang 		printf("Error sending packet to port %d\n", rxtx_porte);
3904418919fSjohnjiang 		return TEST_FAILED;
3914418919fSjohnjiang 	}
3924418919fSjohnjiang 
3934418919fSjohnjiang 	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
3944418919fSjohnjiang 		printf("Error receiving packet from port %d\n", rxtx_porte);
3954418919fSjohnjiang 		return TEST_FAILED;
3964418919fSjohnjiang 	}
3974418919fSjohnjiang 
3984418919fSjohnjiang 	rte_eth_stats_get(rxtx_portd, &stats);
3994418919fSjohnjiang 	rte_eth_stats_get(rxtx_porte, &stats2);
4004418919fSjohnjiang 	if (stats.ipackets != 2 || stats.opackets != 2 ||
4014418919fSjohnjiang 			stats.ibytes != 0 || stats.obytes != 0 ||
4024418919fSjohnjiang 			stats.ierrors != 0 || stats.oerrors != 0) {
4034418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
4044418919fSjohnjiang 				rxtx_portd);
4054418919fSjohnjiang 		return TEST_FAILED;
4064418919fSjohnjiang 	}
4074418919fSjohnjiang 
4084418919fSjohnjiang 	if (stats2.ipackets != 2 || stats2.opackets != 2 ||
4094418919fSjohnjiang 			stats2.ibytes != 0 || stats2.obytes != 0 ||
4104418919fSjohnjiang 			stats2.ierrors != 0 || stats2.oerrors != 0) {
4114418919fSjohnjiang 		printf("Error: port %d stats are not as expected\n",
4124418919fSjohnjiang 				rxtx_porte);
4134418919fSjohnjiang 		return TEST_FAILED;
4144418919fSjohnjiang 	}
4154418919fSjohnjiang 
416*2d9fd380Sjfb8856606 	ret = rte_eth_dev_stop(rxtx_portd);
417*2d9fd380Sjfb8856606 	if (ret != 0)
418*2d9fd380Sjfb8856606 		printf("Error: failed to stop port %u: %s\n",
419*2d9fd380Sjfb8856606 		       rxtx_portd, rte_strerror(-ret));
420*2d9fd380Sjfb8856606 	ret = rte_eth_dev_stop(rxtx_porte);
421*2d9fd380Sjfb8856606 	if (ret != 0)
422*2d9fd380Sjfb8856606 		printf("Error: failed to stop port %u: %s\n",
423*2d9fd380Sjfb8856606 		       rxtx_porte, rte_strerror(-ret));
4244418919fSjohnjiang 
4254418919fSjohnjiang 	return TEST_SUCCESS;
4264418919fSjohnjiang }
4274418919fSjohnjiang 
4284418919fSjohnjiang static void
test_cleanup_resources(void)4294418919fSjohnjiang test_cleanup_resources(void)
4304418919fSjohnjiang {
431*2d9fd380Sjfb8856606 	int itr, ret;
4324418919fSjohnjiang 	for (itr = 0; itr < NUM_RINGS; itr++)
4334418919fSjohnjiang 		rte_ring_free(rxtx[itr]);
4344418919fSjohnjiang 
435*2d9fd380Sjfb8856606 	ret = rte_eth_dev_stop(tx_porta);
436*2d9fd380Sjfb8856606 	if (ret != 0)
437*2d9fd380Sjfb8856606 		printf("Error: failed to stop port %u: %s\n",
438*2d9fd380Sjfb8856606 		       tx_porta, rte_strerror(-ret));
439*2d9fd380Sjfb8856606 	ret = rte_eth_dev_stop(rx_portb);
440*2d9fd380Sjfb8856606 	if (ret != 0)
441*2d9fd380Sjfb8856606 		printf("Error: failed to stop port %u: %s\n",
442*2d9fd380Sjfb8856606 		       rx_portb, rte_strerror(-ret));
443*2d9fd380Sjfb8856606 	ret = rte_eth_dev_stop(rxtx_portc);
444*2d9fd380Sjfb8856606 	if (ret != 0)
445*2d9fd380Sjfb8856606 		printf("Error: failed to stop port %u: %s\n",
446*2d9fd380Sjfb8856606 		       rxtx_portc, rte_strerror(-ret));
4474418919fSjohnjiang 
4484418919fSjohnjiang 	rte_mempool_free(mp);
4494418919fSjohnjiang 	rte_vdev_uninit("net_ring_net_ringa");
4504418919fSjohnjiang 	rte_vdev_uninit("net_ring_net_ringb");
4514418919fSjohnjiang 	rte_vdev_uninit("net_ring_net_ringc");
4524418919fSjohnjiang 	rte_vdev_uninit("net_ring_net_ringd");
4534418919fSjohnjiang 	rte_vdev_uninit("net_ring_net_ringe");
4544418919fSjohnjiang }
4554418919fSjohnjiang 
4564418919fSjohnjiang static int
test_pmd_ringcreate_setup(void)4574418919fSjohnjiang test_pmd_ringcreate_setup(void)
4584418919fSjohnjiang {
4594418919fSjohnjiang 	uint8_t nb_ports;
4604418919fSjohnjiang 
4614418919fSjohnjiang 	nb_ports = rte_eth_dev_count_avail();
4624418919fSjohnjiang 	printf("nb_ports=%d\n", (int)nb_ports);
4634418919fSjohnjiang 
4644418919fSjohnjiang 	/*  create the rings and eth_rings in the test code.
4654418919fSjohnjiang 	 *  This does not test the rte_pmd_ring_devinit function.
4664418919fSjohnjiang 	 *
4674418919fSjohnjiang 	 *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
4684418919fSjohnjiang 	 */
4694418919fSjohnjiang 	rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
4704418919fSjohnjiang 	if (rxtx[0] == NULL) {
4714418919fSjohnjiang 		printf("rte_ring_create R0 failed");
4724418919fSjohnjiang 		return -1;
4734418919fSjohnjiang 	}
4744418919fSjohnjiang 
4754418919fSjohnjiang 	rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
4764418919fSjohnjiang 	if (rxtx[1] == NULL) {
4774418919fSjohnjiang 		printf("rte_ring_create R1 failed");
4784418919fSjohnjiang 		return -1;
4794418919fSjohnjiang 	}
4804418919fSjohnjiang 
4814418919fSjohnjiang 	tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
4824418919fSjohnjiang 	rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
4834418919fSjohnjiang 	rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
4844418919fSjohnjiang 	rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
4854418919fSjohnjiang 	rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
4864418919fSjohnjiang 
4874418919fSjohnjiang 	printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
4884418919fSjohnjiang 			tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
4894418919fSjohnjiang 
4904418919fSjohnjiang 	if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
4914418919fSjohnjiang 			|| (rxtx_portd == -1) || (rxtx_porte == -1)) {
4924418919fSjohnjiang 		printf("rte_eth_from rings failed\n");
4934418919fSjohnjiang 		return -1;
4944418919fSjohnjiang 	}
4954418919fSjohnjiang 
4964418919fSjohnjiang 	mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
4974418919fSjohnjiang 			0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
4984418919fSjohnjiang 	if (mp == NULL)
4994418919fSjohnjiang 		return -1;
5004418919fSjohnjiang 
5014418919fSjohnjiang 	if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
5024418919fSjohnjiang 			|| (rxtx_portc >= RTE_MAX_ETHPORTS)
5034418919fSjohnjiang 			|| (rxtx_portd >= RTE_MAX_ETHPORTS)
5044418919fSjohnjiang 			|| (rxtx_porte >= RTE_MAX_ETHPORTS)) {
5054418919fSjohnjiang 		printf(" port exceed max eth ports\n");
5064418919fSjohnjiang 		return -1;
5074418919fSjohnjiang 	}
5084418919fSjohnjiang 	return 0;
5094418919fSjohnjiang }
5104418919fSjohnjiang 
5114418919fSjohnjiang static int
test_command_line_ring_port(void)5124418919fSjohnjiang test_command_line_ring_port(void)
5134418919fSjohnjiang {
5144418919fSjohnjiang 	int port, cmdl_port0 = -1;
5154418919fSjohnjiang 	int ret;
5164418919fSjohnjiang 
5174418919fSjohnjiang 	/* find a port created with the --vdev=net_ring0 command line option */
5184418919fSjohnjiang 	RTE_ETH_FOREACH_DEV(port) {
5194418919fSjohnjiang 		struct rte_eth_dev_info dev_info;
5204418919fSjohnjiang 
5214418919fSjohnjiang 		ret = rte_eth_dev_info_get(port, &dev_info);
5224418919fSjohnjiang 		TEST_ASSERT((ret == 0),
5234418919fSjohnjiang 				"Error during getting device (port %d) info: %s\n",
5244418919fSjohnjiang 				port, strerror(-ret));
5254418919fSjohnjiang 
5264418919fSjohnjiang 		if (!strcmp(dev_info.driver_name, "Rings PMD")) {
5274418919fSjohnjiang 			printf("found a command line ring port=%d\n", port);
5284418919fSjohnjiang 			cmdl_port0 = port;
5294418919fSjohnjiang 			break;
5304418919fSjohnjiang 		}
5314418919fSjohnjiang 	}
5324418919fSjohnjiang 	if (cmdl_port0 != -1) {
5334418919fSjohnjiang 		TEST_ASSERT((test_ethdev_configure_port(cmdl_port0) < 0),
5344418919fSjohnjiang 				"test ethdev configure port cmdl_port0 is failed");
5354418919fSjohnjiang 		TEST_ASSERT((test_send_basic_packets_port(cmdl_port0) < 0),
5364418919fSjohnjiang 				"test send basic packets port cmdl_port0 is failed");
5374418919fSjohnjiang 		TEST_ASSERT((test_stats_reset(cmdl_port0) < 0),
5384418919fSjohnjiang 				"test stats reset cmdl_port0 is failed");
5394418919fSjohnjiang 		TEST_ASSERT((test_get_stats(cmdl_port0) < 0),
5404418919fSjohnjiang 				"test get stats cmdl_port0 is failed");
541*2d9fd380Sjfb8856606 		TEST_ASSERT((rte_eth_dev_stop(cmdl_port0) == 0),
542*2d9fd380Sjfb8856606 				"test stop cmdl_port0 is failed");
5434418919fSjohnjiang 	}
5444418919fSjohnjiang 	return TEST_SUCCESS;
5454418919fSjohnjiang }
5464418919fSjohnjiang 
5474418919fSjohnjiang static int
test_ethdev_configure_ports(void)5484418919fSjohnjiang test_ethdev_configure_ports(void)
5494418919fSjohnjiang {
5504418919fSjohnjiang 	TEST_ASSERT((test_ethdev_configure_port(tx_porta) == 0),
5514418919fSjohnjiang 			"test ethdev configure ports tx_porta is failed");
5524418919fSjohnjiang 	TEST_ASSERT((test_ethdev_configure_port(rx_portb) == 0),
5534418919fSjohnjiang 			"test ethdev configure ports rx_portb is failed");
5544418919fSjohnjiang 	TEST_ASSERT((test_ethdev_configure_port(rxtx_portc) == 0),
5554418919fSjohnjiang 			"test ethdev configure ports rxtx_portc is failed");
5564418919fSjohnjiang 
5574418919fSjohnjiang 	return TEST_SUCCESS;
5584418919fSjohnjiang }
5594418919fSjohnjiang 
5604418919fSjohnjiang static int
test_get_stats_for_port(void)5614418919fSjohnjiang test_get_stats_for_port(void)
5624418919fSjohnjiang {
5634418919fSjohnjiang 	TEST_ASSERT(test_get_stats(rxtx_portc) == 0, "test get stats failed");
5644418919fSjohnjiang 	return TEST_SUCCESS;
5654418919fSjohnjiang }
5664418919fSjohnjiang 
5674418919fSjohnjiang static int
test_stats_reset_for_port(void)5684418919fSjohnjiang test_stats_reset_for_port(void)
5694418919fSjohnjiang {
5704418919fSjohnjiang 	TEST_ASSERT(test_stats_reset(rxtx_portc) == 0, "test stats reset failed");
5714418919fSjohnjiang 	return TEST_SUCCESS;
5724418919fSjohnjiang }
5734418919fSjohnjiang 
5744418919fSjohnjiang static struct
5754418919fSjohnjiang unit_test_suite test_pmd_ring_suite  = {
5764418919fSjohnjiang 	.setup = test_pmd_ringcreate_setup,
5774418919fSjohnjiang 	.teardown = test_cleanup_resources,
5784418919fSjohnjiang 	.suite_name = "Test Pmd Ring Unit Test Suite",
5794418919fSjohnjiang 	.unit_test_cases = {
5804418919fSjohnjiang 		TEST_CASE(test_ethdev_configure_ports),
5814418919fSjohnjiang 		TEST_CASE(test_send_basic_packets),
5824418919fSjohnjiang 		TEST_CASE(test_get_stats_for_port),
5834418919fSjohnjiang 		TEST_CASE(test_stats_reset_for_port),
5844418919fSjohnjiang 		TEST_CASE(test_pmd_ring_pair_create_attach),
5854418919fSjohnjiang 		TEST_CASE(test_command_line_ring_port),
5864418919fSjohnjiang 		TEST_CASES_END()
5874418919fSjohnjiang 	}
5884418919fSjohnjiang };
5894418919fSjohnjiang 
5904418919fSjohnjiang static int
test_pmd_ring(void)5914418919fSjohnjiang test_pmd_ring(void)
5924418919fSjohnjiang {
5934418919fSjohnjiang 	return unit_test_suite_runner(&test_pmd_ring_suite);
5944418919fSjohnjiang }
5954418919fSjohnjiang 
5964418919fSjohnjiang REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);
597