summaryrefslogtreecommitdiff
path: root/examples/vm_power_manager/vm_power_cli.c
blob: fb388d0b8b68616992c5ae89d02ace10ab55e32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
/*-
 *   BSD LICENSE
 *
 *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <errno.h>

#ifndef RTE_LIBRTE_CLI
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_string.h>
#include <cmdline_parse_num.h>
#include <cmdline_socket.h>
#include <cmdline.h>
#else
#include <rte_string_fns.h>

#include <cli.h>
#include <cli_file.h>
#include <cli_map.h>
#include <cli_help.h>
#include <cli_string_fns.h>
#endif

#include "vm_power_cli.h"
#include "channel_manager.h"
#include "channel_monitor.h"
#include "power_manager.h"
#include "channel_commands.h"

#ifndef RTE_LIBRTE_CLI
struct cmd_quit_result {
	cmdline_fixed_string_t quit;
};

static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
		struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	channel_monitor_exit();
	channel_manager_exit();
	power_manager_exit();
	cmdline_quit(cl);
}

cmdline_parse_token_string_t cmd_quit_quit =
	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");

cmdline_parse_inst_t cmd_quit = {
	.f = cmd_quit_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "close the application",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_quit_quit,
		NULL,
	},
};

/* *** VM operations *** */
struct cmd_show_vm_result {
	cmdline_fixed_string_t show_vm;
	cmdline_fixed_string_t vm_name;
};

static void
cmd_show_vm_parsed(void *parsed_result, struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_show_vm_result *res = parsed_result;
	struct vm_info info;
	unsigned i;

	if (get_info_vm(res->vm_name, &info) != 0)
		return;
	cmdline_printf(cl, "VM: '%s', status = ", info.name);
	if (info.status == CHANNEL_MGR_VM_ACTIVE)
		cmdline_printf(cl, "ACTIVE\n");
	else
		cmdline_printf(cl, "INACTIVE\n");
	cmdline_printf(cl, "Channels %u\n", info.num_channels);
	for (i = 0; i < info.num_channels; i++) {
		cmdline_printf(cl, "  [%u]: %s, status = ", i,
				info.channels[i].channel_path);
		switch (info.channels[i].status) {
		case CHANNEL_MGR_CHANNEL_CONNECTED:
			cmdline_printf(cl, "CONNECTED\n");
			break;
		case CHANNEL_MGR_CHANNEL_DISCONNECTED:
			cmdline_printf(cl, "DISCONNECTED\n");
			break;
		case CHANNEL_MGR_CHANNEL_DISABLED:
			cmdline_printf(cl, "DISABLED\n");
			break;
		case CHANNEL_MGR_CHANNEL_PROCESSING:
			cmdline_printf(cl, "PROCESSING\n");
			break;
		default:
			cmdline_printf(cl, "UNKNOWN\n");
			break;
		}
	}
	cmdline_printf(cl, "Virtual CPU(s): %u\n", info.num_vcpus);
	for (i = 0; i < info.num_vcpus; i++) {
		cmdline_printf(cl, "  [%u]: Physical CPU Mask 0x%"PRIx64"\n", i,
				info.pcpu_mask[i]);
	}
}



cmdline_parse_token_string_t cmd_vm_show =
	TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result,
				show_vm, "show_vm");
cmdline_parse_token_string_t cmd_show_vm_name =
	TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result,
			vm_name, NULL);

cmdline_parse_inst_t cmd_show_vm_set = {
	.f = cmd_show_vm_parsed,
	.data = NULL,
	.help_str = "show_vm <vm_name>, prints the information on the "
			"specified VM(s), the information lists the number of vCPUS, the "
			"pinning to pCPU(s) as a bit mask, along with any communication "
			"channels associated with each VM",
	.tokens = {
		(void *)&cmd_vm_show,
		(void *)&cmd_show_vm_name,
		NULL,
	},
};

/* *** vCPU to pCPU mapping operations *** */
struct cmd_set_pcpu_mask_result {
	cmdline_fixed_string_t set_pcpu_mask;
	cmdline_fixed_string_t vm_name;
	uint8_t vcpu;
	uint64_t core_mask;
};

