Skip to content

Commit 0bc3140

Browse files
authored
Merge pull request #379 from irsync-dev/375-udp-socket-buffers
Warn when the kernel caps the UDP buffers
2 parents 3444b3c + cf78264 commit 0bc3140

26 files changed

Lines changed: 1066 additions & 8 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "sync",
3030
# hybrid key exchange needs (net/endpoint.rs); ring has none.
3131
iroh = { version = "1.5", default-features = false, features = ["metrics", "fast-apple-datapath", "portmapper", "tls-aws-lc-rs"] }
3232
iroh-base = "1.5"
33+
# For the UDP buffer probe (net/udp_buffers.rs); netwatch already uses it.
34+
socket2 = "0.6"
3335
n0-watcher = "1"
3436
noq-udp = { version = "1.4", default-features = false }
3537
# Not 1.4.0: it closes the connection ("too many gaps in stream buffer")

‎book/src/explanation/networking.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ sets its transport parameters in `transport_config` in
115115
flow-control windows, the stream limits, the congestion controller and how
116116
often the peer acknowledges.
117117

118+
Each UDP socket asks the kernel for a 7 MiB buffer each way. Many kernels
119+
give less, and on a link with a long round trip the receiver then drops
120+
packets. irsync warns when that happens, on either side; see
121+
[Tune the kernel for long-distance links](../how-to/troubleshooting.md#tune-the-kernel-for-long-distance-links).
122+
118123
## TLS 1.3 inside QUIC
119124

120125
QUIC has a handshake of its own (RFC 9000, section 7), and that handshake

‎book/src/how-to/troubleshooting.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,68 @@ path.
226226
hole punching fails. When the connection has to stay on a relay, run
227227
[your own relay](direct-and-relay.md#use-your-own-relay) close to both
228228
machines.
229+
230+
## Tune the kernel for long-distance links
231+
232+
A sync starts with a warning like this, about the client, the server or both:
233+
234+
```text
235+
irsync: warning: the kernel on myserver limits UDP buffers to 208 KiB; irsync asks for 7 MiB.
236+
irsync: warning: On a link with a long round trip, such as across an ocean, that loses packets and slows the sync down. To fix it on myserver:
237+
irsync: warning: sudo sysctl -w net.core.rmem_max=16777216 net.core.wmem_max=16777216
238+
irsync: warning: then restart the daemon there: systemctl --user restart irsync
239+
irsync: warning: see "Tune the kernel for long-distance links" in the manual
240+
```
241+
242+
The daemon logs the same at startup, about its own machine.
243+
244+
**Cause.** irsync sends over UDP. Each of its sockets asks the kernel for a
245+
7 MiB buffer, but the kernel gives no more than its limit, and Linux's
246+
default limit is 208 KiB. The buffer has to hold everything sent but not yet
247+
acknowledged. That is about the transfer rate times the round trip: 20 MB/s
248+
over a 143 ms round trip is about 3 MB. So the round trip matters more than
249+
the speed. With a small buffer, the receiver drops packets, and the sync
250+
slows down to make up for them. On one link from California to Europe, a
251+
2.5 GB push took 9 minutes 47 seconds with the default limit and 3 minutes
252+
12 seconds with the limit raised.
253+
254+
**Fix.** Raise the limit on the machine the warning names. On Linux:
255+
256+
```sh
257+
sudo sysctl -w net.core.rmem_max=16777216 net.core.wmem_max=16777216
258+
```
259+
260+
On macOS and FreeBSD:
261+
262+
```sh
263+
sudo sysctl -w kern.ipc.maxsockbuf=16777216
264+
```
265+
266+
A socket keeps the size it got when it was made. A sync makes new ones each
267+
time, but a daemon makes its own when it starts, so restart the daemon after
268+
raising its limit. On Linux, under the user service `irsync pair` set up:
269+
270+
```sh
271+
systemctl --user restart irsync
272+
```
273+
274+
Anywhere, SIGHUP does it too: the daemon reloads and binds new sockets.
275+
276+
```sh
277+
kill -HUP $(pgrep -f 'irsync daemon')
278+
```
279+
280+
`irsync pair` is no fix here: it leaves a running daemon of the same version
281+
alone.
282+
283+
`sysctl -w` lasts until the next reboot. To keep the setting, write it to a
284+
file. On Linux, put these two lines in `/etc/sysctl.d/90-irsync.conf` and
285+
run `sudo sysctl --system`:
286+
287+
```text
288+
net.core.rmem_max = 16777216
289+
net.core.wmem_max = 16777216
290+
```
291+
292+
On macOS and FreeBSD, add `kern.ipc.maxsockbuf=16777216` to
293+
`/etc/sysctl.conf`.

‎book/src/reference/protocol.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ a change to it needs no new version.
2525
| Item | Value |
2626
| --- | --- |
2727
| ALPN | `irsync/1` |
28-
| `PROTOCOL_VERSION` | `10`, carried in `Hello.protocol_version` |
28+
| `PROTOCOL_VERSION` | `11`, carried in `Hello.protocol_version` |
2929
| Framing | A protobuf varint length (at most 4 bytes for `MAX_FRAME`), then that many bytes of one protobuf message: protobuf's standard length-delimited form |
3030
| `MAX_FRAME` | 16 MiB. A longer length is refused before anything is allocated for it. |
3131
| Content hash | BLAKE3, 32 bytes, for chunks and whole files |
@@ -165,6 +165,13 @@ whether the session was resumed, the daemon's `BuildInfo`, the daemon's
165165
`ack` without a `platform` as a failed start. Each side logs the
166166
other's build at info level once the session is up.
167167

168+
The `ack` also carries `socket_buffers`: the UDP receive and send buffer
169+
sizes, in bytes, that the daemon's kernel granted a socket asking for 7 MiB
170+
each way, probed once at startup, and the daemon's `os`, which says which
171+
command raises the limit there. The client warns when either size is below
172+
7 MiB (see [Tune the kernel for long-distance links](../how-to/troubleshooting.md#tune-the-kernel-for-long-distance-links)). A daemon that did not probe
173+
(Windows) sends none.
174+
168175
`list_exports` is answered with `exports`: the exports this key may use with
169176
their paths and effective access, sorted by name. The daemon then closes the
170177
connection.

‎crates/irsync-proto/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub static FILE_DESCRIPTOR_SET: &[u8] = include_bytes!(concat!(env!("OUT_DIR"),
2828
/// ALPN identifier for the irsync protocol.
2929
pub const ALPN: &[u8] = b"irsync/1";
3030
/// Current protocol version.
31-
pub const PROTOCOL_VERSION: u32 = 10;
31+
pub const PROTOCOL_VERSION: u32 = 11;
3232
/// The most bytes of a chunk one `DataChunk` frame carries: a chunk's bytes
3333
/// travel in pieces of this size, so a connection that drops mid-chunk
3434
/// still leaves the receiver the pieces that arrived.

‎crates/irsync-proto/tests/golden_bytes.rs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use prost::Message;
99

1010
#[test]
1111
fn protocol_version_is_pinned() {
12-
assert_eq!(irsync_proto::PROTOCOL_VERSION, 10);
12+
assert_eq!(irsync_proto::PROTOCOL_VERSION, 11);
1313
}
1414

1515
fn enc(m: Msg) -> Vec<u8> {
@@ -184,7 +184,7 @@ fn admin_nack_golden_bytes() {
184184
}
185185

186186
#[test]
187-
fn hello_golden_bytes_carry_protocol_version_10() {
187+
fn hello_golden_bytes_carry_protocol_version_11() {
188188
use irsync_proto::v1::{hello, Hello, OpenAdmin, PlatformFamily};
189189
// protocol_version = 1, admin = 9 (an empty OpenAdmin), platform = 10
190190
let h = Hello {
@@ -193,7 +193,7 @@ fn hello_golden_bytes_carry_protocol_version_10() {
193193
platform: PlatformFamily::Windows as i32,
194194
..Default::default()
195195
};
196-
assert_eq!(h.encode_to_vec(), [0x08, 0x0A, 0x4A, 0x00, 0x50, 0x02]);
196+
assert_eq!(h.encode_to_vec(), [0x08, 0x0B, 0x4A, 0x00, 0x50, 0x02]);
197197
}
198198

199199
#[test]
@@ -207,6 +207,24 @@ fn hello_ack_platform_golden_bytes() {
207207
assert_eq!(a.encode_to_vec(), [0x28, 0x01]);
208208
}
209209

210+
#[test]
211+
fn hello_ack_socket_buffers_golden_bytes() {
212+
use irsync_proto::v1::{socket_buffers::Os, HelloAck, SocketBuffers};
213+
// socket_buffers = 6 (recv = 1, send = 2, os = 3)
214+
let a = HelloAck {
215+
socket_buffers: Some(SocketBuffers {
216+
recv: 1,
217+
send: 2,
218+
os: Os::Linux as i32,
219+
}),
220+
..Default::default()
221+
};
222+
assert_eq!(
223+
a.encode_to_vec(),
224+
[0x32, 0x06, 0x08, 0x01, 0x10, 0x02, 0x18, 0x01]
225+
);
226+
}
227+
210228
#[test]
211229
fn a_hello_without_a_platform_decodes_as_unspecified() {
212230
use irsync_proto::v1::{Hello, PlatformFamily};

‎crates/irsync/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ iroh-base = { workspace = true, optional = true }
3939
n0-watcher = { workspace = true, optional = true }
4040
noq-udp = { workspace = true, optional = true }
4141
ssh-key = { workspace = true }
42+
socket2 = { workspace = true }
4243
thiserror = { workspace = true }
4344
minisign-verify = { workspace = true }
4445
tracing = { workspace = true }

‎crates/irsync/src/cli/progress/mod.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ pub struct Reporter {
110110
/// Events lost because this reporter lagged.
111111
lagged: u64,
112112
last_draw: Option<Instant>,
113+
/// The server as typed, for its UDP buffer warning.
114+
server_name: String,
115+
/// Whether the UDP buffer warning was printed: [this client, the server].
116+
warned_buffers: [bool; 2],
113117
}
114118

115119
/// rsync's human-readable size: `123`, `456.78K`, `12.34M`, `1.23G`, `T`.
@@ -203,9 +207,17 @@ impl Reporter {
203207
dry_run,
204208
lagged: 0,
205209
last_draw: None,
210+
server_name: "the server".to_string(),
211+
warned_buffers: [false; 2],
206212
}
207213
}
208214

215+
/// The server's name as typed, for the warnings about it.
216+
pub fn with_server_name(mut self, name: String) -> Reporter {
217+
self.server_name = name;
218+
self
219+
}
220+
209221
/// `-q`: print nothing on stdout, only the failures (on `errors`).
210222
pub fn with_quiet(mut self, quiet: bool) -> Reporter {
211223
self.quiet = quiet;
@@ -381,7 +393,31 @@ impl Reporter {
381393
*self.shown.entry((p, msg)).or_default() += 1;
382394
}
383395
}
396+
Event::SocketBuffers { server, probe } => self.warn_buffers(server, &probe),
397+
}
398+
}
399+
400+
/// The warning for a kernel that caps the UDP buffers, once per
401+
/// machine: on stderr, or above the block on a terminal; none under `-q`.
402+
fn warn_buffers(&mut self, server: bool, probe: &crate::net::udp_buffers::BufferProbe) {
403+
use crate::net::udp_buffers::{warning_lines, Machine};
404+
if self.quiet || std::mem::replace(&mut self.warned_buffers[usize::from(server)], true) {
405+
return;
406+
}
407+
let machine = if server {
408+
Machine::Server(&self.server_name)
409+
} else {
410+
Machine::ThisClient
411+
};
412+
for l in warning_lines(probe, machine) {
413+
let line = format!("irsync: warning: {}", escape_text(&l));
414+
if self.screen.is_tty() {
415+
self.screen.println(&line);
416+
} else {
417+
let _ = writeln!(self.errors, "{line}");
418+
}
384419
}
420+
let _ = self.errors.flush();
385421
}
386422

387423
/// The finished session's `list`/`transfer`/`delete` lines, into the

‎crates/irsync/src/client/mod.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ pub async fn sync(
107107
};
108108

109109
let conn = connect(host, opts, say).await?;
110+
// This machine's sockets were just bound: what did its kernel grant them?
111+
if let Some(probe) = crate::net::udp_buffers::probe() {
112+
bus.emit(Event::SocketBuffers {
113+
server: false,
114+
probe,
115+
});
116+
}
110117
let filters = match FilterSet::from_rules(&conn.opts.filters).context("reading filter rules") {
111118
Ok(f) => Arc::new(f),
112119
Err(e) => {

0 commit comments

Comments
 (0)