]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemuBuildTPMCommandLine: Use 'qemuPassFD' infrastructure
authorPeter Krempa <pkrempa@redhat.com>
Wed, 9 Feb 2022 14:09:36 +0000 (15:09 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 14 Feb 2022 12:14:00 +0000 (13:14 +0100)
Remove the last code path using hardcoded fdsets.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_command.c
tests/qemuxml2argvdata/tpm-passthrough-crb.x86_64-latest.args
tests/qemuxml2argvdata/tpm-passthrough.x86_64-latest.args

index 2ea993b8a54af050a8da3d4109f625f62962d83d..1abff4fcaaa82fea39f8db9713b17bfffbc835c0 100644 (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)
-            return -1;
-    }
+    switch ((virDomainTPMBackendType) tpm->type) {
+    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: {
+        VIR_AUTOCLOSE fdtpm = -1;
+        VIR_AUTOCLOSE fdcancel = -1;
 
-    if (!(tpmdevstr = qemuBuildTPMBackendStr(cmd, tpm, &tpmfd, &cancelfd)))
-        return -1;
+        if (qemuBuildTPMOpenBackendFDs(tpm->data.passthrough.source->data.file.path,
+                                       &fdtpm, &fdcancel) < 0)
+            return -1;
 
-    virCommandAddArgList(cmd, "-tpmdev", tpmdevstr, NULL);
+        passtpm = qemuFDPassNew(tpm->info.alias, priv);
+        passcancel = qemuFDPassNew(tpm->info.alias, priv);
 
-    if (tpmfd >= 0) {
-        fdset = qemuVirCommandGetFDSet(cmd, tpmfd);
-        if (!fdset)
+        if (qemuFDPassAddFD(passtpm, &fdtpm, "-tpm") < 0 ||
+            qemuFDPassAddFD(passcancel, &fdcancel, "-cancel") < 0)
             return -1;
-
-        virCommandAddArgList(cmd, "-add-fd", fdset, NULL);
-        VIR_FREE(fdset);
     }
+        break;
 
-    if (cancelfd >= 0) {
-        fdset = qemuVirCommandGetFDSet(cmd, cancelfd);
-        if (!fdset)
+    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
+        if (qemuBuildChardevCommand(cmd, tpm->data.emulator.source, "chrtpm", priv->qemuCaps) < 0)
             return -1;
+        break;
 
-        virCommandAddArgList(cmd, "-add-fd", fdset, NULL);
-        VIR_FREE(fdset);
+    case VIR_DOMAIN_TPM_TYPE_LAST:
+        virReportEnumRangeError(virDomainTPMBackendType, tpm->type);
+        return -1;
     }
 
-    if (qemuBuildTPMDevCmd(cmd, def, tpm, qemuCaps) < 0)
+    qemuFDPassTransferCommand(passtpm, cmd);
+    qemuFDPassTransferCommand(passcancel, cmd);
+
+    if (!(tpmdevstr = qemuBuildTPMBackendStr(tpm, passtpm, passcancel)))
+        return -1;
+
+    virCommandAddArgList(cmd, "-tpmdev", tpmdevstr, NULL);
+
+    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)
index e099cf0fa1a9843e3631fb2020cdca67fe45ab0e..e9d785438ba84f87026372c0305f8b11f328568d 100644 (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"}' \
index beb6a307cbbe5a91afc8df22775469c1524562fd..4c6edf674953e37d431dc42a2a983661c1b3e338 100644 (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"}' \