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.ChannelOption;
19 import io.netty.util.internal.PlatformDependent;
20 import io.netty.util.internal.SystemPropertyUtil;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23
24 public final class IoUring {
25
26 private static final Throwable UNAVAILABILITY_CAUSE;
27 private static final boolean IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED;
28 static {
29 Throwable cause = null;
30 boolean supported = false;
31 try {
32 if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
33 cause = new UnsupportedOperationException(
34 "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
35 } else {
36 String kernelVersion = Native.kernelVersion();
37 Native.checkKernelVersion(kernelVersion);
38 Throwable unsafeCause = PlatformDependent.getUnsafeUnavailabilityCause();
39 if (unsafeCause == null) {
40 RingBuffer ringBuffer = null;
41 try {
42 ringBuffer = Native.createRingBuffer();
43 Native.checkAllIOSupported(ringBuffer.fd());
44 supported = Native.isIOUringCqeFSockNonEmptySupported(ringBuffer.fd());
45 } finally {
46 if (ringBuffer != null) {
47 try {
48 ringBuffer.close();
49 } catch (Exception ignore) {
50
51 }
52 }
53 }
54 } else {
55 cause = new UnsupportedOperationException("Unsafe is not supported", unsafeCause);
56 }
57 }
58 } catch (Throwable t) {
59 cause = t;
60 }
61 if (cause != null) {
62 InternalLogger logger = InternalLoggerFactory.getInstance(IoUring.class);
63 if (logger.isTraceEnabled()) {
64 logger.debug("IoUring support is not available", cause);
65 } else if (logger.isDebugEnabled()) {
66 logger.debug("IoUring support is not available: {}", cause.getMessage());
67 }
68 }
69 UNAVAILABILITY_CAUSE = cause;
70 IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED = supported;
71 }
72
73 public static boolean isAvailable() {
74 return UNAVAILABILITY_CAUSE == null;
75 }
76
77
78
79
80
81
82
83 public static boolean isTcpFastOpenClientSideAvailable() {
84 return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
85 }
86
87
88
89
90
91
92
93 public static boolean isTcpFastOpenServerSideAvailable() {
94 return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_SERVER;
95 }
96
97 static boolean isIOUringCqeFSockNonEmptySupported() {
98 return IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED;
99 }
100
101 public static void ensureAvailability() {
102 if (UNAVAILABILITY_CAUSE != null) {
103 throw (Error) new UnsatisfiedLinkError(
104 "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
105 }
106 }
107
108 public static Throwable unavailabilityCause() {
109 return UNAVAILABILITY_CAUSE;
110 }
111
112 private IoUring() {
113 }
114 }