static void
cmd_set_pcpu_mask_parsed(void *parsed_result, struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_set_pcpu_mask_result *res = parsed_result;

	if (set_pcpus_mask(res->vm_name, res->vcpu, res->core_mask) == 0)
		cmdline_printf(cl, "Pinned vCPU(%"PRId8") to pCPU core "
				"mask(0x%"PRIx64")\n", res->vcpu, res->core_mask);
	else
		cmdline_printf(cl, "Unable to pin vCPU(%"PRId8") to pCPU core "
				"mask(0x%"PRIx64")\n", res->vcpu, res->core_mask);
}

cmdline_parse_token_string_t cmd_set_pcpu_mask =
		TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result,
				set_pcpu_mask, "set_pcpu_mask");
cmdline_parse_token_string_t cmd_set_pcpu_mask_vm_name =
		TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result,
				vm_name, NULL);
cmdline_parse_token_num_t set_pcpu_mask_vcpu =
		TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result,
				vcpu, UINT8);
cmdline_parse_token_num_t set_pcpu_mask_core_mask =
		TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result,
				core_mask, UINT64);


cmdline_parse_inst_t cmd_set_pcpu_mask_set = {
		.f = cmd_set_pcpu_mask_parsed,
		.data = NULL,
		.help_str = "set_pcpu_mask <vm_name> <vcpu> <pcpu>, Set the binding "
				"of Virtual CPU on VM to the Physical CPU mask.",
				.tokens = {
						(void *)&cmd_set_pcpu_mask,
						(void *)&cmd_set_pcpu_mask_vm_name,
						(void *)&set_pcpu_mask_vcpu,
						(void *)&set_pcpu_mask_core_mask,
						NULL,
		},
};

struct cmd_set_pcpu_result {
	cmdline_fixed_string_t set_pcpu;
	cmdline_fixed_string_t vm_name;
	uint8_t vcpu;
	uint8_t core;
};

static void
cmd_set_pcpu_parsed(void *parsed_result, struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_set_pcpu_result *res = parsed_result;

	if (set_pcpu(res->vm_name, res->vcpu, res->core) == 0)
		cmdline_printf(cl, "Pinned vCPU(%"PRId8") to pCPU core "
				"%"PRId8")\n", res->vcpu, res->core);
	else
		cmdline_printf(cl, "Unable to pin vCPU(%"PRId8") to pCPU core "
				"%"PRId8")\n", res->vcpu, res->core);
}

cmdline_parse_token_string_t cmd_set_pcpu =
		TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result,
				set_pcpu, "set_pcpu");
cmdline_parse_token_string_t cmd_set_pcpu_vm_name =
		TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result,
				vm_name, NULL);
cmdline_parse_token_num_t set_pcpu_vcpu =
		TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result,
				vcpu, UINT8);
cmdline_parse_token_num_t set_pcpu_core =
		TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result,
				core, UINT64);


cmdline_parse_inst_t cmd_set_pcpu_set = {
		.f = cmd_set_pcpu_parsed,
		.data = NULL,
		.help_str = "set_pcpu <vm_name> <vcpu> <pcpu>, Set the binding "
				"of Virtual CPU on VM to the Physical CPU.",
				.tokens = {
						(void *)&cmd_set_pcpu,
						(void *)&cmd_set_pcpu_vm_name,
						(void *)&set_pcpu_vcpu,
						(void *)&set_pcpu_core,
						NULL,
		},
};

struct cmd_vm_op_result {
	cmdline_fixed_string_t op_vm;
	cmdline_fixed_string_t vm_name;
};

static void
cmd_vm_op_parsed(void *parsed_result, struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_vm_op_result *res = parsed_result;

	if (!strcmp(res->op_vm, "add_vm")) {
		if (add_vm(res->vm_name) < 0)
			cmdline_printf(cl, "Unable to add VM '%s'\n", res->vm_name);
	} else if (remove_vm(res->vm_name) < 0)
		cmdline_printf(cl, "Unable to remove VM '%s'\n", res->vm_name);
}

cmdline_parse_token_string_t cmd_vm_op =
	TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result,
			op_vm, "add_vm#rm_vm");
cmdline_parse_token_string_t cmd_vm_name =
	TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result,
			vm_name, NULL);

cmdline_parse_inst_t cmd_vm_op_set = {
	.f = cmd_vm_op_parsed,
	.data = NULL,
	.help_str = "add_vm|rm_vm <name>, add a VM for "
			"subsequent operations with the CLI or remove a previously added "
			"VM from the VM Power Manager",
	.tokens = {
		(void *)&cmd_vm_op,
		(void *)&cmd_vm_name,
	NULL,
	},
};

