apps: fix default mbuf size
[dpdk.git] / app / test / test_pmd_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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
42 #define TX_PORT 0
43 #define RX_PORT 1
44 #define RXTX_PORT 2
45 #define RXTX_PORT2 3
46 #define RXTX_PORT3 4
47 #define SOCKET0 0
48
49 #define RING_SIZE 256
50
51 #define NB_MBUF   512
52
53 static int
54 test_ethdev_configure(void)
55 {
56         struct rte_eth_conf null_conf;
57         struct rte_eth_link link;
58
59         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
60
61         if ((TX_PORT >= RTE_MAX_ETHPORTS) || (RX_PORT >= RTE_MAX_ETHPORTS)\
62                 || (RXTX_PORT >= RTE_MAX_ETHPORTS)) {
63                 printf(" TX/RX port exceed max eth ports\n");
64                 return -1;
65         }
66         if (rte_eth_dev_configure(TX_PORT, 1, 2, &null_conf) < 0) {
67                 printf("Configure failed for TX port\n");
68                 return -1;
69         }
70
71         /* Test queue release */
72         if (rte_eth_dev_configure(TX_PORT, 1, 1, &null_conf) < 0) {
73                 printf("Configure failed for TX port\n");
74                 return -1;
75         }
76         if (rte_eth_dev_configure(RX_PORT, 1, 1, &null_conf) < 0) {
77                 printf("Configure failed for RX port\n");
78                 return -1;
79         }
80         if (rte_eth_dev_configure(RXTX_PORT, 1, 1, &null_conf) < 0) {
81                 printf("Configure failed for RXTX port\n");
82                 return -1;
83         }
84
85         if (rte_eth_tx_queue_setup(TX_PORT, 0, RING_SIZE, SOCKET0, NULL) < 0) {
86                 printf("TX queue setup failed\n");
87                 return -1;
88         }
89         if (rte_eth_rx_queue_setup(RX_PORT, 0, RING_SIZE, SOCKET0,
90                         NULL, mp) < 0) {
91                 printf("RX queue setup failed\n");
92                 return -1;
93         }
94         if (rte_eth_tx_queue_setup(RXTX_PORT, 0, RING_SIZE, SOCKET0, NULL) < 0) {
95                 printf("TX queue setup failed\n");
96                 return -1;
97         }
98         if (rte_eth_rx_queue_setup(RXTX_PORT, 0, RING_SIZE, SOCKET0,
99                         NULL, mp) < 0) {
100                 printf("RX queue setup failed\n");
101                 return -1;
102         }
103
104         if (rte_eth_dev_start(TX_PORT) < 0) {
105                 printf("Error starting TX port\n");
106                 return -1;
107         }
108         if (rte_eth_dev_start(RX_PORT) < 0) {
109                 printf("Error starting RX port\n");
110                 return -1;
111         }
112         if (rte_eth_dev_start(RXTX_PORT) < 0) {
113                 printf("Error starting RX port\n");
114                 return -1;
115         }
116
117         rte_eth_link_get(TX_PORT, &link);
118         rte_eth_link_get(RX_PORT, &link);
119         rte_eth_link_get(RXTX_PORT, &link);
120
121         return 0;
122 }
123
124 static int
125 test_send_basic_packets(void)
126 {
127         struct rte_mbuf  bufs[RING_SIZE];
128         struct rte_mbuf *pbufs[RING_SIZE];
129         int i;
130
131         printf("Testing ring pmd RX/TX\n");
132
133         for (i = 0; i < RING_SIZE/2; i++)
134                 pbufs[i] = &bufs[i];
135
136         if (rte_eth_tx_burst(TX_PORT, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
137                 printf("Failed to transmit packet burst\n");
138                 return -1;
139         }
140
141         if (rte_eth_rx_burst(RX_PORT, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
142                 printf("Failed to receive packet burst\n");
143                 return -1;
144         }
145
146         for (i = 0; i < RING_SIZE/2; i++)
147                 if (pbufs[i] != &bufs[i]) {
148                         printf("Error: received data does not match that transmitted\n");
149                         return -1;
150                 }
151
152         return 0;
153 }
154
155 static int
156 test_get_stats(void)
157 {
158         struct rte_eth_stats stats;
159         struct rte_mbuf buf, *pbuf = &buf;
160
161         printf("Testing ring PMD stats\n");
162
163         /* check stats of RXTX port, should all be zero */
164         rte_eth_stats_get(RXTX_PORT, &stats);
165         if (stats.ipackets != 0 || stats.opackets != 0 ||
166                         stats.ibytes != 0 || stats.obytes != 0 ||
167                         stats.ierrors != 0 || stats.oerrors != 0) {
168                 printf("Error: RXTX port stats are not zero\n");
169                 return -1;
170         }
171
172         /* send and receive 1 packet and check for stats update */
173         if (rte_eth_tx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
174                 printf("Error sending packet to RXTX port\n");
175                 return -1;
176         }
177         if (rte_eth_rx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
178                 printf("Error receiving packet from RXTX port\n");
179                 return -1;
180         }
181
182         rte_eth_stats_get(RXTX_PORT, &stats);
183         if (stats.ipackets != 1 || stats.opackets != 1 ||
184                         stats.ibytes != 0 || stats.obytes != 0 ||
185                         stats.ierrors != 0 || stats.oerrors != 0) {
186                 printf("Error: RXTX port stats are not as expected\n");
187                 return -1;
188         }
189         return 0;
190 }
191
192 static int
193 test_stats_reset(void)
194 {
195         struct rte_eth_stats stats;
196         struct rte_mbuf buf, *pbuf = &buf;
197
198         printf("Testing ring PMD stats reset\n");
199
200         rte_eth_stats_reset(RXTX_PORT);
201
202         /* check stats of RXTX port, should all be zero */
203         rte_eth_stats_get(RXTX_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: RXTX port stats are not zero\n");
208                 return -1;
209         }
210
211         /* send and receive 1 packet and check for stats update */
212         if (rte_eth_tx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
213                 printf("Error sending packet to RXTX port\n");
214                 return -1;
215         }
216
217         if (rte_eth_rx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
218                 printf("Error receiving packet from RXTX port\n");
219                 return -1;
220         }
221
222         rte_eth_stats_get(RXTX_PORT, &stats);
223         if (stats.ipackets != 1 || stats.opackets != 1 ||
224                         stats.ibytes != 0 || stats.obytes != 0 ||
225                         stats.ierrors != 0 || stats.oerrors != 0) {
226                 printf("Error: RXTX port stats are not as expected\n");
227                 return -1;
228         }
229
230         rte_eth_stats_reset(RXTX_PORT);
231
232         /* check stats of RXTX port, should all be zero */
233         rte_eth_stats_get(RXTX_PORT, &stats);
234         if (stats.ipackets != 0 || stats.opackets != 0 ||
235                         stats.ibytes != 0 || stats.obytes != 0 ||
236                         stats.ierrors != 0 || stats.oerrors != 0) {
237                 printf("Error: RXTX port stats are not zero\n");
238                 return -1;
239         }
240
241         return 0;
242 }
243
244 static int
245 test_pmd_ring_pair_create_attach(void)
246 {
247         struct rte_eth_stats stats, stats2;
248         struct rte_mbuf buf, *pbuf = &buf;
249         struct rte_eth_conf null_conf;
250
251         if ((RXTX_PORT2 >= RTE_MAX_ETHPORTS) || (RXTX_PORT3 >= RTE_MAX_ETHPORTS)) {
252                 printf(" TX/RX port exceed max eth ports\n");
253                 return -1;
254         }
255         if ((rte_eth_dev_configure(RXTX_PORT2, 1, 1, &null_conf) < 0)
256                 || (rte_eth_dev_configure(RXTX_PORT3, 1, 1, &null_conf) < 0)) {
257                 printf("Configure failed for RXTX port\n");
258                 return -1;
259         }
260
261         if ((rte_eth_tx_queue_setup(RXTX_PORT2, 0, RING_SIZE, SOCKET0, NULL) < 0)
262                 || (rte_eth_tx_queue_setup(RXTX_PORT3, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
263                 printf("TX queue setup failed\n");
264                 return -1;
265         }
266
267         if ((rte_eth_rx_queue_setup(RXTX_PORT2, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
268                 || (rte_eth_rx_queue_setup(RXTX_PORT3, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
269                 printf("RX queue setup failed\n");
270                 return -1;
271         }
272
273         if ((rte_eth_dev_start(RXTX_PORT2) < 0)
274                 || (rte_eth_dev_start(RXTX_PORT3) < 0)) {
275                 printf("Error starting RXTX port\n");
276                 return -1;
277         }
278
279         /*
280          * send and receive 1 packet (RXTX_PORT2 -> RXTX_PORT3)
281          * and check for stats update
282          */
283         if (rte_eth_tx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
284                 printf("Error sending packet to RXTX port\n");
285                 return -1;
286         }
287
288         if (rte_eth_rx_burst(RXTX_PORT3, 0, &pbuf, 1) != 1) {
289                 printf("Error receiving packet from RXTX port\n");
290                 return -1;
291         }
292
293         rte_eth_stats_get(RXTX_PORT2, &stats);
294         rte_eth_stats_get(RXTX_PORT3, &stats2);
295         if (stats.ipackets != 0 || stats.opackets != 1 ||
296                         stats.ibytes != 0 || stats.obytes != 0 ||
297                         stats.ierrors != 0 || stats.oerrors != 0) {
298                 printf("Error: RXTX port stats are not as expected\n");
299                 return -1;
300         }
301
302         if (stats2.ipackets != 1 || stats2.opackets != 0 ||
303                         stats2.ibytes != 0 || stats2.obytes != 0 ||
304                         stats2.ierrors != 0 || stats2.oerrors != 0) {
305                 printf("Error: RXTX port stats are not as expected\n");
306                 return -1;
307         }
308
309         /*
310          * send and receive 1 packet (RXTX_PORT3 -> RXTX_PORT2)
311          * and check for stats update
312          */
313         if (rte_eth_tx_burst(RXTX_PORT3, 0, &pbuf, 1) != 1) {
314                 printf("Error sending packet to RXTX port\n");
315                 return -1;
316         }
317
318         if (rte_eth_rx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
319                 printf("Error receiving packet from RXTX port\n");
320                 return -1;
321         }
322
323         rte_eth_stats_get(RXTX_PORT2, &stats);
324         rte_eth_stats_get(RXTX_PORT3, &stats2);
325         if (stats.ipackets != 1 || stats.opackets != 1 ||
326                         stats.ibytes != 0 || stats.obytes != 0 ||
327                         stats.ierrors != 0 || stats.oerrors != 0) {
328                 printf("Error: RXTX port stats are not as expected\n");
329                 return -1;
330         }
331
332         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
333                         stats2.ibytes != 0 || stats2.obytes != 0 ||
334                         stats2.ierrors != 0 || stats2.oerrors != 0) {
335                 printf("Error: RXTX port stats are not as expected\n");
336                 return -1;
337         }
338
339         /*
340          * send and receive 1 packet (RXTX_PORT2 -> RXTX_PORT2)
341          * and check for stats update
342          */
343         if (rte_eth_tx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
344                 printf("Error sending packet to RXTX port\n");
345                 return -1;
346         }
347
348         if (rte_eth_rx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
349                 printf("Error receiving packet from RXTX port\n");
350                 return -1;
351         }
352
353         rte_eth_stats_get(RXTX_PORT2, &stats);
354         rte_eth_stats_get(RXTX_PORT3, &stats2);
355         if (stats.ipackets != 2 || stats.opackets != 2 ||
356                         stats.ibytes != 0 || stats.obytes != 0 ||
357                         stats.ierrors != 0 || stats.oerrors != 0) {
358                 printf("Error: RXTX port stats are not as expected\n");
359                 return -1;
360         }
361
362         if (stats2.ipackets != 1 || stats2.opackets != 1 ||
363                         stats2.ibytes != 0 || stats2.obytes != 0 ||
364                         stats2.ierrors != 0 || stats2.oerrors != 0) {
365                 printf("Error: RXTX port stats are not as expected\n");
366                 return -1;
367         }
368
369         /*
370          * send and receive 1 packet (RXTX_PORT3 -> RXTX_PORT3)
371          * and check for stats update
372          */
373         if (rte_eth_tx_burst(RXTX_PORT3, 0, &pbuf, 1) != 1) {
374                 printf("Error sending packet to RXTX port\n");
375                 return -1;
376         }
377
378         if (rte_eth_rx_burst(RXTX_PORT3, 0, &pbuf, 1) != 1) {
379                 printf("Error receiving packet from RXTX port\n");
380                 return -1;
381         }
382
383         rte_eth_stats_get(RXTX_PORT2, &stats);
384         rte_eth_stats_get(RXTX_PORT3, &stats2);
385         if (stats.ipackets != 2 || stats.opackets != 2 ||
386                         stats.ibytes != 0 || stats.obytes != 0 ||
387                         stats.ierrors != 0 || stats.oerrors != 0) {
388                 printf("Error: RXTX port stats are not as expected\n");
389                 return -1;
390         }
391
392         if (stats2.ipackets != 2 || stats2.opackets != 2 ||
393                         stats2.ibytes != 0 || stats2.obytes != 0 ||
394                         stats2.ierrors != 0 || stats2.oerrors != 0) {
395                 printf("Error: RXTX port stats are not as expected\n");
396                 return -1;
397         }
398
399         rte_eth_dev_stop(RXTX_PORT2);
400         rte_eth_dev_stop(RXTX_PORT3);
401
402         return 0;
403 }
404
405 static int
406 test_pmd_ring(void)
407 {
408         mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
409                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
410         if (mp == NULL)
411                 return -1;
412
413         if ((TX_PORT >= RTE_MAX_ETHPORTS) || (RX_PORT >= RTE_MAX_ETHPORTS)\
414                 || (RXTX_PORT >= RTE_MAX_ETHPORTS)) {
415                 printf(" TX/RX port exceed max eth ports\n");
416                 return -1;
417         }
418
419         if (test_ethdev_configure() < 0)
420                 return -1;
421
422         if (test_send_basic_packets() < 0)
423                 return -1;
424
425         if (test_get_stats() < 0)
426                 return -1;
427
428         if (test_stats_reset() < 0)
429                 return -1;
430
431         rte_eth_dev_stop(RX_PORT);
432         rte_eth_dev_stop(TX_PORT);
433         rte_eth_dev_stop(RXTX_PORT);
434
435         if (test_pmd_ring_pair_create_attach() < 0)
436                 return -1;
437
438         return 0;
439 }
440
441 static struct test_command ring_pmd_cmd = {
442         .command = "ring_pmd_autotest",
443         .callback = test_pmd_ring,
444 };
445 REGISTER_TEST_COMMAND(ring_pmd_cmd);