📄 xc.c
字号:
/****************************************************************************** * Xc.c * * Copyright (c) 2003-2004, K A Fraser (University of Cambridge) */#include <Python.h>#include <xenctrl.h>#include <xenguest.h>#include <zlib.h>#include <fcntl.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/mman.h>#include <netdb.h>#include <arpa/inet.h>#include "xenctrl.h"#include <xen/elfnote.h>#include "xc_dom.h"#include <xen/hvm/hvm_info_table.h>#include <xen/hvm/params.h>#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))/* Needed for Python versions earlier than 2.3. */#ifndef PyMODINIT_FUNC#define PyMODINIT_FUNC DL_EXPORT(void)#endif#define PKG "xen.lowlevel.xc"#define CLS "xc"static PyObject *xc_error_obj, *zero;typedef struct { PyObject_HEAD; int xc_handle;} XcObject;static PyObject *dom_op(XcObject *self, PyObject *args, int (*fn)(int, uint32_t));static PyObject *pyxc_error_to_exception(void){ PyObject *pyerr; const xc_error *err = xc_get_last_error(); const char *desc = xc_error_code_to_desc(err->code); if ( err->code == XC_ERROR_NONE ) return PyErr_SetFromErrno(xc_error_obj); if ( err->message[0] != '\0' ) pyerr = Py_BuildValue("(iss)", err->code, desc, err->message); else pyerr = Py_BuildValue("(is)", err->code, desc); xc_clear_last_error(); if ( pyerr != NULL ) { PyErr_SetObject(xc_error_obj, pyerr); Py_DECREF(pyerr); } return NULL;}static PyObject *pyxc_domain_dumpcore(XcObject *self, PyObject *args){ uint32_t dom; char *corefile; if ( !PyArg_ParseTuple(args, "is", &dom, &corefile) ) return NULL; if ( (corefile == NULL) || (corefile[0] == '\0') ) return NULL; if ( xc_domain_dumpcore(self->xc_handle, dom, corefile) != 0 ) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_handle(XcObject *self){ return PyInt_FromLong(self->xc_handle);}static PyObject *pyxc_domain_create(XcObject *self, PyObject *args, PyObject *kwds){ uint32_t dom = 0, ssidref = 0, flags = 0; int ret, i, hvm = 0; PyObject *pyhandle = NULL; xen_domain_handle_t handle = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef }; static char *kwd_list[] = { "domid", "ssidref", "handle", "hvm", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiOi", kwd_list, &dom, &ssidref, &pyhandle, &hvm)) return NULL; if ( pyhandle != NULL ) { if ( !PyList_Check(pyhandle) || (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) ) goto out_exception; for ( i = 0; i < sizeof(xen_domain_handle_t); i++ ) { PyObject *p = PyList_GetItem(pyhandle, i); if ( !PyInt_Check(p) ) goto out_exception; handle[i] = (uint8_t)PyInt_AsLong(p); } } if ( hvm ) flags |= XEN_DOMCTL_CDF_hvm_guest; if ( (ret = xc_domain_create(self->xc_handle, ssidref, handle, flags, &dom)) < 0 ) return pyxc_error_to_exception(); return PyInt_FromLong(dom);out_exception: errno = EINVAL; PyErr_SetFromErrno(xc_error_obj); return NULL;}static PyObject *pyxc_domain_max_vcpus(XcObject *self, PyObject *args){ uint32_t dom, max; if (!PyArg_ParseTuple(args, "ii", &dom, &max)) return NULL; if (xc_domain_max_vcpus(self->xc_handle, dom, max) != 0) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_domain_pause(XcObject *self, PyObject *args){ return dom_op(self, args, xc_domain_pause);}static PyObject *pyxc_domain_unpause(XcObject *self, PyObject *args){ return dom_op(self, args, xc_domain_unpause);}static PyObject *pyxc_domain_destroy_hook(XcObject *self, PyObject *args){#ifdef __ia64__ dom_op(self, args, xc_ia64_save_to_nvram);#endif Py_INCREF(zero); return zero;}static PyObject *pyxc_domain_destroy(XcObject *self, PyObject *args){ return dom_op(self, args, xc_domain_destroy);}static PyObject *pyxc_domain_shutdown(XcObject *self, PyObject *args){ uint32_t dom, reason; if ( !PyArg_ParseTuple(args, "ii", &dom, &reason) ) return NULL; if ( xc_domain_shutdown(self->xc_handle, dom, reason) != 0 ) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_domain_resume(XcObject *self, PyObject *args){ uint32_t dom; int fast; if ( !PyArg_ParseTuple(args, "ii", &dom, &fast) ) return NULL; if ( xc_domain_resume(self->xc_handle, dom, fast) != 0 ) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_vcpu_setaffinity(XcObject *self, PyObject *args, PyObject *kwds){ uint32_t dom; int vcpu = 0, i; uint64_t cpumap = ~0ULL; PyObject *cpulist = NULL; static char *kwd_list[] = { "domid", "vcpu", "cpumap", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|iO", kwd_list, &dom, &vcpu, &cpulist) ) return NULL; if ( (cpulist != NULL) && PyList_Check(cpulist) ) { cpumap = 0ULL; for ( i = 0; i < PyList_Size(cpulist); i++ ) { long cpu = PyInt_AsLong(PyList_GetItem(cpulist, i)); if ( cpu >= 64 ) { errno = EINVAL; PyErr_SetFromErrno(xc_error_obj); return NULL; } cpumap |= (uint64_t)1 << cpu; } } if ( xc_vcpu_setaffinity(self->xc_handle, dom, vcpu, cpumap) != 0 ) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_domain_setcpuweight(XcObject *self, PyObject *args, PyObject *kwds){ uint32_t dom; float cpuweight = 1; static char *kwd_list[] = { "domid", "cpuweight", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|f", kwd_list, &dom, &cpuweight) ) return NULL; if ( xc_domain_setcpuweight(self->xc_handle, dom, cpuweight) != 0 ) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;}static PyObject *pyxc_domain_sethandle(XcObject *self, PyObject *args){ int i; uint32_t dom; PyObject *pyhandle; xen_domain_handle_t handle; if (!PyArg_ParseTuple(args, "iO", &dom, &pyhandle)) return NULL; if ( !PyList_Check(pyhandle) || (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) ) { goto out_exception; } for ( i = 0; i < sizeof(xen_domain_handle_t); i++ ) { PyObject *p = PyList_GetItem(pyhandle, i); if ( !PyInt_Check(p) ) goto out_exception; handle[i] = (uint8_t)PyInt_AsLong(p); } if (xc_domain_sethandle(self->xc_handle, dom, handle) < 0) return pyxc_error_to_exception(); Py_INCREF(zero); return zero;out_exception: PyErr_SetFromErrno(xc_error_obj); return NULL;}static PyObject *pyxc_domain_getinfo(XcObject *self, PyObject *args, PyObject *kwds){ PyObject *list, *info_dict, *pyhandle; uint32_t first_dom = 0; int max_doms = 1024, nr_doms, i, j; xc_dominfo_t *info; static char *kwd_list[] = { "first_dom", "max_doms", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list, &first_dom, &max_doms) ) return NULL; if ( (info = malloc(max_doms * sizeof(xc_dominfo_t))) == NULL ) return PyErr_NoMemory(); nr_doms = xc_domain_getinfo(self->xc_handle, first_dom, max_doms, info); if (nr_doms < 0) { free(info); return pyxc_error_to_exception(); } list = PyList_New(nr_doms); for ( i = 0 ; i < nr_doms; i++ ) { info_dict = Py_BuildValue( "{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i" ",s:L,s:L,s:L,s:i,s:i}", "domid", (int)info[i].domid, "online_vcpus", info[i].nr_online_vcpus, "max_vcpu_id", info[i].max_vcpu_id, "hvm", info[i].hvm, "dying", info[i].dying, "crashed", info[i].crashed, "shutdown", info[i].shutdown, "paused", info[i].paused, "blocked", info[i].blocked, "running", info[i].running, "mem_kb", (long long)info[i].nr_pages*(XC_PAGE_SIZE/1024), "cpu_time", (long long)info[i].cpu_time, "maxmem_kb", (long long)info[i].max_memkb, "ssidref", (int)info[i].ssidref, "shutdown_reason", info[i].shutdown_reason); pyhandle = PyList_New(sizeof(xen_domain_handle_t)); if ( (pyhandle == NULL) || (info_dict == NULL) ) { Py_DECREF(list); if ( pyhandle != NULL ) { Py_DECREF(pyhandle); } if ( info_dict != NULL ) { Py_DECREF(info_dict); } free(info); return NULL; } for ( j = 0; j < sizeof(xen_domain_handle_t); j++ ) PyList_SetItem(pyhandle, j, PyInt_FromLong(info[i].handle[j])); PyDict_SetItemString(info_dict, "handle", pyhandle); Py_DECREF(pyhandle); PyList_SetItem(list, i, info_dict); } free(info); return list;}static PyObject *pyxc_vcpu_getinfo(XcObject *self, PyObject *args, PyObject *kwds){ PyObject *info_dict, *cpulist; uint32_t dom, vcpu = 0; xc_vcpuinfo_t info; int rc, i; uint64_t cpumap; static char *kwd_list[] = { "domid", "vcpu", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list, &dom, &vcpu) ) return NULL; rc = xc_vcpu_getinfo(self->xc_handle, dom, vcpu, &info); if ( rc < 0 ) return pyxc_error_to_exception(); rc = xc_vcpu_getaffinity(self->xc_handle, dom, vcpu, &cpumap); if ( rc < 0 ) return pyxc_error_to_exception(); info_dict = Py_BuildValue("{s:i,s:i,s:i,s:L,s:i}", "online", info.online, "blocked", info.blocked, "running", info.running, "cpu_time", info.cpu_time, "cpu", info.cpu); cpulist = PyList_New(0); for ( i = 0; cpumap != 0; i++ ) { if ( cpumap & 1 ) PyList_Append(cpulist, PyInt_FromLong(i)); cpumap >>= 1; } PyDict_SetItemString(info_dict, "cpumap", cpulist); Py_DECREF(cpulist); return info_dict;}static PyObject *pyxc_linux_build(XcObject *self, PyObject *args, PyObject *kwds){ uint32_t domid; struct xc_dom_image *dom; char *image, *ramdisk = NULL, *cmdline = "", *features = NULL; int flags = 0; int store_evtchn, console_evtchn; int vhpt = 0; unsigned int mem_mb; unsigned long store_mfn = 0; unsigned long console_mfn = 0; PyObject* elfnote_dict; PyObject* elfnote = NULL; PyObject* ret; int i; static char *kwd_list[] = { "domid", "store_evtchn", "memsize", "console_evtchn", "image", /* optional */ "ramdisk", "cmdline", "flags", "features", "vhpt", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiis|ssisi", kwd_list, &domid, &store_evtchn, &mem_mb, &console_evtchn, &image, /* optional */ &ramdisk, &cmdline, &flags,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -