xref: /dpdk/examples/vm_power_manager/main.c (revision 69a3c6319140b34fb714fa5bd6990cceb2ea2997)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <sys/epoll.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <errno.h>
14 
15 #include <sys/queue.h>
16 
17 #include <rte_common.h>
18 #include <rte_eal.h>
19 #include <rte_launch.h>
20 #include <rte_log.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_ethdev.h>
24 #include <getopt.h>
25 #include <rte_cycles.h>
26 #include <rte_debug.h>
27 
28 #include "channel_manager.h"
29 #include "channel_monitor.h"
30 #include "power_manager.h"
31 #include "vm_power_cli.h"
32 #include "oob_monitor.h"
33 #include "parse.h"
34 #ifdef RTE_NET_IXGBE
35 #include <rte_pmd_ixgbe.h>
36 #endif
37 #ifdef RTE_NET_I40E
38 #include <rte_pmd_i40e.h>
39 #endif
40 #ifdef RTE_NET_BNXT
41 #include <rte_pmd_bnxt.h>
42 #endif
43 
44 #define RX_RING_SIZE 1024
45 #define TX_RING_SIZE 1024
46 
47 #define NUM_MBUFS 8191
48 #define MBUF_CACHE_SIZE 250
49 #define BURST_SIZE 32
50 
51 static uint32_t enabled_port_mask;
52 static volatile bool force_quit;
53 
54 /****************/
55 static const struct rte_eth_conf port_conf_default = {
56 	.rxmode = {
57 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
58 	},
59 };
60 
61 static inline int
62 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
63 {
64 	struct rte_eth_conf port_conf = port_conf_default;
65 	const uint16_t rx_rings = 1, tx_rings = 1;
66 	int retval;
67 	uint16_t q;
68 	struct rte_eth_dev_info dev_info;
69 	struct rte_eth_txconf txq_conf;
70 
71 	if (!rte_eth_dev_is_valid_port(port))
72 		return -1;
73 
74 	retval = rte_eth_dev_info_get(port, &dev_info);
75 	if (retval != 0) {
76 		printf("Error during getting device (port %u) info: %s\n",
77 				port, strerror(-retval));
78 		return retval;
79 	}
80 
81 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
82 		port_conf.txmode.offloads |=
83 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
84 
85 	/* Configure the Ethernet device. */
86 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
87 	if (retval != 0)
88 		return retval;
89 
90 	/* Allocate and set up 1 RX queue per Ethernet port. */
91 	for (q = 0; q < rx_rings; q++) {
92 		retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
93 				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
94 		if (retval < 0)
95 			return retval;
96 	}
97 
98 	txq_conf = dev_info.default_txconf;
99 	txq_conf.offloads = port_conf.txmode.offloads;
100 	/* Allocate and set up 1 TX queue per Ethernet port. */
101 	for (q = 0; q < tx_rings; q++) {
102 		retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
103 				rte_eth_dev_socket_id(port), &txq_conf);
104 		if (retval < 0)
105 			return retval;
106 	}
107 
108 	/* Start the Ethernet port. */
109 	retval = rte_eth_dev_start(port);
110 	if (retval < 0)
111 		return retval;
112 
113 	/* Display the port MAC address. */
114 	struct rte_ether_addr addr;
115 	retval = rte_eth_macaddr_get(port, &addr);
116 	if (retval != 0) {
117 		printf("Failed to get device (port %u) MAC address: %s\n",
118 				port, rte_strerror(-retval));
119 		return retval;
120 	}
121 
122 	printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
123 			   " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
124 			(unsigned int)port, RTE_ETHER_ADDR_BYTES(&addr));
125 
126 	/* Enable RX in promiscuous mode for the Ethernet device. */
127 	retval = rte_eth_promiscuous_enable(port);
128 	if (retval != 0)
129 		return retval;
130 
131 
132 	return 0;
133 }
134 
135 static int
136 parse_portmask(const char *portmask)
137 {
138 	char *end = NULL;
139 	unsigned long pm;
140 
141 	/* parse hexadecimal string */
142 	pm = strtoul(portmask, &end, 16);
143 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
144 		return 0;
145 
146 	return pm;
147 }
148 /* Parse the argument given in the command line of the application */
149 static int
150 parse_args(int argc, char **argv)
151 {
152 	int opt, ret, cnt, i;
153 	char **argvopt;
154 	uint16_t *oob_enable;
155 	int option_index;
156 	char *prgname = argv[0];
157 	struct core_info *ci;
158 	float branch_ratio;
159 	static struct option lgopts[] = {
160 		{ "mac-updating", no_argument, 0, 1},
161 		{ "no-mac-updating", no_argument, 0, 0},
162 		{ "core-branch-ratio", optional_argument, 0, 'b'},
163 		{ "port-list", optional_argument, 0, 'p'},
164 		{NULL, 0, 0, 0}
165 	};
166 	argvopt = argv;
167 	ci = get_core_info();
168 
169 	while ((opt = getopt_long(argc, argvopt, "p:q:T:b:",
170 				  lgopts, &option_index)) != EOF) {
171 
172 		switch (opt) {
173 		/* portmask */
174 		case 'p':
175 			enabled_port_mask = parse_portmask(optarg);
176 			if (enabled_port_mask == 0) {
177 				printf("invalid portmask\n");
178 				return -1;
179 			}
180 			break;
181 		case 'b':
182 			branch_ratio = BRANCH_RATIO_THRESHOLD;
183 			oob_enable = malloc(ci->core_count * sizeof(uint16_t));
184 			if (oob_enable == NULL) {
185 				printf("Error - Unable to allocate memory\n");
186 				return -1;
187 			}
188 			cnt = parse_set(optarg, oob_enable, ci->core_count);
189 			if (cnt < 0) {
190 				printf("Invalid core-list section in "
191 				       "core-branch-ratio matrix - [%s]\n",
192 						optarg);
193 				free(oob_enable);
194 				break;
195 			}
196 			cnt = parse_branch_ratio(optarg, &branch_ratio);
197 			if (cnt < 0) {
198 				printf("Invalid branch-ratio section in "
199 				       "core-branch-ratio matrix - [%s]\n",
200 						optarg);
201 				free(oob_enable);
202 				break;
203 			}
204 			if (branch_ratio <= 0.0 || branch_ratio > 100.0) {
205 				printf("invalid branch ratio specified\n");
206 				free(oob_enable);
207 				return -1;
208 			}
209 			for (i = 0; i < ci->core_count; i++) {
210 				if (oob_enable[i]) {
211 					printf("***Using core %d "
212 					       "with branch ratio %f\n",
213 					       i, branch_ratio);
214 					ci->cd[i].oob_enabled = 1;
215 					ci->cd[i].global_enabled_cpus = 1;
216 					ci->cd[i].branch_ratio_threshold =
217 								branch_ratio;
218 				}
219 			}
220 			free(oob_enable);
221 			break;
222 		/* long options */
223 		case 0:
224 			break;
225 
226 		default:
227 			return -1;
228 		}
229 	}
230 
231 	if (optind >= 0)
232 		argv[optind-1] = prgname;
233 
234 	ret = optind-1;
235 	optind = 0; /* reset getopt lib */
236 	return ret;
237 }
238 
239 static void
240 check_all_ports_link_status(uint32_t port_mask)
241 {
242 #define CHECK_INTERVAL 100 /* 100ms */
243 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
244 	uint16_t portid, count, all_ports_up, print_flag = 0;
245 	struct rte_eth_link link;
246 	int ret;
247 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
248 
249 	printf("\nChecking link status");
250 	fflush(stdout);
251 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
252 		if (force_quit)
253 			return;
254 		all_ports_up = 1;
255 		RTE_ETH_FOREACH_DEV(portid) {
256 			if (force_quit)
257 				return;
258 			if ((port_mask & (1 << portid)) == 0)
259 				continue;
260 			memset(&link, 0, sizeof(link));
261 			ret = rte_eth_link_get_nowait(portid, &link);
262 			if (ret < 0) {
263 				all_ports_up = 0;
264 				if (print_flag == 1)
265 					printf("Port %u link get failed: %s\n",
266 						portid, rte_strerror(-ret));
267 				continue;
268 			}
269 			/* print link status if flag set */
270 			if (print_flag == 1) {
271 				rte_eth_link_to_str(link_status_text,
272 					sizeof(link_status_text), &link);
273 				printf("Port %d %s\n", portid,
274 				       link_status_text);
275 				continue;
276 			}
277 		       /* clear all_ports_up flag if any link down */
278 			if (link.link_status == ETH_LINK_DOWN) {
279 				all_ports_up = 0;
280 				break;
281 			}
282 		}
283 		/* after finally printing all link status, get out */
284 		if (print_flag == 1)
285 			break;
286 
287 		if (all_ports_up == 0) {
288 			printf(".");
289 			fflush(stdout);
290 			rte_delay_ms(CHECK_INTERVAL);
291 		}
292 
293 		/* set the print_flag if all ports up or timeout */
294 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
295 			print_flag = 1;
296 			printf("done\n");
297 		}
298 	}
299 }
300 static int
301 run_monitor(__rte_unused void *arg)
302 {
303 	if (channel_monitor_init() < 0) {
304 		printf("Unable to initialize channel monitor\n");
305 		return -1;
306 	}
307 	run_channel_monitor();
308 	return 0;
309 }
310 
311 static int
312 run_core_monitor(__rte_unused void *arg)
313 {
314 	if (branch_monitor_init() < 0) {
315 		printf("Unable to initialize core monitor\n");
316 		return -1;
317 	}
318 	run_branch_monitor();
319 	return 0;
320 }
321 
322 static void
323 sig_handler(int signo)
324 {
325 	printf("Received signal %d, exiting...\n", signo);
326 	channel_monitor_exit();
327 	channel_manager_exit();
328 	power_manager_exit();
329 
330 }
331 
332 int
333 main(int argc, char **argv)
334 {
335 	int ret;
336 	unsigned lcore_id;
337 	unsigned int nb_ports;
338 	struct rte_mempool *mbuf_pool;
339 	uint16_t portid;
340 	struct core_info *ci;
341 
342 
343 	ret = core_info_init();
344 	if (ret < 0)
345 		rte_panic("Cannot allocate core info\n");
346 
347 	ci = get_core_info();
348 
349 	ret = rte_eal_init(argc, argv);
350 	if (ret < 0)
351 		rte_panic("Cannot init EAL\n");
352 
353 	signal(SIGINT, sig_handler);
354 	signal(SIGTERM, sig_handler);
355 
356 	argc -= ret;
357 	argv += ret;
358 
359 	/* parse application arguments (after the EAL ones) */
360 	ret = parse_args(argc, argv);
361 	if (ret < 0)
362 		rte_exit(EXIT_FAILURE, "Invalid arguments\n");
363 
364 	nb_ports = rte_eth_dev_count_avail();
365 
366 	if (nb_ports > 0) {
367 		mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
368 				NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
369 				RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
370 
371 		if (mbuf_pool == NULL)
372 			rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
373 
374 		/* Initialize ports. */
375 		RTE_ETH_FOREACH_DEV(portid) {
376 			struct rte_ether_addr eth;
377 			int w, j;
378 			int ret;
379 
380 			if ((enabled_port_mask & (1 << portid)) == 0)
381 				continue;
382 
383 			eth.addr_bytes[0] = 0xe0;
384 			eth.addr_bytes[1] = 0xe0;
385 			eth.addr_bytes[2] = 0xe0;
386 			eth.addr_bytes[3] = 0xe0;
387 			eth.addr_bytes[4] = portid + 0xf0;
388 
389 			if (port_init(portid, mbuf_pool) != 0)
390 				rte_exit(EXIT_FAILURE,
391 					"Cannot init port %"PRIu8 "\n",
392 					portid);
393 
394 			for (w = 0; w < RTE_POWER_MAX_VFS; w++) {
395 				eth.addr_bytes[5] = w + 0xf0;
396 
397 				ret = -ENOTSUP;
398 #ifdef RTE_NET_IXGBE
399 				ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
400 							w, &eth);
401 #endif
402 #ifdef RTE_NET_I40E
403 				if (ret == -ENOTSUP)
404 					ret = rte_pmd_i40e_set_vf_mac_addr(
405 							portid, w, &eth);
406 #endif
407 #ifdef RTE_NET_BNXT
408 				if (ret == -ENOTSUP)
409 					ret = rte_pmd_bnxt_set_vf_mac_addr(
410 							portid, w, &eth);
411 #endif
412 
413 				switch (ret) {
414 				case 0:
415 					printf("Port %d VF %d MAC: ",
416 							portid, w);
417 					for (j = 0; j < 5; j++) {
418 						printf("%02x:",
419 							eth.addr_bytes[j]);
420 					}
421 					printf("%02x\n", eth.addr_bytes[5]);
422 					break;
423 				}
424 				printf("\n");
425 			}
426 		}
427 	}
428 
429 	check_all_ports_link_status(enabled_port_mask);
430 
431 	lcore_id = rte_get_next_lcore(-1, 1, 0);
432 	if (lcore_id == RTE_MAX_LCORE) {
433 		RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
434 				"application\n");
435 		return 0;
436 	}
437 	printf("Running channel monitor on lcore id %d\n", lcore_id);
438 	rte_eal_remote_launch(run_monitor, NULL, lcore_id);
439 
440 	lcore_id = rte_get_next_lcore(lcore_id, 1, 0);
441 	if (lcore_id == RTE_MAX_LCORE) {
442 		RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
443 				"application\n");
444 		return 0;
445 	}
446 	if (power_manager_init() < 0) {
447 		printf("Unable to initialize power manager\n");
448 		return -1;
449 	}
450 	if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
451 		printf("Unable to initialize channel manager\n");
452 		return -1;
453 	}
454 
455 	add_host_channels();
456 
457 	printf("Running core monitor on lcore id %d\n", lcore_id);
458 	rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);
459 
460 	run_cli(NULL);
461 
462 	branch_monitor_exit();
463 
464 	rte_eal_mp_wait_lcore();
465 
466 	free(ci->cd);
467 
468 	/* clean up the EAL */
469 	rte_eal_cleanup();
470 
471 	return 0;
472 }
473