This patch adds new public APIs virDomainGetCPUMode and virDomainSetCPUMode to get/set cpu mode of a domain. These APIs are useful for users who want to get/set cpu mode easily without messing around with domain XML by themselves. Signed-off-by: Ken ICHIKAWA <ichikawa.ken@xxxxxxxxxxxxxx> --- include/libvirt/libvirt.h.in | 9 ++++ src/driver.h | 11 +++++ src/libvirt.c | 112 +++++++++++++++++++++++++++++++++++++++++++ src/libvirt_public.syms | 2 + 4 files changed, 134 insertions(+) diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index 17804ca..3e5fbc3 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1739,6 +1739,15 @@ virDomainGetMetadata(virDomainPtr domain, const char *uri, unsigned int flags); +int +virDomainSetCPUMode(virDomainPtr domain, + const char *cpuMode, + unsigned int flags); + +char * +virDomainGetCPUMode(virDomainPtr domain, + unsigned int flags); + /* * XML domain description */ diff --git a/src/driver.h b/src/driver.h index 64d652f..1fc8668 100644 --- a/src/driver.h +++ b/src/driver.h @@ -915,6 +915,15 @@ typedef int unsigned long long minimum, unsigned int flags); +typedef int + (*virDrvDomainSetCPUMode)(virDomainPtr dom, + const char *cpuMode, + unsigned int flags); + +typedef char * + (*virDrvDomainGetCPUMode)(virDomainPtr dom, + unsigned int flags); + /** * _virDriver: * @@ -1107,6 +1116,8 @@ struct _virDriver { virDrvNodeGetCPUMap nodeGetCPUMap; virDrvDomainFSTrim domainFSTrim; virDrvDomainSendProcessSignal domainSendProcessSignal; + virDrvDomainSetCPUMode domainSetCPUMode; + virDrvDomainGetCPUMode domainGetCPUMode; }; typedef int diff --git a/src/libvirt.c b/src/libvirt.c index 6a7a817..9545f36 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -9616,6 +9616,118 @@ error: } /** + * virDomainSetCPUMode: + * @domain: a domain object + * @cpuMode: cpu mode, one of 'custom', 'host-model' and 'host-passthrough' + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Change cpu mode of a domain to @cpuMode. + * @cpuMode means mode attribute of cpu element of the domain XML. + * This function does not affect a running domain state but affects + * the next boot of a persistent domain. + * + * Returns 0 on success, -1 in case of failure. + */ +int +virDomainSetCPUMode(virDomainPtr domain, + const char *cpuMode, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "cpuMode=%s, flags=%x", cpuMode, flags); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(domain)) { + virLibDomainError(VIR_ERR_INVALID_DOMAIN, __FUNCTION__); + goto error; + } + + conn = domain->conn; + + if (conn->flags & VIR_CONNECT_RO) { + virLibDomainError(VIR_ERR_OPERATION_DENIED, __FUNCTION__); + goto error; + } + + if (conn->driver->domainSetCPUMode) { + int ret; + ret = conn->driver->domainSetCPUMode(domain, cpuMode, flags); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__); + +error: + virDispatchError(domain->conn); + return -1; +} + +/** + * virDomainGetCPUMode: + * @domain: a domain object + * @flags: bitwise-OR of virDomainModificationImpact + * + * Get cpu mode of a domain. The cpu mode is specified in mode + * attribute of cpu element of the domain XML. + * If there is no cpu element or mode attribute, this function + * returns 'custom' because 'custom' mode is default. + * + * @flags controls whether the live domain or persistent + * configuration will be queried. If VIR_AFFECT_LIVE is set, this + * will query a running domain state. If VIR_AFFECT_CONFIG is set, + * this will query a persistent configuration. + * If neither flag is specified or VIR_AFFECT_CURRENT is set, this + * will query a running domain state for active domains or a persistent + * configuration for inactive domains. + * These flags are mutually exclusive. + * + * Returns cpu mode string on success (caller must free), + * or NULL in case of failure. + */ +char * +virDomainGetCPUMode(virDomainPtr domain, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "flags=%x", flags); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(domain)) { + virLibDomainError(VIR_ERR_INVALID_DOMAIN, __FUNCTION__); + goto error; + } + + if ((flags & VIR_DOMAIN_AFFECT_LIVE) && + (flags & VIR_DOMAIN_AFFECT_CONFIG)) { + virReportInvalidArg(flags, + _("flags 'affect live' and 'affect config' in %s are mutually exclusive"), + __FUNCTION__); + goto error; + } + + conn = domain->conn; + + if (conn->driver->domainGetCPUMode) { + char *ret; + ret = conn->driver->domainGetCPUMode(domain, flags); + if (!ret) + goto error; + return ret; + } + + virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__); + +error: + virDispatchError(domain->conn); + return NULL; +} + +/** * virNodeGetSecurityModel: * @conn: a connection object * @secmodel: pointer to a virSecurityModel structure diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index e3d63d3..8fc407d 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -578,6 +578,8 @@ LIBVIRT_1.0.1 { global: virDomainFSTrim; virDomainSendProcessSignal; + virDomainSetCPUMode; + virDomainGetCPUMode; } LIBVIRT_1.0.0; # .... define new API here using predicted next version number .... -- 1.7.11.7 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list