*/
package org.opendaylight.protocol.bgp.rib.impl;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.Epoll;
+import io.netty.channel.epoll.EpollChannelOption;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollMode;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.DefaultPromise;
-import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.concurrent.Promise;
import java.net.InetSocketAddress;
+import java.util.concurrent.TimeUnit;
import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
-import org.opendaylight.protocol.framework.ReconnectStrategy;
-import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
-import org.opendaylight.tcpmd5.api.KeyMapping;
-import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
-import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
-import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.protocol.concepts.KeyMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
private static final int SOCKET_BACKLOG_SIZE = 128;
- private final MD5ServerChannelFactory<?> scf;
- private final MD5ChannelFactory<?> cf;
- private final BGPHandlerFactory hf;
+ private static final int HIGH_WATER_MARK = 256 * 1024;
+ private static final int LOW_WATER_MARK = 128 * 1024;
+ private static final long TIMEOUT = 10;
+
+ private final BGPHandlerFactory handlerFactory;
private final EventLoopGroup bossGroup;
private final EventLoopGroup workerGroup;
- private final EventExecutor executor;
- private KeyMapping keys;
public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
- this(messageRegistry, bossGroup, workerGroup, null, null);
- }
-
- public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
- this.bossGroup = Preconditions.checkNotNull(bossGroup);
- this.workerGroup = Preconditions.checkNotNull(workerGroup);
- this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
- this.hf = new BGPHandlerFactory(messageRegistry);
- this.cf = cf;
- this.scf = scf;
- this.keys = null;
+ if (Epoll.isAvailable()) {
+ this.bossGroup = new EpollEventLoopGroup();
+ this.workerGroup = new EpollEventLoopGroup();
+ } else {
+ this.bossGroup = Preconditions.checkNotNull(bossGroup);
+ this.workerGroup = Preconditions.checkNotNull(workerGroup);
+ }
+ this.handlerFactory = new BGPHandlerFactory(messageRegistry);
}
@Override
- public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
- final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
- final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
- final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer
- (BGPDispatcherImpl.this.hf.getDecoders(), snf, BGPDispatcherImpl.this.hf.getEncoders());
-
- final Bootstrap b = new Bootstrap();
- final BGPProtocolSessionPromise p = new BGPProtocolSessionPromise(this.executor, address, strategy, b);
- b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
- b.handler(BGPChannel.createChannelInitializer(initializer, p));
- this.customizeBootstrap(b);
- setWorkerGroup(b);
- p.connect();
- LOG.debug("Client created.");
- return p;
+ public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry listener, final int retryTimer) {
+ return createClient(remoteAddress, listener, retryTimer, createClientBootStrap(Optional.absent(), false));
}
- @Override
- public void close() {
- try {
- this.workerGroup.shutdownGracefully();
- } finally {
- this.bossGroup.shutdownGracefully();
- }
- }
+ private synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry listener, final int retryTimer,
+ final Bootstrap clientBootStrap) {
+ final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(listener);
+ final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
- @Override
- public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
- final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
- final KeyMapping keys) {
- final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
- this.keys = keys;
-
- final Bootstrap b = new Bootstrap();
- final BGPReconnectPromise p = new BGPReconnectPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE, address,
- connectStrategyFactory, b, BGPChannel.createChannelPipelineInitializer(BGPDispatcherImpl.this.hf.getDecoders(), snf, BGPDispatcherImpl.this.hf.getEncoders()));
- b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
- this.customizeBootstrap(b);
- setWorkerGroup(b);
- p.connect();
-
- this.keys = null;
-
- return p;
+ final BGPProtocolSessionPromise sessionPromise = new BGPProtocolSessionPromise(remoteAddress, retryTimer, clientBootStrap, listener);
+ clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
+ sessionPromise.connect();
+ LOG.debug("Client created.");
+ return sessionPromise;
}
- @Override
- public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address) {
- final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(registry);
- final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer
- (BGPDispatcherImpl.this.hf.getDecoders(), snf, BGPDispatcherImpl.this.hf.getEncoders());
- final ServerBootstrap b = new ServerBootstrap();
- b.childHandler(BGPChannel.createChannelInitializer(initializer, new DefaultPromise(BGPDispatcherImpl.this.executor)));
- b.option(ChannelOption.SO_BACKLOG, Integer.valueOf(SOCKET_BACKLOG_SIZE));
- b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
- this.customizeBootstrap(b);
-
- final ChannelFuture f = b.bind(address);
- LOG.debug("Initiated server {} at {}.", f, address);
- return f;
+ @VisibleForTesting
+ public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
+ final BGPPeerRegistry strictBGPPeerRegistry, final int retryTimer, final boolean reuseAddress) {
+ final Bootstrap clientBootStrap = createClientBootStrap(Optional.absent(), reuseAddress);
+ clientBootStrap.localAddress(localAddress);
+ return createClient(remoteAddress, strictBGPPeerRegistry, retryTimer, clientBootStrap);
}
- protected void customizeBootstrap(final Bootstrap b) {
- if (this.keys != null && !this.keys.isEmpty()) {
- if (this.cf == null) {
- throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
+ private synchronized Bootstrap createClientBootStrap(final Optional<KeyMapping> keys, final boolean reuseAddress) {
+ final Bootstrap bootstrap = new Bootstrap();
+ if (Epoll.isAvailable()) {
+ bootstrap.channel(EpollSocketChannel.class);
+ bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
+ } else {
+ bootstrap.channel(NioSocketChannel.class);
+ }
+ if (keys.isPresent()) {
+ if (Epoll.isAvailable()) {
+ bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.get());
+ } else {
+ throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
}
- b.channelFactory(this.cf);
- b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
}
// Make sure we are doing round-robin processing
- b.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+ bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+ bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
+ bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
+ bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
+ bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
+
+ if (bootstrap.group() == null) {
+ bootstrap.group(this.workerGroup);
+ }
+
+ return bootstrap;
}
- private void customizeBootstrap(final ServerBootstrap b) {
- if (this.keys != null && !this.keys.isEmpty()) {
- if (this.scf == null) {
- throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
- }
- b.channelFactory(this.scf);
- b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
+ @Override
+ public synchronized void close() throws InterruptedException {
+ if (Epoll.isAvailable()) {
+ LOG.debug("Closing Dispatcher");
+ this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
+ this.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
}
+ }
- // Make sure we are doing round-robin processing
- b.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+ @Override
+ public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry peerRegistry,
+ final int retryTimer, final Optional<KeyMapping> keys) {
+ return createReconnectingClient(remoteAddress, peerRegistry, retryTimer, keys, null, false);
+ }
- if (b.group() == null) {
- b.group(this.bossGroup, this.workerGroup);
- }
+ @VisibleForTesting
+ protected synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress, final BGPPeerRegistry peerRegistry,
+ final int retryTimer, final Optional<KeyMapping> keys, final InetSocketAddress localAddress, final boolean reuseAddress) {
+ final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(peerRegistry);
+ final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress);
+ bootstrap.localAddress(localAddress);
+ final BGPReconnectPromise reconnectPromise = new BGPReconnectPromise(GlobalEventExecutor.INSTANCE, remoteAddress,
+ retryTimer, bootstrap, peerRegistry, BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
+ reconnectPromise.connect();
+ return reconnectPromise;
+ }
- try {
- b.channel(NioServerSocketChannel.class);
- } catch (final IllegalStateException e) {
- LOG.trace("Not overriding channelFactory on bootstrap {}", b, e);
- }
+ @Override
+ public synchronized ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress serverAddress) {
+ final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(registry);
+ final ChannelPipelineInitializer initializer = BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf);
+ final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
+ final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
+ LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
+ return channelFuture;
}
- private void setWorkerGroup(final Bootstrap b) {
- if (b.group() == null) {
- b.group(this.workerGroup);
+ private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
+ final ServerBootstrap serverBootstrap = new ServerBootstrap();
+ if (Epoll.isAvailable()) {
+ serverBootstrap.channel(EpollServerSocketChannel.class);
+ serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
+ } else {
+ serverBootstrap.channel(NioServerSocketChannel.class);
}
- try {
- b.channel(NioSocketChannel.class);
- } catch (final IllegalStateException e) {
- LOG.trace("Not overriding channelFactory on bootstrap {}", b, e);
+ final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
+ serverBootstrap.childHandler(serverChannelHandler);
+
+ serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
+ serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
+ serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
+ serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);
+
+ // Make sure we are doing round-robin processing
+ serverBootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+
+ if (serverBootstrap.group() == null) {
+ serverBootstrap.group(this.bossGroup, this.workerGroup);
}
+ return serverBootstrap;
}
private static final class BGPChannel {
}
- public static <S extends BGPSession, T extends BGPSessionNegotiatorFactory> ChannelPipelineInitializer
- createChannelPipelineInitializer(final ChannelHandler[] channelDecoder, final T snf, final ChannelHandler[] channelEncoder) {
- return new ChannelPipelineInitializer<S>() {
+ static <T extends BGPSessionNegotiatorFactory> ChannelPipelineInitializer
+ createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
+ return (channel, promise) -> {
+ channel.pipeline().addLast(hf.getDecoders());
+ channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
+ channel.pipeline().addLast(hf.getEncoders());
+ };
+ }
+
+ static <S extends BGPSession> ChannelHandler createClientChannelHandler(final ChannelPipelineInitializer initializer, final Promise<S> promise) {
+ return new ChannelInitializer<SocketChannel>() {
@Override
- public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
- ch.pipeline().addLast(channelDecoder);
- ch.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(ch, promise));
- ch.pipeline().addLast(channelEncoder);
+ protected void initChannel(final SocketChannel channel) {
+ initializer.initializeChannel(channel, promise);
}
};
}
- public static <S extends BGPSession> ChannelHandler createChannelInitializer(final ChannelPipelineInitializer initializer, final Promise<S> promise) {
+ static ChannelHandler createServerChannelHandler(final ChannelPipelineInitializer initializer) {
return new ChannelInitializer<SocketChannel>() {
@Override
- protected void initChannel(final SocketChannel ch) {
- initializer.initializeChannel(ch, promise);
+ protected void initChannel(final SocketChannel channel) {
+ initializer.initializeChannel(channel, new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
}
};
}