/* *** VM channel operations *** */
struct cmd_channels_op_result {
	cmdline_fixed_string_t op;
	cmdline_fixed_string_t vm_name;
	cmdline_fixed_string_t channel_list;
};
static void
cmd_channels_op_parsed(void *parsed_result, struct cmdline *cl,
			__attribute__((unused)) void *data)
{
	unsigned num_channels = 0, channel_num, i;
	int channels_added;
	unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS];
	char *token, *remaining, *tail_ptr;
	struct cmd_channels_op_result *res = parsed_result;

	if (!strcmp(res->channel_list, "all")) {
		channels_added = add_all_channels(res->vm_name);
		cmdline_printf(cl, "Added %d channels for VM '%s'\n",
				channels_added, res->vm_name);
		return;
	}

	remaining = res->channel_list;
	while (1) {
		if (remaining == NULL || remaining[0] == '\0')
			break;

		token = strsep(&remaining, ",");
		if (token == NULL)
			break;
		errno = 0;
		channel_num = (unsigned)strtol(token, &tail_ptr, 10);
		if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
			break;

		if (channel_num == CHANNEL_CMDS_MAX_VM_CHANNELS) {
			cmdline_printf(cl, "Channel number '%u' exceeds the maximum number "
					"of allowable channels(%u) for VM '%s'\n", channel_num,
					CHANNEL_CMDS_MAX_VM_CHANNELS, res->vm_name);
			return;
		}
		channel_list[num_channels++] = channel_num;
	}
	for (i = 0; i < num_channels; i++)
		cmdline_printf(cl, "[%u]: Adding channel %u\n", i, channel_list[i]);

	channels_added = add_channels(res->vm_name, channel_list,
			num_channels);
	cmdline_printf(cl, "Enabled %d channels for '%s'\n", channels_added,
			res->vm_name);
}

cmdline_parse_token_string_t cmd_channels_op =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
				op, "add_channels");
cmdline_parse_token_string_t cmd_channels_vm_name =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
			vm_name, NULL);
cmdline_parse_token_string_t cmd_channels_list =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result,
			channel_list, NULL);

cmdline_parse_inst_t cmd_channels_op_set = {
	.f = cmd_channels_op_parsed,
	.data = NULL,
	.help_str = "add_channels <vm_name> <list>|all, add "
			"communication channels for the specified VM, the "
			"virtio channels must be enabled in the VM "
			"configuration(qemu/libvirt) and the associated VM must be active. "
			"<list> is a comma-separated list of channel numbers to add, using "
			"the keyword 'all' will attempt to add all channels for the VM",
	.tokens = {
		(void *)&cmd_channels_op,
		(void *)&cmd_channels_vm_name,
		(void *)&cmd_channels_list,
		NULL,
	},
};

struct cmd_channels_status_op_result {
	cmdline_fixed_string_t op;
	cmdline_fixed_string_t vm_name;
	cmdline_fixed_string_t channel_list;
	cmdline_fixed_string_t status;
};

static void
cmd_channels_status_op_parsed(void *parsed_result, struct cmdline *cl,
		       __attribute__((unused)) void *data)
{
	unsigned num_channels = 0, channel_num;
	int changed;
	unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS];
	char *token, *remaining, *tail_ptr;
	struct cmd_channels_status_op_result *res = parsed_result;
	enum channel_status status;

	if (!strcmp(res->status, "enabled"))
		status = CHANNEL_MGR_CHANNEL_CONNECTED;
	else
		status = CHANNEL_MGR_CHANNEL_DISABLED;

	if (!strcmp(res->channel_list, "all")) {
		changed = set_channel_status_all(res->vm_name, status);
		cmdline_printf(cl, "Updated status of %d channels "
				"for VM '%s'\n", changed, res->vm_name);
		return;
	}
	remaining = res->channel_list;
	while (1) {
		if (remaining == NULL || remaining[0] == '\0')
			break;
		token = strsep(&remaining, ",");
		if (token == NULL)
			break;
		errno = 0;
		channel_num = (unsigned)strtol(token, &tail_ptr, 10);
		if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
			break;

		if (channel_num == CHANNEL_CMDS_MAX_VM_CHANNELS) {
			cmdline_printf(cl, "%u exceeds the maximum number of allowable "
					"channels(%u) for VM '%s'\n", channel_num,
					CHANNEL_CMDS_MAX_VM_CHANNELS, res->vm_name);
			return;
		}
		channel_list[num_channels++] = channel_num;
	}
	changed = set_channel_status(res->vm_name, channel_list, num_channels,
			status);
	cmdline_printf(cl, "Updated status of %d channels "
					"for VM '%s'\n", changed, res->vm_name);
}

