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