4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <sys/epoll.h>
44 #include <sys/queue.h>
46 #include <rte_common.h>
48 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_ethdev.h>
54 #include <rte_cycles.h>
55 #include <rte_debug.h>
57 #include "channel_manager.h"
58 #include "channel_monitor.h"
59 #include "power_manager.h"
60 #include "vm_power_cli.h"
62 #define RX_RING_SIZE 512
63 #define TX_RING_SIZE 512
65 #define NUM_MBUFS 8191
66 #define MBUF_CACHE_SIZE 250
69 static uint32_t enabled_port_mask;
70 static volatile bool force_quit;
73 static const struct rte_eth_conf port_conf_default = {
74 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
78 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
80 struct rte_eth_conf port_conf = port_conf_default;
81 const uint16_t rx_rings = 1, tx_rings = 1;
85 if (port >= rte_eth_dev_count())
88 /* Configure the Ethernet device. */
89 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
93 /* Allocate and set up 1 RX queue per Ethernet port. */
94 for (q = 0; q < rx_rings; q++) {
95 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
96 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
101 /* Allocate and set up 1 TX queue per Ethernet port. */
102 for (q = 0; q < tx_rings; q++) {
103 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
104 rte_eth_dev_socket_id(port), NULL);
109 /* Start the Ethernet port. */
110 retval = rte_eth_dev_start(port);
114 /* Display the port MAC address. */
115 struct ether_addr addr;
116 rte_eth_macaddr_get(port, &addr);
117 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
118 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
120 addr.addr_bytes[0], addr.addr_bytes[1],
121 addr.addr_bytes[2], addr.addr_bytes[3],
122 addr.addr_bytes[4], addr.addr_bytes[5]);
124 /* Enable RX in promiscuous mode for the Ethernet device. */
125 rte_eth_promiscuous_enable(port);
132 parse_portmask(const char *portmask)
137 /* parse hexadecimal string */
138 pm = strtoul(portmask, &end, 16);
139 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
147 /* Parse the argument given in the command line of the application */
149 parse_args(int argc, char **argv)
154 char *prgname = argv[0];
155 static struct option lgopts[] = {
156 { "mac-updating", no_argument, 0, 1},
157 { "no-mac-updating", no_argument, 0, 0},
162 while ((opt = getopt_long(argc, argvopt, "p:q:T:",
163 lgopts, &option_index)) != EOF) {
168 enabled_port_mask = parse_portmask(optarg);
169 if (enabled_port_mask == 0) {
170 printf("invalid portmask\n");
184 argv[optind-1] = prgname;
187 optind = 0; /* reset getopt lib */
192 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
194 #define CHECK_INTERVAL 100 /* 100ms */
195 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
196 uint16_t portid, count, all_ports_up, print_flag = 0;
197 struct rte_eth_link link;
199 printf("\nChecking link status");
201 for (count = 0; count <= MAX_CHECK_TIME; count++) {
205 for (portid = 0; portid < port_num; portid++) {
208 if ((port_mask & (1 << portid)) == 0)
210 memset(&link, 0, sizeof(link));
211 rte_eth_link_get_nowait(portid, &link);
212 /* print link status if flag set */
213 if (print_flag == 1) {
214 if (link.link_status)
215 printf("Port %d Link Up - speed %u "
216 "Mbps - %s\n", (uint16_t)portid,
217 (unsigned int)link.link_speed,
218 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
219 ("full-duplex") : ("half-duplex\n"));
221 printf("Port %d Link Down\n",
225 /* clear all_ports_up flag if any link down */
226 if (link.link_status == ETH_LINK_DOWN) {
231 /* after finally printing all link status, get out */
235 if (all_ports_up == 0) {
238 rte_delay_ms(CHECK_INTERVAL);
241 /* set the print_flag if all ports up or timeout */
242 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
249 run_monitor(__attribute__((unused)) void *arg)
251 if (channel_monitor_init() < 0) {
252 printf("Unable to initialize channel monitor\n");
255 run_channel_monitor();
260 sig_handler(int signo)
262 printf("Received signal %d, exiting...\n", signo);
263 channel_monitor_exit();
264 channel_manager_exit();
265 power_manager_exit();
270 main(int argc, char **argv)
274 unsigned int nb_ports;
275 struct rte_mempool *mbuf_pool;
279 ret = rte_eal_init(argc, argv);
281 rte_panic("Cannot init EAL\n");
283 signal(SIGINT, sig_handler);
284 signal(SIGTERM, sig_handler);
289 /* parse application arguments (after the EAL ones) */
290 ret = parse_args(argc, argv);
292 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
294 nb_ports = rte_eth_dev_count();
296 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
297 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
299 if (mbuf_pool == NULL)
300 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
302 /* Initialize ports. */
303 for (portid = 0; portid < nb_ports; portid++) {
304 if ((enabled_port_mask & (1 << portid)) == 0)
306 if (port_init(portid, mbuf_pool) != 0)
307 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
311 lcore_id = rte_get_next_lcore(-1, 1, 0);
312 if (lcore_id == RTE_MAX_LCORE) {
313 RTE_LOG(ERR, EAL, "A minimum of two cores are required to run "
318 check_all_ports_link_status(nb_ports, enabled_port_mask);
319 rte_eal_remote_launch(run_monitor, NULL, lcore_id);
321 if (power_manager_init() < 0) {
322 printf("Unable to initialize power manager\n");
325 if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
326 printf("Unable to initialize channel manager\n");
331 rte_eal_mp_wait_lcore();