Added support for new API calls:
[virt-top.git] / libvirt / libvirt.ml
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 type uuid = string
21
22 type xml = string
23
24 external get_version : ?driver:string -> unit -> int * int = "ocaml_libvirt_get_version"
25
26 let uuid_length = 16
27 let uuid_string_length = 36
28
29 (* http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/80683af867cce6bf8fff273973f70c95.en.html *)
30 type rw = [`R|`W]
31 type ro = [`R]
32
33 module Connect =
34 struct
35   type 'rw t
36
37   type node_info = {
38     model : string;
39     memory : int64;
40     cpus : int;
41     mhz : int;
42     nodes : int;
43     sockets : int;
44     cores : int;
45     threads : int;
46   }
47
48   external connect : ?name:string -> unit -> rw t = "ocaml_libvirt_connect_open"
49   external connect_readonly : ?name:string -> unit -> ro t = "ocaml_libvirt_connect_open_readonly"
50   external close : [>`R] t -> unit = "ocaml_libvirt_connect_close"
51   external get_type : [>`R] t -> string = "ocaml_libvirt_connect_get_type"
52   external get_version : [>`R] t -> int = "ocaml_libvirt_connect_get_version"
53   external get_hostname : [>`R] t -> string = "ocaml_libvirt_connect_get_hostname"
54   external get_uri : [>`R] t -> string = "ocaml_libvirt_connect_get_uri"
55   external get_max_vcpus : [>`R] t -> ?type_:string -> unit -> int = "ocaml_libvirt_connect_get_max_vcpus"
56   external list_domains : [>`R] t -> int -> int array = "ocaml_libvirt_connect_list_domains"
57   external num_of_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_domains"
58   external get_capabilities : [>`R] t -> string = "ocaml_libvirt_connect_get_capabilities"
59   external num_of_defined_domains : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_domains"
60   external list_defined_domains : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_domains"
61   external num_of_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_networks"
62   external list_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_networks"
63   external num_of_defined_networks : [>`R] t -> int = "ocaml_libvirt_connect_num_of_defined_networks"
64   external list_defined_networks : [>`R] t -> int -> string array = "ocaml_libvirt_connect_list_defined_networks"
65   external get_node_info : [>`R] t -> node_info = "ocaml_libvirt_connect_get_node_info"
66   external node_get_free_memory : [> `R] t -> int64 = "ocaml_libvirt_connect_node_get_free_memory"
67   external node_get_cells_free_memory : [> `R] t -> int -> int -> int64 array = "ocaml_libvirt_connect_node_get_cells_free_memory"
68
69   (* See VIR_NODEINFO_MAXCPUS macro defined in <libvirt.h>. *)
70   let maxcpus_of_node_info { nodes = nodes; sockets = sockets;
71                              cores = cores; threads = threads } =
72     nodes * sockets * cores * threads
73
74   (* See VIR_CPU_MAPLEN macro defined in <libvirt.h>. *)
75   let cpumaplen nr_cpus =
76     (nr_cpus + 7) / 8
77
78   (* See VIR_USE_CPU, VIR_UNUSE_CPU, VIR_CPU_USABLE macros defined in <libvirt.h>. *)
79   let use_cpu cpumap cpu =
80     cpumap.[cpu/8] <-
81       Char.chr (Char.code cpumap.[cpu/8] lor (1 lsl (cpu mod 8)))
82   let unuse_cpu cpumap cpu =
83     cpumap.[cpu/8] <-
84       Char.chr (Char.code cpumap.[cpu/8] land (lnot (1 lsl (cpu mod 8))))
85   let cpu_usable cpumaps maplen vcpu cpu =
86     Char.code cpumaps.[vcpu*maplen + cpu/8] land (1 lsl (cpu mod 8)) <> 0
87
88   external const : [>`R] t -> ro t = "%identity"
89 end
90
91 module Domain =
92 struct
93   type 'rw dom
94   type 'rw t = 'rw dom * 'rw Connect.t
95
96   type state =
97     | InfoNoState | InfoRunning | InfoBlocked | InfoPaused
98     | InfoShutdown | InfoShutoff | InfoCrashed
99
100   type info = {
101     state : state;
102     max_mem : int64;
103     memory : int64;
104     nr_virt_cpu : int;
105     cpu_time : int64;
106   }
107
108   type vcpu_state = VcpuOffline | VcpuRunning | VcpuBlocked
109
110   type vcpu_info = {
111     number : int;
112     vcpu_state : vcpu_state;
113     vcpu_time : int64;
114     cpu : int;
115   }
116
117   type sched_param = string * sched_param_value
118   and sched_param_value =
119     | SchedFieldInt32 of int32 | SchedFieldUInt32 of int32
120     | SchedFieldInt64 of int64 | SchedFieldUInt64 of int64
121     | SchedFieldFloat of float | SchedFieldBool of bool
122
123   type migrate_flag = Live
124
125   type block_stats = {
126     rd_req : int64;
127     rd_bytes : int64;
128     wr_req : int64;
129     wr_bytes : int64;
130     errs : int64;
131   }
132
133   type interface_stats = {
134     rx_bytes : int64;
135     rx_packets : int64;
136     rx_errs : int64;
137     rx_drop : int64;
138     tx_bytes : int64;
139     tx_packets : int64;
140     tx_errs : int64;
141     tx_drop : int64;
142   }
143
144   external create_linux : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_create_linux"
145   external lookup_by_id : 'a Connect.t -> int -> 'a t = "ocaml_libvirt_domain_lookup_by_id"
146   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid"
147   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_uuid_string"
148   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_domain_lookup_by_name"
149   external destroy : [>`W] t -> unit = "ocaml_libvirt_domain_destroy"
150   external free : [>`R] t -> unit = "ocaml_libvirt_domain_free"
151   external suspend : [>`W] t -> unit = "ocaml_libvirt_domain_suspend"
152   external resume : [>`W] t -> unit = "ocaml_libvirt_domain_resume"
153   external save : [>`W] t -> string -> unit = "ocaml_libvirt_domain_save"
154   external restore : [>`W] Connect.t -> string -> unit = "ocaml_libvirt_domain_restore"
155   external core_dump : [>`W] t -> string -> unit = "ocaml_libvirt_domain_core_dump"
156   external shutdown : [>`W] t -> unit = "ocaml_libvirt_domain_shutdown"
157   external reboot : [>`W] t -> unit = "ocaml_libvirt_domain_reboot"
158   external get_name : [>`R] t -> string = "ocaml_libvirt_domain_get_name"
159   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_domain_get_uuid"
160   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_domain_get_uuid_string"
161   external get_id : [>`R] t -> int = "ocaml_libvirt_domain_get_id"
162   external get_os_type : [>`R] t -> string = "ocaml_libvirt_domain_get_os_type"
163   external get_max_memory : [>`R] t -> int64 = "ocaml_libvirt_domain_get_max_memory"
164   external set_max_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_max_memory"
165   external set_memory : [>`W] t -> int64 -> unit = "ocaml_libvirt_domain_set_memory"
166   external get_info : [>`R] t -> info = "ocaml_libvirt_domain_get_info"
167   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_domain_get_xml_desc"
168   external get_scheduler_type : [>`R] t -> string * int = "ocaml_libvirt_domain_get_scheduler_type"
169   external get_scheduler_parameters : [>`R] t -> int -> sched_param array = "ocaml_libvirt_domain_get_scheduler_parameters"
170   external set_scheduler_parameters : [>`W] t -> sched_param array -> unit = "ocaml_libvirt_domain_set_scheduler_parameters"
171   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_domain_define_xml"
172   external undefine : [>`W] t -> unit = "ocaml_libvirt_domain_undefine"
173   external create : [>`W] t -> unit = "ocaml_libvirt_domain_create"
174   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_domain_get_autostart"
175   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_domain_set_autostart"
176   external set_vcpus : [>`W] t -> int -> unit = "ocaml_libvirt_domain_set_vcpus"
177   external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
178   external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
179   external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
180   external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
181   external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
182   external migrate : [>`W] t -> [>`W] Connect.t -> migrate_flag list -> ?dname:string -> ?uri:string -> ?bandwidth:int -> unit -> rw t = "ocaml_libvirt_domain_migrate_bytecode" "ocaml_libvirt_domain_migrate_native"
183   external block_stats : [>`R] t -> string -> block_stats = "ocaml_libvirt_domain_block_stats"
184   external interface_stats : [>`R] t -> string -> interface_stats = "ocaml_libvirt_domain_interface_stats"
185
186   external const : [>`R] t -> ro t = "%identity"
187 end
188
189 module Network =
190 struct
191   type 'rw net
192   type 'rw t = 'rw net * 'rw Connect.t
193
194   external lookup_by_name : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_name"
195   external lookup_by_uuid : 'a Connect.t -> uuid -> 'a t = "ocaml_libvirt_network_lookup_by_uuid"
196   external lookup_by_uuid_string : 'a Connect.t -> string -> 'a t = "ocaml_libvirt_network_lookup_by_uuid_string"
197   external create_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_create_xml"
198   external define_xml : [>`W] Connect.t -> xml -> rw t = "ocaml_libvirt_network_define_xml"
199   external undefine : [>`W] t -> unit = "ocaml_libvirt_network_undefine"
200   external create : [>`W] t -> unit = "ocaml_libvirt_network_create"
201   external destroy : [>`W] t -> unit = "ocaml_libvirt_network_destroy"
202   external free : [>`R] t -> unit = "ocaml_libvirt_network_free"
203   external get_name : [>`R] t -> string = "ocaml_libvirt_network_get_name"
204   external get_uuid : [>`R] t -> uuid = "ocaml_libvirt_network_get_uuid"
205   external get_uuid_string : [>`R] t -> string = "ocaml_libvirt_network_get_uuid_string"
206   external get_xml_desc : [>`R] t -> xml = "ocaml_libvirt_network_get_xml_desc"
207   external get_bridge_name : [>`R] t -> string = "ocaml_libvirt_network_get_bridge_name"
208   external get_autostart : [>`R] t -> bool = "ocaml_libvirt_network_get_autostart"
209   external set_autostart : [>`W] t -> bool -> unit = "ocaml_libvirt_network_set_autostart"
210
211   external const : [>`R] t -> ro t = "%identity"
212 end
213
214 module Virterror =
215 struct
216   type code =
217     | VIR_ERR_OK
218     | VIR_ERR_INTERNAL_ERROR
219     | VIR_ERR_NO_MEMORY
220     | VIR_ERR_NO_SUPPORT
221     | VIR_ERR_UNKNOWN_HOST
222     | VIR_ERR_NO_CONNECT
223     | VIR_ERR_INVALID_CONN
224     | VIR_ERR_INVALID_DOMAIN
225     | VIR_ERR_INVALID_ARG
226     | VIR_ERR_OPERATION_FAILED
227     | VIR_ERR_GET_FAILED
228     | VIR_ERR_POST_FAILED
229     | VIR_ERR_HTTP_ERROR
230     | VIR_ERR_SEXPR_SERIAL
231     | VIR_ERR_NO_XEN
232     | VIR_ERR_XEN_CALL
233     | VIR_ERR_OS_TYPE
234     | VIR_ERR_NO_KERNEL
235     | VIR_ERR_NO_ROOT
236     | VIR_ERR_NO_SOURCE
237     | VIR_ERR_NO_TARGET
238     | VIR_ERR_NO_NAME
239     | VIR_ERR_NO_OS
240     | VIR_ERR_NO_DEVICE
241     | VIR_ERR_NO_XENSTORE
242     | VIR_ERR_DRIVER_FULL
243     | VIR_ERR_CALL_FAILED
244     | VIR_ERR_XML_ERROR
245     | VIR_ERR_DOM_EXIST
246     | VIR_ERR_OPERATION_DENIED
247     | VIR_ERR_OPEN_FAILED
248     | VIR_ERR_READ_FAILED
249     | VIR_ERR_PARSE_FAILED
250     | VIR_ERR_CONF_SYNTAX
251     | VIR_ERR_WRITE_FAILED
252     | VIR_ERR_XML_DETAIL
253     | VIR_ERR_INVALID_NETWORK
254     | VIR_ERR_NETWORK_EXIST
255     | VIR_ERR_SYSTEM_ERROR
256     | VIR_ERR_RPC
257     | VIR_ERR_GNUTLS_ERROR
258     | VIR_WAR_NO_NETWORK
259     | VIR_ERR_NO_DOMAIN
260     | VIR_ERR_NO_NETWORK
261
262   let string_of_code = function
263     | VIR_ERR_OK -> "VIR_ERR_OK"
264     | VIR_ERR_INTERNAL_ERROR -> "VIR_ERR_INTERNAL_ERROR"
265     | VIR_ERR_NO_MEMORY -> "VIR_ERR_NO_MEMORY"
266     | VIR_ERR_NO_SUPPORT -> "VIR_ERR_NO_SUPPORT"
267     | VIR_ERR_UNKNOWN_HOST -> "VIR_ERR_UNKNOWN_HOST"
268     | VIR_ERR_NO_CONNECT -> "VIR_ERR_NO_CONNECT"
269     | VIR_ERR_INVALID_CONN -> "VIR_ERR_INVALID_CONN"
270     | VIR_ERR_INVALID_DOMAIN -> "VIR_ERR_INVALID_DOMAIN"
271     | VIR_ERR_INVALID_ARG -> "VIR_ERR_INVALID_ARG"
272     | VIR_ERR_OPERATION_FAILED -> "VIR_ERR_OPERATION_FAILED"
273     | VIR_ERR_GET_FAILED -> "VIR_ERR_GET_FAILED"
274     | VIR_ERR_POST_FAILED -> "VIR_ERR_POST_FAILED"
275     | VIR_ERR_HTTP_ERROR -> "VIR_ERR_HTTP_ERROR"
276     | VIR_ERR_SEXPR_SERIAL -> "VIR_ERR_SEXPR_SERIAL"
277     | VIR_ERR_NO_XEN -> "VIR_ERR_NO_XEN"
278     | VIR_ERR_XEN_CALL -> "VIR_ERR_XEN_CALL"
279     | VIR_ERR_OS_TYPE -> "VIR_ERR_OS_TYPE"
280     | VIR_ERR_NO_KERNEL -> "VIR_ERR_NO_KERNEL"
281     | VIR_ERR_NO_ROOT -> "VIR_ERR_NO_ROOT"
282     | VIR_ERR_NO_SOURCE -> "VIR_ERR_NO_SOURCE"
283     | VIR_ERR_NO_TARGET -> "VIR_ERR_NO_TARGET"
284     | VIR_ERR_NO_NAME -> "VIR_ERR_NO_NAME"
285     | VIR_ERR_NO_OS -> "VIR_ERR_NO_OS"
286     | VIR_ERR_NO_DEVICE -> "VIR_ERR_NO_DEVICE"
287     | VIR_ERR_NO_XENSTORE -> "VIR_ERR_NO_XENSTORE"
288     | VIR_ERR_DRIVER_FULL -> "VIR_ERR_DRIVER_FULL"
289     | VIR_ERR_CALL_FAILED -> "VIR_ERR_CALL_FAILED"
290     | VIR_ERR_XML_ERROR -> "VIR_ERR_XML_ERROR"
291     | VIR_ERR_DOM_EXIST -> "VIR_ERR_DOM_EXIST"
292     | VIR_ERR_OPERATION_DENIED -> "VIR_ERR_OPERATION_DENIED"
293     | VIR_ERR_OPEN_FAILED -> "VIR_ERR_OPEN_FAILED"
294     | VIR_ERR_READ_FAILED -> "VIR_ERR_READ_FAILED"
295     | VIR_ERR_PARSE_FAILED -> "VIR_ERR_PARSE_FAILED"
296     | VIR_ERR_CONF_SYNTAX -> "VIR_ERR_CONF_SYNTAX"
297     | VIR_ERR_WRITE_FAILED -> "VIR_ERR_WRITE_FAILED"
298     | VIR_ERR_XML_DETAIL -> "VIR_ERR_XML_DETAIL"
299     | VIR_ERR_INVALID_NETWORK -> "VIR_ERR_INVALID_NETWORK"
300     | VIR_ERR_NETWORK_EXIST -> "VIR_ERR_NETWORK_EXIST"
301     | VIR_ERR_SYSTEM_ERROR -> "VIR_ERR_SYSTEM_ERROR"
302     | VIR_ERR_RPC -> "VIR_ERR_RPC"
303     | VIR_ERR_GNUTLS_ERROR -> "VIR_ERR_GNUTLS_ERROR"
304     | VIR_WAR_NO_NETWORK -> "VIR_WAR_NO_NETWORK"
305     | VIR_ERR_NO_DOMAIN -> "VIR_ERR_NO_DOMAIN"
306     | VIR_ERR_NO_NETWORK -> "VIR_ERR_NO_NETWORK"
307
308   type level =
309     | VIR_ERR_NONE
310     | VIR_ERR_WARNING
311     | VIR_ERR_ERROR
312
313   let string_of_level = function
314     | VIR_ERR_NONE -> "VIR_ERR_NONE"
315     | VIR_ERR_WARNING -> "VIR_ERR_WARNING"
316     | VIR_ERR_ERROR -> "VIR_ERR_ERROR"
317
318   type domain =
319     | VIR_FROM_NONE
320     | VIR_FROM_XEN
321     | VIR_FROM_XEND
322     | VIR_FROM_XENSTORE
323     | VIR_FROM_SEXPR
324     | VIR_FROM_XML
325     | VIR_FROM_DOM
326     | VIR_FROM_RPC
327     | VIR_FROM_PROXY
328     | VIR_FROM_CONF
329     | VIR_FROM_QEMU
330     | VIR_FROM_NET
331     | VIR_FROM_TEST
332     | VIR_FROM_REMOTE
333
334   let string_of_domain = function
335     | VIR_FROM_NONE -> "VIR_FROM_NONE"
336     | VIR_FROM_XEN -> "VIR_FROM_XEN"
337     | VIR_FROM_XEND -> "VIR_FROM_XEND"
338     | VIR_FROM_XENSTORE -> "VIR_FROM_XENSTORE"
339     | VIR_FROM_SEXPR -> "VIR_FROM_SEXPR"
340     | VIR_FROM_XML -> "VIR_FROM_XML"
341     | VIR_FROM_DOM -> "VIR_FROM_DOM"
342     | VIR_FROM_RPC -> "VIR_FROM_RPC"
343     | VIR_FROM_PROXY -> "VIR_FROM_PROXY"
344     | VIR_FROM_CONF -> "VIR_FROM_CONF"
345     | VIR_FROM_QEMU -> "VIR_FROM_QEMU"
346     | VIR_FROM_NET -> "VIR_FROM_NET"
347     | VIR_FROM_TEST -> "VIR_FROM_TEST"
348     | VIR_FROM_REMOTE -> "VIR_FROM_REMOTE"
349
350   type t = {
351     code : code;
352     domain : domain;
353     message : string option;
354     level : level;
355     conn : ro Connect.t option;
356     dom : ro Domain.t option;
357     str1 : string option;
358     str2 : string option;
359     str3 : string option;
360     int1 : int32;
361     int2 : int32;
362     net : ro Network.t option;
363   }
364
365   let to_string { code = code; domain = domain; message = message } =
366     let buf = Buffer.create 128 in
367     Buffer.add_string buf "libvirt: ";
368     Buffer.add_string buf (string_of_code code);
369     Buffer.add_string buf ": ";
370     Buffer.add_string buf (string_of_domain domain);
371     Buffer.add_string buf ": ";
372     (match message with Some msg -> Buffer.add_string buf msg | None -> ());
373     Buffer.contents buf
374
375   external get_last_error : unit -> t option = "ocaml_libvirt_virterror_get_last_error"
376   external get_last_conn_error : [>`R] Connect.t -> t option = "ocaml_libvirt_virterror_get_last_conn_error"
377   external reset_last_error : unit -> unit = "ocaml_libvirt_virterror_reset_last_error"
378   external reset_last_conn_error : [>`R] Connect.t -> unit = "ocaml_libvirt_virterror_reset_last_conn_error"
379
380   let no_error () =
381     { code = VIR_ERR_OK; domain = VIR_FROM_NONE; message = None;
382       level = VIR_ERR_NONE; conn = None; dom = None;
383       str1 = None; str2 = None; str3 = None;
384       int1 = 0_l; int2 = 0_l; net = None }
385 end
386
387 exception Virterror of Virterror.t
388
389 (* Initialization. *)
390 external c_init : unit -> unit = "ocaml_libvirt_init"
391 let () =
392   Callback.register_exception
393     "ocaml_libvirt_virterror" (Virterror (Virterror.no_error ()));
394   c_init ()