net-io 0.5.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! The completion driver on Windows: operations go straight to the kernel.
//!
//! An idle connection waits with a zero-byte receive, so it pins no buffer; the
//! read that follows costs one more system call than epoll's round trip. Sockets
//! use `FILE_SKIP_COMPLETION_PORT_ON_SUCCESS`, so a call that finishes at once
//! completes here without a trip through the port.
//!
//! A connection's socket may not close until its operations are dequeued, which
//! the reactor ensures. `deregister` with operations out cancels them; their
//! packets find no registration and are dropped with their buffers (edge case 64).

use std::collections::VecDeque;
use std::time::Duration;

use super::{
    BorrowedSocket, Completion, Completions, Driver, DriverKind, DriverWaker, Operation,
    OperationKind, Rejected, Token, operation_token,
};
use crate::error::{NetError, OsErrorClass};
use crate::sys::{self, Associated, Finished, InFlight, OpKind, Port, Started};

#[derive(Debug)]
struct Reg {
    token: u64,
    socket: Associated,
    recv: Option<InFlight>,
    out: Option<InFlight>,
    close: bool,
}

/// How many waits may skip the port while local completions are queued, so a busy
/// worker still sees new packets.
const FAIRNESS: u8 = 16;

/// The completion driver of one worker.
#[derive(Debug)]
pub(crate) struct IocpDriver {
    port: Port,
    regs: Vec<Option<Reg>>,
    local: VecDeque<Completion>,
    skipped: u8,
}

fn class(e: &std::io::Error) -> OsErrorClass {
    if sys::is_aborted(e) {
        OsErrorClass::Aborted
    } else {
        OsErrorClass::from_io(e)
    }
}

fn completion(f: Finished) -> Completion {
    let kind = match f.kind {
        OpKind::Receive => OperationKind::Receive,
        OpKind::Send => OperationKind::Send,
        OpKind::Connect => OperationKind::Connect,
    };
    let result = f.result.as_ref().map(|n| *n).map_err(class);
    let mut c = Completion::new(Token(f.token), kind, result);
    c.buffer = f.buffer;
    c.chunks = f.chunks;
    if let (Some(b), Ok(n)) = (c.buffer.as_mut(), result) {
        let len = b.len() + n;
        b.set_len(len);
    }
    c
}

fn try_close(reg: &mut Reg, local: &mut VecDeque<Completion>) {
    if reg.close && reg.recv.is_none() && reg.out.is_none() {
        reg.close = false;
        local.push_back(Completion::new(
            Token(reg.token),
            OperationKind::Close,
            Ok(0),
        ));
    }
}

fn on_finished(regs: &mut [Option<Reg>], local: &mut VecDeque<Completion>, f: Finished) {
    let token = Token(f.token);
    let Some(reg) = regs
        .get_mut(token.index())
        .and_then(Option::as_mut)
        .filter(|r| r.token == f.token)
    else {
        // The slot moved on (edge case 64).
        return;
    };
    match f.kind {
        OpKind::Receive => reg.recv = None,
        OpKind::Send | OpKind::Connect => reg.out = None,
    }
    local.push_back(completion(f));
    try_close(reg, local);
}

impl IocpDriver {
    pub(crate) fn new(capacity: usize) -> Result<IocpDriver, NetError> {
        let port = Port::new().map_err(|e| NetError::DriverSetup {
            driver: DriverKind::Reactor,
            source: e,
        })?;
        Ok(IocpDriver::on(port, capacity))
    }

    /// As `register`, for a named pipe: a pipe is not a socket, so it has no place
    /// in the public signature.
    #[cfg(feature = "local")]
    pub(crate) fn register_pipe(
        &mut self,
        handle: std::os::windows::io::RawHandle,
        token: Token,
    ) -> Result<(), NetError> {
        let i = token.index();
        if i >= self.regs.len() {
            self.regs.resize_with(i + 1, || None);
        }
        let socket = self
            .port
            .associate_pipe(handle)
            .map_err(|e| NetError::from_os("CreateIoCompletionPort", e))?;
        self.regs[i] = Some(Reg {
            token: token.0,
            socket,
            recv: None,
            out: None,
            close: false,
        });
        Ok(())
    }

