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