diff options
author | Jasper Lievisse Adriaanse <jasper@cvs.openbsd.org> | 2019-05-11 19:55:15 +0000 |
---|---|---|
committer | Jasper Lievisse Adriaanse <jasper@cvs.openbsd.org> | 2019-05-11 19:55:15 +0000 |
commit | 635b3de6ae0f031f3a0763ae0d1f650f6acddbbb (patch) | |
tree | 4b099c98124098118df338b550c6bc9438a8aca4 /usr.sbin/vmd | |
parent | 2b87b4bb1e28e68b133067164e311e83bd9d71ed (diff) |
track the state of the vm (running, paused, etc) using a single bitfield instead of
a handful of separate variables. this will makes it easier for vmd to report
and check on the individual vm states
no functional change intended
ok ccardenas@ mlarkin@
Diffstat (limited to 'usr.sbin/vmd')
-rw-r--r-- | usr.sbin/vmd/config.c | 14 | ||||
-rw-r--r-- | usr.sbin/vmd/parse.y | 7 | ||||
-rw-r--r-- | usr.sbin/vmd/vm.c | 22 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.c | 42 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.h | 17 | ||||
-rw-r--r-- | usr.sbin/vmd/vmm.c | 20 |
6 files changed, 62 insertions, 60 deletions
diff --git a/usr.sbin/vmd/config.c b/usr.sbin/vmd/config.c index c49696cb38d..f9f303a3d2b 100644 --- a/usr.sbin/vmd/config.c +++ b/usr.sbin/vmd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.57 2018/11/26 05:44:46 ori Exp $ */ +/* $OpenBSD: config.c,v 1.58 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> @@ -230,7 +230,7 @@ config_setvm(struct privsep *ps, struct vmd_vm *vm, uint32_t peerid, uid_t uid) errno = 0; - if (vm->vm_running) { + if (vm->vm_state & VM_STATE_RUNNING) { log_warnx("%s: vm is already running", __func__); errno = EALREADY; return (-1); @@ -293,7 +293,7 @@ config_setvm(struct privsep *ps, struct vmd_vm *vm, uint32_t peerid, uid_t uid) vm->vm_peerid = peerid; vm->vm_uid = uid; - if (!vm->vm_received) { + if (!(vm->vm_state & VM_STATE_RECEIVED)) { if (strlen(vcp->vcp_kernel)) { /* * Boot kernel from disk image if path matches the @@ -480,7 +480,7 @@ config_setvm(struct privsep *ps, struct vmd_vm *vm, uint32_t peerid, uid_t uid) } /* Send VM information */ - if (vm->vm_received) + if (vm->vm_state & VM_STATE_RECEIVED) proc_compose_imsg(ps, PROC_VMM, -1, IMSG_VMDOP_RECEIVE_VM_REQUEST, vm->vm_vmid, fd, vmc, sizeof(struct vmop_create_params)); @@ -509,13 +509,13 @@ config_setvm(struct privsep *ps, struct vmd_vm *vm, uint32_t peerid, uid_t uid) &i, sizeof(i)); } - if (!vm->vm_received) + if (!(vm->vm_state & VM_STATE_RECEIVED)) proc_compose_imsg(ps, PROC_VMM, -1, IMSG_VMDOP_START_VM_END, vm->vm_vmid, fd, NULL, 0); free(tapfds); - vm->vm_running = 1; + vm->vm_state |= VM_STATE_RUNNING; return (0); fail: @@ -562,7 +562,7 @@ config_getvm(struct privsep *ps, struct imsg *imsg) /* If the fd is -1, the kernel will be searched on the disk */ vm->vm_kernel = imsg->fd; - vm->vm_running = 1; + vm->vm_state |= VM_STATE_RUNNING; vm->vm_peerid = (uint32_t)-1; return (0); diff --git a/usr.sbin/vmd/parse.y b/usr.sbin/vmd/parse.y index fd5902006cc..70ebf2b9b74 100644 --- a/usr.sbin/vmd/parse.y +++ b/usr.sbin/vmd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.50 2019/02/13 22:57:08 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.51 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org> @@ -357,7 +357,8 @@ vm : VM string vm_instance { log_debug("%s:%d: vm \"%s\"" " skipped (%s)", file->name, yylval.lineno, - vcp->vcp_name, vm->vm_running ? + vcp->vcp_name, + (vm->vm_state & VM_STATE_RUNNING) ? "running" : "already exists"); } else if (ret == -1) { yyerror("vm \"%s\" failed: %s", @@ -365,7 +366,7 @@ vm : VM string vm_instance { YYERROR; } else { if (vcp_disable) - vm->vm_disabled = 1; + vm->vm_state |= VM_STATE_DISABLED; log_debug("%s:%d: vm \"%s\" " "registered (%s)", file->name, yylval.lineno, diff --git a/usr.sbin/vmd/vm.c b/usr.sbin/vmd/vm.c index c865137dbe9..2b2acac51fd 100644 --- a/usr.sbin/vmd/vm.c +++ b/usr.sbin/vmd/vm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vm.c,v 1.45 2019/03/01 07:32:29 mlarkin Exp $ */ +/* $OpenBSD: vm.c,v 1.46 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -279,7 +279,7 @@ start_vm(struct vmd_vm *vm, int fd) setproctitle("%s", vcp->vcp_name); log_procinit(vcp->vcp_name); - if (!vm->vm_received) + if (!(vm->vm_state & VM_STATE_RECEIVED)) create_memory_map(vcp); ret = alloc_guest_mem(vcp); @@ -311,7 +311,7 @@ start_vm(struct vmd_vm *vm, int fd) if (pledge("stdio vmm recvfd", NULL) == -1) fatal("pledge"); - if (vm->vm_received) { + if (vm->vm_state & VM_STATE_RECEIVED) { ret = read(vm->vm_receive_fd, &vrp, sizeof(vrp)); if (ret != sizeof(vrp)) { fatal("received incomplete vrp - exiting"); @@ -359,7 +359,7 @@ start_vm(struct vmd_vm *vm, int fd) event_init(); - if (vm->vm_received) { + if (vm->vm_state & VM_STATE_RECEIVED) { restore_emulated_hw(vcp, vm->vm_receive_fd, nicfds, vm->vm_disks, vm->vm_cdrom); mc146818_start(); @@ -685,10 +685,10 @@ restore_vmr(int fd, struct vm_mem_range *vmr) void pause_vm(struct vm_create_params *vcp) { - if (current_vm->vm_paused) + if (current_vm->vm_state & VM_STATE_PAUSED) return; - current_vm->vm_paused = 1; + current_vm->vm_state |= VM_STATE_PAUSED; /* XXX: vcpu_run_loop is running in another thread and we have to wait * for the vm to exit before returning */ @@ -702,10 +702,10 @@ void unpause_vm(struct vm_create_params *vcp) { unsigned int n; - if (!current_vm->vm_paused) + if (!(current_vm->vm_state & VM_STATE_PAUSED)) return; - current_vm->vm_paused = 0; + current_vm->vm_state &= ~VM_STATE_PAUSED; i8253_start(); mc146818_start(); @@ -1094,7 +1094,7 @@ run_vm(int child_cdrom, int child_disks[][VM_MAX_BASE_PER_DISK], log_debug("%s: initializing hardware for vm %s", __func__, vcp->vcp_name); - if (!current_vm->vm_received) + if (!(current_vm->vm_state & VM_STATE_RECEIVED)) init_emulated_hw(vmc, child_cdrom, child_disks, child_taps); ret = pthread_mutex_init(&threadmutex, NULL); @@ -1146,7 +1146,7 @@ run_vm(int child_cdrom, int child_disks[][VM_MAX_BASE_PER_DISK], } /* once more because reset_cpu changes regs */ - if (current_vm->vm_received) { + if (current_vm->vm_state & VM_STATE_RECEIVED) { vregsp.vrwp_vm_id = vcp->vcp_id; vregsp.vrwp_vcpu_id = i; vregsp.vrwp_regs = *vrs; @@ -1295,7 +1295,7 @@ vcpu_run_loop(void *arg) /* If we are halted or paused, wait */ if (vcpu_hlt[n]) { - while (current_vm->vm_paused == 1) { + while (current_vm->vm_state & VM_STATE_PAUSED) { ret = pthread_cond_wait(&vcpu_run_cond[n], &vcpu_run_mtx[n]); if (ret) { diff --git a/usr.sbin/vmd/vmd.c b/usr.sbin/vmd/vmd.c index b8f9cdd2c84..5e86a4c6d93 100644 --- a/usr.sbin/vmd/vmd.c +++ b/usr.sbin/vmd/vmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.c,v 1.109 2019/05/11 01:05:17 jasper Exp $ */ +/* $OpenBSD: vmd.c,v 1.110 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> @@ -101,7 +101,7 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) if (vmc.vmc_flags == 0) { /* start an existing VM with pre-configured options */ if (!(ret == -1 && errno == EALREADY && - vm->vm_running == 0)) { + !(vm->vm_state & VM_STATE_RUNNING))) { res = errno; cmd = IMSG_VMDOP_START_VM_RESPONSE; } @@ -128,12 +128,12 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) res = ENOENT; cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE; break; - } else if (vm->vm_shutdown && + } else if ((vm->vm_state & VM_STATE_SHUTDOWN) && (flags & VMOP_FORCE) == 0) { res = EALREADY; cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE; break; - } else if (vm->vm_running == 0) { + } else if (!(vm->vm_state & VM_STATE_RUNNING)) { res = EINVAL; cmd = IMSG_VMDOP_TERMINATE_VM_RESPONSE; break; @@ -233,7 +233,6 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) cmd = IMSG_VMDOP_SEND_VM_RESPONSE; close(imsg->fd); break; - } else { } vmr.vmr_id = vid.vid_id; log_debug("%s: sending fd to vmm", __func__); @@ -282,7 +281,7 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) cmd = IMSG_VMDOP_START_VM_RESPONSE; close(imsg->fd); } else { - vm->vm_received = 1; + vm->vm_state |= VM_STATE_RECEIVED; config_setvm(ps, vm, imsg->hdr.peerid, vmc.vmc_owner.uid); log_debug("%s: sending fd to vmm", __func__); @@ -345,7 +344,7 @@ vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg) log_info("%s: paused vm %d successfully", vm->vm_params.vmc_params.vcp_name, vm->vm_vmid); - vm->vm_paused = 1; + vm->vm_state |= VM_STATE_PAUSED; break; case IMSG_VMDOP_UNPAUSE_VM_RESPONSE: IMSG_SIZE_CHECK(imsg, &vmr); @@ -358,7 +357,7 @@ vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg) log_info("%s: unpaused vm %d successfully.", vm->vm_params.vmc_params.vcp_name, vm->vm_vmid); - vm->vm_paused = 0; + vm->vm_state &= ~VM_STATE_PAUSED; break; case IMSG_VMDOP_START_VM_RESPONSE: IMSG_SIZE_CHECK(imsg, &vmr); @@ -415,7 +414,7 @@ vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg) break; if (vmr.vmr_result == 0) { /* Mark VM as shutting down */ - vm->vm_shutdown = 1; + vm->vm_state |= VM_STATE_SHUTDOWN; } break; case IMSG_VMDOP_SEND_VM_RESPONSE: @@ -485,12 +484,14 @@ vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg) if (vm->vm_ttyname != NULL) strlcpy(vir.vir_ttyname, vm->vm_ttyname, sizeof(vir.vir_ttyname)); - if (vm->vm_shutdown) { + if (vm->vm_state & VM_STATE_SHUTDOWN) { /* XXX there might be a nicer way */ (void)strlcat(vir.vir_info.vir_name, " - stopping", - sizeof(vir.vir_info.vir_name)); + sizeof(vir.vir_info.vir_name)); } + log_debug("%s: vm: %d, vm_state: 0x%x", + __func__, vm->vm_vmid, vm->vm_state); /* get the user id who started the vm */ vir.vir_uid = vm->vm_uid; vir.vir_gid = vm->vm_params.vmc_owner.gid; @@ -510,7 +511,7 @@ vmd_dispatch_vmm(int fd, struct privsep_proc *p, struct imsg *imsg) * kernel id to indicate that they are not running. */ TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) { - if (!vm->vm_running) { + if (!(vm->vm_state & VM_STATE_RUNNING)) { memset(&vir, 0, sizeof(vir)); vir.vir_info.vir_id = vm->vm_vmid; strlcpy(vir.vir_info.vir_name, @@ -904,7 +905,7 @@ vmd_configure(void) } TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) { - if (vm->vm_disabled) { + if (vm->vm_state & VM_STATE_DISABLED) { log_debug("%s: not creating vm %s (disabled)", __func__, vm->vm_params.vmc_params.vcp_name); @@ -949,7 +950,7 @@ vmd_reload(unsigned int reset, const char *filename) if (reload) { TAILQ_FOREACH_SAFE(vm, env->vmd_vms, vm_entry, next_vm) { - if (vm->vm_running == 0) { + if (!(vm->vm_state & VM_STATE_RUNNING)) { DPRINTF("%s: calling vm_remove", __func__); vm_remove(vm, __func__); @@ -981,8 +982,8 @@ vmd_reload(unsigned int reset, const char *filename) } TAILQ_FOREACH(vm, env->vmd_vms, vm_entry) { - if (vm->vm_running == 0) { - if (vm->vm_disabled) { + if (!(vm->vm_state & VM_STATE_RUNNING)) { + if (vm->vm_state & VM_STATE_DISABLED) { log_debug("%s: not creating vm %s" " (disabled)", __func__, vm->vm_params.vmc_params.vcp_name); @@ -1111,8 +1112,7 @@ vm_stop(struct vmd_vm *vm, int keeptty, const char *caller) __func__, ps->ps_title[privsep_process], caller, vm->vm_vmid, keeptty ? ", keeping tty open" : ""); - vm->vm_running = 0; - vm->vm_shutdown = 0; + vm->vm_state &= ~(VM_STATE_RUNNING | VM_STATE_SHUTDOWN); user_inc(&vm->vm_params.vmc_params, vm->vm_user, 0); user_put(vm->vm_user); @@ -1292,7 +1292,7 @@ vm_register(struct privsep *ps, struct vmop_create_params *vmc, vm->vm_pid = -1; vm->vm_tty = -1; vm->vm_receive_fd = -1; - vm->vm_paused = 0; + vm->vm_state &= ~VM_STATE_PAUSED; vm->vm_user = usr; for (i = 0; i < VMM_MAX_DISKS_PER_VM; i++) @@ -1581,8 +1581,8 @@ vm_checkperm(struct vmd_vm *vm, struct vmop_owner *vmo, uid_t uid) * check user of running vm (the owner of a running vm can * be different to (or more specific than) the configured owner. */ - if ((vm->vm_running && vm->vm_uid == uid) || - (!vm->vm_running && vmo->uid == uid)) + if (((vm->vm_state & VM_STATE_RUNNING) && vm->vm_uid == uid) || + (!(vm->vm_state & VM_STATE_RUNNING) && vmo->uid == uid)) return (0); } diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h index 6358deb9122..d097791159c 100644 --- a/usr.sbin/vmd/vmd.h +++ b/usr.sbin/vmd/vmd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.h,v 1.91 2019/05/10 18:11:27 jasper Exp $ */ +/* $OpenBSD: vmd.h,v 1.92 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -263,19 +263,20 @@ struct vmd_vm { char *vm_ttyname; int vm_tty; uint32_t vm_peerid; - /* When set, VM is running now (PROC_PARENT only) */ - int vm_running; - /* When set, VM is not started by default (PROC_PARENT only) */ - int vm_disabled; /* When set, VM was defined in a config file */ int vm_from_config; struct imsgev vm_iev; - int vm_shutdown; uid_t vm_uid; - int vm_received; - int vm_paused; int vm_receive_fd; struct vmd_user *vm_user; + unsigned int vm_state; +/* When set, VM is running now (PROC_PARENT only) */ +#define VM_STATE_RUNNING 0x01 +/* When set, VM is not started by default (PROC_PARENT only) */ +#define VM_STATE_DISABLED 0x02 +#define VM_STATE_SHUTDOWN 0x04 +#define VM_STATE_RECEIVED 0x08 +#define VM_STATE_PAUSED 0x10 /* For rate-limiting */ struct timeval vm_start_tv; diff --git a/usr.sbin/vmd/vmm.c b/usr.sbin/vmd/vmm.c index a3b20e282e4..1cb70b6989a 100644 --- a/usr.sbin/vmd/vmm.c +++ b/usr.sbin/vmd/vmm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmm.c,v 1.91 2018/12/04 08:15:09 claudio Exp $ */ +/* $OpenBSD: vmm.c,v 1.92 2019/05/11 19:55:14 jasper Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -188,10 +188,10 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) } else if ((vm = vm_getbyvmid(id)) != NULL) { if (flags & VMOP_FORCE) { vtp.vtp_vm_id = vm_vmid2id(vm->vm_vmid, vm); - vm->vm_shutdown = 1; + vm->vm_state |= VM_STATE_SHUTDOWN; (void)terminate_vm(&vtp); res = 0; - } else if (vm->vm_shutdown == 0) { + } else if (!(vm->vm_state & VM_STATE_SHUTDOWN)) { log_debug("%s: sending shutdown request" " to vm %d", __func__, id); @@ -202,7 +202,7 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) * avoid being stuck in the ACPI-less powerdown * ("press any key to reboot") of the VM. */ - vm->vm_shutdown = 1; + vm->vm_state |= VM_STATE_SHUTDOWN; if (imsg_compose_event(&vm->vm_iev, IMSG_VMDOP_VM_REBOOT, 0, 0, -1, NULL, 0) == -1) @@ -222,7 +222,7 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) } } if ((flags & VMOP_WAIT) && - res == 0 && vm->vm_shutdown == 1) { + res == 0 && (vm->vm_state & VM_STATE_SHUTDOWN)) { if (vm->vm_peerid != (uint32_t)-1) { peerid = vm->vm_peerid; res = EINTR; @@ -315,7 +315,7 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) ret = vm_register(ps, &vmc, &vm, imsg->hdr.peerid, vmc.vmc_owner.uid); vm->vm_tty = imsg->fd; - vm->vm_received = 1; + vm->vm_state |= VM_STATE_RECEIVED; break; case IMSG_VMDOP_RECEIVE_VM_END: if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL) { @@ -404,7 +404,7 @@ vmm_sighdlr(int sig, short event, void *arg) ret = WEXITSTATUS(status); /* don't reboot on pending shutdown */ - if (ret == EAGAIN && vm->vm_shutdown) + if (ret == EAGAIN && (vm->vm_state & VM_STATE_SHUTDOWN)) ret = 0; vmid = vm->vm_params.vmc_params.vcp_id; @@ -529,10 +529,10 @@ vmm_dispatch_vm(int fd, short event, void *arg) switch (imsg.hdr.type) { case IMSG_VMDOP_VM_SHUTDOWN: - vm->vm_shutdown = 1; + vm->vm_state |= VM_STATE_SHUTDOWN; break; case IMSG_VMDOP_VM_REBOOT: - vm->vm_shutdown = 0; + vm->vm_state &= ~VM_STATE_SHUTDOWN; break; case IMSG_VMDOP_SEND_VM_RESPONSE: IMSG_SIZE_CHECK(&imsg, &vmr); @@ -645,7 +645,7 @@ vmm_start_vm(struct imsg *imsg, uint32_t *id, pid_t *pid) } vcp = &vm->vm_params.vmc_params; - if (!vm->vm_received) { + if (!(vm->vm_state & VM_STATE_RECEIVED)) { if ((vm->vm_tty = imsg->fd) == -1) { log_warnx("%s: can't get tty", __func__); goto err; |