2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_cli
|
2018-10-25 23:11:41 +00:00
|
|
|
|
2018-11-13 00:02:05 +00:00
|
|
|
open Vmm_core
|
|
|
|
|
2018-03-22 13:09:57 +00:00
|
|
|
type stats = {
|
|
|
|
start : Ptime.t ;
|
|
|
|
vm_created : int ;
|
|
|
|
vm_destroyed : int ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let s = ref { start = Ptime_clock.now () ; vm_created = 0 ; vm_destroyed = 0 }
|
|
|
|
|
|
|
|
let pp_stats ppf s =
|
|
|
|
let diff = Ptime.(diff (Ptime_clock.now ()) s.start) in
|
|
|
|
Fmt.pf ppf "up %a: %d vms created, %d vms destroyed, %d running"
|
|
|
|
Ptime.Span.pp diff
|
|
|
|
s.vm_created s.vm_destroyed (s.vm_created - s.vm_destroyed)
|
|
|
|
|
2017-05-26 14:30:34 +00:00
|
|
|
open Lwt.Infix
|
|
|
|
|
2018-11-12 21:11:06 +00:00
|
|
|
let version = `AV3
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2018-10-23 22:10:08 +00:00
|
|
|
let state = ref (Vmm_vmmd.init version)
|
2018-10-12 23:05:21 +00:00
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let create stat_out log_out cons_out data_out cons succ_cont fail_cont =
|
|
|
|
cons_out "create" cons >>= function
|
|
|
|
| Error () ->
|
|
|
|
let data = fail_cont () in
|
|
|
|
data_out data
|
|
|
|
| Ok () -> match succ_cont !state with
|
|
|
|
| Error (`Msg msg) ->
|
2019-01-27 15:46:49 +00:00
|
|
|
Logs.err (fun m -> m "create (exec) failed %s" msg) ;
|
|
|
|
let data = fail_cont () in
|
|
|
|
data_out data
|
2019-01-27 15:07:53 +00:00
|
|
|
| Ok (state', stat, log, data, name, vm) ->
|
|
|
|
state := state' ;
|
|
|
|
s := { !s with vm_created = succ !s.vm_created } ;
|
|
|
|
Lwt.async (fun () ->
|
|
|
|
Vmm_lwt.wait_and_clear vm.Unikernel.pid >>= fun r ->
|
|
|
|
let state', stat', log' = Vmm_vmmd.handle_shutdown !state name vm r in
|
|
|
|
state := state' ;
|
|
|
|
s := { !s with vm_destroyed = succ !s.vm_destroyed } ;
|
|
|
|
stat_out "handle shutdown stat" stat' >>= fun () ->
|
|
|
|
log_out "handle shutdown log" log' >|= fun () ->
|
|
|
|
let state', waiter_opt = Vmm_vmmd.waiter !state name in
|
|
|
|
state := state' ;
|
|
|
|
(match waiter_opt with
|
|
|
|
| None -> ()
|
|
|
|
| Some wakeme -> Lwt.wakeup wakeme ())) ;
|
|
|
|
stat_out "setting up stat" stat >>= fun () ->
|
|
|
|
log_out "setting up log" log >>= fun () ->
|
|
|
|
data_out data
|
2018-10-25 23:11:41 +00:00
|
|
|
|
2019-01-20 20:41:49 +00:00
|
|
|
let register who header =
|
|
|
|
match Vmm_vmmd.register !state who Lwt.task with
|
2019-01-27 15:07:53 +00:00
|
|
|
| None -> Error (header, `Failure "task already registered")
|
2019-01-20 20:41:49 +00:00
|
|
|
| Some (state', task) -> state := state' ; Ok task
|
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let handle log_out cons_out stat_out fd addr =
|
2018-09-09 18:52:04 +00:00
|
|
|
Logs.debug (fun m -> m "connection from %a" Vmm_lwt.pp_sockaddr addr) ;
|
|
|
|
(* now we need to read a packet and handle it
|
|
|
|
(1)
|
|
|
|
(a) easy for info (look up name/prefix in resources)
|
|
|
|
(b) destroy looks up vm in resources, executes kill (wait for pid will do the cleanup)
|
|
|
|
logs "destroy issued"
|
|
|
|
(c) create initiates the vm startup procedure:
|
|
|
|
write image file, create fifo, create tap(s), send fifo to console
|
|
|
|
-- Lwt effects happen (console) --
|
2018-09-21 20:31:04 +00:00
|
|
|
executes solo5-hvt + waiter, send stats pid and taps, inserts await into state, logs "created vm"
|
2018-09-09 18:52:04 +00:00
|
|
|
-- Lwt effects happen (stats, logs, wait_and_clear) --
|
|
|
|
(2) goto (1)
|
|
|
|
*)
|
2019-01-27 15:07:53 +00:00
|
|
|
let out wire =
|
|
|
|
(* TODO should we terminate the connection on write failure? *)
|
|
|
|
Vmm_lwt.write_wire fd wire >|= fun _ -> ()
|
|
|
|
in
|
|
|
|
|
2018-10-31 21:40:09 +00:00
|
|
|
let rec loop () =
|
|
|
|
Logs.debug (fun m -> m "now reading") ;
|
|
|
|
Vmm_lwt.read_wire fd >>= function
|
2018-09-09 18:52:04 +00:00
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while reading") ;
|
|
|
|
Lwt.return_unit
|
2018-10-22 21:20:00 +00:00
|
|
|
| Ok wire ->
|
2019-01-20 20:56:32 +00:00
|
|
|
Logs.debug (fun m -> m "read %a" Vmm_commands.pp_wire wire) ;
|
2019-01-27 15:07:53 +00:00
|
|
|
match Vmm_vmmd.handle_command !state wire with
|
|
|
|
| Error wire -> out wire
|
|
|
|
| Ok (state', next) ->
|
|
|
|
state := state' ;
|
|
|
|
match next with
|
|
|
|
| `Loop wire -> out wire >>= loop
|
|
|
|
| `End wire -> out wire
|
|
|
|
| `Create (cons, succ, fail) ->
|
|
|
|
create stat_out log_out cons_out out cons succ fail
|
|
|
|
| `Wait (who, data) ->
|
|
|
|
(match register who (fst wire) with
|
|
|
|
| Error data' -> out data'
|
|
|
|
| Ok task ->
|
|
|
|
task >>= fun () ->
|
|
|
|
out data)
|
|
|
|
| `Wait_and_create (who, next) ->
|
|
|
|
(match register who (fst wire) with
|
|
|
|
| Error data -> out data
|
|
|
|
| Ok task ->
|
|
|
|
task >>= fun () ->
|
|
|
|
match next !state with
|
|
|
|
| Error data -> out data
|
|
|
|
| Ok (state', `Create (cons, succ, fail)) ->
|
|
|
|
state := state' ;
|
|
|
|
create stat_out log_out cons_out out cons succ fail)
|
2018-10-31 21:40:09 +00:00
|
|
|
in
|
|
|
|
loop () >>= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
Vmm_lwt.safe_close fd
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-01-27 13:13:08 +00:00
|
|
|
let connect_client_socket sock =
|
2018-11-13 00:02:05 +00:00
|
|
|
let name = socket_path sock in
|
2018-03-18 17:30:43 +00:00
|
|
|
let c = Lwt_unix.(socket PF_UNIX SOCK_STREAM 0) in
|
|
|
|
Lwt_unix.set_close_on_exec c ;
|
|
|
|
Lwt.catch (fun () ->
|
2019-01-27 13:13:08 +00:00
|
|
|
Lwt_unix.(connect c (ADDR_UNIX name)) >|= fun () ->
|
|
|
|
Some (c, Lwt_mutex.create ()))
|
2018-03-18 17:30:43 +00:00
|
|
|
(fun e ->
|
2018-09-19 19:16:44 +00:00
|
|
|
Logs.warn (fun m -> m "error %s connecting to socket %s"
|
|
|
|
(Printexc.to_string e) name) ;
|
2018-03-18 17:30:43 +00:00
|
|
|
(Lwt.catch (fun () -> Lwt_unix.close c) (fun _ -> Lwt.return_unit)) >|= fun () ->
|
|
|
|
None)
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let server_socket sock =
|
2018-11-13 00:02:05 +00:00
|
|
|
let name = socket_path sock in
|
2018-09-19 19:16:44 +00:00
|
|
|
(Lwt_unix.file_exists name >>= function
|
|
|
|
| true -> Lwt_unix.unlink name
|
2018-09-09 18:52:04 +00:00
|
|
|
| false -> Lwt.return_unit) >>= fun () ->
|
|
|
|
let s = Lwt_unix.(socket PF_UNIX SOCK_STREAM 0) in
|
2018-10-28 19:49:42 +00:00
|
|
|
Lwt_unix.set_close_on_exec s ;
|
2018-09-19 19:16:44 +00:00
|
|
|
Lwt_unix.(bind s (ADDR_UNIX name)) >|= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
Lwt_unix.listen s 1 ;
|
|
|
|
s
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-03-22 13:09:57 +00:00
|
|
|
let rec stats_loop () =
|
|
|
|
Logs.info (fun m -> m "%a" pp_stats !s) ;
|
|
|
|
Lwt_unix.sleep 600. >>= fun () ->
|
|
|
|
stats_loop ()
|
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let write_reply name (fd, mut) txt (header, cmd) =
|
2019-01-27 15:11:40 +00:00
|
|
|
Logs.debug (fun m -> m "locking to write to %s" name) ;
|
2019-01-27 15:07:53 +00:00
|
|
|
Lwt_mutex.with_lock mut (fun () ->
|
|
|
|
Vmm_lwt.write_wire fd (header, cmd) >>= function
|
|
|
|
| Error `Exception -> invalid_arg ("exception during " ^ txt ^ " while writing to " ^ name)
|
2019-01-27 15:11:40 +00:00
|
|
|
| Ok () -> Vmm_lwt.read_wire fd) >|= fun r ->
|
|
|
|
Logs.debug (fun m -> m "unlocking, wrote and read %s" name) ;
|
|
|
|
match r with
|
2019-01-27 15:07:53 +00:00
|
|
|
| Ok (header', reply) ->
|
|
|
|
if not Vmm_commands.(version_eq header.version header'.version) then begin
|
|
|
|
Logs.err (fun m -> m "%s: wrong version (got %a, expected %a) in reply from %s"
|
|
|
|
txt
|
|
|
|
Vmm_commands.pp_version header'.Vmm_commands.version
|
|
|
|
Vmm_commands.pp_version header.Vmm_commands.version
|
|
|
|
name) ;
|
|
|
|
invalid_arg "bad version received"
|
|
|
|
end else if not Vmm_commands.(Int64.equal header.sequence header'.sequence) then begin
|
|
|
|
Logs.err (fun m -> m "%s: wrong id %Lu (expected %Lu) in reply from %s"
|
|
|
|
txt header'.Vmm_commands.sequence header.Vmm_commands.sequence name) ;
|
|
|
|
invalid_arg "wrong sequence number received"
|
|
|
|
end else begin
|
|
|
|
Logs.debug (fun m -> m "%s: received valid reply from %s %a"
|
|
|
|
txt name Vmm_commands.pp_wire (header', reply)) ;
|
|
|
|
match reply with
|
|
|
|
| `Success _ -> Ok ()
|
|
|
|
| `Failure msg ->
|
|
|
|
Logs.err (fun m -> m "%s: received failure %s from %s" txt msg name) ;
|
|
|
|
Error ()
|
|
|
|
| _ ->
|
|
|
|
Logs.err (fun m -> m "%s: unexpected data from %s" txt name) ;
|
|
|
|
invalid_arg "unexpected data"
|
|
|
|
end
|
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error in read from %s" name) ;
|
|
|
|
invalid_arg "communication failure"
|
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let jump _ =
|
2019-01-18 00:14:11 +00:00
|
|
|
Sys.(set_signal sigpipe Signal_ignore);
|
2019-01-20 21:17:59 +00:00
|
|
|
match Vmm_vmmd.restore_unikernels () with
|
|
|
|
| Error (`Msg msg) -> Logs.err (fun m -> m "bailing out: %s" msg)
|
|
|
|
| Ok old_unikernels ->
|
|
|
|
Lwt_main.run
|
|
|
|
(server_socket `Vmmd >>= fun ss ->
|
2019-01-27 13:13:08 +00:00
|
|
|
(connect_client_socket `Log >|= function
|
2019-01-20 21:17:59 +00:00
|
|
|
| None -> invalid_arg "cannot connect to log socket"
|
2019-01-27 15:07:53 +00:00
|
|
|
| Some l -> l) >>= fun l ->
|
2019-01-20 21:17:59 +00:00
|
|
|
let self_destruct_mutex = Lwt_mutex.create () in
|
|
|
|
let self_destruct () =
|
|
|
|
Lwt_mutex.with_lock self_destruct_mutex (fun () ->
|
|
|
|
(if Vmm_vmmd.killall !state then
|
|
|
|
(* not too happy about the sleep here, but cleaning up resources
|
|
|
|
is really important (fifos, vm images, tap devices) - which
|
|
|
|
is done asynchronously (in the task waitpid() on the pid) *)
|
|
|
|
Lwt_unix.sleep 1.
|
|
|
|
else
|
|
|
|
Lwt.return_unit) >>= fun () ->
|
|
|
|
Vmm_lwt.safe_close ss)
|
|
|
|
in
|
|
|
|
Sys.(set_signal sigterm (Signal_handle (fun _ -> Lwt.async self_destruct)));
|
2019-01-27 13:13:08 +00:00
|
|
|
(connect_client_socket `Console >|= function
|
2019-01-20 21:17:59 +00:00
|
|
|
| None -> invalid_arg "cannot connect to console socket"
|
2019-01-27 15:07:53 +00:00
|
|
|
| Some c -> c) >>= fun c ->
|
2019-01-27 13:13:08 +00:00
|
|
|
connect_client_socket `Stats >>= fun s ->
|
2019-01-20 21:17:59 +00:00
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let log_out txt wire = write_reply "log" l txt wire >|= fun _ -> ()
|
|
|
|
and cons_out = write_reply "cons" c
|
|
|
|
and stat_out txt wire = match s with
|
|
|
|
| None -> Logs.info (fun m -> m "ignoring stat %s %a" txt Vmm_commands.pp_wire wire) ; Lwt.return_unit
|
|
|
|
| Some s -> write_reply "stat" s txt wire >|= fun _ -> ()
|
2019-01-20 21:17:59 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
Lwt.async stats_loop ;
|
|
|
|
|
|
|
|
let start_unikernel (name, config) =
|
2019-01-27 15:07:53 +00:00
|
|
|
let hdr = Vmm_commands.{ version ; sequence = 0L ; name = Name.root }
|
|
|
|
and data_out _ = Lwt.return_unit
|
|
|
|
in
|
|
|
|
match Vmm_vmmd.handle_create !state hdr name config with
|
2019-01-20 21:17:59 +00:00
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Logs.err (fun m -> m "failed to restart %a: %s" Name.pp name msg) ;
|
|
|
|
Lwt.return_unit
|
2019-01-27 15:07:53 +00:00
|
|
|
| Ok (state', `Create (cons, succ, fail)) ->
|
2019-01-20 21:17:59 +00:00
|
|
|
state := state' ;
|
2019-01-27 15:07:53 +00:00
|
|
|
create stat_out log_out cons_out data_out cons succ fail
|
2019-01-20 21:17:59 +00:00
|
|
|
in
|
|
|
|
Lwt_list.iter_p start_unikernel (Vmm_trie.all old_unikernels) >>= fun () ->
|
|
|
|
|
|
|
|
Lwt.catch (fun () ->
|
|
|
|
let rec loop () =
|
|
|
|
Lwt_unix.accept ss >>= fun (fd, addr) ->
|
|
|
|
Lwt_unix.set_close_on_exec fd ;
|
2019-01-27 15:07:53 +00:00
|
|
|
Lwt.async (fun () -> handle log_out cons_out stat_out fd addr) ;
|
2019-01-20 21:17:59 +00:00
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
(fun e ->
|
|
|
|
Logs.err (fun m -> m "exception %s, shutting down" (Printexc.to_string e));
|
|
|
|
self_destruct ()))
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
|
|
|
|
let cmd =
|
2019-01-18 00:14:11 +00:00
|
|
|
Term.(const jump $ setup_log),
|
2019-03-27 23:11:43 +00:00
|
|
|
Term.info "albatrossd" ~version:"%%VERSION_NUM%%"
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|