Skip to content

Commit a7cd11f

Browse files
authored
Merge pull request #36 from robur-coop/mc-1
remove mirage-random, use mirage-crypto-rng-mirage >= 1.0.0
2 parents 8d0f4ca + b030034 commit a7cd11f

File tree

15 files changed

+55
-39
lines changed

15 files changed

+55
-39
lines changed

app/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(public_name server)
44
(modules server)
55
(libraries utcp mirage-net-unix cstruct lwt logs ethernet arp.mirage
6-
tcpip.ipv4 mirage-random-test mirage-unix mirage-clock-unix lwt.unix
6+
tcpip.ipv4 mirage-crypto-rng-mirage mirage-unix mirage-clock-unix lwt.unix
77
cmdliner logs.fmt fmt.cli logs.cli fmt.tty mtime.clock.os)
88
(optional))
99

@@ -12,7 +12,7 @@
1212
(public_name single)
1313
(modules single)
1414
(libraries utcp mirage-net-unix cstruct lwt logs ethernet arp.mirage
15-
tcpip.ipv4 mirage-random-test mirage-unix mirage-clock-unix lwt.unix
15+
tcpip.ipv4 mirage-crypto-rng mirage-unix mirage-clock-unix lwt.unix
1616
cmdliner logs.fmt fmt.cli logs.cli fmt.tty mtime.clock.os)
1717
(optional))
1818

