Skip to content

Commit

Permalink
deprecate methods rather than change public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcusack committed Dec 3, 2024
1 parent 34638ee commit 9846357
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bench-streamer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn main() -> Result<()> {
let mut read_channels = Vec::new();
let mut read_threads = Vec::new();
let recycler = PacketBatchRecycler::default();
let (_port, read_sockets) = solana_net_utils::multi_bind_in_range(
let (_port, read_sockets) = solana_net_utils::multi_bind_in_range_with_config(
ip_addr,
(port, port + num_sockets as u16),
SocketConfig::default(),
Expand Down
4 changes: 2 additions & 2 deletions connection-cache/src/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ mod tests {
fn default() -> Self {
Self {
udp_socket: Arc::new(
solana_net_utils::bind_with_any_port(
solana_net_utils::bind_with_any_port_with_config(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
SocketConfig::default(),
)
Expand All @@ -586,7 +586,7 @@ mod tests {
fn new() -> Result<Self, ClientError> {
Ok(Self {
udp_socket: Arc::new(
solana_net_utils::bind_with_any_port(
solana_net_utils::bind_with_any_port_with_config(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
SocketConfig::default(),
)
Expand Down
36 changes: 22 additions & 14 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ use {
solana_ledger::shred::Shred,
solana_measure::measure::Measure,
solana_net_utils::{
bind_common_in_range, bind_common_with_config, bind_in_range, bind_in_range_with_config,
bind_more_with_config, bind_to_localhost, bind_to_unspecified,
bind_common_in_range_with_config, bind_common_with_config, bind_in_range,
bind_in_range_with_config, bind_more_with_config, bind_to_localhost, bind_to_unspecified,
bind_two_in_range_with_offset_and_config, find_available_port_in_range,
multi_bind_in_range, PortRange, SocketConfig, SocketUsage, VALIDATOR_PORT_RANGE,
multi_bind_in_range_with_config, PortRange, SocketConfig, SocketUsage,
VALIDATOR_PORT_RANGE,
},
solana_perf::{
data_budget::DataBudget,
Expand Down Expand Up @@ -2642,7 +2643,8 @@ impl Node {
let tpu_quic =
bind_more_with_config(tpu_quic, num_quic_endpoints, quic_config.clone()).unwrap();
let (gossip_port, (gossip, ip_echo)) =
bind_common_in_range(localhost_ip_addr, port_range, udp_config.clone()).unwrap();
bind_common_in_range_with_config(localhost_ip_addr, port_range, udp_config.clone())
.unwrap();
let gossip_addr = SocketAddr::new(localhost_ip_addr, gossip_port);
let tvu = bind_to_localhost().unwrap();
let tvu_quic = bind_to_localhost().unwrap();
Expand Down Expand Up @@ -2755,7 +2757,8 @@ impl Node {
),
)
} else {
bind_common_in_range(bind_ip_addr, port_range, config).expect("Failed to bind")
bind_common_in_range_with_config(bind_ip_addr, port_range, config)
.expect("Failed to bind")
}
}

Expand Down Expand Up @@ -2925,7 +2928,7 @@ impl Node {
Self::get_gossip_port(&gossip_addr, port_range, bind_ip_addr);

let tvu_config = SocketConfig::default().reuseport(true);
let (tvu_port, tvu_sockets) = multi_bind_in_range(
let (tvu_port, tvu_sockets) = multi_bind_in_range_with_config(
bind_ip_addr,
port_range,
tvu_config.clone(),
Expand All @@ -2941,7 +2944,7 @@ impl Node {
.reuseport(true)
.usage(SocketUsage::ReadOnly);
let (tpu_port, tpu_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, tpu_config.clone(), 32)
multi_bind_in_range_with_config(bind_ip_addr, port_range, tpu_config.clone(), 32)
.expect("tpu multi_bind");

let (_tpu_port_quic, tpu_quic) = Self::bind_with_config(
Expand All @@ -2955,9 +2958,13 @@ impl Node {
let tpu_forwards_config = SocketConfig::default()
.reuseport(true)
.usage(SocketUsage::ReadOnly);
let (tpu_forwards_port, tpu_forwards_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, tpu_forwards_config.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,
tpu_forwards_config.clone(),
8,
)
.expect("tpu_forwards multi_bind");

let (_tpu_forwards_port_quic, tpu_forwards_quic) = Self::bind_with_config(
bind_ip_addr,
Expand All @@ -2978,7 +2985,7 @@ impl Node {
.reuseport(true)
.usage(SocketUsage::ReadOnly);
let (tpu_vote_port, tpu_vote_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, tpu_vote_config.clone(), 1)
multi_bind_in_range_with_config(bind_ip_addr, port_range, tpu_vote_config.clone(), 1)
.expect("tpu_vote multi_bind");

let tpu_vote_config = SocketConfig::default().usage(SocketUsage::ReadOnly);
Expand All @@ -2999,7 +3006,7 @@ impl Node {
.reuseport(true)
.usage(SocketUsage::WriteOnly);
let (_, retransmit_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, retransmit_config, 8)
multi_bind_in_range_with_config(bind_ip_addr, port_range, retransmit_config, 8)
.expect("retransmit multi_bind");

let repair_config = SocketConfig::default();
Expand All @@ -3016,8 +3023,9 @@ impl Node {
let broadcast_config = SocketConfig::default()
.reuseport(true)
.usage(SocketUsage::WriteOnly);
let (_, broadcast) = multi_bind_in_range(bind_ip_addr, port_range, broadcast_config, 4)
.expect("broadcast multi_bind");
let (_, broadcast) =
multi_bind_in_range_with_config(bind_ip_addr, port_range, broadcast_config, 4)
.expect("broadcast multi_bind");

let ancestor_hashes_config = SocketConfig::default();
let (_, ancestor_hashes_requests) =
Expand Down
116 changes: 100 additions & 16 deletions net-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ fn udp_socket_with_config(config: SocketConfig) -> io::Result<Socket> {
Ok(sock)
}

// Find a port in the given range that is available for both TCP and UDP
pub fn bind_common_in_range(
// Find a port in the given range with a socket config that is available for both TCP and UDP
pub fn bind_common_in_range_with_config(
ip_addr: IpAddr,
range: PortRange,
config: SocketConfig,
Expand All @@ -544,6 +544,24 @@ pub fn bind_common_in_range(
))
}

// Find a port in the given range that is available for both TCP and UDP
#[deprecated(since = "2.1.5", note = "use `bind_common_in_range_with_config` instead")]
pub fn bind_common_in_range(
ip_addr: IpAddr,
range: PortRange,
) -> io::Result<(u16, (UdpSocket, TcpListener))> {
for port in range.0..range.1 {
if let Ok((sock, listener)) = bind_common(ip_addr, port) {
return Result::Ok((sock.local_addr().unwrap().port(), (sock, listener)));
}
}

Err(io::Error::new(
io::ErrorKind::Other,
format!("No available TCP/UDP ports in {range:?}"),
))
}

pub fn bind_in_range(ip_addr: IpAddr, range: PortRange) -> io::Result<(u16, UdpSocket)> {
let config = SocketConfig::default();
bind_in_range_with_config(ip_addr, range, config)
Expand Down Expand Up @@ -571,7 +589,10 @@ pub fn bind_in_range_with_config(
))
}

pub fn bind_with_any_port(ip_addr: IpAddr, config: SocketConfig) -> io::Result<UdpSocket> {
pub fn bind_with_any_port_with_config(
ip_addr: IpAddr,
config: SocketConfig,
) -> io::Result<UdpSocket> {
let sock = udp_socket_with_config(config)?;
let addr = SocketAddr::new(ip_addr, 0);
match sock.bind(&SockAddr::from(addr)) {
Expand All @@ -583,12 +604,72 @@ pub fn bind_with_any_port(ip_addr: IpAddr, config: SocketConfig) -> io::Result<U
}
}

#[deprecated(since = "2.1.5", note = "use `bind_with_any_port_with_config` instead")]
pub fn bind_with_any_port(ip_addr: IpAddr) -> io::Result<UdpSocket> {
let sock = udp_socket_with_config(SocketConfig::default())?;
let addr = SocketAddr::new(ip_addr, 0);
match sock.bind(&SockAddr::from(addr)) {
Ok(_) => Result::Ok(sock.into()),
Err(err) => Err(io::Error::new(
io::ErrorKind::Other,
format!("No available UDP port: {err}"),
)),
}
}

// binds many sockets to the same port in a range with config
pub fn multi_bind_in_range(
pub fn multi_bind_in_range_with_config(
ip_addr: IpAddr,
range: PortRange,
config: SocketConfig,
mut num: usize,
) -> io::Result<(u16, Vec<UdpSocket>)> {
if cfg!(windows) && num != 1 {
// See https://github.com/solana-labs/solana/issues/4607
warn!(
"multi_bind_in_range_with_config() only supports 1 socket in windows ({} requested)",
num
);
num = 1;
}
let mut sockets = Vec::with_capacity(num);

const NUM_TRIES: usize = 100;
let mut port = 0;
let mut error = None;
for _ in 0..NUM_TRIES {
port = {
let (port, _) = bind_in_range(ip_addr, range)?;
port
}; // drop the probe, port should be available... briefly.

for _ in 0..num {
let sock = bind_to_with_config(ip_addr, port, config.clone());
if let Ok(sock) = sock {
sockets.push(sock);
} else {
error = Some(sock);
break;
}
}
if sockets.len() == num {
break;
} else {
sockets.clear();
}
}
if sockets.len() != num {
error.unwrap()?;
}
Ok((port, sockets))
}

// binds many sockets to the same port in a range
#[deprecated(since = "2.1.5", note = "use `multi_bind_in_range_with_config` instead")]
pub fn multi_bind_in_range(
ip_addr: IpAddr,
range: PortRange,
mut num: usize,
) -> io::Result<(u16, Vec<UdpSocket>)> {
if cfg!(windows) && num != 1 {
// See https://github.com/solana-labs/solana/issues/4607
Expand All @@ -609,6 +690,7 @@ pub fn multi_bind_in_range(
port
}; // drop the probe, port should be available... briefly.

let config = SocketConfig::default().reuseport(true);
for _ in 0..num {
let sock = bind_to_with_config(ip_addr, port, config.clone());
if let Ok(sock) = sock {
Expand Down Expand Up @@ -922,7 +1004,8 @@ 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(ip_addr, (2010, 2110), config.clone(), 10).unwrap();
let (port, v) =
multi_bind_in_range_with_config(ip_addr, (2010, 2110), config.clone(), 10).unwrap();
for sock in &v {
assert_eq!(port, sock.local_addr().unwrap().port());
}
Expand All @@ -932,8 +1015,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(ip_addr, config.clone()).unwrap();
let y = bind_with_any_port(ip_addr, config.clone()).unwrap();
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();
assert_ne!(
x.local_addr().unwrap().port(),
y.local_addr().unwrap().port()
Expand Down Expand Up @@ -965,10 +1048,11 @@ mod tests {
fn test_bind_common_in_range() {
let ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
let config = SocketConfig::default();
let (port, _sockets) = bind_common_in_range(ip_addr, (3100, 3150), config.clone()).unwrap();
let (port, _sockets) =
bind_common_in_range_with_config(ip_addr, (3100, 3150), config.clone()).unwrap();
assert!((3100..3150).contains(&port));

bind_common_in_range(ip_addr, (port, port + 1), config.clone()).unwrap_err();
bind_common_in_range_with_config(ip_addr, (port, port + 1), config.clone()).unwrap_err();
}

#[test]
Expand All @@ -977,7 +1061,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(ip_addr, (3200, 3250), config).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap();

let _runtime = ip_echo_server(
server_tcp_listener,
Expand All @@ -1000,9 +1084,9 @@ 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(ip_addr, (3200, 3250), config.clone()).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap();
let (client_port, (client_udp_socket, client_tcp_listener)) =
bind_common_in_range(ip_addr, (3200, 3250), config).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap();

let _runtime = ip_echo_server(
server_tcp_listener,
Expand All @@ -1029,14 +1113,14 @@ 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(ip_addr, (3200, 3250), config.clone()).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap();

// make the socket unreachable by not running the ip echo server!

let server_ip_echo_addr = server_udp_socket.local_addr().unwrap();

let (correct_client_port, (_client_udp_socket, client_tcp_listener)) =
bind_common_in_range(ip_addr, (3200, 3250), config).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap();

assert!(!do_verify_reachable_ports(
&server_ip_echo_addr,
Expand All @@ -1053,14 +1137,14 @@ 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(ip_addr, (3200, 3250), config.clone()).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config.clone()).unwrap();

// make the socket unreachable by not running the ip echo server!

let server_ip_echo_addr = server_udp_socket.local_addr().unwrap();

let (_correct_client_port, (client_udp_socket, _client_tcp_listener)) =
bind_common_in_range(ip_addr, (3200, 3250), config).unwrap();
bind_common_in_range_with_config(ip_addr, (3200, 3250), config).unwrap();

assert!(!do_verify_reachable_ports(
&server_ip_echo_addr,
Expand Down
2 changes: 1 addition & 1 deletion udp-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct UdpConfig {

impl NewConnectionConfig for UdpConfig {
fn new() -> Result<Self, ClientError> {
let socket = solana_net_utils::bind_with_any_port(
let socket = solana_net_utils::bind_with_any_port_with_config(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
SocketConfig::default(),
)
Expand Down
2 changes: 1 addition & 1 deletion udp-client/src/nonblocking/udp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod tests {
async fn test_send_from_addr() {
let addr_str = "0.0.0.0:50100";
let addr = addr_str.parse().unwrap();
let socket = solana_net_utils::bind_with_any_port(
let socket = solana_net_utils::bind_with_any_port_with_config(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
SocketConfig::default(),
)
Expand Down

0 comments on commit 9846357

Please sign in to comment.