2021-05-12 17:01:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright Intel Corp. 2020-2021
|
|
|
|
*
|
|
|
|
* ch_monitor.c: Manage Cloud-Hypervisor interactions
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "ch_monitor.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "vircommand.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virjson.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CH
|
|
|
|
|
|
|
|
VIR_LOG_INIT("ch.ch_monitor");
|
|
|
|
|
|
|
|
static virClass *virCHMonitorClass;
|
|
|
|
static void virCHMonitorDispose(void *obj);
|
2022-01-25 16:19:54 +00:00
|
|
|
static void virCHMonitorThreadInfoFree(virCHMonitor *mon);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
static int virCHMonitorOnceInit(void)
|
|
|
|
{
|
|
|
|
if (!VIR_CLASS_NEW(virCHMonitor, virClassForObjectLockable()))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virCHMonitor);
|
|
|
|
|
|
|
|
int virCHMonitorShutdownVMM(virCHMonitor *mon);
|
|
|
|
int virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint);
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorBuildCPUJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
2021-09-22 20:45:51 +00:00
|
|
|
g_autoptr(virJSONValue) cpus = NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
unsigned int maxvcpus = 0;
|
|
|
|
unsigned int nvcpus = 0;
|
|
|
|
virDomainVcpuDef *vcpu;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* count maximum allowed number vcpus and enabled vcpus when boot.*/
|
|
|
|
maxvcpus = virDomainDefGetVcpusMax(vmdef);
|
|
|
|
for (i = 0; i < maxvcpus; i++) {
|
|
|
|
vcpu = virDomainDefGetVcpu(vmdef, i);
|
|
|
|
if (vcpu->online)
|
|
|
|
nvcpus++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxvcpus != 0 || nvcpus != 0) {
|
|
|
|
cpus = virJSONValueNewObject();
|
|
|
|
if (virJSONValueObjectAppendNumberInt(cpus, "boot_vcpus", nvcpus) < 0)
|
2021-09-22 20:45:51 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppendNumberInt(cpus, "max_vcpus", vmdef->maxvcpus) < 0)
|
2021-09-22 20:45:51 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppend(content, "cpus", &cpus) < 0)
|
2021-09-22 20:45:51 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:01:19 +00:00
|
|
|
static int
|
|
|
|
virCHMonitorBuildPTYJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
|
|
|
if (vmdef->nconsoles) {
|
|
|
|
g_autoptr(virJSONValue) pty = virJSONValueNewObject();
|
|
|
|
if (virJSONValueObjectAppendString(pty, "mode", "Pty") < 0)
|
|
|
|
return -1;
|
|
|
|
if (virJSONValueObjectAppend(content, "console", &pty) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmdef->nserials) {
|
|
|
|
g_autoptr(virJSONValue) pty = virJSONValueNewObject();
|
2021-11-23 13:51:18 +00:00
|
|
|
if (virJSONValueObjectAppendString(pty, "mode", "Pty") < 0)
|
2021-09-08 18:01:19 +00:00
|
|
|
return -1;
|
|
|
|
if (virJSONValueObjectAppend(content, "serial", &pty) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-10 21:42:57 +00:00
|
|
|
static int
|
|
|
|
virCHMonitorBuildPayloadJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
|
|
|
g_autoptr(virJSONValue) payload = virJSONValueNewObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (vmdef->os.kernel == NULL) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Kernel image path in this domain is not defined"));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virJSONValueObjectAppendString(payload, "kernel", vmdef->os.kernel) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmdef->os.cmdline) {
|
|
|
|
if (virJSONValueObjectAppendString(payload, "cmdline", vmdef->os.cmdline) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmdef->os.initrd != NULL) {
|
|
|
|
if (virJSONValueObjectAppendString(payload, "initramfs", vmdef->os.initrd) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectAppend(content, "payload", &payload) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
static int
|
|
|
|
virCHMonitorBuildKernelRelatedJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
2021-10-01 18:12:36 +00:00
|
|
|
g_autoptr(virJSONValue) kernel = virJSONValueNewObject();
|
|
|
|
g_autoptr(virJSONValue) cmdline = virJSONValueNewObject();
|
|
|
|
g_autoptr(virJSONValue) initramfs = virJSONValueNewObject();
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (vmdef->os.kernel == NULL) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Kernel image path in this domain is not defined"));
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
} else {
|
|
|
|
if (virJSONValueObjectAppendString(kernel, "path", vmdef->os.kernel) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppend(content, "kernel", &kernel) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vmdef->os.cmdline) {
|
|
|
|
if (virJSONValueObjectAppendString(cmdline, "args", vmdef->os.cmdline) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppend(content, "cmdline", &cmdline) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vmdef->os.initrd != NULL) {
|
|
|
|
if (virJSONValueObjectAppendString(initramfs, "path", vmdef->os.initrd) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppend(content, "initramfs", &initramfs) < 0)
|
2021-10-01 18:12:36 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorBuildMemoryJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
|
|
|
unsigned long long total_memory = virDomainDefGetMemoryInitial(vmdef) * 1024;
|
|
|
|
|
|
|
|
if (total_memory != 0) {
|
2021-10-01 18:12:35 +00:00
|
|
|
g_autoptr(virJSONValue) memory = virJSONValueNewObject();
|
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppendNumberUlong(memory, "size", total_memory) < 0)
|
2021-10-01 18:12:35 +00:00
|
|
|
return -1;
|
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virJSONValueObjectAppend(content, "memory", &memory) < 0)
|
2021-10-01 18:12:35 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorBuildDiskJson(virJSONValue *disks, virDomainDiskDef *diskdef)
|
|
|
|
{
|
2021-09-22 20:48:58 +00:00
|
|
|
g_autoptr(virJSONValue) disk = virJSONValueNewObject();
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (!diskdef->src)
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
switch (diskdef->src->type) {
|
|
|
|
case VIR_STORAGE_TYPE_FILE:
|
|
|
|
if (!diskdef->src->path) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
|
|
|
_("Missing disk file path in domain"));
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (diskdef->bus != VIR_DOMAIN_DISK_BUS_VIRTIO) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Only virtio bus types are supported for '%1$s'"),
|
|
|
|
diskdef->src->path);
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppendString(disk, "path", diskdef->src->path) < 0)
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
if (diskdef->src->readonly) {
|
|
|
|
if (virJSONValueObjectAppendBoolean(disk, "readonly", true) < 0)
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueArrayAppend(disks, &disk) < 0)
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case VIR_STORAGE_TYPE_NONE:
|
|
|
|
case VIR_STORAGE_TYPE_BLOCK:
|
|
|
|
case VIR_STORAGE_TYPE_DIR:
|
|
|
|
case VIR_STORAGE_TYPE_NETWORK:
|
|
|
|
case VIR_STORAGE_TYPE_VOLUME:
|
|
|
|
case VIR_STORAGE_TYPE_NVME:
|
|
|
|
case VIR_STORAGE_TYPE_VHOST_USER:
|
2023-02-06 20:20:01 +00:00
|
|
|
case VIR_STORAGE_TYPE_VHOST_VDPA:
|
2022-03-08 14:02:29 +00:00
|
|
|
case VIR_STORAGE_TYPE_LAST:
|
2021-05-12 17:01:31 +00:00
|
|
|
default:
|
|
|
|
virReportEnumRangeError(virStorageType, diskdef->src->type);
|
2021-09-22 20:48:58 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorBuildDisksJson(virJSONValue *content, virDomainDef *vmdef)
|
|
|
|
{
|
2021-09-22 20:49:27 +00:00
|
|
|
g_autoptr(virJSONValue) disks = NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (vmdef->ndisks > 0) {
|
|
|
|
disks = virJSONValueNewArray();
|
|
|
|
|
|
|
|
for (i = 0; i < vmdef->ndisks; i++) {
|
|
|
|
if (virCHMonitorBuildDiskJson(disks, vmdef->disks[i]) < 0)
|
2021-09-22 20:49:27 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppend(content, "disks", &disks) < 0)
|
2021-09-22 20:49:27 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-10 20:34:41 +00:00
|
|
|
virCHMonitorBuildNetJson(virJSONValue *nets,
|
|
|
|
virDomainNetDef *netdef,
|
|
|
|
size_t *nnicindexes,
|
|
|
|
int **nicindexes)
|
2021-05-12 17:01:31 +00:00
|
|
|
{
|
|
|
|
virDomainNetType netType = virDomainNetGetActualType(netdef);
|
|
|
|
char macaddr[VIR_MAC_STRING_BUFLEN];
|
2021-09-22 20:49:57 +00:00
|
|
|
g_autoptr(virJSONValue) net = NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
// check net type at first
|
|
|
|
net = virJSONValueNewObject();
|
|
|
|
|
|
|
|
switch (netType) {
|
2021-12-10 20:34:41 +00:00
|
|
|
case VIR_DOMAIN_NET_TYPE_ETHERNET:
|
|
|
|
if (netdef->guestIP.nips == 1) {
|
|
|
|
const virNetDevIPAddr *ip = netdef->guestIP.ips[0];
|
|
|
|
g_autofree char *addr = NULL;
|
|
|
|
virSocketAddr netmask;
|
|
|
|
g_autofree char *netmaskStr = NULL;
|
|
|
|
|
|
|
|
if (!(addr = virSocketAddrFormat(&ip->address)))
|
|
|
|
return -1;
|
|
|
|
if (virJSONValueObjectAppendString(net, "ip", addr) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virSocketAddrPrefixToNetmask(ip->prefix, &netmask, AF_INET) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Failed to translate net prefix %1$d to netmask"),
|
2021-12-10 20:34:41 +00:00
|
|
|
ip->prefix);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(netmaskStr = virSocketAddrFormat(&netmask)))
|
|
|
|
return -1;
|
|
|
|
if (virJSONValueObjectAppendString(net, "mask", netmaskStr) < 0)
|
|
|
|
return -1;
|
|
|
|
} else if (netdef->guestIP.nips > 1) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("ethernet type supports a single guest ip"));
|
|
|
|
}
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2021-12-10 20:34:41 +00:00
|
|
|
/* network and bridge use a tap device, and direct uses a
|
|
|
|
* macvtap device
|
|
|
|
*/
|
|
|
|
if (nicindexes && nnicindexes && netdef->ifname) {
|
|
|
|
int nicindex = 0;
|
|
|
|
|
|
|
|
if (virNetDevGetIndex(netdef->ifname, &nicindex) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_APPEND_ELEMENT(*nicindexes, *nnicindexes, nicindex);
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
2021-12-10 20:34:41 +00:00
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
|
|
|
|
if ((virDomainChrType)netdef->data.vhostuser->type != VIR_DOMAIN_CHR_TYPE_UNIX) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
2021-05-12 17:01:31 +00:00
|
|
|
_("vhost_user type support UNIX socket in this CH"));
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-12-10 20:34:41 +00:00
|
|
|
} else {
|
|
|
|
if (virJSONValueObjectAppendString(net, "vhost_socket", netdef->data.vhostuser->data.nix.path) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virJSONValueObjectAppendBoolean(net, "vhost_user", true) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_NET_TYPE_BRIDGE:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_NETWORK:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_DIRECT:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_USER:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_SERVER:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_CLIENT:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_MCAST:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_INTERNAL:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_HOSTDEV:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_UDP:
|
|
|
|
case VIR_DOMAIN_NET_TYPE_VDPA:
|
2022-08-24 20:46:05 +00:00
|
|
|
case VIR_DOMAIN_NET_TYPE_NULL:
|
2022-08-17 12:35:17 +00:00
|
|
|
case VIR_DOMAIN_NET_TYPE_VDS:
|
2021-12-10 20:34:41 +00:00
|
|
|
case VIR_DOMAIN_NET_TYPE_LAST:
|
|
|
|
default:
|
|
|
|
virReportEnumRangeError(virDomainNetType, netType);
|
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (netdef->ifname != NULL) {
|
|
|
|
if (virJSONValueObjectAppendString(net, "tap", netdef->ifname) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppendString(net, "mac", virMacAddrFormat(&netdef->mac, macaddr)) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (netdef->virtio != NULL) {
|
|
|
|
if (netdef->virtio->iommu == VIR_TRISTATE_SWITCH_ON) {
|
|
|
|
if (virJSONValueObjectAppendBoolean(net, "iommu", true) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (netdef->driver.virtio.queues) {
|
|
|
|
if (virJSONValueObjectAppendNumberInt(net, "num_queues", netdef->driver.virtio.queues) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (netdef->driver.virtio.rx_queue_size || netdef->driver.virtio.tx_queue_size) {
|
|
|
|
if (netdef->driver.virtio.rx_queue_size != netdef->driver.virtio.tx_queue_size) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("virtio rx_queue_size option %1$d is not same with tx_queue_size %2$d"),
|
2021-05-12 17:01:31 +00:00
|
|
|
netdef->driver.virtio.rx_queue_size,
|
|
|
|
netdef->driver.virtio.tx_queue_size);
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppendNumberInt(net, "queue_size", netdef->driver.virtio.rx_queue_size) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueArrayAppend(nets, &net) < 0)
|
2021-09-22 20:49:57 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-10 20:34:41 +00:00
|
|
|
virCHMonitorBuildNetsJson(virJSONValue *content,
|
|
|
|
virDomainDef *vmdef,
|
|
|
|
size_t *nnicindexes,
|
|
|
|
int **nicindexes)
|
2021-05-12 17:01:31 +00:00
|
|
|
{
|
2021-09-22 20:50:22 +00:00
|
|
|
g_autoptr(virJSONValue) nets = NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (vmdef->nnets > 0) {
|
|
|
|
nets = virJSONValueNewArray();
|
|
|
|
|
|
|
|
for (i = 0; i < vmdef->nnets; i++) {
|
2021-12-10 20:34:41 +00:00
|
|
|
if (virCHMonitorBuildNetJson(nets, vmdef->nets[i],
|
|
|
|
nnicindexes, nicindexes) < 0)
|
2021-09-22 20:50:22 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppend(content, "net", &nets) < 0)
|
2021-09-22 20:50:22 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-10 20:34:41 +00:00
|
|
|
virCHMonitorBuildDeviceJson(virJSONValue *devices,
|
|
|
|
virDomainHostdevDef *hostdevdef)
|
|
|
|
{
|
|
|
|
g_autoptr(virJSONValue) device = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (hostdevdef->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
|
|
|
|
hostdevdef->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
|
|
|
|
g_autofree char *name = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
|
|
|
virDomainHostdevSubsysPCI *pcisrc = &hostdevdef->source.subsys.u.pci;
|
|
|
|
|
|
|
|
device = virJSONValueNewObject();
|
|
|
|
name = virPCIDeviceAddressAsString(&pcisrc->addr);
|
|
|
|
path = g_strdup_printf("/sys/bus/pci/devices/%s/", name);
|
|
|
|
if (!virFileExists(path)) {
|
|
|
|
virReportError(VIR_ERR_DEVICE_MISSING,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("host pci device %1$s not found"), path);
|
2021-12-10 20:34:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppendString(device, "path", path) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virJSONValueArrayAppend(devices, &device) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorBuildDevicesJson(virJSONValue *content,
|
|
|
|
virDomainDef *vmdef)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
g_autoptr(virJSONValue) devices = NULL;
|
|
|
|
|
|
|
|
if (vmdef->nhostdevs == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
devices = virJSONValueNewArray();
|
|
|
|
for (i = 0; i < vmdef->nhostdevs; i++) {
|
|
|
|
if (virCHMonitorBuildDeviceJson(devices, vmdef->hostdevs[i]) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppend(content, "devices", &devices) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-10-10 21:42:57 +00:00
|
|
|
virCHMonitorBuildVMJson(virCHDriver *driver,
|
|
|
|
virDomainDef *vmdef,
|
2021-12-10 20:34:41 +00:00
|
|
|
char **jsonstr,
|
|
|
|
size_t *nnicindexes,
|
|
|
|
int **nicindexes)
|
2021-05-12 17:01:31 +00:00
|
|
|
{
|
2021-09-22 20:50:48 +00:00
|
|
|
g_autoptr(virJSONValue) content = virJSONValueNewObject();
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (vmdef == NULL) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("VM is not defined"));
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 18:01:19 +00:00
|
|
|
if (virCHMonitorBuildPTYJson(content, vmdef) < 0)
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-09-08 18:01:19 +00:00
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
if (virCHMonitorBuildCPUJson(content, vmdef) < 0)
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (virCHMonitorBuildMemoryJson(content, vmdef) < 0)
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2023-10-10 21:42:57 +00:00
|
|
|
if (virBitmapIsBitSet(driver->chCaps, CH_KERNEL_API_DEPRCATED)) {
|
|
|
|
if (virCHMonitorBuildPayloadJson(content, vmdef) < 0)
|
|
|
|
return -1;
|
|
|
|
} else if (virCHMonitorBuildKernelRelatedJson(content, vmdef) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (virCHMonitorBuildDisksJson(content, vmdef) < 0)
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2021-12-10 20:34:41 +00:00
|
|
|
|
|
|
|
if (virCHMonitorBuildNetsJson(content, vmdef, nnicindexes, nicindexes) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virCHMonitorBuildDevicesJson(content, vmdef) < 0)
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (!(*jsonstr = virJSONValueToString(content, false)))
|
2021-09-22 20:50:48 +00:00
|
|
|
return -1;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2021-09-22 20:50:48 +00:00
|
|
|
return 0;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chMonitorCreateSocket(const char *socket_path)
|
|
|
|
{
|
2023-08-02 08:09:52 +00:00
|
|
|
struct sockaddr_un addr = { 0 };
|
2021-05-12 17:01:31 +00:00
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to create UNIX socket"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
if (virStrcpyStatic(addr.sun_path, socket_path) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("UNIX socket path '%1$s' too long"),
|
2021-05-12 17:01:31 +00:00
|
|
|
socket_path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlink(socket_path) < 0 && errno != ENOENT) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Unable to unlink %1$s"),
|
2021-05-12 17:01:31 +00:00
|
|
|
socket_path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr *)&addr, addrlen) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Unable to bind to UNIX socket path '%1$s'"),
|
2021-05-12 17:01:31 +00:00
|
|
|
socket_path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 1) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Unable to listen to UNIX socket path '%1$s'"),
|
2021-05-12 17:01:31 +00:00
|
|
|
socket_path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We run cloud-hypervisor with umask 0002. Compensate for the umask
|
|
|
|
* libvirtd might be running under to get the same permission
|
|
|
|
* cloud-hypervisor would have. */
|
|
|
|
if (virFileUpdatePerm(socket_path, 0002, 0664) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
error:
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCHMonitor *
|
|
|
|
virCHMonitorNew(virDomainObj *vm, const char *socketdir)
|
|
|
|
{
|
2021-10-01 18:12:37 +00:00
|
|
|
g_autoptr(virCHMonitor) mon = NULL;
|
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
int socket_fd = 0;
|
|
|
|
|
|
|
|
if (virCHMonitorInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(mon = virObjectLockableNew(virCHMonitorClass)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!vm->def) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("VM is not defined"));
|
2021-10-01 18:12:37 +00:00
|
|
|
return NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare to launch Cloud-Hypervisor socket */
|
|
|
|
mon->socketpath = g_strdup_printf("%s/%s-socket", socketdir, vm->def->name);
|
|
|
|
if (g_mkdir_with_parents(socketdir, 0777) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Cannot create socket directory '%1$s'"),
|
2021-05-12 17:01:31 +00:00
|
|
|
socketdir);
|
2021-10-01 18:12:37 +00:00
|
|
|
return NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = virCommandNew(vm->def->emulator);
|
|
|
|
virCommandSetUmask(cmd, 0x002);
|
|
|
|
socket_fd = chMonitorCreateSocket(mon->socketpath);
|
|
|
|
if (socket_fd < 0) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("Cannot create socket '%1$s'"),
|
2021-05-12 17:01:31 +00:00
|
|
|
mon->socketpath);
|
2021-10-01 18:12:37 +00:00
|
|
|
return NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virCommandAddArg(cmd, "--api-socket");
|
|
|
|
virCommandAddArgFormat(cmd, "fd=%d", socket_fd);
|
|
|
|
virCommandPassFD(cmd, socket_fd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
|
|
|
|
|
|
|
|
/* launch Cloud-Hypervisor socket */
|
|
|
|
if (virCommandRunAsync(cmd, &mon->pid) < 0)
|
2021-10-01 18:12:37 +00:00
|
|
|
return NULL;
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
/* get a curl handle */
|
|
|
|
mon->handle = curl_easy_init();
|
|
|
|
|
|
|
|
/* now has its own reference */
|
|
|
|
mon->vm = virObjectRef(vm);
|
|
|
|
|
2021-10-01 18:12:37 +00:00
|
|
|
return g_steal_pointer(&mon);
|
2021-05-12 17:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virCHMonitorDispose(void *opaque)
|
|
|
|
{
|
|
|
|
virCHMonitor *mon = opaque;
|
|
|
|
|
|
|
|
VIR_DEBUG("mon=%p", mon);
|
2022-01-25 16:19:54 +00:00
|
|
|
virCHMonitorThreadInfoFree(mon);
|
2021-05-12 17:01:31 +00:00
|
|
|
virObjectUnref(mon->vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void virCHMonitorClose(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
if (!mon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mon->pid > 0) {
|
|
|
|
/* try cleaning up the Cloud-Hypervisor process */
|
|
|
|
virProcessAbort(mon->pid);
|
|
|
|
mon->pid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mon->handle)
|
|
|
|
curl_easy_cleanup(mon->handle);
|
|
|
|
|
|
|
|
if (mon->socketpath) {
|
|
|
|
if (virFileRemove(mon->socketpath, -1, -1) < 0) {
|
|
|
|
VIR_WARN("Unable to remove CH socket file '%s'",
|
|
|
|
mon->socketpath);
|
|
|
|
}
|
|
|
|
g_free(mon->socketpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
virObjectUnref(mon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCHMonitorCurlPerform(CURL *handle)
|
|
|
|
{
|
|
|
|
CURLcode errorCode;
|
|
|
|
long responseCode = 0;
|
|
|
|
|
|
|
|
errorCode = curl_easy_perform(handle);
|
|
|
|
|
|
|
|
if (errorCode != CURLE_OK) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("curl_easy_perform() returned an error: %1$s (%2$d)"),
|
2021-05-12 17:01:31 +00:00
|
|
|
curl_easy_strerror(errorCode), errorCode);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCode = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE,
|
|
|
|
&responseCode);
|
|
|
|
|
|
|
|
if (errorCode != CURLE_OK) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 10:17:57 +00:00
|
|
|
_("curl_easy_getinfo(CURLINFO_RESPONSE_CODE) returned an error: %1$s (%2$d)"),
|
|
|
|
curl_easy_strerror(errorCode),
|
2021-05-12 17:01:31 +00:00
|
|
|
errorCode);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseCode < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2023-08-24 15:03:58 +00:00
|
|
|
_("curl_easy_getinfo(CURLINFO_RESPONSE_CODE) returned a negative response code"));
|
2021-05-12 17:01:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
|
|
|
|
{
|
2022-03-25 09:20:08 +00:00
|
|
|
VIR_LOCK_GUARD lock = virObjectLockGuard(mon);
|
2021-05-12 17:01:31 +00:00
|
|
|
g_autofree char *url = NULL;
|
|
|
|
int responseCode = 0;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
|
|
|
|
|
|
|
|
/* reset all options of a libcurl session handle at first */
|
|
|
|
curl_easy_reset(mon->handle);
|
|
|
|
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_URL, url);
|
2023-01-18 09:45:52 +00:00
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_UPLOAD, 1L);
|
2021-05-12 17:01:31 +00:00
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, NULL);
|
|
|
|
|
|
|
|
responseCode = virCHMonitorCurlPerform(mon->handle);
|
|
|
|
|
|
|
|
if (responseCode == 200 || responseCode == 204)
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:01:17 +00:00
|
|
|
struct curl_data {
|
|
|
|
char *content;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
curl_callback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
|
|
{
|
|
|
|
size_t content_size = size * nmemb;
|
|
|
|
struct curl_data *data = userp;
|
|
|
|
|
|
|
|
if (content_size == 0)
|
|
|
|
return content_size;
|
|
|
|
|
|
|
|
data->content = g_realloc(data->content, data->size + content_size);
|
|
|
|
|
|
|
|
memcpy(&(data->content[data->size]), contents, content_size);
|
|
|
|
data->size += content_size;
|
|
|
|
|
|
|
|
return content_size;
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:01:16 +00:00
|
|
|
static int
|
2021-09-08 18:01:17 +00:00
|
|
|
virCHMonitorGet(virCHMonitor *mon, const char *endpoint, virJSONValue **response)
|
2021-05-12 17:01:31 +00:00
|
|
|
{
|
|
|
|
g_autofree char *url = NULL;
|
|
|
|
int responseCode = 0;
|
|
|
|
int ret = -1;
|
2021-09-08 18:01:17 +00:00
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
struct curl_data data = {0};
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
|
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
VIR_WITH_OBJECT_LOCK_GUARD(mon) {
|
|
|
|
/* reset all options of a libcurl session handle at first */
|
|
|
|
curl_easy_reset(mon->handle);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_URL, url);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
if (response) {
|
|
|
|
headers = curl_slist_append(headers, "Accept: application/json");
|
|
|
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
|
|
|
|
}
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
responseCode = virCHMonitorCurlPerform(mon->handle);
|
2021-09-08 18:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (responseCode == 200 || responseCode == 204) {
|
|
|
|
if (response) {
|
|
|
|
data.content = g_realloc(data.content, data.size + 1);
|
|
|
|
data.content[data.size] = 0;
|
|
|
|
*response = virJSONValueFromString(data.content);
|
|
|
|
if (!*response)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-05-12 17:01:31 +00:00
|
|
|
ret = 0;
|
2021-09-08 18:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
g_free(data.content);
|
|
|
|
/* reset the libcurl handle to avoid leaking a stack pointer to data */
|
|
|
|
curl_easy_reset(mon->handle);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-25 16:19:54 +00:00
|
|
|
static void
|
|
|
|
virCHMonitorThreadInfoFree(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
mon->nthreads = 0;
|
|
|
|
VIR_FREE(mon->threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
virCHMonitorRefreshThreadInfo(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
virCHMonitorThreadInfo *info = NULL;
|
|
|
|
g_autofree pid_t *tids = NULL;
|
|
|
|
virDomainObj *vm = mon->vm;
|
|
|
|
size_t ntids = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
virCHMonitorThreadInfoFree(mon);
|
|
|
|
if (virProcessGetPids(vm->pid, &ntids, &tids) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
info = g_new0(virCHMonitorThreadInfo, ntids);
|
|
|
|
for (i = 0; i < ntids; i++) {
|
|
|
|
g_autofree char *proc = NULL;
|
|
|
|
g_autofree char *data = NULL;
|
|
|
|
|
|
|
|
proc = g_strdup_printf("/proc/%d/task/%d/comm",
|
|
|
|
(int)vm->pid, (int)tids[i]);
|
|
|
|
|
|
|
|
if (virFileReadAll(proc, (1 << 16), &data) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("VM PID: %d, TID %d, COMM: %s",
|
|
|
|
(int)vm->pid, (int)tids[i], data);
|
|
|
|
if (STRPREFIX(data, "vcpu")) {
|
|
|
|
int cpuid;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (virStrToLong_i(data + 4, &tmp, 0, &cpuid) < 0) {
|
|
|
|
VIR_WARN("Index is not specified correctly");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
info[i].type = virCHThreadTypeVcpu;
|
|
|
|
info[i].vcpuInfo.tid = tids[i];
|
|
|
|
info[i].vcpuInfo.online = true;
|
|
|
|
info[i].vcpuInfo.cpuid = cpuid;
|
|
|
|
VIR_DEBUG("vcpu%d -> tid: %d", cpuid, tids[i]);
|
|
|
|
} else if (STRPREFIX(data, "_disk") || STRPREFIX(data, "_net") ||
|
|
|
|
STRPREFIX(data, "_rng")) {
|
|
|
|
/* Prefixes used by cloud-hypervisor for IO Threads are captured at
|
|
|
|
* https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/vmm/src/device_manager.rs */
|
|
|
|
info[i].type = virCHThreadTypeIO;
|
|
|
|
info[i].ioInfo.tid = tids[i];
|
|
|
|
virStrcpy(info[i].ioInfo.thrName, data, VIRCH_THREAD_NAME_LEN - 1);
|
|
|
|
} else {
|
|
|
|
info[i].type = virCHThreadTypeEmulator;
|
|
|
|
info[i].emuInfo.tid = tids[i];
|
|
|
|
virStrcpy(info[i].emuInfo.thrName, data, VIRCH_THREAD_NAME_LEN - 1);
|
|
|
|
}
|
|
|
|
mon->nthreads++;
|
|
|
|
|
|
|
|
}
|
|
|
|
mon->threads = info;
|
|
|
|
|
|
|
|
return mon->nthreads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virCHMonitorGetThreadInfo:
|
|
|
|
* @mon: Pointer to the monitor
|
|
|
|
* @refresh: Refresh thread info or not
|
|
|
|
*
|
|
|
|
* Retrieve thread info and store to @threads
|
|
|
|
*
|
|
|
|
* Returns count of threads on success.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
virCHMonitorGetThreadInfo(virCHMonitor *mon,
|
|
|
|
bool refresh,
|
|
|
|
virCHMonitorThreadInfo **threads)
|
|
|
|
{
|
|
|
|
int nthreads = 0;
|
|
|
|
|
|
|
|
if (refresh)
|
|
|
|
nthreads = virCHMonitorRefreshThreadInfo(mon);
|
|
|
|
|
|
|
|
*threads = mon->threads;
|
|
|
|
|
|
|
|
return nthreads;
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:01:31 +00:00
|
|
|
int
|
|
|
|
virCHMonitorShutdownVMM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VMM_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-10-10 21:42:57 +00:00
|
|
|
virCHMonitorCreateVM(virCHDriver *driver,
|
|
|
|
virCHMonitor *mon,
|
2021-12-10 20:34:41 +00:00
|
|
|
size_t *nnicindexes,
|
|
|
|
int **nicindexes)
|
2021-05-12 17:01:31 +00:00
|
|
|
{
|
|
|
|
g_autofree char *url = NULL;
|
|
|
|
int responseCode = 0;
|
|
|
|
int ret = -1;
|
|
|
|
g_autofree char *payload = NULL;
|
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
|
|
|
|
url = g_strdup_printf("%s/%s", URL_ROOT, URL_VM_CREATE);
|
|
|
|
headers = curl_slist_append(headers, "Accept: application/json");
|
|
|
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
|
|
|
2023-10-10 21:42:57 +00:00
|
|
|
if (virCHMonitorBuildVMJson(driver, mon->vm->def, &payload,
|
2021-12-10 20:34:41 +00:00
|
|
|
nnicindexes, nicindexes) != 0)
|
2021-05-12 17:01:31 +00:00
|
|
|
return -1;
|
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
VIR_WITH_OBJECT_LOCK_GUARD(mon) {
|
|
|
|
/* reset all options of a libcurl session handle at first */
|
|
|
|
curl_easy_reset(mon->handle);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
|
|
|
|
curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
|
2021-05-12 17:01:31 +00:00
|
|
|
|
2022-03-25 09:20:08 +00:00
|
|
|
responseCode = virCHMonitorCurlPerform(mon->handle);
|
|
|
|
}
|
2021-05-12 17:01:31 +00:00
|
|
|
|
|
|
|
if (responseCode == 200 || responseCode == 204)
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
curl_slist_free_all(headers);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorBootVM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VM_BOOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorShutdownVM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VM_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorRebootVM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VM_REBOOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorSuspendVM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VM_Suspend);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virCHMonitorResumeVM(virCHMonitor *mon)
|
|
|
|
{
|
|
|
|
return virCHMonitorPutNoContent(mon, URL_VM_RESUME);
|
|
|
|
}
|
2021-09-08 18:01:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virCHMonitorGetInfo:
|
|
|
|
* @mon: Pointer to the monitor
|
|
|
|
* @info: Get VM info
|
|
|
|
*
|
2021-09-10 10:48:47 +00:00
|
|
|
* Retrieve the VM info and store in @info
|
2021-09-08 18:01:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virCHMonitorGetInfo(virCHMonitor *mon, virJSONValue **info)
|
|
|
|
{
|
|
|
|
return virCHMonitorGet(mon, URL_VM_INFO, info);
|
|
|
|
}
|
2022-01-25 16:19:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virCHMonitorGetIOThreads:
|
|
|
|
* @mon: Pointer to the monitor
|
|
|
|
* @iothreads: Location to return array of IOThreadInfo data
|
|
|
|
*
|
|
|
|
* Retrieve the list of iothreads defined/running for the machine
|
|
|
|
*
|
|
|
|
* Returns count of IOThreadInfo structures on success
|
|
|
|
* -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virCHMonitorGetIOThreads(virCHMonitor *mon,
|
|
|
|
virDomainIOThreadInfo ***iothreads)
|
|
|
|
{
|
|
|
|
size_t nthreads = 0;
|
2023-09-05 10:17:19 +00:00
|
|
|
int niothreads = 0;
|
2022-01-25 16:19:58 +00:00
|
|
|
int thd_index;
|
|
|
|
virDomainIOThreadInfo **iothreadinfolist = NULL;
|
|
|
|
virDomainIOThreadInfo *iothreadinfo = NULL;
|
|
|
|
|
|
|
|
*iothreads = NULL;
|
|
|
|
nthreads = virCHMonitorRefreshThreadInfo(mon);
|
|
|
|
|
|
|
|
iothreadinfolist = g_new0(virDomainIOThreadInfo*, nthreads + 1);
|
|
|
|
|
|
|
|
for (thd_index = 0; thd_index < nthreads; thd_index++) {
|
|
|
|
g_autoptr(virBitmap) map = NULL;
|
|
|
|
|
|
|
|
if (mon->threads[thd_index].type == virCHThreadTypeIO) {
|
|
|
|
iothreadinfo = g_new0(virDomainIOThreadInfo, 1);
|
|
|
|
|
|
|
|
iothreadinfo->iothread_id = mon->threads[thd_index].ioInfo.tid;
|
|
|
|
|
|
|
|
if (!(map = virProcessGetAffinity(iothreadinfo->iothread_id)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virBitmapToData(map, &(iothreadinfo->cpumap),
|
|
|
|
&(iothreadinfo->cpumaplen)) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append to iothreadinfolist */
|
|
|
|
iothreadinfolist[niothreads] = g_steal_pointer(&iothreadinfo);
|
|
|
|
niothreads++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:17:19 +00:00
|
|
|
VIR_DEBUG("niothreads = %d", niothreads);
|
2022-02-02 16:22:50 +00:00
|
|
|
*iothreads = g_steal_pointer(&iothreadinfolist);
|
2022-01-25 16:19:58 +00:00
|
|
|
return niothreads;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (iothreadinfolist) {
|
|
|
|
for (thd_index = 0; thd_index < niothreads; thd_index++)
|
|
|
|
virDomainIOThreadInfoFree(iothreadinfolist[thd_index]);
|
|
|
|
VIR_FREE(iothreadinfolist);
|
|
|
|
}
|
|
|
|
virDomainIOThreadInfoFree(iothreadinfo);
|
|
|
|
return -1;
|
|
|
|
}
|