int libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver, virNodeInfoPtr info) { libxl_physinfo phy_info; virArch hostarch = virArchFromHost(); libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); int ret = -1; if (libxl_get_physinfo(cfg->ctx, &phy_info)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("libxl_get_physinfo_info failed")); goto cleanup; } if (virStrcpyStatic(info->model, virArchToString(hostarch)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("machine type %s too big for destination"), virArchToString(hostarch)); goto cleanup; } info->memory = phy_info.total_pages * (cfg->verInfo->pagesize / 1024); info->cpus = phy_info.nr_cpus; info->nodes = phy_info.nr_nodes; info->cores = phy_info.cores_per_socket; info->threads = phy_info.threads_per_core; info->sockets = 1; info->mhz = phy_info.cpu_khz / 1000; ret = 0; cleanup: virObjectUnref(cfg); return ret; }
/** * virCPUUpdate: * * @arch: CPU architecture * @guest: guest CPU definition to be updated * @host: host CPU definition * * Updates @guest CPU definition according to @host CPU. This is required to * support guest CPU definitions specified relatively to host CPU, such as * CPUs with VIR_CPU_MODE_CUSTOM and optional features or * VIR_CPU_MATCH_MINIMUM, or CPUs with VIR_CPU_MODE_HOST_MODEL. * When the guest CPU was not specified relatively, the function does nothing * and returns success. * * Returns 0 on success, -1 on error. */ int virCPUUpdate(virArch arch, virCPUDefPtr guest, const virCPUDef *host) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, guest=%p mode=%s model=%s, host=%p model=%s", virArchToString(arch), guest, virCPUModeTypeToString(guest->mode), NULLSTR(guest->model), host, NULLSTR(host ? host->model : NULL)); if (!(driver = cpuGetSubDriver(arch))) return -1; if (guest->mode == VIR_CPU_MODE_HOST_PASSTHROUGH) return 0; if (guest->mode == VIR_CPU_MODE_CUSTOM && guest->match != VIR_CPU_MATCH_MINIMUM) { size_t i; bool optional = false; for (i = 0; i < guest->nfeatures; i++) { if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) { optional = true; break; } } if (!optional) return 0; } /* We get here if guest CPU is either * - host-model * - custom with minimum match * - custom with optional features */ if (!driver->update) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot update guest CPU for %s architecture"), virArchToString(arch)); return -1; } if (driver->update(guest, host) < 0) return -1; VIR_DEBUG("model=%s", NULLSTR(guest->model)); return 0; }
/** * cpuCompare: * * @host: host CPU definition * @cpu: either guest or host CPU to be compared with @host * * Compares the CPU described by @cpu with @host CPU. * * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when * the two CPUs are incompatible, VIR_CPU_COMPARE_IDENTICAL when the two CPUs * are identical, VIR_CPU_COMPARE_SUPERSET when the @cpu CPU is a superset of * the @host CPU. */ virCPUCompareResult cpuCompare(virCPUDefPtr host, virCPUDefPtr cpu, bool failIncompatible) { struct cpuArchDriver *driver; VIR_DEBUG("host=%p, cpu=%p", host, cpu); if (!cpu->model) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("no guest CPU model specified")); return VIR_CPU_COMPARE_ERROR; } if ((driver = cpuGetSubDriver(host->arch)) == NULL) return VIR_CPU_COMPARE_ERROR; if (driver->compare == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot compare CPUs of %s architecture"), virArchToString(host->arch)); return VIR_CPU_COMPARE_ERROR; } return driver->compare(host, cpu, failIncompatible); }
/** * virCPUUpdateLive: * * @arch: CPU architecture * @cpu: guest CPU definition to be updated * @dataEnabled: CPU data of the virtual CPU * @dataDisabled: CPU data with features requested by @cpu but disabled by the * hypervisor * * Update custom mode CPU according to the virtual CPU created by the * hypervisor. The function refuses to update the CPU in case cpu->check is set * to VIR_CPU_CHECK_FULL. * * Returns -1 on error, * 0 when the CPU was successfully updated, * 1 when the operation does not make sense on the CPU or it is not * supported for the given architecture. */ int virCPUUpdateLive(virArch arch, virCPUDefPtr cpu, virCPUDataPtr dataEnabled, virCPUDataPtr dataDisabled) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, cpu=%p, dataEnabled=%p, dataDisabled=%p", virArchToString(arch), cpu, dataEnabled, dataDisabled); if (!(driver = cpuGetSubDriver(arch))) return -1; if (!driver->updateLive) return 1; if (cpu->mode != VIR_CPU_MODE_CUSTOM) return 1; if (driver->updateLive(cpu, dataEnabled, dataDisabled) < 0) return -1; return 0; }
int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) { virArch hostarch = virArchFromHost(); if (virStrcpyStatic(nodeinfo->model, virArchToString(hostarch)) == NULL) return -1; #ifdef __linux__ { int ret = -1; FILE *cpuinfo = fopen(CPUINFO_PATH, "r"); if (!cpuinfo) { virReportSystemError(errno, _("cannot open %s"), CPUINFO_PATH); return -1; } ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH, nodeinfo); if (ret < 0) goto cleanup; /* Convert to KB. */ nodeinfo->memory = physmem_total() / 1024; cleanup: VIR_FORCE_FCLOSE(cpuinfo); return ret; } #else /* XXX Solaris will need an impl later if they port QEMU driver */ virReportError(VIR_ERR_NO_SUPPORT, "%s", _("node info not implemented on this platform")); return -1; #endif }
/** * cpuGuestData: * * @host: host CPU definition * @guest: guest CPU definition * @data: computed guest CPU data * @msg: error message describing why the @guest and @host CPUs are considered * incompatible * * Computes guest CPU data for the @guest CPU definition when run on the @host * CPU. * * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when * the two CPUs are incompatible (@msg will describe the incompatibility), * VIR_CPU_COMPARE_IDENTICAL when the two CPUs are identical, * VIR_CPU_COMPARE_SUPERSET when the @guest CPU is a superset of the @host CPU. */ virCPUCompareResult cpuGuestData(virCPUDefPtr host, virCPUDefPtr guest, virCPUDataPtr *data, char **msg) { struct cpuArchDriver *driver; VIR_DEBUG("host=%p, guest=%p, data=%p, msg=%p", host, guest, data, msg); if (!guest->model) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("no guest CPU model specified")); return VIR_CPU_COMPARE_ERROR; } if ((driver = cpuGetSubDriver(host->arch)) == NULL) return VIR_CPU_COMPARE_ERROR; if (driver->guestData == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot compute guest CPU data for %s architecture"), virArchToString(host->arch)); return VIR_CPU_COMPARE_ERROR; } return driver->guestData(host, guest, data, msg); }
/** * virCPUCompareXML: * * @arch: CPU architecture * @host: host CPU definition * @xml: XML description of either guest or host CPU to be compared with @host * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE * * Compares the CPU described by @xml with @host CPU. * * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when * the two CPUs are incompatible, VIR_CPU_COMPARE_IDENTICAL when the two CPUs * are identical, VIR_CPU_COMPARE_SUPERSET when the @xml CPU is a superset of * the @host CPU. If @failIncompatible is true, the function will return * VIR_CPU_COMPARE_ERROR (and set VIR_ERR_CPU_INCOMPATIBLE error) when the * two CPUs are incompatible. */ virCPUCompareResult virCPUCompareXML(virArch arch, virCPUDefPtr host, const char *xml, bool failIncompatible) { xmlDocPtr doc = NULL; xmlXPathContextPtr ctxt = NULL; virCPUDefPtr cpu = NULL; virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR; VIR_DEBUG("arch=%s, host=%p, xml=%s", virArchToString(arch), host, NULLSTR(xml)); if (!xml) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("missing CPU definition")); goto cleanup; } if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt))) goto cleanup; if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu) < 0) goto cleanup; ret = virCPUCompare(arch, host, cpu, failIncompatible); cleanup: virCPUDefFree(cpu); xmlXPathFreeContext(ctxt); xmlFreeDoc(doc); return ret; }
/** * virArchFromHost: * * Return the host architecture. Prefer this to the * uname 'machine' field, since this will canonicalize * architecture names like 'amd64' into 'x86_64'. */ virArch virArchFromHost(void) { struct utsname ut; virArch arch; uname(&ut); /* Some special cases we need to handle first * for non-canonical names */ if (strlen(ut.machine) == 4 && ut.machine[0] == 'i' && ut.machine[2] == '8' && ut.machine[3] == '6' && ut.machine[4] == '\0') { arch = VIR_ARCH_I686; } else if (STREQ(ut.machine, "amd64")) { arch = VIR_ARCH_X86_64; } else { /* Otherwise assume the canonical name */ if ((arch = virArchFromString(ut.machine)) == VIR_ARCH_NONE) { VIR_WARN("Unknown host arch %s, report to [email protected]", ut.machine); } } VIR_DEBUG("Mapped %s to %d (%s)", ut.machine, arch, virArchToString(arch)); return arch; }
/** * cpuDecode: * * @cpu: CPU definition stub to be filled in * @data: internal CPU data to be decoded into @cpu definition * @models: list of CPU models that can be considered when decoding @data * * Decodes internal CPU data into a CPU definition consisting of a CPU model * and a list of CPU features. The @cpu model stub is supposed to have arch, * type, match and fallback members set, this function will add the rest. If * @models list is NULL, all models supported by libvirt will be considered * when decoding the data. In general, this function will select the model * closest to the CPU specified by @data. * * For VIR_ARCH_I686 and VIR_ARCH_X86_64 architectures this means the computed * CPU definition will have the shortest possible list of additional features. * * Returns 0 on success, -1 on error. */ int cpuDecode(virCPUDefPtr cpu, const virCPUData *data, virDomainCapsCPUModelsPtr models) { struct cpuArchDriver *driver; VIR_DEBUG("cpu=%p, data=%p, models=%p", cpu, data, models); if (models) { size_t i; for (i = 0; i < models->nmodels; i++) VIR_DEBUG("models[%zu]=%s", i, models->models[i].name); } if (cpu->type > VIR_CPU_TYPE_GUEST || cpu->mode != VIR_CPU_MODE_CUSTOM) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("invalid CPU definition stub")); return -1; } if ((driver = cpuGetSubDriver(data->arch)) == NULL) return -1; if (driver->decode == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot decode CPU data for %s architecture"), virArchToString(cpu->arch)); return -1; } return driver->decode(cpu, data, models); }
static int fakeHostCPU(virCapsPtr caps, virArch arch) { virCPUDefPtr cpu; switch (arch) { case VIR_ARCH_AARCH64: cpu = &aarch64Cpu; break; case VIR_ARCH_PPC64LE: cpu = &ppc64leCpu; break; case VIR_ARCH_X86_64: cpu = &x86Cpu; break; default: virReportError(VIR_ERR_INTERNAL_ERROR, "cannot fake host CPU for arch %s", virArchToString(arch)); return -1; } if (!(caps->host.cpu = virCPUDefCopy(cpu))) return -1; return 0; }
static int linuxTestNodeInfo(const void *opaque) { int result = -1; char *cpuinfo = NULL; char *sysfs_dir = NULL; char *output = NULL; struct linuxTestNodeInfoData *data = (struct linuxTestNodeInfoData *) opaque; const char *archStr = virArchToString(data->arch); if (virAsprintf(&sysfs_dir, "%s/nodeinfodata/linux-%s", abs_srcdir, data->testName) < 0 || virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s-%s.cpuinfo", abs_srcdir, archStr, data->testName) < 0 || virAsprintf(&output, "%s/nodeinfodata/linux-%s-%s.expected", abs_srcdir, archStr, data->testName) < 0) { goto cleanup; } result = linuxTestCompareFiles(cpuinfo, sysfs_dir, data->arch, output); cleanup: VIR_FREE(cpuinfo); VIR_FREE(output); VIR_FREE(sysfs_dir); return result; }
/** * cpuGetModels: * * @archName: CPU architecture string * @models: where to store the list of supported models * * Fetches all CPU models supported by libvirt on @archName. * * Returns number of supported CPU models or -1 on error. */ int cpuGetModels(const char *archName, char ***models) { struct cpuArchDriver *driver; virArch arch; VIR_DEBUG("arch=%s", archName); arch = virArchFromString(archName); if (arch == VIR_ARCH_NONE) { virReportError(VIR_ERR_INVALID_ARG, _("cannot find architecture %s"), archName); return -1; } driver = cpuGetSubDriver(arch); if (driver == NULL) { virReportError(VIR_ERR_INVALID_ARG, _("cannot find a driver for the architecture %s"), archName); return -1; } if (!driver->getModels) { virReportError(VIR_ERR_NO_SUPPORT, _("CPU driver for %s has no CPU model support"), virArchToString(arch)); return -1; } return driver->getModels(models); }
static int test_virCapabilities(const void *opaque) { struct virCapabilitiesData *data = (struct virCapabilitiesData *) opaque; const char *archStr = virArchToString(data->arch); virCapsPtr caps = NULL; char *capsXML = NULL; char *path = NULL; char *dir = NULL; char *resctrl = NULL; int ret = -1; /* * We want to keep our directory structure clean, so if there's both resctrl * and system used, we need to use slightly different path; a subdir. */ if (virAsprintf(&dir, "%s/vircaps2xmldata/linux-%s%s", abs_srcdir, data->filename, data->resctrl ? "/system" : "") < 0) goto cleanup; if (virAsprintf(&resctrl, "%s/vircaps2xmldata/linux-%s/resctrl", abs_srcdir, data->filename) < 0) goto cleanup; virFileWrapperAddPrefix("/sys/devices/system", dir); virFileWrapperAddPrefix("/sys/fs/resctrl", resctrl); caps = virCapabilitiesNew(data->arch, data->offlineMigrate, data->liveMigrate); if (!caps) goto cleanup; if (virCapabilitiesInitNUMA(caps) < 0 || virCapabilitiesInitCaches(caps) < 0) goto cleanup; virFileWrapperClearPrefixes(); if (!(capsXML = virCapabilitiesFormatXML(caps))) goto cleanup; if (virAsprintf(&path, "%s/vircaps2xmldata/vircaps-%s-%s.xml", abs_srcdir, archStr, data->filename) < 0) goto cleanup; if (virTestCompareToFile(capsXML, path) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(dir); VIR_FREE(resctrl); VIR_FREE(path); VIR_FREE(capsXML); virObjectUnref(caps); return ret; }
/** * virCPUGetHostIsSupported: * * @arch: CPU architecture * * Check whether virCPUGetHost is supported for @arch. * * Returns true if virCPUGetHost is supported, false otherwise. */ bool virCPUGetHostIsSupported(virArch arch) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s", virArchToString(arch)); return (driver = cpuGetSubDriver(arch)) && driver->getHost; }
/** * cpuNodeData: * * @arch: CPU architecture * * Returns CPU data for host CPU or NULL on error. */ virCPUDataPtr cpuNodeData(virArch arch) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s", virArchToString(arch)); if ((driver = cpuGetSubDriver(arch)) == NULL) return NULL; if (driver->nodeData == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot get node CPU data for %s architecture"), virArchToString(arch)); return NULL; } return driver->nodeData(arch); }
/** * cpuBaseline: * * @cpus: list of host CPU definitions * @ncpus: number of CPUs in @cpus * @models: list of CPU models that can be considered for the baseline CPU * @migratable: requests non-migratable features to be removed from the result * * Computes the most feature-rich CPU which is compatible with all given * host CPUs. If @models is NULL, all models supported by libvirt will * be considered when computing the baseline CPU model, otherwise the baseline * CPU model will be one of the provided CPU @models. * * Returns baseline CPU definition or NULL on error. */ virCPUDefPtr cpuBaseline(virCPUDefPtr *cpus, unsigned int ncpus, virDomainCapsCPUModelsPtr models, bool migratable) { struct cpuArchDriver *driver; size_t i; VIR_DEBUG("ncpus=%u, models=%p, migratable=%d", ncpus, models, migratable); if (cpus) { for (i = 0; i < ncpus; i++) VIR_DEBUG("cpus[%zu]=%p", i, cpus[i]); } if (models) { for (i = 0; i < models->nmodels; i++) VIR_DEBUG("models[%zu]=%s", i, models->models[i].name); } if (cpus == NULL && ncpus != 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nonzero ncpus doesn't match with NULL cpus")); return NULL; } if (ncpus < 1) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given")); return NULL; } for (i = 0; i < ncpus; i++) { if (!cpus[i]) { virReportError(VIR_ERR_INVALID_ARG, _("invalid CPU definition at index %zu"), i); return NULL; } if (!cpus[i]->model) { virReportError(VIR_ERR_INVALID_ARG, _("no CPU model specified at index %zu"), i); return NULL; } } if ((driver = cpuGetSubDriver(cpus[0]->arch)) == NULL) return NULL; if (driver->baseline == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot compute baseline CPU of %s architecture"), virArchToString(cpus[0]->arch)); return NULL; } return driver->baseline(cpus, ncpus, models, migratable); }
/** * cpuDataParse: * * @arch: CPU architecture * @xmlStr: XML string produced by cpuDataFormat * * Parses XML representation of virCPUData structure for test purposes. * * Returns internal CPU data structure parsed from the XML or NULL on error. */ virCPUDataPtr cpuDataParse(virArch arch, const char *xmlStr) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, xmlStr=%s", virArchToString(arch), xmlStr); if (!(driver = cpuGetSubDriver(arch))) return NULL; if (!driver->dataParse) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot parse %s CPU data"), virArchToString(arch)); return NULL; } return driver->dataParse(xmlStr); }
/** * virCPUDataCheckFeature: * * @data: CPU data * @feature: feature to be checked for * * Checks whether @feature is supported by the CPU described by @data. * * Returns 1 if the feature is supported, 0 if it's not supported, or * -1 on error. */ int virCPUDataCheckFeature(const virCPUData *data, const char *feature) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, data=%p, feature=%s", virArchToString(data->arch), data, feature); if (!(driver = cpuGetSubDriver(data->arch))) return -1; if (!driver->dataCheckFeature) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot check guest CPU feature for %s architecture"), virArchToString(data->arch)); return -1; } return driver->dataCheckFeature(data, feature); }
char * virDomainCapsFormat(virDomainCapsPtr const caps) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *virttype_str = virDomainVirtTypeToString(caps->virttype); const char *arch_str = virArchToString(caps->arch); virBufferAddLit(&buf, "<domainCapabilities>\n"); virBufferAdjustIndent(&buf, 2); virBufferEscapeString(&buf, "<path>%s</path>\n", caps->path); virBufferAsprintf(&buf, "<domain>%s</domain>\n", virttype_str); if (caps->machine) virBufferAsprintf(&buf, "<machine>%s</machine>\n", caps->machine); virBufferAsprintf(&buf, "<arch>%s</arch>\n", arch_str); if (caps->maxvcpus) virBufferAsprintf(&buf, "<vcpu max='%d'/>\n", caps->maxvcpus); FORMAT_SINGLE("iothreads", caps->iothreads); virDomainCapsOSFormat(&buf, &caps->os); virDomainCapsCPUFormat(&buf, &caps->cpu); virBufferAddLit(&buf, "<devices>\n"); virBufferAdjustIndent(&buf, 2); virDomainCapsDeviceDiskFormat(&buf, &caps->disk); virDomainCapsDeviceGraphicsFormat(&buf, &caps->graphics); virDomainCapsDeviceVideoFormat(&buf, &caps->video); virDomainCapsDeviceHostdevFormat(&buf, &caps->hostdev); virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "</devices>\n"); virBufferAddLit(&buf, "<features>\n"); virBufferAdjustIndent(&buf, 2); virDomainCapsFeatureGICFormat(&buf, &caps->gic); FORMAT_SINGLE("vmcoreinfo", caps->vmcoreinfo); FORMAT_SINGLE("genid", caps->genid); virDomainCapsFeatureSEVFormat(&buf, caps->sev); virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "</features>\n"); virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "</domainCapabilities>\n"); virBufferCheckError(&buf); return virBufferContentAndReset(&buf); }
/** * virCPUCompare: * * @arch: CPU architecture * @host: host CPU definition * @cpu: either guest or host CPU to be compared with @host * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE * * Compares the CPU described by @cpu with @host CPU. * * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when * the two CPUs are incompatible, VIR_CPU_COMPARE_IDENTICAL when the two CPUs * are identical, VIR_CPU_COMPARE_SUPERSET when the @cpu CPU is a superset of * the @host CPU. If @failIncompatible is true, the function will return * VIR_CPU_COMPARE_ERROR (and set VIR_ERR_CPU_INCOMPATIBLE error) when the * two CPUs are incompatible. */ virCPUCompareResult virCPUCompare(virArch arch, virCPUDefPtr host, virCPUDefPtr cpu, bool failIncompatible) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, host=%p, cpu=%p", virArchToString(arch), host, cpu); if (!(driver = cpuGetSubDriver(arch))) return VIR_CPU_COMPARE_ERROR; if (!driver->compare) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot compare CPUs of %s architecture"), virArchToString(arch)); return VIR_CPU_COMPARE_ERROR; } return driver->compare(host, cpu, failIncompatible); }
/** * cpuEncode: * * @arch: CPU architecture * @cpu: CPU definition to be encoded into internal CPU driver representation * @forced: where to store CPU data corresponding to forced features * @required: where to store CPU data corresponding to required features * @optional: where to store CPU data corresponding to optional features * @disabled: where to store CPU data corresponding to disabled features * @forbidden: where to store CPU data corresponding to forbidden features * @vendor: where to store CPU data corresponding to CPU vendor * * Encode CPU definition from @cpu into internal CPU driver representation. * Any of @forced, @required, @optional, @disabled, @forbidden and @vendor * arguments can be NULL in case the caller is not interested in the * corresponding data. * * Returns 0 on success, -1 on error. */ int cpuEncode(virArch arch, const virCPUDef *cpu, virCPUDataPtr *forced, virCPUDataPtr *required, virCPUDataPtr *optional, virCPUDataPtr *disabled, virCPUDataPtr *forbidden, virCPUDataPtr *vendor) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, cpu=%p, forced=%p, required=%p, " "optional=%p, disabled=%p, forbidden=%p, vendor=%p", virArchToString(arch), cpu, forced, required, optional, disabled, forbidden, vendor); if (!cpu->model) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("no guest CPU model specified")); return -1; } if ((driver = cpuGetSubDriver(arch)) == NULL) return -1; if (driver->encode == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot encode CPU data for %s architecture"), virArchToString(arch)); return -1; } return driver->encode(arch, cpu, forced, required, optional, disabled, forbidden, vendor); }
int virCPUDefFormatBufFull(virBufferPtr buf, virCPUDefPtr def, unsigned int flags) { if (!def) return 0; virBufferAddLit(buf, "<cpu"); if (def->type == VIR_CPU_TYPE_GUEST) { const char *tmp; if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) { if (!(tmp = virCPUModeTypeToString(def->mode))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unexpected CPU mode %d"), def->mode); return -1; } virBufferAsprintf(buf, " mode='%s'", tmp); } if (def->model && (def->mode == VIR_CPU_MODE_CUSTOM || (flags & VIR_DOMAIN_XML_UPDATE_CPU))) { if (!(tmp = virCPUMatchTypeToString(def->match))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unexpected CPU match policy %d"), def->match); return -1; } virBufferAsprintf(buf, " match='%s'", tmp); } } virBufferAddLit(buf, ">\n"); if (def->arch) virBufferAsprintf(buf, " <arch>%s</arch>\n", virArchToString(def->arch)); virBufferAdjustIndent(buf, 2); if (virCPUDefFormatBuf(buf, def, flags) < 0) return -1; virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "</cpu>\n"); return 0; }
/** * virCPUCopyMigratable: * * @arch: CPU architecture * @cpu: CPU definition to be copied * * Makes a copy of @cpu with all features which would block migration removed. * If this doesn't make sense for a given architecture, the function returns a * plain copy of @cpu (i.e., a copy with no features removed). * * Returns the copy of the CPU or NULL on error. */ virCPUDefPtr virCPUCopyMigratable(virArch arch, virCPUDefPtr cpu) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, cpu=%p, model=%s", virArchToString(arch), cpu, NULLSTR(cpu->model)); if (!(driver = cpuGetSubDriver(arch))) return NULL; if (driver->copyMigratable) return driver->copyMigratable(cpu); else return virCPUDefCopy(cpu); }
/** * virCPUValidateFeatures: * * @arch: CPU architecture * @cpu: CPU definition to be checked * * Checks whether all CPU features specified in @cpu are valid. * * Returns 0 on success (all features are valid), -1 on error. */ int virCPUValidateFeatures(virArch arch, virCPUDefPtr cpu) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s, cpu=%p, nfeatures=%zu", virArchToString(arch), cpu, cpu->nfeatures); if (!(driver = cpuGetSubDriver(arch))) return -1; if (driver->validateFeatures) return driver->validateFeatures(cpu); else return 0; }
/** * virCPUGetModels: * * @arch: CPU architecture * @models: where to store the NULL-terminated list of supported models * * Fetches all CPU models supported by libvirt on @archName. If there are * no restrictions on CPU models on @archName (i.e., the CPU model is just * passed directly to a hypervisor), this function returns 0 and sets * @models to NULL. * * Returns number of supported CPU models, 0 if any CPU model is supported, * or -1 on error. */ int virCPUGetModels(virArch arch, char ***models) { struct cpuArchDriver *driver; VIR_DEBUG("arch=%s", virArchToString(arch)); if (!(driver = cpuGetSubDriver(arch))) return -1; if (!driver->getModels) { if (models) *models = NULL; return 0; } return driver->getModels(models); }
static int virDomainCapsFormatInternal(virBufferPtr buf, virDomainCapsPtr const caps) { const char *virttype_str = virDomainVirtTypeToString(caps->virttype); const char *arch_str = virArchToString(caps->arch); virBufferAddLit(buf, "<domainCapabilities>\n"); virBufferAdjustIndent(buf, 2); virBufferEscapeString(buf, "<path>%s</path>\n", caps->path); virBufferAsprintf(buf, "<domain>%s</domain>\n", virttype_str); virBufferAsprintf(buf, "<machine>%s</machine>\n", caps->machine); virBufferAsprintf(buf, "<arch>%s</arch>\n", arch_str); if (caps->maxvcpus) virBufferAsprintf(buf, "<vcpu max='%d'/>\n", caps->maxvcpus); virDomainCapsOSFormat(buf, &caps->os); virDomainCapsCPUFormat(buf, &caps->cpu); virBufferAddLit(buf, "<devices>\n"); virBufferAdjustIndent(buf, 2); virDomainCapsDeviceDiskFormat(buf, &caps->disk); virDomainCapsDeviceGraphicsFormat(buf, &caps->graphics); virDomainCapsDeviceVideoFormat(buf, &caps->video); virDomainCapsDeviceHostdevFormat(buf, &caps->hostdev); virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "</devices>\n"); virBufferAddLit(buf, "<features>\n"); virBufferAdjustIndent(buf, 2); virDomainCapsFeatureGICFormat(buf, &caps->gic); virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "</features>\n"); virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "</domainCapabilities>\n"); return 0; }
/** * cpuDataFormat: * * @data: internal CPU representation * * Formats @data into XML for test purposes. * * Returns string representation of the XML describing @data or NULL on error. */ char * cpuDataFormat(const virCPUData *data) { struct cpuArchDriver *driver; VIR_DEBUG("data=%p", data); if (!(driver = cpuGetSubDriver(data->arch))) return NULL; if (!driver->dataFormat) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot format %s CPU data"), virArchToString(data->arch)); return NULL; } return driver->dataFormat(data); }
/** * cpuDecode: * * @cpu: CPU definition stub to be filled in * @data: internal CPU data to be decoded into @cpu definition * @models: list of CPU models that can be considered when decoding @data * @nmodels: number of CPU models in @models * @preferred: CPU models that should be used if possible * * Decodes internal CPU data into a CPU definition consisting of a CPU model * and a list of CPU features. The @cpu model stub is supposed to have arch, * type, match and fallback members set, this function will add the rest. If * @models list is NULL, all models supported by libvirt will be considered * when decoding the data. In general, this function will select the model * closest to the CPU specified by @data unless @preferred is non-NULL, in * which case the @preferred model will be used as long as it is compatible * with @data. * * For VIR_ARCH_I686 and VIR_ARCH_X86_64 architectures this means the computed * CPU definition will have the shortest possible list of additional features. * When @preferred is non-NULL, the @preferred model will be used even if * other models would result in a shorter list of additional features. * * Returns 0 on success, -1 on error. */ int cpuDecode(virCPUDefPtr cpu, const virCPUData *data, const char **models, unsigned int nmodels, const char *preferred) { struct cpuArchDriver *driver; VIR_DEBUG("cpu=%p, data=%p, nmodels=%u, preferred=%s", cpu, data, nmodels, NULLSTR(preferred)); if (models) { size_t i; for (i = 0; i < nmodels; i++) VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i])); } if (models == NULL && nmodels != 0) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("nonzero nmodels doesn't match with NULL models")); return -1; } if (cpu->type > VIR_CPU_TYPE_GUEST || cpu->mode != VIR_CPU_MODE_CUSTOM) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("invalid CPU definition stub")); return -1; } if ((driver = cpuGetSubDriver(cpu->arch)) == NULL) return -1; if (driver->decode == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot decode CPU data for %s architecture"), virArchToString(cpu->arch)); return -1; } return driver->decode(cpu, data, models, nmodels, preferred, 0); }
/** * cpuUpdate: * * @guest: guest CPU definition * @host: host CPU definition * * Updates @guest CPU definition according to @host CPU. This is required to * support guest CPU definition which are relative to host CPU, such as CPUs * with VIR_CPU_MODE_CUSTOM and optional features or VIR_CPU_MATCH_MINIMUM, or * CPUs with non-custom mode (VIR_CPU_MODE_HOST_MODEL, * VIR_CPU_MODE_HOST_PASSTHROUGH). * * Returns 0 on success, -1 on error. */ int cpuUpdate(virCPUDefPtr guest, const virCPUDef *host) { struct cpuArchDriver *driver; VIR_DEBUG("guest=%p, host=%p", guest, host); if ((driver = cpuGetSubDriver(host->arch)) == NULL) return -1; if (driver->update == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot update guest CPU data for %s architecture"), virArchToString(host->arch)); return -1; } return driver->update(guest, host); }
/** * cpuHasFeature: * * @data: internal CPU representation * @feature: feature to be checked for * * Checks whether @feature is supported by the CPU described by @data. * * Returns 1 if the feature is supported, 0 if it's not supported, or * -1 on error. */ int cpuHasFeature(const virCPUData *data, const char *feature) { struct cpuArchDriver *driver; VIR_DEBUG("data=%p, feature=%s", data, feature); if ((driver = cpuGetSubDriver(data->arch)) == NULL) return -1; if (driver->hasFeature == NULL) { virReportError(VIR_ERR_NO_SUPPORT, _("cannot check guest CPU data for %s architecture"), virArchToString(data->arch)); return -1; } return driver->hasFeature(data, feature); }