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