direct-io.hg

view tools/python/xen/lowlevel/xc/xc.c @ 2693:2584528df9e1

bitkeeper revision 1.1159.123.2 (4177d169N58TtQXn_XJO4xNBKbMQUw)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into freefall.cl.cam.ac.uk:/local/scratch/kaf24/xeno
author kaf24@freefall.cl.cam.ac.uk
date Thu Oct 21 15:10:33 2004 +0000 (2004-10-21)
parents 7bc73b540515 d8e27145f1eb
children 8aa9d487a8dd
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_bind_interdomain(PyObject *self,
466 PyObject *args,
467 PyObject *kwds)
468 {
469 XcObject *xc = (XcObject *)self;
471 u32 dom1 = DOMID_SELF, dom2 = DOMID_SELF;
472 int port1, port2;
474 static char *kwd_list[] = { "dom1", "dom2", NULL };
476 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list,
477 &dom1, &dom2) )
478 return NULL;
480 if ( xc_evtchn_bind_interdomain(xc->xc_handle, dom1,
481 dom2, &port1, &port2) != 0 )
482 return PyErr_SetFromErrno(xc_error);
484 return Py_BuildValue("{s:i,s:i}",
485 "port1", port1,
486 "port2", port2);
487 }
489 static PyObject *pyxc_evtchn_bind_virq(PyObject *self,
490 PyObject *args,
491 PyObject *kwds)
492 {
493 XcObject *xc = (XcObject *)self;
495 int virq, port;
497 static char *kwd_list[] = { "virq", NULL };
499 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &virq) )
500 return NULL;
502 if ( xc_evtchn_bind_virq(xc->xc_handle, virq, &port) != 0 )
503 return PyErr_SetFromErrno(xc_error);
505 return PyInt_FromLong(port);
506 }
508 static PyObject *pyxc_evtchn_close(PyObject *self,
509 PyObject *args,
510 PyObject *kwds)
511 {
512 XcObject *xc = (XcObject *)self;
514 u32 dom = DOMID_SELF;
515 int port;
517 static char *kwd_list[] = { "port", "dom", NULL };
519 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
520 &port, &dom) )
521 return NULL;
523 if ( xc_evtchn_close(xc->xc_handle, dom, port) != 0 )
524 return PyErr_SetFromErrno(xc_error);
526 Py_INCREF(zero);
527 return zero;
528 }
530 static PyObject *pyxc_evtchn_send(PyObject *self,
531 PyObject *args,
532 PyObject *kwds)
533 {
534 XcObject *xc = (XcObject *)self;
536 int port;
538 static char *kwd_list[] = { "port", NULL };
540 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &port) )
541 return NULL;
543 if ( xc_evtchn_send(xc->xc_handle, port) != 0 )
544 return PyErr_SetFromErrno(xc_error);
546 Py_INCREF(zero);
547 return zero;
548 }
550 static PyObject *pyxc_evtchn_status(PyObject *self,
551 PyObject *args,
552 PyObject *kwds)
553 {
554 XcObject *xc = (XcObject *)self;
555 PyObject *dict;
557 u32 dom = DOMID_SELF;
558 int port, ret;
559 xc_evtchn_status_t status;
561 static char *kwd_list[] = { "port", "dom", NULL };
563 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
564 &port, &dom) )
565 return NULL;
567 ret = xc_evtchn_status(xc->xc_handle, dom, port, &status);
568 if ( ret != 0 )
569 return PyErr_SetFromErrno(xc_error);
571 switch ( status.status )
572 {
573 case EVTCHNSTAT_closed:
574 dict = Py_BuildValue("{s:s}",
575 "status", "closed");
576 break;
577 case EVTCHNSTAT_unbound:
578 dict = Py_BuildValue("{s:s}",
579 "status", "unbound");
580 break;
581 case EVTCHNSTAT_interdomain:
582 dict = Py_BuildValue("{s:s,s:i,s:i}",
583 "status", "interdomain",
584 "dom", status.u.interdomain.dom,
585 "port", status.u.interdomain.port);
586 break;
587 case EVTCHNSTAT_pirq:
588 dict = Py_BuildValue("{s:s,s:i}",
589 "status", "pirq",
590 "irq", status.u.pirq);
591 break;
592 case EVTCHNSTAT_virq:
593 dict = Py_BuildValue("{s:s,s:i}",
594 "status", "virq",
595 "irq", status.u.virq);
596 break;
597 default:
598 dict = Py_BuildValue("{}");
599 break;
600 }
602 return dict;
603 }
605 static PyObject *pyxc_physdev_pci_access_modify(PyObject *self,
606 PyObject *args,
607 PyObject *kwds)
608 {
609 XcObject *xc = (XcObject *)self;
610 u32 dom;
611 int bus, dev, func, enable, ret;
613 static char *kwd_list[] = { "dom", "bus", "dev", "func", "enable", NULL };
615 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
616 &dom, &bus, &dev, &func, &enable) )
617 return NULL;
619 ret = xc_physdev_pci_access_modify(
620 xc->xc_handle, dom, bus, dev, func, enable);
621 if ( ret != 0 )
622 return PyErr_SetFromErrno(xc_error);
624 Py_INCREF(zero);
625 return zero;
626 }
628 static PyObject *pyxc_readconsolering(PyObject *self,
629 PyObject *args,
630 PyObject *kwds)
631 {
632 XcObject *xc = (XcObject *)self;
634 unsigned int clear = 0;
635 char str[32768];
636 int ret;
638 static char *kwd_list[] = { "clear", NULL };
640 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
641 return NULL;
643 ret = xc_readconsolering(xc->xc_handle, str, sizeof(str), clear);
644 if ( ret < 0 )
645 return PyErr_SetFromErrno(xc_error);
647 return PyString_FromStringAndSize(str, ret);
648 }
650 static PyObject *pyxc_physinfo(PyObject *self,
651 PyObject *args,
652 PyObject *kwds)
653 {
654 XcObject *xc = (XcObject *)self;
655 xc_physinfo_t info;
657 if ( !PyArg_ParseTuple(args, "") )
658 return NULL;
660 if ( xc_physinfo(xc->xc_handle, &info) != 0 )
661 return PyErr_SetFromErrno(xc_error);
663 return Py_BuildValue("{s:i,s:i,s:l,s:l,s:l}",
664 "ht_per_core", info.ht_per_core,
665 "cores", info.cores,
666 "total_pages", info.total_pages,
667 "free_pages", info.free_pages,
668 "cpu_khz", info.cpu_khz);
669 }
671 static PyObject *pyxc_atropos_domain_set(PyObject *self,
672 PyObject *args,
673 PyObject *kwds)
674 {
675 XcObject *xc = (XcObject *)self;
676 u32 domid;
677 u64 period, slice, latency;
678 int xtratime;
680 static char *kwd_list[] = { "dom", "period", "slice", "latency",
681 "xtratime", NULL };
683 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLi", kwd_list, &domid,
684 &period, &slice, &latency, &xtratime) )
685 return NULL;
687 if ( xc_atropos_domain_set(xc->xc_handle, domid, period, slice,
688 latency, xtratime) != 0 )
689 return PyErr_SetFromErrno(xc_error);
691 Py_INCREF(zero);
692 return zero;
693 }
695 static PyObject *pyxc_atropos_domain_get(PyObject *self,
696 PyObject *args,
697 PyObject *kwds)
698 {
699 XcObject *xc = (XcObject *)self;
700 u32 domid;
701 u64 period, slice, latency;
702 int xtratime;
704 static char *kwd_list[] = { "dom", NULL };
706 if( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &domid) )
707 return NULL;
709 if ( xc_atropos_domain_get( xc->xc_handle, domid, &period,
710 &slice, &latency, &xtratime ) )
711 return PyErr_SetFromErrno(xc_error);
713 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i}",
714 "domain", domid,
715 "period", period,
716 "slice", slice,
717 "latency", latency,
718 "xtratime", xtratime);
719 }
722 static PyObject *pyxc_rrobin_global_set(PyObject *self,
723 PyObject *args,
724 PyObject *kwds)
725 {
726 XcObject *xc = (XcObject *)self;
727 u64 slice;
729 static char *kwd_list[] = { "slice", NULL };
731 if( !PyArg_ParseTupleAndKeywords(args, kwds, "L", kwd_list, &slice) )
732 return NULL;
734 if ( xc_rrobin_global_set(xc->xc_handle, slice) != 0 )
735 return PyErr_SetFromErrno(xc_error);
737 Py_INCREF(zero);
738 return zero;
739 }
741 static PyObject *pyxc_shadow_control(PyObject *self,
742 PyObject *args,
743 PyObject *kwds)
744 {
745 XcObject *xc = (XcObject *)self;
747 u32 dom;
748 int op=0;
750 static char *kwd_list[] = { "dom", "op", NULL };
752 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
753 &dom, &op) )
754 return NULL;
756 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, NULL) < 0 )
757 return PyErr_SetFromErrno(xc_error);
759 Py_INCREF(zero);
760 return zero;
761 }
763 static PyObject *pyxc_rrobin_global_get(PyObject *self,
764 PyObject *args,
765 PyObject *kwds)
766 {
767 XcObject *xc = (XcObject *)self;
768 u64 slice;
770 if ( !PyArg_ParseTuple(args, "") )
771 return NULL;
773 if ( xc_rrobin_global_get(xc->xc_handle, &slice) != 0 )
774 return PyErr_SetFromErrno(xc_error);
776 return Py_BuildValue("{s:L}", "slice", slice);
777 }
779 static PyObject *pyxc_domain_setmaxmem(PyObject *self,
780 PyObject *args,
781 PyObject *kwds)
782 {
783 XcObject *xc = (XcObject *)self;
785 u32 dom;
786 unsigned long maxmem_kb;
788 static char *kwd_list[] = { "dom", "maxmem_kb", NULL };
790 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
791 &dom, &maxmem_kb) )
792 return NULL;
794 if ( xc_domain_setmaxmem(xc->xc_handle, dom, maxmem_kb) != 0 )
795 return PyErr_SetFromErrno(xc_error);
797 Py_INCREF(zero);
798 return zero;
799 }
802 static PyMethodDef pyxc_methods[] = {
803 { "domain_create",
804 (PyCFunction)pyxc_domain_create,
805 METH_VARARGS | METH_KEYWORDS, "\n"
806 "Create a new domain.\n"
807 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
808 " mem_kb [int, 0]: Memory allocation, in kilobytes.\n"
809 "Returns: [int] new domain identifier; -1 on error.\n" },
811 { "domain_pause",
812 (PyCFunction)pyxc_domain_pause,
813 METH_VARARGS | METH_KEYWORDS, "\n"
814 "Temporarily pause execution of a domain.\n"
815 " dom [int]: Identifier of domain to be paused.\n\n"
816 "Returns: [int] 0 on success; -1 on error.\n" },
818 { "domain_unpause",
819 (PyCFunction)pyxc_domain_unpause,
820 METH_VARARGS | METH_KEYWORDS, "\n"
821 "(Re)start execution of a domain.\n"
822 " dom [int]: Identifier of domain to be unpaused.\n\n"
823 "Returns: [int] 0 on success; -1 on error.\n" },
825 { "domain_destroy",
826 (PyCFunction)pyxc_domain_destroy,
827 METH_VARARGS | METH_KEYWORDS, "\n"
828 "Destroy a domain.\n"
829 " dom [int]: Identifier of domain to be destroyed.\n\n"
830 "Returns: [int] 0 on success; -1 on error.\n" },
832 { "domain_pincpu",
833 (PyCFunction)pyxc_domain_pincpu,
834 METH_VARARGS | METH_KEYWORDS, "\n"
835 "Pin a domain to a specified CPU.\n"
836 " dom [int]: Identifier of domain to be pinned.\n"
837 " cpu [int, -1]: CPU to pin to, or -1 to unpin\n\n"
838 "Returns: [int] 0 on success; -1 on error.\n" },
840 { "domain_getinfo",
841 (PyCFunction)pyxc_domain_getinfo,
842 METH_VARARGS | METH_KEYWORDS, "\n"
843 "Get information regarding a set of domains, in increasing id order.\n"
844 " first_dom [int, 0]: First domain to retrieve info about.\n"
845 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
846 " about.\n\n"
847 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
848 " parameter then there was an error, or the end of the\n"
849 " domain-id space was reached.\n"
850 " dom [int]: Identifier of domain to which this info pertains\n"
851 " cpu [int]: CPU to which this domain is bound\n"
852 " dying [int]: Bool - is the domain dying?\n"
853 " crashed [int]: Bool - has the domain crashed?\n"
854 " shutdown [int]: Bool - has the domain shut itself down?\n"
855 " paused [int]: Bool - is the domain paused by control software?\n"
856 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
857 " running [int]: Bool - is the domain currently running?\n"
858 " mem_kb [int]: Memory reservation, in kilobytes\n"
859 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
860 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
861 " shutdown_reason [int]: Numeric code from guest OS, explaining "
862 "reason why it shut itself down.\n" },
864 { "linux_save",
865 (PyCFunction)pyxc_linux_save,
866 METH_VARARGS | METH_KEYWORDS, "\n"
867 "Save the CPU and memory state of a Linux guest OS.\n"
868 " dom [int]: Identifier of domain to be saved.\n"
869 " state_file [str]: Name of state file. Must not currently exist.\n"
870 " progress [int, 1]: Bool - display a running progress indication?\n\n"
871 "Returns: [int] 0 on success; -1 on error.\n" },
873 { "linux_restore",
874 (PyCFunction)pyxc_linux_restore,
875 METH_VARARGS | METH_KEYWORDS, "\n"
876 "Restore the CPU and memory state of a Linux guest OS.\n"
877 " state_file [str]: Name of state file. Must not currently exist.\n"
878 " progress [int, 1]: Bool - display a running progress indication?\n\n"
879 "Returns: [int] new domain identifier on success; -1 on error.\n" },
881 { "linux_build",
882 (PyCFunction)pyxc_linux_build,
883 METH_VARARGS | METH_KEYWORDS, "\n"
884 "Build a new Linux guest OS.\n"
885 " dom [int]: Identifier of domain to build into.\n"
886 " image [str]: Name of kernel image file. May be gzipped.\n"
887 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
888 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
889 "Returns: [int] 0 on success; -1 on error.\n" },
891 { "bvtsched_global_set",
892 (PyCFunction)pyxc_bvtsched_global_set,
893 METH_VARARGS | METH_KEYWORDS, "\n"
894 "Set global tuning parameters for Borrowed Virtual Time scheduler.\n"
895 " ctx_allow [int]: Minimal guaranteed quantum.\n\n"
896 "Returns: [int] 0 on success; -1 on error.\n" },
898 { "bvtsched_global_get",
899 (PyCFunction)pyxc_bvtsched_global_get,
900 METH_KEYWORDS, "\n"
901 "Get global tuning parameters for BVT scheduler.\n"
902 "Returns: [dict]:\n"
903 " ctx_allow [int]: context switch allowance\n" },
905 { "bvtsched_domain_set",
906 (PyCFunction)pyxc_bvtsched_domain_set,
907 METH_VARARGS | METH_KEYWORDS, "\n"
908 "Set per-domain tuning parameters for Borrowed Virtual Time scheduler.\n"
909 " dom [int]: Identifier of domain to be tuned.\n"
910 " mcuadv [int]: Proportional to the inverse of the domain's weight.\n"
911 " warpback [int]: Warp ? \n"
912 " warpvalue [int]: How far to warp domain's EVT on unblock.\n"
913 " warpl [int]: How long the domain can run warped.\n"
914 " warpu [int]: How long before the domain can warp again.\n\n"
915 "Returns: [int] 0 on success; -1 on error.\n" },
917 { "bvtsched_domain_get",
918 (PyCFunction)pyxc_bvtsched_domain_get,
919 METH_KEYWORDS, "\n"
920 "Get per-domain tuning parameters under the BVT scheduler.\n"
921 " dom [int]: Identifier of domain to be queried.\n"
922 "Returns [dict]:\n"
923 " domain [int]: Domain ID.\n"
924 " mcuadv [long]: MCU Advance.\n"
925 " warp [long]: Warp.\n"
926 " warpu [long]: Unwarp requirement.\n"
927 " warpl [long]: Warp limit,\n"
928 },
930 { "atropos_domain_set",
931 (PyCFunction)pyxc_atropos_domain_set,
932 METH_KEYWORDS, "\n"
933 "Set the scheduling parameters for a domain when running with Atropos.\n"
934 " dom [int]: domain to set\n"
935 " period [long]: domain's scheduling period\n"
936 " slice [long]: domain's slice per period\n"
937 " latency [long]: wakeup latency hint\n"
938 " xtratime [int]: boolean\n"
939 "Returns: [int] 0 on success; -1 on error.\n" },
941 { "atropos_domain_get",
942 (PyCFunction)pyxc_atropos_domain_get,
943 METH_KEYWORDS, "\n"
944 "Get the current scheduling parameters for a domain when running with\n"
945 "the Atropos scheduler."
946 " dom [int]: domain to query\n"
947 "Returns: [dict]\n"
948 " domain [int]: domain ID\n"
949 " period [long]: scheduler period\n"
950 " slice [long]: CPU reservation per period\n"
951 " latency [long]: unblocking latency hint\n"
952 " xtratime [int] : 0 if not using slack time, nonzero otherwise\n" },
954 { "rrobin_global_set",
955 (PyCFunction)pyxc_rrobin_global_set,
956 METH_KEYWORDS, "\n"
957 "Set Round Robin scheduler slice.\n"
958 " slice [long]: Round Robin scheduler slice\n"
959 "Returns: [int] 0 on success, throws an exception on failure\n" },
961 { "rrobin_global_get",
962 (PyCFunction)pyxc_rrobin_global_get,
963 METH_KEYWORDS, "\n"
964 "Get Round Robin scheduler settings\n"
965 "Returns [dict]:\n"
966 " slice [long]: Scheduler time slice.\n" },
968 { "evtchn_bind_interdomain",
969 (PyCFunction)pyxc_evtchn_bind_interdomain,
970 METH_VARARGS | METH_KEYWORDS, "\n"
971 "Open an event channel between two domains.\n"
972 " dom1 [int, SELF]: First domain to be connected.\n"
973 " dom2 [int, SELF]: Second domain to be connected.\n\n"
974 "Returns: [dict] dictionary is empty on failure.\n"
975 " port1 [int]: Port-id for endpoint at dom1.\n"
976 " port2 [int]: Port-id for endpoint at dom2.\n" },
978 { "evtchn_bind_virq",
979 (PyCFunction)pyxc_evtchn_bind_virq,
980 METH_VARARGS | METH_KEYWORDS, "\n"
981 "Bind an event channel to the specified VIRQ.\n"
982 " virq [int]: VIRQ to bind.\n\n"
983 "Returns: [int] Bound event-channel port.\n" },
985 { "evtchn_close",
986 (PyCFunction)pyxc_evtchn_close,
987 METH_VARARGS | METH_KEYWORDS, "\n"
988 "Close an event channel.\n"
989 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
990 " port [int]: Port-id of one endpoint of the channel.\n\n"
991 "Returns: [int] 0 on success; -1 on error.\n" },
993 { "evtchn_send",
994 (PyCFunction)pyxc_evtchn_send,
995 METH_VARARGS | METH_KEYWORDS, "\n"
996 "Send an event along a locally-connected event channel.\n"
997 " port [int]: Port-id of a local channel endpoint.\n\n"
998 "Returns: [int] 0 on success; -1 on error.\n" },
1000 { "evtchn_status",
1001 (PyCFunction)pyxc_evtchn_status,
1002 METH_VARARGS | METH_KEYWORDS, "\n"
1003 "Query the status of an event channel.\n"
1004 " dom [int, SELF]: Dom-id of one endpoint of the channel.\n"
1005 " port [int]: Port-id of one endpoint of the channel.\n\n"
1006 "Returns: [dict] dictionary is empty on failure.\n"
1007 " status [str]: 'closed', 'unbound', 'interdomain', 'pirq',"
1008 " or 'virq'.\n"
1009 "The following are returned if 'status' is 'interdomain':\n"
1010 " dom [int]: Dom-id of remote endpoint.\n"
1011 " port [int]: Port-id of remote endpoint.\n"
1012 "The following are returned if 'status' is 'pirq' or 'virq':\n"
1013 " irq [int]: IRQ number.\n" },
1015 { "physdev_pci_access_modify",
1016 (PyCFunction)pyxc_physdev_pci_access_modify,
1017 METH_VARARGS | METH_KEYWORDS, "\n"
1018 "Allow a domain access to a PCI device\n"
1019 " dom [int]: Identifier of domain to be allowed access.\n"
1020 " bus [int]: PCI bus\n"
1021 " dev [int]: PCI slot\n"
1022 " func [int]: PCI function\n"
1023 " enable [int]: Non-zero means enable access; else disable access\n\n"
1024 "Returns: [int] 0 on success; -1 on error.\n" },
1026 { "readconsolering",
1027 (PyCFunction)pyxc_readconsolering,
1028 METH_VARARGS | METH_KEYWORDS, "\n"
1029 "Read Xen's console ring.\n"
1030 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1031 "Returns: [str] string is empty on failure.\n" },
1033 { "physinfo",
1034 (PyCFunction)pyxc_physinfo,
1035 METH_VARARGS, "\n"
1036 "Get information about the physical host machine\n"
1037 "Returns [dict]: information about the hardware"
1038 " [None]: on failure.\n" },
1040 { "shadow_control",
1041 (PyCFunction)pyxc_shadow_control,
1042 METH_VARARGS | METH_KEYWORDS, "\n"
1043 "Set parameter for shadow pagetable interface\n"
1044 " dom [int]: Identifier of domain.\n"
1045 " op [int, 0]: operation\n\n"
1046 "Returns: [int] 0 on success; -1 on error.\n" },
1048 { "domain_setmaxmem",
1049 (PyCFunction)pyxc_domain_setmaxmem,
1050 METH_VARARGS | METH_KEYWORDS, "\n"
1051 "Set a domain's memory limit\n"
1052 " dom [int]: Identifier of domain.\n"
1053 " maxmem_kb [long]: .\n"
1054 "Returns: [int] 0 on success; -1 on error.\n" },
1056 { NULL, NULL, 0, NULL }
1057 };
1060 /*
1061 * Definitions for the 'Xc' module wrapper.
1062 */
1064 staticforward PyTypeObject PyXcType;
1066 static PyObject *PyXc_new(PyObject *self, PyObject *args)
1068 XcObject *xc;
1070 if ( !PyArg_ParseTuple(args, ":new") )
1071 return NULL;
1073 xc = PyObject_New(XcObject, &PyXcType);
1075 if ( (xc->xc_handle = xc_interface_open()) == -1 )
1077 PyObject_Del((PyObject *)xc);
1078 return PyErr_SetFromErrno(xc_error);
1081 return (PyObject *)xc;
1084 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1086 return Py_FindMethod(pyxc_methods, obj, name);
1089 static void PyXc_dealloc(PyObject *self)
1091 XcObject *xc = (XcObject *)self;
1092 (void)xc_interface_close(xc->xc_handle);
1093 PyObject_Del(self);
1096 static PyTypeObject PyXcType = {
1097 PyObject_HEAD_INIT(&PyType_Type)
1098 0,
1099 "Xc",
1100 sizeof(XcObject),
1101 0,
1102 PyXc_dealloc, /* tp_dealloc */
1103 NULL, /* tp_print */
1104 PyXc_getattr, /* tp_getattr */
1105 NULL, /* tp_setattr */
1106 NULL, /* tp_compare */
1107 NULL, /* tp_repr */
1108 NULL, /* tp_as_number */
1109 NULL, /* tp_as_sequence */
1110 NULL, /* tp_as_mapping */
1111 NULL /* tp_hash */
1112 };
1114 static PyMethodDef PyXc_methods[] = {
1115 { "new", PyXc_new, METH_VARARGS, "Create a new " XENPKG " object." },
1116 { NULL, NULL, 0, NULL }
1117 };
1119 PyMODINIT_FUNC initxc(void)
1121 PyObject *m, *d;
1123 m = Py_InitModule(XENPKG, PyXc_methods);
1125 d = PyModule_GetDict(m);
1126 xc_error = PyErr_NewException(XENPKG ".error", NULL, NULL);
1127 PyDict_SetItemString(d, "error", xc_error);
1129 zero = PyInt_FromLong(0);
1131 /* KAF: This ensures that we get debug output in a timely manner. */
1132 setbuf(stdout, NULL);
1133 setbuf(stderr, NULL);