xref: /dpdk/app/test-pipeline/init.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
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 		.offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
74 	},
75 	.rx_adv_conf = {
76 		.rss_conf = {
77 			.rss_key = NULL,
78 			.rss_hf = RTE_ETH_RSS_IP,
79 		},
80 	},
81 	.txmode = {
82 		.mq_mode = RTE_ETH_MQ_TX_NONE,
83 	},
84 };
85 
86 static struct rte_eth_rxconf rx_conf = {
87 	.rx_thresh = {
88 		.pthresh = 8,
89 		.hthresh = 8,
90 		.wthresh = 4,
91 	},
92 	.rx_free_thresh = 64,
93 	.rx_drop_en = 0,
94 };
95 
96 static struct rte_eth_txconf tx_conf = {
97 	.tx_thresh = {
98 		.pthresh = 36,
99 		.hthresh = 0,
100 		.wthresh = 0,
101 	},
102 	.tx_free_thresh = 0,
103 	.tx_rs_thresh = 0,
104 };
105 
106 static void
107 app_init_mbuf_pools(void)
108 {
109 	/* Init the buffer pool */
110 	RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n");
111 	app.pool = rte_pktmbuf_pool_create("mempool", app.pool_size,
112 		app.pool_cache_size, 0, app.pool_buffer_size, rte_socket_id());
113 	if (app.pool == NULL)
114 		rte_panic("Cannot create mbuf pool\n");
115 }
116 
117 static void
118 app_init_rings(void)
119 {
120 	uint32_t i;
121 
122 	for (i = 0; i < app.n_ports; i++) {
123 		char name[32];
124 
125 		snprintf(name, sizeof(name), "app_ring_rx_%u", i);
126 
127 		app.rings_rx[i] = rte_ring_create(
128 			name,
129 			app.ring_rx_size,
130 			rte_socket_id(),
131 			RING_F_SP_ENQ | RING_F_SC_DEQ);
132 
133 		if (app.rings_rx[i] == NULL)
134 			rte_panic("Cannot create RX ring %u\n", i);
135 	}
136 
137 	for (i = 0; i < app.n_ports; i++) {
138 		char name[32];
139 
140 		snprintf(name, sizeof(name), "app_ring_tx_%u", i);
141 
142 		app.rings_tx[i] = rte_ring_create(
143 			name,
144 			app.ring_tx_size,
145 			rte_socket_id(),
146 			RING_F_SP_ENQ | RING_F_SC_DEQ);
147 
148 		if (app.rings_tx[i] == NULL)
149 			rte_panic("Cannot create TX ring %u\n", i);
150 	}
151 
152 }
153 
154 static void
155 app_ports_check_link(void)
156 {
157 	uint32_t all_ports_up, i;
158 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
159 	all_ports_up = 1;
160 
161 	for (i = 0; i < app.n_ports; i++) {
162 		struct rte_eth_link link;
163 		uint16_t port;
164 		int ret;
165 
166 		port = app.ports[i];
167 		memset(&link, 0, sizeof(link));
168 		ret = rte_eth_link_get_nowait(port, &link);
169 		if (ret < 0) {
170 			RTE_LOG(INFO, USER1,
171 				"Failed to get port %u link status: %s\n",
172 				port, rte_strerror(-ret));
173 			all_ports_up = 0;
174 			continue;
175 		}
176 		rte_eth_link_to_str(link_status_text, sizeof(link_status_text),
177 				    &link);
178 		RTE_LOG(INFO, USER1, "Port %u %s\n",
179 			port,
180 			link_status_text);
181 		if (link.link_status == RTE_ETH_LINK_DOWN)
182 			all_ports_up = 0;
183 	}
184 
185 	if (all_ports_up == 0)
186 		rte_panic("Some NIC ports are DOWN\n");
187 }
188 
189 static void
190 app_init_ports(void)
191 {
192 	uint32_t i;
193 
194 	/* Init NIC ports, then start the ports */
195 	for (i = 0; i < app.n_ports; i++) {
196 		uint16_t port;
197 		int ret;
198 
199 		port = app.ports[i];
200 		RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port);
201 
202 		/* Init port */
203 		ret = rte_eth_dev_configure(
204 			port,
205 			1,
206 			1,
207 			&port_conf);
208 		if (ret < 0)
209 			rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
210 
211 		ret = rte_eth_promiscuous_enable(port);
212 		if (ret != 0)
213 			rte_panic("Cannot enable promiscuous mode for port %u: %s\n",
214 				port, rte_strerror(-ret));
215 
216 		/* Init RX queues */
217 		ret = rte_eth_rx_queue_setup(
218 			port,
219 			0,
220 			app.port_rx_ring_size,
221 			rte_eth_dev_socket_id(port),
222 			&rx_conf,
223 			app.pool);
224 		if (ret < 0)
225 			rte_panic("Cannot init RX for port %u (%d)\n",
226 				(uint32_t) port, ret);
227 
228 		/* Init TX queues */
229 		ret = rte_eth_tx_queue_setup(
230 			port,
231 			0,
232 			app.port_tx_ring_size,
233 			rte_eth_dev_socket_id(port),
234 			&tx_conf);
235 		if (ret < 0)
236 			rte_panic("Cannot init TX for port %u (%d)\n",
237 				(uint32_t) port, ret);
238 
239 		/* Start port */
240 		ret = rte_eth_dev_start(port);
241 		if (ret < 0)
242 			rte_panic("Cannot start port %u (%d)\n", port, ret);
243 	}
244 
245 	app_ports_check_link();
246 }
247 
248 void
249 app_init(void)
250 {
251 	app_init_mbuf_pools();
252 	app_init_rings();
253 	app_init_ports();
254 
255 	RTE_LOG(INFO, USER1, "Initialization completed\n");
256 }
257