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