2 * Copyright (c) 2014 Contextream, 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
9 package org.opendaylight.lispflowmapping.southbound;
11 import static io.netty.buffer.Unpooled.wrappedBuffer;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.bootstrap.Bootstrap;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.buffer.PooledByteBufAllocator;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.epoll.Epoll;
24 import io.netty.channel.epoll.EpollChannelOption;
25 import io.netty.channel.epoll.EpollDatagramChannel;
26 import io.netty.channel.epoll.EpollEventLoopGroup;
27 import io.netty.channel.nio.NioEventLoopGroup;
28 import io.netty.channel.socket.DatagramPacket;
29 import io.netty.channel.socket.nio.NioDatagramChannel;
30 import io.netty.util.concurrent.DefaultThreadFactory;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.UnknownHostException;
34 import java.nio.ByteBuffer;
35 import java.util.List;
36 import java.util.concurrent.ThreadFactory;
37 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
38 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
39 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
40 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
41 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
42 import org.opendaylight.lispflowmapping.southbound.lisp.AuthenticationKeyDataListener;
43 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
44 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
45 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
46 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
47 import org.opendaylight.mdsal.binding.api.DataBroker;
48 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
49 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
50 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
51 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
58 import org.opendaylight.yangtools.yang.binding.Notification;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, ClusterSingletonService {
63 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
64 public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
65 public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
66 LISPFLOWMAPPING_ENTITY_NAME);
68 private volatile boolean isMaster = false;
69 private volatile String bindingAddress;
70 private AuthKeyDb akdb;
71 private final MapRegisterCache mapRegisterCache = new MapRegisterCache();
72 private boolean mapRegisterCacheEnabled;
73 private long mapRegisterCacheTimeout;
75 private static Object startLock = new Object();
76 private final ClusterSingletonServiceProvider clusterSingletonService;
77 private LispSouthboundHandler lispSouthboundHandler;
78 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
79 private final NotificationPublishService notificationPublishService;
80 private int numChannels = 1;
81 private final Channel[] channel;
82 private Channel xtrChannel;
83 private Class channelType;
84 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
85 private volatile boolean listenOnXtrPort = false;
86 private final ConcurrentLispSouthboundStats statistics = new ConcurrentLispSouthboundStats();
87 private final Bootstrap bootstrap = new Bootstrap();
88 private final Bootstrap xtrBootstrap = new Bootstrap();
89 private final ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
90 private EventLoopGroup eventLoopGroup;
91 private final DataBroker dataBroker;
92 private AuthenticationKeyDataListener authenticationKeyDataListener;
93 private DataStoreBackEnd dsbe;
95 public LispSouthboundPlugin(final DataBroker dataBroker,
96 final NotificationPublishService notificationPublishService,
97 final ClusterSingletonServiceProvider clusterSingletonService) {
98 this.dataBroker = dataBroker;
99 this.notificationPublishService = notificationPublishService;
100 this.clusterSingletonService = clusterSingletonService;
101 if (Epoll.isAvailable()) {
102 // When lispflowmapping is under heavy load, there are usually two threads nearing 100% CPU core
103 // utilization. In order to have some headroom, we reserve 3 cores for "other" tasks, and allow the
104 // rest to be used for southbound packet processing, which is the most CPU intensive work done in lfm
105 numChannels = Math.max(1, Runtime.getRuntime().availableProcessors() - 3);
107 channel = new Channel[numChannels];
111 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
112 synchronized (startLock) {
113 this.akdb = new AuthKeyDb(new HashMapDb());
114 this.authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, akdb);
115 this.dsbe = new DataStoreBackEnd(dataBroker);
116 restoreDaoFromDatastore();
118 LispSouthboundHandler lsbh = new LispSouthboundHandler(this);
119 this.lispSouthboundHandler = lsbh;
121 LispXtrSouthboundHandler lxsbh = new LispXtrSouthboundHandler(this);
122 this.lispXtrSouthboundHandler = lxsbh;
124 if (Epoll.isAvailable()) {
125 eventLoopGroup = new EpollEventLoopGroup(numChannels, threadFactory);
126 channelType = EpollDatagramChannel.class;
127 bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
128 bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
129 LOG.debug("Using Netty Epoll for UDP sockets");
131 eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
132 channelType = NioDatagramChannel.class;
133 LOG.debug("Using Netty I/O (non-Epoll) for UDP sockets");
136 bootstrap.group(eventLoopGroup);
137 bootstrap.channel(channelType);
138 bootstrap.handler(lsbh);
140 xtrBootstrap.group(eventLoopGroup);
141 xtrBootstrap.channel(channelType);
142 xtrBootstrap.handler(lxsbh);
147 clusterSingletonService.registerClusterSingletonService(this);
148 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
152 @SuppressWarnings("checkstyle:IllegalCatch")
153 private void start() {
155 for (int i = 0; i < numChannels; ++i) {
156 channel[i] = bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
158 LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
159 } catch (Exception e) {
160 LOG.error("Failed to open main socket ", e);
164 @SuppressWarnings("checkstyle:IllegalCatch")
165 private void startXtr() {
166 if (listenOnXtrPort) {
168 xtrChannel = xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
169 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
170 } catch (Exception e) {
171 LOG.error("Failed to open xTR socket ", e);
176 @SuppressWarnings("checkstyle:IllegalCatch")
177 private void stop() {
179 for (int i = 0; i < numChannels; ++i) {
180 channel[i].close().sync();
183 } catch (Exception e) {
184 LOG.error("Failed to close main socket ", e);
188 @SuppressWarnings("checkstyle:IllegalCatch")
189 private void stopXtr() {
190 if (listenOnXtrPort) {
192 xtrChannel.close().sync();
194 } catch (Exception e) {
195 LOG.error("Failed to close xTR socket ", e);
200 private void restart() {
201 LOG.info("Reloading");
206 private void restartXtr() {
207 LOG.info("Reloading xTR");
212 private void unloadActions() {
213 lispSouthboundHandler = null;
214 lispXtrSouthboundHandler = null;
219 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
223 * Restore all keys from MDSAL datastore.
225 public void restoreDaoFromDatastore() {
226 final List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
227 LOG.info("Restoring {} keys from datastore into southbound DAO", authKeys.size());
229 for (AuthenticationKey authKey : authKeys) {
230 final Eid key = authKey.getEid();
231 final MappingAuthkey mappingAuthkey = authKey.getMappingAuthkey();
232 LOG.debug("Adding authentication key '{}' with key-ID {} for {}", mappingAuthkey.getKeyString(),
233 mappingAuthkey.getKeyType(),
234 LispAddressStringifier.getString(key));
235 akdb.addAuthenticationKey(key, mappingAuthkey);
239 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
240 final MessageType packetType) {
241 InetAddress ip = getInetAddress(address);
242 handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue(), null);
245 public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
246 final MessageType packetType, final int portNumber, Channel senderChannel) {
247 if (senderChannel == null) {
248 senderChannel = this.channel[0];
250 InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
251 outBuffer.position(0);
252 ByteBuf data = wrappedBuffer(outBuffer);
253 DatagramPacket packet = new DatagramPacket(data, recipient);
254 LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
255 if (LOG.isTraceEnabled()) {
256 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
258 senderChannel.write(packet).addListener(future -> {
259 if (future.isSuccess()) {
260 LOG.trace("Success");
261 statistics.incrementTx(packetType.getIntValue());
263 LOG.warn("Failed to send packet");
264 statistics.incrementTxErrors();
267 senderChannel.flush();
270 private InetAddress getInetAddress(TransportAddress address) {
271 Preconditions.checkNotNull(address, "TransportAddress must not be null");
272 IpAddressBinary ip = address.getIpAddress();
274 if (ip.getIpv4AddressBinary() != null) {
275 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
276 } else if (ip.getIpv6AddressBinary() != null) {
277 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
279 } catch (UnknownHostException e) {
280 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
286 @SuppressWarnings("checkstyle:IllegalCatch")
287 public void setLispAddress(String address) {
288 synchronized (startLock) {
289 if (bindingAddress.equals(address)) {
290 LOG.debug("Configured LISP binding address didn't change.");
292 LOG.debug("Setting LISP binding address to {}", address);
293 bindingAddress = address;
294 if (channel != null) {
298 } catch (Exception e) {
299 LOG.error("Failed to set LISP binding address: ", e);
307 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
308 listenOnXtrPort = shouldListenOnXtrPort;
309 if (listenOnXtrPort) {
312 LOG.info("Shutting down xTR");
318 public void setXtrPort(int port) {
320 if (listenOnXtrPort) {
325 public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
326 this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
327 if (mapRegisterCacheEnabled) {
328 LOG.info("Enabling Map-Register cache");
330 LOG.info("Disabling Map-Register cache");
334 public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
335 this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
338 public void setBindingAddress(String bindingAddress) {
339 this.bindingAddress = bindingAddress;
343 public void close() throws Exception {
344 eventLoopGroup.shutdownGracefully();
345 lispSouthboundHandler.close();
347 clusterSingletonService.close();
348 dsbe.closeTransactionChain();
352 public void instantiateServiceInstance() {
353 this.isMaster = true;
357 public ListenableFuture<Void> closeServiceInstance() {
358 this.isMaster = false;
359 return Futures.<Void>immediateFuture(null);
363 public ServiceGroupIdentifier getIdentifier() {
364 return SERVICE_GROUP_IDENTIFIER;
367 public synchronized void sendNotificationIfPossible(final Notification notification) throws InterruptedException {
368 if (isMaster && notificationPublishService != null) {
369 notificationPublishService.putNotification(notification);
370 LOG.trace("Publishing notification: {}", notification);
371 } else if (notificationPublishService == null) {
372 LOG.warn("Can't publish notification because no reference to publication service exists!");
376 public AuthKeyDb getAkdb() {
380 public ConcurrentLispSouthboundStats getStats() {
384 public DataBroker getDataBroker() {
388 public AuthenticationKeyDataListener getAuthenticationKeyDataListener() {
389 return authenticationKeyDataListener;
392 public MapRegisterCache getMapRegisterCache() {
393 return mapRegisterCache;
396 public boolean isMapRegisterCacheEnabled() {
397 return mapRegisterCacheEnabled;
400 public long getMapRegisterCacheTimeout() {
401 return mapRegisterCacheTimeout;