qemuBuildVsockCommandLine: Generate via JSON

Build the properties of 'vhost-vsock' device via JSON. In comparison to
previous similar refactors this also modifies the hotplug code to attach
the vhost fd handle explicitly rather than using
'qemuMonitorAddDeviceWithFd'.

The properties of vhost-vsock have the following types according to
QEMU:

  guest-cid=<uint64>     -  (default: 0)
  vhostfd=<str>

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-10-05 15:39:42 +02:00
parent 44b52c18e3
commit 3b4cc1d5a6
3 changed files with 44 additions and 31 deletions

View File

@ -10589,27 +10589,30 @@ qemuBuildSeccompSandboxCommandLine(virCommand *cmd,
}
char *
qemuBuildVsockDevStr(virDomainDef *def,
virDomainVsockDef *vsock,
virQEMUCaps *qemuCaps,
const char *fdprefix)
virJSONValue *
qemuBuildVsockDevProps(virDomainDef *def,
virDomainVsockDef *vsock,
virQEMUCaps *qemuCaps,
const char *fdprefix)
{
qemuDomainVsockPrivate *priv = (qemuDomainVsockPrivate *)vsock->privateData;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autoptr(virJSONValue) props = NULL;
g_autofree char *vhostfd = g_strdup_printf("%s%u", fdprefix, priv->vhostfd);
if (qemuBuildVirtioDevStr(&buf, qemuCaps, VIR_DOMAIN_DEVICE_VSOCK, vsock) < 0) {
return NULL;
}
virBufferAsprintf(&buf, ",id=%s", vsock->info.alias);
virBufferAsprintf(&buf, ",guest-cid=%u", vsock->guest_cid);
virBufferAsprintf(&buf, ",vhostfd=%s%u", fdprefix, priv->vhostfd);
if (qemuBuildDeviceAddressStr(&buf, def, &vsock->info) < 0)
if (!(props = qemuBuildVirtioDevProps(VIR_DOMAIN_DEVICE_VSOCK, vsock, qemuCaps)))
return NULL;
return virBufferContentAndReset(&buf);
if (virJSONValueObjectAdd(props,
"s:id", vsock->info.alias,
"u:guest-cid", vsock->guest_cid,
"s:vhostfd", vhostfd,
NULL) < 0)
return NULL;
if (qemuBuildDeviceAddressProps(props, def, &vsock->info) < 0)
return NULL;
return g_steal_pointer(&props);
}
@ -10620,9 +10623,9 @@ qemuBuildVsockCommandLine(virCommand *cmd,
virQEMUCaps *qemuCaps)
{
qemuDomainVsockPrivate *priv = (qemuDomainVsockPrivate *)vsock->privateData;
g_autofree char *devstr = NULL;
g_autoptr(virJSONValue) devprops = NULL;
if (!(devstr = qemuBuildVsockDevStr(def, vsock, qemuCaps, "")))
if (!(devprops = qemuBuildVsockDevProps(def, vsock, qemuCaps, "")))
return -1;
virCommandPassFD(cmd, priv->vhostfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
@ -10631,7 +10634,8 @@ qemuBuildVsockCommandLine(virCommand *cmd,
if (qemuCommandAddExtDevice(cmd, &vsock->info, qemuCaps) < 0)
return -1;
virCommandAddArgList(cmd, "-device", devstr, NULL);
if (qemuBuildDeviceCommandlineFromJSON(cmd, devprops, qemuCaps) < 0)
return -1;
return 0;
}

View File

@ -248,13 +248,11 @@ virJSONValue *
qemuBuildInputUSBDevProps(const virDomainDef *def,
virDomainInputDef *dev);
char *
qemuBuildVsockDevStr(virDomainDef *def,
virDomainVsockDef *vsock,
virQEMUCaps *qemuCaps,
const char *fdprefix)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
ATTRIBUTE_NONNULL(4);
virJSONValue *
qemuBuildVsockDevProps(virDomainDef *def,
virDomainVsockDef *vsock,
virQEMUCaps *qemuCaps,
const char *fdprefix);
/* this function is exported so that tests can mock the FDs */
int

View File

@ -3349,7 +3349,9 @@ qemuDomainAttachVsockDevice(virQEMUDriver *driver,
const char *fdprefix = "vsockfd";
bool releaseaddr = false;
g_autofree char *fdname = NULL;
g_autofree char *devstr = NULL;
g_autoptr(virJSONValue) devprops = NULL;
bool removeextension = false;
bool removehandle = false;
int ret = -1;
if (vm->def->vsock) {
@ -3369,7 +3371,7 @@ qemuDomainAttachVsockDevice(virQEMUDriver *driver,
fdname = g_strdup_printf("%s%u", fdprefix, vsockPriv->vhostfd);
if (!(devstr = qemuBuildVsockDevStr(vm->def, vsock, priv->qemuCaps, fdprefix)))
if (!(devprops = qemuBuildVsockDevProps(vm->def, vsock, priv->qemuCaps, fdprefix)))
goto cleanup;
qemuDomainObjEnterMonitor(driver, vm);
@ -3377,10 +3379,15 @@ qemuDomainAttachVsockDevice(virQEMUDriver *driver,
if (qemuDomainAttachExtensionDevice(priv->mon, &vsock->info) < 0)
goto exit_monitor;
if (qemuMonitorAddDeviceWithFd(priv->mon, devstr, vsockPriv->vhostfd, fdname) < 0) {
ignore_value(qemuDomainDetachExtensionDevice(priv->mon, &vsock->info));
removeextension = true;
if ((ret = qemuMonitorSendFileHandle(priv->mon, fdname, vsockPriv->vhostfd)) < 0)
goto exit_monitor;
removehandle = true;
if ((ret = qemuMonitorAddDeviceProps(priv->mon, &devprops)) < 0)
goto exit_monitor;
}
if (qemuDomainObjExitMonitor(driver, vm) < 0) {
releaseaddr = false;
@ -3402,6 +3409,10 @@ qemuDomainAttachVsockDevice(virQEMUDriver *driver,
return ret;
exit_monitor:
if (removehandle)
ignore_value(qemuMonitorCloseFileHandle(priv->mon, fdname));
if (removeextension)
ignore_value(qemuDomainDetachExtensionDevice(priv->mon, &vsock->info));
if (qemuDomainObjExitMonitor(driver, vm) < 0)
releaseaddr = false;
goto cleanup;