BUG-6038: Fix race condition when Open message...
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.Lists;
16 import com.google.common.collect.Sets;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelFutureListener;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.ChannelPipeline;
22 import io.netty.channel.SimpleChannelInboundHandler;
23 import java.io.IOException;
24 import java.nio.channels.NonWritableChannelException;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.TimeUnit;
29 import javax.annotation.concurrent.GuardedBy;
30 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
31 import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
32 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
33 import org.opendaylight.protocol.bgp.parser.BGPError;
34 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
35 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
36 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
37 import org.opendaylight.protocol.bgp.parser.spi.pojo.MultiPathSupportImpl;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
41 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
42 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
43 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapability;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
62 import org.opendaylight.yangtools.yang.binding.Notification;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 @VisibleForTesting
67 public class BGPSessionImpl extends SimpleChannelInboundHandler<Notification> implements BGPSession, BGPSessionStatistics, AutoCloseable {
68
69     private static final Logger LOG = LoggerFactory.getLogger(BGPSessionImpl.class);
70
71     private static final Notification KEEP_ALIVE = new KeepaliveBuilder().build();
72
73     private static final int KA_TO_DEADTIMER_RATIO = 3;
74
75     private static final String EXTENDED_MSG_DECODER = "EXTENDED_MSG_DECODER";
76
77     static final String END_OF_INPUT = "End of input detected. Close the session.";
78
79     /**
80      * Internal session state.
81      */
82     public enum State {
83         /**
84          * The session object is created by the negotiator in OpenConfirm state. While in this state, the session object
85          * is half-alive, e.g. the timers are running, but the session is not completely up, e.g. it has not been
86          * announced to the listener. If the session is torn down in this state, we do not inform the listener.
87          */
88         OPEN_CONFIRM,
89         /**
90          * The session has been completely established.
91          */
92         UP,
93         /**
94          * The session has been closed. It will not be resurrected.
95          */
96         IDLE,
97     }
98
99     /**
100      * System.nanoTime value about when was sent the last message.
101      */
102     @VisibleForTesting
103     private long lastMessageSentAt;
104
105     /**
106      * System.nanoTime value about when was received the last message
107      */
108     private long lastMessageReceivedAt;
109
110     private final BGPSessionListener listener;
111
112     private final BGPSynchronization sync;
113
114     private int kaCounter = 0;
115
116     private final Channel channel;
117
118     @GuardedBy("this")
119     private State state = State.OPEN_CONFIRM;
120
121     private final Set<BgpTableType> tableTypes;
122     private final List<AddressFamilies> addPathTypes;
123     private final int holdTimerValue;
124     private final int keepAlive;
125     private final AsNumber asNumber;
126     private final Ipv4Address bgpId;
127     private final BGPPeerRegistry peerRegistry;
128     private final ChannelOutputLimiter limiter;
129
130     private BGPSessionStats sessionStats;
131
132     public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen, final BGPSessionPreferences localPreferences,
133             final BGPPeerRegistry peerRegistry) {
134         this(listener, channel, remoteOpen, localPreferences.getHoldTime(), peerRegistry);
135         this.sessionStats = new BGPSessionStats(remoteOpen, this.holdTimerValue, this.keepAlive, channel, Optional.of(localPreferences), this.tableTypes, this.addPathTypes);
136     }
137
138     public BGPSessionImpl(final BGPSessionListener listener, final Channel channel, final Open remoteOpen, final int localHoldTimer,
139             final BGPPeerRegistry peerRegistry) {
140         this.listener = Preconditions.checkNotNull(listener);
141         this.channel = Preconditions.checkNotNull(channel);
142         this.limiter = new ChannelOutputLimiter(this);
143         this.channel.pipeline().addLast(this.limiter);
144         this.holdTimerValue = (remoteOpen.getHoldTimer() < localHoldTimer) ? remoteOpen.getHoldTimer() : localHoldTimer;
145         LOG.info("BGP HoldTimer new value: {}", this.holdTimerValue);
146         this.keepAlive = this.holdTimerValue / KA_TO_DEADTIMER_RATIO;
147         this.asNumber = AsNumberUtil.advertizedAsNumber(remoteOpen);
148         this.peerRegistry = peerRegistry;
149         final boolean enableExMess = BgpExtendedMessageUtil.advertizedBgpExtendedMessageCapability(remoteOpen);
150         if (enableExMess) {
151             this.channel.pipeline().replace(BGPMessageHeaderDecoder.class, EXTENDED_MSG_DECODER, BGPMessageHeaderDecoder.getExtendedBGPMessageHeaderDecoder());
152         }
153
154         final Set<TablesKey> tts = Sets.newHashSet();
155         final Set<BgpTableType> tats = Sets.newHashSet();
156         final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
157         if (remoteOpen.getBgpParameters() != null) {
158             for (final BgpParameters param : remoteOpen.getBgpParameters()) {
159                 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
160                     final CParameters cParam = optCapa.getCParameters();
161                     if ( cParam.getAugmentation(CParameters1.class) == null) {
162                         continue;
163                     }
164                     if(cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
165                         final MultiprotocolCapability multi = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
166                         final TablesKey tt = new TablesKey(multi.getAfi(), multi.getSafi());
167                         LOG.trace("Added table type to sync {}", tt);
168                         tts.add(tt);
169                         tats.add(new BgpTableTypeImpl(tt.getAfi(), tt.getSafi()));
170                     } else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
171                         final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class).getAddPathCapability();
172                         addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
173                     }
174                 }
175             }
176         }
177
178         this.sync = new BGPSynchronization(this.listener, tts);
179         this.tableTypes = tats;
180         this.addPathTypes = addPathCapabilitiesList;
181
182         if (! this.addPathTypes.isEmpty()) {
183             final ChannelPipeline pipeline = this.channel.pipeline();
184             final BGPByteToMessageDecoder decoder = pipeline.get(BGPByteToMessageDecoder.class);
185             decoder.addDecoderConstraint(MultiPathSupport.class,
186                     MultiPathSupportImpl.createParserMultiPathSupport(this.addPathTypes));
187         }
188
189         if (this.holdTimerValue != 0) {
190             channel.eventLoop().schedule(new Runnable() {
191                 @Override
192                 public void run() {
193                     handleHoldTimer();
194                 }
195             }, this.holdTimerValue, TimeUnit.SECONDS);
196
197             channel.eventLoop().schedule(new Runnable() {
198                 @Override
199                 public void run() {
200                     handleKeepaliveTimer();
201                 }
202             }, this.keepAlive, TimeUnit.SECONDS);
203         }
204         this.bgpId = remoteOpen.getBgpIdentifier();
205         this.sessionStats = new BGPSessionStats(remoteOpen, this.holdTimerValue, this.keepAlive, channel, Optional.<BGPSessionPreferences>absent(),
206                 this.tableTypes, this.addPathTypes);
207     }
208
209     @Override
210     public synchronized void close() {
211         if (this.state != State.IDLE) {
212             this.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode(BGPError.CEASE.getSubcode()).build());
213         }
214         this.closeWithoutMessage();
215     }
216
217     /**
218      * Handles incoming message based on their type.
219      *
220      * @param msg incoming message
221      */
222     synchronized void handleMessage(final Notification msg) throws BGPDocumentedException {
223         // Update last reception time
224         this.lastMessageReceivedAt = System.nanoTime();
225         this.sessionStats.updateReceivedMsgTotal();
226
227         if (msg instanceof Open) {
228             // Open messages should not be present here
229             this.terminate(new BGPDocumentedException(null, BGPError.FSM_ERROR));
230         } else if (msg instanceof Notify) {
231             // Notifications are handled internally
232             LOG.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
233                     ((Notify) msg).getErrorSubcode());
234             this.closeWithoutMessage();
235             this.listener.onSessionTerminated(this, new BGPTerminationReason(
236                     BGPError.forValue(((Notify) msg).getErrorCode(), ((Notify) msg).getErrorSubcode())));
237             this.sessionStats.updateReceivedMsgErr((Notify) msg);
238         } else if (msg instanceof Keepalive) {
239             // Keepalives are handled internally
240             LOG.trace("Received KeepAlive messsage.");
241             this.kaCounter++;
242             this.sessionStats.updateReceivedMsgKA();
243             if (this.kaCounter >= 2) {
244                 this.sync.kaReceived();
245             }
246         } else if (msg instanceof RouteRefresh) {
247             this.listener.onMessage(this, msg);
248             this.sessionStats.updateReceivedMsgRR();
249         } else if (msg instanceof Update) {
250             this.listener.onMessage(this, msg);
251             this.sync.updReceived((Update) msg);
252             this.sessionStats.updateReceivedMsgUpd();
253         } else {
254             LOG.warn("Ignoring unhandled message: {}.", msg.getClass());
255         }
256     }
257
258     synchronized void endOfInput() {
259         if (this.state == State.UP) {
260             LOG.info(END_OF_INPUT);
261             this.listener.onSessionDown(this, new IOException(END_OF_INPUT));
262         }
263     }
264
265     @GuardedBy("this")
266     private ChannelFuture writeEpilogue(final ChannelFuture future, final Notification msg) {
267         future.addListener(
268             new ChannelFutureListener() {
269                 @Override
270                 public void operationComplete(final ChannelFuture f) {
271                     if (!f.isSuccess()) {
272                         LOG.warn("Failed to send message {} to socket {}", msg, BGPSessionImpl.this.channel, f.cause());
273                     } else {
274                         LOG.trace("Message {} sent to socket {}", msg, BGPSessionImpl.this.channel);
275                     }
276                 }
277             });
278         this.lastMessageSentAt = System.nanoTime();
279         this.sessionStats.updateSentMsgTotal();
280         if (msg instanceof Update) {
281             this.sessionStats.updateSentMsgUpd();
282         } else if (msg instanceof Notify) {
283             this.sessionStats.updateSentMsgErr((Notify) msg);
284         }
285         return future;
286     }
287
288     void flush() {
289         this.channel.flush();
290     }
291
292     synchronized void write(final Notification msg) {
293         try {
294             writeEpilogue(this.channel.write(msg), msg);
295         } catch (final Exception e) {
296             LOG.warn("Message {} was not sent.", msg, e);
297         }
298     }
299
300     synchronized ChannelFuture writeAndFlush(final Notification msg) {
301         if (isWritable()) {
302             return writeEpilogue(this.channel.writeAndFlush(msg), msg);
303         }
304         return this.channel.newFailedFuture(new NonWritableChannelException());
305     }
306
307     private synchronized void closeWithoutMessage() {
308         LOG.info("Closing session: {}", this);
309         removePeerSession();
310         this.channel.close().addListener(new ChannelFutureListener() {
311             @Override
312             public void operationComplete(final ChannelFuture future) throws Exception {
313                 Preconditions.checkArgument(future.isSuccess(), "Channel failed to close: %s", future.cause());
314             }
315         });
316         this.state = State.IDLE;
317     }
318
319     /**
320      * Closes BGP session from the parent with given reason. A message needs to be sent, but parent doesn't have to be
321      * modified, because he initiated the closing. (To prevent concurrent modification exception).
322      *
323      * @param e BGPDocumentedException
324      */
325     private synchronized void terminate(final BGPDocumentedException e) {
326         final BGPError error = e.getError();
327         final byte[] data = e.getData();
328         final NotifyBuilder builder = new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode());
329         if (data != null && data.length != 0) {
330             builder.setData(data);
331         }
332         this.writeAndFlush(builder.build());
333         this.closeWithoutMessage();
334
335         this.listener.onSessionTerminated(this, new BGPTerminationReason(error));
336     }
337
338     private void removePeerSession() {
339         if (this.peerRegistry != null) {
340             this.peerRegistry.removePeerSession(StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress()));
341         }
342     }
343
344     /**
345      * If HoldTimer expires, the session ends. If a message (whichever) was received during this period, the HoldTimer
346      * will be rescheduled by HOLD_TIMER_VALUE + the time that has passed from the start of the HoldTimer to the time at
347      * which the message was received. If the session was closed by the time this method starts to execute (the session
348      * state will become IDLE), then rescheduling won't occur.
349      */
350     private synchronized void handleHoldTimer() {
351         if (this.state == State.IDLE) {
352             return;
353         }
354
355         final long ct = System.nanoTime();
356         final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(this.holdTimerValue);
357
358         if (ct >= nextHold) {
359             LOG.debug("HoldTimer expired. {}", new Date());
360             this.terminate(new BGPDocumentedException(BGPError.HOLD_TIMER_EXPIRED));
361         } else {
362             this.channel.eventLoop().schedule(new Runnable() {
363                 @Override
364                 public void run() {
365                     handleHoldTimer();
366                 }
367             }, nextHold - ct, TimeUnit.NANOSECONDS);
368         }
369     }
370
371     /**
372      * If KeepAlive Timer expires, sends KeepAlive message. If a message (whichever) was send during this period, the
373      * KeepAlive Timer will be rescheduled by KEEP_ALIVE_TIMER_VALUE + the time that has passed from the start of the
374      * KeepAlive timer to the time at which the message was sent. If the session was closed by the time this method
375      * starts to execute (the session state will become IDLE), that rescheduling won't occur.
376      */
377     private synchronized void handleKeepaliveTimer() {
378         if (this.state == State.IDLE) {
379             return;
380         }
381
382         final long ct = System.nanoTime();
383         long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
384
385         if (ct >= nextKeepalive) {
386             this.writeAndFlush(KEEP_ALIVE);
387             nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
388             this.sessionStats.updateSentMsgKA();
389         }
390         this.channel.eventLoop().schedule(new Runnable() {
391             @Override
392             public void run() {
393                 handleKeepaliveTimer();
394             }
395         }, nextKeepalive - ct, TimeUnit.NANOSECONDS);
396     }
397
398     @Override
399     public final String toString() {
400         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
401     }
402
403     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
404         toStringHelper.add("channel", this.channel);
405         toStringHelper.add("state", this.getState());
406         return toStringHelper;
407     }
408
409     @Override
410     public Set<BgpTableType> getAdvertisedTableTypes() {
411         return this.tableTypes;
412     }
413
414     @Override
415     public List<AddressFamilies> getAdvertisedAddPathTableTypes() {
416         return this.addPathTypes;
417     }
418
419     protected synchronized void sessionUp() {
420         this.sessionStats.startSessionStopwatch();
421         this.state = State.UP;
422         this.listener.onSessionUp(this);
423     }
424
425     public synchronized State getState() {
426         return this.state;
427     }
428
429     @Override
430     public final Ipv4Address getBgpId() {
431         return this.bgpId;
432     }
433
434     @Override
435     public final AsNumber getAsNumber() {
436         return this.asNumber;
437     }
438
439     synchronized boolean isWritable() {
440         return this.channel != null && this.channel.isWritable();
441     }
442
443     void schedule(final Runnable task) {
444         Preconditions.checkState(this.channel != null);
445         this.channel.eventLoop().submit(task);
446     }
447
448     @VisibleForTesting
449     protected synchronized void setLastMessageSentAt(final long lastMessageSentAt) {
450         this.lastMessageSentAt = lastMessageSentAt;
451     }
452
453     @Override
454     public synchronized BgpSessionState getBgpSessionState() {
455         return this.sessionStats.getBgpSessionState(this.state);
456     }
457
458     @Override
459     public synchronized void resetSessionStats() {
460         this.sessionStats.resetStats();
461     }
462
463     public ChannelOutputLimiter getLimiter() {
464         return this.limiter;
465     }
466
467     @Override
468     public final void channelInactive(final ChannelHandlerContext ctx) {
469         LOG.debug("Channel {} inactive.", ctx.channel());
470         this.endOfInput();
471
472         try {
473             super.channelInactive(ctx);
474         } catch (final Exception e) {
475             throw new IllegalStateException("Failed to delegate channel inactive event on channel " + ctx.channel(), e);
476         }
477     }
478
479     @Override
480     protected final void channelRead0(final ChannelHandlerContext ctx, final Notification msg) {
481         LOG.debug("Message was received: {}", msg);
482         try {
483             this.handleMessage(msg);
484         } catch (final BGPDocumentedException e) {
485             this.terminate(e);
486         }
487     }
488
489     @Override
490     public final void handlerAdded(final ChannelHandlerContext ctx) {
491         this.sessionUp();
492     }
493
494     @Override
495     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
496         LOG.warn("BGP session encountered error", cause);
497         if (cause.getCause() instanceof BGPDocumentedException) {
498             this.terminate((BGPDocumentedException) cause.getCause());
499         } else {
500             this.close();
501         }
502     }
503 }