e10da2
From 9d2c60799271d605f82dfd4bfa6ed7d14ad87e26 Mon Sep 17 00:00:00 2001
e10da2
From: Eric Blake <eblake@redhat.com>
e10da2
Date: Mon, 27 Sep 2010 09:37:22 -0600
e10da2
Subject: [PATCH 04/15] vcpu: implement the public APIs
e10da2
e10da2
Factors common checks (such as nonzero vcpu count) up front, but
e10da2
drivers will still need to do additional flag checks.
e10da2
e10da2
* src/libvirt.c (virDomainSetVcpusFlags, virDomainGetVcpusFlags):
e10da2
New functions.
e10da2
(virDomainSetVcpus, virDomainGetMaxVcpus): Refer to new API.
e10da2
---
e10da2
 src/libvirt.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
e10da2
 1 files changed, 134 insertions(+), 6 deletions(-)
e10da2
e10da2
diff --git a/src/libvirt.c b/src/libvirt.c
e10da2
index 629d97b..1b39210 100644
e10da2
--- a/src/libvirt.c
e10da2
+++ b/src/libvirt.c
e10da2
@@ -5192,7 +5192,9 @@ error:
e10da2
  * This function requires privileged access to the hypervisor.
e10da2
  *
e10da2
  * This command only changes the runtime configuration of the domain,
e10da2
- * so can only be called on an active domain.
e10da2
+ * so can only be called on an active domain.  It is hypervisor-dependent
e10da2
+ * whether it also affects persistent configuration; for more control,
e10da2
+ * use virDomainSetVcpusFlags().
e10da2
  *
e10da2
  * Returns 0 in case of success, -1 in case of failure.
e10da2
  */
e10da2
@@ -5237,13 +5239,139 @@ error:
e10da2
 }
