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  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                                  // ignore
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       * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
79       * {@linkplain ChannelOption#TCP_FASTOPEN_CONNECT client-side TCP FastOpen}.
80       *
81       * @return {@code true} if it's possible to use client-side TCP FastOpen via io_uring, otherwise {@code false}.
82       */
83      public static boolean isTcpFastOpenClientSideAvailable() {
84          return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
85      }
86  
87      /**
88       * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
89       * {@linkplain ChannelOption#TCP_FASTOPEN server-side TCP FastOpen}.
90       *
91       * @return {@code true} if it's possible to use server-side TCP FastOpen via io_uring, otherwise {@code false}.
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 }