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 | |
| 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
| -rw-r--r-- | config/slskd.example.yml | 1 | ||||
| -rw-r--r-- | docs/config.md | 15 | ||||
| -rw-r--r-- | src/slskd/Application.cs | 49 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 10 |
4 files changed, 73 insertions, 2 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 86b7b17f..1edcb919 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -233,6 +233,7 @@ # write: 16384 # transfer: 262144 # write_queue: 250 +# mtu: 1500 # proxy: # enabled: false # address: ~ diff --git a/docs/config.md b/docs/config.md index 2c21d12b..d811c0e0 100644 --- a/docs/config.md +++ b/docs/config.md @@ -834,6 +834,21 @@ soulseek: password: ~ ``` +### MTU + +If needed, the MTU advertised to the server can be set. + +| Command-Line | Environment Variable | Description | +| ------------------- | ----------------------- | ---------------------------------------------------------------------------------- | +| `--slsk-server-mtu` | `SLSKD_SLSK_SERVER_MTU` | The maximum TCP transmission unit size advertised to the server (0 for OS default) | + +#### **YAML** +```yaml +soulseek: + connection: + mtu: 1500 +``` + ## Diagnostic Level The diagnostic level option is passed to the Soulseek.NET configuration and determines the level of detail the library produces diagnostic messages. This option should generally be left to `Info` or `Warning` but can be set to `Debug` if more verbose logging is desired. 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 |
