| ... | @@ -1840,42 +1840,73 @@ test "timeout_remove" { | ... | @@ -1840,42 +1840,73 @@ test "timeout_remove" { |
| 1840 | }, cqe_timeout_remove); | 1840 | }, cqe_timeout_remove); |
| 1841 | } | 1841 | } |
| 1842 | | 1842 | |
| 1843 | test "timeout_link_chain1" { | 1843 | test "accept/connect/recv/link_timeout" { |
| 1844 | if (builtin.os.tag != .linux) return error.SkipZigTest; | 1844 | if (builtin.os.tag != .linux) return error.SkipZigTest; |
| 1845 | | 1845 | |
| 1846 | var ring = IO_Uring.init(8, 0) catch |err| switch (err) { | 1846 | var ring = IO_Uring.init(16, 0) catch |err| switch (err) { |
| 1847 | error.SystemOutdated => return error.SkipZigTest, | 1847 | error.SystemOutdated => return error.SkipZigTest, |
| 1848 | error.PermissionDenied => return error.SkipZigTest, | 1848 | error.PermissionDenied => return error.SkipZigTest, |
| 1849 | else => return err, | 1849 | else => return err, |
| 1850 | }; | 1850 | }; |
| 1851 | defer ring.deinit(); | 1851 | defer ring.deinit(); |
| 1852 | | 1852 | |
| 1853 | var fds = try os.pipe(); | 1853 | const address = try net.Address.parseIp4("127.0.0.1", 3131); |
| 1854 | defer { | 1854 | const kernel_backlog = 1; |
| 1855 | os.close(fds[0]); | 1855 | const server = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0); |
| 1856 | os.close(fds[1]); | 1856 | defer os.close(server); |
| | 1857 | try os.setsockopt(server, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); |
| | 1858 | try os.bind(server, &address.any, address.getOsSockLen()); |
| | 1859 | try os.listen(server, kernel_backlog); |
| | 1860 | |
| | 1861 | var buffer_recv = [_]u8{ 0, 1, 0, 1, 0 }; |
| | 1862 | |
| | 1863 | var accept_addr: os.sockaddr = undefined; |
| | 1864 | var accept_addr_len: os.socklen_t = @sizeOf(@TypeOf(accept_addr)); |
| | 1865 | _ = try ring.accept(0xaaaaaaaa, server, &accept_addr, &accept_addr_len, 0); |
| | 1866 | try testing.expectEqual(@as(u32, 1), try ring.submit()); |
| | 1867 | |
| | 1868 | const client = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0); |
| | 1869 | defer os.close(client); |
| | 1870 | _ = try ring.connect(0xcccccccc, client, &address.any, address.getOsSockLen()); |
| | 1871 | try testing.expectEqual(@as(u32, 1), try ring.submit()); |
| | 1872 | |
| | 1873 | var cqe_accept = try ring.copy_cqe(); |
| | 1874 | if (cqe_accept.err() == .INVAL) return error.SkipZigTest; |
| | 1875 | var cqe_connect = try ring.copy_cqe(); |
| | 1876 | if (cqe_connect.err() == .INVAL) return error.SkipZigTest; |
| | 1877 | |
| | 1878 | // The accept/connect CQEs may arrive in any order, the connect CQE will sometimes come first: |
| | 1879 | if (cqe_accept.user_data == 0xcccccccc and cqe_connect.user_data == 0xaaaaaaaa) { |
| | 1880 | const a = cqe_accept; |
| | 1881 | const b = cqe_connect; |
| | 1882 | cqe_accept = b; |
| | 1883 | cqe_connect = a; |
| 1857 | } | 1884 | } |
| 1858 | | 1885 | |
| 1859 | var buffer = [_]u8{0} ** 128; | 1886 | try testing.expectEqual(@as(u64, 0xaaaaaaaa), cqe_accept.user_data); |
| 1860 | const iovecs = [_]os.iovec{os.iovec{ .iov_base = &buffer, .iov_len = buffer.len }}; | 1887 | if (cqe_accept.res <= 0) std.debug.print("\ncqe_accept.res={}\n", .{cqe_accept.res}); |
| 1861 | const sqe_readv = try ring.readv(0x11111111, fds[0], &iovecs, 0); | 1888 | try testing.expect(cqe_accept.res > 0); |
| 1862 | sqe_readv.flags |= linux.IOSQE_IO_LINK; | 1889 | try testing.expectEqual(@as(u32, 0), cqe_accept.flags); |
| | 1890 | try testing.expectEqual(linux.io_uring_cqe{ |
| | 1891 | .user_data = 0xcccccccc, |
| | 1892 | .res = 0, |
| | 1893 | .flags = 0, |
| | 1894 | }, cqe_connect); |
| | 1895 | |
| | 1896 | const sqe_recv = try ring.recv(0xffffffff, cqe_accept.res, buffer_recv[0..], 0); |
| | 1897 | sqe_recv.flags |= linux.IOSQE_IO_LINK; |
| 1863 | | 1898 | |
| 1864 | const ts = os.linux.kernel_timespec{ .tv_sec = 0, .tv_nsec = 1000000 }; | 1899 | const ts = os.linux.kernel_timespec{ .tv_sec = 0, .tv_nsec = 1000000 }; |
| 1865 | const seq_link_timeout = try ring.link_timeout(0x22222222, &ts, 0); | 1900 | _ = try ring.link_timeout(0x22222222, &ts, 0); |
| 1866 | seq_link_timeout.flags |= linux.IOSQE_IO_LINK; | | |
| 1867 | | | |
| 1868 | _ = try ring.nop(0x33333333); | | |
| 1869 | | 1901 | |
| 1870 | const nr_wait = try ring.submit(); | 1902 | const nr_wait = try ring.submit(); |
| 1871 | try testing.expectEqual(@as(u32, 3), nr_wait); | 1903 | try testing.expectEqual(@as(u32, 2), nr_wait); |
| 1872 | | 1904 | |
| 1873 | var i: usize = 0; | 1905 | var i: usize = 0; |
| 1874 | while (i < nr_wait) : (i += 1) { | 1906 | while (i < nr_wait) : (i += 1) { |
| 1875 | const cqe = try ring.copy_cqe(); | 1907 | const cqe = try ring.copy_cqe(); |
| 1876 | switch (cqe.user_data) { | 1908 | switch (cqe.user_data) { |
| 1877 | // poll cancel really should return -ECANCEL... | 1909 | 0xffffffff => { |
| 1878 | 0x11111111 => { | | |
| 1879 | if (cqe.res != -@as(i32, @enumToInt(linux.E.INTR)) and | 1910 | if (cqe.res != -@as(i32, @enumToInt(linux.E.INTR)) and |
| 1880 | cqe.res != -@as(i32, @enumToInt(linux.E.CANCELED))) | 1911 | cqe.res != -@as(i32, @enumToInt(linux.E.CANCELED))) |
| 1881 | { | 1912 | { |
| ... | @@ -1884,20 +1915,9 @@ test "timeout_link_chain1" { | ... | @@ -1884,20 +1915,9 @@ test "timeout_link_chain1" { |
| 1884 | } | 1915 | } |
| 1885 | }, | 1916 | }, |
| 1886 | 0x22222222 => { | 1917 | 0x22222222 => { |
| 1887 | // FASTPOLL kernels can cancel successfully | | |
| 1888 | if (cqe.res != -@as(i32, @enumToInt(linux.E.ALREADY)) and | 1918 | if (cqe.res != -@as(i32, @enumToInt(linux.E.ALREADY)) and |
| 1889 | cqe.res != -@as(i32, @enumToInt(linux.E.TIME))) | 1919 | cqe.res != -@as(i32, @enumToInt(linux.E.TIME))) |
| 1890 | { | 1920 | { |
| 1891 | if (cqe.res == -@as(i32, @enumToInt(linux.E.BADF))) { | | |
| 1892 | // https://github.com/ziglang/zig/issues/10247 | | |
| 1893 | return error.SkipZigTest; | | |
| 1894 | } | | |
| 1895 | std.debug.print("Req 0x{x} got {d}\n", .{ cqe.user_data, cqe.res }); | | |
| 1896 | try testing.expect(false); | | |
| 1897 | } | | |
| 1898 | }, | | |
| 1899 | 0x33333333 => { | | |
| 1900 | if (cqe.res != -@as(i32, @enumToInt(linux.E.CANCELED))) { | | |
| 1901 | std.debug.print("Req 0x{x} got {d}\n", .{ cqe.user_data, cqe.res }); | 1921 | std.debug.print("Req 0x{x} got {d}\n", .{ cqe.user_data, cqe.res }); |
| 1902 | try testing.expect(false); | 1922 | try testing.expect(false); |
| 1903 | } | 1923 | } |