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
//! Operation code

#![allow(clippy::new_without_default)]

use crate::squeue::Entry;
use crate::sys;

pub mod types {
    use std::os::unix::io::RawFd;
    use bitflags::bitflags;
    use crate::sys;

    pub use sys::__kernel_timespec as Timespec;
    pub use sys::__kernel_rwf_t as RwFlags;

    #[derive(Debug, Clone, Copy)]
    pub enum Target {
        Fd(RawFd),

        /// The index of registered fd.
        Fixed(u32)
    }

    bitflags!{
        pub struct TimeoutFlags: u32 {
            const ABS = sys::IORING_TIMEOUT_ABS;
        }
    }

    bitflags!{
        pub struct FsyncFlags: u32 {
            const DATASYNC = sys::IORING_FSYNC_DATASYNC;
        }
    }
}


macro_rules! assign_fd {
    ( $sqe:ident . fd = $opfd:expr ) => {
        match $opfd {
            types::Target::Fd(fd) => $sqe.fd = fd,
            types::Target::Fixed(i) => {
                $sqe.fd = i as _;
                $sqe.flags |= crate::squeue::Flags::FIXED_FILE.bits();
            }
        }
    }
}

macro_rules! opcode {
    (
        $( #[$outer:meta] )*
        pub struct $name:ident {
            $( #[$new_meta:meta] )*
            $( $field:ident : $tname:ty ),* $(,)?
            ;;
            $(
                $( #[$opt_meta:meta] )*
                $opt_field:ident : $opt_tname:ty = $default:expr
            ),* $(,)?
        }

        pub fn build($self:ident) -> Entry $build_block:block
    ) => {
        $( #[$outer] )*
        pub struct $name {
            $( $field : $tname, )*
            $( $opt_field : $opt_tname, )*
        }

        impl $name {
            $( #[$new_meta] )*
            pub const fn new( $( $field : $tname ),* ) -> Self {
                $name {
                    $( $field , )*
                    $( $opt_field: $default, )*
                }
            }

            $(
                $( #[$opt_meta] )*
                pub const fn $opt_field(mut self, $opt_field: $opt_tname) -> Self {
                    self.$opt_field = $opt_field;
                    self
                }
            )*

            pub fn build($self) -> Entry $build_block
        }
    }
}

/// inline zeroed to improve codegen
#[inline(always)]
fn sqe_zeroed() -> sys::io_uring_sqe {
    unsafe { std::mem::zeroed() }
}

opcode!(
    /// Do not perform any I/O.
    ///
    /// This is useful for testing the performance of the io_uring implementation itself.
    #[derive(Debug)]
    pub struct Nop { ;; }

    pub fn build(self) -> Entry {
        let Nop {} = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_NOP as _;
        sqe.fd = -1;
        Entry(sqe)
    }
);

opcode!(
    /// Vectored read operations, similar to `preadv2 (2)`.
    ///
    /// The return values match those documented in the `preadv2 (2)` man pages.
    #[derive(Debug)]
    pub struct Readv {
        fd: types::Target,
        iovec: *mut libc::iovec,
        len: u32,
        ;;
        ioprio: u16 = 0,
        offset: libc::off_t = 0,
        /// specified for read operations, contains a bitwise OR of per-I/O flags,
        /// as described in the `preadv2 (2)` man page.
        rw_flags: types::RwFlags = 0
    }

    pub fn build(self) -> Entry {
        let Readv {
            fd,
            iovec, len, offset,
            ioprio, rw_flags
        } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_READV as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = iovec as _;
        sqe.len = len;
        sqe.__bindgen_anon_1.off = offset as _;
        sqe.__bindgen_anon_2.rw_flags = rw_flags;
        Entry(sqe)
    }
);

opcode!(
    /// Vectored write operations, similar to `pwritev2 (2)`.
    ///
    /// The return values match those documented in the `pwritev2 (2)` man pages.
    #[derive(Debug)]
    pub struct Writev {
        fd: types::Target,
        iovec: *const libc::iovec,
        len: u32,
        ;;
        ioprio: u16 = 0,
        offset: libc::off_t = 0,
        /// specified for write operations, contains a bitwise OR of per-I/O flags,
        /// as described in the `preadv2 (2)` man page.
        rw_flags: types::RwFlags = 0
    }

    pub fn build(self) -> Entry {
        let Writev {
            fd,
            iovec, len, offset,
            ioprio, rw_flags
        } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_WRITEV as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = iovec as _;
        sqe.len = len;
        sqe.__bindgen_anon_1.off = offset as _;
        sqe.__bindgen_anon_2.rw_flags = rw_flags;
        Entry(sqe)
    }
);

opcode!(
    /// File sync. See also `fsync (2)`.
    ///
    /// Note that, while I/O is initiated in the order in which it appears in the submission queue, completions are unordered.
    /// For example, an application which places a write I/O followed by an fsync in the submission queue cannot expect the fsync to apply to the write. The two operations execute in parallel, so the fsync may complete before the write is issued to the storage. The same is also true for previously issued writes that have not completed prior to the fsync.
    #[derive(Debug)]
    pub struct Fsync {
        fd: types::Target,
        ;;
        /// The `flags` bit mask may contain either 0, for a normal file integrity sync,
        /// or [types::FsyncFlags::DATASYNC] to provide data sync only semantics.
        /// See the descriptions of `O_SYNC` and `O_DSYNC` in the `open (2)` manual page for more information.
        flags: types::FsyncFlags = types::FsyncFlags::empty()
    }

    pub fn build(self) -> Entry {
        let Fsync { fd, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_FSYNC as _;
        assign_fd!(sqe.fd = fd);
        sqe.__bindgen_anon_2.fsync_flags = flags.bits();
        Entry(sqe)
    }
);

opcode!(
    /// Read from pre-mapped buffers.
    ///
    /// The return values match those documented in the `preadv2 (2)` man pages.
    #[derive(Debug)]
    pub struct ReadFixed {
        /// The `buf_index` is an index into an array of fixed buffers,
        /// and is only valid if fixed buffers were registered.
        fd: types::Target,
        buf: *mut u8,
        len: u32,
        buf_index: u16,
        ;;
        ioprio: u16 = 0,
        offset: libc::off_t = 0,
        /// specified for read operations, contains a bitwise OR of per-I/O flags,
        /// as described in the `preadv2 (2)` man page.
        rw_flags: types::RwFlags = 0
    }

    pub fn build(self) -> Entry {
        let ReadFixed {
            fd,
            buf, len, offset,
            buf_index,
            ioprio, rw_flags
        } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_READ_FIXED as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = buf as _;
        sqe.len = len;
        sqe.__bindgen_anon_1.off = offset as _;
        sqe.__bindgen_anon_2.rw_flags = rw_flags;
        sqe.__bindgen_anon_3.buf_index = buf_index;
        Entry(sqe)
    }
);

opcode!(
    /// Write to pre-mapped buffers.
    ///
    /// The return values match those documented in the `pwritev2 (2)` man pages.
    #[derive(Debug)]
    pub struct WriteFixed {
        /// The `buf_index` is an index into an array of fixed buffers,
        /// and is only valid if fixed buffers were registered.
        fd: types::Target,
        buf: *const u8,
        len: u32,
        buf_index: u16,
        ;;
        ioprio: u16 = 0,
        offset: libc::off_t = 0,
        /// specified for write operations, contains a bitwise OR of per-I/O flags,
        /// as described in the `preadv2 (2)` man page.
        rw_flags: types::RwFlags = 0
    }

    pub fn build(self) -> Entry {
        let WriteFixed {
            fd,
            buf, len, offset,
            buf_index,
            ioprio, rw_flags
        } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_WRITE_FIXED as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = buf as _;
        sqe.len = len;
        sqe.__bindgen_anon_1.off = offset as _;
        sqe.__bindgen_anon_2.rw_flags = rw_flags;
        sqe.__bindgen_anon_3.buf_index = buf_index;
        Entry(sqe)
    }
);

opcode!(
    /// Poll the specified fd.
    ///
    /// Unlike poll or epoll without `EPOLLONESHOT`, this interface always works in one shot mode.
    /// That is, once the poll operation is completed, it will have to be resubmitted.
    #[derive(Debug)]
    pub struct PollAdd {
        /// The bits that may be set in `flags` are defined in `<poll.h>`,
        /// and documented in `poll (2)`.
        fd: types::Target,
        flags: libc::c_short,
        ;;
    }

    pub fn build(self) -> Entry {
        let PollAdd { fd, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_POLL_ADD as _;
        assign_fd!(sqe.fd = fd);
        sqe.__bindgen_anon_2.poll_events = flags as _;
        Entry(sqe)
    }
);

opcode!(
    /// Remove an existing poll request.
    ///
    /// If found, the `result` method of the `cqueue::Entry` will return 0.
    /// If not found, `result` will return `-libc::ENOENT`.
    #[derive(Debug)]
    pub struct PollRemove {
        user_data: u64
        ;;
    }

    pub fn build(self) -> Entry {
        let PollRemove { user_data } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_POLL_REMOVE as _;
        sqe.fd = -1;
        sqe.addr = user_data as _;
        Entry(sqe)
    }
);

opcode!(
    /// Issue the equivalent of a `sync_file_range (2)` on the file descriptor.
    ///
    /// See also `sync_file_range (2)`. for the general description of the related system call.
    #[derive(Debug)]
    pub struct SyncFileRange {
        fd: types::Target,
        len: u32,
        ;;
        /// the offset method holds the offset in bytes
        offset: libc::off_t = 0,
        /// the flags method holds the flags for the command
        flags: u32 = 0
    }

    pub fn build(self) -> Entry {
        let SyncFileRange {
            fd,
            len, offset,
            flags
        } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_SYNC_FILE_RANGE as _;
        assign_fd!(sqe.fd = fd);
        sqe.len = len;
        sqe.__bindgen_anon_1.off = offset as _;
        sqe.__bindgen_anon_2.sync_range_flags = flags;
        Entry(sqe)
    }
);

opcode!(
    /// Issue the equivalent of a `sendmsg (2)` system call.
    ///
    /// fd must be set to the socket file descriptor, addr must contains a pointer to the msghdr structure,
    /// and flags holds the flags associated with the system call.
    /// See also `sendmsg (2)`. for the general description of the related system call.
    #[derive(Debug)]
    pub struct SendMsg {
        fd: types::Target,
        msg: *const libc::msghdr,
        ;;
        ioprio: u16 = 0,
        flags: u32 = 0
    }

    pub fn build(self) -> Entry {
        let SendMsg { fd, msg, ioprio, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_SENDMSG as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = msg as _;
        sqe.len = 1;
        sqe.__bindgen_anon_2.msg_flags = flags;
        Entry(sqe)
    }
);

opcode!(
    /// Works just like [SendMsg], except for instead.
    ///
    /// See the description of [SendMsg].
    #[derive(Debug)]
    pub struct RecvMsg {
        fd: types::Target,
        msg: *mut libc::msghdr,
        ;;
        ioprio: u16 = 0,
        flags: u32 = 0
    }

    pub fn build(self) -> Entry {
        let RecvMsg { fd, msg, ioprio, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_RECVMSG as _;
        assign_fd!(sqe.fd = fd);
        sqe.ioprio = ioprio;
        sqe.addr = msg as _;
        sqe.len = 1;
        sqe.__bindgen_anon_2.msg_flags = flags;
        Entry(sqe)
    }
);

opcode!(
    /// This command will register a timeout operation.
    ///
    /// A timeout will trigger a wakeup event on the completion ring for anyone waiting for events.
    /// A timeout condition is met when either the specified timeout expires, or the specified number of events have completed.
    /// Either condition will trigger the event.
    /// The request will complete with `-ETIME` if the timeout got completed through expiration of the timer,
    /// or 0 if the timeout got completed through requests completing on their own.
    /// If the timeout was cancelled before it expired, the request will complete with `-ECANCELED`.
    #[derive(Debug)]
    pub struct Timeout {
        timespec: *const types::Timespec,
        ;;
        /// `count` may contain a completion event count. If not set, this defaults to 1.
        count: u32 = 0,

        /// `flags` may contain [types::TimeoutFlags::ABS] for an absolutel timeout value, or 0 for a relative timeout.
        flags: types::TimeoutFlags = types::TimeoutFlags::empty()
    }

    pub fn build(self) -> Entry {
        let Timeout { timespec, count, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_TIMEOUT as _;
        sqe.fd = -1;
        sqe.addr = timespec as _;
        sqe.len = 1;
        sqe.__bindgen_anon_1.off = count as _;
        sqe.__bindgen_anon_2.timeout_flags = flags.bits();
        Entry(sqe)
    }
);

opcode!(
    /// Attempt to remove an existing timeout operation.
    pub struct TimeoutRemove {
        user_data: u64,
        ;;
        flags: types::TimeoutFlags = types::TimeoutFlags::empty()
    }

    pub fn build(self) -> Entry {
        let TimeoutRemove { user_data, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_TIMEOUT_REMOVE as _;
        sqe.fd = -1;
        sqe.addr = user_data as _;
        sqe.__bindgen_anon_2.timeout_flags = flags.bits();
        Entry(sqe)
    }
);

opcode!(
    /// Issue the equivalent of an `accept4 (2)` system call.
    pub struct Accept {
        fd: types::Target,
        addr: *mut libc::sockaddr,
        addrlen: *mut libc::socklen_t
        ;;
        flags: u32 = 0
    }

    pub fn build(self) -> Entry {
        let Accept { fd, addr, addrlen, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_ACCEPT as _;
        assign_fd!(sqe.fd = fd);
        sqe.addr = addr as _;
        sqe.__bindgen_anon_1.addr2 = addrlen as _;
        sqe.__bindgen_anon_2.accept_flags = flags;
        Entry(sqe)
    }
);

opcode!(
    /// Attempt to cancel an already issued request.
    pub struct AsyncCancel {
        user_data: u64,
        ;;

        // TODO flags
    }

    pub fn build(self) -> Entry {
        let AsyncCancel { user_data } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_ASYNC_CANCEL as _;
        sqe.fd = -1;
        sqe.addr = user_data as _;
        Entry(sqe)
    }
);

opcode!(
    /// This request must be linked with
    /// another request through [Flags::IO_LINK](crate::squeue::Flags::IO_LINK) which is described below.
    /// Unlike [Timeout], [LinkTimeout] acts on the linked request, not the completion queue.
    pub struct LinkTimeout {
        timespec: *const types::Timespec,
        ;;
        flags: types::TimeoutFlags = types::TimeoutFlags::empty()
    }

    pub fn build(self) -> Entry {
        let LinkTimeout { timespec, flags } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_LINK_TIMEOUT as _;
        sqe.fd = -1;
        sqe.addr = timespec as _;
        sqe.len = 1;
        sqe.__bindgen_anon_2.timeout_flags = flags.bits();
        Entry(sqe)
    }
);

opcode!(
    /// Issue the equivalent of a `connect (2)` system call.
    pub struct Connect {
        fd: types::Target,
        addr: *const libc::sockaddr,
        addrlen: libc::socklen_t
        ;;
    }

    pub fn build(self) -> Entry {
        let Connect { fd, addr, addrlen } = self;

        let mut sqe = sqe_zeroed();
        sqe.opcode = sys::IORING_OP_CONNECT as _;
        assign_fd!(sqe.fd = fd);
        sqe.addr = addr as _;
        sqe.__bindgen_anon_1.off = addrlen as _;
        Entry(sqe)
    }
);