aboutsummaryrefslogtreecommitdiff
path: root/src/io/io_linux.zig
blob: 794f18f90014858818f74c542dfab4f89e130d3c (plain) (blame)
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
const std = @import("std");
const assert = std.debug.assert;
const os = std.os;
const linux = os.linux;
const IO_Uring = linux.IO_Uring;
const io_uring_cqe = linux.io_uring_cqe;
const io_uring_sqe = linux.io_uring_sqe;

const FIFO = @import("./fifo.zig").FIFO;
const IO = @This();

ring: IO_Uring,

/// Operations not yet submitted to the kernel and waiting on available space in the
/// submission queue.
unqueued: FIFO(Completion) = .{},

/// Completions that are ready to have their callbacks run.
completed: FIFO(Completion) = .{},

pub fn init(entries: u12, flags: u32) !IO {
    return IO{ .ring = try IO_Uring.init(entries, flags) };
}

pub fn deinit(self: *IO) void {
    self.ring.deinit();
}

/// Pass all queued submissions to the kernel and peek for completions.
pub fn tick(self: *IO) !void {
    // We assume that all timeouts submitted by `run_for_ns()` will be reaped by `run_for_ns()`
    // and that `tick()` and `run_for_ns()` cannot be run concurrently.
    // Therefore `timeouts` here will never be decremented and `etime` will always be false.
    var timeouts: usize = 0;
    var etime = false;

    try self.flush(0, &timeouts, &etime);
    assert(etime == false);

    // Flush any SQEs that were queued while running completion callbacks in `flush()`:
    // This is an optimization to avoid delaying submissions until the next tick.
    // At the same time, we do not flush any ready CQEs since SQEs may complete synchronously.
    // We guard against an io_uring_enter() syscall if we know we do not have any queued SQEs.
    // We cannot use `self.ring.sq_ready()` here since this counts flushed and unflushed SQEs.
    const queued = self.ring.sq.sqe_tail -% self.ring.sq.sqe_head;
    if (queued > 0) {
        try self.flush_submissions(0, &timeouts, &etime);
        assert(etime == false);
    }
}

/// Pass all queued submissions to the kernel and run for `nanoseconds`.
/// The `nanoseconds` argument is a u63 to allow coercion to the i64 used
/// in the __kernel_timespec struct.
pub fn run_for_ns(self: *IO, nanoseconds: u63) !void {
    // We must use the same clock source used by io_uring (CLOCK_MONOTONIC) since we specify the
    // timeout below as an absolute value. Otherwise, we may deadlock if the clock sources are
    // dramatically different. Any kernel that supports io_uring will support CLOCK_MONOTONIC.
    var current_ts: os.timespec = undefined;
    os.clock_gettime(os.CLOCK_MONOTONIC, &current_ts) catch unreachable;
    // The absolute CLOCK_MONOTONIC time after which we may return from this function:
    const timeout_ts: os.__kernel_timespec = .{
        .tv_sec = current_ts.tv_sec,
        .tv_nsec = current_ts.tv_nsec + nanoseconds,
    };
    var timeouts: usize = 0;
    var etime = false;
    while (!etime) {
        const timeout_sqe = self.ring.get_sqe() catch blk: {
            // The submission queue is full, so flush submissions to make space:
            try self.flush_submissions(0, &timeouts, &etime);
            break :blk self.ring.get_sqe() catch unreachable;
        };
        // Submit an absolute timeout that will be canceled if any other SQE completes first:
        linux.io_uring_prep_timeout(timeout_sqe, &timeout_ts, 1, os.IORING_TIMEOUT_ABS);
        timeout_sqe.user_data = 0;
        timeouts += 1;
        // The amount of time this call will block is bounded by the timeout we just submitted:
        try self.flush(1, &timeouts, &etime);
    }
    // Reap any remaining timeouts, which reference the timespec in the current stack frame.
    // The busy loop here is required to avoid a potential deadlock, as the kernel determines
    // when the timeouts are pushed to the completion queue, not us.
    while (timeouts > 0) _ = try self.flush_completions(0, &timeouts, &etime);
}

fn flush(self: *IO, wait_nr: u32, timeouts: *usize, etime: *bool) !void {
    // Flush any queued SQEs and reuse the same syscall to wait for completions if required:
    try self.flush_submissions(wait_nr, timeouts, etime);
    // We can now just peek for any CQEs without waiting and without another syscall:
    try self.flush_completions(0, timeouts, etime);
    // Run completions only after all completions have been flushed:
    // Loop on a copy of the linked list, having reset the list first, so that any synchronous
    // append on running a completion is executed only the next time round the event loop,
    // without creating an infinite loop.
    {
        var copy = self.completed;
        self.completed = .{};
        while (copy.pop()) |completion| completion.complete();
    }
    // Again, loop on a copy of the list to avoid an infinite loop:
    {
        var copy = self.unqueued;
        self.unqueued = .{};
        while (copy.pop()) |completion| self.enqueue(completion);
    }
}

