|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.channel.uring; |
| 17 | + |
| 18 | +import io.netty.bootstrap.Bootstrap; |
| 19 | +import io.netty.channel.Channel; |
| 20 | +import io.netty.channel.ChannelHandlerContext; |
| 21 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 22 | +import io.netty.channel.ChannelOption; |
| 23 | +import io.netty.channel.FixedRecvByteBufAllocator; |
| 24 | +import io.netty.channel.IoEvent; |
| 25 | +import io.netty.channel.IoHandle; |
| 26 | +import io.netty.channel.IoHandler; |
| 27 | +import io.netty.channel.IoHandlerContext; |
| 28 | +import io.netty.channel.IoHandlerFactory; |
| 29 | +import io.netty.channel.IoRegistration; |
| 30 | +import io.netty.channel.MultiThreadIoEventLoopGroup; |
| 31 | +import io.netty.channel.socket.DatagramPacket; |
| 32 | +import io.netty.channel.socket.SocketProtocolFamily; |
| 33 | +import io.netty.util.NetUtil; |
| 34 | +import io.netty.util.concurrent.ThreadAwareExecutor; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.Timeout; |
| 38 | + |
| 39 | +import java.net.InetSocketAddress; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +import static io.netty.channel.unix.Errors.ERRNO_EAGAIN_NEGATIVE; |
| 46 | +import static io.netty.channel.unix.Errors.ERRNO_EBADF_NEGATIVE; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 49 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 50 | + |
| 51 | +@Timeout(value = 1, unit = TimeUnit.MINUTES) |
| 52 | +public class IoUringDatagramHardlinkTest { |
| 53 | + |
| 54 | + private static final int DATAGRAM_SIZE = 512; |
| 55 | + private static final int BATCH_SIZE = 4; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + public static void loadJNI() { |
| 59 | + assumeTrue(IoUring.isAvailable()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testRecvmmsgHardlinkOrder() throws Exception { |
| 64 | + CompletionRecorder recorder = new CompletionRecorder(); |
| 65 | + IoHandlerFactory factory = executor -> new RecordingIoUringIoHandler( |
| 66 | + IoUringIoHandler.newFactory().newHandler(executor), recorder); |
| 67 | + MultiThreadIoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, factory); |
| 68 | + Channel receiver = null; |
| 69 | + Channel sender = null; |
| 70 | + try { |
| 71 | + receiver = newBootstrap(group) |
| 72 | + .handler(new ChannelInboundHandlerAdapter()) |
| 73 | + .bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0)) |
| 74 | + .sync() |
| 75 | + .channel(); |
| 76 | + |
| 77 | + sender = new Bootstrap() |
| 78 | + .group(group) |
| 79 | + .channelFactory(() -> new IoUringDatagramChannel(SocketProtocolFamily.INET)) |
| 80 | + .handler(new ChannelInboundHandlerAdapter()) |
| 81 | + .bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0)) |
| 82 | + .sync() |
| 83 | + .channel(); |
| 84 | + |
| 85 | + // The hard linked recvmsg chain must park as a whole. Without hard linking the |
| 86 | + // MSG_DONTWAIT recvmsg ops would already have completed with EAGAIN here. |
| 87 | + Thread.sleep(100); |
| 88 | + assertEquals(0, recorder.size()); |
| 89 | + |
| 90 | + sender.writeAndFlush(new DatagramPacket( |
| 91 | + sender.alloc().buffer(DATAGRAM_SIZE).writeZero(DATAGRAM_SIZE), |
| 92 | + (InetSocketAddress) receiver.localAddress())).sync(); |
| 93 | + recorder.await(BATCH_SIZE); |
| 94 | + |
| 95 | + assertEquals(Arrays.asList( |
| 96 | + DATAGRAM_SIZE, |
| 97 | + ERRNO_EAGAIN_NEGATIVE, |
| 98 | + ERRNO_EAGAIN_NEGATIVE, |
| 99 | + ERRNO_EAGAIN_NEGATIVE), recorder.completions()); |
| 100 | + } finally { |
| 101 | + if (sender != null) { |
| 102 | + sender.close().syncUninterruptibly(); |
| 103 | + } |
| 104 | + if (receiver != null) { |
| 105 | + receiver.close().syncUninterruptibly(); |
| 106 | + } |
| 107 | + group.shutdownGracefully().syncUninterruptibly(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testClosePendingRecvmmsgHardlink() throws Exception { |
| 113 | + CompletionRecorder recorder = new CompletionRecorder(); |
| 114 | + IoHandlerFactory factory = executor -> new RecordingIoUringIoHandler( |
| 115 | + IoUringIoHandler.newFactory().newHandler(executor), recorder); |
| 116 | + MultiThreadIoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, factory); |
| 117 | + Channel receiver = null; |
| 118 | + Channel sender = null; |
| 119 | + try { |
| 120 | + receiver = newBootstrap(group) |
| 121 | + .handler(new ChannelInboundHandlerAdapter()) |
| 122 | + .bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0)) |
| 123 | + .sync() |
| 124 | + .channel(); |
| 125 | + |
| 126 | + sender = new Bootstrap() |
| 127 | + .group(group) |
| 128 | + .channelFactory(() -> new IoUringDatagramChannel(SocketProtocolFamily.INET)) |
| 129 | + .handler(new ChannelInboundHandlerAdapter()) |
| 130 | + .bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0)) |
| 131 | + .sync() |
| 132 | + .channel(); |
| 133 | + |
| 134 | + // Let the read loop schedule the first batch and the AUTO_READ re-scheduled batch. |
| 135 | + Thread.sleep(100); |
| 136 | + assertEquals(0, recorder.size()); |
| 137 | + |
| 138 | + sender.writeAndFlush(new DatagramPacket( |
| 139 | + sender.alloc().buffer(DATAGRAM_SIZE).writeZero(DATAGRAM_SIZE), |
| 140 | + (InetSocketAddress) receiver.localAddress())).sync(); |
| 141 | + recorder.await(BATCH_SIZE); |
| 142 | + |
| 143 | + // Close while the re-scheduled (hard linked) batch is still pending. Without the fix in |
| 144 | + // readComplete(...) the fd would be closed before all recvmsg ops completed and so some |
| 145 | + // of them would complete with EBADF. |
| 146 | + receiver.close().syncUninterruptibly(); |
| 147 | + recorder.await(BATCH_SIZE * 2); |
| 148 | + |
| 149 | + assertFalse(recorder.completions().contains(ERRNO_EBADF_NEGATIVE), recorder.completions().toString()); |
| 150 | + } finally { |
| 151 | + if (sender != null) { |
| 152 | + sender.close().syncUninterruptibly(); |
| 153 | + } |
| 154 | + if (receiver != null) { |
| 155 | + receiver.close().syncUninterruptibly(); |
| 156 | + } |
| 157 | + group.shutdownGracefully().syncUninterruptibly(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static Bootstrap newBootstrap(MultiThreadIoEventLoopGroup group) { |
| 162 | + return new Bootstrap() |
| 163 | + .group(group) |
| 164 | + .channelFactory(() -> new IoUringDatagramChannel(SocketProtocolFamily.INET)) |
| 165 | + .option(ChannelOption.RECVBUF_ALLOCATOR, |
| 166 | + new FixedRecvByteBufAllocator(BATCH_SIZE * DATAGRAM_SIZE)) |
| 167 | + .option(IoUringChannelOption.MAX_DATAGRAM_PAYLOAD_SIZE, DATAGRAM_SIZE); |
| 168 | + } |
| 169 | + |
| 170 | + private static final class CompletionRecorder { |
| 171 | + private final List<Integer> completions = new ArrayList<Integer>(); |
| 172 | + |
| 173 | + synchronized void record(IoUringIoEvent event) { |
| 174 | + if (event.opcode() == Native.IORING_OP_RECVMSG) { |
| 175 | + completions.add(event.res()); |
| 176 | + notifyAll(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + synchronized int size() { |
| 181 | + return completions.size(); |
| 182 | + } |
| 183 | + |
| 184 | + synchronized List<Integer> completions() { |
| 185 | + return new ArrayList<Integer>(completions); |
| 186 | + } |
| 187 | + |
| 188 | + synchronized void await(int count) throws InterruptedException { |
| 189 | + while (completions.size() < count) { |
| 190 | + wait(); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private static final class RecordingIoUringIoHandler implements IoHandler { |
| 196 | + private final IoHandler delegate; |
| 197 | + private final CompletionRecorder recorder; |
| 198 | + |
| 199 | + RecordingIoUringIoHandler(IoHandler delegate, CompletionRecorder recorder) { |
| 200 | + this.delegate = delegate; |
| 201 | + this.recorder = recorder; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void initialize() { |
| 206 | + delegate.initialize(); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public int run(IoHandlerContext context) { |
| 211 | + return delegate.run(context); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void prepareToDestroy() { |
| 216 | + delegate.prepareToDestroy(); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void destroy() { |
| 221 | + delegate.destroy(); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public IoRegistration register(IoHandle handle) throws Exception { |
| 226 | + IoUringIoHandle ioHandle = (IoUringIoHandle) handle; |
| 227 | + return delegate.register(new IoUringIoHandle() { |
| 228 | + @Override |
| 229 | + public void handle(IoRegistration registration, IoEvent event) { |
| 230 | + recorder.record((IoUringIoEvent) event); |
| 231 | + ioHandle.handle(registration, event); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public void registered() { |
| 236 | + ioHandle.registered(); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public void unregistered() { |
| 241 | + ioHandle.unregistered(); |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public void close() throws Exception { |
| 246 | + ioHandle.close(); |
| 247 | + } |
| 248 | + }); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void wakeup() { |
| 253 | + delegate.wakeup(); |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public boolean isCompatible(Class<? extends IoHandle> handleType) { |
| 258 | + return delegate.isCompatible(handleType); |
| 259 | + } |
| 260 | + } |
| 261 | +} |