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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/*
* Copyright Intel Corp. 2020-2021
*
* ch_domain.c: Domain manager functions for Cloud-Hypervisor driver
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "ch_domain.h"
#include "domain_driver.h"
#include "virchrdev.h"
#include "virlog.h"
#include "virtime.h"
#include "virsystemd.h"
#include "datatypes.h"
#define VIR_FROM_THIS VIR_FROM_CH
VIR_LOG_INIT("ch.ch_domain");
void
virCHDomainRemoveInactive(virCHDriver *driver,
virDomainObj *vm)
{
if (vm->persistent) {
virDomainObjListRemove(driver->domains, vm);
}
}
static void *
virCHDomainObjPrivateAlloc(void *opaque)
{
virCHDomainObjPrivate *priv;
priv = g_new0(virCHDomainObjPrivate, 1);
if (!(priv->chrdevs = virChrdevAlloc())) {
g_free(priv);
return NULL;
}
priv->driver = opaque;
return priv;
}
static void
virCHDomainObjPrivateFree(void *data)
{
virCHDomainObjPrivate *priv = data;
virChrdevFree(priv->chrdevs);
g_free(priv->machineName);
g_free(priv);
}
static int
virCHDomainDefPostParseBasic(virDomainDef *def,
void *opaque G_GNUC_UNUSED)
{
/* check for emulator and create a default one if needed */
if (!def->emulator) {
if (!(def->emulator = g_find_program_in_path(CH_CMD))) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("No emulator found for cloud-hypervisor"));
return 1;
}
}
return 0;
}
static virClass *virCHDomainVcpuPrivateClass;
static void
virCHDomainVcpuPrivateDispose(void *obj G_GNUC_UNUSED)
{
}
static int
virCHDomainVcpuPrivateOnceInit(void)
{
if (!VIR_CLASS_NEW(virCHDomainVcpuPrivate, virClassForObject()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virCHDomainVcpuPrivate);
static virObject *
virCHDomainVcpuPrivateNew(void)
{
virCHDomainVcpuPrivate *priv;
if (virCHDomainVcpuPrivateInitialize() < 0)
return NULL;
if (!(priv = virObjectNew(virCHDomainVcpuPrivateClass)))
return NULL;
return (virObject *) priv;
}
static int
virCHDomainDefPostParse(virDomainDef *def,
unsigned int parseFlags G_GNUC_UNUSED,
void *opaque,
void *parseOpaque G_GNUC_UNUSED)
{
virCHDriver *driver = opaque;
g_autoptr(virCaps) caps = virCHDriverGetCapabilities(driver, false);
if (!caps)
return -1;
if (!virCapabilitiesDomainSupported(caps, def->os.type,
def->os.arch,
def->virtType))
return -1;
return 0;
}
virDomainXMLPrivateDataCallbacks virCHDriverPrivateDataCallbacks = {
.alloc = virCHDomainObjPrivateAlloc,
.free = virCHDomainObjPrivateFree,
.vcpuNew = virCHDomainVcpuPrivateNew,
};
static int
chValidateDomainDeviceDef(const virDomainDeviceDef *dev,
const virDomainDef *def G_GNUC_UNUSED,
void *opaque G_GNUC_UNUSED,
void *parseOpaque G_GNUC_UNUSED)
{
switch ((virDomainDeviceType)dev->type) {
case VIR_DOMAIN_DEVICE_DISK:
case VIR_DOMAIN_DEVICE_NET:
case VIR_DOMAIN_DEVICE_MEMORY:
case VIR_DOMAIN_DEVICE_VSOCK:
case VIR_DOMAIN_DEVICE_CONTROLLER:
case VIR_DOMAIN_DEVICE_CHR:
break;
case VIR_DOMAIN_DEVICE_LEASE:
case VIR_DOMAIN_DEVICE_FS:
case VIR_DOMAIN_DEVICE_INPUT:
case VIR_DOMAIN_DEVICE_SOUND:
case VIR_DOMAIN_DEVICE_VIDEO:
case VIR_DOMAIN_DEVICE_HOSTDEV:
case VIR_DOMAIN_DEVICE_WATCHDOG:
case VIR_DOMAIN_DEVICE_GRAPHICS:
case VIR_DOMAIN_DEVICE_HUB:
case VIR_DOMAIN_DEVICE_REDIRDEV:
case VIR_DOMAIN_DEVICE_SMARTCARD:
case VIR_DOMAIN_DEVICE_MEMBALLOON:
case VIR_DOMAIN_DEVICE_NVRAM:
case VIR_DOMAIN_DEVICE_RNG:
case VIR_DOMAIN_DEVICE_SHMEM:
case VIR_DOMAIN_DEVICE_TPM:
case VIR_DOMAIN_DEVICE_PANIC:
case VIR_DOMAIN_DEVICE_IOMMU:
case VIR_DOMAIN_DEVICE_AUDIO:
case VIR_DOMAIN_DEVICE_CRYPTO:
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Cloud-Hypervisor doesn't support '%1$s' device"),
virDomainDeviceTypeToString(dev->type));
return -1;
case VIR_DOMAIN_DEVICE_NONE:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("unexpected VIR_DOMAIN_DEVICE_NONE"));
return -1;
case VIR_DOMAIN_DEVICE_LAST:
default:
virReportEnumRangeError(virDomainDeviceType, dev->type);
return -1;
}
if ((def->nconsoles &&
def->consoles[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY)
&& (def->nserials &&
def->serials[0]->source->type == VIR_DOMAIN_CHR_TYPE_PTY)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Only a single console or serial can be configured for this domain"));
return -1;
} else if (def->nconsoles > 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Only a single console can be configured for this domain"));
return -1;
} else if (def->nserials > 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Only a single serial can be configured for this domain"));
return -1;
}
if (def->nconsoles && def->consoles[0]->source->type != VIR_DOMAIN_CHR_TYPE_PTY) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Console can only be enabled for a PTY"));
return -1;
}
if (def->nserials && def->serials[0]->source->type != VIR_DOMAIN_CHR_TYPE_PTY) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Serial can only be enabled for a PTY"));
return -1;
}
return 0;
}
int
virCHDomainRefreshThreadInfo(virDomainObj *vm)
{
size_t maxvcpus = virDomainDefGetVcpusMax(vm->def);
virCHMonitorThreadInfo *info = NULL;
size_t nthreads;
size_t ncpus = 0;
size_t i;
nthreads = virCHMonitorGetThreadInfo(virCHDomainGetMonitor(vm),
true, &info);
for (i = 0; i < nthreads; i++) {
virCHDomainVcpuPrivate *vcpupriv;
virDomainVcpuDef *vcpu;
virCHMonitorCPUInfo *vcpuInfo;
if (info[i].type != virCHThreadTypeVcpu)
continue;
/* TODO: hotplug support */
vcpuInfo = &info[i].vcpuInfo;
vcpu = virDomainDefGetVcpu(vm->def, vcpuInfo->cpuid);
vcpupriv = CH_DOMAIN_VCPU_PRIVATE(vcpu);
vcpupriv->tid = vcpuInfo->tid;
ncpus++;
}
/* TODO: Remove the warning when hotplug is implemented.*/
if (ncpus != maxvcpus)
VIR_WARN("Mismatch in the number of cpus, expected: %ld, actual: %ld",
maxvcpus, ncpus);
return 0;
}
virDomainDefParserConfig virCHDriverDomainDefParserConfig = {
.domainPostParseBasicCallback = virCHDomainDefPostParseBasic,
.domainPostParseCallback = virCHDomainDefPostParse,
.deviceValidateCallback = chValidateDomainDeviceDef,
.features = VIR_DOMAIN_DEF_FEATURE_NO_STUB_CONSOLE,
};
virCHMonitor *
virCHDomainGetMonitor(virDomainObj *vm)
{
return CH_DOMAIN_PRIVATE(vm)->monitor;
}
pid_t
virCHDomainGetVcpuPid(virDomainObj *vm,
unsigned int vcpuid)
{
virDomainVcpuDef *vcpu = virDomainDefGetVcpu(vm->def, vcpuid);
return CH_DOMAIN_VCPU_PRIVATE(vcpu)->tid;
}
bool
virCHDomainHasVcpuPids(virDomainObj *vm)
{
size_t i;
size_t maxvcpus = virDomainDefGetVcpusMax(vm->def);
virDomainVcpuDef *vcpu;
for (i = 0; i < maxvcpus; i++) {
vcpu = virDomainDefGetVcpu(vm->def, i);
if (CH_DOMAIN_VCPU_PRIVATE(vcpu)->tid > 0)
return true;
}
return false;
}
char *
virCHDomainGetMachineName(virDomainObj *vm)
{
virCHDomainObjPrivate *priv = CH_DOMAIN_PRIVATE(vm);
virCHDriver *driver = priv->driver;
char *ret = NULL;
if (vm->pid != 0) {
ret = virSystemdGetMachineNameByPID(vm->pid);
if (!ret)
virResetLastError();
}
if (!ret)
ret = virDomainDriverGenerateMachineName("ch",
NULL,
vm->def->id, vm->def->name,
driver->privileged);
return ret;
}
/**
* virCHDomainObjFromDomain:
* @domain: Domain pointer that has to be looked up
*
* This function looks up @domain and returns the appropriate virDomainObjPtr
* that has to be released by calling virDomainObjEndAPI().
*
* Returns the domain object with incremented reference counter which is locked
* on success, NULL otherwise.
*/
virDomainObj *
virCHDomainObjFromDomain(virDomainPtr domain)
{
virDomainObj *vm;
virCHDriver *driver = domain->conn->privateData;
char uuidstr[VIR_UUID_STRING_BUFLEN];
vm = virDomainObjListFindByUUID(driver->domains, domain->uuid);
if (!vm) {
virUUIDFormat(domain->uuid, uuidstr);
virReportError(VIR_ERR_NO_DOMAIN,
_("no domain with matching uuid '%1$s' (%2$s)"),
uuidstr, domain->name);
return NULL;
}
return vm;
}
|