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      private static final boolean IORING_SPLICE_SUPPORTED;
29      private static final boolean IORING_ACCEPT_NO_WAIT_SUPPORTED;
30      private static final boolean IORING_REGISTER_IOWQ_MAX_WORKERS_SUPPORTED;
31  
32      static {
33          Throwable cause = null;
34          boolean socketNonEmptySupported = false;
35          boolean spliceSupported = false;
36          boolean acceptSupportNoWait = false;
37          boolean registerIowqWorkersSupported = false;
38          try {
39              if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
40                  cause = new UnsupportedOperationException(
41                          "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
42              } else {
43                  String kernelVersion = Native.kernelVersion();
44                  Native.checkKernelVersion(kernelVersion);
45                  Throwable unsafeCause = PlatformDependent.getUnsafeUnavailabilityCause();
46                  if (unsafeCause == null) {
47                      RingBuffer ringBuffer = null;
48                      try {
49                          ringBuffer = Native.createRingBuffer();
50                          Native.checkAllIOSupported(ringBuffer.fd());
51                          socketNonEmptySupported = Native.isIOUringCqeFSockNonEmptySupported(ringBuffer.fd());
52                          spliceSupported = Native.isIOUringSupportSplice(ringBuffer.fd());
53                          // IORING_FEAT_RECVSEND_BUNDLE was adde in the same release.
54                          acceptSupportNoWait = (ringBuffer.features() & Native.IORING_FEAT_RECVSEND_BUNDLE) != 0;
55                          registerIowqWorkersSupported = Native.isRegisterIOWQWorkerSupported(ringBuffer.fd());
56                      } finally {
57                          if (ringBuffer != null) {
58                              try {
59                                  ringBuffer.close();
60                              } catch (Exception ignore) {
61                                  // ignore
62                              }
63                          }
64                      }
65                  } else {
66                      cause = new UnsupportedOperationException("Unsafe is not supported", unsafeCause);
67                  }
68              }
69          } catch (Throwable t) {
70              cause = t;
71          }
72          if (cause != null) {
73              InternalLogger logger = InternalLoggerFactory.getInstance(IoUring.class);
74              if (logger.isTraceEnabled()) {
75                  logger.debug("IoUring support is not available", cause);
76              } else if (logger.isDebugEnabled()) {
77                  logger.debug("IoUring support is not available: {}", cause.getMessage());
78              }
79          }
80          UNAVAILABILITY_CAUSE = cause;
81          IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED = socketNonEmptySupported;
82          IORING_SPLICE_SUPPORTED = spliceSupported;
83          IORING_ACCEPT_NO_WAIT_SUPPORTED = acceptSupportNoWait;
84          IORING_REGISTER_IOWQ_MAX_WORKERS_SUPPORTED = registerIowqWorkersSupported;
85      }
86  
87      public static boolean isAvailable() {
88          return UNAVAILABILITY_CAUSE == null;
89      }
90  
91      /**
92       * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
93       * {@linkplain ChannelOption#TCP_FASTOPEN_CONNECT client-side TCP FastOpen}.
94       *
95       * @return {@code true} if it's possible to use client-side TCP FastOpen via io_uring, otherwise {@code false}.
96       */
97      public static boolean isTcpFastOpenClientSideAvailable() {
98          return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
99      }
100 
101     /**
102      * Returns {@code true} if the io_uring native transport is both {@linkplain #isAvailable() available} and supports
103      * {@linkplain ChannelOption#TCP_FASTOPEN server-side TCP FastOpen}.
104      *
105      * @return {@code true} if it's possible to use server-side TCP FastOpen via io_uring, otherwise {@code false}.
106      */
107     public static boolean isTcpFastOpenServerSideAvailable() {
108         return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_SERVER;
109     }
110 
111     static boolean isIOUringCqeFSockNonEmptySupported() {
112         return IORING_CQE_F_SOCK_NONEMPTY_SUPPORTED;
113     }
114 
115     static boolean isIOUringSpliceSupported() {
116         return IORING_SPLICE_SUPPORTED;
117     }
118 
119     static boolean isIOUringAcceptNoWaitSupported() {
120         return IORING_ACCEPT_NO_WAIT_SUPPORTED;
121     }
122 
123     static boolean isRegisterIowqMaxWorkersSupported() {
124         return IORING_REGISTER_IOWQ_MAX_WORKERS_SUPPORTED;
125     }
126 
127     public static void ensureAvailability() {
128         if (UNAVAILABILITY_CAUSE != null) {
129             throw (Error) new UnsatisfiedLinkError(
130                     "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
131         }
132     }
133 
134     public static Throwable unavailabilityCause() {
135         return UNAVAILABILITY_CAUSE;
136     }
137 
138     private IoUring() {
139     }
140 }