net-io 0.8.0

Network IO substrate for Rust: TCP, UDP and local-socket listeners, connection lifecycle, limits, graceful drain, PROXY protocol, and a TLS seam. Protocol-free and dependency-free.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Readiness on macOS and FreeBSD: kqueue with `EV_CLEAR` (edge-triggered) and
//! `EVFILT_USER` for wake-ups.
//!
//! A socket's write filter exists only while a send or connect waits: registered
//! for good, it fires now and then after an ACK and wakes the worker for nothing.
//! Adding the filter looks at the socket, so a send that blocked just before the
//! buffer drained still gets its event.
//!
//! References: `kqueue(2)` and `kevent(2)` in the macOS and FreeBSD man pages.
//! Layout and constants are checked against `libc` by the tests below.

use std::ffi::{c_int, c_void};
use std::io;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use super::{PollEvent, Registered, Sock, WAKE_KEY};

const EVFILT_READ: i16 = -1;
const EVFILT_WRITE: i16 = -2;
#[cfg(target_os = "macos")]
const EVFILT_USER: i16 = -10;
#[cfg(target_os = "freebsd")]
const EVFILT_USER: i16 = -11;
const EV_ADD: u16 = 0x0001;
const EV_DELETE: u16 = 0x0002;
const EV_ENABLE: u16 = 0x0004;
const EV_CLEAR: u16 = 0x0020;
const EV_DISPATCH: u16 = 0x0080;
const EV_EOF: u16 = 0x8000;
const EV_ERROR: u16 = 0x4000;
const NOTE_TRIGGER: u32 = 0x0100_0000;

/// `struct kevent`.
#[repr(C)]
#[derive(Clone, Copy)]
struct Kevent {
    ident: usize,
    filter: i16,
    flags: u16,
    fflags: u32,
    data: isize,
    udata: *mut c_void,
    #[cfg(target_os = "freebsd")]
    ext: [u64; 4],
}

/// `struct timespec`.
#[repr(C)]
#[derive(Clone, Copy)]
struct Timespec {
    tv_sec: i64,
    tv_nsec: i64,
}

#[cfg(target_os = "macos")]
const _: () = assert!(size_of::<Kevent>() == 32 && align_of::<Kevent>() == 8);
#[cfg(target_os = "freebsd")]
const _: () = assert!(size_of::<Kevent>() == 64 && align_of::<Kevent>() == 8);
const _: () = assert!(size_of::<Timespec>() == 16 && align_of::<Timespec>() == 8);

unsafe extern "C" {
    fn kqueue() -> c_int;
    fn kevent(
        kq: c_int,
        changes: *const Kevent,
        nchanges: c_int,
        events: *mut Kevent,
        nevents: c_int,
        timeout: *const Timespec,
    ) -> c_int;
    fn fcntl(fd: c_int, cmd: c_int, ...) -> c_int;
}

const F_SETFD: c_int = 2;
const FD_CLOEXEC: c_int = 1;
/// The `ident` of the wake event.
const WAKE_IDENT: usize = 1;
/// How many events one wait returns.
const EVENTS: usize = 256;

fn cvt(ret: c_int) -> io::Result<c_int> {
    if ret == -1 {
        Err(io::Error::last_os_error())
    } else {
        Ok(ret)
    }
}

fn change(ident: usize, filter: i16, flags: u16, fflags: u32, key: u64) -> Kevent {
    Kevent {
        ident,
        filter,
        flags,
        fflags,
        data: 0,
        udata: key as usize as *mut c_void,
        #[cfg(target_os = "freebsd")]
        ext: [0; 4],
    }
}

/// Wakes a worker's wait from any thread; repeated wakes before the worker runs
/// cost one atomic load.
#[derive(Debug)]
pub(crate) struct PollWaker {
    kq: Arc<OwnedFd>,
    pending: AtomicBool,
}

impl PollWaker {
    pub(crate) fn wake(&self) {
        if !self.pending.swap(true, Ordering::AcqRel) {
            let ev = change(WAKE_IDENT, EVFILT_USER, 0, NOTE_TRIGGER, WAKE_KEY);
            // SAFETY: `kq` is open; one live change, no events asked for, a zero
            // timeout pointer is not read because nothing waits.
            let _ = unsafe {
                kevent(
                    self.kq.as_raw_fd(),
                    &raw const ev,
                    1,
                    std::ptr::null_mut(),
                    0,
                    std::ptr::null(),
                )
            };
        }
    }
}

/// The kqueue of one worker.
pub(crate) struct Poller {
    kq: Arc<OwnedFd>,
    wake: Arc<PollWaker>,
    buf: Box<[Kevent; EVENTS]>,
}

// SAFETY: the raw `udata` pointers in `buf` are plain numbers (token keys), never
// dereferenced; the kqueue descriptor may be used from any thread.
unsafe impl Send for Poller {}

impl std::fmt::Debug for Poller {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Poller").finish_non_exhaustive()
    }
}

