xref: /dpdk/examples/qos_sched/init.c (revision af0785a2447b307965377b62f46a5f39457a85a3)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <memory.h>
8 
9 #include <rte_log.h>
10 #include <rte_mbuf.h>
11 #include <rte_debug.h>
12 #include <rte_ethdev.h>
13 #include <rte_mempool.h>
14 #include <rte_sched.h>
15 #include <rte_cycles.h>
16 #include <rte_string_fns.h>
17 #include <rte_cfgfile.h>
18 
19 #include "main.h"
20 #include "cfg_file.h"
21 
22 uint32_t app_numa_mask = 0;
23 static uint32_t app_inited_port_mask = 0;
24 
25 int app_pipe_to_profile[MAX_SCHED_SUBPORTS][MAX_SCHED_PIPES];
26 
27 #define MAX_NAME_LEN 32
28 
29 struct ring_conf ring_conf = {
30 	.rx_size   = APP_RX_DESC_DEFAULT,
31 	.ring_size = APP_RING_SIZE,
32 	.tx_size   = APP_TX_DESC_DEFAULT,
33 };
34 
35 struct burst_conf burst_conf = {
36 	.rx_burst    = MAX_PKT_RX_BURST,
37 	.ring_burst  = PKT_ENQUEUE,
38 	.qos_dequeue = PKT_DEQUEUE,
39 	.tx_burst    = MAX_PKT_TX_BURST,
40 };
41 
42 struct ring_thresh rx_thresh = {
43 	.pthresh = RX_PTHRESH,
44 	.hthresh = RX_HTHRESH,
45 	.wthresh = RX_WTHRESH,
46 };
47 
48 struct ring_thresh tx_thresh = {
49 	.pthresh = TX_PTHRESH,
50 	.hthresh = TX_HTHRESH,
51 	.wthresh = TX_WTHRESH,
52 };
53 
54 uint32_t nb_pfc;
55 const char *cfg_profile = NULL;
56 int mp_size = NB_MBUF;
57 struct flow_conf qos_conf[MAX_DATA_STREAMS];
58 
59 static struct rte_eth_conf port_conf = {
60 	.txmode = {
61 		.mq_mode = RTE_ETH_MQ_TX_NONE,
62 	},
63 };
64 
65 static int
66 app_init_port(uint16_t portid, struct rte_mempool *mp)
67 {
68 	int ret;
69 	struct rte_eth_link link;
70 	struct rte_eth_dev_info dev_info;
71 	struct rte_eth_rxconf rx_conf;
72 	struct rte_eth_txconf tx_conf;
73 	uint16_t rx_size;
74 	uint16_t tx_size;
75 	struct rte_eth_conf local_port_conf = port_conf;
76 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
77 
78 	/* check if port already initialized (multistream configuration) */
79 	if (app_inited_port_mask & (1u << portid))
80 		return 0;
81 
82 	rx_conf.rx_thresh.pthresh = rx_thresh.pthresh;
83 	rx_conf.rx_thresh.hthresh = rx_thresh.hthresh;
84 	rx_conf.rx_thresh.wthresh = rx_thresh.wthresh;
85 	rx_conf.rx_free_thresh = 32;
86 	rx_conf.rx_drop_en = 0;
87 	rx_conf.rx_deferred_start = 0;
88 
89 	tx_conf.tx_thresh.pthresh = tx_thresh.pthresh;
90 	tx_conf.tx_thresh.hthresh = tx_thresh.hthresh;
91 	tx_conf.tx_thresh.wthresh = tx_thresh.wthresh;
92 	tx_conf.tx_free_thresh = 0;
93 	tx_conf.tx_rs_thresh = 0;
94 	tx_conf.tx_deferred_start = 0;
95 
96 	/* init port */
97 	RTE_LOG(INFO, APP, "Initializing port %"PRIu16"... ", portid);
98 	fflush(stdout);
99 
100 	ret = rte_eth_dev_info_get(portid, &dev_info);
101 	if (ret != 0)
102 		rte_exit(EXIT_FAILURE,
103 			"Error during getting device (port %u) info: %s\n",
104 			portid, strerror(-ret));
105 
106 	if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
107 		local_port_conf.txmode.offloads |=
108 			RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
109 	ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
110 	if (ret < 0)
111 		rte_exit(EXIT_FAILURE,
112 			 "Cannot configure device: err=%d, port=%u\n",
113 			 ret, portid);
114 
115 	rx_size = ring_conf.rx_size;
116 	tx_size = ring_conf.tx_size;
117 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &rx_size, &tx_size);
118 	if (ret < 0)
119 		rte_exit(EXIT_FAILURE,
120 			 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d,port=%u\n",
121 			 ret, portid);
122 	ring_conf.rx_size = rx_size;
123 	ring_conf.tx_size = tx_size;
124 
125 	/* init one RX queue */
126 	fflush(stdout);
127 	rx_conf.offloads = local_port_conf.rxmode.offloads;
128 	ret = rte_eth_rx_queue_setup(portid, 0, (uint16_t)ring_conf.rx_size,
129 		rte_eth_dev_socket_id(portid), &rx_conf, mp);
130 	if (ret < 0)
131 		rte_exit(EXIT_FAILURE,
132 			 "rte_eth_tx_queue_setup: err=%d, port=%u\n",
133 			 ret, portid);
134 
135 	/* init one TX queue */
136 	fflush(stdout);
137 	tx_conf.offloads = local_port_conf.txmode.offloads;
138 	ret = rte_eth_tx_queue_setup(portid, 0,
139 		(uint16_t)ring_conf.tx_size, rte_eth_dev_socket_id(portid), &tx_conf);
140 	if (ret < 0)
141 		rte_exit(EXIT_FAILURE,
142 			 "rte_eth_tx_queue_setup: err=%d, port=%u queue=%d\n",
143 			 ret, portid, 0);
144 
145 	/* Start device */
146 	ret = rte_eth_dev_start(portid);
147 	if (ret < 0)
148 		rte_exit(EXIT_FAILURE,
149 			 "rte_pmd_port_start: err=%d, port=%u\n",
150 			 ret, portid);
151 
152 	printf("done: ");
153 
154 	/* get link status */
155 	ret = rte_eth_link_get(portid, &link);
156 	if (ret < 0)
157 		rte_exit(EXIT_FAILURE,
158 			 "rte_eth_link_get: err=%d, port=%u: %s\n",
159 			 ret, portid, rte_strerror(-ret));
160 
161 	rte_eth_link_to_str(link_status_text, sizeof(link_status_text), &link);
162 	printf("%s\n", link_status_text);
163 
164 	ret = rte_eth_promiscuous_enable(portid);
165 	if (ret != 0)
166 		rte_exit(EXIT_FAILURE,
167 			"rte_eth_promiscuous_enable: err=%s, port=%u\n",
168 			rte_strerror(-ret), portid);
169 
170 	/* mark port as initialized */
171 	app_inited_port_mask |= 1u << portid;
172 
173 	return 0;
174 }
175 
176 static struct rte_sched_pipe_params pipe_profiles[MAX_SCHED_PIPE_PROFILES] = {
177 	{ /* Profile #0 */
178 		.tb_rate = 305175,
179 		.tb_size = 1000000,
180 
181 		.tc_rate = {305175, 305175, 305175, 305175, 305175, 305175,
182 			305175, 305175, 305175, 305175, 305175, 305175, 305175},
183 		.tc_period = 40,
184 		.tc_ov_weight = 1,
185 
186 		.wrr_weights = {1, 1, 1, 1},
187 	},
188 };
189 
190 static struct rte_sched_subport_profile_params
191 		subport_profile[MAX_SCHED_SUBPORT_PROFILES] = {
192 	{
193 		.tb_rate = 1250000000,
194 		.tb_size = 1000000,
195 		.tc_rate = {1250000000, 1250000000, 1250000000, 1250000000,
196 			1250000000, 1250000000, 1250000000, 1250000000, 1250000000,
197 			1250000000, 1250000000, 1250000000, 1250000000},
198 		.tc_period = 10,
199 	},
200 };
201 
202 struct rte_sched_subport_params subport_params[MAX_SCHED_SUBPORTS] = {
203 	{
204 		.n_pipes_per_subport_enabled = 4096,
205 		.qsize = {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
206 		.pipe_profiles = pipe_profiles,
207 		.n_pipe_profiles = sizeof(pipe_profiles) /
208 			sizeof(struct rte_sched_pipe_params),
209 		.n_max_pipe_profiles = MAX_SCHED_PIPE_PROFILES,
210 		.cman_params = NULL,
211 	},
212 };
213 
214 struct rte_sched_port_params port_params = {
215 	.name = "port_scheduler_0",
216 	.socket = 0, /* computed */
217 	.rate = 0, /* computed */
218 	.mtu = 6 + 6 + 4 + 4 + 2 + 1500,
219 	.frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
220 	.n_subports_per_port = 1,
221 	.n_subport_profiles = 1,
222 	.subport_profiles = subport_profile,
223 	.n_max_subport_profiles = MAX_SCHED_SUBPORT_PROFILES,
224 	.n_pipes_per_subport = MAX_SCHED_PIPES,
225 };
226 
227 static struct rte_sched_port *
228 app_init_sched_port(uint32_t portid, uint32_t socketid)
229 {
230 	static char port_name[32]; /* static as referenced from global port_params*/
231 	struct rte_eth_link link;
232 	struct rte_sched_port *port = NULL;
233 	uint32_t pipe, subport;
234 	int err;
235 
236 	err = rte_eth_link_get(portid, &link);
237 	if (err < 0)
238 		rte_exit(EXIT_FAILURE,
239 			 "rte_eth_link_get: err=%d, port=%u: %s\n",
240 			 err, portid, rte_strerror(-err));
241 
242 	port_params.socket = socketid;
243 	port_params.rate = (uint64_t) link.link_speed * 1000 * 1000 / 8;
244 	snprintf(port_name, sizeof(port_name), "port_%d", portid);
245 	port_params.name = port_name;
246 
247 	port = rte_sched_port_config(&port_params);
248 	if (port == NULL){
249 		rte_exit(EXIT_FAILURE, "Unable to config sched port\n");
250 	}
251 
252 	for (subport = 0; subport < port_params.n_subports_per_port; subport ++) {
253 		err = rte_sched_subport_config(port, subport,
254 				&subport_params[subport],
255 				0);
256 		if (err) {
257 			rte_exit(EXIT_FAILURE, "Unable to config sched "
258 				 "subport %u, err=%d\n", subport, err);
259 		}
260 
261 		uint32_t n_pipes_per_subport =
262 			subport_params[subport].n_pipes_per_subport_enabled;
263 
264 		for (pipe = 0; pipe < n_pipes_per_subport; pipe++) {
265 			if (app_pipe_to_profile[subport][pipe] != -1) {
266 				err = rte_sched_pipe_config(port, subport, pipe,
267 						app_pipe_to_profile[subport][pipe]);
268 				if (err) {
269 					rte_exit(EXIT_FAILURE, "Unable to config sched pipe %u "
270 							"for profile %d, err=%d\n", pipe,
271 							app_pipe_to_profile[subport][pipe], err);
272 				}
273 			}
274 		}
275 	}
276 
277 	return port;
278 }
279 
280 static int
281 app_load_cfg_profile(const char *profile)
282 {
283 	int ret  = 0;
284 	if (profile == NULL)
285 		return 0;
286 	struct rte_cfgfile *file = rte_cfgfile_load(profile, 0);
287 	if (file == NULL)
288 		rte_exit(EXIT_FAILURE, "Cannot load configuration profile %s\n", profile);
289 
290 	ret = cfg_load_port(file, &port_params);
291 	if (ret)
292 		goto _app_load_cfg_profile_error_return;
293 
294 	ret = cfg_load_subport(file, subport_params);
295 	if (ret)
296 		goto _app_load_cfg_profile_error_return;
297 
298 	ret = cfg_load_subport_profile(file, subport_profile);
299 	if (ret)
300 		goto _app_load_cfg_profile_error_return;
301 
302 	ret = cfg_load_pipe(file, pipe_profiles);
303 	if (ret)
304 		goto _app_load_cfg_profile_error_return;
305 
306 _app_load_cfg_profile_error_return:
307 	rte_cfgfile_close(file);
308 
309 	return ret;
310 }
311 
312 int app_init(void)
313 {
314 	uint32_t i;
315 	char ring_name[MAX_NAME_LEN];
316 	char pool_name[MAX_NAME_LEN];
317 
318 	if (rte_eth_dev_count_avail() == 0)
319 		rte_exit(EXIT_FAILURE, "No Ethernet port - bye\n");
320 
321 	/* load configuration profile */
322 	if (app_load_cfg_profile(cfg_profile) != 0)
323 		rte_exit(EXIT_FAILURE, "Invalid configuration profile\n");
324 
325 	/* Initialize each active flow */
326 	for(i = 0; i < nb_pfc; i++) {
327 		uint32_t socket = rte_lcore_to_socket_id(qos_conf[i].rx_core);
328 		struct rte_ring *ring;
329 
330 		snprintf(ring_name, MAX_NAME_LEN, "ring-%u-%u", i, qos_conf[i].rx_core);
331 		ring = rte_ring_lookup(ring_name);
332 		if (ring == NULL)
333 			qos_conf[i].rx_ring = rte_ring_create(ring_name, ring_conf.ring_size,
334 			 	socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
335 		else
336 			qos_conf[i].rx_ring = ring;
337 
338 		snprintf(ring_name, MAX_NAME_LEN, "ring-%u-%u", i, qos_conf[i].tx_core);
339 		ring = rte_ring_lookup(ring_name);
340 		if (ring == NULL)
341 			qos_conf[i].tx_ring = rte_ring_create(ring_name, ring_conf.ring_size,
342 				socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
343 		else
344 			qos_conf[i].tx_ring = ring;
345 
346 
347 		/* create the mbuf pools for each RX Port */
348 		snprintf(pool_name, MAX_NAME_LEN, "mbuf_pool%u", i);
349 		qos_conf[i].mbuf_pool = rte_pktmbuf_pool_create(pool_name,
350 			mp_size, burst_conf.rx_burst * 4, 0,
351 			RTE_MBUF_DEFAULT_BUF_SIZE,
352 			rte_eth_dev_socket_id(qos_conf[i].rx_port));
353 		if (qos_conf[i].mbuf_pool == NULL)
354 			rte_exit(EXIT_FAILURE, "Cannot init mbuf pool for socket %u\n", i);
355 
356 		app_init_port(qos_conf[i].rx_port, qos_conf[i].mbuf_pool);
357 		app_init_port(qos_conf[i].tx_port, qos_conf[i].mbuf_pool);
358 
359 		qos_conf[i].sched_port = app_init_sched_port(qos_conf[i].tx_port, socket);
360 	}
361 
362 	RTE_LOG(INFO, APP, "time stamp clock running at %" PRIu64 " Hz\n",
363 			 rte_get_timer_hz());
364 
365 	RTE_LOG(INFO, APP, "Ring sizes: NIC RX = %u, Mempool = %d SW queue = %u,"
366 			 "NIC TX = %u\n", ring_conf.rx_size, mp_size, ring_conf.ring_size,
367 			 ring_conf.tx_size);
368 
369 	RTE_LOG(INFO, APP, "Burst sizes: RX read = %hu, RX write = %hu,\n"
370 						  "             Worker read/QoS enqueue = %hu,\n"
371 						  "             QoS dequeue = %hu, Worker write = %hu\n",
372 		burst_conf.rx_burst, burst_conf.ring_burst, burst_conf.ring_burst,
373 		burst_conf.qos_dequeue, burst_conf.tx_burst);
374 
375 	RTE_LOG(INFO, APP, "NIC thresholds RX (p = %hhu, h = %hhu, w = %hhu),"
376 				 "TX (p = %hhu, h = %hhu, w = %hhu)\n",
377 		rx_thresh.pthresh, rx_thresh.hthresh, rx_thresh.wthresh,
378 		tx_thresh.pthresh, tx_thresh.hthresh, tx_thresh.wthresh);
379 
380 	return 0;
381 }
382