Skip to content

Commit

Permalink
Add DERP userspace keepalives
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Fetti <[email protected]>
  • Loading branch information
dfetti committed Nov 28, 2024
1 parent 30c2b4d commit 9c41e3d
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 75 deletions.
4 changes: 4 additions & 0 deletions crates/telio-model/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ pub struct FeatureDerp {
pub tcp_keepalive: Option<u32>,
/// Derp will send empty messages after this many seconds of not sending/receiving any data [default 60s]
pub derp_keepalive: Option<u32>,
/// Poll Keepalive: Application level keepalives meant to replace the TCP keepalives
pub poll_keepalive: Option<u32>,
/// Enable polling of remote peer states to reduce derp traffic
pub enable_polling: Option<bool>,
/// Use Mozilla's root certificates instead of OS ones [default false]
Expand Down Expand Up @@ -532,6 +534,7 @@ mod tests {
"derp": {
"tcp_keepalive": 13,
"derp_keepalive": 14,
"poll_keepalive": 15,
"enable_polling": true,
"use_built_in_root_certificates": true
},
Expand Down Expand Up @@ -621,6 +624,7 @@ mod tests {
derp: Some(FeatureDerp {
tcp_keepalive: Some(13),
derp_keepalive: Some(14),
poll_keepalive: Some(15),
enable_polling: Some(true),
use_built_in_root_certificates: true,
}),
Expand Down
9 changes: 8 additions & 1 deletion crates/telio-relay/src/derp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,41 @@ struct State {
/// Keepalive values that help keeping Derp connection in conntrack alive,
/// so server can send traffic after being silent for a while
/// *derp_keepalive* is also used as an interval for retrieving remote peer states.
/// TODO: Update comments for Poll Keepalives
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DerpKeepaliveConfig {
tcp_keepalive: u32,
derp_keepalive: u32,
poll_keepalive: Option<u32>,
}

impl From<&Option<FeatureDerp>> for DerpKeepaliveConfig {
fn from(derp: &Option<FeatureDerp>) -> Self {
let mut tcp_keepalive = proto::DERP_TCP_KEEPALIVE_INTERVAL;
let mut derp_keepalive = proto::DERP_KEEPALIVE_INTERVAL;
let mut poll_keepalive = None;
if let Some(derp) = derp {
if let Some(tcp_ka) = derp.tcp_keepalive {
tcp_keepalive = tcp_ka;
}
if let Some(derp_ka) = derp.derp_keepalive {
derp_keepalive = derp_ka;
}
poll_keepalive = derp.poll_keepalive;
}

DerpKeepaliveConfig {
tcp_keepalive,
derp_keepalive,
poll_keepalive,
}
}
}

const DEFAULT_SERVER_KEEPALIVE_CONFIG: DerpKeepaliveConfig = DerpKeepaliveConfig {
tcp_keepalive: proto::DERP_TCP_KEEPALIVE_INTERVAL,
derp_keepalive: proto::DERP_KEEPALIVE_INTERVAL,
poll_keepalive: None,
};

/// Derp configuration
Expand Down Expand Up @@ -725,8 +731,9 @@ impl Runtime for State {
},
// On tick send derp poll request to derp stream
Some((permit, _)) = wait_for_tx(&c.comms_direct.tx, poll_timer_tick) => {
if config.enable_polling {
if config.enable_polling || config.server_keepalives.poll_keepalive.is_some() {
self.derp_poll_session = self.derp_poll_session.wrapping_add(1);
telio_log_debug!("Sending DerpPollRequest with session {}", self.derp_poll_session);
Self::handle_outcoming_payload_direct(permit, PacketControl::DerpPollRequest(DerpPollRequestMsg::new(
self.derp_poll_session, &config.meshnet_peers
))).await;
Expand Down
Loading

0 comments on commit 9c41e3d

Please sign in to comment.