Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net-utils: consume port range #620

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions bench-streamer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ 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(
ip_addr,
(port, port + num_sockets as u16),
num_sockets,
)
.unwrap();
let mut port_range = port..port + num_sockets as u16;
let (_port, read_sockets) =
solana_net_utils::multi_bind_in_range(ip_addr, &mut port_range, num_sockets).unwrap();
let stats = Arc::new(StreamerReceiveStats::new("bench-streamer-test"));
for read in read_sockets {
read.set_read_timeout(Some(Duration::new(1, 0))).unwrap();
Expand Down
142 changes: 69 additions & 73 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use {
solana_measure::measure::Measure,
solana_net_utils::{
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,
multi_bind_in_range, PortRange,
},
solana_perf::{
data_budget::DataBudget,
Expand Down Expand Up @@ -2729,8 +2729,9 @@ impl ClusterInfo {
shred_version: u16,
) -> (ContactInfo, UdpSocket, Option<TcpListener>) {
let bind_ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
let mut port_range = VALIDATOR_PORT_RANGE.clone();
let (port, (gossip_socket, ip_echo)) =
Node::get_gossip_port(gossip_addr, VALIDATOR_PORT_RANGE, bind_ip_addr);
Node::get_gossip_port(gossip_addr, &mut port_range, bind_ip_addr);
let contact_info =
Self::gossip_contact_info(id, SocketAddr::new(gossip_addr.ip(), port), shred_version);

Expand All @@ -2743,7 +2744,8 @@ impl ClusterInfo {
shred_version: u16,
) -> (ContactInfo, UdpSocket, Option<TcpListener>) {
let bind_ip_addr = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
let (_, gossip_socket) = bind_in_range(bind_ip_addr, VALIDATOR_PORT_RANGE).unwrap();
let mut port_range = VALIDATOR_PORT_RANGE.clone();
let (_, gossip_socket) = bind_in_range(bind_ip_addr, &mut port_range).unwrap();
let contact_info = Self::gossip_contact_info(id, socketaddr_any!(), shred_version);

(contact_info, gossip_socket, None)
Expand Down Expand Up @@ -2809,22 +2811,25 @@ impl Node {
let localhost_ip_addr = IpAddr::V4(Ipv4Addr::LOCALHOST);
let localhost_bind_addr = format!("{localhost_ip_addr:?}:0");
let unspecified_bind_addr = format!("{:?}:0", IpAddr::V4(Ipv4Addr::UNSPECIFIED));
let port_range = (1024, 65535);
let mut port_range = 1024..65535;

let ((_tpu_port, tpu), (_tpu_quic_port, tpu_quic)) =
bind_two_in_range_with_offset(localhost_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
bind_two_in_range_with_offset(localhost_ip_addr, &mut port_range, QUIC_PORT_OFFSET)
.unwrap();
let (gossip_port, (gossip, ip_echo)) =
bind_common_in_range(localhost_ip_addr, port_range).unwrap();
bind_common_in_range(localhost_ip_addr, &mut port_range).unwrap();
let gossip_addr = SocketAddr::new(localhost_ip_addr, gossip_port);
let tvu = UdpSocket::bind(&localhost_bind_addr).unwrap();
let tvu_quic = UdpSocket::bind(&localhost_bind_addr).unwrap();
let ((_tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) =
bind_two_in_range_with_offset(localhost_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
bind_two_in_range_with_offset(localhost_ip_addr, &mut port_range, QUIC_PORT_OFFSET)
.unwrap();
let tpu_vote = UdpSocket::bind(&localhost_bind_addr).unwrap();
let repair = UdpSocket::bind(&localhost_bind_addr).unwrap();
let rpc_port = find_available_port_in_range(localhost_ip_addr, port_range).unwrap();
let (rpc_port, _) = bind_common_in_range(localhost_ip_addr, &mut port_range).unwrap();
let rpc_addr = SocketAddr::new(localhost_ip_addr, rpc_port);
let rpc_pubsub_port = find_available_port_in_range(localhost_ip_addr, port_range).unwrap();
let (rpc_pubsub_port, _) =
bind_common_in_range(localhost_ip_addr, &mut port_range).unwrap();
let rpc_pubsub_addr = SocketAddr::new(localhost_ip_addr, rpc_pubsub_port);
let broadcast = vec![UdpSocket::bind(&unspecified_bind_addr).unwrap()];
let retransmit_socket = UdpSocket::bind(&unspecified_bind_addr).unwrap();
Expand Down Expand Up @@ -2891,7 +2896,7 @@ impl Node {

fn get_gossip_port(
gossip_addr: &SocketAddr,
port_range: PortRange,
port_range: &mut impl ExactSizeIterator<Item = u16>,
bind_ip_addr: IpAddr,
) -> (u16, (UdpSocket, TcpListener)) {
if gossip_addr.port() != 0 {
Expand All @@ -2905,34 +2910,37 @@ impl Node {
bind_common_in_range(bind_ip_addr, port_range).expect("Failed to bind")
}
}
fn bind(bind_ip_addr: IpAddr, port_range: PortRange) -> (u16, UdpSocket) {
fn bind(
bind_ip_addr: IpAddr,
port_range: &mut impl ExactSizeIterator<Item = u16>,
) -> (u16, UdpSocket) {
bind_in_range(bind_ip_addr, port_range).expect("Failed to bind")
}

pub fn new_single_bind(
pubkey: &Pubkey,
gossip_addr: &SocketAddr,
port_range: PortRange,
mut port_range: PortRange,
bind_ip_addr: IpAddr,
) -> Self {
let (gossip_port, (gossip, ip_echo)) =
Self::get_gossip_port(gossip_addr, port_range, bind_ip_addr);
let (tvu_port, tvu) = Self::bind(bind_ip_addr, port_range);
let (tvu_quic_port, tvu_quic) = Self::bind(bind_ip_addr, port_range);
Self::get_gossip_port(gossip_addr, &mut port_range, bind_ip_addr);
let (tvu_port, tvu) = Self::bind(bind_ip_addr, &mut port_range);
let (tvu_quic_port, tvu_quic) = Self::bind(bind_ip_addr, &mut port_range);
let ((tpu_port, tpu), (_tpu_quic_port, tpu_quic)) =
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
bind_two_in_range_with_offset(bind_ip_addr, &mut port_range, QUIC_PORT_OFFSET).unwrap();
let ((tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) =
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) = Self::bind(bind_ip_addr, port_range);
let (serve_repair_port, serve_repair) = Self::bind(bind_ip_addr, port_range);
let (serve_repair_quic_port, serve_repair_quic) = Self::bind(bind_ip_addr, port_range);
let (_, broadcast) = Self::bind(bind_ip_addr, port_range);
let (_, ancestor_hashes_requests) = Self::bind(bind_ip_addr, port_range);

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();
bind_two_in_range_with_offset(bind_ip_addr, &mut port_range, QUIC_PORT_OFFSET).unwrap();
let (tpu_vote_port, tpu_vote) = Self::bind(bind_ip_addr, &mut port_range);
let (_, retransmit_socket) = Self::bind(bind_ip_addr, &mut port_range);
let (_, repair) = Self::bind(bind_ip_addr, &mut port_range);
let (serve_repair_port, serve_repair) = Self::bind(bind_ip_addr, &mut port_range);
let (serve_repair_quic_port, serve_repair_quic) = Self::bind(bind_ip_addr, &mut port_range);
let (_, broadcast) = Self::bind(bind_ip_addr, &mut port_range);
let (_, ancestor_hashes_requests) = Self::bind(bind_ip_addr, &mut port_range);

let (rpc_port, _) = bind_common_in_range(bind_ip_addr, &mut port_range).unwrap();
let (rpc_pubsub_port, _) = bind_common_in_range(bind_ip_addr, &mut port_range).unwrap();

let addr = gossip_addr.ip();
let mut info = ContactInfo::new(
Expand Down Expand Up @@ -2989,51 +2997,43 @@ impl Node {
pub fn new_with_external_ip(pubkey: &Pubkey, config: NodeConfig) -> Node {
let NodeConfig {
gossip_addr,
port_range,
mut port_range,
bind_ip_addr,
public_tpu_addr,
public_tpu_forwards_addr,
} = config;

let (gossip_port, (gossip, ip_echo)) =
Self::get_gossip_port(&gossip_addr, port_range, bind_ip_addr);
Self::get_gossip_port(&gossip_addr, &mut port_range, bind_ip_addr);

let (tvu_port, tvu_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 8).expect("tvu multi_bind");
let (tvu_quic_port, tvu_quic) = Self::bind(bind_ip_addr, port_range);
multi_bind_in_range(bind_ip_addr, &mut port_range, 8).expect("tvu multi_bind");
let (tvu_quic_port, tvu_quic) = Self::bind(bind_ip_addr, &mut port_range);
let (tpu_port, tpu_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 32).expect("tpu multi_bind");
multi_bind_in_range(bind_ip_addr, &mut port_range, 32).expect("tpu multi_bind");

let (_tpu_port_quic, tpu_quic) = Self::bind(
bind_ip_addr,
(tpu_port + QUIC_PORT_OFFSET, tpu_port + QUIC_PORT_OFFSET + 1),
);
let (_tpu_port_quic, tpu_quic) = Self::bind(bind_ip_addr, &mut port_range);

let (tpu_forwards_port, tpu_forwards_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 8).expect("tpu_forwards multi_bind");
multi_bind_in_range(bind_ip_addr, &mut port_range, 8).expect("tpu_forwards multi_bind");

let (_tpu_forwards_port_quic, tpu_forwards_quic) = Self::bind(
bind_ip_addr,
(
tpu_forwards_port + QUIC_PORT_OFFSET,
tpu_forwards_port + QUIC_PORT_OFFSET + 1,
),
);
let (_tpu_forwards_port_quic, tpu_forwards_quic) =
Self::bind(bind_ip_addr, &mut port_range);

let (tpu_vote_port, tpu_vote_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 1).expect("tpu_vote multi_bind");
multi_bind_in_range(bind_ip_addr, &mut port_range, 1).expect("tpu_vote multi_bind");

let (_, retransmit_sockets) =
multi_bind_in_range(bind_ip_addr, port_range, 8).expect("retransmit multi_bind");
multi_bind_in_range(bind_ip_addr, &mut port_range, 8).expect("retransmit multi_bind");

let (_, repair) = Self::bind(bind_ip_addr, port_range);
let (serve_repair_port, serve_repair) = Self::bind(bind_ip_addr, port_range);
let (serve_repair_quic_port, serve_repair_quic) = Self::bind(bind_ip_addr, port_range);
let (_, repair) = Self::bind(bind_ip_addr, &mut port_range);
let (serve_repair_port, serve_repair) = Self::bind(bind_ip_addr, &mut port_range);
let (serve_repair_quic_port, serve_repair_quic) = Self::bind(bind_ip_addr, &mut port_range);

let (_, broadcast) =
multi_bind_in_range(bind_ip_addr, port_range, 4).expect("broadcast multi_bind");
multi_bind_in_range(bind_ip_addr, &mut port_range, 4).expect("broadcast multi_bind");

let (_, ancestor_hashes_requests) = Self::bind(bind_ip_addr, port_range);
let (_, ancestor_hashes_requests) = Self::bind(bind_ip_addr, &mut port_range);

let mut info = ContactInfo::new(
*pubkey,
Expand Down Expand Up @@ -3570,33 +3570,27 @@ mod tests {
assert!(gossip_crds.get::<&CrdsValue>(&label).is_some());
}

fn assert_in_range(x: u16, range: (u16, u16)) {
assert!(x >= range.0);
assert!(x < range.1);
}

fn check_sockets(sockets: &[UdpSocket], ip: IpAddr, range: (u16, u16)) {
fn check_sockets(sockets: &[UdpSocket], ip: IpAddr) {
assert!(sockets.len() > 1);
let port = sockets[0].local_addr().unwrap().port();
for socket in sockets.iter() {
check_socket(socket, ip, range);
check_socket(socket, ip);
assert_eq!(socket.local_addr().unwrap().port(), port);
}
}

fn check_socket(socket: &UdpSocket, ip: IpAddr, range: (u16, u16)) {
fn check_socket(socket: &UdpSocket, ip: IpAddr) {
let local_addr = socket.local_addr().unwrap();
assert_eq!(local_addr.ip(), ip);
assert_in_range(local_addr.port(), range);
}

fn check_node_sockets(node: &Node, ip: IpAddr, range: (u16, u16)) {
check_socket(&node.sockets.gossip, ip, range);
check_socket(&node.sockets.repair, ip, range);
check_socket(&node.sockets.tvu_quic, ip, range);
fn check_node_sockets(node: &Node, ip: IpAddr) {
check_socket(&node.sockets.gossip, ip);
check_socket(&node.sockets.repair, ip);
check_socket(&node.sockets.tvu_quic, ip);

check_sockets(&node.sockets.tvu, ip, range);
check_sockets(&node.sockets.tpu, ip, range);
check_sockets(&node.sockets.tvu, ip);
check_sockets(&node.sockets.tpu, ip);
}

#[test]
Expand All @@ -3612,19 +3606,21 @@ mod tests {

let node = Node::new_with_external_ip(&solana_sdk::pubkey::new_rand(), config);

check_node_sockets(&node, IpAddr::V4(ip), VALIDATOR_PORT_RANGE);
check_node_sockets(&node, IpAddr::V4(ip));
}

#[test]
fn new_with_external_ip_test_gossip() {
// Can't use VALIDATOR_PORT_RANGE because if this test runs in parallel with others, the
// port returned by `bind_in_range()` might be snatched up before `Node::new_with_external_ip()` runs
let port_range = (
VALIDATOR_PORT_RANGE.1 + MINIMUM_VALIDATOR_PORT_RANGE_WIDTH,
VALIDATOR_PORT_RANGE.1 + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH),
);
let new_start = VALIDATOR_PORT_RANGE.end + MINIMUM_VALIDATOR_PORT_RANGE_WIDTH;
let new_end = VALIDATOR_PORT_RANGE.end + (2 * MINIMUM_VALIDATOR_PORT_RANGE_WIDTH);
let mut port_range = new_start..new_end;

let ip = IpAddr::V4(Ipv4Addr::LOCALHOST);
let port = bind_in_range(ip, port_range).expect("Failed to bind").0;
let port = bind_in_range(ip, &mut port_range)
.expect("Failed to bind")
.0;
let config = NodeConfig {
gossip_addr: socketaddr!(Ipv4Addr::LOCALHOST, port),
port_range,
Expand All @@ -3635,7 +3631,7 @@ mod tests {

let node = Node::new_with_external_ip(&solana_sdk::pubkey::new_rand(), config);

check_node_sockets(&node, ip, port_range);
check_node_sockets(&node, ip);

assert_eq!(node.sockets.gossip.local_addr().unwrap().port(), port);
}
Expand Down
7 changes: 4 additions & 3 deletions gossip/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@ fn process_spy(matches: &ArgMatches, socket_addr_space: SocketAddrSpace) -> std:
let gossip_addr = SocketAddr::new(
gossip_host,
value_t!(matches, "gossip_port", u16).unwrap_or_else(|_| {
solana_net_utils::find_available_port_in_range(
let (gossip_port, _) = solana_net_utils::bind_common_in_range(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
(0, 1),
&mut (0..1),
)
.expect("unable to find an available gossip port")
.expect("unable to find an available gossip port");
gossip_port
}),
);
let discover_timeout = Duration::from_secs(timeout.unwrap_or(u64::MAX));
Expand Down
Loading
Loading