Index: usr.sbin/bhyvectl/bhyvectl.c =================================================================== --- usr.sbin/bhyvectl/bhyvectl.c +++ usr.sbin/bhyvectl/bhyvectl.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -74,6 +75,7 @@ " [--cpu=]\n" " [--create]\n" " [--destroy]\n" + " [--list]\n" " [--get-all]\n" " [--get-stats]\n" " [--set-desc-ds]\n" @@ -236,7 +238,7 @@ static int inject_nmi, assert_lapic_lvt; static int force_reset, force_poweroff; static const char *capname; -static int create, destroy, get_lowmem, get_highmem; +static int create, destroy, list, get_lowmem, get_highmem; static int get_intinfo; static int get_active_cpus, get_suspended_cpus; static uint64_t memsize; @@ -582,6 +584,25 @@ printf("\n"); } +/* + * Parse the number of CPU specified to a VM and + * returns the total number of it. + */ +static int +count_vcpus(const cpuset_t *cpus) +{ + int i, count = 0; + + if (!CPU_EMPTY(cpus)) { + for (i = 0; i < CPU_SETSIZE; i++) { + if (CPU_ISSET(count, cpus)) + count++; + } + } + + return count; +} + static void print_intinfo(const char *banner, uint64_t info) { @@ -1373,6 +1394,7 @@ { "run", NO_ARG, &run, 1 }, { "create", NO_ARG, &create, 1 }, { "destroy", NO_ARG, &destroy, 1 }, + { "list", NO_ARG, &list, 1}, { "inject-nmi", NO_ARG, &inject_nmi, 1 }, { "force-reset", NO_ARG, &force_reset, 1 }, { "force-poweroff", NO_ARG, &force_poweroff, 1 }, @@ -1520,6 +1542,58 @@ return ("UNK"); } +static void +list_vm(char *vmname) +{ + DIR *path_vmm; + struct dirent *dir; + struct vmctx *ctx; + size_t memory_size; + int error, wired, vcpus = 0; + vm_paddr_t gpa; + cpuset_t cpus; + + printf("NAME\t\t MEM\t\t\t VCPU\n"); + + path_vmm = opendir("/dev/vmm"); + if (path_vmm) { + while ((dir = readdir(path_vmm)) != NULL) { + if (strcmp(".", dir->d_name) != 0 && + strcmp("..", dir->d_name) != 0) { + + ctx = vm_open(dir->d_name); + if (ctx == NULL) { + printf("You have no permission to open: %s\n", vmname); + exit (1); + } + + gpa = 0; + + error = vm_get_memory_seg(ctx, gpa, &memory_size, + &wired); + if (error) + printf("Error to get memory segment information.\n"); + + error = vm_active_cpus(ctx, &cpus); + if (!error) + vcpus = count_vcpus(&cpus); + else + printf("Error to get the number of vcpus.\n"); + + if (memory_size && vcpus) { + if (vmname && strcmp(vmname, dir->d_name) == 0) { + printf("%s\t\t %zu\t\t %d\n", dir->d_name, + memory_size, vcpus); + break; + } else if (!vmname) + printf("%s\t\t %zu\t\t %d\n", dir->d_name, + memory_size, vcpus); + } + } + } + } +} + int main(int argc, char *argv[]) { @@ -1686,7 +1760,16 @@ argc -= optind; argv += optind; - if (vmname == NULL) + if (list) { + if (vmname) + list_vm(vmname); + else + list_vm(NULL); + + exit (0); + } + + if (vmname == NULL && !list) usage(cpu_intel); error = 0;