This patch implements support for the ivshmem device in QEMU.
Signed-off-by: Maxime Leroy <maxime.leroy(a)6wind.com>
Signed-off-by: Martin Kletzander <mkletzan(a)redhat.com>
---
src/qemu/qemu_command.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++-
src/qemu/qemu_command.h | 1 +
tests/qemuxml2argvtest.c | 3 ++
3 files changed, 121 insertions(+), 1 deletion(-)
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 65864d2..2f26bcd 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1029,6 +1029,10 @@ qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr
qemuCaps)
if (virAsprintf(&def->hubs[i]->info.alias, "hub%zu", i) <
0)
return -1;
}
+ for (i = 0; i < def->nshmems; i++) {
+ if (virAsprintf(&def->shmems[i]->info.alias, "shmem%zu", i)
< 0)
+ return -1;
+ }
for (i = 0; i < def->nsmartcards; i++) {
if (virAsprintf(&def->smartcards[i]->info.alias,
"smartcard%zu", i) < 0)
return -1;
@@ -7452,6 +7456,114 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd,
}
static int
+qemuBuildShmemDevCmd(virCommandPtr cmd,
+ virDomainDefPtr def,
+ virDomainShmemDefPtr shmem,
+ virQEMUCapsPtr qemuCaps)
+{
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+ if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_IVSHMEM)) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("ivshmem device is not supported "
+ "with this QEMU binary"));
+ goto error;
+ }
+
+ virBufferAddLit(&buf, "ivshmem");
+ if (shmem->size) {
+ /*
+ * Thanks to our parsing code, we have a guarantee that the
+ * size is power of two and is at least a mebibyte in size.
+ * But because it may change inthe future, the checks are
+ * doubled in here.
+ */
+ if (shmem->size & (shmem->size - 1)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("shmem size must be a power of two"));
+ goto error;
+ }
+ if (shmem->size < 1024 * 1024) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("shmem size must be at least 1 MiB"));
+ goto error;
+ }
+ virBufferAsprintf(&buf, ",size=%llum",
+ VIR_DIV_UP(shmem->size, 1024 * 1024));
+ }
+
+ if (!shmem->server.enabled) {
+ virBufferAsprintf(&buf, ",shm=%s", shmem->name);
+ } else {
+ virBufferAsprintf(&buf, ",chardev=char%s", shmem->info.alias);
+ if (shmem->msi.enabled) {
+ virBufferAddLit(&buf, ",msi=on");
+ if (shmem->msi.vectors)
+ virBufferAsprintf(&buf, ",vectors=%u",
shmem->msi.vectors);
+ if (shmem->msi.ioeventfd)
+ virBufferAsprintf(&buf, ",ioeventfd=%s",
+
virTristateSwitchTypeToString(shmem->msi.ioeventfd));
+ }
+ }
+
+ if (qemuBuildDeviceAddressStr(&buf, def, &shmem->info, qemuCaps) < 0)
+ goto error;
+
+ if (virBufferCheckError(&buf) < 0)
+ goto error;
+
+ virCommandAddArg(cmd, "-device");
+ virCommandAddArgBuffer(cmd, &buf);
+
+ return 0;
+
+ error:
+ virBufferFreeAndReset(&buf);
+ return -1;
+}
+
+static int
+qemuBuildShmemCommandLine(virCommandPtr cmd,
+ virDomainDefPtr def,
+ virDomainShmemDefPtr shmem,
+ virQEMUCapsPtr qemuCaps)
+{
+ if (qemuBuildShmemDevCmd(cmd, def, shmem, qemuCaps) < 0)
+ return -1;
+
+ if (shmem->server.enabled) {
+ char *devstr = NULL;
+ virDomainChrSourceDef source = {
+ .type = VIR_DOMAIN_CHR_TYPE_UNIX,
+ .data.nix = {
+ .path = shmem->server.path,
+ .listen = false,
+ }
+ };
+
+ if (!shmem->server.path &&
+ virAsprintf(&source.data.nix.path,
+ "/var/lib/libvirt/shmem-%s-sock",
+ shmem->info.alias) < 0)
+ return -1;
+
+ devstr = qemuBuildChrChardevStr(&source, shmem->info.alias, qemuCaps);
+
+ if (!shmem->server.path)
+ VIR_FREE(source.data.nix.path);
+
+ if (!devstr)
+ return -1;
+
+ virCommandAddArg(cmd, "-chardev");
+ virCommandAddArg(cmd, devstr);
+ VIR_FREE(devstr);
+ }
+
+ return 0;
+}
+
+static int
qemuBuildChrDeviceCommandLine(virCommandPtr cmd,
virDomainDefPtr def,
virDomainChrDefPtr chr,
@@ -9646,6 +9758,11 @@ qemuBuildCommandLine(virConnectPtr conn,
}
}
+ for (i = 0; i < def->nshmems; i++) {
+ if (qemuBuildShmemCommandLine(cmd, def, def->shmems[i], qemuCaps))
+ goto error;
+ }
+
if (mlock) {
unsigned long long memKB;
@@ -9853,7 +9970,6 @@ qemuBuildChrDeviceStr(char **deviceStr,
return ret;
}
-
/*
* This method takes a string representing a QEMU command line ARGV set
* optionally prefixed by a list of environment variables. It then tries
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index aa40c9e..5083223 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -131,6 +131,7 @@ char *qemuBuildDriveDevStr(virDomainDefPtr def,
char *qemuBuildFSDevStr(virDomainDefPtr domainDef,
virDomainFSDefPtr fs,
virQEMUCapsPtr qemuCaps);
+
/* Current, best practice */
char *qemuBuildControllerDevStr(virDomainDefPtr domainDef,
virDomainControllerDefPtr def,
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 60c4405..e58717b 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -1462,6 +1462,9 @@ mymain(void)
DO_TEST("fips-enabled", QEMU_CAPS_ENABLE_FIPS);
+ DO_TEST("shmem", QEMU_CAPS_PCIDEVICE,
+ QEMU_CAPS_DEVICE, QEMU_CAPS_DEVICE_IVSHMEM);
+ DO_TEST_FAILURE("shmem", NONE);
DO_TEST_PARSE_ERROR("shmem-invalid", NONE);
DO_TEST_PARSE_ERROR("shmem-msi-only", NONE);
DO_TEST_PARSE_ERROR("shmem-invalid-size", NONE);
--
2.1.1