X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Frib-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FBGPPeer.java;h=57d2d38ca953382ef2537c16e4986e9713c43413;hb=20fe6f4c4deb5f0c2b48128ee98846b2853633ea;hp=ab8f0e2d61fd6d24fbf55960293688e87f98b605;hpb=45e441f5b159f616ae85bcf62c0e0d2432d485ad;p=bgpcep.git diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java index ab8f0e2d61..57d2d38ca9 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java @@ -30,8 +30,9 @@ import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListen import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics; import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; -import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer; +import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext; import org.opendaylight.protocol.bgp.rib.spi.BGPSession; +import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener; import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason; import org.opendaylight.protocol.bgp.rib.spi.Peer; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; @@ -41,6 +42,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType; @@ -50,6 +52,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily; @@ -62,7 +65,7 @@ import org.slf4j.LoggerFactory; * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into * RIB actions. */ -public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener { +public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener { private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class); @@ -82,6 +85,7 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun private BGPPeerRuntimeRegistrator registrator; private BGPPeerRuntimeRegistration runtimeReg; private long sessionEstablishedCounter = 0L; + private final Set adjRibOutListenerSet = new HashSet<>(); public BGPPeer(final String name, final RIB rib) { this(name, rib, PeerRole.Ibgp); @@ -96,8 +100,8 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun @Override public synchronized void close() { - dropConnection(); - // TODO should this perform cleanup ? + releaseConnection(); + this.chain.close(); } @Override @@ -111,18 +115,18 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun // update AdjRibs final Attributes attrs = message.getAttributes(); MpReachNlri mpReach = null; - if (message.getNlri() != null) { + final boolean isAnyNlriAnnounced = message.getNlri() != null; + if (isAnyNlriAnnounced) { mpReach = prefixesToMpReach(message); } else if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) { mpReach = attrs.getAugmentation(Attributes1.class).getMpReachNlri(); } if (mpReach != null) { - this.ribWriter.updateRoutes(mpReach, attrs); - return; + this.ribWriter.updateRoutes(mpReach, nextHopToAttribute(attrs, mpReach)); } MpUnreachNlri mpUnreach = null; if (message.getWithdrawnRoutes() != null) { - mpUnreach = prefixesToMpUnreach(message); + mpUnreach = prefixesToMpUnreach(message, isAnyNlriAnnounced); } else if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) { mpUnreach = attrs.getAugmentation(Attributes2.class).getMpUnreachNlri(); } @@ -131,13 +135,22 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun } } + private static Attributes nextHopToAttribute(final Attributes attrs, final MpReachNlri mpReach) { + if (attrs.getCNextHop() == null && mpReach.getCNextHop() != null) { + final AttributesBuilder attributesBuilder = new AttributesBuilder(attrs); + attributesBuilder.setCNextHop(mpReach.getCNextHop()); + return attributesBuilder.build(); + } + return attrs; + } + /** * Creates MPReach for the prefixes to be handled in the same way as linkstate routes * * @param message Update message containing prefixes in NLRI * @return MpReachNlri with prefixes from the nlri field */ - private MpReachNlri prefixesToMpReach(final Update message) { + private static MpReachNlri prefixesToMpReach(final Update message) { final List prefixes = new ArrayList<>(); for (final Ipv4Prefix p : message.getNlri().getNlri()) { prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build()); @@ -157,12 +170,20 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun * Create MPUnreach for the prefixes to be handled in the same way as linkstate routes * * @param message Update message containing withdrawn routes + * @param isAnyNlriAnnounced * @return MpUnreachNlri with prefixes from the withdrawn routes field */ - private MpUnreachNlri prefixesToMpUnreach(final Update message) { + private static MpUnreachNlri prefixesToMpUnreach(final Update message, final boolean isAnyNlriAnnounced) { final List prefixes = new ArrayList<>(); for (final Ipv4Prefix p : message.getWithdrawnRoutes().getWithdrawnRoutes()) { - prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build()); + boolean nlriAnounced = false; + if(isAnyNlriAnnounced) { + nlriAnounced = message.getNlri().getNlri().contains(p); + } + + if(!nlriAnounced) { + prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build()); + } } return new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes( new WithdrawnRoutesBuilder().setDestinationType( @@ -175,33 +196,63 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes()); this.session = session; this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress(); + final PeerId peerId = RouterIds.createPeerId(session.getBgpId()); - for (final BgpTableType t : session.getAdvertisedTableTypes()) { - final TablesKey key = new TablesKey(t.getAfi(), t.getSafi()); - this.tables.add(key); + createAdjRibOutListener(peerId); - // not particularly nice - if (session instanceof BGPSessionImpl) { - AdjRibOutListener.create(key, this.rib.getYangRibId(), ((RIBImpl)this.rib).getService(), this.rib.getRibSupportContext(), ((BGPSessionImpl) session).getLimiter()); - } - } - this.ribWriter = this.ribWriter.transform(RouterIds.createPeerId(session.getBgpId()), this.rib.getRibSupportContext(), this.tables, false); + this.ribWriter = this.ribWriter.transform(peerId, this.rib.getRibSupportContext(), this.tables, false); this.sessionEstablishedCounter++; if (this.registrator != null) { this.runtimeReg = this.registrator.register(this); } } + private void createAdjRibOutListener(final PeerId peerId) { + for (final BgpTableType t : session.getAdvertisedTableTypes()) { + final TablesKey key = new TablesKey(t.getAfi(), t.getSafi()); + if (this.tables.add(key)) { + createAdjRibOutListener(peerId, key, true); + } + } + + addBgp4Support(peerId); + } + + //try to add a support for old-school BGP-4, if peer did not advertise IPv4-Unicast MP capability + private void addBgp4Support(final PeerId peerId) { + final TablesKey key = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class); + if (this.tables.add(key)) { + createAdjRibOutListener(peerId, key, false); + } + } + + private void createAdjRibOutListener(final PeerId peerId, final TablesKey key, final boolean mpSupport) { + final RIBSupportContext context = this.rib.getRibSupportContext().getRIBSupportContext(key); + + // not particularly nice + if (context != null && this.session instanceof BGPSessionImpl) { + this.adjRibOutListenerSet.add(AdjRibOutListener.create(peerId, key, this.rib.getYangRibId(), this.rib.getCodecsRegistry(), + context.getRibSupport(), ((RIBImpl) this.rib).getService(), ((BGPSessionImpl) this.session).getLimiter(), mpSupport)); + } + } + private synchronized void cleanup() { - // FIXME: BUG-196: support graceful restart - this.ribWriter.cleanTables(this.tables); - this.chain.close(); + // FIXME: BUG-196: support graceful + for (final AdjRibOutListener adjRibOutListener : this.adjRibOutListenerSet) { + adjRibOutListener.close(); + } + this.adjRibOutListenerSet.clear(); + this.ribWriter.removePeer(); this.tables.clear(); } @Override public void onSessionDown(final BGPSession session, final Exception e) { - LOG.info("Session with peer {} went down", this.name, e); + if(e.getMessage().equals(BGPSessionImpl.END_OF_INPUT)) { + LOG.info("Session with peer {} went down", this.name); + } else { + LOG.info("Session with peer {} went down", this.name, e); + } releaseConnection(); } @@ -229,8 +280,8 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun @Override public void releaseConnection() { - dropConnection(); cleanup(); + dropConnection(); } @GuardedBy("this") @@ -240,7 +291,11 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun this.runtimeReg = null; } if (this.session != null) { - this.session.close(); + try { + this.session.close(); + } catch (final Exception e) { + LOG.warn("Error closing session with peer", e); + } this.session = null; } } @@ -297,7 +352,8 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun @Override public void onTransactionChainFailed(final TransactionChain chain, final AsyncTransaction transaction, final Throwable cause) { LOG.error("Transaction chain failed.", cause); - this.dropConnection(); + releaseConnection(); + this.chain.close(); this.chain = this.rib.createPeerChain(this); }