    fn on(port: Port, capacity: usize) -> IocpDriver {
        IocpDriver {
            port,
            regs: Vec::with_capacity(capacity),
            local: VecDeque::with_capacity(capacity.min(1024)),
            skipped: 0,
        }
    }

    /// Moves what finished without the port into `out`.
    pub(crate) fn take_local(&mut self, out: &mut Completions) -> usize {
        let mut n = 0;
        while out.has_room() {
            let Some(c) = self.local.pop_front() else {
                break;
            };
            out.push(c);
            n += 1;
        }
        n
    }

    fn reg(&mut self, token: Token) -> Option<&mut Reg> {
        self.regs
            .get_mut(token.index())
            .and_then(Option::as_mut)
            .filter(|r| r.token == token.0)
    }
}

impl Driver for IocpDriver {
    fn kind(&self) -> DriverKind {
        DriverKind::Reactor
    }

    fn register(&mut self, socket: BorrowedSocket<'_>, token: Token) -> Result<(), NetError> {
        let i = token.index();
        if i >= self.regs.len() {
            self.regs.resize_with(i + 1, || None);
        }
        let socket = self
            .port
            .associate(socket)
            .map_err(|e| NetError::from_os("CreateIoCompletionPort", e))?;
        self.regs[i] = Some(Reg {
            token: token.0,
            socket,
            recv: None,
            out: None,
            close: false,
        });
        Ok(())
    }

    fn submit(&mut self, operation: Operation) -> Result<(), Rejected> {
        let token = operation_token(&operation);
        if let Operation::Nop { .. } = operation {
            self.local
                .push_back(Completion::new(token, OperationKind::Nop, Ok(0)));
            return Ok(());
        }
        let Some(reg) = self.reg(token) else {
            return Err(Rejected { operation });
        };
        let (on, recv_free, out_free) = (reg.socket, reg.recv.is_none(), reg.out.is_none());
        let is_recv = matches!(
            operation,
            Operation::Receive { .. } | Operation::ReceiveZero { .. }
        );
        let started = match operation {
            Operation::ReceiveZero { .. } if recv_free => self.port.receive(on, token.0, None),
            Operation::Receive { buffer, .. } if recv_free => {
                self.port.receive(on, token.0, Some(buffer))
            }
            Operation::Send { chunks, .. } if out_free => self.port.send(on, token.0, chunks),
            Operation::Connect { addr, .. } if out_free => {
                match self.port.connect(on, token.0, addr) {
                    Ok(started) => started,
                    Err(e) => {
                        self.local.push_back(Completion::new(
                            token,
                            OperationKind::Connect,
                            Err(class(&e)),
                        ));
                        return Ok(());
                    }
                }
            }
            Operation::Close { abortive, .. } => {
                let local = &mut self.local;
                if let Some(reg) = self.regs.get_mut(token.index()).and_then(Option::as_mut) {
                    if abortive {
                        reg.socket.set_abortive();
                    }
                    reg.close = true;
                    try_close(reg, local);
                }
                return Ok(());
            }
            // A listener is never on a port (D-016), and a second operation of a kind
            // waits for the first.
            operation => return Err(Rejected { operation }),
        };
        match started {
            Started::Done(f) => self.local.push_back(completion(f)),
            Started::Pending(op) => {
                if let Some(reg) = self.reg(token) {
                    if is_recv {
                        reg.recv = Some(op);
                    } else {
                        reg.out = Some(op);
                    }
                }
            }
        }
        Ok(())
    }

    fn wait(
        &mut self,
        out: &mut Completions,
        timeout: Option<Duration>,
    ) -> Result<usize, NetError> {
        let queued = !self.local.is_empty();
        if !queued || self.skipped >= FAIRNESS {
            self.skipped = 0;
            let timeout = if queued {
                Some(Duration::ZERO)
            } else {
                timeout
            };
            let (regs, local) = (&mut self.regs, &mut self.local);
            self.port
                .wait(timeout, |f| on_finished(regs, local, f))
                .map_err(|e| NetError::from_os("GetQueuedCompletionStatusEx", e))?;
        } else {
            self.skipped += 1;
        }
        Ok(self.take_local(out))
    }

