Do support SO_BACKLOG in io_uring

Motivation:

Due a bug SO_BACKLOG was not supported via ChannelOption when using io_uring. Be

Modification:

- Add SO_BACKLOG to the supported ChannelOptions.
- Merge IOUringServerChannelConfig into IOUringServerSocketChannelConfig

Result:

SO_BACKLOG is supported
This commit is contained in:
Norman Maurer 2020-09-03 09:40:55 +02:00
parent 5691fe8a44
commit f57fcd6c4a
2 changed files with 79 additions and 204 deletions

View File

@ -1,160 +0,0 @@
/*
* Copyright 2020 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.uring;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
import io.netty.channel.MessageSizeEstimator;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.socket.ServerSocketChannelConfig;
import io.netty.util.NetUtil;
import java.io.IOException;
import java.util.Map;
import static io.netty.channel.ChannelOption.*;
import static io.netty.util.internal.ObjectUtil.*;
public class IOUringServerChannelConfig extends DefaultChannelConfig implements ServerSocketChannelConfig {
private volatile int backlog = NetUtil.SOMAXCONN;
IOUringServerChannelConfig(AbstractIOUringServerChannel channel) {
super(channel);
}
@Override
public boolean isReuseAddress() {
try {
return ((AbstractIOUringChannel) channel).socket.isReuseAddress();
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public IOUringServerChannelConfig setReuseAddress(boolean reuseAddress) {
try {
((AbstractIOUringChannel) channel).socket.setReuseAddress(reuseAddress);
return this;
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public int getReceiveBufferSize() {
try {
return ((AbstractIOUringChannel) channel).socket.getReceiveBufferSize();
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public IOUringServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
((AbstractIOUringChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
return this;
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public int getBacklog() {
return backlog;
}
@Override
public IOUringServerChannelConfig setBacklog(int backlog) {
checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this;
}
@Override
public IOUringServerChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
return this;
}
@Override
public IOUringServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
super.setConnectTimeoutMillis(connectTimeoutMillis);
return this;
}
@Override
@Deprecated
public IOUringServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
super.setMaxMessagesPerRead(maxMessagesPerRead);
return this;
}
@Override
public IOUringServerChannelConfig setWriteSpinCount(int writeSpinCount) {
super.setWriteSpinCount(writeSpinCount);
return this;
}
@Override
public IOUringServerChannelConfig setAllocator(ByteBufAllocator allocator) {
super.setAllocator(allocator);
return this;
}
@Override
public IOUringServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
super.setRecvByteBufAllocator(allocator);
return this;
}
@Override
public IOUringServerChannelConfig setAutoRead(boolean autoRead) {
super.setAutoRead(autoRead);
return this;
}
@Override
@Deprecated
public IOUringServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
return this;
}
@Override
@Deprecated
public IOUringServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
return this;
}
@Override
public IOUringServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
super.setWriteBufferWaterMark(writeBufferWaterMark);
return this;
}
@Override
public IOUringServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
super.setMessageSizeEstimator(estimator);
return this;
}
}

View File

@ -16,39 +16,45 @@
package io.netty.channel.uring; package io.netty.channel.uring;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException; import io.netty.channel.*;
import io.netty.channel.ChannelOption;
import io.netty.channel.MessageSizeEstimator;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.socket.ServerSocketChannelConfig; import io.netty.channel.socket.ServerSocketChannelConfig;
import io.netty.util.NetUtil;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.util.Map; import java.util.Map;
public class IOUringServerSocketChannelConfig extends IOUringServerChannelConfig implements ServerSocketChannelConfig { import static io.netty.channel.ChannelOption.SO_BACKLOG;
import static io.netty.channel.ChannelOption.SO_RCVBUF;
import static io.netty.channel.ChannelOption.SO_REUSEADDR;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
public final class IOUringServerSocketChannelConfig extends DefaultChannelConfig implements ServerSocketChannelConfig {
private volatile int backlog = NetUtil.SOMAXCONN;
IOUringServerSocketChannelConfig(AbstractIOUringServerChannel channel) { IOUringServerSocketChannelConfig(AbstractIOUringServerChannel channel) {
super(channel); super(channel);
setReuseAddress(true); setReuseAddress(true);
} }
@Override
public IOUringServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
super.setReuseAddress(reuseAddress);
return this;
}
@Override @Override
public Map<ChannelOption<?>, Object> getOptions() { public Map<ChannelOption<?>, Object> getOptions() {
return getOptions(super.getOptions(), IOUringChannelOption.SO_REUSEPORT, IOUringChannelOption.IP_FREEBIND, return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG,
IOUringChannelOption.SO_REUSEPORT, IOUringChannelOption.IP_FREEBIND,
IOUringChannelOption.IP_TRANSPARENT, IOUringChannelOption.TCP_DEFER_ACCEPT); IOUringChannelOption.IP_TRANSPARENT, IOUringChannelOption.TCP_DEFER_ACCEPT);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T getOption(ChannelOption<T> option) { public <T> T getOption(ChannelOption<T> option) {
if (option == SO_RCVBUF) {
return (T) Integer.valueOf(getReceiveBufferSize());
}
if (option == SO_REUSEADDR) {
return (T) Boolean.valueOf(isReuseAddress());
}
if (option == SO_BACKLOG) {
return (T) Integer.valueOf(getBacklog());
}
if (option == IOUringChannelOption.SO_REUSEPORT) { if (option == IOUringChannelOption.SO_REUSEPORT) {
return (T) Boolean.valueOf(isReusePort()); return (T) Boolean.valueOf(isReusePort());
} }
@ -67,8 +73,13 @@ public class IOUringServerSocketChannelConfig extends IOUringServerChannelConfig
@Override @Override
public <T> boolean setOption(ChannelOption<T> option, T value) { public <T> boolean setOption(ChannelOption<T> option, T value) {
validate(option, value); validate(option, value);
if (option == SO_RCVBUF) {
if (option == IOUringChannelOption.SO_REUSEPORT) { setReceiveBufferSize((Integer) value);
} else if (option == SO_REUSEADDR) {
setReuseAddress((Boolean) value);
} else if (option == SO_BACKLOG) {
setBacklog((Integer) value);
} else if (option == IOUringChannelOption.SO_REUSEPORT) {
setReusePort((Boolean) value); setReusePort((Boolean) value);
} else if (option == IOUringChannelOption.IP_FREEBIND) { } else if (option == IOUringChannelOption.IP_FREEBIND) {
setFreeBind((Boolean) value); setFreeBind((Boolean) value);
@ -83,20 +94,58 @@ public class IOUringServerSocketChannelConfig extends IOUringServerChannelConfig
return true; return true;
} }
@Override
public IOUringServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
super.setReceiveBufferSize(receiveBufferSize);
return this;
}
@Override @Override
public IOUringServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) { public IOUringServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
return this; return this;
} }
@Override
public boolean isReuseAddress() {
try {
return ((AbstractIOUringChannel) channel).socket.isReuseAddress();
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public IOUringServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
try {
((AbstractIOUringChannel) channel).socket.setReuseAddress(reuseAddress);
return this;
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public int getReceiveBufferSize() {
try {
return ((AbstractIOUringChannel) channel).socket.getReceiveBufferSize();
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public IOUringServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
((AbstractIOUringChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
return this;
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public int getBacklog() {
return backlog;
}
@Override @Override
public IOUringServerSocketChannelConfig setBacklog(int backlog) { public IOUringServerSocketChannelConfig setBacklog(int backlog) {
super.setBacklog(backlog); checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this; return this;
} }
@ -163,20 +212,6 @@ public class IOUringServerSocketChannelConfig extends IOUringServerChannelConfig
return this; return this;
} }
// /**
// * Set the {@code TCP_MD5SIG} option on the socket. See {@code linux/tcp.h} for more details.
// * Keys can only be set on, not read to prevent a potential leak, as they are confidential.
// * Allowing them being read would mean anyone with access to the channel could get them.
// */
// public IOUringServerSocketChannelConfig setTcpMd5Sig(Map<InetAddress, byte[]> keys) {
// try {
// ((IOUringServerSocketChannel) channel).setTcpMd5Sig(keys);
// return this;
// } catch (IOException e) {
// throw new ChannelException(e);
// }
// }
/** /**
* Returns {@code true} if the SO_REUSEPORT option is set. * Returns {@code true} if the SO_REUSEPORT option is set.
*/ */
@ -188,13 +223,13 @@ public class IOUringServerSocketChannelConfig extends IOUringServerChannelConfig
} }
} }
// /** /**
// * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple * Set the SO_REUSEPORT option on the underlying Channel. This will allow to bind multiple
// * {@link EpollSocketChannel}s to the same port and so accept connections with multiple threads. * {@link io.netty.channel.socket.ServerSocketChannel}s to the same port and so accept connections with multiple threads.
// * *
// * Be aware this method needs be called before {@link EpollSocketChannel#bind(java.net.SocketAddress)} to have * Be aware this method needs be called before
// * any affect. * {@link io.netty.channel.socket.ServerSocketChannel#bind(java.net.SocketAddress)} to have any affect.
// */ */
public IOUringServerSocketChannelConfig setReusePort(boolean reusePort) { public IOUringServerSocketChannelConfig setReusePort(boolean reusePort) {
try { try {
((IOUringServerSocketChannel) channel).socket.setReusePort(reusePort); ((IOUringServerSocketChannel) channel).socket.setReusePort(reusePort);