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
26 #include <libvirt/libvirt.h>
27 #include <libvirt/virterror.h>
29 #include <caml/config.h>
30 #include <caml/alloc.h>
31 #include <caml/callback.h>
32 #include <caml/custom.h>
33 #include <caml/fail.h>
34 #include <caml/memory.h>
35 #include <caml/misc.h>
36 #include <caml/mlvalues.h>
37 #include <caml/signals.h>
39 static char *Optstring_val (value strv);
40 typedef value (*Val_ptr_t) (void *);
41 static value Val_opt (void *ptr, Val_ptr_t Val_ptr);
42 /*static value option_default (value option, value deflt);*/
43 static value _raise_virterror (virConnectPtr conn, const char *fn);
44 static value Val_virterror (virErrorPtr err);
46 /* Use this around synchronous libvirt API calls to release the OCaml
47 * lock, allowing other threads to run simultaneously. 'code' must not
48 * perform any caml_* calls, run any OCaml code, or raise any exception.
49 * http://web.archive.org/web/20030521020915/http://caml.inria.fr/archives/200106/msg00199.html
51 #define NONBLOCKING(code) \
53 caml_enter_blocking_section (); \
55 caml_leave_blocking_section (); \
58 /* Check error condition from a libvirt function, and automatically raise
59 * an exception if one is found.
61 #define CHECK_ERROR(cond, conn, fn) \
62 do { if (cond) _raise_virterror (conn, fn); } while (0)
64 #define NOT_SUPPORTED(fn) \
65 caml_invalid_argument (fn " not supported")
67 /* For more about weak symbols, see:
68 * http://kolpackov.net/pipermail/notes/2004-March/000006.html
69 * We are using this to do runtime detection of library functions
70 * so that if we dynamically link with an older version of
71 * libvirt than we were compiled against, it won't fail (provided
72 * libvirt >= 0.2.1 - we don't support anything older).
76 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
77 #define HAVE_WEAK_SYMBOLS 1
82 #ifdef HAVE_WEAK_SYMBOLS
83 #define WEAK_SYMBOL_CHECK(sym) \
84 do { if (!sym) NOT_SUPPORTED(#sym); } while (0)
86 #define WEAK_SYMBOL_CHECK(sym)
87 #endif /* HAVE_WEAK_SYMBOLS */
89 #ifdef HAVE_WEAK_SYMBOLS
90 #ifdef HAVE_VIRCONNECTGETHOSTNAME
91 extern char *virConnectGetHostname (virConnectPtr conn)
92 __attribute__((weak));
94 #ifdef HAVE_VIRCONNECTGETURI
95 extern char *virConnectGetURI (virConnectPtr conn)
96 __attribute__((weak));
98 #ifdef HAVE_VIRDOMAINBLOCKSTATS
99 extern int virDomainBlockStats (virDomainPtr dom,
101 virDomainBlockStatsPtr stats,
103 __attribute__((weak));
105 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
106 extern int virDomainGetSchedulerParameters (virDomainPtr domain,
107 virSchedParameterPtr params,
109 __attribute__((weak));
111 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
112 extern char *virDomainGetSchedulerType(virDomainPtr domain,
114 __attribute__((weak));
116 #ifdef HAVE_VIRDOMAININTERFACESTATS
117 extern int virDomainInterfaceStats (virDomainPtr dom,
119 virDomainInterfaceStatsPtr stats,
121 __attribute__((weak));
123 #ifdef HAVE_VIRDOMAINMIGRATE
124 extern virDomainPtr virDomainMigrate (virDomainPtr domain, virConnectPtr dconn,
125 unsigned long flags, const char *dname,
126 const char *uri, unsigned long bandwidth)
127 __attribute__((weak));
129 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
130 extern int virDomainSetSchedulerParameters (virDomainPtr domain,
131 virSchedParameterPtr params,
133 __attribute__((weak));
135 #ifdef HAVE_VIRNODEGETFREEMEMORY
136 extern unsigned long long virNodeGetFreeMemory (virConnectPtr conn)
137 __attribute__((weak));
139 #ifdef HAVE_VIRNODEGETCELLSFREEMEMORY
140 extern int virNodeGetCellsFreeMemory (virConnectPtr conn,
141 unsigned long long *freeMems,
142 int startCell, int maxCells)
143 __attribute__((weak));
145 #endif /* HAVE_WEAK_SYMBOLS */
147 /*----------------------------------------------------------------------*/
150 ocaml_libvirt_get_version (value driverv, value unit)
152 CAMLparam2 (driverv, unit);
154 const char *driver = Optstring_val (driverv);
155 unsigned long libVer, typeVer = 0, *typeVer_ptr;
158 typeVer_ptr = driver ? &typeVer : NULL;
159 NONBLOCKING (r = virGetVersion (&libVer, driver, typeVer_ptr));
160 CHECK_ERROR (r == -1, NULL, "virGetVersion");
162 rv = caml_alloc_tuple (2);
163 Store_field (rv, 0, Val_int (libVer));
164 Store_field (rv, 1, Val_int (typeVer));
168 /*----------------------------------------------------------------------*/
170 /* Some notes about the use of custom blocks to store virConnectPtr,
171 * virDomainPtr and virNetworkPtr.
172 *------------------------------------------------------------------
174 * Libvirt does some tricky reference counting to keep track of
175 * virConnectPtr's, virDomainPtr's and virNetworkPtr's.
177 * There is only one function which can return a virConnectPtr
178 * (virConnectOpen*) and that allocates a new one each time.
180 * virDomainPtr/virNetworkPtr's on the other hand can be returned
181 * repeatedly (for the same underlying domain/network), and we must
182 * keep track of each one and explicitly free it with virDomainFree
183 * or virNetworkFree. If we lose track of one then the reference
184 * counting in libvirt will keep it open. We therefore wrap these
185 * in a custom block with a finalizer function.
187 * We also have to allow the user to explicitly free them, in
188 * which case we set the pointer inside the custom block to NULL.
189 * The finalizer notices this and doesn't free the object.
191 * Domains and networks "belong to" a connection. We have to avoid
192 * the situation like this:
194 * let conn = Connect.open ... in
195 * let dom = Domain.lookup_by_id conn 0 in
196 * (* conn goes out of scope and is garbage collected *)
197 * printf "dom name = %s\n" (Domain.get_name dom)
199 * The reason is that when conn is garbage collected, virConnectClose
200 * is called and any subsequent operations on dom will fail (in fact
201 * will probably segfault). To stop this from happening, the OCaml
202 * wrappers store domains (and networks) as explicit (dom, conn)
205 * Further complication with virterror / exceptions: Virterror gives
206 * us virConnectPtr, virDomainPtr, virNetworkPtr pointers. If we
207 * follow standard practice and wrap these up in blocks with
208 * finalizers then we'll end up double-freeing (in particular, calling
209 * virConnectClose at the wrong time). So for virterror, we have
210 * "special" wrapper functions (Val_connect_no_finalize, etc.).
213 /* Unwrap a custom block. */
214 #define Connect_val(rv) (*((virConnectPtr *)Data_custom_val(rv)))
215 #define Dom_val(rv) (*((virDomainPtr *)Data_custom_val(rv)))
216 #define Net_val(rv) (*((virNetworkPtr *)Data_custom_val(rv)))
218 /* Wrap up a pointer to something in a custom block. */
219 static value Val_connect (virConnectPtr conn);
220 static value Val_dom (virDomainPtr dom);
221 static value Val_net (virNetworkPtr net);
223 /* ONLY for use by virterror wrappers. */
224 static value Val_connect_no_finalize (virConnectPtr conn);
225 static value Val_dom_no_finalize (virDomainPtr dom);
226 static value Val_net_no_finalize (virNetworkPtr net);
228 /* Domains and networks are stored as pairs (dom/net, conn), so have
229 * some convenience functions for unwrapping and wrapping them.
231 #define Domain_val(rv) (Dom_val(Field((rv),0)))
232 #define Network_val(rv) (Net_val(Field((rv),0)))
233 #define Connect_domv(rv) (Connect_val(Field((rv),1)))
234 #define Connect_netv(rv) (Connect_val(Field((rv),1)))
236 static value Val_domain (virDomainPtr dom, value connv);
237 static value Val_network (virNetworkPtr net, value connv);
239 /* ONLY for use by virterror wrappers. */
240 static value Val_domain_no_finalize (virDomainPtr dom, value connv);
241 static value Val_network_no_finalize (virNetworkPtr net, value connv);
243 /*----------------------------------------------------------------------*/
245 /* Connection object. */
248 ocaml_libvirt_connect_open (value namev, value unit)
250 CAMLparam2 (namev, unit);
252 const char *name = Optstring_val (namev);
255 NONBLOCKING (conn = virConnectOpen (name));
256 CHECK_ERROR (!conn, NULL, "virConnectOpen");
258 rv = Val_connect (conn);
264 ocaml_libvirt_connect_open_readonly (value namev, value unit)
266 CAMLparam2 (namev, unit);
268 const char *name = Optstring_val (namev);
271 NONBLOCKING (conn = virConnectOpenReadOnly (name));
272 CHECK_ERROR (!conn, NULL, "virConnectOpen");
274 rv = Val_connect (conn);
280 ocaml_libvirt_connect_close (value connv)
283 virConnectPtr conn = Connect_val (connv);
286 NONBLOCKING (r = virConnectClose (conn));
287 CHECK_ERROR (r == -1, conn, "virConnectClose");
289 /* So that we don't double-free in the finalizer: */
290 Connect_val (connv) = NULL;
292 CAMLreturn (Val_unit);
296 ocaml_libvirt_connect_get_type (value connv)
300 virConnectPtr conn = Connect_val (connv);
303 NONBLOCKING (r = virConnectGetType (conn));
304 CHECK_ERROR (!r, conn, "virConnectGetType");
306 rv = caml_copy_string (r);
311 ocaml_libvirt_connect_get_version (value connv)
314 virConnectPtr conn = Connect_val (connv);
318 NONBLOCKING (r = virConnectGetVersion (conn, &hvVer));
319 CHECK_ERROR (r == -1, conn, "virConnectGetVersion");
321 CAMLreturn (Val_int (hvVer));
325 ocaml_libvirt_connect_get_hostname (value connv)
327 #ifdef HAVE_VIRCONNECTGETHOSTNAME
330 virConnectPtr conn = Connect_val (connv);
333 WEAK_SYMBOL_CHECK (virConnectGetHostname);
334 NONBLOCKING (r = virConnectGetHostname (conn));
335 CHECK_ERROR (!r, conn, "virConnectGetHostname");
337 rv = caml_copy_string (r);
341 NOT_SUPPORTED ("virConnectGetHostname");
346 ocaml_libvirt_connect_get_uri (value connv)
348 #ifdef HAVE_VIRCONNECTGETURI
351 virConnectPtr conn = Connect_val (connv);
354 WEAK_SYMBOL_CHECK (virConnectGetURI);
355 NONBLOCKING (r = virConnectGetURI (conn));
356 CHECK_ERROR (!r, conn, "virConnectGetURI");
358 rv = caml_copy_string (r);
362 NOT_SUPPORTED ("virConnectGetURI");
367 ocaml_libvirt_connect_get_max_vcpus (value connv, value typev)
369 CAMLparam2 (connv, typev);
370 virConnectPtr conn = Connect_val (connv);
371 const char *type = Optstring_val (typev);
374 NONBLOCKING (r = virConnectGetMaxVcpus (conn, type));
375 CHECK_ERROR (r == -1, conn, "virConnectGetMaxVcpus");
377 CAMLreturn (Val_int (r));
381 ocaml_libvirt_connect_list_domains (value connv, value iv)
383 CAMLparam2 (connv, iv);
385 virConnectPtr conn = Connect_val (connv);
386 int i = Int_val (iv);
389 NONBLOCKING (r = virConnectListDomains (conn, ids, i));
390 CHECK_ERROR (r == -1, conn, "virConnectListDomains");
392 rv = caml_alloc (r, 0);
393 for (i = 0; i < r; ++i)
394 Store_field (rv, i, Val_int (ids[i]));
400 ocaml_libvirt_connect_num_of_domains (value connv)
403 virConnectPtr conn = Connect_val (connv);
406 NONBLOCKING (r = virConnectNumOfDomains (conn));
407 CHECK_ERROR (r == -1, conn, "virConnectNumOfDomains");
409 CAMLreturn (Val_int (r));
413 ocaml_libvirt_connect_get_capabilities (value connv)
417 virConnectPtr conn = Connect_val (connv);
420 NONBLOCKING (r = virConnectGetCapabilities (conn));
421 CHECK_ERROR (!r, conn, "virConnectGetCapabilities");
423 rv = caml_copy_string (r);
430 ocaml_libvirt_connect_num_of_defined_domains (value connv)
433 virConnectPtr conn = Connect_val (connv);
436 NONBLOCKING (r = virConnectNumOfDefinedDomains (conn));
437 CHECK_ERROR (r == -1, conn, "virConnectNumOfDefinedDomains");
439 CAMLreturn (Val_int (r));
443 ocaml_libvirt_connect_list_defined_domains (value connv, value iv)
445 CAMLparam2 (connv, iv);
446 CAMLlocal2 (rv, strv);
447 virConnectPtr conn = Connect_val (connv);
448 int i = Int_val (iv);
452 NONBLOCKING (r = virConnectListDefinedDomains (conn, names, i));
453 CHECK_ERROR (r == -1, conn, "virConnectListDefinedDomains");
455 rv = caml_alloc (r, 0);
456 for (i = 0; i < r; ++i) {
457 strv = caml_copy_string (names[i]);
458 Store_field (rv, i, strv);
466 ocaml_libvirt_connect_num_of_networks (value connv)
469 virConnectPtr conn = Connect_val (connv);
472 NONBLOCKING (r = virConnectNumOfNetworks (conn));
473 CHECK_ERROR (r == -1, conn, "virConnectNumOfNetworks");
475 CAMLreturn (Val_int (r));
479 ocaml_libvirt_connect_list_networks (value connv, value iv)
481 CAMLparam2 (connv, iv);
482 CAMLlocal2 (rv, strv);
483 virConnectPtr conn = Connect_val (connv);
484 int i = Int_val (iv);
488 NONBLOCKING (r = virConnectListNetworks (conn, names, i));
489 CHECK_ERROR (r == -1, conn, "virConnectListNetworks");
491 rv = caml_alloc (r, 0);
492 for (i = 0; i < r; ++i) {
493 strv = caml_copy_string (names[i]);
494 Store_field (rv, i, strv);
502 ocaml_libvirt_connect_num_of_defined_networks (value connv)
505 virConnectPtr conn = Connect_val (connv);
508 NONBLOCKING (r = virConnectNumOfDefinedNetworks (conn));
509 CHECK_ERROR (r == -1, conn, "virConnectNumOfDefinedNetworks");
511 CAMLreturn (Val_int (r));
515 ocaml_libvirt_connect_list_defined_networks (value connv, value iv)
517 CAMLparam2 (connv, iv);
518 CAMLlocal2 (rv, strv);
519 virConnectPtr conn = Connect_val (connv);
520 int i = Int_val (iv);
524 NONBLOCKING (r = virConnectListDefinedNetworks (conn, names, i));
525 CHECK_ERROR (r == -1, conn, "virConnectListDefinedNetworks");
527 rv = caml_alloc (r, 0);
528 for (i = 0; i < r; ++i) {
529 strv = caml_copy_string (names[i]);
530 Store_field (rv, i, strv);
538 ocaml_libvirt_connect_get_node_info (value connv)
542 virConnectPtr conn = Connect_val (connv);
546 NONBLOCKING (r = virNodeGetInfo (conn, &info));
547 CHECK_ERROR (r == -1, conn, "virNodeGetInfo");
549 rv = caml_alloc (8, 0);
550 v = caml_copy_string (info.model); Store_field (rv, 0, v);
551 v = caml_copy_int64 (info.memory); Store_field (rv, 1, v);
552 Store_field (rv, 2, Val_int (info.cpus));
553 Store_field (rv, 3, Val_int (info.mhz));
554 Store_field (rv, 4, Val_int (info.nodes));
555 Store_field (rv, 5, Val_int (info.sockets));
556 Store_field (rv, 6, Val_int (info.cores));
557 Store_field (rv, 7, Val_int (info.threads));
563 ocaml_libvirt_connect_node_get_free_memory (value connv)
565 #ifdef HAVE_VIRNODEGETFREEMEMORY
568 virConnectPtr conn = Connect_val (connv);
569 unsigned long long r;
571 WEAK_SYMBOL_CHECK (virNodeGetFreeMemory);
572 NONBLOCKING (r = virNodeGetFreeMemory (conn));
573 CHECK_ERROR (r == 0, conn, "virNodeGetFreeMemory");
575 rv = caml_copy_int64 ((int64) r);
578 NOT_SUPPORTED ("virNodeGetFreeMemory");
583 ocaml_libvirt_connect_node_get_cells_free_memory (value connv,
584 value startv, value maxv)
586 #ifdef HAVE_VIRNODEGETCELLSFREEMEMORY
587 CAMLparam3 (connv, startv, maxv);
589 virConnectPtr conn = Connect_val (connv);
590 int start = Int_val (startv);
591 int max = Int_val (maxv);
593 unsigned long long freemems[max];
595 WEAK_SYMBOL_CHECK (virNodeGetCellsFreeMemory);
596 NONBLOCKING (r = virNodeGetCellsFreeMemory (conn, freemems, start, max));
597 CHECK_ERROR (r == -1, conn, "virNodeGetCellsFreeMemory");
599 rv = caml_alloc (r, 0);
600 for (i = 0; i < r; ++i) {
601 iv = caml_copy_int64 ((int64) freemems[i]);
602 Store_field (rv, i, iv);
607 NOT_SUPPORTED ("virNodeGetCellsFreeMemory");
612 ocaml_libvirt_domain_create_linux (value connv, value xmlv)
614 CAMLparam2 (connv, xmlv);
616 virConnectPtr conn = Connect_val (connv);
617 char *xml = String_val (xmlv);
620 NONBLOCKING (r = virDomainCreateLinux (conn, xml, 0));
621 CHECK_ERROR (!r, conn, "virDomainCreateLinux");
623 rv = Val_domain (r, connv);
628 ocaml_libvirt_domain_lookup_by_id (value connv, value iv)
630 CAMLparam2 (connv, iv);
632 virConnectPtr conn = Connect_val (connv);
633 int i = Int_val (iv);
636 NONBLOCKING (r = virDomainLookupByID (conn, i));
637 CHECK_ERROR (!r, conn, "virDomainLookupByID");
639 rv = Val_domain (r, connv);
644 ocaml_libvirt_domain_lookup_by_uuid (value connv, value uuidv)
646 CAMLparam2 (connv, uuidv);
648 virConnectPtr conn = Connect_val (connv);
649 char *uuid = String_val (uuidv);
652 NONBLOCKING (r = virDomainLookupByUUID (conn, (unsigned char *) uuid));
653 CHECK_ERROR (!r, conn, "virDomainLookupByUUID");
655 rv = Val_domain (r, connv);
660 ocaml_libvirt_domain_lookup_by_uuid_string (value connv, value uuidv)
662 CAMLparam2 (connv, uuidv);
664 virConnectPtr conn = Connect_val (connv);
665 char *uuid = String_val (uuidv);
668 NONBLOCKING (r = virDomainLookupByUUIDString (conn, uuid));
669 CHECK_ERROR (!r, conn, "virDomainLookupByUUIDString");
671 rv = Val_domain (r, connv);
676 ocaml_libvirt_domain_lookup_by_name (value connv, value namev)
678 CAMLparam2 (connv, namev);
680 virConnectPtr conn = Connect_val (connv);
681 char *name = String_val (namev);
684 NONBLOCKING (r = virDomainLookupByName (conn, name));
685 CHECK_ERROR (!r, conn, "virDomainLookupByName");
687 rv = Val_domain (r, connv);
692 ocaml_libvirt_domain_destroy (value domv)
695 virDomainPtr dom = Domain_val (domv);
696 virConnectPtr conn = Connect_domv (domv);
699 NONBLOCKING (r = virDomainDestroy (dom));
700 CHECK_ERROR (r == -1, conn, "virDomainDestroy");
702 /* So that we don't double-free in the finalizer: */
703 Domain_val (domv) = NULL;
705 CAMLreturn (Val_unit);
709 ocaml_libvirt_domain_free (value domv)
712 virDomainPtr dom = Domain_val (domv);
713 virConnectPtr conn = Connect_domv (domv);
716 NONBLOCKING (r = virDomainFree (dom));
717 CHECK_ERROR (r == -1, conn, "virDomainFree");
719 /* So that we don't double-free in the finalizer: */
720 Domain_val (domv) = NULL;
722 CAMLreturn (Val_unit);
726 ocaml_libvirt_domain_suspend (value domv)
729 virDomainPtr dom = Domain_val (domv);
730 virConnectPtr conn = Connect_domv (domv);
733 NONBLOCKING (r = virDomainSuspend (dom));
734 CHECK_ERROR (r == -1, conn, "virDomainSuspend");
736 CAMLreturn (Val_unit);
740 ocaml_libvirt_domain_resume (value domv)
743 virDomainPtr dom = Domain_val (domv);
744 virConnectPtr conn = Connect_domv (domv);
747 NONBLOCKING (r = virDomainResume (dom));
748 CHECK_ERROR (r == -1, conn, "virDomainResume");
750 CAMLreturn (Val_unit);
754 ocaml_libvirt_domain_save (value domv, value pathv)
756 CAMLparam2 (domv, pathv);
757 virDomainPtr dom = Domain_val (domv);
758 virConnectPtr conn = Connect_domv (domv);
759 char *path = String_val (pathv);
762 NONBLOCKING (r = virDomainSave (dom, path));
763 CHECK_ERROR (r == -1, conn, "virDomainSave");
765 CAMLreturn (Val_unit);
769 ocaml_libvirt_domain_restore (value connv, value pathv)
771 CAMLparam2 (connv, pathv);
772 virConnectPtr conn = Connect_val (connv);
773 char *path = String_val (pathv);
776 NONBLOCKING (r = virDomainRestore (conn, path));
777 CHECK_ERROR (r == -1, conn, "virDomainRestore");
779 CAMLreturn (Val_unit);
783 ocaml_libvirt_domain_core_dump (value domv, value pathv)
785 CAMLparam2 (domv, pathv);
786 virDomainPtr dom = Domain_val (domv);
787 virConnectPtr conn = Connect_domv (domv);
788 char *path = String_val (pathv);
791 NONBLOCKING (r = virDomainCoreDump (dom, path, 0));
792 CHECK_ERROR (r == -1, conn, "virDomainCoreDump");
794 CAMLreturn (Val_unit);
798 ocaml_libvirt_domain_shutdown (value domv)
801 virDomainPtr dom = Domain_val (domv);
802 virConnectPtr conn = Connect_domv (domv);
805 NONBLOCKING (r = virDomainShutdown (dom));
806 CHECK_ERROR (r == -1, conn, "virDomainShutdown");
808 CAMLreturn (Val_unit);
812 ocaml_libvirt_domain_reboot (value domv)
815 virDomainPtr dom = Domain_val (domv);
816 virConnectPtr conn = Connect_domv (domv);
819 NONBLOCKING (r = virDomainReboot (dom, 0));
820 CHECK_ERROR (r == -1, conn, "virDomainReboot");
822 CAMLreturn (Val_unit);
826 ocaml_libvirt_domain_get_name (value domv)
830 virDomainPtr dom = Domain_val (domv);
831 virConnectPtr conn = Connect_domv (domv);
834 NONBLOCKING (r = virDomainGetName (dom));
835 CHECK_ERROR (!r, conn, "virDomainGetName");
837 rv = caml_copy_string (r);
842 ocaml_libvirt_domain_get_uuid (value domv)
846 virDomainPtr dom = Domain_val (domv);
847 virConnectPtr conn = Connect_domv (domv);
848 unsigned char uuid[VIR_UUID_BUFLEN];
851 NONBLOCKING (r = virDomainGetUUID (dom, uuid));
852 CHECK_ERROR (r == -1, conn, "virDomainGetUUID");
854 rv = caml_copy_string ((char *) uuid);
859 ocaml_libvirt_domain_get_uuid_string (value domv)
863 virDomainPtr dom = Domain_val (domv);
864 virConnectPtr conn = Connect_domv (domv);
865 char uuid[VIR_UUID_STRING_BUFLEN];
868 NONBLOCKING (r = virDomainGetUUIDString (dom, uuid));
869 CHECK_ERROR (r == -1, conn, "virDomainGetUUIDString");
871 rv = caml_copy_string (uuid);
876 ocaml_libvirt_domain_get_id (value domv)
879 virDomainPtr dom = Domain_val (domv);
880 virConnectPtr conn = Connect_domv (domv);
883 NONBLOCKING (r = virDomainGetID (dom));
884 /* There's a bug in libvirt which means that if you try to get
885 * the ID of a defined-but-not-running domain, it returns -1,
886 * and there's no way to distinguish that from an error.
888 CHECK_ERROR (r == (unsigned int) -1, conn, "virDomainGetID");
890 CAMLreturn (Val_int ((int) r));
894 ocaml_libvirt_domain_get_os_type (value domv)
898 virDomainPtr dom = Domain_val (domv);
899 virConnectPtr conn = Connect_domv (domv);
902 NONBLOCKING (r = virDomainGetOSType (dom));
903 CHECK_ERROR (!r, conn, "virDomainGetOSType");
905 rv = caml_copy_string (r);
911 ocaml_libvirt_domain_get_max_memory (value domv)
915 virDomainPtr dom = Domain_val (domv);
916 virConnectPtr conn = Connect_domv (domv);
919 NONBLOCKING (r = virDomainGetMaxMemory (dom));
920 CHECK_ERROR (r == 0 /* [sic] */, conn, "virDomainGetMaxMemory");
922 rv = caml_copy_int64 (r);
927 ocaml_libvirt_domain_set_max_memory (value domv, value memv)
929 CAMLparam2 (domv, memv);
930 virDomainPtr dom = Domain_val (domv);
931 virConnectPtr conn = Connect_domv (domv);
932 unsigned long mem = Int64_val (memv);
935 NONBLOCKING (r = virDomainSetMaxMemory (dom, mem));
936 CHECK_ERROR (r == -1, conn, "virDomainSetMaxMemory");
938 CAMLreturn (Val_unit);
942 ocaml_libvirt_domain_set_memory (value domv, value memv)
944 CAMLparam2 (domv, memv);
945 virDomainPtr dom = Domain_val (domv);
946 virConnectPtr conn = Connect_domv (domv);
947 unsigned long mem = Int64_val (memv);
950 NONBLOCKING (r = virDomainSetMemory (dom, mem));
951 CHECK_ERROR (r == -1, conn, "virDomainSetMemory");
953 CAMLreturn (Val_unit);
957 ocaml_libvirt_domain_get_info (value domv)
961 virDomainPtr dom = Domain_val (domv);
962 virConnectPtr conn = Connect_domv (domv);
966 NONBLOCKING (r = virDomainGetInfo (dom, &info));
967 CHECK_ERROR (r == -1, conn, "virDomainGetInfo");
969 rv = caml_alloc (5, 0);
970 Store_field (rv, 0, Val_int (info.state)); // These flags are compatible.
971 v = caml_copy_int64 (info.maxMem); Store_field (rv, 1, v);
972 v = caml_copy_int64 (info.memory); Store_field (rv, 2, v);
973 Store_field (rv, 3, Val_int (info.nrVirtCpu));
974 v = caml_copy_int64 (info.cpuTime); Store_field (rv, 4, v);
980 ocaml_libvirt_domain_get_xml_desc (value domv)
984 virDomainPtr dom = Domain_val (domv);
985 virConnectPtr conn = Connect_domv (domv);
988 NONBLOCKING (r = virDomainGetXMLDesc (dom, 0));
989 CHECK_ERROR (!r, conn, "virDomainGetXMLDesc");
991 rv = caml_copy_string (r);
997 ocaml_libvirt_domain_get_scheduler_type (value domv)
999 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
1001 CAMLlocal2 (rv, strv);
1002 virDomainPtr dom = Domain_val (domv);
1003 virConnectPtr conn = Connect_domv (domv);
1007 WEAK_SYMBOL_CHECK (virDomainGetSchedulerType);
1008 NONBLOCKING (r = virDomainGetSchedulerType (dom, &nparams));
1009 CHECK_ERROR (!r, conn, "virDomainGetSchedulerType");
1011 rv = caml_alloc_tuple (2);
1012 strv = caml_copy_string (r); Store_field (rv, 0, strv);
1014 Store_field (rv, 1, nparams);
1017 NOT_SUPPORTED ("virDomainGetSchedulerType");
1022 ocaml_libvirt_domain_get_scheduler_parameters (value domv, value nparamsv)
1024 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
1025 CAMLparam2 (domv, nparamsv);
1026 CAMLlocal4 (rv, v, v2, v3);
1027 virDomainPtr dom = Domain_val (domv);
1028 virConnectPtr conn = Connect_domv (domv);
1029 int nparams = Int_val (nparamsv);
1030 virSchedParameter params[nparams];
1033 WEAK_SYMBOL_CHECK (virDomainGetSchedulerParameters);
1034 NONBLOCKING (r = virDomainGetSchedulerParameters (dom, params, &nparams));
1035 CHECK_ERROR (r == -1, conn, "virDomainGetSchedulerParameters");
1037 rv = caml_alloc (nparams, 0);
1038 for (i = 0; i < nparams; ++i) {
1039 v = caml_alloc_tuple (2); Store_field (rv, i, v);
1040 v2 = caml_copy_string (params[i].field); Store_field (v, 0, v2);
1041 switch (params[i].type) {
1042 case VIR_DOMAIN_SCHED_FIELD_INT:
1043 v2 = caml_alloc (1, 0);
1044 v3 = caml_copy_int32 (params[i].value.i); Store_field (v2, 0, v3);
1046 case VIR_DOMAIN_SCHED_FIELD_UINT:
1047 v2 = caml_alloc (1, 1);
1048 v3 = caml_copy_int32 (params[i].value.ui); Store_field (v2, 0, v3);
1050 case VIR_DOMAIN_SCHED_FIELD_LLONG:
1051 v2 = caml_alloc (1, 2);
1052 v3 = caml_copy_int64 (params[i].value.l); Store_field (v2, 0, v3);
1054 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
1055 v2 = caml_alloc (1, 3);
1056 v3 = caml_copy_int64 (params[i].value.ul); Store_field (v2, 0, v3);
1058 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
1059 v2 = caml_alloc (1, 4);
1060 v3 = caml_copy_double (params[i].value.d); Store_field (v2, 0, v3);
1062 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
1063 v2 = caml_alloc (1, 5);
1064 Store_field (v2, 0, Val_int (params[i].value.b));
1067 caml_failwith ((char *)__FUNCTION__);
1069 Store_field (v, 1, v2);
1073 NOT_SUPPORTED ("virDomainGetSchedulerParameters");
1078 ocaml_libvirt_domain_set_scheduler_parameters (value domv, value paramsv)
1080 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
1081 CAMLparam2 (domv, paramsv);
1083 virDomainPtr dom = Domain_val (domv);
1084 virConnectPtr conn = Connect_domv (domv);
1085 int nparams = Wosize_val (paramsv);
1086 virSchedParameter params[nparams];
1090 for (i = 0; i < nparams; ++i) {
1091 v = Field (paramsv, i); /* Points to the two-element tuple. */
1092 name = String_val (Field (v, 0));
1093 strncpy (params[i].field, name, VIR_DOMAIN_SCHED_FIELD_LENGTH);
1094 params[i].field[VIR_DOMAIN_SCHED_FIELD_LENGTH-1] = '\0';
1095 v = Field (v, 1); /* Points to the sched_param_value block. */
1096 switch (Tag_val (v)) {
1098 params[i].type = VIR_DOMAIN_SCHED_FIELD_INT;
1099 params[i].value.i = Int32_val (Field (v, 0));
1102 params[i].type = VIR_DOMAIN_SCHED_FIELD_UINT;
1103 params[i].value.ui = Int32_val (Field (v, 0));
1106 params[i].type = VIR_DOMAIN_SCHED_FIELD_LLONG;
1107 params[i].value.l = Int64_val (Field (v, 0));
1110 params[i].type = VIR_DOMAIN_SCHED_FIELD_ULLONG;
1111 params[i].value.ul = Int64_val (Field (v, 0));
1114 params[i].type = VIR_DOMAIN_SCHED_FIELD_DOUBLE;
1115 params[i].value.d = Double_val (Field (v, 0));
1118 params[i].type = VIR_DOMAIN_SCHED_FIELD_BOOLEAN;
1119 params[i].value.b = Int_val (Field (v, 0));
1122 caml_failwith ((char *)__FUNCTION__);
1126 WEAK_SYMBOL_CHECK (virDomainSetSchedulerParameters);
1127 NONBLOCKING (r = virDomainSetSchedulerParameters (dom, params, nparams));
1128 CHECK_ERROR (r == -1, conn, "virDomainSetSchedulerParameters");
1130 CAMLreturn (Val_unit);
1132 NOT_SUPPORTED ("virDomainSetSchedulerParameters");
1137 ocaml_libvirt_domain_define_xml (value connv, value xmlv)
1139 CAMLparam2 (connv, xmlv);
1141 virConnectPtr conn = Connect_val (connv);
1142 char *xml = String_val (xmlv);
1145 NONBLOCKING (r = virDomainDefineXML (conn, xml));
1146 CHECK_ERROR (!r, conn, "virDomainDefineXML");
1148 rv = Val_domain (r, connv);
1153 ocaml_libvirt_domain_undefine (value domv)
1156 virDomainPtr dom = Domain_val (domv);
1157 virConnectPtr conn = Connect_domv (domv);
1160 NONBLOCKING (r = virDomainUndefine (dom));
1161 CHECK_ERROR (r == -1, conn, "virDomainUndefine");
1163 CAMLreturn (Val_unit);
1167 ocaml_libvirt_domain_create (value domv)
1170 virDomainPtr dom = Domain_val (domv);
1171 virConnectPtr conn = Connect_domv (domv);
1174 NONBLOCKING (r = virDomainCreate (dom));
1175 CHECK_ERROR (r == -1, conn, "virDomainCreate");
1177 CAMLreturn (Val_unit);
1181 ocaml_libvirt_domain_get_autostart (value domv)
1184 virDomainPtr dom = Domain_val (domv);
1185 virConnectPtr conn = Connect_domv (domv);
1188 NONBLOCKING (r = virDomainGetAutostart (dom, &autostart));
1189 CHECK_ERROR (r == -1, conn, "virDomainGetAutostart");
1191 CAMLreturn (autostart ? Val_true : Val_false);
1195 ocaml_libvirt_domain_set_autostart (value domv, value autostartv)
1197 CAMLparam2 (domv, autostartv);
1198 virDomainPtr dom = Domain_val (domv);
1199 virConnectPtr conn = Connect_domv (domv);
1200 int r, autostart = autostartv == Val_true ? 1 : 0;
1202 NONBLOCKING (r = virDomainSetAutostart (dom, autostart));
1203 CHECK_ERROR (r == -1, conn, "virDomainSetAutostart");
1205 CAMLreturn (Val_unit);
1209 ocaml_libvirt_domain_set_vcpus (value domv, value nvcpusv)
1211 CAMLparam2 (domv, nvcpusv);
1212 virDomainPtr dom = Domain_val (domv);
1213 virConnectPtr conn = Connect_domv (domv);
1214 int r, nvcpus = Int_val (nvcpusv);
1216 NONBLOCKING (r = virDomainSetVcpus (dom, nvcpus));
1217 CHECK_ERROR (r == -1, conn, "virDomainSetVcpus");
1219 CAMLreturn (Val_unit);
1223 ocaml_libvirt_domain_pin_vcpu (value domv, value vcpuv, value cpumapv)
1225 CAMLparam3 (domv, vcpuv, cpumapv);
1226 virDomainPtr dom = Domain_val (domv);
1227 virConnectPtr conn = Connect_domv (domv);
1228 int maplen = caml_string_length (cpumapv);
1229 unsigned char *cpumap = (unsigned char *) String_val (cpumapv);
1230 int vcpu = Int_val (vcpuv);
1233 NONBLOCKING (r = virDomainPinVcpu (dom, vcpu, cpumap, maplen));
1234 CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
1236 CAMLreturn (Val_unit);
1240 ocaml_libvirt_domain_get_vcpus (value domv, value maxinfov, value maplenv)
1242 CAMLparam3 (domv, maxinfov, maplenv);
1243 CAMLlocal5 (rv, infov, strv, v, v2);
1244 virDomainPtr dom = Domain_val (domv);
1245 virConnectPtr conn = Connect_domv (domv);
1246 int maxinfo = Int_val (maxinfov);
1247 int maplen = Int_val (maplenv);
1248 virVcpuInfo info[maxinfo];
1249 unsigned char cpumaps[maxinfo * maplen];
1252 memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
1253 memset (cpumaps, 0, maxinfo * maplen);
1255 NONBLOCKING (r = virDomainGetVcpus (dom, info, maxinfo, cpumaps, maplen));
1256 CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
1258 /* Copy the virVcpuInfo structures. */
1259 infov = caml_alloc (maxinfo, 0);
1260 for (i = 0; i < maxinfo; ++i) {
1261 v2 = caml_alloc (4, 0); Store_field (infov, i, v2);
1262 Store_field (v2, 0, Val_int (info[i].number));
1263 Store_field (v2, 1, Val_int (info[i].state));
1264 v = caml_copy_int64 (info[i].cpuTime); Store_field (v2, 2, v);
1265 Store_field (v2, 3, Val_int (info[i].cpu));
1268 /* Copy the bitmap. */
1269 strv = caml_alloc_string (maxinfo * maplen);
1270 memcpy (String_val (strv), cpumaps, maxinfo * maplen);
1272 /* Allocate the tuple and return it. */
1273 rv = caml_alloc_tuple (3);
1274 Store_field (rv, 0, Val_int (r)); /* number of CPUs. */
1275 Store_field (rv, 1, infov);
1276 Store_field (rv, 2, strv);
1282 ocaml_libvirt_domain_get_max_vcpus (value domv)
1285 virDomainPtr dom = Domain_val (domv);
1286 virConnectPtr conn = Connect_domv (domv);
1289 NONBLOCKING (r = virDomainGetMaxVcpus (dom));
1290 CHECK_ERROR (r == -1, conn, "virDomainGetMaxVcpus");
1292 CAMLreturn (Val_int (r));
1296 ocaml_libvirt_domain_attach_device (value domv, value xmlv)
1298 CAMLparam2 (domv, xmlv);
1299 virDomainPtr dom = Domain_val (domv);
1300 virConnectPtr conn = Connect_domv (domv);
1301 char *xml = String_val (xmlv);
1304 NONBLOCKING (r = virDomainAttachDevice (dom, xml));
1305 CHECK_ERROR (r == -1, conn, "virDomainAttachDevice");
1307 CAMLreturn (Val_unit);
1311 ocaml_libvirt_domain_detach_device (value domv, value xmlv)
1313 CAMLparam2 (domv, xmlv);
1314 virDomainPtr dom = Domain_val (domv);
1315 virConnectPtr conn = Connect_domv (domv);
1316 char *xml = String_val (xmlv);
1319 NONBLOCKING (r = virDomainDetachDevice (dom, xml));
1320 CHECK_ERROR (r == -1, conn, "virDomainDetachDevice");
1322 CAMLreturn (Val_unit);
1326 ocaml_libvirt_domain_migrate_native (value domv, value dconnv, value flagsv, value optdnamev, value opturiv, value optbandwidthv, value unitv)
1328 #ifdef HAVE_VIRDOMAINMIGRATE
1329 CAMLparam5 (domv, dconnv, flagsv, optdnamev, opturiv);
1330 CAMLxparam2 (optbandwidthv, unitv);
1331 CAMLlocal2 (flagv, rv);
1332 virDomainPtr dom = Domain_val (domv);
1333 virConnectPtr conn = Connect_domv (domv);
1334 virConnectPtr dconn = Connect_val (dconnv);
1336 const char *dname = Optstring_val (optdnamev);
1337 const char *uri = Optstring_val (opturiv);
1338 unsigned long bandwidth;
1341 /* Iterate over the list of flags. */
1342 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1))
1344 flagv = Field (flagsv, 0);
1345 if (flagv == Int_val(0))
1346 flags |= VIR_MIGRATE_LIVE;
1349 if (optbandwidthv == Val_int (0)) /* None */
1351 else /* Some bandwidth */
1352 bandwidth = Int_val (Field (optbandwidthv, 0));
1354 WEAK_SYMBOL_CHECK (virDomainMigrate);
1355 NONBLOCKING (r = virDomainMigrate (dom, dconn, flags, dname, uri, bandwidth));
1356 CHECK_ERROR (!r, conn, "virDomainMigrate");
1358 rv = Val_domain (r, dconnv);
1362 #else /* virDomainMigrate not supported */
1363 NOT_SUPPORTED ("virDomainMigrate");
1368 ocaml_libvirt_domain_migrate_bytecode (value *argv, int argn)
1370 return ocaml_libvirt_domain_migrate_native (argv[0], argv[1], argv[2],
1371 argv[3], argv[4], argv[5],
1376 ocaml_libvirt_domain_block_stats (value domv, value pathv)
1378 #if HAVE_VIRDOMAINBLOCKSTATS
1379 CAMLparam2 (domv, pathv);
1381 virDomainPtr dom = Domain_val (domv);
1382 virConnectPtr conn = Connect_domv (domv);
1383 char *path = String_val (pathv);
1384 struct _virDomainBlockStats stats;
1387 WEAK_SYMBOL_CHECK (virDomainBlockStats);
1388 NONBLOCKING (r = virDomainBlockStats (dom, path, &stats, sizeof stats));
1389 CHECK_ERROR (r == -1, conn, "virDomainBlockStats");
1391 rv = caml_alloc (5, 0);
1392 v = caml_copy_int64 (stats.rd_req); Store_field (rv, 0, v);
1393 v = caml_copy_int64 (stats.rd_bytes); Store_field (rv, 1, v);
1394 v = caml_copy_int64 (stats.wr_req); Store_field (rv, 2, v);
1395 v = caml_copy_int64 (stats.wr_bytes); Store_field (rv, 3, v);
1396 v = caml_copy_int64 (stats.errs); Store_field (rv, 4, v);
1400 NOT_SUPPORTED ("virDomainBlockStats");
1405 ocaml_libvirt_domain_interface_stats (value domv, value pathv)
1407 #if HAVE_VIRDOMAININTERFACESTATS
1408 CAMLparam2 (domv, pathv);
1410 virDomainPtr dom = Domain_val (domv);
1411 virConnectPtr conn = Connect_domv (domv);
1412 char *path = String_val (pathv);
1413 struct _virDomainInterfaceStats stats;
1416 WEAK_SYMBOL_CHECK (virDomainInterfaceStats);
1417 NONBLOCKING (r = virDomainInterfaceStats (dom, path, &stats, sizeof stats));
1418 CHECK_ERROR (r == -1, conn, "virDomainInterfaceStats");
1420 rv = caml_alloc (8, 0);
1421 v = caml_copy_int64 (stats.rx_bytes); Store_field (rv, 0, v);
1422 v = caml_copy_int64 (stats.rx_packets); Store_field (rv, 1, v);
1423 v = caml_copy_int64 (stats.rx_errs); Store_field (rv, 2, v);
1424 v = caml_copy_int64 (stats.rx_drop); Store_field (rv, 3, v);
1425 v = caml_copy_int64 (stats.tx_bytes); Store_field (rv, 4, v);
1426 v = caml_copy_int64 (stats.tx_packets); Store_field (rv, 5, v);
1427 v = caml_copy_int64 (stats.tx_errs); Store_field (rv, 6, v);
1428 v = caml_copy_int64 (stats.tx_drop); Store_field (rv, 7, v);
1432 NOT_SUPPORTED ("virDomainInterfaceStats");
1437 ocaml_libvirt_network_lookup_by_name (value connv, value namev)
1439 CAMLparam2 (connv, namev);
1441 virConnectPtr conn = Connect_val (connv);
1442 char *name = String_val (namev);
1445 NONBLOCKING (r = virNetworkLookupByName (conn, name));
1446 CHECK_ERROR (!r, conn, "virNetworkLookupByName");
1448 rv = Val_network (r, connv);
1453 ocaml_libvirt_network_lookup_by_uuid (value connv, value uuidv)
1455 CAMLparam2 (connv, uuidv);
1457 virConnectPtr conn = Connect_val (connv);
1458 char *uuid = String_val (uuidv);
1461 NONBLOCKING (r = virNetworkLookupByUUID (conn, (unsigned char *) uuid));
1462 CHECK_ERROR (!r, conn, "virNetworkLookupByUUID");
1464 rv = Val_network (r, connv);
1469 ocaml_libvirt_network_lookup_by_uuid_string (value connv, value uuidv)
1471 CAMLparam2 (connv, uuidv);
1473 virConnectPtr conn = Connect_val (connv);
1474 char *uuid = String_val (uuidv);
1477 NONBLOCKING (r = virNetworkLookupByUUIDString (conn, uuid));
1478 CHECK_ERROR (!r, conn, "virNetworkLookupByUUIDString");
1480 rv = Val_network (r, connv);
1485 ocaml_libvirt_network_create_xml (value connv, value xmlv)
1487 CAMLparam2 (connv, xmlv);
1489 virConnectPtr conn = Connect_val (connv);
1490 char *xml = String_val (xmlv);
1493 NONBLOCKING (r = virNetworkCreateXML (conn, xml));
1494 CHECK_ERROR (!r, conn, "virNetworkCreateXML");
1496 rv = Val_network (r, connv);
1501 ocaml_libvirt_network_define_xml (value connv, value xmlv)
1503 CAMLparam2 (connv, xmlv);
1505 virConnectPtr conn = Connect_val (connv);
1506 char *xml = String_val (xmlv);
1509 NONBLOCKING (r = virNetworkDefineXML (conn, xml));
1510 CHECK_ERROR (!r, conn, "virNetworkDefineXML");
1512 rv = Val_network (r, connv);
1517 ocaml_libvirt_network_undefine (value netv)
1520 virNetworkPtr net = Network_val (netv);
1521 virConnectPtr conn = Connect_netv (netv);
1524 NONBLOCKING (r = virNetworkUndefine (net));
1525 CHECK_ERROR (r == -1, conn, "virNetworkUndefine");
1527 CAMLreturn (Val_unit);
1531 ocaml_libvirt_network_create (value netv)
1534 virNetworkPtr net = Network_val (netv);
1535 virConnectPtr conn = Connect_netv (netv);
1538 NONBLOCKING (r = virNetworkCreate (net));
1539 CHECK_ERROR (r == -1, conn, "virNetworkCreate");
1541 CAMLreturn (Val_unit);
1545 ocaml_libvirt_network_destroy (value netv)
1548 virNetworkPtr net = Network_val (netv);
1549 virConnectPtr conn = Connect_netv (netv);
1552 NONBLOCKING (r = virNetworkDestroy (net));
1553 CHECK_ERROR (r == -1, conn, "virNetworkDestroy");
1555 /* So that we don't double-free in the finalizer: */
1556 Network_val (netv) = NULL;
1558 CAMLreturn (Val_unit);
1562 ocaml_libvirt_network_free (value netv)
1565 virNetworkPtr net = Network_val (netv);
1566 virConnectPtr conn = Connect_netv (netv);
1569 NONBLOCKING (r = virNetworkFree (net));
1570 CHECK_ERROR (r == -1, conn, "virNetworkFree");
1572 /* So that we don't double-free in the finalizer: */
1573 Network_val (netv) = NULL;
1575 CAMLreturn (Val_unit);
1579 ocaml_libvirt_network_get_name (value netv)
1583 virNetworkPtr net = Network_val (netv);
1584 virConnectPtr conn = Connect_netv (netv);
1587 NONBLOCKING (r = virNetworkGetName (net));
1588 CHECK_ERROR (!r, conn, "virNetworkGetName");
1590 rv = caml_copy_string (r);
1595 ocaml_libvirt_network_get_uuid (value netv)
1599 virNetworkPtr net = Network_val (netv);
1600 virConnectPtr conn = Connect_netv (netv);
1601 unsigned char uuid[VIR_UUID_BUFLEN];
1604 NONBLOCKING (r = virNetworkGetUUID (net, uuid));
1605 CHECK_ERROR (r == -1, conn, "virNetworkGetUUID");
1607 rv = caml_copy_string ((char *) uuid);
1612 ocaml_libvirt_network_get_uuid_string (value netv)
1616 virNetworkPtr net = Network_val (netv);
1617 virConnectPtr conn = Connect_netv (netv);
1618 char uuid[VIR_UUID_STRING_BUFLEN];
1621 NONBLOCKING (r = virNetworkGetUUIDString (net, uuid));
1622 CHECK_ERROR (r == -1, conn, "virNetworkGetUUIDString");
1624 rv = caml_copy_string (uuid);
1629 ocaml_libvirt_network_get_xml_desc (value netv)
1633 virNetworkPtr net = Network_val (netv);
1634 virConnectPtr conn = Connect_netv (netv);
1637 NONBLOCKING (r = virNetworkGetXMLDesc (net, 0));
1638 CHECK_ERROR (!r, conn, "virNetworkGetXMLDesc");
1640 rv = caml_copy_string (r);
1646 ocaml_libvirt_network_get_bridge_name (value netv)
1650 virNetworkPtr net = Network_val (netv);
1651 virConnectPtr conn = Connect_netv (netv);
1654 NONBLOCKING (r = virNetworkGetBridgeName (net));
1655 CHECK_ERROR (!r, conn, "virNetworkGetBridgeName");
1657 rv = caml_copy_string (r);
1663 ocaml_libvirt_network_get_autostart (value netv)
1666 virNetworkPtr net = Network_val (netv);
1667 virConnectPtr conn = Connect_netv (netv);
1670 NONBLOCKING (r = virNetworkGetAutostart (net, &autostart));
1671 CHECK_ERROR (r == -1, conn, "virNetworkGetAutostart");
1673 CAMLreturn (autostart ? Val_true : Val_false);
1677 ocaml_libvirt_network_set_autostart (value netv, value autostartv)
1679 CAMLparam2 (netv, autostartv);
1680 virNetworkPtr net = Network_val (netv);
1681 virConnectPtr conn = Connect_netv (netv);
1682 int r, autostart = autostartv == Val_true ? 1 : 0;
1684 NONBLOCKING (r = virNetworkSetAutostart (net, autostart));
1685 CHECK_ERROR (r == -1, conn, "virNetworkSetAutostart");
1687 CAMLreturn (Val_unit);
1690 /*----------------------------------------------------------------------*/
1693 ocaml_libvirt_virterror_get_last_error (value unitv)
1697 virErrorPtr err = virGetLastError ();
1699 rv = Val_opt (err, (Val_ptr_t) Val_virterror);
1705 ocaml_libvirt_virterror_get_last_conn_error (value connv)
1709 virConnectPtr conn = Connect_val (connv);
1711 rv = Val_opt (conn, (Val_ptr_t) Val_connect);
1717 ocaml_libvirt_virterror_reset_last_error (value unitv)
1720 virResetLastError ();
1721 CAMLreturn (Val_unit);
1725 ocaml_libvirt_virterror_reset_last_conn_error (value connv)
1728 virConnectPtr conn = Connect_val (connv);
1729 virConnResetLastError (conn);
1730 CAMLreturn (Val_unit);
1733 /*----------------------------------------------------------------------*/
1735 /* Initialise the library. */
1737 ocaml_libvirt_init (value unit)
1743 r = virInitialize ();
1744 CHECK_ERROR (r == -1, NULL, "virInitialize");
1746 CAMLreturn (Val_unit);
1749 /*----------------------------------------------------------------------*/
1752 Optstring_val (value strv)
1754 if (strv == Val_int (0)) /* None */
1756 else /* Some string */
1757 return String_val (Field (strv, 0));
1761 Val_opt (void *ptr, Val_ptr_t Val_ptr)
1764 CAMLlocal2 (optv, ptrv);
1766 if (ptr) { /* Some ptr */
1767 optv = caml_alloc (1, 0);
1768 ptrv = Val_ptr (ptr);
1769 Store_field (optv, 0, ptrv);
1778 option_default (value option, value deflt)
1780 if (option == Val_int (0)) /* "None" */
1782 else /* "Some 'a" */
1783 return Field (option, 0);
1788 _raise_virterror (virConnectPtr conn, const char *fn)
1793 struct _virError err;
1795 errp = conn ? virConnGetLastError (conn) : virGetLastError ();
1798 /* Fake a _virError structure. */
1799 memset (&err, 0, sizeof err);
1800 err.code = VIR_ERR_INTERNAL_ERROR;
1801 err.domain = VIR_FROM_NONE;
1802 err.level = VIR_ERR_ERROR;
1803 err.message = (char *) fn;
1807 rv = Val_virterror (errp);
1808 caml_raise_with_arg (*caml_named_value ("ocaml_libvirt_virterror"), rv);
1811 CAMLreturn (Val_unit);
1814 /* Convert the virErrorNumber, virErrorDomain and virErrorLevel enums
1815 * into values (longs because they are variants in OCaml).
1817 * The enum values are part of the libvirt ABI so they cannot change,
1818 * which means that we can convert these numbers directly into
1819 * OCaml variants (which use the same ordering) very fast.
1821 * The tricky part here is when we are linked to a newer version of
1822 * libvirt than the one we were compiled against. If the newer libvirt
1823 * generates an error code which we don't know about then we need
1824 * to convert it into VIR_*_UNKNOWN (code).
1827 #define MAX_VIR_CODE 44 /* VIR_ERR_INVALID_MAC */
1828 #define MAX_VIR_DOMAIN 16 /* VIR_FROM_STATS_LINUX */
1829 #define MAX_VIR_LEVEL VIR_ERR_ERROR
1832 Val_err_number (virErrorNumber code)
1837 if (0 <= code && code <= MAX_VIR_CODE)
1838 rv = Val_int (code);
1840 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN (code) */
1841 Store_field (rv, 0, Val_int (code));
1848 Val_err_domain (virErrorDomain code)
1853 if (0 <= code && code <= MAX_VIR_DOMAIN)
1854 rv = Val_int (code);
1856 rv = caml_alloc (1, 0); /* VIR_FROM_UNKNOWN (code) */
1857 Store_field (rv, 0, Val_int (code));
1864 Val_err_level (virErrorLevel code)
1869 if (0 <= code && code <= MAX_VIR_LEVEL)
1870 rv = Val_int (code);
1872 rv = caml_alloc (1, 0); /* VIR_ERR_UNKNOWN_LEVEL (code) */
1873 Store_field (rv, 0, Val_int (code));
1879 /* Convert a virterror to a value. */
1881 Val_virterror (virErrorPtr err)
1884 CAMLlocal3 (rv, connv, optv);
1886 rv = caml_alloc (12, 0);
1887 Store_field (rv, 0, Val_err_number (err->code));
1888 Store_field (rv, 1, Val_err_domain (err->domain));
1890 Val_opt (err->message, (Val_ptr_t) caml_copy_string));
1891 Store_field (rv, 3, Val_err_level (err->level));
1893 /* conn, dom and net fields, all optional */
1895 connv = Val_connect_no_finalize (err->conn);
1896 optv = caml_alloc (1, 0);
1897 Store_field (optv, 0, connv);
1898 Store_field (rv, 4, optv); /* Some conn */
1901 optv = caml_alloc (1, 0);
1902 Store_field (optv, 0, Val_domain_no_finalize (err->dom, connv));
1903 Store_field (rv, 5, optv); /* Some (dom, conn) */
1906 Store_field (rv, 5, Val_int (0)); /* None */
1908 optv = caml_alloc (1, 0);
1909 Store_field (optv, 0, Val_network_no_finalize (err->net, connv));
1910 Store_field (rv, 11, optv); /* Some (net, conn) */
1912 Store_field (rv, 11, Val_int (0)); /* None */
1914 Store_field (rv, 4, Val_int (0)); /* None */
1915 Store_field (rv, 5, Val_int (0)); /* None */
1916 Store_field (rv, 11, Val_int (0)); /* None */
1920 Val_opt (err->str1, (Val_ptr_t) caml_copy_string));
1922 Val_opt (err->str2, (Val_ptr_t) caml_copy_string));
1924 Val_opt (err->str3, (Val_ptr_t) caml_copy_string));
1925 Store_field (rv, 9, caml_copy_int32 (err->int1));
1926 Store_field (rv, 10, caml_copy_int32 (err->int2));
1931 static void conn_finalize (value);
1932 static void dom_finalize (value);
1933 static void net_finalize (value);
1935 static struct custom_operations conn_custom_operations = {
1936 "conn_custom_operations",
1938 custom_compare_default,
1939 custom_hash_default,
1940 custom_serialize_default,
1941 custom_deserialize_default
1944 static struct custom_operations dom_custom_operations = {
1945 "dom_custom_operations",
1947 custom_compare_default,
1948 custom_hash_default,
1949 custom_serialize_default,
1950 custom_deserialize_default
1954 static struct custom_operations net_custom_operations = {
1955 "net_custom_operations",
1957 custom_compare_default,
1958 custom_hash_default,
1959 custom_serialize_default,
1960 custom_deserialize_default
1964 Val_connect (virConnectPtr conn)
1968 rv = caml_alloc_custom (&conn_custom_operations,
1969 sizeof (virConnectPtr), 0, 1);
1970 Connect_val (rv) = conn;
1974 /* This wraps up the raw domain handle (Domain.dom). */
1976 Val_dom (virDomainPtr dom)
1980 rv = caml_alloc_custom (&dom_custom_operations,
1981 sizeof (virDomainPtr), 0, 1);
1986 /* This wraps up the raw network handle (Network.net). */
1988 Val_net (virNetworkPtr net)
1992 rv = caml_alloc_custom (&net_custom_operations,
1993 sizeof (virNetworkPtr), 0, 1);
1998 /* No-finalize versions of Val_connect, Val_dom, Val_net ONLY for use
1999 * by virterror wrappers.
2002 Val_connect_no_finalize (virConnectPtr conn)
2006 rv = caml_alloc (1, Abstract_tag);
2007 Store_field (rv, 0, (value) conn);
2012 Val_dom_no_finalize (virDomainPtr dom)
2016 rv = caml_alloc (1, Abstract_tag);
2017 Store_field (rv, 0, (value) dom);
2022 Val_net_no_finalize (virNetworkPtr net)
2026 rv = caml_alloc (1, Abstract_tag);
2027 Store_field (rv, 0, (value) net);
2031 /* This wraps up the (dom, conn) pair (Domain.t). */
2033 Val_domain (virDomainPtr dom, value connv)
2038 rv = caml_alloc_tuple (2);
2040 Store_field (rv, 0, v);
2041 Store_field (rv, 1, connv);
2045 /* This wraps up the (net, conn) pair (Network.t). */
2047 Val_network (virNetworkPtr net, value connv)
2052 rv = caml_alloc_tuple (2);
2054 Store_field (rv, 0, v);
2055 Store_field (rv, 1, connv);
2059 /* No-finalize versions of Val_domain, Val_network ONLY for use by
2060 * virterror wrappers.
2063 Val_domain_no_finalize (virDomainPtr dom, value connv)
2068 rv = caml_alloc_tuple (2);
2069 v = Val_dom_no_finalize (dom);
2070 Store_field (rv, 0, v);
2071 Store_field (rv, 1, connv);
2076 Val_network_no_finalize (virNetworkPtr net, value connv)
2081 rv = caml_alloc_tuple (2);
2082 v = Val_net_no_finalize (net);
2083 Store_field (rv, 0, v);
2084 Store_field (rv, 1, connv);
2089 conn_finalize (value connv)
2091 virConnectPtr conn = Connect_val (connv);
2092 if (conn) (void) virConnectClose (conn);
2096 dom_finalize (value domv)
2098 virDomainPtr dom = Dom_val (domv);
2099 if (dom) (void) virDomainFree (dom);
2103 net_finalize (value netv)
2105 virNetworkPtr net = Net_val (netv);
2106 if (net) (void) virNetworkFree (net);