remove trailing whitespaces
[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 /* two test rings, r1 is used by two ports, r2 just by one */
43 static struct rte_ring *r1[2], *r2;
44
45 static struct rte_mempool *mp;
46 static uint8_t start_idx; /* will store the port id of the first of our new ports */
47
48 #define TX_PORT (uint8_t)(start_idx + 1)
49 #define RX_PORT (uint8_t)(start_idx + 2)
50 #define RXTX_PORT (uint8_t)(start_idx + 3)
51 #define RXTX_PORT2 (uint8_t)(start_idx + 4)
52 #define RXTX_PORT4 (uint8_t)(start_idx + 6)
53 #define RXTX_PORT5 (uint8_t)(start_idx + 7)
54 #define SOCKET0 0
55
56 #define RING_SIZE 256
57
58 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
59 #define NB_MBUF   512
60
61 static int
62 test_ethdev_configure(void)
63 {
64         struct rte_eth_conf null_conf;
65         struct rte_eth_link link;
66
67         memset(&null_conf, 0, sizeof(struct rte_eth_conf));
68
69         if ((TX_PORT >= RTE_MAX_ETHPORTS) || (RX_PORT >= RTE_MAX_ETHPORTS)\
70                 || (RXTX_PORT >= RTE_MAX_ETHPORTS)) {
71                 printf(" TX/RX port exceed max eth ports\n");
72                 return -1;
73         }
74         if (rte_eth_dev_configure(TX_PORT, 1, 2, &null_conf) < 0) {
75                 printf("Configure failed for TX port\n");
76                 return -1;
77         }
78
79         /* Test queue release */
80         if (rte_eth_dev_configure(TX_PORT, 1, 1, &null_conf) < 0) {
81                 printf("Configure failed for TX port\n");
82                 return -1;
83         }
84         if (rte_eth_dev_configure(RX_PORT, 1, 1, &null_conf) < 0) {
85                 printf("Configure failed for RX port\n");
86                 return -1;
87         }
88         if (rte_eth_dev_configure(RXTX_PORT, 1, 1, &null_conf) < 0) {
89                 printf("Configure failed for RX port\n");
90                 return -1;
91         }
92
93         if (rte_eth_tx_queue_setup(TX_PORT, 0, RING_SIZE, SOCKET0, NULL) < 0) {
94                 printf("TX queue setup failed\n");
95                 return -1;
96         }
97         if (rte_eth_rx_queue_setup(RX_PORT, 0, RING_SIZE, SOCKET0,
98                         NULL, mp) < 0) {
99                 printf("RX queue setup failed\n");
100                 return -1;
101         }
102         if (rte_eth_tx_queue_setup(RXTX_PORT, 0, RING_SIZE, SOCKET0, NULL) < 0) {
103                 printf("TX queue setup failed\n");
104                 return -1;
105         }
106         if (rte_eth_rx_queue_setup(RXTX_PORT, 0, RING_SIZE, SOCKET0,
107                         NULL, mp) < 0) {
108                 printf("RX queue setup failed\n");
109                 return -1;
110         }
111
112         if (rte_eth_dev_start(TX_PORT) < 0) {
113                 printf("Error starting TX port\n");
114                 return -1;
115         }
116         if (rte_eth_dev_start(RX_PORT) < 0) {
117                 printf("Error starting RX port\n");
118                 return -1;
119         }
120         if (rte_eth_dev_start(RXTX_PORT) < 0) {
121                 printf("Error starting RX port\n");
122                 return -1;
123         }
124
125         rte_eth_link_get(TX_PORT, &link);
126         rte_eth_link_get(RX_PORT, &link);
127         rte_eth_link_get(RXTX_PORT, &link);
128
129         return 0;
130 }
131
132 static int
133 test_send_basic_packets(void)
134 {
135         struct rte_mbuf  bufs[RING_SIZE];
136         struct rte_mbuf *pbufs[RING_SIZE];
137         int i;
138
139         printf("Testing ring pmd RX/TX\n");
140
141         for (i = 0; i < RING_SIZE/2; i++)
142                 pbufs[i] = &bufs[i];
143
144         if (rte_eth_tx_burst(TX_PORT, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
145                 printf("Failed to transmit packet burst\n");
146                 return -1;
147         }
148
149         if (rte_eth_rx_burst(RX_PORT, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
150                 printf("Failed to receive packet burst\n");
151                 return -1;
152         }
153
154         for (i = 0; i < RING_SIZE/2; i++)
155                 if (pbufs[i] != &bufs[i]) {
156                         printf("Error: received data does not match that transmitted\n");
157                         return -1;
158                 }
159
160         return 0;
161 }
162
163 static int
164 test_get_stats(void)
165 {
166         struct rte_eth_stats stats;
167         struct rte_mbuf buf, *pbuf = &buf;
168
169         printf("Testing ring PMD stats\n");
170
171         /* check stats of RXTX port, should all be zero */
172         rte_eth_stats_get(RXTX_PORT, &stats);
173         if (stats.ipackets != 0 || stats.opackets != 0 ||
174                         stats.ibytes != 0 || stats.obytes != 0 ||
175                         stats.ierrors != 0 || stats.oerrors != 0) {
176                 printf("Error: RXTX port stats are not zero\n");
177                 return -1;
178         }
179
180         /* send and receive 1 packet and check for stats update */
181         if (rte_eth_tx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
182                 printf("Error sending packet to RXTX port\n");
183                 return -1;
184         }
185         if (rte_eth_rx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
186                 printf("Error receiving packet from RXTX port\n");
187                 return -1;
188         }
189
190         rte_eth_stats_get(RXTX_PORT, &stats);
191         if (stats.ipackets != 1 || stats.opackets != 1 ||
192                         stats.ibytes != 0 || stats.obytes != 0 ||
193                         stats.ierrors != 0 || stats.oerrors != 0) {
194                 printf("Error: RXTX port stats are not as expected\n");
195                 return -1;
196         }
197         return 0;
198 }
199
200 static int
201 test_stats_reset(void)
202 {
203         struct rte_eth_stats stats;
204         struct rte_mbuf buf, *pbuf = &buf;
205
206         printf("Testing ring PMD stats reset\n");
207
208         rte_eth_stats_reset(RXTX_PORT);
209
210         /* check stats of RXTX port, should all be zero */
211         rte_eth_stats_get(RXTX_PORT, &stats);
212         if (stats.ipackets != 0 || stats.opackets != 0 ||
213                         stats.ibytes != 0 || stats.obytes != 0 ||
214                         stats.ierrors != 0 || stats.oerrors != 0) {
215                 printf("Error: RXTX port stats are not zero\n");
216                 return -1;
217         }
218
219         /* send and receive 1 packet and check for stats update */
220         if (rte_eth_tx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
221                 printf("Error sending packet to RXTX port\n");
222                 return -1;
223         }
224
225         if (rte_eth_rx_burst(RXTX_PORT, 0, &pbuf, 1) != 1) {
226                 printf("Error receiving packet from RXTX port\n");
227                 return -1;
228         }
229
230         rte_eth_stats_get(RXTX_PORT, &stats);
231         if (stats.ipackets != 1 || stats.opackets != 1 ||
232                         stats.ibytes != 0 || stats.obytes != 0 ||
233                         stats.ierrors != 0 || stats.oerrors != 0) {
234                 printf("Error: RXTX port stats are not as expected\n");
235                 return -1;
236         }
237
238         rte_eth_stats_reset(RXTX_PORT);
239
240         /* check stats of RXTX port, should all be zero */
241         rte_eth_stats_get(RXTX_PORT, &stats);
242         if (stats.ipackets != 0 || stats.opackets != 0 ||
243                         stats.ibytes != 0 || stats.obytes != 0 ||
244                         stats.ierrors != 0 || stats.oerrors != 0) {
245                 printf("Error: RXTX port stats are not zero\n");
246                 return -1;
247         }
248
249         return 0;
250 }
251
252 static int
253 test_pmd_ring_init(void)
254 {
255         struct rte_eth_stats stats;
256         struct rte_mbuf buf, *pbuf = &buf;
257         struct rte_eth_conf null_conf;
258
259         printf("Testing ring pmd init\n");
260
261         if (RXTX_PORT2 >= RTE_MAX_ETHPORTS) {
262                 printf(" TX/RX port exceed max eth ports\n");
263                 return -1;
264         }
265         if (rte_eth_dev_configure(RXTX_PORT2, 1, 1, &null_conf) < 0) {
266                 printf("Configure failed for RXTX port\n");
267                 return -1;
268         }
269
270         if (rte_eth_tx_queue_setup(RXTX_PORT2, 0, RING_SIZE, SOCKET0, NULL) < 0) {
271                 printf("TX queue setup failed\n");
272                 return -1;
273         }
274
275         if (rte_eth_rx_queue_setup(RXTX_PORT2, 0, RING_SIZE, SOCKET0,
276                         NULL, mp) < 0) {
277                 printf("RX queue setup failed\n");
278                 return -1;
279         }
280
281         if (rte_eth_dev_start(RXTX_PORT2) < 0) {
282                 printf("Error starting RX port\n");
283                 return -1;
284         }
285
286         /* send and receive 1 packet and check for stats update */
287         if (rte_eth_tx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
288                 printf("Error sending packet to RXTX port\n");
289                 return -1;
290         }
291
292         if (rte_eth_rx_burst(RXTX_PORT2, 0, &pbuf, 1) != 1) {
293                 printf("Error receiving packet from RXTX port\n");
294                 return -1;
295         }
296
297         rte_eth_stats_get(RXTX_PORT2, &stats);
298         if (stats.ipackets != 1 || 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         rte_eth_dev_stop(RXTX_PORT2);
306
307         return 0;
308 }
309
310 static int
311 test_pmd_ring_pair_create(void)
312 {
313         struct rte_eth_stats stats, stats2;
314         struct rte_mbuf buf, *pbuf = &buf;
315         struct rte_eth_conf null_conf;
316
317         if ((RXTX_PORT4 >= RTE_MAX_ETHPORTS) || (RXTX_PORT5 >= RTE_MAX_ETHPORTS)) {
318                 printf(" TX/RX port exceed max eth ports\n");
319                 return -1;
320         }
321         if ((rte_eth_dev_configure(RXTX_PORT4, 1, 1, &null_conf) < 0)
322                 || (rte_eth_dev_configure(RXTX_PORT5, 1, 1, &null_conf) < 0)) {
323                 printf("Configure failed for RXTX port\n");
324                 return -1;
325         }
326
327         if ((rte_eth_tx_queue_setup(RXTX_PORT4, 0, RING_SIZE, SOCKET0, NULL) < 0)
328                 || (rte_eth_tx_queue_setup(RXTX_PORT5, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
329                 printf("TX queue setup failed\n");
330                 return -1;
331         }
332
333         if ((rte_eth_rx_queue_setup(RXTX_PORT4, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
334                 || (rte_eth_rx_queue_setup(RXTX_PORT5, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
335                 printf("RX queue setup failed\n");
336                 return -1;
337         }
338
339         if ((rte_eth_dev_start(RXTX_PORT4) < 0)
340                 || (rte_eth_dev_start(RXTX_PORT5) < 0)) {
341                 printf("Error starting RXTX port\n");
342                 return -1;
343         }
344
345         /* send and receive 1 packet and check for stats update */
346         if (rte_eth_tx_burst(RXTX_PORT4, 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_PORT5, 0, &pbuf, 1) != 1) {
352                 printf("Error receiving packet from RXTX port\n");
353                 return -1;
354         }
355
356         rte_eth_stats_get(RXTX_PORT4, &stats);
357         rte_eth_stats_get(RXTX_PORT5, &stats2);
358         if (stats.ipackets != 0 || stats.opackets != 1 ||
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 != 0 ||
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         rte_eth_dev_stop(RXTX_PORT4);
373         rte_eth_dev_stop(RXTX_PORT5);
374
375         return 0;
376 }
377
378 static int
379 test_pmd_ring_pair_attach(void)
380 {
381         struct rte_eth_stats stats, stats2;
382         struct rte_mbuf buf, *pbuf = &buf;
383         struct rte_eth_conf null_conf;
384
385         if ((RXTX_PORT4 >= RTE_MAX_ETHPORTS) || (RXTX_PORT5 >= RTE_MAX_ETHPORTS)) {
386                 printf(" TX/RX port exceed max eth ports\n");
387                 return -1;
388         }
389         if ((rte_eth_dev_configure(RXTX_PORT4, 1, 1, &null_conf) < 0)
390                 || (rte_eth_dev_configure(RXTX_PORT5, 1, 1, &null_conf) < 0)) {
391                 printf("Configure failed for RXTX port\n");
392                 return -1;
393         }
394
395         if ((rte_eth_tx_queue_setup(RXTX_PORT4, 0, RING_SIZE, SOCKET0, NULL) < 0)
396                 || (rte_eth_tx_queue_setup(RXTX_PORT5, 0, RING_SIZE, SOCKET0, NULL) < 0)) {
397                 printf("TX queue setup failed\n");
398                 return -1;
399         }
400
401         if ((rte_eth_rx_queue_setup(RXTX_PORT4, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)
402                 || (rte_eth_rx_queue_setup(RXTX_PORT5, 0, RING_SIZE, SOCKET0, NULL, mp) < 0)) {
403                 printf("RX queue setup failed\n");
404                 return -1;
405         }
406
407         if ((rte_eth_dev_start(RXTX_PORT4) < 0)
408                 || (rte_eth_dev_start(RXTX_PORT5) < 0)) {
409                 printf("Error starting RXTX port\n");
410                 return -1;
411         }
412
413         rte_eth_stats_reset(RXTX_PORT4);
414         rte_eth_stats_reset(RXTX_PORT5);
415
416         /* send and receive 1 packet and check for stats update */
417         if (rte_eth_tx_burst(RXTX_PORT4, 0, &pbuf, 1) != 1) {
418                 printf("Error sending packet to RXTX port\n");
419                 return -1;
420         }
421         if (rte_eth_rx_burst(RXTX_PORT5, 0, &pbuf, 1) != 1) {
422                 printf("Error receiving packet from RXTX port\n");
423                 return -1;
424         }
425
426         rte_eth_stats_get(RXTX_PORT4, &stats);
427         rte_eth_stats_get(RXTX_PORT5, &stats2);
428         if (stats.ipackets != 0 || stats.opackets != 1 ||
429                         stats.ibytes != 0 || stats.obytes != 0 ||
430                         stats.ierrors != 0 || stats.oerrors != 0) {
431                 printf("Error: RXTX port stats are not as expected\n");
432                 return -1;
433         }
434
435         if (stats2.ipackets != 1 || stats2.opackets != 0 ||
436                         stats2.ibytes != 0 || stats2.obytes != 0 ||
437                         stats2.ierrors != 0 || stats2.oerrors != 0) {
438                 printf("Error: RXTX port stats are not as expected\n");
439                 return -1;
440         }
441
442         rte_eth_dev_stop(RXTX_PORT4);
443         rte_eth_dev_stop(RXTX_PORT5);
444
445         return 0;
446 }
447
448 int
449 test_pmd_ring(void)
450 {
451         r1[0] = rte_ring_create("R1", RING_SIZE, 0, 0);
452         r1[1] = rte_ring_create("R2", RING_SIZE, 0, 0);
453         if (r1[0] == NULL && (r1[0] = rte_ring_lookup("R1")) == NULL)
454                 return -1;
455         if (r1[1] == NULL && (r1[1] = rte_ring_lookup("R2")) == NULL)
456                 return -1;
457
458         r2 = rte_ring_create("R3", RING_SIZE, 0, RING_F_SP_ENQ|RING_F_SC_DEQ);
459         if (r2 == NULL && (r2 = rte_ring_lookup("R3")) == NULL)
460                 return -1;
461
462         mp = rte_mempool_create("mbuf_pool", NB_MBUF,
463                         MBUF_SIZE, 32,
464                         sizeof(struct rte_pktmbuf_pool_private),
465                         rte_pktmbuf_pool_init, NULL,
466                         rte_pktmbuf_init, NULL,
467                         rte_socket_id(), 0);
468         if (mp == NULL)
469                 return -1;
470
471         start_idx = rte_eth_dev_count();
472
473         if ((TX_PORT >= RTE_MAX_ETHPORTS) || (RX_PORT >= RTE_MAX_ETHPORTS)\
474                 || (RXTX_PORT >= RTE_MAX_ETHPORTS)) {
475                 printf(" TX/RX port exceed max eth ports\n");
476                 return -1;
477         }
478
479         if (test_ethdev_configure() < 0)
480                 return -1;
481
482         if (test_send_basic_packets() < 0)
483                 return -1;
484
485         if (test_get_stats() < 0)
486                 return -1;
487
488         if (test_stats_reset() < 0)
489                 return -1;
490
491         rte_eth_dev_stop(RX_PORT);
492         rte_eth_dev_stop(TX_PORT);
493         rte_eth_dev_stop(RXTX_PORT);
494
495         if (test_pmd_ring_init() < 0)
496                 return -1;
497
498         if (test_pmd_ring_pair_create() < 0)
499                 return -1;
500
501         if (test_pmd_ring_pair_attach() < 0)
502                 return -1;
503         return 0;
504 }
505
506 #else
507
508 int
509 test_pmd_ring(void)
510 {
511         return 0;
512 }
513
514 #endif
515