New upstream version 17.11-rc3
[deb_dpdk.git] / test / test-pipeline / init.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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_eal.h>
51 #include <rte_per_lcore.h>
52 #include <rte_launch.h>
53 #include <rte_atomic.h>
54 #include <rte_cycles.h>
55 #include <rte_prefetch.h>
56 #include <rte_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_interrupts.h>
59 #include <rte_pci.h>
60 #include <rte_random.h>
61 #include <rte_debug.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_ring.h>
65 #include <rte_mempool.h>
66 #include <rte_mbuf.h>
67 #include <rte_string_fns.h>
68 #include <rte_ip.h>
69 #include <rte_tcp.h>
70 #include <rte_lpm.h>
71 #include <rte_lpm6.h>
72
73 #include "main.h"
74
75 struct app_params app = {
76         /* Ports*/
77         .n_ports = APP_MAX_PORTS,
78         .port_rx_ring_size = 128,
79         .port_tx_ring_size = 512,
80
81         /* Rings */
82         .ring_rx_size = 128,
83         .ring_tx_size = 128,
84
85         /* Buffer pool */
86         .pool_buffer_size = 2048 + RTE_PKTMBUF_HEADROOM,
87         .pool_size = 32 * 1024,
88         .pool_cache_size = 256,
89
90         /* Burst sizes */
91         .burst_size_rx_read = 64,
92         .burst_size_rx_write = 64,
93         .burst_size_worker_read = 64,
94         .burst_size_worker_write = 64,
95         .burst_size_tx_read = 64,
96         .burst_size_tx_write = 64,
97 };
98
99 static struct rte_eth_conf port_conf = {
100         .rxmode = {
101                 .split_hdr_size = 0,
102                 .header_split   = 0, /* Header Split disabled */
103                 .hw_ip_checksum = 1, /* IP checksum offload enabled */
104                 .hw_vlan_filter = 0, /* VLAN filtering disabled */
105                 .jumbo_frame    = 0, /* Jumbo Frame Support disabled */
106                 .hw_strip_crc   = 1, /* CRC stripped by hardware */
107         },
108         .rx_adv_conf = {
109                 .rss_conf = {
110                         .rss_key = NULL,
111                         .rss_hf = ETH_RSS_IP,
112                 },
113         },
114         .txmode = {
115                 .mq_mode = ETH_MQ_TX_NONE,
116         },
117 };
118
119 static struct rte_eth_rxconf rx_conf = {
120         .rx_thresh = {
121                 .pthresh = 8,
122                 .hthresh = 8,
123                 .wthresh = 4,
124         },
125         .rx_free_thresh = 64,
126         .rx_drop_en = 0,
127 };
128
129 static struct rte_eth_txconf tx_conf = {
130         .tx_thresh = {
131                 .pthresh = 36,
132                 .hthresh = 0,
133                 .wthresh = 0,
134         },
135         .tx_free_thresh = 0,
136         .tx_rs_thresh = 0,
137 };
138
139 static void
140 app_init_mbuf_pools(void)
141 {
142         /* Init the buffer pool */
143         RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n");
144         app.pool = rte_pktmbuf_pool_create("mempool", app.pool_size,
145                 app.pool_cache_size, 0, app.pool_buffer_size, rte_socket_id());
146         if (app.pool == NULL)
147                 rte_panic("Cannot create mbuf pool\n");
148 }
149
150 static void
151 app_init_rings(void)
152 {
153         uint32_t i;
154
155         for (i = 0; i < app.n_ports; i++) {
156                 char name[32];
157
158                 snprintf(name, sizeof(name), "app_ring_rx_%u", i);
159
160                 app.rings_rx[i] = rte_ring_create(
161                         name,
162                         app.ring_rx_size,
163                         rte_socket_id(),
164                         RING_F_SP_ENQ | RING_F_SC_DEQ);
165
166                 if (app.rings_rx[i] == NULL)
167                         rte_panic("Cannot create RX ring %u\n", i);
168         }
169
170         for (i = 0; i < app.n_ports; i++) {
171                 char name[32];
172
173                 snprintf(name, sizeof(name), "app_ring_tx_%u", i);
174
175                 app.rings_tx[i] = rte_ring_create(
176                         name,
177                         app.ring_tx_size,
178                         rte_socket_id(),
179                         RING_F_SP_ENQ | RING_F_SC_DEQ);
180
181                 if (app.rings_tx[i] == NULL)
182                         rte_panic("Cannot create TX ring %u\n", i);
183         }
184
185 }
186
187 static void
188 app_ports_check_link(void)
189 {
190         uint32_t all_ports_up, i;
191
192         all_ports_up = 1;
193
194         for (i = 0; i < app.n_ports; i++) {
195                 struct rte_eth_link link;
196                 uint16_t port;
197
198                 port = app.ports[i];
199                 memset(&link, 0, sizeof(link));
200                 rte_eth_link_get_nowait(port, &link);
201                 RTE_LOG(INFO, USER1, "Port %u (%u Gbps) %s\n",
202                         port,
203                         link.link_speed / 1000,
204                         link.link_status ? "UP" : "DOWN");
205
206                 if (link.link_status == ETH_LINK_DOWN)
207                         all_ports_up = 0;
208         }
209
210         if (all_ports_up == 0)
211                 rte_panic("Some NIC ports are DOWN\n");
212 }
213
214 static void
215 app_init_ports(void)
216 {
217         uint32_t i;
218
219         /* Init NIC ports, then start the ports */
220         for (i = 0; i < app.n_ports; i++) {
221                 uint16_t port;
222                 int ret;
223
224                 port = app.ports[i];
225                 RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port);
226
227                 /* Init port */
228                 ret = rte_eth_dev_configure(
229                         port,
230                         1,
231                         1,
232                         &port_conf);
233                 if (ret < 0)
234                         rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
235
236                 rte_eth_promiscuous_enable(port);
237
238                 /* Init RX queues */
239                 ret = rte_eth_rx_queue_setup(
240                         port,
241                         0,
242                         app.port_rx_ring_size,
243                         rte_eth_dev_socket_id(port),
244                         &rx_conf,
245                         app.pool);
246                 if (ret < 0)
247                         rte_panic("Cannot init RX for port %u (%d)\n",
248                                 (uint32_t) port, ret);
249
250                 /* Init TX queues */
251                 ret = rte_eth_tx_queue_setup(
252                         port,
253                         0,
254                         app.port_tx_ring_size,
255                         rte_eth_dev_socket_id(port),
256                         &tx_conf);
257                 if (ret < 0)
258                         rte_panic("Cannot init TX for port %u (%d)\n",
259                                 (uint32_t) port, ret);
260
261                 /* Start port */
262                 ret = rte_eth_dev_start(port);
263                 if (ret < 0)
264                         rte_panic("Cannot start port %u (%d)\n", port, ret);
265         }
266
267         app_ports_check_link();
268 }
269
270 void
271 app_init(void)
272 {
273         app_init_mbuf_pools();
274         app_init_rings();
275         app_init_ports();
276
277         RTE_LOG(INFO, USER1, "Initialization completed\n");
278 }