New upstream version 18.08
[deb_dpdk.git] / examples / vm_power_manager / guest_cli / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <signal.h>
8 #include <getopt.h>
9 #include <string.h>
10
11 #include <rte_lcore.h>
12 #include <rte_power.h>
13 #include <rte_debug.h>
14 #include <rte_eal.h>
15 #include <rte_log.h>
16
17 #include "vm_power_cli_guest.h"
18 #include "parse.h"
19
20 static void
21 sig_handler(int signo)
22 {
23         printf("Received signal %d, exiting...\n", signo);
24         unsigned lcore_id;
25
26         RTE_LCORE_FOREACH(lcore_id) {
27                 rte_power_exit(lcore_id);
28         }
29
30 }
31
32 #define MAX_HOURS 24
33
34 /* Parse the argument given in the command line of the application */
35 static int
36 parse_args(int argc, char **argv)
37 {
38         int opt, ret;
39         char **argvopt;
40         int option_index;
41         char *prgname = argv[0];
42         const struct option lgopts[] = {
43                 { "vm-name", required_argument, 0, 'n'},
44                 { "busy-hours", required_argument, 0, 'b'},
45                 { "quiet-hours", required_argument, 0, 'q'},
46                 { "port-list", required_argument, 0, 'p'},
47                 { "vcpu-list", required_argument, 0, 'l'},
48                 { "policy", required_argument, 0, 'o'},
49                 {NULL, 0, 0, 0}
50         };
51         struct channel_packet *policy;
52         unsigned short int hours[MAX_HOURS];
53         unsigned short int cores[MAX_VCPU_PER_VM];
54         unsigned short int ports[MAX_VCPU_PER_VM];
55         int i, cnt, idx;
56
57         policy = get_policy();
58         set_policy_defaults(policy);
59
60         argvopt = argv;
61
62         while ((opt = getopt_long(argc, argvopt, "n:b:q:p:",
63                                   lgopts, &option_index)) != EOF) {
64
65                 switch (opt) {
66                 /* portmask */
67                 case 'n':
68                         strcpy(policy->vm_name, optarg);
69                         printf("Setting VM Name to [%s]\n", policy->vm_name);
70                         break;
71                 case 'b':
72                 case 'q':
73                         //printf("***Processing set using [%s]\n", optarg);
74                         cnt = parse_set(optarg, hours, MAX_HOURS);
75                         if (cnt < 0) {
76                                 printf("Invalid value passed to quiet/busy hours - [%s]\n",
77                                                 optarg);
78                                 break;
79                         }
80                         idx = 0;
81                         for (i = 0; i < MAX_HOURS; i++) {
82                                 if (hours[i]) {
83                                         if (opt == 'b') {
84                                                 printf("***Busy Hour %d\n", i);
85                                                 policy->timer_policy.busy_hours
86                                                         [idx++] = i;
87                                         } else {
88                                                 printf("***Quiet Hour %d\n", i);
89                                                 policy->timer_policy.quiet_hours
90                                                         [idx++] = i;
91                                         }
92                                 }
93                         }
94                         break;
95                 case 'l':
96                         cnt = parse_set(optarg, cores, MAX_VCPU_PER_VM);
97                         if (cnt < 0) {
98                                 printf("Invalid value passed to vcpu-list - [%s]\n",
99                                                 optarg);
100                                 break;
101                         }
102                         idx = 0;
103                         for (i = 0; i < MAX_VCPU_PER_VM; i++) {
104                                 if (cores[i]) {
105                                         printf("***Using core %d\n", i);
106                                         policy->vcpu_to_control[idx++] = i;
107                                 }
108                         }
109                         policy->num_vcpu = idx;
110                         printf("Total cores: %d\n", idx);
111                         break;
112                 case 'p':
113                         cnt = parse_set(optarg, ports, MAX_VCPU_PER_VM);
114                         if (cnt < 0) {
115                                 printf("Invalid value passed to port-list - [%s]\n",
116                                                 optarg);
117                                 break;
118                         }
119                         idx = 0;
120                         for (i = 0; i < MAX_VCPU_PER_VM; i++) {
121                                 if (ports[i]) {
122                                         printf("***Using port %d\n", i);
123                                         set_policy_mac(i, idx++);
124                                 }
125                         }
126                         policy->nb_mac_to_monitor = idx;
127                         printf("Total Ports: %d\n", idx);
128                         break;
129                 case 'o':
130                         if (!strcmp(optarg, "TRAFFIC"))
131                                 policy->policy_to_use = TRAFFIC;
132                         else if (!strcmp(optarg, "TIME"))
133                                 policy->policy_to_use = TIME;
134                         else if (!strcmp(optarg, "WORKLOAD"))
135                                 policy->policy_to_use = WORKLOAD;
136                         else if (!strcmp(optarg, "BRANCH_RATIO"))
137                                 policy->policy_to_use = BRANCH_RATIO;
138                         else {
139                                 printf("Invalid policy specified: %s\n",
140                                                 optarg);
141                                 return -1;
142                         }
143                         break;
144                 /* long options */
145
146                 case 0:
147                         break;
148
149                 default:
150                         return -1;
151                 }
152         }
153
154         if (optind >= 0)
155                 argv[optind-1] = prgname;
156
157         ret = optind-1;
158         optind = 0; /* reset getopt lib */
159         return ret;
160 }
161
162 int
163 main(int argc, char **argv)
164 {
165         int ret;
166         unsigned lcore_id;
167
168         ret = rte_eal_init(argc, argv);
169         if (ret < 0)
170                 rte_panic("Cannot init EAL\n");
171
172         signal(SIGINT, sig_handler);
173         signal(SIGTERM, sig_handler);
174
175         argc -= ret;
176         argv += ret;
177
178         /* parse application arguments (after the EAL ones) */
179         ret = parse_args(argc, argv);
180         if (ret < 0)
181                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
182
183         rte_power_set_env(PM_ENV_KVM_VM);
184         RTE_LCORE_FOREACH(lcore_id) {
185                 rte_power_init(lcore_id);
186         }
187         run_cli(NULL);
188
189         return 0;
190 }