2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.bootstrap.ServerBootstrap;
15 import io.netty.buffer.PooledByteBufAllocator;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandler;
18 import io.netty.channel.ChannelInitializer;
19 import io.netty.channel.ChannelOption;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.FixedRecvByteBufAllocator;
22 import io.netty.channel.WriteBufferWaterMark;
23 import io.netty.channel.epoll.Epoll;
24 import io.netty.channel.epoll.EpollChannelOption;
25 import io.netty.channel.epoll.EpollEventLoopGroup;
26 import io.netty.channel.epoll.EpollMode;
27 import io.netty.channel.epoll.EpollServerSocketChannel;
28 import io.netty.channel.epoll.EpollSocketChannel;
29 import io.netty.channel.socket.SocketChannel;
30 import io.netty.channel.socket.nio.NioServerSocketChannel;
31 import io.netty.channel.socket.nio.NioSocketChannel;
32 import io.netty.util.concurrent.DefaultPromise;
33 import io.netty.util.concurrent.Future;
34 import io.netty.util.concurrent.GlobalEventExecutor;
35 import io.netty.util.concurrent.Promise;
36 import java.net.InetSocketAddress;
37 import java.util.concurrent.TimeUnit;
38 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
39 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPProtocolSessionPromise;
40 import org.opendaylight.protocol.bgp.rib.impl.protocol.BGPReconnectPromise;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.ChannelPipelineInitializer;
44 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
45 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionNegotiatorFactory;
46 import org.opendaylight.protocol.concepts.KeyMapping;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Implementation of BGPDispatcher.
53 public class BGPDispatcherImpl implements BGPDispatcher, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(BGPDispatcherImpl.class);
55 private static final int SOCKET_BACKLOG_SIZE = 128;
56 private static final int FIX_BUFFER_SIZE = 1;
57 private static final long TIMEOUT = 10;
59 private static final WriteBufferWaterMark WATER_MARK = new WriteBufferWaterMark(128 * 1024, 256 * 1024);
61 private final BGPHandlerFactory handlerFactory;
62 private final EventLoopGroup bossGroup;
63 private final EventLoopGroup workerGroup;
64 private final BGPPeerRegistry bgpPeerRegistry;
66 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup,
67 final EventLoopGroup workerGroup, final BGPPeerRegistry bgpPeerRegistry) {
68 if (Epoll.isAvailable()) {
69 this.bossGroup = new EpollEventLoopGroup();
70 this.workerGroup = new EpollEventLoopGroup();
72 this.bossGroup = requireNonNull(bossGroup);
73 this.workerGroup = requireNonNull(workerGroup);
75 this.bgpPeerRegistry = requireNonNull(bgpPeerRegistry);
76 this.handlerFactory = new BGPHandlerFactory(messageRegistry);
80 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress localAddress,
81 final InetSocketAddress remoteAddress, final int retryTimer, final boolean reuseAddress) {
82 final Bootstrap clientBootStrap = createClientBootStrap(KeyMapping.getKeyMapping(), reuseAddress, localAddress);
83 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
84 final ChannelPipelineInitializer<BGPSessionImpl> initializer = BGPChannel.createChannelPipelineInitializer(
85 this.handlerFactory, snf);
87 final BGPProtocolSessionPromise<BGPSessionImpl> sessionPromise = new BGPProtocolSessionPromise<>(remoteAddress,
88 retryTimer, clientBootStrap, this.bgpPeerRegistry);
89 clientBootStrap.handler(BGPChannel.createClientChannelHandler(initializer, sessionPromise));
90 sessionPromise.connect();
91 LOG.debug("Client created.");
92 return sessionPromise;
95 private synchronized Bootstrap createClientBootStrap(final KeyMapping keys, final boolean reuseAddress,
96 final InetSocketAddress localAddress) {
97 final Bootstrap bootstrap = new Bootstrap();
98 if (Epoll.isAvailable()) {
99 bootstrap.channel(EpollSocketChannel.class);
100 bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
102 bootstrap.channel(NioSocketChannel.class);
104 if (keys != null && !keys.isEmpty()) {
105 if (Epoll.isAvailable()) {
106 bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys);
108 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
112 // Make sure we are doing round-robin processing
113 bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
114 bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
115 bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
116 bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
118 if (bootstrap.config().group() == null) {
119 bootstrap.group(this.workerGroup);
121 bootstrap.localAddress(localAddress);
127 public synchronized void close() {
128 if (Epoll.isAvailable()) {
129 LOG.debug("Closing Dispatcher");
130 this.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
131 this.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
136 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
137 final InetSocketAddress localAddress, final int retryTimer, final KeyMapping keys) {
138 return createReconnectingClient(remoteAddress, retryTimer, keys, localAddress, false);
142 synchronized Future<Void> createReconnectingClient(final InetSocketAddress remoteAddress,
143 final int retryTimer, final KeyMapping keys, final InetSocketAddress localAddress,
144 final boolean reuseAddress) {
145 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.bgpPeerRegistry);
146 final Bootstrap bootstrap = createClientBootStrap(keys, reuseAddress, localAddress);
147 final BGPReconnectPromise<?> reconnectPromise = new BGPReconnectPromise<>(GlobalEventExecutor.INSTANCE,
148 remoteAddress, retryTimer, bootstrap, this.bgpPeerRegistry,
149 BGPChannel.createChannelPipelineInitializer(this.handlerFactory, snf));
150 reconnectPromise.connect();
151 return reconnectPromise;
155 public synchronized ChannelFuture createServer(final InetSocketAddress serverAddress) {
156 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.bgpPeerRegistry);
157 final ChannelPipelineInitializer<?> initializer = BGPChannel.
158 createChannelPipelineInitializer(this.handlerFactory, snf);
159 final ServerBootstrap serverBootstrap = createServerBootstrap(initializer);
160 final ChannelFuture channelFuture = serverBootstrap.bind(serverAddress);
161 LOG.debug("Initiated server {} at {}.", channelFuture, serverAddress);
162 return channelFuture;
166 public BGPPeerRegistry getBGPPeerRegistry() {
167 return this.bgpPeerRegistry;
170 private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
171 final ServerBootstrap serverBootstrap = new ServerBootstrap();
172 if (Epoll.isAvailable()) {
173 serverBootstrap.channel(EpollServerSocketChannel.class);
174 serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
176 serverBootstrap.channel(NioServerSocketChannel.class);
178 final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
179 serverBootstrap.childHandler(serverChannelHandler);
181 serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
182 serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
183 serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
185 // Make sure we are doing round-robin processing
186 serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(FIX_BUFFER_SIZE));
188 if (serverBootstrap.config().group() == null) {
189 serverBootstrap.group(this.bossGroup, this.workerGroup);
191 return serverBootstrap;
194 private static final class BGPChannel {
195 private static final String NEGOTIATOR = "negotiator";
197 private BGPChannel() {
201 static <S extends BGPSession, T extends BGPSessionNegotiatorFactory<S>> ChannelPipelineInitializer<S>
202 createChannelPipelineInitializer(final BGPHandlerFactory hf, final T snf) {
203 return (channel, promise) -> {
204 channel.pipeline().addLast(hf.getDecoders());
205 channel.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(channel, promise));
206 channel.pipeline().addLast(hf.getEncoders());
210 static <S extends BGPSession> ChannelHandler createClientChannelHandler(
211 final ChannelPipelineInitializer<S> initializer, final Promise<S> promise) {
212 return new ChannelInitializer<SocketChannel>() {
214 protected void initChannel(final SocketChannel channel) {
215 initializer.initializeChannel(channel, promise);
220 static <S extends BGPSession> ChannelHandler createServerChannelHandler(
221 final ChannelPipelineInitializer<S> initializer) {
222 return new ChannelInitializer<SocketChannel>() {
224 protected void initChannel(final SocketChannel channel) {
225 initializer.initializeChannel(channel, new DefaultPromise<>(GlobalEventExecutor.INSTANCE));