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