Skip to content

Commit b8cce09

Browse files
committed
Allow switch port timeout to be configured
Previously we would timeout switch ports after 300s of inactivity. Ideally active connections would be configured to send keep-alive packets sooner than this. To better support cases with no keep-alives, it's useful to be able to reconfigure the idle timeout (e.g. to make it much bigger). This patch adds - a command-line argument `--port-max-idle-time` - a database key `slirp/port-max-idle-time` which allow the default 300s to be overriden. Related to moby#235 Related to moby#234 Signed-off-by: David Scott <[email protected]>
1 parent 3dd816d commit b8cce09

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

Diff for: src/bin/main.ml

+10-5
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ let hvsock_addr_of_uri ~default_serviceid uri =
194194
let main_t
195195
socket_url port_control_url introspection_url diagnostics_url
196196
max_connections vsock_path db_path db_branch dns hosts host_names
197-
listen_backlog debug
197+
listen_backlog port_max_idle_time debug
198198
=
199199
(* Write to stdout if expicitly requested [debug = true] or if the
200200
environment variable DEBUG is set *)
@@ -304,7 +304,8 @@ let hvsock_addr_of_uri ~default_serviceid uri =
304304
vnet_switch;
305305
mtu = 1500;
306306
host_names;
307-
clock }
307+
clock;
308+
port_max_idle_time }
308309
in
309310

310311
let config = match db_path with
@@ -374,12 +375,12 @@ let hvsock_addr_of_uri ~default_serviceid uri =
374375
let main
375376
socket_url port_control_url introspection_url diagnostics_url
376377
max_connections vsock_path db_path db_branch dns hosts host_names
377-
listen_backlog debug
378+
listen_backlog port_max_idle_time debug
378379
=
379380
Host.Main.run
380381
(main_t socket_url port_control_url introspection_url diagnostics_url
381382
max_connections vsock_path db_path db_branch dns hosts host_names
382-
listen_backlog debug)
383+
listen_backlog port_max_idle_time debug)
383384

384385
open Cmdliner
385386

@@ -501,6 +502,10 @@ let listen_backlog =
501502
then we will use SOMAXCONN." in
502503
Arg.(value & opt (some int) None & info [ "listen-backlog" ] ~doc)
503504

505+
let port_max_idle_time =
506+
let doc = "Idle time to wait before timing out and disconnecting switch ports." in
507+
Arg.(value & opt int 30 & info [ "port-max-idle-time" ] ~doc)
508+
504509
let debug =
505510
let doc = "Verbose debug logging to stdout" in
506511
Arg.(value & flag & info [ "debug" ] ~doc)
@@ -515,7 +520,7 @@ let command =
515520
Term.(pure main
516521
$ socket $ port_control_path $ introspection_path $ diagnostics_path
517522
$ max_connections $ vsock_path $ db_path $ db_branch $ dns $ hosts
518-
$ host_names $ listen_backlog $ debug),
523+
$ host_names $ listen_backlog $ port_max_idle_time $ debug),
519524
Term.info (Filename.basename Sys.argv.(0)) ~version:Depends.version ~doc ~man
520525

521526
let () =

Diff for: src/hostnet/slirp.ml

+16-9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type ('a, 'b) config = {
8686
mtu: int;
8787
host_names: Dns.Name.t list;
8888
clock: 'a;
89+
port_max_idle_time: int;
8990
}
9091

9192
module Make
@@ -794,17 +795,17 @@ struct
794795
Lwt.return (Ok ())
795796
end
796797

797-
(* If no traffic is received for 5 minutes, delete the endpoint and
798+
(* If no traffic is received for `port_max_idle_time`, delete the endpoint and
798799
the switch port. *)
799-
let rec delete_unused_endpoints t () =
800+
let rec delete_unused_endpoints t ~port_max_idle_time () =
800801
Host.Time.sleep_ns (Duration.of_sec 30)
801802
>>= fun () ->
802803
Lwt_mutex.with_lock t.endpoints_m
803804
(fun () ->
804805
let now = Unix.gettimeofday () in
805806
let old_ips = IPMap.fold (fun ip endpoint acc ->
806807
let age = now -. endpoint.Endpoint.last_active_time in
807-
if age > 300.0 then ip :: acc else acc
808+
if age > (float_of_int port_max_idle_time) then ip :: acc else acc
808809
) t.endpoints [] in
809810
List.iter (fun ip ->
810811
Switch.remove t.switch ip;
@@ -813,11 +814,11 @@ struct
813814
Lwt.return_unit
814815
)
815816
>>= fun () ->
816-
delete_unused_endpoints t ()
817+
delete_unused_endpoints t ~port_max_idle_time ()
817818

818819
let connect x vnet_switch vnet_client_id client_macaddr server_macaddr peer_ip
819820
local_ip highest_ip extra_dns_ip mtu get_domain_search get_domain_name
820-
(global_arp_table:arp_table) clock
821+
(global_arp_table:arp_table) clock port_max_idle_time
821822
=
822823

823824
let valid_subnets = [ Ipaddr.V4.Prefix.global ] in
@@ -871,7 +872,7 @@ struct
871872
udp_nat;
872873
icmp_nat;
873874
} in
874-
Lwt.async @@ delete_unused_endpoints t;
875+
Lwt.async @@ delete_unused_endpoints ~port_max_idle_time t;
875876

876877
let find_endpoint ip =
877878
Lwt_mutex.with_lock t.endpoints_m
@@ -1342,11 +1343,16 @@ struct
13421343
log_exception_continue "monitor http interception settings" (fun () ->
13431344
monitor_http_intercept_settings http_intercept_settings));
13441345

1346+
let port_max_idle_time_path = driver @ [ "slirp"; "port-max-idle-time" ] in
1347+
Config.int config ~default:300 port_max_idle_time_path
1348+
>>= fun port_max_idle_times ->
1349+
let port_max_idle_time = Active_config.hd port_max_idle_times in
1350+
13451351
Log.info (fun f ->
13461352
f "Creating slirp server peer_ip:%s local_ip:%s domain_search:%s \
1347-
mtu:%d"
1353+
mtu:%d port_max_idle_time:%d"
13481354
(Ipaddr.V4.to_string peer_ip) (Ipaddr.V4.to_string local_ip)
1349-
(String.concat " " !domain_search) mtu
1355+
(String.concat " " !domain_search) mtu port_max_idle_time
13501356
);
13511357

13521358
let global_arp_table : arp_table = {
@@ -1371,6 +1377,7 @@ struct
13711377
mtu;
13721378
host_names;
13731379
clock;
1380+
port_max_idle_time;
13741381
} in
13751382
Lwt.return t
13761383

@@ -1484,7 +1491,7 @@ struct
14841491
connect x t.vnet_switch vnet_client_id client_macaddr t.server_macaddr
14851492
client_ip t.local_ip t.highest_ip t.extra_dns_ip t.mtu
14861493
t.get_domain_search t.get_domain_name t.global_arp_table
1487-
t.clock
1494+
t.clock t.port_max_idle_time
14881495
end
14891496

14901497
end

Diff for: src/hostnet/slirp.mli

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ('clock, 'vnet_switch) config = {
3131
mtu: int;
3232
host_names: Dns.Name.t list;
3333
clock: 'clock;
34+
port_max_idle_time: int;
3435
}
3536

3637
module Make

Diff for: src/hostnet_test/slirp_stack.ml

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ let config =
169169
mtu = 1500;
170170
host_names = [];
171171
clock;
172+
port_max_idle_time = 300;
172173
}
173174

174175
(* This is a hacky way to get a hancle to the server side of the stack. *)

0 commit comments

Comments
 (0)