From b57f5d3e6a2df8d435e606797cf3934811848343 Mon Sep 17 00:00:00 2001 From: Patrick Mooney Date: Fri, 28 May 2021 21:07:11 +0000 Subject: 13833 want bhyve memory reservoir 13822 bhyve memory should exert memory pressure 13834 want extensible page_resv 13821 vmmctl ioctls should have more structure Reviewed by: Andy Fiddaman Reviewed by: Jason King Reviewed by: Dan Cross Reviewed by: Hans Rosenfeld Reviewed by: Mike Zeller Approved by: Dan McDonald --- usr/src/lib/libvmmapi/common/vmmapi.c | 106 +++++++++++++++++++++------------- usr/src/lib/libvmmapi/common/vmmapi.h | 6 +- 2 files changed, 70 insertions(+), 42 deletions(-) (limited to 'usr/src/lib/libvmmapi') diff --git a/usr/src/lib/libvmmapi/common/vmmapi.c b/usr/src/lib/libvmmapi/common/vmmapi.c index ba3fb7f8dd..ec27949a43 100644 --- a/usr/src/lib/libvmmapi/common/vmmapi.c +++ b/usr/src/lib/libvmmapi/common/vmmapi.c @@ -39,7 +39,7 @@ * * Copyright 2015 Pluribus Networks Inc. * Copyright 2019 Joyent, Inc. - * Copyright 2020 Oxide Computer Company + * Copyright 2021 Oxide Computer Company */ #include @@ -109,12 +109,31 @@ struct vmctx { #ifdef __FreeBSD__ #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x))) #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x))) -#else -#define CREATE(x) vm_do_ctl(VMM_CREATE_VM, (x)) -#define DESTROY(x) vm_do_ctl(VMM_DESTROY_VM, (x)) +int +vm_create(const char *name) +{ + /* Try to load vmm(4) module before creating a guest. */ + if (modfind("vmm") < 0) + kldload("vmm"); + return (CREATE((char *)name)); +} + +void +vm_destroy(struct vmctx *vm) +{ + assert(vm != NULL); + + if (vm->fd >= 0) + close(vm->fd); + DESTROY(vm->name); + + free(vm); +} + +#else static int -vm_do_ctl(int cmd, const char *name) +vm_do_ctl(int cmd, void *req) { int ctl_fd; @@ -123,7 +142,7 @@ vm_do_ctl(int cmd, const char *name) return (-1); } - if (ioctl(ctl_fd, cmd, name) == -1) { + if (ioctl(ctl_fd, cmd, req) == -1) { int err = errno; /* Do not lose ioctl errno through the close(2) */ @@ -135,6 +154,46 @@ vm_do_ctl(int cmd, const char *name) return (0); } + +int +vm_create(const char *name, uint64_t flags) +{ + struct vm_create_req req; + + (void) strncpy(req.name, name, VM_MAX_NAMELEN); + req.flags = flags; + + return (vm_do_ctl(VMM_CREATE_VM, &req)); +} + +void +vm_close(struct vmctx *vm) +{ + assert(vm != NULL); + assert(vm->fd >= 0); + + (void) close(vm->fd); + + free(vm); +} + +void +vm_destroy(struct vmctx *vm) +{ + struct vm_destroy_req req; + + assert(vm != NULL); + + if (vm->fd >= 0) { + (void) close(vm->fd); + vm->fd = -1; + } + + (void) strncpy(req.name, vm->name, VM_MAX_NAMELEN); + (void) vm_do_ctl(VMM_DESTROY_VM, &req); + + free(vm); +} #endif static int @@ -155,17 +214,6 @@ vm_device_open(const char *name) return (fd); } -int -vm_create(const char *name) -{ -#ifdef __FreeBSD__ - /* Try to load vmm(4) module before creating a guest. */ - if (modfind("vmm") < 0) - kldload("vmm"); -#endif - return (CREATE((char *)name)); -} - struct vmctx * vm_open(const char *name) { @@ -189,30 +237,6 @@ err: return (NULL); } -#ifndef __FreeBSD__ -void -vm_close(struct vmctx *vm) -{ - assert(vm != NULL); - assert(vm->fd >= 0); - - (void) close(vm->fd); - - free(vm); -} -#endif - -void -vm_destroy(struct vmctx *vm) -{ - assert(vm != NULL); - - if (vm->fd >= 0) - close(vm->fd); - DESTROY(vm->name); - - free(vm); -} int vm_parse_memsize(const char *optarg, size_t *ret_memsize) diff --git a/usr/src/lib/libvmmapi/common/vmmapi.h b/usr/src/lib/libvmmapi/common/vmmapi.h index 79c7dc02ee..e239b70a56 100644 --- a/usr/src/lib/libvmmapi/common/vmmapi.h +++ b/usr/src/lib/libvmmapi/common/vmmapi.h @@ -39,7 +39,7 @@ * * Copyright 2015 Pluribus Networks Inc. * Copyright 2019 Joyent, Inc. - * Copyright 2020 Oxide Computer Company + * Copyright 2021 Oxide Computer Company */ #ifndef _VMMAPI_H_ @@ -134,7 +134,11 @@ int vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, int vm_munmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, size_t len); +#ifndef __FreeBSD__ +int vm_create(const char *name, uint64_t flags); +#else int vm_create(const char *name); +#endif /* __FreeBSD__ */ int vm_get_device_fd(struct vmctx *ctx); struct vmctx *vm_open(const char *name); #ifndef __FreeBSD__ -- cgit v1.2.3