LCOV - code coverage report
Current view: top level - bgp/routing-instance - peer_manager.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 55 133 41.4 %
Date: 2026-11-16 02:06:09 Functions: 9 17 52.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 "bgp/routing-instance/peer_manager.h"
       7             : 
       8             : #include "base/task_annotations.h"
       9             : #include "base/address_util.h"
      10             : #include "bgp/bgp_factory.h"
      11             : #include "bgp/bgp_log.h"
      12             : #include "bgp/bgp_peer.h"
      13             : #include "bgp/bgp_peer_types.h"
      14             : #include "bgp/bgp_server.h"
      15             : #include "bgp/routing-instance/routing_instance.h"
      16             : #include "bgp/routing-instance/routing_instance_log.h"
      17             : 
      18             : struct BgpSandeshContext;
      19             : 
      20             : using std::make_pair;
      21             : using std::string;
      22             : using std::vector;
      23             : 
      24             : //
      25             : // Find or create a BgpPeer for the given BgpNeighborConfig.
      26             : // Return NULL if the peer already exists and is being deleted. The BgpPeer
      27             : // will eventually get created in this case via PeerResurrect.
      28             : //
      29          32 : BgpPeer *PeerManager::PeerLocate(BgpServer *server,
      30             :     const BgpNeighborConfig *config) {
      31          32 :     BgpPeerKey key(config);
      32             : 
      33          32 :     BgpPeerNameMap::iterator loc = peers_by_name_.find(config->name());
      34          32 :     if (loc != peers_by_name_.end()) {
      35           0 :         if (loc->second->IsDeleted())
      36           0 :             return NULL;
      37           0 :         RemovePeerByKey(loc->second->peer_key(), loc->second);
      38           0 :         InsertPeerByKey(key, loc->second);
      39           0 :         return loc->second;
      40             :     }
      41             : 
      42             :     BgpPeer *peer =
      43          32 :         BgpStaticObjectFactory::Create<BgpPeer>(server, instance(), config);
      44          32 :     peer->Initialize();
      45          32 :     InsertPeerByKey(key, peer);
      46          32 :     InsertPeerByName(config->name(), peer);
      47          32 :     RoutingInstanceInfo info = instance_->GetDataCollection("Add");
      48          32 :     info.set_peer(peer->ToString());
      49          32 :     ROUTING_INSTANCE_COLLECTOR_INFO(info);
      50             : 
      51          32 :     BGP_LOG_PEER(Config, peer, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL,
      52             :                  BGP_PEER_DIR_NA, "Created peer");
      53          32 :     RTINSTANCE_LOG_PEER(Create, instance_, peer,
      54             :         SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL);
      55          32 :     return peer;
      56          32 : }
      57             : 
      58             : //
      59             : // Resurrect the BgpPeer with given name if we have configuration for it.
      60             : // Also insert it into the BgpServer's EndpointPeerList - in case it is
      61             : // a BGPaaS peer. This needs to happen here since we would not have been
      62             : // able to do it from BgpServer::ConfigUpdater::ProcessNeighborConfig since
      63             : // old incarnation of the BgpPeer still existed at that point.
      64             : //
      65          32 : void PeerManager::PeerResurrect(string name) {
      66          32 :     CHECK_CONCURRENCY("bgp::Config");
      67             : 
      68          32 :     if (instance_->deleted())
      69          32 :         return;
      70             : 
      71             :     const BgpConfigManager *config_manager =
      72           0 :             instance_->manager()->server()->config_manager();
      73             :     const BgpNeighborConfig *config =
      74           0 :             config_manager->FindNeighbor(instance_->name(), name);
      75           0 :     if (!config)
      76           0 :         return;
      77             : 
      78           0 :     BgpPeer *peer = PeerLocate(server(), config);
      79           0 :     assert(peer);
      80           0 :     server()->InsertPeer(peer->endpoint(), peer);
      81             : }
      82             : 
      83             : //
      84             : // Delete the BgpPeer corresponding to the given BgpNeighborConfig.
      85             : //
      86           0 : BgpPeer *PeerManager::TriggerPeerDeletion(const BgpNeighborConfig *config) {
      87           0 :     CHECK_CONCURRENCY("bgp::Config");
      88             : 
      89           0 :     BgpPeerNameMap::iterator loc = peers_by_name_.find(config->name());
      90           0 :     if (loc == peers_by_name_.end())
      91           0 :         return NULL;
      92             : 
      93           0 :     BgpPeer *peer = loc->second;
      94           0 :     peer->ManagedDelete();
      95             : 
      96           0 :     RoutingInstanceInfo info = instance_->GetDataCollection("Delete");
      97           0 :     info.set_peer(peer->ToString());
      98           0 :     ROUTING_INSTANCE_COLLECTOR_INFO(info);
      99             : 
     100           0 :     RTINSTANCE_LOG_PEER(Delete, instance_, peer,
     101             :         SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL);
     102             : 
     103             :     // Configuration is deleted by the config manager (parser)
     104             :     // Do not hold reference to it any more
     105           0 :     peer->ClearConfig();
     106           0 :     return peer;
     107           0 : }
     108             : 
     109             : //
     110             : // Concurrency: Called from bgp config task
     111             : //
     112             : // Complete the deletion process of a peer.  Remove it from BgpPeerKeyMap and
     113             : // BgpPeerNameMap.
     114             : //
     115          32 : void PeerManager::DestroyIPeer(IPeer *ipeer) {
     116          32 :     CHECK_CONCURRENCY("bgp::Config");
     117             : 
     118          32 :     BgpPeer *peer = static_cast<BgpPeer *>(ipeer);
     119          32 :     string peer_name = peer->peer_name();
     120          32 :     BGP_LOG_PEER(Config, peer, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL,
     121             :                  BGP_PEER_DIR_NA, "Destroyed peer");
     122          32 :     RTINSTANCE_LOG_PEER(Destroy, instance_, peer,
     123             :         SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL);
     124          32 :     RemovePeerByKey(peer->peer_key(), peer);
     125          32 :     RemovePeerByName(peer->peer_name(), peer);
     126          32 :     delete peer;
     127             : 
     128          32 :     PeerResurrect(peer_name);
     129          32 : }
     130             : 
     131             : //
     132             : // Insert the BgpPeer info the BgpPeerKeyMap.
     133             : //
     134          32 : void PeerManager::InsertPeerByKey(const BgpPeerKey key, BgpPeer *peer) {
     135          32 :     peers_by_key_.insert(make_pair(key, peer));
     136          32 : }
     137             : 
     138             : //
     139             : // Remove the BgpPeer from the BgpPeerKeyMap.  There may be more than
     140             : // one BgpPeer with the same key, so we need to find the right one.
     141             : //
     142          32 : void PeerManager::RemovePeerByKey(const BgpPeerKey key, BgpPeer *peer) {
     143          32 :     for (BgpPeerKeyMap::iterator loc = peers_by_key_.find(key);
     144          32 :          loc != peers_by_key_.end() && loc->first == key; ++loc) {
     145          32 :         if (loc->second == peer) {
     146          32 :             peers_by_key_.erase(loc);
     147          32 :             return;
     148             :         }
     149             :     }
     150           0 :     assert(false);
     151             : }
     152             : 
     153             : //
     154             : // Insert the BgpPeer info the BgpPeerNameMap.
     155             : //
     156          32 : void PeerManager::InsertPeerByName(const string name, BgpPeer *peer) {
     157          32 :     peers_by_name_.insert(make_pair(name, peer));
     158          32 : }
     159             : 
     160             : //
     161             : // Remove the BgpPeer from the BgpPeerNameMap.
     162             : //
     163          32 : void PeerManager::RemovePeerByName(const string name, BgpPeer *peer) {
     164          32 :     BgpPeerNameMap::iterator loc = peers_by_name_.find(name);
     165          32 :     assert(loc != peers_by_name_.end() && loc->second == peer);
     166          32 :     peers_by_name_.erase(loc);
     167          32 : }
     168             : 
     169           0 : BgpPeer *PeerManager::PeerFind(string ip_address) const {
     170           0 :     if (ip_address.empty())
     171           0 :         return NULL;
     172             : 
     173           0 :     boost::system::error_code ec;
     174           0 :     boost::asio::ip::tcp::endpoint endpoint;
     175           0 :     endpoint.address(AddressFromString(ip_address, &ec));
     176           0 :     if (ec)
     177           0 :         return NULL;
     178             : 
     179           0 :     return PeerLookup(endpoint);
     180             : }
     181             : 
     182           0 : BgpPeer *PeerManager::PeerLookup(string name) const {
     183           0 :     BgpPeerNameMap::const_iterator loc = peers_by_name_.find(name);
     184           0 :     return (loc != peers_by_name_.end() ? loc->second : NULL);
     185             : }
     186             : 
     187           0 : size_t PeerManager::GetNeighborCount(string up_or_down) {
     188           0 :     size_t count = 0;
     189           0 :     BgpPeerNameMap::iterator iter;
     190             : 
     191           0 :     for (iter = peers_by_name_.begin(); iter != peers_by_name_.end(); iter++) {
     192           0 :         BgpPeer *peer = iter->second;
     193           0 :         if (boost::iequals(up_or_down, "up") && !peer->IsReady())
     194           0 :             continue;
     195           0 :         if (boost::iequals(up_or_down, "down") && peer->IsReady())
     196           0 :             continue;
     197           0 :         count += 1;
     198             :     }
     199             : 
     200           0 :     return count;
     201             : }
     202             : 
     203          30 : void PeerManager::ClearAllPeers() {
     204          30 :     BgpPeerNameMap::iterator iter;
     205             : 
     206          46 :     for (iter = peers_by_name_.begin(); iter != peers_by_name_.end(); iter++) {
     207          16 :         BgpPeer *peer = iter->second;
     208          16 :         peer->Clear(BgpProto::Notification::OtherConfigChange);
     209             :     }
     210          30 : }
     211             : 
     212           0 : void PeerManager::ClearAllInternalPeers() {
     213           0 :     BgpPeerNameMap::iterator iter;
     214             : 
     215           0 :     for (iter = peers_by_name_.begin(); iter != peers_by_name_.end(); iter++) {
     216           0 :         BgpPeer *peer = iter->second;
     217           0 :         if (peer->PeerType() ==  BgpProto::IBGP)
     218           0 :             peer->Clear(BgpProto::Notification::OtherConfigChange);
     219             :     }
     220           0 : }
     221             : 
     222             : //
     223             : // Concurrency: Called from state machine thread
     224             : //
     225           0 : BgpPeer *PeerManager::PeerLookup(TcpSession::Endpoint remote_endpoint) const {
     226           0 :     BgpPeer    *peer = NULL;
     227           0 :     BgpPeerKey  peer_key;
     228             : 
     229             :     // Bail if the instance is undergoing deletion.
     230           0 :     if (instance_->deleted())
     231           0 :         return NULL;
     232             : 
     233           0 :     peer_key.endpoint.address(remote_endpoint.address());
     234             : 
     235             :     // Do a partial match, as we do not know the peer's port yet.
     236           0 :     BgpPeerKeyMap::const_iterator loc = peers_by_key_.lower_bound(peer_key);
     237           0 :     while (loc != peers_by_key_.end()) {
     238             :         // Check if the address does indeed match as we are doing a partial
     239             :         // match here
     240           0 :         if (loc->second->peer_key().endpoint.address() !=
     241           0 :             peer_key.endpoint.address()) {
     242           0 :             break;
     243             :         }
     244             : 
     245             :         // This peer certainly matches with the IP address. If we do not find
     246             :         // an exact match with the peer-id, then just use this.
     247           0 :         peer = loc->second;
     248           0 :         break;
     249             :     }
     250             : 
     251           0 :     return peer;
     252             : }
     253             : 
     254           0 : const BgpPeer *PeerManager::NextPeer(const BgpPeerKey &peer_key) const {
     255             :     // Do a partial match
     256           0 :     BgpPeerKeyMap::const_iterator loc = peers_by_key_.upper_bound(peer_key);
     257           0 :     if (loc != peers_by_key_.end()) {
     258           0 :         return loc->second;
     259             :     }
     260             : 
     261           0 :     return NULL;
     262             : }
     263             : 
     264           0 : const string &PeerManager::name() const {
     265           0 :     return instance_->name();
     266             : }
     267             : 
     268         148 : BgpServer *PeerManager::server() const {
     269         148 :     return instance_->server();
     270             : }

Generated by: LCOV version 1.14