View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.channel.ChannelOption;
19  import io.netty.channel.unix.FileDescriptor;
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  /**
25   * Tells if <a href="https://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
26   * supported.
27   */
28  public final class Epoll {
29  
30      private static final Throwable UNAVAILABILITY_CAUSE;
31  
32      static {
33          Throwable cause = null;
34  
35          if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
36              cause = new UnsupportedOperationException(
37                      "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
38          } else {
39              FileDescriptor epollFd = null;
40              FileDescriptor eventFd = null;
41              try {
42                  epollFd = Native.newEpollCreate();
43                  eventFd = Native.newEventFd();
44              } catch (Throwable t) {
45                  cause = t;
46              } finally {
47                  if (epollFd != null) {
48                      try {
49                          epollFd.close();
50                      } catch (Exception ignore) {
51                          // ignore
52                      }
53                  }
54                  if (eventFd != null) {
55                      try {
56                          eventFd.close();
57                      } catch (Exception ignore) {
58                          // ignore
59                      }
60                  }
61              }
62          }
63          if (cause != null) {
64              InternalLogger logger = InternalLoggerFactory.getInstance(Epoll.class);
65              if (logger.isTraceEnabled()) {
66                  logger.debug("Epoll support is not available", cause);
67              } else if (logger.isDebugEnabled()) {
68                  logger.debug("Epoll support is not available: {}", cause.getMessage());
69              }
70          }
71          UNAVAILABILITY_CAUSE = cause;
72      }
73  
74      /**
75       * Returns {@code true} if and only if the <a href="https://netty.io/wiki/native-transports.html">{@code
76       * netty-transport-native-epoll}</a> is available.
77       */
78      public static boolean isAvailable() {
79          return UNAVAILABILITY_CAUSE == null;
80      }
81  
82      /**
83       * Ensure that <a href="https://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
84       * available.
85       *
86       * @throws UnsatisfiedLinkError if unavailable
87       */
88      public static void ensureAvailability() {
89          if (UNAVAILABILITY_CAUSE != null) {
90              throw (Error) new UnsatisfiedLinkError(
91                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
92          }
93      }
94  
95      /**
96       * Returns the cause of unavailability of <a href="https://netty.io/wiki/native-transports.html">
97       * {@code netty-transport-native-epoll}</a>.
98       *
99       * @return the cause if unavailable. {@code null} if available.
100      */
101     public static Throwable unavailabilityCause() {
102         return UNAVAILABILITY_CAUSE;
103     }
104 
105     /**
106      * Returns {@code true} if the epoll native transport is both {@linkplain #isAvailable() available} and supports
107      * {@linkplain ChannelOption#TCP_FASTOPEN_CONNECT client-side TCP FastOpen}.
108      *
109      * @return {@code true} if it's possible to use client-side TCP FastOpen via epoll, otherwise {@code false}.
110      */
111     public static boolean isTcpFastOpenClientSideAvailable() {
112         return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
113     }
114 
115     /**
116      * Returns {@code true} if the epoll native transport is both {@linkplain #isAvailable() available} and supports
117      * {@linkplain ChannelOption#TCP_FASTOPEN server-side TCP FastOpen}.
118      *
119      * @return {@code true} if it's possible to use server-side TCP FastOpen via epoll, otherwise {@code false}.
120      */
121     public static boolean isTcpFastOpenServerSideAvailable() {
122         return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_SERVER;
123     }
124 
125     private Epoll() {
126     }
127 }