1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.channel.DefaultFileRegion;
19 import io.netty.channel.unix.Buffer;
20 import io.netty.util.internal.ObjectUtil;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23 import io.netty.channel.unix.FileDescriptor;
24 import io.netty.channel.unix.PeerCredentials;
25 import io.netty.channel.unix.Unix;
26 import io.netty.util.internal.ClassInitializerUtil;
27 import io.netty.util.internal.NativeLibraryLoader;
28 import io.netty.util.internal.PlatformDependent;
29 import io.netty.util.internal.SystemPropertyUtil;
30 import io.netty.util.internal.ThrowableUtil;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.channels.Selector;
35 import java.nio.file.Path;
36 import java.util.Arrays;
37 import java.util.Locale;
38
39 final class Native {
40 private static final InternalLogger logger = InternalLoggerFactory.getInstance(Native.class);
41
42 static {
43 Selector selector = null;
44 try {
45
46
47
48
49 selector = Selector.open();
50 } catch (IOException ignore) {
51
52 }
53
54
55
56
57
58
59 ClassInitializerUtil.tryLoadClasses(
60 Native.class,
61
62 PeerCredentials.class, java.io.FileDescriptor.class
63 );
64
65 File tmpDir = PlatformDependent.tmpdir();
66 Path tmpFile = tmpDir.toPath().resolve("netty_io_uring.tmp");
67 try {
68
69
70 Native.createFile(tmpFile.toString());
71 } catch (UnsatisfiedLinkError ignore) {
72
73 loadNativeLibrary();
74 } finally {
75 tmpFile.toFile().delete();
76 try {
77 if (selector != null) {
78 selector.close();
79 }
80 } catch (IOException ignore) {
81
82 }
83 }
84 Unix.registerInternal(Native::registerUnix);
85 }
86
87 static final int SOCK_NONBLOCK = NativeStaticallyReferencedJniMethods.sockNonblock();
88 static final int SOCK_CLOEXEC = NativeStaticallyReferencedJniMethods.sockCloexec();
89 static final short AF_INET = (short) NativeStaticallyReferencedJniMethods.afInet();
90 static final short AF_INET6 = (short) NativeStaticallyReferencedJniMethods.afInet6();
91 static final short AF_UNIX = (short) NativeStaticallyReferencedJniMethods.afUnix();
92 static final int SIZEOF_SOCKADDR_STORAGE = NativeStaticallyReferencedJniMethods.sizeofSockaddrStorage();
93 static final int SIZEOF_SOCKADDR_UN = NativeStaticallyReferencedJniMethods.sizeofSockaddrUn();
94 static final int SOCKADDR_UN_OFFSETOF_SUN_FAMILY =
95 NativeStaticallyReferencedJniMethods.sockaddrUnOffsetofSunFamily();
96 static final int SOCKADDR_UN_OFFSETOF_SUN_PATH =
97 NativeStaticallyReferencedJniMethods.sockaddrUnOffsetofSunPath();
98 static final int MAX_SUN_PATH_LEN =
99 NativeStaticallyReferencedJniMethods.maxSunPathLen();
100 static final int SIZEOF_SOCKADDR_IN = NativeStaticallyReferencedJniMethods.sizeofSockaddrIn();
101 static final int SIZEOF_SOCKADDR_IN6 = NativeStaticallyReferencedJniMethods.sizeofSockaddrIn6();
102 static final int SOCKADDR_IN_OFFSETOF_SIN_FAMILY =
103 NativeStaticallyReferencedJniMethods.sockaddrInOffsetofSinFamily();
104 static final int SOCKADDR_IN_OFFSETOF_SIN_PORT = NativeStaticallyReferencedJniMethods.sockaddrInOffsetofSinPort();
105 static final int SOCKADDR_IN_OFFSETOF_SIN_ADDR = NativeStaticallyReferencedJniMethods.sockaddrInOffsetofSinAddr();
106 static final int IN_ADDRESS_OFFSETOF_S_ADDR = NativeStaticallyReferencedJniMethods.inAddressOffsetofSAddr();
107 static final int SOCKADDR_IN6_OFFSETOF_SIN6_FAMILY =
108 NativeStaticallyReferencedJniMethods.sockaddrIn6OffsetofSin6Family();
109 static final int SOCKADDR_IN6_OFFSETOF_SIN6_PORT =
110 NativeStaticallyReferencedJniMethods.sockaddrIn6OffsetofSin6Port();
111 static final int SOCKADDR_IN6_OFFSETOF_SIN6_FLOWINFO =
112 NativeStaticallyReferencedJniMethods.sockaddrIn6OffsetofSin6Flowinfo();
113 static final int SOCKADDR_IN6_OFFSETOF_SIN6_ADDR =
114 NativeStaticallyReferencedJniMethods.sockaddrIn6OffsetofSin6Addr();
115 static final int SOCKADDR_IN6_OFFSETOF_SIN6_SCOPE_ID =
116 NativeStaticallyReferencedJniMethods.sockaddrIn6OffsetofSin6ScopeId();
117 static final int IN6_ADDRESS_OFFSETOF_S6_ADDR = NativeStaticallyReferencedJniMethods.in6AddressOffsetofS6Addr();
118 static final int SIZEOF_SIZE_T = NativeStaticallyReferencedJniMethods.sizeofSizeT();
119 static final int SIZEOF_IOVEC = NativeStaticallyReferencedJniMethods.sizeofIovec();
120 static final int CMSG_SPACE = NativeStaticallyReferencedJniMethods.cmsgSpace();
121 static final int CMSG_SPACE_FOR_FD = NativeStaticallyReferencedJniMethods.cmsgSpaceForFd();
122 static final int CMSG_LEN = NativeStaticallyReferencedJniMethods.cmsgLen();
123 static final int CMSG_LEN_FOR_FD = NativeStaticallyReferencedJniMethods.cmsgLenForFd();
124 static final int MSG_CONTROL_LEN_FOR_FD = NativeStaticallyReferencedJniMethods.msgControlLenForFd();
125 static final int CMSG_OFFSETOF_CMSG_LEN = NativeStaticallyReferencedJniMethods.cmsghdrOffsetofCmsgLen();
126 static final int CMSG_OFFSETOF_CMSG_LEVEL = NativeStaticallyReferencedJniMethods.cmsghdrOffsetofCmsgLevel();
127 static final int CMSG_OFFSETOF_CMSG_TYPE = NativeStaticallyReferencedJniMethods.cmsghdrOffsetofCmsgType();
128
129 static final int IO_URING_BUFFER_RING_TAIL = NativeStaticallyReferencedJniMethods.ioUringBufferRingOffsetTail();
130
131 static final int IOVEC_OFFSETOF_IOV_BASE = NativeStaticallyReferencedJniMethods.iovecOffsetofIovBase();
132 static final int IOVEC_OFFSETOF_IOV_LEN = NativeStaticallyReferencedJniMethods.iovecOffsetofIovLen();
133 static final int SIZEOF_MSGHDR = NativeStaticallyReferencedJniMethods.sizeofMsghdr();
134 static final int MSGHDR_OFFSETOF_MSG_NAME = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgName();
135 static final int MSGHDR_OFFSETOF_MSG_NAMELEN = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgNamelen();
136 static final int MSGHDR_OFFSETOF_MSG_IOV = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgIov();
137 static final int MSGHDR_OFFSETOF_MSG_IOVLEN = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgIovlen();
138 static final int MSGHDR_OFFSETOF_MSG_CONTROL = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgControl();
139 static final int MSGHDR_OFFSETOF_MSG_CONTROLLEN =
140 NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgControllen();
141 static final int MSGHDR_OFFSETOF_MSG_FLAGS = NativeStaticallyReferencedJniMethods.msghdrOffsetofMsgFlags();
142 static final int POLLIN = NativeStaticallyReferencedJniMethods.pollin();
143 static final int POLLOUT = NativeStaticallyReferencedJniMethods.pollout();
144 static final int POLLRDHUP = NativeStaticallyReferencedJniMethods.pollrdhup();
145 static final int ERRNO_ECANCELED_NEGATIVE = -NativeStaticallyReferencedJniMethods.ecanceled();
146 static final int ERRNO_ETIME_NEGATIVE = -NativeStaticallyReferencedJniMethods.etime();
147 static final int ERRNO_NOBUFS_NEGATIVE = -NativeStaticallyReferencedJniMethods.enobufs();
148
149 static final int PAGE_SIZE = NativeStaticallyReferencedJniMethods.pageSize();
150 static final int MAX_SKB_FRAGS = NativeStaticallyReferencedJniMethods.maxSkbFrags();
151
152 static final int SIZEOF_IOURING_BUF = NativeStaticallyReferencedJniMethods.sizeofIoUringBuf();
153 static final int IOURING_BUFFER_OFFSETOF_ADDR = NativeStaticallyReferencedJniMethods.ioUringBufferOffsetAddr();
154 static final int IOURING_BUFFER_OFFSETOF_LEN = NativeStaticallyReferencedJniMethods.ioUringBufferOffsetLen();
155 static final int IOURING_BUFFER_OFFSETOF_BID = NativeStaticallyReferencedJniMethods.ioUringBufferOffsetBid();
156
157
158
159
160 static final byte IORING_OP_NOP = 0;
161 static final byte IORING_OP_READV = 1;
162 static final byte IORING_OP_WRITEV = 2;
163 static final byte IORING_OP_FSYNC = 3;
164 static final byte IORING_OP_READ_FIXED = 4;
165 static final byte IORING_OP_WRITE_FIXED = 5;
166 static final byte IORING_OP_POLL_ADD = 6;
167 static final byte IORING_OP_POLL_REMOVE = 7;
168 static final byte IORING_OP_SYNC_FILE_RANGE = 8;
169 static final byte IORING_OP_SENDMSG = 9;
170 static final byte IORING_OP_RECVMSG = 10;
171 static final byte IORING_OP_TIMEOUT = 11;
172 static final byte IORING_OP_TIMEOUT_REMOVE = 12;
173 static final byte IORING_OP_ACCEPT = 13;
174 static final byte IORING_OP_ASYNC_CANCEL = 14;
175 static final byte IORING_OP_LINK_TIMEOUT = 15;
176 static final byte IORING_OP_CONNECT = 16;
177 static final byte IORING_OP_FALLOCATE = 17;
178 static final byte IORING_OP_OPENAT = 18;
179 static final byte IORING_OP_CLOSE = 19;
180 static final byte IORING_OP_FILES_UPDATE = 20;
181 static final byte IORING_OP_STATX = 21;
182 static final byte IORING_OP_READ = 22;
183 static final byte IORING_OP_WRITE = 23;
184 static final byte IORING_OP_FADVISE = 24;
185 static final byte IORING_OP_MADVISE = 25;
186 static final byte IORING_OP_SEND = 26;
187 static final byte IORING_OP_RECV = 27;
188 static final byte IORING_OP_OPENAT2 = 28;
189 static final byte IORING_OP_EPOLL_CTL = 29;
190 static final byte IORING_OP_SPLICE = 30;
191 static final byte IORING_OP_PROVIDE_BUFFERS = 31;
192 static final byte IORING_OP_REMOVE_BUFFERS = 32;
193 static final byte IORING_OP_TEE = 33;
194 static final byte IORING_OP_SHUTDOWN = 34;
195 static final byte IORING_OP_RENAMEAT = 35;
196 static final byte IORING_OP_UNLINKAT = 36;
197 static final byte IORING_OP_MKDIRAT = 37;
198 static final byte IORING_OP_SYMLINKAT = 38;
199 static final byte IORING_OP_LINKAT = 39;
200 static final byte IORING_OP_MSG_RING = 40;
201 static final byte IORING_OP_FSETXATTR = 41;
202 static final byte IORING_OP_SETXATTR = 42;
203 static final byte IORING_OP_FGETXATTR = 43;
204 static final byte IORING_OP_GETXATTR = 44;
205 static final byte IORING_OP_SOCKET = 45;
206 static final byte IORING_OP_URING_CMD = 46;
207 static final byte IORING_OP_SEND_ZC = 47;
208 static final byte IORING_OP_SENDMSG_ZC = 48;
209 static final byte IORING_OP_READ_MULTISHOT = 49;
210 static final byte IORING_OP_WAITID = 50;
211 static final byte IORING_OP_FUTEX_WAIT = 51;
212 static final byte IORING_OP_FUTEX_WAKE = 52;
213 static final byte IORING_OP_FUTEX_WAITV = 53;
214 static final byte IORING_OP_FIXED_FD_INSTALL = 54;
215 static final byte IORING_OP_FTRUNCATE = 55;
216 static final byte IORING_OP_BIND = 56;
217 static final byte IORING_CQE_F_BUFFER = 1 << 0;
218 static final byte IORING_CQE_F_MORE = 1 << 1;
219 static final byte IORING_CQE_F_SOCK_NONEMPTY = 1 << 2;
220 static final byte IORING_CQE_F_NOTIF = 1 << 3;
221 static final byte IORING_CQE_F_BUF_MORE = 1 << 4;
222
223 static final int IORING_SETUP_CQSIZE = 1 << 3;
224 static final int IORING_SETUP_CLAMP = 1 << 4;
225
226 static final int IORING_SETUP_R_DISABLED = 1 << 6;
227 static final int IORING_SETUP_SUBMIT_ALL = 1 << 7;
228 static final int IORING_SETUP_CQE32 = 1 << 11;
229
230 static final int IORING_SETUP_SINGLE_ISSUER = 1 << 12;
231 static final int IORING_SETUP_DEFER_TASKRUN = 1 << 13;
232 static final int IORING_SETUP_NO_SQARRAY = 1 << 16;
233 static final int IORING_CQE_BUFFER_SHIFT = 16;
234
235 static final short IORING_POLL_ADD_MULTI = 1 << 0;
236
237 static final short IORING_RECVSEND_POLL_FIRST = 1 << 0;
238 static final short IORING_RECVSEND_BUNDLE = 1 << 4;
239 static final short IORING_RECV_MULTISHOT = 1 << 1;
240 static final short IORING_SEND_ZC_REPORT_USAGE = 1 << 3;
241
242 static final int IORING_NOTIF_USAGE_ZC_COPIED = 1 << 31;
243
244 static final short IORING_ACCEPT_MULTISHOT = 1 << 0;
245 static final short IORING_ACCEPT_DONTWAIT = 1 << 1;
246 static final short IORING_ACCEPT_POLL_FIRST = 1 << 2;
247
248 static final int IORING_FEAT_NODROP = 1 << 1;
249 static final int IORING_FEAT_SUBMIT_STABLE = 1 << 2;
250 static final int IORING_FEAT_RECVSEND_BUNDLE = 1 << 14;
251
252 static final int IORING_SQ_NEED_WAKEUP = 1 << 0;
253 static final int IORING_SQ_CQ_OVERFLOW = 1 << 1;
254 static final int IORING_SQ_TASKRUN = 1 << 2;
255
256 static final int SPLICE_F_MOVE = 1;
257
258 static final int IOU_PBUF_RING_INC = 2;
259 static final int IO_URING_OP_SUPPORTED = 1;
260
261 static final int CQE_SIZE = 16;
262 static final int CQE32_SIZE = 32;
263
264 static String opToStr(byte op) {
265 switch (op) {
266 case IORING_OP_NOP: return "NOP";
267 case IORING_OP_READV: return "READV";
268 case IORING_OP_WRITEV: return "WRITEV";
269 case IORING_OP_FSYNC: return "FSYNC";
270 case IORING_OP_READ_FIXED: return "READ_FIXED";
271 case IORING_OP_WRITE_FIXED: return "WRITE_FIXED";
272 case IORING_OP_POLL_ADD: return "POLL_ADD";
273 case IORING_OP_POLL_REMOVE: return "POLL_REMOVE";
274 case IORING_OP_SYNC_FILE_RANGE: return "SYNC_FILE_RANGE";
275 case IORING_OP_SENDMSG: return "SENDMSG";
276 case IORING_OP_RECVMSG: return "RECVMSG";
277 case IORING_OP_TIMEOUT: return "TIMEOUT";
278 case IORING_OP_TIMEOUT_REMOVE: return "TIMEOUT_REMOVE";
279 case IORING_OP_ACCEPT: return "ACCEPT";
280 case IORING_OP_ASYNC_CANCEL: return "ASYNC_CANCEL";
281 case IORING_OP_LINK_TIMEOUT: return "LINK_TIMEOUT";
282 case IORING_OP_CONNECT: return "CONNECT";
283 case IORING_OP_FALLOCATE: return "FALLOCATE";
284 case IORING_OP_OPENAT: return "OPENAT";
285 case IORING_OP_CLOSE: return "CLOSE";
286 case IORING_OP_FILES_UPDATE: return "FILES_UPDATE";
287 case IORING_OP_STATX: return "STATX";
288 case IORING_OP_READ: return "READ";
289 case IORING_OP_WRITE: return "WRITE";
290 case IORING_OP_FADVISE: return "FADVISE";
291 case IORING_OP_MADVISE: return "MADVISE";
292 case IORING_OP_SEND: return "SEND";
293 case IORING_OP_RECV: return "RECV";
294 case IORING_OP_OPENAT2: return "OPENAT2";
295 case IORING_OP_EPOLL_CTL: return "EPOLL_CTL";
296 case IORING_OP_SPLICE: return "SPLICE";
297 case IORING_OP_PROVIDE_BUFFERS: return "PROVIDE_BUFFERS";
298 case IORING_OP_REMOVE_BUFFERS: return "REMOVE_BUFFERS";
299 case IORING_OP_TEE: return "TEE";
300 case IORING_OP_SHUTDOWN: return "SHUTDOWN";
301 case IORING_OP_RENAMEAT: return "RENAMEAT";
302 case IORING_OP_UNLINKAT: return "UNLINKAT";
303 case IORING_OP_MKDIRAT: return "MKDIRAT";
304 case IORING_OP_SYMLINKAT: return "SYMLINKAT";
305 case IORING_OP_LINKAT: return "LINKAT";
306 default: return "[OP CODE " + op + ']';
307 }
308 }
309
310 static final int IORING_ENTER_GETEVENTS = NativeStaticallyReferencedJniMethods.ioringEnterGetevents();
311 static final int IORING_ENTER_REGISTERED_RING = 1 << 4;
312 static final int IOSQE_ASYNC = NativeStaticallyReferencedJniMethods.iosqeAsync();
313 static final int IOSQE_LINK = NativeStaticallyReferencedJniMethods.iosqeLink();
314 static final int IOSQE_IO_DRAIN = NativeStaticallyReferencedJniMethods.iosqeDrain();
315 static final int IOSQE_BUFFER_SELECT = NativeStaticallyReferencedJniMethods.iosqeBufferSelect();
316 static final int IOSQE_CQE_SKIP_SUCCESS = 1 << 6;
317 static final int MSG_DONTWAIT = NativeStaticallyReferencedJniMethods.msgDontwait();
318 static final int MSG_FASTOPEN = NativeStaticallyReferencedJniMethods.msgFastopen();
319 static final int SOL_UDP = NativeStaticallyReferencedJniMethods.solUdp();
320 static final int SOL_SOCKET = NativeStaticallyReferencedJniMethods.solSocket();
321 static final int UDP_SEGMENT = NativeStaticallyReferencedJniMethods.udpSegment();
322 static final int SCM_RIGHTS = NativeStaticallyReferencedJniMethods.scmRights();
323 private static final int TFO_ENABLED_CLIENT_MASK = 0x1;
324 private static final int TFO_ENABLED_SERVER_MASK = 0x2;
325 private static final int TCP_FASTOPEN_MODE = NativeStaticallyReferencedJniMethods.tcpFastopenMode();
326
327
328
329
330 static final boolean IS_SUPPORTING_TCP_FASTOPEN_CLIENT =
331 (TCP_FASTOPEN_MODE & TFO_ENABLED_CLIENT_MASK) == TFO_ENABLED_CLIENT_MASK;
332
333
334
335
336 static final boolean IS_SUPPORTING_TCP_FASTOPEN_SERVER =
337 (TCP_FASTOPEN_MODE & TFO_ENABLED_SERVER_MASK) == TFO_ENABLED_SERVER_MASK;
338
339 private static final int[] REQUIRED_IORING_OPS = {
340 IORING_OP_POLL_ADD,
341 IORING_OP_TIMEOUT,
342 IORING_OP_ACCEPT,
343 IORING_OP_READ,
344 IORING_OP_WRITE,
345 IORING_OP_POLL_REMOVE,
346 IORING_OP_CONNECT,
347 IORING_OP_CLOSE,
348 IORING_OP_WRITEV,
349 IORING_OP_SENDMSG,
350 IORING_OP_RECVMSG,
351 IORING_OP_ASYNC_CANCEL,
352 IORING_OP_RECV,
353 IORING_OP_NOP,
354 IORING_OP_SHUTDOWN,
355 IORING_OP_SEND
356 };
357
358 static int setupFlags() {
359 int flags = Native.IORING_SETUP_R_DISABLED | Native.IORING_SETUP_CLAMP;
360 if (IoUring.isSetupSubmitAllSupported()) {
361 flags |= Native.IORING_SETUP_SUBMIT_ALL;
362 }
363
364
365 if (IoUring.isSetupSingleIssuerSupported()) {
366 flags |= Native.IORING_SETUP_SINGLE_ISSUER;
367 }
368 if (IoUring.isSetupDeferTaskrunSupported()) {
369 flags |= Native.IORING_SETUP_DEFER_TASKRUN;
370 }
371
372
373 if (IoUring.isIoringSetupNoSqarraySupported()) {
374 flags |= Native.IORING_SETUP_NO_SQARRAY;
375 }
376 return flags;
377 }
378
379 static RingBuffer createRingBuffer(int ringSize, int setupFlags) {
380 return createRingBuffer(ringSize, ringSize * 2, setupFlags);
381 }
382
383 static RingBuffer createRingBuffer(int ringSize, int cqeSize, int setupFlags) {
384 ObjectUtil.checkPositive(ringSize, "ringSize");
385 ObjectUtil.checkPositive(cqeSize, "cqeSize");
386 long[] values = ioUringSetup(ringSize, cqeSize, setupFlags);
387 assert values.length == 20;
388 long cqkhead = values[0];
389 long cqktail = values[1];
390 int cqringMask = (int) values[2];
391 int cqringEntries = (int) values[3];
392 long cqkflags = values[4];
393 long cqArrayAddress = values[5];
394 int cqringSize = (int) values[6];
395 long cqringAddress = values[7];
396 int cqringFd = (int) values[8];
397 int cqringCapacity = (int) values[9];
398 int cqeLength = (setupFlags & IORING_SETUP_CQE32) == 0 ? CQE_SIZE : CQE32_SIZE;
399 CompletionQueue completionQueue = new CompletionQueue(
400 Buffer.wrapMemoryAddressWithNativeOrder(cqkhead, Integer.BYTES),
401 Buffer.wrapMemoryAddressWithNativeOrder(cqktail, Integer.BYTES),
402 cqringMask,
403 cqringEntries,
404 Buffer.wrapMemoryAddressWithNativeOrder(cqkflags, Integer.BYTES),
405 Buffer.wrapMemoryAddressWithNativeOrder(cqArrayAddress, cqringEntries * cqeLength),
406 cqringSize,
407 cqringAddress,
408 cqringFd,
409 cqringCapacity, cqeLength);
410
411 long sqkhead = values[10];
412 long sqktail = values[11];
413 int sqringMask = (int) values[12];
414 int sqringEntries = (int) values[13];
415 long sqkflags = values[14];
416 long sqArrayAddress = values[15];
417 int sqringSize = (int) values[16];
418 long sqringAddress = values[17];
419 int sqringFd = (int) values[18];
420 SubmissionQueue submissionQueue = new SubmissionQueue(
421 Buffer.wrapMemoryAddressWithNativeOrder(sqkhead, Integer.BYTES),
422 Buffer.wrapMemoryAddressWithNativeOrder(sqktail, Integer.BYTES),
423 sqringMask,
424 sqringEntries,
425 Buffer.wrapMemoryAddressWithNativeOrder(sqkflags, Integer.BYTES),
426 Buffer.wrapMemoryAddressWithNativeOrder(sqArrayAddress, sqringEntries * SubmissionQueue.SQE_SIZE),
427 sqringSize,
428 sqringAddress,
429 sqringFd);
430 return new RingBuffer(submissionQueue, completionQueue, (int) values[19]);
431 }
432
433 static void checkAllIOSupported(IoUringProbe probe) {
434 if (!ioUringProbe(probe, REQUIRED_IORING_OPS)) {
435 throw new UnsupportedOperationException("Not all operations are supported: "
436 + Arrays.toString(REQUIRED_IORING_OPS));
437 }
438 }
439
440 static boolean isRecvMultishotSupported() {
441
442 return Native.ioUringSetupSupportsFlags(Native.IORING_SETUP_SINGLE_ISSUER);
443 }
444
445 static boolean isAcceptMultishotSupported(IoUringProbe probe) {
446
447 return ioUringProbe(probe, new int[] { Native.IORING_OP_SOCKET });
448 }
449
450 static boolean isCqeFSockNonEmptySupported(IoUringProbe probe) {
451
452 return ioUringProbe(probe, new int[] { Native.IORING_OP_SOCKET });
453 }
454
455 static boolean isSpliceSupported(IoUringProbe probe) {
456
457 return ioUringProbe(probe, new int[] { Native.IORING_OP_SPLICE });
458 }
459
460 static boolean isPollAddMultiShotSupported(IoUringProbe probe) {
461
462 return isCqeFSockNonEmptySupported(probe);
463 }
464
465 static boolean isSendZcSupported(IoUringProbe probe) {
466
467 return ioUringProbe(probe, new int[] { Native.IORING_OP_SEND_ZC });
468 }
469
470 static boolean isSendmsgZcSupported(IoUringProbe probe) {
471
472 return ioUringProbe(probe, new int[] { Native.IORING_OP_SENDMSG_ZC });
473 }
474
475
476
477
478
479
480 static boolean isRegisterIoWqWorkerSupported(int ringFd) {
481
482 int result = ioUringRegisterIoWqMaxWorkers(ringFd, 0, 0);
483 if (result >= 0) {
484 return true;
485 }
486
487 return false;
488 }
489
490 static boolean isRegisterBufferRingSupported(int ringFd, int flags) {
491 int entries = 2;
492 short bgid = 1;
493 long result = ioUringRegisterBufRing(ringFd, entries, bgid, flags);
494 if (result >= 0) {
495 ioUringUnRegisterBufRing(ringFd, result, entries, bgid);
496 return true;
497 }
498
499 return false;
500 }
501
502 static void checkKernelVersion(String kernelVersion) {
503 boolean enforceKernelVersion = SystemPropertyUtil.getBoolean(
504 "io.netty.transport.iouring.enforceKernelVersion", true);
505 boolean kernelSupported = checkKernelVersion(kernelVersion, 5, 9);
506 if (!kernelSupported) {
507 if (enforceKernelVersion) {
508 throw new UnsupportedOperationException(
509 "you need at least kernel version 5.9, current kernel version: " + kernelVersion);
510 } else {
511 logger.debug("Detected kernel " + kernelVersion + " does not match minimum version of 5.9, " +
512 "trying to use io_uring anyway");
513 }
514 }
515 }
516
517 private static boolean checkKernelVersion(String kernelVersion, int major, int minor) {
518 String[] versionComponents = kernelVersion.split("\\.");
519 if (versionComponents.length < 3) {
520 return false;
521 }
522 int nativeMajor;
523 try {
524 nativeMajor = Integer.parseInt(versionComponents[0]);
525 } catch (NumberFormatException e) {
526 return false;
527 }
528
529 if (nativeMajor < major) {
530 return false;
531 }
532
533 if (nativeMajor > major) {
534 return true;
535 }
536
537 int nativeMinor;
538 try {
539 nativeMinor = Integer.parseInt(versionComponents[1]);
540 } catch (NumberFormatException e) {
541 return false;
542 }
543
544 return nativeMinor >= minor;
545 }
546
547 static final class IoUringProbe {
548 final byte lastOp;
549 final byte opsLen;
550 final IoUringProbeOp[] ops;
551
552 IoUringProbe(int[] values) {
553 int idx = 0;
554 lastOp = (byte) values[idx++];
555 opsLen = (byte) values[idx++];
556 ops = new IoUringProbeOp[opsLen];
557 for (int i = 0; i < opsLen; i++) {
558 ops[i] = new IoUringProbeOp((byte) values[idx++], values[idx++]);
559 }
560 }
561 }
562
563 static class IoUringProbeOp {
564 final byte op;
565 final int flags;
566
567 IoUringProbeOp(byte op, int flags) {
568 this.op = op;
569 this.flags = flags;
570 }
571 }
572
573 static boolean ioUringProbe(IoUringProbe probe, int[] ops) {
574 IoUringProbeOp[] ioUringProbeOps = probe.ops;
575 if (ioUringProbeOps == null) {
576 return false;
577 }
578 for (int op : ops) {
579 if (op > probe.lastOp || (ioUringProbeOps[op].flags & IO_URING_OP_SUPPORTED) == 0) {
580 return false;
581 }
582 }
583 return true;
584 }
585
586 static native boolean ioUringSetupSupportsFlags(int setupFlags);;
587 private static native long[] ioUringSetup(int entries, int cqeSize, int setupFlags);
588
589 static IoUringProbe ioUringProbe(int ringfd) {
590 int[] values = ioUringProbe0(ringfd);
591 if (values == null) {
592 return null;
593 }
594 return new IoUringProbe(values);
595 }
596 private static native int[] ioUringProbe0(int ringFd);
597
598 static native int ioUringRegisterIoWqMaxWorkers(int ringFd, int maxBoundedValue, int maxUnboundedValue);
599 static native int ioUringRegisterEnableRings(int ringFd);
600 static native int ioUringRegisterRingFds(int ringFds);
601
602 static native long ioUringRegisterBufRing(int ringFd, int entries, short bufferGroup, int flags);
603 static native int ioUringUnRegisterBufRing(int ringFd, long ioUringBufRingAddr, int entries, short bufferGroupId);
604 static native int ioUringBufRingSize(int entries);
605 static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags);
606
607 static native void eventFdWrite(int fd, long value);
608
609 static int getFd(DefaultFileRegion fileChannel) {
610 return getFd0(fileChannel);
611 }
612
613 private static native int getFd0(Object fileChannel);
614
615 static FileDescriptor newBlockingEventFd() {
616 return new FileDescriptor(blockingEventFd());
617 }
618
619 static native void ioUringExit(long submissionQueueArrayAddress, int submissionQueueRingEntries,
620 long submissionQueueRingAddress, int submissionQueueRingSize,
621 long completionQueueRingAddress, int completionQueueRingSize,
622 int ringFd, int enterRingFd);
623
624 private static native int blockingEventFd();
625
626
627 static native int createFile(String name);
628
629 private static native int registerUnix();
630
631 static native long cmsghdrData(long hdrAddr);
632
633 static native String kernelVersion();
634
635 private Native() {
636
637 }
638
639
640 private static void loadNativeLibrary() {
641 String name = PlatformDependent.normalizedOs().toLowerCase(Locale.ROOT).trim();
642 if (!name.startsWith("linux")) {
643 throw new IllegalStateException("Only supported on Linux");
644 }
645 String staticLibName = "netty_transport_native_io_uring42";
646 String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();
647 ClassLoader cl = PlatformDependent.getClassLoader(Native.class);
648 try {
649 NativeLibraryLoader.load(sharedLibName, cl);
650 } catch (UnsatisfiedLinkError e1) {
651 try {
652 NativeLibraryLoader.load(staticLibName, cl);
653 logger.info("Failed to load io_uring");
654 } catch (UnsatisfiedLinkError e2) {
655 ThrowableUtil.addSuppressed(e1, e2);
656 throw e1;
657 }
658 }
659 }
660 }