Skip to content

Commit

Permalink
Add DERP userspace keepalives
Browse files Browse the repository at this point in the history
More info on that in RFC-LLT-0070

Signed-off-by: Daniel Fetti <[email protected]>
  • Loading branch information
dfetti committed Dec 18, 2024
1 parent 07f2d21 commit dd0efad
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 76 deletions.
1 change: 1 addition & 0 deletions .unreleased/LLT-5639
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add DERP userspace keepalives
6 changes: 6 additions & 0 deletions crates/telio-model/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ 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
/// They will use derp_keepalive as interval
#[serde(default)]
pub poll_keepalive: bool,
/// 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 @@ -536,6 +540,7 @@ mod tests {
"derp": {
"tcp_keepalive": 13,
"derp_keepalive": 14,
"poll_keepalive": true,
"enable_polling": true,
"use_built_in_root_certificates": true
},
Expand Down Expand Up @@ -626,6 +631,7 @@ mod tests {
derp: Some(FeatureDerp {
tcp_keepalive: Some(13),
derp_keepalive: Some(14),
poll_keepalive: true,
enable_polling: Some(true),
use_built_in_root_certificates: true,
}),
Expand Down
11 changes: 10 additions & 1 deletion crates/telio-relay/src/derp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,43 @@ 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.
/// PollKeepalive is a feature that is meant to replace TCP keepalives with
/// application level keepalives (DerpPollRequest).
/// More info on PollKeepalive can be found in RFC-LLT-0070
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DerpKeepaliveConfig {
tcp_keepalive: u32,
derp_keepalive: u32,
poll_keepalive: bool,
}

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 = false;
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: false,
};

/// Derp configuration
Expand Down Expand Up @@ -726,8 +734,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 {
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 dd0efad

Please sign in to comment.