cmdline_parse_token_string_t cmd_channels_status_op =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
				op, "set_channel_status");
cmdline_parse_token_string_t cmd_channels_status_vm_name =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
			vm_name, NULL);
cmdline_parse_token_string_t cmd_channels_status_list =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
			channel_list, NULL);
cmdline_parse_token_string_t cmd_channels_status =
	TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result,
			status, "enabled#disabled");

cmdline_parse_inst_t cmd_channels_status_op_set = {
	.f = cmd_channels_status_op_parsed,
	.data = NULL,
	.help_str = "set_channel_status <vm_name> <list>|all enabled|disabled, "
			" enable or disable the communication channels in "
			"list(comma-separated) for the specified VM, alternatively "
			"list can be replaced with keyword 'all'. "
			"Disabled channels will still receive packets on the host, "
			"however the commands they specify will be ignored. "
			"Set status to 'enabled' to begin processing requests again.",
	.tokens = {
		(void *)&cmd_channels_status_op,
		(void *)&cmd_channels_status_vm_name,
		(void *)&cmd_channels_status_list,
		(void *)&cmd_channels_status,
		NULL,
	},
};

/* *** CPU Frequency operations *** */
struct cmd_show_cpu_freq_mask_result {
	cmdline_fixed_string_t show_cpu_freq_mask;
	uint64_t core_mask;
};

static void
cmd_show_cpu_freq_mask_parsed(void *parsed_result, struct cmdline *cl,
		       __attribute__((unused)) void *data)
{
	struct cmd_show_cpu_freq_mask_result *res = parsed_result;
	unsigned i;
	uint64_t mask = res->core_mask;
	uint32_t freq;

	for (i = 0; mask; mask &= ~(1ULL << i++)) {
		if ((mask >> i) & 1) {
			freq = power_manager_get_current_frequency(i);
			if (freq > 0)
				cmdline_printf(cl, "Core %u: %"PRId32"\n", i, freq);
		}
	}
}

cmdline_parse_token_string_t cmd_show_cpu_freq_mask =
	TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_mask_result,
			show_cpu_freq_mask, "show_cpu_freq_mask");
cmdline_parse_token_num_t cmd_show_cpu_freq_mask_core_mask =
	TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_mask_result,
			core_mask, UINT64);

cmdline_parse_inst_t cmd_show_cpu_freq_mask_set = {
	.f = cmd_show_cpu_freq_mask_parsed,
	.data = NULL,
	.help_str = "show_cpu_freq_mask <mask>, Get the current frequency for each "
			"core specified in the mask",
	.tokens = {
		(void *)&cmd_show_cpu_freq_mask,
		(void *)&cmd_show_cpu_freq_mask_core_mask,
		NULL,
	},
};

struct cmd_set_cpu_freq_mask_result {
	cmdline_fixed_string_t set_cpu_freq_mask;
	uint64_t core_mask;
	cmdline_fixed_string_t cmd;
};

static void
cmd_set_cpu_freq_mask_parsed(void *parsed_result, struct cmdline *cl,
			__attribute__((unused)) void *data)
{
	struct cmd_set_cpu_freq_mask_result *res = parsed_result;
	int ret = -1;

	if (!strcmp(res->cmd , "up"))
		ret = power_manager_scale_mask_up(res->core_mask);
	else if (!strcmp(res->cmd , "down"))
		ret = power_manager_scale_mask_down(res->core_mask);
	else if (!strcmp(res->cmd , "min"))
		ret = power_manager_scale_mask_min(res->core_mask);
	else if (!strcmp(res->cmd , "max"))
		ret = power_manager_scale_mask_max(res->core_mask);
	else if (!strcmp(res->cmd, "enable_turbo"))
		ret = power_manager_enable_turbo_mask(res->core_mask);
	else if (!strcmp(res->cmd, "disable_turbo"))
		ret = power_manager_disable_turbo_mask(res->core_mask);
	if (ret < 0) {
		cmdline_printf(cl, "Error scaling core_mask(0x%"PRIx64") '%s' , not "
				"all cores specified have been scaled\n",
				res->core_mask, res->cmd);
	};
}

cmdline_parse_token_string_t cmd_set_cpu_freq_mask =
	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result,
			set_cpu_freq_mask, "set_cpu_freq_mask");
