forked from mirage/mirage
/
mirage.ml
2038 lines (1812 loc) · 70.5 KB
/
mirage.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright (c) 2013 Thomas Gazagnaire <thomas@gazagnaire.org>
* Copyright (c) 2013 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
open Rresult
open Astring
module Key = Mirage_key
module Name = Functoria_app.Name
module Codegen = Functoria_app.Codegen
let src = Logs.Src.create "mirage" ~doc:"mirage cli tool"
module Log = (val Logs.src_log src : Logs.LOG)
include Functoria
let get_target i = Key.(get (Info.context i) target)
let with_output ?mode f k err =
match Bos.OS.File.with_oc ?mode f k () with
| Ok b -> b
| Error _ -> R.error_msg ("couldn't open output channel for " ^ err)
let connect_err name number =
Fmt.strf "The %s connect expects exactly %d argument%s"
name number (if number = 1 then "" else "s")
(** {2 OCamlfind predicates} *)
(* Mirage implementation backing the target. *)
let backend_predicate = function
| `Xen | `Qubes -> "mirage_xen"
| `Virtio | `Ukvm -> "mirage_solo5"
| `Unix | `MacOSX -> "mirage_unix"
(** {2 Devices} *)
let qrexec = job
let qrexec_qubes = impl @@ object
inherit base_configurable
method ty = qrexec
val name = Name.ocamlify @@ "qrexec_"
method name = name
method module_name = "Qubes.RExec"
method! packages = Key.pure [ package ~min:"0.4" "mirage-qubes" ]
method! configure i =
match get_target i with
| `Qubes -> R.ok ()
| _ -> R.error_msg "Qubes remote-exec invoked for non-Qubes target."
method! connect _ modname _args =
Fmt.strf
"@[<v 2>\
%s.connect ~domid:0 () >>= fun qrexec ->@ \
Lwt.async (fun () ->@ \
OS.Lifecycle.await_shutdown_request () >>= fun _ ->@ \
%s.disconnect qrexec);@ \
Lwt.return (`Ok qrexec)@]"
modname modname
end
let gui = job
let gui_qubes = impl @@ object
inherit base_configurable
method ty = gui
val name = Name.ocamlify @@ "gui"
method name = name
method module_name = "Qubes.GUI"
method! packages = Key.pure [ package ~min:"0.4" "mirage-qubes" ]
method! configure i =
match get_target i with
| `Qubes -> R.ok ()
| _ -> R.error_msg "Qubes GUI invoked for non-Qubes target."
method! connect _ modname _args =
Fmt.strf
"@[<v 2>\
%s.connect ~domid:0 () >>= fun gui ->@ \
Lwt.async (fun () -> %s.listen gui);@ \
Lwt.return (`Ok gui)@]"
modname modname
end
type qubesdb = QUBES_DB
let qubesdb = Type QUBES_DB
let qubesdb_conf = object
inherit base_configurable
method ty = qubesdb
method name = "qubesdb"
method module_name = "Qubes.DB"
method! packages = Key.pure [ package ~min:"0.4" "mirage-qubes" ]
method! configure i =
match get_target i with
| `Qubes | `Xen -> R.ok ()
| _ -> R.error_msg "Qubes DB invoked for an unsupported target; qubes and xen are supported"
method! connect _ modname _args = Fmt.strf "%s.connect ~domid:0 ()" modname
end
let default_qubesdb = impl qubesdb_conf
type io_page = IO_PAGE
let io_page = Type IO_PAGE
let io_page_conf = object
inherit base_configurable
method ty = io_page
method name = "io_page"
method module_name = "Io_page"
method! packages =
Key.(if_ is_unix)
[ package ~sublibs:["unix"] "io-page" ]
[ package "io-page" ]
end
let default_io_page = impl io_page_conf
type time = TIME
let time = Type TIME
let time_conf = object
inherit base_configurable
method ty = time
method name = "time"
method module_name = "OS.Time"
end
let default_time = impl time_conf
type pclock = PCLOCK
let pclock = Type PCLOCK
let posix_clock_conf = object
inherit base_configurable
method ty = pclock
method name = "pclock"
method module_name = "Pclock"
method! packages =
Key.(if_ is_unix)
[ package ~min:"1.2.0" "mirage-clock-unix" ]
[ package ~min:"1.2.0" "mirage-clock-freestanding" ]
method! connect _ modname _args = Fmt.strf "%s.connect ()" modname
end
let default_posix_clock = impl posix_clock_conf
type mclock = MCLOCK
let mclock = Type MCLOCK
let monotonic_clock_conf = object
inherit base_configurable
method ty = mclock
method name = "mclock"
method module_name = "Mclock"
method! packages =
Key.(if_ is_unix)
[ package ~min:"1.2.0" "mirage-clock-unix" ]
[ package ~min:"1.2.0" "mirage-clock-freestanding" ]
method! connect _ modname _args = Fmt.strf "%s.connect ()" modname
end
let default_monotonic_clock = impl monotonic_clock_conf
type random = RANDOM
let random = Type RANDOM
let stdlib_random_conf = object
inherit base_configurable
method ty = random
method name = "random"
method module_name = "Stdlibrandom"
method! packages = Key.pure [ package "mirage-random" ]
method! connect _ modname _ = Fmt.strf "Lwt.return (%s.initialize ())" modname
end
let stdlib_random = impl stdlib_random_conf
let query_ocamlfind ?(recursive = false) ?(format="%p") ?predicates libs =
let open Bos in
let flag = if recursive then "-recursive" else ""
and format = Cmd.of_list [ "-format" ; format ]
and predicate = match predicates with
| None -> []
| Some x -> [ "-predicates" ; x ]
and q = "query"
in
let cmd =
Cmd.(v "ocamlfind" % q % flag %% format %% of_list predicate %% of_list libs)
in
OS.Cmd.run_out cmd |> OS.Cmd.out_lines >>| fst
(* This is to check that entropy is a dependency if "tls" is in
the package array. *)
let enable_entropy, is_entropy_enabled =
let r = ref false in
let f () = r := true in
let g () = !r in
(f, g)
let check_entropy libs =
query_ocamlfind ~recursive:true libs >>= fun ps ->
if List.mem "nocrypto" ps && not (is_entropy_enabled ()) then
R.error_msg
{___|The \"nocrypto\" library is loaded but entropy is not enabled!@ \
Please enable the entropy by adding a dependency to the nocrypto \
device. You can do so by adding ~deps:[abstract nocrypto] \
to the arguments of Mirage.foreign.|___}
else
R.ok ()
let nocrypto = impl @@ object
inherit base_configurable
method ty = job
method name = "nocrypto"
method module_name = "Nocrypto_entropy"
method! packages =
Key.match_ Key.(value target) @@ function
| `Xen | `Qubes ->
[ package ~min:"0.5.4" ~sublibs:["mirage"] "nocrypto";
package ~ocamlfind:[] "zarith-xen" ]
| `Virtio | `Ukvm ->
[ package ~min:"0.5.4" ~sublibs:["mirage"] "nocrypto";
package ~ocamlfind:[] "zarith-freestanding" ]
| `Unix | `MacOSX ->
[ package ~min:"0.5.4" ~sublibs:["lwt"] "nocrypto" ]
method! build _ = R.ok (enable_entropy ())
method! connect i _ _ =
match get_target i with
| `Xen | `Qubes | `Virtio | `Ukvm -> "Nocrypto_entropy_mirage.initialize ()"
| `Unix | `MacOSX -> "Nocrypto_entropy_lwt.initialize ()"
end
let nocrypto_random_conf = object
inherit base_configurable
method ty = random
method name = "random"
method module_name = "Nocrypto.Rng"
method! packages = Key.pure [ package ~min:"0.5.4" "nocrypto" ]
method! deps = [abstract nocrypto]
end
let nocrypto_random = impl nocrypto_random_conf
let default_random =
match_impl (Key.value @@ Key.prng ()) [
`Stdlib , stdlib_random;
`Nocrypto, nocrypto_random;
] ~default:stdlib_random
type console = CONSOLE
let console = Type CONSOLE
let console_unix str = impl @@ object
inherit base_configurable
method ty = console
val name = Name.ocamlify @@ "console_unix_" ^ str
method name = name
method module_name = "Console_unix"
method! packages = Key.pure [ package ~min:"2.2.0" "mirage-console-unix" ]
method! connect _ modname _args = Fmt.strf "%s.connect %S" modname str
end
let console_xen str = impl @@ object
inherit base_configurable
method ty = console
val name = Name.ocamlify @@ "console_xen_" ^ str
method name = name
method module_name = "Console_xen"
method! packages = Key.pure [ package ~min:"2.2.0" "mirage-console-xen" ]
method! connect _ modname _args = Fmt.strf "%s.connect %S" modname str
end
let console_solo5 str = impl @@ object
inherit base_configurable
method ty = console
val name = Name.ocamlify @@ "console_solo5_" ^ str
method name = name
method module_name = "Console_solo5"
method! packages = Key.pure [ package ~min:"0.2.0" "mirage-console-solo5" ]
method! connect _ modname _args = Fmt.strf "%s.connect %S" modname str
end
let custom_console str =
match_impl Key.(value target) [
`Xen, console_xen str;
`Qubes, console_xen str;
`Virtio, console_solo5 str;
`Ukvm, console_solo5 str
] ~default:(console_unix str)
let default_console = custom_console "0"
type kv_ro = KV_RO
let kv_ro = Type KV_RO
let crunch dirname = impl @@ object
inherit base_configurable
method ty = kv_ro
val name = Name.create ("static" ^ dirname) ~prefix:"static"
method name = name
method module_name = String.Ascii.capitalize name
method! packages =
Key.pure [ package "io-page"; package ~min:"2.0.0" ~build:true "crunch" ]
method! deps = [ abstract default_io_page ]
method! connect _ modname _ = Fmt.strf "%s.connect ()" modname
method! build _i =
let dir = Fpath.(v dirname) in
let file = Fpath.(v name + "ml") in
Bos.OS.Path.exists dir >>= function
| true ->
Log.info (fun m -> m "Generating: %a" Fpath.pp file);
Bos.OS.Cmd.run Bos.Cmd.(v "ocaml-crunch" % "-o" % p file % p dir)
| false ->
R.error_msg (Fmt.strf "The directory %s does not exist." dirname)
method! clean _i =
Bos.OS.File.delete Fpath.(v name + "ml") >>= fun () ->
Bos.OS.File.delete Fpath.(v name + "mli")
end
let direct_kv_ro_conf dirname = impl @@ object
inherit base_configurable
method ty = kv_ro
val name = Name.create ("direct" ^ dirname) ~prefix:"direct"
method name = name
method module_name = "Kvro_fs_unix"
method! packages = Key.pure [ package ~min:"1.3.0" "mirage-fs-unix" ]
method! connect i _modname _names =
let path = Fpath.(Info.build_dir i / dirname) in
Fmt.strf "Kvro_fs_unix.connect \"%a\"" Fpath.pp path
end
let direct_kv_ro dirname =
match_impl Key.(value target) [
`Xen, crunch dirname;
`Qubes, crunch dirname;
`Virtio, crunch dirname;
`Ukvm, crunch dirname
] ~default:(direct_kv_ro_conf dirname)
type block = BLOCK
let block = Type BLOCK
type block_t = { filename: string; number: int }
let all_blocks = Hashtbl.create 7
let make_block_t =
(* NB: reserve number 0 for the boot disk *)
let next_number = ref 1 in
fun filename ->
let b =
if Hashtbl.mem all_blocks filename then
Hashtbl.find all_blocks filename
else begin
let number = !next_number in
incr next_number;
let b = { filename; number } in
Hashtbl.add all_blocks filename b;
b
end in
b
class block_conf file =
let b = make_block_t file in
let name = Name.create file ~prefix:"block" in
object (self)
inherit base_configurable
method ty = block
method name = name
method module_name = "Block"
method! packages =
Key.match_ Key.(value target) @@ function
| `Xen | `Qubes -> [ package ~min:"1.5.0" ~sublibs:["front"] "mirage-block-xen" ]
| `Virtio | `Ukvm -> [ package ~min:"0.2.1" "mirage-block-solo5" ]
| `Unix | `MacOSX -> [ package ~min:"2.5.0" "mirage-block-unix" ]
method private connect_name target root =
match target with
| `Unix | `MacOSX | `Virtio | `Ukvm ->
Fpath.(to_string (root / b.filename)) (* open the file directly *)
| `Xen | `Qubes ->
(* We need the xenstore id *)
(* Taken from https://github.com/mirage/mirage-block-xen/blob/
a64d152586c7ebc1d23c5adaa4ddd440b45a3a83/lib/device_number.ml#L64 *)
(if b. number < 16
then (202 lsl 8) lor (b.number lsl 4)
else (1 lsl 28) lor (b.number lsl 8)) |> string_of_int
method! connect i s _ =
Fmt.strf "%s.connect %S" s
(self#connect_name (get_target i) @@ Info.build_dir i)
end
let block_of_file file = impl (new block_conf file)
class ramdisk_conf rname =
object
inherit base_configurable
method ty = block
method name = "ramdisk"
method module_name = "Ramdisk"
method! packages =
Key.pure [ package "mirage-block-ramdisk" ]
method! connect _i modname _names =
Fmt.strf "%s.connect ~name:%S" modname rname
end
let ramdisk rname = impl (new ramdisk_conf rname)
let generic_block ?(key = Key.value @@ Key.block ()) name =
match_impl key [
`BlockFile, block_of_file name;
`Ramdisk, ramdisk name;
] ~default:(ramdisk name)
let tar_block dir =
let name = Name.create ("tar_block" ^ dir) ~prefix:"tar_block" in
let block_file = name ^ ".img" in
impl @@ object
inherit block_conf block_file as super
method! build i =
Bos.OS.Cmd.run Bos.Cmd.(v "tar" % "-cvf" % block_file % dir) >>= fun () ->
super#build i
end
let archive_conf = impl @@ object
inherit base_configurable
method ty = block @-> kv_ro
method name = "archive"
method module_name = "Tar_mirage.Make_KV_RO"
method! packages =
Key.pure [ package ~min:"0.8.0" "tar-mirage" ]
method! connect _ modname = function
| [ block ] -> Fmt.strf "%s.connect %s" modname block
| _ -> failwith (connect_err "archive" 1)
end
let archive block = archive_conf $ block
let archive_of_files ?(dir=".") () = archive @@ tar_block dir
type fs = FS
let fs = Type FS
let fat_conf = impl @@ object
inherit base_configurable
method ty = (block @-> fs)
method! packages = Key.pure [ package ~min:"0.12.0" "fat-filesystem" ]
method name = "fat"
method module_name = "Fat.FS"
method! connect _ modname l = match l with
| [ block_name ] -> Fmt.strf "%s.connect %s" modname block_name
| _ -> failwith (connect_err "fat" 1)
end
let fat block = fat_conf $ block
let fat_block ?(dir=".") ?(regexp="*") () =
let name =
Name.(ocamlify @@ create (Fmt.strf "fat%s:%s" dir regexp) ~prefix:"fat_block")
in
let block_file = name ^ ".img" in
impl @@ object
inherit block_conf block_file as super
method! packages =
Key.map (List.cons (package ~min:"0.12.0" ~build:true "fat-filesystem")) super#packages
method! build i =
let root = Info.build_dir i in
let file = Fmt.strf "make-%s-image.sh" name in
let dir = Fpath.of_string dir |> R.error_msg_to_invalid_arg in
Log.info (fun m -> m "Generating block generator script: %s" file);
with_output ~mode:0o755 (Fpath.v file)
(fun oc () ->
let fmt = Format.formatter_of_out_channel oc in
Codegen.append fmt "#!/bin/sh";
Codegen.append fmt "";
Codegen.append fmt "echo This uses the 'fat' command-line tool to \
build a simple FAT";
Codegen.append fmt "echo filesystem image.";
Codegen.append fmt "";
Codegen.append fmt "FAT=$(which fat)";
Codegen.append fmt "if [ ! -x \"${FAT}\" ]; then";
Codegen.append fmt " echo I couldn\\'t find the 'fat' command-line \
tool.";
Codegen.append fmt " echo Try running 'opam install fat-filesystem'";
Codegen.append fmt " exit 1";
Codegen.append fmt "fi";
Codegen.append fmt "";
Codegen.append fmt "IMG=$(pwd)/%s" block_file;
Codegen.append fmt "rm -f ${IMG}";
Codegen.append fmt "cd %a" Fpath.pp (Fpath.append root dir);
Codegen.append fmt "SIZE=$(du -s . | cut -f 1)";
Codegen.append fmt "${FAT} create ${IMG} ${SIZE}KiB";
Codegen.append fmt "${FAT} add ${IMG} %s" regexp;
Codegen.append fmt "echo Created '%s'" block_file;
R.ok ())
"fat shell script" >>= fun () ->
Log.info (fun m -> m "Executing block generator script: ./%s" file);
Bos.OS.Cmd.run (Bos.Cmd.v ("./" ^ file)) >>= fun () ->
super#build i
method! clean i =
let file = Fmt.strf "make-%s-image.sh" name in
Bos.OS.File.delete (Fpath.v file) >>= fun () ->
Bos.OS.File.delete (Fpath.v block_file) >>= fun () ->
super#clean i
end
let fat_of_files ?dir ?regexp () = fat @@ fat_block ?dir ?regexp ()
let kv_ro_of_fs_conf = impl @@ object
inherit base_configurable
method ty = fs @-> kv_ro
method name = "kv_ro_of_fs"
method module_name = "Mirage_fs_lwt.To_KV_RO"
method! packages = Key.pure [ package "mirage-fs-lwt" ]
method! connect _ modname = function
| [ fs ] -> Fmt.strf "%s.connect %s" modname fs
| _ -> failwith (connect_err "kv_ro_of_fs" 1)
end
let kv_ro_of_fs x = kv_ro_of_fs_conf $ x
(** generic kv_ro. *)
let generic_kv_ro ?(key = Key.value @@ Key.kv_ro ()) dir =
match_impl key [
`Fat , kv_ro_of_fs @@ fat_of_files ~dir () ;
`Archive, archive_of_files ~dir () ;
`Crunch , crunch dir ;
`Direct , direct_kv_ro dir ;
] ~default:(direct_kv_ro dir)
(** network devices *)
type network = NETWORK
let network = Type NETWORK
let all_networks = ref []
let network_conf (intf : string Key.key) =
let key = Key.abstract intf in
object
inherit base_configurable
method ty = network
val name = Functoria_app.Name.create "net" ~prefix:"net"
method name = name
method module_name = "Netif"
method! keys = [ key ]
method! packages =
Key.match_ Key.(value target) @@ function
| `Unix -> [ package ~min:"2.3.0" "mirage-net-unix" ]
| `MacOSX -> [ package ~min:"1.3.0" "mirage-net-macosx" ]
| `Xen -> [ package ~min:"1.7.0" "mirage-net-xen"]
| `Qubes -> [ package ~min:"1.7.0" "mirage-net-xen" ; package ~min:"0.4" "mirage-qubes" ]
| `Virtio | `Ukvm -> [ package ~min:"0.2.0" "mirage-net-solo5" ]
method! connect _ modname _ =
Fmt.strf "%s.connect %a" modname Key.serialize_call key
method! configure i =
all_networks := Key.get (Info.context i) intf :: !all_networks;
R.ok ()
end
let netif ?group dev = impl (network_conf @@ Key.interface ?group dev)
let default_network =
match_impl Key.(value target) [
`Unix , netif "tap0";
`MacOSX , netif "tap0";
] ~default:(netif "0")
type dhcp = Dhcp_client
let dhcp = Type Dhcp_client
type ethernet = ETHERNET
let ethernet = Type ETHERNET
let ethernet_conf = object
inherit base_configurable
method ty = network @-> ethernet
method name = "ethif"
method module_name = "Ethif.Make"
method! packages = Key.pure [ package ~min:"3.1.0" ~sublibs:["ethif"] "tcpip" ]
method! connect _ modname = function
| [ eth ] -> Fmt.strf "%s.connect %s" modname eth
| _ -> failwith (connect_err "ethernet" 1)
end
let etif_func = impl ethernet_conf
let etif network = etif_func $ network
type arpv4 = Arpv4
let arpv4 = Type Arpv4
let arpv4_conf = object
inherit base_configurable
method ty = ethernet @-> mclock @-> time @-> arpv4
method name = "arpv4"
method module_name = "Arpv4.Make"
method! packages = Key.pure [ package ~min:"3.0.0" ~sublibs:["arpv4"] "tcpip" ]
method! connect _ modname = function
| [ eth ; clock ; _time ] -> Fmt.strf "%s.connect %s %s" modname eth clock
| _ -> failwith (connect_err "arpv4" 3)
end
let arp_func = impl arpv4_conf
let arp
?(clock = default_monotonic_clock)
?(time = default_time)
(eth : ethernet impl) =
arp_func $ eth $ clock $ time
let farp_conf = object
inherit base_configurable
method ty = ethernet @-> mclock @-> time @-> arpv4
method name = "arp"
method module_name = "Arp.Make"
method! packages = Key.pure [ package ~min:"0.2.0" ~sublibs:["mirage"] "arp" ]
method! connect _ modname = function
| [ eth ; clock ; _time ] -> Fmt.strf "%s.connect %s %s" modname eth clock
| _ -> failwith (connect_err "arp" 3)
end
let farp_func = impl farp_conf
let farp
?(clock = default_monotonic_clock)
?(time = default_time)
(eth : ethernet impl) =
farp_func $ eth $ clock $ time
type v4
type v6
type 'a ip = IP
type ipv4 = v4 ip
type ipv6 = v6 ip
let ip = Type IP
let ipv4: ipv4 typ = ip
let ipv6: ipv6 typ = ip
let meta_ipv4 ppf s =
Fmt.pf ppf "(Ipaddr.V4.of_string_exn %S)" (Ipaddr.V4.to_string s)
type ipv4_config = {
network : Ipaddr.V4.Prefix.t * Ipaddr.V4.t;
gateway : Ipaddr.V4.t option;
}
(** Types for IPv4 manual configuration. *)
let pp_key fmt k = Key.serialize_call fmt (Key.abstract k)
let opt_key s = Fmt.(option @@ prefix (unit ("~"^^s^^":")) pp_key)
let opt_map f = function Some x -> Some (f x) | None -> None
let (@?) x l = match x with Some s -> s :: l | None -> l
let (@??) x y = opt_map Key.abstract x @? y
(* convenience function for linking tcpip.unix or .xen for checksums *)
let right_tcpip_library ?min ?max ?ocamlfind ~sublibs pkg =
Key.match_ Key.(value target) @@ function
|`MacOSX | `Unix -> [ package ?min ?max ?ocamlfind ~sublibs:("unix"::sublibs) pkg ]
|`Qubes | `Xen -> [ package ?min ?max ?ocamlfind ~sublibs:("xen"::sublibs) pkg ]
|`Virtio | `Ukvm -> [ package ?min ?max ?ocamlfind ~sublibs pkg ]
let ipv4_keyed_conf ?network ?gateway () = impl @@ object
inherit base_configurable
method ty = ethernet @-> arpv4 @-> ipv4
method name = Name.create "ipv4" ~prefix:"ipv4"
method module_name = "Static_ipv4.Make"
method! packages = right_tcpip_library ~min:"3.1.0" ~sublibs:["ipv4"] "tcpip"
method! keys = network @?? gateway @?? []
method! connect _ modname = function
| [ etif ; arp ] ->
Fmt.strf
"let (network, ip) = %a in @ \
%s.connect@[@ ~ip ~network %a@ %s@ %s@]"
(Fmt.option pp_key) network
modname
(opt_key "gateway") gateway
etif arp
| _ -> failwith (connect_err "ipv4 keyed" 2)
end
let dhcp_conf = impl @@ object
inherit base_configurable
method ty = time @-> network @-> dhcp
method name = "dhcp_client"
method module_name = "Dhcp_client_mirage.Make"
method! packages = Key.pure [ package "charrua-client-mirage" ]
method! connect _ modname = function
| [ _time; network ] -> Fmt.strf "%s.connect %s " modname network
| _ -> failwith (connect_err "dhcp" 2)
end
let ipv4_dhcp_conf = impl @@ object
inherit base_configurable
method ty = dhcp @-> ethernet @-> arpv4 @-> ipv4
method name = Name.create "dhcp_ipv4" ~prefix:"dhcp_ipv4"
method module_name = "Dhcp_ipv4.Make"
method! packages = Key.pure [ package "charrua-client-mirage" ]
method! connect _ modname = function
| [ dhcp ; ethernet ; arp ] ->
Fmt.strf "%s.connect@[@ %s@ %s@ %s@]" modname dhcp ethernet arp
| _ -> failwith (connect_err "ipv4 dhcp" 3)
end
let dhcp time net = dhcp_conf $ time $ net
let ipv4_of_dhcp dhcp ethif arp = ipv4_dhcp_conf $ dhcp $ ethif $ arp
let create_ipv4 ?group ?config etif arp =
let config = match config with
| None ->
let network = Ipaddr.V4.Prefix.of_address_string_exn "10.0.0.2/24"
and gateway = Some (Ipaddr.V4.of_string_exn "10.0.0.1")
in
{ network; gateway }
| Some config -> config
in
let network = Key.V4.network ?group config.network
and gateway = Key.V4.gateway ?group config.gateway
in
ipv4_keyed_conf ~network ~gateway () $ etif $ arp
type ipv6_config = {
addresses: Ipaddr.V6.t list;
netmasks: Ipaddr.V6.Prefix.t list;
gateways: Ipaddr.V6.t list;
}
(** Types for IP manual configuration. *)
let ipv4_qubes_conf = impl @@ object
inherit base_configurable
method ty = qubesdb @-> ethernet @-> arpv4 @-> ipv4
method name = Name.create "qubes_ipv4" ~prefix:"qubes_ipv4"
method module_name = "Qubesdb_ipv4.Make"
method! packages = Key.pure [ package ~min:"0.5" "mirage-qubes-ipv4" ]
method! connect _ modname = function
| [ db ; etif; arp ] -> Fmt.strf "%s.connect %s %s %s" modname db etif arp
| _ -> failwith (connect_err "qubes ipv4" 3)
end
let ipv4_qubes db ethernet arp = ipv4_qubes_conf $ db $ ethernet $ arp
let ipv6_conf ?addresses ?netmasks ?gateways () = impl @@ object
inherit base_configurable
method ty = ethernet @-> random @-> time @-> mclock @-> ipv6
method name = Name.create "ipv6" ~prefix:"ipv6"
method module_name = "Ipv6.Make"
method! packages = right_tcpip_library ~min:"3.1.0" ~sublibs:["ipv6"] "tcpip"
method! keys = addresses @?? netmasks @?? gateways @?? []
method! connect _ modname = function
| [ etif ; _random ; _time ; clock ] ->
Fmt.strf "%s.connect@[@ %a@ %a@ %a@ %s@ %s@]"
modname
(opt_key "ip") addresses
(opt_key "netmask") netmasks
(opt_key "gateways") gateways
etif clock
| _ -> failwith (connect_err "ipv6" 3)
end
let create_ipv6
?(random = default_random)
?(time = default_time)
?(clock = default_monotonic_clock)
?group etif { addresses ; netmasks ; gateways } =
let addresses = Key.V6.ips ?group addresses in
let netmasks = Key.V6.netmasks ?group netmasks in
let gateways = Key.V6.gateways ?group gateways in
ipv6_conf ~addresses ~netmasks ~gateways () $ etif $ random $ time $ clock
type 'a icmp = ICMP
type icmpv4 = v4 icmp
let icmp = Type ICMP
let icmpv4: icmpv4 typ = icmp
let icmpv4_direct_conf () = object
inherit base_configurable
method ty : ('a ip -> 'a icmp) typ = ip @-> icmp
method name = "icmpv4"
method module_name = "Icmpv4.Make"
method! packages = right_tcpip_library ~min:"3.0.0" ~sublibs:["icmpv4"] "tcpip"
method! connect _ modname = function
| [ ip ] -> Fmt.strf "%s.connect %s" modname ip
| _ -> failwith (connect_err "icmpv4" 1)
end
let icmpv4_direct_func () = impl (icmpv4_direct_conf ())
let direct_icmpv4 ip = icmpv4_direct_func () $ ip
type 'a udp = UDP
type udpv4 = v4 udp
type udpv6 = v6 udp
let udp = Type UDP
let udpv4: udpv4 typ = udp
let udpv6: udpv6 typ = udp
(* Value restriction ... *)
let udp_direct_conf () = object
inherit base_configurable
method ty = (ip: 'a ip typ) @-> random @-> (udp: 'a udp typ)
method name = "udp"
method module_name = "Udp.Make"
method! packages = right_tcpip_library ~min:"3.0.0" ~sublibs:["udp"] "tcpip"
method! connect _ modname = function
| [ ip; _random ] -> Fmt.strf "%s.connect %s" modname ip
| _ -> failwith (connect_err "udp" 2)
end
(* Value restriction ... *)
let udp_direct_func () = impl (udp_direct_conf ())
let direct_udp ?(random=default_random) ip = udp_direct_func () $ ip $ random
let udpv4_socket_conf ipv4_key = object
inherit base_configurable
method ty = udpv4
val name = Name.create "udpv4_socket" ~prefix:"udpv4_socket"
method name = name
method module_name = "Udpv4_socket"
method! keys = [ Key.abstract ipv4_key ]
method! packages =
Key.(if_ is_unix) [ package ~min:"3.0.0" ~sublibs:["udpv4-socket"; "unix"]
"tcpip" ] []
method! configure i =
match get_target i with
| `Unix | `MacOSX -> R.ok ()
| _ -> R.error_msg "UDPv4 socket not supported on non-UNIX targets."
method! connect _ modname _ = Fmt.strf "%s.connect %a" modname pp_key ipv4_key
end
let socket_udpv4 ?group ip = impl (udpv4_socket_conf @@ Key.V4.socket ?group ip)
type 'a tcp = TCP
type tcpv4 = v4 tcp
type tcpv6 = v6 tcp
let tcp = Type TCP
let tcpv4 : tcpv4 typ = tcp
let tcpv6 : tcpv6 typ = tcp
(* Value restriction ... *)
let tcp_direct_conf () = object
inherit base_configurable
method ty = (ip: 'a ip typ) @-> time @-> mclock @-> random @-> (tcp: 'a tcp typ)
method name = "tcp"
method module_name = "Tcp.Flow.Make"
method! packages = right_tcpip_library ~min:"3.1.0" ~sublibs:["tcp"] "tcpip"
method! connect _ modname = function
| [ip; _time; clock; _random] -> Fmt.strf "%s.connect %s %s" modname ip clock
| _ -> failwith (connect_err "direct tcp" 4)
end
(* Value restriction ... *)
let tcp_direct_func () = impl (tcp_direct_conf ())
let direct_tcp
?(clock=default_monotonic_clock)
?(random=default_random)
?(time=default_time) ip =
tcp_direct_func () $ ip $ time $ clock $ random
let tcpv4_socket_conf ipv4_key = object
inherit base_configurable
method ty = tcpv4
val name = Name.create "tcpv4_socket" ~prefix:"tcpv4_socket"
method name = name
method module_name = "Tcpv4_socket"
method! keys = [ Key.abstract ipv4_key ]
method! packages =
Key.(if_ is_unix) [ package ~min:"3.0.0" ~sublibs:["tcpv4-socket"; "unix"] "tcpip" ] []
method! configure i =
match get_target i with
| `Unix | `MacOSX -> R.ok ()
| _ -> R.error_msg "TCPv4 socket not supported on non-UNIX targets."
method! connect _ modname _ = Fmt.strf "%s.connect %a" modname pp_key ipv4_key
end
let socket_tcpv4 ?group ip = impl (tcpv4_socket_conf @@ Key.V4.socket ?group ip)
type stackv4 = STACKV4
let stackv4 = Type STACKV4
let add_suffix s ~suffix = if suffix = "" then s else s^"_"^suffix
let stackv4_direct_conf ?(group="") () = impl @@ object
inherit base_configurable
method ty =
time @-> random @-> network @->
ethernet @-> arpv4 @-> ipv4 @-> icmpv4 @-> udpv4 @-> tcpv4 @->
stackv4
val name = add_suffix "stackv4_" ~suffix:group
method name = name
method module_name = "Tcpip_stack_direct.Make"
method! packages = Key.pure [ package ~min:"3.0.0" ~sublibs:["stack-direct"] "tcpip" ]
method! connect _i modname = function
| [ _t; _r; interface; ethif; arp; ip; icmp; udp; tcp ] ->
Fmt.strf
"@[<2>let config = {Mirage_stack_lwt.@ \
name = %S;@ \
interface = %s;}@]@ in@ \
%s.connect config@ %s %s %s %s %s %s"
name interface modname ethif arp ip icmp udp tcp
| _ -> failwith (connect_err "direct stackv4" 9)
end
let direct_stackv4
?(clock=default_monotonic_clock)
?(random=default_random)
?(time=default_time)
?group
network eth arp ip =
stackv4_direct_conf ?group ()
$ time $ random $ network
$ eth $ arp $ ip
$ direct_icmpv4 ip
$ direct_udp ~random ip
$ direct_tcp ~clock ~random ~time ip
let dhcp_ipv4_stack ?group ?(time = default_time) ?(arp = arp ?clock:None ?time:None) tap =
let config = dhcp time tap in
let e = etif tap in
let a = arp e in
let i = ipv4_of_dhcp config e a in
direct_stackv4 ?group tap e a i
let static_ipv4_stack ?group ?config ?(arp = arp ?clock:None ?time:None) tap =
let e = etif tap in
let a = arp e in
let i = create_ipv4 ?group ?config e a in
direct_stackv4 ?group tap e a i
let qubes_ipv4_stack ?group ?(qubesdb = default_qubesdb) ?(arp = arp ?clock:None ?time:None) tap =
let e = etif tap in
let a = arp e in
let i = ipv4_qubes qubesdb e a in
direct_stackv4 ?group tap e a i
let stackv4_socket_conf ?(group="") interfaces = impl @@ object
inherit base_configurable
method ty = stackv4
val name = add_suffix "stackv4_socket" ~suffix:group
method name = name
method module_name = "Tcpip_stack_socket"
method! keys = [ Key.abstract interfaces ]
method! packages = Key.pure [ package ~min:"3.0.0" ~sublibs:["stack-socket"; "unix"] "tcpip" ]
method! deps = [abstract (socket_udpv4 None); abstract (socket_tcpv4 None)]
method! connect _i modname = function
| [ udpv4 ; tcpv4 ] ->
Fmt.strf
"let config =@[@ \
{ Mirage_stack_lwt.name = %S;@ \
interface = %a ;}@] in@ \
%s.connect config %s %s"
name pp_key interfaces modname udpv4 tcpv4
| _ -> failwith (connect_err "socket stack" 2)
end
let socket_stackv4 ?group ipv4s =
stackv4_socket_conf ?group (Key.V4.interfaces ?group ipv4s)
(** Generic stack *)
let generic_stackv4
?group ?config
?(dhcp_key = Key.value @@ Key.dhcp ?group ())
?(net_key = Key.value @@ Key.net ?group ())
(tap : network impl) : stackv4 impl =
let eq a b = Key.(pure ((=) a) $ b) in
let choose qubes socket dhcp =
if qubes then `Qubes
else if socket then `Socket
else if dhcp then `Dhcp
else `Static
in
let p = Functoria_key.((pure choose)
$ eq `Qubes Key.(value target)
$ eq `Socket net_key
$ eq true dhcp_key) in
match_impl p [
`Dhcp, dhcp_ipv4_stack ?group tap;
`Socket, socket_stackv4 ?group [Ipaddr.V4.any];
`Qubes, qubes_ipv4_stack ?group tap;
] ~default:(static_ipv4_stack ?config ?group tap)