1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <sys/queue.h>
17 #include <rte_common.h>
19 #include <rte_launch.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_ethdev.h>
25 #include <rte_cycles.h>
26 #include <rte_debug.h>
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"
34 #include <rte_pmd_ixgbe.h>
35 #include <rte_pmd_i40e.h>
36 #include <rte_pmd_bnxt.h>
38 #define RX_RING_SIZE 1024
39 #define TX_RING_SIZE 1024
41 #define NUM_MBUFS 8191
42 #define MBUF_CACHE_SIZE 250
45 static uint32_t enabled_port_mask;
46 static volatile bool force_quit;
49 static const struct rte_eth_conf port_conf_default = {
51 .max_rx_pkt_len = ETHER_MAX_LEN,
56 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
58 struct rte_eth_conf port_conf = port_conf_default;
59 const uint16_t rx_rings = 1, tx_rings = 1;
62 struct rte_eth_dev_info dev_info;
63 struct rte_eth_txconf txq_conf;
65 if (!rte_eth_dev_is_valid_port(port))
68 rte_eth_dev_info_get(port, &dev_info);
69 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
70 port_conf.txmode.offloads |=
71 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
73 /* Configure the Ethernet device. */
74 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
78 /* Allocate and set up 1 RX queue per Ethernet port. */
79 for (q = 0; q < rx_rings; q++) {
80 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
81 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
86 txq_conf = dev_info.default_txconf;
87 txq_conf.offloads = port_conf.txmode.offloads;
88 /* Allocate and set up 1 TX queue per Ethernet port. */
89 for (q = 0; q < tx_rings; q++) {
90 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
91 rte_eth_dev_socket_id(port), &txq_conf);
96 /* Start the Ethernet port. */
97 retval = rte_eth_dev_start(port);
101 /* Display the port MAC address. */
102 struct ether_addr addr;
103 rte_eth_macaddr_get(port, &addr);
104 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
105 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
107 addr.addr_bytes[0], addr.addr_bytes[1],
108 addr.addr_bytes[2], addr.addr_bytes[3],
109 addr.addr_bytes[4], addr.addr_bytes[5]);
111 /* Enable RX in promiscuous mode for the Ethernet device. */
112 rte_eth_promiscuous_enable(port);
119 parse_portmask(const char *portmask)
124 /* parse hexadecimal string */
125 pm = strtoul(portmask, &end, 16);
126 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
134 /* Parse the argument given in the command line of the application */
136 parse_args(int argc, char **argv)
138 int opt, ret, cnt, i;
140 uint16_t *oob_enable;
142 char *prgname = argv[0];
143 struct core_info *ci;
145 static struct option lgopts[] = {
146 { "mac-updating", no_argument, 0, 1},
147 { "no-mac-updating", no_argument, 0, 0},
148 { "core-list", optional_argument, 0, 'l'},
149 { "port-list", optional_argument, 0, 'p'},
150 { "branch-ratio", optional_argument, 0, 'b'},
154 ci = get_core_info();
156 while ((opt = getopt_long(argc, argvopt, "l:p:q:T:b:",
157 lgopts, &option_index)) != EOF) {
162 enabled_port_mask = parse_portmask(optarg);
163 if (enabled_port_mask == 0) {
164 printf("invalid portmask\n");
169 oob_enable = malloc(ci->core_count * sizeof(uint16_t));
170 if (oob_enable == NULL) {
171 printf("Error - Unable to allocate memory\n");
174 cnt = parse_set(optarg, oob_enable, ci->core_count);
176 printf("Invalid core-list - [%s]\n",
180 for (i = 0; i < ci->core_count; i++) {
182 printf("***Using core %d\n", i);
183 ci->cd[i].oob_enabled = 1;
184 ci->cd[i].global_enabled_cpus = 1;
192 branch_ratio = atof(optarg);
193 if (branch_ratio <= 0.0) {
194 printf("invalid branch ratio specified\n");
197 ci->branch_ratio_threshold = branch_ratio;
198 printf("***Setting branch ratio to %f\n",
211 argv[optind-1] = prgname;
214 optind = 0; /* reset getopt lib */
219 check_all_ports_link_status(uint32_t port_mask)
221 #define CHECK_INTERVAL 100 /* 100ms */
222 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
223 uint16_t portid, count, all_ports_up, print_flag = 0;
224 struct rte_eth_link link;
226 printf("\nChecking link status");
228 for (count = 0; count <= MAX_CHECK_TIME; count++) {
232 RTE_ETH_FOREACH_DEV(portid) {
235 if ((port_mask & (1 << portid)) == 0)
237 memset(&link, 0, sizeof(link));
238 rte_eth_link_get_nowait(portid, &link);
239 /* print link status if flag set */
240 if (print_flag == 1) {
241 if (link.link_status)
242 printf("Port %d Link Up - speed %u "
243 "Mbps - %s\n", (uint16_t)portid,
244 (unsigned int)link.link_speed,
245 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
246 ("full-duplex") : ("half-duplex\n"));
248 printf("Port %d Link Down\n",
252 /* clear all_ports_up flag if any link down */
253 if (link.link_status == ETH_LINK_DOWN) {
258 /* after finally printing all link status, get out */
262 if (all_ports_up == 0) {
265 rte_delay_ms(CHECK_INTERVAL);
268 /* set the print_flag if all ports up or timeout */
269 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
276 run_monitor(__attribute__((unused)) void *arg)
278 if (channel_monitor_init() < 0) {
279 printf("Unable to initialize channel monitor\n");
282 run_channel_monitor();
287 run_core_monitor(__attribute__((unused)) void *arg)
289 if (branch_monitor_init() < 0) {
290 printf("Unable to initialize core monitor\n");
293 run_branch_monitor();
298 sig_handler(int signo)
300 printf("Received signal %d, exiting...\n", signo);
301 channel_monitor_exit();
302 channel_manager_exit();
303 power_manager_exit();
308 main(int argc, char **argv)
312 unsigned int nb_ports;
313 struct rte_mempool *mbuf_pool;
315 struct core_info *ci;
318 ret = core_info_init();
320 rte_panic("Cannot allocate core info\n");
322 ci = get_core_info();
324 ret = rte_eal_init(argc, argv);
326 rte_panic("Cannot init EAL\n");
328 signal(SIGINT, sig_handler);
329 signal(SIGTERM, sig_handler);
334 /* parse application arguments (after the EAL ones) */
335 ret = parse_args(argc, argv);
337 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
339 nb_ports = rte_eth_dev_count_avail();
342 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
343 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
344 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
346 if (mbuf_pool == NULL)
347 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
349 /* Initialize ports. */
350 RTE_ETH_FOREACH_DEV(portid) {
351 struct ether_addr eth;
355 if ((enabled_port_mask & (1 << portid)) == 0)
358 eth.addr_bytes[0] = 0xe0;
359 eth.addr_bytes[1] = 0xe0;
360 eth.addr_bytes[2] = 0xe0;
361 eth.addr_bytes[3] = 0xe0;
362 eth.addr_bytes[4] = portid + 0xf0;
364 if (port_init(portid, mbuf_pool) != 0)
365 rte_exit(EXIT_FAILURE,
366 "Cannot init port %"PRIu8 "\n",
369 for (w = 0; w < MAX_VFS; w++) {
370 eth.addr_bytes[5] = w + 0xf0;
372 ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
375 ret = rte_pmd_i40e_set_vf_mac_addr(
378 ret = rte_pmd_bnxt_set_vf_mac_addr(
383 printf("Port %d VF %d MAC: ",
385 for (j = 0; j < 5; j++) {
389 printf("%02x\n", eth.addr_bytes[5]);
398 check_all_ports_link_status(enabled_port_mask);
400 lcore_id = rte_get_next_lcore(-1, 1, 0);
401 if (lcore_id == RTE_MAX_LCORE) {
402 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
406 printf("Running channel monitor on lcore id %d\n", lcore_id);
407 rte_eal_remote_launch(run_monitor, NULL, lcore_id);
409 lcore_id = rte_get_next_lcore(lcore_id, 1, 0);
410 if (lcore_id == RTE_MAX_LCORE) {
411 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
415 if (power_manager_init() < 0) {
416 printf("Unable to initialize power manager\n");
419 if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
420 printf("Unable to initialize channel manager\n");
426 printf("Running core monitor on lcore id %d\n", lcore_id);
427 rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);
431 branch_monitor_exit();
433 rte_eal_mp_wait_lcore();