fn flush_completions(self: *IO, wait_nr: u32, timeouts: *usize, etime: *bool) !void {
    var cqes: [256]io_uring_cqe = undefined;
    var wait_remaining = wait_nr;
    while (true) {
        // Guard against waiting indefinitely (if there are too few requests inflight),
        // especially if this is not the first time round the loop:
        const completed = self.ring.copy_cqes(&cqes, wait_remaining) catch |err| switch (err) {
            error.SignalInterrupt => continue,
            else => return err,
        };
        if (completed > wait_remaining) wait_remaining = 0 else wait_remaining -= completed;
        for (cqes[0..completed]) |cqe| {
            if (cqe.user_data == 0) {
                timeouts.* -= 1;
                // We are only done if the timeout submitted was completed due to time, not if
                // it was completed due to the completion of an event, in which case `cqe.res`
                // would be 0. It is possible for multiple timeout operations to complete at the
                // same time if the nanoseconds value passed to `run_for_ns()` is very short.
                if (-cqe.res == os.ETIME) etime.* = true;
                continue;
            }
            const completion = @intToPtr(*Completion, @intCast(usize, cqe.user_data));
            completion.result = cqe.res;
            // We do not run the completion here (instead appending to a linked list) to avoid:
            // * recursion through `flush_submissions()` and `flush_completions()`,
            // * unbounded stack usage, and
            // * confusing stack traces.
            self.completed.push(completion);
        }
        if (completed < cqes.len) break;
    }
}

fn flush_submissions(self: *IO, wait_nr: u32, timeouts: *usize, etime: *bool) !void {
    while (true) {
        _ = self.ring.submit_and_wait(wait_nr) catch |err| switch (err) {
            error.SignalInterrupt => continue,
            // Wait for some completions and then try again:
            // See https://github.com/axboe/liburing/issues/281 re: error.SystemResources.
            // Be careful also that copy_cqes() will flush before entering to wait (it does):
            // https://github.com/axboe/liburing/commit/35c199c48dfd54ad46b96e386882e7ac341314c5
            error.CompletionQueueOvercommitted, error.SystemResources => {
                try self.flush_completions(1, timeouts, etime);
                continue;
            },
            else => return err,
        };
        break;
    }
}

fn enqueue(self: *IO, completion: *Completion) void {
    const sqe = self.ring.get_sqe() catch |err| switch (err) {
        error.SubmissionQueueFull => {
            self.unqueued.push(completion);
            return;
        },
    };
    completion.prep(sqe);
}

