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