Remove virConnectListAllDomains.
[ocaml-libvirt.git] / libvirt / libvirt_c_oneoffs.c
1 /* OCaml bindings for libvirt.
2  * (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
3  * http://libvirt.org/
4  *
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.
9  *
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.
14  *
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
18  */
19
20 /* Please read libvirt/README file. */
21
22 /*----------------------------------------------------------------------*/
23
24 CAMLprim value
25 ocaml_libvirt_get_version (value driverv, value unit)
26 {
27   CAMLparam2 (driverv, unit);
28   CAMLlocal1 (rv);
29   const char *driver = Optstring_val (driverv);
30   unsigned long libVer, typeVer = 0, *typeVer_ptr;
31   int r;
32
33   typeVer_ptr = driver ? &typeVer : NULL;
34   NONBLOCKING (r = virGetVersion (&libVer, driver, typeVer_ptr));
35   CHECK_ERROR (r == -1, NULL, "virGetVersion");
36
37   rv = caml_alloc_tuple (2);
38   Store_field (rv, 0, Val_int (libVer));
39   Store_field (rv, 1, Val_int (typeVer));
40   CAMLreturn (rv);
41 }
42
43 /*----------------------------------------------------------------------*/
44
45 /* Connection object. */
46
47 CAMLprim value
48 ocaml_libvirt_connect_open (value namev, value unit)
49 {
50   CAMLparam2 (namev, unit);
51   CAMLlocal1 (rv);
52   const char *name = Optstring_val (namev);
53   virConnectPtr conn;
54
55   NONBLOCKING (conn = virConnectOpen (name));
56   CHECK_ERROR (!conn, NULL, "virConnectOpen");
57
58   rv = Val_connect (conn);
59
60   CAMLreturn (rv);
61 }
62
63 CAMLprim value
64 ocaml_libvirt_connect_open_readonly (value namev, value unit)
65 {
66   CAMLparam2 (namev, unit);
67   CAMLlocal1 (rv);
68   const char *name = Optstring_val (namev);
69   virConnectPtr conn;
70
71   NONBLOCKING (conn = virConnectOpenReadOnly (name));
72   CHECK_ERROR (!conn, NULL, "virConnectOpen");
73
74   rv = Val_connect (conn);
75
76   CAMLreturn (rv);
77 }
78
79 CAMLprim value
80 ocaml_libvirt_connect_get_version (value connv)
81 {
82   CAMLparam1 (connv);
83   virConnectPtr conn = Connect_val (connv);
84   unsigned long hvVer;
85   int r;
86
87   NONBLOCKING (r = virConnectGetVersion (conn, &hvVer));
88   CHECK_ERROR (r == -1, conn, "virConnectGetVersion");
89
90   CAMLreturn (Val_int (hvVer));
91 }
92
93 CAMLprim value
94 ocaml_libvirt_connect_get_max_vcpus (value connv, value typev)
95 {
96   CAMLparam2 (connv, typev);
97   virConnectPtr conn = Connect_val (connv);
98   const char *type = Optstring_val (typev);
99   int r;
100
101   NONBLOCKING (r = virConnectGetMaxVcpus (conn, type));
102   CHECK_ERROR (r == -1, conn, "virConnectGetMaxVcpus");
103
104   CAMLreturn (Val_int (r));
105 }
106
107 CAMLprim value
108 ocaml_libvirt_connect_get_node_info (value connv)
109 {
110   CAMLparam1 (connv);
111   CAMLlocal2 (rv, v);
112   virConnectPtr conn = Connect_val (connv);
113   virNodeInfo info;
114   int r;
115
116   NONBLOCKING (r = virNodeGetInfo (conn, &info));
117   CHECK_ERROR (r == -1, conn, "virNodeGetInfo");
118
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));
128
129   CAMLreturn (rv);
130 }
131
132 #ifdef HAVE_WEAK_SYMBOLS
133 #ifdef HAVE_VIRNODEGETFREEMEMORY
134 extern unsigned long long virNodeGetFreeMemory (virConnectPtr conn)
135   __attribute__((weak));
136 #endif
137 #endif
138
139 CAMLprim value
140 ocaml_libvirt_connect_node_get_free_memory (value connv)
141 {
142 #ifdef HAVE_VIRNODEGETFREEMEMORY
143   CAMLparam1 (connv);
144   CAMLlocal1 (rv);
145   virConnectPtr conn = Connect_val (connv);
146   unsigned long long r;
147
148   WEAK_SYMBOL_CHECK (virNodeGetFreeMemory);
149   NONBLOCKING (r = virNodeGetFreeMemory (conn));
150   CHECK_ERROR (r == 0, conn, "virNodeGetFreeMemory");
151
152   rv = caml_copy_int64 ((int64) r);
153   CAMLreturn (rv);
154 #else
155   not_supported ("virNodeGetFreeMemory");
156 #endif
157 }
158
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));
165 #endif
166 #endif
167
168 CAMLprim value
169 ocaml_libvirt_connect_node_get_cells_free_memory (value connv,
170                                                   value startv, value maxv)
171 {
172 #ifdef HAVE_VIRNODEGETCELLSFREEMEMORY
173   CAMLparam3 (connv, startv, maxv);
174   CAMLlocal2 (rv, iv);
175   virConnectPtr conn = Connect_val (connv);
176   int start = Int_val (startv);
177   int max = Int_val (maxv);
178   int r, i;
179   unsigned long long freemems[max];
180
181   WEAK_SYMBOL_CHECK (virNodeGetCellsFreeMemory);
182   NONBLOCKING (r = virNodeGetCellsFreeMemory (conn, freemems, start, max));
183   CHECK_ERROR (r == -1, conn, "virNodeGetCellsFreeMemory");
184
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);
189   }
190
191   CAMLreturn (rv);
192 #else
193   not_supported ("virNodeGetCellsFreeMemory");
194 #endif
195 }
196
197 CAMLprim value
198 ocaml_libvirt_domain_get_id (value domv)
199 {
200   CAMLparam1 (domv);
201   virDomainPtr dom = Domain_val (domv);
202   /*virConnectPtr conn = Connect_domv (domv);*/
203   unsigned int r;
204
205   NONBLOCKING (r = virDomainGetID (dom));
206   /* In theory this could return -1 on error, but in practice
207    * libvirt never does this unless you call it with a corrupted
208    * or NULL dom object.  So ignore errors here.
209    */
210
211   CAMLreturn (Val_int ((int) r));
212 }
213
214 CAMLprim value
215 ocaml_libvirt_domain_get_max_memory (value domv)
216 {
217   CAMLparam1 (domv);
218   CAMLlocal1 (rv);
219   virDomainPtr dom = Domain_val (domv);
220   virConnectPtr conn = Connect_domv (domv);
221   unsigned long r;
222
223   NONBLOCKING (r = virDomainGetMaxMemory (dom));
224   CHECK_ERROR (r == 0 /* [sic] */, conn, "virDomainGetMaxMemory");
225
226   rv = caml_copy_int64 (r);
227   CAMLreturn (rv);
228 }
229
230 CAMLprim value
231 ocaml_libvirt_domain_set_max_memory (value domv, value memv)
232 {
233   CAMLparam2 (domv, memv);
234   virDomainPtr dom = Domain_val (domv);
235   virConnectPtr conn = Connect_domv (domv);
236   unsigned long mem = Int64_val (memv);
237   int r;
238
239   NONBLOCKING (r = virDomainSetMaxMemory (dom, mem));
240   CHECK_ERROR (r == -1, conn, "virDomainSetMaxMemory");
241
242   CAMLreturn (Val_unit);
243 }
244
245 CAMLprim value
246 ocaml_libvirt_domain_set_memory (value domv, value memv)
247 {
248   CAMLparam2 (domv, memv);
249   virDomainPtr dom = Domain_val (domv);
250   virConnectPtr conn = Connect_domv (domv);
251   unsigned long mem = Int64_val (memv);
252   int r;
253
254   NONBLOCKING (r = virDomainSetMemory (dom, mem));
255   CHECK_ERROR (r == -1, conn, "virDomainSetMemory");
256
257   CAMLreturn (Val_unit);
258 }
259
260 CAMLprim value
261 ocaml_libvirt_domain_get_info (value domv)
262 {
263   CAMLparam1 (domv);
264   CAMLlocal2 (rv, v);
265   virDomainPtr dom = Domain_val (domv);
266   virConnectPtr conn = Connect_domv (domv);
267   virDomainInfo info;
268   int r;
269
270   NONBLOCKING (r = virDomainGetInfo (dom, &info));
271   CHECK_ERROR (r == -1, conn, "virDomainGetInfo");
272
273   rv = caml_alloc (5, 0);
274   Store_field (rv, 0, Val_int (info.state)); // These flags are compatible.
275   v = caml_copy_int64 (info.maxMem); Store_field (rv, 1, v);
276   v = caml_copy_int64 (info.memory); Store_field (rv, 2, v);
277   Store_field (rv, 3, Val_int (info.nrVirtCpu));
278   v = caml_copy_int64 (info.cpuTime); Store_field (rv, 4, v);
279
280   CAMLreturn (rv);
281 }
282
283 #ifdef HAVE_WEAK_SYMBOLS
284 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
285 extern char *virDomainGetSchedulerType(virDomainPtr domain,
286                                        int *nparams)
287   __attribute__((weak));
288 #endif
289 #endif
290
291 CAMLprim value
292 ocaml_libvirt_domain_get_scheduler_type (value domv)
293 {
294 #ifdef HAVE_VIRDOMAINGETSCHEDULERTYPE
295   CAMLparam1 (domv);
296   CAMLlocal2 (rv, strv);
297   virDomainPtr dom = Domain_val (domv);
298   virConnectPtr conn = Connect_domv (domv);
299   char *r;
300   int nparams;
301
302   WEAK_SYMBOL_CHECK (virDomainGetSchedulerType);
303   NONBLOCKING (r = virDomainGetSchedulerType (dom, &nparams));
304   CHECK_ERROR (!r, conn, "virDomainGetSchedulerType");
305
306   rv = caml_alloc_tuple (2);
307   strv = caml_copy_string (r); Store_field (rv, 0, strv);
308   free (r);
309   Store_field (rv, 1, nparams);
310   CAMLreturn (rv);
311 #else
312   not_supported ("virDomainGetSchedulerType");
313 #endif
314 }
315
316 #ifdef HAVE_WEAK_SYMBOLS
317 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
318 extern int virDomainGetSchedulerParameters (virDomainPtr domain,
319                                             virSchedParameterPtr params,
320                                             int *nparams)
321   __attribute__((weak));
322 #endif
323 #endif
324
325 CAMLprim value
326 ocaml_libvirt_domain_get_scheduler_parameters (value domv, value nparamsv)
327 {
328 #ifdef HAVE_VIRDOMAINGETSCHEDULERPARAMETERS
329   CAMLparam2 (domv, nparamsv);
330   CAMLlocal4 (rv, v, v2, v3);
331   virDomainPtr dom = Domain_val (domv);
332   virConnectPtr conn = Connect_domv (domv);
333   int nparams = Int_val (nparamsv);
334   virSchedParameter params[nparams];
335   int r, i;
336
337   WEAK_SYMBOL_CHECK (virDomainGetSchedulerParameters);
338   NONBLOCKING (r = virDomainGetSchedulerParameters (dom, params, &nparams));
339   CHECK_ERROR (r == -1, conn, "virDomainGetSchedulerParameters");
340
341   rv = caml_alloc (nparams, 0);
342   for (i = 0; i < nparams; ++i) {
343     v = caml_alloc_tuple (2); Store_field (rv, i, v);
344     v2 = caml_copy_string (params[i].field); Store_field (v, 0, v2);
345     switch (params[i].type) {
346     case VIR_DOMAIN_SCHED_FIELD_INT:
347       v2 = caml_alloc (1, 0);
348       v3 = caml_copy_int32 (params[i].value.i); Store_field (v2, 0, v3);
349       break;
350     case VIR_DOMAIN_SCHED_FIELD_UINT:
351       v2 = caml_alloc (1, 1);
352       v3 = caml_copy_int32 (params[i].value.ui); Store_field (v2, 0, v3);
353       break;
354     case VIR_DOMAIN_SCHED_FIELD_LLONG:
355       v2 = caml_alloc (1, 2);
356       v3 = caml_copy_int64 (params[i].value.l); Store_field (v2, 0, v3);
357       break;
358     case VIR_DOMAIN_SCHED_FIELD_ULLONG:
359       v2 = caml_alloc (1, 3);
360       v3 = caml_copy_int64 (params[i].value.ul); Store_field (v2, 0, v3);
361       break;
362     case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
363       v2 = caml_alloc (1, 4);
364       v3 = caml_copy_double (params[i].value.d); Store_field (v2, 0, v3);
365       break;
366     case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
367       v2 = caml_alloc (1, 5);
368       Store_field (v2, 0, Val_int (params[i].value.b));
369       break;
370     default:
371       caml_failwith ((char *)__FUNCTION__);
372     }
373     Store_field (v, 1, v2);
374   }
375   CAMLreturn (rv);
376 #else
377   not_supported ("virDomainGetSchedulerParameters");
378 #endif
379 }
380
381 #ifdef HAVE_WEAK_SYMBOLS
382 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
383 extern int virDomainSetSchedulerParameters (virDomainPtr domain,
384                                             virSchedParameterPtr params,
385                                             int nparams)
386   __attribute__((weak));
387 #endif
388 #endif
389
390 CAMLprim value
391 ocaml_libvirt_domain_set_scheduler_parameters (value domv, value paramsv)
392 {
393 #ifdef HAVE_VIRDOMAINSETSCHEDULERPARAMETERS
394   CAMLparam2 (domv, paramsv);
395   CAMLlocal1 (v);
396   virDomainPtr dom = Domain_val (domv);
397   virConnectPtr conn = Connect_domv (domv);
398   int nparams = Wosize_val (paramsv);
399   virSchedParameter params[nparams];
400   int r, i;
401   char *name;
402
403   for (i = 0; i < nparams; ++i) {
404     v = Field (paramsv, i);     /* Points to the two-element tuple. */
405     name = String_val (Field (v, 0));
406     strncpy (params[i].field, name, VIR_DOMAIN_SCHED_FIELD_LENGTH);
407     params[i].field[VIR_DOMAIN_SCHED_FIELD_LENGTH-1] = '\0';
408     v = Field (v, 1);           /* Points to the sched_param_value block. */
409     switch (Tag_val (v)) {
410     case 0:
411       params[i].type = VIR_DOMAIN_SCHED_FIELD_INT;
412       params[i].value.i = Int32_val (Field (v, 0));
413       break;
414     case 1:
415       params[i].type = VIR_DOMAIN_SCHED_FIELD_UINT;
416       params[i].value.ui = Int32_val (Field (v, 0));
417       break;
418     case 2:
419       params[i].type = VIR_DOMAIN_SCHED_FIELD_LLONG;
420       params[i].value.l = Int64_val (Field (v, 0));
421       break;
422     case 3:
423       params[i].type = VIR_DOMAIN_SCHED_FIELD_ULLONG;
424       params[i].value.ul = Int64_val (Field (v, 0));
425       break;
426     case 4:
427       params[i].type = VIR_DOMAIN_SCHED_FIELD_DOUBLE;
428       params[i].value.d = Double_val (Field (v, 0));
429       break;
430     case 5:
431       params[i].type = VIR_DOMAIN_SCHED_FIELD_BOOLEAN;
432       params[i].value.b = Int_val (Field (v, 0));
433       break;
434     default:
435       caml_failwith ((char *)__FUNCTION__);
436     }
437   }
438
439   WEAK_SYMBOL_CHECK (virDomainSetSchedulerParameters);
440   NONBLOCKING (r = virDomainSetSchedulerParameters (dom, params, nparams));
441   CHECK_ERROR (r == -1, conn, "virDomainSetSchedulerParameters");
442
443   CAMLreturn (Val_unit);
444 #else
445   not_supported ("virDomainSetSchedulerParameters");
446 #endif
447 }
448
449 CAMLprim value
450 ocaml_libvirt_domain_set_vcpus (value domv, value nvcpusv)
451 {
452   CAMLparam2 (domv, nvcpusv);
453   virDomainPtr dom = Domain_val (domv);
454   virConnectPtr conn = Connect_domv (domv);
455   int r, nvcpus = Int_val (nvcpusv);
456
457   NONBLOCKING (r = virDomainSetVcpus (dom, nvcpus));
458   CHECK_ERROR (r == -1, conn, "virDomainSetVcpus");
459
460   CAMLreturn (Val_unit);
461 }
462
463 CAMLprim value
464 ocaml_libvirt_domain_pin_vcpu (value domv, value vcpuv, value cpumapv)
465 {
466   CAMLparam3 (domv, vcpuv, cpumapv);
467   virDomainPtr dom = Domain_val (domv);
468   virConnectPtr conn = Connect_domv (domv);
469   int maplen = caml_string_length (cpumapv);
470   unsigned char *cpumap = (unsigned char *) String_val (cpumapv);
471   int vcpu = Int_val (vcpuv);
472   int r;
473
474   NONBLOCKING (r = virDomainPinVcpu (dom, vcpu, cpumap, maplen));
475   CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
476
477   CAMLreturn (Val_unit);
478 }
479
480 CAMLprim value
481 ocaml_libvirt_domain_get_vcpus (value domv, value maxinfov, value maplenv)
482 {
483   CAMLparam3 (domv, maxinfov, maplenv);
484   CAMLlocal5 (rv, infov, strv, v, v2);
485   virDomainPtr dom = Domain_val (domv);
486   virConnectPtr conn = Connect_domv (domv);
487   int maxinfo = Int_val (maxinfov);
488   int maplen = Int_val (maplenv);
489   virVcpuInfo info[maxinfo];
490   unsigned char cpumaps[maxinfo * maplen];
491   int r, i;
492
493   memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
494   memset (cpumaps, 0, maxinfo * maplen);
495
496   NONBLOCKING (r = virDomainGetVcpus (dom, info, maxinfo, cpumaps, maplen));
497   CHECK_ERROR (r == -1, conn, "virDomainPinVcpu");
498
499   /* Copy the virVcpuInfo structures. */
500   infov = caml_alloc (maxinfo, 0);
501   for (i = 0; i < maxinfo; ++i) {
502     v2 = caml_alloc (4, 0); Store_field (infov, i, v2);
503     Store_field (v2, 0, Val_int (info[i].number));
504     Store_field (v2, 1, Val_int (info[i].state));
505     v = caml_copy_int64 (info[i].cpuTime); Store_field (v2, 2, v);
506     Store_field (v2, 3, Val_int (info[i].cpu));
507   }
508
509   /* Copy the bitmap. */
510   strv = caml_alloc_string (maxinfo * maplen);
511   memcpy (String_val (strv), cpumaps, maxinfo * maplen);
512
513   /* Allocate the tuple and return it. */
514   rv = caml_alloc_tuple (3);
515   Store_field (rv, 0, Val_int (r)); /* number of CPUs. */
516   Store_field (rv, 1, infov);
517   Store_field (rv, 2, strv);
518
519   CAMLreturn (rv);
520 }
521
522 #ifdef HAVE_WEAK_SYMBOLS
523 #ifdef HAVE_VIRDOMAINMIGRATE
524 extern virDomainPtr virDomainMigrate (virDomainPtr domain, virConnectPtr dconn,
525                                       unsigned long flags, const char *dname,
526                                       const char *uri, unsigned long bandwidth)
527   __attribute__((weak));
528 #endif
529 #endif
530
531 CAMLprim value
532 ocaml_libvirt_domain_migrate_native (value domv, value dconnv, value flagsv, value optdnamev, value opturiv, value optbandwidthv, value unitv)
533 {
534 #ifdef HAVE_VIRDOMAINMIGRATE
535   CAMLparam5 (domv, dconnv, flagsv, optdnamev, opturiv);
536   CAMLxparam2 (optbandwidthv, unitv);
537   CAMLlocal2 (flagv, rv);
538   virDomainPtr dom = Domain_val (domv);
539   virConnectPtr conn = Connect_domv (domv);
540   virConnectPtr dconn = Connect_val (dconnv);
541   int flags = 0;
542   const char *dname = Optstring_val (optdnamev);
543   const char *uri = Optstring_val (opturiv);
544   unsigned long bandwidth;
545   virDomainPtr r;
546
547   /* Iterate over the list of flags. */
548   for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1))
549     {
550       flagv = Field (flagsv, 0);
551       if (flagv == Val_int (0))
552         flags |= VIR_MIGRATE_LIVE;
553     }
554
555   if (optbandwidthv == Val_int (0)) /* None */
556     bandwidth = 0;
557   else                          /* Some bandwidth */
558     bandwidth = Int_val (Field (optbandwidthv, 0));
559
560   WEAK_SYMBOL_CHECK (virDomainMigrate);
561   NONBLOCKING (r = virDomainMigrate (dom, dconn, flags, dname, uri, bandwidth));
562   CHECK_ERROR (!r, conn, "virDomainMigrate");
563
564   rv = Val_domain (r, dconnv);
565
566   CAMLreturn (rv);
567
568 #else /* virDomainMigrate not supported */
569   not_supported ("virDomainMigrate");
570 #endif
571 }
572
573 CAMLprim value
574 ocaml_libvirt_domain_migrate_bytecode (value *argv, int argn)
575 {
576   return ocaml_libvirt_domain_migrate_native (argv[0], argv[1], argv[2],
577                                               argv[3], argv[4], argv[5],
578                                               argv[6]);
579 }
580
581 #ifdef HAVE_WEAK_SYMBOLS
582 #ifdef HAVE_VIRDOMAINBLOCKSTATS
583 extern int virDomainBlockStats (virDomainPtr dom,
584                                 const char *path,
585                                 virDomainBlockStatsPtr stats,
586                                 size_t size)
587   __attribute__((weak));
588 #endif
589 #endif
590
591 CAMLprim value
592 ocaml_libvirt_domain_block_stats (value domv, value pathv)
593 {
594 #if HAVE_VIRDOMAINBLOCKSTATS
595   CAMLparam2 (domv, pathv);
596   CAMLlocal2 (rv,v);
597   virDomainPtr dom = Domain_val (domv);
598   virConnectPtr conn = Connect_domv (domv);
599   char *path = String_val (pathv);
600   struct _virDomainBlockStats stats;
601   int r;
602
603   WEAK_SYMBOL_CHECK (virDomainBlockStats);
604   NONBLOCKING (r = virDomainBlockStats (dom, path, &stats, sizeof stats));
605   CHECK_ERROR (r == -1, conn, "virDomainBlockStats");
606
607   rv = caml_alloc (5, 0);
608   v = caml_copy_int64 (stats.rd_req); Store_field (rv, 0, v);
609   v = caml_copy_int64 (stats.rd_bytes); Store_field (rv, 1, v);
610   v = caml_copy_int64 (stats.wr_req); Store_field (rv, 2, v);
611   v = caml_copy_int64 (stats.wr_bytes); Store_field (rv, 3, v);
612   v = caml_copy_int64 (stats.errs); Store_field (rv, 4, v);
613
614   CAMLreturn (rv);
615 #else
616   not_supported ("virDomainBlockStats");
617 #endif
618 }
619
620 #ifdef HAVE_WEAK_SYMBOLS
621 #ifdef HAVE_VIRDOMAININTERFACESTATS
622 extern int virDomainInterfaceStats (virDomainPtr dom,
623                                     const char *path,
624                                     virDomainInterfaceStatsPtr stats,
625                                     size_t size)
626   __attribute__((weak));
627 #endif
628 #endif
629
630 CAMLprim value
631 ocaml_libvirt_domain_interface_stats (value domv, value pathv)
632 {
633 #if HAVE_VIRDOMAININTERFACESTATS
634   CAMLparam2 (domv, pathv);
635   CAMLlocal2 (rv,v);
636   virDomainPtr dom = Domain_val (domv);
637   virConnectPtr conn = Connect_domv (domv);
638   char *path = String_val (pathv);
639   struct _virDomainInterfaceStats stats;
640   int r;
641
642   WEAK_SYMBOL_CHECK (virDomainInterfaceStats);
643   NONBLOCKING (r = virDomainInterfaceStats (dom, path, &stats, sizeof stats));
644   CHECK_ERROR (r == -1, conn, "virDomainInterfaceStats");
645
646   rv = caml_alloc (8, 0);
647   v = caml_copy_int64 (stats.rx_bytes); Store_field (rv, 0, v);
648   v = caml_copy_int64 (stats.rx_packets); Store_field (rv, 1, v);
649   v = caml_copy_int64 (stats.rx_errs); Store_field (rv, 2, v);
650   v = caml_copy_int64 (stats.rx_drop); Store_field (rv, 3, v);
651   v = caml_copy_int64 (stats.tx_bytes); Store_field (rv, 4, v);
652   v = caml_copy_int64 (stats.tx_packets); Store_field (rv, 5, v);
653   v = caml_copy_int64 (stats.tx_errs); Store_field (rv, 6, v);
654   v = caml_copy_int64 (stats.tx_drop); Store_field (rv, 7, v);
655
656   CAMLreturn (rv);
657 #else
658   not_supported ("virDomainInterfaceStats");
659 #endif
660 }
661
662 #ifdef HAVE_WEAK_SYMBOLS
663 #ifdef HAVE_VIRDOMAINBLOCKPEEK
664 extern int virDomainBlockPeek (virDomainPtr domain,
665                                const char *path,
666                                unsigned long long offset,
667                                size_t size,
668                                void *buffer,
669                                unsigned int flags)
670   __attribute__((weak));
671 #endif
672 #endif
673
674 CAMLprim value
675 ocaml_libvirt_domain_block_peek_native (value domv, value pathv, value offsetv, value sizev, value bufferv, value boffv)
676 {
677 #ifdef HAVE_VIRDOMAINBLOCKPEEK
678   CAMLparam5 (domv, pathv, offsetv, sizev, bufferv);
679   CAMLxparam1 (boffv);
680   virDomainPtr dom = Domain_val (domv);
681   virConnectPtr conn = Connect_domv (domv);
682   const char *path = String_val (pathv);
683   unsigned long long offset = Int64_val (offsetv);
684   size_t size = Int_val (sizev);
685   char *buffer = String_val (bufferv);
686   int boff = Int_val (boffv);
687   int r;
688
689   /* Check that the return buffer is big enough. */
690   if (caml_string_length (bufferv) < boff + size)
691     caml_failwith ("virDomainBlockPeek: return buffer too short");
692
693   WEAK_SYMBOL_CHECK (virDomainBlockPeek);
694   /* NB. not NONBLOCKING because buffer might move (XXX) */
695   r = virDomainBlockPeek (dom, path, offset, size, buffer+boff, 0);
696   CHECK_ERROR (r == -1, conn, "virDomainBlockPeek");
697
698   CAMLreturn (Val_unit);
699
700 #else /* virDomainBlockPeek not supported */
701   not_supported ("virDomainBlockPeek");
702 #endif
703 }
704
705 CAMLprim value
706 ocaml_libvirt_domain_block_peek_bytecode (value *argv, int argn)
707 {
708   return ocaml_libvirt_domain_block_peek_native (argv[0], argv[1], argv[2],
709                                                  argv[3], argv[4], argv[5]);
710 }
711
712 #ifdef HAVE_WEAK_SYMBOLS
713 #ifdef HAVE_VIRDOMAINMEMORYPEEK
714 extern int virDomainMemoryPeek (virDomainPtr domain,
715                                 unsigned long long start,
716                                 size_t size,
717                                 void *buffer,
718                                 unsigned int flags)
719   __attribute__((weak));
720 #endif
721 #endif
722
723 CAMLprim value
724 ocaml_libvirt_domain_memory_peek_native (value domv, value flagsv, value offsetv, value sizev, value bufferv, value boffv)
725 {
726 #ifdef HAVE_VIRDOMAINMEMORYPEEK
727   CAMLparam5 (domv, flagsv, offsetv, sizev, bufferv);
728   CAMLxparam1 (boffv);
729   CAMLlocal1 (flagv);
730   virDomainPtr dom = Domain_val (domv);
731   virConnectPtr conn = Connect_domv (domv);
732   int flags = 0;
733   unsigned long long offset = Int64_val (offsetv);
734   size_t size = Int_val (sizev);
735   char *buffer = String_val (bufferv);
736   int boff = Int_val (boffv);
737   int r;
738
739   /* Check that the return buffer is big enough. */
740   if (caml_string_length (bufferv) < boff + size)
741     caml_failwith ("virDomainMemoryPeek: return buffer too short");
742
743   /* Do flags. */
744   for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1))
745     {
746       flagv = Field (flagsv, 0);
747       if (flagv == Val_int (0))
748         flags |= VIR_MEMORY_VIRTUAL;
749     }
750
751   WEAK_SYMBOL_CHECK (virDomainMemoryPeek);
752   /* NB. not NONBLOCKING because buffer might move (XXX) */
753   r = virDomainMemoryPeek (dom, offset, size, buffer+boff, flags);
754   CHECK_ERROR (r == -1, conn, "virDomainMemoryPeek");
755
756   CAMLreturn (Val_unit);
757
758 #else /* virDomainMemoryPeek not supported */
759   not_supported ("virDomainMemoryPeek");
760 #endif
761 }
762
763 CAMLprim value
764 ocaml_libvirt_domain_memory_peek_bytecode (value *argv, int argn)
765 {
766   return ocaml_libvirt_domain_memory_peek_native (argv[0], argv[1], argv[2],
767                                                   argv[3], argv[4], argv[5]);
768 }
769
770 #ifdef HAVE_WEAK_SYMBOLS
771 #ifdef HAVE_VIRSTORAGEPOOLGETINFO
772 extern int virStoragePoolGetInfo(virStoragePoolPtr pool, virStoragePoolInfoPtr info)
773   __attribute__((weak));
774 #endif
775 #endif
776
777 CAMLprim value
778 ocaml_libvirt_storage_pool_get_info (value poolv)
779 {
780 #if HAVE_VIRSTORAGEPOOLGETINFO
781   CAMLparam1 (poolv);
782   CAMLlocal2 (rv, v);
783   virStoragePoolPtr pool = Pool_val (poolv);
784   virConnectPtr conn = Connect_polv (poolv);
785   virStoragePoolInfo info;
786   int r;
787
788   WEAK_SYMBOL_CHECK (virStoragePoolGetInfo);
789   NONBLOCKING (r = virStoragePoolGetInfo (pool, &info));
790   CHECK_ERROR (r == -1, conn, "virStoragePoolGetInfo");
791
792   rv = caml_alloc (4, 0);
793   Store_field (rv, 0, Val_int (info.state));
794   v = caml_copy_int64 (info.capacity); Store_field (rv, 1, v);
795   v = caml_copy_int64 (info.allocation); Store_field (rv, 2, v);
796   v = caml_copy_int64 (info.available); Store_field (rv, 3, v);
797
798   CAMLreturn (rv);
799 #else
800   not_supported ("virStoragePoolGetInfo");
801 #endif
802 }
803
804 #ifdef HAVE_WEAK_SYMBOLS
805 #ifdef HAVE_VIRSTORAGEVOLGETINFO
806 extern int virStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info)
807   __attribute__((weak));
808 #endif
809 #endif
810
811 CAMLprim value
812 ocaml_libvirt_storage_vol_get_info (value volv)
813 {
814 #if HAVE_VIRSTORAGEVOLGETINFO
815   CAMLparam1 (volv);
816   CAMLlocal2 (rv, v);
817   virStorageVolPtr vol = Volume_val (volv);
818   virConnectPtr conn = Connect_volv (volv);
819   virStorageVolInfo info;
820   int r;
821
822   WEAK_SYMBOL_CHECK (virStorageVolGetInfo);
823   NONBLOCKING (r = virStorageVolGetInfo (vol, &info));
824   CHECK_ERROR (r == -1, conn, "virStorageVolGetInfo");
825
826   rv = caml_alloc (3, 0);
827   Store_field (rv, 0, Val_int (info.type));
828   v = caml_copy_int64 (info.capacity); Store_field (rv, 1, v);
829   v = caml_copy_int64 (info.allocation); Store_field (rv, 1, v);
830
831   CAMLreturn (rv);
832 #else
833   not_supported ("virStorageVolGetInfo");
834 #endif
835 }
836
837 /*----------------------------------------------------------------------*/
838
839 CAMLprim value
840 ocaml_libvirt_virterror_get_last_error (value unitv)
841 {
842   CAMLparam1 (unitv);
843   CAMLlocal1 (rv);
844   virErrorPtr err = virGetLastError ();
845
846   rv = Val_opt (err, (Val_ptr_t) Val_virterror);
847
848   CAMLreturn (rv);
849 }
850
851 CAMLprim value
852 ocaml_libvirt_virterror_get_last_conn_error (value connv)
853 {
854   CAMLparam1 (connv);
855   CAMLlocal1 (rv);
856   virConnectPtr conn = Connect_val (connv);
857
858   rv = Val_opt (conn, (Val_ptr_t) Val_connect);
859
860   CAMLreturn (rv);
861 }
862
863 CAMLprim value
864 ocaml_libvirt_virterror_reset_last_error (value unitv)
865 {
866   CAMLparam1 (unitv);
867   virResetLastError ();
868   CAMLreturn (Val_unit);
869 }
870
871 CAMLprim value
872 ocaml_libvirt_virterror_reset_last_conn_error (value connv)
873 {
874   CAMLparam1 (connv);
875   virConnectPtr conn = Connect_val (connv);
876   virConnResetLastError (conn);
877   CAMLreturn (Val_unit);
878 }
879
880 /*----------------------------------------------------------------------*/
881
882 /* Initialise the library. */
883 CAMLprim value
884 ocaml_libvirt_init (value unit)
885 {
886   CAMLparam1 (unit);
887   CAMLlocal1 (rv);
888   int r;
889
890   r = virInitialize ();
891   CHECK_ERROR (r == -1, NULL, "virInitialize");
892
893   CAMLreturn (Val_unit);
894 }