app/test: packet framework unit tests
[dpdk.git] / app / test / test_table_ports.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
34 #ifdef RTE_LIBRTE_TABLE
35
36 #include "test_table_ports.h"
37 #include "test_table.h"
38
39 port_test port_tests[] = {
40         test_port_ring_reader,
41         test_port_ring_writer,
42 };
43
44 unsigned n_port_tests = RTE_DIM(port_tests);
45
46 /* Port tests */
47 int
48 test_port_ring_reader(void)
49 {
50         int status, i;
51         struct rte_port_ring_reader_params port_ring_reader_params;
52         void *port;
53
54         /* Invalid params */
55         port = rte_port_ring_reader_ops.f_create(NULL, 0);
56         if (port != NULL)
57                 return -1;
58
59         status = rte_port_ring_reader_ops.f_free(port);
60         if (status >= 0)
61                 return -2;
62
63         /* Create and free */
64         port_ring_reader_params.ring = RING_RX;
65         port = rte_port_ring_reader_ops.f_create(&port_ring_reader_params, 0);
66         if (port == NULL)
67                 return -3;
68
69         status = rte_port_ring_reader_ops.f_free(port);
70         if (status != 0)
71                 return -4;
72
73         /* -- Traffic RX -- */
74         int expected_pkts, received_pkts;
75         struct rte_mbuf *res_mbuf[RTE_PORT_IN_BURST_SIZE_MAX];
76         void *mbuf[RTE_PORT_IN_BURST_SIZE_MAX];
77
78         port_ring_reader_params.ring = RING_RX;
79         port = rte_port_ring_reader_ops.f_create(&port_ring_reader_params, 0);
80
81         /* Single packet */
82         mbuf[0] = (void *)rte_pktmbuf_alloc(pool);
83
84         expected_pkts = rte_ring_sp_enqueue_burst(port_ring_reader_params.ring,
85                 mbuf, 1);
86         received_pkts = rte_port_ring_reader_ops.f_rx(port, res_mbuf, 1);
87
88         if (received_pkts < expected_pkts)
89                 return -5;
90
91         rte_pktmbuf_free(res_mbuf[0]);
92
93         /* Multiple packets */
94         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
95                 mbuf[i] = rte_pktmbuf_alloc(pool);
96
97         expected_pkts = rte_ring_sp_enqueue_burst(port_ring_reader_params.ring,
98                 (void * const *) mbuf, RTE_PORT_IN_BURST_SIZE_MAX);
99         received_pkts = rte_port_ring_reader_ops.f_rx(port, res_mbuf,
100                 RTE_PORT_IN_BURST_SIZE_MAX);
101
102         if (received_pkts < expected_pkts)
103                 return -6;
104
105         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
106                 rte_pktmbuf_free(res_mbuf[i]);
107
108         return 0;
109 }
110
111 int
112 test_port_ring_writer(void)
113 {
114         int status, i;
115         struct rte_port_ring_writer_params port_ring_writer_params;
116         void *port;
117
118         /* Invalid params */
119         port = rte_port_ring_writer_ops.f_create(NULL, 0);
120         if (port != NULL)
121                 return -1;
122
123         status = rte_port_ring_writer_ops.f_free(port);
124         if (status >= 0)
125                 return -2;
126
127         port_ring_writer_params.ring = NULL;
128
129         port = rte_port_ring_writer_ops.f_create(&port_ring_writer_params, 0);
130         if (port != NULL)
131                 return -3;
132
133         port_ring_writer_params.ring = RING_TX;
134         port_ring_writer_params.tx_burst_sz = RTE_PORT_IN_BURST_SIZE_MAX + 1;
135
136         port = rte_port_ring_writer_ops.f_create(&port_ring_writer_params, 0);
137         if (port != NULL)
138                 return -4;
139
140         /* Create and free */
141         port_ring_writer_params.ring = RING_TX;
142         port_ring_writer_params.tx_burst_sz = RTE_PORT_IN_BURST_SIZE_MAX;
143
144         port = rte_port_ring_writer_ops.f_create(&port_ring_writer_params, 0);
145         if (port == NULL)
146                 return -5;
147
148         status = rte_port_ring_writer_ops.f_free(port);
149         if (status != 0)
150                 return -6;
151
152         /* -- Traffic TX -- */
153         int expected_pkts, received_pkts;
154         struct rte_mbuf *mbuf[RTE_PORT_IN_BURST_SIZE_MAX];
155         struct rte_mbuf *res_mbuf[RTE_PORT_IN_BURST_SIZE_MAX];
156
157         port_ring_writer_params.ring = RING_TX;
158         port_ring_writer_params.tx_burst_sz = RTE_PORT_IN_BURST_SIZE_MAX;
159         port = rte_port_ring_writer_ops.f_create(&port_ring_writer_params, 0);
160
161         /* Single packet */
162         mbuf[0] = rte_pktmbuf_alloc(pool);
163
164         rte_port_ring_writer_ops.f_tx(port, mbuf[0]);
165         rte_port_ring_writer_ops.f_flush(port);
166         expected_pkts = 1;
167         received_pkts = rte_ring_sc_dequeue_burst(port_ring_writer_params.ring,
168                 (void **)res_mbuf, port_ring_writer_params.tx_burst_sz);
169
170         if (received_pkts < expected_pkts)
171                 return -7;
172
173         rte_pktmbuf_free(res_mbuf[0]);
174
175         /* Multiple packets */
176         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++) {
177                 mbuf[i] = rte_pktmbuf_alloc(pool);
178                 rte_port_ring_writer_ops.f_tx(port, mbuf[i]);
179         }
180
181         expected_pkts = RTE_PORT_IN_BURST_SIZE_MAX;
182         received_pkts = rte_ring_sc_dequeue_burst(port_ring_writer_params.ring,
183                 (void **)res_mbuf, port_ring_writer_params.tx_burst_sz);
184
185         if (received_pkts < expected_pkts)
186                 return -8;
187
188         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
189                 rte_pktmbuf_free(res_mbuf[i]);
190
191         /* TX Bulk */
192         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
193                 mbuf[i] = rte_pktmbuf_alloc(pool);
194         rte_port_ring_writer_ops.f_tx_bulk(port, mbuf, (uint64_t)-1);
195
196         expected_pkts = RTE_PORT_IN_BURST_SIZE_MAX;
197         received_pkts = rte_ring_sc_dequeue_burst(port_ring_writer_params.ring,
198                 (void **)res_mbuf, port_ring_writer_params.tx_burst_sz);
199
200         if (received_pkts < expected_pkts)
201                 return -8;
202
203         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
204                 rte_pktmbuf_free(res_mbuf[i]);
205
206         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
207                 mbuf[i] = rte_pktmbuf_alloc(pool);
208         rte_port_ring_writer_ops.f_tx_bulk(port, mbuf, (uint64_t)-3);
209         rte_port_ring_writer_ops.f_tx_bulk(port, mbuf, (uint64_t)2);
210
211         expected_pkts = RTE_PORT_IN_BURST_SIZE_MAX;
212         received_pkts = rte_ring_sc_dequeue_burst(port_ring_writer_params.ring,
213                 (void **)res_mbuf, port_ring_writer_params.tx_burst_sz);
214
215         if (received_pkts < expected_pkts)
216                 return -9;
217
218         for (i = 0; i < RTE_PORT_IN_BURST_SIZE_MAX; i++)
219                 rte_pktmbuf_free(res_mbuf[i]);
220
221         return 0;
222 }
223
224 #endif