aef97b9aeb031835f1673e2701cba46f88193f2a
[dpdk.git] / examples / vm_power_manager / main.c
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 #include <rte_pmd_ixgbe.h>
35 #include <rte_pmd_i40e.h>
36 #include <rte_pmd_bnxt.h>
37
38 #define RX_RING_SIZE 1024
39 #define TX_RING_SIZE 1024
40
41 #define NUM_MBUFS 8191
42 #define MBUF_CACHE_SIZE 250
43 #define BURST_SIZE 32
44
45 static uint32_t enabled_port_mask;
46 static volatile bool force_quit;
47
48 /****************/
49 static const struct rte_eth_conf port_conf_default = {
50         .rxmode = {
51                 .max_rx_pkt_len = ETHER_MAX_LEN,
52         },
53 };
54
55 static inline int
56 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
57 {
58         struct rte_eth_conf port_conf = port_conf_default;
59         const uint16_t rx_rings = 1, tx_rings = 1;
60         int retval;
61         uint16_t q;
62         struct rte_eth_dev_info dev_info;
63         struct rte_eth_txconf txq_conf;
64
65         if (!rte_eth_dev_is_valid_port(port))
66                 return -1;
67
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;
72
73         /* Configure the Ethernet device. */
74         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
75         if (retval != 0)
76                 return retval;
77
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);
82                 if (retval < 0)
83                         return retval;
84         }
85
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);
92                 if (retval < 0)
93                         return retval;
94         }
95
96         /* Start the Ethernet port. */
97         retval = rte_eth_dev_start(port);
98         if (retval < 0)
99                 return retval;
100
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",
106                         (unsigned int)port,
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]);
110
111         /* Enable RX in promiscuous mode for the Ethernet device. */
112         rte_eth_promiscuous_enable(port);
113
114
115         return 0;
116 }
117
118 static int
119 parse_portmask(const char *portmask)
120 {
121         char *end = NULL;
122         unsigned long pm;
123
124         /* parse hexadecimal string */
125         pm = strtoul(portmask, &end, 16);
126         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
127                 return -1;
128
129         if (pm == 0)
130                 return -1;
131
132         return pm;
133 }
134 /* Parse the argument given in the command line of the application */
135 static int
136 parse_args(int argc, char **argv)
137 {
138         int opt, ret, cnt, i;
139         char **argvopt;
140         uint16_t *oob_enable;
141         int option_index;
142         char *prgname = argv[0];
143         struct core_info *ci;
144         static struct option lgopts[] = {
145                 { "mac-updating", no_argument, 0, 1},
146                 { "no-mac-updating", no_argument, 0, 0},
147                 { "core-list", optional_argument, 0, 'l'},
148                 {NULL, 0, 0, 0}
149         };
150         argvopt = argv;
151         ci = get_core_info();
152
153         while ((opt = getopt_long(argc, argvopt, "l:p:q:T:",
154                                   lgopts, &option_index)) != EOF) {
155
156                 switch (opt) {
157                 /* portmask */
158                 case 'p':
159                         enabled_port_mask = parse_portmask(optarg);
160                         if (enabled_port_mask == 0) {
161                                 printf("invalid portmask\n");
162                                 return -1;
163                         }
164                         break;
165                 case 'l':
166                         oob_enable = malloc(ci->core_count * sizeof(uint16_t));
167                         if (oob_enable == NULL) {
168                                 printf("Error - Unable to allocate memory\n");
169                                 return -1;
170                         }
171                         cnt = parse_set(optarg, oob_enable, ci->core_count);
172                         if (cnt < 0) {
173                                 printf("Invalid core-list - [%s]\n",
174                                                 optarg);
175                                 break;
176                         }
177                         for (i = 0; i < ci->core_count; i++) {
178                                 if (oob_enable[i]) {
179                                         printf("***Using core %d\n", i);
180                                         ci->cd[i].oob_enabled = 1;
181                                         ci->cd[i].global_enabled_cpus = 1;
182                                 }
183                         }
184                         free(oob_enable);
185                         break;
186                 /* long options */
187                 case 0:
188                         break;
189
190                 default:
191                         return -1;
192                 }
193         }
194
195         if (optind >= 0)
196                 argv[optind-1] = prgname;
197
198         ret = optind-1;
199         optind = 0; /* reset getopt lib */
200         return ret;
201 }
202
203 static void
204 check_all_ports_link_status(uint32_t port_mask)
205 {
206 #define CHECK_INTERVAL 100 /* 100ms */
207 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
208         uint16_t portid, count, all_ports_up, print_flag = 0;
209         struct rte_eth_link link;
210
211         printf("\nChecking link status");
212         fflush(stdout);
213         for (count = 0; count <= MAX_CHECK_TIME; count++) {
214                 if (force_quit)
215                         return;
216                 all_ports_up = 1;
217                 RTE_ETH_FOREACH_DEV(portid) {
218                         if (force_quit)
219                                 return;
220                         if ((port_mask & (1 << portid)) == 0)
221                                 continue;
222                         memset(&link, 0, sizeof(link));
223                         rte_eth_link_get_nowait(portid, &link);
224                         /* print link status if flag set */
225                         if (print_flag == 1) {
226                                 if (link.link_status)
227                                         printf("Port %d Link Up - speed %u "
228                                                 "Mbps - %s\n", (uint16_t)portid,
229                                                 (unsigned int)link.link_speed,
230                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
231                                         ("full-duplex") : ("half-duplex\n"));
232                                 else
233                                         printf("Port %d Link Down\n",
234                                                 (uint16_t)portid);
235                                 continue;
236                         }
237                        /* clear all_ports_up flag if any link down */
238                         if (link.link_status == ETH_LINK_DOWN) {
239                                 all_ports_up = 0;
240                                 break;
241                         }
242                 }
243                 /* after finally printing all link status, get out */
244                 if (print_flag == 1)
245                         break;
246
247                 if (all_ports_up == 0) {
248                         printf(".");
249                         fflush(stdout);
250                         rte_delay_ms(CHECK_INTERVAL);
251                 }
252
253                 /* set the print_flag if all ports up or timeout */
254                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
255                         print_flag = 1;
256                         printf("done\n");
257                 }
258         }
259 }
260 static int
261 run_monitor(__attribute__((unused)) void *arg)
262 {
263         if (channel_monitor_init() < 0) {
264                 printf("Unable to initialize channel monitor\n");
265                 return -1;
266         }
267         run_channel_monitor();
268         return 0;
269 }
270
271 static int
272 run_core_monitor(__attribute__((unused)) void *arg)
273 {
274         if (branch_monitor_init() < 0) {
275                 printf("Unable to initialize core monitor\n");
276                 return -1;
277         }
278         run_branch_monitor();
279         return 0;
280 }
281
282 static void
283 sig_handler(int signo)
284 {
285         printf("Received signal %d, exiting...\n", signo);
286         channel_monitor_exit();
287         channel_manager_exit();
288         power_manager_exit();
289
290 }
291
292 int
293 main(int argc, char **argv)
294 {
295         int ret;
296         unsigned lcore_id;
297         unsigned int nb_ports;
298         struct rte_mempool *mbuf_pool;
299         uint16_t portid;
300         struct core_info *ci;
301
302
303         ret = core_info_init();
304         if (ret < 0)
305                 rte_panic("Cannot allocate core info\n");
306
307         ci = get_core_info();
308
309         ret = rte_eal_init(argc, argv);
310         if (ret < 0)
311                 rte_panic("Cannot init EAL\n");
312
313         signal(SIGINT, sig_handler);
314         signal(SIGTERM, sig_handler);
315
316         argc -= ret;
317         argv += ret;
318
319         /* parse application arguments (after the EAL ones) */
320         ret = parse_args(argc, argv);
321         if (ret < 0)
322                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
323
324         nb_ports = rte_eth_dev_count_avail();
325
326         if (nb_ports > 0) {
327                 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
328                                 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
329                                 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
330
331                 if (mbuf_pool == NULL)
332                         rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
333
334                 /* Initialize ports. */
335                 RTE_ETH_FOREACH_DEV(portid) {
336                         struct ether_addr eth;
337                         int w, j;
338                         int ret;
339
340                         if ((enabled_port_mask & (1 << portid)) == 0)
341                                 continue;
342
343                         eth.addr_bytes[0] = 0xe0;
344                         eth.addr_bytes[1] = 0xe0;
345                         eth.addr_bytes[2] = 0xe0;
346                         eth.addr_bytes[3] = 0xe0;
347                         eth.addr_bytes[4] = portid + 0xf0;
348
349                         if (port_init(portid, mbuf_pool) != 0)
350                                 rte_exit(EXIT_FAILURE,
351                                         "Cannot init port %"PRIu8 "\n",
352                                         portid);
353
354                         for (w = 0; w < MAX_VFS; w++) {
355                                 eth.addr_bytes[5] = w + 0xf0;
356
357                                 ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
358                                                         w, &eth);
359                                 if (ret == -ENOTSUP)
360                                         ret = rte_pmd_i40e_set_vf_mac_addr(
361                                                         portid, w, &eth);
362                                 if (ret == -ENOTSUP)
363                                         ret = rte_pmd_bnxt_set_vf_mac_addr(
364                                                         portid, w, &eth);
365
366                                 switch (ret) {
367                                 case 0:
368                                         printf("Port %d VF %d MAC: ",
369                                                         portid, w);
370                                         for (j = 0; j < 5; j++) {
371                                                 printf("%02x:",
372                                                         eth.addr_bytes[j]);
373                                         }
374                                         printf("%02x\n", eth.addr_bytes[5]);
375                                         break;
376                                 }
377                                 printf("\n");
378                                 break;
379                         }
380                 }
381         }
382
383         check_all_ports_link_status(enabled_port_mask);
384
385         lcore_id = rte_get_next_lcore(-1, 1, 0);
386         if (lcore_id == RTE_MAX_LCORE) {
387                 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
388                                 "application\n");
389                 return 0;
390         }
391         printf("Running channel monitor on lcore id %d\n", lcore_id);
392         rte_eal_remote_launch(run_monitor, NULL, lcore_id);
393
394         lcore_id = rte_get_next_lcore(lcore_id, 1, 0);
395         if (lcore_id == RTE_MAX_LCORE) {
396                 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
397                                 "application\n");
398                 return 0;
399         }
400         if (power_manager_init() < 0) {
401                 printf("Unable to initialize power manager\n");
402                 return -1;
403         }
404         if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
405                 printf("Unable to initialize channel manager\n");
406                 return -1;
407         }
408
409         printf("Running core monitor on lcore id %d\n", lcore_id);
410         rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);
411
412         run_cli(NULL);
413
414         branch_monitor_exit();
415
416         rte_eal_mp_wait_lcore();
417
418         free(ci->cd);
419
420         return 0;
421 }