LCOV - code coverage report
Current view: top level - bgp - bgp_peer_close.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 179 235 76.2 %
Date: 2026-11-16 02:06:09 Functions: 38 42 90.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <boost/foreach.hpp>
       7             : #include <boost/assign/list_of.hpp>
       8             : #include <boost/scoped_ptr.hpp>
       9             : 
      10             : #include "base/task_annotations.h"
      11             : #include "net/bgp_af.h"
      12             : #include "bgp/bgp_factory.h"
      13             : #include "bgp/bgp_path.h"
      14             : #include "bgp/bgp_log.h"
      15             : #include "bgp/bgp_peer.h"
      16             : #include "bgp/bgp_peer_close.h"
      17             : #include "bgp/bgp_peer_types.h"
      18             : #include "bgp/peer_close_manager.h"
      19             : 
      20             : using boost::assign::list_of;
      21             : using std::back_inserter;
      22             : using std::includes;
      23             : using std::string;
      24             : using std::vector;
      25             : 
      26          32 : BgpPeerClose::BgpPeerClose(BgpPeer *peer)
      27          32 :         : peer_(peer), flap_count_(0) {
      28          32 : }
      29             : 
      30          64 : BgpPeerClose::~BgpPeerClose() {
      31          64 : }
      32             : 
      33        8893 : PeerCloseManager *BgpPeerClose::GetManager() const {
      34        8893 :     return peer_->close_manager();
      35             : }
      36             : 
      37         168 : void BgpPeerClose::CustomClose() {
      38         168 :     return peer_->CustomClose();
      39             : }
      40             : 
      41          16 : void BgpPeerClose::GracefulRestartStale() {
      42          16 :     negotiated_families_ = peer_->negotiated_families();
      43          16 : }
      44             : 
      45           0 : void BgpPeerClose::LongLivedGracefulRestartStale() {
      46           0 : }
      47             : 
      48           8 : void BgpPeerClose::GracefulRestartSweep() {
      49           8 :     negotiated_families_.clear();
      50           8 : }
      51             : 
      52          16 : bool BgpPeerClose::IsReady() const {
      53          16 :     return peer_->IsReady();
      54             : }
      55             : 
      56       15965 : IPeer *BgpPeerClose::peer() const {
      57       15965 :     return peer_;
      58             : }
      59             : 
      60             : // Return the time to wait for, in seconds to exit GR_TIMER state.
      61          96 : int BgpPeerClose::GetGracefulRestartTime() const {
      62          96 :     return gr_params_.time;
      63             : }
      64             : 
      65             : // Return the time to wait for, in seconds to exit LLGR_TIMER state.
      66          80 : int BgpPeerClose::GetLongLivedGracefulRestartTime() const {
      67          80 :     return llgr_params_.time;
      68             : }
      69             : 
      70           0 : void BgpPeerClose::ReceiveEndOfRIB(Address::Family family) {
      71           0 :     peer_->ReceiveEndOfRIB(family, 0);
      72           0 : }
      73             : 
      74        1692 : const char *BgpPeerClose::GetTaskName() const {
      75        1692 :     return "bgp::Config";
      76             : }
      77             : 
      78          32 : int BgpPeerClose::GetTaskInstance() const {
      79          32 :     return 0;
      80             : }
      81             : 
      82          88 : void BgpPeerClose::MembershipRequestCallbackComplete() {
      83          88 :     CHECK_CONCURRENCY(GetTaskName());
      84          88 : }
      85             : 
      86         391 : bool BgpPeerClose::IsGRHelperModeEnabled() const {
      87         391 :     return peer_->server()->IsGRHelperModeEnabled();
      88             : }
      89             : 
      90         248 : const std::vector<std::string> &BgpPeerClose::PeerNegotiatedFamilies() const {
      91         248 :     return peer_->negotiated_families();
      92             : }
      93             : 
      94         408 : bool BgpPeerClose::IsPeerDeleted() const {
      95         408 :     return peer_->IsDeleted();
      96             : }
      97             : 
      98         408 : bool BgpPeerClose::IsPeerAdminDown() const {
      99         408 :     return peer_->IsAdminDown();
     100             : }
     101             : 
     102         392 : bool BgpPeerClose::IsServerDeleted() const {
     103         392 :     return peer_->server()->IsDeleted();
     104             : }
     105             : 
     106         391 : bool BgpPeerClose::IsServerAdminDown() const {
     107         391 :     return peer_->server()->admin_down();
     108             : }
     109             : 
     110         160 : bool BgpPeerClose::IsInGRTimerWaitState() const {
     111         160 :     return GetManager()->IsInGRTimerWaitState();
     112             : }
     113             : 
     114          16 : bool BgpPeerClose::IsInLlgrTimerWaitState() const {
     115          16 :     return GetManager()->IsInLlgrTimerWaitState();
     116             : }
     117             : 
     118         248 : const std::vector<std::string> &BgpPeerClose::negotiated_families() const {
     119         248 :     return negotiated_families_;
     120             : }
     121             : 
     122             : const std::vector<BgpProto::OpenMessage::Capability *> &
     123         160 : BgpPeerClose::capabilities() const {
     124         160 :     return peer_->capabilities();
     125             : }
     126             : 
     127        8533 : void BgpPeerClose::UpdateRouteStats(Address::Family family,
     128             :                                     const BgpPath *old_path,
     129             :                                     uint32_t path_flags) const {
     130        8533 :     GetManager()->UpdateRouteStats(family, old_path, path_flags);
     131        8533 : }
     132             : 
     133         184 : void BgpPeerClose::Close(bool graceful) {
     134             :     // Abort GR-Closure if this request is for non-graceful closure.
     135             :     // Reset GR-Closure if previous closure is still in progress or if
     136             :     // this is a flip (from established state).
     137         184 :     if (!graceful || flap_count_ != peer_->total_flap_count()) {
     138          96 :         if (flap_count_ != peer_->total_flap_count()) {
     139          80 :             flap_count_++;
     140          80 :             assert(peer_->total_flap_count() == flap_count_);
     141             :         }
     142          96 :         GetManager()->Close(graceful);
     143          96 :         return;
     144             :     }
     145             : 
     146             :     // Ignore if close is already in progress.
     147          88 :     if (GetManager()->IsCloseInProgress() && !IsInGRTimerWaitState())
     148           0 :         return;
     149             : 
     150          88 :     if (peer_->IsDeleted()) {
     151           0 :         peer_->RetryDelete();
     152             :     } else {
     153          88 :         CustomClose();
     154          88 :         CloseComplete();
     155             :     }
     156             : }
     157             : 
     158          80 : void BgpPeerClose::Delete() {
     159          80 :     gr_params_.Initialize();
     160          80 :     llgr_params_.Initialize();
     161          80 :     gr_families_.clear();
     162          80 :     llgr_families_.clear();
     163          80 :     negotiated_families_.clear();
     164          80 :     if (peer_->IsDeleted()) {
     165          32 :         peer_->RetryDelete();
     166             :     } else {
     167          48 :         CloseComplete();
     168             :     }
     169          80 : }
     170             : 
     171         391 : bool BgpPeerClose::IsGRReady() const {
     172             :     // Check if GR Helper mode is disabled.
     173         391 :     if (!IsGRHelperModeEnabled()) {
     174         224 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     175             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because it is not"
     176             :             " configured");
     177         224 :         return false;
     178             :     }
     179             : 
     180             :     // Check if GR is supported by the peer.
     181         168 :     if (gr_params_.families.empty()) {
     182           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     183             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because received "
     184             :             "GR address families list is empty");
     185           0 :         return false;
     186             :     }
     187             : 
     188             :     // LLGR should be in effect to enable GR Helper mode with 0 restart time.
     189         168 :     if (!gr_params_.time && !IsCloseLongLivedGracefulInternal()) {
     190           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     191             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because received "
     192             :             "GR restart time value is 0 seconds and (there is no applicable "
     193             :             "LLGR as well");
     194           0 :         return false;
     195             :     }
     196             : 
     197             :     // Abort GR if currently negotiated families differ from already
     198             :     // staled address families.
     199         248 :     if (!negotiated_families().empty() &&
     200          80 :             PeerNegotiatedFamilies() != negotiated_families()) {
     201           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     202             :             BGP_PEER_DIR_IN, "GR Helper mode is aborted because received "
     203             :             "GR families list differs from the list received last time");
     204           0 :         return false;
     205             :     }
     206             : 
     207             :     // If GR is not supported for any of the negotiated address family,
     208             :     // then consider GR as not supported
     209         168 :     if (PeerNegotiatedFamilies() != gr_families_) {
     210           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     211             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because GR address "
     212             :             "families differs from negotiated address families");
     213           0 :         return false;
     214             :     }
     215             : 
     216             :     // Make sure that forwarding state is preserved for all families in
     217             :     // the restarting speaker. (Except for ERMVPN)
     218        1008 :     for (auto family : gr_params_.families) {
     219             :         // Check if forwarding state was preserved during restart.
     220         840 :         if (family.forwarding_state_preserved())
     221         840 :             continue;
     222             : 
     223             :         // Ignore forwarding-state preservation check for certain families.
     224             :         Address::Family addr_family =
     225           0 :             BgpAf::AfiSafiToFamily(family.afi, family.safi);
     226           0 :         if (addr_family == Address::ERMVPN || addr_family == Address::MVPN)
     227           0 :             continue;
     228             : 
     229           0 :         string family_str = Address::FamilyToString(addr_family);
     230           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG,
     231             :             BGP_LOG_FLAG_ALL, BGP_PEER_DIR_IN, "GR Helper mode is not  "
     232             :             "enabled because after restart, GR forwarding state is not "
     233             :             "preserved for address family " << family_str);
     234           0 :         return false;
     235           0 :     }
     236         168 :     return true;
     237             : }
     238             : 
     239             : // If the peer is deleted or administratively held down, do not attempt
     240             : // graceful restart
     241         408 : bool BgpPeerClose::IsCloseGraceful() const {
     242         408 :     if (IsPeerDeleted()) {
     243           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     244             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpPeer has"
     245             :             " been deleted");
     246           0 :         return false;
     247             :     }
     248             : 
     249         408 :     if (IsPeerAdminDown()) {
     250          16 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     251             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpPeer has"
     252             :             " been held administratively down");
     253          16 :         return false;
     254             :     }
     255             : 
     256         392 :     if (IsServerDeleted()) {
     257           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     258             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpServer "
     259             :             "has been deletd");
     260           0 :         return false;
     261             :     }
     262             : 
     263         391 :     if (IsServerAdminDown()) {
     264           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     265             :             BGP_PEER_DIR_IN, "GR Helper mode is not enabled because BgpServer "
     266             :             "has been held administratively down");
     267           0 :         return false;
     268             :     }
     269             : 
     270         391 :     if (!IsGRReady())
     271         224 :         return false;
     272         168 :     return true;
     273             : }
     274             : 
     275             : // LLGR families should be identical to GR families.
     276         136 : bool BgpPeerClose::IsLlgrSupportedForFamilies() const {
     277             :     // Keep a sorted list of unsupported families.
     278             :     static vector<string> unsupported_families = list_of
     279           2 :         (Address::FamilyToString(Address::ERMVPN))
     280         137 :         (Address::FamilyToString(Address::MVPN));
     281             : 
     282         136 :     if (gr_families_ == llgr_families_)
     283         136 :         return true;
     284             : 
     285             :     // Ignore families mis-match for certain families.
     286           0 :     vector<string> differing_families;
     287           0 :     std::set_symmetric_difference(gr_families_.begin(), gr_families_.end(),
     288             :                                   llgr_families_.begin(), llgr_families_.end(),
     289             :                                   back_inserter(differing_families));
     290             : 
     291             :     // Ignore if differing families are only those which are unsupported.
     292           0 :     return includes(unsupported_families.begin(), unsupported_families.end(),
     293           0 :                     differing_families.begin(), differing_families.end());
     294           0 : }
     295             : 
     296             : // Check if we need to trigger Long Lived Graceful Restart. In addition to
     297             : // normal GR checks, we also need to check LLGR capability was negotiated
     298             : // and non-zero restart time was inferred.
     299         344 : bool BgpPeerClose::IsCloseLongLivedGraceful() const {
     300         344 :     if (!IsCloseGraceful())
     301         208 :         return false;
     302         136 :     return BgpPeerClose::IsCloseLongLivedGracefulInternal();
     303             : }
     304             : 
     305         136 : bool BgpPeerClose::IsCloseLongLivedGracefulInternal() const {
     306         136 :     if (llgr_params_.families.empty()) {
     307           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     308             :             BGP_PEER_DIR_IN,
     309             :             "No LLGR support due to empty LLGR address families list");
     310           0 :         return false;
     311             :     }
     312         136 :     if (!llgr_params_.time) {
     313           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     314             :             BGP_PEER_DIR_IN, "No LLGR support due to zero time value");
     315           0 :         return false;
     316             :     }
     317             : 
     318         136 :     if (!IsLlgrSupportedForFamilies()) {
     319           0 :         BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
     320             :             BGP_PEER_DIR_IN, "No LLGR support due to dissimilar "
     321             :             "GR address families and LLGR address families");
     322           0 :         return false;
     323             :     }
     324             : 
     325             :     // Make sure that forwarding state is preserved for all families in
     326             :     // the restarting speaker.
     327         816 :     for (auto family : llgr_params_.families) {
     328             :         // Ignore forwarding-state preservation check for certain families.
     329             :         Address::Family addr_family =
     330         680 :             BgpAf::AfiSafiToFamily(family.afi, family.safi);
     331         680 :         if (addr_family == Address::ERMVPN || addr_family == Address::MVPN)
     332         136 :             continue;
     333             : 
     334         544 :         if (!family.forwarding_state_preserved()) {
     335           0 :             string family_str = Address::FamilyToString(addr_family);
     336           0 :             BGP_LOG_PEER(Message, peer_, SandeshLevel::SYS_DEBUG,
     337             :                 BGP_LOG_FLAG_ALL, BGP_PEER_DIR_IN, "GR Helper mode is not "
     338             :                 "enabled because after restart, LLGR forwarding state is not "
     339             :                 "preserved for address family " << family_str);
     340           0 :             return false;
     341           0 :         }
     342             :     }
     343         136 :     return true;
     344             : }
     345             : 
     346         136 : void BgpPeerClose::RestartStateMachine() {
     347         136 :     peer_->state_machine()->Initialize();
     348         136 : }
     349             : 
     350             : // Close process for this peer is complete. Restart the state machine and
     351             : // attempt to bring up session with the neighbor
     352         152 : void BgpPeerClose::CloseComplete() {
     353         152 :     if (!peer_->IsDeleted() && !peer_->IsAdminDown())
     354         136 :         RestartStateMachine();
     355         152 : }
     356             : 
     357          16 : void BgpPeerClose::GetGracefulRestartFamilies(Families *families) const {
     358          16 :     families->clear();
     359          96 :     for (const auto& family : gr_families_) {
     360          80 :         families->insert(Address::FamilyFromString(family));
     361             :     }
     362          16 : }
     363             : 
     364           0 : void BgpPeerClose::GetLongLivedGracefulRestartFamilies(Families *families) const {
     365           0 :     families->clear();
     366           0 :     for (const auto& family : llgr_families_) {
     367           0 :         families->insert(Address::FamilyFromString(family));
     368             :     }
     369           0 : }
     370             : 
     371         188 : void BgpPeerClose::AddGRCapabilities(
     372             :         BgpProto::OpenMessage::OptParam *opt_param) {
     373         188 :     vector<Address::Family> gr_families;
     374         188 :     vector<uint8_t> afi_flags;
     375             : 
     376             :     // Indicate to the Peer if we are in restarting phase.
     377         188 :     bool restarted = peer_->IsServerStartingUp();
     378             : 
     379             :     // Indicate EOR support by default.
     380         188 :     if (!peer_->server()->global_config()->gr_enable()) {
     381             :         BgpProto::OpenMessage::Capability *gr_cap =
     382          26 :             BgpProto::OpenMessage::Capability::GR::Encode(0, restarted, false,
     383             :                                                           afi_flags,
     384          26 :                                                           gr_families);
     385          26 :         opt_param->capabilities.push_back(gr_cap);
     386          26 :         return;
     387             :     }
     388             : 
     389        1618 :     for (auto family : peer_->supported_families()) {
     390        1455 :         if (!peer_->LookupFamily(family))
     391         646 :             continue;
     392         809 :         gr_families.push_back(family);
     393         809 :         afi_flags.push_back(BgpProto::OpenMessage::Capability::GR::
     394             :                                 ForwardingStatePreservedFlag);
     395             :     }
     396             : 
     397         162 :     uint16_t time = peer_->server()->GetGracefulRestartTime();
     398             :     BgpProto::OpenMessage::Capability *gr_cap =
     399         162 :         BgpProto::OpenMessage::Capability::GR::Encode(time, restarted, true,
     400         162 :                                                       afi_flags, gr_families);
     401         162 :     opt_param->capabilities.push_back(gr_cap);
     402         214 : }
     403             : 
     404             : // Process received GR and LLGR Capabilities. Return true if the values are sane
     405             : // to proceed with further processing. Return false if not to abort any ongoing
     406             : // GR and instead trigger non-graceful closure.
     407          80 : bool BgpPeerClose::SetGRCapabilities(BgpPeerInfoData *peer_info) {
     408          80 :     BgpProto::OpenMessage::Capability::GR::Decode(&gr_params_, capabilities());
     409          80 :     BgpProto::OpenMessage::Capability::GR::GetFamilies(gr_params_,
     410             :                                                        &gr_families_);
     411             : 
     412          80 :     BgpProto::OpenMessage::Capability::LLGR::Decode(&llgr_params_,
     413          80 :                                                     capabilities());
     414          80 :     BgpProto::OpenMessage::Capability::LLGR::GetFamilies(llgr_params_,
     415             :                                                          &llgr_families_);
     416             : 
     417          80 :     if (peer_info) {
     418          80 :         peer_info->set_graceful_restart_families(gr_families_);
     419          80 :         peer_info->set_long_lived_graceful_restart_families(llgr_families_);
     420          80 :         peer_info->set_graceful_restart_time(GetGracefulRestartTime());
     421         160 :         peer_info->set_long_lived_graceful_restart_time(
     422          80 :                 GetLongLivedGracefulRestartTime());
     423          80 :         assert(!peer_info->get_name().empty());
     424          80 :         BGP_UVE_SEND(BGPPeerInfo, *peer_info);
     425             :     }
     426             : 
     427             :     // If we are not in GR Timer waiting state, then there is no case to abort
     428             :     // GR when new session is coming up.
     429          80 :     if (!IsInGRTimerWaitState())
     430          64 :         return true;
     431             : 
     432             :     // If LLGR is no longer supported, terminate GR right away. This can happen
     433             :     // due to mis-match between gr and llgr afis. For now, we expect an
     434             :     // identical set.
     435          16 :     if (IsInLlgrTimerWaitState())
     436           0 :         return IsCloseLongLivedGraceful();
     437          16 :     return IsCloseGraceful();
     438             : }
     439             : 
     440         188 : void BgpPeerClose::AddLLGRCapabilities(
     441             :         BgpProto::OpenMessage::OptParam *opt_param) {
     442         350 :     if (!peer_->server()->global_config()->gr_enable() ||
     443         162 :             !peer_->server()->GetLongLivedGracefulRestartTime()) {
     444          26 :         return;
     445             :     }
     446             : 
     447         162 :     vector<Address::Family> llgr_families;
     448        1620 :     for (auto family : peer_->supported_families()) {
     449        1458 :         if (peer_->LookupFamily(family))
     450         810 :             llgr_families.push_back(family);
     451             :     }
     452             : 
     453         162 :     uint32_t time = peer_->server()->GetLongLivedGracefulRestartTime();
     454         162 :     uint8_t afi_flags =
     455             :         BgpProto::OpenMessage::Capability::LLGR::ForwardingStatePreservedFlag;
     456             :     BgpProto::OpenMessage::Capability *llgr_cap =
     457         162 :         BgpProto::OpenMessage::Capability::LLGR::Encode(time, afi_flags,
     458         162 :                                                         llgr_families);
     459         162 :     opt_param->capabilities.push_back(llgr_cap);
     460         162 : }
     461             : 
     462           0 : void BgpPeerClose::FillNeighborInfo(BgpNeighborResp *bnr) const {
     463           0 :     bnr->set_graceful_restart_address_families(gr_families_);
     464           0 :     bnr->set_long_lived_graceful_restart_address_families(llgr_families_);
     465           0 :     bnr->set_graceful_restart_time(GetGracefulRestartTime());
     466           0 :     bnr->set_long_lived_graceful_restart_time(
     467           0 :             GetLongLivedGracefulRestartTime());
     468           0 : }

Generated by: LCOV version 1.14