GeezerVison is showing github.com — Port:5.0:IoUring: Add SO_INQ support for Unix domain (#17144) · netty/netty@21d5e2f
File treeExpand file treeCollapse file tree Open diff view settings transport-classes-io_uring/src/main/java/io/netty/channel/uring transport-native-io_uring/src test/java/io/netty/channel/uring
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringChannel.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -653,10 +653,14 @@ final void schedulePollIn() { | 653 | 653 | pollInId = schedulePollAdd(POLL_IN_SCHEDULED, Native.POLLIN, allowMultiShotPollIn()); | 654 | 654 | } | 655 | 655 |
| | 656 | + protected final boolean isReadMultishot() { | | 657 | + return numOutstandingReads == -1; | | 658 | + } | | 659 | + | 656 | 660 | private void readComplete(byte op, int res, int flags, short data) { | 657 | 661 | assert numOutstandingReads > 0 || numOutstandingReads == -1 : numOutstandingReads; | 658 | 662 |
| 659 | | - boolean multishot = numOutstandingReads == -1; | | 663 | + boolean multishot = isReadMultishot(); | 660 | 664 | boolean rearm = (flags & Native.IORING_CQE_F_MORE) == 0; | 661 | 665 | if (rearm) { | 662 | 666 | // Reset READ_SCHEDULED if there is nothing more to handle and so we need to re-arm. This works for | |
Collapse file Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUring.java +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ public final class IoUring { | 31 | 31 |
| 32 | 32 | private static final Throwable UNAVAILABILITY_CAUSE; | 33 | 33 | private static final boolean IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED; | | 34 | + private static final boolean UNIX_DOMAIN_SOCKET_INQ_SUPPORTED; | 34 | 35 | private static final boolean IORING_SPLICE_SUPPORTED; | 35 | 36 | private static final boolean IORING_SEND_ZC_SUPPORTED; | 36 | 37 | private static final boolean IORING_SENDMSG_ZC_SUPPORTED; | @@ -66,6 +67,7 @@ public final class IoUring { | 66 | 67 | logger = InternalLoggerFactory.getInstance(IoUring.class); | 67 | 68 | Throwable cause = null; | 68 | 69 | boolean socketNonEmptySupported = false; | | 70 | + boolean unixDomainSocketInqSupported = false; | 69 | 71 | boolean spliceSupported = false; | 70 | 72 | boolean sendZcSupported = false; | 71 | 73 | boolean sendmsgZcSupported = false; | @@ -133,6 +135,7 @@ public final class IoUring { | 133 | 135 | registerBufferRingSupported = Native.isRegisterBufferRingSupported(ringBuffer.fd(), 0); | 134 | 136 | registerBufferRingIncSupported = Native.isRegisterBufferRingSupported(ringBuffer.fd(), | 135 | 137 | Native.IOU_PBUF_RING_INC); | | 138 | + unixDomainSocketInqSupported = Native.isUnixDomainSocketInqSupported(); | 136 | 139 | } finally { | 137 | 140 | if (ringBuffer != null) { | 138 | 141 | try { | @@ -149,6 +152,7 @@ public final class IoUring { | 149 | 152 | // Assign static finals first so printFeatures() (no-arg) can read them. | 150 | 153 | UNAVAILABILITY_CAUSE = cause; | 151 | 154 | IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED = socketNonEmptySupported; | | 155 | + UNIX_DOMAIN_SOCKET_INQ_SUPPORTED = unixDomainSocketInqSupported; | 152 | 156 | IORING_SPLICE_SUPPORTED = spliceSupported; | 153 | 157 | IORING_SEND_ZC_SUPPORTED = sendZcSupported; | 154 | 158 | IORING_SENDMSG_ZC_SUPPORTED = sendmsgZcSupported; | @@ -246,6 +250,10 @@ static boolean isCqeFSockNonEmptySupported() { | 246 | 250 | return IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED; | 247 | 251 | } | 248 | 252 |
| | 253 | + static boolean isUnixDomainSocketInqSupported() { | | 254 | + return UNIX_DOMAIN_SOCKET_INQ_SUPPORTED; | | 255 | + } | | 256 | + | 249 | 257 | /** | 250 | 258 | * Returns if SPLICE is supported or not. | 251 | 259 | * | @@ -320,7 +328,6 @@ static boolean isSetupDeferTaskrunSupported() { | 320 | 328 | static boolean isIoringSetupNoSqarraySupported() { | 321 | 329 | return IORING_SETUP_NO_SQARRAY_SUPPORTED; | 322 | 330 | } | 323 | | - | 324 | 331 | /** | 325 | 332 | * Returns if it is supported to use a buffer ring. | 326 | 333 | * | @@ -417,6 +424,7 @@ private static String supportedFeatures() { | 417 | 424 | return ""; | 418 | 425 | } | 419 | 426 | return "CQE_F_SOCK_NONEMPTY_SUPPORTED=" + IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED | | 427 | + + ", UNIX_DOMAIN_SOCKET_INQ_SUPPORTED=" + UNIX_DOMAIN_SOCKET_INQ_SUPPORTED | 420 | 428 | + ", SPLICE_SUPPORTED=" + IORING_SPLICE_SUPPORTED | 421 | 429 | + ", ACCEPT_NO_WAIT_SUPPORTED=" + IORING_ACCEPT_NO_WAIT_SUPPORTED | 422 | 430 | + ", ACCEPT_MULTISHOT_SUPPORTED=" + IORING_ACCEPT_MULTISHOT_SUPPORTED | |
Collapse file Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringSocketChannel.java +18-4Lines changed: 18 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -537,7 +537,7 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta | 537 | 537 | short bid = (short) (flags >> Native.IORING_CQE_BUFFER_SHIFT); | 538 | 538 | boolean more = (flags & Native.IORING_CQE_F_BUF_MORE) != 0; | 539 | 539 |
| 540 | | - boolean empty = socketIsEmpty(flags); | | 540 | + boolean completeRead = shouldCompleteReadLoop(flags, isReadMultishot()); | 541 | 541 | if (rearm) { | 542 | 542 | // Only reset if we don't use multi-shot or we need to re-arm because the multi-shot was cancelled. | 543 | 543 | readId = 0; | @@ -631,15 +631,26 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta | 631 | 631 | allocHandle.incMessagesRead(1); | 632 | 632 | pipeline.fireChannelRead(byteBuf); | 633 | 633 | byteBuf = null; | 634 | | - scheduleNextRead(pipeline, allocHandle, rearm, empty); | | 634 | + scheduleNextRead(pipeline, allocHandle, rearm, completeRead); | 635 | 635 | } catch (Throwable t) { | 636 | 636 | handleReadException(pipeline, byteBuf, t, allDataRead, allocHandle); | 637 | 637 | } | 638 | 638 | } | 639 | 639 |
| | 640 | + private boolean shouldCompleteReadLoop(int flags, boolean multishot) { | | 641 | + if (socket.protocolFamily() == SocketProtocolFamily.UNIX && !IoUring.isUnixDomainSocketInqSupported()) { | | 642 | + // Older kernels cannot report IORING_CQE_F_SOCK_NONEMPTY for UDS, so the read-loop boundary cannot be | | 643 | + // determined reliably. | | 644 | + // Multishot recv does not produce an EAGAIN completion while it remains armed, so | | 645 | + // complete the read loop for each multishot completion. A one-shot recv can continue until EAGAIN. | | 646 | + return multishot; | | 647 | + } | | 648 | + return socketIsEmpty(flags); | | 649 | + } | | 650 | + | 640 | 651 | private void scheduleNextRead(ChannelPipeline pipeline, IoUringRecvByteAllocatorHandle allocHandle, | 641 | | - boolean rearm, boolean empty) { | 642 | | - if (allocHandle.continueReading() && !empty) { | | 652 | + boolean rearm, boolean completeRead) { | | 653 | + if (allocHandle.continueReading() && !completeRead) { | 643 | 654 | if (rearm) { | 644 | 655 | // We only should schedule another read if we need to rearm. | 645 | 656 | // See https://github.com/axboe/liburing/wiki/io_uring-and-networking-in-2023#multi-shot | @@ -775,6 +786,9 @@ protected void cancelOutstandingWrites(IoRegistration registration, int numOutst | 775 | 786 |
| 776 | 787 | @Override | 777 | 788 | protected boolean socketIsEmpty(int flags) { | | 789 | + if (socket.protocolFamily() == SocketProtocolFamily.UNIX && !IoUring.isUnixDomainSocketInqSupported()) { | | 790 | + return false; | | 791 | + } | 778 | 792 | return IoUring.isCqeFSockNonEmptySupported() && (flags & Native.IORING_CQE_F_SOCK_NONEMPTY) == 0; | 779 | 793 | } | 780 | 794 |
| |
Collapse file Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/Native.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -603,6 +603,7 @@ static boolean ioUringProbe(IoUringProbe probe, int[] ops) { | 603 | 603 | } | 604 | 604 |
| 605 | 605 | static native boolean ioUringSetupSupportsFlags(int setupFlags); | | 606 | + static native boolean isUnixDomainSocketInqSupported(); | 606 | 607 | private static native long[] ioUringSetup(int entries, int cqeSize, int setupFlags); | 607 | 608 |
| 608 | 609 | static IoUringProbe ioUringProbe(int ringfd) { | |
Collapse file Copy file name to clipboardExpand all lines: transport-native-io_uring/src/main/c/netty_io_uring_native.c +19-1Lines changed: 19 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 53 | 53 | #define UDP_SEGMENT 103 | 54 | 54 | #endif | 55 | 55 |
| | 56 | +// SO_INQ was added in Linux 6.17. Define it here so we can compile with older kernel headers. | | 57 | +#ifndef SO_INQ | | 58 | +#define SO_INQ 84 | | 59 | +#endif | | 60 | + | 56 | 61 | // Add define if NETTY_IO_URING_BUILD_STATIC is defined so it is picked up in netty_jni_util.c | 57 | 62 | #ifdef NETTY_IO_URING_BUILD_STATIC | 58 | 63 | #define NETTY_JNI_UTIL_BUILD_STATIC | @@ -279,6 +284,18 @@ static jboolean netty_io_uring_setup_supports_flags(JNIEnv *env, jclass clazz, j | 279 | 284 | return JNI_TRUE; | 280 | 285 | } | 281 | 286 |
| | 287 | +static jboolean netty_io_uring_is_unix_domain_socket_inq_supported(JNIEnv *env, jclass clazz) { | | 288 | + int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); | | 289 | + if (fd < 0) { | | 290 | + return JNI_FALSE; | | 291 | + } | | 292 | + | | 293 | + int enable = 1; | | 294 | + int ret = setsockopt(fd, SOL_SOCKET, SO_INQ, &enable, sizeof(enable)); | | 295 | + close(fd); | | 296 | + return ret == 0 ? JNI_TRUE : JNI_FALSE; | | 297 | +} | | 298 | + | 282 | 299 | static jintArray netty_io_uring_probe0(JNIEnv *env, jclass clazz, jint ring_fd) { | 283 | 300 | jintArray array = NULL; | 284 | 301 | struct io_uring_probe *probe; | @@ -848,6 +865,8 @@ static const jint statically_referenced_fixed_method_table_size = sizeof(statica | 848 | 865 |
| 849 | 866 | static const JNINativeMethod method_table[] = { | 850 | 867 | {"ioUringSetupSupportsFlags", "(I)Z", (void *) netty_io_uring_setup_supports_flags }, | | 868 | + {"isUnixDomainSocketInqSupported", "()Z", | | 869 | + (void *) netty_io_uring_is_unix_domain_socket_inq_supported }, | 851 | 870 | {"ioUringSetup", "(III)[J", (void *) netty_io_uring_setup}, | 852 | 871 | {"ioUringRegisterIoWqMaxWorkers","(III)I", (void*) netty_io_uring_register_iowq_max_workers }, | 853 | 872 | {"ioUringRegisterEnableRings","(I)I", (void*) netty_io_uring_register_enable_rings }, | @@ -974,4 +993,3 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) { | 974 | 993 | netty_jni_util_JNI_OnUnload(vm, reserved, netty_iouring_native_JNI_OnUnload); | 975 | 994 | } | 976 | 995 | #endif /* NETTY_IO_URING_BUILD_STATIC */ | 977 | | - | |
|