This function needs to be used at two different places and make it global now. Also, extend it to return the NR_CPUs when needed. Signed-off-by: Shivaprasad G Bhat <sbhat@xxxxxxxxxxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/qemu/qemu_driver.c | 52 +--------------------------------------------- src/util/virhostcpu.c | 37 +++++++++++++++++++++++++++++++++ src/util/virhostcpu.h | 7 ++++++ 4 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e939de3..569f8e8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1066,6 +1066,7 @@ virLogManagerNew; nodeCapsInitNUMA; nodeGetInfo; virHostCPUGetCount; +virHostCPUGetKVMVCPUs; virHostCPUGetMap; virHostCPUGetOnlineBitmap; virHostCPUGetPresentBitmap; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index a45fd55..65ef68c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -122,24 +122,6 @@ VIR_LOG_INIT("qemu.qemu_driver"); #define QEMU_SCHED_MIN_QUOTA 1000LL #define QEMU_SCHED_MAX_QUOTA 18446744073709551LL -#if HAVE_LINUX_KVM_H -# include <linux/kvm.h> -#endif - -/* device for kvm ioctls */ -#define KVM_DEVICE "/dev/kvm" - -/* add definitions missing in older linux/kvm.h */ -#ifndef KVMIO -# define KVMIO 0xAE -#endif -#ifndef KVM_CHECK_EXTENSION -# define KVM_CHECK_EXTENSION _IO(KVMIO, 0x03) -#endif -#ifndef KVM_CAP_NR_VCPUS -# define KVM_CAP_NR_VCPUS 9 /* returns max vcpus per vm */ -#endif - #define QEMU_NB_BLKIO_PARAM 6 #define QEMU_NB_BANDWIDTH_PARAM 7 @@ -1269,38 +1251,6 @@ static int qemuConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED) } -static int -kvmGetMaxVCPUs(void) -{ - int fd; - int ret; - - if ((fd = open(KVM_DEVICE, O_RDONLY)) < 0) { - virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE); - return -1; - } - -#ifdef KVM_CAP_MAX_VCPUS - /* at first try KVM_CAP_MAX_VCPUS to determine the maximum count */ - if ((ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS)) > 0) - goto cleanup; -#endif /* KVM_CAP_MAX_VCPUS */ - - /* as a fallback get KVM_CAP_NR_VCPUS (the recommended maximum number of - * vcpus). Note that on most machines this is set to 160. */ - if ((ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS)) > 0) - goto cleanup; - - /* if KVM_CAP_NR_VCPUS doesn't exist either, kernel documentation states - * that 4 should be used as the maximum number of cpus */ - ret = 4; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; -} - - static char * qemuConnectGetSysinfo(virConnectPtr conn, unsigned int flags) { @@ -1338,7 +1288,7 @@ qemuConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED, const char *type) return 16; if (STRCASEEQ(type, "kvm")) - return kvmGetMaxVCPUs(); + return virHostCPUGetKVMVCPUs(VIR_HOSTCPU_KVM_MAXVCPUS); if (STRCASEEQ(type, "kqemu")) return 1; diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index 00c09cd..5712eda 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -37,6 +37,7 @@ #if HAVE_LINUX_KVM_H # include <linux/kvm.h> #endif +#define KVM_DEVICE "/dev/kvm" #if defined(__FreeBSD__) || defined(__APPLE__) # include <sys/time.h> @@ -1297,3 +1298,39 @@ virHostCPUGetThreadsPerSubcore(virArch arch ATTRIBUTE_UNUSED) } #endif /* HAVE_LINUX_KVM_H && defined(KVM_CAP_PPC_SMT) */ + +#ifndef KVM_CAP_NR_VCPUS +# define KVM_CAP_NR_VCPUS 9 /* returns max vcpus per vm */ +#endif + +int +virHostCPUGetKVMVCPUs(virHostCPUKVMWrapperFlags flag) +{ + int fd; + int ret; + + if ((fd = open(KVM_DEVICE, O_RDONLY)) < 0) { + virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE); + return -1; + } + +#ifdef KVM_CAP_MAX_VCPUS + /* at first try KVM_CAP_MAX_VCPUS to determine the maximum count */ + if (flag & VIR_HOSTCPU_KVM_MAXVCPUS && + (ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS)) > 0) + goto cleanup; +#endif /* KVM_CAP_MAX_VCPUS */ + + /* as a fallback get KVM_CAP_NR_VCPUS (the recommended maximum number of + * vcpus). Note that on most machines this is set to 160. */ + if ((flag & VIR_HOSTCPU_KVM_MAXVCPUS || flag & VIR_HOSTCPU_KVM_NR_VCPUS) && + (ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS)) > 0) + goto cleanup; + /* if KVM_CAP_NR_VCPUS doesn't exist either, kernel documentation states + * that 4 should be used as the maximum number of cpus */ + ret = 4; + + cleanup: + VIR_FORCE_CLOSE(fd); + return ret; +} diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h index e5ffc70..c1b855a 100644 --- a/src/util/virhostcpu.h +++ b/src/util/virhostcpu.h @@ -51,4 +51,11 @@ int virHostCPUGetInfo(virArch hostarch, unsigned int *cores, unsigned int *threads); +typedef enum { + VIR_HOSTCPU_KVM_MAXVCPUS = (1 << 0), + VIR_HOSTCPU_KVM_NR_VCPUS = (1 << 1), +} virHostCPUKVMWrapperFlags; + +int virHostCPUGetKVMVCPUs(virHostCPUKVMWrapperFlags flag); + #endif /* __VIR_HOSTCPU_H__*/ -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list