2014-03-17 02:35:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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.epoll;
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
2014-04-11 21:08:26 +02:00
|
|
|
import io.netty.channel.Channel;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.channel.ChannelFactory;
|
2019-01-14 20:11:13 +01:00
|
|
|
import io.netty.channel.EventLoop;
|
2014-03-17 02:35:48 +01:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
2019-01-23 08:32:05 +01:00
|
|
|
import io.netty.channel.MultithreadEventLoopGroup;
|
2014-04-11 21:08:26 +02:00
|
|
|
import io.netty.channel.socket.InternetProtocolFamily;
|
|
|
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
2014-03-17 02:35:48 +01:00
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
2015-02-12 20:44:36 +01:00
|
|
|
import io.netty.channel.unix.DomainSocketAddress;
|
2017-01-19 17:31:34 +01:00
|
|
|
import io.netty.channel.unix.tests.UnixTestUtils;
|
2014-03-17 02:35:48 +01:00
|
|
|
import io.netty.testsuite.transport.TestsuitePermutation;
|
|
|
|
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
|
|
|
|
import io.netty.testsuite.transport.socket.SocketTestPermutation;
|
|
|
|
import io.netty.util.concurrent.DefaultThreadFactory;
|
2015-09-05 00:03:10 +02:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2014-03-17 02:35:48 +01:00
|
|
|
|
2015-09-05 00:03:10 +02:00
|
|
|
import java.io.BufferedReader;
|
2015-01-14 16:38:46 +01:00
|
|
|
import java.io.File;
|
2015-09-05 00:03:10 +02:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.security.AccessController;
|
|
|
|
import java.security.PrivilegedAction;
|
|
|
|
import java.util.ArrayList;
|
2014-03-17 02:35:48 +01:00
|
|
|
import java.util.Arrays;
|
2015-01-14 16:38:46 +01:00
|
|
|
import java.util.Collections;
|
2014-03-17 02:35:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
class EpollSocketTestPermutation extends SocketTestPermutation {
|
|
|
|
|
2015-01-14 16:38:46 +01:00
|
|
|
static final EpollSocketTestPermutation INSTANCE = new EpollSocketTestPermutation();
|
2014-03-17 02:35:48 +01:00
|
|
|
|
2014-04-11 15:54:31 +02:00
|
|
|
static final EventLoopGroup EPOLL_BOSS_GROUP =
|
2019-01-23 08:32:05 +01:00
|
|
|
new MultithreadEventLoopGroup(BOSSES, new DefaultThreadFactory("testsuite-epoll-boss", true),
|
|
|
|
EpollHandler.newFactory());
|
2014-04-11 15:54:31 +02:00
|
|
|
static final EventLoopGroup EPOLL_WORKER_GROUP =
|
2019-01-23 08:32:05 +01:00
|
|
|
new MultithreadEventLoopGroup(WORKERS, new DefaultThreadFactory("testsuite-epoll-worker", true),
|
|
|
|
EpollHandler.newFactory());
|
2014-03-17 02:35:48 +01:00
|
|
|
|
2015-09-05 00:03:10 +02:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(EpollSocketTestPermutation.class);
|
|
|
|
|
2014-03-17 02:35:48 +01:00
|
|
|
@Override
|
|
|
|
public List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
|
|
|
|
|
|
|
|
List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
|
|
|
|
combo(serverSocket(), clientSocket());
|
|
|
|
|
|
|
|
list.remove(list.size() - 1); // Exclude NIO x NIO test
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2015-05-28 03:37:56 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-03-17 02:35:48 +01:00
|
|
|
@Override
|
|
|
|
public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
|
2019-01-22 16:07:26 +01:00
|
|
|
List<BootstrapFactory<ServerBootstrap>> toReturn = new ArrayList<>();
|
2019-01-29 14:06:05 +01:00
|
|
|
toReturn.add(() -> new ServerBootstrap().group(EPOLL_BOSS_GROUP, EPOLL_WORKER_GROUP)
|
|
|
|
.channel(EpollServerSocketChannel.class));
|
2015-09-05 00:03:10 +02:00
|
|
|
if (isServerFastOpen()) {
|
2019-01-29 14:06:05 +01:00
|
|
|
toReturn.add(() -> {
|
|
|
|
ServerBootstrap serverBootstrap = new ServerBootstrap().group(EPOLL_BOSS_GROUP, EPOLL_WORKER_GROUP)
|
|
|
|
.channel(EpollServerSocketChannel.class);
|
|
|
|
serverBootstrap.option(EpollChannelOption.TCP_FASTOPEN, 5);
|
|
|
|
return serverBootstrap;
|
2015-09-05 00:03:10 +02:00
|
|
|
});
|
|
|
|
}
|
2019-01-29 14:06:05 +01:00
|
|
|
toReturn.add(() -> new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
|
|
|
|
.channel(NioServerSocketChannel.class));
|
2015-09-05 00:03:10 +02:00
|
|
|
|
|
|
|
return toReturn;
|
2014-03-17 02:35:48 +01:00
|
|
|
}
|
|
|
|
|
2015-05-28 03:37:56 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-03-17 02:35:48 +01:00
|
|
|
@Override
|
|
|
|
public List<BootstrapFactory<Bootstrap>> clientSocket() {
|
|
|
|
return Arrays.asList(
|
2019-01-29 14:06:05 +01:00
|
|
|
() -> new Bootstrap().group(EPOLL_WORKER_GROUP).channel(EpollSocketChannel.class),
|
|
|
|
() -> new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class)
|
2014-03-17 02:35:48 +01:00
|
|
|
);
|
|
|
|
}
|
2014-04-11 21:08:26 +02:00
|
|
|
|
|
|
|
@Override
|
2019-04-12 12:29:08 +02:00
|
|
|
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(
|
|
|
|
final InternetProtocolFamily family) {
|
2014-04-11 21:08:26 +02:00
|
|
|
// Make the list of Bootstrap factories.
|
2015-05-28 03:37:56 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-04-11 21:08:26 +02:00
|
|
|
List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
|
2019-01-29 14:06:05 +01:00
|
|
|
() -> new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
|
2014-04-11 21:08:26 +02:00
|
|
|
@Override
|
2019-01-29 14:06:05 +01:00
|
|
|
public Channel newChannel(EventLoop eventLoop) {
|
2019-04-12 12:29:08 +02:00
|
|
|
return new NioDatagramChannel(eventLoop, family);
|
2014-04-11 21:08:26 +02:00
|
|
|
}
|
2019-01-29 14:06:05 +01:00
|
|
|
|
2014-04-11 21:08:26 +02:00
|
|
|
@Override
|
2019-01-29 14:06:05 +01:00
|
|
|
public String toString() {
|
|
|
|
return NioDatagramChannel.class.getSimpleName() + ".class";
|
2014-04-11 21:08:26 +02:00
|
|
|
}
|
2019-01-29 14:06:05 +01:00
|
|
|
}),
|
2019-04-08 20:13:39 +02:00
|
|
|
() -> new Bootstrap().group(EPOLL_WORKER_GROUP).channelFactory(new ChannelFactory<Channel>() {
|
|
|
|
@Override
|
|
|
|
public Channel newChannel(EventLoop eventLoop) {
|
|
|
|
return new EpollDatagramChannel(eventLoop, family);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return InternetProtocolFamily.class.getSimpleName() + ".class";
|
|
|
|
}
|
|
|
|
})
|
2014-04-11 21:08:26 +02:00
|
|
|
);
|
|
|
|
return combo(bfs, bfs);
|
|
|
|
}
|
2015-01-14 16:38:46 +01:00
|
|
|
|
2019-09-03 08:40:17 +02:00
|
|
|
List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> epollOnlyDatagram(
|
|
|
|
final InternetProtocolFamily family) {
|
|
|
|
return combo(Collections.singletonList(datagramBootstrapFactory(family)),
|
|
|
|
Collections.singletonList(datagramBootstrapFactory(family)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private BootstrapFactory<Bootstrap> datagramBootstrapFactory(final InternetProtocolFamily family) {
|
|
|
|
return () -> new Bootstrap().group(EPOLL_WORKER_GROUP).channelFactory(new ChannelFactory<Channel>() {
|
|
|
|
@Override
|
|
|
|
public Channel newChannel(EventLoop eventLoop) {
|
|
|
|
return new EpollDatagramChannel(eventLoop, family);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return InternetProtocolFamily.class.getSimpleName() + ".class";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:38:46 +01:00
|
|
|
public List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> domainSocket() {
|
|
|
|
|
|
|
|
List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
|
|
|
|
combo(serverDomainSocket(), clientDomainSocket());
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BootstrapFactory<ServerBootstrap>> serverDomainSocket() {
|
2019-01-28 05:55:30 +01:00
|
|
|
return Collections.singletonList(
|
2019-01-29 14:06:05 +01:00
|
|
|
() -> new ServerBootstrap().group(EPOLL_BOSS_GROUP, EPOLL_WORKER_GROUP)
|
|
|
|
.channel(EpollServerDomainSocketChannel.class)
|
2015-01-14 16:38:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BootstrapFactory<Bootstrap>> clientDomainSocket() {
|
2019-01-28 05:55:30 +01:00
|
|
|
return Collections.singletonList(
|
2019-01-29 14:06:05 +01:00
|
|
|
() -> new Bootstrap().group(EPOLL_WORKER_GROUP).channel(EpollDomainSocketChannel.class)
|
2015-01-14 16:38:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-09 14:15:48 +01:00
|
|
|
@Override
|
|
|
|
public List<BootstrapFactory<Bootstrap>> datagramSocket() {
|
2019-01-28 05:55:30 +01:00
|
|
|
return Collections.singletonList(
|
2019-01-29 14:06:05 +01:00
|
|
|
() -> new Bootstrap().group(EPOLL_WORKER_GROUP).channel(EpollDatagramChannel.class)
|
2017-01-09 14:15:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-05 00:03:10 +02:00
|
|
|
public boolean isServerFastOpen() {
|
2019-01-29 14:06:05 +01:00
|
|
|
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> {
|
|
|
|
int fastopen = 0;
|
|
|
|
File file = new File("/proc/sys/net/ipv4/tcp_fastopen");
|
|
|
|
if (file.exists()) {
|
|
|
|
BufferedReader in = null;
|
|
|
|
try {
|
|
|
|
in = new BufferedReader(new FileReader(file));
|
|
|
|
fastopen = Integer.parseInt(in.readLine());
|
2015-09-05 00:03:10 +02:00
|
|
|
if (logger.isDebugEnabled()) {
|
2019-01-29 14:06:05 +01:00
|
|
|
logger.debug("{}: {}", file, fastopen);
|
2015-09-05 00:03:10 +02:00
|
|
|
}
|
2019-01-29 14:06:05 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.debug("Failed to get TCP_FASTOPEN from: {}", file, e);
|
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Ignored.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("{}: {} (non-existent)", file, fastopen);
|
2015-09-05 00:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-29 14:06:05 +01:00
|
|
|
return fastopen;
|
2015-09-05 00:03:10 +02:00
|
|
|
}) == 3;
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:38:46 +01:00
|
|
|
public static DomainSocketAddress newSocketAddress() {
|
2017-01-19 17:31:34 +01:00
|
|
|
return UnixTestUtils.newSocketAddress();
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
2014-03-17 02:35:48 +01:00
|
|
|
}
|