xref: /dpdk/app/test/test_pmd_ring.c (revision d83fb967212efa19d272e7fa65d17c9ad94b17c1)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2015 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson #include "test.h"
5a9de470cSBruce Richardson #include <string.h>
6a9de470cSBruce Richardson 
7a9de470cSBruce Richardson #include <stdio.h>
8a9de470cSBruce Richardson 
9a9de470cSBruce Richardson #include <rte_eth_ring.h>
10a9de470cSBruce Richardson #include <rte_ethdev.h>
11a9de470cSBruce Richardson #include <rte_bus_vdev.h>
12a9de470cSBruce Richardson 
13a9de470cSBruce Richardson #define SOCKET0 0
14a9de470cSBruce Richardson #define RING_SIZE 256
15a9de470cSBruce Richardson #define NUM_RINGS 2
16a9de470cSBruce Richardson #define NB_MBUF 512
17a9de470cSBruce Richardson 
18a9de470cSBruce Richardson static struct rte_mempool *mp;
19a9de470cSBruce Richardson struct rte_ring *rxtx[NUM_RINGS];
20a9de470cSBruce Richardson static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;
21a9de470cSBruce Richardson 
22a9de470cSBruce Richardson static int
test_ethdev_configure_port(int port)23a9de470cSBruce Richardson test_ethdev_configure_port(int port)
24a9de470cSBruce Richardson {
25a9de470cSBruce Richardson 	struct rte_eth_conf null_conf;
26a9de470cSBruce Richardson 	struct rte_eth_link link;
27844949ecSIgor Romanov 	int ret;
28a9de470cSBruce Richardson 
29a9de470cSBruce Richardson 	memset(&null_conf, 0, sizeof(struct rte_eth_conf));
30a9de470cSBruce Richardson 
31a9de470cSBruce Richardson 	if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
32a9de470cSBruce Richardson 		printf("Configure failed for port %d\n", port);
33a9de470cSBruce Richardson 		return -1;
34a9de470cSBruce Richardson 	}
35a9de470cSBruce Richardson 
36a9de470cSBruce Richardson 	/* Test queue release */
37a9de470cSBruce Richardson 	if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
38a9de470cSBruce Richardson 		printf("Configure failed for port %d\n", port);
39a9de470cSBruce Richardson 		return -1;
40a9de470cSBruce Richardson 	}
41a9de470cSBruce Richardson 
42a9de470cSBruce Richardson 	if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
43a9de470cSBruce Richardson 		printf("TX queue setup failed port %d\n", port);
44a9de470cSBruce Richardson 		return -1;
45a9de470cSBruce Richardson 	}
46a9de470cSBruce Richardson 
47a9de470cSBruce Richardson 	if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
48a9de470cSBruce Richardson 				NULL, mp) < 0) {
49a9de470cSBruce Richardson 		printf("RX queue setup failed port %d\n", port);
50a9de470cSBruce Richardson 		return -1;
51a9de470cSBruce Richardson 	}
52a9de470cSBruce Richardson 
53a9de470cSBruce Richardson 	if (rte_eth_dev_start(port) < 0) {
54a9de470cSBruce Richardson 		printf("Error starting port %d\n", port);
55a9de470cSBruce Richardson 		return -1;
56a9de470cSBruce Richardson 	}
57a9de470cSBruce Richardson 
58844949ecSIgor Romanov 	ret = rte_eth_link_get(port, &link);
59844949ecSIgor Romanov 	if (ret < 0) {
60844949ecSIgor Romanov 		printf("Link get failed for port %u: %s",
61844949ecSIgor Romanov 		       port, rte_strerror(-ret));
62844949ecSIgor Romanov 		return -1;
63844949ecSIgor Romanov 	}
64a9de470cSBruce Richardson 
65a9de470cSBruce Richardson 	return 0;
66a9de470cSBruce Richardson }
67a9de470cSBruce Richardson 
68a9de470cSBruce Richardson static int
test_send_basic_packets(void)69a9de470cSBruce Richardson test_send_basic_packets(void)
70a9de470cSBruce Richardson {
71a9de470cSBruce Richardson 	struct rte_mbuf  bufs[RING_SIZE];
72a9de470cSBruce Richardson 	struct rte_mbuf *pbufs[RING_SIZE];
73a9de470cSBruce Richardson 	int i;
74a9de470cSBruce Richardson 
75a9de470cSBruce Richardson 	printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");
76a9de470cSBruce Richardson 
77a9de470cSBruce Richardson 	for (i = 0; i < RING_SIZE/2; i++)
78a9de470cSBruce Richardson 		pbufs[i] = &bufs[i];
79a9de470cSBruce Richardson 
80a9de470cSBruce Richardson 	if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
81a9de470cSBruce Richardson 		printf("Failed to transmit packet burst port %d\n", tx_porta);
82a9de470cSBruce Richardson 		return TEST_FAILED;
83a9de470cSBruce Richardson 	}
84a9de470cSBruce Richardson 
85a9de470cSBruce Richardson 	if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
86a9de470cSBruce Richardson 		printf("Failed to receive packet burst on port %d\n", rx_portb);
87a9de470cSBruce Richardson 		return TEST_FAILED;
88a9de470cSBruce Richardson 	}
89a9de470cSBruce Richardson 
90a9de470cSBruce Richardson 	for (i = 0; i < RING_SIZE/2; i++)
91a9de470cSBruce Richardson 		if (pbufs[i] != &bufs[i]) {
92a9de470cSBruce Richardson 			printf("Error: received data does not match that transmitted\n");
93a9de470cSBruce Richardson 			return TEST_FAILED;
94a9de470cSBruce Richardson 		}
95a9de470cSBruce Richardson 
96a9de470cSBruce Richardson 	return TEST_SUCCESS;
97a9de470cSBruce Richardson }
98a9de470cSBruce Richardson 
99a9de470cSBruce Richardson static int
test_send_basic_packets_port(int port)100a9de470cSBruce Richardson test_send_basic_packets_port(int port)
101a9de470cSBruce Richardson {
102a9de470cSBruce Richardson 	struct rte_mbuf  bufs[RING_SIZE];
103a9de470cSBruce Richardson 	struct rte_mbuf *pbufs[RING_SIZE];
104a9de470cSBruce Richardson 	int i;
105a9de470cSBruce Richardson 
106a9de470cSBruce Richardson 	printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");
107a9de470cSBruce Richardson 
108a9de470cSBruce Richardson 	for (i = 0; i < RING_SIZE/2; i++)
109a9de470cSBruce Richardson 		pbufs[i] = &bufs[i];
110a9de470cSBruce Richardson 
111a9de470cSBruce Richardson 	if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
112a9de470cSBruce Richardson 		printf("Failed to transmit packet burst port %d\n", port);
113a9de470cSBruce Richardson 		return -1;
114a9de470cSBruce Richardson 	}
115a9de470cSBruce Richardson 
116a9de470cSBruce Richardson 	if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
117a9de470cSBruce Richardson 		printf("Failed to receive packet burst on port %d\n", port);
118a9de470cSBruce Richardson 		return -1;
119a9de470cSBruce Richardson 	}
120a9de470cSBruce Richardson 
121a9de470cSBruce Richardson 	for (i = 0; i < RING_SIZE/2; i++)
122a9de470cSBruce Richardson 		if (pbufs[i] != &bufs[i]) {
123a9de470cSBruce Richardson 			printf("Error: received data does not match that transmitted\n");
124a9de470cSBruce Richardson 			return -1;
125a9de470cSBruce Richardson 		}
126a9de470cSBruce Richardson 
127a9de470cSBruce Richardson 	return 0;
128a9de470cSBruce Richardson }
129a9de470cSBruce Richardson 
130a9de470cSBruce Richardson 
131a9de470cSBruce Richardson static int
test_get_stats(int port)132a9de470cSBruce Richardson test_get_stats(int port)
133a9de470cSBruce Richardson {
134a9de470cSBruce Richardson 	struct rte_eth_stats stats;
135a9de470cSBruce Richardson 	struct rte_mbuf buf, *pbuf = &buf;
136a9de470cSBruce Richardson 
137a9de470cSBruce Richardson 	printf("Testing ring PMD stats_get port %d\n", port);
138a9de470cSBruce Richardson 
139a9de470cSBruce Richardson 	/* check stats of RXTX port, should all be zero */
140a9de470cSBruce Richardson 
141a9de470cSBruce Richardson 	rte_eth_stats_get(port, &stats);
142a9de470cSBruce Richardson 	if (stats.ipackets != 0 || stats.opackets != 0 ||
143a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
144a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
145a9de470cSBruce Richardson 		printf("Error: port %d stats are not zero\n", port);
146a9de470cSBruce Richardson 		return -1;
147a9de470cSBruce Richardson 	}
148a9de470cSBruce Richardson 
149a9de470cSBruce Richardson 	/* send and receive 1 packet and check for stats update */
150a9de470cSBruce Richardson 	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
151a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", port);
152a9de470cSBruce Richardson 		return -1;
153a9de470cSBruce Richardson 	}
154a9de470cSBruce Richardson 
155a9de470cSBruce Richardson 	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
156a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", port);
157a9de470cSBruce Richardson 		return -1;
158a9de470cSBruce Richardson 	}
159a9de470cSBruce Richardson 
160a9de470cSBruce Richardson 	rte_eth_stats_get(port, &stats);
161a9de470cSBruce Richardson 	if (stats.ipackets != 1 || stats.opackets != 1 ||
162a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
163a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
164a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n", port);
165a9de470cSBruce Richardson 		return -1;
166a9de470cSBruce Richardson 	}
167a9de470cSBruce Richardson 	return 0;
168a9de470cSBruce Richardson }
169a9de470cSBruce Richardson 
170a9de470cSBruce Richardson static int
test_stats_reset(int port)171a9de470cSBruce Richardson test_stats_reset(int port)
172a9de470cSBruce Richardson {
173a9de470cSBruce Richardson 	struct rte_eth_stats stats;
174a9de470cSBruce Richardson 	struct rte_mbuf buf, *pbuf = &buf;
175a9de470cSBruce Richardson 
176a9de470cSBruce Richardson 	printf("Testing ring PMD stats_reset port %d\n", port);
177a9de470cSBruce Richardson 
178a9de470cSBruce Richardson 	rte_eth_stats_reset(port);
179a9de470cSBruce Richardson 
180a9de470cSBruce Richardson 	/* check stats of RXTX port, should all be zero */
181a9de470cSBruce Richardson 	rte_eth_stats_get(port, &stats);
182a9de470cSBruce Richardson 	if (stats.ipackets != 0 || stats.opackets != 0 ||
183a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
184a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
185a9de470cSBruce Richardson 		printf("Error: port %d stats are not zero\n", port);
186a9de470cSBruce Richardson 		return -1;
187a9de470cSBruce Richardson 	}
188a9de470cSBruce Richardson 
189a9de470cSBruce Richardson 	/* send and receive 1 packet and check for stats update */
190a9de470cSBruce Richardson 	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
191a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", port);
192a9de470cSBruce Richardson 		return -1;
193a9de470cSBruce Richardson 	}
194a9de470cSBruce Richardson 
195a9de470cSBruce Richardson 	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
196a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", port);
197a9de470cSBruce Richardson 		return -1;
198a9de470cSBruce Richardson 	}
199a9de470cSBruce Richardson 
200a9de470cSBruce Richardson 	rte_eth_stats_get(port, &stats);
201a9de470cSBruce Richardson 	if (stats.ipackets != 1 || stats.opackets != 1 ||
202a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
203a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
204a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n", port);
205a9de470cSBruce Richardson 		return -1;
206a9de470cSBruce Richardson 	}
207a9de470cSBruce Richardson 
208a9de470cSBruce Richardson 	rte_eth_stats_reset(port);
209a9de470cSBruce Richardson 
210a9de470cSBruce Richardson 	/* check stats of RXTX port, should all be zero */
211a9de470cSBruce Richardson 	rte_eth_stats_get(port, &stats);
212a9de470cSBruce Richardson 	if (stats.ipackets != 0 || stats.opackets != 0 ||
213a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
214a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
215a9de470cSBruce Richardson 		printf("Error: port %d stats are not zero\n", port);
216a9de470cSBruce Richardson 		return -1;
217a9de470cSBruce Richardson 	}
218a9de470cSBruce Richardson 
219a9de470cSBruce Richardson 	return 0;
220a9de470cSBruce Richardson }
221a9de470cSBruce Richardson 
222a9de470cSBruce Richardson static int
test_pmd_ring_pair_create_attach(void)223a9de470cSBruce Richardson test_pmd_ring_pair_create_attach(void)
224a9de470cSBruce Richardson {
225a9de470cSBruce Richardson 	struct rte_eth_stats stats, stats2;
226a9de470cSBruce Richardson 	struct rte_mbuf buf, *pbuf = &buf;
227a9de470cSBruce Richardson 	struct rte_eth_conf null_conf;
2280ead65afSIvan Ilchenko 	int ret;
229a9de470cSBruce Richardson 
230a9de470cSBruce Richardson 	memset(&null_conf, 0, sizeof(struct rte_eth_conf));
231a9de470cSBruce Richardson 
232a9de470cSBruce Richardson 	if ((rte_eth_dev_configure(rxtx_portd, 1, 1, &null_conf) < 0)
233a9de470cSBruce Richardson 			|| (rte_eth_dev_configure(rxtx_porte, 1, 1,
234a9de470cSBruce Richardson 					&null_conf) < 0)) {
235a9de470cSBruce Richardson 		printf("Configure failed for port\n");
236a9de470cSBruce Richardson 		return TEST_FAILED;
237a9de470cSBruce Richardson 	}
238a9de470cSBruce Richardson 
239a9de470cSBruce Richardson 	if ((rte_eth_tx_queue_setup(rxtx_portd, 0, RING_SIZE,
240a9de470cSBruce Richardson 					SOCKET0, NULL) < 0)
241a9de470cSBruce Richardson 			|| (rte_eth_tx_queue_setup(rxtx_porte, 0, RING_SIZE,
242a9de470cSBruce Richardson 					SOCKET0, NULL) < 0)) {
243a9de470cSBruce Richardson 		printf("TX queue setup failed\n");
244a9de470cSBruce Richardson 		return TEST_FAILED;
245a9de470cSBruce Richardson 	}
246a9de470cSBruce Richardson 
247a9de470cSBruce Richardson 	if ((rte_eth_rx_queue_setup(rxtx_portd, 0, RING_SIZE,
248a9de470cSBruce Richardson 					SOCKET0, NULL, mp) < 0)
249a9de470cSBruce Richardson 			|| (rte_eth_rx_queue_setup(rxtx_porte, 0, RING_SIZE,
250a9de470cSBruce Richardson 					SOCKET0, NULL, mp) < 0)) {
251a9de470cSBruce Richardson 		printf("RX queue setup failed\n");
252a9de470cSBruce Richardson 		return TEST_FAILED;
253a9de470cSBruce Richardson 	}
254a9de470cSBruce Richardson 
255a9de470cSBruce Richardson 	if ((rte_eth_dev_start(rxtx_portd) < 0)
256a9de470cSBruce Richardson 			|| (rte_eth_dev_start(rxtx_porte) < 0)) {
257a9de470cSBruce Richardson 		printf("Error starting port\n");
258a9de470cSBruce Richardson 		return TEST_FAILED;
259a9de470cSBruce Richardson 	}
260a9de470cSBruce Richardson 
261a9de470cSBruce Richardson 	rte_eth_stats_reset(rxtx_portd);
262a9de470cSBruce Richardson 	/* check stats of port, should all be zero */
263a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_portd, &stats);
264a9de470cSBruce Richardson 	if (stats.ipackets != 0 || stats.opackets != 0 ||
265a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
266a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
267a9de470cSBruce Richardson 		printf("Error: port %d stats are not zero\n", rxtx_portd);
268a9de470cSBruce Richardson 		return TEST_FAILED;
269a9de470cSBruce Richardson 	}
270a9de470cSBruce Richardson 
271a9de470cSBruce Richardson 	rte_eth_stats_reset(rxtx_porte);
272a9de470cSBruce Richardson 	/* check stats of port, should all be zero */
273a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_porte, &stats2);
274a9de470cSBruce Richardson 	if (stats2.ipackets != 0 || stats2.opackets != 0 ||
275a9de470cSBruce Richardson 			stats2.ibytes != 0 || stats2.obytes != 0 ||
276a9de470cSBruce Richardson 			stats2.ierrors != 0 || stats2.oerrors != 0) {
277a9de470cSBruce Richardson 		printf("Error: port %d stats are not zero\n", rxtx_porte);
278a9de470cSBruce Richardson 		return TEST_FAILED;
279a9de470cSBruce Richardson 	}
280a9de470cSBruce Richardson 
281a9de470cSBruce Richardson 	/*
282a9de470cSBruce Richardson 	 * send and receive 1 packet (rxtx_portd -> rxtx_porte)
283a9de470cSBruce Richardson 	 * and check for stats update
284a9de470cSBruce Richardson 	 */
285a9de470cSBruce Richardson 	printf("Testing send and receive 1 packet (rxtx_portd -> rxtx_porte)\n");
286a9de470cSBruce Richardson 	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
287a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", rxtx_portd);
288a9de470cSBruce Richardson 		return TEST_FAILED;
289a9de470cSBruce Richardson 	}
290a9de470cSBruce Richardson 
291a9de470cSBruce Richardson 	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
292a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", rxtx_porte);
293a9de470cSBruce Richardson 		return TEST_FAILED;
294a9de470cSBruce Richardson 	}
295a9de470cSBruce Richardson 
296a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_portd, &stats);
297a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_porte, &stats2);
298a9de470cSBruce Richardson 	if (stats.ipackets != 0 || stats.opackets != 1 ||
299a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
300a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
301a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
302a9de470cSBruce Richardson 				rxtx_portd);
303a9de470cSBruce Richardson 		return TEST_FAILED;
304a9de470cSBruce Richardson 	}
305a9de470cSBruce Richardson 
306a9de470cSBruce Richardson 	if (stats2.ipackets != 1 || stats2.opackets != 0 ||
307a9de470cSBruce Richardson 			stats2.ibytes != 0 || stats2.obytes != 0 ||
308a9de470cSBruce Richardson 			stats2.ierrors != 0 || stats2.oerrors != 0) {
309a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
310a9de470cSBruce Richardson 				rxtx_porte);
311a9de470cSBruce Richardson 		return TEST_FAILED;
312a9de470cSBruce Richardson 	}
313a9de470cSBruce Richardson 
314a9de470cSBruce Richardson 	/*
315a9de470cSBruce Richardson 	 * send and receive 1 packet (rxtx_porte -> rxtx_portd)
316a9de470cSBruce Richardson 	 * and check for stats update
317a9de470cSBruce Richardson 	 */
318a9de470cSBruce Richardson 	printf("Testing send and receive 1 packet "
319a9de470cSBruce Richardson 			"(rxtx_porte -> rxtx_portd)\n");
320a9de470cSBruce Richardson 	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
321a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", rxtx_porte);
322a9de470cSBruce Richardson 		return TEST_FAILED;
323a9de470cSBruce Richardson 	}
324a9de470cSBruce Richardson 
325a9de470cSBruce Richardson 	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
326a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", rxtx_portd);
327a9de470cSBruce Richardson 		return TEST_FAILED;
328a9de470cSBruce Richardson 	}
329a9de470cSBruce Richardson 
330a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_portd, &stats);
331a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_porte, &stats2);
332a9de470cSBruce Richardson 	if (stats.ipackets != 1 || stats.opackets != 1 ||
333a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
334a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
335a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
336a9de470cSBruce Richardson 				rxtx_portd);
337a9de470cSBruce Richardson 		return TEST_FAILED;
338a9de470cSBruce Richardson 	}
339a9de470cSBruce Richardson 
340a9de470cSBruce Richardson 	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
341a9de470cSBruce Richardson 			stats2.ibytes != 0 || stats2.obytes != 0 ||
342a9de470cSBruce Richardson 			stats2.ierrors != 0 || stats2.oerrors != 0) {
343a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
344a9de470cSBruce Richardson 				rxtx_porte);
345a9de470cSBruce Richardson 		return TEST_FAILED;
346a9de470cSBruce Richardson 	}
347a9de470cSBruce Richardson 
348a9de470cSBruce Richardson 	/*
349a9de470cSBruce Richardson 	 * send and receive 1 packet (rxtx_portd -> rxtx_portd)
350a9de470cSBruce Richardson 	 * and check for stats update
351a9de470cSBruce Richardson 	 */
352a9de470cSBruce Richardson 	printf("Testing send and receive 1 packet "
353a9de470cSBruce Richardson 			"(rxtx_portd -> rxtx_portd)\n");
354a9de470cSBruce Richardson 	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
355a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", rxtx_portd);
356a9de470cSBruce Richardson 		return TEST_FAILED;
357a9de470cSBruce Richardson 	}
358a9de470cSBruce Richardson 
359a9de470cSBruce Richardson 	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
360a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", rxtx_porte);
361a9de470cSBruce Richardson 		return TEST_FAILED;
362a9de470cSBruce Richardson 	}
363a9de470cSBruce Richardson 
364a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_portd, &stats);
365a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_porte, &stats2);
366a9de470cSBruce Richardson 	if (stats.ipackets != 2 || stats.opackets != 2 ||
367a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
368a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
369a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
370a9de470cSBruce Richardson 				rxtx_portd);
371a9de470cSBruce Richardson 		return TEST_FAILED;
372a9de470cSBruce Richardson 	}
373a9de470cSBruce Richardson 
374a9de470cSBruce Richardson 	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
375a9de470cSBruce Richardson 			stats2.ibytes != 0 || stats2.obytes != 0 ||
376a9de470cSBruce Richardson 			stats2.ierrors != 0 || stats2.oerrors != 0) {
377a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
378a9de470cSBruce Richardson 				rxtx_porte);
379a9de470cSBruce Richardson 		return TEST_FAILED;
380a9de470cSBruce Richardson 	}
381a9de470cSBruce Richardson 
382a9de470cSBruce Richardson 	/*
383a9de470cSBruce Richardson 	 * send and receive 1 packet (rxtx_porte -> rxtx_porte)
384a9de470cSBruce Richardson 	 * and check for stats update
385a9de470cSBruce Richardson 	 */
386a9de470cSBruce Richardson 	printf("Testing send and receive 1 packet "
387a9de470cSBruce Richardson 			"(rxtx_porte -> rxtx_porte)\n");
388a9de470cSBruce Richardson 	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
389a9de470cSBruce Richardson 		printf("Error sending packet to port %d\n", rxtx_porte);
390a9de470cSBruce Richardson 		return TEST_FAILED;
391a9de470cSBruce Richardson 	}
392a9de470cSBruce Richardson 
393a9de470cSBruce Richardson 	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
394a9de470cSBruce Richardson 		printf("Error receiving packet from port %d\n", rxtx_porte);
395a9de470cSBruce Richardson 		return TEST_FAILED;
396a9de470cSBruce Richardson 	}
397a9de470cSBruce Richardson 
398a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_portd, &stats);
399a9de470cSBruce Richardson 	rte_eth_stats_get(rxtx_porte, &stats2);
400a9de470cSBruce Richardson 	if (stats.ipackets != 2 || stats.opackets != 2 ||
401a9de470cSBruce Richardson 			stats.ibytes != 0 || stats.obytes != 0 ||
402a9de470cSBruce Richardson 			stats.ierrors != 0 || stats.oerrors != 0) {
403a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
404a9de470cSBruce Richardson 				rxtx_portd);
405a9de470cSBruce Richardson 		return TEST_FAILED;
406a9de470cSBruce Richardson 	}
407a9de470cSBruce Richardson 
408a9de470cSBruce Richardson 	if (stats2.ipackets != 2 || stats2.opackets != 2 ||
409a9de470cSBruce Richardson 			stats2.ibytes != 0 || stats2.obytes != 0 ||
410a9de470cSBruce Richardson 			stats2.ierrors != 0 || stats2.oerrors != 0) {
411a9de470cSBruce Richardson 		printf("Error: port %d stats are not as expected\n",
412a9de470cSBruce Richardson 				rxtx_porte);
413a9de470cSBruce Richardson 		return TEST_FAILED;
414a9de470cSBruce Richardson 	}
415a9de470cSBruce Richardson 
4160ead65afSIvan Ilchenko 	ret = rte_eth_dev_stop(rxtx_portd);
4170ead65afSIvan Ilchenko 	if (ret != 0)
4180ead65afSIvan Ilchenko 		printf("Error: failed to stop port %u: %s\n",
4190ead65afSIvan Ilchenko 		       rxtx_portd, rte_strerror(-ret));
4200ead65afSIvan Ilchenko 	ret = rte_eth_dev_stop(rxtx_porte);
4210ead65afSIvan Ilchenko 	if (ret != 0)
4220ead65afSIvan Ilchenko 		printf("Error: failed to stop port %u: %s\n",
4230ead65afSIvan Ilchenko 		       rxtx_porte, rte_strerror(-ret));
424a9de470cSBruce Richardson 
425a9de470cSBruce Richardson 	return TEST_SUCCESS;
426a9de470cSBruce Richardson }
427a9de470cSBruce Richardson 
428a9de470cSBruce Richardson static void
test_cleanup_resources(void)429a9de470cSBruce Richardson test_cleanup_resources(void)
430a9de470cSBruce Richardson {
4310ead65afSIvan Ilchenko 	int itr, ret;
432a9de470cSBruce Richardson 	for (itr = 0; itr < NUM_RINGS; itr++)
433a9de470cSBruce Richardson 		rte_ring_free(rxtx[itr]);
434a9de470cSBruce Richardson 
4350ead65afSIvan Ilchenko 	ret = rte_eth_dev_stop(tx_porta);
4360ead65afSIvan Ilchenko 	if (ret != 0)
4370ead65afSIvan Ilchenko 		printf("Error: failed to stop port %u: %s\n",
4380ead65afSIvan Ilchenko 		       tx_porta, rte_strerror(-ret));
4390ead65afSIvan Ilchenko 	ret = rte_eth_dev_stop(rx_portb);
4400ead65afSIvan Ilchenko 	if (ret != 0)
4410ead65afSIvan Ilchenko 		printf("Error: failed to stop port %u: %s\n",
4420ead65afSIvan Ilchenko 		       rx_portb, rte_strerror(-ret));
4430ead65afSIvan Ilchenko 	ret = rte_eth_dev_stop(rxtx_portc);
4440ead65afSIvan Ilchenko 	if (ret != 0)
4450ead65afSIvan Ilchenko 		printf("Error: failed to stop port %u: %s\n",
4460ead65afSIvan Ilchenko 		       rxtx_portc, rte_strerror(-ret));
447a9de470cSBruce Richardson 
448a9de470cSBruce Richardson 	rte_mempool_free(mp);
449a9de470cSBruce Richardson 	rte_vdev_uninit("net_ring_net_ringa");
450a9de470cSBruce Richardson 	rte_vdev_uninit("net_ring_net_ringb");
451a9de470cSBruce Richardson 	rte_vdev_uninit("net_ring_net_ringc");
452a9de470cSBruce Richardson 	rte_vdev_uninit("net_ring_net_ringd");
453a9de470cSBruce Richardson 	rte_vdev_uninit("net_ring_net_ringe");
454a9de470cSBruce Richardson }
455a9de470cSBruce Richardson 
456a9de470cSBruce Richardson static int
test_pmd_ringcreate_setup(void)457a9de470cSBruce Richardson test_pmd_ringcreate_setup(void)
458a9de470cSBruce Richardson {
459a9de470cSBruce Richardson 	uint8_t nb_ports;
460a9de470cSBruce Richardson 
461a9de470cSBruce Richardson 	nb_ports = rte_eth_dev_count_avail();
462a9de470cSBruce Richardson 	printf("nb_ports=%d\n", (int)nb_ports);
463a9de470cSBruce Richardson 
464a9de470cSBruce Richardson 	/*  create the rings and eth_rings in the test code.
465a9de470cSBruce Richardson 	 *  This does not test the rte_pmd_ring_devinit function.
466a9de470cSBruce Richardson 	 *
467a9de470cSBruce Richardson 	 *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
468a9de470cSBruce Richardson 	 */
469a9de470cSBruce Richardson 	rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
470a9de470cSBruce Richardson 	if (rxtx[0] == NULL) {
471a9de470cSBruce Richardson 		printf("rte_ring_create R0 failed");
472a9de470cSBruce Richardson 		return -1;
473a9de470cSBruce Richardson 	}
474a9de470cSBruce Richardson 
475a9de470cSBruce Richardson 	rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
476a9de470cSBruce Richardson 	if (rxtx[1] == NULL) {
477a9de470cSBruce Richardson 		printf("rte_ring_create R1 failed");
478a9de470cSBruce Richardson 		return -1;
479a9de470cSBruce Richardson 	}
480a9de470cSBruce Richardson 
481a9de470cSBruce Richardson 	tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
482a9de470cSBruce Richardson 	rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
483a9de470cSBruce Richardson 	rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
484a9de470cSBruce Richardson 	rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
485a9de470cSBruce Richardson 	rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
486a9de470cSBruce Richardson 
487a9de470cSBruce Richardson 	printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
488a9de470cSBruce Richardson 			tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
489a9de470cSBruce Richardson 
490a9de470cSBruce Richardson 	if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
491a9de470cSBruce Richardson 			|| (rxtx_portd == -1) || (rxtx_porte == -1)) {
492a9de470cSBruce Richardson 		printf("rte_eth_from rings failed\n");
493a9de470cSBruce Richardson 		return -1;
494a9de470cSBruce Richardson 	}
495a9de470cSBruce Richardson 
496a9de470cSBruce Richardson 	mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
497a9de470cSBruce Richardson 			0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
498a9de470cSBruce Richardson 	if (mp == NULL)
499a9de470cSBruce Richardson 		return -1;
500a9de470cSBruce Richardson 
501a9de470cSBruce Richardson 	if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
502a9de470cSBruce Richardson 			|| (rxtx_portc >= RTE_MAX_ETHPORTS)
503a9de470cSBruce Richardson 			|| (rxtx_portd >= RTE_MAX_ETHPORTS)
504a9de470cSBruce Richardson 			|| (rxtx_porte >= RTE_MAX_ETHPORTS)) {
505a9de470cSBruce Richardson 		printf(" port exceed max eth ports\n");
506a9de470cSBruce Richardson 		return -1;
507a9de470cSBruce Richardson 	}
508a9de470cSBruce Richardson 	return 0;
509a9de470cSBruce Richardson }
510a9de470cSBruce Richardson 
511a9de470cSBruce Richardson static int
test_command_line_ring_port(void)512a9de470cSBruce Richardson test_command_line_ring_port(void)
513a9de470cSBruce Richardson {
514a9de470cSBruce Richardson 	int port, cmdl_port0 = -1;
51577339255SIvan Ilchenko 	int ret;
51677339255SIvan Ilchenko 
517a9de470cSBruce Richardson 	/* find a port created with the --vdev=net_ring0 command line option */
518a9de470cSBruce Richardson 	RTE_ETH_FOREACH_DEV(port) {
519a9de470cSBruce Richardson 		struct rte_eth_dev_info dev_info;
52077339255SIvan Ilchenko 
52177339255SIvan Ilchenko 		ret = rte_eth_dev_info_get(port, &dev_info);
52277339255SIvan Ilchenko 		TEST_ASSERT((ret == 0),
52377339255SIvan Ilchenko 				"Error during getting device (port %d) info: %s\n",
52477339255SIvan Ilchenko 				port, strerror(-ret));
52577339255SIvan Ilchenko 
526a9de470cSBruce Richardson 		if (!strcmp(dev_info.driver_name, "Rings PMD")) {
527a9de470cSBruce Richardson 			printf("found a command line ring port=%d\n", port);
528a9de470cSBruce Richardson 			cmdl_port0 = port;
529a9de470cSBruce Richardson 			break;
530a9de470cSBruce Richardson 		}
531a9de470cSBruce Richardson 	}
532a9de470cSBruce Richardson 	if (cmdl_port0 != -1) {
533a9de470cSBruce Richardson 		TEST_ASSERT((test_ethdev_configure_port(cmdl_port0) < 0),
534a9de470cSBruce Richardson 				"test ethdev configure port cmdl_port0 is failed");
535a9de470cSBruce Richardson 		TEST_ASSERT((test_send_basic_packets_port(cmdl_port0) < 0),
536a9de470cSBruce Richardson 				"test send basic packets port cmdl_port0 is failed");
537a9de470cSBruce Richardson 		TEST_ASSERT((test_stats_reset(cmdl_port0) < 0),
538a9de470cSBruce Richardson 				"test stats reset cmdl_port0 is failed");
539a9de470cSBruce Richardson 		TEST_ASSERT((test_get_stats(cmdl_port0) < 0),
540a9de470cSBruce Richardson 				"test get stats cmdl_port0 is failed");
5410ead65afSIvan Ilchenko 		TEST_ASSERT((rte_eth_dev_stop(cmdl_port0) == 0),
5420ead65afSIvan Ilchenko 				"test stop cmdl_port0 is failed");
543a9de470cSBruce Richardson 	}
544a9de470cSBruce Richardson 	return TEST_SUCCESS;
545a9de470cSBruce Richardson }
546a9de470cSBruce Richardson 
547a9de470cSBruce Richardson static int
test_ethdev_configure_ports(void)548a9de470cSBruce Richardson test_ethdev_configure_ports(void)
549a9de470cSBruce Richardson {
550a9de470cSBruce Richardson 	TEST_ASSERT((test_ethdev_configure_port(tx_porta) == 0),
551a9de470cSBruce Richardson 			"test ethdev configure ports tx_porta is failed");
552a9de470cSBruce Richardson 	TEST_ASSERT((test_ethdev_configure_port(rx_portb) == 0),
553a9de470cSBruce Richardson 			"test ethdev configure ports rx_portb is failed");
554a9de470cSBruce Richardson 	TEST_ASSERT((test_ethdev_configure_port(rxtx_portc) == 0),
555a9de470cSBruce Richardson 			"test ethdev configure ports rxtx_portc is failed");
556a9de470cSBruce Richardson 
557a9de470cSBruce Richardson 	return TEST_SUCCESS;
558a9de470cSBruce Richardson }
559a9de470cSBruce Richardson 
560a9de470cSBruce Richardson static int
test_get_stats_for_port(void)561a9de470cSBruce Richardson test_get_stats_for_port(void)
562a9de470cSBruce Richardson {
563a9de470cSBruce Richardson 	TEST_ASSERT(test_get_stats(rxtx_portc) == 0, "test get stats failed");
564a9de470cSBruce Richardson 	return TEST_SUCCESS;
565a9de470cSBruce Richardson }
566a9de470cSBruce Richardson 
567a9de470cSBruce Richardson static int
test_stats_reset_for_port(void)568a9de470cSBruce Richardson test_stats_reset_for_port(void)
569a9de470cSBruce Richardson {
570a9de470cSBruce Richardson 	TEST_ASSERT(test_stats_reset(rxtx_portc) == 0, "test stats reset failed");
571a9de470cSBruce Richardson 	return TEST_SUCCESS;
572a9de470cSBruce Richardson }
573a9de470cSBruce Richardson 
574a9de470cSBruce Richardson static struct
575a9de470cSBruce Richardson unit_test_suite test_pmd_ring_suite  = {
576a9de470cSBruce Richardson 	.setup = test_pmd_ringcreate_setup,
577a9de470cSBruce Richardson 	.teardown = test_cleanup_resources,
578a9de470cSBruce Richardson 	.suite_name = "Test Pmd Ring Unit Test Suite",
579a9de470cSBruce Richardson 	.unit_test_cases = {
580a9de470cSBruce Richardson 		TEST_CASE(test_ethdev_configure_ports),
581a9de470cSBruce Richardson 		TEST_CASE(test_send_basic_packets),
582a9de470cSBruce Richardson 		TEST_CASE(test_get_stats_for_port),
583a9de470cSBruce Richardson 		TEST_CASE(test_stats_reset_for_port),
584a9de470cSBruce Richardson 		TEST_CASE(test_pmd_ring_pair_create_attach),
585a9de470cSBruce Richardson 		TEST_CASE(test_command_line_ring_port),
586a9de470cSBruce Richardson 		TEST_CASES_END()
587a9de470cSBruce Richardson 	}
588a9de470cSBruce Richardson };
589a9de470cSBruce Richardson 
590a9de470cSBruce Richardson static int
test_pmd_ring(void)591a9de470cSBruce Richardson test_pmd_ring(void)
592a9de470cSBruce Richardson {
593a9de470cSBruce Richardson 	return unit_test_suite_runner(&test_pmd_ring_suite);
594a9de470cSBruce Richardson }
595a9de470cSBruce Richardson 
596*d83fb967SDavid Marchand REGISTER_FAST_TEST(ring_pmd_autotest, true, true, test_pmd_ring);
597