[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2 22/30] libxl: ocaml: event management



Signed-off-by: Rob Hoes <rob.hoes@xxxxxxxxxx>
---
 tools/ocaml/libs/xl/xenlight.ml.in   |   70 +++++++-
 tools/ocaml/libs/xl/xenlight.mli.in  |   48 +++++
 tools/ocaml/libs/xl/xenlight_stubs.c |  325 ++++++++++++++++++++++++++++++++++
 3 files changed, 442 insertions(+), 1 deletion(-)

diff --git a/tools/ocaml/libs/xl/xenlight.ml.in 
b/tools/ocaml/libs/xl/xenlight.ml.in
index fd5c4ce..06c9f52 100644
--- a/tools/ocaml/libs/xl/xenlight.ml.in
+++ b/tools/ocaml/libs/xl/xenlight.ml.in
@@ -56,6 +56,14 @@ external ctx_alloc: Xentoollog.handle -> ctx = 
"stub_libxl_ctx_alloc"
 
 external test_raise_exception: unit -> unit = "stub_raise_exception"
 
+type event =
+       | POLLIN (* There is data to read *)
+       | POLLPRI (* There is urgent data to read *)
+       | POLLOUT (* Writing now will not block *)
+       | POLLERR (* Error condition (revents only) *)
+       | POLLHUP (* Device has been disconnected (revents only) *)
+       | POLLNVAL (* Invalid request: fd not open (revents only). *)
+
 (* @@LIBXL_TYPES@@ *)
 
 external send_trigger : ctx -> domid -> trigger -> int -> unit = 
"stub_xl_send_trigger"
@@ -63,4 +71,64 @@ external send_sysrq : ctx -> domid -> char -> unit = 
"stub_xl_send_sysrq"
 external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 external xen_console_read : ctx -> string list = "stub_xl_xen_console_read"
 
-let _ = Callback.register_exception "Xenlight.Error" (Error(Fail, ""))
+module type EVENT_USERS =
+       sig
+               type osevent_user
+               type event_user
+               type async_user
+       end
+
+module Async = functor (S: EVENT_USERS) -> struct
+       type for_libxl
+       type event_hooks
+       type osevent_hooks
+
+       module OseventSet = Set.Make(struct type t = S.osevent_user;; let 
compare = Pervasives.compare end)
+       module EventSet = Set.Make(struct type t = S.event_user;; let compare = 
Pervasives.compare end)
+       module AsyncSet = Set.Make(struct type t = S.async_user;; let compare = 
Pervasives.compare end)
+
+       let osevent_users = ref OseventSet.empty
+       let event_users = ref EventSet.empty
+       let async_users = ref AsyncSet.empty
+       let async_callback_ref = ref None
+
+       external osevent_register_hooks' : ctx -> S.osevent_user -> 
osevent_hooks = "stub_libxl_osevent_register_hooks"
+       external osevent_occurred_fd : ctx -> for_libxl -> Unix.file_descr -> 
event list -> event list -> unit = "stub_libxl_osevent_occurred_fd"
+       external osevent_occurred_timeout : ctx -> for_libxl -> unit = 
"stub_libxl_osevent_occurred_timeout"
+
+       let osevent_register_hooks ctx ~user ~fd_register ~fd_modify 
~fd_deregister ~timeout_register ~timeout_modify =
+               Callback.register "libxl_fd_register" fd_register;
+               Callback.register "libxl_fd_modify" fd_modify;
+               Callback.register "libxl_fd_deregister" fd_deregister;
+               Callback.register "libxl_timeout_register" timeout_register;
+               Callback.register "libxl_timeout_modify" timeout_modify;
+               osevent_users := OseventSet.add user !osevent_users;
+               osevent_register_hooks' ctx user
+
+       let async f user =
+               async_users := AsyncSet.add user !async_users;
+               f ?async:(Some user) ()
+
+       let async_callback' result user =
+               async_users := AsyncSet.remove user !async_users;
+               match !async_callback_ref with
+               | None -> ()
+               | Some f -> f ~result ~user
+
+       let async_register_callback ~async_callback =
+               async_callback_ref := Some async_callback;
+               Callback.register "libxl_async_callback" async_callback'
+
+       external evenable_domain_death : ctx -> domid -> int -> unit = 
"stub_libxl_evenable_domain_death"
+       external event_register_callbacks' : ctx -> S.event_user -> event_hooks 
= "stub_libxl_event_register_callbacks"
+
+       let event_register_callbacks ctx ~user ~event_occurs_callback 
~event_disaster_callback =
+               Callback.register "libxl_event_occurs_callback" 
event_occurs_callback;
+               Callback.register "libxl_event_disaster_callback" 
event_disaster_callback;
+               event_users := EventSet.add user !event_users;
+               event_register_callbacks' ctx user
+end
+
+let _ =
+       Callback.register_exception "Xenlight.Error" (Error(Fail, ""))
+
diff --git a/tools/ocaml/libs/xl/xenlight.mli.in 
b/tools/ocaml/libs/xl/xenlight.mli.in
index 11ea43c..0b06712 100644
--- a/tools/ocaml/libs/xl/xenlight.mli.in
+++ b/tools/ocaml/libs/xl/xenlight.mli.in
@@ -41,9 +41,57 @@ external ctx_alloc: Xentoollog.handle -> ctx = 
"stub_libxl_ctx_alloc"
 
 external test_raise_exception: unit -> unit = "stub_raise_exception"
 
