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