app/pcap_replay.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ let initial_packet state ip rng_data now ~src ~dst tcp =
8585
in
8686
if Ipaddr.compare ip src = 0 then begin
8787
(* we're the client - set ISS to match *)
88-
Cstruct.LE.set_uint32 rng_data 0 (Cstruct.BE.get_uint32 tcp 4);
88+
Bytes.set_int32_le rng_data 0 (Cstruct.BE.get_uint32 tcp 4);
8989
let state, flow, _cond, out =
9090
Utcp.connect ~src ~src_port ~dst ~dst_port state now
9191
in
@@ -102,7 +102,7 @@ let initial_packet state ip rng_data now ~src ~dst tcp =
102102
assert (dst_port = Cstruct.BE.get_uint16 tcp' 0);
103103
assert (src_port = Cstruct.BE.get_uint16 tcp' 2);
104104
(* set ISS to the seq in the next segment ;) *)
105-
Cstruct.LE.set_uint32 rng_data 0 (Cstruct.BE.get_uint32 tcp' 4);
105+
Bytes.set_int32_le rng_data 0 (Cstruct.BE.get_uint32 tcp' 4);
106106
(* replay the 0 packet *)
107107
Logs.info (fun m -> m "replay packet 0");
108108
let state, _stuff, out = Utcp.handle_buf state now ~src ~dst tcp in
@@ -116,8 +116,8 @@ let jump () filename ip =
116116
if ip = None then
117117
Logs.warn (fun m -> m "only decoding and printing pcap, no replaying done (specify --ip=<IP> to take an endpoint)");
118118
let fold = pcap_reader filename in
119-
let rng_data = Cstruct.create 4 in
120-
let rng i = assert (i = 4) ; rng_data in
119+
let rng_data = Bytes.make 4 '\000' in
120+
let rng i = assert (i = 4) ; Bytes.unsafe_to_string rng_data in
121121
let state = Utcp.empty Fun.id "pcap-replay" rng in
122122
let flow = ref None in
123123
fold (fun (state, act) idx ts ~src ~dst tcp ->

app/server.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ open Lwt.Infix
22

33
module Ethernet = Ethernet.Make(Netif)
44
module ARP = Arp.Make(Ethernet)(Unix_os.Time)
5-
module IPv4 = Static_ipv4.Make(Mirage_random_test)(Mclock)(Ethernet)(ARP)
5+
module R = Mirage_crypto_rng_mirage.Make(Unix_os.Time)(Mclock)
6+
module IPv4 = Static_ipv4.Make(R)(Mclock)(Ethernet)(ARP)
67

78
let log_err ~pp_error = function
89
| Ok _ -> ()
@@ -26,8 +27,8 @@ let cb ~proto ~src ~dst payload =
2627

2728
let jump () =
2829
Printexc.record_backtrace true;
29-
Mirage_random_test.initialize ();
3030
Lwt_main.run (
31+
R.initialize (module Mirage_crypto_rng.Fortuna) >>= fun () ->
3132
Netif.connect "tap2" >>= fun tap ->
3233
Ethernet.connect tap >>= fun eth ->
3334
ARP.connect eth >>= fun arp ->
@@ -36,7 +37,7 @@ let jump () =
3637
let tcp (*, clo, out *) =
3738
(* let dst = Ipaddr.(V4 (V4.of_string_exn "10.0.42.1")) in *)
3839
let init (*, conn, out *) =
39-
let s = Utcp.empty Fun.id "" Mirage_random_test.generate in
40+
let s = Utcp.empty Fun.id "" R.generate in
4041
let s' = Utcp.start_listen s 23 in
4142
(* Tcp.connect ~src:Ipaddr.(V4 (V4.Prefix.address cidr)) ~dst ~dst_port:1234 s' (Mtime_clock.now ()) *)
4243
s'

app/single.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ open Lwt.Infix
22

33
module Ethernet = Ethernet.Make(Netif)
44
module ARP = Arp.Make(Ethernet)(Unix_os.Time)
5-
module IPv4 = Static_ipv4.Make(Mirage_random_test)(Mclock)(Ethernet)(ARP)
5+
module R = Mirage_crypto_rng_mirage.Make(Unix_os.Time)(Mclock)
6+
module IPv4 = Static_ipv4.Make(R)(Mclock)(Ethernet)(ARP)
67

78
let log_err ~pp_error = function
89
| Ok _ -> ()
@@ -30,8 +31,8 @@ let tcp_cb ~src ~dst payload =
3031

3132
let jump _ src src_port dst dst_port syn fin rst push ack seq window data =
3233
Printexc.record_backtrace true;
33-
Mirage_random_test.initialize ();
3434
Lwt_main.run (
35+
R.initialize (module Mirage_crypto_rng.Fortuna) >>= fun () ->
3536
let cidr = Ipaddr.V4.Prefix.of_string_exn src
3637
and dst = Ipaddr.(V4 (V4.of_string_exn dst))
3738
in

app/trace_replay.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ let jump () filename ip =
198198
if ip = None then
199199
Logs.warn (fun m -> m "only decoding and printing trace, no replaying done (specify --ip=<IP> to take an endpoint)");
200200
let msgs = trace_reader filename in
201-
let rng_data = Cstruct.create 4 in
202-
let rng i = assert (i = 4) ; rng_data in
201+
let rng_data = Bytes.make 4 '\000' in
202+
let rng i = assert (i = 4) ; Bytes.unsafe_to_string rng_data in
203203
let state =
204204
let s = Utcp.empty Fun.id "trace-replay" rng in
205205
Utcp.start_listen s 443
@@ -213,7 +213,7 @@ let jump () filename ip =
213213
|> List.hd
214214
in
215215
let data = Option.get (snd first_out.data) in
216-
Cstruct.LE.set_uint32 rng_data 0
216+
Bytes.set_int32_le rng_data 0
217217
(Cstruct.BE.get_uint32 (Cstruct.of_string data) 4)
218218
in
219219
let _, _ , _ =

mirage/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name utcp_mirage)
33
(public_name utcp.mirage)
4-
(libraries utcp lwt tcpip mirage-random mirage-time mirage-clock))
4+
(libraries utcp lwt tcpip mirage-crypto-rng-mirage mirage-time mirage-clock))

mirage/utcp_mirage.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open Lwt.Infix
33
let src = Logs.Src.create "tcp.mirage" ~doc:"TCP mirage"
44
module Log = (val Logs.src_log src : Logs.LOG)
55

6-
module Make (R : Mirage_random.S) (Mclock : Mirage_clock.MCLOCK) (Time : Mirage_time.S) (Ip : Tcpip.Ip.S with type ipaddr = Ipaddr.t) = struct
6+
module Make (R : Mirage_crypto_rng_mirage.S) (Mclock : Mirage_clock.MCLOCK) (Time : Mirage_time.S) (Ip : Tcpip.Ip.S with type ipaddr = Ipaddr.t) = struct
77

88
let now () = Mtime.of_uint64_ns (Mclock.elapsed_ns ())
99

mirage/utcp_mirage.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
module Make (R : Mirage_random.S) (Mclock : Mirage_clock.MCLOCK) (Time : Mirage_time.S) (Ip : Tcpip.Ip.S with type ipaddr = Ipaddr.t) : sig
2+
module Make (R : Mirage_crypto_rng_mirage.S) (Mclock : Mirage_clock.MCLOCK) (Time : Mirage_time.S) (Ip : Tcpip.Ip.S with type ipaddr = Ipaddr.t) : sig
33
include Tcpip.Tcp.S with type ipaddr = Ip.ipaddr
44

55
val connect : string -> Ip.t -> t

src/input.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let deliver_in_1 mk_notify m stats rng now id seg =
8787
last_ack_sent = ack' ;
8888
t_rttseg }
8989
in
90-
conn_state mk_notify ~rcvbufsize ~sndbufsize Syn_received control_block
90+
conn_state now mk_notify ~rcvbufsize ~sndbufsize Syn_received control_block
9191
in
9292
let reply = Segment.make_syn_ack conn.control_block id in
9393
Log.debug (fun m -> m "%a passive open %a" Connection.pp id (pp_conn_state now) conn);

src/state.ml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,25 @@ type 'a conn_state = {
412412
sndq : Cstruct.t list ; (* reverse list of data to be sent out *)
413413
rcv_notify : 'a;
414414
snd_notify : 'a;
415+
created : Mtime.t;
415416
}
416417

417-
let conn_state mk_notify ~rcvbufsize ~sndbufsize tcp_state control_block = {
418+
let conn_state created mk_notify ~rcvbufsize ~sndbufsize tcp_state control_block = {
418419
tcp_state ; control_block ;
419420
cantrcvmore = false ; cantsndmore = false ;
420421
rcvq = [] ; sndq = [] ;
421422
rcvbufsize ; sndbufsize ;
422423
rcv_notify = mk_notify () ; snd_notify = mk_notify () ;
424+
created ;
423425
}
424426

425427
let pp_conn_state now ppf c =
426-
Fmt.pf ppf "TCP %a cb %a" pp_fsm c.tcp_state (pp_control now) c.control_block
428+
let created_span = Mtime.Span.of_uint64_ns (Mtime.to_uint64_ns c.created) in
429+
Fmt.pf ppf "TCP (since %a) %a cb %a"
430+
Duration.pp
431+
(Mtime.to_uint64_ns
432+
(Option.value ~default:Mtime.min_stamp (Mtime.sub_span now created_span)))
433+
pp_fsm c.tcp_state (pp_control now) c.control_block
427434

428435
module IS = Set.Make(struct type t = int let compare = compare_int end)
429436

@@ -452,13 +459,13 @@ end
452459

453460
(* path mtu (its global to a stack) *)
454461
type 'a t = {
455-
rng : int -> Cstruct.t ;
462+
rng : int -> string ;
456463
listeners : IS.t ;
457464
connections : 'a conn_state CM.t ;
458465
stats : Stats.t ;
459466
id : string ;
460467
mutable ctr : int ;
461-
metrics : (string -> Metrics.field list, 'a conn_state CM.t * Stats.t -> Metrics.data) Metrics.src;
468+
metrics : (string -> Metrics.field list, Mtime.t * 'a conn_state CM.t * Stats.t -> Metrics.data) Metrics.src;
462469
transitions : (string -> Metrics.field list, string -> Metrics.data) Metrics.src;
463470
mk_notify : unit -> 'a;
464471
}
@@ -468,6 +475,9 @@ module States = Map.Make (struct
468475
let compare a b = compare a b
469476
end)
470477

478+
let src = Logs.Src.create "tcp.state" ~doc:"TCP state"
479+
module Log = (val Logs.src_log src : Logs.LOG)
480+
471481
let metrics () =
472482
let tcp_states =
473483
[ Syn_sent ; Syn_received ; Established ; Close_wait ; Fin_wait_1 ;
@@ -476,9 +486,11 @@ let metrics () =
476486
in
477487
let open Metrics in
478488
let doc = "uTCP metrics" in
479-
let data (connections, stats) =
489+
let data (now, connections, stats) =
480490
let rcvq, sndq, states =
481-
CM.fold (fun _ conn (rcvq, sndq, acc) ->
491+
CM.fold (fun k conn (rcvq, sndq, acc) ->
492+
if Mtime.(Span.to_uint64_ns (span now conn.created)) > Duration.of_min 1 then
493+
Log.info (fun m -> m "%a in %a" Connection.pp k (pp_conn_state now) conn);
482494
rcvq + Cstruct.lenv conn.rcvq,
483495
sndq + Cstruct.lenv conn.sndq,
484496
States.update conn.tcp_state (fun v -> Some (succ (Option.value ~default:0 v))) acc)
@@ -502,8 +514,8 @@ let metrics () =
502514
let tag = Tags.string "stack-id" in
503515
Src.v ~doc ~tags:Tags.[ tag ] ~data "utcp"
504516

505-
let add_metrics t =
506-
Metrics.add t.metrics (fun x -> x t.id) (fun d -> d (t.connections, t.stats))
517+
let add_metrics t now =
518+
Metrics.add t.metrics (fun x -> x t.id) (fun d -> d (now, t.connections, t.stats))
507519

508520
let transitions () =
509521
let create () =

0 commit comments

Comments
 (0)