From f5670514e5e51b356b5ea913c484edb79d6e9775 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 13 Dec 2024 22:58:50 +0000 Subject: [PATCH] remove cfg gating and add derive copy --- gossip/src/cluster_info.rs | 149 +++++++++++++++---------------------- net-utils/src/lib.rs | 55 +++++--------- 2 files changed, 78 insertions(+), 126 deletions(-) diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index 8e0acf18e6930a..5f1945b83b8465 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -2619,15 +2619,13 @@ impl Node { localhost_ip_addr, port_range, QUIC_PORT_OFFSET, - udp_config.clone(), - quic_config.clone(), + udp_config, + quic_config, ) .unwrap(); - let tpu_quic = - bind_more_with_config(tpu_quic, num_quic_endpoints, quic_config.clone()).unwrap(); + let tpu_quic = bind_more_with_config(tpu_quic, num_quic_endpoints, quic_config).unwrap(); let (gossip_port, (gossip, ip_echo)) = - bind_common_in_range_with_config(localhost_ip_addr, port_range, udp_config.clone()) - .unwrap(); + bind_common_in_range_with_config(localhost_ip_addr, port_range, udp_config).unwrap(); let gossip_addr = SocketAddr::new(localhost_ip_addr, gossip_port); let tvu = bind_to_localhost().unwrap(); let tvu_quic = bind_to_localhost().unwrap(); @@ -2636,13 +2634,12 @@ impl Node { localhost_ip_addr, port_range, QUIC_PORT_OFFSET, - udp_config.clone(), - quic_config.clone(), + udp_config, + quic_config, ) .unwrap(); let tpu_forwards_quic = - bind_more_with_config(tpu_forwards_quic, num_quic_endpoints, quic_config.clone()) - .unwrap(); + bind_more_with_config(tpu_forwards_quic, num_quic_endpoints, quic_config).unwrap(); let tpu_vote = bind_to_localhost().unwrap(); let tpu_vote_quic = bind_to_localhost().unwrap(); let tpu_vote_quic = @@ -2764,46 +2761,42 @@ impl Node { let socket_config = SocketConfig::default(); let socket_config_reuseport = SocketConfig::default().reuseport(true); - let (tvu_port, tvu) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + let (tvu_port, tvu) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (tvu_quic_port, tvu_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let ((tpu_port, tpu), (_tpu_quic_port, tpu_quic)) = bind_two_in_range_with_offset_and_config( bind_ip_addr, port_range, QUIC_PORT_OFFSET, - socket_config.clone(), - socket_config_reuseport.clone(), + socket_config, + socket_config_reuseport, ) .unwrap(); - let tpu_quic: Vec = bind_more_with_config( - tpu_quic, - DEFAULT_QUIC_ENDPOINTS, - socket_config_reuseport.clone(), - ) - .unwrap(); + let tpu_quic: Vec = + bind_more_with_config(tpu_quic, DEFAULT_QUIC_ENDPOINTS, socket_config_reuseport) + .unwrap(); let ((tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) = bind_two_in_range_with_offset_and_config( bind_ip_addr, port_range, QUIC_PORT_OFFSET, - socket_config.clone(), - socket_config_reuseport.clone(), + socket_config, + socket_config_reuseport, ) .unwrap(); let tpu_forwards_quic = bind_more_with_config( tpu_forwards_quic, DEFAULT_QUIC_ENDPOINTS, - socket_config_reuseport.clone(), + socket_config_reuseport, ) .unwrap(); let (tpu_vote_port, tpu_vote) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (tpu_vote_quic_port, tpu_vote_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let tpu_vote_quic: Vec = bind_more_with_config( tpu_vote_quic, DEFAULT_QUIC_ENDPOINTS, @@ -2812,20 +2805,18 @@ impl Node { .unwrap(); let (_, retransmit_socket) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); - let (_, repair) = Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); - let (_, repair_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); + let (_, repair) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); + let (_, repair_quic) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (serve_repair_port, serve_repair) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (serve_repair_quic_port, serve_repair_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); - let (_, broadcast) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); + let (_, broadcast) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (_, ancestor_hashes_requests) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (_, ancestor_hashes_requests_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let rpc_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap(); let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap(); @@ -2907,41 +2898,30 @@ impl Node { let (tvu_port, tvu_sockets) = multi_bind_in_range_with_config( bind_ip_addr, port_range, - socket_config_reuseport.clone(), + socket_config_reuseport, num_tvu_sockets.get(), ) .expect("tvu multi_bind"); let (tvu_quic_port, tvu_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); - let (tpu_port, tpu_sockets) = multi_bind_in_range_with_config( - bind_ip_addr, - port_range, - socket_config_reuseport.clone(), - 32, - ) - .expect("tpu multi_bind"); + let (tpu_port, tpu_sockets) = + multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 32) + .expect("tpu multi_bind"); let (_tpu_port_quic, tpu_quic) = Self::bind_with_config( bind_ip_addr, (tpu_port + QUIC_PORT_OFFSET, tpu_port + QUIC_PORT_OFFSET + 1), - socket_config_reuseport.clone(), + socket_config_reuseport, ); - let tpu_quic = bind_more_with_config( - tpu_quic, - num_quic_endpoints.get(), - socket_config_reuseport.clone(), - ) - .unwrap(); + let tpu_quic = + bind_more_with_config(tpu_quic, num_quic_endpoints.get(), socket_config_reuseport) + .unwrap(); - let (tpu_forwards_port, tpu_forwards_sockets) = multi_bind_in_range_with_config( - bind_ip_addr, - port_range, - socket_config_reuseport.clone(), - 8, - ) - .expect("tpu_forwards multi_bind"); + let (tpu_forwards_port, tpu_forwards_sockets) = + multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 8) + .expect("tpu_forwards multi_bind"); let (_tpu_forwards_port_quic, tpu_forwards_quic) = Self::bind_with_config( bind_ip_addr, @@ -2949,62 +2929,49 @@ impl Node { tpu_forwards_port + QUIC_PORT_OFFSET, tpu_forwards_port + QUIC_PORT_OFFSET + 1, ), - socket_config_reuseport.clone(), + socket_config_reuseport, ); let tpu_forwards_quic = bind_more_with_config( tpu_forwards_quic, num_quic_endpoints.get(), - socket_config_reuseport.clone(), + socket_config_reuseport, ) .unwrap(); - let (tpu_vote_port, tpu_vote_sockets) = multi_bind_in_range_with_config( - bind_ip_addr, - port_range, - socket_config_reuseport.clone(), - 1, - ) - .expect("tpu_vote multi_bind"); + let (tpu_vote_port, tpu_vote_sockets) = + multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 1) + .expect("tpu_vote multi_bind"); let (tpu_vote_quic_port, tpu_vote_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let tpu_vote_quic = bind_more_with_config( tpu_vote_quic, num_quic_endpoints.get(), - socket_config_reuseport.clone(), + socket_config_reuseport, ) .unwrap(); - let (_, retransmit_sockets) = multi_bind_in_range_with_config( - bind_ip_addr, - port_range, - socket_config_reuseport.clone(), - 8, - ) - .expect("retransmit multi_bind"); + let (_, retransmit_sockets) = + multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 8) + .expect("retransmit multi_bind"); - let (_, repair) = Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); - let (_, repair_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + let (_, repair) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); + let (_, repair_quic) = Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (serve_repair_port, serve_repair) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (serve_repair_quic_port, serve_repair_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); - let (_, broadcast) = multi_bind_in_range_with_config( - bind_ip_addr, - port_range, - socket_config_reuseport.clone(), - 4, - ) - .expect("broadcast multi_bind"); + let (_, broadcast) = + multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 4) + .expect("broadcast multi_bind"); let (_, ancestor_hashes_requests) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let (_, ancestor_hashes_requests_quic) = - Self::bind_with_config(bind_ip_addr, port_range, socket_config.clone()); + Self::bind_with_config(bind_ip_addr, port_range, socket_config); let mut info = ContactInfo::new( *pubkey, diff --git a/net-utils/src/lib.rs b/net-utils/src/lib.rs index 3e032c53b6fae9..d4d89abf097686 100644 --- a/net-utils/src/lib.rs +++ b/net-utils/src/lib.rs @@ -386,12 +386,10 @@ pub fn is_host_port(string: String) -> Result<(), String> { parse_host_port(&string).map(|_| ()) } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Copy, Debug, Default)] pub struct SocketConfig { reuseport: bool, - #[cfg(not(any(windows, target_os = "ios")))] recv_buffer_size: Option, - #[cfg(not(any(windows, target_os = "ios")))] send_buffer_size: Option, } @@ -401,35 +399,25 @@ impl SocketConfig { self } - /// Sets the receive buffer size for the socket. + /// Sets the receive buffer size for the socket (no effect on windows/ios). /// /// **Note:** On Linux the kernel will double the value you specify. /// For example, if you specify `16MB`, the kernel will configure the /// socket to use `32MB`. /// See: https://man7.org/linux/man-pages/man7/socket.7.html: SO_RCVBUF - // allow here to supress unused warnings from windows/ios builds - #[allow(unused_mut, unused_variables)] pub fn recv_buffer_size(mut self, size: usize) -> Self { - #[cfg(not(any(windows, target_os = "ios")))] - { - self.recv_buffer_size = Some(size); - } + self.recv_buffer_size = Some(size); self } - /// Sets the send buffer size for the socket. + /// Sets the send buffer size for the socket (no effect on windows/ios) /// /// **Note:** On Linux the kernel will double the value you specify. /// For example, if you specify `16MB`, the kernel will configure the /// socket to use `32MB`. /// See: https://man7.org/linux/man-pages/man7/socket.7.html: SO_SNDBUF - // allow here to supress unused warnings from windows/ios builds - #[allow(unused_mut, unused_variables)] pub fn send_buffer_size(mut self, size: usize) -> Self { - #[cfg(not(any(windows, target_os = "ios")))] - { - self.send_buffer_size = Some(size); - } + self.send_buffer_size = Some(size); self } } @@ -474,7 +462,7 @@ pub fn bind_common_in_range_with_config( config: SocketConfig, ) -> io::Result<(u16, (UdpSocket, TcpListener))> { for port in range.0..range.1 { - if let Ok((sock, listener)) = bind_common_with_config(ip_addr, port, config.clone()) { + if let Ok((sock, listener)) = bind_common_with_config(ip_addr, port, config) { return Result::Ok((sock.local_addr().unwrap().port(), (sock, listener))); } } @@ -577,7 +565,7 @@ pub fn multi_bind_in_range_with_config( }; // drop the probe, port should be available... briefly. for _ in 0..num { - let sock = bind_to_with_config(ip_addr, port, config.clone()); + let sock = bind_to_with_config(ip_addr, port, config); if let Ok(sock) = sock { sockets.push(sock); } else { @@ -735,11 +723,9 @@ pub fn bind_two_in_range_with_offset_and_config( )); } for port in range.0..range.1 { - if let Ok(first_bind) = bind_to_with_config(ip_addr, port, sock1_config.clone()) { + if let Ok(first_bind) = bind_to_with_config(ip_addr, port, sock1_config) { if range.1.saturating_sub(port) >= offset { - if let Ok(second_bind) = - bind_to_with_config(ip_addr, port + offset, sock2_config.clone()) - { + if let Ok(second_bind) = bind_to_with_config(ip_addr, port + offset, sock2_config) { return Ok(( (first_bind.local_addr().unwrap().port(), first_bind), (second_bind.local_addr().unwrap().port(), second_bind), @@ -788,7 +774,7 @@ pub fn bind_more_with_config( let ip = addr.ip(); let port = addr.port(); std::iter::once(Ok(socket)) - .chain((1..num).map(|_| bind_to_with_config(ip, port, config.clone()))) + .chain((1..num).map(|_| bind_to_with_config(ip, port, config))) .collect() } @@ -896,8 +882,8 @@ mod tests { assert_eq!(bind_in_range(ip_addr, (2000, 2001)).unwrap().0, 2000); let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default().reuseport(true); - let x = bind_to_with_config(ip_addr, 2002, config.clone()).unwrap(); - let y = bind_to_with_config(ip_addr, 2002, config.clone()).unwrap(); + let x = bind_to_with_config(ip_addr, 2002, config).unwrap(); + let y = bind_to_with_config(ip_addr, 2002, config).unwrap(); assert_eq!( x.local_addr().unwrap().port(), y.local_addr().unwrap().port() @@ -905,8 +891,7 @@ mod tests { bind_to(ip_addr, 2002, false).unwrap_err(); bind_in_range(ip_addr, (2002, 2003)).unwrap_err(); - let (port, v) = - multi_bind_in_range_with_config(ip_addr, (2010, 2110), config.clone(), 10).unwrap(); + let (port, v) = multi_bind_in_range_with_config(ip_addr, (2010, 2110), config, 10).unwrap(); for sock in &v { assert_eq!(port, sock.local_addr().unwrap().port()); } @@ -916,8 +901,8 @@ mod tests { fn test_bind_with_any_port() { let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default(); - let x = bind_with_any_port_with_config(ip_addr, config.clone()).unwrap(); - let y = bind_with_any_port_with_config(ip_addr, config.clone()).unwrap(); + let x = bind_with_any_port_with_config(ip_addr, config).unwrap(); + let y = bind_with_any_port_with_config(ip_addr, config).unwrap(); assert_ne!( x.local_addr().unwrap().port(), y.local_addr().unwrap().port() @@ -950,10 +935,10 @@ mod tests { let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default(); let (port, _sockets) = - bind_common_in_range_with_config(ip_addr, (3100, 3150), config.clone()).unwrap(); + bind_common_in_range_with_config(ip_addr, (3100, 3150), config).unwrap(); assert!((3100..3150).contains(&port)); - bind_common_in_range_with_config(ip_addr, (port, port + 1), config.clone()).unwrap_err(); + bind_common_in_range_with_config(ip_addr, (port, port + 1), config).unwrap_err(); } #[test] @@ -985,7 +970,7 @@ mod tests { let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default(); let (_server_port, (server_udp_socket, server_tcp_listener)) = - bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap(); + bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap(); let (client_port, (client_udp_socket, client_tcp_listener)) = bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap(); @@ -1014,7 +999,7 @@ mod tests { let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default(); let (_server_port, (server_udp_socket, _server_tcp_listener)) = - bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap(); + bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap(); // make the socket unreachable by not running the ip echo server! @@ -1038,7 +1023,7 @@ mod tests { let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); let config = SocketConfig::default(); let (_server_port, (server_udp_socket, _server_tcp_listener)) = - bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap(); + bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap(); // make the socket unreachable by not running the ip echo server!