GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port:5.0:IoUring: Add SO_INQ support for Unix domain (#17144) · netty/netty@21d5e2f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • AbstractIoUringChannel.java
    • IoUring.java
    • IoUringSocketChannel.java
    • Native.java
  • transport-native-io_uring/src
    • main/c
      • netty_io_uring_native.c
    • test/java/io/netty/channel/uring
      • IoUringDomainSocketReadLoopTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringChannel.java‎

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 numberDiff line numberDiff line change
@@ -653,10 +653,14 @@ final void schedulePollIn() {
653653
pollInId = schedulePollAdd(POLL_IN_SCHEDULED, Native.POLLIN, allowMultiShotPollIn());
654654
}
655655

656+
protected final boolean isReadMultishot() {
657+
return numOutstandingReads == -1;
658+
}
659+
656660
private void readComplete(byte op, int res, int flags, short data) {
657661
assert numOutstandingReads > 0 || numOutstandingReads == -1 : numOutstandingReads;
658662

659-
boolean multishot = numOutstandingReads == -1;
663+
boolean multishot = isReadMultishot();
660664
boolean rearm = (flags & Native.IORING_CQE_F_MORE) == 0;
661665
if (rearm) {
662666
// Reset READ_SCHEDULED if there is nothing more to handle and so we need to re-arm. This works for
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUring.java‎

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 numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class IoUring {
3131

3232
private static final Throwable UNAVAILABILITY_CAUSE;
3333
private static final boolean IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED;
34+
private static final boolean UNIX_DOMAIN_SOCKET_INQ_SUPPORTED;
3435
private static final boolean IORING_SPLICE_SUPPORTED;
3536
private static final boolean IORING_SEND_ZC_SUPPORTED;
3637
private static final boolean IORING_SENDMSG_ZC_SUPPORTED;
@@ -66,6 +67,7 @@ public final class IoUring {
6667
logger = InternalLoggerFactory.getInstance(IoUring.class);
6768
Throwable cause = null;
6869
boolean socketNonEmptySupported = false;
70+
boolean unixDomainSocketInqSupported = false;
6971
boolean spliceSupported = false;
7072
boolean sendZcSupported = false;
7173
boolean sendmsgZcSupported = false;
@@ -133,6 +135,7 @@ public final class IoUring {
133135
registerBufferRingSupported = Native.isRegisterBufferRingSupported(ringBuffer.fd(), 0);
134136
registerBufferRingIncSupported = Native.isRegisterBufferRingSupported(ringBuffer.fd(),
135137
Native.IOU_PBUF_RING_INC);
138+
unixDomainSocketInqSupported = Native.isUnixDomainSocketInqSupported();
136139
} finally {
137140
if (ringBuffer != null) {
138141
try {
@@ -149,6 +152,7 @@ public final class IoUring {
149152
// Assign static finals first so printFeatures() (no-arg) can read them.
150153
UNAVAILABILITY_CAUSE = cause;
151154
IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED = socketNonEmptySupported;
155+
UNIX_DOMAIN_SOCKET_INQ_SUPPORTED = unixDomainSocketInqSupported;
152156
IORING_SPLICE_SUPPORTED = spliceSupported;
153157
IORING_SEND_ZC_SUPPORTED = sendZcSupported;
154158
IORING_SENDMSG_ZC_SUPPORTED = sendmsgZcSupported;
@@ -246,6 +250,10 @@ static boolean isCqeFSockNonEmptySupported() {
246250
return IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED;
247251
}
248252

253+
static boolean isUnixDomainSocketInqSupported() {
254+
return UNIX_DOMAIN_SOCKET_INQ_SUPPORTED;
255+
}
256+
249257
/**
250258
* Returns if SPLICE is supported or not.
251259
*
@@ -320,7 +328,6 @@ static boolean isSetupDeferTaskrunSupported() {
320328
static boolean isIoringSetupNoSqarraySupported() {
321329
return IORING_SETUP_NO_SQARRAY_SUPPORTED;
322330
}
323-
324331
/**
325332
* Returns if it is supported to use a buffer ring.
326333
*
@@ -417,6 +424,7 @@ private static String supportedFeatures() {
417424
return "";
418425
}
419426
return "CQE_F_SOCK_NONEMPTY_SUPPORTED=" + IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED
427+
+ ", UNIX_DOMAIN_SOCKET_INQ_SUPPORTED=" + UNIX_DOMAIN_SOCKET_INQ_SUPPORTED
420428
+ ", SPLICE_SUPPORTED=" + IORING_SPLICE_SUPPORTED
421429
+ ", ACCEPT_NO_WAIT_SUPPORTED=" + IORING_ACCEPT_NO_WAIT_SUPPORTED
422430
+ ", ACCEPT_MULTISHOT_SUPPORTED=" + IORING_ACCEPT_MULTISHOT_SUPPORTED
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringSocketChannel.java‎

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 numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta
537537
short bid = (short) (flags >> Native.IORING_CQE_BUFFER_SHIFT);
538538
boolean more = (flags & Native.IORING_CQE_F_BUF_MORE) != 0;
539539

540-
boolean empty = socketIsEmpty(flags);
540+
boolean completeRead = shouldCompleteReadLoop(flags, isReadMultishot());
541541
if (rearm) {
542542
// Only reset if we don't use multi-shot or we need to re-arm because the multi-shot was cancelled.
543543
readId = 0;
@@ -631,15 +631,26 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta
631631
allocHandle.incMessagesRead(1);
632632
pipeline.fireChannelRead(byteBuf);
633633
byteBuf = null;
634-
scheduleNextRead(pipeline, allocHandle, rearm, empty);
634+
scheduleNextRead(pipeline, allocHandle, rearm, completeRead);
635635
} catch (Throwable t) {
636636
handleReadException(pipeline, byteBuf, t, allDataRead, allocHandle);
637637
}
638638
}
639639

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+
640651
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) {
643654
if (rearm) {
644655
// We only should schedule another read if we need to rearm.
645656
// 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
775786

776787
@Override
777788
protected boolean socketIsEmpty(int flags) {
789+
if (socket.protocolFamily() == SocketProtocolFamily.UNIX && !IoUring.isUnixDomainSocketInqSupported()) {
790+
return false;
791+
}
778792
return IoUring.isCqeFSockNonEmptySupported() && (flags & Native.IORING_CQE_F_SOCK_NONEMPTY) == 0;
779793
}
780794

Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/Native.java‎

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 numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ static boolean ioUringProbe(IoUringProbe probe, int[] ops) {
603603
}
604604

605605
static native boolean ioUringSetupSupportsFlags(int setupFlags);
606+
static native boolean isUnixDomainSocketInqSupported();
606607
private static native long[] ioUringSetup(int entries, int cqeSize, int setupFlags);
607608

608609
static IoUringProbe ioUringProbe(int ringfd) {
Collapse file

‎transport-native-io_uring/src/main/c/netty_io_uring_native.c‎

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 numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
#define UDP_SEGMENT 103
5454
#endif
5555

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+
5661
// Add define if NETTY_IO_URING_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
5762
#ifdef NETTY_IO_URING_BUILD_STATIC
5863
#define NETTY_JNI_UTIL_BUILD_STATIC
@@ -279,6 +284,18 @@ static jboolean netty_io_uring_setup_supports_flags(JNIEnv *env, jclass clazz, j
279284
return JNI_TRUE;
280285
}
281286

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+
282299
static jintArray netty_io_uring_probe0(JNIEnv *env, jclass clazz, jint ring_fd) {
283300
jintArray array = NULL;
284301
struct io_uring_probe *probe;
@@ -848,6 +865,8 @@ static const jint statically_referenced_fixed_method_table_size = sizeof(statica
848865

849866
static const JNINativeMethod method_table[] = {
850867
{"ioUringSetupSupportsFlags", "(I)Z", (void *) netty_io_uring_setup_supports_flags },
868+
{"isUnixDomainSocketInqSupported", "()Z",
869+
(void *) netty_io_uring_is_unix_domain_socket_inq_supported },
851870
{"ioUringSetup", "(III)[J", (void *) netty_io_uring_setup},
852871
{"ioUringRegisterIoWqMaxWorkers","(III)I", (void*) netty_io_uring_register_iowq_max_workers },
853872
{"ioUringRegisterEnableRings","(I)I", (void*) netty_io_uring_register_enable_rings },
@@ -974,4 +993,3 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) {
974993
netty_jni_util_JNI_OnUnload(vm, reserved, netty_iouring_native_JNI_OnUnload);
975994
}
976995
#endif /* NETTY_IO_URING_BUILD_STATIC */
977-

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.