Add nvmmctl, with two commands for now.

This commit is contained in:
maxv 2019-10-28 13:04:18 +00:00
parent f2feb5edb1
commit a8c6c0bf54
7 changed files with 270 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: md.amd64,v 1.280 2019/08/26 04:49:45 kamil Exp $
# $NetBSD: md.amd64,v 1.281 2019/10/28 13:04:18 maxv Exp $
./dev/lms0 base-obsolete obsolete
./dev/mms0 base-obsolete obsolete
./libexec/ld.elf_so-i386 base-sys-shlib compat,pic
@ -71,3 +71,4 @@
./usr/mdec/pxeboot_ia32_com0.bin base-obsolete obsolete
./usr/sbin/acpidump base-sysutil-bin
./usr/sbin/amldb base-sysutil-bin
./usr/sbin/nvmmctl base-sysutil-bin

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1659 2019/10/28 02:56:40 ozaki-r Exp $
# $NetBSD: mi,v 1.1660 2019/10/28 13:04:18 maxv Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -2873,6 +2873,7 @@
./usr/share/man/cat8/ntptime.0 man-ntp-catman .cat
./usr/share/man/cat8/ntptrace.0 man-ntp-catman .cat
./usr/share/man/cat8/nvmectl.0 man-sysutil-catman .cat
./usr/share/man/cat8/nvmmctl.0 man-sysutil-catman .cat
./usr/share/man/cat8/ofctl.0 man-sysutil-catman .cat
./usr/share/man/cat8/ofppc/MAKEDEV.0 man-obsolete obsolete
./usr/share/man/cat8/ofppc/makedev.0 man-obsolete obsolete
@ -5823,6 +5824,7 @@
./usr/share/man/html8/ntptime.html man-ntp-htmlman html
./usr/share/man/html8/ntptrace.html man-ntp-htmlman html
./usr/share/man/html8/nvmectl.html man-sysutil-htmlman html
./usr/share/man/html8/nvmmctl.html man-sysutil-htmlman html
./usr/share/man/html8/ofctl.html man-sysutil-htmlman html
./usr/share/man/html8/oqmgr.html man-postfix-htmlman postfix,html
./usr/share/man/html8/pac.html man-sysutil-htmlman html
@ -8997,6 +8999,7 @@
./usr/share/man/man8/ntptime.8 man-ntp-man .man
./usr/share/man/man8/ntptrace.8 man-ntp-man .man
./usr/share/man/man8/nvmectl.8 man-sysutil-man .man
./usr/share/man/man8/nvmmctl.8 man-sysutil-man .man
./usr/share/man/man8/ofctl.8 man-sysutil-man .man
./usr/share/man/man8/ofppc/MAKEDEV.8 man-obsolete obsolete
./usr/share/man/man8/ofppc/makedev.8 man-obsolete obsolete

View File

@ -1,4 +1,4 @@
# $NetBSD: bsd.hostprog.mk,v 1.81 2019/01/21 21:11:54 christos Exp $
# $NetBSD: bsd.hostprog.mk,v 1.82 2019/10/28 13:04:19 maxv Exp $
# @(#)bsd.prog.mk 8.2 (Berkeley) 4/2/94
.include <bsd.hostinit.mk>
@ -39,6 +39,7 @@ LIBMAGIC?= /usr/lib/libmagic.a
LIBMENU?= /usr/lib/libmenu.a
LIBMP?= /usr/lib/libmp.a
LIBNTP?= /usr/lib/libntp.a
LIBNVMM?= /usr/lib/libnvmm.a
LIBOBJC?= /usr/lib/libobjc.a
LIBP2K?= /usr/lib/libp2k.a
LIBPANEL?= /usr/lib/libpanel.a

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.281 2019/10/15 18:33:23 christos Exp $
# $NetBSD: Makefile,v 1.282 2019/10/28 13:04:18 maxv Exp $
# from: @(#)Makefile 5.20 (Berkeley) 6/12/93
.include <bsd.own.mk>
@ -54,6 +54,8 @@ SUBDIR+=mld6query route6d rtadvd traceroute6
SUBDIR+= racoon racoonctl
SUBDIR+= nvmmctl
# NPF
.if (${MKNPF} != "no")
SUBDIR+=npf

16
usr.sbin/nvmmctl/Makefile Normal file
View File

@ -0,0 +1,16 @@
# $NetBSD: Makefile,v 1.1 2019/10/28 13:04:18 maxv Exp $
.include <bsd.own.mk>
MAN= nvmmctl.8
.if (${MACHINE_ARCH} == "x86_64")
PROG= nvmmctl
SRCS= nvmmctl.c
CPPFLAGS+=-D_KERNTYPES
LDADD+=-lutil -lnvmm
DPADD+=${LIBUTIL} ${LIBNVMM}
.endif
.include <bsd.prog.mk>

View File

