New upstream version 18.08
[deb_dpdk.git] / test / test / test_pmd_ring.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4 #include "test.h"
5
6 #include <stdio.h>
7
8 #include <rte_eth_ring.h>
9 #include <rte_ethdev.h>
10
11 static struct rte_mempool *mp;
12 static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;
13
14 #define SOCKET0 0
15 #define RING_SIZE 256
16 #define NUM_RINGS 2
17 #define NB_MBUF 512
18
19
20 static int
21 test_ethdev_configure_port(int port)
22 {
23         struct rte_eth_conf null_conf;
24         struct rte_eth_link link;
25
26         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
27
28         if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
29                 printf("Configure failed for port %d\n", port);
30                 return -1;
31         }
32
33         /* Test queue release */
34         if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
35                 printf("Configure failed for port %d\n", port);
36                 return -1;
37         }
38
39         if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
40                 printf("TX queue setup failed port %d\n", port);
41                 return -1;
42         }
43
44         if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
45                         NULL, mp) < 0) {
46                 printf("RX queue setup failed port %d\n", port);
47                 return -1;
48         }
49
50         if (rte_eth_dev_start(port) < 0) {
51                 printf("Error starting port %d\n", port);
52                 return -1;
53         }
54
55         rte_eth_link_get(port, &link);
56
57         return 0;
58 }
59
60 static int
61 test_send_basic_packets(void)
62 {
63         struct rte_mbuf  bufs[RING_SIZE];
64         struct rte_mbuf *pbufs[RING_SIZE];
65         int i;
66
67         printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");
68
69         for (i = 0; i < RING_SIZE/2; i++)
70                 pbufs[i] = &bufs[i];
71
72         if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
73                 printf("Failed to transmit packet burst port %d\n", tx_porta);
74                 return -1;
75         }
76
77         if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
78                 printf("Failed to receive packet burst on port %d\n", rx_portb);
79                 return -1;
80         }
81
82         for (i = 0; i < RING_SIZE/2; i++)
83                 if (pbufs[i] != &bufs[i]) {
84                         printf("Error: received data does not match that transmitted\n");
85                         return -1;
86                 }
87
88         return 0;
89 }
90
91 static int
92 test_send_basic_packets_port(int port)
93 {
94         struct rte_mbuf  bufs[RING_SIZE];
95         struct rte_mbuf *pbufs[RING_SIZE];
96         int i;
97
98         printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");
99
100         for (i = 0; i < RING_SIZE/2; i++)
101                 pbufs[i] = &bufs[i];
102
103         if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
104                 printf("Failed to transmit packet burst port %d\n", port);
105                 return -1;
106         }
107
108         if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
109                 printf("Failed to receive packet burst on port %d\n", port);
110                 return -1;
111         }
112
113         for (i = 0; i < RING_SIZE/2; i++)
114                 if (pbufs[i] != &bufs[i]) {
115                         printf("Error: received data does not match that transmitted\n");
116                         return -1;
117                 }
118
119         return 0;
120 }
121
122
123 static int
124 test_get_stats(int port)
125 {
126         struct rte_eth_stats stats;
127         struct rte_mbuf buf, *pbuf = &buf;
128
129         printf("Testing ring PMD stats_get port %d\n", port);
130
131         /* check stats of RXTX port, should all be zero */
132
133         rte_eth_stats_get(port, &stats);
134         if (stats.ipackets != 0 || stats.opackets != 0 ||
135                         stats.ibytes != 0 || stats.obytes != 0 ||
136                         stats.ierrors != 0 || stats.oerrors != 0) {
137                 printf("Error: port %d stats are not zero\n", port);
138                 return -1;
139         }
140
141         /* send and receive 1 packet and check for stats update */
142         if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
143                 printf("Error sending packet to port %d\n", port);
144                 return -1;
145         }
146
147         if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
148                 printf("Error receiving packet from port %d\n", port);
149                 return -1;
150         }
151
152         rte_eth_stats_get(port, &stats);
153         if (stats.ipackets != 1 || stats.opackets != 1 ||
154                         stats.ibytes != 0 || stats.obytes != 0 ||
155                         stats.ierrors != 0 || stats.oerrors != 0) {
156                 printf("Error: port %d stats are not as expected\n", port);
157                 return -1;
158         }
159         return 0;
160 }
161
162 static int
163 test_stats_reset(int port)
164 {
165         struct rte_eth_stats stats;
166         struct rte_mbuf buf, *pbuf = &buf;
167
168         printf("Testing ring PMD stats_reset port %d\n", port);
169
170         rte_eth_stats_reset(port);
171
172         /* check stats of RXTX port, should all be zero */
173         rte_eth_stats_get(port, &stats);
174         if (stats.ipackets != 0 || stats.opackets != 0 ||
175                         stats.ibytes != 0 || stats.obytes != 0 ||
176                         stats.ierrors != 0 || stats.oerrors != 0) {
177                 printf("Error: port %d stats are not zero\n", port);
178                 return -1;
179         }
180
181         /* send and receive 1 packet and check for stats update */
182         if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
183                 printf("Error sending packet to port %d\n", port);
184                 return -1;
185         }
186
187         if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
188                 printf("Error receiving packet from port %d\n", port);
189                 return -1;
190         }
191
192         rte_eth_stats_get(port, &stats);
193         if (stats.ipackets != 1 || stats.opackets != 1 ||
194                         stats.ibytes != 0 || stats.obytes != 0 ||
195                         stats.ierrors != 0 || stats.oerrors != 0) {
196                 printf("Error: port %d stats are not as expected\n", port);
197                 return -1;
198         }
199
200         rte_eth_stats_reset(port);
201
202         /* check stats of RXTX port, should all be zero */
203         rte_eth_stats_get(port, &stats);
204         if (stats.ipackets != 0 || stats.opackets != 0 ||
205                         stats.ibytes != 0 || stats.obytes != 0 ||
206                         stats.ierrors != 0 || stats.oerrors != 0) {
207                 printf("Error: port %d stats are not zero\n", port);
208                 return -1;
209         }
210
211         return 0;
212 }
213
214 static int
215 test_pmd_ring_pair_create_attach(int portd, int porte)
216 {
217         struct rte_eth_stats stats, stats2;
218         struct rte_mbuf buf, *pbuf = &buf;
219         struct rte_eth_conf null_conf;
220
221         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
222
223         if ((rte_eth_dev_configure(portd, 1, 1, &null_conf) < 0)
224                 || (rte_eth_dev_configure(porte, 1, 1, &null_conf) < 0)) {
225                 printf("Configure failed for port\n");
226                 return -1;
227         }
228
229         if ((rte_eth_tx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL) < 0)
230                 || (rte_eth_tx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
231                 printf("TX queue setup failed\n");
232                 return -1;
233         }
234
235         if ((rte_eth_rx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
236                 || (rte_eth_rx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
237                 printf("RX queue setup failed\n");
238                 return -1;
239         }
240
241         if ((rte_eth_dev_start(portd) < 0)
242                 || (rte_eth_dev_start(porte) < 0)) {
243                 printf("Error starting port\n");
244                 return -1;
245         }
246
247         rte_eth_stats_reset(portd);
248         /* check stats of port, should all be zero */
249         rte_eth_stats_get(portd, &stats);
250         if (stats.ipackets != 0 || stats.opackets != 0 ||
251                         stats.ibytes != 0 || stats.obytes != 0 ||
252                         stats.ierrors != 0 || stats.oerrors != 0) {
253                 printf("Error: port %d stats are not zero\n", portd);
254                 return -1;
255         }
256
257         rte_eth_stats_reset(porte);
258         /* check stats of port, should all be zero */
259         rte_eth_stats_get(porte, &stats2);
260         if (stats2.ipackets != 0 || stats2.opackets != 0 ||
261                         stats2.ibytes != 0 || stats2.obytes != 0 ||
262                         stats2.ierrors != 0 || stats2.oerrors != 0) {
263                 printf("Error: port %d stats are not zero\n", porte);
264                 return -1;
265         }
266
267         /*
268          * send and receive 1 packet (portd -> porte)
269          * and check for stats update
270          */
271         printf("Testing send and receive 1 packet (portd -> porte)\n");
272         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
273                 printf("Error sending packet to port %d\n", portd);
274                 return -1;
275         }
276
277         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
278                 printf("Error receiving packet from port %d\n", porte);
279                 return -1;
280         }
281
282         rte_eth_stats_get(portd, &stats);
283         rte_eth_stats_get(porte, &stats2);
284         if (stats.ipackets != 0 || stats.opackets != 1 ||
285                         stats.ibytes != 0 || stats.obytes != 0 ||
286                         stats.ierrors != 0 || stats.oerrors != 0) {
287                 printf("Error: port %d stats are not as expected\n", portd);
288                 return -1;
289         }
290
291         if (stats2.ipackets != 1 || stats2.opackets != 0 ||
292                         stats2.ibytes != 0 || stats2.obytes != 0 ||
293                         stats2.ierrors != 0 || stats2.oerrors != 0) {
294                 printf("Error: port %d stats are not as expected\n", porte);
295                 return -1;
296         }
297
298         /*
299          * send and receive 1 packet (porte -> portd)
300          * and check for stats update
301          */
302         printf("Testing send and receive 1 packet (porte -> portd)\n");
303         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
304                 printf("Error sending packet to port %d\n", porte);
305                 return -1;
306         }
307
308         if (rte_eth_rx_burst(portd, 0, &pbuf, 1) != 1) {
309                 printf("Error receiving packet from port %d\n", portd);
310                 return -1;
311         }
312
313         rte_eth_stats_get(portd, &stats);
314         rte_eth_stats_get(porte, &stats2);
315         if (stats.ipackets != 1 || stats.opackets != 1 ||
316                         stats.ibytes != 0 || stats.obytes != 0 ||
317                         stats.ierrors != 0 || stats.oerrors != 0) {
318                 printf("Error: port %d stats are not as expected\n", portd);
319                 return -1;
320         }
321
322         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
323                         stats2.ibytes != 0 || stats2.obytes != 0 ||
324                         stats2.ierrors != 0 || stats2.oerrors != 0) {
325                 printf("Error: port %d stats are not as expected\n", porte);
326                 return -1;
327         }
328
329         /*
330          * send and receive 1 packet (portd -> portd)
331          * and check for stats update
332          */
333         printf("Testing send and receive 1 packet (portd -> portd)\n");
334         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
335                 printf("Error sending packet to port %d\n", portd);
336                 return -1;
337         }
338
339         if (rte_eth_rx_burst(portd, 0, &pbuf, 1) != 1) {
340                 printf("Error receiving packet from port %d\n", porte);
341                 return -1;
342         }
343
344         rte_eth_stats_get(portd, &stats);
345         rte_eth_stats_get(porte, &stats2);
346         if (stats.ipackets != 2 || stats.opackets != 2 ||
347                         stats.ibytes != 0 || stats.obytes != 0 ||
348                         stats.ierrors != 0 || stats.oerrors != 0) {
349                 printf("Error: port %d stats are not as expected\n", portd);
350                 return -1;
351         }
352
353         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
354                         stats2.ibytes != 0 || stats2.obytes != 0 ||
355                         stats2.ierrors != 0 || stats2.oerrors != 0) {
356                 printf("Error: port %d stats are not as expected\n", porte);
357                 return -1;
358         }
359
360         /*
361          * send and receive 1 packet (porte -> porte)
362          * and check for stats update
363          */
364         printf("Testing send and receive 1 packet (porte -> porte)\n");
365         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
366                 printf("Error sending packet to port %d\n", porte);
367                 return -1;
368         }
369
370         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
371                 printf("Error receiving packet from port %d\n", porte);
372                 return -1;
373         }
374
375         rte_eth_stats_get(portd, &stats);
376         rte_eth_stats_get(porte, &stats2);
377         if (stats.ipackets != 2 || stats.opackets != 2 ||
378                         stats.ibytes != 0 || stats.obytes != 0 ||
379                         stats.ierrors != 0 || stats.oerrors != 0) {
380                 printf("Error: port %d stats are not as expected\n", portd);
381                 return -1;
382         }
383
384         if (stats2.ipackets != 2 || stats2.opackets != 2 ||
385                         stats2.ibytes != 0 || stats2.obytes != 0 ||
386                         stats2.ierrors != 0 || stats2.oerrors != 0) {
387                 printf("Error: port %d stats are not as expected\n", porte);
388                 return -1;
389         }
390
391         rte_eth_dev_stop(portd);
392         rte_eth_dev_stop(porte);
393
394         return 0;
395 }
396
397 static int
398 test_pmd_ring(void)
399 {
400         struct rte_ring *rxtx[NUM_RINGS];
401         int port, cmdl_port0 = -1;
402         uint8_t nb_ports;
403
404         nb_ports = rte_eth_dev_count_avail();
405         printf("nb_ports=%d\n", (int)nb_ports);
406
407         /*  create the rings and eth_rings in the test code.
408          *  This does not test the rte_pmd_ring_devinit function.
409          *
410          *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
411          */
412         rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
413         if (rxtx[0] == NULL) {
414                 printf("rte_ring_create R0 failed");
415                 return -1;
416         }
417
418         rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
419         if (rxtx[1] == NULL) {
420                 printf("rte_ring_create R1 failed");
421                 return -1;
422         }
423
424         tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
425         rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
426         rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
427         rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
428         rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
429
430         printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
431                         tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
432
433         if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
434                 || (rxtx_portd == -1) || (rxtx_porte == -1)) {
435                 printf("rte_eth_from rings failed\n");
436                 return -1;
437         }
438
439         mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
440                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
441         if (mp == NULL)
442                 return -1;
443
444         if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
445                 || (rxtx_portc >= RTE_MAX_ETHPORTS)
446                 || (rxtx_portd >= RTE_MAX_ETHPORTS)
447                 || (rxtx_porte >= RTE_MAX_ETHPORTS)) {
448                 printf(" port exceed max eth ports\n");
449                 return -1;
450         }
451
452         if (test_ethdev_configure_port(tx_porta) < 0)
453                 return -1;
454
455         if (test_ethdev_configure_port(rx_portb) < 0)
456                 return -1;
457
458         if (test_ethdev_configure_port(rxtx_portc) < 0)
459                 return -1;
460
461         if (test_send_basic_packets() < 0)
462                 return -1;
463
464         if (test_get_stats(rxtx_portc) < 0)
465                 return -1;
466
467         if (test_stats_reset(rxtx_portc) < 0)
468                 return -1;
469
470         rte_eth_dev_stop(tx_porta);
471         rte_eth_dev_stop(rx_portb);
472         rte_eth_dev_stop(rxtx_portc);
473
474         if (test_pmd_ring_pair_create_attach(rxtx_portd, rxtx_porte) < 0)
475                 return -1;
476
477         /* find a port created with the --vdev=net_ring0 command line option */
478         RTE_ETH_FOREACH_DEV(port) {
479                 struct rte_eth_dev_info dev_info;
480
481                 rte_eth_dev_info_get(port, &dev_info);
482                 if (!strcmp(dev_info.driver_name, "Rings PMD")) {
483                         printf("found a command line ring port=%d\n", port);
484                         cmdl_port0 = port;
485                         break;
486                 }
487         }
488         if (cmdl_port0 != -1) {
489                 if (test_ethdev_configure_port(cmdl_port0) < 0)
490                         return -1;
491                 if (test_send_basic_packets_port(cmdl_port0) < 0)
492                         return -1;
493                 if (test_stats_reset(cmdl_port0) < 0)
494                         return -1;
495                 if (test_get_stats(cmdl_port0) < 0)
496                         return -1;
497                 rte_eth_dev_stop(cmdl_port0);
498         }
499         return 0;
500 }
501
502 REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);