New upstream version 17.11.4
[deb_dpdk.git] / test / test / test_pmd_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include "test.h"
34
35 #include <stdio.h>
36
37 #include <rte_eth_ring.h>
38 #include <rte_ethdev.h>
39
40 static struct rte_mempool *mp;
41 static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;
42
43 #define SOCKET0 0
44 #define RING_SIZE 256
45 #define NUM_RINGS 2
46 #define NB_MBUF 512
47
48
49 static int
50 test_ethdev_configure_port(int port)
51 {
52         struct rte_eth_conf null_conf;
53         struct rte_eth_link link;
54
55         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
56
57         if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
58                 printf("Configure failed for port %d\n", port);
59                 return -1;
60         }
61
62         /* Test queue release */
63         if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
64                 printf("Configure failed for port %d\n", port);
65                 return -1;
66         }
67
68         if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
69                 printf("TX queue setup failed port %d\n", port);
70                 return -1;
71         }
72
73         if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
74                         NULL, mp) < 0) {
75                 printf("RX queue setup failed port %d\n", port);
76                 return -1;
77         }
78
79         if (rte_eth_dev_start(port) < 0) {
80                 printf("Error starting port %d\n", port);
81                 return -1;
82         }
83
84         rte_eth_link_get(port, &link);
85
86         return 0;
87 }
88
89 static int
90 test_send_basic_packets(void)
91 {
92         struct rte_mbuf  bufs[RING_SIZE];
93         struct rte_mbuf *pbufs[RING_SIZE];
94         int i;
95
96         printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");
97
98         for (i = 0; i < RING_SIZE/2; i++)
99                 pbufs[i] = &bufs[i];
100
101         if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
102                 printf("Failed to transmit packet burst port %d\n", tx_porta);
103                 return -1;
104         }
105
106         if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
107                 printf("Failed to receive packet burst on port %d\n", rx_portb);
108                 return -1;
109         }
110
111         for (i = 0; i < RING_SIZE/2; i++)
112                 if (pbufs[i] != &bufs[i]) {
113                         printf("Error: received data does not match that transmitted\n");
114                         return -1;
115                 }
116
117         return 0;
118 }
119
120 static int
121 test_send_basic_packets_port(int port)
122 {
123         struct rte_mbuf  bufs[RING_SIZE];
124         struct rte_mbuf *pbufs[RING_SIZE];
125         int i;
126
127         printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");
128
129         for (i = 0; i < RING_SIZE/2; i++)
130                 pbufs[i] = &bufs[i];
131
132         if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
133                 printf("Failed to transmit packet burst port %d\n", port);
134                 return -1;
135         }
136
137         if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
138                 printf("Failed to receive packet burst on port %d\n", port);
139                 return -1;
140         }
141
142         for (i = 0; i < RING_SIZE/2; i++)
143                 if (pbufs[i] != &bufs[i]) {
144                         printf("Error: received data does not match that transmitted\n");
145                         return -1;
146                 }
147
148         return 0;
149 }
150
151
152 static int
153 test_get_stats(int port)
154 {
155         struct rte_eth_stats stats;
156         struct rte_mbuf buf, *pbuf = &buf;
157
158         printf("Testing ring PMD stats_get port %d\n", port);
159
160         /* check stats of RXTX port, should all be zero */
161
162         rte_eth_stats_get(port, &stats);
163         if (stats.ipackets != 0 || stats.opackets != 0 ||
164                         stats.ibytes != 0 || stats.obytes != 0 ||
165                         stats.ierrors != 0 || stats.oerrors != 0) {
166                 printf("Error: port %d stats are not zero\n", port);
167                 return -1;
168         }
169
170         /* send and receive 1 packet and check for stats update */
171         if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
172                 printf("Error sending packet to port %d\n", port);
173                 return -1;
174         }
175
176         if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
177                 printf("Error receiving packet from port %d\n", port);
178                 return -1;
179         }
180
181         rte_eth_stats_get(port, &stats);
182         if (stats.ipackets != 1 || stats.opackets != 1 ||
183                         stats.ibytes != 0 || stats.obytes != 0 ||
184                         stats.ierrors != 0 || stats.oerrors != 0) {
185                 printf("Error: port %d stats are not as expected\n", port);
186                 return -1;
187         }
188         return 0;
189 }
190
191 static int
192 test_stats_reset(int port)
193 {
194         struct rte_eth_stats stats;
195         struct rte_mbuf buf, *pbuf = &buf;
196
197         printf("Testing ring PMD stats_reset port %d\n", port);
198
199         rte_eth_stats_reset(port);
200
201         /* check stats of RXTX port, should all be zero */
202         rte_eth_stats_get(port, &stats);
203         if (stats.ipackets != 0 || stats.opackets != 0 ||
204                         stats.ibytes != 0 || stats.obytes != 0 ||
205                         stats.ierrors != 0 || stats.oerrors != 0) {
206                 printf("Error: port %d stats are not zero\n", port);
207                 return -1;
208         }
209
210         /* send and receive 1 packet and check for stats update */
211         if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
212                 printf("Error sending packet to port %d\n", port);
213                 return -1;
214         }
215
216         if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
217                 printf("Error receiving packet from port %d\n", port);
218                 return -1;
219         }
220
221         rte_eth_stats_get(port, &stats);
222         if (stats.ipackets != 1 || stats.opackets != 1 ||
223                         stats.ibytes != 0 || stats.obytes != 0 ||
224                         stats.ierrors != 0 || stats.oerrors != 0) {
225                 printf("Error: port %d stats are not as expected\n", port);
226                 return -1;
227         }
228
229         rte_eth_stats_reset(port);
230
231         /* check stats of RXTX port, should all be zero */
232         rte_eth_stats_get(port, &stats);
233         if (stats.ipackets != 0 || stats.opackets != 0 ||
234                         stats.ibytes != 0 || stats.obytes != 0 ||
235                         stats.ierrors != 0 || stats.oerrors != 0) {
236                 printf("Error: port %d stats are not zero\n", port);
237                 return -1;
238         }
239
240         return 0;
241 }
242
243 static int
244 test_pmd_ring_pair_create_attach(int portd, int porte)
245 {
246         struct rte_eth_stats stats, stats2;
247         struct rte_mbuf buf, *pbuf = &buf;
248         struct rte_eth_conf null_conf;
249
250         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
251
252         if ((rte_eth_dev_configure(portd, 1, 1, &null_conf) < 0)
253                 || (rte_eth_dev_configure(porte, 1, 1, &null_conf) < 0)) {
254                 printf("Configure failed for port\n");
255                 return -1;
256         }
257
258         if ((rte_eth_tx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL) < 0)
259                 || (rte_eth_tx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
260                 printf("TX queue setup failed\n");
261                 return -1;
262         }
263
264         if ((rte_eth_rx_queue_setup(portd, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
265                 || (rte_eth_rx_queue_setup(porte, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
266                 printf("RX queue setup failed\n");
267                 return -1;
268         }
269
270         if ((rte_eth_dev_start(portd) < 0)
271                 || (rte_eth_dev_start(porte) < 0)) {
272                 printf("Error starting port\n");
273                 return -1;
274         }
275
276         rte_eth_stats_reset(portd);
277         /* check stats of port, should all be zero */
278         rte_eth_stats_get(portd, &stats);
279         if (stats.ipackets != 0 || stats.opackets != 0 ||
280                         stats.ibytes != 0 || stats.obytes != 0 ||
281                         stats.ierrors != 0 || stats.oerrors != 0) {
282                 printf("Error: port %d stats are not zero\n", portd);
283                 return -1;
284         }
285
286         rte_eth_stats_reset(porte);
287         /* check stats of port, should all be zero */
288         rte_eth_stats_get(porte, &stats2);
289         if (stats2.ipackets != 0 || 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 zero\n", porte);
293                 return -1;
294         }
295
296         /*
297          * send and receive 1 packet (portd -> porte)
298          * and check for stats update
299          */
300         printf("Testing send and receive 1 packet (portd -> porte)\n");
301         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
302                 printf("Error sending packet to port %d\n", portd);
303                 return -1;
304         }
305
306         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
307                 printf("Error receiving packet from port %d\n", porte);
308                 return -1;
309         }
310
311         rte_eth_stats_get(portd, &stats);
312         rte_eth_stats_get(porte, &stats2);
313         if (stats.ipackets != 0 || 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 != 0 ||
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 (porte -> portd)
329          * and check for stats update
330          */
331         printf("Testing send and receive 1 packet (porte -> portd)\n");
332         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
333                 printf("Error sending packet to port %d\n", porte);
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", portd);
339                 return -1;
340         }
341
342         rte_eth_stats_get(portd, &stats);
343         rte_eth_stats_get(porte, &stats2);
344         if (stats.ipackets != 1 || stats.opackets != 1 ||
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 (portd -> portd)
360          * and check for stats update
361          */
362         printf("Testing send and receive 1 packet (portd -> portd)\n");
363         if (rte_eth_tx_burst(portd, 0, &pbuf, 1) != 1) {
364                 printf("Error sending packet to port %d\n", portd);
365                 return -1;
366         }
367
368         if (rte_eth_rx_burst(portd, 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 != 1 || stats2.opackets != 1 ||
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         /*
390          * send and receive 1 packet (porte -> porte)
391          * and check for stats update
392          */
393         printf("Testing send and receive 1 packet (porte -> porte)\n");
394         if (rte_eth_tx_burst(porte, 0, &pbuf, 1) != 1) {
395                 printf("Error sending packet to port %d\n", porte);
396                 return -1;
397         }
398
399         if (rte_eth_rx_burst(porte, 0, &pbuf, 1) != 1) {
400                 printf("Error receiving packet from port %d\n", porte);
401                 return -1;
402         }
403
404         rte_eth_stats_get(portd, &stats);
405         rte_eth_stats_get(porte, &stats2);
406         if (stats.ipackets != 2 || stats.opackets != 2 ||
407                         stats.ibytes != 0 || stats.obytes != 0 ||
408                         stats.ierrors != 0 || stats.oerrors != 0) {
409                 printf("Error: port %d stats are not as expected\n", portd);
410                 return -1;
411         }
412
413         if (stats2.ipackets != 2 || stats2.opackets != 2 ||
414                         stats2.ibytes != 0 || stats2.obytes != 0 ||
415                         stats2.ierrors != 0 || stats2.oerrors != 0) {
416                 printf("Error: port %d stats are not as expected\n", porte);
417                 return -1;
418         }
419
420         rte_eth_dev_stop(portd);
421         rte_eth_dev_stop(porte);
422
423         return 0;
424 }
425
426 static int
427 test_pmd_ring(void)
428 {
429         struct rte_ring *rxtx[NUM_RINGS];
430         int port, cmdl_port0 = -1;
431         uint8_t nb_ports;
432
433         nb_ports = rte_eth_dev_count();
434         printf("nb_ports=%d\n", (int)nb_ports);
435
436         /*  create the rings and eth_rings in the test code.
437          *  This does not test the rte_pmd_ring_devinit function.
438          *
439          *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
440          */
441         rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
442         if (rxtx[0] == NULL) {
443                 printf("rte_ring_create R0 failed");
444                 return -1;
445         }
446
447         rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
448         if (rxtx[1] == NULL) {
449                 printf("rte_ring_create R1 failed");
450                 return -1;
451         }
452
453         tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
454         rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
455         rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
456         rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
457         rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
458
459         printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
460                         tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);
461
462         if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
463                 || (rxtx_portd == -1) || (rxtx_porte == -1)) {
464                 printf("rte_eth_from rings failed\n");
465                 return -1;
466         }
467
468         mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
469                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
470         if (mp == NULL)
471                 return -1;
472
473         if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
474                 || (rxtx_portc >= RTE_MAX_ETHPORTS)
475                 || (rxtx_portd >= RTE_MAX_ETHPORTS)
476                 || (rxtx_porte >= RTE_MAX_ETHPORTS)) {
477                 printf(" port exceed max eth ports\n");
478                 return -1;
479         }
480
481         if (test_ethdev_configure_port(tx_porta) < 0)
482                 return -1;
483
484         if (test_ethdev_configure_port(rx_portb) < 0)
485                 return -1;
486
487         if (test_ethdev_configure_port(rxtx_portc) < 0)
488                 return -1;
489
490         if (test_send_basic_packets() < 0)
491                 return -1;
492
493         if (test_get_stats(rxtx_portc) < 0)
494                 return -1;
495
496         if (test_stats_reset(rxtx_portc) < 0)
497                 return -1;
498
499         rte_eth_dev_stop(tx_porta);
500         rte_eth_dev_stop(rx_portb);
501         rte_eth_dev_stop(rxtx_portc);
502
503         if (test_pmd_ring_pair_create_attach(rxtx_portd, rxtx_porte) < 0)
504                 return -1;
505
506         /* find a port created with the --vdev=net_ring0 command line option */
507         for (port = 0; port < nb_ports; port++) {
508                 struct rte_eth_dev_info dev_info;
509
510                 rte_eth_dev_info_get(port, &dev_info);
511                 if (!strcmp(dev_info.driver_name, "Rings PMD")) {
512                         printf("found a command line ring port=%d\n", port);
513                         cmdl_port0 = port;
514                         break;
515                 }
516         }
517         if (cmdl_port0 != -1) {
518                 if (test_ethdev_configure_port(cmdl_port0) < 0)
519                         return -1;
520                 if (test_send_basic_packets_port(cmdl_port0) < 0)
521                         return -1;
522                 if (test_stats_reset(cmdl_port0) < 0)
523                         return -1;
524                 if (test_get_stats(cmdl_port0) < 0)
525                         return -1;
526                 rte_eth_dev_stop(cmdl_port0);
527         }
528         return 0;
529 }
530
531 REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);