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