qemu: don't continue loading caps if outdated

The XML format used for QEMU capabilities is not required to be
stable across releases, as we invalidate the cache whenever the
libvirt binary changes.

We none the less always try to parse te entire XML file before
we do any validity checks. Thus if we change the format of any
part of the data, or change permitted values for enums, then
libvirtd logs will be spammed with errors.

These are not in fact errors, but an expected scenario.

This change makes the loading code validate the cache timestamp
against the libvirtd timestamp immediately. If they don't match
then we stop loading the rest of the XML file.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2020-06-18 14:44:16 +01:00
parent e80eafa287
commit 66ce769d27
6 changed files with 61 additions and 36 deletions

View File

@ -1811,14 +1811,6 @@ virQEMUCapsNewBinary(const char *binary)
} }
void
virQEMUCapsSetInvalidation(virQEMUCapsPtr qemuCaps,
bool enabled)
{
qemuCaps->invalidation = enabled;
}
static int static int
virQEMUCapsHostCPUDataCopy(virQEMUCapsHostCPUDataPtr dst, virQEMUCapsHostCPUDataCopy(virQEMUCapsHostCPUDataPtr dst,
virQEMUCapsHostCPUDataPtr src) virQEMUCapsHostCPUDataPtr src)
@ -4209,11 +4201,14 @@ virQEMUCapsParseSEVInfo(virQEMUCapsPtr qemuCaps, xmlXPathContextPtr ctxt)
* <machine name='pc-1.0' alias='pc' hotplugCpus='yes' maxCpus='4' default='yes' numaMemSupported='yes'/> * <machine name='pc-1.0' alias='pc' hotplugCpus='yes' maxCpus='4' default='yes' numaMemSupported='yes'/>
* ... * ...
* </qemuCaps> * </qemuCaps>
*
* Returns 0 on success, 1 if outdated, -1 on error
*/ */
int int
virQEMUCapsLoadCache(virArch hostArch, virQEMUCapsLoadCache(virArch hostArch,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
const char *filename) const char *filename,
bool skipInvalidation)
{ {
xmlDocPtr doc = NULL; xmlDocPtr doc = NULL;
int ret = -1; int ret = -1;
@ -4241,6 +4236,31 @@ virQEMUCapsLoadCache(virArch hostArch,
goto cleanup; goto cleanup;
} }
if (virXPathLongLong("string(./selfctime)", ctxt, &l) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing selfctime in QEMU capabilities XML"));
goto cleanup;
}
qemuCaps->libvirtCtime = (time_t)l;
qemuCaps->libvirtVersion = 0;
if (virXPathULong("string(./selfvers)", ctxt, &lu) == 0)
qemuCaps->libvirtVersion = lu;
if (!skipInvalidation &&
(qemuCaps->libvirtCtime != virGetSelfLastChanged() ||
qemuCaps->libvirtVersion != LIBVIR_VERSION_NUMBER)) {
VIR_DEBUG("Outdated capabilities in %s: libvirt changed "
"(%lld vs %lld, %lu vs %lu), stopping load",
qemuCaps->binary,
(long long)qemuCaps->libvirtCtime,
(long long)virGetSelfLastChanged(),
(unsigned long)qemuCaps->libvirtVersion,
(unsigned long)LIBVIR_VERSION_NUMBER);
ret = 1;
goto cleanup;
}
if (!(str = virXPathString("string(./emulator)", ctxt))) { if (!(str = virXPathString("string(./emulator)", ctxt))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing emulator in QEMU capabilities cache")); _("missing emulator in QEMU capabilities cache"));
@ -4260,17 +4280,6 @@ virQEMUCapsLoadCache(virArch hostArch,
} }
qemuCaps->ctime = (time_t)l; qemuCaps->ctime = (time_t)l;
if (virXPathLongLong("string(./selfctime)", ctxt, &l) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing selfctime in QEMU capabilities XML"));
goto cleanup;
}
qemuCaps->libvirtCtime = (time_t)l;
qemuCaps->libvirtVersion = 0;
if (virXPathULong("string(./selfvers)", ctxt, &lu) == 0)
qemuCaps->libvirtVersion = lu;
if ((n = virXPathNodeSet("./flag", ctxt, &nodes)) < 0) { if ((n = virXPathNodeSet("./flag", ctxt, &nodes)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse qemu capabilities flags")); _("failed to parse qemu capabilities flags"));
@ -4422,6 +4431,9 @@ virQEMUCapsLoadCache(virArch hostArch,
if (virXPathBoolean("boolean(./kvmSupportsSecureGuest)", ctxt) > 0) if (virXPathBoolean("boolean(./kvmSupportsSecureGuest)", ctxt) > 0)
qemuCaps->kvmSupportsSecureGuest = true; qemuCaps->kvmSupportsSecureGuest = true;
if (skipInvalidation)
qemuCaps->invalidation = false;
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(str); VIR_FREE(str);
@ -5493,16 +5505,23 @@ virQEMUCapsNewData(const char *binary,
static void * static void *
virQEMUCapsLoadFile(const char *filename, virQEMUCapsLoadFile(const char *filename,
const char *binary, const char *binary,
void *privData) void *privData,
bool *outdated)
{ {
virQEMUCapsPtr qemuCaps = virQEMUCapsNewBinary(binary); virQEMUCapsPtr qemuCaps = virQEMUCapsNewBinary(binary);
virQEMUCapsCachePrivPtr priv = privData; virQEMUCapsCachePrivPtr priv = privData;
int ret;
if (!qemuCaps) if (!qemuCaps)
return NULL; return NULL;
if (virQEMUCapsLoadCache(priv->hostArch, qemuCaps, filename) < 0) ret = virQEMUCapsLoadCache(priv->hostArch, qemuCaps, filename, false);
if (ret < 0)
goto error; goto error;
if (ret == 1) {
*outdated = true;
goto error;
}
return qemuCaps; return qemuCaps;

View File

@ -37,12 +37,10 @@ virQEMUCapsNewForBinaryInternal(virArch hostArch,
unsigned int microcodeVersion, unsigned int microcodeVersion,
const char *kernelVersion); const char *kernelVersion);
void virQEMUCapsSetInvalidation(virQEMUCapsPtr qemuCaps,
bool enabled);
int virQEMUCapsLoadCache(virArch hostArch, int virQEMUCapsLoadCache(virArch hostArch,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
const char *filename); const char *filename,
bool skipInvalidation);
char *virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps); char *virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps);
int int

View File

@ -130,6 +130,7 @@ virFileCacheLoad(virFileCachePtr cache,
g_autofree char *file = NULL; g_autofree char *file = NULL;
int ret = -1; int ret = -1;
void *loadData = NULL; void *loadData = NULL;
bool outdated = false;
*data = NULL; *data = NULL;
@ -148,10 +149,12 @@ virFileCacheLoad(virFileCachePtr cache,
goto cleanup; goto cleanup;
} }
if (!(loadData = cache->handlers.loadFile(file, name, cache->priv))) { if (!(loadData = cache->handlers.loadFile(file, name, cache->priv, &outdated))) {
VIR_WARN("Failed to load cached data from '%s' for '%s': %s", if (!outdated) {
file, name, virGetLastErrorMessage()); VIR_WARN("Failed to load cached data from '%s' for '%s': %s",
virResetLastError(); file, name, virGetLastErrorMessage());
virResetLastError();
}
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }

View File

@ -62,15 +62,20 @@ typedef void *
* @filename: name of a file with cached data * @filename: name of a file with cached data
* @name: name of the cached data * @name: name of the cached data
* @priv: private data created together with cache * @priv: private data created together with cache
* @outdated: set to true if data was outdated
* *
* Loads the cached data from a file @filename. * Loads the cached data from a file @filename. If
* NULL is returned, then @oudated indicates whether
* this was due to the data being outdated, or an
* error loading the cache.
* *
* Returns cached data object or NULL on error. * Returns cached data object or NULL on outdated data or error.
*/ */
typedef void * typedef void *
(*virFileCacheLoadFilePtr)(const char *filename, (*virFileCacheLoadFilePtr)(const char *filename,
const char *name, const char *name,
void *priv); void *priv,
bool *outdated);
/** /**
* virFileCacheSaveFilePtr: * virFileCacheSaveFilePtr:

View File

@ -293,10 +293,9 @@ qemuTestParseCapabilitiesArch(virArch arch,
virArchToString(arch)); virArchToString(arch));
if (!(qemuCaps = virQEMUCapsNewBinary(binary)) || if (!(qemuCaps = virQEMUCapsNewBinary(binary)) ||
virQEMUCapsLoadCache(arch, qemuCaps, capsFile) < 0) virQEMUCapsLoadCache(arch, qemuCaps, capsFile, true) < 0)
goto error; goto error;
virQEMUCapsSetInvalidation(qemuCaps, false);
return qemuCaps; return qemuCaps;
error: error:

View File

@ -110,7 +110,8 @@ testFileCacheNewData(const char *name G_GNUC_UNUSED,
static void * static void *
testFileCacheLoadFile(const char *filename, testFileCacheLoadFile(const char *filename,
const char *name G_GNUC_UNUSED, const char *name G_GNUC_UNUSED,
void *priv G_GNUC_UNUSED) void *priv G_GNUC_UNUSED,
bool *outdated G_GNUC_UNUSED)
{ {
testFileCacheObjPtr obj; testFileCacheObjPtr obj;
char *data; char *data;