1 /* OCaml bindings for libvirt.
2 * (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /* Please read libvirt/README file. */
22 /*----------------------------------------------------------------------*/
25 ocaml_libvirt_get_version (value driverv, value unit)
27 CAMLparam2 (driverv, unit);
29 const char *driver = Optstring_val (driverv);
30 unsigned long libVer, typeVer = 0, *typeVer_ptr;
33 typeVer_ptr = driver ? &typeVer : NULL;
34 NONBLOCKING (r = virGetVersion (&libVer, driver, typeVer_ptr));
35 CHECK_ERROR (r == -1, NULL, "virGetVersion");
37 rv = caml_alloc_tuple (2);
38 Store_field (rv, 0, Val_int (libVer));
39 Store_field (rv, 1, Val_int (typeVer));
43 /*----------------------------------------------------------------------*/
45 /* Connection object. */
48 ocaml_libvirt_connect_open (value namev, value unit)
50 CAMLparam2 (namev, unit);
52 const char *name = Optstring_val (namev);
55 NONBLOCKING (conn = virConnectOpen (name));
56 CHECK_ERROR (!conn, NULL, "virConnectOpen");
58 rv = Val_connect (conn);
64 ocaml_libvirt_connect_open_readonly (value namev, value unit)
66 CAMLparam2 (namev, unit);
68 const char *name = Optstring_val (namev);
71 NONBLOCKING (conn = virConnectOpenReadOnly (name));
72 CHECK_ERROR (!conn, NULL, "virConnectOpen");
74 rv = Val_connect (conn);
80 ocaml_libvirt_connect_get_version (value connv)
83 virConnectPtr conn = Connect_val (connv);
87 NONBLOCKING (r = virConnectGetVersion (conn, &hvVer));
88 CHECK_ERROR (r == -1, conn, "virConnectGetVersion");
90 CAMLreturn (Val_int (hvVer));
94 ocaml_libvirt_connect_get_max_vcpus (value connv, value typev)
96 CAMLparam2 (connv, typev);
97 virConnectPtr conn = Connect_val (connv);
98 const char *type = Optstring_val (typev);
101 NONBLOCKING (r = virConnectGetMaxVcpus (conn, type));
102 CHECK_ERROR (r == -1, conn, "virConnectGetMaxVcpus");
104 CAMLreturn (Val_int (r));
108 ocaml_libvirt_connect_get_node_info (value connv)
112 virConnectPtr conn = Connect_val (connv);
116 NONBLOCKING (r = virNodeGetInfo (conn, &info));
117 CHECK_ERROR (r == -1, conn, "virNodeGetInfo");
119 rv = caml_alloc (8, 0);
120 v = caml_copy_string (info.model); Store_field (rv, 0, v);
121 v = caml_copy_int64 (info.memory); Store_field (rv, 1, v);
122 Store_field (rv, 2, Val_int (info.cpus));
123 Store_field (rv, 3, Val_int (info.mhz));
124 Store_field (rv, 4, Val_int (info.nodes));
125 Store_field (rv, 5, Val_int (info.sockets));
126 Store_field (rv, 6, Val_int (info.cores));
127 Store_field (rv, 7, Val_int (info.threads));
132 #ifdef HAVE_WEAK_SYMBOLS
133 #ifdef HAVE_VIRNODEGETFREEMEMORY
134 extern unsigned long long virNodeGetFreeMemory (virConnectPtr conn)
135 __attribute__((weak));
140 ocaml_libvirt_connect_node_get_free_memory (value connv)
142 #ifdef HAVE_VIRNODEGETFREEMEMORY
145 virConnectPtr conn = Connect_val (connv);
146 unsigned long long r;
148 WEAK_SYMBOL_CHECK (virNodeGetFreeMemory);
149 NONBLOCKING (r = virNodeGetFreeMemory (conn));
150 CHECK_ERROR (r == 0, conn, "virNodeGetFreeMemory");
152 rv = caml_copy_int64 ((int64) r);
155 not_supported ("virNodeGetFreeMemory");
159 #ifdef HAVE_WEAK_SYMBOLS
160 #ifdef HAVE_VIRNODEGETCELLSFREEMEMORY
161 extern int virNodeGetCellsFreeMemory (virConnectPtr conn,
162 unsigned long long *freeMems,
163 int startCell, int maxCells)
164 __attribute__((weak));
169 ocaml_libvirt_connect_node_get_cells_free_memory (value connv,
170 value startv, value maxv)
172 #ifdef HAVE_VIRNODEGETCELLSFREEMEMORY
173 CAMLparam3 (connv, startv, maxv);
175 virConnectPtr conn = Connect_val (connv);
176 int start = Int_val (startv);
177 int max = Int_val (maxv);
179 unsigned long long freemems[max];
181 WEAK_SYMBOL_CHECK (virNodeGetCellsFreeMemory);
182 NONBLOCKING (r = virNodeGetCellsFreeMemory (conn, freemems, start, max));
183 CHECK_ERROR (r == -1, conn, "virNodeGetCellsFreeMemory");
185 rv = caml_alloc (r, 0);
186 for (i = 0; i < r; ++i) {
187 iv = caml_copy_int64 ((int64) freemems[i]);
188 Store_field (rv, i, iv);
193 not_supported ("virNodeGetCellsFreeMemory");
197 #ifdef HAVE_WEAK_SYMBOLS
198 #ifdef HAVE_VIRCONNECTLISTALLDOMAINS
199 extern int virConnectListAllDomains (virConnectPtr conn,
200 virDomainPtr **domains,
201 virDomainInfo **infos,
202 unsigned long stateflags,
204 __attribute__((weak));
209 ocaml_libvirt_connect_list_all_domains (value connv,
213 #ifdef HAVE_VIRCONNECTLISTALLDOMAINS
214 CAMLparam3 (connv, wantinfov, flagsv);
215 CAMLlocal4 (flagv, rv, rv1, rv2);
217 virConnectPtr conn = Connect_val (connv);
218 virDomainPtr *domains;
219 virDomainInfo *infos;
220 int want_info, i, r, flag;
221 unsigned long flags = 0;
224 if (wantinfov == Val_int (0)) /* None == true */
227 want_info = Bool_val (Field (wantinfov, 0));
229 /* Iterate over the list of flags. */
230 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
231 flagv = Field (flagsv, 0);
232 flag = Int_val (flagv);
234 case 0: flags |= VIR_DOMAIN_LIST_ACTIVE; break;
235 case 1: flags |= VIR_DOMAIN_LIST_INACTIVE; break;
236 case 2: flags |= VIR_DOMAIN_LIST_ALL; break;
240 WEAK_SYMBOL_CHECK (virConnectListAllDomains);
241 NONBLOCKING (r = virConnectListAllDomains (conn, &domains,
242 want_info ? &infos : NULL,
244 CHECK_ERROR (r == -1, conn, "virConnectListAllDomains");
246 /* Convert the result into a pair of arrays. */
247 rv1 = caml_alloc (r, 0);
248 for (i = 0; i < r; ++i) {
249 v1 = Val_domain (domains[i], connv);
250 Store_field (rv1, i, v1);
255 rv2 = caml_alloc (r, 0);
257 for (i = 0; i < r; ++i) {
258 v1 = caml_alloc (5, 0);
259 Store_field (v1, 0, Val_int (infos[i].state));
260 v2 = caml_copy_int64 (infos[i].maxMem); Store_field (v1, 1, v2);
261 v2 = caml_copy_int64 (infos[i].memory); Store_field (v1, 2, v2);
262 Store_field (v1, 3, Val_int (infos[i].nrVirtCpu));
263 v2 = caml_copy_int64 (infos[i].cpuTime); Store_field (v1, 4, v2);
265 Store_field (rv2, i, v1);
271 rv2 = caml_alloc (0, 0); /* zero-length array */
273 rv = caml_alloc_tuple (2);
274 Store_field (rv, 0, rv1);
275 Store_field (rv, 1, rv2);
278 not_supported ("virConnectListAllDomains");
283 ocaml_libvirt_domain_get_id (value domv)
286 virDomainPtr dom = Domain_val (domv);
287 virConnectPtr conn = Connect_domv (domv);
290 NONBLOCKING (r = virDomainGetID (dom));
291 /* There's a bug in libvirt which means that if you try to get
292 * the ID of a defined-but-not-running domain, it returns -1,
293 * and there's no way to distinguish that from an error.
295 CHECK_ERROR (r == (unsigned int) -1, conn, "virDomainGetID");
297 CAMLreturn (Val_int ((int) r));
301 ocaml_libvirt_domain_get_max_memory (value domv)
305 virDomainPtr dom = Domain_val (domv);
306 virConnectPtr conn = Connect_domv (domv);
309 NONBLOCKING (r = virDomainGetMaxMemory (dom));
310 CHECK_ERROR (r == 0 /* [sic] */, conn, "virDomainGetMaxMemory");
312 rv = caml_copy_int64 (r);
317 ocaml_libvirt_domain_set_max_memory (value domv, value memv)
319 CAMLparam2 (domv, memv);
320 virDomainPtr dom = Domain_val (domv);
321 virConnectPtr conn = Connect_domv (domv);
322 unsigned long mem = Int64_val (memv);
325 NONBLOCKING (r = virDomainSetMaxMemory (dom, mem));
326 CHECK_ERROR (r == -1, conn, "virDomainSetMaxMemory");
328 CAMLreturn (Val_unit);
332 ocaml_libvirt_domain_set_memory (value domv, value memv)
334 CAMLparam2 (domv, memv);
335 virDomainPtr dom = Domain_val (domv);
336 virConnectPtr conn = Connect_domv (domv);
337 unsigned long mem = Int64_val (memv);
340 NONBLOCKING (r = virDomainSetMemory (dom, mem));
341 CHECK_ERROR (r == -1, conn, "virDomainSetMemory");
343 CAMLreturn (Val_unit);
347 ocaml_libvirt_domain_get_info (value domv)
351 virDomainPtr dom = Domain_val (domv);
352 virConnectPtr conn = Connect_domv (domv);
356 NONBLOCKING (r = virDomainGetInfo (dom, &info));
357 CHECK_ERROR (r == -1, conn, "virDomainGetInfo");
359 rv = caml_alloc (5, 0);
360 Store_field (rv, 0, Val_int (info.state)); // These flags are compatible.
361 v = caml_copy_int64 (info.maxMem); Store_field (rv, 1, v);
362 v = caml_copy_int64 (info.memory); Store_field (rv, 2, v);
363 Store_field (rv, 3, Val_int (info.nrVirtCpu));
364 v = caml_copy_int64 (info.cpuTime); Store_field (rv, 4, v);
369 #ifdef HAVE_WEAK_SYMBOLS
370 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
371 extern char *virDomainGetSchedulerType(virDomainPtr domain,
373 __attribute__((weak));
378 ocaml_libvirt_domain_get_scheduler_type (value domv)
380 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
382 CAMLlocal2 (rv, strv);
383 virDomainPtr dom = Domain_val (domv);
384 virConnectPtr conn = Connect_domv (domv);
388 WEAK_SYMBOL_CHECK (virDomainGetSchedulerType);
389 NONBLOCKING (r = virDomainGetSchedulerType (dom, &nparams));
390 CHECK_ERROR (!r, conn, "virDomainGetSchedulerType");
392 rv = caml_alloc_tuple (2);
393 strv = caml_copy_string (r); Store_field (rv, 0, strv);
395 Store_field (rv, 1, nparams);
398 not_supported ("virDomainGetSchedulerType");
402 #ifdef HAVE_WEAK_SYMBOLS
403 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
404 extern int virDomainGetSchedulerParameters (virDomainPtr domain,
405 virSchedParameterPtr params,
407 __attribute__((weak));
412 ocaml_libvirt_domain_get_scheduler_parameters (value domv, value nparamsv)
414 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
415 CAMLparam2 (domv, nparamsv);
416 CAMLlocal4 (rv, v, v2, v3);
417 virDomainPtr dom = Domain_val (domv);
418 virConnectPtr conn = Connect_domv (domv);
419 int nparams = Int_val (nparamsv);
420 virSchedParameter params[nparams];
423 WEAK_SYMBOL_CHECK (virDomainGetSchedulerParameters);
424 NONBLOCKING (r = virDomainGetSchedulerParameters (dom, params, &nparams));
425 CHECK_ERROR (r == -1, conn, "virDomainGetSchedulerParameters");
427 rv = caml_alloc (nparams, 0);
428 for (i = 0; i < nparams; ++i) {
429 v = caml_alloc_tuple (2); Store_field (rv, i, v);
430 v2 = caml_copy_string (params[i].field); Store_field (v, 0, v2);
431 switch (params[i].type) {
432 case VIR_DOMAIN_SCHED_FIELD_INT:
433 v2 = caml_alloc (1, 0);
434 v3 = caml_copy_int32 (params[i].value.i); Store_field (v2, 0, v3);
436 case VIR_DOMAIN_SCHED_FIELD_UINT:
437 v2 = caml_alloc (1, 1);
438 v3 = caml_copy_int32 (params[i].value.ui); Store_field (v2, 0, v3);
440 case VIR_DOMAIN_SCHED_FIELD_LLONG:
441 v2 = caml_alloc (1, 2);
442 v3 = caml_copy_int64 (params[i].value.l); Store_field (v2, 0, v3);
444 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
445 v2 = caml_alloc (1, 3);
446 v3 = caml_copy_int64 (params[i].value.ul); Store_field (v2, 0, v3);
448 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
449 v2 = caml_alloc (1, 4);
450 v3 = caml_copy_double (params[i].value.d); Store_field (v2, 0, v3);
452 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
453 v2 = caml_alloc (1, 5);
454 Store_field (v2, 0, Val_int (params[i].value.b));
457 caml_failwith ((char *)__FUNCTION__);
459 Store_field (v, 1, v2);
463 not_supported ("virDomainGetSchedulerParameters");
467 #ifdef HAVE_WEAK_SYMBOLS
468 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
469 extern int virDomainSetSchedulerParameters (virDomainPtr domain,
470 virSchedParameterPtr params,
472 __attribute__((weak));
477 ocaml_libvirt_domain_set_scheduler_parameters (value domv, value paramsv)
479 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
480 CAMLparam2 (domv, paramsv);
482 virDomainPtr dom = Domain_val (domv);
483 virConnectPtr conn = Connect_domv (domv);
484 int nparams = Wosize_val (paramsv);
485 virSchedParameter params[nparams];
489 for (i = 0; i < nparams; ++i) {
490 v = Field (paramsv, i); /* Points to the two-element tuple. */
491 name = String_val (Field (v, 0));
492 strncpy (params[i].field, name, VIR_DOMAIN_SCHED_FIELD_LENGTH);
493 params[i].field[VIR_DOMAIN_SCHED_FIELD_LENGTH-1] = '\0';
494 v = Field (v, 1); /* Points to the sched_param_value block. */
495 switch (Tag_val (v)) {
497 params[i].type = VIR_DOMAIN_SCHED_FIELD_INT;
498 params[i].value.i = Int32_val (Field (v, 0));
501 params[i].type = VIR_DOMAIN_SCHED_FIELD_UINT;
502 params[i].value.ui = Int32_val (Field (v, 0));
505 params[i].type = VIR_DOMAIN_SCHED_FIELD_LLONG;
506 params[i].value.l = Int64_val (Field (v, 0));
509 params[i].type = VIR_DOMAIN_SCHED_FIELD_ULLONG;
510 params[i].value.ul = Int64_val (Field (v, 0));
513 params[i].type = VIR_DOMAIN_SCHED_FIELD_DOUBLE;
514 params[i].value.d = Double_val (Field (v, 0));
517 params[i].type = VIR_DOMAIN_SCHED_FIELD_BOOLEAN;
518 params[i].value.b = Int_val (Field (v, 0));
521 caml_failwith ((char *)__FUNCTION__);
525 WEAK_SYMBOL_CHECK (virDomainSetSchedulerParameters);
526 NONBLOCKING (r = virDomainSetSchedulerParameters (dom, params, nparams));
527 CHECK_ERROR (r == -1, conn, "virDomainSetSchedulerParameters");
529 CAMLreturn (Val_unit);
531 not_supported ("virDomainSetSchedulerParameters");
536 ocaml_libvirt_domain_set_vcpus (value domv, value nvcpusv)
538 CAMLparam2 (domv, nvcpusv);
539 virDomainPtr dom = Domain_val (domv);
540 virConnectPtr conn = Connect_domv (domv);
541 int r, nvcpus = Int_val (nvcpusv);
543 NONBLOCKING (r = virDomainSetVcpus (dom, nvcpus));
544 CHECK_ERROR (r == -1, conn, "virDomainSetVcpus");
546 CAMLreturn (Val_unit);
550 ocaml_libvirt_domain_pin_vcpu (value domv, value vcpuv, value cpumapv)
552 CAMLparam3 (domv, vcpuv, cpumapv);
553 virDomainPtr dom = Domain_val (domv);
554 virConnectPtr conn = Connect_domv (domv);
555 int maplen = caml_string_length (cpumapv);
556 unsigned char *cpumap = (unsigned char *) String_val (cpumapv);
557 int vcpu = Int_val (vcpuv);
560 NONBLOCKING (r = virDomainPinVcpu (dom, vcpu, cpumap, maplen));
561 CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
563 CAMLreturn (Val_unit);
567 ocaml_libvirt_domain_get_vcpus (value domv, value maxinfov, value maplenv)
569 CAMLparam3 (domv, maxinfov, maplenv);
570 CAMLlocal5 (rv, infov, strv, v, v2);
571 virDomainPtr dom = Domain_val (domv);
572 virConnectPtr conn = Connect_domv (domv);
573 int maxinfo = Int_val (maxinfov);
574 int maplen = Int_val (maplenv);
575 virVcpuInfo info[maxinfo];
576 unsigned char cpumaps[maxinfo * maplen];
579 memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
580 memset (cpumaps, 0, maxinfo * maplen);
582 NONBLOCKING (r = virDomainGetVcpus (dom, info, maxinfo, cpumaps, maplen));
583 CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
585 /* Copy the virVcpuInfo structures. */
586 infov = caml_alloc (maxinfo, 0);
587 for (i = 0; i < maxinfo; ++i) {
588 v2 = caml_alloc (4, 0); Store_field (infov, i, v2);
589 Store_field (v2, 0, Val_int (info[i].number));
590 Store_field (v2, 1, Val_int (info[i].state));
591 v = caml_copy_int64 (info[i].cpuTime); Store_field (v2, 2, v);
592 Store_field (v2, 3, Val_int (info[i].cpu));
595 /* Copy the bitmap. */
596 strv = caml_alloc_string (maxinfo * maplen);
597 memcpy (String_val (strv), cpumaps, maxinfo * maplen);
599 /* Allocate the tuple and return it. */
600 rv = caml_alloc_tuple (3);
601 Store_field (rv, 0, Val_int (r)); /* number of CPUs. */
602 Store_field (rv, 1, infov);
603 Store_field (rv, 2, strv);
608 #ifdef HAVE_WEAK_SYMBOLS
609 #ifdef HAVE_VIRDOMAINMIGRATE
610 extern virDomainPtr virDomainMigrate (virDomainPtr domain, virConnectPtr dconn,
611 unsigned long flags, const char *dname,
612 const char *uri, unsigned long bandwidth)
613 __attribute__((weak));
618 ocaml_libvirt_domain_migrate_native (value domv, value dconnv, value flagsv, value optdnamev, value opturiv, value optbandwidthv, value unitv)
620 #ifdef HAVE_VIRDOMAINMIGRATE
621 CAMLparam5 (domv, dconnv, flagsv, optdnamev, opturiv);
622 CAMLxparam2 (optbandwidthv, unitv);
623 CAMLlocal2 (flagv, rv);
624 virDomainPtr dom = Domain_val (domv);
625 virConnectPtr conn = Connect_domv (domv);
626 virConnectPtr dconn = Connect_val (dconnv);
628 const char *dname = Optstring_val (optdnamev);
629 const char *uri = Optstring_val (opturiv);
630 unsigned long bandwidth;
633 /* Iterate over the list of flags. */
634 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1))
636 flagv = Field (flagsv, 0);
637 if (flagv == Val_int (0))
638 flags |= VIR_MIGRATE_LIVE;
641 if (optbandwidthv == Val_int (0)) /* None */
643 else /* Some bandwidth */
644 bandwidth = Int_val (Field (optbandwidthv, 0));
646 WEAK_SYMBOL_CHECK (virDomainMigrate);
647 NONBLOCKING (r = virDomainMigrate (dom, dconn, flags, dname, uri, bandwidth));
648 CHECK_ERROR (!r, conn, "virDomainMigrate");
650 rv = Val_domain (r, dconnv);
654 #else /* virDomainMigrate not supported */
655 not_supported ("virDomainMigrate");
660 ocaml_libvirt_domain_migrate_bytecode (value *argv, int argn)
662 return ocaml_libvirt_domain_migrate_native (argv[0], argv[1], argv[2],
663 argv[3], argv[4], argv[5],
667 #ifdef HAVE_WEAK_SYMBOLS
668 #ifdef HAVE_VIRDOMAINBLOCKSTATS
669 extern int virDomainBlockStats (virDomainPtr dom,
671 virDomainBlockStatsPtr stats,
673 __attribute__((weak));
678 ocaml_libvirt_domain_block_stats (value domv, value pathv)
680 #if HAVE_VIRDOMAINBLOCKSTATS
681 CAMLparam2 (domv, pathv);
683 virDomainPtr dom = Domain_val (domv);
684 virConnectPtr conn = Connect_domv (domv);
685 char *path = String_val (pathv);
686 struct _virDomainBlockStats stats;
689 WEAK_SYMBOL_CHECK (virDomainBlockStats);
690 NONBLOCKING (r = virDomainBlockStats (dom, path, &stats, sizeof stats));
691 CHECK_ERROR (r == -1, conn, "virDomainBlockStats");
693 rv = caml_alloc (5, 0);
694 v = caml_copy_int64 (stats.rd_req); Store_field (rv, 0, v);
695 v = caml_copy_int64 (stats.rd_bytes); Store_field (rv, 1, v);
696 v = caml_copy_int64 (stats.wr_req); Store_field (rv, 2, v);
697 v = caml_copy_int64 (stats.wr_bytes); Store_field (rv, 3, v);
698 v = caml_copy_int64 (stats.errs); Store_field (rv, 4, v);
702 not_supported ("virDomainBlockStats");
706 #ifdef HAVE_WEAK_SYMBOLS
707 #ifdef HAVE_VIRDOMAININTERFACESTATS
708 extern int virDomainInterfaceStats (virDomainPtr dom,
710 virDomainInterfaceStatsPtr stats,
712 __attribute__((weak));
717 ocaml_libvirt_domain_interface_stats (value domv, value pathv)
719 #if HAVE_VIRDOMAININTERFACESTATS
720 CAMLparam2 (domv, pathv);
722 virDomainPtr dom = Domain_val (domv);
723 virConnectPtr conn = Connect_domv (domv);
724 char *path = String_val (pathv);
725 struct _virDomainInterfaceStats stats;
728 WEAK_SYMBOL_CHECK (virDomainInterfaceStats);
729 NONBLOCKING (r = virDomainInterfaceStats (dom, path, &stats, sizeof stats));
730 CHECK_ERROR (r == -1, conn, "virDomainInterfaceStats");
732 rv = caml_alloc (8, 0);
733 v = caml_copy_int64 (stats.rx_bytes); Store_field (rv, 0, v);
734 v = caml_copy_int64 (stats.rx_packets); Store_field (rv, 1, v);
735 v = caml_copy_int64 (stats.rx_errs); Store_field (rv, 2, v);
736 v = caml_copy_int64 (stats.rx_drop); Store_field (rv, 3, v);
737 v = caml_copy_int64 (stats.tx_bytes); Store_field (rv, 4, v);
738 v = caml_copy_int64 (stats.tx_packets); Store_field (rv, 5, v);
739 v = caml_copy_int64 (stats.tx_errs); Store_field (rv, 6, v);
740 v = caml_copy_int64 (stats.tx_drop); Store_field (rv, 7, v);
744 not_supported ("virDomainInterfaceStats");
748 #ifdef HAVE_WEAK_SYMBOLS
749 #ifdef HAVE_VIRDOMAINBLOCKPEEK
750 extern int virDomainBlockPeek (virDomainPtr domain,
752 unsigned long long offset,
756 __attribute__((weak));
761 ocaml_libvirt_domain_block_peek_native (value domv, value pathv, value offsetv, value sizev, value bufferv, value boffv)
763 #ifdef HAVE_VIRDOMAINBLOCKPEEK
764 CAMLparam5 (domv, pathv, offsetv, sizev, bufferv);
766 virDomainPtr dom = Domain_val (domv);
767 virConnectPtr conn = Connect_domv (domv);
768 const char *path = String_val (pathv);
769 unsigned long long offset = Int64_val (offsetv);
770 size_t size = Int_val (sizev);
771 char *buffer = String_val (bufferv);
772 int boff = Int_val (boffv);
775 /* Check that the return buffer is big enough. */
776 if (caml_string_length (bufferv) < boff + size)
777 caml_failwith ("virDomainBlockPeek: return buffer too short");
779 WEAK_SYMBOL_CHECK (virDomainBlockPeek);
780 /* NB. not NONBLOCKING because buffer might move (XXX) */
781 r = virDomainBlockPeek (dom, path, offset, size, buffer+boff, 0);
782 CHECK_ERROR (r == -1, conn, "virDomainBlockPeek");
784 CAMLreturn (Val_unit);
786 #else /* virDomainBlockPeek not supported */
787 not_supported ("virDomainBlockPeek");
792 ocaml_libvirt_domain_block_peek_bytecode (value *argv, int argn)
794 return ocaml_libvirt_domain_block_peek_native (argv[0], argv[1], argv[2],
795 argv[3], argv[4], argv[5]);
798 #ifdef HAVE_WEAK_SYMBOLS
799 #ifdef HAVE_VIRDOMAINMEMORYPEEK
800 extern int virDomainMemoryPeek (virDomainPtr domain,
801 unsigned long long start,
805 __attribute__((weak));
810 ocaml_libvirt_domain_memory_peek_native (value domv, value flagsv, value offsetv, value sizev, value bufferv, value boffv)
812 #ifdef HAVE_VIRDOMAINMEMORYPEEK
813 CAMLparam5 (domv, flagsv, offsetv, sizev, bufferv);
816 virDomainPtr dom = Domain_val (domv);
817 virConnectPtr conn = Connect_domv (domv);
819 unsigned long long offset = Int64_val (offsetv);
820 size_t size = Int_val (sizev);
821 char *buffer = String_val (bufferv);
822 int boff = Int_val (boffv);
825 /* Check that the return buffer is big enough. */
826 if (caml_string_length (bufferv) < boff + size)
827 caml_failwith ("virDomainMemoryPeek: return buffer too short");
830 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1))
832 flagv = Field (flagsv, 0);
833 if (flagv == Val_int (0))
834 flags |= VIR_MEMORY_VIRTUAL;
837 WEAK_SYMBOL_CHECK (virDomainMemoryPeek);
838 /* NB. not NONBLOCKING because buffer might move (XXX) */
839 r = virDomainMemoryPeek (dom, offset, size, buffer+boff, flags);
840 CHECK_ERROR (r == -1, conn, "virDomainMemoryPeek");
842 CAMLreturn (Val_unit);
844 #else /* virDomainMemoryPeek not supported */
845 not_supported ("virDomainMemoryPeek");
850 ocaml_libvirt_domain_memory_peek_bytecode (value *argv, int argn)
852 return ocaml_libvirt_domain_memory_peek_native (argv[0], argv[1], argv[2],
853 argv[3], argv[4], argv[5]);
856 #ifdef HAVE_WEAK_SYMBOLS
857 #ifdef HAVE_VIRSTORAGEPOOLGETINFO
858 extern int virStoragePoolGetInfo(virStoragePoolPtr pool, virStoragePoolInfoPtr info)
859 __attribute__((weak));
864 ocaml_libvirt_storage_pool_get_info (value poolv)
866 #if HAVE_VIRSTORAGEPOOLGETINFO
869 virStoragePoolPtr pool = Pool_val (poolv);
870 virConnectPtr conn = Connect_polv (poolv);
871 virStoragePoolInfo info;
874 WEAK_SYMBOL_CHECK (virStoragePoolGetInfo);
875 NONBLOCKING (r = virStoragePoolGetInfo (pool, &info));
876 CHECK_ERROR (r == -1, conn, "virStoragePoolGetInfo");
878 rv = caml_alloc (4, 0);
879 Store_field (rv, 0, Val_int (info.state));
880 v = caml_copy_int64 (info.capacity); Store_field (rv, 1, v);
881 v = caml_copy_int64 (info.allocation); Store_field (rv, 2, v);
882 v = caml_copy_int64 (info.available); Store_field (rv, 3, v);
886 not_supported ("virStoragePoolGetInfo");
890 #ifdef HAVE_WEAK_SYMBOLS
891 #ifdef HAVE_VIRSTORAGEVOLGETINFO
892 extern int virStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info)
893 __attribute__((weak));
898 ocaml_libvirt_storage_vol_get_info (value volv)
900 #if HAVE_VIRSTORAGEVOLGETINFO
903 virStorageVolPtr vol = Volume_val (volv);
904 virConnectPtr conn = Connect_volv (volv);
905 virStorageVolInfo info;
908 WEAK_SYMBOL_CHECK (virStorageVolGetInfo);
909 NONBLOCKING (r = virStorageVolGetInfo (vol, &info));
910 CHECK_ERROR (r == -1, conn, "virStorageVolGetInfo");
912 rv = caml_alloc (3, 0);
913 Store_field (rv, 0, Val_int (info.type));
914 v = caml_copy_int64 (info.capacity); Store_field (rv, 1, v);
915 v = caml_copy_int64 (info.allocation); Store_field (rv, 1, v);
919 not_supported ("virStorageVolGetInfo");
923 #ifdef HAVE_WEAK_SYMBOLS
924 #ifdef HAVE_VIRJOBGETINFO
925 extern int virJobGetInfo(virJobPtr job, virJobInfoPtr info)
926 __attribute__((weak));
931 ocaml_libvirt_job_get_info (value jobv)
933 #if HAVE_VIRJOBGETINFO
936 virJobPtr job = Job_val (jobv);
937 virConnectPtr conn = Connect_jobv (jobv);
941 WEAK_SYMBOL_CHECK (virJobGetInfo);
942 NONBLOCKING (r = virJobGetInfo (job, &info));
943 CHECK_ERROR (r == -1, conn, "virJobGetInfo");
945 rv = caml_alloc (5, 0);
946 Store_field (rv, 0, Val_int (info.type));
947 Store_field (rv, 1, Val_int (info.state));
948 Store_field (rv, 2, Val_int (info.runningTime));
949 Store_field (rv, 3, Val_int (info.remainingTime));
950 Store_field (rv, 4, Val_int (info.percentComplete));
954 not_supported ("virJobGetInfo");
958 /*----------------------------------------------------------------------*/
961 ocaml_libvirt_virterror_get_last_error (value unitv)
965 virErrorPtr err = virGetLastError ();
967 rv = Val_opt (err, (Val_ptr_t) Val_virterror);
973 ocaml_libvirt_virterror_get_last_conn_error (value connv)
977 virConnectPtr conn = Connect_val (connv);
979 rv = Val_opt (conn, (Val_ptr_t) Val_connect);
985 ocaml_libvirt_virterror_reset_last_error (value unitv)
988 virResetLastError ();
989 CAMLreturn (Val_unit);
993 ocaml_libvirt_virterror_reset_last_conn_error (value connv)
996 virConnectPtr conn = Connect_val (connv);
997 virConnResetLastError (conn);
998 CAMLreturn (Val_unit);
1001 /*----------------------------------------------------------------------*/
1003 /* Initialise the library. */
1005 ocaml_libvirt_init (value unit)
1011 r = virInitialize ();
1012 CHECK_ERROR (r == -1, NULL, "virInitialize");
1014 CAMLreturn (Val_unit);