/// This struct holds the data needed for a single io_uring operation
pub const Completion = struct {
    io: *IO,
    result: i32 = undefined,
    next: ?*Completion = null,
    operation: Operation,
    // This is one of the usecases for c_void outside of C code and as such c_void will
    // be replaced with anyopaque eventually: https://github.com/ziglang/zig/issues/323
    context: ?*c_void,
    callback: fn (context: ?*c_void, completion: *Completion, result: *const c_void) void,

    fn prep(completion: *Completion, sqe: *io_uring_sqe) void {
        switch (completion.operation) {
            .accept => |*op| {
                linux.io_uring_prep_accept(
                    sqe,
                    op.socket,
                    &op.address,
                    &op.address_size,
                    os.SOCK_CLOEXEC,
                );
            },
            .close => |op| {
                linux.io_uring_prep_close(sqe, op.fd);
            },
            .connect => |*op| {
                linux.io_uring_prep_connect(
                    sqe,
                    op.socket,
                    &op.address.any,
                    op.address.getOsSockLen(),
                );
            },
            .fsync => |op| {
                linux.io_uring_prep_fsync(sqe, op.fd, 0);
            },
            .read => |op| {
                linux.io_uring_prep_read(
                    sqe,
                    op.fd,
                    op.buffer[0..buffer_limit(op.buffer.len)],
                    op.offset,
                );
            },
            .recv => |op| {
                linux.io_uring_prep_recv(sqe, op.socket, op.buffer, os.MSG_NOSIGNAL);
            },
            .send => |op| {
                linux.io_uring_prep_send(sqe, op.socket, op.buffer, os.MSG_NOSIGNAL);
            },
            .timeout => |*op| {
                linux.io_uring_prep_timeout(sqe, &op.timespec, 0, 0);
            },
            .write => |op| {
                linux.io_uring_prep_write(
                    sqe,
                    op.fd,
                    op.buffer[0..buffer_limit(op.buffer.len)],
                    op.offset,
                );
            },
        }
        sqe.user_data = @ptrToInt(completion);
    }

    fn complete(completion: *Completion) void {
        switch (completion.operation) {
            .accept => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EAGAIN => error.WouldBlock,
                    os.EBADF => error.FileDescriptorInvalid,
                    os.ECONNABORTED => error.ConnectionAborted,
                    os.EFAULT => unreachable,
                    os.EINVAL => error.SocketNotListening,
                    os.EMFILE => error.ProcessFdQuotaExceeded,
                    os.ENFILE => error.SystemFdQuotaExceeded,
                    os.ENOBUFS => error.SystemResources,
                    os.ENOMEM => error.SystemResources,
                    os.ENOTSOCK => error.FileDescriptorNotASocket,
                    os.EOPNOTSUPP => error.OperationNotSupported,
                    os.EPERM => error.PermissionDenied,
                    os.EPROTO => error.ProtocolFailure,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else @intCast(os.socket_t, completion.result);
                completion.callback(completion.context, completion, &result);
            },
            .close => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {}, // A success, see https://github.com/ziglang/zig/issues/2425
                    os.EBADF => error.FileDescriptorInvalid,
                    os.EDQUOT => error.DiskQuota,
                    os.EIO => error.InputOutput,
                    os.ENOSPC => error.NoSpaceLeft,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else assert(completion.result == 0);
                completion.callback(completion.context, completion, &result);
            },
            .connect => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EACCES => error.AccessDenied,
                    os.EADDRINUSE => error.AddressInUse,
                    os.EADDRNOTAVAIL => error.AddressNotAvailable,
                    os.EAFNOSUPPORT => error.AddressFamilyNotSupported,
                    os.EAGAIN, os.EINPROGRESS => error.WouldBlock,
                    os.EALREADY => error.OpenAlreadyInProgress,
                    os.EBADF => error.FileDescriptorInvalid,
                    os.ECONNREFUSED => error.ConnectionRefused,
                    os.ECONNRESET => error.ConnectionResetByPeer,
                    os.EFAULT => unreachable,
                    os.EISCONN => error.AlreadyConnected,
                    os.ENETUNREACH => error.NetworkUnreachable,
                    os.ENOENT => error.FileNotFound,
                    os.ENOTSOCK => error.FileDescriptorNotASocket,
                    os.EPERM => error.PermissionDenied,
                    os.EPROTOTYPE => error.ProtocolNotSupported,
                    os.ETIMEDOUT => error.ConnectionTimedOut,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else assert(completion.result == 0);
                completion.callback(completion.context, completion, &result);
            },
            .fsync => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EBADF => error.FileDescriptorInvalid,
                    os.EDQUOT => error.DiskQuota,
                    os.EINVAL => error.ArgumentsInvalid,
                    os.EIO => error.InputOutput,
                    os.ENOSPC => error.NoSpaceLeft,
                    os.EROFS => error.ReadOnlyFileSystem,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else assert(completion.result == 0);
                completion.callback(completion.context, completion, &result);
            },
            .read => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EAGAIN => error.WouldBlock,
                    os.EBADF => error.NotOpenForReading,
                    os.ECONNRESET => error.ConnectionResetByPeer,
                    os.EFAULT => unreachable,
                    os.EINVAL => error.Alignment,
                    os.EIO => error.InputOutput,
                    os.EISDIR => error.IsDir,
                    os.ENOBUFS => error.SystemResources,
                    os.ENOMEM => error.SystemResources,
                    os.ENXIO => error.Unseekable,
                    os.EOVERFLOW => error.Unseekable,
                    os.ESPIPE => error.Unseekable,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else @intCast(usize, completion.result);
                completion.callback(completion.context, completion, &result);
            },
            .recv => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EAGAIN => error.WouldBlock,
                    os.EBADF => error.FileDescriptorInvalid,
                    os.ECONNREFUSED => error.ConnectionRefused,
                    os.EFAULT => unreachable,
                    os.EINVAL => unreachable,
                    os.ENOMEM => error.SystemResources,
                    os.ENOTCONN => error.SocketNotConnected,
                    os.ENOTSOCK => error.FileDescriptorNotASocket,
                    os.ECONNRESET => error.ConnectionResetByPeer,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else @intCast(usize, completion.result);
                completion.callback(completion.context, completion, &result);
            },
            .send => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EACCES => error.AccessDenied,
                    os.EAGAIN => error.WouldBlock,
                    os.EALREADY => error.FastOpenAlreadyInProgress,
                    os.EAFNOSUPPORT => error.AddressFamilyNotSupported,
                    os.EBADF => error.FileDescriptorInvalid,
                    os.ECONNRESET => error.ConnectionResetByPeer,
                    os.EDESTADDRREQ => unreachable,
                    os.EFAULT => unreachable,
                    os.EINVAL => unreachable,
                    os.EISCONN => unreachable,
                    os.EMSGSIZE => error.MessageTooBig,
                    os.ENOBUFS => error.SystemResources,
                    os.ENOMEM => error.SystemResources,
                    os.ENOTCONN => error.SocketNotConnected,
                    os.ENOTSOCK => error.FileDescriptorNotASocket,
                    os.EOPNOTSUPP => error.OperationNotSupported,
                    os.EPIPE => error.BrokenPipe,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else @intCast(usize, completion.result);
                completion.callback(completion.context, completion, &result);
            },
            .timeout => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.ECANCELED => error.Canceled,
                    os.ETIME => {}, // A success.
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else unreachable;
                completion.callback(completion.context, completion, &result);
            },
            .write => {
                const result = if (completion.result < 0) switch (-completion.result) {
                    os.EINTR => {
                        completion.io.enqueue(completion);
                        return;
                    },
                    os.EAGAIN => error.WouldBlock,
                    os.EBADF => error.NotOpenForWriting,
                    os.EDESTADDRREQ => error.NotConnected,
                    os.EDQUOT => error.DiskQuota,
                    os.EFAULT => unreachable,
                    os.EFBIG => error.FileTooBig,
                    os.EINVAL => error.Alignment,
                    os.EIO => error.InputOutput,
                    os.ENOSPC => error.NoSpaceLeft,
                    os.ENXIO => error.Unseekable,
                    os.EOVERFLOW => error.Unseekable,
                    os.EPERM => error.AccessDenied,
                    os.EPIPE => error.BrokenPipe,
                    os.ESPIPE => error.Unseekable,
                    else => |errno| os.unexpectedErrno(@intCast(usize, errno)),
                } else @intCast(usize, completion.result);
                completion.callback(completion.context, completion, &result);
            },
        }
    }
};

