Skip to content

Commit 98b3a71

Browse files
committed
IoUring: use the splice flags type for splice and tee
Use appropriately sized integers where applicable Signed-off-by: Bernard Assan <mega.alpha100@gmail.com>
1 parent 645acf1 commit 98b3a71

File tree

1 file changed

+80
-34
lines changed

1 file changed

+80
-34
lines changed

lib/std/os/linux/IoUring.zig

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -424,25 +424,32 @@ pub fn splice(
424424
off_in: u64,
425425
fd_out: linux.fd_t,
426426
off_out: u64,
427-
len: usize,
427+
len: u32,
428+
flags: uflags.Splice,
428429
) !*Sqe {
429430
const sqe = try self.get_sqe();
430-
sqe.prep_splice(fd_in, off_in, fd_out, off_out, len);
431+
sqe.prep_splice(
432+
fd_in,
433+
off_in,
434+
fd_out,
435+
off_out,
436+
len,
437+
flags,
438+
);
431439
sqe.user_data = user_data;
432440
return sqe;
433441
}
434442

435-
// COMMIT: ignored flags for splice and tee lets see if they become important
436-
// in the future
437443
pub fn tee(
438444
self: *IoUring,
439445
user_data: u64,
440446
fd_in: linux.fd_t,
441447
fd_out: linux.fd_t,
442-
len: usize,
448+
len: u32,
449+
flags: uflags.Splice,
443450
) !*Sqe {
444451
const sqe = try self.get_sqe();
445-
sqe.prep_tee(fd_in, fd_out, len);
452+
sqe.prep_tee(fd_in, fd_out, len, flags);
446453
sqe.user_data = user_data;
447454
return sqe;
448455
}
@@ -759,8 +766,6 @@ pub fn timeout_update(
759766
/// Queues (but does not submit) an SQE to perform an `accept4(2)` on a socket.
760767
/// Returns a pointer to the SQE.
761768
/// Available since 5.5
762-
// TODO: can't we make the sockaddr and socklen_t combo in our api better?
763-
// Investigate this
764769
pub fn accept(
765770
self: *IoUring,
766771
user_data: u64,
@@ -939,7 +944,7 @@ pub fn listen(
939944
self: *IoUring,
940945
user_data: u64,
941946
fd: linux.fd_t,
942-
backlog: usize,
947+
backlog: u32,
943948
// liburing doesn't have this flag, hence 0 should be passed
944949
// TODO: consider removing this and all flags like this
945950
flags: u32,
@@ -1339,21 +1344,24 @@ pub fn recv_multishot(
13391344
/// buffer_selection.
13401345
///
13411346
/// The kernel expects a contiguous block of memory of size (buffers_count *
1342-
/// buffer_size).
1343-
// TODO: why not use a slice with `buffers_count`
1347+
/// buffer_len).
13441348
pub fn provide_buffers(
13451349
self: *IoUring,
13461350
user_data: u64,
1351+
/// an array of `buffers_count` buffers of len `buffer_len` laid out as a
1352+
/// contiguous slice of memory
13471353
buffers: [*]u8,
1348-
buffer_size: usize,
1349-
buffers_count: usize,
1350-
group_id: usize,
1351-
buffer_id: usize,
1354+
/// lenght of each buffer in `buffers`
1355+
buffer_len: u32,
1356+
/// count of buffer in `buffers`
1357+
buffers_count: u32,
1358+
group_id: u32,
1359+
buffer_id: u32,
13521360
) !*Sqe {
13531361
const sqe = try self.get_sqe();
13541362
sqe.prep_provide_buffers(
13551363
buffers,
1356-
buffer_size,
1364+
buffer_len,
13571365
buffers_count,
13581366
group_id,
13591367
buffer_id,
@@ -1367,8 +1375,8 @@ pub fn provide_buffers(
13671375
pub fn remove_buffers(
13681376
self: *IoUring,
13691377
user_data: u64,
1370-
buffers_count: usize,
1371-
group_id: usize,
1378+
buffers_count: u32,
1379+
group_id: u32,
13721380
) !*Sqe {
13731381
const sqe = try self.get_sqe();
13741382
sqe.prep_remove_buffers(buffers_count, group_id);
@@ -2550,26 +2558,54 @@ pub const Sqe = extern struct {
25502558
sqe.prep_rw(.writev, fd, @intFromPtr(iovecs.ptr), iovecs.len, offset);
25512559
}
25522560

2553-
pub fn prep_write_fixed(sqe: *Sqe, fd: linux.fd_t, buffer: []const u8, offset: u64, buffer_index: u16) void {
2561+
pub fn prep_write_fixed(
2562+
sqe: *Sqe,
2563+
fd: linux.fd_t,
2564+
buffer: []const u8,
2565+
offset: u64,
2566+
buffer_index: u16,
2567+
) void {
25542568
sqe.prep_rw(.write_fixed, fd, @intFromPtr(buffer.ptr), buffer.len, offset);
25552569
sqe.buf_index = buffer_index;
25562570
}
25572571

2558-
pub fn prep_writev_fixed(sqe: *Sqe, fd: linux.fd_t, iovecs: []const posix.iovec_const, offset: u64, buffer_index: u16) void {
2572+
pub fn prep_writev_fixed(
2573+
sqe: *Sqe,
2574+
fd: linux.fd_t,
2575+
iovecs: []const posix.iovec_const,
2576+
offset: u64,
2577+
buffer_index: u16,
2578+
) void {
25592579
sqe.prep_rw(.write_fixed, fd, @intFromPtr(iovecs.ptr), iovecs.len, offset);
25602580
sqe.buf_index = buffer_index;
25612581
}
25622582

2563-
pub fn prep_splice(sqe: *Sqe, fd_in: linux.fd_t, off_in: u64, fd_out: linux.fd_t, off_out: u64, len: usize) void {
2583+
pub fn prep_splice(
2584+
sqe: *Sqe,
2585+
fd_in: linux.fd_t,
2586+
off_in: u64,
2587+
fd_out: linux.fd_t,
2588+
off_out: u64,
2589+
len: u32,
2590+
flags: uflags.Splice,
2591+
) void {
25642592
sqe.prep_rw(.splice, fd_out, undefined, len, off_out);
25652593
sqe.addr = off_in;
25662594
sqe.splice_fd_in = fd_in;
2595+
sqe.rw_flags = @bitCast(flags);
25672596
}
25682597

2569-
pub fn prep_tee(sqe: *Sqe, fd_in: linux.fd_t, fd_out: linux.fd_t, len: usize) void {
2598+
pub fn prep_tee(
2599+
sqe: *Sqe,
2600+
fd_in: linux.fd_t,
2601+
fd_out: linux.fd_t,
2602+
len: u32,
2603+
flags: uflags.Splice,
2604+
) void {
25702605
sqe.prep_rw(.tee, fd_out, undefined, len, 0);
25712606
sqe.addr = undefined;
25722607
sqe.splice_fd_in = fd_in;
2608+
sqe.rw_flags = @bitCast(flags);
25732609
}
25742610

25752611
pub fn prep_read(sqe: *Sqe, fd: linux.fd_t, buffer: []u8, offset: u64) void {
@@ -3260,24 +3296,34 @@ pub const Sqe = extern struct {
32603296
sqe.prep_rw(.files_update, -1, @intFromPtr(fds.ptr), fds.len, constants.FILE_INDEX_ALLOC);
32613297
}
32623298

3263-
// TODO: why can't slice be used here ?
3299+
// Note: It is more appropriate to use a `[*]u8` than `[]u8` slice here
3300+
// because `[]u8` would free us of the extra `buffer_len` parameter but
3301+
// would require us to alway calculate the `buffer_len` in the function
3302+
// which is redundant since the `buffer_len` and `buffers_count`
3303+
// information are alway available for any 2 dimentional array type
3304+
// .ie [buffers_count][buffer_len]u8
32643305
pub fn prep_provide_buffers(
32653306
sqe: *Sqe,
32663307
buffers: [*]u8,
3267-
buffer_len: usize,
3268-
num: usize,
3269-
group_id: usize,
3270-
buffer_id: usize,
3308+
buffer_len: u32,
3309+
buffers_count: u32,
3310+
group_id: u32,
3311+
buffer_id: u32,
32713312
) void {
3272-
const ptr = @intFromPtr(buffers);
3273-
sqe.prep_rw(.provide_buffers, @intCast(num), ptr, buffer_len, buffer_id);
3313+
sqe.prep_rw(
3314+
.provide_buffers,
3315+
@intCast(buffers_count),
3316+
@intFromPtr(buffers),
3317+
buffer_len,
3318+
buffer_id,
3319+
);
32743320
sqe.buf_index = @intCast(group_id);
32753321
}
32763322

32773323
pub fn prep_remove_buffers(
32783324
sqe: *Sqe,
3279-
num: usize,
3280-
group_id: usize,
3325+
num: u32,
3326+
group_id: u32,
32813327
) void {
32823328
sqe.prep_rw(.remove_buffers, @intCast(num), 0, 0, 0);
32833329
sqe.buf_index = @intCast(group_id);
@@ -3478,7 +3524,7 @@ pub const Sqe = extern struct {
34783524
pub fn prep_listen(
34793525
sqe: *Sqe,
34803526
fd: linux.fd_t,
3481-
backlog: usize,
3527+
backlog: u32,
34823528
flags: u32, // flags is unused and does't exist in io_uring's api
34833529
) void {
34843530
sqe.prep_rw(.listen, fd, 0, backlog, 0);
@@ -5044,13 +5090,13 @@ test "splice/read" {
50445090
const fds = try posix.pipe();
50455091
const pipe_offset: u64 = math.maxInt(u64);
50465092

5047-
const sqe_splice_to_pipe = try ring.splice(0x11111111, fd_src, 0, fds[1], pipe_offset, buffer_write.len);
5093+
const sqe_splice_to_pipe = try ring.splice(0x11111111, fd_src, 0, fds[1], pipe_offset, buffer_write.len, .{});
50485094
try testing.expectEqual(Op.splice, sqe_splice_to_pipe.opcode);
50495095
try testing.expectEqual(0, sqe_splice_to_pipe.addr);
50505096
try testing.expectEqual(pipe_offset, sqe_splice_to_pipe.off);
50515097
sqe_splice_to_pipe.link_next();
50525098

5053-
const sqe_splice_from_pipe = try ring.splice(0x22222222, fds[0], pipe_offset, fd_dst, 10, buffer_write.len);
5099+
const sqe_splice_from_pipe = try ring.splice(0x22222222, fds[0], pipe_offset, fd_dst, 10, buffer_write.len, .{});
50545100
try testing.expectEqual(Op.splice, sqe_splice_from_pipe.opcode);
50555101
try testing.expectEqual(pipe_offset, sqe_splice_from_pipe.addr);
50565102
try testing.expectEqual(10, sqe_splice_from_pipe.off);

0 commit comments

Comments
 (0)