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