+type event =
+       | POLLIN (* There is data to read *)
+       | POLLPRI (* There is urgent data to read *)
+       | POLLOUT (* Writing now will not block *)
+       | POLLERR (* Error condition (revents only) *)
+       | POLLHUP (* Device has been disconnected (revents only) *)
+       | POLLNVAL (* Invalid request: fd not open (revents only). *)
+
 (* @@LIBXL_TYPES@@ *)
 
 external send_trigger : ctx -> domid -> trigger -> int -> unit = 
"stub_xl_send_trigger"
 external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 external xen_console_read : ctx -> string list = "stub_xl_xen_console_read"
+
+module type EVENT_USERS =
+       sig
+               type osevent_user
+               type event_user
+               type async_user
+       end
+
+module Async : functor (S: EVENT_USERS) -> sig
+       type for_libxl
+       type event_hooks
+       type osevent_hooks
+
+       val osevent_register_hooks : ctx ->
+               user:S.osevent_user ->
+               fd_register:(S.osevent_user -> Unix.file_descr -> event list -> 
for_libxl -> unit) ->
+               fd_modify:(S.osevent_user -> Unix.file_descr -> event list -> 
unit) ->
+               fd_deregister:(S.osevent_user -> Unix.file_descr -> unit) ->
+               timeout_register:(S.osevent_user -> int -> int -> for_libxl -> 
unit) ->
+               timeout_modify:(S.osevent_user -> unit) ->
+               osevent_hooks
+
+       external osevent_occurred_fd : ctx -> for_libxl -> Unix.file_descr -> 
event list -> event list -> unit = "stub_libxl_osevent_occurred_fd"
+       external osevent_occurred_timeout : ctx -> for_libxl -> unit = 
"stub_libxl_osevent_occurred_timeout"
+
+       val async : (?async:S.async_user -> unit -> 'a) -> S.async_user -> 'a
+
+       val async_register_callback :
+               async_callback:(result:error option -> user:S.async_user -> 
unit) ->
+               unit
+
+       external evenable_domain_death : ctx -> domid -> int -> unit = 
"stub_libxl_evenable_domain_death"
+
+       val event_register_callbacks : ctx ->
+               user:S.event_user ->
+               event_occurs_callback:(S.event_user -> Event.t -> unit) ->
+               event_disaster_callback:(S.event_user -> event_type -> string 
-> int -> unit) ->
+               event_hooks
+end
+
diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c 
b/tools/ocaml/libs/xl/xenlight_stubs.c
index 4e15edb..c98a660 100644
--- a/tools/ocaml/libs/xl/xenlight_stubs.c
+++ b/tools/ocaml/libs/xl/xenlight_stubs.c
@@ -30,6 +30,8 @@
 #include <libxl.h>
 #include <libxl_utils.h>
 
+#include <unistd.h>
+
 #include "caml_xentoollog.h"
 
 #define Ctx_val(x)(*((libxl_ctx **) Data_custom_val(x)))
@@ -394,6 +396,26 @@ static char *String_option_val(value v)
 
 #include "_libxl_types.inc"
 
+void async_callback(libxl_ctx *ctx, int rc, void *for_callback)
+{
+       CAMLparam0();
+       CAMLlocal1(error);
+       int *task = (int *) for_callback;
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_async_callback");
+       }
+
+       if (rc == 0)
+               error = Val_none;
+       else
+               error = Val_some(Val_error(rc));
+
+       caml_callback2(*func, error, (value) for_callback);
+}
+
 #define _STRINGIFY(x) #x
 #define STRINGIFY(x) _STRINGIFY(x)
 
