2018-03-24 21:26:10 +00:00
|
|
|
(* (c) 2017, 2018 Hannes Mehnert, all rights reserved *)
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
open Astring
|
2018-04-01 21:13:11 +00:00
|
|
|
open Rresult.R.Infix
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
open Vmm_core
|
|
|
|
|
2020-03-30 16:42:50 +00:00
|
|
|
external sysctl_kinfo_proc : int -> Stats.rusage * Stats.kinfo_mem =
|
|
|
|
"vmmanage_sysctl_kinfo_proc"
|
2017-05-26 14:30:34 +00:00
|
|
|
external sysctl_ifcount : unit -> int = "vmmanage_sysctl_ifcount"
|
2018-10-23 22:03:36 +00:00
|
|
|
external sysctl_ifdata : int -> Stats.ifdata = "vmmanage_sysctl_ifdata"
|
2018-03-24 21:26:10 +00:00
|
|
|
|
|
|
|
type vmctx
|
|
|
|
|
|
|
|
external vmmapi_open : string -> vmctx = "vmmanage_vmmapi_open"
|
|
|
|
external vmmapi_close : vmctx -> unit = "vmmanage_vmmapi_close"
|
|
|
|
external vmmapi_statnames : vmctx -> string list = "vmmanage_vmmapi_statnames"
|
|
|
|
external vmmapi_stats : vmctx -> int64 list = "vmmanage_vmmapi_stats"
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-03-24 21:26:10 +00:00
|
|
|
let descr = ref []
|
|
|
|
|
2018-09-20 20:53:42 +00:00
|
|
|
type 'a t = {
|
2019-01-14 23:25:59 +00:00
|
|
|
pid_nic : ((vmctx, int) result * string * (string * int * string) list) IM.t ;
|
2018-09-28 20:44:38 +00:00
|
|
|
vmid_pid : int Vmm_trie.t ;
|
|
|
|
name_sockets : 'a Vmm_trie.t ;
|
2017-05-26 14:30:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 23:25:59 +00:00
|
|
|
let pp_strings pp strs = Fmt.(list ~sep:(unit ",@ ") string) pp strs
|
|
|
|
|
|
|
|
let pp_nics pp nets =
|
|
|
|
Fmt.(list ~sep:(unit ",@ ") (pair ~sep:(unit ": ") string string)) pp nets
|
2018-04-01 21:13:11 +00:00
|
|
|
|
2017-05-26 14:30:34 +00:00
|
|
|
let empty () =
|
2018-09-28 20:44:38 +00:00
|
|
|
{ pid_nic = IM.empty ; vmid_pid = Vmm_trie.empty ; name_sockets = Vmm_trie.empty }
|
2018-09-20 20:53:42 +00:00
|
|
|
|
2018-09-28 20:44:38 +00:00
|
|
|
let remove_entry t name =
|
|
|
|
let name_sockets = Vmm_trie.remove name t.name_sockets in
|
2018-09-20 20:53:42 +00:00
|
|
|
{ t with name_sockets }
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-03-24 21:26:10 +00:00
|
|
|
let rec wrap f arg =
|
2017-05-26 14:30:34 +00:00
|
|
|
try Some (f arg) with
|
2018-03-24 21:26:10 +00:00
|
|
|
| Unix.Unix_error (Unix.EINTR, _, _) -> wrap f arg
|
2018-04-01 21:13:11 +00:00
|
|
|
| e ->
|
|
|
|
Logs.err (fun m -> m "exception %s" (Printexc.to_string e)) ;
|
|
|
|
None
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-10-12 00:15:44 +00:00
|
|
|
let vmmapi = conn_metrics "vmmapi"
|
2019-10-10 20:26:36 +00:00
|
|
|
|
2019-01-14 23:39:59 +00:00
|
|
|
let remove_vmid t vmid =
|
|
|
|
Logs.info (fun m -> m "removing vmid %a" Vmm_core.Name.pp vmid) ;
|
|
|
|
match Vmm_trie.find vmid t.vmid_pid with
|
|
|
|
| None -> Logs.warn (fun m -> m "no pid found for %a" Vmm_core.Name.pp vmid) ; t
|
|
|
|
| Some pid ->
|
|
|
|
Logs.info (fun m -> m "removing pid %d" pid) ;
|
2019-01-20 19:29:51 +00:00
|
|
|
(match IM.find_opt pid t.pid_nic with
|
2019-10-10 20:26:36 +00:00
|
|
|
| Some (Ok vmctx, _, _) -> ignore (wrap vmmapi_close vmctx) ; vmmapi `Close
|
2019-01-20 19:29:51 +00:00
|
|
|
| _ -> ()) ;
|
2019-01-14 23:39:59 +00:00
|
|
|
let pid_nic = IM.remove pid t.pid_nic
|
|
|
|
and vmid_pid = Vmm_trie.remove vmid t.vmid_pid
|
|
|
|
in
|
|
|
|
{ t with pid_nic ; vmid_pid }
|
|
|
|
|
2018-04-26 21:55:03 +00:00
|
|
|
let fill_descr ctx =
|
|
|
|
match !descr with
|
|
|
|
| [] ->
|
|
|
|
begin match wrap vmmapi_statnames ctx with
|
|
|
|
| None ->
|
|
|
|
Logs.err (fun m -> m "vmmapi_statnames failed, shouldn't happen") ;
|
|
|
|
()
|
|
|
|
| Some d ->
|
2018-09-28 20:44:38 +00:00
|
|
|
Logs.debug (fun m -> m "descr are %a" pp_strings d) ;
|
2018-04-26 21:55:03 +00:00
|
|
|
descr := d
|
|
|
|
end
|
2018-09-28 20:44:38 +00:00
|
|
|
| ds -> Logs.debug (fun m -> m "%d descr are already present" (List.length ds))
|
2018-04-26 21:55:03 +00:00
|
|
|
|
2019-01-20 19:29:51 +00:00
|
|
|
let open_vmmapi ~retries name =
|
2018-04-26 21:55:03 +00:00
|
|
|
if retries = 0 then begin
|
2019-01-14 23:25:59 +00:00
|
|
|
Logs.debug (fun m -> m "(ignored 0) vmmapi_open failed for %s" name) ;
|
2018-04-26 21:55:03 +00:00
|
|
|
Error 0
|
|
|
|
end else
|
|
|
|
match wrap vmmapi_open name with
|
|
|
|
| None ->
|
|
|
|
let left = max 0 (pred retries) in
|
2019-01-14 23:25:59 +00:00
|
|
|
Logs.warn (fun m -> m "(ignored, %d attempts left) vmmapi_open failed for %s" left name) ;
|
2018-04-26 21:55:03 +00:00
|
|
|
Error left
|
|
|
|
| Some vmctx ->
|
2019-10-10 20:26:36 +00:00
|
|
|
vmmapi `Open;
|
2019-01-14 23:25:59 +00:00
|
|
|
Logs.info (fun m -> m "vmmapi_open succeeded for %s" name) ;
|
2018-04-26 21:55:03 +00:00
|
|
|
fill_descr vmctx ;
|
|
|
|
Ok vmctx
|
|
|
|
|
|
|
|
let try_open_vmmapi pid_nic =
|
2019-01-14 23:25:59 +00:00
|
|
|
IM.fold (fun pid (vmctx, vmmdev, nics) fresh ->
|
2018-04-26 21:55:03 +00:00
|
|
|
let vmctx =
|
|
|
|
match vmctx with
|
|
|
|
| Ok vmctx -> Ok vmctx
|
2019-01-14 23:25:59 +00:00
|
|
|
| Error retries -> open_vmmapi ~retries vmmdev
|
2018-04-26 21:55:03 +00:00
|
|
|
in
|
2019-01-14 23:25:59 +00:00
|
|
|
IM.add pid (vmctx, vmmdev, nics) fresh)
|
2018-04-26 21:55:03 +00:00
|
|
|
pid_nic IM.empty
|
|
|
|
|
2018-03-24 21:26:10 +00:00
|
|
|
let gather pid vmctx nics =
|
2020-03-30 16:42:50 +00:00
|
|
|
let ru, mem =
|
|
|
|
match wrap sysctl_kinfo_proc pid with
|
|
|
|
| None -> None, None
|
|
|
|
| Some (mem, ru) -> Some mem, Some ru
|
|
|
|
in
|
|
|
|
ru, mem,
|
2018-04-01 21:13:11 +00:00
|
|
|
(match vmctx with
|
2018-04-26 21:55:03 +00:00
|
|
|
| Error _ -> None
|
|
|
|
| Ok vmctx -> wrap vmmapi_stats vmctx),
|
2019-01-14 23:25:59 +00:00
|
|
|
List.fold_left (fun ifd (bridge, nic, nname) ->
|
2018-03-24 21:26:10 +00:00
|
|
|
match wrap sysctl_ifdata nic with
|
|
|
|
| None ->
|
|
|
|
Logs.warn (fun m -> m "failed to get ifdata for %s" nname) ;
|
|
|
|
ifd
|
2019-01-20 19:29:51 +00:00
|
|
|
| Some data -> { data with Stats.bridge }::ifd)
|
2018-09-20 20:53:42 +00:00
|
|
|
[] nics
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let tick t =
|
2018-04-26 21:55:03 +00:00
|
|
|
let pid_nic = try_open_vmmapi t.pid_nic in
|
2018-09-20 20:53:42 +00:00
|
|
|
let t' = { t with pid_nic } in
|
2019-01-14 23:39:59 +00:00
|
|
|
let outs, to_remove =
|
|
|
|
List.fold_left (fun (out, to_remove) (vmid, pid) ->
|
2018-09-28 20:44:38 +00:00
|
|
|
let listeners = Vmm_trie.collect vmid t'.name_sockets in
|
|
|
|
match listeners with
|
2019-01-27 21:33:15 +00:00
|
|
|
| [] -> Logs.debug (fun m -> m "nobody is listening") ; (out, to_remove)
|
2018-09-28 20:44:38 +00:00
|
|
|
| xs -> match IM.find_opt pid t.pid_nic with
|
2019-01-14 23:39:59 +00:00
|
|
|
| None ->
|
|
|
|
Logs.warn (fun m -> m "couldn't find nics of %d" pid) ;
|
|
|
|
out, to_remove
|
2019-01-14 23:25:59 +00:00
|
|
|
| Some (vmctx, _, nics) ->
|
2019-01-06 00:12:56 +00:00
|
|
|
let ru, mem, vmm, ifd = gather pid vmctx nics in
|
2018-09-20 20:53:42 +00:00
|
|
|
match ru with
|
2019-01-14 23:39:59 +00:00
|
|
|
| None ->
|
|
|
|
Logs.err (fun m -> m "failed to get rusage for %d" pid) ;
|
|
|
|
out, vmid :: to_remove
|
2018-09-20 20:53:42 +00:00
|
|
|
| Some ru' ->
|
|
|
|
let stats =
|
2018-10-23 19:53:44 +00:00
|
|
|
let vmm' = match vmm with None -> None | Some xs -> Some (List.combine !descr xs) in
|
2019-01-06 00:12:56 +00:00
|
|
|
ru', mem, vmm', ifd
|
2018-09-20 20:53:42 +00:00
|
|
|
in
|
2019-01-14 23:39:59 +00:00
|
|
|
let outs =
|
2019-11-11 20:49:51 +00:00
|
|
|
List.fold_left (fun out (id, (version, socket)) ->
|
2019-01-14 23:39:59 +00:00
|
|
|
match Vmm_core.Name.drop_super ~super:id ~sub:vmid with
|
|
|
|
| None -> Logs.err (fun m -> m "couldn't drop super %a from sub %a" Vmm_core.Name.pp id Vmm_core.Name.pp vmid) ; out
|
|
|
|
| Some real_id ->
|
2019-11-11 20:49:51 +00:00
|
|
|
let header = Vmm_commands.header ~version real_id in
|
2019-01-14 23:39:59 +00:00
|
|
|
((socket, id, (header, `Data (`Stats_data stats))) :: out))
|
|
|
|
out xs
|
|
|
|
in
|
|
|
|
outs, to_remove)
|
|
|
|
([], []) (Vmm_trie.all t'.vmid_pid)
|
2018-09-20 20:53:42 +00:00
|
|
|
in
|
2019-01-14 23:39:59 +00:00
|
|
|
let t'' = List.fold_left remove_vmid t' to_remove in
|
|
|
|
(t'', outs)
|
2018-03-24 21:26:10 +00:00
|
|
|
|
2019-01-14 23:25:59 +00:00
|
|
|
let add_pid t vmid vmmdev pid nics =
|
2018-04-01 21:13:11 +00:00
|
|
|
match wrap sysctl_ifcount () with
|
|
|
|
| None ->
|
2019-01-14 23:25:59 +00:00
|
|
|
Logs.err (fun m -> m "sysctl ifcount failed for %d %a" pid pp_nics nics) ;
|
2018-04-01 21:13:11 +00:00
|
|
|
Error (`Msg "sysctl ifcount failed")
|
|
|
|
| Some max_nic ->
|
2018-03-24 22:39:16 +00:00
|
|
|
let rec go cnt acc id =
|
|
|
|
if id > 0 && cnt > 0 then
|
|
|
|
match wrap sysctl_ifdata id with
|
2019-01-14 23:25:59 +00:00
|
|
|
| None -> go cnt acc (pred id)
|
|
|
|
| Some ifd ->
|
2019-01-27 21:31:04 +00:00
|
|
|
match List.find_opt (fun (_, tap) -> String.equal tap ifd.Stats.bridge) nics with
|
2019-01-14 23:25:59 +00:00
|
|
|
| Some (bridge, tap) -> go (pred cnt) ((bridge, id, tap) :: acc) (pred id)
|
|
|
|
| None -> go cnt acc (pred id)
|
2018-03-24 22:39:16 +00:00
|
|
|
else
|
|
|
|
List.rev acc
|
|
|
|
in
|
2018-04-01 21:13:11 +00:00
|
|
|
Ok (go (List.length nics) [] max_nic) >>= fun nic_ids ->
|
2019-01-20 19:29:51 +00:00
|
|
|
Logs.info (fun m -> m "adding %a %d %a" Name.pp vmid pid pp_nics nics) ;
|
|
|
|
let pid_nic = IM.add pid (Error 4, vmmdev, nic_ids) t.pid_nic
|
2018-09-28 20:44:38 +00:00
|
|
|
and vmid_pid, ret = Vmm_trie.insert vmid pid t.vmid_pid
|
2017-08-17 17:53:36 +00:00
|
|
|
in
|
2018-09-28 20:44:38 +00:00
|
|
|
assert (ret = None) ;
|
2018-04-25 11:15:53 +00:00
|
|
|
Ok { t with pid_nic ; vmid_pid }
|
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let handle t socket (hdr, wire) =
|
|
|
|
match wire with
|
|
|
|
| `Command (`Stats_cmd cmd) ->
|
|
|
|
begin
|
|
|
|
let id = hdr.Vmm_commands.name in
|
|
|
|
match cmd with
|
|
|
|
| `Stats_add (vmmdev, pid, taps) ->
|
|
|
|
add_pid t id vmmdev pid taps >>= fun t ->
|
|
|
|
Ok (t, None, "added")
|
|
|
|
| `Stats_remove ->
|
|
|
|
let t = remove_vmid t id in
|
|
|
|
Ok (t, None, "removed")
|
|
|
|
| `Stats_subscribe ->
|
|
|
|
let name_sockets, close =
|
|
|
|
Vmm_trie.insert id (hdr.Vmm_commands.version, socket) t.name_sockets
|
|
|
|
in
|
|
|
|
Ok ({ t with name_sockets }, close, "subscribed")
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
Logs.err (fun m -> m "unexpected wire %a" Vmm_commands.pp_wire (hdr, wire)) ;
|
|
|
|
Error (`Msg "unexpected command")
|