In qemu_extdevice.c lives code that handles helper daemons that
are required for some types of devices (e.g. virtiofsd,
vhost-user-gpu, swtpm, etc.). These devices have their own
handling code in separate files, with only a very basic functions
exposed (e.g. for starting/stopping helper process, placing it
into given CGroup, etc.). And these functions all work over a
single instance of device (virDomainVideoDef *, virDomainFSDef *,
etc.), except for TPM handling code which takes virDomainDef *
and iterates over it inside its module.
Remove this oddness and make qemuExtTPM*() functions look closer
to the rest of the code.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/qemu/qemu_extdevice.c | 51 ++++++++++++++++------
src/qemu/qemu_tpm.c | 89 +++++++++++----------------------------
src/qemu/qemu_tpm.h | 11 +++--
3 files changed, 69 insertions(+), 82 deletions(-)
diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index 537b130394..234815c075 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -73,8 +73,15 @@ static int
qemuExtDevicesInitPaths(virQEMUDriver *driver,
virDomainDef *def)
{
- if (def->ntpms > 0)
- return qemuExtTPMInitPaths(driver, def);
+ size_t i;
+
+ for (i = 0; i < def->ntpms; i++) {
+ virDomainTPMDef *tpm = def->tpms[i];
+
+ if (tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR &&
+ qemuExtTPMInitPaths(driver, def, tpm) < 0)
+ return -1;
+ }
return 0;
}
@@ -135,9 +142,13 @@ qemuExtDevicesPrepareHost(virQEMUDriver *driver,
if (qemuExtDevicesInitPaths(driver, def) < 0)
return -1;
- if (def->ntpms > 0 &&
- qemuExtTPMPrepareHost(driver, def) < 0)
- return -1;
+ for (i = 0; i < def->ntpms; i++) {
+ virDomainTPMDef *tpm = def->tpms[i];
+
+ if (tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR &&
+ qemuExtTPMPrepareHost(driver, def, tpm) < 0)
+ return -1;
+ }
for (i = 0; i < def->nnets; i++) {
virDomainNetDef *net = def->nets[i];
@@ -155,11 +166,14 @@ void
qemuExtDevicesCleanupHost(virQEMUDriver *driver,
virDomainDef *def)
{
+ size_t i;
+
if (qemuExtDevicesInitPaths(driver, def) < 0)
return;
- if (def->ntpms > 0)
- qemuExtTPMCleanupHost(def);
+ for (i = 0; i < def->ntpms; i++) {
+ qemuExtTPMCleanupHost(def->tpms[i]);
+ }
}
@@ -180,8 +194,13 @@ qemuExtDevicesStart(virQEMUDriver *driver,
}
}
- if (def->ntpms > 0 && qemuExtTPMStart(driver, vm, incomingMigration)
< 0)
- return -1;
+ for (i = 0; i < def->ntpms; i++) {
+ virDomainTPMDef *tpm = def->tpms[i];
+
+ if (tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR &&
+ qemuExtTPMStart(driver, vm, tpm, incomingMigration) < 0)
+ return -1;
+ }
for (i = 0; i < def->nnets; i++) {
virDomainNetDef *net = def->nets[i];
@@ -222,8 +241,10 @@ qemuExtDevicesStop(virQEMUDriver *driver,
qemuExtVhostUserGPUStop(driver, vm, video);
}
- if (def->ntpms > 0)
- qemuExtTPMStop(driver, vm);
+ for (i = 0; i < def->ntpms; i++) {
+ if (def->tpms[i]->type == VIR_DOMAIN_TPM_TYPE_EMULATOR)
+ qemuExtTPMStop(driver, vm);
+ }
for (i = 0; i < def->nnets; i++) {
virDomainNetDef *net = def->nets[i];
@@ -299,9 +320,11 @@ qemuExtDevicesSetupCgroup(virQEMUDriver *driver,
return -1;
}
- if (def->ntpms > 0 &&
- qemuExtTPMSetupCgroup(driver, def, cgroup) < 0)
- return -1;
+ for (i = 0; i < def->ntpms; i++) {
+ if (def->tpms[i]->type == VIR_DOMAIN_TPM_TYPE_EMULATOR &&
+ qemuExtTPMSetupCgroup(driver, def, cgroup) < 0)
+ return -1;
+ }
for (i = 0; i < def->nfss; i++) {
virDomainFSDef *fs = def->fss[i];
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 56bccee128..086780edcd 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -971,86 +971,59 @@ qemuTPMEmulatorStart(virQEMUDriver *driver,
int
qemuExtTPMInitPaths(virQEMUDriver *driver,
- virDomainDef *def)
+ virDomainDef *def,
+ virDomainTPMDef *tpm)
{
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
- size_t i;
- for (i = 0; i < def->ntpms; i++) {
- if (def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- return qemuTPMEmulatorInitPaths(def->tpms[i],
- cfg->swtpmStorageDir,
- cfg->swtpmLogDir,
- def->name,
- def->uuid);
- }
-
- return 0;
+ return qemuTPMEmulatorInitPaths(tpm,
+ cfg->swtpmStorageDir,
+ cfg->swtpmLogDir,
+ def->name,
+ def->uuid);
}
int
qemuExtTPMPrepareHost(virQEMUDriver *driver,
- virDomainDef *def)
+ virDomainDef *def,
+ virDomainTPMDef *tpm)
{
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
g_autofree char *shortName = virDomainDefGetShortName(def);
- size_t i;
if (!shortName)
return -1;
- for (i = 0; i < def->ntpms; i++) {
- if (def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- return qemuTPMEmulatorPrepareHost(def->tpms[i], cfg->swtpmLogDir,
- cfg->swtpm_user,
- cfg->swtpm_group,
- cfg->swtpmStateDir, cfg->user,
- shortName);
- }
-
- return 0;
+ return qemuTPMEmulatorPrepareHost(tpm,
+ cfg->swtpmLogDir,
+ cfg->swtpm_user,
+ cfg->swtpm_group,
+ cfg->swtpmStateDir,
+ cfg->user,
+ shortName);
}
void
-qemuExtTPMCleanupHost(virDomainDef *def)
+qemuExtTPMCleanupHost(virDomainTPMDef *tpm)
{
- size_t i;
-
- for (i = 0; i < def->ntpms; i++) {
- if (def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- qemuTPMEmulatorCleanupHost(def->tpms[i]);
- }
+ qemuTPMEmulatorCleanupHost(tpm);
}
int
qemuExtTPMStart(virQEMUDriver *driver,
virDomainObj *vm,
+ virDomainTPMDef *tpm,
bool incomingMigration)
{
g_autofree char *shortName = virDomainDefGetShortName(vm->def);
- size_t i;
if (!shortName)
return -1;
- for (i = 0; i < vm->def->ntpms; i++) {
- if (vm->def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- return qemuTPMEmulatorStart(driver, vm, shortName, vm->def->tpms[i],
- incomingMigration);
- }
-
- return 0;
+ return qemuTPMEmulatorStart(driver, vm, shortName, tpm, incomingMigration);
}
@@ -1060,20 +1033,12 @@ qemuExtTPMStop(virQEMUDriver *driver,
{
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
g_autofree char *shortName = virDomainDefGetShortName(vm->def);
- size_t i;
if (!shortName)
return;
- for (i = 0; i < vm->def->ntpms; i++) {
- if (vm->def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- qemuTPMEmulatorStop(cfg->swtpmStateDir, shortName);
- qemuSecurityCleanupTPMEmulator(driver, vm);
- }
-
- return;
+ qemuTPMEmulatorStop(cfg->swtpmStateDir, shortName);
+ qemuSecurityCleanupTPMEmulator(driver, vm);
}
@@ -1084,18 +1049,12 @@ qemuExtTPMSetupCgroup(virQEMUDriver *driver,
{
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
g_autofree char *shortName = virDomainDefGetShortName(def);
- size_t i;
if (!shortName)
return -1;
- for (i = 0; i < def->ntpms; i++) {
- if (def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
- continue;
-
- if (qemuExtTPMEmulatorSetupCgroup(cfg->swtpmStateDir, shortName, cgroup) <
0)
- return -1;
- }
+ if (qemuExtTPMEmulatorSetupCgroup(cfg->swtpmStateDir, shortName, cgroup) < 0)
+ return -1;
return 0;
}
diff --git a/src/qemu/qemu_tpm.h b/src/qemu/qemu_tpm.h
index da8ce4c369..9951f025a6 100644
--- a/src/qemu/qemu_tpm.h
+++ b/src/qemu/qemu_tpm.h
@@ -23,22 +23,27 @@
#include "vircommand.h"
int qemuExtTPMInitPaths(virQEMUDriver *driver,
- virDomainDef *def)
+ virDomainDef *def,
+ virDomainTPMDef *tpm)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
G_GNUC_WARN_UNUSED_RESULT;
int qemuExtTPMPrepareHost(virQEMUDriver *driver,
- virDomainDef *def)
+ virDomainDef *def,
+ virDomainTPMDef *tpm)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+ ATTRIBUTE_NONNULL(3)
G_GNUC_WARN_UNUSED_RESULT;
-void qemuExtTPMCleanupHost(virDomainDef *def)
+void qemuExtTPMCleanupHost(virDomainTPMDef *tpm)
ATTRIBUTE_NONNULL(1);
int qemuExtTPMStart(virQEMUDriver *driver,
virDomainObj *vm,
+ virDomainTPMDef *def,
bool incomingMigration)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+ ATTRIBUTE_NONNULL(3)
G_GNUC_WARN_UNUSED_RESULT;
void qemuExtTPMStop(virQEMUDriver *driver,
--
2.35.1