/// Asks a poller for one write event on a socket.
#[derive(Clone, Copy, Debug)]
pub(crate) struct WriteArm {
    kq: c_int,
}

impl WriteArm {
    /// The next report that `reg` can take bytes, under `key`, once.
    ///
    /// A failure here is the kernel out of memory for a filter: the send waits, and
    /// its write deadline ends the connection.
    pub(crate) fn arm(self, reg: Registered, key: u64) {
        // Adding again does not enable what the last event disabled.
        let flags = EV_ADD | EV_ENABLE | EV_CLEAR | EV_DISPATCH;
        let changes = [change(reg.raw() as usize, EVFILT_WRITE, flags, 0, key)];
        // SAFETY: `kq` outlives the driver that holds this and `reg` is registered;
        // one live change, no events asked for.
        let _ = unsafe {
            kevent(
                self.kq,
                changes.as_ptr(),
                1,
                std::ptr::null_mut(),
                0,
                std::ptr::null(),
            )
        };
    }
}

impl Poller {
    pub(crate) fn new() -> io::Result<Poller> {
        // SAFETY: no arguments; a new descriptor or -1.
        let fd = cvt(unsafe { kqueue() })?;
        // SAFETY: `fd` was just returned, is open, and has no other owner.
        let kq = Arc::new(unsafe { OwnedFd::from_raw_fd(fd) });
        // SAFETY: `kq` is open; F_SETFD with FD_CLOEXEC takes an int.
        cvt(unsafe { fcntl(kq.as_raw_fd(), F_SETFD, FD_CLOEXEC) })?;
        let wake = change(WAKE_IDENT, EVFILT_USER, EV_ADD | EV_CLEAR, 0, WAKE_KEY);
        // SAFETY: as in `PollWaker::wake`.
        cvt(unsafe {
            kevent(
                kq.as_raw_fd(),
                &raw const wake,
                1,
                std::ptr::null_mut(),
                0,
                std::ptr::null(),
            )
        })?;
        Ok(Poller {
            wake: Arc::new(PollWaker {
                kq: kq.clone(),
                pending: AtomicBool::new(false),
            }),
            kq,
            buf: Box::new([change(0, 0, 0, 0, 0); EVENTS]),
        })
    }

    pub(crate) fn waker(&self) -> Arc<PollWaker> {
        self.wake.clone()
    }

    /// Registers a socket under `key` for reading, edge-triggered. Writing waits
    /// through [`WriteArm`].
    pub(crate) fn add(&self, sock: Sock<'_>, key: u64) -> io::Result<Registered> {
        #[cfg(any(test, net_io_shim))]
        super::shim::check(super::shim::Op::Register)?;
        let raw = sock.as_raw_fd();
        let changes = [change(raw as usize, EVFILT_READ, EV_ADD | EV_CLEAR, 0, key)];
        // SAFETY: `kq` and `raw` are open; one live change, no events asked for.
        cvt(unsafe {
            kevent(
                self.kq.as_raw_fd(),
                changes.as_ptr(),
                1,
                std::ptr::null_mut(),
                0,
                std::ptr::null(),
            )
        })?;
        Ok(Registered::new(raw))
    }

    /// What asks for one write event; usable while a wait is delivering events.
    pub(crate) fn write_arm(&self) -> WriteArm {
        WriteArm {
            kq: self.kq.as_raw_fd(),
        }
    }

    /// Unregisters a socket. Its write filter may never have existed, so it goes last.
    pub(crate) fn remove(&self, reg: Registered) -> io::Result<()> {
        let raw = reg.raw();
        let changes = [
            change(raw as usize, EVFILT_READ, EV_DELETE, 0, 0),
            change(raw as usize, EVFILT_WRITE, EV_DELETE, 0, 0),
        ];
        // SAFETY: as in `add`. A filter that is already gone reports an error
        // the caller has no use for.
        let _ = unsafe {
            kevent(
                self.kq.as_raw_fd(),
                changes.as_ptr(),
                2,
                std::ptr::null_mut(),
                0,
                std::ptr::null(),
            )
        };
        Ok(())
    }

