xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/auto_destruct.c (revision 5d9d9091f564c198a760790b0bfa72c44e17912b)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <libgen.h>
21 #include <errno.h>
22 #include <err.h>
23 #include <assert.h>
24 
25 #include <sys/vmm.h>
26 #include <sys/vmm_dev.h>
27 #include <sys/vmm_drv_test.h>
28 #include <vmmapi.h>
29 
30 #include "common.h"
31 
32 
33 int
34 main(int argc, char *argv[])
35 {
36 	const char *suite_name = basename(argv[0]);
37 	struct vmctx *ctx;
38 
39 	ctx = create_test_vm(suite_name);
40 	if (ctx == NULL) {
41 		errx(EXIT_FAILURE, "could open test VM");
42 	}
43 
44 	/*
45 	 * It would be odd if we had the freshly created VM instance, but it did
46 	 * not appear to exist.
47 	 */
48 	assert(check_instance_usable(suite_name));
49 
50 	/* Make sure that auto-destruct is off */
51 	if (ioctl(vm_get_device_fd(ctx), VM_SET_AUTODESTRUCT, 0) != 0) {
52 		errx(EXIT_FAILURE, "could not disable auto-destruct");
53 	}
54 
55 	vm_close(ctx);
56 	if (!check_instance_usable(suite_name)) {
57 		err(EXIT_FAILURE, "instance missing after close");
58 	}
59 	ctx = NULL;
60 
61 	if (destroy_instance(suite_name) != 0) {
62 		errx(EXIT_FAILURE, "could not clean up instance");
63 	}
64 
65 	/* Now repeat that process, but enable auto-destruct */
66 	ctx = create_test_vm(suite_name);
67 	if (ctx == NULL) {
68 		errx(EXIT_FAILURE, "could open test VM");
69 	}
70 	if (ioctl(vm_get_device_fd(ctx), VM_SET_AUTODESTRUCT, 1) != 0) {
71 		errx(EXIT_FAILURE, "could not enable auto-destruct");
72 	}
73 	vm_close(ctx);
74 	ctx = NULL;
75 	/* At this point, the instance should be gone */
76 	if (check_instance_usable(suite_name)) {
77 		err(EXIT_FAILURE,
78 		    "instance did not auto-destruct as expected");
79 	}
80 
81 	/*
82 	 * Repeat the test again, but establish a vmm_drv hold first.
83 	 * The instance should auto-destruct when the hold is released.
84 	 */
85 	ctx = create_test_vm(suite_name);
86 	if (ctx == NULL) {
87 		errx(EXIT_FAILURE, "could open test VM");
88 	}
89 	if (ioctl(vm_get_device_fd(ctx), VM_SET_AUTODESTRUCT, 1) != 0) {
90 		errx(EXIT_FAILURE, "could not enable auto-destruct");
91 	}
92 	int vdtfd = open_drv_test();
93 	if (vdtfd < 0) {
94 		errx(EXIT_FAILURE, "could open drv_test device");
95 	}
96 	if (ioctl(vdtfd, VDT_IOC_HOLD, vm_get_device_fd(ctx)) != 0) {
97 		errx(EXIT_FAILURE, "could not hold VM from vmm_drv device");
98 	}
99 	vm_close(ctx);
100 	ctx = NULL;
101 
102 	/*
103 	 * With the vmm_drv hold remaining on the instance, we expect it to
104 	 * exist, but not be usable (due to in-progress destroy).
105 	 */
106 	if (!check_instance_exists(suite_name)) {
107 		err(EXIT_FAILURE, "instance completed auto-destruct despite "
108 		    "existing vmm_drv hold");
109 	}
110 	if (check_instance_usable(suite_name)) {
111 		err(EXIT_FAILURE, "instance still usable despite close() after "
112 		    "auto-destroy configured");
113 	}
114 
115 	if (ioctl(vdtfd, VDT_IOC_RELE, 0) != 0) {
116 		errx(EXIT_FAILURE, "could not release VM from vmm_drv device");
117 	}
118 	if (check_instance_usable(suite_name)) {
119 		err(EXIT_FAILURE, "instance did not complete destruction "
120 		    "after vmm_drv release");
121 	}
122 
123 	(void) printf("%s\tPASS\n", suite_name);
124 	return (EXIT_SUCCESS);
125 }
126