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.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.ServerChannelRecvByteBufAllocator;
25  import io.netty.channel.WriteBufferWaterMark;
26  import io.netty.channel.socket.ServerSocketChannelConfig;
27  import io.netty.util.NetUtil;
28  
29  import java.io.IOException;
30  import java.util.Map;
31  
32  import static io.netty.channel.ChannelOption.TCP_FASTOPEN;
33  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
34  
35  final class IoUringServerSocketChannelConfig extends DefaultChannelConfig implements ServerSocketChannelConfig {
36      private volatile int backlog = NetUtil.SOMAXCONN;
37      private volatile int pendingFastOpenRequestsThreshold;
38  
39      IoUringServerSocketChannelConfig(AbstractIoUringServerChannel channel) {
40          super(channel, new ServerChannelRecvByteBufAllocator());
41          setReuseAddress(true);
42      }
43  
44      @Override
45      public Map<ChannelOption<?>, Object> getOptions() {
46          return getOptions(super.getOptions(), ChannelOption.SO_RCVBUF, ChannelOption.SO_REUSEADDR,
47                  ChannelOption.SO_BACKLOG, IoUringChannelOption.SO_REUSEPORT, IoUringChannelOption.IP_FREEBIND,
48                  IoUringChannelOption.IP_TRANSPARENT, IoUringChannelOption.TCP_DEFER_ACCEPT, ChannelOption.TCP_FASTOPEN);
49      }
50  
51      @SuppressWarnings("unchecked")
52      @Override
53      public <T> T getOption(ChannelOption<T> option) {
54          if (option == ChannelOption.SO_RCVBUF) {
55              return (T) Integer.valueOf(getReceiveBufferSize());
56          }
57          if (option == ChannelOption.SO_REUSEADDR) {
58              return (T) Boolean.valueOf(isReuseAddress());
59          }
60          if (option == ChannelOption.SO_BACKLOG) {
61              return (T) Integer.valueOf(getBacklog());
62          }
63          if (option == IoUringChannelOption.SO_REUSEPORT) {
64              return (T) Boolean.valueOf(isReusePort());
65          }
66          if (option == IoUringChannelOption.IP_FREEBIND) {
67              return (T) Boolean.valueOf(isFreeBind());
68          }
69          if (option == IoUringChannelOption.IP_TRANSPARENT) {
70              return (T) Boolean.valueOf(isIpTransparent());
71          }
72          if (option == IoUringChannelOption.TCP_DEFER_ACCEPT) {
73              return (T) Integer.valueOf(getTcpDeferAccept());
74          }
75          if (option == TCP_FASTOPEN) {
76              return (T) Integer.valueOf(getTcpFastopen());
77          }
78          return super.getOption(option);
79      }
80  
81      @Override
82      public <T> boolean setOption(ChannelOption<T> option, T value) {
83          validate(option, value);
84          if (option == ChannelOption.SO_RCVBUF) {
85              setReceiveBufferSize((Integer) value);
86          } else if (option == ChannelOption.SO_REUSEADDR) {
87              setReuseAddress((Boolean) value);
88          } else if (option == ChannelOption.SO_BACKLOG) {
89              setBacklog((Integer) value);
90          } else if (option == IoUringChannelOption.SO_REUSEPORT) {
91              setReusePort((Boolean) value);
92          } else if (option == IoUringChannelOption.IP_FREEBIND) {
93              setFreeBind((Boolean) value);
94          } else if (option == IoUringChannelOption.IP_TRANSPARENT) {
95              setIpTransparent((Boolean) value);
96          } else if (option == IoUringChannelOption.TCP_DEFER_ACCEPT) {
97              setTcpDeferAccept((Integer) value);
98          } else if (option == TCP_FASTOPEN) {
99              setTcpFastopen((Integer) value);
100         } else {
101             return super.setOption(option, value);
102         }
103 
104         return true;
105     }
106 
107     @Override
108     public IoUringServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
109         return this;
110     }
111 
112     @Override
113     public boolean isReuseAddress() {
114         try {
115             return ((AbstractIoUringChannel) channel).socket.isReuseAddress();
116         } catch (IOException e) {
117             throw new ChannelException(e);
118         }
119     }
120 
121     @Override
122     public IoUringServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
123         try {
124             ((AbstractIoUringChannel) channel).socket.setReuseAddress(reuseAddress);
125             return this;
126         } catch (IOException e) {
127             throw new ChannelException(e);
128         }
129     }
130 
131     @Override
132     public int getReceiveBufferSize() {
133         try {
134             return ((AbstractIoUringChannel) channel).socket.getReceiveBufferSize();
135         } catch (IOException e) {
136             throw new ChannelException(e);
137         }
138     }
139 
140     @Override
141     public IoUringServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
142         try {
143             ((AbstractIoUringChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
144             return this;
145         } catch (IOException e) {
146             throw new ChannelException(e);
147         }
148     }
149 
150     @Override
151     public int getBacklog() {
152         return backlog;
153     }
154 
155     @Override
156     public IoUringServerSocketChannelConfig setBacklog(int backlog) {
157         checkPositiveOrZero(backlog, "backlog");
158         this.backlog = backlog;
159         return this;
160     }
161 
162     @Override
163     public IoUringServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
164         super.setConnectTimeoutMillis(connectTimeoutMillis);
165         return this;
166     }
167 
168     @Override
169     @Deprecated
170     public IoUringServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
171         super.setMaxMessagesPerRead(maxMessagesPerRead);
172         return this;
173     }
174 
175     @Override
176     public IoUringServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
177         super.setWriteSpinCount(writeSpinCount);
178         return this;
179     }
180 
181     @Override
182     public IoUringServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
183         super.setAllocator(allocator);
184         return this;
185     }
186 
187     @Override
188     public IoUringServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
189         super.setRecvByteBufAllocator(allocator);
190         return this;
191     }
192 
193     @Override
194     public IoUringServerSocketChannelConfig setAutoRead(boolean autoRead) {
195         super.setAutoRead(autoRead);
196         return this;
197     }
198 
199     @Override
200     @Deprecated
201     public IoUringServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
202         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
203         return this;
204     }
205 
206     @Override
207     @Deprecated
208     public IoUringServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
209         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
210         return this;
211     }
212 
213     @Override
214     public IoUringServerSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
215         super.setWriteBufferWaterMark(writeBufferWaterMark);
216         return this;
217     }
218 
219     @Override
220     public IoUringServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
221         super.setMessageSizeEstimator(estimator);
222         return this;
223     }
224 
225     /**
226      * Returns {@code true} if the SO_REUSEPORT option is set.
227      */
228     public boolean isReusePort() {
229         try {
230             return ((IoUringServerSocketChannel) channel).socket.isReusePort();
231         } catch (IOException e) {
232             throw new ChannelException(e);
233         }
234     }
235 
236     /**
237      * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple
238      * {@link io.netty.channel.socket.ServerSocketChannel}s to the same port and so accept connections with multiple
239      * threads.
240      *
241      * Be aware this method needs be called before
242      * {@link io.netty.channel.socket.ServerSocketChannel#bind(java.net.SocketAddress)} to have any affect.
243      */
244     public IoUringServerSocketChannelConfig setReusePort(boolean reusePort) {
245         try {
246             ((IoUringServerSocketChannel) channel).socket.setReusePort(reusePort);
247             return this;
248         } catch (IOException e) {
249             throw new ChannelException(e);
250         }
251     }
252 
253     /**
254      * Returns {@code true} if <a href="https://man7.org/linux/man-pages/man7/ip.7.html">IP_FREEBIND</a> is enabled,
255      * {@code false} otherwise.
256      */
257     public boolean isFreeBind() {
258         try {
259             return ((IoUringServerSocketChannel) channel).socket.isIpFreeBind();
260         } catch (IOException e) {
261             throw new ChannelException(e);
262         }
263     }
264 
265     /**
266      * If {@code true} is used <a href="https://man7.org/linux/man-pages/man7/ip.7.html">IP_FREEBIND</a> is enabled,
267      * {@code false} for disable it. Default is disabled.
268      */
269     public IoUringServerSocketChannelConfig setFreeBind(boolean freeBind) {
270         try {
271             ((IoUringServerSocketChannel) channel).socket.setIpFreeBind(freeBind);
272             return this;
273         } catch (IOException e) {
274             throw new ChannelException(e);
275         }
276     }
277 
278     /**
279      * Returns {@code true} if <a href="https://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
280      * {@code false} otherwise.
281      */
282     public boolean isIpTransparent() {
283         try {
284             return ((IoUringServerSocketChannel) channel).socket.isIpTransparent();
285         } catch (IOException e) {
286             throw new ChannelException(e);
287         }
288     }
289 
290     /**
291      * If {@code true} is used <a href="https://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
292      * {@code false} for disable it. Default is disabled.
293      */
294     public IoUringServerSocketChannelConfig setIpTransparent(boolean transparent) {
295         try {
296             ((IoUringServerSocketChannel) channel).socket.setIpTransparent(transparent);
297             return this;
298         } catch (IOException e) {
299             throw new ChannelException(e);
300         }
301     }
302 
303     /**
304      * Set the {@code TCP_DEFER_ACCEPT} option on the socket. See {@code man 7 tcp} for more details.
305      */
306     public IoUringServerSocketChannelConfig setTcpDeferAccept(int deferAccept) {
307         try {
308             ((IoUringServerSocketChannel) channel).socket.setTcpDeferAccept(deferAccept);
309             return this;
310         } catch (IOException e) {
311             throw new ChannelException(e);
312         }
313     }
314 
315     /**
316      * Returns a positive value if <a href="https://linux.die.net/man/7/tcp">TCP_DEFER_ACCEPT</a> is enabled.
317      */
318     public int getTcpDeferAccept() {
319         try {
320             return ((IoUringServerSocketChannel) channel).socket.getTcpDeferAccept();
321         } catch (IOException e) {
322             throw new ChannelException(e);
323         }
324     }
325 
326     /**
327      * Returns threshold value of number of pending for fast open connect.
328      *
329      * @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
330      */
331     public int getTcpFastopen() {
332         return pendingFastOpenRequestsThreshold;
333     }
334 
335     /**
336      * Enables tcpFastOpen on the server channel. If the underlying os doesn't support TCP_FASTOPEN setting this has no
337      * effect. This has to be set before doing listen on the socket otherwise this takes no effect.
338      *
339      * @param pendingFastOpenRequestsThreshold number of requests to be pending for fastopen at a given point in time
340      * for security.
341      *
342      * @see <a href="https://tools.ietf.org/html/rfc7413#appendix-A.2">RFC 7413 Passive Open</a>
343      */
344     public IoUringServerSocketChannelConfig setTcpFastopen(int pendingFastOpenRequestsThreshold) {
345         this.pendingFastOpenRequestsThreshold = checkPositiveOrZero(pendingFastOpenRequestsThreshold,
346                 "pendingFastOpenRequestsThreshold");
347         return this;
348     }
349 }