Skip to contentSkip to conversation

Move TPM passthrough specific command line options to backend structure

PATCH Superseded Stefan Berger 2 participants 3 messages Apr 2, 2013 – Apr 10, 2013

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Move TPM passthrough specific command line options to backend structure

Move the TPM passthrough specific command line options to the passthrough backend implementation and attach them to the backend's interface structure.

Add code to tpm.c for validating the TPM command line options.

---
 tpm/tpm.c             |   10 +++++++++-
 tpm/tpm_int.h         |    8 ++++++++
 tpm/tpm_passthrough.c |   16 ++++++++++++++++
 vl.c                  |   15 ---------------
 4 files changed, 33 insertions(+), 16 deletions(-)
vl.c +0 -15
Index: qemu-git.pt/vl.c
===================================================================
@@ -502,21 +502,6 @@ static QemuOptsList qemu_tpmdev_opts = {
     .implied_opt_name = "type",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
     .desc = {
-        {
-            .name = "type",
-            .type = QEMU_OPT_STRING,
-            .help = "Type of TPM backend",
-        },
-        {
-            .name = "cancel-path",
-            .type = QEMU_OPT_STRING,
-            .help = "Sysfs file entry for canceling TPM commands",
-        },
-        {
-            .name = "path",
-            .type = QEMU_OPT_STRING,
-            .help = "Path to TPM device on the host",
-        },
         { /* end of list */ }
     },
 };
tpm/tpm.c +9 -1
Index: qemu-git.pt/tpm/tpm.c
===================================================================
@@ -146,7 +146,7 @@ static int configure_tpm(QemuOpts *opts)
     const char *id;
     const TPMDriverOps *be;
     TPMBackend *drv;
-    Error *local_err = NULL;
+    Error *local_err = NULL, *errp = NULL;
 
     if (!QLIST_EMPTY(&tpm_backends)) {
         error_report("Only one TPM is allowed.\n");
@@ -174,6 +174,14 @@ static int configure_tpm(QemuOpts *opts)
         return 1;
     }
 
