replace unused attributes
[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 #ifdef RTE_LIBRTE_IXGBE_PMD
35 #include <rte_pmd_ixgbe.h>
36 #endif
37 #ifdef RTE_LIBRTE_I40E_PMD
38 #include <rte_pmd_i40e.h>
39 #endif
40 #ifdef RTE_LIBRTE_BNXT_PMD
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,
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]);
128
129         /* Enable RX in promiscuous mode for the Ethernet device. */
130         retval = rte_eth_promiscuous_enable(port);
131         if (retval != 0)
132                 return retval;
133
134
135         return 0;
136 }
137
138 static int
139 parse_portmask(const char *portmask)
140 {
141         char *end = NULL;
142         unsigned long pm;
143
144         /* parse hexadecimal string */
145         pm = strtoul(portmask, &end, 16);
146         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
147                 return -1;
148
149         if (pm == 0)
150                 return -1;
151
152         return pm;
153 }
154 /* Parse the argument given in the command line of the application */
155 static int
156 parse_args(int argc, char **argv)
157 {
158         int opt, ret, cnt, i;
159         char **argvopt;
160         uint16_t *oob_enable;
161         int option_index;
162         char *prgname = argv[0];
163         struct core_info *ci;
164         float branch_ratio;
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'},
171                 {NULL, 0, 0, 0}
172         };
173         argvopt = argv;
174         ci = get_core_info();
175
176         while ((opt = getopt_long(argc, argvopt, "l:p:q:T:b:",
177                                   lgopts, &option_index)) != EOF) {
178
179                 switch (opt) {
180                 /* portmask */
181                 case 'p':
182                         enabled_port_mask = parse_portmask(optarg);
183                         if (enabled_port_mask == 0) {
184                                 printf("invalid portmask\n");
185                                 return -1;
186                         }
187                         break;
188                 case 'l':
189                         oob_enable = malloc(ci->core_count * sizeof(uint16_t));
190                         if (oob_enable == NULL) {
191                                 printf("Error - Unable to allocate memory\n");
192                                 return -1;
193                         }
194                         cnt = parse_set(optarg, oob_enable, ci->core_count);
195                         if (cnt < 0) {
196                                 printf("Invalid core-list - [%s]\n",
197                                                 optarg);
198                                 free(oob_enable);
199                                 break;
200                         }
201                         for (i = 0; i < ci->core_count; i++) {
202                                 if (oob_enable[i]) {
203                                         printf("***Using core %d\n", i);
204                                         ci->cd[i].oob_enabled = 1;
205                                         ci->cd[i].global_enabled_cpus = 1;
206                                 }
207                         }
208                         free(oob_enable);
209                         break;
210                 case 'b':
211                         branch_ratio = 0.0;
212                         if (strlen(optarg))
213                                 branch_ratio = atof(optarg);
214                         if (branch_ratio <= 0.0) {
215                                 printf("invalid branch ratio specified\n");
216                                 return -1;
217                         }
218                         ci->branch_ratio_threshold = branch_ratio;
219                         printf("***Setting branch ratio to %f\n",
220                                         branch_ratio);
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
248         printf("\nChecking link status");
249         fflush(stdout);
250         for (count = 0; count <= MAX_CHECK_TIME; count++) {
251                 if (force_quit)
252                         return;
253                 all_ports_up = 1;
254                 RTE_ETH_FOREACH_DEV(portid) {
255                         if (force_quit)
256                                 return;
257                         if ((port_mask & (1 << portid)) == 0)
258                                 continue;
259                         memset(&link, 0, sizeof(link));
260                         ret = rte_eth_link_get_nowait(portid, &link);
261                         if (ret < 0) {
262                                 all_ports_up = 0;
263                                 if (print_flag == 1)
264                                         printf("Port %u link get failed: %s\n",
265                                                 portid, rte_strerror(-ret));
266                                 continue;
267                         }
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\n"));
276                                 else
277                                         printf("Port %d Link Down\n",
278                                                 (uint16_t)portid);
279                                 continue;
280                         }
281                        /* clear all_ports_up flag if any link down */
282                         if (link.link_status == ETH_LINK_DOWN) {
283                                 all_ports_up = 0;
284                                 break;
285                         }
286                 }
287                 /* after finally printing all link status, get out */
288                 if (print_flag == 1)
289                         break;
290
291                 if (all_ports_up == 0) {
292                         printf(".");
293                         fflush(stdout);
294                         rte_delay_ms(CHECK_INTERVAL);
295                 }
296
297                 /* set the print_flag if all ports up or timeout */
298                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
299                         print_flag = 1;
300                         printf("done\n");
301                 }
302         }
303 }
304 static int
305 run_monitor(__rte_unused void *arg)
306 {
307         if (channel_monitor_init() < 0) {
308                 printf("Unable to initialize channel monitor\n");
309                 return -1;
310         }
311         run_channel_monitor();
312         return 0;
313 }
314
315 static int
316 run_core_monitor(__rte_unused void *arg)
317 {
318         if (branch_monitor_init() < 0) {
319                 printf("Unable to initialize core monitor\n");
320                 return -1;
321         }
322         run_branch_monitor();
323         return 0;
324 }
325
326 static void
327 sig_handler(int signo)
328 {
329         printf("Received signal %d, exiting...\n", signo);
330         channel_monitor_exit();
331         channel_manager_exit();
332         power_manager_exit();
333
334 }
335
336 int
337 main(int argc, char **argv)
338 {
339         int ret;
340         unsigned lcore_id;
341         unsigned int nb_ports;
342         struct rte_mempool *mbuf_pool;
343         uint16_t portid;
344         struct core_info *ci;
345
346
347         ret = core_info_init();
348         if (ret < 0)
349                 rte_panic("Cannot allocate core info\n");
350
351         ci = get_core_info();
352
353         ret = rte_eal_init(argc, argv);
354         if (ret < 0)
355                 rte_panic("Cannot init EAL\n");
356
357         signal(SIGINT, sig_handler);
358         signal(SIGTERM, sig_handler);
359
360         argc -= ret;
361         argv += ret;
362
363         /* parse application arguments (after the EAL ones) */
364         ret = parse_args(argc, argv);
365         if (ret < 0)
366                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
367
368         nb_ports = rte_eth_dev_count_avail();
369
370         if (nb_ports > 0) {
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());
374
375                 if (mbuf_pool == NULL)
376                         rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
377
378                 /* Initialize ports. */
379                 RTE_ETH_FOREACH_DEV(portid) {
380                         struct rte_ether_addr eth;
381                         int w, j;
382                         int ret;
383
384                         if ((enabled_port_mask & (1 << portid)) == 0)
385                                 continue;
386
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;
392
393                         if (port_init(portid, mbuf_pool) != 0)
394                                 rte_exit(EXIT_FAILURE,
395                                         "Cannot init port %"PRIu8 "\n",
396                                         portid);
397
398                         for (w = 0; w < MAX_VFS; w++) {
399                                 eth.addr_bytes[5] = w + 0xf0;
400
401                                 ret = -ENOTSUP;
402 #ifdef RTE_LIBRTE_IXGBE_PMD
403                                 ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
404                                                         w, &eth);
405 #endif
406 #ifdef RTE_LIBRTE_I40E_PMD
407                                 if (ret == -ENOTSUP)
408                                         ret = rte_pmd_i40e_set_vf_mac_addr(
409                                                         portid, w, &eth);
410 #endif
411 #ifdef RTE_LIBRTE_BNXT_PMD
412                                 if (ret == -ENOTSUP)
413                                         ret = rte_pmd_bnxt_set_vf_mac_addr(
414                                                         portid, w, &eth);
415 #endif
416
417                                 switch (ret) {
418                                 case 0:
419                                         printf("Port %d VF %d MAC: ",
420                                                         portid, w);
421                                         for (j = 0; j < 5; j++) {
422                                                 printf("%02x:",
423                                                         eth.addr_bytes[j]);
424                                         }
425                                         printf("%02x\n", eth.addr_bytes[5]);
426                                         break;
427                                 }
428                                 printf("\n");
429                         }
430                 }
431         }
432
433         check_all_ports_link_status(enabled_port_mask);
434
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 "
438                                 "application\n");
439                 return 0;
440         }
441         printf("Running channel monitor on lcore id %d\n", lcore_id);
442         rte_eal_remote_launch(run_monitor, NULL, lcore_id);
443
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 "
447                                 "application\n");
448                 return 0;
449         }
450         if (power_manager_init() < 0) {
451                 printf("Unable to initialize power manager\n");
452                 return -1;
453         }
454         if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
455                 printf("Unable to initialize channel manager\n");
456                 return -1;
457         }
458
459         add_host_channels();
460
461         printf("Running core monitor on lcore id %d\n", lcore_id);
462         rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);
463
464         run_cli(NULL);
465
466         branch_monitor_exit();
467
468         rte_eal_mp_wait_lcore();
469
470         free(ci->cd);
471
472         return 0;
473 }