Skip to content

Commit

Permalink
Add component trace to ping packet
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-grz committed Feb 10, 2025
1 parent 7fcb687 commit 18632ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion crates/telio-nurse/src/qos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Analytics {
let (ping_channel_tx, ping_channel_rx) = mpsc::channel(1);

let ping_backend = if config.rtt_types.contains(&RttType::Ping) {
Arc::new(Pinger::new(config.rtt_tries, ipv6_enabled, socket_pool).ok())
Arc::new(Pinger::new(config.rtt_tries, ipv6_enabled, socket_pool, Some("qos_rtt")).ok())
} else {
Arc::new(None)
};
Expand Down Expand Up @@ -467,6 +467,7 @@ impl Analytics {
_ => {}
}

telio_log_trace!("Performing ping {:?}", dpt);
dpr = Box::pin(pinger.perform(dpt)).await;

if let Some(results_v4) = &dpr.v4 {
Expand Down
24 changes: 18 additions & 6 deletions crates/telio-pinger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Pinger {
/// Number of tries
pub no_of_tries: u32,
socket_pool: Arc<SocketPool>,
/// data of the ping, used for tracking in pcaps
payload_data: Option<String>,
}

/// Information gathered after a ping action
Expand Down Expand Up @@ -54,10 +56,12 @@ impl Pinger {
/// * `no_of_tries` - How many pings should be sent.
/// * `ipv6` - Enable IPv6 support.
/// * `socket_pool` - Optional SocketPool used to protect the sockets.
/// * `payload_data` - Optional payload used to debug the ping packets.
pub fn new(
no_of_tries: u32,
ipv6: bool,
socket_pool: Arc<SocketPool>,
payload_data: Option<&str>,
) -> std::io::Result<Self> {
let client_v6 = if ipv6 {
let client_v6 = Arc::new(Self::build_client(ICMP::V6)?);
Expand All @@ -77,6 +81,7 @@ impl Pinger {
client_v6,
no_of_tries,
socket_pool,
payload_data: payload_data.map(|s| s.to_string()),
})
}

Expand Down Expand Up @@ -155,6 +160,16 @@ impl Pinger {
pinger.timeout(Duration::from_millis(10));

let mut sum = Duration::default();
let mut payload = [0; PING_PAYLOAD_SIZE];

// Fingerprint the compomnent that sent the ping for debugging purposes
#[cfg(debug_assertions)]
if let Some(data) = &self.payload_data {
let data = data.as_bytes();
for (dest, &src) in payload.iter_mut().zip(data) {
*dest = src;
}
}

for i in 0..self.no_of_tries {
// This is a temporary solution for iOS due to NECP re-binding the socket to
Expand All @@ -168,10 +183,7 @@ impl Pinger {
}

match pinger
.ping(
PingSequence(i.try_into().unwrap_or(0)),
&[0; PING_PAYLOAD_SIZE],
)
.ping(PingSequence(i.try_into().unwrap_or(0)), &payload)
.await
{
Ok((_, duration)) => {
Expand Down Expand Up @@ -234,7 +246,7 @@ mod tests {
let mut protect = MockProtector::default();
protect.expect_make_internal().returning(|_| Ok(()));

let pinger = Pinger::new(1, true, Arc::new(SocketPool::new(protect)))
let pinger = Pinger::new(1, true, Arc::new(SocketPool::new(protect)), None)
.expect("Failed to create Pinger");
assert!(pinger.client_v4.get_socket().get_native_sock() > 0);
assert!(pinger.client_v6.is_some());
Expand All @@ -247,7 +259,7 @@ mod tests {
let mut protect = MockProtector::default();
protect.expect_make_internal().returning(|_| Ok(()));

let pinger = Pinger::new(2, false, Arc::new(SocketPool::new(protect)))
let pinger = Pinger::new(2, false, Arc::new(SocketPool::new(protect)), None)
.expect("Failed to create Pinger");

let target =
Expand Down
6 changes: 4 additions & 2 deletions crates/telio-traversal/src/session_keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use telio_pinger::Pinger;
use telio_sockets::SocketPool;
use telio_task::{task_exec, BoxAction, Runtime, Task};
use telio_utils::{
dual_target, repeated_actions, telio_log_debug, telio_log_warn, DualTarget, RepeatedActions,
dual_target, repeated_actions, telio_log_debug, telio_log_trace, telio_log_warn, DualTarget,
RepeatedActions,
};

/// Possible [SessionKeeper] errors.
Expand Down Expand Up @@ -57,7 +58,7 @@ pub struct SessionKeeper {
impl SessionKeeper {
pub fn start(sock_pool: Arc<SocketPool>, batch_all: bool) -> Result<Self> {
telio_log_debug!("Starting with batch_all({})", batch_all);
let pinger = Pinger::new(1, true, sock_pool)?;
let pinger = Pinger::new(1, true, sock_pool, Some("session_keeper"))?;

Ok(Self {
batch_all,
Expand Down Expand Up @@ -112,6 +113,7 @@ impl SessionKeeperTrait for SessionKeeper {
interval,
Arc::new(move |c| {
Box::pin(async move {
telio_log_trace!("Performing ping {:?}", dual_target);
let result = c.pinger.perform(dual_target).await;
let failed = match (result.v4, result.v6) {
(None, None) => true,
Expand Down
7 changes: 6 additions & 1 deletion crates/telio-wg/src/link_detection/enhanced_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ impl State {
) -> std::io::Result<Self> {
Ok(State {
ping_channel,
pinger: Pinger::new(no_of_pings, ipv6_enabled, socket_pool)?,
pinger: Pinger::new(
no_of_pings,
ipv6_enabled,
socket_pool,
Some("link_detection"),
)?,
})
}
}
Expand Down

0 comments on commit 18632ac

Please sign in to comment.