build: replace use of old build macros
[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_NET_IXGBE
35 #include <rte_pmd_ixgbe.h>
36 #endif
37 #ifdef RTE_NET_I40E
38 #include <rte_pmd_i40e.h>
39 #endif
40 #ifdef RTE_NET_BNXT
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 0;
148
149         return pm;
150 }
151 /* Parse the argument given in the command line of the application */
152 static int
153 parse_args(int argc, char **argv)
154 {
155         int opt, ret, cnt, i;
156         char **argvopt;
157         uint16_t *oob_enable;
158         int option_index;
159         char *prgname = argv[0];
160         struct core_info *ci;
161         float branch_ratio;
162         static struct option lgopts[] = {
163                 { "mac-updating", no_argument, 0, 1},
164                 { "no-mac-updating", no_argument, 0, 0},
165                 { "core-branch-ratio", optional_argument, 0, 'b'},
166                 { "port-list", optional_argument, 0, 'p'},
167                 {NULL, 0, 0, 0}
168         };
169         argvopt = argv;
170         ci = get_core_info();
171
172         while ((opt = getopt_long(argc, argvopt, "p:q:T:b:",
173                                   lgopts, &option_index)) != EOF) {
174
175                 switch (opt) {
176                 /* portmask */
177                 case 'p':
178                         enabled_port_mask = parse_portmask(optarg);
179                         if (enabled_port_mask == 0) {
180                                 printf("invalid portmask\n");
181                                 return -1;
182                         }
183                         break;
184                 case 'b':
185                         branch_ratio = BRANCH_RATIO_THRESHOLD;
186                         oob_enable = malloc(ci->core_count * sizeof(uint16_t));
187                         if (oob_enable == NULL) {
188                                 printf("Error - Unable to allocate memory\n");
189                                 return -1;
190                         }
191                         cnt = parse_set(optarg, oob_enable, ci->core_count);
192                         if (cnt < 0) {
193                                 printf("Invalid core-list section in "
194                                        "core-branch-ratio matrix - [%s]\n",
195                                                 optarg);
196                                 free(oob_enable);
197                                 break;
198                         }
199                         cnt = parse_branch_ratio(optarg, &branch_ratio);
200                         if (cnt < 0) {
201                                 printf("Invalid branch-ratio section in "
202                                        "core-branch-ratio matrix - [%s]\n",
203                                                 optarg);
204                                 free(oob_enable);
205                                 break;
206                         }
207                         if (branch_ratio <= 0.0 || branch_ratio > 100.0) {
208                                 printf("invalid branch ratio specified\n");
209                                 return -1;
210                         }
211                         for (i = 0; i < ci->core_count; i++) {
212                                 if (oob_enable[i]) {
213                                         printf("***Using core %d "
214                                                "with branch ratio %f\n",
215                                                i, branch_ratio);
216                                         ci->cd[i].oob_enabled = 1;
217                                         ci->cd[i].global_enabled_cpus = 1;
218                                         ci->cd[i].branch_ratio_threshold =
219                                                                 branch_ratio;
220                                 }
221                         }
222                         free(oob_enable);
223                         break;
224                 /* long options */
225                 case 0:
226                         break;
227
228                 default:
229                         return -1;
230                 }
231         }
232
233         if (optind >= 0)
234                 argv[optind-1] = prgname;
235
236         ret = optind-1;
237         optind = 0; /* reset getopt lib */
238         return ret;
239 }
240
241 static void
242 check_all_ports_link_status(uint32_t port_mask)
243 {
244 #define CHECK_INTERVAL 100 /* 100ms */
245 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
246         uint16_t portid, count, all_ports_up, print_flag = 0;
247         struct rte_eth_link link;
248         int ret;
249         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
250
251         printf("\nChecking link status");
252         fflush(stdout);
253         for (count = 0; count <= MAX_CHECK_TIME; count++) {
254                 if (force_quit)
255                         return;
256                 all_ports_up = 1;
257                 RTE_ETH_FOREACH_DEV(portid) {
258                         if (force_quit)
259                                 return;
260                         if ((port_mask & (1 << portid)) == 0)
261                                 continue;
262                         memset(&link, 0, sizeof(link));
263                         ret = rte_eth_link_get_nowait(portid, &link);
264                         if (ret < 0) {
265                                 all_ports_up = 0;
266                                 if (print_flag == 1)
267                                         printf("Port %u link get failed: %s\n",
268                                                 portid, rte_strerror(-ret));
269                                 continue;
270                         }
271                         /* print link status if flag set */
272                         if (print_flag == 1) {
273                                 rte_eth_link_to_str(link_status_text,
274                                         sizeof(link_status_text), &link);
275                                 printf("Port %d %s\n", portid,
276                                        link_status_text);
277                                 continue;
278                         }
279                        /* clear all_ports_up flag if any link down */
280                         if (link.link_status == ETH_LINK_DOWN) {
281                                 all_ports_up = 0;
282                                 break;
283                         }
284                 }
285                 /* after finally printing all link status, get out */
286                 if (print_flag == 1)
287                         break;
288
289                 if (all_ports_up == 0) {
290                         printf(".");
291                         fflush(stdout);
292                         rte_delay_ms(CHECK_INTERVAL);
293                 }
294
295                 /* set the print_flag if all ports up or timeout */
296                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
297                         print_flag = 1;
298                         printf("done\n");
299                 }
300         }
301 }
302 static int
303 run_monitor(__rte_unused void *arg)
304 {
305         if (channel_monitor_init() < 0) {
306                 printf("Unable to initialize channel monitor\n");
307                 return -1;
308         }
309         run_channel_monitor();
310         return 0;
311 }
312
313 static int
314 run_core_monitor(__rte_unused void *arg)
315 {
316         if (branch_monitor_init() < 0) {
317                 printf("Unable to initialize core monitor\n");
318                 return -1;
319         }
320         run_branch_monitor();
321         return 0;
322 }
323
324 static void
325 sig_handler(int signo)
326 {
327         printf("Received signal %d, exiting...\n", signo);
328         channel_monitor_exit();
329         channel_manager_exit();
330         power_manager_exit();
331
332 }
333
334 int
335 main(int argc, char **argv)
336 {
337         int ret;
338         unsigned lcore_id;
339         unsigned int nb_ports;
340         struct rte_mempool *mbuf_pool;
341         uint16_t portid;
342         struct core_info *ci;
343
344
345         ret = core_info_init();
346         if (ret < 0)
347                 rte_panic("Cannot allocate core info\n");
348
349         ci = get_core_info();
350
351         ret = rte_eal_init(argc, argv);
352         if (ret < 0)
353                 rte_panic("Cannot init EAL\n");
354
355         signal(SIGINT, sig_handler);
356         signal(SIGTERM, sig_handler);
357
358         argc -= ret;
359         argv += ret;
360
361         /* parse application arguments (after the EAL ones) */
362         ret = parse_args(argc, argv);
363         if (ret < 0)
364                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
365
366         nb_ports = rte_eth_dev_count_avail();
367
368         if (nb_ports > 0) {
369                 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
370                                 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
371                                 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
372
373                 if (mbuf_pool == NULL)
374                         rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
375
376                 /* Initialize ports. */
377                 RTE_ETH_FOREACH_DEV(portid) {
378                         struct rte_ether_addr eth;
379                         int w, j;
380                         int ret;
381
382                         if ((enabled_port_mask & (1 << portid)) == 0)
383                                 continue;
384
385                         eth.addr_bytes[0] = 0xe0;
386                         eth.addr_bytes[1] = 0xe0;
387                         eth.addr_bytes[2] = 0xe0;
388                         eth.addr_bytes[3] = 0xe0;
389                         eth.addr_bytes[4] = portid + 0xf0;
390
391                         if (port_init(portid, mbuf_pool) != 0)
392                                 rte_exit(EXIT_FAILURE,
393                                         "Cannot init port %"PRIu8 "\n",
394                                         portid);
395
396                         for (w = 0; w < MAX_VFS; w++) {
397                                 eth.addr_bytes[5] = w + 0xf0;
398
399                                 ret = -ENOTSUP;
400 #ifdef RTE_NET_IXGBE
401                                 ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
402                                                         w, &eth);
403 #endif
404 #ifdef RTE_NET_I40E
405                                 if (ret == -ENOTSUP)
406                                         ret = rte_pmd_i40e_set_vf_mac_addr(
407                                                         portid, w, &eth);
408 #endif
409 #ifdef RTE_NET_BNXT
410                                 if (ret == -ENOTSUP)
411                                         ret = rte_pmd_bnxt_set_vf_mac_addr(
412                                                         portid, w, &eth);
413 #endif
414
415                                 switch (ret) {
416                                 case 0:
417                                         printf("Port %d VF %d MAC: ",
418                                                         portid, w);
419                                         for (j = 0; j < 5; j++) {
420                                                 printf("%02x:",
421                                                         eth.addr_bytes[j]);
422                                         }
423                                         printf("%02x\n", eth.addr_bytes[5]);
424                                         break;
425                                 }
426                                 printf("\n");
427                         }
428                 }
429         }
430
431         check_all_ports_link_status(enabled_port_mask);
432
433         lcore_id = rte_get_next_lcore(-1, 1, 0);
434         if (lcore_id == RTE_MAX_LCORE) {
435                 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
436                                 "application\n");
437                 return 0;
438         }
439         printf("Running channel monitor on lcore id %d\n", lcore_id);
440         rte_eal_remote_launch(run_monitor, NULL, lcore_id);
441
442         lcore_id = rte_get_next_lcore(lcore_id, 1, 0);
443         if (lcore_id == RTE_MAX_LCORE) {
444                 RTE_LOG(ERR, EAL, "A minimum of three cores are required to run "
445                                 "application\n");
446                 return 0;
447         }
448         if (power_manager_init() < 0) {
449                 printf("Unable to initialize power manager\n");
450                 return -1;
451         }
452         if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
453                 printf("Unable to initialize channel manager\n");
454                 return -1;
455         }
456
457         add_host_channels();
458
459         printf("Running core monitor on lcore id %d\n", lcore_id);
460         rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);
461
462         run_cli(NULL);
463
464         branch_monitor_exit();
465
466         rte_eal_mp_wait_lcore();
467
468         free(ci->cd);
469
470         return 0;
471 }