New upstream version 18.02
[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         if ((rte_eth_dev_configure(portd, 1, 1, &null_conf) < 0)
222                 || (rte_eth_dev_configure(porte, 1, 1, &null_conf) < 0)) {
223                 printf("Configure failed for port\n");
224                 return -1;
225         }
226
227         if ((rte_eth_tx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL) < 0)
228                 || (rte_eth_tx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
229                 printf("TX queue setup failed\n");
230                 return -1;
231         }
232
233         if ((rte_eth_rx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
234                 || (rte_eth_rx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
235                 printf("RX queue setup failed\n");
236                 return -1;
237         }
238
239         if ((rte_eth_dev_start(portd) < 0)
240                 || (rte_eth_dev_start(porte) < 0)) {
241                 printf("Error starting port\n");
242                 return -1;
243         }
244
245         rte_eth_stats_reset(portd);
246         /* check stats of port, should all be zero */
247         rte_eth_stats_get(portd, &stats);
248         if (stats.ipackets != 0 || stats.opackets != 0 ||
249                         stats.ibytes != 0 || stats.obytes != 0 ||
250                         stats.ierrors != 0 || stats.oerrors != 0) {
251                 printf("Error: port %d stats are not zero\n", portd);
252                 return -1;
253         }
254
255         rte_eth_stats_reset(porte);
256         /* check stats of port, should all be zero */
257         rte_eth_stats_get(porte, &stats2);
258         if (stats2.ipackets != 0 || stats2.opackets != 0 ||
259                         stats2.ibytes != 0 || stats2.obytes != 0 ||
260                         stats2.ierrors != 0 || stats2.oerrors != 0) {
261                 printf("Error: port %d stats are not zero\n", porte);
262                 return -1;
263         }
264
265         /*
266          * send and receive 1 packet (portd -> porte)
267          * and check for stats update
268          */
269         printf("Testing send and receive 1 packet (portd -> porte)\n");
270         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
271                 printf("Error sending packet to port %d\n", portd);
272                 return -1;
273         }
274
275         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
276                 printf("Error receiving packet from port %d\n", porte);
277                 return -1;
278         }
279
280         rte_eth_stats_get(portd, &stats);
281         rte_eth_stats_get(porte, &stats2);
282         if (stats.ipackets != 0 || stats.opackets != 1 ||
283                         stats.ibytes != 0 || stats.obytes != 0 ||
284                         stats.ierrors != 0 || stats.oerrors != 0) {
285                 printf("Error: port %d stats are not as expected\n", portd);
286                 return -1;
287         }
288
289         if (stats2.ipackets != 1 || stats2.opackets != 0 ||
290                         stats2.ibytes != 0 || stats2.obytes != 0 ||
291                         stats2.ierrors != 0 || stats2.oerrors != 0) {
292                 printf("Error: port %d stats are not as expected\n", porte);
293                 return -1;
294         }
295
296         /*
297          * send and receive 1 packet (porte -> portd)
298          * and check for stats update
299          */
300         printf("Testing send and receive 1 packet (porte -> portd)\n");
301         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
302                 printf("Error sending packet to port %d\n", porte);
303                 return -1;
304         }
305
306         if (rte_eth_rx_burst(portd, 0, &pbuf, 1) != 1) {
307                 printf("Error receiving packet from port %d\n", portd);
308                 return -1;
309         }
310
311         rte_eth_stats_get(portd, &stats);
312         rte_eth_stats_get(porte, &stats2);
313         if (stats.ipackets != 1 || stats.opackets != 1 ||
314                         stats.ibytes != 0 || stats.obytes != 0 ||
315                         stats.ierrors != 0 || stats.oerrors != 0) {
316                 printf("Error: port %d stats are not as expected\n", portd);
317                 return -1;
318         }
319
320         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
321                         stats2.ibytes != 0 || stats2.obytes != 0 ||
322                         stats2.ierrors != 0 || stats2.oerrors != 0) {
323                 printf("Error: port %d stats are not as expected\n", porte);
324                 return -1;
325         }
326
327         /*
328          * send and receive 1 packet (portd -> portd)
329          * and check for stats update
330          */
331         printf("Testing send and receive 1 packet (portd -> portd)\n");
332         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
333                 printf("Error sending packet to port %d\n", portd);
334                 return -1;
335         }
336
337         if (rte_eth_rx_burst(portd, 0, &pbuf, 1) != 1) {
338                 printf("Error receiving packet from port %d\n", porte);
339                 return -1;
340         }
341
342         rte_eth_stats_get(portd, &stats);
343         rte_eth_stats_get(porte, &stats2);
344         if (stats.ipackets != 2 || stats.opackets != 2 ||
345                         stats.ibytes != 0 || stats.obytes != 0 ||
346                         stats.ierrors != 0 || stats.oerrors != 0) {
347                 printf("Error: port %d stats are not as expected\n", portd);
348                 return -1;
349         }
350
351         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
352                         stats2.ibytes != 0 || stats2.obytes != 0 ||
353                         stats2.ierrors != 0 || stats2.oerrors != 0) {
354                 printf("Error: port %d stats are not as expected\n", porte);
355                 return -1;
356         }
357
358         /*
359          * send and receive 1 packet (porte -> porte)
360          * and check for stats update
361          */
362         printf("Testing send and receive 1 packet (porte -> porte)\n");
363         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
364                 printf("Error sending packet to port %d\n", porte);
365                 return -1;
366         }
367
368         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
369                 printf("Error receiving packet from port %d\n", porte);
370                 return -1;
371         }
372
373         rte_eth_stats_get(portd, &stats);
374         rte_eth_stats_get(porte, &stats2);
375         if (stats.ipackets != 2 || stats.opackets != 2 ||
376                         stats.ibytes != 0 || stats.obytes != 0 ||
377                         stats.ierrors != 0 || stats.oerrors != 0) {
378                 printf("Error: port %d stats are not as expected\n", portd);
379                 return -1;
380         }
381
382         if (stats2.ipackets != 2 || stats2.opackets != 2 ||
383                         stats2.ibytes != 0 || stats2.obytes != 0 ||
384                         stats2.ierrors != 0 || stats2.oerrors != 0) {
385                 printf("Error: port %d stats are not as expected\n", porte);
386                 return -1;
387         }
388
389         rte_eth_dev_stop(portd);
390         rte_eth_dev_stop(porte);
391
392         return 0;
393 }
394
395 static int
396 test_pmd_ring(void)
397 {
398         struct rte_ring *rxtx[NUM_RINGS];
399         int port, cmdl_port0 = -1;
400         uint8_t nb_ports;
401
402         nb_ports = rte_eth_dev_count();
403         printf("nb_ports=%d\n", (int)nb_ports);
404
405         /*  create the rings and eth_rings in the test code.
406          *  This does not test the rte_pmd_ring_devinit function.
407          *
408          *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
409          */
410         rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
411         if (rxtx[0] == NULL) {
412                 printf("rte_ring_create R0 failed");
413                 return -1;
414         }
415
416         rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
417         if (rxtx[1] == NULL) {
418                 printf("rte_ring_create R1 failed");
419                 return -1;
420         }
421
422         tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
423         rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
424         rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
425         rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
426         rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
427
428         printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
429                         tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
430
431         if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
432                 || (rxtx_portd == -1) || (rxtx_porte == -1)) {
433                 printf("rte_eth_from rings failed\n");
434                 return -1;
435         }
436
437         mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
438                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
439         if (mp == NULL)
440                 return -1;
441
442         if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
443                 || (rxtx_portc >= RTE_MAX_ETHPORTS)
444                 || (rxtx_portd >= RTE_MAX_ETHPORTS)
445                 || (rxtx_porte >= RTE_MAX_ETHPORTS)) {
446                 printf(" port exceed max eth ports\n");
447                 return -1;
448         }
449
450         if (test_ethdev_configure_port(tx_porta) < 0)
451                 return -1;
452
453         if (test_ethdev_configure_port(rx_portb) < 0)
454                 return -1;
455
456         if (test_ethdev_configure_port(rxtx_portc) < 0)
457                 return -1;
458
459         if (test_send_basic_packets() < 0)
460                 return -1;
461
462         if (test_get_stats(rxtx_portc) < 0)
463                 return -1;
464
465         if (test_stats_reset(rxtx_portc) < 0)
466                 return -1;
467
468         rte_eth_dev_stop(tx_porta);
469         rte_eth_dev_stop(rx_portb);
470         rte_eth_dev_stop(rxtx_portc);
471
472         if (test_pmd_ring_pair_create_attach(rxtx_portd, rxtx_porte) < 0)
473                 return -1;
474
475         /* find a port created with the --vdev=net_ring0 command line option */
476         for (port = 0; port < nb_ports; port++) {
477                 struct rte_eth_dev_info dev_info;
478
479                 rte_eth_dev_info_get(port, &dev_info);
480                 if (!strcmp(dev_info.driver_name, "Rings PMD")) {
481                         printf("found a command line ring port=%d\n", port);
482                         cmdl_port0 = port;
483                         break;
484                 }
485         }
486         if (cmdl_port0 != -1) {
487                 if (test_ethdev_configure_port(cmdl_port0) < 0)
488                         return -1;
489                 if (test_send_basic_packets_port(cmdl_port0) < 0)
490                         return -1;
491                 if (test_stats_reset(cmdl_port0) < 0)
492                         return -1;
493                 if (test_get_stats(cmdl_port0) < 0)
494                         return -1;
495                 rte_eth_dev_stop(cmdl_port0);
496         }
497         return 0;
498 }
499
500 REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);