direct-io.hg

view tools/python/xen/lowlevel/xc/xc.c @ 3326:f36de623daa0

bitkeeper revision 1.1159.187.81 (41cc934bUot13IO0-1LsmhYl0sW9Zw)

Add plan9 builder support.
author iap10@labyrinth.cl.cam.ac.uk
date Fri Dec 24 22:08:11 2004 +0000 (2004-12-24)
parents 77fbe6c094f2
children 6b1aa25ff284
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 <xc.h>
9 #include <zlib.h>
10 #include <fcntl.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netdb.h>
16 #include <arpa/inet.h>
17 #include "xc_private.h"
18 #include "gzip_stream.h"
20 /* Needed for Python versions earlier than 2.3. */
21 #ifndef PyMODINIT_FUNC
22 #define PyMODINIT_FUNC DL_EXPORT(void)
23 #endif
25 #define XENPKG "xen.lowlevel.xc"
27 static PyObject *xc_error, *zero;
29 typedef struct {
30 PyObject_HEAD;
31 int xc_handle;
32 } XcObject;
34 /*
35 * Definitions for the 'xc' object type.
36 */
38 static PyObject *pyxc_domain_create(PyObject *self,
39 PyObject *args,
40 PyObject *kwds)
41 {
42 XcObject *xc = (XcObject *)self;
44 unsigned int mem_kb = 0;
45 int cpu = -1;
46 float cpu_weight = 1;
47 u32 dom = 0;
48 int ret;
50 static char *kwd_list[] = { "dom", "mem_kb", "cpu", "cpu_weight", NULL };
52 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiif", kwd_list,
53 &dom, &mem_kb, &cpu, &cpu_weight))
54 return NULL;
56 if ( (ret = xc_domain_create(
57 xc->xc_handle, mem_kb, cpu, cpu_weight, &dom)) < 0 )
58 return PyErr_SetFromErrno(xc_error);
60 return PyInt_FromLong(dom);
61 }
63 static PyObject *pyxc_domain_pause(PyObject *self,
64 PyObject *args,
65 PyObject *kwds)
66 {
67 XcObject *xc = (XcObject *)self;
69 u32 dom;
71 static char *kwd_list[] = { "dom", NULL };
73 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
74 return NULL;
76 if ( xc_domain_pause(xc->xc_handle, dom) != 0 )
77 return PyErr_SetFromErrno(xc_error);
79 Py_INCREF(zero);
80 return zero;
81 }
83 static PyObject *pyxc_domain_unpause(PyObject *self,
84 PyObject *args,
85 PyObject *kwds)
86 {
87 XcObject *xc = (XcObject *)self;
89 u32 dom;
91 static char *kwd_list[] = { "dom", NULL };
93 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
94 return NULL;
96 if ( xc_domain_unpause(xc->xc_handle, dom) != 0 )
97 return PyErr_SetFromErrno(xc_error);
99 Py_INCREF(zero);
100 return zero;
101 }
103 static PyObject *pyxc_domain_destroy(PyObject *self,
104 PyObject *args,
105 PyObject *kwds)
106 {
107 XcObject *xc = (XcObject *)self;
109 u32 dom;
111 static char *kwd_list[] = { "dom", NULL };
113 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
114 return NULL;
116 if ( xc_domain_destroy(xc->xc_handle, dom) != 0 )
117 return PyErr_SetFromErrno(xc_error);
119 Py_INCREF(zero);
120 return zero;
121 }
123 static PyObject *pyxc_domain_pincpu(PyObject *self,
124 PyObject *args,
125 PyObject *kwds)
126 {
127 XcObject *xc = (XcObject *)self;
129 u32 dom;
130 int cpu = -1;
132 static char *kwd_list[] = { "dom", "cpu", NULL };
134 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
135 &dom, &cpu) )
136 return NULL;
138 if ( xc_domain_pincpu(xc->xc_handle, dom, cpu) != 0 )
139 return PyErr_SetFromErrno(xc_error);
141 Py_INCREF(zero);
142 return zero;
143 }
145 static PyObject *pyxc_domain_getinfo(PyObject *self,
146 PyObject *args,
147 PyObject *kwds)
148 {
149 XcObject *xc = (XcObject *)self;
150 PyObject *list;
152 u32 first_dom = 0;
153 int max_doms = 1024, nr_doms, i;
154 xc_dominfo_t *info;
156 static char *kwd_list[] = { "first_dom", "max_doms", NULL };
158 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list,
159 &first_dom, &max_doms) )
160 return NULL;
162 if ( (info = malloc(max_doms * sizeof(xc_dominfo_t))) == NULL )
163 return PyErr_NoMemory();
165 nr_doms = xc_domain_getinfo(xc->xc_handle, first_dom, max_doms, info);
167 list = PyList_New(nr_doms);
168 for ( i = 0 ; i < nr_doms; i++ )
169 {
170 PyList_SetItem(
171 list, i,
172 Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i"
173 ",s:l,s:L,s:l,s:i}",
174 "dom", info[i].domid,
175 "cpu", info[i].cpu,
176 "dying", info[i].dying,
177 "crashed", info[i].crashed,
178 "shutdown", info[i].shutdown,
179 "paused", info[i].paused,
180 "blocked", info[i].blocked,
181 "running", info[i].running,
182 "mem_kb", info[i].nr_pages*4,
183 "cpu_time", info[i].cpu_time,
184 "maxmem_kb", info[i].max_memkb,
185 "shutdown_reason", info[i].shutdown_reason
186 ));
187 }
189 free(info);
191 return list;
192 }
194 static int file_save(XcObject *xc, XcIOContext *ctxt, char *state_file)
195 {
196 int rc = -1;
197 int fd = -1;
198 int open_flags = (O_CREAT | O_EXCL | O_WRONLY);
199 int open_mode = 0644;
201 printf("%s>\n", __FUNCTION__);
203 if ( (fd = open(state_file, open_flags, open_mode)) < 0 )
204 {
205 xcio_perror(ctxt, "Could not open file for writing");
206 goto exit;
207 }
209 printf("%s>gzip_stream_fdopen... \n", __FUNCTION__);
211 /* Compression rate 1: we want speed over compression.
212 * We're mainly going for those zero pages, after all.
213 */
214 ctxt->io = gzip_stream_fdopen(fd, "wb1");
215 if ( ctxt->io == NULL )
216 {
217 xcio_perror(ctxt, "Could not allocate compression state");
218 goto exit;
219 }
221 printf("%s> xc_linux_save...\n", __FUNCTION__);
223 rc = xc_linux_save(xc->xc_handle, ctxt);
225 exit:
226 if ( ctxt->io != NULL )
227 IOStream_close(ctxt->io);
228 if ( fd >= 0 )
229 close(fd);
230 unlink(state_file);
231 printf("%s> rc=%d\n", __FUNCTION__, rc);
232 return rc;
233 }
235 static PyObject *pyxc_linux_save(PyObject *self,
236 PyObject *args,
237 PyObject *kwds)
238 {
239 XcObject *xc = (XcObject *)self;
241 char *state_file;
242 int progress = 1, debug = 0;
243 PyObject *val = NULL;
244 int rc = -1;
245 XcIOContext ioctxt = { .info = iostdout, .err = iostderr };
247 static char *kwd_list[] = { "dom", "state_file", "vmconfig", "progress", "debug", NULL };
249 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "is|sii", kwd_list,
250 &ioctxt.domain,
251 &state_file,
252 &ioctxt.vmconfig,
253 &progress,
254 &debug) )
255 goto exit;
257 ioctxt.vmconfig_n = (ioctxt.vmconfig ? strlen(ioctxt.vmconfig) : 0);
259 if ( progress )
260 ioctxt.flags |= XCFLAGS_VERBOSE;
261 if ( debug )
262 ioctxt.flags |= XCFLAGS_DEBUG;
264 if ( (state_file == NULL) || (state_file[0] == '\0') )
265 goto exit;
267 rc = file_save(xc, &ioctxt, state_file);
268 if ( rc != 0 )
269 {
270 PyErr_SetFromErrno(xc_error);
271 goto exit;
272 }
274 Py_INCREF(zero);
275 val = zero;
277 exit:
278 return val;
279 }
282 static int file_restore(XcObject *xc, XcIOContext *ioctxt, char *state_file)
283 {
284 int rc = -1;
286 ioctxt->io = gzip_stream_fopen(state_file, "rb");
287 if ( ioctxt->io == NULL )
288 {
289 xcio_perror(ioctxt, "Could not open file for reading");
290 return rc;
291 }
293 rc = xc_linux_restore(xc->xc_handle, ioctxt);
295 IOStream_close(ioctxt->io);
297 return rc;
298 }
300 static PyObject *pyxc_linux_restore(PyObject *self,
301 PyObject *args,
302 PyObject *kwds)
303 {
304 XcObject *xc = (XcObject *)self;
305 char *state_file;
306 int progress = 1, debug = 0;
307 PyObject *val = NULL;
308 XcIOContext ioctxt = { .info = iostdout, .err = iostderr };
309 int rc =-1;
311 static char *kwd_list[] = { "state_file", "progress", "debug", NULL };
313 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "s|ii", kwd_list,
314 &state_file,
315 &progress,
316 &debug) )
317 goto exit;
319 if ( progress )
320 ioctxt.flags |= XCFLAGS_VERBOSE;
321 if ( debug )
322 ioctxt.flags |= XCFLAGS_DEBUG;
324 if ( (state_file == NULL) || (state_file[0] == '\0') )
325 goto exit;
327 rc = file_restore(xc, &ioctxt, state_file);
328 if ( rc != 0 )
329 {
330 PyErr_SetFromErrno(xc_error);
331 goto exit;
332 }
334 val = Py_BuildValue("{s:i,s:s}",
335 "dom", ioctxt.domain,
336 "vmconfig", ioctxt.vmconfig);
338 exit:
339 return val;
340 }
342 static PyObject *pyxc_linux_build(PyObject *self,
343 PyObject *args,
344 PyObject *kwds)
345 {
346 XcObject *xc = (XcObject *)self;
348 u32 dom;
349 char *image, *ramdisk = NULL, *cmdline = "";
350 int control_evtchn, flags = 0;
352 static char *kwd_list[] = { "dom", "control_evtchn",
353 "image", "ramdisk", "cmdline", "flags",
354 NULL };
356 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iis|ssi", kwd_list,
357 &dom, &control_evtchn,
358 &image, &ramdisk, &cmdline, &flags) )
359 return NULL;
361 if ( xc_linux_build(xc->xc_handle, dom, image,
362 ramdisk, cmdline, control_evtchn, flags) != 0 )
363 return PyErr_SetFromErrno(xc_error);
365 Py_INCREF(zero);
366 return zero;
367 }
369 static PyObject *pyxc_plan9_build(PyObject *self,
370 PyObject *args,
371 PyObject *kwds)
372 {
373 XcObject *xc = (XcObject *)self;
375 u32 dom;
376 char *image, *ramdisk = NULL, *cmdline = "";
377 int control_evtchn, flags = 0;
379 static char *kwd_list[] = { "dom", "control_evtchn",
380 "image", "ramdisk", "cmdline", "flags",
381 NULL };
383 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iis|ssi", kwd_list,
384 &dom, &control_evtchn,
385 &image, &ramdisk, &cmdline, &flags) )
386 return NULL;
388 if ( xc_plan9_build(xc->xc_handle, dom, image,
389 cmdline, control_evtchn, flags) != 0 )
390 return PyErr_SetFromErrno(xc_error);
392 Py_INCREF(zero);
393 return zero;
394 }
396 static PyObject *pyxc_bvtsched_global_set(PyObject *self,
397 PyObject *args,
398 PyObject *kwds)
399 {
400 XcObject *xc = (XcObject *)self;
402 unsigned long ctx_allow;
404 static char *kwd_list[] = { "ctx_allow", NULL };
406 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "l", kwd_list, &ctx_allow) )
407 return NULL;
409 if ( xc_bvtsched_global_set(xc->xc_handle, ctx_allow) != 0 )
410 return PyErr_SetFromErrno(xc_error);
412 Py_INCREF(zero);
413 return zero;
414 }
416 static PyObject *pyxc_bvtsched_global_get(PyObject *self,
417 PyObject *args,
418 PyObject *kwds)
419 {
420 XcObject *xc = (XcObject *)self;
422 unsigned long ctx_allow;
424 if ( !PyArg_ParseTuple(args, "") )
425 return NULL;
427 if ( xc_bvtsched_global_get(xc->xc_handle, &ctx_allow) != 0 )
428 return PyErr_SetFromErrno(xc_error);
430 return Py_BuildValue("s:l", "ctx_allow", ctx_allow);
431 }
433 static PyObject *pyxc_bvtsched_domain_set(PyObject *self,
434 PyObject *args,
435 PyObject *kwds)
436 {
437 XcObject *xc = (XcObject *)self;
439 u32 dom;
440 u32 mcuadv;
441 int warpback;
442 s32 warpvalue;
443 long long warpl;
444 long long warpu;
446 static char *kwd_list[] = { "dom", "mcuadv", "warpback", "warpvalue",
447 "warpl", "warpu", NULL };
449 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiiLL", kwd_list,
450 &dom, &mcuadv, &warpback, &warpvalue,
451 &warpl, &warpu) )
452 return NULL;
454 if ( xc_bvtsched_domain_set(xc->xc_handle, dom, mcuadv,
455 warpback, warpvalue, warpl, warpu) != 0 )
456 return PyErr_SetFromErrno(xc_error);
458 Py_INCREF(zero);
459 return zero;
460 }
462 static PyObject *pyxc_bvtsched_domain_get(PyObject *self,
463 PyObject *args,
464 PyObject *kwds)
465 {
466 XcObject *xc = (XcObject *)self;
467 u32 dom;
468 u32 mcuadv;
469 int warpback;
470 s32 warpvalue;
471 long long warpl;
472 long long warpu;
474 static char *kwd_list[] = { "dom", NULL };
476 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
477 return NULL;
479 if ( xc_bvtsched_domain_get(xc->xc_handle, dom, &mcuadv, &warpback,
480 &warpvalue, &warpl, &warpu) != 0 )
481 return PyErr_SetFromErrno(xc_error);
483 return Py_BuildValue("{s:i,s:l,s:l,s:l,s:l}",
484 "domain", dom,
485 "mcuadv", mcuadv,
486 "warpback", warpback,
487 "warpvalue", warpvalue,
488 "warpl", warpl,
489 "warpu", warpu);
490 }
492 static PyObject *pyxc_evtchn_alloc_unbound(PyObject *self,
493 PyObject *args,
494 PyObject *kwds)
495 {
496 XcObject *xc = (XcObject *)self;
498 u32 dom;
499 int port;
501 static char *kwd_list[] = { "dom", NULL };
503 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
504 return NULL;
506 if ( xc_evtchn_alloc_unbound(xc->xc_handle, dom, &port) != 0 )
507 return PyErr_SetFromErrno(xc_error);
509 return PyInt_FromLong(port);
510 }
512 static PyObject *pyxc_evtchn_bind_interdomain(PyObject *self,
513 PyObject *args,
514 PyObject *kwds)
515 {
516 XcObject *xc = (XcObject *)self;
518 u32 dom1 = DOMID_SELF, dom2 = DOMID_SELF;
519 int port1 = 0, port2 = 0;
521 static char *kwd_list[] = { "dom1", "dom2", "port1", "port2", NULL };
523 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiii", kwd_list,
524 &dom1, &dom2, &port1, &port2) )
525 return NULL;
527 if ( xc_evtchn_bind_interdomain(xc->xc_handle, dom1,
528 dom2, &port1, &port2) != 0 )
529 return PyErr_SetFromErrno(xc_error);
531 return Py_BuildValue("{s:i,s:i}",
532 "port1", port1,
533 "port2", port2);
534 }
536 static PyObject *pyxc_evtchn_bind_virq(PyObject *self,
537 PyObject *args,
538 PyObject *kwds)
539 {
540 XcObject *xc = (XcObject *)self;
542 int virq, port;
544 static char *kwd_list[] = { "virq", NULL };
546 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &virq) )
547 return NULL;
549 if ( xc_evtchn_bind_virq(xc->xc_handle, virq, &port) != 0 )
550 return PyErr_SetFromErrno(xc_error);
552 return PyInt_FromLong(port);
553 }
555 static PyObject *pyxc_evtchn_close(PyObject *self,
556 PyObject *args,
557 PyObject *kwds)
558 {
559 XcObject *xc = (XcObject *)self;
561 u32 dom = DOMID_SELF;
562 int port;
564 static char *kwd_list[] = { "port", "dom", NULL };
566 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
567 &port, &dom) )
568 return NULL;
570 if ( xc_evtchn_close(xc->xc_handle, dom, port) != 0 )
571 return PyErr_SetFromErrno(xc_error);
573 Py_INCREF(zero);
574 return zero;
575 }
577 static PyObject *pyxc_evtchn_send(PyObject *self,
578 PyObject *args,
579 PyObject *kwds)
580 {
581 XcObject *xc = (XcObject *)self;
583 int port;
585 static char *kwd_list[] = { "port", NULL };
587 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &port) )
588 return NULL;
590 if ( xc_evtchn_send(xc->xc_handle, port) != 0 )
591 return PyErr_SetFromErrno(xc_error);
593 Py_INCREF(zero);
594 return zero;
595 }
597 static PyObject *pyxc_evtchn_status(PyObject *self,
598 PyObject *args,
599 PyObject *kwds)
600 {
601 XcObject *xc = (XcObject *)self;
602 PyObject *dict;
604 u32 dom = DOMID_SELF;
605 int port, ret;
606 xc_evtchn_status_t status;
608 static char *kwd_list[] = { "port", "dom", NULL };
610 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
611 &port, &dom) )
612 return NULL;
614 ret = xc_evtchn_status(xc->xc_handle, dom, port, &status);
615 if ( ret != 0 )
616 return PyErr_SetFromErrno(xc_error);
618 switch ( status.status )
619 {
620 case EVTCHNSTAT_closed:
621 dict = Py_BuildValue("{s:s}",
622 "status", "closed");
623 break;
624 case EVTCHNSTAT_unbound:
625 dict = Py_BuildValue("{s:s}",
626 "status", "unbound");
627 break;
628 case EVTCHNSTAT_interdomain:
629 dict = Py_BuildValue("{s:s,s:i,s:i}",
630 "status", "interdomain",
631 "dom", status.u.interdomain.dom,
632 "port", status.u.interdomain.port);
633 break;
634 case EVTCHNSTAT_pirq:
635 dict = Py_BuildValue("{s:s,s:i}",
636 "status", "pirq",
637 "irq", status.u.pirq);
638 break;
639 case EVTCHNSTAT_virq:
640 dict = Py_BuildValue("{s:s,s:i}",
641 "status", "virq",
642 "irq", status.u.virq);
643 break;
644 default:
645 dict = Py_BuildValue("{}");
646 break;
647 }
649 return dict;
650 }
652 static PyObject *pyxc_physdev_pci_access_modify(PyObject *self,
653 PyObject *args,
654 PyObject *kwds)
655 {
656 XcObject *xc = (XcObject *)self;
657 u32 dom;
658 int bus, dev, func, enable, ret;
660 static char *kwd_list[] = { "dom", "bus", "dev", "func", "enable", NULL };
662 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
663 &dom, &bus, &dev, &func, &enable) )
664 return NULL;
666 ret = xc_physdev_pci_access_modify(
667 xc->xc_handle, dom, bus, dev, func, enable);
668 if ( ret != 0 )
669 return PyErr_SetFromErrno(xc_error);
671 Py_INCREF(zero);
672 return zero;
673 }
675 static PyObject *pyxc_readconsolering(PyObject *self,
676 PyObject *args,
677 PyObject *kwds)
678 {
679 XcObject *xc = (XcObject *)self;
681 unsigned int clear = 0;
682 char str[32768];
683 int ret;
685 static char *kwd_list[] = { "clear", NULL };
687 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
688 return NULL;
690 ret = xc_readconsolering(xc->xc_handle, str, sizeof(str), clear);
691 if ( ret < 0 )
692 return PyErr_SetFromErrno(xc_error);
694 return PyString_FromStringAndSize(str, ret);
695 }
697 static PyObject *pyxc_physinfo(PyObject *self,
698 PyObject *args,
699 PyObject *kwds)
700 {
701 XcObject *xc = (XcObject *)self;
702 xc_physinfo_t info;
704 if ( !PyArg_ParseTuple(args, "") )
705 return NULL;
707 if ( xc_physinfo(xc->xc_handle, &info) != 0 )
708 return PyErr_SetFromErrno(xc_error);
710 return Py_BuildValue("{s:i,s:i,s:l,s:l,s:l}",
711 "ht_per_core", info.ht_per_core,
712 "cores", info.cores,
713 "total_pages", info.total_pages,
714 "free_pages", info.free_pages,
715 "cpu_khz", info.cpu_khz);
716 }
718 static PyObject *pyxc_atropos_domain_set(PyObject *self,
719 PyObject *args,
720 PyObject *kwds)
721 {
722 XcObject *xc = (XcObject *)self;
723 u32 domid;
724 u64 period, slice, latency;
725 int xtratime;
727 static char *kwd_list[] = { "dom", "period", "slice", "latency",
728 "xtratime", NULL };
730 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLi", kwd_list, &domid,
731 &period, &slice, &latency, &xtratime) )
732 return NULL;
734 if ( xc_atropos_domain_set(xc->xc_handle, domid, period, slice,
735 latency, xtratime) != 0 )
736 return PyErr_SetFromErrno(xc_error);
738 Py_INCREF(zero);
739 return zero;
740 }
742 static PyObject *pyxc_atropos_domain_get(PyObject *self,
743 PyObject *args,
744 PyObject *kwds)
745 {
746 XcObject *xc = (XcObject *)self;
747 u32 domid;
748 u64 period, slice, latency;
749 int xtratime;
751 static char *kwd_list[] = { "dom", NULL };
753 if( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &domid) )
754 return NULL;
756 if ( xc_atropos_domain_get( xc->xc_handle, domid, &period,
757 &slice, &latency, &xtratime ) )
758 return PyErr_SetFromErrno(xc_error);
760 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i}",
761 "domain", domid,
762 "period", period,
763 "slice", slice,
764 "latency", latency,
765 "xtratime", xtratime);
766 }
769 static PyObject *pyxc_rrobin_global_set(PyObject *self,
770 PyObject *args,
771 PyObject *kwds)
772 {
773 XcObject *xc = (XcObject *)self;
774 u64 slice;
776 static char *kwd_list[] = { "slice", NULL };
778 if( !PyArg_ParseTupleAndKeywords(args, kwds, "L", kwd_list, &slice) )
779 return NULL;
781 if ( xc_rrobin_global_set(xc->xc_handle, slice) != 0 )
782 return PyErr_SetFromErrno(xc_error);
784 Py_INCREF(zero);
785 return zero;
786 }
788 static PyObject *pyxc_shadow_control(PyObject *self,
789 PyObject *args,
790 PyObject *kwds)
791 {
792 XcObject *xc = (XcObject *)self;
794 u32 dom;
795 int op=0;
797 static char *kwd_list[] = { "dom", "op", NULL };
799 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
800 &dom, &op) )
801 return NULL;
803 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, NULL) < 0 )
804 return PyErr_SetFromErrno(xc_error);
806 Py_INCREF(zero);
807 return zero;
808 }
810 static PyObject *pyxc_rrobin_global_get(PyObject *self,
811 PyObject *args,
812 PyObject *kwds)
813 {
814 XcObject *xc = (XcObject *)self;
815 u64 slice;
817 if ( !PyArg_ParseTuple(args, "") )
818 return NULL;
820 if ( xc_rrobin_global_get(xc->xc_handle, &slice) != 0 )
821 return PyErr_SetFromErrno(xc_error);
823 return Py_BuildValue("{s:L}", "slice", slice);
824 }
826 static PyObject *pyxc_domain_setmaxmem(PyObject *self,
827 PyObject *args,
828 PyObject *kwds)
829 {
830 XcObject *xc = (XcObject *)self;
832 u32 dom;
833 unsigned long maxmem_kb;
835 static char *kwd_list[] = { "dom", "maxmem_kb", NULL };
837 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
838 &dom, &maxmem_kb) )
839 return NULL;
841 if ( xc_domain_setmaxmem(xc->xc_handle, dom, maxmem_kb) != 0 )
842 return PyErr_SetFromErrno(xc_error);
844 Py_INCREF(zero);
845 return zero;
846 }
849 static PyMethodDef pyxc_methods[] = {
850 { "domain_create",
851 (PyCFunction)pyxc_domain_create,
852 METH_VARARGS | METH_KEYWORDS, "\n"
853 "Create a new domain.\n"
854 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
855 " mem_kb [int, 0]: Memory allocation, in kilobytes.\n"
856 "Returns: [int] new domain identifier; -1 on error.\n" },
858 { "domain_pause",
859 (PyCFunction)pyxc_domain_pause,
860 METH_VARARGS | METH_KEYWORDS, "\n"
861 "Temporarily pause execution of a domain.\n"
862 " dom [int]: Identifier of domain to be paused.\n\n"
863 "Returns: [int] 0 on success; -1 on error.\n" },
865 { "domain_unpause",
866 (PyCFunction)pyxc_domain_unpause,
867 METH_VARARGS | METH_KEYWORDS, "\n"
868 "(Re)start execution of a domain.\n"
869 " dom [int]: Identifier of domain to be unpaused.\n\n"
870 "Returns: [int] 0 on success; -1 on error.\n" },
872 { "domain_destroy",
873 (PyCFunction)pyxc_domain_destroy,
874 METH_VARARGS | METH_KEYWORDS, "\n"
875 "Destroy a domain.\n"
876 " dom [int]: Identifier of domain to be destroyed.\n\n"
877 "Returns: [int] 0 on success; -1 on error.\n" },
879 { "domain_pincpu",
880 (PyCFunction)pyxc_domain_pincpu,
881 METH_VARARGS | METH_KEYWORDS, "\n"
882 "Pin a domain to a specified CPU.\n"
883 " dom [int]: Identifier of domain to be pinned.\n"
884 " cpu [int, -1]: CPU to pin to, or -1 to unpin\n\n"
885 "Returns: [int] 0 on success; -1 on error.\n" },
887 { "domain_getinfo",
888 (PyCFunction)pyxc_domain_getinfo,
889 METH_VARARGS | METH_KEYWORDS, "\n"
890 "Get information regarding a set of domains, in increasing id order.\n"
891 " first_dom [int, 0]: First domain to retrieve info about.\n"
892 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
893 " about.\n\n"
894 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
895 " parameter then there was an error, or the end of the\n"
896 " domain-id space was reached.\n"
897 " dom [int]: Identifier of domain to which this info pertains\n"
898 " cpu [int]: CPU to which this domain is bound\n"
899 " dying [int]: Bool - is the domain dying?\n"
900 " crashed [int]: Bool - has the domain crashed?\n"
901 " shutdown [int]: Bool - has the domain shut itself down?\n"
902 " paused [int]: Bool - is the domain paused by control software?\n"
903 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
904 " running [int]: Bool - is the domain currently running?\n"
905 " mem_kb [int]: Memory reservation, in kilobytes\n"
906 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
907 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
908 " shutdown_reason [int]: Numeric code from guest OS, explaining "
909 "reason why it shut itself down.\n" },
911 { "linux_save",
912 (PyCFunction)pyxc_linux_save,
913 METH_VARARGS | METH_KEYWORDS, "\n"
914 "Save the CPU and memory state of a Linux guest OS.\n"
915 " dom [int]: Identifier of domain to be saved.\n"
916 " state_file [str]: Name of state file. Must not currently exist.\n"
917 " progress [int, 1]: Bool - display a running progress indication?\n\n"
918 "Returns: [int] 0 on success; -1 on error.\n" },
919 { "plan9_build",
920 (PyCFunction)pyxc_plan9_build,
921 METH_VARARGS | METH_KEYWORDS, "\n"
922 "Build a new Plan 9 guest OS.\n"
923 " dom [long]: Identifier of domain to build into.\n"
924 " image [str]: Name of kernel image file. May be gzipped.\n"
925 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
926 "Returns: [int] 0 on success; -1 on error.\n" },
928 { "linux_restore",
929 (PyCFunction)pyxc_linux_restore,
930 METH_VARARGS | METH_KEYWORDS, "\n"
931 "Restore the CPU and memory state of a Linux guest OS.\n"
932 " state_file [str]: Name of state file. Must not currently exist.\n"
933 " progress [int, 1]: Bool - display a running progress indication?\n\n"
934 "Returns: [int] new domain identifier on success; -1 on error.\n" },
936 { "linux_build",
937 (PyCFunction)pyxc_linux_build,
938 METH_VARARGS | METH_KEYWORDS, "\n"
939 "Build a new Linux guest OS.\n"
940 " dom [int]: Identifier of domain to build into.\n"
941 " image [str]: Name of kernel image file. May be gzipped.\n"
942 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
943 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
944 "Returns: [int] 0 on success; -1 on error.\n" },
946 { "bvtsched_global_set",
947 (PyCFunction)pyxc_bvtsched_global_set,
948 METH_VARARGS | METH_KEYWORDS, "\n"
949 "Set global tuning parameters for Borrowed Virtual Time scheduler.\n"
950 " ctx_allow [int]: Minimal guaranteed quantum.\n\n"
951 "Returns: [int] 0 on success; -1 on error.\n" },
953 { "bvtsched_global_get",
954 (PyCFunction)pyxc_bvtsched_global_get,
955 METH_KEYWORDS, "\n"
956 "Get global tuning parameters for BVT scheduler.\n"
957 "Returns: [dict]:\n"
958 " ctx_allow [int]: context switch allowance\n" },
960 { "bvtsched_domain_set",
961 (PyCFunction)pyxc_bvtsched_domain_set,
962 METH_VARARGS | METH_KEYWORDS, "\n"
963 "Set per-domain tuning parameters for Borrowed Virtual Time scheduler.\n"
964 " dom [int]: Identifier of domain to be tuned.\n"
965 " mcuadv [int]: Proportional to the inverse of the domain's weight.\n"
966 " warpback [int]: Warp ? \n"
967 " warpvalue [int]: How far to warp domain's EVT on unblock.\n"
968 " warpl [int]: How long the domain can run warped.\n"
969 " warpu [int]: How long before the domain can warp again.\n\n"
970 "Returns: [int] 0 on success; -1 on error.\n" },
972 { "bvtsched_domain_get",
973 (PyCFunction)pyxc_bvtsched_domain_get,
974 METH_KEYWORDS, "\n"
975 "Get per-domain tuning parameters under the BVT scheduler.\n"
976 " dom [int]: Identifier of domain to be queried.\n"
977 "Returns [dict]:\n"
978 " domain [int]: Domain ID.\n"
979 " mcuadv [long]: MCU Advance.\n"
980 " warp [long]: Warp.\n"
981 " warpu [long]: Unwarp requirement.\n"
982 " warpl [long]: Warp limit,\n"
983 },
985 { "atropos_domain_set",
986 (PyCFunction)pyxc_atropos_domain_set,
987 METH_KEYWORDS, "\n"
988 "Set the scheduling parameters for a domain when running with Atropos.\n"
989 " dom [int]: domain to set\n"
990 " period [long]: domain's scheduling period\n"
991 " slice [long]: domain's slice per period\n"
992 " latency [long]: wakeup latency hint\n"
993 " xtratime [int]: boolean\n"
994 "Returns: [int] 0 on success; -1 on error.\n" },
996 { "atropos_domain_get",
997 (PyCFunction)pyxc_atropos_domain_get,
998 METH_KEYWORDS, "\n"
999 "Get the current scheduling parameters for a domain when running with\n"
1000 "the Atropos scheduler."
1001 " dom [int]: domain to query\n"
1002 "Returns: [dict]\n"
1003 " domain [int]: domain ID\n"
1004 " period [long]: scheduler period\n"
1005 " slice [long]: CPU reservation per period\n"
1006 " latency [long]: unblocking latency hint\n"
1007 " xtratime [int] : 0 if not using slack time, nonzero otherwise\n" },
1009 { "rrobin_global_set",
1010 (PyCFunction)pyxc_rrobin_global_set,
1011 METH_KEYWORDS, "\n"
1012 "Set Round Robin scheduler slice.\n"
1013 " slice [long]: Round Robin scheduler slice\n"
1014 "Returns: [int] 0 on success, throws an exception on failure\n" },
1016 { "rrobin_global_get",
1017 (PyCFunction)pyxc_rrobin_global_get,
1018 METH_KEYWORDS, "\n"
1019 "Get Round Robin scheduler settings\n"
1020 "Returns [dict]:\n"
1021 " slice [long]: Scheduler time slice.\n" },
1023 { "evtchn_alloc_unbound",
1024 (PyCFunction)pyxc_evtchn_alloc_unbound,
1025 METH_VARARGS | METH_KEYWORDS, "\n"
1026 "Allocate an unbound local port that will await a remote connection.\n"
1027 " dom [int]: Remote domain to accept connections from.\n\n"
1028 "Returns: [int] Unbound event-channel port.\n" },
1030 { "evtchn_bind_interdomain",
1031 (PyCFunction)pyxc_evtchn_bind_interdomain,
1032 METH_VARARGS | METH_KEYWORDS, "\n"
1033 "Open an event channel between two domains.\n"
1034 " dom1 [int, SELF]: First domain to be connected.\n"
1035 " dom2 [int, SELF]: Second domain to be connected.\n\n"
1036 "Returns: [dict] dictionary is empty on failure.\n"
1037 " port1 [int]: Port-id for endpoint at dom1.\n"
1038 " port2 [int]: Port-id for endpoint at dom2.\n" },
1040 { "evtchn_bind_virq",
1041 (PyCFunction)pyxc_evtchn_bind_virq,
1042 METH_VARARGS | METH_KEYWORDS, "\n"
1043 "Bind an event channel to the specified VIRQ.\n"
1044 " virq [int]: VIRQ to bind.\n\n"
1045 "Returns: [int] Bound event-channel port.\n" },
1047 { "evtchn_close",
1048 (PyCFunction)pyxc_evtchn_close,
1049 METH_VARARGS | METH_KEYWORDS, "\n"
1050 "Close an event channel. If interdomain, sets remote end to 'unbound'.\n"
1051 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1052 " port [int]: Port-id of one endpoint of the channel.\n\n"
1053 "Returns: [int] 0 on success; -1 on error.\n" },
1055 { "evtchn_send",
1056 (PyCFunction)pyxc_evtchn_send,
1057 METH_VARARGS | METH_KEYWORDS, "\n"
1058 "Send an event along a locally-connected event channel.\n"
1059 " port [int]: Port-id of a local channel endpoint.\n\n"
1060 "Returns: [int] 0 on success; -1 on error.\n" },
1062 { "evtchn_status",
1063 (PyCFunction)pyxc_evtchn_status,
1064 METH_VARARGS | METH_KEYWORDS, "\n"
1065 "Query the status of an event channel.\n"
1066 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1067 " port [int]: Port-id of one endpoint of the channel.\n\n"
1068 "Returns: [dict] dictionary is empty on failure.\n"
1069 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
1070 " or 'virq'.\n"
1071 "The following are returned if 'status' is 'interdomain':\n"
1072 " dom [int]: Dom-id of remote endpoint.\n"
1073 " port [int]: Port-id of remote endpoint.\n"
1074 "The following are returned if 'status' is 'pirq' or 'virq':\n"
1075 " irq [int]: IRQ number.\n" },
1077 { "physdev_pci_access_modify",
1078 (PyCFunction)pyxc_physdev_pci_access_modify,
1079 METH_VARARGS | METH_KEYWORDS, "\n"
1080 "Allow a domain access to a PCI device\n"
1081 " dom [int]: Identifier of domain to be allowed access.\n"
1082 " bus [int]: PCI bus\n"
1083 " dev [int]: PCI slot\n"
1084 " func [int]: PCI function\n"
1085 " enable [int]: Non-zero means enable access; else disable access\n\n"
1086 "Returns: [int] 0 on success; -1 on error.\n" },
1088 { "readconsolering",
1089 (PyCFunction)pyxc_readconsolering,
1090 METH_VARARGS | METH_KEYWORDS, "\n"
1091 "Read Xen's console ring.\n"
1092 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1093 "Returns: [str] string is empty on failure.\n" },
1095 { "physinfo",
1096 (PyCFunction)pyxc_physinfo,
1097 METH_VARARGS, "\n"
1098 "Get information about the physical host machine\n"
1099 "Returns [dict]: information about the hardware"
1100 " [None]: on failure.\n" },
1102 { "shadow_control",
1103 (PyCFunction)pyxc_shadow_control,
1104 METH_VARARGS | METH_KEYWORDS, "\n"
1105 "Set parameter for shadow pagetable interface\n"
1106 " dom [int]: Identifier of domain.\n"
1107 " op [int, 0]: operation\n\n"
1108 "Returns: [int] 0 on success; -1 on error.\n" },
1110 { "domain_setmaxmem",
1111 (PyCFunction)pyxc_domain_setmaxmem,
1112 METH_VARARGS | METH_KEYWORDS, "\n"
1113 "Set a domain's memory limit\n"
1114 " dom [int]: Identifier of domain.\n"
1115 " maxmem_kb [long]: .\n"
1116 "Returns: [int] 0 on success; -1 on error.\n" },
1118 { NULL, NULL, 0, NULL }
1119 };
1122 /*
1123 * Definitions for the 'Xc' module wrapper.
1124 */
1126 staticforward PyTypeObject PyXcType;
1128 static PyObject *PyXc_new(PyObject *self, PyObject *args)
1130 XcObject *xc;
1132 if ( !PyArg_ParseTuple(args, ":new") )
1133 return NULL;
1135 xc = PyObject_New(XcObject, &PyXcType);
1137 if ( (xc->xc_handle = xc_interface_open()) == -1 )
1139 PyObject_Del((PyObject *)xc);
1140 return PyErr_SetFromErrno(xc_error);
1143 return (PyObject *)xc;
1146 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1148 return Py_FindMethod(pyxc_methods, obj, name);
1151 static void PyXc_dealloc(PyObject *self)
1153 XcObject *xc = (XcObject *)self;
1154 (void)xc_interface_close(xc->xc_handle);
1155 PyObject_Del(self);
1158 static PyTypeObject PyXcType = {
1159 PyObject_HEAD_INIT(&PyType_Type)
1160 0,
1161 "Xc",
1162 sizeof(XcObject),
1163 0,
1164 PyXc_dealloc, /* tp_dealloc */
1165 NULL, /* tp_print */
1166 PyXc_getattr, /* tp_getattr */
1167 NULL, /* tp_setattr */
1168 NULL, /* tp_compare */
1169 NULL, /* tp_repr */
1170 NULL, /* tp_as_number */
1171 NULL, /* tp_as_sequence */
1172 NULL, /* tp_as_mapping */
1173 NULL /* tp_hash */
1174 };
1176 static PyMethodDef PyXc_methods[] = {
1177 { "new", PyXc_new, METH_VARARGS, "Create a new " XENPKG " object." },
1178 { NULL, NULL, 0, NULL }
1179 };
1181 PyMODINIT_FUNC initxc(void)
1183 PyObject *m, *d;
1185 m = Py_InitModule(XENPKG, PyXc_methods);
1187 d = PyModule_GetDict(m);
1188 xc_error = PyErr_NewException(XENPKG ".error", NULL, NULL);
1189 PyDict_SetItemString(d, "error", xc_error);
1191 zero = PyInt_FromLong(0);
1193 /* KAF: This ensures that we get debug output in a timely manner. */
1194 setbuf(stdout, NULL);
1195 setbuf(stderr, NULL);