From 608b0eb08eddac3de2f027f8dd203306b7699147 Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Fri, 13 Dec 2024 10:43:18 +0100 Subject: [PATCH] Add gossip target option for p2p peers --- commons/zenoh-config/src/defaults.rs | 9 +++++++ commons/zenoh-config/src/lib.rs | 2 ++ zenoh/src/net/routing/hat/p2p_peer/gossip.rs | 27 +++++++++++++++----- zenoh/src/net/routing/hat/p2p_peer/mod.rs | 6 +++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/commons/zenoh-config/src/defaults.rs b/commons/zenoh-config/src/defaults.rs index 195a08a07..eb000923e 100644 --- a/commons/zenoh-config/src/defaults.rs +++ b/commons/zenoh-config/src/defaults.rs @@ -95,6 +95,15 @@ pub mod scouting { pub mod gossip { pub const enabled: bool = true; pub const multihop: bool = false; + pub mod target { + pub const router: &crate::WhatAmIMatcher = // "router|peer" + &crate::WhatAmIMatcher::empty().router().peer(); + pub const peer: &crate::WhatAmIMatcher = // "router|peer" + &crate::WhatAmIMatcher::empty().router().peer(); + pub const client: &crate::WhatAmIMatcher = // "" + &crate::WhatAmIMatcher::empty(); + mode_accessor!(crate::WhatAmIMatcher); + } pub mod autoconnect { pub const router: &crate::WhatAmIMatcher = // "" &crate::WhatAmIMatcher::empty(); diff --git a/commons/zenoh-config/src/lib.rs b/commons/zenoh-config/src/lib.rs index 30bd9a7c8..a00755c87 100644 --- a/commons/zenoh-config/src/lib.rs +++ b/commons/zenoh-config/src/lib.rs @@ -315,6 +315,8 @@ validated_struct::validator! { /// It mostly makes sense when using "linkstate" routing mode where all nodes in the subsystem don't have /// direct connectivity with each other. multihop: Option, + /// Which type of Zenoh instances to send gossip messages to. + target: Option>, /// Which type of Zenoh instances to automatically establish sessions with upon discovery through gossip. autoconnect: Option>, }, diff --git a/zenoh/src/net/routing/hat/p2p_peer/gossip.rs b/zenoh/src/net/routing/hat/p2p_peer/gossip.rs index b9155b44b..715ab0d1d 100644 --- a/zenoh/src/net/routing/hat/p2p_peer/gossip.rs +++ b/zenoh/src/net/routing/hat/p2p_peer/gossip.rs @@ -96,6 +96,7 @@ pub(super) struct Network { pub(super) router_peers_failover_brokering: bool, pub(super) gossip: bool, pub(super) gossip_multihop: bool, + pub(super) target: WhatAmIMatcher, pub(super) autoconnect: WhatAmIMatcher, pub(super) wait_declares: bool, pub(super) idx: NodeIndex, @@ -113,6 +114,7 @@ impl Network { router_peers_failover_brokering: bool, gossip: bool, gossip_multihop: bool, + target: WhatAmIMatcher, autoconnect: WhatAmIMatcher, wait_declares: bool, ) -> Self { @@ -130,6 +132,7 @@ impl Network { router_peers_failover_brokering, gossip, gossip_multihop, + target, autoconnect, wait_declares, idx, @@ -231,13 +234,18 @@ impl Network { } fn send_on_link(&self, idxs: Vec<(NodeIndex, Details)>, transport: &TransportUnicast) { - if let Ok(msg) = self.make_msg(idxs) { - tracing::trace!("{} Send to {:?} {:?}", self.name, transport.get_zid(), msg); - if let Err(e) = transport.schedule(msg) { - tracing::debug!("{} Error sending LinkStateList: {}", self.name, e); + if transport + .get_whatami() + .is_ok_and(|w| self.target.matches(w)) + { + if let Ok(msg) = self.make_msg(idxs) { + tracing::trace!("{} Send to {:?} {:?}", self.name, transport.get_zid(), msg); + if let Err(e) = transport.schedule(msg) { + tracing::debug!("{} Error sending LinkStateList: {}", self.name, e); + } + } else { + tracing::error!("Failed to encode Linkstate message"); } - } else { - tracing::error!("Failed to encode Linkstate message"); } } @@ -247,7 +255,12 @@ impl Network { { if let Ok(msg) = self.make_msg(idxs) { for link in self.links.values() { - if parameters(link) { + if link + .transport + .get_whatami() + .is_ok_and(|w| self.target.matches(w)) + && parameters(link) + { tracing::trace!("{} Send to {} {:?}", self.name, link.zid, msg); if let Err(e) = link.transport.schedule(msg.clone()) { tracing::debug!("{} Error sending LinkStateList: {}", self.name, e); diff --git a/zenoh/src/net/routing/hat/p2p_peer/mod.rs b/zenoh/src/net/routing/hat/p2p_peer/mod.rs index f50e57ba9..4b5d727b5 100644 --- a/zenoh/src/net/routing/hat/p2p_peer/mod.rs +++ b/zenoh/src/net/routing/hat/p2p_peer/mod.rs @@ -115,6 +115,11 @@ impl HatBaseTrait for HatCode { let whatami = tables.whatami; let gossip = unwrap_or_default!(config.scouting().gossip().enabled()); let gossip_multihop = unwrap_or_default!(config.scouting().gossip().multihop()); + let gossip_target = if gossip { + *unwrap_or_default!(config.scouting().gossip().target().get(whatami)) + } else { + WhatAmIMatcher::empty() + }; let autoconnect = if gossip { *unwrap_or_default!(config.scouting().gossip().autoconnect().get(whatami)) } else { @@ -133,6 +138,7 @@ impl HatBaseTrait for HatCode { router_peers_failover_brokering, gossip, gossip_multihop, + gossip_target, autoconnect, wait_declares, ));