View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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                                  // ignore
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       * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
69       * {@linkplain ChannelOption#TCP_FASTOPEN_CONNECT client-side TCP FastOpen}.
70       *
71       * @return {@code true} if it's possible to use client-side TCP FastOpen via io_uring, otherwise {@code false}.
72       */
73      public static boolean isTcpFastOpenClientSideAvailable() {
74          return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
75      }
76  
77      /**
78       * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
79       * {@linkplain ChannelOption#TCP_FASTOPEN server-side TCP FastOpen}.
80       *
81       * @return {@code true} if it's possible to use server-side TCP FastOpen via io_uring, otherwise {@code false}.
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 }