From 511da9febcf93cda202bb6748ccd7f843091d097 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Fri, 4 Oct 2024 23:25:28 +0200 Subject: [PATCH 1/3] Vmm_core.Policy.usable: check if block is present, it is >= 0 --- src/vmm_core.ml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/vmm_core.ml b/src/vmm_core.ml index 380a998..d2f0861 100644 --- a/src/vmm_core.ml +++ b/src/vmm_core.ml @@ -247,15 +247,17 @@ module Policy = struct bridges Fmt.(list ~sep:(any ", ") string) (String_set.elements res.bridges) - let usable { vms ; cpuids ; memory ; _ } = + let usable { vms ; cpuids ; memory ; block ; _ } = if vms <= 0 then Error (`Msg "Unusable policy with no VMs") else if IS.is_empty cpuids then Error (`Msg "Unusable policy with no CPUids") else if memory <= 16 then - Error (`Msg "Unusable policy with memory <= 16 MB") - else - Ok () + Error (`Msg ("Unusable policy with memory " ^ string_of_int memory ^ " MB <= 16 MB")) + else match block with + | None -> Ok () + | Some x when x >= 0 -> Ok () + | Some x -> Error (`Msg ("Unusable policy with block " ^ string_of_int x ^ " MB < 0 MB")) let is_smaller ~super ~sub = let sub_block sub super = From 582f22f6eb826580152da1fa14b8b3e3863963ad Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Sat, 5 Oct 2024 00:04:20 +0200 Subject: [PATCH 2/3] the big rename, what used to be "vm" as identifier or log output or error, is now unikernel fixes #191 --- client/albatross_client.ml | 56 +++++++++---------- daemon/albatross_influx.ml | 38 ++++++------- daemon/albatrossd.ml | 20 +++---- src/vmm_asn.ml | 74 ++++++++++++------------ src/vmm_commands.ml | 6 +- src/vmm_core.ml | 62 ++++++++++---------- src/vmm_core.mli | 2 +- src/vmm_resources.ml | 101 ++++++++++++++++----------------- src/vmm_resources.mli | 28 +++++----- src/vmm_unix.ml | 26 ++++----- src/vmm_unix.mli | 2 +- src/vmm_vmmd.ml | 112 ++++++++++++++++++------------------- test/tests.ml | 74 ++++++++++++------------ 13 files changed, 302 insertions(+), 299 deletions(-) diff --git a/client/albatross_client.ml b/client/albatross_client.ml index 409e779..5820377 100644 --- a/client/albatross_client.ml +++ b/client/albatross_client.ml @@ -61,11 +61,11 @@ let output_result ((hdr, reply) as wire) = | `Unikernel_image (compressed, image) -> let name = hdr.Vmm_commands.name in write_to_file name compressed image - | `Old_unikernels vms -> + | `Old_unikernels unikernels -> List.iter (fun (name, cfg) -> if String.length cfg.Vmm_core.Unikernel.image > 0 then write_to_file name cfg.compressed cfg.image) - vms + unikernels | `Block_device_image (compressed, image) -> let name = hdr.Vmm_commands.name in write_to_file name compressed image @@ -175,7 +175,7 @@ let prepare_update ~happy_eyeballs level host dryrun = function Lwt.return (Error Communication_failed) | Error _ -> Lwt.return (Error Communication_failed) -let create_vm force image cpuid memory argv block_devices bridges compression restart_on_fail exit_codes = +let create_unikernel force image cpuid memory argv block_devices bridges compression restart_on_fail exit_codes = let ( let* ) = Result.bind in let* () = if Vmm_core.String_set.(cardinal (of_list (List.map (fun (n, _, _) -> n) bridges))) = List.length bridges then @@ -222,7 +222,7 @@ let create_block size compression data = else Error (`Msg "data exceeds size") -let policy vms memory cpus block bridgesl = +let policy unikernels memory cpus block bridgesl = let bridges = Vmm_core.String_set.of_list bridgesl and cpuids = Vmm_core.IS.of_list cpus in @@ -230,7 +230,7 @@ let policy vms memory cpus block bridgesl = Logs.warn (fun m -> m "Bridges is not a set"); if not (Vmm_core.IS.cardinal cpuids = List.length cpus) then Logs.warn (fun m -> m "CPUids is not a set"); - Vmm_core.Policy.{ vms ; cpuids ; memory ; block ; bridges } + Vmm_core.Policy.{ unikernels ; cpuids ; memory ; block ; bridges } let to_exit_code = function | Error `Eof -> Error Success @@ -668,8 +668,8 @@ let info_policy () path = let remove_policy () path = jump (`Policy_cmd `Policy_remove) (Vmm_core.Name.create_of_path path) -let add_policy () vms memory cpus block bridges path d cert key ca key_type tmpdir = - let p = policy vms memory cpus block bridges in +let add_policy () unikernels memory cpus block bridges path d cert key ca key_type tmpdir = + let p = policy unikernels memory cpus block bridges in match Vmm_core.Policy.usable p with | Error `Msg msg -> Logs.err (fun m -> m "%s" msg); @@ -689,7 +689,7 @@ let destroy () = jump (`Unikernel_cmd `Unikernel_destroy) let create () force image cpuid memory argv block network compression restart_on_fail exit_code name d cert key ca key_type tmpdir = - match create_vm force image cpuid memory argv block network (compress_default compression d) restart_on_fail exit_code with + match create_unikernel force image cpuid memory argv block network (compress_default compression d) restart_on_fail exit_code with | Ok cmd -> jump (`Unikernel_cmd cmd) name d cert key ca key_type tmpdir | Error _ as e -> e @@ -972,7 +972,7 @@ let cpus = let doc = "CPUids to allow for this policy (argument may be repeated)." in Arg.(value & opt_all int [] & info [ "cpu" ] ~doc) -let vms = +let unikernels = let doc = "Number of unikernels to allow running at the same time." in Arg.(required & pos 1 (some int) None & info [] ~doc ~docv:"UNIKERNELS") @@ -1016,7 +1016,7 @@ let cpu = let doc = "CPUid to use." in Arg.(value & opt int 0 & info [ "cpu" ] ~doc) -let vm_mem = +let unikernel_mem = let doc = "Memory to assign (in MB)." in Arg.(value & opt int 32 & info [ "mem" ] ~doc) @@ -1127,23 +1127,23 @@ let path = let doc = "Path to unikernels." in Arg.(required & pos 0 (some path_c) None & info [] ~doc ~docv:"PATH") -let vm_c = Arg.conv (Name.of_string, Name.pp) +let unikernel_c = Arg.conv (Name.of_string, Name.pp) -let opt_vm_name = +let opt_unikernel_name = let doc = "Name of unikernel." in - Arg.(value & opt vm_c Name.root & info [ "n" ; "name"] ~doc) + Arg.(value & opt unikernel_c Name.root & info [ "n" ; "name"] ~doc) -let vm_name = +let unikernel_name = let doc = "Name of unikernel." in - Arg.(required & pos 0 (some vm_c) None & info [] ~doc ~docv:"UNIKERNEL-NAME") + Arg.(required & pos 0 (some unikernel_c) None & info [] ~doc ~docv:"UNIKERNEL-NAME") let block_name = let doc = "Name of block device." in - Arg.(required & pos 0 (some vm_c) None & info [] ~doc ~docv:"BLOCK-NAME") + Arg.(required & pos 0 (some unikernel_c) None & info [] ~doc ~docv:"BLOCK-NAME") let opt_block_name = let doc = "Name of block device." in - Arg.(value & opt vm_c Name.root & info [ "name" ] ~doc) + Arg.(value & opt unikernel_c Name.root & info [ "name" ] ~doc) let remote_host default_port = let parse s = @@ -1225,7 +1225,7 @@ let destroy_cmd = `P "Destroy a unikernel."] in let term = - Term.(term_result (const destroy $ (Albatross_cli.setup_log (const false)) $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const destroy $ (Albatross_cli.setup_log (const false)) $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "destroy" ~doc ~man ~exits in Cmd.v info term @@ -1237,7 +1237,7 @@ let restart_cmd = `P "Restarts a unikernel."] in let term = - Term.(term_result (const restart $ (Albatross_cli.setup_log (const false)) $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const restart $ (Albatross_cli.setup_log (const false)) $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "restart" ~doc ~man ~exits in Cmd.v info term @@ -1261,7 +1261,7 @@ let info_cmd = `P "Shows information about unikernels."] in let term = - Term.(term_result (const info_ $ (Albatross_cli.setup_log (const false)) $ opt_vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const info_ $ (Albatross_cli.setup_log (const false)) $ opt_unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "info" ~doc ~man ~exits in Cmd.v info term @@ -1273,7 +1273,7 @@ let get_cmd = `P "Downloads a unikernel image from albatross to disk."] in let term = - Term.(term_result (const get $ (Albatross_cli.setup_log (const false)) $ compress_level $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const get $ (Albatross_cli.setup_log (const false)) $ compress_level $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "get" ~doc ~man ~exits in Cmd.v info term @@ -1297,7 +1297,7 @@ let add_policy_cmd = `P "Adds a policy."] in let term = - Term.(term_result (const add_policy $ (Albatross_cli.setup_log (const false)) $ vms $ mem $ cpus $ opt_block_size $ bridge $ path $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const add_policy $ (Albatross_cli.setup_log (const false)) $ unikernels $ mem $ cpus $ opt_block_size $ bridge $ path $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "add-policy" ~doc ~man ~exits in Cmd.v info term @@ -1309,7 +1309,7 @@ let create_cmd = `P "Creates a unikernel."] in let term = - Term.(term_result (const create $ (Albatross_cli.setup_log (const false)) $ force $ image $ cpu $ vm_mem $ args $ block $ net $ compress_level $ restart_on_fail $ exit_code $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const create $ (Albatross_cli.setup_log (const false)) $ force $ image $ cpu $ unikernel_mem $ args $ block $ net $ compress_level $ restart_on_fail $ exit_code $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "create" ~doc ~man ~exits in Cmd.v info term @@ -1321,7 +1321,7 @@ let console_cmd = `P "Shows console output of a unikernel."] in let term = - Term.(term_result (const console $ (Albatross_cli.setup_log (const false)) $ since $ count $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const console $ (Albatross_cli.setup_log (const false)) $ since $ count $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "console" ~doc ~man ~exits in Cmd.v info term @@ -1333,7 +1333,7 @@ let stats_subscribe_cmd = `P "Shows statistics of unikernel."] in let term = - Term.(term_result (const stats_subscribe $ (Albatross_cli.setup_log (const false)) $ opt_vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const stats_subscribe $ (Albatross_cli.setup_log (const false)) $ opt_unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "stats" ~doc ~man ~exits in Cmd.v info term @@ -1345,7 +1345,7 @@ let stats_remove_cmd = `P "Removes statistics of unikernel."] in let term = - Term.(term_result (const stats_remove $ (Albatross_cli.setup_log (const false)) $ opt_vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const stats_remove $ (Albatross_cli.setup_log (const false)) $ opt_unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "stats-remove" ~doc ~man ~exits in Cmd.v info term @@ -1357,7 +1357,7 @@ let stats_add_cmd = `P "Add unikernel to statistics gathering."] in let term = - Term.(term_result (const stats_add $ (Albatross_cli.setup_log (const false)) $ vmm_dev $ pid_req0 $ bridge_taps $ opt_vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) + Term.(term_result (const stats_add $ (Albatross_cli.setup_log (const false)) $ vmm_dev $ pid_req0 $ bridge_taps $ opt_unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir)) and info = Cmd.info "stats-add" ~doc ~man ~exits in Cmd.v info term @@ -1429,7 +1429,7 @@ let update_cmd = `P "Check and update a unikernel from the binary repository."] in let term = - Term.(const update $ (Albatross_cli.setup_log (const false)) $ http_host $ dryrun $ compress_level $ vm_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir) + Term.(const update $ (Albatross_cli.setup_log (const false)) $ http_host $ dryrun $ compress_level $ unikernel_name $ dst $ ca_cert $ ca_key $ server_ca $ pub_key_type $ Albatross_cli.tmpdir) and info = Cmd.info "update" ~doc ~man ~exits in Cmd.v info term diff --git a/daemon/albatross_influx.ml b/daemon/albatross_influx.ml index 274be60..24a91f9 100644 --- a/daemon/albatross_influx.ml +++ b/daemon/albatross_influx.ml @@ -75,7 +75,7 @@ module P = struct let i64 i = Printf.sprintf "%Lui" i - let encode_ru vm ru = + let encode_ru unikernel ru = let fields = [ "utime", tv ru.utime ; "stime", tv ru.stime ; @@ -96,9 +96,9 @@ module P = struct ] in let fields = List.map (fun (k, v) -> k ^ "=" ^ v) fields in - Printf.sprintf "resource_usage,vm=%s %s" vm (String.concat "," fields) + Printf.sprintf "resource_usage,vm=%s %s" unikernel (String.concat "," fields) - let encode_kinfo_mem vm mem = + let encode_kinfo_mem unikernel mem = let now = Unix.gettimeofday () in let started = Int64.to_float (fst mem.start) +. (float_of_int (snd mem.start) /. 1_000_000.) @@ -117,9 +117,9 @@ module P = struct ] in let fields = List.map (fun (k, v) -> k ^ "=" ^ v) fields in - Printf.sprintf "kinfo_mem,vm=%s %s" vm (String.concat "," fields) + Printf.sprintf "kinfo_mem,vm=%s %s" unikernel (String.concat "," fields) - let encode_vmm vm xs = + let encode_vmm unikernel xs = let escape s = let cutted = String.split_on_char ',' s in let cutted = String.concat "\\," cutted in @@ -128,13 +128,13 @@ module P = struct let cutted = String.split_on_char '=' cutted in String.concat "\\=" cutted in - Printf.sprintf "vmm,vm=%s %s" vm + Printf.sprintf "vmm,vm=%s %s" unikernel (String.concat "," (List.map (fun (k, v) -> (escape k) ^ "=" ^ (i64 v)) xs)) let i32 i = Printf.sprintf "%lui" i - let encode_if vm ifd = + let encode_if unikernel ifd = let fields = (* TODO: flags *) [ "send_queue_length", i32 ifd.send_length ; @@ -157,7 +157,7 @@ module P = struct in let fields = List.map (fun (k, v) -> k ^ "=" ^ v) fields in Printf.sprintf "interface,vm=%s,bridge=%s %s" - vm ifd.bridge (String.concat "," fields) + unikernel ifd.bridge (String.concat "," fields) end let command = ref 1L @@ -233,10 +233,10 @@ let rec read_sock_write_tcp drop c ?fd fam addr = Lwt.return (Some fd) >>= fun fd -> read_sock_write_tcp drop c ?fd fam addr -let query_sock vm c = - let header = Vmm_commands.header ~sequence:!command vm in +let query_sock unikernel c = + let header = Vmm_commands.header ~sequence:!command unikernel in command := Int64.succ !command ; - Logs.debug (fun m -> m "%Lu requesting %a via socket" !command Name.pp vm) ; + Logs.debug (fun m -> m "%Lu requesting %a via socket" !command Name.pp unikernel) ; Vmm_lwt.write_wire c (header, `Command (`Stats_cmd `Stats_subscribe)) let rec maybe_connect () = @@ -251,7 +251,7 @@ let rec maybe_connect () = Logs.debug (fun m -> m "connected"); Lwt.return c -let client influx vm drop = +let client influx unikernel drop = match influx with | None -> Lwt.return (Error (`Msg "influx host not provided")) | Some (ip, port) -> @@ -276,7 +276,7 @@ let client influx vm drop = let rec loop () = (* start a socket connection to vmm_stats *) maybe_connect () >>= fun c -> - query_sock vm c >>= function + query_sock unikernel c >>= function | Error e -> let err = Error (`Msg (Fmt.str "error %s while writing to stat socket" (str_of_e e))) @@ -288,10 +288,10 @@ let client influx vm drop = in loop () -let run_client _ influx vm drop tmpdir = +let run_client _ influx unikernel drop tmpdir = Sys.(set_signal sigpipe Signal_ignore) ; Albatross_cli.set_tmpdir tmpdir; - Lwt_main.run (client influx vm drop) + Lwt_main.run (client influx unikernel drop) open Cmdliner @@ -299,11 +299,11 @@ let drop_label = let doc = "Drop unikernel path" in Arg.(value & flag & info [ "drop-label" ] ~doc) -let vm_c = Arg.conv (Name.of_string, Name.pp) +let unikernel_c = Arg.conv (Name.of_string, Name.pp) -let opt_vm_name = +let opt_unikernel_name = let doc = "name of unikernel." in - Arg.(value & opt vm_c Name.root & info [ "n" ; "name"] ~doc) + Arg.(value & opt unikernel_c Name.root & info [ "n" ; "name"] ~doc) let cmd = let doc = "Albatross Influx connector" in @@ -313,7 +313,7 @@ let cmd = statistics and pushes them via TCP to influxdb"; ] in let term = - Term.(term_result (const run_client $ (Albatross_cli.setup_log Albatrossd_utils.syslog) $ Albatrossd_utils.influx $ opt_vm_name $ drop_label $ Albatross_cli.tmpdir)) + Term.(term_result (const run_client $ (Albatross_cli.setup_log Albatrossd_utils.syslog) $ Albatrossd_utils.influx $ opt_unikernel_name $ drop_label $ Albatross_cli.tmpdir)) and info = Cmd.info "albatross-influx" ~version:Albatross_cli.version ~doc ~man in Cmd.v info term diff --git a/daemon/albatrossd.ml b/daemon/albatrossd.ml index 5390042..70d0f35 100644 --- a/daemon/albatrossd.ml +++ b/daemon/albatrossd.ml @@ -28,7 +28,7 @@ let rec create stat_out cons_out data_out name config = | Error (`Msg msg) -> Logs.err (fun m -> m "create (exec) failed %s" msg) ; Lwt.return (None, fail_cont ()) - | Ok (state', stat, data, name, vm) -> + | Ok (state', stat, data, name, unikernel) -> state := state'; (if Unikernel.restart_handler config then match Vmm_vmmd.register_restart !state name Lwt.task with @@ -42,18 +42,18 @@ let rec create stat_out cons_out data_out name config = state := state'; if may && should_restart config name r then create stat_out cons_out stub_data_out - name vm.Unikernel.config + name unikernel.Unikernel.config else Lwt.return_unit))); stat_out "setting up stat" stat >|= fun () -> - (Some vm, data)) >>= fun (started, data) -> + (Some unikernel, data)) >>= fun (started, data) -> (match started with | None -> () - | Some vm -> + | Some unikernel -> Lwt.async (fun () -> - Vmm_lwt.wait_and_clear vm.Unikernel.pid >>= fun r -> + Vmm_lwt.wait_and_clear unikernel.Unikernel.pid >>= fun r -> Lwt_mutex.with_lock create_lock (fun () -> - let state', stat' = Vmm_vmmd.handle_shutdown !state name vm r in + let state', stat' = Vmm_vmmd.handle_shutdown !state name unikernel r in state := state'; stat_out "handle shutdown stat" stat' >|= fun () -> let state', waiter_opt = Vmm_vmmd.waiter !state name in @@ -94,8 +94,8 @@ let handle cons_out stat_out fd addr = Lwt_mutex.unlock create_lock; out wire >|= fun () -> `Close - | `Create (id, vm) -> - create stat_out cons_out out id vm >|= fun () -> + | `Create (id, unikernel) -> + create stat_out cons_out out id unikernel >|= fun () -> Lwt_mutex.unlock create_lock; `Close | `Wait (who, data) -> @@ -105,14 +105,14 @@ let handle cons_out stat_out fd addr = task >>= fun r -> out (data r) >|= fun () -> `Close - | `Wait_and_create (who, (id, vm)) -> + | `Wait_and_create (who, (id, unikernel)) -> let state', task = Vmm_vmmd.register !state who Lwt.task in state := state'; Lwt_mutex.unlock create_lock; task >>= fun r -> Logs.info (fun m -> m "wait returned %a" pp_process_exit r); Lwt_mutex.with_lock create_lock (fun () -> - create stat_out cons_out out id vm) >|= fun () -> + create stat_out cons_out out id unikernel) >|= fun () -> `Close | `Replace_stats (wire, datas) -> (Option.fold diff --git a/src/vmm_asn.ml b/src/vmm_asn.ml index 4e26e70..618a628 100644 --- a/src/vmm_asn.ml +++ b/src/vmm_asn.ml @@ -113,14 +113,14 @@ let projections_of asn = (decode_strict c, Asn.encode c) let policy = - let f (cpuids, vms, memory, block, bridges) = + let f (cpuids, unikernels, memory, block, bridges) = let bridges = String_set.of_list bridges and cpuids = IS.of_list cpuids in - Policy.{ vms ; cpuids ; memory ; block ; bridges } + Policy.{ unikernels ; cpuids ; memory ; block ; bridges } and g policy = (IS.elements policy.Policy.cpuids, - policy.Policy.vms, + policy.Policy.unikernels, policy.Policy.memory, policy.Policy.block, String_set.elements policy.Policy.bridges) @@ -128,7 +128,7 @@ let policy = Asn.S.map f g @@ Asn.S.(sequence5 (required ~label:"cpuids" Asn.S.(sequence_of int)) - (required ~label:"vms" int) + (required ~label:"unikernels" int) (required ~label:"memory" int) (optional ~label:"block" int) (required ~label:"bridges" Asn.S.(sequence_of utf8_string))) @@ -376,7 +376,7 @@ let v0_unikernel_config = and fail_behaviour = `Quit (* TODO maybe set to restart by default :) *) in { typ ; compressed ; image ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv } - and g _vm = failwith "cannot encode v0 unikernel configs" + and g _unikernel = failwith "cannot encode v0 unikernel configs" in Asn.S.map f g @@ Asn.S.(sequence6 @@ -396,7 +396,7 @@ let v1_unikernel_config = and block_devices = match blocks with None -> [] | Some xs -> List.map (fun b -> b, None, None) xs in { typ ; compressed ; image ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv } - and g _vm = failwith "cannot encode v1 unikernel configs" + and g _unikernel = failwith "cannot encode v1 unikernel configs" in Asn.S.(map f g @@ sequence @@ (required ~label:"typ" typ) @@ -416,16 +416,16 @@ let v2_unikernel_config = and block_devices = match blocks with None -> [] | Some xs -> List.map (fun b -> b, None, None) xs in { typ ; compressed ; image ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv } - and g (vm : config) = + and g (unikernel : config) = let bridges = - match vm.bridges with + match unikernel.bridges with | [] -> None | xs -> Some (List.map (fun (a, b, _) -> a, b) xs) - and blocks = match vm.block_devices with + and blocks = match unikernel.block_devices with | [] -> None | xs -> Some (List.map (fun (a, _, _) -> a) xs) in - (vm.typ, (vm.compressed, (vm.image, (vm.fail_behaviour, (vm.cpuid, (vm.memory, (blocks, (bridges, vm.argv)))))))) + (unikernel.typ, (unikernel.compressed, (unikernel.image, (unikernel.fail_behaviour, (unikernel.cpuid, (unikernel.memory, (blocks, (bridges, unikernel.argv)))))))) in Asn.S.(map f g @@ sequence @@ (required ~label:"typ" typ) @@ -449,11 +449,11 @@ let unikernel_config = and block_devices = match blocks with None -> [] | Some xs -> xs in { typ ; compressed ; image ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv } - and g (vm : config) = - let bridges = match vm.bridges with [] -> None | xs -> Some xs - and blocks = match vm.block_devices with [] -> None | xs -> Some xs + and g (unikernel : config) = + let bridges = match unikernel.bridges with [] -> None | xs -> Some xs + and blocks = match unikernel.block_devices with [] -> None | xs -> Some xs in - (vm.typ, (vm.compressed, (vm.image, (vm.fail_behaviour, (vm.cpuid, (vm.memory, (blocks, (bridges, vm.argv)))))))) + (unikernel.typ, (unikernel.compressed, (unikernel.image, (unikernel.fail_behaviour, (unikernel.cpuid, (unikernel.memory, (blocks, (bridges, unikernel.argv)))))))) in Asn.S.(map f g @@ sequence @@ (required ~label:"typ" typ) @@ -479,23 +479,23 @@ let unikernel_config = let unikernel_cmd = let f = function | `C1 `C1 () -> `Old_unikernel_info1 - | `C1 `C2 vm -> `Unikernel_create vm - | `C1 `C3 vm -> `Unikernel_force_create vm + | `C1 `C2 unikernel -> `Unikernel_create unikernel + | `C1 `C3 unikernel -> `Unikernel_force_create unikernel | `C1 `C4 () -> `Unikernel_destroy - | `C1 `C5 vm -> `Unikernel_create vm - | `C1 `C6 vm -> `Unikernel_force_create vm + | `C1 `C5 unikernel -> `Unikernel_create unikernel + | `C1 `C6 unikernel -> `Unikernel_force_create unikernel | `C2 `C1 () -> `Old_unikernel_get | `C2 `C2 () -> `Old_unikernel_info2 | `C2 `C3 () -> `Unikernel_get 0 - | `C2 `C4 vm -> `Unikernel_create vm - | `C2 `C5 vm -> `Unikernel_force_create vm + | `C2 `C4 unikernel -> `Unikernel_create unikernel + | `C2 `C5 unikernel -> `Unikernel_force_create unikernel | `C2 `C6 level -> `Unikernel_get level | `C3 `C1 () -> `Unikernel_restart | `C3 `C2 () -> `Unikernel_info and g = function | `Old_unikernel_info1 -> `C1 (`C1 ()) - | `Unikernel_create vm -> `C2 (`C4 vm) - | `Unikernel_force_create vm -> `C2 (`C5 vm) + | `Unikernel_create unikernel -> `C2 (`C4 unikernel) + | `Unikernel_force_create unikernel -> `C2 (`C5 unikernel) | `Unikernel_destroy -> `C1 (`C4 ()) | `Old_unikernel_get -> `C2 (`C1 ()) | `Old_unikernel_info2 -> `C2 (`C2 ()) @@ -575,7 +575,7 @@ let wire_command = | `C1 console -> `Console_cmd console | `C2 stats -> `Stats_cmd stats | `C3 () -> Asn.S.parse_error "support for log dropped" - | `C4 vm -> `Unikernel_cmd vm + | `C4 unikernel -> `Unikernel_cmd unikernel | `C5 policy -> `Policy_cmd policy | `C6 block -> `Block_cmd block and g = function @@ -634,11 +634,11 @@ let old_unikernel_info = and started = Ptime.epoch in { typ ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv ; digest ; started } - and g (vm : info) = - let bridges = match vm.bridges with [] -> None | xs -> Some xs - and blocks = match vm.block_devices with [] -> None | xs -> Some xs + and g (unikernel : info) = + let bridges = match unikernel.bridges with [] -> None | xs -> Some xs + and blocks = match unikernel.block_devices with [] -> None | xs -> Some xs in - (vm.typ, (vm.fail_behaviour, (vm.cpuid, (vm.memory, (vm.digest, (blocks, (bridges, vm.argv))))))) + (unikernel.typ, (unikernel.fail_behaviour, (unikernel.cpuid, (unikernel.memory, (unikernel.digest, (blocks, (bridges, unikernel.argv))))))) in Asn.S.(map f g @@ sequence @@ (required ~label:"typ" typ) @@ -668,11 +668,11 @@ let unikernel_info = and started = Option.value ~default:Ptime.epoch started in { typ ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv ; digest ; started } - and g (vm : info) = - let bridges = match vm.bridges with [] -> None | xs -> Some xs - and blocks = match vm.block_devices with [] -> None | xs -> Some xs + and g (unikernel : info) = + let bridges = match unikernel.bridges with [] -> None | xs -> Some xs + and blocks = match unikernel.block_devices with [] -> None | xs -> Some xs in - (vm.typ, (vm.fail_behaviour, (vm.cpuid, (vm.memory, (vm.digest, (blocks, (bridges, (vm.argv, Some vm.started)))))))) + (unikernel.typ, (unikernel.fail_behaviour, (unikernel.cpuid, (unikernel.memory, (unikernel.digest, (blocks, (bridges, (unikernel.argv, Some unikernel.started)))))))) in Asn.S.(map f g @@ sequence @@ (required ~label:"typ" typ) @@ -710,22 +710,22 @@ let success name = | `C1 `C1 () -> `Empty | `C1 `C2 str -> `String str | `C1 `C3 policies -> `Policies policies - | `C1 `C4 vms -> `Old_unikernels vms + | `C1 `C4 unikernels -> `Old_unikernels unikernels | `C1 `C5 blocks -> `Block_devices blocks - | `C1 `C6 vms -> `Old_unikernel_info vms + | `C1 `C6 unikernels -> `Old_unikernel_info unikernels | `C2 `C1 (c, i) -> `Unikernel_image (c, i) | `C2 `C2 (compress, data) -> `Block_device_image (compress, data) - | `C2 `C3 vms -> `Unikernel_info vms + | `C2 `C3 unikernels -> `Unikernel_info unikernels and g = function | `Empty -> `C1 (`C1 ()) | `String s -> `C1 (`C2 s) | `Policies ps -> `C1 (`C3 ps) - | `Old_unikernels vms -> `C1 (`C4 vms) + | `Old_unikernels unikernels -> `C1 (`C4 unikernels) | `Block_devices blocks -> `C1 (`C5 blocks) - | `Old_unikernel_info vms -> `C1 (`C6 vms) + | `Old_unikernel_info unikernels -> `C1 (`C6 unikernels) | `Unikernel_image (c, i) -> `C2 (`C1 (c, i)) | `Block_device_image (compress, data) -> `C2 (`C2 (compress, data)) - | `Unikernel_info vms -> `C2 (`C3 vms) + | `Unikernel_info unikernels -> `C2 (`C3 unikernels) in Asn.S.map f g @@ Asn.S.(choice2 diff --git a/src/vmm_commands.ml b/src/vmm_commands.ml index 4f7bb39..b9bc441 100644 --- a/src/vmm_commands.ml +++ b/src/vmm_commands.ml @@ -74,7 +74,7 @@ let pp_unikernel_cmd ~verbose ppf = function (if verbose then Unikernel.pp_config_with_argv else Unikernel.pp_config) config | `Unikernel_force_create config -> - Fmt.pf ppf "vm force create %a" + Fmt.pf ppf "unikernel force create %a" (if verbose then Unikernel.pp_config_with_argv else Unikernel.pp_config) config | `Unikernel_restart -> Fmt.string ppf "unikernel restart" @@ -176,11 +176,11 @@ let pp_success ~verbose ppf = function | `String data -> Fmt.pf ppf "success: %s" data | `Policies ps -> my_fmt_list "no policies" Fmt.(pair ~sep:(any ": ") Name.pp Policy.pp) ppf ps - | `Old_unikernels vms -> + | `Old_unikernels unikernels -> my_fmt_list "no unikernels" Fmt.(pair ~sep:(any ": ") Name.pp (if verbose then Unikernel.pp_config_with_argv else Unikernel.pp_config)) - ppf vms + ppf unikernels | `Unikernel_info infos | `Old_unikernel_info infos -> my_fmt_list "no unikernels" Fmt.(pair ~sep:(any ": ") Name.pp diff --git a/src/vmm_core.ml b/src/vmm_core.ml index d2f0861..d0712f8 100644 --- a/src/vmm_core.ml +++ b/src/vmm_core.ml @@ -92,7 +92,7 @@ module Name = struct path_equal x y && opt_eq (snd x) (snd y) let pp ppf (p, name) = - Fmt.(pf ppf "[vm: %a:%a]" (list ~sep:(any ":") string) p + Fmt.(pf ppf "[unikernel: %a:%a]" (list ~sep:(any ":") string) p (option ~none:(any "") string) name) let path (p, _) = p @@ -201,7 +201,7 @@ module Name = struct let file = to_string name in Fpath.(!tmpdir / "fifo" / file) - let block_name vm_name dev = path vm_name, Some dev + let block_name unikernel_name dev = path unikernel_name, Some dev let mac name bridge = (* deterministic mac address computation: VEB Kombinat Robotron prefix @@ -218,7 +218,7 @@ module Policy = struct let eq_int (a : int) (b : int) = a = b type t = { - vms : int ; + unikernels : int ; cpuids : IS.t ; memory : int ; block : int option ; @@ -231,7 +231,7 @@ module Policy = struct | Some a, Some b -> eq_int a b | _ -> false in - eq_int p1.vms p2.vms && + eq_int p1.unikernels p2.unikernels && IS.equal p1.cpuids p2.cpuids && eq_int p1.memory p2.memory && eq_opt p1.block p2.block && @@ -241,15 +241,15 @@ module Policy = struct let bridges = if String_set.is_empty res.bridges then "" else ", bridges: " in - Fmt.pf ppf "policy: %d vms %a cpus %d MB memory %a block%s%a" - res.vms pp_is res.cpuids res.memory + Fmt.pf ppf "policy: %d unikernels %a cpus %d MB memory %a block%s%a" + res.unikernels pp_is res.cpuids res.memory Fmt.(option ~none:(any "no") (int ++ any " MB")) res.block bridges Fmt.(list ~sep:(any ", ") string) (String_set.elements res.bridges) - let usable { vms ; cpuids ; memory ; block ; _ } = - if vms <= 0 then - Error (`Msg "Unusable policy with no VMs") + let usable { unikernels ; cpuids ; memory ; block ; _ } = + if unikernels <= 0 then + Error (`Msg "Unusable policy with no unikernels") else if IS.is_empty cpuids then Error (`Msg "Unusable policy with no CPUids") else if memory <= 16 then @@ -267,9 +267,9 @@ module Policy = struct | Some x, Some y -> x >= y | None, Some _ -> false in - if super.vms < sub.vms then + if super.unikernels < sub.unikernels then Error (`Msg (Fmt.str "policy above allows %d unikernels, which is fewer than %d" - super.vms sub.vms)) + super.unikernels sub.unikernels)) else if super.memory < sub.memory then Error (`Msg (Fmt.str "policy above allows %d MB memory, which is fewer than %d MB" super.memory sub.memory)) @@ -316,10 +316,10 @@ module Unikernel = struct argv : string list option ; } - let bridges (vm : config) = + let bridges (unikernel : config) = List.map (fun (net, bri, _mac) -> match bri with None -> net | Some s -> s) - vm.bridges + unikernel.bridges let fine_with_policy (p : Policy.t) (c : config) = let bridge_allowed set s = String_set.mem s set in @@ -345,19 +345,19 @@ module Unikernel = struct Fmt.pf ppf "%s -> %s%a" name (Option.value ~default:name bridge) Fmt.(option ((any "@") ++ Macaddr.pp)) mac - let pp_config ppf (vm : config) = + let pp_config ppf (unikernel : config) = Fmt.pf ppf "typ %a@ compression %B image %d bytes@ fail behaviour %a@ cpu %d@ %d MB memory@ block devices %a@ bridge %a" - pp_typ vm.typ - vm.compressed - (String.length vm.image) - pp_fail_behaviour vm.fail_behaviour - vm.cpuid vm.memory - Fmt.(list ~sep:(any ", ") pp_block) vm.block_devices - Fmt.(list ~sep:(any ", ") pp_bridge) vm.bridges - - let pp_config_with_argv ppf (vm : config) = - Fmt.pf ppf "%a@ argv %a" pp_config vm - Fmt.(option ~none:(any "no") (list ~sep:(any " ") string)) vm.argv + pp_typ unikernel.typ + unikernel.compressed + (String.length unikernel.image) + pp_fail_behaviour unikernel.fail_behaviour + unikernel.cpuid unikernel.memory + Fmt.(list ~sep:(any ", ") pp_block) unikernel.block_devices + Fmt.(list ~sep:(any ", ") pp_bridge) unikernel.bridges + + let pp_config_with_argv ppf (unikernel : config) = + Fmt.pf ppf "%a@ argv %a" pp_config unikernel + Fmt.(option ~none:(any "no") (list ~sep:(any " ") string)) unikernel.argv let restart_handler config = match config.fail_behaviour with `Quit -> false | `Restart _ -> true @@ -371,13 +371,13 @@ module Unikernel = struct started : Ptime.t ; } - let pp ppf vm = - let hex_digest = Ohex.encode vm.digest in + let pp ppf unikernel = + let hex_digest = Ohex.encode unikernel.digest in Fmt.pf ppf "pid %d@ taps %a (block %a) cmdline %a digest %s" - vm.pid - Fmt.(list ~sep:(any ", ") string) vm.taps - Fmt.(list ~sep:(any ", ") pp_block) vm.config.block_devices - Fmt.(array ~sep:(any " ") string) vm.cmd + unikernel.pid + Fmt.(list ~sep:(any ", ") string) unikernel.taps + Fmt.(list ~sep:(any ", ") pp_block) unikernel.config.block_devices + Fmt.(array ~sep:(any " ") string) unikernel.cmd hex_digest type info = { diff --git a/src/vmm_core.mli b/src/vmm_core.mli index ab55ca4..4a0e18a 100644 --- a/src/vmm_core.mli +++ b/src/vmm_core.mli @@ -79,7 +79,7 @@ end module Policy : sig type t = { - vms : int; + unikernels : int; cpuids : IS.t; memory : int; block : int option; diff --git a/src/vmm_resources.ml b/src/vmm_resources.ml index 1b732e4..ff881da 100644 --- a/src/vmm_resources.ml +++ b/src/vmm_resources.ml @@ -18,8 +18,8 @@ let pp ppf t = (fun id (size, used) () -> Fmt.pf ppf "block device %a: %d MB (used %B)@." Name.pp id size used) () ; Vmm_trie.fold Name.root_path t.unikernels - (fun id vm () -> - Fmt.pf ppf "vm %a: %a@." Name.pp id Unikernel.pp_config vm.Unikernel.config) () + (fun id unikernel () -> + Fmt.pf ppf "unikernel %a: %a@." Name.pp id Unikernel.pp_config unikernel.Unikernel.config) () let empty = { policies = Vmm_trie.empty ; @@ -29,20 +29,20 @@ let empty = { let policy_metrics = let open Metrics in - let doc = "VMM resource policies" in + let doc = "Albatross resource policies" in let data policy = Data.v [ - uint "maximum unikernels" policy.Policy.vms ; + uint "maximum unikernels" policy.Policy.unikernels ; uint "maximum memory" policy.Policy.memory ; uint "maximum block" (match policy.Policy.block with None -> 0 | Some x -> x) ] in let tag = Tags.string "domain" in - Src.v ~doc ~tags:Tags.[tag] ~data "vmm-policies" + Src.v ~doc ~tags:Tags.[tag] ~data "albatross-policies" -let no_policy = Policy.{ vms = 0 ; cpuids = IS.empty ; memory = 0 ; block = None ; bridges = String_set.empty } +let no_policy = Policy.{ unikernels = 0 ; cpuids = IS.empty ; memory = 0 ; block = None ; bridges = String_set.empty } -(* we should confirm the following invariant: Vm or Block have no siblings *) +(* we should confirm the following invariant: Unikernel or Block have no siblings *) let block_usage t path = Vmm_trie.fold path t.block_devices @@ -54,30 +54,31 @@ let total_block_usage t path = let act, inact = block_usage t path in act + inact -let vm_usage t path = +let unikernel_usage t path = Vmm_trie.fold path t.unikernels - (fun _ vm (vms, memory) -> (succ vms, memory + vm.Unikernel.config.Unikernel.memory)) + (fun _ unikernel (unikernels, memory) -> + (succ unikernels, memory + unikernel.Unikernel.config.Unikernel.memory)) (0, 0) let unikernel_metrics = let open Metrics in - let doc = "VMM unikernels" in + let doc = "Albatross unikernels" in let data (t, path) = - let vms, memory = vm_usage t path + let unikernels, memory = unikernel_usage t path and act, inact = block_usage t path in Data.v [ uint "attached used block" act ; uint "unattached used block" inact ; uint "total used block" (act + inact) ; - uint "running unikernels" vms ; + uint "running unikernels" unikernels ; uint "used memory" memory ] in let tag = Tags.string "domain" in - Src.v ~doc ~tags:Tags.[tag] ~data "vmm-unikernels" + Src.v ~doc ~tags:Tags.[tag] ~data "albatross-unikernels" -let report_vms t name = +let report_unikernels t name = let rec doit path = let str = if Name.is_root_path path then ":" else Name.path_to_string path @@ -87,7 +88,7 @@ let report_vms t name = in doit (Name.path name) -let find_vm t name = Vmm_trie.find name t.unikernels +let find_unikernel t name = Vmm_trie.find name t.unikernels let find_policy t path = Vmm_trie.find (Vmm_core.Name.create_of_path path) t.policies @@ -102,8 +103,8 @@ let set_block_usage t name active = then invalid_arg ("block device " ^ Name.to_string name ^ " already in state " ^ (if curr then "active" else "inactive")) else fst (Vmm_trie.insert name (size, active) t) -let use_blocks t name vm active = - match vm.Unikernel.config.Unikernel.block_devices with +let use_blocks t name unikernel active = + match unikernel.Unikernel.config.Unikernel.block_devices with | [] -> t | blocks -> let block_names = @@ -114,13 +115,13 @@ let use_blocks t name vm active = in List.fold_left (fun t' n -> set_block_usage t' n active) t block_names -let remove_vm t name = match find_vm t name with - | None -> Error (`Msg "unknown vm") - | Some vm -> - let block_devices = use_blocks t.block_devices name vm false in +let remove_unikernel t name = match find_unikernel t name with + | None -> Error (`Msg "unknown unikernel") + | Some unikernel -> + let block_devices = use_blocks t.block_devices name unikernel false in let unikernels = Vmm_trie.remove name t.unikernels in let t' = { t with block_devices ; unikernels } in - report_vms t' name; + report_unikernels t' name; Ok t' let remove_policy t path = match find_policy t path with @@ -142,35 +143,35 @@ let remove_block t name = else let block_devices = Vmm_trie.remove name t.block_devices in let t' = { t with block_devices } in - report_vms t' name; + report_unikernels t' name; Ok t' let bridge_allowed set s = String_set.mem s set -let check_policy (p : Policy.t) (running_vms, used_memory) (vm : Unikernel.config) = - if succ running_vms > p.Policy.vms then - Error (`Msg (Fmt.str "maximum amount of unikernels (%d) reached" p.Policy.vms)) - else if vm.Unikernel.memory > p.Policy.memory - used_memory then +let check_policy (p : Policy.t) (running_unikernels, used_memory) (unikernel : Unikernel.config) = + if succ running_unikernels > p.Policy.unikernels then + Error (`Msg (Fmt.str "maximum amount of unikernels (%d) reached" p.Policy.unikernels)) + else if unikernel.Unikernel.memory > p.Policy.memory - used_memory then Error (`Msg (Fmt.str "maximum allowed memory (%d, used %d) would be exceeded (requesting %d)" - p.Policy.memory used_memory vm.Unikernel.memory)) - else if not (IS.mem vm.Unikernel.cpuid p.Policy.cpuids) then - Error (`Msg (Fmt.str "CPUid %u is not allowed by policy" vm.Unikernel.cpuid)) + p.Policy.memory used_memory unikernel.Unikernel.memory)) + else if not (IS.mem unikernel.Unikernel.cpuid p.Policy.cpuids) then + Error (`Msg (Fmt.str "CPUid %u is not allowed by policy" unikernel.Unikernel.cpuid)) else - match List.partition (bridge_allowed p.Policy.bridges) (Unikernel.bridges vm) with + match List.partition (bridge_allowed p.Policy.bridges) (Unikernel.bridges unikernel) with | _, [] -> Ok () | _, disallowed -> Error (`Msg (Fmt.str "bridges %a not allowed by policy" Fmt.(list ~sep:(any ", ") string) disallowed)) -let check_vm t name vm = +let check_unikernel t name unikernel = let policy_ok = let path = Name.path name in match find_policy t path with | None -> Ok () | Some p -> - let used = vm_usage t path in - check_policy p used vm + let used = unikernel_usage t path in + check_policy p used unikernel and block_ok = List.fold_left (fun r (block, dev, _sector_size) -> let* () = r in @@ -184,21 +185,21 @@ let check_vm t name vm = Error (`Msg (Fmt.str "block device %s already in use" (Name.to_string block_name))) else Ok ()) - (Ok ()) vm.block_devices - and vm_ok = match find_vm t name with + (Ok ()) unikernel.block_devices + and unikernel_ok = match find_unikernel t name with | None -> Ok () - | Some _ -> Error (`Msg "vm with same name already exists") + | Some _ -> Error (`Msg "unikernel with same name already exists") in let* () = policy_ok in let* () = block_ok in - vm_ok + unikernel_ok -let insert_vm t name vm = - let unikernels, old = Vmm_trie.insert name vm t.unikernels in +let insert_unikernel t name unikernel = + let unikernels, old = Vmm_trie.insert name unikernel t.unikernels in (match old with None -> () | Some _ -> invalid_arg ("unikernel " ^ Name.to_string name ^ " already exists in trie")) ; - let block_devices = use_blocks t.block_devices name vm true in + let block_devices = use_blocks t.block_devices name unikernel true in let t' = { t with unikernels ; block_devices } in - report_vms t' name; + report_unikernels t' name; t' let check_block t name size = @@ -228,7 +229,7 @@ let insert_block t name size = let* () = check_block t name size in let block_devices = fst (Vmm_trie.insert name (size, false) t.block_devices) in let t' = { t with block_devices } in - report_vms t' name; + report_unikernels t' name; Ok t' let check_policies_above t path sub = @@ -254,14 +255,14 @@ let check_policies_below t path super = Policy.is_smaller ~super ~sub:policy) (Ok ()) -let check_vms t path p = - let (vms, used_memory) = vm_usage t path +let check_unikernels t path p = + let (unikernels, used_memory) = unikernel_usage t path and block = total_block_usage t path in let bridges, cpuids = Vmm_trie.fold path t.unikernels - (fun _ vm (bridges, cpuids) -> - let config = vm.Unikernel.config in + (fun _ unikernel (bridges, cpuids) -> + let config = unikernel.Unikernel.config in (String_set.(union (of_list (Unikernel.bridges config)) bridges), IS.add config.Unikernel.cpuid cpuids)) (String_set.empty, IS.empty) @@ -275,9 +276,9 @@ let check_vms t path p = Error (`Msg (Fmt.str "policy allows bridges %a, which is not a superset of %a" Fmt.(list ~sep:(any ", ") string) (String_set.elements p.Policy.bridges) Fmt.(list ~sep:(any ", ") string) (String_set.elements bridges))) - else if vms > p.Policy.vms then + else if unikernels > p.Policy.unikernels then Error (`Msg (Fmt.str "unikernel would exceed running unikernel limit set by policy to %d, running %d" - p.Policy.vms vms)) + p.Policy.unikernels unikernels)) else if used_memory > p.Policy.memory then Error (`Msg (Fmt.str "unikernel would exceed running memory limit set by policy to %d MB, used %d MB" p.Policy.memory used_memory)) @@ -290,7 +291,7 @@ let check_vms t path p = let insert_policy t path p = let* () = check_policies_above t path p in let* () = check_policies_below t path p in - let* () = check_vms t path p in + let* () = check_unikernels t path p in let policies = fst (Vmm_trie.insert (Vmm_core.Name.create_of_path path) p t.policies) in diff --git a/src/vmm_resources.mli b/src/vmm_resources.mli index 527d32c..d256f03 100644 --- a/src/vmm_resources.mli +++ b/src/vmm_resources.mli @@ -24,8 +24,8 @@ type t = private { (** [empty] is the empty tree. *) val empty : t -(** [find_vm t name] is either [Some vm] or [None]. *) -val find_vm : t -> Name.t -> Unikernel.t option +(** [find_unikernel t name] is either [Some unikernel] or [None]. *) +val find_unikernel : t -> Name.t -> Unikernel.t option (** [find_policy t path] is either [Some policy] or [None]. *) val find_policy : t -> Name.path -> Policy.t option @@ -33,16 +33,18 @@ val find_policy : t -> Name.path -> Policy.t option (** [find_block t name] is either [Some (size, active)] or [None]. *) val find_block : t -> Name.t -> (int * bool) option -(** [check_vm t name vm] checks whether [vm] under [name] in [t] would be - allowed under the current policies. *) -val check_vm : t -> Name.t -> Unikernel.config -> (unit, [> `Msg of string ]) result +(** [check_unikernel t name unikernel] checks whether [unikernel] under [name] + in [t] would be allowed under the current policies. *) +val check_unikernel : t -> Name.t -> Unikernel.config -> (unit, [> `Msg of string ]) result + +(** [insert_unikernel t name unikernel] inserts [unikernel] under [name] in [t], + and returns the new [t]. The caller has to ensure (using {!check_unikernel}) + that an unikernel with the same name does not yet exist, and the block + device is not in use. -(** [insert_vm t name vm] inserts [vm] under [name] in [t], and returns the - new [t]. The caller has to ensure (using {!check_vm}) that a VM with the - same name does not yet exist, and the block device is not in use. - @raise Invalid_argument if block device is already in use, or VM already - exists. *) -val insert_vm : t -> Name.t -> Unikernel.t -> t + @raise Invalid_argument if block device is already in use, or unikernel + already exists. *) +val insert_unikernel : t -> Name.t -> Unikernel.t -> t (** [insert_policy t path policy] inserts [policy] under [path] in [t], and returns the new [t] or an error. *) @@ -56,8 +58,8 @@ val check_block : t -> Name.t -> int -> (unit, [> `Msg of string ]) result the new [t] or an error. *) val insert_block : t -> Name.t -> int -> (t, [> `Msg of string]) result -(** [remove_vm t name] removes vm [name] from [t]. *) -val remove_vm : t -> Name.t -> (t, [> `Msg of string ]) result +(** [remove_unikernel t name] removes unikernel [name] from [t]. *) +val remove_unikernel : t -> Name.t -> (t, [> `Msg of string ]) result (** [remove_policy t path] removes policy [path] from [t]. *) val remove_policy : t -> Name.path -> (t, [> `Msg of string ]) result diff --git a/src/vmm_unix.ml b/src/vmm_unix.ml index 3b15cfa..89fb605 100644 --- a/src/vmm_unix.ml +++ b/src/vmm_unix.ml @@ -300,24 +300,24 @@ let bridges_exist bridges = bridge_exists (bridge_name b)) (Ok ()) bridges -let prepare name (vm : Unikernel.config) = +let prepare name (unikernel : Unikernel.config) = let* image = - match vm.Unikernel.typ with + match unikernel.Unikernel.typ with | `Solo5 -> - if vm.Unikernel.compressed then - match Vmm_compress.uncompress vm.Unikernel.image with + if unikernel.Unikernel.compressed then + match Vmm_compress.uncompress unikernel.Unikernel.image with | Ok blob -> Ok blob | Error `Msg msg -> Error (`Msg ("failed to uncompress: " ^ msg)) else - Ok vm.Unikernel.image + Ok unikernel.Unikernel.image in let filename = Name.image_file name in let digest = Digestif.SHA256.(to_raw_string (digest_string image)) in let* target, version = solo5_image_target image in let* _ = check_solo5_tender target version in - let* () = manifest_devices_match ~bridges:vm.Unikernel.bridges ~block_devices:vm.Unikernel.block_devices image in + let* () = manifest_devices_match ~bridges:unikernel.Unikernel.bridges ~block_devices:unikernel.Unikernel.block_devices image in let* () = Bos.OS.File.write filename image in - let* () = bridges_exist vm.Unikernel.bridges in + let* () = bridges_exist unikernel.Unikernel.bridges in let fifo = Name.fifo_file name in let* () = match fifo_exists fifo with @@ -342,14 +342,14 @@ let prepare name (vm : Unikernel.config) = let* tap = create_tap bridge in let (service, _, mac) = arg in Ok ((service, tap, mac) :: acc)) - (Ok []) vm.Unikernel.bridges + (Ok []) unikernel.Unikernel.bridges in Ok (List.rev taps, digest) -let vm_device vm = +let unikernel_device unikernel = match Lazy.force uname with - | FreeBSD -> Ok ("solo5-" ^ string_of_int vm.Unikernel.pid) - | Linux -> Error (`Msg "don't know what you mean (trying to find vm device)") + | FreeBSD -> Ok ("solo5-" ^ string_of_int unikernel.Unikernel.pid) + | Linux -> Error (`Msg "don't know what you mean (trying to find unikernel device)") let free_system_resources name taps = (* same order as prepare! *) @@ -426,7 +426,7 @@ let exec name (config : Unikernel.config) bridge_taps blocks digest = close_no_err stdout; Error (`Msg (Fmt.str "cmd %a exits: %a" Bos.Cmd.pp cmd pp_unix_err e)) -let destroy vm = Unix.kill vm.Unikernel.pid Sys.sigterm +let destroy unikernel = Unix.kill unikernel.Unikernel.pid Sys.sigterm let bytes_of_mb size = let res = size lsl 20 in @@ -519,7 +519,7 @@ let root_policy () = let rec gen_cpu acc n = if n = 0 then acc else gen_cpu (Vmm_core.IS.add (pred n) acc) (pred n) in - Ok { Vmm_core.Policy.vms = max_int ; + Ok { Vmm_core.Policy.unikernels = max_int ; cpuids = gen_cpu Vmm_core.IS.empty cpus ; memory ; block = Some disk_space ; diff --git a/src/vmm_unix.mli b/src/vmm_unix.mli index aaa617a..1f04707 100644 --- a/src/vmm_unix.mli +++ b/src/vmm_unix.mli @@ -38,7 +38,7 @@ val dump : ?name:string -> string -> (unit, [> `Msg of string ]) result val restore : ?name:string -> unit -> (string, [> `Msg of string | `NoFile ]) result -val vm_device : Unikernel.t -> (string, [> `Msg of string ]) result +val unikernel_device : Unikernel.t -> (string, [> `Msg of string ]) result val manifest_devices_match : bridges:(string * string option * Macaddr.t option) list -> block_devices:(string * string option * int option) list -> string -> diff --git a/src/vmm_vmmd.ml b/src/vmm_vmmd.ml index d12cbfa..301d0fa 100644 --- a/src/vmm_vmmd.ml +++ b/src/vmm_vmmd.ml @@ -30,9 +30,9 @@ type 'a t = { let in_shutdown = ref false let remove_resources t name = - let resources = match Vmm_resources.remove_vm t.resources name with + let resources = match Vmm_resources.remove_unikernel t.resources name with | Error (`Msg e) -> - Logs.warn (fun m -> m "%s while removing vm %a from resources" e Name.pp name) ; + Logs.warn (fun m -> m "%s while removing unikernel %a from resources" e Name.pp name) ; t.resources | Ok resources -> resources in @@ -94,14 +94,14 @@ let stop_create t id = Ok (t, `End (`Success (`String "destroyed: removed waiter"))) let killall t create = - let vms = Vmm_trie.all t.resources.Vmm_resources.unikernels in + let unikernels = Vmm_trie.all t.resources.Vmm_resources.unikernels in in_shutdown := true ; let t, xs = List.fold_left (fun (t, acc) (id, _) -> let (t, a) = register t id create in (t, a :: acc)) - (t, []) vms in - List.iter Vmm_unix.destroy (List.map snd vms) ; + (t, []) unikernels in + List.iter Vmm_unix.destroy (List.map snd unikernels) ; t, xs let empty = { @@ -170,14 +170,14 @@ let restore_policies t policies = | Ok resources -> Ok { t with resources } | Error _ as e -> e -let setup_stats t name vm = +let setup_stats t name unikernel = let stat_out = - let name = match Vmm_unix.vm_device vm with + let name = match Vmm_unix.unikernel_device unikernel with | Error _ -> "" | Ok name -> name - and ifs = Unikernel.(List.combine (List.map (fun (x,_,_) -> x) vm.config.bridges) vm.taps) + and ifs = Unikernel.(List.combine (List.map (fun (x,_,_) -> x) unikernel.config.bridges) unikernel.taps) in - `Stats_add (name, vm.Unikernel.pid, ifs) + `Stats_add (name, unikernel.Unikernel.pid, ifs) in let header = Vmm_commands.header ~sequence:t.stats_counter name in let t = { t with stats_counter = Int64.succ t.stats_counter } in @@ -188,63 +188,63 @@ let remove_stats t name = let t = { t with stats_counter = Int64.succ t.stats_counter } in (t, (header, `Command (`Stats_cmd `Stats_remove))) -let handle_create t name vm_config = +let handle_create t name unikernel_config = let* () = - match Vmm_resources.find_vm t.resources name with - | Some _ -> Error (`Msg "VM with same name is already running") + match Vmm_resources.find_unikernel t.resources name with + | Some _ -> Error (`Msg "Unikernel with same name is already running") | None -> Ok () in Logs.debug (fun m -> m "now checking resource policies") ; - let* () = Vmm_resources.check_vm t.resources name vm_config in + let* () = Vmm_resources.check_unikernel t.resources name unikernel_config in (* prepare VM: save VM image to disk, create fifo, ... *) - let* taps, digest = Vmm_unix.prepare name vm_config in + let* taps, digest = Vmm_unix.prepare name unikernel_config in let pp_tap ppf (a, b, mac) = Fmt.pf ppf "%s -> %s%a" a b Fmt.(option ((any "@") ++ Macaddr.pp)) mac in - Logs.debug (fun m -> m "prepared vm with taps %a" + Logs.debug (fun m -> m "prepared unikernel with taps %a" Fmt.(list ~sep:(any ",@ ") pp_tap) taps) ; let cons_out = let header = Vmm_commands.header ~sequence:t.console_counter name in (header, `Command (`Console_cmd `Console_add)) in let success t = - (* actually execute the vm: + (* actually execute the unikernel: - check for safety that executing it would not exceed any resources - execute it - update resources --> if either the first or second fails, then the fail continuation below needs to be called *) - let* () = Vmm_resources.check_vm t.resources name vm_config in + let* () = Vmm_resources.check_unikernel t.resources name unikernel_config in let block_devices = List.map (fun (n, device, sector_size) -> n, Name.block_name name (Option.value ~default:n device), sector_size) - vm_config.Unikernel.block_devices + unikernel_config.Unikernel.block_devices in - let* vm = Vmm_unix.exec name vm_config taps block_devices digest in - Logs.debug (fun m -> m "exec()ed vm") ; - let resources = Vmm_resources.insert_vm t.resources name vm in + let* unikernel = Vmm_unix.exec name unikernel_config taps block_devices digest in + Logs.debug (fun m -> m "exec()ed unikernel") ; + let resources = Vmm_resources.insert_unikernel t.resources name unikernel in let t = { t with resources } in dump_state t ; - Logs.info (fun m -> m "created %a: %a" Name.pp name Unikernel.pp vm); - let t, stat_out = setup_stats t name vm in - Ok (t, stat_out, `Success (`String "created VM"), name, vm) + Logs.info (fun m -> m "created %a: %a" Name.pp name Unikernel.pp unikernel); + let t, stat_out = setup_stats t name unikernel in + Ok (t, stat_out, `Success (`String "created unikernel"), name, unikernel) and fail () = match Vmm_unix.free_system_resources name (List.map (fun (_,tap,_) -> tap) taps) with - | Ok () -> `Failure "could not create VM: console failed" + | Ok () -> `Failure "could not create unikernel: console failed" | Error (`Msg msg) -> - let m = "could not create VM: console failed, and also " ^ msg ^ " while cleaning resources" in + let m = "could not create unikernel: console failed, and also " ^ msg ^ " while cleaning resources" in `Failure m in Ok ({ t with console_counter = Int64.succ t.console_counter }, (cons_out, success, fail)) -let handle_shutdown t name vm r = - (match Vmm_unix.free_system_resources name vm.Unikernel.taps with +let handle_shutdown t name unikernel r = + (match Vmm_unix.free_system_resources name unikernel.Unikernel.taps with | Ok () -> () | Error (`Msg e) -> - Logs.err (fun m -> m "%s while shutdown vm %a" e Unikernel.pp vm)); + Logs.err (fun m -> m "%s while shutdown unikernel %a" e Unikernel.pp unikernel)); Logs.info (fun m -> m "unikernel %a (PID %d) stopped with %a" - Name.pp name vm.Unikernel.pid pp_process_exit r); + Name.pp name unikernel.Unikernel.pid pp_process_exit r); let t, stat_out = remove_stats t name in (t, stat_out) @@ -287,17 +287,17 @@ let handle_policy_cmd t id = let handle_unikernel_cmd t id = function | `Old_unikernel_info1 -> Logs.debug (fun m -> m "old info1 %a" Name.pp id) ; - let empty_image vm = { vm.Unikernel.config with image = "" } in - let vms = + let empty_image unikernel = { unikernel.Unikernel.config with image = "" } in + let unikernels = match Name.name id with | None -> Vmm_trie.fold (Name.path id) t.resources.Vmm_resources.unikernels - (fun id vm vms -> (id, empty_image vm) :: vms) [] + (fun id unikernel unikernels -> (id, empty_image unikernel) :: unikernels) [] | Some _ -> - Option.fold ~none:[] ~some:(fun vm -> [ id, empty_image vm ]) + Option.fold ~none:[] ~some:(fun unikernel -> [ id, empty_image unikernel ]) (Vmm_trie.find id t.resources.Vmm_resources.unikernels) in - Ok (t, `End (`Success (`Old_unikernels vms))) + Ok (t, `End (`Success (`Old_unikernels unikernels))) | `Old_unikernel_get -> Logs.debug (fun m -> m "old get %a" Name.pp id) ; begin match Vmm_trie.find id t.resources.Vmm_resources.unikernels with @@ -311,9 +311,9 @@ let handle_unikernel_cmd t id = function match Name.name id with | None -> Vmm_trie.fold (Name.path id) t.resources.Vmm_resources.unikernels - (fun id vm vms -> (id, Unikernel.info vm) :: vms) [] + (fun id unikernel unikernels -> (id, Unikernel.info unikernel) :: unikernels) [] | Some _ -> - Option.fold ~none:[] ~some:(fun vm -> [ id, Unikernel.info vm ]) + Option.fold ~none:[] ~some:(fun unikernel -> [ id, Unikernel.info unikernel ]) (Vmm_trie.find id t.resources.Vmm_resources.unikernels) in Ok (t, `End (`Success (`Old_unikernel_info infos))) @@ -323,9 +323,9 @@ let handle_unikernel_cmd t id = function match Name.name id with | None -> Vmm_trie.fold (Name.path id) t.resources.Vmm_resources.unikernels - (fun id vm vms -> (id, Unikernel.info vm) :: vms) [] + (fun id unikernel unikernels -> (id, Unikernel.info unikernel) :: unikernels) [] | Some _ -> - Option.fold ~none:[] ~some:(fun vm -> [ id, Unikernel.info vm ]) + Option.fold ~none:[] ~some:(fun unikernel -> [ id, Unikernel.info unikernel ]) (Vmm_trie.find id t.resources.Vmm_resources.unikernels) in Ok (t, `End (`Success (`Unikernel_info infos))) @@ -352,38 +352,38 @@ let handle_unikernel_cmd t id = function let r = `Unikernel_image (compress, img) in Ok (t, `End (`Success r)) end - | `Unikernel_create vm_config -> Ok (t, `Create (id, vm_config)) - | `Unikernel_force_create vm_config -> + | `Unikernel_create unikernel_config -> Ok (t, `Create (id, unikernel_config)) + | `Unikernel_force_create unikernel_config -> begin let resources = - match Vmm_resources.remove_vm t.resources id with + match Vmm_resources.remove_unikernel t.resources id with | Error _ -> t.resources | Ok r -> r in - let* () = Vmm_resources.check_vm resources id vm_config in - match Vmm_resources.find_vm t.resources id with + let* () = Vmm_resources.check_unikernel resources id unikernel_config in + match Vmm_resources.find_unikernel t.resources id with | None -> ignore (stop_create t id); - Ok (t, `Create (id, vm_config)) - | Some vm -> - (match Vmm_unix.destroy vm with + Ok (t, `Create (id, unikernel_config)) + | Some unikernel -> + (match Vmm_unix.destroy unikernel with | exception Unix.Unix_error _ -> () | () -> ()); - Ok (t, `Wait_and_create (id, (id, vm_config))) + Ok (t, `Wait_and_create (id, (id, unikernel_config))) end | `Unikernel_restart -> begin - match Vmm_resources.find_vm t.resources id with + match Vmm_resources.find_unikernel t.resources id with | None -> stop_create t id - | Some vm -> - Ok (t, `Wait_and_create (id, (id, vm.Unikernel.config))) + | Some unikernel -> + Ok (t, `Wait_and_create (id, (id, unikernel.Unikernel.config))) end | `Unikernel_destroy -> - match Vmm_resources.find_vm t.resources id with + match Vmm_resources.find_unikernel t.resources id with | None -> stop_create t id - | Some vm -> + | Some unikernel -> let answer = try - Vmm_unix.destroy vm ; "destroyed unikernel" + Vmm_unix.destroy unikernel ; "destroyed unikernel" with Unix.Unix_error _ -> "kill failed" in @@ -488,8 +488,8 @@ let handle_stats_initial t stats_counter = let t = { t with stats_counter = Int64.succ stats_counter } in let t, data = let unikernels = Vmm_trie.all t.resources.Vmm_resources.unikernels in - List.fold_left (fun (t, acc) (name, vm) -> - let t, out = setup_stats t name vm in + List.fold_left (fun (t, acc) (name, unikernel) -> + let t, out = setup_stats t name unikernel in (t, out :: acc)) (t, []) unikernels in diff --git a/test/tests.ml b/test/tests.ml index edd755a..74d5320 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -297,16 +297,16 @@ let ok_msg = Alcotest.(result unit msg) let empty_resources () = Alcotest.check test_resources __LOC__ Vmm_resources.empty Vmm_resources.empty; Alcotest.check ok_msg __LOC__ (Ok ()) - Vmm_resources.(check_vm empty (n_o_s "foo") u); + Vmm_resources.(check_unikernel empty (n_o_s "foo") u); Alcotest.check ok_msg __LOC__ (Ok ()) - Vmm_resources.(check_vm empty (n_o_s "bar") u); + Vmm_resources.(check_unikernel empty (n_o_s "bar") u); Alcotest.check ok_msg __LOC__ (Ok ()) - Vmm_resources.(check_vm empty (n_o_s "foo:bar") u); + Vmm_resources.(check_unikernel empty (n_o_s "foo:bar") u); Alcotest.check ok_msg __LOC__ (Ok ()) Vmm_resources.(check_block empty (n_o_s "foo:bar") 10) let p1 = Policy.{ - vms = 1 ; + unikernels = 1 ; cpuids = IS.singleton 0 ; memory = 10 ; block = Some 5 ; @@ -316,20 +316,20 @@ let p1 = Policy.{ let r1 = Result.get_ok (Vmm_resources.(insert_policy empty (p_o_s "alpha") p1)) -let policy_is_respected_vm () = +let policy_is_respected_unikernel () = Alcotest.check ok_msg __LOC__ (Ok ()) - (Vmm_resources.check_vm r1 (n_o_s "alpha:bar") u); + (Vmm_resources.check_unikernel r1 (n_o_s "alpha:bar") u); Alcotest.check ok_msg __LOC__ (Ok ()) - (Vmm_resources.check_vm r1 (n_o_s "alpha:bar") u); + (Vmm_resources.check_unikernel r1 (n_o_s "alpha:bar") u); let u' = { u with cpuid = 1 } in Alcotest.check ok_msg __LOC__ (Error (`Msg "cpuid not allowed")) - (Vmm_resources.check_vm r1 (n_o_s "alpha:bar") u'); + (Vmm_resources.check_unikernel r1 (n_o_s "alpha:bar") u'); let u' = { u with memory = 11 } in Alcotest.check ok_msg __LOC__ (Error (`Msg "too much memory")) - (Vmm_resources.check_vm r1 (n_o_s "alpha:bar") u'); + (Vmm_resources.check_unikernel r1 (n_o_s "alpha:bar") u'); let u' = { u with bridges = [ "service2", None, None ] } in Alcotest.check ok_msg __LOC__ (Error (`Msg "wrong bridge")) - (Vmm_resources.check_vm r1 (n_o_s "alpha:bar") u') + (Vmm_resources.check_unikernel r1 (n_o_s "alpha:bar") u') let policy_is_respected_block () = Alcotest.check ok_msg __LOC__ (Ok ()) @@ -345,9 +345,9 @@ let policy_is_respected_sub () = (match Vmm_resources.insert_policy r1 (p_o_s "alpha:beta") p1 with | Error _ -> Alcotest.fail "expected insertion of sub-policy to succeed" | Ok _ -> ()); - let p' = { p1 with vms = 2 } in + let p' = { p1 with unikernels = 2 } in (match Vmm_resources.insert_policy r1 (p_o_s "alpha:beta") p' with - | Ok _ -> Alcotest.fail "insertion of subpolicy increasing vms should fail" + | Ok _ -> Alcotest.fail "insertion of subpolicy increasing unikernels should fail" | Error _ -> ()); let p' = { p1 with cpuids = IS.singleton 1 } in (match Vmm_resources.insert_policy r1 (p_o_s "alpha:beta") p' with @@ -379,13 +379,13 @@ let policy_is_respected_sub () = | Error _ -> ()) let policy_is_respected_super () = - let p' = { p1 with vms = 2 } in + let p' = { p1 with unikernels = 2 } in (match Vmm_resources.insert_policy r1 Name.root_path p' with | Ok _ -> () - | Error _ -> Alcotest.fail "insertion of superpolicy increasing vms should work"); - let p' = { p1 with vms = 0 } in + | Error _ -> Alcotest.fail "insertion of superpolicy increasing unikernels should work"); + let p' = { p1 with unikernels = 0 } in (match Vmm_resources.insert_policy r1 Name.root_path p' with - | Ok _ -> Alcotest.fail "insertion of superpolicy decreasing vms should fail" + | Ok _ -> Alcotest.fail "insertion of superpolicy decreasing unikernels should fail" | Error _ -> ()); let p' = { p1 with cpuids = IS.(add 1 (singleton 0)) } in (match Vmm_resources.insert_policy r1 Name.root_path p' with @@ -425,7 +425,7 @@ let policy_is_respected_super () = | Error _ -> ()) let policy_can_be_overwritten () = - let p' = { p1 with vms = 2 } in + let p' = { p1 with unikernels = 2 } in match Vmm_resources.insert_policy r1 (p_o_s "alpha") p' with | Ok _ -> () | Error _ -> Alcotest.fail "overwriting of policy should work" @@ -458,7 +458,7 @@ let resource_remove_policy () = | Error _ -> Alcotest.fail "expected removal of policy to succeed" | Ok _ -> ()) -let resource_add_remove_vm () = +let resource_add_remove_unikernel () = let u1 = Unikernel.{ config = u ; @@ -469,53 +469,53 @@ let resource_add_remove_vm () = started = Ptime.epoch ; } in - (match Vmm_resources.remove_vm r1 (n_o_s "alpha:beta") with - | Ok _ -> Alcotest.fail "expected non-existing vm removal to fail" + (match Vmm_resources.remove_unikernel r1 (n_o_s "alpha:beta") with + | Ok _ -> Alcotest.fail "expected non-existing unikernel removal to fail" | Error _ -> ()); - let r2 = Vmm_resources.insert_vm r1 (n_o_s "alpha:beta") u1 in - Alcotest.check ok_msg __LOC__ (Error (`Msg "vm with same name already present")) - Vmm_resources.(check_vm r2 (n_o_s "alpha:beta") u); + let r2 = Vmm_resources.insert_unikernel r1 (n_o_s "alpha:beta") u1 in + Alcotest.check ok_msg __LOC__ (Error (`Msg "unikernel with same name already present")) + Vmm_resources.(check_unikernel r2 (n_o_s "alpha:beta") u); (try - ignore (Vmm_resources.insert_vm r2 (n_o_s "alpha:beta") u1); - Alcotest.fail "expected exception (second vm with same name)" + ignore (Vmm_resources.insert_unikernel r2 (n_o_s "alpha:beta") u1); + Alcotest.fail "expected exception (second unikernel with same name)" with Invalid_argument _ -> ()); - match Vmm_resources.remove_vm r2 (n_o_s "alpha:beta") with + match Vmm_resources.remove_unikernel r2 (n_o_s "alpha:beta") with | Ok r3 -> - ignore (Vmm_resources.insert_vm r3 (n_o_s "alpha:beta") u1) - | Error _ -> Alcotest.fail "expected vm removal to succeed" + ignore (Vmm_resources.insert_unikernel r3 (n_o_s "alpha:beta") u1) + | Error _ -> Alcotest.fail "expected unikernel removal to succeed" -let resource_vm_with_block () = +let resource_unikernel_with_block () = let uc2 = Unikernel.{ u with block_devices = [ "block", None, None ] } in Alcotest.check ok_msg __LOC__ (Error (`Msg "block device not found")) - Vmm_resources.(check_vm r1 (n_o_s "alpha:bar") uc2); + Vmm_resources.(check_unikernel r1 (n_o_s "alpha:bar") uc2); let r2 = Result.get_ok (Vmm_resources.insert_block r1 (n_o_s "alpha:block") 5) in Alcotest.check ok_msg __LOC__ (Ok ()) - Vmm_resources.(check_vm r2 (n_o_s "alpha:bar") uc2); + Vmm_resources.(check_unikernel r2 (n_o_s "alpha:bar") uc2); let uc3 = { uc2 with block_devices = [ "block", Some "b", None ] } in Alcotest.check ok_msg __LOC__ (Error (`Msg "block device not found")) - Vmm_resources.(check_vm r1 (n_o_s "alpha:bar") uc3); + Vmm_resources.(check_unikernel r1 (n_o_s "alpha:bar") uc3); let u = Unikernel.{ config = uc2; cmd = Array.make 0 "" ; pid = 0 ; taps = [] ; digest = "" ; started = Ptime.epoch ; } in - let r3 = Vmm_resources.insert_vm r2 (n_o_s "alpha:bar") u in + let r3 = Vmm_resources.insert_unikernel r2 (n_o_s "alpha:bar") u in Alcotest.check ok_msg __LOC__ (Error (`Msg "block device already in use")) - Vmm_resources.(check_vm r3 (n_o_s "alpha:bar2") uc2); + Vmm_resources.(check_unikernel r3 (n_o_s "alpha:bar2") uc2); (match Vmm_resources.remove_block r3 (n_o_s "alpha:block") with | Ok _ -> Alcotest.fail "block device should still be in use" | Error _ -> ()) let resource_tests = [ "empty resources is empty, everything accepted", `Quick, empty_resources ; - "policy is respected when checking vm", `Quick, policy_is_respected_vm ; + "policy is respected when checking unikernel", `Quick, policy_is_respected_unikernel ; "policy is respected when checking block", `Quick, policy_is_respected_block ; "policy is respected when checking sub-policy", `Quick, policy_is_respected_sub ; "policy is respected when checking super-policy", `Quick, policy_is_respected_super ; "policy can be overwritten", `Quick, policy_can_be_overwritten ; "block insertion and removal", `Quick, resource_insert_block ; "policy removal", `Quick, resource_remove_policy ; - "vm insertion and removal", `Quick, resource_add_remove_vm ; - "vm with block", `Quick, resource_vm_with_block ; + "unikernel insertion and removal", `Quick, resource_add_remove_unikernel ; + "unikernel with block", `Quick, resource_unikernel_with_block ; ] let test_version = From a8c5a97a3a4720279cdd0506dfb8901d30cb34dc Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Sat, 5 Oct 2024 00:22:58 +0200 Subject: [PATCH 3/3] add documentation to vmm_trie --- src/vmm_trie.mli | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/vmm_trie.mli b/src/vmm_trie.mli index fee11f1..4cd6e1e 100644 --- a/src/vmm_trie.mli +++ b/src/vmm_trie.mli @@ -1,19 +1,45 @@ (* (c) 2018 Hannes Mehnert, all rights reserved *) +(** A trie data structure where {!Vmm_core.Name.t} elements are the edges, and + ['a option] is at each nodes. + + Since policies are modeled as X.509 arcs, or paths, or domain names - we + often need a data structure to access all nodes at the same level (and + ensure there's at most one thing at each level. *) + open Vmm_core +(** The type of a Vmm_trie. *) type 'a t +(** [empty] is the empty trie. *) val empty : 'a t +(** [insert name v t] returns the new [t'], where [t'(name) = Some v] (this is + the only modification). Also, potentially [t(name)] is returned, if it was + present. +*) val insert : Name.t -> 'a -> 'a t -> 'a t * 'a option +(** [remove name t] removes the value [t(name)], and returns the new [t']. *) val remove : Name.t -> 'a t -> 'a t +(** [find name t] returns the value [t(name)], if present. *) val find : Name.t -> 'a t -> 'a option +(** [collect name t] finds for each sub-element of name the connected values. + If [name] is "foo:bar", + [("foo:bar", t("foo:bar")) :: ("foo", t("foo")) :: ("", t("")) :: []] + are returned (only the values present are returned. + + This is at the moment used in the albatross statistics daemon, but it may + be removed soon. +*) val collect : Name.t -> 'a t -> (Name.t * 'a) list +(** [all t] flattens the trie into an associative list. *) val all : 'a t -> (Name.t * 'a) list +(** [fold path t f init] folds [f] over [t] at [path]. Each subnode of [path] is + passed to [f]. *) val fold : Name.path -> 'a t -> (Name.t -> 'a -> 'b -> 'b) -> 'b -> 'b