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 #ifdef RTE_LIBRTE_IXGBE_PMD
35 #include <rte_pmd_ixgbe.h>
37 #ifdef RTE_LIBRTE_I40E_PMD
38 #include <rte_pmd_i40e.h>
40 #ifdef RTE_LIBRTE_BNXT_PMD
41 #include <rte_pmd_bnxt.h>
44 #define RX_RING_SIZE 1024
45 #define TX_RING_SIZE 1024
47 #define NUM_MBUFS 8191
48 #define MBUF_CACHE_SIZE 250
51 static uint32_t enabled_port_mask
;
52 static volatile bool force_quit
;
55 static const struct rte_eth_conf port_conf_default
= {
57 .max_rx_pkt_len
= RTE_ETHER_MAX_LEN
,
62 port_init(uint16_t port
, struct rte_mempool
*mbuf_pool
)
64 struct rte_eth_conf port_conf
= port_conf_default
;
65 const uint16_t rx_rings
= 1, tx_rings
= 1;
68 struct rte_eth_dev_info dev_info
;
69 struct rte_eth_txconf txq_conf
;
71 if (!rte_eth_dev_is_valid_port(port
))
74 retval
= rte_eth_dev_info_get(port
, &dev_info
);
76 printf("Error during getting device (port %u) info: %s\n",
77 port
, strerror(-retval
));
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
;
85 /* Configure the Ethernet device. */
86 retval
= rte_eth_dev_configure(port
, rx_rings
, tx_rings
, &port_conf
);
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
);
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
);
108 /* Start the Ethernet port. */
109 retval
= rte_eth_dev_start(port
);
113 /* Display the port MAC address. */
114 struct rte_ether_addr addr
;
115 retval
= rte_eth_macaddr_get(port
, &addr
);
117 printf("Failed to get device (port %u) MAC address: %s\n",
118 port
, rte_strerror(-retval
));
122 printf("Port %u MAC: %02" PRIx8
" %02" PRIx8
" %02" PRIx8
123 " %02" PRIx8
" %02" PRIx8
" %02" PRIx8
"\n",
125 addr
.addr_bytes
[0], addr
.addr_bytes
[1],
126 addr
.addr_bytes
[2], addr
.addr_bytes
[3],
127 addr
.addr_bytes
[4], addr
.addr_bytes
[5]);
129 /* Enable RX in promiscuous mode for the Ethernet device. */
130 retval
= rte_eth_promiscuous_enable(port
);
139 parse_portmask(const char *portmask
)
144 /* parse hexadecimal string */
145 pm
= strtoul(portmask
, &end
, 16);
146 if ((portmask
[0] == '\0') || (end
== NULL
) || (*end
!= '\0'))
154 /* Parse the argument given in the command line of the application */
156 parse_args(int argc
, char **argv
)
158 int opt
, ret
, cnt
, i
;
160 uint16_t *oob_enable
;
162 char *prgname
= argv
[0];
163 struct core_info
*ci
;
165 static struct option lgopts
[] = {
166 { "mac-updating", no_argument
, 0, 1},
167 { "no-mac-updating", no_argument
, 0, 0},
168 { "core-list", optional_argument
, 0, 'l'},
169 { "port-list", optional_argument
, 0, 'p'},
170 { "branch-ratio", optional_argument
, 0, 'b'},
174 ci
= get_core_info();
176 while ((opt
= getopt_long(argc
, argvopt
, "l:p:q:T:b:",
177 lgopts
, &option_index
)) != EOF
) {
182 enabled_port_mask
= parse_portmask(optarg
);
183 if (enabled_port_mask
== 0) {
184 printf("invalid portmask\n");
189 oob_enable
= malloc(ci
->core_count
* sizeof(uint16_t));
190 if (oob_enable
== NULL
) {
191 printf("Error - Unable to allocate memory\n");
194 cnt
= parse_set(optarg
, oob_enable
, ci
->core_count
);
196 printf("Invalid core-list - [%s]\n",
201 for (i
= 0; i
< ci
->core_count
; i
++) {
203 printf("***Using core %d\n", i
);
204 ci
->cd
[i
].oob_enabled
= 1;
205 ci
->cd
[i
].global_enabled_cpus
= 1;
213 branch_ratio
= atof(optarg
);
214 if (branch_ratio
<= 0.0) {
215 printf("invalid branch ratio specified\n");
218 ci
->branch_ratio_threshold
= branch_ratio
;
219 printf("***Setting branch ratio to %f\n",
232 argv
[optind
-1] = prgname
;
235 optind
= 0; /* reset getopt lib */
240 check_all_ports_link_status(uint32_t port_mask
)
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
;
248 printf("\nChecking link status");
250 for (count
= 0; count
<= MAX_CHECK_TIME
; count
++) {
254 RTE_ETH_FOREACH_DEV(portid
) {
257 if ((port_mask
& (1 << portid
)) == 0)
259 memset(&link
, 0, sizeof(link
));
260 ret
= rte_eth_link_get_nowait(portid
, &link
);
264 printf("Port %u link get failed: %s\n",
265 portid
, rte_strerror(-ret
));
268 /* print link status if flag set */
269 if (print_flag
== 1) {
270 if (link
.link_status
)
271 printf("Port %d Link Up - speed %u "
272 "Mbps - %s\n", (uint16_t)portid
,
273 (unsigned int)link
.link_speed
,
274 (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
275 ("full-duplex") : ("half-duplex"));
277 printf("Port %d Link Down\n",
281 /* clear all_ports_up flag if any link down */
282 if (link
.link_status
== ETH_LINK_DOWN
) {
287 /* after finally printing all link status, get out */
291 if (all_ports_up
== 0) {
294 rte_delay_ms(CHECK_INTERVAL
);
297 /* set the print_flag if all ports up or timeout */
298 if (all_ports_up
== 1 || count
== (MAX_CHECK_TIME
- 1)) {
305 run_monitor(__rte_unused
void *arg
)
307 if (channel_monitor_init() < 0) {
308 printf("Unable to initialize channel monitor\n");
311 run_channel_monitor();
316 run_core_monitor(__rte_unused
void *arg
)
318 if (branch_monitor_init() < 0) {
319 printf("Unable to initialize core monitor\n");
322 run_branch_monitor();
327 sig_handler(int signo
)
329 printf("Received signal %d, exiting...\n", signo
);
330 channel_monitor_exit();
331 channel_manager_exit();
332 power_manager_exit();
337 main(int argc
, char **argv
)
341 unsigned int nb_ports
;
342 struct rte_mempool
*mbuf_pool
;
344 struct core_info
*ci
;
347 ret
= core_info_init();
349 rte_panic("Cannot allocate core info\n");
351 ci
= get_core_info();
353 ret
= rte_eal_init(argc
, argv
);
355 rte_panic("Cannot init EAL\n");
357 signal(SIGINT
, sig_handler
);
358 signal(SIGTERM
, sig_handler
);
363 /* parse application arguments (after the EAL ones) */
364 ret
= parse_args(argc
, argv
);
366 rte_exit(EXIT_FAILURE
, "Invalid arguments\n");
368 nb_ports
= rte_eth_dev_count_avail();
371 mbuf_pool
= rte_pktmbuf_pool_create("MBUF_POOL",
372 NUM_MBUFS
* nb_ports
, MBUF_CACHE_SIZE
, 0,
373 RTE_MBUF_DEFAULT_BUF_SIZE
, rte_socket_id());
375 if (mbuf_pool
== NULL
)
376 rte_exit(EXIT_FAILURE
, "Cannot create mbuf pool\n");
378 /* Initialize ports. */
379 RTE_ETH_FOREACH_DEV(portid
) {
380 struct rte_ether_addr eth
;
384 if ((enabled_port_mask
& (1 << portid
)) == 0)
387 eth
.addr_bytes
[0] = 0xe0;
388 eth
.addr_bytes
[1] = 0xe0;
389 eth
.addr_bytes
[2] = 0xe0;
390 eth
.addr_bytes
[3] = 0xe0;
391 eth
.addr_bytes
[4] = portid
+ 0xf0;
393 if (port_init(portid
, mbuf_pool
) != 0)
394 rte_exit(EXIT_FAILURE
,
395 "Cannot init port %"PRIu8
"\n",
398 for (w
= 0; w
< MAX_VFS
; w
++) {
399 eth
.addr_bytes
[5] = w
+ 0xf0;
402 #ifdef RTE_LIBRTE_IXGBE_PMD
403 ret
= rte_pmd_ixgbe_set_vf_mac_addr(portid
,
406 #ifdef RTE_LIBRTE_I40E_PMD
408 ret
= rte_pmd_i40e_set_vf_mac_addr(
411 #ifdef RTE_LIBRTE_BNXT_PMD
413 ret
= rte_pmd_bnxt_set_vf_mac_addr(
419 printf("Port %d VF %d MAC: ",
421 for (j
= 0; j
< 5; j
++) {
425 printf("%02x\n", eth
.addr_bytes
[5]);
433 check_all_ports_link_status(enabled_port_mask
);
435 lcore_id
= rte_get_next_lcore(-1, 1, 0);
436 if (lcore_id
== RTE_MAX_LCORE
) {
437 RTE_LOG(ERR
, EAL
, "A minimum of three cores are required to run "
441 printf("Running channel monitor on lcore id %d\n", lcore_id
);
442 rte_eal_remote_launch(run_monitor
, NULL
, lcore_id
);
444 lcore_id
= rte_get_next_lcore(lcore_id
, 1, 0);
445 if (lcore_id
== RTE_MAX_LCORE
) {
446 RTE_LOG(ERR
, EAL
, "A minimum of three cores are required to run "
450 if (power_manager_init() < 0) {
451 printf("Unable to initialize power manager\n");
454 if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH
) < 0) {
455 printf("Unable to initialize channel manager\n");
461 printf("Running core monitor on lcore id %d\n", lcore_id
);
462 rte_eal_remote_launch(run_core_monitor
, NULL
, lcore_id
);
466 branch_monitor_exit();
468 rte_eal_mp_wait_lcore();