/// This union encodes the set of operations supported as well as their arguments.
const Operation = union(enum) {
    accept: struct {
        socket: os.socket_t,
        address: os.sockaddr = undefined,
        address_size: os.socklen_t = @sizeOf(os.sockaddr),
    },
    close: struct {
        fd: os.fd_t,
    },
    connect: struct {
        socket: os.socket_t,
        address: std.net.Address,
    },
    fsync: struct {
        fd: os.fd_t,
    },
    read: struct {
        fd: os.fd_t,
        buffer: []u8,
        offset: u64,
    },
    recv: struct {
        socket: os.socket_t,
        buffer: []u8,
    },
    send: struct {
        socket: os.socket_t,
        buffer: []const u8,
    },
    timeout: struct {
        timespec: os.__kernel_timespec,
    },
    write: struct {
        fd: os.fd_t,
        buffer: []const u8,
        offset: u64,
    },
};

pub const AcceptError = error{
    WouldBlock,
    FileDescriptorInvalid,
    ConnectionAborted,
    SocketNotListening,
    ProcessFdQuotaExceeded,
    SystemFdQuotaExceeded,
    SystemResources,
    FileDescriptorNotASocket,
    OperationNotSupported,
    PermissionDenied,
    ProtocolFailure,
} || os.UnexpectedError;

pub fn accept(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: AcceptError!os.socket_t,
    ) void,
    completion: *Completion,
    socket: os.socket_t,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const AcceptError!os.socket_t, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .accept = .{
                .socket = socket,
                .address = undefined,
                .address_size = @sizeOf(os.sockaddr),
            },
        },
    };
    self.enqueue(completion);
}

pub const CloseError = error{
    FileDescriptorInvalid,
    DiskQuota,
    InputOutput,
    NoSpaceLeft,
} || os.UnexpectedError;

pub fn close(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: CloseError!void,
    ) void,
    completion: *Completion,
    fd: os.fd_t,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const CloseError!void, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .close = .{ .fd = fd },
        },
    };
    self.enqueue(completion);
}

