conf: capabilities: Refactor API for setting guest capability features

Remove the need to pass around strings and switch to the enum values
instead.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-11-12 13:57:23 +01:00
parent 7a6e7bad1c
commit 9a2ca9c947
7 changed files with 77 additions and 97 deletions

View File

@ -557,49 +557,55 @@ static const struct virCapsGuestFeatureInfo virCapsGuestFeatureInfos[VIR_CAPS_GU
};
static void
virCapabilitiesAddGuestFeatureInternal(virCapsGuestPtr guest,
virCapsGuestFeatureType feature,
bool defaultOn,
bool toggle)
{
guest->features[feature].present = true;
if (virCapsGuestFeatureInfos[feature].togglesRequired) {
guest->features[feature].defaultOn = virTristateSwitchFromBool(defaultOn);
guest->features[feature].toggle = virTristateBoolFromBool(toggle);
}
}
/**
* virCapabilitiesAddGuestFeature:
* @guest: guest to associate feature with
* @name: name of feature ('pae', 'acpi', 'apic')
* @defaultOn: true if it defaults to on
* @toggle: true if its state can be toggled
* @feature: feature to add
*
* Registers a feature for a guest domain.
*/
virCapsGuestFeaturePtr
void
virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
const char *name,
bool defaultOn,
bool toggle)
virCapsGuestFeatureType feature)
{
virCapsGuestFeaturePtr feature = NULL;
bool togglesRequired = false;
size_t i;
for (i = 0; i < VIR_CAPS_GUEST_FEATURE_TYPE_LAST; i++) {
if (STRNEQ(name, virCapsGuestFeatureInfos[i].name))
continue;
feature = guest->features + i;
togglesRequired = virCapsGuestFeatureInfos[i].togglesRequired;
}
if (!feature) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid feature '%s'"), name);
return NULL;
}
feature->present = true;
if (togglesRequired) {
feature->defaultOn = virTristateSwitchFromBool(defaultOn);
feature->toggle = virTristateBoolFromBool(toggle);
}
return feature;
virCapabilitiesAddGuestFeatureInternal(guest, feature, false, false);
}
/**
* virCapabilitiesAddGuestFeatureWithToggle:
* @guest: guest to associate feature with
* @feature: feature to add
* @defaultOn: true if it defaults to on
* @toggle: true if its state can be toggled
*
* Registers a feature with toggles for a guest domain.
*/
void
virCapabilitiesAddGuestFeatureWithToggle(virCapsGuestPtr guest,
virCapsGuestFeatureType feature,
bool defaultOn,
bool toggle)
{
virCapabilitiesAddGuestFeatureInternal(guest, feature, defaultOn, toggle);
}
/**
* virCapabilitiesHostSecModelAddBaseLabel
* @secmodel: Security model to add a base label for

View File

@ -283,11 +283,14 @@ virCapabilitiesAddGuestDomain(virCapsGuestPtr guest,
int nmachines,
virCapsGuestMachinePtr *machines);
virCapsGuestFeaturePtr
void
virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
const char *name,
bool defaultOn,
bool toggle);
virCapsGuestFeatureType feature);
void
virCapabilitiesAddGuestFeatureWithToggle(virCapsGuestPtr guest,
virCapsGuestFeatureType feature,
bool defaultOn,
bool toggle);
int
virCapabilitiesAddStoragePool(virCapsPtr caps,

View File

@ -46,6 +46,7 @@ virAccessPermStorageVolTypeToString;
virCapabilitiesAddGuest;
virCapabilitiesAddGuestDomain;
virCapabilitiesAddGuestFeature;
virCapabilitiesAddGuestFeatureWithToggle;
virCapabilitiesAddHostFeature;
virCapabilitiesAddHostMigrateTransport;
virCapabilitiesAddHostNUMACell;

View File

@ -547,46 +547,26 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
NULL) == NULL)
return -1;
if (guest_archs[i].pae &&
virCapabilitiesAddGuestFeature(guest,
"pae",
1,
0) == NULL)
return -1;
if (guest_archs[i].pae)
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_PAE);
if (guest_archs[i].nonpae &&
virCapabilitiesAddGuestFeature(guest,
"nonpae",
1,
0) == NULL)
return -1;
if (guest_archs[i].nonpae)
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_NONPAE);
if (guest_archs[i].ia64_be &&
virCapabilitiesAddGuestFeature(guest,
"ia64_be",
1,
0) == NULL)
return -1;
if (guest_archs[i].ia64_be)
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_IA64_BE);
if (guest_archs[i].hvm) {
if (virCapabilitiesAddGuestFeature(guest,
"acpi",
1,
1) == NULL)
return -1;
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_ACPI,
true, true);
if (virCapabilitiesAddGuestFeature(guest, "apic",
1,
0) == NULL)
return -1;
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_APIC,
true, false);
}
if (guest_archs[i].hvm || guest_archs[i].pvh) {
if (virCapabilitiesAddGuestFeature(guest,
"hap",
1,
1) == NULL)
return -1;
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_HAP,
true, true);
}
}

View File

@ -849,14 +849,10 @@ virQEMUCapsInitGuestFromBinary(virCapsPtr caps,
/* CPU selection is always available, because all QEMU versions
* we support can use at least '-cpu host' */
if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
goto cleanup;
if (!virCapabilitiesAddGuestFeature(guest, "deviceboot", true, false))
goto cleanup;
if (!virCapabilitiesAddGuestFeature(guest, "disksnapshot", true, false))
goto cleanup;
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION);
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_DEVICEBOOT);
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_DISKSNAPSHOT,
true, false);
if (virCapabilitiesAddGuestDomain(guest,
VIR_DOMAIN_VIRT_QEMU,
@ -877,20 +873,18 @@ virQEMUCapsInitGuestFromBinary(virCapsPtr caps,
}
}
if ((ARCH_IS_X86(guestarch) || guestarch == VIR_ARCH_AARCH64) &&
virCapabilitiesAddGuestFeature(guest, "acpi", true, true) == NULL) {
goto cleanup;
}
if ((ARCH_IS_X86(guestarch) || guestarch == VIR_ARCH_AARCH64))
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_ACPI,
true, true);
if (ARCH_IS_X86(guestarch) &&
virCapabilitiesAddGuestFeature(guest, "apic", true, false) == NULL) {
goto cleanup;
}
if (ARCH_IS_X86(guestarch))
virCapabilitiesAddGuestFeatureWithToggle(guest, VIR_CAPS_GUEST_FEATURE_TYPE_APIC,
true, false);
if ((guestarch == VIR_ARCH_I686) &&
(virCapabilitiesAddGuestFeature(guest, "pae", true, false) == NULL ||
virCapabilitiesAddGuestFeature(guest, "nonpae", true, false) == NULL))
goto cleanup;
if (guestarch == VIR_ARCH_I686) {
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_PAE);
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_NONPAE);
}
ret = 0;

View File

@ -350,10 +350,8 @@ testBuildCapabilities(virConnectPtr conn)
NULL) == NULL)
goto error;
if (virCapabilitiesAddGuestFeature(guest, "pae", true, true) == NULL)
goto error;
if (virCapabilitiesAddGuestFeature(guest, "nonpae", true, true) == NULL)
goto error;
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_PAE);
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_NONPAE);
}
caps->host.nsecModels = 1;

View File

@ -110,8 +110,7 @@ testQemuAddI686Guest(virCapsPtr caps)
machines)))
goto error;
if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
goto error;
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION);
machines = NULL;
@ -161,8 +160,7 @@ testQemuAddX86_64Guest(virCapsPtr caps)
machines)))
goto error;
if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
goto error;
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION);
machines = NULL;