Skip to content

Commit

Permalink
Fix quic client on TestValidator, alternative (backport #27046) (#27053)
Browse files Browse the repository at this point in the history
Fix quic client on TestValidator, alternative (#27046)

Add new method to enable custom offset

(cherry picked from commit 45c0da8)

Co-authored-by: Tyera Eulberg <tyera@solana.com>
  • Loading branch information
2 people authored and lijunwangs committed Sep 14, 2022
1 parent 118d3bf commit 6cd38e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
17 changes: 9 additions & 8 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use {
solana_ledger::shred::Shred,
solana_measure::measure::Measure,
solana_net_utils::{
bind_common, bind_common_in_range, bind_in_range, bind_two_consecutive_in_range,
bind_common, bind_common_in_range, bind_in_range, bind_two_in_range_with_offset,
find_available_port_in_range, multi_bind_in_range, PortRange,
},
solana_perf::{
Expand Down Expand Up @@ -2740,20 +2740,21 @@ impl Node {
}
pub fn new_localhost_with_pubkey(pubkey: &Pubkey) -> Self {
let bind_ip_addr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
let port_range = (1024, 65535);
let ((_tpu_port, tpu), (_tpu_quic_port, tpu_quic)) =
bind_two_consecutive_in_range(bind_ip_addr, (1024, 65535)).unwrap();
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
let (gossip_port, (gossip, ip_echo)) =
bind_common_in_range(bind_ip_addr, (1024, 65535)).unwrap();
bind_common_in_range(bind_ip_addr, port_range).unwrap();
let gossip_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), gossip_port);
let tvu = UdpSocket::bind("127.0.0.1:0").unwrap();
let tvu_forwards = UdpSocket::bind("127.0.0.1:0").unwrap();
let ((_tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) =
bind_two_consecutive_in_range(bind_ip_addr, (1024, 65535)).unwrap();
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
let tpu_vote = UdpSocket::bind("127.0.0.1:0").unwrap();
let repair = UdpSocket::bind("127.0.0.1:0").unwrap();
let rpc_port = find_available_port_in_range(bind_ip_addr, (1024, 65535)).unwrap();
let rpc_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), rpc_port);
let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, (1024, 65535)).unwrap();
let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
let rpc_pubsub_addr =
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), rpc_pubsub_port);

Expand Down Expand Up @@ -2829,9 +2830,9 @@ impl Node {
let (tvu_port, tvu) = Self::bind(bind_ip_addr, port_range);
let (tvu_forwards_port, tvu_forwards) = Self::bind(bind_ip_addr, port_range);
let ((tpu_port, tpu), (_tpu_quic_port, tpu_quic)) =
bind_two_consecutive_in_range(bind_ip_addr, port_range).unwrap();
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
let ((tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) =
bind_two_consecutive_in_range(bind_ip_addr, port_range).unwrap();
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
let (tpu_vote_port, tpu_vote) = Self::bind(bind_ip_addr, port_range);
let (_, retransmit_socket) = Self::bind(bind_ip_addr, port_range);
let (repair_port, repair) = Self::bind(bind_ip_addr, port_range);
Expand Down
42 changes: 27 additions & 15 deletions net-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,31 +519,34 @@ pub fn bind_common(
.and_then(|_| TcpListener::bind(&addr).map(|listener| (sock.into(), listener)))
}

pub fn bind_two_consecutive_in_range(
pub fn bind_two_in_range_with_offset(
ip_addr: IpAddr,
range: PortRange,
offset: u16,
) -> io::Result<((u16, UdpSocket), (u16, UdpSocket))> {
let mut first: Option<UdpSocket> = None;
if range.1.saturating_sub(range.0) < offset {
return Err(io::Error::new(
io::ErrorKind::Other,
"range too small to find two ports with the correct offset".to_string(),
));
}
for port in range.0..range.1 {
if let Ok(bind) = bind_to(ip_addr, port, false) {
match first {
Some(first_bind) => {
if let Ok(first_bind) = bind_to(ip_addr, port, false) {
if range.1.saturating_sub(port) >= offset {
if let Ok(second_bind) = bind_to(ip_addr, port + offset, false) {
return Ok((
(first_bind.local_addr().unwrap().port(), first_bind),
(bind.local_addr().unwrap().port(), bind),
(second_bind.local_addr().unwrap().port(), second_bind),
));
}
None => {
first = Some(bind);
}
} else {
break;
}
} else {
first = None;
}
}
Err(io::Error::new(
io::ErrorKind::Other,
"couldn't find two consecutive ports in range".to_string(),
"couldn't find two ports with the correct offset in range".to_string(),
))
}

Expand Down Expand Up @@ -818,12 +821,21 @@ mod tests {
}

#[test]
fn test_bind_two_consecutive_in_range() {
fn test_bind_two_in_range_with_offset() {
solana_logger::setup();
let ip_addr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
if let Ok(((port1, _), (port2, _))) = bind_two_consecutive_in_range(ip_addr, (1024, 65535))
let offset = 6;
if let Ok(((port1, _), (port2, _))) =
bind_two_in_range_with_offset(ip_addr, (1024, 65535), offset)
{
assert!(port2 == port1 + offset);
}
let offset = 42;
if let Ok(((port1, _), (port2, _))) =
bind_two_in_range_with_offset(ip_addr, (1024, 65535), offset)
{
assert!(port2 == port1 + 1);
assert!(port2 == port1 + offset);
}
assert!(bind_two_in_range_with_offset(ip_addr, (1024, 1044), offset).is_err());
}
}

0 comments on commit 6cd38e5

Please sign in to comment.