pub const ConnectError = error{
    AccessDenied,
    AddressInUse,
    AddressNotAvailable,
    AddressFamilyNotSupported,
    WouldBlock,
    OpenAlreadyInProgress,
    FileDescriptorInvalid,
    ConnectionRefused,
    AlreadyConnected,
    NetworkUnreachable,
    FileNotFound,
    FileDescriptorNotASocket,
    PermissionDenied,
    ProtocolNotSupported,
    ConnectionTimedOut,
} || os.UnexpectedError;

pub fn connect(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: ConnectError!void,
    ) void,
    completion: *Completion,
    socket: os.socket_t,
    address: std.net.Address,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const ConnectError!void, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .connect = .{
                .socket = socket,
                .address = address,
            },
        },
    };
    self.enqueue(completion);
}

pub const FsyncError = error{
    FileDescriptorInvalid,
    DiskQuota,
    ArgumentsInvalid,
    InputOutput,
    NoSpaceLeft,
    ReadOnlyFileSystem,
} || os.UnexpectedError;

pub fn fsync(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: FsyncError!void,
    ) void,
    completion: *Completion,
    fd: os.fd_t,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const FsyncError!void, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .fsync = .{
                .fd = fd,
            },
        },
    };
    self.enqueue(completion);
}

pub const ReadError = error{
    WouldBlock,
    NotOpenForReading,
    ConnectionResetByPeer,
    Alignment,
    InputOutput,
    IsDir,
    SystemResources,
    Unseekable,
} || os.UnexpectedError;

pub fn read(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: ReadError!usize,
    ) void,
    completion: *Completion,
    fd: os.fd_t,
    buffer: []u8,
    offset: u64,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const ReadError!usize, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .read = .{
                .fd = fd,
                .buffer = buffer,
                .offset = offset,
            },
        },
    };
    self.enqueue(completion);
}

pub const RecvError = error{
    WouldBlock,
    FileDescriptorInvalid,
    ConnectionRefused,
    SystemResources,
    SocketNotConnected,
    FileDescriptorNotASocket,
} || os.UnexpectedError;

pub fn recv(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: RecvError!usize,
    ) void,
    completion: *Completion,
    socket: os.socket_t,
    buffer: []u8,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const RecvError!usize, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .recv = .{
                .socket = socket,
                .buffer = buffer,
            },
        },
    };
    self.enqueue(completion);
}

pub const SendError = error{
    AccessDenied,
    WouldBlock,
    FastOpenAlreadyInProgress,
    AddressFamilyNotSupported,
    FileDescriptorInvalid,
    ConnectionResetByPeer,
    MessageTooBig,
    SystemResources,
    SocketNotConnected,
    FileDescriptorNotASocket,
    OperationNotSupported,
    BrokenPipe,
} || os.UnexpectedError;

pub fn send(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: SendError!usize,
    ) void,
    completion: *Completion,
    socket: os.socket_t,
    buffer: []const u8,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const SendError!usize, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .send = .{
                .socket = socket,
                .buffer = buffer,
            },
        },
    };
    self.enqueue(completion);
}

pub const TimeoutError = error{Canceled} || os.UnexpectedError;

pub fn timeout(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: TimeoutError!void,
    ) void,
    completion: *Completion,
    nanoseconds: u63,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const TimeoutError!void, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .timeout = .{
                .timespec = .{ .tv_sec = 0, .tv_nsec = nanoseconds },
            },
        },
    };
    self.enqueue(completion);
}

pub const WriteError = error{
    WouldBlock,
    NotOpenForWriting,
    NotConnected,
    DiskQuota,
    FileTooBig,
    Alignment,
    InputOutput,
    NoSpaceLeft,
    Unseekable,
    AccessDenied,
    BrokenPipe,
} || os.UnexpectedError;

pub fn write(
    self: *IO,
    comptime Context: type,
    context: Context,
    comptime callback: fn (
        context: Context,
        completion: *Completion,
        result: WriteError!usize,
    ) void,
    completion: *Completion,
    fd: os.fd_t,
    buffer: []const u8,
    offset: u64,
) void {
    completion.* = .{
        .io = self,
        .context = context,
        .callback = struct {
            fn wrapper(ctx: ?*c_void, comp: *Completion, res: *const c_void) void {
                callback(
                    @intToPtr(Context, @ptrToInt(ctx)),
                    comp,
                    @intToPtr(*const WriteError!usize, @ptrToInt(res)).*,
                );
            }
        }.wrapper,
        .operation = .{
            .write = .{
                .fd = fd,
                .buffer = buffer,
                .offset = offset,
            },
        },
    };
    self.enqueue(completion);
}

pub fn openSocket(family: u32, sock_type: u32, protocol: u32) !os.socket_t {
    return os.socket(family, sock_type, protocol);
}