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.Channel;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.socket.ServerSocketChannel;
21  import io.netty.channel.socket.ServerSocketChannelConfig;
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  
26  public final class IoUringServerSocketChannel extends AbstractIoUringServerChannel implements ServerSocketChannel {
27      private final IoUringServerSocketChannelConfig config;
28  
29      public IoUringServerSocketChannel() {
30          // We don't use a blocking fd for the server channel at the moment as
31          // there is no support for IORING_CQE_F_SOCK_NONEMPTY and IORING_ACCEPT_DONTWAIT
32          // at the moment. Once these land in the kernel we should check if we can use these and if so make
33          // the fd blocking to get rid of POLLIN etc.
34          // See:
35          //
36          //  - https://lore.kernel.org/netdev/[email protected]/
37          //  - https://lore.kernel.org/io-uring/[email protected]/
38          super(LinuxSocket.newSocketStream(), false);
39          this.config = new IoUringServerSocketChannelConfig(this);
40      }
41  
42      @Override
43      public ServerSocketChannelConfig config() {
44          return config;
45      }
46  
47      @Override
48      Channel newChildChannel(int fd, long acceptedAddressMemoryAddress, long acceptedAddressLengthMemoryAddress) {
49          final InetSocketAddress address;
50          IoUringIoHandler handler = (IoUringIoHandler) registration().ioHandler();
51          if (socket.isIpv6()) {
52              byte[] ipv6Array = handler.inet6AddressArray();
53              byte[] ipv4Array = handler.inet4AddressArray();
54              address = SockaddrIn.readIPv6(acceptedAddressMemoryAddress, ipv6Array, ipv4Array);
55          } else {
56              byte[] addressArray = handler.inet4AddressArray();
57              address = SockaddrIn.readIPv4(acceptedAddressMemoryAddress, addressArray);
58          }
59          return new IoUringSocketChannel(this, new LinuxSocket(fd), address);
60      }
61  
62      @Override
63      public InetSocketAddress remoteAddress() {
64          return (InetSocketAddress) super.remoteAddress();
65      }
66  
67      @Override
68      public InetSocketAddress localAddress() {
69          return (InetSocketAddress) super.localAddress();
70      }
71  
72      @Override
73      public void doBind(SocketAddress localAddress) throws Exception {
74          super.doBind(localAddress);
75          if (IoUring.isTcpFastOpenServerSideAvailable()) {
76              Integer fastOpen = config().getOption(ChannelOption.TCP_FASTOPEN);
77              if (fastOpen != null && fastOpen > 0) {
78                  socket.setTcpFastOpen(fastOpen);
79              }
80          }
81          socket.listen(config.getBacklog());
82          active = true;
83      }
84  }