+    /* validate backend specific opts */
+    qemu_opts_validate(opts, be->opts, &errp);
+    if (error_is_set(&errp)) {
+        qerror_report_err(errp);
+        error_free(errp);
+        return 1;
+    }
+
     drv = be->create(opts, id);
     if (!drv) {
         return 1;
tpm/tpm_passthrough.c +16 -0
Index: qemu-git.pt/tpm/tpm_passthrough.c
===================================================================
@@ -512,8 +512,24 @@ static void tpm_passthrough_destroy(TPMB
     g_free(tpm_pt->tpm_dev);
 }
 
+static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
+    TPM_STANDARD_CMDLINE_OPTS,
+    {
+        .name = "cancel-path",
+        .type = QEMU_OPT_STRING,
+        .help = "Sysfs file entry for canceling TPM commands",
+    },
+    {
+        .name = "path",
+        .type = QEMU_OPT_STRING,
+        .help = "Path to TPM device on the host",
+    },
+    { /* end of list */ },
+};
+
 const TPMDriverOps tpm_passthrough_driver = {
     .type                     = TPM_TYPE_PASSTHROUGH,
+    .opts                     = tpm_passthrough_cmdline_opts,
     .desc                     = tpm_passthrough_create_desc,
     .create                   = tpm_passthrough_create,
     .destroy                  = tpm_passthrough_destroy,
tpm/tpm_int.h +8 -0
Index: qemu-git.pt/tpm/tpm_int.h
===================================================================
@@ -40,6 +40,7 @@ typedef void (TPMRecvDataCB)(TPMState *,
 
 struct TPMDriverOps {
     enum TpmType type;
+    const QemuOptDesc *opts;
     /* get a descriptive text of the backend to display to the user */
     const char *(*desc)(void);
 
@@ -64,6 +65,13 @@ struct TPMDriverOps {
     bool (*get_tpm_established_flag)(TPMBackend *t);
 };
 
+#define TPM_STANDARD_CMDLINE_OPTS \
+    { \
+        .name = "type", \
+        .type = QEMU_OPT_STRING, \
+        .help = "Type of TPM backend", \
+    }
+
 struct tpm_req_hdr {
     uint16_t tag;
     uint32_t len;
#1 lore raw
Quoted 28 lines · Stefan Berger

Move the TPM passthrough specific command line options to the passthrough backend implementation and attach them to the backend's interface structure.

Add code to tpm.c for validating the TPM command line options.

---
  tpm/tpm.c             |   10 +++++++++-
  tpm/tpm_int.h         |    8 ++++++++
  tpm/tpm_passthrough.c |   16 ++++++++++++++++
  vl.c                  |   15 ---------------
  4 files changed, 33 insertions(+), 16 deletions(-)

Index: qemu-git.pt/vl.c
===================================================================

vl.c +0 -6
@@ -502,21 +502,6 @@ static QemuOptsList qemu_tpmdev_opts = {
      .implied_opt_name = "type",
      .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
      .desc = {
-        {
-            .name = "type",
-            .type = QEMU_OPT_STRING,
-            .help = "Type of TPM backend",
-        },
-        {

vl.c static QemuOptsList qemu_tpmdev_opts = { · line 505

Quoted 10 lines
vl.c +0 -9
-            .name = "cancel-path",
-            .type = QEMU_OPT_STRING,
-            .help = "Sysfs file entry for canceling TPM commands",
-        },
-        {
-            .name = "path",
-            .type = QEMU_OPT_STRING,
-            .help = "Path to TPM device on the host",
-        },
          { /* end of list */ }

Does this need a comment to say where these options are now defined?

tpm/tpm.c static int configure_tpm(QemuOpts *opts) · line 149

Quoted 12 lines
vl.c +0 -0
      },
  };

Index: qemu-git.pt/tpm/tpm.c
===================================================================

tpm/tpm.c +1 -1
@@ -146,7 +146,7 @@ static int configure_tpm(QemuOpts *opts)
      const char *id;
      const TPMDriverOps *be;
      TPMBackend *drv;
-    Error *local_err = NULL;
+    Error *local_err = NULL, *errp = NULL;

Can't you use local_err in the new code below instead of creating a new variable?

Quoted 5 lines
tpm/tpm.c +0 -0
      if (!QLIST_EMPTY(&tpm_backends)) {
          error_report("Only one TPM is allowed.\n");
@@ -174,6 +174,14 @@ static int configure_tpm(QemuOpts *opts)
          return 1;

tpm/tpm.c static int configure_tpm(QemuOpts *opts) · line 184

Quoted 10 lines
tpm/tpm.c +8 -0
      }
 
+    /* validate backend specific opts */
+    qemu_opts_validate(opts, be->opts, &errp);
+    if (error_is_set(&errp)) {
+        qerror_report_err(errp);
+        error_free(errp);
+        return 1;
+    }
+

This looks fine to me but I see this is the first call to qemu_opts_validate() in QEMU. I wonder why.

Quoted 60 lines
tpm/tpm.c +0 -0
      drv = be->create(opts, id);
      if (!drv) {
          return 1;

Index: qemu-git.pt/tpm/tpm_passthrough.c
===================================================================

tpm/tpm_passthrough.c +16 -0
@@ -512,8 +512,24 @@ static void tpm_passthrough_destroy(TPMB
      g_free(tpm_pt->tpm_dev);
  }
 
+static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
+    TPM_STANDARD_CMDLINE_OPTS,
+    {
+        .name = "cancel-path",
+        .type = QEMU_OPT_STRING,
+        .help = "Sysfs file entry for canceling TPM commands",
+    },
+    {
+        .name = "path",
+        .type = QEMU_OPT_STRING,
+        .help = "Path to TPM device on the host",
+    },
+    { /* end of list */ },
+};
+
  const TPMDriverOps tpm_passthrough_driver = {
      .type                     = TPM_TYPE_PASSTHROUGH,
+    .opts                     = tpm_passthrough_cmdline_opts,
      .desc                     = tpm_passthrough_create_desc,
      .create                   = tpm_passthrough_create,
      .destroy                  = tpm_passthrough_destroy,

Index: qemu-git.pt/tpm/tpm_int.h
===================================================================

tpm/tpm_int.h +8 -0
@@ -40,6 +40,7 @@ typedef void (TPMRecvDataCB)(TPMState *,
 
  struct TPMDriverOps {
      enum TpmType type;
+    const QemuOptDesc *opts;
      /* get a descriptive text of the backend to display to the user */
      const char *(*desc)(void);
 
@@ -64,6 +65,13 @@ struct TPMDriverOps {
      bool (*get_tpm_established_flag)(TPMBackend *t);
  };
 
+#define TPM_STANDARD_CMDLINE_OPTS \
+    { \
+        .name = "type", \
+        .type = QEMU_OPT_STRING, \
+        .help = "Type of TPM backend", \
+    }
+
  struct tpm_req_hdr {
      uint16_t tag;
      uint32_t len;
Signature · 2 lines
Regards,
Corey Bryant
#2 lore raw
Quoted 15 lines · Corey Bryant

tpm/tpm.c static int configure_tpm(QemuOpts *opts) · line 184

Quoted 11 lines · Stefan Berger
          return 1;
      }
tpm/tpm.c +8 -0
+    /* validate backend specific opts */
+    qemu_opts_validate(opts, be->opts, &errp);
+    if (error_is_set(&errp)) {
+        qerror_report_err(errp);
+        error_free(errp);
+        return 1;
+    }
+
Quoted 1 line

This looks fine to me but I see this is the first call to qemu_opts_validate() in QEMU. I wonder why.

Well, the function does what is needed here, sorting out command line parameters that are unknown. It also fits the available parameters.

I fixed the code related to the other comments. Thanks for the review.

Stefan
I