@ -0,0 +1,63 @@
.\" $NetBSD: nvmmctl.8,v 1.1 2019/10/28 13:04:18 maxv Exp $
.\"
.\" Copyright (c) 2019 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Maxime Villard.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
.\"
.Dd October 27, 2019
.Dt NVMMCTL 8
.Os
.Sh NAME
.Nm nvmmctl
.Nd program to control virtual machines
.Sh SYNOPSIS
.Nm nvmmctl
.Ar command
.Op Ar arguments
.Sh DESCRIPTION
The
.Nm
command can be used to control and inspect the state of virtual machines in the
system.
.Pp
The first argument,
.Ar command ,
specifies the action to take.
Valid commands are:
.Bl -tag -width XXXXXXXX
.It identify
Display the capabilities of the system.
.It list
Display information on each virtual machine registered in the system.
.El
.Sh SEE ALSO
.Xr nvmm 4 ,
.Xr libnvmm 3
.Sh AUTHORS
The
.Nm
tool was written by
.An Maxime Villard .

180
usr.sbin/nvmmctl/nvmmctl.c Normal file
View File

@ -0,0 +1,180 @@
/* $NetBSD: nvmmctl.c,v 1.1 2019/10/28 13:04:18 maxv Exp $ */
/*
* Copyright (c) 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Maxime Villard.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: nvmmctl.c,v 1.1 2019/10/28 13:04:18 maxv Exp $");
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <util.h>
#include <nvmm.h>
#include <x86/specialreg.h>
__dead static void usage(void);
static void nvmm_identify(char **);
static void nvmm_list(char **);
static struct cmdtab {
const char *label;
bool takesargs;
bool argsoptional;
void (*func)(char **);
} const nvmm_cmdtab[] = {
{ "identify", false, false, nvmm_identify },
{ "list", false, false, nvmm_list },
{ NULL, false, false, NULL },
};
static struct nvmm_capability cap;
int
main(int argc, char **argv)
{
const struct cmdtab *ct;
argc -= 1;
argv += 1;
if (argc < 1)
usage();
for (ct = nvmm_cmdtab; ct->label != NULL; ct++) {
if (strcmp(argv[0], ct->label) == 0) {
if (!ct->argsoptional &&
((ct->takesargs == 0) ^ (argv[1] == NULL)))
{
usage();
}
(*ct->func)(argv + 1);
break;
}
}
if (ct->label == NULL)
errx(EXIT_FAILURE, "unknown command ``%s''", argv[0]);
exit(EXIT_SUCCESS);
/* NOTREACHED */
}
static void
usage(void)
{
const char *progname = getprogname();
fprintf(stderr, "usage: %s identify\n", progname);
fprintf(stderr, " %s list\n", progname);
exit(EXIT_FAILURE);
/* NOTREACHED */
}
#define MACH_CONF_FLAGS "\20"
#define VCPU_CONF_FLAGS "\20" "\1" "CPUID" "\2" "TPR"
static void
nvmm_identify(char **argv)
{
char buf[256], ram[4+1];
if (nvmm_init() == -1)
err(EXIT_FAILURE, "nvmm_init failed");
if (nvmm_capability(&cap) == -1)
err(EXIT_FAILURE, "nvmm_capability failed");
printf("nvmm: Kernel API version %u\n", cap.version);
printf("nvmm: State size %u\n", cap.state_size);
printf("nvmm: Max machines %u\n", cap.max_machines);
printf("nvmm: Max VCPUs per machine %u\n", cap.max_vcpus);
if (humanize_number(ram, sizeof(ram), cap.max_ram, NULL, HN_AUTOSCALE,
(HN_DECIMAL | HN_B | HN_NOSPACE)) == -1)
err(EXIT_FAILURE, "humanize_number");
printf("nvmm: Max RAM per machine %s\n", ram);
snprintb(buf, sizeof(buf), MACH_CONF_FLAGS, cap.arch.mach_conf_support);
printf("nvmm: Arch Mach conf %s\n", buf);
snprintb(buf, sizeof(buf), VCPU_CONF_FLAGS, cap.arch.vcpu_conf_support);
printf("nvmm: Arch VCPU conf %s\n", buf);
snprintb(buf, sizeof(buf), XCR0_FLAGS1, cap.arch.xcr0_mask);
printf("nvmm: Guest FPU states %s\n", buf);
}
static void
nvmm_list(char **argv)
{
struct nvmm_ctl_mach_info machinfo;
char ram[4+1], *ts;
size_t i;
int ret;
if (nvmm_root_init() == -1)
err(EXIT_FAILURE, "nvmm_root_init failed");
if (nvmm_capability(&cap) == -1)
err(EXIT_FAILURE, "nvmm_capability failed");
printf(
"Machine ID VCPUs RAM Owner PID Creation Time \n"
"---------- ----- ---- --------- ------------------------\n");
for (i = 0; i < cap.max_machines; i++) {
machinfo.machid = i;
ret = nvmm_ctl(NVMM_CTL_MACH_INFO, &machinfo, sizeof(machinfo));
if (ret == -1) {
if (errno == ENOENT)
continue;
err(EXIT_FAILURE, "nvmm_ctl failed");
}
ts = asctime(localtime(&machinfo.time));
ts[strlen(ts) - 1] = '\0';
if (humanize_number(ram, sizeof(ram), machinfo.nram, NULL,
HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE)) == -1)
err(EXIT_FAILURE, "humanize_number");
printf("%-10zu %-5u %-4s %-9d %s\n", i, machinfo.nvcpus, ram,
machinfo.pid, ts);
}
}