summaryrefslogtreecommitdiff
path: root/usr/src/test/bhyve-tests/tests/vmm/common.c
blob: ca34dc8cb2bf78dcdfccf0a39557cd35a7fc5dc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 */

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <fcntl.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/vmm.h>
#include <sys/vmm_dev.h>
#include <vmmapi.h>


/*
 * Generate name for test VM based on the name of the test suite (and the pid).
 */
void
name_test_vm(const char *test_suite_name, char *outp)
{
	(void) snprintf(outp, VM_MAX_NAMELEN, "bhyve-test-%s-%d",
	    test_suite_name, getpid());
}

/*
 * Create a test VM. The name of the test suite will be used to derive the name
 * of the instance.
 */
struct vmctx *
create_test_vm(const char *test_suite_name)
{
	char name[VM_MAX_NAMELEN];
	int res;

	name_test_vm(test_suite_name, name);

	res = vm_create(name, 0);
	if (res != 0) {
		return (NULL);
	}

	return (vm_open(name));
}

/*
 * Given a segment ID, length, and name, allocate a memseg in the given VM.
 */
int
alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
{
	struct vm_memseg memseg = {
		.segid = segid,
		.len = len,
	};
	(void) strlcpy(memseg.name, name, sizeof (memseg.name));

	int fd = vm_get_device_fd(ctx);

	return (ioctl(fd, VM_ALLOC_MEMSEG, &memseg));
}

/*
 * Open the vmm_drv_test device.
 */
int
open_drv_test(void)
{
	return (open("/dev/vmm_drv_test", O_RDWR));
}


/*
 * Test if VMM instance exists (and is not being destroyed).
 */
bool
check_instance_usable(const char *suite_name)
{
	char vm_name[VM_MAX_NAMELEN];
	char vm_path[MAXPATHLEN];

	name_test_vm(suite_name, vm_name);
	(void) snprintf(vm_path, sizeof (vm_path), "/dev/vmm/%s", vm_name);

	int fd = open(vm_path, O_RDWR, 0);
	if (fd < 0) {
		return (false);
	}

	const int destroy_pending = ioctl(fd, VM_DESTROY_PENDING, 0);
	(void) close(fd);

	return (destroy_pending == 0);
}

/*
 * Does an instance exist in /dev/vmm?  (No check for in-progress destroy)
 */
bool
check_instance_exists(const char *suite_name)
{
	char vm_name[VM_MAX_NAMELEN];
	char vm_path[MAXPATHLEN];

	name_test_vm(suite_name, vm_name);
	(void) snprintf(vm_path, sizeof (vm_path), "/dev/vmm/%s", vm_name);

	return (access(vm_path, F_OK) == 0);
}


/*
 * Destroy a VMM instance via the vmmctl device.
 */
int
destroy_instance(const char *suite_name)
{
	int ctl_fd = open(VMM_CTL_DEV, O_EXCL | O_RDWR);
	if (ctl_fd < 0) {
		return (-1);
	}

	struct vm_destroy_req req;
	name_test_vm(suite_name, req.name);

	if (ioctl(ctl_fd, VMM_DESTROY_VM, &req) != 0) {
		/* Preserve the destroy error across the close() */
		int err = errno;
		(void) close(ctl_fd);
		errno = err;
		return (-1);
	} else {
		(void) close(ctl_fd);
		return (0);
	}
}