Skip to content

Commit 7d4371c

Browse files
committed
Enable automatic MTU detection on linux
1 parent 48bdf22 commit 7d4371c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

talpid-core/src/tunnel/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ impl TunnelMonitor {
175175
)
176176
.map(|mtu| Self::clamp_mtu(params, mtu))
177177
.unwrap_or(default_mtu);
178+
179+
#[cfg(target_os = "linux")]
180+
let detect_mtu = params.options.mtu.is_none();
181+
178182
let config = talpid_wireguard::config::Config::from_parameters(params, default_mtu)?;
179183
let monitor = talpid_wireguard::WireguardMonitor::start(
180184
config,

talpid-wireguard/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl WireguardMonitor {
270270
>(
271271
mut config: Config,
272272
psk_negotiation: bool,
273+
#[cfg(target_os = "linux")] detect_mtu: bool,
273274
log_path: Option<&Path>,
274275
args: TunnelArgs<'_, F>,
275276
) -> Result<WireguardMonitor> {
@@ -388,7 +389,36 @@ impl WireguardMonitor {
388389
)
389390
.await?;
390391
}
392+
#[cfg(target_os = "linux")]
393+
if detect_mtu {
394+
let iface_name_clone = iface_name.clone();
395+
tokio::task::spawn(async move {
396+
log::debug!("Starting MTU detection");
397+
let verified_mtu = match auto_mtu_detection(
398+
gateway,
399+
#[cfg(any(target_os = "macos", target_os = "linux"))]
400+
iface_name_clone.clone(),
401+
config.mtu,
402+
)
403+
.await
404+
{
405+
Ok(mtu) => mtu,
406+
Err(e) => {
407+
log::error!("{}", e.display_chain_with_msg("Failed to detect MTU"));
408+
return;
409+
}
410+
};
391411

412+
if verified_mtu != config.mtu {
413+
log::warn!("Lowering MTU from {} to {verified_mtu}", config.mtu);
414+
if let Err(e) = unix::set_mtu(&iface_name_clone, verified_mtu) {
415+
log::error!("{}", e.display_chain_with_msg("Failed to set MTU"))
416+
};
417+
} else {
418+
log::debug!("MTU {verified_mtu} verified to not drop packets");
419+
}
420+
});
421+
}
392422
let mut connectivity_monitor = tokio::task::spawn_blocking(move || {
393423
match connectivity_monitor.establish_connectivity(args.retry_attempt) {
394424
Ok(true) => Ok(connectivity_monitor),

0 commit comments

Comments
 (0)