From 65e30b75178524730636a1b00621e05c08913d63 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 8 Oct 2026 20:48:57 -0500 Subject: Mtu property --- src/slskd/Core/Options.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 13a24eed..f278d895 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1814,6 +1814,16 @@ namespace slskd [Validate] public ProxyOptions Proxy { get; init; } = new ProxyOptions(); + /// + /// Gets the MTU to use for the server connection, or null to use the OS default. + /// + [Argument(default, "slsk-server-mtu")] + [EnvironmentVariable("SLSK_SERVER_MTU")] + [Description("MTU to use for the server connection")] + [Range(576, 65535)] + [RequiresReconnect] + public int? Mtu { get; init; } = null; + /// /// Connection buffer options. /// -- cgit v1.2.3 From 6490ca194b7ca5d9b110d82ebbf5fe86e1af015d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 8 Oct 2026 21:03:35 -0500 Subject: add ConfigureSocketMtu --- src/slskd/Application.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 506b1980..9d172a76 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -568,6 +568,35 @@ namespace slskd } } + private void ConfigureSocketMtu(Socket socket, int mtu) + { + /* + limits the size of the packets sent and received on this connection to the given MTU. there's no per-socket MTU setting, + so this is done by setting TCP_MAXSEG to the MTU minus the IPv4 and TCP headers (20 bytes each) + + Windows is skipped because it only supports reading TCP_MAXSEG, not setting it + see: https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options + and: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockopt + */ + if (!(System.OperatingSystem.IsLinux() || System.OperatingSystem.IsMacOS() || System.OperatingSystem.IsFreeBSD())) + { + return; + } + + try + { + const int IPPROTO_TCP = 6; + const int TCP_MAXSEG = 2; + const int IPV4_AND_TCP_HEADER_SIZE = 40; + + socket.SetRawSocketOption(IPPROTO_TCP, TCP_MAXSEG, BitConverter.GetBytes(mtu - IPV4_AND_TCP_HEADER_SIZE)); + } + catch (SocketException ex) + { + Log.Warning("Failed to set the MTU of the server connection to {MTU}: {Message}", mtu, ex.Message); + } + } + private async Task EnqueueDownload(string username, IPEndPoint endpoint, string filename) { Metrics.Enqueue.RequestsReceived.Inc(1); -- cgit v1.2.3 From 75fb01e431292bf2e43639506364a74bbab1fbdb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 8 Oct 2026 21:03:56 -0500 Subject: apply custom MTU if specified --- src/slskd/Application.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 9d172a76..51289970 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -418,7 +418,15 @@ namespace slskd // os-specific keepalive is configured for long-lived connections for the server and distributed parent/children var serverOptions = connectionOptions.With( inactivityTimeout: -1, // don't disconnect due to inactivity - configureSocket: socket => ConfigureSocketKeepaliveOptions(socket, OptionsAtStartup.Soulseek.Connection)); + configureSocket: socket => + { + ConfigureSocketKeepaliveOptions(socket, OptionsAtStartup.Soulseek.Connection); + + if (OptionsAtStartup.Soulseek.Connection.Mtu.HasValue) + { + ConfigureSocketMtu(socket, OptionsAtStartup.Soulseek.Connection.Mtu.Value); + } + }); var distributedOptions = connectionOptions.With( writeQueueSize: OptionsAtStartup.Soulseek.Connection.Buffer.WriteQueue, // write queue set to keep distributed children from impacting performance @@ -1614,7 +1622,15 @@ namespace slskd serverPatch = connectionPatch.With( inactivityTimeout: -1, // don't disconnect due to inactivity - configureSocket: socket => ConfigureSocketKeepaliveOptions(socket, options: connection)); + configureSocket: socket => + { + ConfigureSocketKeepaliveOptions(socket, options: connection); + + if (connection.Mtu.HasValue) + { + ConfigureSocketMtu(socket, connection.Mtu.Value); + } + }); distributedPatch = connectionPatch.With( writeQueueSize: connection.Buffer.WriteQueue, // write queue set to keep distributed children from impacting performance -- cgit v1.2.3