e10da2
e10da2
 /**
e10da2
+ * virDomainSetVcpusFlags:
e10da2
+ * @domain: pointer to domain object, or NULL for Domain0
e10da2
+ * @nvcpus: the new number of virtual CPUs for this domain, must be at least 1
e10da2
+ * @flags: an OR'ed set of virDomainVcpuFlags
e10da2
+ *
e10da2
+ * Dynamically change the number of virtual CPUs used by the domain.
e10da2
+ * Note that this call may fail if the underlying virtualization hypervisor
e10da2
+ * does not support it or if growing the number is arbitrary limited.
e10da2
+ * This function requires privileged access to the hypervisor.
e10da2
+ *
e10da2
+ * @flags must include VIR_DOMAIN_VCPU_LIVE to affect a running
e10da2
+ * domain (which may fail if domain is not active), or
e10da2
+ * VIR_DOMAIN_VCPU_CONFIG to affect the next boot via the XML
e10da2
+ * description of the domain.  Both flags may be set.
e10da2
+ *
e10da2
+ * If @flags includes VIR_DOMAIN_VCPU_MAXIMUM, then
e10da2
+ * VIR_DOMAIN_VCPU_LIVE must be clear, and only the maximum virtual
e10da2
+ * CPU limit is altered; generally, this value must be less than or
e10da2
+ * equal to virConnectGetMaxVcpus().  Otherwise, this call affects the
e10da2
+ * current virtual CPU limit, which must be less than or equal to the
e10da2
+ * maximum limit.
e10da2
+ *
e10da2
+ * Returns 0 in case of success, -1 in case of failure.
e10da2
+ */
e10da2
+
e10da2
+int
e10da2
+virDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
e10da2
+                       unsigned int flags)
e10da2
+{
e10da2
+    virConnectPtr conn;
e10da2
+    VIR_DEBUG("domain=%p, nvcpus=%u, flags=%u", domain, nvcpus, flags);
e10da2
+
e10da2
+    virResetLastError();
e10da2
+
e10da2
+    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
e10da2
+        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
e10da2
+        virDispatchError(NULL);
e10da2
+        return (-1);
e10da2
+    }
e10da2
+    if (domain->conn->flags & VIR_CONNECT_RO) {
e10da2
+        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
e10da2
+        goto error;
e10da2
+    }
e10da2
+
e10da2
+    /* Perform some argument validation common to all implementations.  */
e10da2
+    if (nvcpus < 1 || (unsigned short) nvcpus != nvcpus ||
e10da2
+        (flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0) {
e10da2
+        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
e10da2
+        goto error;
e10da2
+    }
e10da2
+    conn = domain->conn;
e10da2
+
e10da2
+    if (conn->driver->domainSetVcpusFlags) {
e10da2
+        int ret;
e10da2
+        ret = conn->driver->domainSetVcpusFlags (domain, nvcpus, flags);
e10da2
+        if (ret < 0)
e10da2
+            goto error;
e10da2
+        return ret;
e10da2
+    }
e10da2
+
e10da2
+    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
e10da2
+
e10da2
+error:
e10da2
+    virDispatchError(domain->conn);
e10da2
+    return -1;
e10da2
+}
e10da2
+
e10da2
+/**
e10da2
+ * virDomainGetVcpusFlags:
e10da2
+ * @domain: pointer to domain object, or NULL for Domain0
e10da2
+ * @flags: an OR'ed set of virDomainVcpuFlags
e10da2
+ *
e10da2
+ * Query the number of virtual CPUs used by the domain.  Note that
e10da2
+ * this call may fail if the underlying virtualization hypervisor does
e10da2
+ * not support it.  This function requires privileged access to the
e10da2
+ * hypervisor.
e10da2
+ *
e10da2
+ * @flags must include either VIR_DOMAIN_VCPU_ACTIVE to query a
e10da2
+ * running domain (which will fail if domain is not active), or
e10da2
+ * VIR_DOMAIN_VCPU_PERSISTENT to query the XML description of the
e10da2
+ * domain.  It is an error to set both flags.
e10da2
+ *
e10da2
+ * If @flags includes VIR_DOMAIN_VCPU_MAXIMUM, then the maximum
e10da2
+ * virtual CPU limit is queried.  Otherwise, this call queries the
e10da2
+ * current virtual CPU limit.
e10da2
+ *
e10da2
+ * Returns 0 in case of success, -1 in case of failure.
e10da2
+ */
e10da2
+
e10da2
+int
e10da2
+virDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
e10da2
+{
e10da2
+    virConnectPtr conn;
e10da2
+    VIR_DEBUG("domain=%p, flags=%u", domain, flags);
e10da2
+
e10da2
+    virResetLastError();
e10da2
+
e10da2
+    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
e10da2
+        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
e10da2
+        virDispatchError(NULL);
e10da2
+        return (-1);
e10da2
+    }
e10da2
+
e10da2
+    /* Exactly one of these two flags should be set.  */
e10da2
+    if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
e10da2
+        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
e10da2
+        goto error;
e10da2
+    }
e10da2
+    conn = domain->conn;
e10da2
+
e10da2
+    if (conn->driver->domainGetVcpusFlags) {
e10da2
+        int ret;
e10da2
+        ret = conn->driver->domainGetVcpusFlags (domain, flags);
e10da2
+        if (ret < 0)
e10da2
+            goto error;
e10da2
+        return ret;
e10da2
+    }
e10da2
+
e10da2
+    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
e10da2
+
e10da2
+error:
e10da2
+    virDispatchError(domain->conn);
e10da2
+    return -1;
e10da2
+}
e10da2
+
e10da2
+/**
e10da2
  * virDomainPinVcpu:
e10da2
  * @domain: pointer to domain object, or NULL for Domain0
e10da2
  * @vcpu: virtual CPU number
e10da2
  * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes) (IN)
e10da2
- * 	Each bit set to 1 means that corresponding CPU is usable.
e10da2
- * 	Bytes are stored in little-endian order: CPU0-7, 8-15...
e10da2
- * 	In each byte, lowest CPU number is least significant bit.
e10da2
+ *      Each bit set to 1 means that corresponding CPU is usable.
e10da2
+ *      Bytes are stored in little-endian order: CPU0-7, 8-15...
e10da2
+ *      In each byte, lowest CPU number is least significant bit.
e10da2
  * @maplen: number of bytes in cpumap, from 1 up to size of CPU map in
e10da2
  *	underlying virtualization system (Xen...).
e10da2
  *	If maplen < size, missing bytes are set to zero.
e10da2
@@ -5371,9 +5499,9 @@ error:
e10da2
  *
e10da2
  * Provides the maximum number of virtual CPUs supported for
e10da2
  * the guest VM. If the guest is inactive, this is basically
e10da2
- * the same as virConnectGetMaxVcpus. If the guest is running
e10da2
+ * the same as virConnectGetMaxVcpus(). If the guest is running
e10da2
  * this will reflect the maximum number of virtual CPUs the
e10da2
- * guest was booted with.
e10da2
+ * guest was booted with.  For more details, see virDomainGetVcpusFlags().
e10da2
  *
e10da2
  * Returns the maximum of virtual CPU or -1 in case of error.
e10da2
  */
e10da2
-- 
e10da2
1.7.2.3
e10da2