authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-14 22:51:11-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-11-14 22:51:11-05:00
log46af3a320c717cfcb13dbf3071a41c161b0a33f9
tree4771a1f95fea9a79cddd772eb660feafc3d65360
parent29f531bec9fd8cedc0e0da0981d1a71bb3348641
parent77d1d5839aaa24564d10f334f50bb3995fa7c83c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10081 from hnakamur/lib_std_os_linux_io_uring_cancel

std.os.linux: Add cancel and io_uring_prep_cancel

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

lib/std/os/linux/io_uring.zig+107
...@@ -683,6 +683,26 @@ pub const IO_Uring = struct {...@@ -683,6 +683,26 @@ pub const IO_Uring = struct {
683 return sqe;683 return sqe;
684 }684 }
685685
686 /// Queues (but does not submit) an SQE to remove an existing operation.
687 /// Returns a pointer to the SQE.
688 ///
689 /// The operation is identified by its `user_data`.
690 ///
691 /// The completion event result will be `0` if the operation was found and cancelled successfully,
692 /// `-EALREADY` if the operation was found but was already in progress, or
693 /// `-ENOENT` if the operation was not found.
694 pub fn cancel(
695 self: *IO_Uring,
696 user_data: u64,
697 cancel_user_data: u64,
698 flags: u32,
699 ) !*io_uring_sqe {
700 const sqe = try self.get_sqe();
701 io_uring_prep_cancel(sqe, cancel_user_data, flags);
702 sqe.user_data = user_data;
703 return sqe;
704 }
705
686 /// Registers an array of file descriptors.706 /// Registers an array of file descriptors.
687 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must707 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must
688 /// retrieve a reference to the file, and once I/O has completed the file reference must be708 /// retrieve a reference to the file, and once I/O has completed the file reference must be
...@@ -1182,6 +1202,15 @@ pub fn io_uring_prep_statx(...@@ -1182,6 +1202,15 @@ pub fn io_uring_prep_statx(
1182 sqe.rw_flags = flags;1202 sqe.rw_flags = flags;
1183}1203}
11841204
1205pub fn io_uring_prep_cancel(
1206 sqe: *io_uring_sqe,
1207 cancel_user_data: u64,
1208 flags: u32,
1209) void {
1210 io_uring_prep_rw(.ASYNC_CANCEL, sqe, -1, cancel_user_data, 0, 0);
1211 sqe.rw_flags = flags;
1212}
1213
1185test "structs/offsets/entries" {1214test "structs/offsets/entries" {
1186 if (builtin.os.tag != .linux) return error.SkipZigTest;1215 if (builtin.os.tag != .linux) return error.SkipZigTest;
11871216
...@@ -1842,3 +1871,81 @@ test "statx" {...@@ -1842,3 +1871,81 @@ test "statx" {
1842 try testing.expect(buf.mask & os.linux.STATX_SIZE == os.linux.STATX_SIZE);1871 try testing.expect(buf.mask & os.linux.STATX_SIZE == os.linux.STATX_SIZE);
1843 try testing.expectEqual(@as(u64, 6), buf.size);1872 try testing.expectEqual(@as(u64, 6), buf.size);
1844}1873}
1874
1875test "accept/connect/recv/cancel" {
1876 if (builtin.os.tag != .linux) return error.SkipZigTest;
1877
1878 var ring = IO_Uring.init(16, 0) catch |err| switch (err) {
1879 error.SystemOutdated => return error.SkipZigTest,
1880 error.PermissionDenied => return error.SkipZigTest,
1881 else => return err,
1882 };
1883 defer ring.deinit();
1884
1885 const address = try net.Address.parseIp4("127.0.0.1", 3131);
1886 const kernel_backlog = 1;
1887 const server = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
1888 defer os.close(server);
1889 try os.setsockopt(server, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
1890 try os.bind(server, &address.any, address.getOsSockLen());
1891 try os.listen(server, kernel_backlog);
1892
1893 var buffer_recv = [_]u8{ 0, 1, 0, 1, 0 };
1894
1895 var accept_addr: os.sockaddr = undefined;
1896 var accept_addr_len: os.socklen_t = @sizeOf(@TypeOf(accept_addr));
1897 _ = try ring.accept(0xaaaaaaaa, server, &accept_addr, &accept_addr_len, 0);
1898 try testing.expectEqual(@as(u32, 1), try ring.submit());
1899
1900 const client = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
1901 defer os.close(client);
1902 _ = try ring.connect(0xcccccccc, client, &address.any, address.getOsSockLen());
1903 try testing.expectEqual(@as(u32, 1), try ring.submit());
1904
1905 var cqe_accept = try ring.copy_cqe();
1906 if (cqe_accept.err() == .INVAL) return error.SkipZigTest;
1907 var cqe_connect = try ring.copy_cqe();
1908 if (cqe_connect.err() == .INVAL) return error.SkipZigTest;
1909
1910 // The accept/connect CQEs may arrive in any order, the connect CQE will sometimes come first:
1911 if (cqe_accept.user_data == 0xcccccccc and cqe_connect.user_data == 0xaaaaaaaa) {
1912 const a = cqe_accept;
1913 const b = cqe_connect;
1914 cqe_accept = b;
1915 cqe_connect = a;
1916 }
1917
1918 try testing.expectEqual(@as(u64, 0xaaaaaaaa), cqe_accept.user_data);
1919 if (cqe_accept.res <= 0) std.debug.print("\ncqe_accept.res={}\n", .{cqe_accept.res});
1920 try testing.expect(cqe_accept.res > 0);
1921 try testing.expectEqual(@as(u32, 0), cqe_accept.flags);
1922 try testing.expectEqual(linux.io_uring_cqe{
1923 .user_data = 0xcccccccc,
1924 .res = 0,
1925 .flags = 0,
1926 }, cqe_connect);
1927
1928 _ = try ring.recv(0xffffffff, cqe_accept.res, buffer_recv[0..], 0);
1929 try testing.expectEqual(@as(u32, 1), try ring.submit());
1930
1931 const sqe_cancel = try ring.cancel(0x99999999, 0xffffffff, 0);
1932 try testing.expectEqual(linux.IORING_OP.ASYNC_CANCEL, sqe_cancel.opcode);
1933 try testing.expectEqual(@as(u64, 0xffffffff), sqe_cancel.addr);
1934 try testing.expectEqual(@as(u64, 0x99999999), sqe_cancel.user_data);
1935 try testing.expectEqual(@as(u32, 1), try ring.submit());
1936
1937 const cqe_recv = try ring.copy_cqe();
1938 if (cqe_recv.err() == .INVAL) return error.SkipZigTest;
1939 try testing.expectEqual(linux.io_uring_cqe{
1940 .user_data = 0xffffffff,
1941 .res = -@as(i32, @enumToInt(linux.E.CANCELED)),
1942 .flags = 0,
1943 }, cqe_recv);
1944
1945 const cqe_cancel = try ring.copy_cqe();
1946 try testing.expectEqual(linux.io_uring_cqe{
1947 .user_data = 0x99999999,
1948 .res = 0,
1949 .flags = 0,
1950 }, cqe_cancel);
1951}