direct-io.hg

view tools/python/xen/lowlevel/xc/xc.c @ 7961:f546d8697b0e

Misc fixes for breakage when moving to new Python/C interface style.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Nov 21 19:08:27 2005 +0100 (2005-11-21)
parents 87b520c30cb2
children e33f8d664e2d
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 }
578 static PyObject *pyxc_physinfo(XcObject *self)
579 {
580 xc_physinfo_t info;
581 char cpu_cap[128], *p=cpu_cap, *q=cpu_cap;
582 int i;
584 if ( xc_physinfo(self->xc_handle, &info) != 0 )
585 return PyErr_SetFromErrno(xc_error);
587 *q=0;
588 for(i=0;i<sizeof(info.hw_cap)/4;i++)
589 {
590 p+=sprintf(p,"%08x:",info.hw_cap[i]);
591 if(info.hw_cap[i])
592 q=p;
593 }
594 if(q>cpu_cap)
595 *(q-1)=0;
597 return Py_BuildValue("{s:i,s:i,s:i,s:i,s:l,s:l,s:i,s:s}",
598 "threads_per_core", info.threads_per_core,
599 "cores_per_socket", info.cores_per_socket,
600 "sockets_per_node", info.sockets_per_node,
601 "nr_nodes", info.nr_nodes,
602 "total_pages", info.total_pages,
603 "free_pages", info.free_pages,
604 "cpu_khz", info.cpu_khz,
605 "hw_caps", cpu_cap);
606 }
608 static PyObject *pyxc_xeninfo(XcObject *self)
609 {
610 xen_extraversion_t xen_extra;
611 xen_compile_info_t xen_cc;
612 xen_changeset_info_t xen_chgset;
613 xen_capabilities_info_t xen_caps;
614 xen_platform_parameters_t p_parms;
615 long xen_version;
616 char str[128];
618 xen_version = xc_version(self->xc_handle, XENVER_version, NULL);
620 if ( xc_version(self->xc_handle, XENVER_extraversion, &xen_extra) != 0 )
621 return PyErr_SetFromErrno(xc_error);
623 if ( xc_version(self->xc_handle, XENVER_compile_info, &xen_cc) != 0 )
624 return PyErr_SetFromErrno(xc_error);
626 if ( xc_version(self->xc_handle, XENVER_changeset, &xen_chgset) != 0 )
627 return PyErr_SetFromErrno(xc_error);
629 if ( xc_version(self->xc_handle, XENVER_capabilities, &xen_caps) != 0 )
630 return PyErr_SetFromErrno(xc_error);
632 if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
633 return PyErr_SetFromErrno(xc_error);
635 sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
637 return Py_BuildValue("{s:i,s:i,s:s,s:s,s:s,s:s,s:s,s:s,s:s,s:s}",
638 "xen_major", xen_version >> 16,
639 "xen_minor", (xen_version & 0xffff),
640 "xen_extra", xen_extra,
641 "xen_caps", xen_caps,
642 "platform_params", str,
643 "xen_changeset", xen_chgset,
644 "cc_compiler", xen_cc.compiler,
645 "cc_compile_by", xen_cc.compile_by,
646 "cc_compile_domain", xen_cc.compile_domain,
647 "cc_compile_date", xen_cc.compile_date);
648 }
651 static PyObject *pyxc_sedf_domain_set(XcObject *self,
652 PyObject *args,
653 PyObject *kwds)
654 {
655 uint32_t domid;
656 uint64_t period, slice, latency;
657 uint16_t extratime, weight;
658 static char *kwd_list[] = { "dom", "period", "slice",
659 "latency", "extratime", "weight",NULL };
661 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLhh", kwd_list,
662 &domid, &period, &slice,
663 &latency, &extratime, &weight) )
664 return NULL;
665 if ( xc_sedf_domain_set(self->xc_handle, domid, period,
666 slice, latency, extratime,weight) != 0 )
667 return PyErr_SetFromErrno(xc_error);
669 Py_INCREF(zero);
670 return zero;
671 }
673 static PyObject *pyxc_sedf_domain_get(XcObject *self, PyObject *args)
674 {
675 uint32_t domid;
676 uint64_t period, slice,latency;
677 uint16_t weight, extratime;
679 if(!PyArg_ParseTuple(args, "i", &domid))
680 return NULL;
682 if (xc_sedf_domain_get(self->xc_handle, domid, &period,
683 &slice,&latency,&extratime,&weight))
684 return PyErr_SetFromErrno(xc_error);
686 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i,s:i}",
687 "domain", domid,
688 "period", period,
689 "slice", slice,
690 "latency", latency,
691 "extratime", extratime,
692 "weight", weight);
693 }
695 static PyObject *pyxc_domain_setmaxmem(XcObject *self, PyObject *args)
696 {
697 uint32_t dom;
698 unsigned int maxmem_kb;
700 if (!PyArg_ParseTuple(args, "ii", &dom, &maxmem_kb))
701 return NULL;
703 if (xc_domain_setmaxmem(self->xc_handle, dom, maxmem_kb) != 0)
704 return PyErr_SetFromErrno(xc_error);
706 Py_INCREF(zero);
707 return zero;
708 }
710 static PyObject *pyxc_domain_memory_increase_reservation(XcObject *self,
711 PyObject *args,
712 PyObject *kwds)
713 {
714 uint32_t dom;
715 unsigned long mem_kb;
716 unsigned int extent_order = 0 , address_bits = 0;
717 unsigned long nr_extents;
719 static char *kwd_list[] = { "dom", "mem_kb", "extent_order", "address_bits", NULL };
721 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "il|ii", kwd_list,
722 &dom, &mem_kb, &extent_order, &address_bits) )
723 return NULL;
725 /* round down to nearest power of 2. Assume callers using extent_order>0
726 know what they are doing */
727 nr_extents = (mem_kb / (XC_PAGE_SIZE/1024)) >> extent_order;
728 if ( xc_domain_memory_increase_reservation(self->xc_handle, dom,
729 nr_extents, extent_order,
730 address_bits, NULL) )
731 return PyErr_SetFromErrno(xc_error);
733 Py_INCREF(zero);
734 return zero;
735 }
737 static PyObject *pyxc_domain_ioport_permission(XcObject *self,
738 PyObject *args,
739 PyObject *kwds)
740 {
741 uint32_t dom;
742 int first_port, nr_ports, allow_access, ret;
744 static char *kwd_list[] = { "dom", "first_port", "nr_ports", "allow_access", NULL };
746 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiii", kwd_list,
747 &dom, &first_port, &nr_ports, &allow_access) )
748 return NULL;
750 ret = xc_domain_ioport_permission(
751 self->xc_handle, dom, first_port, nr_ports, allow_access);
752 if ( ret != 0 )
753 return PyErr_SetFromErrno(xc_error);
755 Py_INCREF(zero);
756 return zero;
757 }
760 static PyObject *dom_op(XcObject *self, PyObject *args,
761 int (*fn)(int, uint32_t))
762 {
763 uint32_t dom;
765 if (!PyArg_ParseTuple(args, "i", &dom))
766 return NULL;
768 if (fn(self->xc_handle, dom) != 0)
769 return PyErr_SetFromErrno(xc_error);
771 Py_INCREF(zero);
772 return zero;
773 }
776 static PyMethodDef pyxc_methods[] = {
777 { "handle",
778 (PyCFunction)pyxc_handle,
779 METH_NOARGS, "\n"
780 "Query the xc control interface file descriptor.\n\n"
781 "Returns: [int] file descriptor\n" },
783 { "domain_create",
784 (PyCFunction)pyxc_domain_create,
785 METH_VARARGS | METH_KEYWORDS, "\n"
786 "Create a new domain.\n"
787 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
788 "Returns: [int] new domain identifier; -1 on error.\n" },
790 { "domain_max_vcpus",
791 (PyCFunction)pyxc_domain_max_vcpus,
792 METH_VARARGS, "\n"
793 "Set the maximum number of VCPUs a domain may create.\n"
794 " dom [int, 0]: Domain identifier to use.\n"
795 " max [int, 0]: New maximum number of VCPUs in domain.\n"
796 "Returns: [int] 0 on success; -1 on error.\n" },
798 { "domain_dumpcore",
799 (PyCFunction)pyxc_domain_dumpcore,
800 METH_VARARGS, "\n"
801 "Dump core of a domain.\n"
802 " dom [int]: Identifier of domain to dump core of.\n"
803 " corefile [string]: Name of corefile to be created.\n\n"
804 "Returns: [int] 0 on success; -1 on error.\n" },
806 { "domain_pause",
807 (PyCFunction)pyxc_domain_pause,
808 METH_VARARGS, "\n"
809 "Temporarily pause execution of a domain.\n"
810 " dom [int]: Identifier of domain to be paused.\n\n"
811 "Returns: [int] 0 on success; -1 on error.\n" },
813 { "domain_unpause",
814 (PyCFunction)pyxc_domain_unpause,
815 METH_VARARGS, "\n"
816 "(Re)start execution of a domain.\n"
817 " dom [int]: Identifier of domain to be unpaused.\n\n"
818 "Returns: [int] 0 on success; -1 on error.\n" },
820 { "domain_destroy",
821 (PyCFunction)pyxc_domain_destroy,
822 METH_VARARGS, "\n"
823 "Destroy a domain.\n"
824 " dom [int]: Identifier of domain to be destroyed.\n\n"
825 "Returns: [int] 0 on success; -1 on error.\n" },
827 { "domain_pincpu",
828 (PyCFunction)pyxc_domain_pincpu,
829 METH_VARARGS | METH_KEYWORDS, "\n"
830 "Pin a VCPU to a specified set CPUs.\n"
831 " dom [int]: Identifier of domain to which VCPU belongs.\n"
832 " vcpu [int, 0]: VCPU being pinned.\n"
833 " cpumap [list, []]: list of usable CPUs.\n\n"
834 "Returns: [int] 0 on success; -1 on error.\n" },
836 { "domain_setcpuweight",
837 (PyCFunction)pyxc_domain_setcpuweight,
838 METH_VARARGS | METH_KEYWORDS, "\n"
839 "Set cpuweight scheduler parameter for domain.\n"
840 " dom [int]: Identifier of domain to be changed.\n"
841 " cpuweight [float, 1]: VCPU being pinned.\n"
842 "Returns: [int] 0 on success; -1 on error.\n" },
844 { "domain_sethandle",
845 (PyCFunction)pyxc_domain_sethandle,
846 METH_VARARGS, "\n"
847 "Set domain's opaque handle.\n"
848 " dom [int]: Identifier of domain.\n"
849 " handle [list of 16 ints]: New opaque handle.\n"
850 "Returns: [int] 0 on success; -1 on error.\n" },
852 { "domain_getinfo",
853 (PyCFunction)pyxc_domain_getinfo,
854 METH_VARARGS | METH_KEYWORDS, "\n"
855 "Get information regarding a set of domains, in increasing id order.\n"
856 " first_dom [int, 0]: First domain to retrieve info about.\n"
857 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
858 " about.\n\n"
859 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
860 " parameter then there was an error, or the end of the\n"
861 " domain-id space was reached.\n"
862 " dom [int]: Identifier of domain to which this info pertains\n"
863 " cpu [int]: CPU to which this domain is bound\n"
864 " vcpus [int]: Number of Virtual CPUS in this domain\n"
865 " dying [int]: Bool - is the domain dying?\n"
866 " crashed [int]: Bool - has the domain crashed?\n"
867 " shutdown [int]: Bool - has the domain shut itself down?\n"
868 " paused [int]: Bool - is the domain paused by control software?\n"
869 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
870 " running [int]: Bool - is the domain currently running?\n"
871 " mem_kb [int]: Memory reservation, in kilobytes\n"
872 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
873 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
874 " shutdown_reason [int]: Numeric code from guest OS, explaining "
875 "reason why it shut itself down.\n" },
877 { "vcpu_getinfo",
878 (PyCFunction)pyxc_vcpu_getinfo,
879 METH_VARARGS | METH_KEYWORDS, "\n"
880 "Get information regarding a VCPU.\n"
881 " dom [int]: Domain to retrieve info about.\n"
882 " vcpu [int, 0]: VCPU to retrieve info about.\n\n"
883 "Returns: [dict]\n"
884 " online [int]: Bool - Is this VCPU currently online?\n"
885 " blocked [int]: Bool - Is this VCPU blocked waiting for an event?\n"
886 " running [int]: Bool - Is this VCPU currently running on a CPU?\n"
887 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
888 " cpumap [int]: Bitmap of CPUs this VCPU can run on\n"
889 " cpu [int]: CPU that this VCPU is currently bound to\n" },
891 { "linux_build",
892 (PyCFunction)pyxc_linux_build,
893 METH_VARARGS | METH_KEYWORDS, "\n"
894 "Build a new Linux guest OS.\n"
895 " dom [int]: Identifier of domain to build into.\n"
896 " image [str]: Name of kernel image file. May be gzipped.\n"
897 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
898 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
899 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
900 "Returns: [int] 0 on success; -1 on error.\n" },
902 { "vmx_build",
903 (PyCFunction)pyxc_vmx_build,
904 METH_VARARGS | METH_KEYWORDS, "\n"
905 "Build a new VMX guest OS.\n"
906 " dom [int]: Identifier of domain to build into.\n"
907 " image [str]: Name of VMX loader image file.\n"
908 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
909 "Returns: [int] 0 on success; -1 on error.\n" },
911 { "bvtsched_global_set",
912 (PyCFunction)pyxc_bvtsched_global_set,
913 METH_VARARGS | METH_KEYWORDS, "\n"
914 "Set global tuning parameters for Borrowed Virtual Time scheduler.\n"
915 " ctx_allow [int]: Minimal guaranteed quantum.\n\n"
916 "Returns: [int] 0 on success; -1 on error.\n" },
918 { "bvtsched_global_get",
919 (PyCFunction)pyxc_bvtsched_global_get,
920 METH_NOARGS, "\n"
921 "Get global tuning parameters for BVT scheduler.\n"
922 "Returns: [dict]:\n"
923 " ctx_allow [int]: context switch allowance\n" },
925 { "bvtsched_domain_set",
926 (PyCFunction)pyxc_bvtsched_domain_set,
927 METH_VARARGS | METH_KEYWORDS, "\n"
928 "Set per-domain tuning parameters for Borrowed Virtual Time scheduler.\n"
929 " dom [int]: Identifier of domain to be tuned.\n"
930 " mcuadv [int]: Proportional to the inverse of the domain's weight.\n"
931 " warpback [int]: Warp ? \n"
932 " warpvalue [int]: How far to warp domain's EVT on unblock.\n"
933 " warpl [int]: How long the domain can run warped.\n"
934 " warpu [int]: How long before the domain can warp again.\n\n"
935 "Returns: [int] 0 on success; -1 on error.\n" },
937 { "bvtsched_domain_get",
938 (PyCFunction)pyxc_bvtsched_domain_get,
939 METH_VARARGS, "\n"
940 "Get per-domain tuning parameters under the BVT scheduler.\n"
941 " dom [int]: Identifier of domain to be queried.\n"
942 "Returns [dict]:\n"
943 " domain [int]: Domain ID.\n"
944 " mcuadv [long]: MCU Advance.\n"
945 " warp [long]: Warp.\n"
946 " warpu [long]: Unwarp requirement.\n"
947 " warpl [long]: Warp limit,\n"
948 },
950 { "sedf_domain_set",
951 (PyCFunction)pyxc_sedf_domain_set,
952 METH_KEYWORDS, "\n"
953 "Set the scheduling parameters for a domain when running with Atropos.\n"
954 " dom [int]: domain to set\n"
955 " period [long]: domain's scheduling period\n"
956 " slice [long]: domain's slice per period\n"
957 " latency [long]: domain's wakeup latency hint\n"
958 " extratime [int]: domain aware of extratime?\n"
959 "Returns: [int] 0 on success; -1 on error.\n" },
961 { "sedf_domain_get",
962 (PyCFunction)pyxc_sedf_domain_get,
963 METH_VARARGS, "\n"
964 "Get the current scheduling parameters for a domain when running with\n"
965 "the Atropos scheduler."
966 " dom [int]: domain to query\n"
967 "Returns: [dict]\n"
968 " domain [int]: domain ID\n"
969 " period [long]: scheduler period\n"
970 " slice [long]: CPU reservation per period\n"
971 " latency [long]: domain's wakeup latency hint\n"
972 " extratime [int]: domain aware of extratime?\n"},
974 { "evtchn_alloc_unbound",
975 (PyCFunction)pyxc_evtchn_alloc_unbound,
976 METH_VARARGS | METH_KEYWORDS, "\n"
977 "Allocate an unbound port that will await a remote connection.\n"
978 " dom [int]: Domain whose port space to allocate from.\n"
979 " remote_dom [int]: Remote domain to accept connections from.\n\n"
980 "Returns: [int] Unbound event-channel port.\n" },
982 { "evtchn_status",
983 (PyCFunction)pyxc_evtchn_status,
984 METH_VARARGS | METH_KEYWORDS, "\n"
985 "Query the status of an event channel.\n"
986 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
987 " port [int]: Port-id of one endpoint of the channel.\n\n"
988 "Returns: [dict] dictionary is empty on failure.\n"
989 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
990 " or 'virq'.\n"
991 "The following are returned if 'status' is 'interdomain':\n"
992 " dom [int]: Dom-id of remote endpoint.\n"
993 " port [int]: Port-id of remote endpoint.\n"
994 "The following are returned if 'status' is 'pirq' or 'virq':\n"
995 " irq [int]: IRQ number.\n" },
997 { "physdev_pci_access_modify",
998 (PyCFunction)pyxc_physdev_pci_access_modify,
999 METH_VARARGS | METH_KEYWORDS, "\n"
1000 "Allow a domain access to a PCI device\n"
1001 " dom [int]: Identifier of domain to be allowed access.\n"
1002 " bus [int]: PCI bus\n"
1003 " dev [int]: PCI slot\n"
1004 " func [int]: PCI function\n"
1005 " enable [int]: Non-zero means enable access; else disable access\n\n"
1006 "Returns: [int] 0 on success; -1 on error.\n" },
1008 { "readconsolering",
1009 (PyCFunction)pyxc_readconsolering,
1010 METH_VARARGS | METH_KEYWORDS, "\n"
1011 "Read Xen's console ring.\n"
1012 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1013 "Returns: [str] string is empty on failure.\n" },
1015 { "physinfo",
1016 (PyCFunction)pyxc_physinfo,
1017 METH_NOARGS, "\n"
1018 "Get information about the physical host machine\n"
1019 "Returns [dict]: information about the hardware"
1020 " [None]: on failure.\n" },
1022 { "xeninfo",
1023 (PyCFunction)pyxc_xeninfo,
1024 METH_NOARGS, "\n"
1025 "Get information about the Xen host\n"
1026 "Returns [dict]: information about Xen"
1027 " [None]: on failure.\n" },
1029 { "domain_setmaxmem",
1030 (PyCFunction)pyxc_domain_setmaxmem,
1031 METH_VARARGS, "\n"
1032 "Set a domain's memory limit\n"
1033 " dom [int]: Identifier of domain.\n"
1034 " maxmem_kb [int]: .\n"
1035 "Returns: [int] 0 on success; -1 on error.\n" },
1037 { "domain_memory_increase_reservation",
1038 (PyCFunction)pyxc_domain_memory_increase_reservation,
1039 METH_VARARGS | METH_KEYWORDS, "\n"
1040 "Increase a domain's memory reservation\n"
1041 " dom [int]: Identifier of domain.\n"
1042 " mem_kb [long]: .\n"
1043 "Returns: [int] 0 on success; -1 on error.\n" },
1045 { "domain_ioport_permission",
1046 (PyCFunction)pyxc_domain_ioport_permission,
1047 METH_VARARGS | METH_KEYWORDS, "\n"
1048 "Allow a domain access to a range of IO ports\n"
1049 " dom [int]: Identifier of domain to be allowed access.\n"
1050 " first_port [int]: First IO port\n"
1051 " nr_ports [int]: Number of IO ports\n"
1052 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1053 "Returns: [int] 0 on success; -1 on error.\n" },
1055 { NULL, NULL, 0, NULL }
1056 };
1059 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1061 return Py_FindMethod(pyxc_methods, obj, name);
1064 static PyObject *PyXc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1066 XcObject *self = (XcObject *)type->tp_alloc(type, 0);
1068 if (self == NULL)
1069 return NULL;
1071 self->xc_handle = -1;
1073 return (PyObject *)self;
1076 static int
1077 PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
1079 if ((self->xc_handle = xc_interface_open()) == -1) {
1080 PyErr_SetFromErrno(PyExc_RuntimeError);
1081 return -1;
1084 return 0;
1087 static void PyXc_dealloc(XcObject *self)
1089 if (self->xc_handle != -1) {
1090 xc_interface_close(self->xc_handle);
1091 self->xc_handle = -1;
1094 self->ob_type->tp_free((PyObject *)self);
1097 static PyTypeObject PyXcType = {
1098 PyObject_HEAD_INIT(NULL)
1099 0,
1100 PKG "." CLS,
1101 sizeof(XcObject),
1102 0,
1103 (destructor)PyXc_dealloc, /* tp_dealloc */
1104 NULL, /* tp_print */
1105 PyXc_getattr, /* tp_getattr */
1106 NULL, /* tp_setattr */
1107 NULL, /* tp_compare */
1108 NULL, /* tp_repr */
1109 NULL, /* tp_as_number */
1110 NULL, /* tp_as_sequence */
1111 NULL, /* tp_as_mapping */
1112 NULL, /* tp_hash */
1113 NULL, /* tp_call */
1114 NULL, /* tp_str */
1115 NULL, /* tp_getattro */
1116 NULL, /* tp_setattro */
1117 NULL, /* tp_as_buffer */
1118 Py_TPFLAGS_DEFAULT, /* tp_flags */
1119 "Xen client connections", /* tp_doc */
1120 NULL, /* tp_traverse */
1121 NULL, /* tp_clear */
1122 NULL, /* tp_richcompare */
1123 0, /* tp_weaklistoffset */
1124 NULL, /* tp_iter */
1125 NULL, /* tp_iternext */
1126 pyxc_methods, /* tp_methods */
1127 NULL, /* tp_members */
1128 NULL, /* tp_getset */
1129 NULL, /* tp_base */
1130 NULL, /* tp_dict */
1131 NULL, /* tp_descr_get */
1132 NULL, /* tp_descr_set */
1133 0, /* tp_dictoffset */
1134 (initproc)PyXc_init, /* tp_init */
1135 NULL, /* tp_alloc */
1136 PyXc_new, /* tp_new */
1137 };
1139 static PyMethodDef xc_methods[] = { { NULL } };
1141 PyMODINIT_FUNC initxc(void)
1143 PyObject *m;
1145 if (PyType_Ready(&PyXcType) < 0)
1146 return;
1148 m = Py_InitModule(PKG, xc_methods);
1150 if (m == NULL)
1151 return;
1153 xc_error = PyErr_NewException(PKG ".error", NULL, NULL);
1154 zero = PyInt_FromLong(0);
1156 /* KAF: This ensures that we get debug output in a timely manner. */
1157 setbuf(stdout, NULL);
1158 setbuf(stderr, NULL);
1160 Py_INCREF(&PyXcType);
1161 PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
1165 /*
1166 * Local variables:
1167 * c-indent-level: 4
1168 * c-basic-offset: 4
1169 * End:
1170 */