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: 176 235 74.9 %
Date: 2026-11-23 02:04:52 Functions: 39 42 92.9 %
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         102 : BgpPeerClose::BgpPeerClose(BgpPeer *peer)
      27         102 :         : peer_(peer), flap_count_(0) {
      28         102 : }
      29             : 
      30         102 : BgpPeerClose::~BgpPeerClose() {
      31         102 : }
      32             : 
      33        7417 : PeerCloseManager *BgpPeerClose::GetManager() const {
      34        7417 :     return peer_->close_manager();
      35             : }
      36             : 
      37         404 : void BgpPeerClose::CustomClose() {
      38         404 :     return peer_->CustomClose();
      39             : }
      40             : 
      41         398 : void BgpPeerClose::GracefulRestartStale() {
      42         398 :     negotiated_families_ = peer_->negotiated_families();
      43         398 : }
      44             : 
      45          68 : void BgpPeerClose::LongLivedGracefulRestartStale() {
      46          68 : }
      47             : 
      48         396 : void BgpPeerClose::GracefulRestartSweep() {
      49         396 :     negotiated_families_.clear();
      50         396 : }
      51             : 
      52         928 : bool BgpPeerClose::IsReady() const {
      53         928 :     return peer_->IsReady();
      54             : }
      55             : 
      56       63991 : IPeer *BgpPeerClose::peer() const {
      57       63991 :     return peer_;
      58             : }
      59             : 
      60             : // Return the time to wait for, in seconds to exit GR_TIMER state.
      61         896 : int BgpPeerClose::GetGracefulRestartTime() const {
      62         896 :     return gr_params_.time;
      63             : }
      64             : 
      65             : // Return the time to wait for, in seconds to exit LLGR_TIMER state.
      66         566 : int BgpPeerClose::GetLongLivedGracefulRestartTime() const {
      67         566 :     return llgr_params_.time;
      68             : }
      69             : 
      70           0 : void BgpPeerClose::ReceiveEndOfRIB(Address::Family family) {
      71           0 :     peer_->ReceiveEndOfRIB(family, 0);
      72           0 : }
      73             : 
      74        8916 : const char *BgpPeerClose::GetTaskName() const {
      75        8916 :     return "bgp::Config";
      76             : }
      77             : 
      78         102 : int BgpPeerClose::GetTaskInstance() const {
      79         102 :     return 0;
      80             : }
      81             : 
      82         962 : void BgpPeerClose::MembershipRequestCallbackComplete() {
      83         962 :     CHECK_CONCURRENCY(GetTaskName());
      84         962 : }
      85             : 
      86        1692 : bool BgpPeerClose::IsGRHelperModeEnabled() const {
      87        1692 :     return peer_->server()->IsGRHelperModeEnabled();
      88             : }
      89             : 
      90        2687 : const std::vector<std::string> &BgpPeerClose::PeerNegotiatedFamilies() const {
      91        2687 :     return peer_->negotiated_families();
      92             : }
      93             : 
      94        1692 : bool BgpPeerClose::IsPeerDeleted() const {
      95        1692 :     return peer_->IsDeleted();
      96             : }
      97             : 
      98        1692 : bool BgpPeerClose::IsPeerAdminDown() const {
      99        1692 :     return peer_->IsAdminDown();
     100             : }
     101             : 
     102        1692 : bool BgpPeerClose::IsServerDeleted() const {
     103        1692 :     return peer_->server()->IsDeleted();
     104             : }
     105             : 
     106        1692 : bool BgpPeerClose::IsServerAdminDown() const {
     107        1692 :     return peer_->server()->admin_down();
     108             : }
     109             : 
     110         659 : bool BgpPeerClose::IsInGRTimerWaitState() const {
     111         659 :     return GetManager()->IsInGRTimerWaitState();
     112             : }
     113             : 
     114         398 : bool BgpPeerClose::IsInLlgrTimerWaitState() const {
     115         398 :     return GetManager()->IsInLlgrTimerWaitState();
     116             : }
     117             : 
     118        2687 : const std::vector<std::string> &BgpPeerClose::negotiated_families() const {
     119        2687 :     return negotiated_families_;
     120             : }
     121             : 
     122             : const std::vector<BgpProto::OpenMessage::Capability *> &
     123         996 : BgpPeerClose::capabilities() const {
     124         996 :     return peer_->capabilities();
     125             : }
     126             : 
     127        5555 : void BgpPeerClose::UpdateRouteStats(Address::Family family,
     128             :                                     const BgpPath *old_path,
     129             :                                     uint32_t path_flags) const {
     130        5555 :     GetManager()->UpdateRouteStats(family, old_path, path_flags);
     131        5552 : }
     132             : 
     133         804 : 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         804 :     if (!graceful || flap_count_ != peer_->total_flap_count()) {
     138         563 :         if (flap_count_ != peer_->total_flap_count()) {
     139         498 :             flap_count_++;
     140         498 :             assert(peer_->total_flap_count() == flap_count_);
     141             :         }
     142         563 :         GetManager()->Close(graceful);
     143         563 :         return;
     144             :     }
     145             : 
     146             :     // Ignore if close is already in progress.
     147         241 :     if (GetManager()->IsCloseInProgress() && !IsInGRTimerWaitState())
     148           2 :         return;
     149             : 
     150         239 :     if (peer_->IsDeleted()) {
     151           0 :         peer_->RetryDelete();
     152             :     } else {
     153         239 :         CustomClose();
     154         239 :         CloseComplete();
     155             :     }
     156             : }
     157             : 
     158         165 : void BgpPeerClose::Delete() {
     159         165 :     gr_params_.Initialize();
     160         165 :     llgr_params_.Initialize();
     161         165 :     gr_families_.clear();
     162         165 :     llgr_families_.clear();
     163         165 :     negotiated_families_.clear();
     164         165 :     if (peer_->IsDeleted()) {
     165         102 :         peer_->RetryDelete();
     166             :     } else {
     167          63 :         CloseComplete();
     168             :     }
     169         165 : }
     170             : 
     171        1692 : bool BgpPeerClose::IsGRReady() const {
     172             :     // Check if GR Helper mode is disabled.
     173        1692 :     if (!IsGRHelperModeEnabled()) {
     174           0 :         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           0 :         return false;
     178             :     }
     179             : 
     180             :     // Check if GR is supported by the peer.
     181        1692 :     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        1692 :     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        2687 :     if (!negotiated_families().empty() &&
     200         995 :             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        1692 :     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        4096 :     for (auto family : gr_params_.families) {
     219             :         // Check if forwarding state was preserved during restart.
     220        2404 :         if (family.forwarding_state_preserved())
     221        2404 :             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        1692 :     return true;
     237             : }
     238             : 
     239             : // If the peer is deleted or administratively held down, do not attempt
     240             : // graceful restart
     241        1692 : bool BgpPeerClose::IsCloseGraceful() const {
     242        1692 :     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        1692 :     if (IsPeerAdminDown()) {
     250           0 :         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           0 :         return false;
     254             :     }
     255             : 
     256        1692 :     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        1692 :     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        1692 :     if (!IsGRReady())
     271           0 :         return false;
     272        1692 :     return true;
     273             : }
     274             : 
     275             : // LLGR families should be identical to GR families.
     276         828 : 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         829 :         (Address::FamilyToString(Address::MVPN));
     281             : 
     282         828 :     if (gr_families_ == llgr_families_)
     283         828 :         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         828 : bool BgpPeerClose::IsCloseLongLivedGraceful() const {
     300         828 :     if (!IsCloseGraceful())
     301           0 :         return false;
     302         828 :     return BgpPeerClose::IsCloseLongLivedGracefulInternal();
     303             : }
     304             : 
     305         828 : bool BgpPeerClose::IsCloseLongLivedGracefulInternal() const {
     306         828 :     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         828 :     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         828 :     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        2180 :     for (auto family : llgr_params_.families) {
     328             :         // Ignore forwarding-state preservation check for certain families.
     329             :         Address::Family addr_family =
     330        1352 :             BgpAf::AfiSafiToFamily(family.afi, family.safi);
     331        1352 :         if (addr_family == Address::ERMVPN || addr_family == Address::MVPN)
     332           0 :             continue;
     333             : 
     334        1352 :         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         828 :     return true;
     344             : }
     345             : 
     346         736 : void BgpPeerClose::RestartStateMachine() {
     347         736 :     peer_->state_machine()->Initialize();
     348         736 : }
     349             : 
     350             : // Close process for this peer is complete. Restart the state machine and
     351             : // attempt to bring up session with the neighbor
     352         768 : void BgpPeerClose::CloseComplete() {
     353         768 :     if (!peer_->IsDeleted() && !peer_->IsAdminDown())
     354         768 :         RestartStateMachine();
     355         768 : }
     356             : 
     357         398 : void BgpPeerClose::GetGracefulRestartFamilies(Families *families) const {
     358         398 :     families->clear();
     359         924 :     for (const auto& family : gr_families_) {
     360         526 :         families->insert(Address::FamilyFromString(family));
     361             :     }
     362         398 : }
     363             : 
     364          68 : void BgpPeerClose::GetLongLivedGracefulRestartFamilies(Families *families) const {
     365          68 :     families->clear();
     366         204 :     for (const auto& family : llgr_families_) {
     367         136 :         families->insert(Address::FamilyFromString(family));
     368             :     }
     369          68 : }
     370             : 
     371        1055 : void BgpPeerClose::AddGRCapabilities(
     372             :         BgpProto::OpenMessage::OptParam *opt_param) {
     373        1055 :     vector<Address::Family> gr_families;
     374        1055 :     vector<uint8_t> afi_flags;
     375             : 
     376             :     // Indicate to the Peer if we are in restarting phase.
     377        1055 :     bool restarted = peer_->IsServerStartingUp();
     378             : 
     379             :     // Indicate EOR support by default.
     380        1055 :     if (!peer_->server()->global_config()->gr_enable()) {
     381             :         BgpProto::OpenMessage::Capability *gr_cap =
     382           0 :             BgpProto::OpenMessage::Capability::GR::Encode(0, restarted, false,
     383             :                                                           afi_flags,
     384           0 :                                                           gr_families);
     385           0 :         opt_param->capabilities.push_back(gr_cap);
     386           0 :         return;
     387             :     }
     388             : 
     389       10550 :     for (auto family : peer_->supported_families()) {
     390        9495 :         if (!peer_->LookupFamily(family))
     391        7779 :             continue;
     392        1716 :         gr_families.push_back(family);
     393        1716 :         afi_flags.push_back(BgpProto::OpenMessage::Capability::GR::
     394             :                                 ForwardingStatePreservedFlag);
     395             :     }
     396             : 
     397        1055 :     uint16_t time = peer_->server()->GetGracefulRestartTime();
     398             :     BgpProto::OpenMessage::Capability *gr_cap =
     399        1055 :         BgpProto::OpenMessage::Capability::GR::Encode(time, restarted, true,
     400        1055 :                                                       afi_flags, gr_families);
     401        1055 :     opt_param->capabilities.push_back(gr_cap);
     402        1055 : }
     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         498 : bool BgpPeerClose::SetGRCapabilities(BgpPeerInfoData *peer_info) {
     408         498 :     BgpProto::OpenMessage::Capability::GR::Decode(&gr_params_, capabilities());
     409         498 :     BgpProto::OpenMessage::Capability::GR::GetFamilies(gr_params_,
     410             :                                                        &gr_families_);
     411             : 
     412         498 :     BgpProto::OpenMessage::Capability::LLGR::Decode(&llgr_params_,
     413         498 :                                                     capabilities());
     414         498 :     BgpProto::OpenMessage::Capability::LLGR::GetFamilies(llgr_params_,
     415             :                                                          &llgr_families_);
     416             : 
     417         498 :     if (peer_info) {
     418         498 :         peer_info->set_graceful_restart_families(gr_families_);
     419         498 :         peer_info->set_long_lived_graceful_restart_families(llgr_families_);
     420         498 :         peer_info->set_graceful_restart_time(GetGracefulRestartTime());
     421         996 :         peer_info->set_long_lived_graceful_restart_time(
     422         498 :                 GetLongLivedGracefulRestartTime());
     423         498 :         assert(!peer_info->get_name().empty());
     424         498 :         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         498 :     if (!IsInGRTimerWaitState())
     430         100 :         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         398 :     if (IsInLlgrTimerWaitState())
     436          68 :         return IsCloseLongLivedGraceful();
     437         330 :     return IsCloseGraceful();
     438             : }
     439             : 
     440        1055 : void BgpPeerClose::AddLLGRCapabilities(
     441             :         BgpProto::OpenMessage::OptParam *opt_param) {
     442        2110 :     if (!peer_->server()->global_config()->gr_enable() ||
     443        1055 :             !peer_->server()->GetLongLivedGracefulRestartTime()) {
     444           0 :         return;
     445             :     }
     446             : 
     447        1055 :     vector<Address::Family> llgr_families;
     448       10550 :     for (auto family : peer_->supported_families()) {
     449        9495 :         if (peer_->LookupFamily(family))
     450        1716 :             llgr_families.push_back(family);
     451             :     }
     452             : 
     453        1055 :     uint32_t time = peer_->server()->GetLongLivedGracefulRestartTime();
     454        1055 :     uint8_t afi_flags =
     455             :         BgpProto::OpenMessage::Capability::LLGR::ForwardingStatePreservedFlag;
     456             :     BgpProto::OpenMessage::Capability *llgr_cap =
     457        1055 :         BgpProto::OpenMessage::Capability::LLGR::Encode(time, afi_flags,
     458        1055 :                                                         llgr_families);
     459        1055 :     opt_param->capabilities.push_back(llgr_cap);
     460        1055 : }
     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