Skip to content

Commit c5b321c

Browse files
committed
allow multicast addresses for TVU
1 parent de54ce0 commit c5b321c

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

gossip/src/contact_info.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const SOCKET_TAG_TPU_QUIC: u8 = 8;
4343
const SOCKET_TAG_TPU_VOTE: u8 = 9;
4444
const SOCKET_TAG_TPU_VOTE_QUIC: u8 = 12;
4545
const SOCKET_TAG_TVU: u8 = 10;
46+
/// When multicast is enabled, this port has to be used for TVU
47+
/// by all validators to ensure they can actually leverage multicasting
48+
const TVU_MULTICAST_PORT: u16 = 8002;
4649
const SOCKET_TAG_TVU_QUIC: u8 = 11;
4750
const SOCKET_TAG_ALPENGLOW: u8 = 13;
4851
const_assert_eq!(SOCKET_CACHE_SIZE, 14);
@@ -329,7 +332,7 @@ impl ContactInfo {
329332
num_addrs: self.addrs.len(),
330333
})?;
331334
let socket = SocketAddr::new(*addr, port);
332-
sanitize_socket(&socket)?;
335+
sanitize_socket(&socket, key)?;
333336
return Ok(socket);
334337
}
335338
}
@@ -349,7 +352,7 @@ impl ContactInfo {
349352
}
350353

351354
pub fn set_socket(&mut self, key: u8, socket: SocketAddr) -> Result<(), Error> {
352-
sanitize_socket(&socket)?;
355+
sanitize_socket(&socket, key)?;
353356
// Remove the old entry associated with this key (if any).
354357
self.remove_socket(key);
355358
// Find the index at which the new socket entry would be inserted into
@@ -463,7 +466,7 @@ impl ContactInfo {
463466
// Only for tests and simulations.
464467
pub fn new_with_socketaddr(pubkey: &Pubkey, socket: &SocketAddr) -> Self {
465468
use Protocol::{QUIC, UDP};
466-
assert_matches!(sanitize_socket(socket), Ok(()));
469+
assert_matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(()));
467470
let mut node = Self::new(
468471
*pubkey,
469472
solana_time_utils::timestamp(), // wallclock,
@@ -576,7 +579,7 @@ impl TryFrom<ContactInfoLite> for ContactInfo {
576579
continue;
577580
};
578581
let socket = SocketAddr::new(addr, port);
579-
if sanitize_socket(&socket).is_ok() {
582+
if sanitize_socket(&socket, key).is_ok() {
580583
*entry = socket;
581584
}
582585
}
@@ -593,16 +596,21 @@ impl Sanitize for ContactInfo {
593596
}
594597
}
595598

596-
pub(crate) fn sanitize_socket(socket: &SocketAddr) -> Result<(), Error> {
599+
pub(crate) fn sanitize_socket(socket: &SocketAddr, key: u8) -> Result<(), Error> {
597600
if socket.port() == 0u16 {
598601
return Err(Error::InvalidPort(socket.port()));
599602
}
600603
let addr = socket.ip();
601604
if addr.is_unspecified() {
602605
return Err(Error::UnspecifiedIpAddr(addr));
603606
}
607+
604608
if addr.is_multicast() {
605-
return Err(Error::MulticastIpAddr(addr));
609+
if key != SOCKET_TAG_TVU {
610+
return Err(Error::MulticastIpAddr(addr));
611+
} else if socket.port() != TVU_MULTICAST_PORT {
612+
return Err(Error::InvalidPort(socket.port()));
613+
}
606614
}
607615
Ok(())
608616
}
@@ -863,7 +871,7 @@ mod tests {
863871
let addr = addrs.choose(&mut rng).unwrap();
864872
let socket = SocketAddr::new(*addr, new_rand_port(&mut rng));
865873
let key = rng.gen_range(KEYS.start..KEYS.end);
866-
if sanitize_socket(&socket).is_ok() {
874+
if sanitize_socket(&socket, key).is_ok() {
867875
sockets.insert(key, socket);
868876
assert_matches!(node.set_socket(key, socket), Ok(()));
869877
assert_matches!(sanitize_entries(&node.addrs, &node.sockets), Ok(()));
@@ -877,7 +885,7 @@ mod tests {
877885
assert_eq!(
878886
node.cache[usize::from(key)],
879887
socket
880-
.filter(|socket| sanitize_socket(socket).is_ok())
888+
.filter(|socket| sanitize_socket(socket, key).is_ok())
881889
.copied()
882890
.unwrap_or(SOCKET_ADDR_UNSPECIFIED),
883891
);
@@ -1036,7 +1044,7 @@ mod tests {
10361044
fn test_new_with_socketaddr() {
10371045
let mut rng = rand::thread_rng();
10381046
let socket = repeat_with(|| new_rand_socket(&mut rng))
1039-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1047+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10401048
.find(|socket| socket.port().checked_add(11).is_some())
10411049
.unwrap();
10421050
let node = ContactInfo::new_with_socketaddr(&Keypair::new().pubkey(), &socket);
@@ -1047,7 +1055,7 @@ mod tests {
10471055
fn test_sanitize_quic_offset() {
10481056
let mut rng = rand::thread_rng();
10491057
let socket = repeat_with(|| new_rand_socket(&mut rng))
1050-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1058+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10511059
.find(|socket| socket.port().checked_add(QUIC_PORT_OFFSET).is_some())
10521060
.unwrap();
10531061
let mut other = get_quic_socket(&socket).unwrap();
@@ -1079,7 +1087,7 @@ mod tests {
10791087
rng.gen(), // shred_version
10801088
);
10811089
let socket = repeat_with(|| new_rand_socket(&mut rng))
1082-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1090+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10831091
.find(|socket| socket.port().checked_add(QUIC_PORT_OFFSET).is_some())
10841092
.unwrap();
10851093
// TPU socket.

gossip/src/legacy_contact_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! get_socket {
5353
#[cfg(test)]
5454
pub(crate) fn $name(&self) -> Option<SocketAddr> {
5555
let socket = self.$name;
56-
sanitize_socket(&socket).ok()?;
56+
sanitize_socket(&socket, 0).ok()?;
5757
Some(socket)
5858
}
5959
};
@@ -64,7 +64,7 @@ macro_rules! get_socket {
6464
Protocol::QUIC => self.$quic,
6565
Protocol::UDP => self.$name,
6666
};
67-
sanitize_socket(&socket).ok()?;
67+
sanitize_socket(&socket, 0).ok()?;
6868
Some(socket)
6969
}
7070
};
@@ -126,7 +126,7 @@ impl LegacyContactInfo {
126126

127127
pub(crate) fn gossip(&self) -> Option<SocketAddr> {
128128
let socket = self.gossip;
129-
crate::contact_info::sanitize_socket(&socket).ok()?;
129+
crate::contact_info::sanitize_socket(&socket, 0).ok()?;
130130
Some(socket)
131131
}
132132

0 commit comments

Comments
 (0)