direct-io.hg

view tools/python/xen/lowlevel/xc/xc.c @ 8500:dd5649730b32

Fix a couple of bogus dom0_op names:
setdomaininfo -> setvcpucontext
pincpudomain -> setvcpuaffinity

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jan 06 12:53:19 2006 +0100 (2006-01-06)
parents 412995d28a07
children 8af1199488d3
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_vcpu_setaffinity(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_vcpu_setaffinity(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_vcpu_getinfo(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 memsize;
366 int vcpus = 1;
367 int acpi = 0;
368 int apic = 0;
369 unsigned long store_mfn = 0;
371 static char *kwd_list[] = { "dom", "control_evtchn", "store_evtchn",
372 "memsize", "image", "vcpus", "acpi", "apic",
373 NULL };
375 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiisiii", kwd_list,
376 &dom, &control_evtchn, &store_evtchn,
377 &memsize, &image, &vcpus, &acpi, &apic) )
378 return NULL;
380 if ( xc_vmx_build(self->xc_handle, dom, memsize, image, control_evtchn,
381 vcpus, acpi, apic, store_evtchn, &store_mfn) != 0 )
382 return PyErr_SetFromErrno(xc_error);
384 return Py_BuildValue("{s:i}", "store_mfn", store_mfn);
385 }
387 static PyObject *pyxc_bvtsched_global_set(XcObject *self, PyObject *args)
388 {
389 unsigned long ctx_allow;
391 if (!PyArg_ParseTuple(args, "l", &ctx_allow))
392 return NULL;
394 if (xc_bvtsched_global_set(self->xc_handle, ctx_allow) != 0)
395 return PyErr_SetFromErrno(xc_error);
397 Py_INCREF(zero);
398 return zero;
399 }
401 static PyObject *pyxc_bvtsched_global_get(XcObject *self)
402 {
403 unsigned long ctx_allow;
405 if (xc_bvtsched_global_get(self->xc_handle, &ctx_allow) != 0)
406 return PyErr_SetFromErrno(xc_error);
408 return Py_BuildValue("s:l", "ctx_allow", ctx_allow);
409 }
411 static PyObject *pyxc_bvtsched_domain_set(XcObject *self,
412 PyObject *args,
413 PyObject *kwds)
414 {
415 uint32_t dom;
416 uint32_t mcuadv;
417 int warpback;
418 int32_t warpvalue;
419 long long warpl;
420 long long warpu;
422 static char *kwd_list[] = { "dom", "mcuadv", "warpback", "warpvalue",
423 "warpl", "warpu", NULL };
425 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiiLL", kwd_list,
426 &dom, &mcuadv, &warpback, &warpvalue,
427 &warpl, &warpu) )
428 return NULL;
430 if ( xc_bvtsched_domain_set(self->xc_handle, dom, mcuadv,
431 warpback, warpvalue, warpl, warpu) != 0 )
432 return PyErr_SetFromErrno(xc_error);
434 Py_INCREF(zero);
435 return zero;
436 }
438 static PyObject *pyxc_bvtsched_domain_get(XcObject *self,
439 PyObject *args)
440 {
441 uint32_t dom;
442 uint32_t mcuadv;
443 int warpback;
444 int32_t warpvalue;
445 long long warpl;
446 long long warpu;
448 if (!PyArg_ParseTuple(args, "i", &dom))
449 return NULL;
451 if (xc_bvtsched_domain_get(self->xc_handle, dom, &mcuadv, &warpback,
452 &warpvalue, &warpl, &warpu) != 0)
453 return PyErr_SetFromErrno(xc_error);
455 return Py_BuildValue("{s:i,s:l,s:l,s:l,s:l}",
456 "domain", dom,
457 "mcuadv", mcuadv,
458 "warpback", warpback,
459 "warpvalue", warpvalue,
460 "warpl", warpl,
461 "warpu", warpu);
462 }
464 static PyObject *pyxc_evtchn_alloc_unbound(XcObject *self,
465 PyObject *args,
466 PyObject *kwds)
467 {
468 uint32_t dom, remote_dom;
469 int port;
471 static char *kwd_list[] = { "dom", "remote_dom", NULL };
473 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
474 &dom, &remote_dom) )
475 return NULL;
477 if ( (port = xc_evtchn_alloc_unbound(self->xc_handle, dom, remote_dom)) < 0 )
478 return PyErr_SetFromErrno(xc_error);
480 return PyInt_FromLong(port);
481 }
483 static PyObject *pyxc_evtchn_status(XcObject *self,
484 PyObject *args,
485 PyObject *kwds)
486 {
487 PyObject *dict;
489 uint32_t dom = DOMID_SELF;
490 int port, ret;
491 xc_evtchn_status_t status;
493 static char *kwd_list[] = { "port", "dom", NULL };
495 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
496 &port, &dom) )
497 return NULL;
499 ret = xc_evtchn_status(self->xc_handle, dom, port, &status);
500 if ( ret != 0 )
501 return PyErr_SetFromErrno(xc_error);
503 switch ( status.status )
504 {
505 case EVTCHNSTAT_closed:
506 dict = Py_BuildValue("{s:s}",
507 "status", "closed");
508 break;
509 case EVTCHNSTAT_unbound:
510 dict = Py_BuildValue("{s:s}",
511 "status", "unbound");
512 break;
513 case EVTCHNSTAT_interdomain:
514 dict = Py_BuildValue("{s:s,s:i,s:i}",
515 "status", "interdomain",
516 "dom", status.u.interdomain.dom,
517 "port", status.u.interdomain.port);
518 break;
519 case EVTCHNSTAT_pirq:
520 dict = Py_BuildValue("{s:s,s:i}",
521 "status", "pirq",
522 "irq", status.u.pirq);
523 break;
524 case EVTCHNSTAT_virq:
525 dict = Py_BuildValue("{s:s,s:i}",
526 "status", "virq",
527 "irq", status.u.virq);
528 break;
529 default:
530 dict = Py_BuildValue("{}");
531 break;
532 }
534 return dict;
535 }
537 static PyObject *pyxc_physdev_pci_access_modify(XcObject *self,
538 PyObject *args,
539 PyObject *kwds)
540 {
541 uint32_t dom;
542 int bus, dev, func, enable, ret;
544 static char *kwd_list[] = { "dom", "bus", "dev", "func", "enable", NULL };
546 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
547 &dom, &bus, &dev, &func, &enable) )
548 return NULL;
550 ret = xc_physdev_pci_access_modify(
551 self->xc_handle, dom, bus, dev, func, enable);
552 if ( ret != 0 )
553 return PyErr_SetFromErrno(xc_error);
555 Py_INCREF(zero);
556 return zero;
557 }
559 static PyObject *pyxc_readconsolering(XcObject *self,
560 PyObject *args,
561 PyObject *kwds)
562 {
563 unsigned int clear = 0;
564 char _str[32768], *str = _str;
565 unsigned int count = 32768;
566 int ret;
568 static char *kwd_list[] = { "clear", NULL };
570 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
571 return NULL;
573 ret = xc_readconsolering(self->xc_handle, &str, &count, clear);
574 if ( ret < 0 )
575 return PyErr_SetFromErrno(xc_error);
577 return PyString_FromStringAndSize(str, count);
578 }
581 static PyObject *pyxc_pages_to_kib(XcObject *self, PyObject *args)
582 {
583 unsigned long pages;
585 if (!PyArg_ParseTuple(args, "l", &pages))
586 return NULL;
588 return PyLong_FromUnsignedLong(pages * (XC_PAGE_SIZE / 1024));
589 }
592 static unsigned long pages_to_mb(unsigned long pages)
593 {
594 return (pages * (XC_PAGE_SIZE / 1024) + 1023) / 1024;
595 }
598 static PyObject *pyxc_physinfo(XcObject *self)
599 {
600 xc_physinfo_t info;
601 char cpu_cap[128], *p=cpu_cap, *q=cpu_cap;
602 int i;
604 if ( xc_physinfo(self->xc_handle, &info) != 0 )
605 return PyErr_SetFromErrno(xc_error);
607 *q=0;
608 for(i=0;i<sizeof(info.hw_cap)/4;i++)
609 {
610 p+=sprintf(p,"%08x:",info.hw_cap[i]);
611 if(info.hw_cap[i])
612 q=p;
613 }
614 if(q>cpu_cap)
615 *(q-1)=0;
617 return Py_BuildValue("{s:i,s:i,s:i,s:i,s:l,s:l,s:i,s:s}",
618 "threads_per_core", info.threads_per_core,
619 "cores_per_socket", info.cores_per_socket,
620 "sockets_per_node", info.sockets_per_node,
621 "nr_nodes", info.nr_nodes,
622 "total_memory", pages_to_mb(info.total_pages),
623 "free_memory", pages_to_mb(info.free_pages),
624 "cpu_khz", info.cpu_khz,
625 "hw_caps", cpu_cap);
626 }
628 static PyObject *pyxc_xeninfo(XcObject *self)
629 {
630 xen_extraversion_t xen_extra;
631 xen_compile_info_t xen_cc;
632 xen_changeset_info_t xen_chgset;
633 xen_capabilities_info_t xen_caps;
634 xen_platform_parameters_t p_parms;
635 long xen_version;
636 char str[128];
638 xen_version = xc_version(self->xc_handle, XENVER_version, NULL);
640 if ( xc_version(self->xc_handle, XENVER_extraversion, &xen_extra) != 0 )
641 return PyErr_SetFromErrno(xc_error);
643 if ( xc_version(self->xc_handle, XENVER_compile_info, &xen_cc) != 0 )
644 return PyErr_SetFromErrno(xc_error);
646 if ( xc_version(self->xc_handle, XENVER_changeset, &xen_chgset) != 0 )
647 return PyErr_SetFromErrno(xc_error);
649 if ( xc_version(self->xc_handle, XENVER_capabilities, &xen_caps) != 0 )
650 return PyErr_SetFromErrno(xc_error);
652 if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
653 return PyErr_SetFromErrno(xc_error);
655 sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
657 return Py_BuildValue("{s:i,s:i,s:s,s:s,s:s,s:s,s:s,s:s,s:s,s:s}",
658 "xen_major", xen_version >> 16,
659 "xen_minor", (xen_version & 0xffff),
660 "xen_extra", xen_extra,
661 "xen_caps", xen_caps,
662 "platform_params", str,
663 "xen_changeset", xen_chgset,
664 "cc_compiler", xen_cc.compiler,
665 "cc_compile_by", xen_cc.compile_by,
666 "cc_compile_domain", xen_cc.compile_domain,
667 "cc_compile_date", xen_cc.compile_date);
668 }
671 static PyObject *pyxc_sedf_domain_set(XcObject *self,
672 PyObject *args,
673 PyObject *kwds)
674 {
675 uint32_t domid;
676 uint64_t period, slice, latency;
677 uint16_t extratime, weight;
678 static char *kwd_list[] = { "dom", "period", "slice",
679 "latency", "extratime", "weight",NULL };
681 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLhh", kwd_list,
682 &domid, &period, &slice,
683 &latency, &extratime, &weight) )
684 return NULL;
685 if ( xc_sedf_domain_set(self->xc_handle, domid, period,
686 slice, latency, extratime,weight) != 0 )
687 return PyErr_SetFromErrno(xc_error);
689 Py_INCREF(zero);
690 return zero;
691 }
693 static PyObject *pyxc_sedf_domain_get(XcObject *self, PyObject *args)
694 {
695 uint32_t domid;
696 uint64_t period, slice,latency;
697 uint16_t weight, extratime;
699 if(!PyArg_ParseTuple(args, "i", &domid))
700 return NULL;
702 if (xc_sedf_domain_get(self->xc_handle, domid, &period,
703 &slice,&latency,&extratime,&weight))
704 return PyErr_SetFromErrno(xc_error);
706 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i,s:i}",
707 "domain", domid,
708 "period", period,
709 "slice", slice,
710 "latency", latency,
711 "extratime", extratime,
712 "weight", weight);
713 }
715 static PyObject *pyxc_domain_setmaxmem(XcObject *self, PyObject *args)
716 {
717 uint32_t dom;
718 unsigned int maxmem_kb;
720 if (!PyArg_ParseTuple(args, "ii", &dom, &maxmem_kb))
721 return NULL;
723 if (xc_domain_setmaxmem(self->xc_handle, dom, maxmem_kb) != 0)
724 return PyErr_SetFromErrno(xc_error);
726 Py_INCREF(zero);
727 return zero;
728 }
730 static PyObject *pyxc_domain_memory_increase_reservation(XcObject *self,
731 PyObject *args,
732 PyObject *kwds)
733 {
734 uint32_t dom;
735 unsigned long mem_kb;
736 unsigned int extent_order = 0 , address_bits = 0;
737 unsigned long nr_extents;
739 static char *kwd_list[] = { "dom", "mem_kb", "extent_order", "address_bits", NULL };
741 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "il|ii", kwd_list,
742 &dom, &mem_kb, &extent_order, &address_bits) )
743 return NULL;
745 /* round down to nearest power of 2. Assume callers using extent_order>0
746 know what they are doing */
747 nr_extents = (mem_kb / (XC_PAGE_SIZE/1024)) >> extent_order;
748 if ( xc_domain_memory_increase_reservation(self->xc_handle, dom,
749 nr_extents, extent_order,
750 address_bits, NULL) )
751 return PyErr_SetFromErrno(xc_error);
753 Py_INCREF(zero);
754 return zero;
755 }
757 static PyObject *pyxc_domain_ioport_permission(XcObject *self,
758 PyObject *args,
759 PyObject *kwds)
760 {
761 uint32_t dom;
762 int first_port, nr_ports, allow_access, ret;
764 static char *kwd_list[] = { "dom", "first_port", "nr_ports", "allow_access", NULL };
766 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiii", kwd_list,
767 &dom, &first_port, &nr_ports, &allow_access) )
768 return NULL;
770 ret = xc_domain_ioport_permission(
771 self->xc_handle, dom, first_port, nr_ports, allow_access);
772 if ( ret != 0 )
773 return PyErr_SetFromErrno(xc_error);
775 Py_INCREF(zero);
776 return zero;
777 }
779 static PyObject *pyxc_domain_irq_permission(PyObject *self,
780 PyObject *args,
781 PyObject *kwds)
782 {
783 XcObject *xc = (XcObject *)self;
784 uint32_t dom;
785 int pirq, allow_access, ret;
787 static char *kwd_list[] = { "dom", "pirq", "allow_access", NULL };
789 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwd_list,
790 &dom, &pirq, &allow_access) )
791 return NULL;
793 ret = xc_domain_irq_permission(
794 xc->xc_handle, dom, pirq, allow_access);
795 if ( ret != 0 )
796 return PyErr_SetFromErrno(xc_error);
798 Py_INCREF(zero);
799 return zero;
800 }
802 static PyObject *pyxc_domain_iomem_permission(PyObject *self,
803 PyObject *args,
804 PyObject *kwds)
805 {
806 XcObject *xc = (XcObject *)self;
807 uint32_t dom;
808 unsigned long first_pfn, nr_pfns, allow_access, ret;
810 static char *kwd_list[] = { "dom", "first_pfn", "nr_pfns", "allow_access", NULL };
812 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "illi", kwd_list,
813 &dom, &first_pfn, &nr_pfns, &allow_access) )
814 return NULL;
816 ret = xc_domain_iomem_permission(
817 xc->xc_handle, dom, first_pfn, nr_pfns, allow_access);
818 if ( ret != 0 )
819 return PyErr_SetFromErrno(xc_error);
821 Py_INCREF(zero);
822 return zero;
823 }
826 static PyObject *dom_op(XcObject *self, PyObject *args,
827 int (*fn)(int, uint32_t))
828 {
829 uint32_t dom;
831 if (!PyArg_ParseTuple(args, "i", &dom))
832 return NULL;
834 if (fn(self->xc_handle, dom) != 0)
835 return PyErr_SetFromErrno(xc_error);
837 Py_INCREF(zero);
838 return zero;
839 }
842 static PyMethodDef pyxc_methods[] = {
843 { "handle",
844 (PyCFunction)pyxc_handle,
845 METH_NOARGS, "\n"
846 "Query the xc control interface file descriptor.\n\n"
847 "Returns: [int] file descriptor\n" },
849 { "domain_create",
850 (PyCFunction)pyxc_domain_create,
851 METH_VARARGS | METH_KEYWORDS, "\n"
852 "Create a new domain.\n"
853 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
854 "Returns: [int] new domain identifier; -1 on error.\n" },
856 { "domain_max_vcpus",
857 (PyCFunction)pyxc_domain_max_vcpus,
858 METH_VARARGS, "\n"
859 "Set the maximum number of VCPUs a domain may create.\n"
860 " dom [int, 0]: Domain identifier to use.\n"
861 " max [int, 0]: New maximum number of VCPUs in domain.\n"
862 "Returns: [int] 0 on success; -1 on error.\n" },
864 { "domain_dumpcore",
865 (PyCFunction)pyxc_domain_dumpcore,
866 METH_VARARGS, "\n"
867 "Dump core of a domain.\n"
868 " dom [int]: Identifier of domain to dump core of.\n"
869 " corefile [string]: Name of corefile to be created.\n\n"
870 "Returns: [int] 0 on success; -1 on error.\n" },
872 { "domain_pause",
873 (PyCFunction)pyxc_domain_pause,
874 METH_VARARGS, "\n"
875 "Temporarily pause execution of a domain.\n"
876 " dom [int]: Identifier of domain to be paused.\n\n"
877 "Returns: [int] 0 on success; -1 on error.\n" },
879 { "domain_unpause",
880 (PyCFunction)pyxc_domain_unpause,
881 METH_VARARGS, "\n"
882 "(Re)start execution of a domain.\n"
883 " dom [int]: Identifier of domain to be unpaused.\n\n"
884 "Returns: [int] 0 on success; -1 on error.\n" },
886 { "domain_destroy",
887 (PyCFunction)pyxc_domain_destroy,
888 METH_VARARGS, "\n"
889 "Destroy a domain.\n"
890 " dom [int]: Identifier of domain to be destroyed.\n\n"
891 "Returns: [int] 0 on success; -1 on error.\n" },
893 { "vcpu_setaffinity",
894 (PyCFunction)pyxc_vcpu_setaffinity,
895 METH_VARARGS | METH_KEYWORDS, "\n"
896 "Pin a VCPU to a specified set CPUs.\n"
897 " dom [int]: Identifier of domain to which VCPU belongs.\n"
898 " vcpu [int, 0]: VCPU being pinned.\n"
899 " cpumap [list, []]: list of usable CPUs.\n\n"
900 "Returns: [int] 0 on success; -1 on error.\n" },
902 { "domain_setcpuweight",
903 (PyCFunction)pyxc_domain_setcpuweight,
904 METH_VARARGS | METH_KEYWORDS, "\n"
905 "Set cpuweight scheduler parameter for domain.\n"
906 " dom [int]: Identifier of domain to be changed.\n"
907 " cpuweight [float, 1]: VCPU being pinned.\n"
908 "Returns: [int] 0 on success; -1 on error.\n" },
910 { "domain_sethandle",
911 (PyCFunction)pyxc_domain_sethandle,
912 METH_VARARGS, "\n"
913 "Set domain's opaque handle.\n"
914 " dom [int]: Identifier of domain.\n"
915 " handle [list of 16 ints]: New opaque handle.\n"
916 "Returns: [int] 0 on success; -1 on error.\n" },
918 { "domain_getinfo",
919 (PyCFunction)pyxc_domain_getinfo,
920 METH_VARARGS | METH_KEYWORDS, "\n"
921 "Get information regarding a set of domains, in increasing id order.\n"
922 " first_dom [int, 0]: First domain to retrieve info about.\n"
923 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
924 " about.\n\n"
925 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
926 " parameter then there was an error, or the end of the\n"
927 " domain-id space was reached.\n"
928 " dom [int]: Identifier of domain to which this info pertains\n"
929 " cpu [int]: CPU to which this domain is bound\n"
930 " vcpus [int]: Number of Virtual CPUS in this domain\n"
931 " dying [int]: Bool - is the domain dying?\n"
932 " crashed [int]: Bool - has the domain crashed?\n"
933 " shutdown [int]: Bool - has the domain shut itself down?\n"
934 " paused [int]: Bool - is the domain paused by control software?\n"
935 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
936 " running [int]: Bool - is the domain currently running?\n"
937 " mem_kb [int]: Memory reservation, in kilobytes\n"
938 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
939 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
940 " shutdown_reason [int]: Numeric code from guest OS, explaining "
941 "reason why it shut itself down.\n" },
943 { "vcpu_getinfo",
944 (PyCFunction)pyxc_vcpu_getinfo,
945 METH_VARARGS | METH_KEYWORDS, "\n"
946 "Get information regarding a VCPU.\n"
947 " dom [int]: Domain to retrieve info about.\n"
948 " vcpu [int, 0]: VCPU to retrieve info about.\n\n"
949 "Returns: [dict]\n"
950 " online [int]: Bool - Is this VCPU currently online?\n"
951 " blocked [int]: Bool - Is this VCPU blocked waiting for an event?\n"
952 " running [int]: Bool - Is this VCPU currently running on a CPU?\n"
953 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
954 " cpumap [int]: Bitmap of CPUs this VCPU can run on\n"
955 " cpu [int]: CPU that this VCPU is currently bound to\n" },
957 { "linux_build",
958 (PyCFunction)pyxc_linux_build,
959 METH_VARARGS | METH_KEYWORDS, "\n"
960 "Build a new Linux guest OS.\n"
961 " dom [int]: Identifier of domain to build into.\n"
962 " image [str]: Name of kernel image file. May be gzipped.\n"
963 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
964 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
965 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
966 "Returns: [int] 0 on success; -1 on error.\n" },
968 { "vmx_build",
969 (PyCFunction)pyxc_vmx_build,
970 METH_VARARGS | METH_KEYWORDS, "\n"
971 "Build a new VMX guest OS.\n"
972 " dom [int]: Identifier of domain to build into.\n"
973 " image [str]: Name of VMX loader image file.\n"
974 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
975 "Returns: [int] 0 on success; -1 on error.\n" },
977 { "bvtsched_global_set",
978 (PyCFunction)pyxc_bvtsched_global_set,
979 METH_VARARGS | METH_KEYWORDS, "\n"
980 "Set global tuning parameters for Borrowed Virtual Time scheduler.\n"
981 " ctx_allow [int]: Minimal guaranteed quantum.\n\n"
982 "Returns: [int] 0 on success; -1 on error.\n" },
984 { "bvtsched_global_get",
985 (PyCFunction)pyxc_bvtsched_global_get,
986 METH_NOARGS, "\n"
987 "Get global tuning parameters for BVT scheduler.\n"
988 "Returns: [dict]:\n"
989 " ctx_allow [int]: context switch allowance\n" },
991 { "bvtsched_domain_set",
992 (PyCFunction)pyxc_bvtsched_domain_set,
993 METH_VARARGS | METH_KEYWORDS, "\n"
994 "Set per-domain tuning parameters for Borrowed Virtual Time scheduler.\n"
995 " dom [int]: Identifier of domain to be tuned.\n"
996 " mcuadv [int]: Proportional to the inverse of the domain's weight.\n"
997 " warpback [int]: Warp ? \n"
998 " warpvalue [int]: How far to warp domain's EVT on unblock.\n"
999 " warpl [int]: How long the domain can run warped.\n"
1000 " warpu [int]: How long before the domain can warp again.\n\n"
1001 "Returns: [int] 0 on success; -1 on error.\n" },
1003 { "bvtsched_domain_get",
1004 (PyCFunction)pyxc_bvtsched_domain_get,
1005 METH_VARARGS, "\n"
1006 "Get per-domain tuning parameters under the BVT scheduler.\n"
1007 " dom [int]: Identifier of domain to be queried.\n"
1008 "Returns [dict]:\n"
1009 " domain [int]: Domain ID.\n"
1010 " mcuadv [long]: MCU Advance.\n"
1011 " warp [long]: Warp.\n"
1012 " warpu [long]: Unwarp requirement.\n"
1013 " warpl [long]: Warp limit,\n"
1014 },
1016 { "sedf_domain_set",
1017 (PyCFunction)pyxc_sedf_domain_set,
1018 METH_KEYWORDS, "\n"
1019 "Set the scheduling parameters for a domain when running with Atropos.\n"
1020 " dom [int]: domain to set\n"
1021 " period [long]: domain's scheduling period\n"
1022 " slice [long]: domain's slice per period\n"
1023 " latency [long]: domain's wakeup latency hint\n"
1024 " extratime [int]: domain aware of extratime?\n"
1025 "Returns: [int] 0 on success; -1 on error.\n" },
1027 { "sedf_domain_get",
1028 (PyCFunction)pyxc_sedf_domain_get,
1029 METH_VARARGS, "\n"
1030 "Get the current scheduling parameters for a domain when running with\n"
1031 "the Atropos scheduler."
1032 " dom [int]: domain to query\n"
1033 "Returns: [dict]\n"
1034 " domain [int]: domain ID\n"
1035 " period [long]: scheduler period\n"
1036 " slice [long]: CPU reservation per period\n"
1037 " latency [long]: domain's wakeup latency hint\n"
1038 " extratime [int]: domain aware of extratime?\n"},
1040 { "evtchn_alloc_unbound",
1041 (PyCFunction)pyxc_evtchn_alloc_unbound,
1042 METH_VARARGS | METH_KEYWORDS, "\n"
1043 "Allocate an unbound port that will await a remote connection.\n"
1044 " dom [int]: Domain whose port space to allocate from.\n"
1045 " remote_dom [int]: Remote domain to accept connections from.\n\n"
1046 "Returns: [int] Unbound event-channel port.\n" },
1048 { "evtchn_status",
1049 (PyCFunction)pyxc_evtchn_status,
1050 METH_VARARGS | METH_KEYWORDS, "\n"
1051 "Query the status of an event channel.\n"
1052 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1053 " port [int]: Port-id of one endpoint of the channel.\n\n"
1054 "Returns: [dict] dictionary is empty on failure.\n"
1055 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
1056 " or 'virq'.\n"
1057 "The following are returned if 'status' is 'interdomain':\n"
1058 " dom [int]: Dom-id of remote endpoint.\n"
1059 " port [int]: Port-id of remote endpoint.\n"
1060 "The following are returned if 'status' is 'pirq' or 'virq':\n"
1061 " irq [int]: IRQ number.\n" },
1063 { "physdev_pci_access_modify",
1064 (PyCFunction)pyxc_physdev_pci_access_modify,
1065 METH_VARARGS | METH_KEYWORDS, "\n"
1066 "Allow a domain access to a PCI device\n"
1067 " dom [int]: Identifier of domain to be allowed access.\n"
1068 " bus [int]: PCI bus\n"
1069 " dev [int]: PCI slot\n"
1070 " func [int]: PCI function\n"
1071 " enable [int]: Non-zero means enable access; else disable access\n\n"
1072 "Returns: [int] 0 on success; -1 on error.\n" },
1074 { "readconsolering",
1075 (PyCFunction)pyxc_readconsolering,
1076 METH_VARARGS | METH_KEYWORDS, "\n"
1077 "Read Xen's console ring.\n"
1078 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1079 "Returns: [str] string is empty on failure.\n" },
1081 { "physinfo",
1082 (PyCFunction)pyxc_physinfo,
1083 METH_NOARGS, "\n"
1084 "Get information about the physical host machine\n"
1085 "Returns [dict]: information about the hardware"
1086 " [None]: on failure.\n" },
1088 { "xeninfo",
1089 (PyCFunction)pyxc_xeninfo,
1090 METH_NOARGS, "\n"
1091 "Get information about the Xen host\n"
1092 "Returns [dict]: information about Xen"
1093 " [None]: on failure.\n" },
1095 { "domain_setmaxmem",
1096 (PyCFunction)pyxc_domain_setmaxmem,
1097 METH_VARARGS, "\n"
1098 "Set a domain's memory limit\n"
1099 " dom [int]: Identifier of domain.\n"
1100 " maxmem_kb [int]: .\n"
1101 "Returns: [int] 0 on success; -1 on error.\n" },
1103 { "domain_memory_increase_reservation",
1104 (PyCFunction)pyxc_domain_memory_increase_reservation,
1105 METH_VARARGS | METH_KEYWORDS, "\n"
1106 "Increase a domain's memory reservation\n"
1107 " dom [int]: Identifier of domain.\n"
1108 " mem_kb [long]: .\n"
1109 "Returns: [int] 0 on success; -1 on error.\n" },
1111 { "domain_ioport_permission",
1112 (PyCFunction)pyxc_domain_ioport_permission,
1113 METH_VARARGS | METH_KEYWORDS, "\n"
1114 "Allow a domain access to a range of IO ports\n"
1115 " dom [int]: Identifier of domain to be allowed access.\n"
1116 " first_port [int]: First IO port\n"
1117 " nr_ports [int]: Number of IO ports\n"
1118 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1119 "Returns: [int] 0 on success; -1 on error.\n" },
1121 { "domain_irq_permission",
1122 (PyCFunction)pyxc_domain_irq_permission,
1123 METH_VARARGS | METH_KEYWORDS, "\n"
1124 "Allow a domain access to a physical IRQ\n"
1125 " dom [int]: Identifier of domain to be allowed access.\n"
1126 " pirq [int]: The Physical IRQ\n"
1127 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1128 "Returns: [int] 0 on success; -1 on error.\n" },
1130 { "domain_iomem_permission",
1131 (PyCFunction)pyxc_domain_iomem_permission,
1132 METH_VARARGS | METH_KEYWORDS, "\n"
1133 "Allow a domain access to a range of IO memory pages\n"
1134 " dom [int]: Identifier of domain to be allowed access.\n"
1135 " first_pfn [long]: First page of I/O Memory\n"
1136 " nr_pfns [long]: Number of pages of I/O Memory (>0)\n"
1137 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1138 "Returns: [int] 0 on success; -1 on error.\n" },
1140 { "pages_to_kib",
1141 (PyCFunction)pyxc_pages_to_kib,
1142 METH_VARARGS, "\n"
1143 "Returns: [int]: The size in KiB of memory spanning the given number "
1144 "of pages.\n" },
1146 { NULL, NULL, 0, NULL }
1147 };
1150 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1152 return Py_FindMethod(pyxc_methods, obj, name);
1155 static PyObject *PyXc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1157 XcObject *self = (XcObject *)type->tp_alloc(type, 0);
1159 if (self == NULL)
1160 return NULL;
1162 self->xc_handle = -1;
1164 return (PyObject *)self;
1167 static int
1168 PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
1170 if ((self->xc_handle = xc_interface_open()) == -1) {
1171 PyErr_SetFromErrno(PyExc_RuntimeError);
1172 return -1;
1175 return 0;
1178 static void PyXc_dealloc(XcObject *self)
1180 if (self->xc_handle != -1) {
1181 xc_interface_close(self->xc_handle);
1182 self->xc_handle = -1;
1185 self->ob_type->tp_free((PyObject *)self);
1188 static PyTypeObject PyXcType = {
1189 PyObject_HEAD_INIT(NULL)
1190 0,
1191 PKG "." CLS,
1192 sizeof(XcObject),
1193 0,
1194 (destructor)PyXc_dealloc, /* tp_dealloc */
1195 NULL, /* tp_print */
1196 PyXc_getattr, /* tp_getattr */
1197 NULL, /* tp_setattr */
1198 NULL, /* tp_compare */
1199 NULL, /* tp_repr */
1200 NULL, /* tp_as_number */
1201 NULL, /* tp_as_sequence */
1202 NULL, /* tp_as_mapping */
1203 NULL, /* tp_hash */
1204 NULL, /* tp_call */
1205 NULL, /* tp_str */
1206 NULL, /* tp_getattro */
1207 NULL, /* tp_setattro */
1208 NULL, /* tp_as_buffer */
1209 Py_TPFLAGS_DEFAULT, /* tp_flags */
1210 "Xen client connections", /* tp_doc */
1211 NULL, /* tp_traverse */
1212 NULL, /* tp_clear */
1213 NULL, /* tp_richcompare */
1214 0, /* tp_weaklistoffset */
1215 NULL, /* tp_iter */
1216 NULL, /* tp_iternext */
1217 pyxc_methods, /* tp_methods */
1218 NULL, /* tp_members */
1219 NULL, /* tp_getset */
1220 NULL, /* tp_base */
1221 NULL, /* tp_dict */
1222 NULL, /* tp_descr_get */
1223 NULL, /* tp_descr_set */
1224 0, /* tp_dictoffset */
1225 (initproc)PyXc_init, /* tp_init */
1226 NULL, /* tp_alloc */
1227 PyXc_new, /* tp_new */
1228 };
1230 static PyMethodDef xc_methods[] = { { NULL } };
1232 PyMODINIT_FUNC initxc(void)
1234 PyObject *m;
1236 if (PyType_Ready(&PyXcType) < 0)
1237 return;
1239 m = Py_InitModule(PKG, xc_methods);
1241 if (m == NULL)
1242 return;
1244 xc_error = PyErr_NewException(PKG ".error", NULL, NULL);
1245 zero = PyInt_FromLong(0);
1247 /* KAF: This ensures that we get debug output in a timely manner. */
1248 setbuf(stdout, NULL);
1249 setbuf(stderr, NULL);
1251 Py_INCREF(&PyXcType);
1252 PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
1256 /*
1257 * Local variables:
1258 * c-indent-level: 4
1259 * c-basic-offset: 4
1260 * End:
1261 */