    fn cancel(&mut self, token: Token, which: OperationKind) -> Result<(), NetError> {
        let Some(reg) = self.reg(token) else {
            return Ok(());
        };
        let (on, op) = match which {
            OperationKind::Receive | OperationKind::Accept => (reg.socket, reg.recv),
            OperationKind::Send | OperationKind::Connect => (reg.socket, reg.out),
            _ => return Ok(()),
        };
        if let Some(op) = op {
            self.port
                .cancel(on, op)
                .map_err(|e| NetError::from_os("CancelIoEx", e))?;
        }
        Ok(())
    }

    fn deregister(&mut self, token: Token) -> Result<(), NetError> {
        let Some(slot) = self.regs.get_mut(token.index()) else {
            return Ok(());
        };
        if slot.as_ref().is_some_and(|r| r.token == token.0) {
            if let Some(reg) = slot.take() {
                for op in [reg.recv, reg.out].into_iter().flatten() {
                    let _ = self.port.cancel(reg.socket, op);
                }
            }
        }
        // Completions still queued for it are dropped with their buffers.
        self.local.retain(|c| c.token != token);
        Ok(())
    }

    fn waker(&self) -> DriverWaker {
        DriverWaker {
            inner: self.port.waker(),
        }
    }
}

impl Drop for IocpDriver {
    fn drop(&mut self) {
        for reg in self.regs.iter().flatten() {
            for op in [reg.recv, reg.out].into_iter().flatten() {
                let _ = self.port.cancel(reg.socket, op);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::buf::Buffer;
    use crate::driver::SendChunks;
    use crate::limits::SizeClass;
    use std::io::{Read, Write};
    use std::net::{SocketAddr, TcpListener, TcpStream};
    use std::os::windows::io::AsSocket;

    fn pair() -> (TcpStream, TcpStream) {
        let l = TcpListener::bind("127.0.0.1:0").unwrap();
        let c = TcpStream::connect(l.local_addr().unwrap()).unwrap();
        let (s, _) = l.accept().unwrap();
        s.set_nonblocking(true).unwrap();
        (c, s)
    }

    fn one(d: &mut IocpDriver) -> Completion {
        let mut out = Completions::new();
        for _ in 0..50 {
            if d.wait(&mut out, Some(Duration::from_millis(100))).unwrap() > 0 {
                return out.drain().next().unwrap();
            }
        }
        panic!("no completion");
    }

    fn both() -> [IocpDriver; 2] {
        [
            IocpDriver::on(Port::with_skip(true).unwrap(), 8),
            // Edge case 69: every completion through the port.
            IocpDriver::on(Port::with_skip(false).unwrap(), 8),
        ]
    }

    #[test]
    fn test_receive_and_send_complete_with_their_buffers() {
        for mut d in both() {
            let (mut c, s) = pair();
            let t = Token(3);
            d.register(s.as_socket(), t).unwrap();
            c.write_all(b"ping").unwrap();
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            let r = one(&mut d);
            assert_eq!(r.kind, OperationKind::Receive);
            assert_eq!(r.result, Ok(4));
            let buffer = r.buffer.unwrap();
            assert_eq!(buffer.as_slice(), b"ping");
            d.submit(Operation::Send {
                token: t,
                chunks: SendChunks::from_buffer(buffer),
            })
            .unwrap();
            let w = one(&mut d);
            assert_eq!(w.kind, OperationKind::Send);
            assert_eq!(w.result, Ok(4));
            assert!(w.chunks.is_some());
            let mut back = [0; 4];
            c.read_exact(&mut back).unwrap();
            assert_eq!(&back, b"ping");
            d.deregister(t).unwrap();
        }
    }

    #[test]
    fn test_receive_pends_until_bytes_arrive() {
        for mut d in both() {
            let (mut c, s) = pair();
            let t = Token(1);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            let mut out = Completions::new();
            assert_eq!(
                d.wait(&mut out, Some(Duration::from_millis(20))).unwrap(),
                0
            );
            c.write_all(b"late").unwrap();
            let r = one(&mut d);
            assert_eq!(r.result, Ok(4));
            assert_eq!(r.buffer.unwrap().as_slice(), b"late");
        }
    }

    #[test]
    fn test_zero_byte_receive_holds_no_buffer() {
        for mut d in both() {
            let (mut c, s) = pair();
            let t = Token(2);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::ReceiveZero { token: t }).unwrap();
            c.write_all(b"x").unwrap();
            let r = one(&mut d);
            assert_eq!(r.kind, OperationKind::Receive);
            assert_eq!(r.result, Ok(0));
            assert!(r.buffer.is_none());
            // Readable: the bytes are still there.
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            assert_eq!(one(&mut d).result, Ok(1));
        }
    }

    /// Edge case 68.
    #[test]
    fn test_zero_byte_receive_then_end_of_stream() {
        for mut d in both() {
            let (c, s) = pair();
            let t = Token(2);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::ReceiveZero { token: t }).unwrap();
            drop(c);
            assert_eq!(one(&mut d).result, Ok(0));
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            assert_eq!(one(&mut d).result, Ok(0), "end of stream");
        }
    }

    /// Exactly one completion, with the buffer back, after a cancel; a second
    /// cancel finds nothing (edge case 62).
    #[test]
    fn test_cancel_completes_once_with_the_buffer() {
        for mut d in both() {
            let (_c, s) = pair();
            let t = Token(5);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            d.cancel(t, OperationKind::Receive).unwrap();
            let r = one(&mut d);
            assert_eq!(r.result, Err(OsErrorClass::Aborted));
            assert!(r.buffer.is_some());
            d.cancel(t, OperationKind::Receive).unwrap();
            let mut out = Completions::new();
            assert_eq!(
                d.wait(&mut out, Some(Duration::from_millis(20))).unwrap(),
                0
            );
        }
    }

    /// Edge case 63: an operation that finished before the cancel keeps its result.
    #[test]
    fn test_cancel_after_success_keeps_the_result() {
        for mut d in both() {
            let (mut c, s) = pair();
            let t = Token(5);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            c.write_all(b"done").unwrap();
            std::thread::sleep(Duration::from_millis(20));
            d.cancel(t, OperationKind::Receive).unwrap();
            assert_eq!(one(&mut d).result, Ok(4));
        }
    }

    /// Edge case 64: a packet for a slot that moved on goes nowhere.
    #[test]
    fn test_stale_packet_is_dropped() {
        for mut d in both() {
            let (_c, s) = pair();
            let (_c2, s2) = pair();
            let old = Token(7 | (1 << 32));
            let new = Token(7 | (2 << 32));
            d.register(s.as_socket(), old).unwrap();
            d.submit(Operation::Receive {
                token: old,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            d.deregister(old).unwrap();
            d.register(s2.as_socket(), new).unwrap();
            let mut out = Completions::new();
            d.wait(&mut out, Some(Duration::from_millis(50))).unwrap();
            assert!(out.drain().all(|c| c.token != old));
        }
    }

    #[test]
    fn test_second_receive_and_unknown_token_are_rejected() {
        let mut d = IocpDriver::new(8).unwrap();
        let (_c, s) = pair();
        let t = Token(1);
        d.register(s.as_socket(), t).unwrap();
        d.submit(Operation::ReceiveZero { token: t }).unwrap();
        assert!(d.submit(Operation::ReceiveZero { token: t }).is_err());
        assert!(
            d.submit(Operation::ReceiveZero { token: Token(9) })
                .is_err()
        );
    }

    /// Edge case 67, D-016: a listener never joins a port, and `Accept` has no
    /// meaning here.
    #[test]
    fn test_listener_is_refused() {
        let mut d = IocpDriver::new(8).unwrap();
        let l = TcpListener::bind("127.0.0.1:0").unwrap();
        assert!(d.register(l.as_socket(), Token(1)).is_err());
        let (_c, s) = pair();
        d.register(s.as_socket(), Token(2)).unwrap();
        assert!(d.submit(Operation::Accept { listener: Token(2) }).is_err());
    }

    #[test]
    fn test_waker_ends_a_wait() {
        let mut d = IocpDriver::new(8).unwrap();
        let w = d.waker();
        w.wake();
        w.wake();
        let mut out = Completions::new();
        let start = std::time::Instant::now();
        assert_eq!(d.wait(&mut out, Some(Duration::from_secs(5))).unwrap(), 0);
        assert!(start.elapsed() < Duration::from_secs(1));
    }

    #[test]
    fn test_nop_and_close_complete() {
        for mut d in both() {
            let (_c, s) = pair();
            let t = Token(4);
            d.register(s.as_socket(), t).unwrap();
            d.submit(Operation::Nop { token: t }).unwrap();
            assert_eq!(one(&mut d).kind, OperationKind::Nop);
            d.submit(Operation::ReceiveZero { token: t }).unwrap();
            d.submit(Operation::Close {
                token: t,
                abortive: false,
            })
            .unwrap();
            d.cancel(t, OperationKind::Receive).unwrap();
            let mut kinds = Vec::new();
            let mut out = Completions::new();
            while kinds.len() < 2 {
                d.wait(&mut out, Some(Duration::from_secs(1))).unwrap();
                kinds.extend(out.drain().map(|c| c.kind));
            }
            assert_eq!(kinds, [OperationKind::Receive, OperationKind::Close]);
        }
    }

    #[test]
    fn test_connect() {
        for mut d in both() {
            let l = TcpListener::bind("127.0.0.1:0").unwrap();
            let addr = l.local_addr().unwrap();
            let sock = sys::tcp_socket(false).unwrap();
            let t = Token(6);
            d.register(sock.as_socket(), t).unwrap();
            d.submit(Operation::Connect { token: t, addr }).unwrap();
            let c = one(&mut d);
            assert_eq!(c.kind, OperationKind::Connect);
            assert_eq!(c.result, Ok(0));
            let (mut s, peer) = l.accept().unwrap();
            let stream = TcpStream::from(sock);
            assert_eq!(stream.local_addr().unwrap(), peer, "context updated");
            s.write_all(b"z").unwrap();
            d.submit(Operation::Receive {
                token: t,
                buffer: Buffer::detached(SizeClass::SMALL),
            })
            .unwrap();
            assert_eq!(one(&mut d).result, Ok(1));
        }
    }

    #[test]
    fn test_connect_refused() {
        let mut d = IocpDriver::new(8).unwrap();
        let addr: SocketAddr = {
            let l = TcpListener::bind("127.0.0.1:0").unwrap();
            l.local_addr().unwrap()
        };
        let sock = sys::tcp_socket(false).unwrap();
        let t = Token(6);
        d.register(sock.as_socket(), t).unwrap();
        d.submit(Operation::Connect { token: t, addr }).unwrap();
        assert_eq!(one(&mut d).result, Err(OsErrorClass::Refused));
    }

    /// The send pends once the peer stops reading; a cancel gives the chunks back.
    #[test]
    fn test_blocked_send_cancels_with_its_chunks() {
        for mut d in both() {
            let (_c, s) = pair();
            let t = Token(8);
            d.register(s.as_socket(), t).unwrap();
            let mut pending = false;
            for _ in 0..4096 {
                d.submit(Operation::Send {
                    token: t,
                    chunks: SendChunks::from_buffer({
                        let mut b = Buffer::detached(SizeClass::SMALL);
                        let room = b.spare().len();
                        b.set_len(room);
                        b
                    }),
                })
                .unwrap();
                let mut out = Completions::new();
                if d.wait(&mut out, Some(Duration::from_millis(5))).unwrap() == 0 {
                    pending = true;
                    break;
                }
            }
            assert!(pending, "the peer's window filled");
            d.cancel(t, OperationKind::Send).unwrap();
            let r = one(&mut d);
            assert_eq!(r.kind, OperationKind::Send);
            assert!(r.chunks.is_some());
        }
    }
}