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