ia64/xen-unstable

view tools/python/xen/lowlevel/xc/xc.c @ 8158:e6d64c356d63

Added a pages_to_kib function, which converts a page count into a number of
KiB. This allows us to compute the correct amount to balloon out to support
a migration.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Dec 01 03:19:40 2005 +0000 (2005-12-01)
parents e33f8d664e2d
children d966b7a00959 f3d2e3659786
line source
1 /******************************************************************************
2 * Xc.c
3 *
4 * Copyright (c) 2003-2004, K A Fraser (University of Cambridge)
5 */
7 #include <Python.h>
8 #include <xenctrl.h>
9 #include <xenguest.h>
10 #include <zlib.h>
11 #include <fcntl.h>
12 #include <netinet/in.h>
13 #include <netinet/tcp.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netdb.h>
17 #include <arpa/inet.h>
19 #include "xc_private.h"
21 /* Needed for Python versions earlier than 2.3. */
22 #ifndef PyMODINIT_FUNC
23 #define PyMODINIT_FUNC DL_EXPORT(void)
24 #endif
26 #define PKG "xen.lowlevel.xc"
27 #define CLS "xc"
29 static PyObject *xc_error, *zero;
31 typedef struct {
32 PyObject_HEAD;
33 int xc_handle;
34 } XcObject;
37 static PyObject *dom_op(XcObject *self, PyObject *args,
38 int (*fn)(int, uint32_t));
41 static PyObject *pyxc_domain_dumpcore(XcObject *self, PyObject *args)
42 {
43 uint32_t dom;
44 char *corefile;
46 if (!PyArg_ParseTuple(args, "is", &dom, &corefile))
47 return NULL;
49 if ( (corefile == NULL) || (corefile[0] == '\0') )
50 return NULL;
52 if (xc_domain_dumpcore(self->xc_handle, dom, corefile) != 0)
53 return PyErr_SetFromErrno(xc_error);
55 Py_INCREF(zero);
56 return zero;
57 }
59 static PyObject *pyxc_handle(XcObject *self)
60 {
61 return PyInt_FromLong(self->xc_handle);
62 }
64 static PyObject *pyxc_domain_create(XcObject *self,
65 PyObject *args,
66 PyObject *kwds)
67 {
68 uint32_t dom = 0;
69 int ret, i;
70 uint32_t ssidref = 0;
71 PyObject *pyhandle = NULL;
72 xen_domain_handle_t handle = {
73 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
74 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef };
76 static char *kwd_list[] = { "dom", "ssidref", "handle", NULL };
78 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiO", kwd_list,
79 &dom, &ssidref, &pyhandle))
80 return NULL;
82 if ( pyhandle != NULL )
83 {
84 if ( !PyList_Check(pyhandle) ||
85 (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) )
86 goto out_exception;
88 for ( i = 0; i < sizeof(xen_domain_handle_t); i++ )
89 {
90 PyObject *p = PyList_GetItem(pyhandle, i);
91 if ( !PyInt_Check(p) )
92 goto out_exception;
93 handle[i] = (uint8_t)PyInt_AsLong(p);
94 }
95 }
97 if ( (ret = xc_domain_create(self->xc_handle, ssidref, handle, &dom)) < 0 )
98 return PyErr_SetFromErrno(xc_error);
100 return PyInt_FromLong(dom);
102 out_exception:
103 errno = EINVAL;
104 PyErr_SetFromErrno(xc_error);
105 return NULL;
106 }
108 static PyObject *pyxc_domain_max_vcpus(XcObject *self, PyObject *args)
109 {
110 uint32_t dom, max;
112 if (!PyArg_ParseTuple(args, "ii", &dom, &max))
113 return NULL;
115 if (xc_domain_max_vcpus(self->xc_handle, dom, max) != 0)
116 return PyErr_SetFromErrno(xc_error);
118 Py_INCREF(zero);
119 return zero;
120 }
122 static PyObject *pyxc_domain_pause(XcObject *self, PyObject *args)
123 {
124 return dom_op(self, args, xc_domain_pause);
125 }
127 static PyObject *pyxc_domain_unpause(XcObject *self, PyObject *args)
128 {
129 return dom_op(self, args, xc_domain_unpause);
130 }
132 static PyObject *pyxc_domain_destroy(XcObject *self, PyObject *args)
133 {
134 return dom_op(self, args, xc_domain_destroy);
135 }
138 static PyObject *pyxc_domain_pincpu(XcObject *self,
139 PyObject *args,
140 PyObject *kwds)
141 {
142 uint32_t dom;
143 int vcpu = 0, i;
144 cpumap_t cpumap = ~0ULL;
145 PyObject *cpulist = NULL;
147 static char *kwd_list[] = { "dom", "vcpu", "cpumap", NULL };
149 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|iO", kwd_list,
150 &dom, &vcpu, &cpulist) )
151 return NULL;
153 if ( (cpulist != NULL) && PyList_Check(cpulist) )
154 {
155 cpumap = 0ULL;
156 for ( i = 0; i < PyList_Size(cpulist); i++ )
157 cpumap |= (cpumap_t)1 << PyInt_AsLong(PyList_GetItem(cpulist, i));
158 }
160 if ( xc_domain_pincpu(self->xc_handle, dom, vcpu, cpumap) != 0 )
161 return PyErr_SetFromErrno(xc_error);
163 Py_INCREF(zero);
164 return zero;
165 }
167 static PyObject *pyxc_domain_setcpuweight(XcObject *self,
168 PyObject *args,
169 PyObject *kwds)
170 {
171 uint32_t dom;
172 float cpuweight = 1;
174 static char *kwd_list[] = { "dom", "cpuweight", NULL };
176 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|f", kwd_list,
177 &dom, &cpuweight) )
178 return NULL;
180 if ( xc_domain_setcpuweight(self->xc_handle, dom, cpuweight) != 0 )
181 return PyErr_SetFromErrno(xc_error);
183 Py_INCREF(zero);
184 return zero;
185 }
187 static PyObject *pyxc_domain_sethandle(XcObject *self, PyObject *args)
188 {
189 int i;
190 uint32_t dom;
191 PyObject *pyhandle;
192 xen_domain_handle_t handle;
194 if (!PyArg_ParseTuple(args, "iO", &dom, &pyhandle))
195 return NULL;
197 if ( !PyList_Check(pyhandle) ||
198 (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) )
199 {
200 goto out_exception;
201 }
203 for ( i = 0; i < sizeof(xen_domain_handle_t); i++ )
204 {
205 PyObject *p = PyList_GetItem(pyhandle, i);
206 if ( !PyInt_Check(p) )
207 goto out_exception;
208 handle[i] = (uint8_t)PyInt_AsLong(p);
209 }
211 if (xc_domain_sethandle(self->xc_handle, dom, handle) < 0)
212 return PyErr_SetFromErrno(xc_error);
214 Py_INCREF(zero);
215 return zero;
217 out_exception:
218 errno = EINVAL;
219 PyErr_SetFromErrno(xc_error);
220 return NULL;
221 }
224 static PyObject *pyxc_domain_getinfo(XcObject *self,
225 PyObject *args,
226 PyObject *kwds)
227 {
228 PyObject *list, *info_dict;
230 uint32_t first_dom = 0;
231 int max_doms = 1024, nr_doms, i, j;
232 xc_dominfo_t *info;
234 static char *kwd_list[] = { "first_dom", "max_doms", NULL };
236 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list,
237 &first_dom, &max_doms) )
238 return NULL;
240 if ( (info = malloc(max_doms * sizeof(xc_dominfo_t))) == NULL )
241 return PyErr_NoMemory();
243 nr_doms = xc_domain_getinfo(self->xc_handle, first_dom, max_doms, info);
245 if (nr_doms < 0)
246 {
247 free(info);
248 return PyErr_SetFromErrno(xc_error);
249 }
251 list = PyList_New(nr_doms);
252 for ( i = 0 ; i < nr_doms; i++ )
253 {
254 PyObject *pyhandle = PyList_New(sizeof(xen_domain_handle_t));
255 for ( j = 0; j < sizeof(xen_domain_handle_t); j++ )
256 PyList_SetItem(pyhandle, j, PyInt_FromLong(info[i].handle[j]));
257 info_dict = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i"
258 ",s:l,s:L,s:l,s:i,s:i}",
259 "dom", info[i].domid,
260 "online_vcpus", info[i].nr_online_vcpus,
261 "max_vcpu_id", info[i].max_vcpu_id,
262 "dying", info[i].dying,
263 "crashed", info[i].crashed,
264 "shutdown", info[i].shutdown,
265 "paused", info[i].paused,
266 "blocked", info[i].blocked,
267 "running", info[i].running,
268 "mem_kb", info[i].nr_pages*(XC_PAGE_SIZE/1024),
269 "cpu_time", info[i].cpu_time,
270 "maxmem_kb", info[i].max_memkb,
271 "ssidref", info[i].ssidref,
272 "shutdown_reason", info[i].shutdown_reason);
273 PyDict_SetItemString(info_dict, "handle", pyhandle);
274 Py_DECREF(pyhandle);
275 PyList_SetItem(list, i, info_dict);
276 }
278 free(info);
280 return list;
281 }
283 static PyObject *pyxc_vcpu_getinfo(XcObject *self,
284 PyObject *args,
285 PyObject *kwds)
286 {
287 PyObject *info_dict, *cpulist;
289 uint32_t dom, vcpu = 0;
290 xc_vcpuinfo_t info;
291 int rc, i;
292 cpumap_t cpumap;
294 static char *kwd_list[] = { "dom", "vcpu", NULL };
296 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
297 &dom, &vcpu) )
298 return NULL;
300 rc = xc_domain_get_vcpu_info(self->xc_handle, dom, vcpu, &info);
301 if ( rc < 0 )
302 return PyErr_SetFromErrno(xc_error);
304 info_dict = Py_BuildValue("{s:i,s:i,s:i,s:L,s:i}",
305 "online", info.online,
306 "blocked", info.blocked,
307 "running", info.running,
308 "cpu_time", info.cpu_time,
309 "cpu", info.cpu);
311 cpumap = info.cpumap;
312 cpulist = PyList_New(0);
313 for ( i = 0; cpumap != 0; i++ )
314 {
315 if ( cpumap & 1 )
316 PyList_Append(cpulist, PyInt_FromLong(i));
317 cpumap >>= 1;
318 }
319 PyDict_SetItemString(info_dict, "cpumap", cpulist);
320 Py_DECREF(cpulist);
321 return info_dict;
322 }
324 static PyObject *pyxc_linux_build(XcObject *self,
325 PyObject *args,
326 PyObject *kwds)
327 {
328 uint32_t dom;
329 char *image, *ramdisk = NULL, *cmdline = "";
330 int flags = 0;
331 int store_evtchn, console_evtchn;
332 unsigned long store_mfn = 0;
333 unsigned long console_mfn = 0;
335 static char *kwd_list[] = { "dom", "store_evtchn",
336 "console_evtchn", "image",
337 /* optional */
338 "ramdisk", "cmdline", "flags", NULL };
340 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiis|ssi", kwd_list,
341 &dom, &store_evtchn,
342 &console_evtchn, &image,
343 /* optional */
344 &ramdisk, &cmdline, &flags) )
345 return NULL;
347 if ( xc_linux_build(self->xc_handle, dom, image,
348 ramdisk, cmdline, flags,
349 store_evtchn, &store_mfn,
350 console_evtchn, &console_mfn) != 0 )
351 return PyErr_SetFromErrno(xc_error);
353 return Py_BuildValue("{s:i,s:i}",
354 "store_mfn", store_mfn,
355 "console_mfn", console_mfn);
356 }
358 static PyObject *pyxc_vmx_build(XcObject *self,
359 PyObject *args,
360 PyObject *kwds)
361 {
362 uint32_t dom;
363 char *image;
364 int control_evtchn, store_evtchn;
365 int vcpus = 1;
366 int lapic = 0;
367 int memsize;
368 unsigned long store_mfn = 0;
370 static char *kwd_list[] = { "dom", "control_evtchn", "store_evtchn",
371 "memsize", "image", "lapic", "vcpus", NULL };
373 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiisii", kwd_list,
374 &dom, &control_evtchn, &store_evtchn,
375 &memsize, &image, &lapic, &vcpus) )
376 return NULL;
378 if ( xc_vmx_build(self->xc_handle, dom, memsize, image, control_evtchn,
379 lapic, vcpus, store_evtchn, &store_mfn) != 0 )
380 return PyErr_SetFromErrno(xc_error);
382 return Py_BuildValue("{s:i}", "store_mfn", store_mfn);
383 }
385 static PyObject *pyxc_bvtsched_global_set(XcObject *self, PyObject *args)
386 {
387 unsigned long ctx_allow;
389 if (!PyArg_ParseTuple(args, "l", &ctx_allow))
390 return NULL;
392 if (xc_bvtsched_global_set(self->xc_handle, ctx_allow) != 0)
393 return PyErr_SetFromErrno(xc_error);
395 Py_INCREF(zero);
396 return zero;
397 }
399 static PyObject *pyxc_bvtsched_global_get(XcObject *self)
400 {
401 unsigned long ctx_allow;
403 if (xc_bvtsched_global_get(self->xc_handle, &ctx_allow) != 0)
404 return PyErr_SetFromErrno(xc_error);
406 return Py_BuildValue("s:l", "ctx_allow", ctx_allow);
407 }
409 static PyObject *pyxc_bvtsched_domain_set(XcObject *self,
410 PyObject *args,
411 PyObject *kwds)
412 {
413 uint32_t dom;
414 uint32_t mcuadv;
415 int warpback;
416 int32_t warpvalue;
417 long long warpl;
418 long long warpu;
420 static char *kwd_list[] = { "dom", "mcuadv", "warpback", "warpvalue",
421 "warpl", "warpu", NULL };
423 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiiLL", kwd_list,
424 &dom, &mcuadv, &warpback, &warpvalue,
425 &warpl, &warpu) )
426 return NULL;
428 if ( xc_bvtsched_domain_set(self->xc_handle, dom, mcuadv,
429 warpback, warpvalue, warpl, warpu) != 0 )
430 return PyErr_SetFromErrno(xc_error);
432 Py_INCREF(zero);
433 return zero;
434 }
436 static PyObject *pyxc_bvtsched_domain_get(XcObject *self,
437 PyObject *args)
438 {
439 uint32_t dom;
440 uint32_t mcuadv;
441 int warpback;
442 int32_t warpvalue;
443 long long warpl;
444 long long warpu;
446 if (!PyArg_ParseTuple(args, "i", &dom))
447 return NULL;
449 if (xc_bvtsched_domain_get(self->xc_handle, dom, &mcuadv, &warpback,
450 &warpvalue, &warpl, &warpu) != 0)
451 return PyErr_SetFromErrno(xc_error);
453 return Py_BuildValue("{s:i,s:l,s:l,s:l,s:l}",
454 "domain", dom,
455 "mcuadv", mcuadv,
456 "warpback", warpback,
457 "warpvalue", warpvalue,
458 "warpl", warpl,
459 "warpu", warpu);
460 }
462 static PyObject *pyxc_evtchn_alloc_unbound(XcObject *self,
463 PyObject *args,
464 PyObject *kwds)
465 {
466 uint32_t dom, remote_dom;
467 int port;
469 static char *kwd_list[] = { "dom", "remote_dom", NULL };
471 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
472 &dom, &remote_dom) )
473 return NULL;
475 if ( (port = xc_evtchn_alloc_unbound(self->xc_handle, dom, remote_dom)) < 0 )
476 return PyErr_SetFromErrno(xc_error);
478 return PyInt_FromLong(port);
479 }
481 static PyObject *pyxc_evtchn_status(XcObject *self,
482 PyObject *args,
483 PyObject *kwds)
484 {
485 PyObject *dict;
487 uint32_t dom = DOMID_SELF;
488 int port, ret;
489 xc_evtchn_status_t status;
491 static char *kwd_list[] = { "port", "dom", NULL };
493 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
494 &port, &dom) )
495 return NULL;
497 ret = xc_evtchn_status(self->xc_handle, dom, port, &status);
498 if ( ret != 0 )
499 return PyErr_SetFromErrno(xc_error);
501 switch ( status.status )
502 {
503 case EVTCHNSTAT_closed:
504 dict = Py_BuildValue("{s:s}",
505 "status", "closed");
506 break;
507 case EVTCHNSTAT_unbound:
508 dict = Py_BuildValue("{s:s}",
509 "status", "unbound");
510 break;
511 case EVTCHNSTAT_interdomain:
512 dict = Py_BuildValue("{s:s,s:i,s:i}",
513 "status", "interdomain",
514 "dom", status.u.interdomain.dom,
515 "port", status.u.interdomain.port);
516 break;
517 case EVTCHNSTAT_pirq:
518 dict = Py_BuildValue("{s:s,s:i}",
519 "status", "pirq",
520 "irq", status.u.pirq);
521 break;
522 case EVTCHNSTAT_virq:
523 dict = Py_BuildValue("{s:s,s:i}",
524 "status", "virq",
525 "irq", status.u.virq);
526 break;
527 default:
528 dict = Py_BuildValue("{}");
529 break;
530 }
532 return dict;
533 }
535 static PyObject *pyxc_physdev_pci_access_modify(XcObject *self,
536 PyObject *args,
537 PyObject *kwds)
538 {
539 uint32_t dom;
540 int bus, dev, func, enable, ret;
542 static char *kwd_list[] = { "dom", "bus", "dev", "func", "enable", NULL };
544 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
545 &dom, &bus, &dev, &func, &enable) )
546 return NULL;
548 ret = xc_physdev_pci_access_modify(
549 self->xc_handle, dom, bus, dev, func, enable);
550 if ( ret != 0 )
551 return PyErr_SetFromErrno(xc_error);
553 Py_INCREF(zero);
554 return zero;
555 }
557 static PyObject *pyxc_readconsolering(XcObject *self,
558 PyObject *args,
559 PyObject *kwds)
560 {
561 unsigned int clear = 0;
562 char _str[32768], *str = _str;
563 unsigned int count = 32768;
564 int ret;
566 static char *kwd_list[] = { "clear", NULL };
568 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
569 return NULL;
571 ret = xc_readconsolering(self->xc_handle, &str, &count, clear);
572 if ( ret < 0 )
573 return PyErr_SetFromErrno(xc_error);
575 return PyString_FromStringAndSize(str, count);
576 }
579 static PyObject *pyxc_pages_to_kib(XcObject *self, PyObject *args)
580 {
581 unsigned long pages;
583 if (!PyArg_ParseTuple(args, "l", &pages))
584 return NULL;
586 return PyLong_FromUnsignedLong(pages * (XC_PAGE_SIZE / 1024));
587 }
590 static unsigned long pages_to_mb(unsigned long pages)
591 {
592 return (pages * (XC_PAGE_SIZE / 1024) + 1023) / 1024;
593 }
596 static PyObject *pyxc_physinfo(XcObject *self)
597 {
598 xc_physinfo_t info;
599 char cpu_cap[128], *p=cpu_cap, *q=cpu_cap;
600 int i;
602 if ( xc_physinfo(self->xc_handle, &info) != 0 )
603 return PyErr_SetFromErrno(xc_error);
605 *q=0;
606 for(i=0;i<sizeof(info.hw_cap)/4;i++)
607 {
608 p+=sprintf(p,"%08x:",info.hw_cap[i]);
609 if(info.hw_cap[i])
610 q=p;
611 }
612 if(q>cpu_cap)
613 *(q-1)=0;
615 return Py_BuildValue("{s:i,s:i,s:i,s:i,s:l,s:l,s:i,s:s}",
616 "threads_per_core", info.threads_per_core,
617 "cores_per_socket", info.cores_per_socket,
618 "sockets_per_node", info.sockets_per_node,
619 "nr_nodes", info.nr_nodes,
620 "total_memory", pages_to_mb(info.total_pages),
621 "free_memory", pages_to_mb(info.free_pages),
622 "cpu_khz", info.cpu_khz,
623 "hw_caps", cpu_cap);
624 }
626 static PyObject *pyxc_xeninfo(XcObject *self)
627 {
628 xen_extraversion_t xen_extra;
629 xen_compile_info_t xen_cc;
630 xen_changeset_info_t xen_chgset;
631 xen_capabilities_info_t xen_caps;
632 xen_platform_parameters_t p_parms;
633 long xen_version;
634 char str[128];
636 xen_version = xc_version(self->xc_handle, XENVER_version, NULL);
638 if ( xc_version(self->xc_handle, XENVER_extraversion, &xen_extra) != 0 )
639 return PyErr_SetFromErrno(xc_error);
641 if ( xc_version(self->xc_handle, XENVER_compile_info, &xen_cc) != 0 )
642 return PyErr_SetFromErrno(xc_error);
644 if ( xc_version(self->xc_handle, XENVER_changeset, &xen_chgset) != 0 )
645 return PyErr_SetFromErrno(xc_error);
647 if ( xc_version(self->xc_handle, XENVER_capabilities, &xen_caps) != 0 )
648 return PyErr_SetFromErrno(xc_error);
650 if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
651 return PyErr_SetFromErrno(xc_error);
653 sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
655 return Py_BuildValue("{s:i,s:i,s:s,s:s,s:s,s:s,s:s,s:s,s:s,s:s}",
656 "xen_major", xen_version >> 16,
657 "xen_minor", (xen_version & 0xffff),
658 "xen_extra", xen_extra,
659 "xen_caps", xen_caps,
660 "platform_params", str,
661 "xen_changeset", xen_chgset,
662 "cc_compiler", xen_cc.compiler,
663 "cc_compile_by", xen_cc.compile_by,
664 "cc_compile_domain", xen_cc.compile_domain,
665 "cc_compile_date", xen_cc.compile_date);
666 }
669 static PyObject *pyxc_sedf_domain_set(XcObject *self,
670 PyObject *args,
671 PyObject *kwds)
672 {
673 uint32_t domid;
674 uint64_t period, slice, latency;
675 uint16_t extratime, weight;
676 static char *kwd_list[] = { "dom", "period", "slice",
677 "latency", "extratime", "weight",NULL };
679 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLhh", kwd_list,
680 &domid, &period, &slice,
681 &latency, &extratime, &weight) )
682 return NULL;
683 if ( xc_sedf_domain_set(self->xc_handle, domid, period,
684 slice, latency, extratime,weight) != 0 )
685 return PyErr_SetFromErrno(xc_error);
687 Py_INCREF(zero);
688 return zero;
689 }
691 static PyObject *pyxc_sedf_domain_get(XcObject *self, PyObject *args)
692 {
693 uint32_t domid;
694 uint64_t period, slice,latency;
695 uint16_t weight, extratime;
697 if(!PyArg_ParseTuple(args, "i", &domid))
698 return NULL;
700 if (xc_sedf_domain_get(self->xc_handle, domid, &period,
701 &slice,&latency,&extratime,&weight))
702 return PyErr_SetFromErrno(xc_error);
704 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i,s:i}",
705 "domain", domid,
706 "period", period,
707 "slice", slice,
708 "latency", latency,
709 "extratime", extratime,
710 "weight", weight);
711 }
713 static PyObject *pyxc_domain_setmaxmem(XcObject *self, PyObject *args)
714 {
715 uint32_t dom;
716 unsigned int maxmem_kb;
718 if (!PyArg_ParseTuple(args, "ii", &dom, &maxmem_kb))
719 return NULL;
721 if (xc_domain_setmaxmem(self->xc_handle, dom, maxmem_kb) != 0)
722 return PyErr_SetFromErrno(xc_error);
724 Py_INCREF(zero);
725 return zero;
726 }
728 static PyObject *pyxc_domain_memory_increase_reservation(XcObject *self,
729 PyObject *args,
730 PyObject *kwds)
731 {
732 uint32_t dom;
733 unsigned long mem_kb;
734 unsigned int extent_order = 0 , address_bits = 0;
735 unsigned long nr_extents;
737 static char *kwd_list[] = { "dom", "mem_kb", "extent_order", "address_bits", NULL };
739 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "il|ii", kwd_list,
740 &dom, &mem_kb, &extent_order, &address_bits) )
741 return NULL;
743 /* round down to nearest power of 2. Assume callers using extent_order>0
744 know what they are doing */
745 nr_extents = (mem_kb / (XC_PAGE_SIZE/1024)) >> extent_order;
746 if ( xc_domain_memory_increase_reservation(self->xc_handle, dom,
747 nr_extents, extent_order,
748 address_bits, NULL) )
749 return PyErr_SetFromErrno(xc_error);
751 Py_INCREF(zero);
752 return zero;
753 }
755 static PyObject *pyxc_domain_ioport_permission(XcObject *self,
756 PyObject *args,
757 PyObject *kwds)
758 {
759 uint32_t dom;
760 int first_port, nr_ports, allow_access, ret;
762 static char *kwd_list[] = { "dom", "first_port", "nr_ports", "allow_access", NULL };
764 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiii", kwd_list,
765 &dom, &first_port, &nr_ports, &allow_access) )
766 return NULL;
768 ret = xc_domain_ioport_permission(
769 self->xc_handle, dom, first_port, nr_ports, allow_access);
770 if ( ret != 0 )
771 return PyErr_SetFromErrno(xc_error);
773 Py_INCREF(zero);
774 return zero;
775 }
778 static PyObject *dom_op(XcObject *self, PyObject *args,
779 int (*fn)(int, uint32_t))
780 {
781 uint32_t dom;
783 if (!PyArg_ParseTuple(args, "i", &dom))
784 return NULL;
786 if (fn(self->xc_handle, dom) != 0)
787 return PyErr_SetFromErrno(xc_error);
789 Py_INCREF(zero);
790 return zero;
791 }
794 static PyMethodDef pyxc_methods[] = {
795 { "handle",
796 (PyCFunction)pyxc_handle,
797 METH_NOARGS, "\n"
798 "Query the xc control interface file descriptor.\n\n"
799 "Returns: [int] file descriptor\n" },
801 { "domain_create",
802 (PyCFunction)pyxc_domain_create,
803 METH_VARARGS | METH_KEYWORDS, "\n"
804 "Create a new domain.\n"
805 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
806 "Returns: [int] new domain identifier; -1 on error.\n" },
808 { "domain_max_vcpus",
809 (PyCFunction)pyxc_domain_max_vcpus,
810 METH_VARARGS, "\n"
811 "Set the maximum number of VCPUs a domain may create.\n"
812 " dom [int, 0]: Domain identifier to use.\n"
813 " max [int, 0]: New maximum number of VCPUs in domain.\n"
814 "Returns: [int] 0 on success; -1 on error.\n" },
816 { "domain_dumpcore",
817 (PyCFunction)pyxc_domain_dumpcore,
818 METH_VARARGS, "\n"
819 "Dump core of a domain.\n"
820 " dom [int]: Identifier of domain to dump core of.\n"
821 " corefile [string]: Name of corefile to be created.\n\n"
822 "Returns: [int] 0 on success; -1 on error.\n" },
824 { "domain_pause",
825 (PyCFunction)pyxc_domain_pause,
826 METH_VARARGS, "\n"
827 "Temporarily pause execution of a domain.\n"
828 " dom [int]: Identifier of domain to be paused.\n\n"
829 "Returns: [int] 0 on success; -1 on error.\n" },
831 { "domain_unpause",
832 (PyCFunction)pyxc_domain_unpause,
833 METH_VARARGS, "\n"
834 "(Re)start execution of a domain.\n"
835 " dom [int]: Identifier of domain to be unpaused.\n\n"
836 "Returns: [int] 0 on success; -1 on error.\n" },
838 { "domain_destroy",
839 (PyCFunction)pyxc_domain_destroy,
840 METH_VARARGS, "\n"
841 "Destroy a domain.\n"
842 " dom [int]: Identifier of domain to be destroyed.\n\n"
843 "Returns: [int] 0 on success; -1 on error.\n" },
845 { "domain_pincpu",
846 (PyCFunction)pyxc_domain_pincpu,
847 METH_VARARGS | METH_KEYWORDS, "\n"
848 "Pin a VCPU to a specified set CPUs.\n"
849 " dom [int]: Identifier of domain to which VCPU belongs.\n"
850 " vcpu [int, 0]: VCPU being pinned.\n"
851 " cpumap [list, []]: list of usable CPUs.\n\n"
852 "Returns: [int] 0 on success; -1 on error.\n" },
854 { "domain_setcpuweight",
855 (PyCFunction)pyxc_domain_setcpuweight,
856 METH_VARARGS | METH_KEYWORDS, "\n"
857 "Set cpuweight scheduler parameter for domain.\n"
858 " dom [int]: Identifier of domain to be changed.\n"
859 " cpuweight [float, 1]: VCPU being pinned.\n"
860 "Returns: [int] 0 on success; -1 on error.\n" },
862 { "domain_sethandle",
863 (PyCFunction)pyxc_domain_sethandle,
864 METH_VARARGS, "\n"
865 "Set domain's opaque handle.\n"
866 " dom [int]: Identifier of domain.\n"
867 " handle [list of 16 ints]: New opaque handle.\n"
868 "Returns: [int] 0 on success; -1 on error.\n" },
870 { "domain_getinfo",
871 (PyCFunction)pyxc_domain_getinfo,
872 METH_VARARGS | METH_KEYWORDS, "\n"
873 "Get information regarding a set of domains, in increasing id order.\n"
874 " first_dom [int, 0]: First domain to retrieve info about.\n"
875 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
876 " about.\n\n"
877 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
878 " parameter then there was an error, or the end of the\n"
879 " domain-id space was reached.\n"
880 " dom [int]: Identifier of domain to which this info pertains\n"
881 " cpu [int]: CPU to which this domain is bound\n"
882 " vcpus [int]: Number of Virtual CPUS in this domain\n"
883 " dying [int]: Bool - is the domain dying?\n"
884 " crashed [int]: Bool - has the domain crashed?\n"
885 " shutdown [int]: Bool - has the domain shut itself down?\n"
886 " paused [int]: Bool - is the domain paused by control software?\n"
887 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
888 " running [int]: Bool - is the domain currently running?\n"
889 " mem_kb [int]: Memory reservation, in kilobytes\n"
890 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
891 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
892 " shutdown_reason [int]: Numeric code from guest OS, explaining "
893 "reason why it shut itself down.\n" },
895 { "vcpu_getinfo",
896 (PyCFunction)pyxc_vcpu_getinfo,
897 METH_VARARGS | METH_KEYWORDS, "\n"
898 "Get information regarding a VCPU.\n"
899 " dom [int]: Domain to retrieve info about.\n"
900 " vcpu [int, 0]: VCPU to retrieve info about.\n\n"
901 "Returns: [dict]\n"
902 " online [int]: Bool - Is this VCPU currently online?\n"
903 " blocked [int]: Bool - Is this VCPU blocked waiting for an event?\n"
904 " running [int]: Bool - Is this VCPU currently running on a CPU?\n"
905 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
906 " cpumap [int]: Bitmap of CPUs this VCPU can run on\n"
907 " cpu [int]: CPU that this VCPU is currently bound to\n" },
909 { "linux_build",
910 (PyCFunction)pyxc_linux_build,
911 METH_VARARGS | METH_KEYWORDS, "\n"
912 "Build a new Linux guest OS.\n"
913 " dom [int]: Identifier of domain to build into.\n"
914 " image [str]: Name of kernel image file. May be gzipped.\n"
915 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
916 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
917 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
918 "Returns: [int] 0 on success; -1 on error.\n" },
920 { "vmx_build",
921 (PyCFunction)pyxc_vmx_build,
922 METH_VARARGS | METH_KEYWORDS, "\n"
923 "Build a new VMX guest OS.\n"
924 " dom [int]: Identifier of domain to build into.\n"
925 " image [str]: Name of VMX loader image file.\n"
926 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
927 "Returns: [int] 0 on success; -1 on error.\n" },
929 { "bvtsched_global_set",
930 (PyCFunction)pyxc_bvtsched_global_set,
931 METH_VARARGS | METH_KEYWORDS, "\n"
932 "Set global tuning parameters for Borrowed Virtual Time scheduler.\n"
933 " ctx_allow [int]: Minimal guaranteed quantum.\n\n"
934 "Returns: [int] 0 on success; -1 on error.\n" },
936 { "bvtsched_global_get",
937 (PyCFunction)pyxc_bvtsched_global_get,
938 METH_NOARGS, "\n"
939 "Get global tuning parameters for BVT scheduler.\n"
940 "Returns: [dict]:\n"
941 " ctx_allow [int]: context switch allowance\n" },
943 { "bvtsched_domain_set",
944 (PyCFunction)pyxc_bvtsched_domain_set,
945 METH_VARARGS | METH_KEYWORDS, "\n"
946 "Set per-domain tuning parameters for Borrowed Virtual Time scheduler.\n"
947 " dom [int]: Identifier of domain to be tuned.\n"
948 " mcuadv [int]: Proportional to the inverse of the domain's weight.\n"
949 " warpback [int]: Warp ? \n"
950 " warpvalue [int]: How far to warp domain's EVT on unblock.\n"
951 " warpl [int]: How long the domain can run warped.\n"
952 " warpu [int]: How long before the domain can warp again.\n\n"
953 "Returns: [int] 0 on success; -1 on error.\n" },
955 { "bvtsched_domain_get",
956 (PyCFunction)pyxc_bvtsched_domain_get,
957 METH_VARARGS, "\n"
958 "Get per-domain tuning parameters under the BVT scheduler.\n"
959 " dom [int]: Identifier of domain to be queried.\n"
960 "Returns [dict]:\n"
961 " domain [int]: Domain ID.\n"
962 " mcuadv [long]: MCU Advance.\n"
963 " warp [long]: Warp.\n"
964 " warpu [long]: Unwarp requirement.\n"
965 " warpl [long]: Warp limit,\n"
966 },
968 { "sedf_domain_set",
969 (PyCFunction)pyxc_sedf_domain_set,
970 METH_KEYWORDS, "\n"
971 "Set the scheduling parameters for a domain when running with Atropos.\n"
972 " dom [int]: domain to set\n"
973 " period [long]: domain's scheduling period\n"
974 " slice [long]: domain's slice per period\n"
975 " latency [long]: domain's wakeup latency hint\n"
976 " extratime [int]: domain aware of extratime?\n"
977 "Returns: [int] 0 on success; -1 on error.\n" },
979 { "sedf_domain_get",
980 (PyCFunction)pyxc_sedf_domain_get,
981 METH_VARARGS, "\n"
982 "Get the current scheduling parameters for a domain when running with\n"
983 "the Atropos scheduler."
984 " dom [int]: domain to query\n"
985 "Returns: [dict]\n"
986 " domain [int]: domain ID\n"
987 " period [long]: scheduler period\n"
988 " slice [long]: CPU reservation per period\n"
989 " latency [long]: domain's wakeup latency hint\n"
990 " extratime [int]: domain aware of extratime?\n"},
992 { "evtchn_alloc_unbound",
993 (PyCFunction)pyxc_evtchn_alloc_unbound,
994 METH_VARARGS | METH_KEYWORDS, "\n"
995 "Allocate an unbound port that will await a remote connection.\n"
996 " dom [int]: Domain whose port space to allocate from.\n"
997 " remote_dom [int]: Remote domain to accept connections from.\n\n"
998 "Returns: [int] Unbound event-channel port.\n" },
1000 { "evtchn_status",
1001 (PyCFunction)pyxc_evtchn_status,
1002 METH_VARARGS | METH_KEYWORDS, "\n"
1003 "Query the status of an event channel.\n"
1004 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1005 " port [int]: Port-id of one endpoint of the channel.\n\n"
1006 "Returns: [dict] dictionary is empty on failure.\n"
1007 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
1008 " or 'virq'.\n"
1009 "The following are returned if 'status' is 'interdomain':\n"
1010 " dom [int]: Dom-id of remote endpoint.\n"
1011 " port [int]: Port-id of remote endpoint.\n"
1012 "The following are returned if 'status' is 'pirq' or 'virq':\n"
1013 " irq [int]: IRQ number.\n" },
1015 { "physdev_pci_access_modify",
1016 (PyCFunction)pyxc_physdev_pci_access_modify,
1017 METH_VARARGS | METH_KEYWORDS, "\n"
1018 "Allow a domain access to a PCI device\n"
1019 " dom [int]: Identifier of domain to be allowed access.\n"
1020 " bus [int]: PCI bus\n"
1021 " dev [int]: PCI slot\n"
1022 " func [int]: PCI function\n"
1023 " enable [int]: Non-zero means enable access; else disable access\n\n"
1024 "Returns: [int] 0 on success; -1 on error.\n" },
1026 { "readconsolering",
1027 (PyCFunction)pyxc_readconsolering,
1028 METH_VARARGS | METH_KEYWORDS, "\n"
1029 "Read Xen's console ring.\n"
1030 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1031 "Returns: [str] string is empty on failure.\n" },
1033 { "physinfo",
1034 (PyCFunction)pyxc_physinfo,
1035 METH_NOARGS, "\n"
1036 "Get information about the physical host machine\n"
1037 "Returns [dict]: information about the hardware"
1038 " [None]: on failure.\n" },
1040 { "xeninfo",
1041 (PyCFunction)pyxc_xeninfo,
1042 METH_NOARGS, "\n"
1043 "Get information about the Xen host\n"
1044 "Returns [dict]: information about Xen"
1045 " [None]: on failure.\n" },
1047 { "domain_setmaxmem",
1048 (PyCFunction)pyxc_domain_setmaxmem,
1049 METH_VARARGS, "\n"
1050 "Set a domain's memory limit\n"
1051 " dom [int]: Identifier of domain.\n"
1052 " maxmem_kb [int]: .\n"
1053 "Returns: [int] 0 on success; -1 on error.\n" },
1055 { "domain_memory_increase_reservation",
1056 (PyCFunction)pyxc_domain_memory_increase_reservation,
1057 METH_VARARGS | METH_KEYWORDS, "\n"
1058 "Increase a domain's memory reservation\n"
1059 " dom [int]: Identifier of domain.\n"
1060 " mem_kb [long]: .\n"
1061 "Returns: [int] 0 on success; -1 on error.\n" },
1063 { "domain_ioport_permission",
1064 (PyCFunction)pyxc_domain_ioport_permission,
1065 METH_VARARGS | METH_KEYWORDS, "\n"
1066 "Allow a domain access to a range of IO ports\n"
1067 " dom [int]: Identifier of domain to be allowed access.\n"
1068 " first_port [int]: First IO port\n"
1069 " nr_ports [int]: Number of IO ports\n"
1070 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1071 "Returns: [int] 0 on success; -1 on error.\n" },
1073 { "pages_to_kib",
1074 (PyCFunction)pyxc_pages_to_kib,
1075 METH_VARARGS, "\n"
1076 "Returns: [int]: The size in KiB of memory spanning the given number "
1077 "of pages.\n" },
1079 { NULL, NULL, 0, NULL }
1080 };
1083 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1085 return Py_FindMethod(pyxc_methods, obj, name);
1088 static PyObject *PyXc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1090 XcObject *self = (XcObject *)type->tp_alloc(type, 0);
1092 if (self == NULL)
1093 return NULL;
1095 self->xc_handle = -1;
1097 return (PyObject *)self;
1100 static int
1101 PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
1103 if ((self->xc_handle = xc_interface_open()) == -1) {
1104 PyErr_SetFromErrno(PyExc_RuntimeError);
1105 return -1;
1108 return 0;
1111 static void PyXc_dealloc(XcObject *self)
1113 if (self->xc_handle != -1) {
1114 xc_interface_close(self->xc_handle);
1115 self->xc_handle = -1;
1118 self->ob_type->tp_free((PyObject *)self);
1121 static PyTypeObject PyXcType = {
1122 PyObject_HEAD_INIT(NULL)
1123 0,
1124 PKG "." CLS,
1125 sizeof(XcObject),
1126 0,
1127 (destructor)PyXc_dealloc, /* tp_dealloc */
1128 NULL, /* tp_print */
1129 PyXc_getattr, /* tp_getattr */
1130 NULL, /* tp_setattr */
1131 NULL, /* tp_compare */
1132 NULL, /* tp_repr */
1133 NULL, /* tp_as_number */
1134 NULL, /* tp_as_sequence */
1135 NULL, /* tp_as_mapping */
1136 NULL, /* tp_hash */
1137 NULL, /* tp_call */
1138 NULL, /* tp_str */
1139 NULL, /* tp_getattro */
1140 NULL, /* tp_setattro */
1141 NULL, /* tp_as_buffer */
1142 Py_TPFLAGS_DEFAULT, /* tp_flags */
1143 "Xen client connections", /* tp_doc */
1144 NULL, /* tp_traverse */
1145 NULL, /* tp_clear */
1146 NULL, /* tp_richcompare */
1147 0, /* tp_weaklistoffset */
1148 NULL, /* tp_iter */
1149 NULL, /* tp_iternext */
1150 pyxc_methods, /* tp_methods */
1151 NULL, /* tp_members */
1152 NULL, /* tp_getset */
1153 NULL, /* tp_base */
1154 NULL, /* tp_dict */
1155 NULL, /* tp_descr_get */
1156 NULL, /* tp_descr_set */
1157 0, /* tp_dictoffset */
1158 (initproc)PyXc_init, /* tp_init */
1159 NULL, /* tp_alloc */
1160 PyXc_new, /* tp_new */
1161 };
1163 static PyMethodDef xc_methods[] = { { NULL } };
1165 PyMODINIT_FUNC initxc(void)
1167 PyObject *m;
1169 if (PyType_Ready(&PyXcType) < 0)
1170 return;
1172 m = Py_InitModule(PKG, xc_methods);
1174 if (m == NULL)
1175 return;
1177 xc_error = PyErr_NewException(PKG ".error", NULL, NULL);
1178 zero = PyInt_FromLong(0);
1180 /* KAF: This ensures that we get debug output in a timely manner. */
1181 setbuf(stdout, NULL);
1182 setbuf(stderr, NULL);
1184 Py_INCREF(&PyXcType);
1185 PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
1189 /*
1190 * Local variables:
1191 * c-indent-level: 4
1192 * c-basic-offset: 4
1193 * End:
1194 */