authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2022-05-12 13:28:34+02:00
committergravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2022-05-25 13:53:09+02:00
log7b3e5ce0b3d3e6c8e24dd147d23eec3fe5088d74
tree2294e90753feb3243a69a0175de194112f9a90f9
parent71e2a56e3ef7aba10cc0648aab786973cf8416bc

io_uring: add provide_buffers and remove_buffers

These functions are needed to implement automatic buffer selection. This maps to the IORING_OP_PROVIDE_BUFFERS and IORING_OP_PROVIDE_BUFFERS ops.

1 files changed, 57 insertions(+), 0 deletions(-)

lib/std/os/linux/io_uring.zig+57
......@@ -857,6 +857,41 @@ pub const IO_Uring = struct {
857857 return sqe;
858858 }
859859
860 /// Queues (but does not submit) an SQE to provide a group of buffers used for commands that read/receive data.
861 /// Returns a pointer to the SQE.
862 ///
863 /// Provided buffers can be used in `read`, `recv` or `recvmsg` commands via .buffer_selection.
864 ///
865 /// The kernel expects a contiguous block of memory of size (buffers_count * buffer_size).
866 pub fn provide_buffers(
867 self: *IO_Uring,
868 user_data: u64,
869 buffers: [*]u8,
870 buffers_count: usize,
871 buffer_size: usize,
872 group_id: usize,
873 buffer_id: usize,
874 ) !*io_uring_sqe {
875 const sqe = try self.get_sqe();
876 io_uring_prep_provide_buffers(sqe, buffers, buffers_count, buffer_size, group_id, buffer_id);
877 sqe.user_data = user_data;
878 return sqe;
879 }
880
881 /// Queues (but does not submit) an SQE to remove a group of provided buffers.
882 /// Returns a pointer to the SQE.
883 pub fn remove_buffers(
884 self: *IO_Uring,
885 user_data: u64,
886 buffers_count: usize,
887 group_id: usize,
888 ) !*io_uring_sqe {
889 const sqe = try self.get_sqe();
890 io_uring_prep_remove_buffers(sqe, buffers_count, group_id);
891 sqe.user_data = user_data;
892 return sqe;
893 }
894
860895 /// Registers an array of file descriptors.
861896 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must
862897 /// retrieve a reference to the file, and once I/O has completed the file reference must be
......@@ -1508,6 +1543,28 @@ pub fn io_uring_prep_linkat(
15081543 sqe.rw_flags = flags;
15091544}
15101545
1546pub fn io_uring_prep_provide_buffers(
1547 sqe: *io_uring_sqe,
1548 buffers: [*]u8,
1549 num: usize,
1550 buffer_len: usize,
1551 group_id: usize,
1552 buffer_id: usize,
1553) void {
1554 const ptr = @ptrToInt(buffers);
1555 io_uring_prep_rw(.PROVIDE_BUFFERS, sqe, @intCast(i32, num), ptr, buffer_len, buffer_id);
1556 sqe.buf_index = @intCast(u16, group_id);
1557}
1558
1559pub fn io_uring_prep_remove_buffers(
1560 sqe: *io_uring_sqe,
1561 num: usize,
1562 group_id: usize,
1563) void {
1564 io_uring_prep_rw(.REMOVE_BUFFERS, sqe, @intCast(i32, num), 0, 0, 0);
1565 sqe.buf_index = @intCast(u16, group_id);
1566}
1567
15111568test "structs/offsets/entries" {
15121569 if (builtin.os.tag != .linux) return error.SkipZigTest;
15131570