cmdline_parse_token_num_t cmd_set_cpu_freq_mask_core_mask =
	TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_mask_result,
			core_mask, UINT64);
cmdline_parse_token_string_t cmd_set_cpu_freq_mask_result =
	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result,
			cmd, "up#down#min#max#enable_turbo#disable_turbo");

cmdline_parse_inst_t cmd_set_cpu_freq_mask_set = {
	.f = cmd_set_cpu_freq_mask_parsed,
	.data = NULL,
	.help_str = "set_cpu_freq <core_mask> <up|down|min|max|enable_turbo|disable_turbo>, adjust the current "
			"frequency for the cores specified in <core_mask>",
	.tokens = {
		(void *)&cmd_set_cpu_freq_mask,
		(void *)&cmd_set_cpu_freq_mask_core_mask,
		(void *)&cmd_set_cpu_freq_mask_result,
		NULL,
	},
};



struct cmd_show_cpu_freq_result {
	cmdline_fixed_string_t show_cpu_freq;
	uint8_t core_num;
};

static void
cmd_show_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
		       __attribute__((unused)) void *data)
{
	struct cmd_show_cpu_freq_result *res = parsed_result;
	uint32_t curr_freq = power_manager_get_current_frequency(res->core_num);

	if (curr_freq == 0) {
		cmdline_printf(cl, "Unable to get frequency for core %u\n",
				res->core_num);
		return;
	}
	cmdline_printf(cl, "Core %u frequency: %"PRId32"\n", res->core_num,
			curr_freq);
}

cmdline_parse_token_string_t cmd_show_cpu_freq =
	TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_result,
			show_cpu_freq, "show_cpu_freq");

cmdline_parse_token_num_t cmd_show_cpu_freq_core_num =
	TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_result,
			core_num, UINT8);

cmdline_parse_inst_t cmd_show_cpu_freq_set = {
	.f = cmd_show_cpu_freq_parsed,
	.data = NULL,
	.help_str = "Get the current frequency for the specified core",
	.tokens = {
		(void *)&cmd_show_cpu_freq,
		(void *)&cmd_show_cpu_freq_core_num,
		NULL,
	},
};

struct cmd_set_cpu_freq_result {
	cmdline_fixed_string_t set_cpu_freq;
	uint8_t core_num;
	cmdline_fixed_string_t cmd;
};

static void
cmd_set_cpu_freq_parsed(void *parsed_result, struct cmdline *cl,
		       __attribute__((unused)) void *data)
{
	int ret = -1;
	struct cmd_set_cpu_freq_result *res = parsed_result;

	if (!strcmp(res->cmd , "up"))
		ret = power_manager_scale_core_up(res->core_num);
	else if (!strcmp(res->cmd , "down"))
		ret = power_manager_scale_core_down(res->core_num);
	else if (!strcmp(res->cmd , "min"))
		ret = power_manager_scale_core_min(res->core_num);
	else if (!strcmp(res->cmd , "max"))
		ret = power_manager_scale_core_max(res->core_num);
	else if (!strcmp(res->cmd, "enable_turbo"))
		ret = power_manager_enable_turbo_core(res->core_num);
	else if (!strcmp(res->cmd, "disable_turbo"))
		ret = power_manager_disable_turbo_core(res->core_num);
	if (ret < 0) {
		cmdline_printf(cl, "Error scaling core(%u) '%s'\n", res->core_num,
				res->cmd);
	}
}

cmdline_parse_token_string_t cmd_set_cpu_freq =
	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
			set_cpu_freq, "set_cpu_freq");
cmdline_parse_token_num_t cmd_set_cpu_freq_core_num =
	TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result,
			core_num, UINT8);
cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd =
	TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result,
			cmd, "up#down#min#max#enable_turbo#disable_turbo");

cmdline_parse_inst_t cmd_set_cpu_freq_set = {
	.f = cmd_set_cpu_freq_parsed,
	.data = NULL,
	.help_str = "set_cpu_freq <core_num> <up|down|min|max|enable_turbo|disable_turbo>, adjust the current "
			"frequency for the specified core",
	.tokens = {
		(void *)&cmd_set_cpu_freq,
		(void *)&cmd_set_cpu_freq_core_num,
		(void *)&cmd_set_cpu_freq_cmd_cmd,
		NULL,
	},
};