    /// Waits and calls `f` for each report. Interrupted waits report nothing.
    pub(crate) fn wait(
        &mut self,
        timeout: Option<Duration>,
        mut f: impl FnMut(PollEvent),
    ) -> io::Result<()> {
        #[cfg(any(test, net_io_shim))]
        super::shim::check(super::shim::Op::Wait)?;
        let ts = timeout.map(|d| Timespec {
            tv_sec: i64::try_from(d.as_secs()).unwrap_or(i64::MAX),
            tv_nsec: i64::from(d.subsec_nanos()),
        });
        let ts_ptr = ts
            .as_ref()
            .map_or(std::ptr::null(), |t| t as *const Timespec);
        // SAFETY: `buf` holds EVENTS entries and `ts` lives for the call.
        let n = unsafe {
            kevent(
                self.kq.as_raw_fd(),
                std::ptr::null(),
                0,
                self.buf.as_mut_ptr(),
                EVENTS as c_int,
                ts_ptr,
            )
        };
        if n < 0 {
            let e = io::Error::last_os_error();
            if e.kind() == io::ErrorKind::Interrupted {
                return Ok(());
            }
            return Err(e);
        }
        for i in 0..n as usize {
            let ev = self.buf[i];
            let key = ev.udata as usize as u64;
            if ev.filter == EVFILT_USER || key == WAKE_KEY {
                self.wake.pending.store(false, Ordering::Release);
                continue;
            }
            let failed = ev.flags & EV_ERROR != 0;
            let hangup = ev.flags & EV_EOF != 0 || failed;
            f(PollEvent {
                key,
                readable: ev.filter == EVFILT_READ || failed,
                writable: ev.filter == EVFILT_WRITE || failed,
                hangup: hangup && ev.filter == EVFILT_READ,
            });
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use std::mem::{offset_of, size_of};
    use std::os::fd::AsFd;

    #[test]
    fn test_layout_matches_libc() {
        assert_eq!(size_of::<Kevent>(), size_of::<libc::kevent>());
        assert_eq!(offset_of!(Kevent, udata), offset_of!(libc::kevent, udata));
        assert_eq!(offset_of!(Kevent, data), offset_of!(libc::kevent, data));
        assert_eq!(EVFILT_READ, libc::EVFILT_READ);
        assert_eq!(EVFILT_WRITE, libc::EVFILT_WRITE);
        assert_eq!(EVFILT_USER, libc::EVFILT_USER);
        assert_eq!(EV_ADD, libc::EV_ADD);
        assert_eq!(EV_DELETE, libc::EV_DELETE);
        assert_eq!(EV_CLEAR, libc::EV_CLEAR);
        assert_eq!(EV_ENABLE, libc::EV_ENABLE);
        assert_eq!(EV_DISPATCH, libc::EV_DISPATCH);
        assert_eq!(EV_EOF, libc::EV_EOF);
        assert_eq!(EV_ERROR, libc::EV_ERROR);
        assert_eq!(NOTE_TRIGGER, libc::NOTE_TRIGGER);
        assert_eq!(F_SETFD, libc::F_SETFD);
        assert_eq!(FD_CLOEXEC, libc::FD_CLOEXEC);
        assert_eq!(size_of::<Timespec>(), size_of::<libc::timespec>());
        // A mismatch in argument or result types fails to compile; `Kevent` and
        // `Timespec` against `libc`'s are compared above.
        type Kqueue = unsafe extern "C" fn() -> c_int;
        let (_, _): (Kqueue, Kqueue) = (kqueue, libc::kqueue);
        let _: unsafe extern "C" fn(
            c_int,
            *const Kevent,
            c_int,
            *mut Kevent,
            c_int,
            *const Timespec,
        ) -> c_int = kevent;
        let _: unsafe extern "C" fn(
            c_int,
            *const libc::kevent,
            c_int,
            *mut libc::kevent,
            c_int,
            *const libc::timespec,
        ) -> c_int = libc::kevent;
        type Fcntl = unsafe extern "C" fn(c_int, c_int, ...) -> c_int;
        let (_, _): (Fcntl, Fcntl) = (fcntl, libc::fcntl);
    }

    #[test]
    fn test_edges_and_waker() {
        let mut p = Poller::new().unwrap();
        let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let c = std::net::TcpStream::connect(l.local_addr().unwrap()).unwrap();
        let (s, _) = l.accept().unwrap();
        s.set_nonblocking(true).unwrap();
        let reg = p.add(s.as_fd(), 7).unwrap();
        let mut seen = Vec::new();
        p.wait(Some(Duration::from_millis(50)), |e| seen.push(e))
            .unwrap();
        assert!(!seen.iter().any(|e| e.writable), "nobody asked");
        // One event per ask, and the second ask works as the first did.
        for _ in 0..2 {
            seen.clear();
            p.write_arm().arm(reg, 7);
            p.wait(Some(Duration::from_secs(1)), |e| seen.push(e))
                .unwrap();
            assert!(seen.iter().any(|e| e.key == 7 && e.writable));
            seen.clear();
            p.wait(Some(Duration::from_millis(50)), |e| seen.push(e))
                .unwrap();
            assert!(!seen.iter().any(|e| e.writable), "once");
        }
        seen.clear();
        (&c).write_all(b"hi").unwrap();
        p.wait(Some(Duration::from_secs(1)), |e| seen.push(e))
            .unwrap();
        assert!(seen.iter().any(|e| e.readable));
        let w = p.waker();
        w.wake();
        w.wake();
        seen.clear();
        p.wait(Some(Duration::from_secs(1)), |e| seen.push(e))
            .unwrap();
        assert!(seen.is_empty(), "the wake reports nothing");
        p.remove(reg).unwrap();
    }
}