@@ -625,6 +647,309 @@ value stub_xl_xen_console_read(value ctx)
        CAMLreturn(list);
 }
 
+
+/* Event handling */
+
+short Poll_val(value event)
+{
+       CAMLparam1(event);
+       short res = -1;
+
+       switch (Int_val(event)) {
+               case 0: res = POLLIN; break;
+               case 1: res = POLLPRI; break;
+               case 2: res = POLLOUT; break;
+               case 3: res = POLLERR; break;
+               case 4: res = POLLHUP; break;
+               case 5: res = POLLNVAL; break;
+       }
+
+       CAMLreturn(res);
+}
+
+short Poll_events_val(value event_list)
+{
+       CAMLparam1(event_list);
+       short events = 0;
+
+       while (event_list != Val_emptylist) {
+               events |= Poll_val(Field(event_list, 0));
+               event_list = Field(event_list, 1);
+       }
+
+       CAMLreturn(events);
+}
+
+value Val_poll(short event)
+{
+       CAMLparam0();
+       CAMLlocal1(res);
+
+       switch (event) {
+               case POLLIN: res = Val_int(0); break;
+               case POLLPRI: res = Val_int(1); break;
+               case POLLOUT: res = Val_int(2); break;
+               case POLLERR: res = Val_int(3); break;
+               case POLLHUP: res = Val_int(4); break;
+               case POLLNVAL: res = Val_int(5); break;
+               default: failwith_xl(ERROR_FAIL, "cannot convert poll event 
value"); break;
+       }
+
+       CAMLreturn(res);
+}
+
+value add_event(value event_list, short event)
+{
+       CAMLparam1(event_list);
+       CAMLlocal1(new_list);
+
+       new_list = caml_alloc(2, 0);
+       Store_field(new_list, 0, Val_poll(event));
+       Store_field(new_list, 1, event_list);
+
+       CAMLreturn(new_list);
+}
+
+value Val_poll_events(short events)
+{
+       CAMLparam0();
+       CAMLlocal1(event_list);
+
+       event_list = Val_emptylist;
+       if (events & POLLIN)
+               event_list = add_event(event_list, POLLIN);
+       if (events & POLLPRI)
+               event_list = add_event(event_list, POLLPRI);
+       if (events & POLLOUT)
+               event_list = add_event(event_list, POLLOUT);
+       if (events & POLLERR)
+               event_list = add_event(event_list, POLLERR);
+       if (events & POLLHUP)
+               event_list = add_event(event_list, POLLHUP);
+       if (events & POLLNVAL)
+               event_list = add_event(event_list, POLLNVAL);
+
+       CAMLreturn(event_list);
+}
+
+int fd_register(void *user, int fd, void **for_app_registration_out,
+                     short events, void *for_libxl)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 4);
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_fd_register");
+       }
+
+       args[0] = (value) user;
+       args[1] = Val_int(fd);
+       args[2] = Val_poll_events(events);
+       args[3] = (value) for_libxl;
+
+       caml_callbackN(*func, 4, args);
+       CAMLreturn(0);
+}
+
+int fd_modify(void *user, int fd, void **for_app_registration_update,
+                   short events)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 3);
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_fd_modify");
+       }
+
+       args[0] = (value) user;
+       args[1] = Val_int(fd);
+       args[2] = Val_poll_events(events);
+
+       caml_callbackN(*func, 3, args);
+       CAMLreturn(0);
+}
+
+void fd_deregister(void *user, int fd, void *for_app_registration)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 2);
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_fd_deregister");
+       }
+
+       args[0] = (value) user;
+       args[1] = Val_int(fd);
+
+       caml_callbackN(*func, 2, args);
+       CAMLreturn0;
+}
+
+int timeout_register(void *user, void **for_app_registration_out,
+                          struct timeval abs, void *for_libxl)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 4);
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_timeout_register");
+       }
+
+       args[0] = (value) user;
+       args[1] = Val_int(abs.tv_sec);
+       args[2] = Val_int(abs.tv_usec);
+       args[3] = (value) for_libxl;
+
+       caml_callbackN(*func, 4, args);
+       CAMLreturn(0);
+}
+
+int timeout_modify(void *user, void **for_app_registration_update,
+                         struct timeval abs)
+{
+       CAMLparam0();
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_timeout_modify");
+       }
+
+       caml_callback(*func, (value) user);
+       CAMLreturn(0);
+}
+
+void timeout_deregister(void *user, void *for_app_registration)
+{
+       failwith_xl(ERROR_FAIL, "timeout_deregister not yet implemented");
+       return;
+}
+
+value stub_libxl_osevent_register_hooks(value ctx, value user)
+{
+       CAMLparam2(ctx, user);
+       CAMLlocal1(result);
+       libxl_osevent_hooks *hooks;
+
+       hooks = malloc(sizeof(*hooks));
+       hooks->fd_register = fd_register;
+       hooks->fd_modify = fd_modify;
+       hooks->fd_deregister = fd_deregister;
+       hooks->timeout_register = timeout_register;
+       hooks->timeout_modify = timeout_modify;
+       hooks->timeout_deregister = timeout_deregister;
+
+       libxl_osevent_register_hooks(CTX, hooks, (void *) user);
+       result = caml_alloc(1, Abstract_tag);
+       *((libxl_osevent_hooks **) result) = hooks;
+
+       CAMLreturn(result);
+}
+
+value stub_libxl_osevent_occurred_fd(value ctx, value for_libxl, value fd,
+       value events, value revents)
+{
+       CAMLparam5(ctx, for_libxl, fd, events, revents);
+       libxl_osevent_occurred_fd(CTX, (void *) for_libxl, Int_val(fd),
+               Poll_events_val(events), Poll_events_val(revents));
+       CAMLreturn(Val_unit);
+}
+
+value stub_libxl_osevent_occurred_timeout(value ctx, value for_libxl)
+{
+       CAMLparam2(ctx, for_libxl);
+       libxl_osevent_occurred_timeout(CTX, (void *) for_libxl);
+       CAMLreturn(Val_unit);
+}
+
+struct user_with_ctx {
+       libxl_ctx *ctx;
+       void *user;
+};
+
+void event_occurs(void *user, libxl_event *event)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 2);
+       struct user_with_ctx *c_user = (struct user_with_ctx *) user;
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_event_occurs_callback");
+       }
+
+       args[0] = (value) c_user->user;
+       args[1] = Val_event(event);
+       libxl_event_free(c_user->ctx, event);
+
+       caml_callbackN(*func, 2, args);
+       CAMLreturn0;
+}
+
+void disaster(void *user, libxl_event_type type,
+                     const char *msg, int errnoval)
+{
+       CAMLparam0();
+       CAMLlocalN(args, 2);
+       struct user_with_ctx *c_user = (struct user_with_ctx *) user;
+       static value *func = NULL;
+
+       if (func == NULL) {
+               /* First time around, lookup by name */
+               func = caml_named_value("libxl_event_disaster_callback");
+       }
+
+       args[0] = (value) c_user->user;
+       args[1] = Val_event_type(type);
+       args[2] = caml_copy_string(msg);
+       args[3] = Val_int(errnoval);
+
+       caml_callbackN(*func, 4, args);
+       CAMLreturn0;
+}
+
+value stub_libxl_event_register_callbacks(value ctx, value user)
+{
+       CAMLparam2(ctx, user);
+       CAMLlocal1(result);
+       struct user_with_ctx *c_user = NULL;
+       libxl_event_hooks *hooks;
+
+       c_user = malloc(sizeof(*c_user));
+       c_user->user = (void *) user;
+       c_user->ctx = CTX;
+
+       hooks = malloc(sizeof(*hooks));
+       hooks->event_occurs_mask = LIBXL_EVENTMASK_ALL;
+       hooks->event_occurs = event_occurs;
+       hooks->disaster = disaster;
+
+       libxl_event_register_callbacks(CTX, hooks, (void *) c_user);
+       result = caml_alloc(1, Abstract_tag);
+       *((libxl_event_hooks **) result) = hooks;
+
+       CAMLreturn(result);
+}
+
+value stub_libxl_evenable_domain_death(value ctx, value domid, value user)
+{
+       CAMLparam3(ctx, domid, user);
+       libxl_evgen_domain_death *evgen_out;
+
+       libxl_evenable_domain_death(CTX, Int_val(domid), Int_val(user), 
&evgen_out);
+
+       CAMLreturn(Val_unit);
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: t
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.