提交 de5a1d1d 编写于 作者: D Daniel Veillard

* src/driver.h src/internal.h src/libvirt.c src/xen_internal.c

  src/xen_internal.h docs/apibuild.py: starting the refactoring,
  first the direct Xen hypervisor module. New header describing the
  entry points of a driver.
Daniel
上级 247cf7a3
Mon Mar 20 18:43:19 CET 2006 Daniel Veillard <veillard@redhat.com>
* src/driver.h src/internal.h src/libvirt.c src/xen_internal.c
src/xen_internal.h docs/apibuild.py: starting the refactoring,
first the direct Xen hypervisor module. New header describing the
entry points of a driver.
Wed Mar 15 13:10:25 CET 2006 Daniel Veillard <veillard@redhat.com> Wed Mar 15 13:10:25 CET 2006 Daniel Veillard <veillard@redhat.com>
* src/hash.c src/hash.h src/internal.h src/libvirt.c src/sexpr.c * src/hash.c src/hash.h src/internal.h src/libvirt.c src/sexpr.c
......
...@@ -22,6 +22,7 @@ ignored_files = { ...@@ -22,6 +22,7 @@ ignored_files = {
"hash.c": "internal hash table stuff", "hash.c": "internal hash table stuff",
"hash.h": "internal hash table stuff", "hash.h": "internal hash table stuff",
"internal.h": "internal includes and defines", "internal.h": "internal includes and defines",
"driver.h": "internal driver interfaces",
"xend_internal.h": "internal includes and defines", "xend_internal.h": "internal includes and defines",
"xend_internal.c": "internal code", "xend_internal.c": "internal code",
"sexpr.h": "internal includes and defines", "sexpr.h": "internal includes and defines",
......
/*
* driver.h: description of the set of interfaces provided by a
* entry point to the virtualization engine
*/
#ifndef __VIR_DRIVER_H__
#define __VIR_DRIVER_H__
#include "libvirt.h"
#include "virterror.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
VIR_DRV_OPEN_QUIET = 1,
VIR_DRV_OPEN_RO = 2
} virDrvOpenFlag;
typedef int
(*virDrvInit) (void);
typedef int
(*virDrvOpen) (virConnectPtr conn,
const char *name,
int flags);
typedef int
(*virDrvClose) (virConnectPtr conn);
typedef const char *
(*virDrvGetType) (virConnectPtr conn);
typedef int
(*virDrvGetVersion) (void * conn,
unsigned long *hvVer);
typedef int
(*virDrvListDomains) (virConnectPtr conn,
int *ids,
int maxids);
typedef int
(*virDrvNumOfDomains) (virConnectPtr conn);
typedef virDomainPtr
(*virDrvDomainCreateLinux) (virConnectPtr conn,
const char *xmlDesc,
unsigned int flags);
typedef virDomainPtr
(*virDrvDomainLookupByID) (virConnectPtr conn,
int id);
typedef virDomainPtr
(*virDrvDomainLookupByUUID) (virConnectPtr conn,
const unsigned char *uuid);
typedef virDomainPtr
(*virDrvDomainLookupByName) (virConnectPtr conn,
const char *name);
typedef int
(*virDrvDomainSuspend) (virDomainPtr domain);
typedef int
(*virDrvDomainResume) (virDomainPtr domain);
typedef int
(*virDrvDomainShutdown) (virDomainPtr domain);
typedef int
(*virDrvDomainDestroy) (virDomainPtr domain);
typedef int
(*virDrvDomainFree) (virDomainPtr domain);
typedef const char *
(*virDrvDomainGetName) (virDomainPtr domain);
typedef int
(*virDrvDomainGetID) (virDomainPtr domain);
typedef int
(*virDrvDomainGetUUID) (virDomainPtr domain,
unsigned char *uuid);
typedef char *
(*virDrvDomainGetOSType) (virDomainPtr domain);
typedef unsigned long
(*virDrvDomainGetMaxMemory) (virDomainPtr domain);
typedef int
(*virDrvDomainSetMaxMemory) (virDomainPtr domain,
unsigned long memory);
typedef int
(*virDrvDomainGetInfo) (virDomainPtr domain,
virDomainInfoPtr info);
typedef int
(*virDrvDomainSave) (virDomainPtr domain,
const char *to);
typedef int
(*virDrvDomainRestore) (virConnectPtr conn,
const char *from);
typedef struct _virDriver virDriver;
typedef virDriver *virDriverPtr;
/**
* _virDriver:
*
* Structure associated to a virtualization driver, defining the various
* entry points for it.
*/
struct _virDriver {
const char *name;
virDrvInit init;
virDrvOpen open;
virDrvClose close;
virDrvGetType type;
virDrvGetVersion version;
virDrvListDomains listDomains;
virDrvNumOfDomains numOfDomains;
virDrvDomainCreateLinux domainCreateLinux;
virDrvDomainLookupByID domainLookupByID;
virDrvDomainLookupByUUID domainLookupByUUID;
virDrvDomainLookupByName domainLookupByName;
virDrvDomainSuspend domainSuspend;
virDrvDomainResume domainResume;
virDrvDomainShutdown domainShutdown;
virDrvDomainDestroy domainDestroy;
virDrvDomainFree domainFree;
virDrvDomainGetName domainGetName;
virDrvDomainGetID domainGetID;
virDrvDomainGetUUID domainGetUUID;
virDrvDomainGetOSType domainGetOSType;
virDrvDomainGetMaxMemory domainGetMaxMemory;
virDrvDomainSetMaxMemory domainSetMaxMemory;
virDrvDomainGetInfo domainGetInfo;
virDrvDomainSave domainSave;
virDrvDomainRestore domainRestore;
};
/*
* Registration
* TODO: also need ways to (des)activate a given driver
* lookup based on the URI given in a virConnectOpen(ReadOnly)
*/
int virRegisterDriver(virDriverPtr);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __VIR_DRIVER_H__ */
...@@ -74,70 +74,70 @@ extern "C" { ...@@ -74,70 +74,70 @@ extern "C" {
* *
* Internal structure associated to a connection * Internal structure associated to a connection
*/ */
struct _virConnect { struct _virConnect {
unsigned int magic; /* specific value to check */ unsigned int magic; /* specific value to check */
int handle; /* internal handle used for hypercall */ int handle; /* internal handle used for hypercall */
struct xs_handle *xshandle; /* handle to talk to the xenstore */ struct xs_handle *xshandle; /* handle to talk to the xenstore */
/* connection to xend */ /* connection to xend */
int type; /* PF_UNIX or PF_INET */ int type; /* PF_UNIX or PF_INET */
int len; /* lenght of addr */ int len; /* lenght of addr */
struct sockaddr *addr; /* type of address used */ struct sockaddr *addr; /* type of address used */
struct sockaddr_un addr_un; /* the unix address */ struct sockaddr_un addr_un; /* the unix address */
struct sockaddr_in addr_in; /* the inet address */ struct sockaddr_in addr_in; /* the inet address */
/* error stuff */ /* error stuff */
virError err; /* the last error */ virError err; /* the last error */
virErrorFunc handler; /* associated handlet */ virErrorFunc handler; /* associated handlet */
void *userData; /* the user data */ void *userData; /* the user data */
/* misc */ /* misc */
virHashTablePtr domains; /* hash table for known domains */ virHashTablePtr domains; /* hash table for known domains */
int flags; /* a set of connection flags */ int flags; /* a set of connection flags */
}; };
/** /**
* virDomainFlags: * virDomainFlags:
* *
* a set of special flag values associated to the domain * a set of special flag values associated to the domain
*/ */
enum { enum {
DOMAIN_IS_SHUTDOWN = (1 << 0) /* the domain is being shutdown */ DOMAIN_IS_SHUTDOWN = (1 << 0) /* the domain is being shutdown */
} virDomainFlags; } virDomainFlags;
/** /**
* _virDomain: * _virDomain:
* *
* Internal structure associated to a domain * Internal structure associated to a domain
*/ */
struct _virDomain { struct _virDomain {
unsigned int magic; /* specific value to check */ unsigned int magic; /* specific value to check */
virConnectPtr conn; /* pointer back to the connection */ virConnectPtr conn; /* pointer back to the connection */
char *name; /* the domain external name */ char *name; /* the domain external name */
char *path; /* the domain internal path */ char *path; /* the domain internal path */
int handle; /* internal handle for the dmonain ID */ int handle; /* internal handle for the domnain ID */
int flags; /* extra flags */ int flags; /* extra flags */
unsigned char uuid[16]; /* the domain unique identifier */ unsigned char uuid[16]; /* the domain unique identifier */
}; };
/* /*
* Internal routines * Internal routines
*/ */
char *virDomainGetVM(virDomainPtr domain); char *virDomainGetVM(virDomainPtr domain);
char *virDomainGetVMInfo(virDomainPtr domain, char *virDomainGetVMInfo(virDomainPtr domain,
const char *vm, const char *name); const char *vm, const char *name);
void __virRaiseError(virConnectPtr conn, void __virRaiseError(virConnectPtr conn,
virDomainPtr dom, virDomainPtr dom,
int domain, int domain,
int code, int code,
virErrorLevel level, virErrorLevel level,
const char *str1, const char *str1,
const char *str2, const char *str2,
const char *str3, const char *str3,
int int1, int int2, const char *msg, ...); int int1, int int2, const char *msg, ...);
const char *__virErrorMsg(virErrorNumber error, const char *info); const char *__virErrorMsg(virErrorNumber error, const char *info);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <unistd.h> #include <unistd.h>
#include <xs.h> #include <xs.h>
#include "internal.h" #include "internal.h"
#include "driver.h"
#include "xen_internal.h" #include "xen_internal.h"
#include "xend_internal.h" #include "xend_internal.h"
#include "hash.h" #include "hash.h"
...@@ -137,7 +138,7 @@ virConnectPtr ...@@ -137,7 +138,7 @@ virConnectPtr
virConnectOpen(const char *name) virConnectOpen(const char *name)
{ {
virConnectPtr ret = NULL; virConnectPtr ret = NULL;
int handle = -1; int res;
struct xs_handle *xshandle = NULL; struct xs_handle *xshandle = NULL;
/* we can only talk to the local Xen supervisor ATM */ /* we can only talk to the local Xen supervisor ATM */
...@@ -146,8 +147,16 @@ virConnectOpen(const char *name) ...@@ -146,8 +147,16 @@ virConnectOpen(const char *name)
return (NULL); return (NULL);
} }
handle = xenHypervisorOpen(0); ret = (virConnectPtr) malloc(sizeof(virConnect));
if (handle == -1) { if (ret == NULL) {
virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
goto failed;
}
memset(ret, 0, sizeof(virConnect));
ret->magic = VIR_CONNECT_MAGIC;
res = xenHypervisorOpen(ret, name, 0);
if (res < 0) {
goto failed; goto failed;
} }
xshandle = xs_daemon_open(); xshandle = xs_daemon_open();
...@@ -156,14 +165,6 @@ virConnectOpen(const char *name) ...@@ -156,14 +165,6 @@ virConnectOpen(const char *name)
goto failed; goto failed;
} }
ret = (virConnectPtr) malloc(sizeof(virConnect));
if (ret == NULL) {
virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
goto failed;
}
memset(ret, 0, sizeof(virConnect));
ret->magic = VIR_CONNECT_MAGIC;
ret->handle = handle;
ret->xshandle = xshandle; ret->xshandle = xshandle;
if (xend_setup(ret) < 0) if (xend_setup(ret) < 0)
goto failed; goto failed;
...@@ -174,8 +175,7 @@ virConnectOpen(const char *name) ...@@ -174,8 +175,7 @@ virConnectOpen(const char *name)
return (ret); return (ret);
failed: failed:
if (handle >= 0) xenHypervisorClose(ret);
xenHypervisorClose(handle);
if (xshandle != NULL) if (xshandle != NULL)
xs_daemon_close(xshandle); xs_daemon_close(xshandle);
if (ret != NULL) if (ret != NULL)
...@@ -197,7 +197,7 @@ virConnectPtr ...@@ -197,7 +197,7 @@ virConnectPtr
virConnectOpenReadOnly(const char *name) virConnectOpenReadOnly(const char *name)
{ {
int method = 0; int method = 0;
int handle; int res;
virConnectPtr ret = NULL; virConnectPtr ret = NULL;
struct xs_handle *xshandle = NULL; struct xs_handle *xshandle = NULL;
...@@ -207,16 +207,6 @@ virConnectOpenReadOnly(const char *name) ...@@ -207,16 +207,6 @@ virConnectOpenReadOnly(const char *name)
return (NULL); return (NULL);
} }
handle = xenHypervisorOpen(1);
if (handle >= 0)
method++;
else
handle = -1;
xshandle = xs_daemon_open_readonly();
if (xshandle != NULL)
method++;
ret = (virConnectPtr) malloc(sizeof(virConnect)); ret = (virConnectPtr) malloc(sizeof(virConnect));
if (ret == NULL) { if (ret == NULL) {
virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection"); virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
...@@ -224,7 +214,15 @@ virConnectOpenReadOnly(const char *name) ...@@ -224,7 +214,15 @@ virConnectOpenReadOnly(const char *name)
} }
memset(ret, 0, sizeof(virConnect)); memset(ret, 0, sizeof(virConnect));
ret->magic = VIR_CONNECT_MAGIC; ret->magic = VIR_CONNECT_MAGIC;
ret->handle = handle;
res = xenHypervisorOpen(ret, name, VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
if (res >= 0)
method++;
xshandle = xs_daemon_open_readonly();
if (xshandle != NULL)
method++;
ret->xshandle = xshandle; ret->xshandle = xshandle;
if (xend_setup(ret) == 0) if (xend_setup(ret) == 0)
method++; method++;
...@@ -240,8 +238,7 @@ virConnectOpenReadOnly(const char *name) ...@@ -240,8 +238,7 @@ virConnectOpenReadOnly(const char *name)
return (ret); return (ret);
failed: failed:
if (handle >= 0) xenHypervisorClose(ret);
xenHypervisorClose(handle);
if (xshandle != NULL) if (xshandle != NULL)
xs_daemon_close(xshandle); xs_daemon_close(xshandle);
if (ret != NULL) { if (ret != NULL) {
...@@ -266,6 +263,8 @@ static int ...@@ -266,6 +263,8 @@ static int
virConnectCheckStoreID(virConnectPtr conn, int id) virConnectCheckStoreID(virConnectPtr conn, int id)
{ {
if (conn->handle >= 0) { if (conn->handle >= 0) {
TODO
/*
dom0_getdomaininfo_t dominfo; dom0_getdomaininfo_t dominfo;
int tmp; int tmp;
...@@ -273,6 +272,7 @@ virConnectCheckStoreID(virConnectPtr conn, int id) ...@@ -273,6 +272,7 @@ virConnectCheckStoreID(virConnectPtr conn, int id)
tmp = xenHypervisorGetDomainInfo(conn->handle, id, &dominfo); tmp = xenHypervisorGetDomainInfo(conn->handle, id, &dominfo);
if (tmp < 0) if (tmp < 0)
return (-1); return (-1);
*/
} }
return (0); return (0);
} }
...@@ -313,8 +313,7 @@ virConnectClose(virConnectPtr conn) ...@@ -313,8 +313,7 @@ virConnectClose(virConnectPtr conn)
if (conn->xshandle != NULL) if (conn->xshandle != NULL)
xs_daemon_close(conn->xshandle); xs_daemon_close(conn->xshandle);
conn->xshandle = NULL; conn->xshandle = NULL;
if (conn->handle != -1) xenHypervisorClose(conn);
xenHypervisorClose(conn->handle);
conn->handle = -1; conn->handle = -1;
free(conn); free(conn);
return (0); return (0);
...@@ -373,8 +372,7 @@ virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer) ...@@ -373,8 +372,7 @@ virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
return (0); return (0);
} }
ver = xenHypervisorGetVersion(conn->handle); ver = xenHypervisorGetVersion(conn, hvVer);
*hvVer = (ver >> 16) * 1000000 + (ver & 0xFFFF) * 1000;
return (0); return (0);
} }
...@@ -961,7 +959,7 @@ virDomainDestroy(virDomainPtr domain) ...@@ -961,7 +959,7 @@ virDomainDestroy(virDomainPtr domain)
return (0); return (0);
} }
ret = xenHypervisorDestroyDomain(domain->conn->handle, domain->handle); ret = xenHypervisorDestroyDomain(domain);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
...@@ -1023,8 +1021,7 @@ virDomainSuspend(virDomainPtr domain) ...@@ -1023,8 +1021,7 @@ virDomainSuspend(virDomainPtr domain)
return (0); return (0);
/* then try a direct hypervisor access */ /* then try a direct hypervisor access */
return (xenHypervisorPauseDomain return (xenHypervisorPauseDomain(domain));
(domain->conn->handle, domain->handle));
} }
/** /**
...@@ -1053,8 +1050,7 @@ virDomainResume(virDomainPtr domain) ...@@ -1053,8 +1050,7 @@ virDomainResume(virDomainPtr domain)
return (0); return (0);
/* then try a direct hypervisor access */ /* then try a direct hypervisor access */
return (xenHypervisorResumeDomain return (xenHypervisorResumeDomain(domain));
(domain->conn->handle, domain->handle));
} }
/** /**
...@@ -1278,7 +1274,8 @@ virDomainGetID(virDomainPtr domain) ...@@ -1278,7 +1274,8 @@ virDomainGetID(virDomainPtr domain)
* *
* Get the type of domain operation system. * Get the type of domain operation system.
* *
* Returns the new string or NULL in case of error * Returns the new string or NULL in case of error, the string must be
* freed by the caller.
*/ */
char * char *
virDomainGetOSType(virDomainPtr domain) virDomainGetOSType(virDomainPtr domain)
...@@ -1330,15 +1327,12 @@ virDomainGetMaxMemory(virDomainPtr domain) ...@@ -1330,15 +1327,12 @@ virDomainGetMaxMemory(virDomainPtr domain)
free(tmp); free(tmp);
} }
} else { } else {
dom0_getdomaininfo_t dominfo; virDomainInfo dominfo;
int tmp; int tmp;
dominfo.domain = domain->handle; tmp = xenHypervisorGetDomainInfo(domain, &dominfo);
tmp =
xenHypervisorGetDomainInfo(domain->conn->handle,
domain->handle, &dominfo);
if (tmp >= 0) if (tmp >= 0)
ret = dominfo.max_pages * 4; ret = dominfo.maxMem;
} }
return (ret); return (ret);
} }
...@@ -1373,8 +1367,7 @@ virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) ...@@ -1373,8 +1367,7 @@ virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
return (-1); return (-1);
if (domain->conn->xshandle == NULL) if (domain->conn->xshandle == NULL)
return (-1); return (-1);
ret = xenHypervisorSetMaxMemory(domain->conn->handle, domain->handle, ret = xenHypervisorSetMaxMemory(domain, memory);
memory);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
...@@ -1429,12 +1422,15 @@ virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) ...@@ -1429,12 +1422,15 @@ virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
* if we have direct access though the hypervisor do a direct call * if we have direct access though the hypervisor do a direct call
*/ */
if (domain->conn->handle >= 0) { if (domain->conn->handle >= 0) {
ret = xenHypervisorGetDomainInfo(domain, info);
if (ret < 0)
goto xend_info;
return (0);
/*
dom0_getdomaininfo_t dominfo; dom0_getdomaininfo_t dominfo;
dominfo.domain = domain->handle; dominfo.domain = domain->handle;
ret = ret = xenHypervisorGetDomainInfo(domain, &dominfo);
xenHypervisorGetDomainInfo(domain->conn->handle,
domain->handle, &dominfo);
if (ret < 0) if (ret < 0)
goto xend_info; goto xend_info;
...@@ -1458,16 +1454,15 @@ virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) ...@@ -1458,16 +1454,15 @@ virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
info->state = VIR_DOMAIN_NONE; info->state = VIR_DOMAIN_NONE;
} }
/*
* the API brings back the cpu time in nanoseconds, * the API brings back the cpu time in nanoseconds,
* convert to microseconds, same thing convert to * convert to microseconds, same thing convert to
* kilobytes from page counts * kilobytes from page counts
*/
info->cpuTime = dominfo.cpu_time; info->cpuTime = dominfo.cpu_time;
info->memory = dominfo.tot_pages * 4; info->memory = dominfo.tot_pages * 4;
info->maxMem = dominfo.max_pages * 4; info->maxMem = dominfo.max_pages * 4;
info->nrVirtCpu = dominfo.nr_online_vcpus; info->nrVirtCpu = dominfo.nr_online_vcpus;
return (0); return (0);
*/
} }
xend_info: xend_info:
......
...@@ -31,6 +31,7 @@ typedef struct hypercall_struct { ...@@ -31,6 +31,7 @@ typedef struct hypercall_struct {
#include "internal.h" #include "internal.h"
#include "driver.h"
#include "xen_internal.h" #include "xen_internal.h"
#define XEN_HYPERVISOR_SOCKET "/proc/xen/privcmd" #define XEN_HYPERVISOR_SOCKET "/proc/xen/privcmd"
...@@ -58,44 +59,52 @@ virXenError(virErrorNumber error, const char *info, int value) ...@@ -58,44 +59,52 @@ virXenError(virErrorNumber error, const char *info, int value)
/** /**
* xenHypervisorOpen: * xenHypervisorOpen:
* @quiet: don'r raise an error on failure if set * @conn: pointer to the connection block
* @name: URL for the target, NULL for local
* @flags: combination of virDrvOpenFlag(s)
* *
* Connects to the Xen hypervisor. * Connects to the Xen hypervisor.
* *
* Returns the handle or -1 in case of error. * Returns 0 or -1 in case of error.
*/ */
int int
xenHypervisorOpen(int quiet) xenHypervisorOpen(virConnectPtr conn, const char *name, int flags)
{ {
int ret; int ret;
if ((name != NULL) && (strcmp(name, "xen")))
return(-1);
conn->handle = -1;
ret = open(XEN_HYPERVISOR_SOCKET, O_RDWR); ret = open(XEN_HYPERVISOR_SOCKET, O_RDWR);
if (ret < 0) { if (ret < 0) {
if (!quiet) if (!(flags & VIR_DRV_OPEN_QUIET))
virXenError(VIR_ERR_NO_XEN, XEN_HYPERVISOR_SOCKET, 0); virXenError(VIR_ERR_NO_XEN, XEN_HYPERVISOR_SOCKET, 0);
return (-1); return (-1);
} }
conn->handle = ret;
return (ret); return (ret);
} }
/** /**
* xenHypervisorClose: * xenHypervisorClose:
* @handle: the handle to the Xen hypervisor * @conn: pointer to the connection block
* *
* Close the connection to the Xen hypervisor. * Close the connection to the Xen hypervisor.
* *
* Returns 0 in case of success or -1 in case of error. * Returns 0 in case of success or -1 in case of error.
*/ */
int int
xenHypervisorClose(int handle) xenHypervisorClose(virConnectPtr conn)
{ {
int ret; int ret;
if (handle < 0) if ((conn == NULL) || (conn->handle < 0))
return (-1); return (-1);
ret = close(handle); ret = close(conn->handle);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
return (0); return (0);
...@@ -145,41 +154,42 @@ xenHypervisorDoOp(int handle, dom0_op_t * op) ...@@ -145,41 +154,42 @@ xenHypervisorDoOp(int handle, dom0_op_t * op)
/** /**
* xenHypervisorGetVersion: * xenHypervisorGetVersion:
* @handle: the handle to the Xen hypervisor * @conn: pointer to the connection block
* @hvVer: where to store the version
* *
* Call the hypervisor to extracts his own internal API version * Call the hypervisor to extracts his own internal API version
* *
* Returns the hypervisor running version or 0 in case of error. * Returns 0 in case of success, -1 in case of error
*/ */
unsigned long int
xenHypervisorGetVersion(int handle) xenHypervisorGetVersion(virConnectPtr conn, unsigned long *hvVer)
{ {
int ret; int ret;
unsigned int cmd; unsigned int cmd;
hypercall_t hc; hypercall_t hc;
if ((conn == NULL) || (conn->handle < 0) || (hvVer == NULL))
return (-1);
*hvVer = 0;
hc.op = __HYPERVISOR_xen_version; hc.op = __HYPERVISOR_xen_version;
hc.arg[0] = (unsigned long) XENVER_version; hc.arg[0] = (unsigned long) XENVER_version;
hc.arg[1] = 0; hc.arg[1] = 0;
cmd = _IOC(_IOC_NONE, 'P', 0, sizeof(hc)); cmd = _IOC(_IOC_NONE, 'P', 0, sizeof(hc));
ret = ioctl(handle, cmd, (unsigned long) &hc); ret = ioctl(conn->handle, cmd, (unsigned long) &hc);
if (ret < 0) { if (ret < 0) {
virXenError(VIR_ERR_XEN_CALL, " getting version ", XENVER_version); virXenError(VIR_ERR_XEN_CALL, " getting version ", XENVER_version);
return (0); return (-1);
} }
/* *hvVer = (ret >> 16) * 1000000 + (ret & 0xFFFF) * 1000;
* use unsigned long in case the version grows behind expectations return(0);
* allowed by int
*/
return ((unsigned long) ret);
} }
/** /**
* xenHypervisorGetDomainInfo: * xenHypervisorGetDomainInfo:
* @handle: the handle to the Xen hypervisor * @domain: pointer to the domain block
* @domain: the domain ID
* @info: the place where informations should be stored * @info: the place where informations should be stored
* *
* Do an hypervisor call to get the related set of domain informations. * Do an hypervisor call to get the related set of domain informations.
...@@ -187,16 +197,18 @@ xenHypervisorGetVersion(int handle) ...@@ -187,16 +197,18 @@ xenHypervisorGetVersion(int handle)
* Returns 0 in case of success, -1 in case of error. * Returns 0 in case of success, -1 in case of error.
*/ */
int int
xenHypervisorGetDomainInfo(int handle, int domain, xenHypervisorGetDomainInfo(virDomainPtr domain, virDomainInfoPtr info)
dom0_getdomaininfo_t * info)
{ {
dom0_op_t op; dom0_op_t op;
dom0_getdomaininfo_t dominfo;
int ret; int ret;
if (info == NULL) if ((domain == NULL) || (domain->conn == NULL) ||
(domain->conn->handle < 0) || (info == NULL))
return (-1); return (-1);
memset(info, 0, sizeof(dom0_getdomaininfo_t)); memset(info, 0, sizeof(virDomainInfo));
memset(&dominfo, 0, sizeof(dom0_getdomaininfo_t));
if (mlock(info, sizeof(dom0_getdomaininfo_t)) < 0) { if (mlock(info, sizeof(dom0_getdomaininfo_t)) < 0) {
virXenError(VIR_ERR_XEN_CALL, " locking", virXenError(VIR_ERR_XEN_CALL, " locking",
...@@ -205,13 +217,13 @@ xenHypervisorGetDomainInfo(int handle, int domain, ...@@ -205,13 +217,13 @@ xenHypervisorGetDomainInfo(int handle, int domain,
} }
op.cmd = DOM0_GETDOMAININFOLIST; op.cmd = DOM0_GETDOMAININFOLIST;
op.u.getdomaininfolist.first_domain = (domid_t) domain; op.u.getdomaininfolist.first_domain = (domid_t) domain->handle;
op.u.getdomaininfolist.max_domains = 1; op.u.getdomaininfolist.max_domains = 1;
op.u.getdomaininfolist.buffer = info; op.u.getdomaininfolist.buffer = &dominfo;
op.u.getdomaininfolist.num_domains = 1; op.u.getdomaininfolist.num_domains = 1;
info->domain = domain; dominfo.domain = domain->handle;
ret = xenHypervisorDoOp(handle, &op); ret = xenHypervisorDoOp(domain->conn->handle, &op);
if (munlock(info, sizeof(dom0_getdomaininfo_t)) < 0) { if (munlock(info, sizeof(dom0_getdomaininfo_t)) < 0) {
virXenError(VIR_ERR_XEN_CALL, " release", virXenError(VIR_ERR_XEN_CALL, " release",
...@@ -221,28 +233,61 @@ xenHypervisorGetDomainInfo(int handle, int domain, ...@@ -221,28 +233,61 @@ xenHypervisorGetDomainInfo(int handle, int domain,
if (ret < 0) if (ret < 0)
return (-1); return (-1);
switch (dominfo.flags & 0xFF) {
case DOMFLAGS_DYING:
info->state = VIR_DOMAIN_SHUTDOWN;
break;
case DOMFLAGS_SHUTDOWN:
info->state = VIR_DOMAIN_SHUTOFF;
break;
case DOMFLAGS_PAUSED:
info->state = VIR_DOMAIN_PAUSED;
break;
case DOMFLAGS_BLOCKED:
info->state = VIR_DOMAIN_BLOCKED;
break;
case DOMFLAGS_RUNNING:
info->state = VIR_DOMAIN_RUNNING;
break;
default:
info->state = VIR_DOMAIN_NONE;
}
/*
* the API brings back the cpu time in nanoseconds,
* convert to microseconds, same thing convert to
* kilobytes from page counts
*/
info->cpuTime = dominfo.cpu_time;
info->memory = dominfo.tot_pages * 4;
info->maxMem = dominfo.max_pages * 4;
info->nrVirtCpu = dominfo.nr_online_vcpus;
return (0); return (0);
} }
/** /**
* xenHypervisorPauseDomain: * xenHypervisorPauseDomain:
* @handle: the handle to the Xen hypervisor * @domain: pointer to the domain block
* @domain: the domain ID
* *
* Do an hypervisor call to pause the given domain * Do an hypervisor call to pause the given domain
* *
* Returns 0 in case of success, -1 in case of error. * Returns 0 in case of success, -1 in case of error.
*/ */
int int
xenHypervisorPauseDomain(int handle, int domain) xenHypervisorPauseDomain(virDomainPtr domain)
{ {
dom0_op_t op; dom0_op_t op;
int ret; int ret;
if ((domain == NULL) || (domain->conn == NULL) ||
(domain->conn->handle < 0))
return (-1);
op.cmd = DOM0_PAUSEDOMAIN; op.cmd = DOM0_PAUSEDOMAIN;
op.u.pausedomain.domain = (domid_t) domain; op.u.pausedomain.domain = (domid_t) domain->handle;
ret = xenHypervisorDoOp(handle, &op); ret = xenHypervisorDoOp(domain->conn->handle, &op);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
...@@ -251,23 +296,26 @@ xenHypervisorPauseDomain(int handle, int domain) ...@@ -251,23 +296,26 @@ xenHypervisorPauseDomain(int handle, int domain)
/** /**
* xenHypervisorResumeDomain: * xenHypervisorResumeDomain:
* @handle: the handle to the Xen hypervisor * @domain: pointer to the domain block
* @domain: the domain ID
* *
* Do an hypervisor call to resume the given domain * Do an hypervisor call to resume the given domain
* *
* Returns 0 in case of success, -1 in case of error. * Returns 0 in case of success, -1 in case of error.
*/ */
int int
xenHypervisorResumeDomain(int handle, int domain) xenHypervisorResumeDomain(virDomainPtr domain)
{ {
dom0_op_t op; dom0_op_t op;
int ret; int ret;
if ((domain == NULL) || (domain->conn == NULL) ||
(domain->conn->handle < 0))
return (-1);
op.cmd = DOM0_UNPAUSEDOMAIN; op.cmd = DOM0_UNPAUSEDOMAIN;
op.u.unpausedomain.domain = (domid_t) domain; op.u.unpausedomain.domain = (domid_t) domain->handle;
ret = xenHypervisorDoOp(handle, &op); ret = xenHypervisorDoOp(domain->conn->handle, &op);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
...@@ -276,23 +324,26 @@ xenHypervisorResumeDomain(int handle, int domain) ...@@ -276,23 +324,26 @@ xenHypervisorResumeDomain(int handle, int domain)
/** /**
* xenHypervisorDestroyDomain: * xenHypervisorDestroyDomain:
* @handle: the handle to the Xen hypervisor * @domain: pointer to the domain block
* @domain: the domain ID
* *
* Do an hypervisor call to destroy the given domain * Do an hypervisor call to destroy the given domain
* *
* Returns 0 in case of success, -1 in case of error. * Returns 0 in case of success, -1 in case of error.
*/ */
int int
xenHypervisorDestroyDomain(int handle, int domain) xenHypervisorDestroyDomain(virDomainPtr domain)
{ {
dom0_op_t op; dom0_op_t op;
int ret; int ret;
if ((domain == NULL) || (domain->conn == NULL) ||
(domain->conn->handle < 0))
return (-1);
op.cmd = DOM0_DESTROYDOMAIN; op.cmd = DOM0_DESTROYDOMAIN;
op.u.destroydomain.domain = (domid_t) domain; op.u.destroydomain.domain = (domid_t) domain->handle;
ret = xenHypervisorDoOp(handle, &op); ret = xenHypervisorDoOp(domain->conn->handle, &op);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
...@@ -301,8 +352,7 @@ xenHypervisorDestroyDomain(int handle, int domain) ...@@ -301,8 +352,7 @@ xenHypervisorDestroyDomain(int handle, int domain)
/** /**
* xenHypervisorSetMaxMemory: * xenHypervisorSetMaxMemory:
* @handle: the handle to the Xen hypervisor * @domain: pointer to the domain block
* @domain: the domain ID
* @memory: the max memory size in kilobytes. * @memory: the max memory size in kilobytes.
* *
* Do an hypervisor call to change the maximum amount of memory used * Do an hypervisor call to change the maximum amount of memory used
...@@ -310,16 +360,20 @@ xenHypervisorDestroyDomain(int handle, int domain) ...@@ -310,16 +360,20 @@ xenHypervisorDestroyDomain(int handle, int domain)
* Returns 0 in case of success, -1 in case of error. * Returns 0 in case of success, -1 in case of error.
*/ */
int int
xenHypervisorSetMaxMemory(int handle, int domain, unsigned long memory) xenHypervisorSetMaxMemory(virDomainPtr domain, unsigned long memory)
{ {
dom0_op_t op; dom0_op_t op;
int ret; int ret;
if ((domain == NULL) || (domain->conn == NULL) ||
(domain->conn->handle < 0))
return (-1);
op.cmd = DOM0_SETDOMAINMAXMEM; op.cmd = DOM0_SETDOMAINMAXMEM;
op.u.setdomainmaxmem.domain = (domid_t) domain; op.u.setdomainmaxmem.domain = (domid_t) domain->handle;
op.u.setdomainmaxmem.max_memkb = memory; op.u.setdomainmaxmem.max_memkb = memory;
ret = xenHypervisorDoOp(handle, &op); ret = xenHypervisorDoOp(domain->conn->handle, &op);
if (ret < 0) if (ret < 0)
return (-1); return (-1);
......
...@@ -21,17 +21,19 @@ ...@@ -21,17 +21,19 @@
extern "C" { extern "C" {
#endif #endif
int xenHypervisorOpen(int quiet); int xenHypervisorOpen (virConnectPtr conn,
int xenHypervisorClose(int handle); const char *name,
unsigned long xenHypervisorGetVersion(int handle); int flags);
int xenHypervisorDestroyDomain(int handle, int domain); int xenHypervisorClose (virConnectPtr conn);
int xenHypervisorResumeDomain(int handle, int domain); int xenHypervisorGetVersion (virConnectPtr conn,
int xenHypervisorPauseDomain(int handle, int domain); unsigned long *hvVer);
int xenHypervisorGetDomainInfo(int handle, int xenHypervisorDestroyDomain (virDomainPtr domain);
int domain, int xenHypervisorResumeDomain (virDomainPtr domain);
dom0_getdomaininfo_t * info); int xenHypervisorPauseDomain (virDomainPtr domain);
int xenHypervisorSetMaxMemory(int handle, int xenHypervisorGetDomainInfo (virDomainPtr domain,
int domain, unsigned long memory); virDomainInfoPtr info);
int xenHypervisorSetMaxMemory (virDomainPtr domain,
unsigned long memory);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册