cmdline_parse_ctx_t main_ctx[] = {
		(cmdline_parse_inst_t *)&cmd_quit,
		(cmdline_parse_inst_t *)&cmd_vm_op_set,
		(cmdline_parse_inst_t *)&cmd_channels_op_set,
		(cmdline_parse_inst_t *)&cmd_channels_status_op_set,
		(cmdline_parse_inst_t *)&cmd_show_vm_set,
		(cmdline_parse_inst_t *)&cmd_show_cpu_freq_mask_set,
		(cmdline_parse_inst_t *)&cmd_set_cpu_freq_mask_set,
		(cmdline_parse_inst_t *)&cmd_show_cpu_freq_set,
		(cmdline_parse_inst_t *)&cmd_set_cpu_freq_set,
		(cmdline_parse_inst_t *)&cmd_set_pcpu_mask_set,
		(cmdline_parse_inst_t *)&cmd_set_pcpu_set,
		NULL,
};

void
run_cli(__attribute__((unused)) void *arg)
{
	struct cmdline *cl;

	cl = cmdline_stdin_new(main_ctx, "vmpower> ");
	if (cl == NULL)
		return;

	cmdline_interact(cl);
	cmdline_stdin_exit(cl);
}
#else
static struct cli_map vm_map[] = {
	{  10, "show_vm %s" },
	{  20, "add_vm %s" },
	{  30, "rm_vm %s" },
	{-1, NULL}
};

static int
vm_cmd(int argc, char **argv)
{
	struct cli_map *m;

	m = cli_mapping(vm_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10: {
		struct vm_info info;
		unsigned i;

		if (get_info_vm(argv[1], &info) != 0)
			return 0;
		cli_printf("VM: '%s', status = ", info.name);
		if (info.status == CHANNEL_MGR_VM_ACTIVE)
			cli_printf("ACTIVE\n");
		else
			cli_printf("INACTIVE\n");
		cli_printf("Channels %u\n", info.num_channels);
		for (i = 0; i < info.num_channels; i++) {
			cli_printf("  [%u]: %s, status = ", i,
					info.channels[i].channel_path);
			switch (info.channels[i].status) {
			case CHANNEL_MGR_CHANNEL_CONNECTED:
				cli_printf("CONNECTED\n");
				break;
			case CHANNEL_MGR_CHANNEL_DISCONNECTED:
				cli_printf("DISCONNECTED\n");
				break;
			case CHANNEL_MGR_CHANNEL_DISABLED:
				cli_printf("DISABLED\n");
				break;
			case CHANNEL_MGR_CHANNEL_PROCESSING:
				cli_printf("PROCESSING\n");
				break;
			default:
				cli_printf("UNKNOWN\n");
				break;
			}
		}
		cli_printf("Virtual CPU(s): %u\n", info.num_vcpus);
		for (i = 0; i < info.num_vcpus; i++) {
			cli_printf("  [%u]: Physical CPU Mask 0x%"PRIx64"\n", i,
					info.pcpu_mask[i]);
		}
		}
		break;
	case 20:
	case 30:
		if (!strcmp(argv[0], "add_vm")) {
			if (add_vm(argv[1]) < 0)
				cli_printf("Unable to add VM '%s'\n", argv[1]);
		} else if (remove_vm(argv[1]) < 0)
			cli_printf("Unable to remove VM '%s'\n", argv[1]);
		break;
	}
	return 0;
}

static struct cli_map channel_map[] = {
	{  10, "add_channels %s %s"},
	{  20, "set_channel_status %s %s %|enable|disable"},
	{-1, NULL}
};

