@@ -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
9192module Make
@@ -794,30 +795,34 @@ 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- Host.Time. sleep_ns (Duration. of_sec 30 )
801- >> = fun () ->
802- Lwt_mutex. with_lock t.endpoints_m
803- (fun () ->
804- let now = Unix. gettimeofday () in
805- let old_ips = IPMap. fold (fun ip endpoint acc ->
806- let age = now -. endpoint.Endpoint. last_active_time in
807- if age > 300.0 then ip :: acc else acc
808- ) t.endpoints [] in
809- List. iter (fun ip ->
810- Switch. remove t.switch ip;
811- t.endpoints < - IPMap. remove ip t.endpoints
812- ) old_ips;
813- Lwt. return_unit
814- )
815- >> = fun () ->
816- delete_unused_endpoints t ()
800+ let rec delete_unused_endpoints t ~port_max_idle_time () =
801+ if port_max_idle_time < = 0
802+ then Lwt. return_unit (* never delete a port *)
803+ else begin
804+ Host.Time. sleep_ns (Duration. of_sec 30 )
805+ >> = fun () ->
806+ Lwt_mutex. with_lock t.endpoints_m
807+ (fun () ->
808+ let now = Unix. gettimeofday () in
809+ let old_ips = IPMap. fold (fun ip endpoint acc ->
810+ let age = now -. endpoint.Endpoint. last_active_time in
811+ if age > (float_of_int port_max_idle_time) then ip :: acc else acc
812+ ) t.endpoints [] in
813+ List. iter (fun ip ->
814+ Switch. remove t.switch ip;
815+ t.endpoints < - IPMap. remove ip t.endpoints
816+ ) old_ips;
817+ Lwt. return_unit
818+ )
819+ >> = fun () ->
820+ delete_unused_endpoints t ~port_max_idle_time ()
821+ end
817822
818823 let connect x vnet_switch vnet_client_id client_macaddr server_macaddr peer_ip
819824 local_ip highest_ip extra_dns_ip mtu get_domain_search get_domain_name
820- (global_arp_table :arp_table ) clock
825+ (global_arp_table :arp_table ) clock port_max_idle_time
821826 =
822827
823828 let valid_subnets = [ Ipaddr.V4.Prefix. global ] in
@@ -871,7 +876,7 @@ struct
871876 udp_nat;
872877 icmp_nat;
873878 } in
874- Lwt. async @@ delete_unused_endpoints t;
879+ Lwt. async @@ delete_unused_endpoints ~port_max_idle_time t;
875880
876881 let find_endpoint ip =
877882 Lwt_mutex. with_lock t.endpoints_m
@@ -1342,11 +1347,16 @@ struct
13421347 log_exception_continue " monitor http interception settings" (fun () ->
13431348 monitor_http_intercept_settings http_intercept_settings));
13441349
1350+ let port_max_idle_time_path = driver @ [ " slirp" ; " port-max-idle-time" ] in
1351+ Config. int config ~default: 300 port_max_idle_time_path
1352+ >> = fun port_max_idle_times ->
1353+ let port_max_idle_time = Active_config. hd port_max_idle_times in
1354+
13451355 Log. info (fun f ->
13461356 f " Creating slirp server peer_ip:%s local_ip:%s domain_search:%s \
1347- mtu:%d"
1357+ mtu:%d port_max_idle_time:%d "
13481358 (Ipaddr.V4. to_string peer_ip) (Ipaddr.V4. to_string local_ip)
1349- (String. concat " " ! domain_search) mtu
1359+ (String. concat " " ! domain_search) mtu port_max_idle_time
13501360 );
13511361
13521362 let global_arp_table : arp_table = {
@@ -1371,6 +1381,7 @@ struct
13711381 mtu;
13721382 host_names;
13731383 clock;
1384+ port_max_idle_time;
13741385 } in
13751386 Lwt. return t
13761387
@@ -1484,7 +1495,7 @@ struct
14841495 connect x t.vnet_switch vnet_client_id client_macaddr t.server_macaddr
14851496 client_ip t.local_ip t.highest_ip t.extra_dns_ip t.mtu
14861497 t.get_domain_search t.get_domain_name t.global_arp_table
1487- t.clock
1498+ t.clock t.port_max_idle_time
14881499 end
14891500
14901501end
0 commit comments