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