qemuBuildTPMCommandLine: Use 'qemuPassFD' infrastructure

Remove the last code path using hardcoded fdsets.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-02-09 15:09:36 +01:00
parent 4225de7c73
commit f923620031
3 changed files with 46 additions and 108 deletions

View File

@ -301,56 +301,6 @@ qemuBuildMasterKeyCommandLine(virCommand *cmd,
}
/**
* qemuVirCommandGetFDSet:
* @cmd: the command to modify
* @fd: fd to reassign to the child
*
* Get the parameters for the QEMU -add-fd command line option
* for the given file descriptor. The file descriptor must previously
* have been 'transferred' in a virCommandPassFD() call.
* This function for example returns "set=10,fd=20".
*/
static char *
qemuVirCommandGetFDSet(virCommand *cmd, int fd)
{
int idx = virCommandPassFDGetFDIndex(cmd, fd);
if (idx < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("file descriptor %d has not been transferred"), fd);
return NULL;
}
return g_strdup_printf("set=%d,fd=%d", idx, fd);
}
/**
* qemuVirCommandGetDevSet:
* @cmd: the command to modify
* @fd: fd to reassign to the child
*
* Get the parameters for the QEMU path= parameter where a file
* descriptor is accessed via a file descriptor set, for example
* /dev/fdset/10. The file descriptor must previously have been
* 'transferred' in a virCommandPassFD() call.
*/
static char *
qemuVirCommandGetDevSet(virCommand *cmd, int fd)
{
int idx = virCommandPassFDGetFDIndex(cmd, fd);
if (idx < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("file descriptor %d has not been transferred"), fd);
return NULL;
}
return g_strdup_printf("/dev/fdset/%d", idx);
}
static char *
qemuBuildDeviceAddressPCIGetBus(const virDomainDef *domainDef,
const virDomainDeviceInfo *info)
@ -9764,40 +9714,22 @@ qemuBuildTPMOpenBackendFDs(const char *tpmdev,
static char *
qemuBuildTPMBackendStr(virCommand *cmd,
virDomainTPMDef *tpm,
int *tpmfd,
int *cancelfd)
qemuBuildTPMBackendStr(virDomainTPMDef *tpm,
qemuFDPass *passtpm,
qemuFDPass *passcancel)
{
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *devset = NULL;
g_autofree char *cancelset = NULL;
*tpmfd = -1;
*cancelfd = -1;
virBufferAsprintf(&buf, "%s", virDomainTPMBackendTypeToString(tpm->type));
virBufferAsprintf(&buf, ",id=tpm-%s", tpm->info.alias);
switch (tpm->type) {
case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
if (qemuBuildTPMOpenBackendFDs(tpm->data.passthrough.source->data.file.path,
tpmfd, cancelfd) < 0)
return NULL;
virCommandPassFD(cmd, *tpmfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
virCommandPassFD(cmd, *cancelfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
if (!(devset = qemuVirCommandGetDevSet(cmd, *tpmfd)) ||
!(cancelset = qemuVirCommandGetDevSet(cmd, *cancelfd)))
return NULL;
virBufferAddLit(&buf, ",path=");
virQEMUBuildBufferEscapeComma(&buf, devset);
virQEMUBuildBufferEscapeComma(&buf, qemuFDPassGetPath(passtpm));
virBufferAddLit(&buf, ",cancel-path=");
virQEMUBuildBufferEscapeComma(&buf, cancelset);
virQEMUBuildBufferEscapeComma(&buf, qemuFDPassGetPath(passcancel));
break;
case VIR_DOMAIN_TPM_TYPE_EMULATOR:
virBufferAddLit(&buf, ",chardev=chrtpm");
@ -9814,42 +9746,49 @@ static int
qemuBuildTPMCommandLine(virCommand *cmd,
const virDomainDef *def,
virDomainTPMDef *tpm,
virQEMUCaps *qemuCaps)
qemuDomainObjPrivate *priv)
{
g_autofree char *tpmdevstr = NULL;
int tpmfd = -1;
int cancelfd = -1;
char *fdset;
g_autoptr(qemuFDPass) passtpm = NULL;
g_autoptr(qemuFDPass) passcancel = NULL;
if (tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR) {
if (qemuBuildChardevCommand(cmd, tpm->data.emulator.source, "chrtpm", qemuCaps) < 0)
switch ((virDomainTPMBackendType) tpm->type) {
case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: {
VIR_AUTOCLOSE fdtpm = -1;
VIR_AUTOCLOSE fdcancel = -1;
if (qemuBuildTPMOpenBackendFDs(tpm->data.passthrough.source->data.file.path,
&fdtpm, &fdcancel) < 0)
return -1;
passtpm = qemuFDPassNew(tpm->info.alias, priv);
passcancel = qemuFDPassNew(tpm->info.alias, priv);
if (qemuFDPassAddFD(passtpm, &fdtpm, "-tpm") < 0 ||
qemuFDPassAddFD(passcancel, &fdcancel, "-cancel") < 0)
return -1;
}
break;
if (!(tpmdevstr = qemuBuildTPMBackendStr(cmd, tpm, &tpmfd, &cancelfd)))
case VIR_DOMAIN_TPM_TYPE_EMULATOR:
if (qemuBuildChardevCommand(cmd, tpm->data.emulator.source, "chrtpm", priv->qemuCaps) < 0)
return -1;
break;
case VIR_DOMAIN_TPM_TYPE_LAST:
virReportEnumRangeError(virDomainTPMBackendType, tpm->type);
return -1;
}
qemuFDPassTransferCommand(passtpm, cmd);
qemuFDPassTransferCommand(passcancel, cmd);
if (!(tpmdevstr = qemuBuildTPMBackendStr(tpm, passtpm, passcancel)))
return -1;
virCommandAddArgList(cmd, "-tpmdev", tpmdevstr, NULL);
if (tpmfd >= 0) {
fdset = qemuVirCommandGetFDSet(cmd, tpmfd);
if (!fdset)
return -1;
virCommandAddArgList(cmd, "-add-fd", fdset, NULL);
VIR_FREE(fdset);
}
if (cancelfd >= 0) {
fdset = qemuVirCommandGetFDSet(cmd, cancelfd);
if (!fdset)
return -1;
virCommandAddArgList(cmd, "-add-fd", fdset, NULL);
VIR_FREE(fdset);
}
if (qemuBuildTPMDevCmd(cmd, def, tpm, qemuCaps) < 0)
if (qemuBuildTPMDevCmd(cmd, def, tpm, priv->qemuCaps) < 0)
return -1;
return 0;
@ -9880,16 +9819,15 @@ qemuBuildTPMProxyCommandLine(virCommand *cmd,
static int
qemuBuildTPMsCommandLine(virCommand *cmd,
const virDomainDef *def,
virQEMUCaps *qemuCaps)
qemuDomainObjPrivate *priv)
{
size_t i;
for (i = 0; i < def->ntpms; i++) {
if (def->tpms[i]->model == VIR_DOMAIN_TPM_MODEL_SPAPR_PROXY) {
if (qemuBuildTPMProxyCommandLine(cmd, def->tpms[i], qemuCaps) < 0)
if (qemuBuildTPMProxyCommandLine(cmd, def->tpms[i], priv->qemuCaps) < 0)
return -1;
} else if (qemuBuildTPMCommandLine(cmd, def,
def->tpms[i], qemuCaps) < 0) {
} else if (qemuBuildTPMCommandLine(cmd, def, def->tpms[i], priv) < 0) {
return -1;
}
}
@ -10633,7 +10571,7 @@ qemuBuildCommandLine(virQEMUDriver *driver,
if (qemuBuildConsoleCommandLine(cmd, def, qemuCaps) < 0)
return NULL;
if (qemuBuildTPMsCommandLine(cmd, def, qemuCaps) < 0)
if (qemuBuildTPMsCommandLine(cmd, def, priv) < 0)
return NULL;
if (qemuBuildInputCommandLine(cmd, def, qemuCaps) < 0)

View File

@ -27,9 +27,9 @@ XDG_CONFIG_HOME=/tmp/lib/domain--1-TPM-VM/.config \
-no-shutdown \
-boot menu=on,strict=on \
-device '{"driver":"piix3-usb-uhci","id":"usb","bus":"pci.0","addr":"0x1.0x2"}' \
-add-fd set=0,fd=1730,opaque=tpm0-tpm \
-add-fd set=1,fd=1731,opaque=tpm0-cancel \
-tpmdev passthrough,id=tpm-tpm0,path=/dev/fdset/0,cancel-path=/dev/fdset/1 \
-add-fd set=0,fd=1730 \
-add-fd set=1,fd=1731 \
-device '{"driver":"tpm-crb","tpmdev":"tpm-tpm0","id":"tpm0"}' \
-audiodev '{"id":"audio1","driver":"none"}' \
-device '{"driver":"virtio-balloon-pci","id":"balloon0","bus":"pci.0","addr":"0x2"}' \

View File

@ -27,9 +27,9 @@ XDG_CONFIG_HOME=/tmp/lib/domain--1-TPM-VM/.config \
-no-shutdown \
-boot menu=on,strict=on \
-device '{"driver":"piix3-usb-uhci","id":"usb","bus":"pci.0","addr":"0x1.0x2"}' \
-add-fd set=0,fd=1730,opaque=tpm0-tpm \
-add-fd set=1,fd=1731,opaque=tpm0-cancel \
-tpmdev passthrough,id=tpm-tpm0,path=/dev/fdset/0,cancel-path=/dev/fdset/1 \
-add-fd set=0,fd=1730 \
-add-fd set=1,fd=1731 \
-device '{"driver":"tpm-tis","tpmdev":"tpm-tpm0","id":"tpm0"}' \
-audiodev '{"id":"audio1","driver":"none"}' \
-device '{"driver":"virtio-balloon-pci","id":"balloon0","bus":"pci.0","addr":"0x2"}' \