static int
channel_cmd(int argc, char **argv)
{
	struct cli_map *m;

	m = cli_mapping(channel_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10: {
		unsigned num_channels = 0, channel_num, i;
		int channels_added;
		unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS];
		char *token, *remaining, *tail_ptr;

		if (!strcmp(argv[2], "all")) {
			channels_added = add_all_channels(argv[1]);
			cli_printf("Added %d channels for VM '%s'\n",
					channels_added, argv[1]);
			return 0;
		}

		remaining = argv[2];
		while (1) {
			if (remaining == NULL || remaining[0] == '\0')
				break;

			token = strsep(&remaining, ",");
			if (token == NULL)
				break;
			errno = 0;
			channel_num = (unsigned)strtol(token, &tail_ptr, 10);
			if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
				break;

			if (channel_num == CHANNEL_CMDS_MAX_VM_CHANNELS) {
				cli_printf("Channel number '%u' exceeds the maximum number "
						"of allowable channels(%u) for VM '%s'\n", channel_num,
						CHANNEL_CMDS_MAX_VM_CHANNELS, argv[1]);
				return 0;
			}
			channel_list[num_channels++] = channel_num;
		}
		for (i = 0; i < num_channels; i++)
			cli_printf("[%u]: Adding channel %u\n", i, channel_list[i]);

		channels_added = add_channels(argv[1], channel_list,
				num_channels);
		cli_printf("Enabled %d channels for '%s'\n", channels_added,
				argv[1]);
		}
		break;
	case 20: {
		unsigned num_channels = 0, channel_num;
		int changed;
		unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS];
		char *token, *remaining, *tail_ptr;
		enum channel_status status;

		if (!strcmp(argv[3], "enabled"))
			status = CHANNEL_MGR_CHANNEL_CONNECTED;
		else
			status = CHANNEL_MGR_CHANNEL_DISABLED;

		if (!strcmp(argv[2], "all")) {
			changed = set_channel_status_all(argv[1], status);
			cli_printf("Updated status of %d channels "
					"for VM '%s'\n", changed, argv[1]);
			return 0;
		}
		remaining = argv[2];
		while (1) {
			if (remaining == NULL || remaining[0] == '\0')
				break;
			token = strsep(&remaining, ",");
			if (token == NULL)
				break;
			errno = 0;
			channel_num = (unsigned)strtol(token, &tail_ptr, 10);
			if ((errno != 0) || tail_ptr == NULL || (*tail_ptr != '\0'))
				break;

			if (channel_num == CHANNEL_CMDS_MAX_VM_CHANNELS) {
				cli_printf("%u exceeds the maximum number of allowable "
						"channels(%u) for VM '%s'\n", channel_num,
						CHANNEL_CMDS_MAX_VM_CHANNELS, argv[1]);
				return 0;
			}
			channel_list[num_channels++] = channel_num;
		}
		changed = set_channel_status(argv[1], channel_list, num_channels,
				status);
		cli_printf("Updated status of %d channels "
					"for VM '%s'\n", changed, argv[1]);
		}
		break;
	}
	return 0;
}

static struct cli_map cpu_map[] = {
	{  10, "set_cpu_freq %d %|up|down|min|max" },
	{  15, "set_cpu_freq_mask %D %|up|down|min|max"},
	{  20, "show_cpu_freq %d" },
	{  25, "show_cpu_freq_mask %d" },
	{-1, NULL}
};

static int
cpu_cmd(int argc, char **argv)
{
	struct cli_map *m;
	uint64_t core_mask;
	uint8_t core_num;
	unsigned i;
	char *cmd;
	int ret = -1;
	uint32_t freq;

	m = cli_mapping(cpu_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10:
		cmd = argv[2];
		core_num = atoi(argv[1]);
		if (!strcmp(cmd , "up"))
			ret = power_manager_scale_core_up(core_num);
		else if (!strcmp(cmd , "down"))
			ret = power_manager_scale_core_down(core_num);
		else if (!strcmp(cmd , "min"))
			ret = power_manager_scale_core_min(core_num);
		else if (!strcmp(cmd , "max"))
			ret = power_manager_scale_core_max(core_num);
		if (ret < 0) {
			cli_printf("Error scaling core(%u) '%s'\n",
				core_num, cmd);
		}
		break;
	case 15:
		cmd = argv[2];
		core_mask = strtoull(argv[1], NULL, 0);
		if (!strcmp(cmd , "up"))
			ret = power_manager_scale_mask_up(core_mask);
		else if (!strcmp(cmd , "down"))
			ret = power_manager_scale_mask_down(core_mask);
		else if (!strcmp(cmd , "min"))
			ret = power_manager_scale_mask_min(core_mask);
		else if (!strcmp(cmd , "max"))
			ret = power_manager_scale_mask_max(core_mask);
		if (ret < 0) {
			cli_printf("Error scaling core_mask(0x%"PRIx64
				") '%s' , not "
				"all cores specified have been scaled\n",
				core_mask, cmd);
		};
		break;
	case 20: {
		uint8_t core_num = atoi(argv[1]);
		uint32_t curr_freq =
			power_manager_get_current_frequency(core_num);

		if (curr_freq == 0) {
			cli_printf("Unable to get frequency for core %u\n",
					core_num);
			return 0;
		}
		cli_printf("Core %u frequency: %"PRId32"\n", core_num,
				curr_freq);
		}
		break;
	case 25:
		core_mask = strtoull(argv[1], NULL, 0);
		for (i = 0; core_mask; core_mask &= ~(1ULL << i++)) {
			if ((core_mask >> i) & 1) {
				freq = power_manager_get_current_frequency(i);
				if (freq > 0)
					cli_printf("Core %u: %"PRId32"\n",
						i, freq);
			}
		}
		break;
	}

	return 0;
}

