2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
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-10-22 21:20:00 +00:00
|
|
|
let version = `AV2
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2018-10-22 21:20:00 +00:00
|
|
|
let state = ref (Vmm_engine.init version)
|
2018-10-12 23:05:21 +00:00
|
|
|
|
|
|
|
let create c_fd process cont =
|
|
|
|
Vmm_lwt.read_wire c_fd >>= function
|
2018-10-22 21:20:00 +00:00
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Logs.err (fun m -> m "error %s while reading from console" msg) ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while reading from console") ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| Ok (header, wire) ->
|
|
|
|
if not (Vmm_asn.version_eq version header.Vmm_asn.version) then begin
|
|
|
|
Logs.err (fun m -> m "invalid version while reading from console") ;
|
2018-10-12 23:05:21 +00:00
|
|
|
Lwt.return_unit
|
2018-10-22 21:20:00 +00:00
|
|
|
end else
|
|
|
|
match wire with
|
|
|
|
| `Command _ ->
|
|
|
|
Logs.err (fun m -> m "console returned a command") ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| `Failure f ->
|
|
|
|
Logs.err (fun m -> m "console failed with %s" f) ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| `Success _msg ->
|
|
|
|
(* assert hdr.id = id! *)
|
|
|
|
let await, wakeme = Lwt.wait () in
|
|
|
|
match cont !state await with
|
2018-10-12 23:05:21 +00:00
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Logs.err (fun m -> m "create continuation failed %s" msg) ;
|
|
|
|
Lwt.return_unit
|
2018-10-22 22:54:05 +00:00
|
|
|
| Ok (state'', out, name, vm) ->
|
2018-10-12 23:05:21 +00:00
|
|
|
state := state'' ;
|
|
|
|
s := { !s with vm_created = succ !s.vm_created } ;
|
|
|
|
Lwt.async (fun () ->
|
|
|
|
Vmm_lwt.wait_and_clear vm.Vmm_core.pid vm.Vmm_core.stdout >>= fun r ->
|
2018-10-22 22:54:05 +00:00
|
|
|
let state', out' = Vmm_engine.handle_shutdown !state name vm r in
|
2018-10-12 23:05:21 +00:00
|
|
|
s := { !s with vm_destroyed = succ !s.vm_destroyed } ;
|
|
|
|
state := state' ;
|
|
|
|
process out' >|= fun () ->
|
|
|
|
Lwt.wakeup wakeme ()) ;
|
|
|
|
process out >>= fun () ->
|
2018-10-22 22:54:05 +00:00
|
|
|
let state', out = Vmm_engine.setup_stats !state name vm in
|
2018-10-22 21:20:00 +00:00
|
|
|
state := state' ;
|
|
|
|
process out (* TODO: need to read from stats socket! *)
|
2018-10-12 23:05:21 +00:00
|
|
|
|
|
|
|
let handle out c_fd fd addr =
|
2018-09-09 18:52:04 +00:00
|
|
|
(* out is for `Log | `Stat | `Cons (including reconnect semantics) *)
|
|
|
|
(* need to handle data out (+ die on write failure) *)
|
|
|
|
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)
|
|
|
|
*)
|
|
|
|
let process xs =
|
|
|
|
Lwt_list.iter_p (function
|
2018-10-22 21:20:00 +00:00
|
|
|
| #Vmm_engine.service_out as o -> out o
|
2018-09-09 18:52:04 +00:00
|
|
|
| `Data cs ->
|
|
|
|
(* rather: terminate connection *)
|
|
|
|
Vmm_lwt.write_wire fd cs >|= fun _ -> ()) xs
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
2018-09-09 18:52:04 +00:00
|
|
|
Logs.debug (fun m -> m "now reading") ;
|
|
|
|
(Vmm_lwt.read_wire fd >>= function
|
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while reading") ;
|
|
|
|
Lwt.return_unit
|
2018-10-22 21:20:00 +00:00
|
|
|
| Ok wire ->
|
2018-09-09 18:52:04 +00:00
|
|
|
Logs.debug (fun m -> m "read sth") ;
|
2018-10-22 21:20:00 +00:00
|
|
|
let state', data, next = Vmm_engine.handle_command !state wire in
|
2018-09-09 18:52:04 +00:00
|
|
|
state := state' ;
|
|
|
|
process data >>= fun () ->
|
|
|
|
match next with
|
|
|
|
| `End -> Lwt.return_unit
|
|
|
|
| `Wait (task, out) -> task >>= fun () -> process out
|
2018-10-22 21:20:00 +00:00
|
|
|
| `Wait_and_create (task, next) ->
|
2018-10-12 23:05:21 +00:00
|
|
|
task >>= fun () ->
|
|
|
|
let state', data, n = next !state in
|
|
|
|
state := state' ;
|
|
|
|
process data >>= fun () ->
|
|
|
|
(match n with
|
|
|
|
| `End -> Lwt.return_unit
|
|
|
|
| `Create cont -> create c_fd process cont)
|
2018-09-09 18:52:04 +00:00
|
|
|
| `Create cont ->
|
2018-10-12 23:05:21 +00:00
|
|
|
create c_fd process cont
|
2018-09-09 18:52:04 +00:00
|
|
|
(* data contained a write to console, we need to wait for its reply first *)
|
2018-10-12 23:05:21 +00:00
|
|
|
) >>= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
Vmm_lwt.safe_close fd
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let init_sock sock =
|
|
|
|
let name = Vmm_core.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 () ->
|
2018-09-19 19:16:44 +00:00
|
|
|
Lwt_unix.(connect c (ADDR_UNIX name)) >|= fun () -> Some c)
|
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 create_mbox sock =
|
|
|
|
init_sock sock >|= function
|
2018-09-09 18:52:04 +00:00
|
|
|
| None -> None
|
|
|
|
| Some fd ->
|
|
|
|
let mvar = Lwt_mvar.create_empty () in
|
|
|
|
(* could be more elaborate:
|
|
|
|
if <log> fails, we can reconnect and spit our more log messages to the new socket
|
|
|
|
if <console> fails, all running VMs terminate, so we can terminate as well ;)
|
|
|
|
if <stat> fails, we'd need to retransmit all VM info to stat (or stat has to ask at connect) *)
|
|
|
|
let rec loop () =
|
|
|
|
Lwt_mvar.take mvar >>= fun data ->
|
|
|
|
Vmm_lwt.write_wire fd data >>= function
|
|
|
|
| Ok () -> loop ()
|
2018-09-19 19:16:44 +00:00
|
|
|
| Error `Exception -> invalid_arg ("exception while writing to " ^ Fmt.to_to_string Vmm_core.pp_socket sock) ;
|
2018-09-09 18:52:04 +00:00
|
|
|
in
|
|
|
|
Lwt.async loop ;
|
|
|
|
Some (mvar, fd)
|
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let server_socket sock =
|
|
|
|
let name = Vmm_core.socket_path sock in
|
|
|
|
(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-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 ()
|
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
(* TODO nobody reads stat and log file descriptors - that's likely a bad idea!
|
|
|
|
- create_mbox could after take & write do a read and check for failures! *)
|
2018-09-09 18:52:04 +00:00
|
|
|
let jump _ =
|
2017-05-26 14:30:34 +00:00
|
|
|
Sys.(set_signal sigpipe Signal_ignore) ;
|
|
|
|
Lwt_main.run
|
2018-09-19 19:16:44 +00:00
|
|
|
(server_socket `Vmmd >>= fun ss ->
|
|
|
|
(create_mbox `Console >|= function
|
2018-03-18 17:30:43 +00:00
|
|
|
| None -> invalid_arg "cannot connect to console socket"
|
2018-09-09 18:52:04 +00:00
|
|
|
| Some c -> c) >>= fun (c, c_fd) ->
|
2018-09-19 19:16:44 +00:00
|
|
|
create_mbox `Stats >>= fun s ->
|
|
|
|
(create_mbox `Log >|= function
|
2018-03-18 17:30:43 +00:00
|
|
|
| None -> invalid_arg "cannot connect to log socket"
|
2018-09-09 18:52:04 +00:00
|
|
|
| Some l -> l) >>= fun (l, _l_fd) ->
|
|
|
|
let out = function
|
|
|
|
| `Stat data -> (match s with None -> Lwt.return_unit | Some (s, _s_fd) -> Lwt_mvar.put s data)
|
|
|
|
| `Log data -> Lwt_mvar.put l data
|
|
|
|
| `Cons data -> Lwt_mvar.put c data
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
2018-03-22 13:09:57 +00:00
|
|
|
Lwt.async stats_loop ;
|
2017-05-26 14:30:34 +00:00
|
|
|
let rec loop () =
|
2018-09-09 18:52:04 +00:00
|
|
|
Lwt_unix.accept ss >>= fun (fd, addr) ->
|
|
|
|
Lwt_unix.set_close_on_exec fd ;
|
2018-10-12 23:05:21 +00:00
|
|
|
Lwt.async (fun () -> handle out c_fd fd addr) ;
|
2018-09-09 18:52:04 +00:00
|
|
|
loop ()
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
|
|
|
|
let setup_log style_renderer level =
|
|
|
|
Fmt_tty.setup_std_outputs ?style_renderer ();
|
|
|
|
Logs.set_level level;
|
|
|
|
Logs.set_reporter (Logs_fmt.reporter ~dst:Format.std_formatter ())
|
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
|
|
|
|
let setup_log =
|
|
|
|
Term.(const setup_log
|
|
|
|
$ Fmt_cli.style_renderer ()
|
|
|
|
$ Logs_cli.level ())
|
|
|
|
|
|
|
|
let cmd =
|
2018-09-09 18:52:04 +00:00
|
|
|
Term.(ret (const jump $ setup_log)),
|
2017-05-26 14:30:34 +00:00
|
|
|
Term.info "vmmd" ~version:"%%VERSION_NUM%%"
|
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|