direct-io.hg

view tools/python/xen/lowlevel/xc/xc.c @ 2714:77fbe6c094f2

bitkeeper revision 1.1159.1.261 (41792d94c9Ck-m72kpZkL5_a2aSoZA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-24dom0
author cl349@freefall.cl.cam.ac.uk
date Fri Oct 22 15:56:04 2004 +0000 (2004-10-22)
parents 8aa9d487a8dd 06527e1d6757
children f36de623daa0 6b1aa25ff284 52e7357311c2
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_bvtsched_global_set(PyObject *self,
370 PyObject *args,
371 PyObject *kwds)
372 {
373 XcObject *xc = (XcObject *)self;
375 unsigned long ctx_allow;
377 static char *kwd_list[] = { "ctx_allow", NULL };
379 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "l", kwd_list, &ctx_allow) )
380 return NULL;
382 if ( xc_bvtsched_global_set(xc->xc_handle, ctx_allow) != 0 )
383 return PyErr_SetFromErrno(xc_error);
385 Py_INCREF(zero);
386 return zero;
387 }
389 static PyObject *pyxc_bvtsched_global_get(PyObject *self,
390 PyObject *args,
391 PyObject *kwds)
392 {
393 XcObject *xc = (XcObject *)self;
395 unsigned long ctx_allow;
397 if ( !PyArg_ParseTuple(args, "") )
398 return NULL;
400 if ( xc_bvtsched_global_get(xc->xc_handle, &ctx_allow) != 0 )
401 return PyErr_SetFromErrno(xc_error);
403 return Py_BuildValue("s:l", "ctx_allow", ctx_allow);
404 }
406 static PyObject *pyxc_bvtsched_domain_set(PyObject *self,
407 PyObject *args,
408 PyObject *kwds)
409 {
410 XcObject *xc = (XcObject *)self;
412 u32 dom;
413 u32 mcuadv;
414 int warpback;
415 s32 warpvalue;
416 long long warpl;
417 long long warpu;
419 static char *kwd_list[] = { "dom", "mcuadv", "warpback", "warpvalue",
420 "warpl", "warpu", NULL };
422 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiiLL", kwd_list,
423 &dom, &mcuadv, &warpback, &warpvalue,
424 &warpl, &warpu) )
425 return NULL;
427 if ( xc_bvtsched_domain_set(xc->xc_handle, dom, mcuadv,
428 warpback, warpvalue, warpl, warpu) != 0 )
429 return PyErr_SetFromErrno(xc_error);
431 Py_INCREF(zero);
432 return zero;
433 }
435 static PyObject *pyxc_bvtsched_domain_get(PyObject *self,
436 PyObject *args,
437 PyObject *kwds)
438 {
439 XcObject *xc = (XcObject *)self;
440 u32 dom;
441 u32 mcuadv;
442 int warpback;
443 s32 warpvalue;
444 long long warpl;
445 long long warpu;
447 static char *kwd_list[] = { "dom", NULL };
449 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
450 return NULL;
452 if ( xc_bvtsched_domain_get(xc->xc_handle, dom, &mcuadv, &warpback,
453 &warpvalue, &warpl, &warpu) != 0 )
454 return PyErr_SetFromErrno(xc_error);
456 return Py_BuildValue("{s:i,s:l,s:l,s:l,s:l}",
457 "domain", dom,
458 "mcuadv", mcuadv,
459 "warpback", warpback,
460 "warpvalue", warpvalue,
461 "warpl", warpl,
462 "warpu", warpu);
463 }
465 static PyObject *pyxc_evtchn_alloc_unbound(PyObject *self,
466 PyObject *args,
467 PyObject *kwds)
468 {
469 XcObject *xc = (XcObject *)self;
471 u32 dom;
472 int port;
474 static char *kwd_list[] = { "dom", NULL };
476 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
477 return NULL;
479 if ( xc_evtchn_alloc_unbound(xc->xc_handle, dom, &port) != 0 )
480 return PyErr_SetFromErrno(xc_error);
482 return PyInt_FromLong(port);
483 }
485 static PyObject *pyxc_evtchn_bind_interdomain(PyObject *self,
486 PyObject *args,
487 PyObject *kwds)
488 {
489 XcObject *xc = (XcObject *)self;
491 u32 dom1 = DOMID_SELF, dom2 = DOMID_SELF;
492 int port1 = 0, port2 = 0;
494 static char *kwd_list[] = { "dom1", "dom2", "port1", "port2", NULL };
496 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiii", kwd_list,
497 &dom1, &dom2, &port1, &port2) )
498 return NULL;
500 if ( xc_evtchn_bind_interdomain(xc->xc_handle, dom1,
501 dom2, &port1, &port2) != 0 )
502 return PyErr_SetFromErrno(xc_error);
504 return Py_BuildValue("{s:i,s:i}",
505 "port1", port1,
506 "port2", port2);
507 }
509 static PyObject *pyxc_evtchn_bind_virq(PyObject *self,
510 PyObject *args,
511 PyObject *kwds)
512 {
513 XcObject *xc = (XcObject *)self;
515 int virq, port;
517 static char *kwd_list[] = { "virq", NULL };
519 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &virq) )
520 return NULL;
522 if ( xc_evtchn_bind_virq(xc->xc_handle, virq, &port) != 0 )
523 return PyErr_SetFromErrno(xc_error);
525 return PyInt_FromLong(port);
526 }
528 static PyObject *pyxc_evtchn_close(PyObject *self,
529 PyObject *args,
530 PyObject *kwds)
531 {
532 XcObject *xc = (XcObject *)self;
534 u32 dom = DOMID_SELF;
535 int port;
537 static char *kwd_list[] = { "port", "dom", NULL };
539 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
540 &port, &dom) )
541 return NULL;
543 if ( xc_evtchn_close(xc->xc_handle, dom, port) != 0 )
544 return PyErr_SetFromErrno(xc_error);
546 Py_INCREF(zero);
547 return zero;
548 }
550 static PyObject *pyxc_evtchn_send(PyObject *self,
551 PyObject *args,
552 PyObject *kwds)
553 {
554 XcObject *xc = (XcObject *)self;
556 int port;
558 static char *kwd_list[] = { "port", NULL };
560 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &port) )
561 return NULL;
563 if ( xc_evtchn_send(xc->xc_handle, port) != 0 )
564 return PyErr_SetFromErrno(xc_error);
566 Py_INCREF(zero);
567 return zero;
568 }
570 static PyObject *pyxc_evtchn_status(PyObject *self,
571 PyObject *args,
572 PyObject *kwds)
573 {
574 XcObject *xc = (XcObject *)self;
575 PyObject *dict;
577 u32 dom = DOMID_SELF;
578 int port, ret;
579 xc_evtchn_status_t status;
581 static char *kwd_list[] = { "port", "dom", NULL };
583 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
584 &port, &dom) )
585 return NULL;
587 ret = xc_evtchn_status(xc->xc_handle, dom, port, &status);
588 if ( ret != 0 )
589 return PyErr_SetFromErrno(xc_error);
591 switch ( status.status )
592 {
593 case EVTCHNSTAT_closed:
594 dict = Py_BuildValue("{s:s}",
595 "status", "closed");
596 break;
597 case EVTCHNSTAT_unbound:
598 dict = Py_BuildValue("{s:s}",
599 "status", "unbound");
600 break;
601 case EVTCHNSTAT_interdomain:
602 dict = Py_BuildValue("{s:s,s:i,s:i}",
603 "status", "interdomain",
604 "dom", status.u.interdomain.dom,
605 "port", status.u.interdomain.port);
606 break;
607 case EVTCHNSTAT_pirq:
608 dict = Py_BuildValue("{s:s,s:i}",
609 "status", "pirq",
610 "irq", status.u.pirq);
611 break;
612 case EVTCHNSTAT_virq:
613 dict = Py_BuildValue("{s:s,s:i}",
614 "status", "virq",
615 "irq", status.u.virq);
616 break;
617 default:
618 dict = Py_BuildValue("{}");
619 break;
620 }
622 return dict;
623 }
625 static PyObject *pyxc_physdev_pci_access_modify(PyObject *self,
626 PyObject *args,
627 PyObject *kwds)
628 {
629 XcObject *xc = (XcObject *)self;
630 u32 dom;
631 int bus, dev, func, enable, ret;
633 static char *kwd_list[] = { "dom", "bus", "dev", "func", "enable", NULL };
635 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
636 &dom, &bus, &dev, &func, &enable) )
637 return NULL;
639 ret = xc_physdev_pci_access_modify(
640 xc->xc_handle, dom, bus, dev, func, enable);
641 if ( ret != 0 )
642 return PyErr_SetFromErrno(xc_error);
644 Py_INCREF(zero);
645 return zero;
646 }
648 static PyObject *pyxc_readconsolering(PyObject *self,
649 PyObject *args,
650 PyObject *kwds)
651 {
652 XcObject *xc = (XcObject *)self;
654 unsigned int clear = 0;
655 char str[32768];
656 int ret;
658 static char *kwd_list[] = { "clear", NULL };
660 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
661 return NULL;
663 ret = xc_readconsolering(xc->xc_handle, str, sizeof(str), clear);
664 if ( ret < 0 )
665 return PyErr_SetFromErrno(xc_error);
667 return PyString_FromStringAndSize(str, ret);
668 }
670 static PyObject *pyxc_physinfo(PyObject *self,
671 PyObject *args,
672 PyObject *kwds)
673 {
674 XcObject *xc = (XcObject *)self;
675 xc_physinfo_t info;
677 if ( !PyArg_ParseTuple(args, "") )
678 return NULL;
680 if ( xc_physinfo(xc->xc_handle, &info) != 0 )
681 return PyErr_SetFromErrno(xc_error);
683 return Py_BuildValue("{s:i,s:i,s:l,s:l,s:l}",
684 "ht_per_core", info.ht_per_core,
685 "cores", info.cores,
686 "total_pages", info.total_pages,
687 "free_pages", info.free_pages,
688 "cpu_khz", info.cpu_khz);
689 }
691 static PyObject *pyxc_atropos_domain_set(PyObject *self,
692 PyObject *args,
693 PyObject *kwds)
694 {
695 XcObject *xc = (XcObject *)self;
696 u32 domid;
697 u64 period, slice, latency;
698 int xtratime;
700 static char *kwd_list[] = { "dom", "period", "slice", "latency",
701 "xtratime", NULL };
703 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLi", kwd_list, &domid,
704 &period, &slice, &latency, &xtratime) )
705 return NULL;
707 if ( xc_atropos_domain_set(xc->xc_handle, domid, period, slice,
708 latency, xtratime) != 0 )
709 return PyErr_SetFromErrno(xc_error);
711 Py_INCREF(zero);
712 return zero;
713 }
715 static PyObject *pyxc_atropos_domain_get(PyObject *self,
716 PyObject *args,
717 PyObject *kwds)
718 {
719 XcObject *xc = (XcObject *)self;
720 u32 domid;
721 u64 period, slice, latency;
722 int xtratime;
724 static char *kwd_list[] = { "dom", NULL };
726 if( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &domid) )
727 return NULL;
729 if ( xc_atropos_domain_get( xc->xc_handle, domid, &period,
730 &slice, &latency, &xtratime ) )
731 return PyErr_SetFromErrno(xc_error);
733 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i}",
734 "domain", domid,
735 "period", period,
736 "slice", slice,
737 "latency", latency,
738 "xtratime", xtratime);
739 }
742 static PyObject *pyxc_rrobin_global_set(PyObject *self,
743 PyObject *args,
744 PyObject *kwds)
745 {
746 XcObject *xc = (XcObject *)self;
747 u64 slice;
749 static char *kwd_list[] = { "slice", NULL };
751 if( !PyArg_ParseTupleAndKeywords(args, kwds, "L", kwd_list, &slice) )
752 return NULL;
754 if ( xc_rrobin_global_set(xc->xc_handle, slice) != 0 )
755 return PyErr_SetFromErrno(xc_error);
757 Py_INCREF(zero);
758 return zero;
759 }
761 static PyObject *pyxc_shadow_control(PyObject *self,
762 PyObject *args,
763 PyObject *kwds)
764 {
765 XcObject *xc = (XcObject *)self;
767 u32 dom;
768 int op=0;
770 static char *kwd_list[] = { "dom", "op", NULL };
772 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
773 &dom, &op) )
774 return NULL;
776 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, NULL) < 0 )
777 return PyErr_SetFromErrno(xc_error);
779 Py_INCREF(zero);
780 return zero;
781 }
783 static PyObject *pyxc_rrobin_global_get(PyObject *self,
784 PyObject *args,
785 PyObject *kwds)
786 {
787 XcObject *xc = (XcObject *)self;
788 u64 slice;
790 if ( !PyArg_ParseTuple(args, "") )
791 return NULL;
793 if ( xc_rrobin_global_get(xc->xc_handle, &slice) != 0 )
794 return PyErr_SetFromErrno(xc_error);
796 return Py_BuildValue("{s:L}", "slice", slice);
797 }
799 static PyObject *pyxc_domain_setmaxmem(PyObject *self,
800 PyObject *args,
801 PyObject *kwds)
802 {
803 XcObject *xc = (XcObject *)self;
805 u32 dom;
806 unsigned long maxmem_kb;
808 static char *kwd_list[] = { "dom", "maxmem_kb", NULL };
810 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
811 &dom, &maxmem_kb) )
812 return NULL;
814 if ( xc_domain_setmaxmem(xc->xc_handle, dom, maxmem_kb) != 0 )
815 return PyErr_SetFromErrno(xc_error);
817 Py_INCREF(zero);
818 return zero;
819 }
822 static PyMethodDef pyxc_methods[] = {
823 { "domain_create",
824 (PyCFunction)pyxc_domain_create,
825 METH_VARARGS | METH_KEYWORDS, "\n"
826 "Create a new domain.\n"
827 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
828 " mem_kb [int, 0]: Memory allocation, in kilobytes.\n"
829 "Returns: [int] new domain identifier; -1 on error.\n" },
831 { "domain_pause",
832 (PyCFunction)pyxc_domain_pause,
833 METH_VARARGS | METH_KEYWORDS, "\n"
834 "Temporarily pause execution of a domain.\n"
835 " dom [int]: Identifier of domain to be paused.\n\n"
836 "Returns: [int] 0 on success; -1 on error.\n" },
838 { "domain_unpause",
839 (PyCFunction)pyxc_domain_unpause,
840 METH_VARARGS | METH_KEYWORDS, "\n"
841 "(Re)start execution of a domain.\n"
842 " dom [int]: Identifier of domain to be unpaused.\n\n"
843 "Returns: [int] 0 on success; -1 on error.\n" },
845 { "domain_destroy",
846 (PyCFunction)pyxc_domain_destroy,
847 METH_VARARGS | METH_KEYWORDS, "\n"
848 "Destroy a domain.\n"
849 " dom [int]: Identifier of domain to be destroyed.\n\n"
850 "Returns: [int] 0 on success; -1 on error.\n" },
852 { "domain_pincpu",
853 (PyCFunction)pyxc_domain_pincpu,
854 METH_VARARGS | METH_KEYWORDS, "\n"
855 "Pin a domain to a specified CPU.\n"
856 " dom [int]: Identifier of domain to be pinned.\n"
857 " cpu [int, -1]: CPU to pin to, or -1 to unpin\n\n"
858 "Returns: [int] 0 on success; -1 on error.\n" },
860 { "domain_getinfo",
861 (PyCFunction)pyxc_domain_getinfo,
862 METH_VARARGS | METH_KEYWORDS, "\n"
863 "Get information regarding a set of domains, in increasing id order.\n"
864 " first_dom [int, 0]: First domain to retrieve info about.\n"
865 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
866 " about.\n\n"
867 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
868 " parameter then there was an error, or the end of the\n"
869 " domain-id space was reached.\n"
870 " dom [int]: Identifier of domain to which this info pertains\n"
871 " cpu [int]: CPU to which this domain is bound\n"
872 " dying [int]: Bool - is the domain dying?\n"
873 " crashed [int]: Bool - has the domain crashed?\n"
874 " shutdown [int]: Bool - has the domain shut itself down?\n"
875 " paused [int]: Bool - is the domain paused by control software?\n"
876 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
877 " running [int]: Bool - is the domain currently running?\n"
878 " mem_kb [int]: Memory reservation, in kilobytes\n"
879 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
880 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
881 " shutdown_reason [int]: Numeric code from guest OS, explaining "
882 "reason why it shut itself down.\n" },
884 { "linux_save",
885 (PyCFunction)pyxc_linux_save,
886 METH_VARARGS | METH_KEYWORDS, "\n"
887 "Save the CPU and memory state of a Linux guest OS.\n"
888 " dom [int]: Identifier of domain to be saved.\n"
889 " state_file [str]: Name of state file. Must not currently exist.\n"
890 " progress [int, 1]: Bool - display a running progress indication?\n\n"
891 "Returns: [int] 0 on success; -1 on error.\n" },
893 { "linux_restore",
894 (PyCFunction)pyxc_linux_restore,
895 METH_VARARGS | METH_KEYWORDS, "\n"
896 "Restore the CPU and memory state of a Linux guest OS.\n"
897 " state_file [str]: Name of state file. Must not currently exist.\n"
898 " progress [int, 1]: Bool - display a running progress indication?\n\n"
899 "Returns: [int] new domain identifier on success; -1 on error.\n" },
901 { "linux_build",
902 (PyCFunction)pyxc_linux_build,
903 METH_VARARGS | METH_KEYWORDS, "\n"
904 "Build a new Linux guest OS.\n"
905 " dom [int]: Identifier of domain to build into.\n"
906 " image [str]: Name of kernel image file. May be gzipped.\n"
907 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
908 " cmdline [str, n/a]: Kernel parameters, if any.\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_KEYWORDS, "\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_KEYWORDS, "\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 { "atropos_domain_set",
951 (PyCFunction)pyxc_atropos_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]: wakeup latency hint\n"
958 " xtratime [int]: boolean\n"
959 "Returns: [int] 0 on success; -1 on error.\n" },
961 { "atropos_domain_get",
962 (PyCFunction)pyxc_atropos_domain_get,
963 METH_KEYWORDS, "\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]: unblocking latency hint\n"
972 " xtratime [int] : 0 if not using slack time, nonzero otherwise\n" },
974 { "rrobin_global_set",
975 (PyCFunction)pyxc_rrobin_global_set,
976 METH_KEYWORDS, "\n"
977 "Set Round Robin scheduler slice.\n"
978 " slice [long]: Round Robin scheduler slice\n"
979 "Returns: [int] 0 on success, throws an exception on failure\n" },
981 { "rrobin_global_get",
982 (PyCFunction)pyxc_rrobin_global_get,
983 METH_KEYWORDS, "\n"
984 "Get Round Robin scheduler settings\n"
985 "Returns [dict]:\n"
986 " slice [long]: Scheduler time slice.\n" },
988 { "evtchn_alloc_unbound",
989 (PyCFunction)pyxc_evtchn_alloc_unbound,
990 METH_VARARGS | METH_KEYWORDS, "\n"
991 "Allocate an unbound local port that will await a remote connection.\n"
992 " dom [int]: Remote domain to accept connections from.\n\n"
993 "Returns: [int] Unbound event-channel port.\n" },
995 { "evtchn_bind_interdomain",
996 (PyCFunction)pyxc_evtchn_bind_interdomain,
997 METH_VARARGS | METH_KEYWORDS, "\n"
998 "Open an event channel between two domains.\n"
999 " dom1 [int, SELF]: First domain to be connected.\n"
1000 " dom2 [int, SELF]: Second domain to be connected.\n\n"
1001 "Returns: [dict] dictionary is empty on failure.\n"
1002 " port1 [int]: Port-id for endpoint at dom1.\n"
1003 " port2 [int]: Port-id for endpoint at dom2.\n" },
1005 { "evtchn_bind_virq",
1006 (PyCFunction)pyxc_evtchn_bind_virq,
1007 METH_VARARGS | METH_KEYWORDS, "\n"
1008 "Bind an event channel to the specified VIRQ.\n"
1009 " virq [int]: VIRQ to bind.\n\n"
1010 "Returns: [int] Bound event-channel port.\n" },
1012 { "evtchn_close",
1013 (PyCFunction)pyxc_evtchn_close,
1014 METH_VARARGS | METH_KEYWORDS, "\n"
1015 "Close an event channel. If interdomain, sets remote end to 'unbound'.\n"
1016 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1017 " port [int]: Port-id of one endpoint of the channel.\n\n"
1018 "Returns: [int] 0 on success; -1 on error.\n" },
1020 { "evtchn_send",
1021 (PyCFunction)pyxc_evtchn_send,
1022 METH_VARARGS | METH_KEYWORDS, "\n"
1023 "Send an event along a locally-connected event channel.\n"
1024 " port [int]: Port-id of a local channel endpoint.\n\n"
1025 "Returns: [int] 0 on success; -1 on error.\n" },
1027 { "evtchn_status",
1028 (PyCFunction)pyxc_evtchn_status,
1029 METH_VARARGS | METH_KEYWORDS, "\n"
1030 "Query the status of an event channel.\n"
1031 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1032 " port [int]: Port-id of one endpoint of the channel.\n\n"
1033 "Returns: [dict] dictionary is empty on failure.\n"
1034 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
1035 " or 'virq'.\n"
1036 "The following are returned if 'status' is 'interdomain':\n"
1037 " dom [int]: Dom-id of remote endpoint.\n"
1038 " port [int]: Port-id of remote endpoint.\n"
1039 "The following are returned if 'status' is 'pirq' or 'virq':\n"
1040 " irq [int]: IRQ number.\n" },
1042 { "physdev_pci_access_modify",
1043 (PyCFunction)pyxc_physdev_pci_access_modify,
1044 METH_VARARGS | METH_KEYWORDS, "\n"
1045 "Allow a domain access to a PCI device\n"
1046 " dom [int]: Identifier of domain to be allowed access.\n"
1047 " bus [int]: PCI bus\n"
1048 " dev [int]: PCI slot\n"
1049 " func [int]: PCI function\n"
1050 " enable [int]: Non-zero means enable access; else disable access\n\n"
1051 "Returns: [int] 0 on success; -1 on error.\n" },
1053 { "readconsolering",
1054 (PyCFunction)pyxc_readconsolering,
1055 METH_VARARGS | METH_KEYWORDS, "\n"
1056 "Read Xen's console ring.\n"
1057 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1058 "Returns: [str] string is empty on failure.\n" },
1060 { "physinfo",
1061 (PyCFunction)pyxc_physinfo,
1062 METH_VARARGS, "\n"
1063 "Get information about the physical host machine\n"
1064 "Returns [dict]: information about the hardware"
1065 " [None]: on failure.\n" },
1067 { "shadow_control",
1068 (PyCFunction)pyxc_shadow_control,
1069 METH_VARARGS | METH_KEYWORDS, "\n"
1070 "Set parameter for shadow pagetable interface\n"
1071 " dom [int]: Identifier of domain.\n"
1072 " op [int, 0]: operation\n\n"
1073 "Returns: [int] 0 on success; -1 on error.\n" },
1075 { "domain_setmaxmem",
1076 (PyCFunction)pyxc_domain_setmaxmem,
1077 METH_VARARGS | METH_KEYWORDS, "\n"
1078 "Set a domain's memory limit\n"
1079 " dom [int]: Identifier of domain.\n"
1080 " maxmem_kb [long]: .\n"
1081 "Returns: [int] 0 on success; -1 on error.\n" },
1083 { NULL, NULL, 0, NULL }
1084 };
1087 /*
1088 * Definitions for the 'Xc' module wrapper.
1089 */
1091 staticforward PyTypeObject PyXcType;
1093 static PyObject *PyXc_new(PyObject *self, PyObject *args)
1095 XcObject *xc;
1097 if ( !PyArg_ParseTuple(args, ":new") )
1098 return NULL;
1100 xc = PyObject_New(XcObject, &PyXcType);
1102 if ( (xc->xc_handle = xc_interface_open()) == -1 )
1104 PyObject_Del((PyObject *)xc);
1105 return PyErr_SetFromErrno(xc_error);
1108 return (PyObject *)xc;
1111 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1113 return Py_FindMethod(pyxc_methods, obj, name);
1116 static void PyXc_dealloc(PyObject *self)
1118 XcObject *xc = (XcObject *)self;
1119 (void)xc_interface_close(xc->xc_handle);
1120 PyObject_Del(self);
1123 static PyTypeObject PyXcType = {
1124 PyObject_HEAD_INIT(&PyType_Type)
1125 0,
1126 "Xc",
1127 sizeof(XcObject),
1128 0,
1129 PyXc_dealloc, /* tp_dealloc */
1130 NULL, /* tp_print */
1131 PyXc_getattr, /* tp_getattr */
1132 NULL, /* tp_setattr */
1133 NULL, /* tp_compare */
1134 NULL, /* tp_repr */
1135 NULL, /* tp_as_number */
1136 NULL, /* tp_as_sequence */
1137 NULL, /* tp_as_mapping */
1138 NULL /* tp_hash */
1139 };
1141 static PyMethodDef PyXc_methods[] = {
1142 { "new", PyXc_new, METH_VARARGS, "Create a new " XENPKG " object." },
1143 { NULL, NULL, 0, NULL }
1144 };
1146 PyMODINIT_FUNC initxc(void)
1148 PyObject *m, *d;
1150 m = Py_InitModule(XENPKG, PyXc_methods);
1152 d = PyModule_GetDict(m);
1153 xc_error = PyErr_NewException(XENPKG ".error", NULL, NULL);
1154 PyDict_SetItemString(d, "error", xc_error);
1156 zero = PyInt_FromLong(0);
1158 /* KAF: This ensures that we get debug output in a timely manner. */
1159 setbuf(stdout, NULL);
1160 setbuf(stderr, NULL);