diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-08 21:08:50 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-08 21:08:50 -0500 |
| commit | f4edb6baa76bbe1465d30bd69de06d57e4d23c0c (patch) | |
| tree | 242c29d677c67ca3b1f63a69595789932e16a823 /src | |
| parent | 85ce16a71eb88134a9b3230ea832e6dc37c7c216 (diff) | |
| parent | 75fb01e431292bf2e43639506364a74bbab1fbdb (diff) | |
| download | slskd-f4edb6baa76bbe1465d30bd69de06d57e4d23c0c.tar.gz | |
Merge pull request #1875 from slskd/mtu
Add an option to configure MTU for the server connection
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 49 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 10 |
2 files changed, 57 insertions, 2 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 506b1980..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 @@ -568,6 +576,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); @@ -1585,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 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 @@ -1815,6 +1815,16 @@ namespace slskd public ProxyOptions Proxy { get; init; } = new ProxyOptions(); /// <summary> + /// Gets the MTU to use for the server connection, or null to use the OS default. + /// </summary> + [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; + + /// <summary> /// Connection buffer options. /// </summary> public class BufferOptions |