static struct cli_map pcpu_map[] = {
	{  10, "set_pcpu_mask %s %d %D" },
	{  20, "set_pcpu %s %d %D"},
	{-1, NULL}
};

static int
pcpu_cmd(int argc, char **argv)
{
	struct cli_map *m;
	uint8_t vcpu;
	uint64_t mask;

	m = cli_mapping(pcpu_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10:
		vcpu = atoi(argv[2]);
		mask = strtoull(argv[3], NULL, 0);
		if (set_pcpus_mask(argv[1], vcpu, mask) == 0)
			cli_printf("Pinned vCPU(%"PRId8") to pCPU core "
				"mask(0x%"PRIx64")\n", vcpu, mask);
		else
			cli_printf("Unable to pin vCPU(%"PRId8") to pCPU core "
					"mask(0x%"PRIx64")\n", vcpu, mask);
		break;
	case 20:
		vcpu = atoi(argv[2]);
		mask = strtoull(argv[3], NULL, 0);
		if (set_pcpu(argv[1], vcpu, mask) == 0)
			cli_printf("Pinned vCPU(%"PRId8") to pCPU core "
					"%"PRId64")\n", vcpu, mask);
		else
			cli_printf("Unable to pin vCPU(%"PRId8") to pCPU core "
					"%"PRId64")\n", vcpu, mask);
		break;
	}
	return 0;
}

static struct cli_map my_map[] = {
	{ 10, "exit" },
	{ 20, "help" },
	{-1, NULL}
};

static int
my_cmd(int argc, char **argv)
{
	struct cli_map *m;

	m = cli_mapping(my_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10:
		channel_monitor_exit();
		channel_manager_exit();
		power_manager_exit();
		cli_quit();
		break;
	case 20:
		break;
	}
	return 0;
}

static struct cli_tree my_tree[] = {
	c_dir("/bin"),
	c_cmd("add_vm",	vm_cmd,	"add a VM"),
	c_cmd("rm_vm",	vm_cmd, "remove a VM"),
	c_cmd("show_vm", vm_cmd, "show a VM"),

	c_cmd("add_channels", channel_cmd, "add channels"),
	c_cmd("set_channel_status", channel_cmd, "set channel status"),

	c_cmd("show_cpu_freq_mask", cpu_cmd, "show cpu freq mask"),
	c_cmd("set_cpu_freq", cpu_cmd, "set cpu freq mask"),
	c_cmd("show_cpu_freq", cpu_cmd, "show cpu freq"),
	c_cmd("set_pcpu_mask", pcpu_cmd, "set pcpu mask"),
	c_cmd("set_pcpu", pcpu_cmd, "set pcpu"),

	c_cmd("help",	my_cmd,	"print help"),
	c_cmd("exit",	my_cmd,	"quit program"),
	c_end()
};

static void
my_prompt(int cont __rte_unused)
{
	char *str = cli_cwd_path();

	if (strlen(str) > 1)	/* trim the trailing '/' from string */
		str[strlen(str) - 1] = '\0';

	vt100_color(SCRN_GREEN, SCRN_NO_CHANGE, SCRN_BOLD);
	cli_printf("%s:", (cont) ? " >> " : "vmpower");
	vt100_color(SCRN_BLUE, SCRN_NO_CHANGE, SCRN_BOLD);
	cli_printf("%s", str);
	vt100_color(SCRN_DEFAULT_FG, SCRN_DEFAULT_BG, SCRN_OFF);
	cli_printf("> ");
}

static int
init_tree(void)
{
	/*
	 * Root is created already and using system default cmds and dirs, the
	 * developer is not required to use the system default cmds/dirs.
	 */
	if (cli_default_tree_init())
		 return -1;

	/* Using NULL here to start at root directory */
	if (cli_add_tree(NULL, my_tree))
		return -1;

	cli_set_prompt(my_prompt);

	return cli_add_bin_path("/bin");
}

/* prompt function, called from main on MASTER lcore */
void
run_cli(__attribute__((unused)) void *arg1)
{
	if (cli_create_with_tree(init_tree) == 0) {
		cli_start(NULL);

		cli_destroy();
	}
}
#endif