qemu: domain: Refactor chardev definition preparing

Use the qemuDomainDeviceBackendChardevForeach helper to iterate all
eligible structs and convert the setup of the TLS defaults from the
config.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-11-18 15:16:20 +01:00
parent 7f339e3ea9
commit 73871c3a30
5 changed files with 86 additions and 65 deletions

View File

@ -9720,66 +9720,63 @@ qemuDomainPrepareChannel(virDomainChrDef *channel,
}
/* qemuDomainPrepareChardevSourceTLS:
* @source: pointer to host interface data for char devices
* @cfg: driver configuration
/* qemuDomainPrepareChardevSourceOne:
* @dev: device definition
* @charsrc: chardev source definition
* @opaque: pointer to struct qemuDomainPrepareChardevSourceData
*
* Updates host interface TLS encryption setting based on qemu.conf
* for char devices. This will be presented as "tls='yes|no'" in
* live XML of a guest.
* Updates the config of a chardev source based on the qemu driver configuration.
* Note that this is meant to be called via
* qemuDomainDeviceBackendChardevForeach(One).
*/
void
qemuDomainPrepareChardevSourceTLS(virDomainChrSourceDef *source,
virQEMUDriverConfig *cfg)
int
qemuDomainPrepareChardevSourceOne(virDomainDeviceDef *dev,
virDomainChrSourceDef *charsrc,
void *opaque)
{
if (source->type == VIR_DOMAIN_CHR_TYPE_TCP) {
if (source->data.tcp.haveTLS == VIR_TRISTATE_BOOL_ABSENT) {
if (cfg->chardevTLS)
source->data.tcp.haveTLS = VIR_TRISTATE_BOOL_YES;
else
source->data.tcp.haveTLS = VIR_TRISTATE_BOOL_NO;
source->data.tcp.tlsFromConfig = true;
struct qemuDomainPrepareChardevSourceData *data = opaque;
switch ((virDomainDeviceType) dev->type) {
case VIR_DOMAIN_DEVICE_CHR:
case VIR_DOMAIN_DEVICE_RNG:
case VIR_DOMAIN_DEVICE_SMARTCARD:
case VIR_DOMAIN_DEVICE_REDIRDEV:
if (charsrc->type == VIR_DOMAIN_CHR_TYPE_TCP) {
if (charsrc->data.tcp.haveTLS == VIR_TRISTATE_BOOL_ABSENT) {
charsrc->data.tcp.haveTLS = virTristateBoolFromBool(data->cfg->chardevTLS);
charsrc->data.tcp.tlsFromConfig = true;
}
}
break;
case VIR_DOMAIN_DEVICE_DISK:
case VIR_DOMAIN_DEVICE_NET:
case VIR_DOMAIN_DEVICE_SHMEM:
case VIR_DOMAIN_DEVICE_LEASE:
case VIR_DOMAIN_DEVICE_FS:
case VIR_DOMAIN_DEVICE_INPUT:
case VIR_DOMAIN_DEVICE_SOUND:
case VIR_DOMAIN_DEVICE_VIDEO:
case VIR_DOMAIN_DEVICE_HOSTDEV:
case VIR_DOMAIN_DEVICE_WATCHDOG:
case VIR_DOMAIN_DEVICE_CONTROLLER:
case VIR_DOMAIN_DEVICE_GRAPHICS:
case VIR_DOMAIN_DEVICE_HUB:
case VIR_DOMAIN_DEVICE_NONE:
case VIR_DOMAIN_DEVICE_MEMBALLOON:
case VIR_DOMAIN_DEVICE_NVRAM:
case VIR_DOMAIN_DEVICE_TPM:
case VIR_DOMAIN_DEVICE_PANIC:
case VIR_DOMAIN_DEVICE_LAST:
case VIR_DOMAIN_DEVICE_MEMORY:
case VIR_DOMAIN_DEVICE_IOMMU:
case VIR_DOMAIN_DEVICE_VSOCK:
case VIR_DOMAIN_DEVICE_AUDIO:
break;
}
}
/* qemuDomainPrepareChardevSource:
* @def: live domain definition
* @cfg: driver configuration
*
* Iterate through all devices that use virDomainChrSourceDef *as host
* interface part.
*/
void
qemuDomainPrepareChardevSource(virDomainDef *def,
virQEMUDriverConfig *cfg)
{
size_t i;
for (i = 0; i < def->nserials; i++)
qemuDomainPrepareChardevSourceTLS(def->serials[i]->source, cfg);
for (i = 0; i < def->nparallels; i++)
qemuDomainPrepareChardevSourceTLS(def->parallels[i]->source, cfg);
for (i = 0; i < def->nchannels; i++)
qemuDomainPrepareChardevSourceTLS(def->channels[i]->source, cfg);
for (i = 0; i < def->nconsoles; i++)
qemuDomainPrepareChardevSourceTLS(def->consoles[i]->source, cfg);
for (i = 0; i < def->nrngs; i++)
if (def->rngs[i]->backend == VIR_DOMAIN_RNG_BACKEND_EGD)
qemuDomainPrepareChardevSourceTLS(def->rngs[i]->source.chardev, cfg);
for (i = 0; i < def->nsmartcards; i++)
if (def->smartcards[i]->type == VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH)
qemuDomainPrepareChardevSourceTLS(def->smartcards[i]->data.passthru,
cfg);
for (i = 0; i < def->nredirdevs; i++)
qemuDomainPrepareChardevSourceTLS(def->redirdevs[i]->source, cfg);
return 0;
}

View File

@ -864,13 +864,14 @@ int qemuDomainPrepareChannel(virDomainChrDef *chr,
const char *domainChannelTargetDir)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
void qemuDomainPrepareChardevSourceTLS(virDomainChrSourceDef *source,
virQEMUDriverConfig *cfg)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
struct qemuDomainPrepareChardevSourceData {
virQEMUDriverConfig *cfg;
};
void qemuDomainPrepareChardevSource(virDomainDef *def,
virQEMUDriverConfig *cfg)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int
qemuDomainPrepareChardevSourceOne(virDomainDeviceDef *dev,
virDomainChrSourceDef *charsrc,
void *opaque);
void qemuDomainPrepareShmemChardev(virDomainShmemDef *shmem)
ATTRIBUTE_NONNULL(1);

View File

@ -6776,6 +6776,13 @@ qemuDomainAttachDeviceLive(virDomainObj *vm,
{
int ret = -1;
const char *alias = NULL;
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
struct qemuDomainPrepareChardevSourceData chardevBackendData = { .cfg = cfg };
if (qemuDomainDeviceBackendChardevForeachOne(dev,
qemuDomainPrepareChardevSourceOne,
&chardevBackendData) < 0)
return -1;
switch ((virDomainDeviceType)dev->type) {
case VIR_DOMAIN_DEVICE_DISK:

View File

@ -1852,9 +1852,6 @@ qemuDomainAddChardevTLSObjects(virQEMUDriver *driver,
g_autoptr(virJSONValue) tlsProps = NULL;
g_autoptr(virJSONValue) secProps = NULL;
/* NB: This may alter haveTLS based on cfg */
qemuDomainPrepareChardevSourceTLS(dev, cfg);
if (dev->type != VIR_DOMAIN_CHR_TYPE_TCP ||
dev->data.tcp.haveTLS != VIR_TRISTATE_BOOL_YES)
return 0;

View File

@ -6494,6 +6494,24 @@ qemuProcessUpdateSEVInfo(virDomainObj *vm)
}
/* qemuProcessPrepareChardevSource:
* @def: live domain definition
* @cfg: driver configuration
*
* Iterate through all devices that use virDomainChrSourceDef as backend.
*/
static int
qemuProcessPrepareChardevSource(virDomainDef *def,
virQEMUDriverConfig *cfg)
{
struct qemuDomainPrepareChardevSourceData data = { .cfg = cfg };
return qemuDomainDeviceBackendChardevForeach(def,
qemuDomainPrepareChardevSourceOne,
&data);
}
/**
* qemuProcessPrepareDomain:
* @driver: qemu driver
@ -6582,8 +6600,9 @@ qemuProcessPrepareDomain(virQEMUDriver *driver,
if (qemuProcessPrepareDomainHostdevs(vm, priv) < 0)
return -1;
VIR_DEBUG("Prepare chardev source backends for TLS");
qemuDomainPrepareChardevSource(vm->def, cfg);
VIR_DEBUG("Prepare chardev source backends");
if (qemuProcessPrepareChardevSource(vm->def, cfg) < 0)
return -1;
VIR_DEBUG("Prepare device secrets");
if (qemuDomainSecretPrepare(driver, vm) < 0)