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