LCOV - code coverage report
Current view: top level - bgp - bgp_session_manager.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 73 133 54.9 %
Date: 2026-09-21 02:11:39 Functions: 12 18 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "bgp/bgp_session_manager.h"
       6             : 
       7             : #include "base/bgp_as_service_utils.h"
       8             : #include "base/task_annotations.h"
       9             : #include "base/address_util.h"
      10             : #include "bgp/bgp_log.h"
      11             : #include "bgp/bgp_peer.h"
      12             : #include "bgp/bgp_server.h"
      13             : #include "bgp/bgp_session.h"
      14             : #include "bgp/routing-instance/peer_manager.h"
      15             : #include "bgp/routing-instance/routing_instance.h"
      16             : 
      17             : using namespace boost::asio::ip;
      18             : 
      19             : static const std::string kDefaultBgpSessionIp = "0.0.0.0";
      20             : 
      21          32 : BgpSessionManager::BgpSessionManager(EventManager *evm, BgpServer *server)
      22             :     : TcpServer(evm),
      23          32 :       server_(server),
      24          32 :       session_queue_(
      25             :           TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
      26             :           boost::bind(&BgpSessionManager::ProcessSession, this, _1)),
      27          32 :       write_ready_queue_(
      28             :           TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
      29          64 :           boost::bind(&BgpSessionManager::ProcessWriteReady, this, _1)) {
      30             : 
      31          32 :       boost::system::error_code ec;
      32          32 :       session_ip_ = AddressFromString(kDefaultBgpSessionIp, &ec);
      33          32 : }
      34             : 
      35          64 : BgpSessionManager::~BgpSessionManager() {
      36          64 : }
      37             : 
      38             : //
      39             : // Start listening at the given port.
      40             : //
      41          64 : bool BgpSessionManager::Initialize(unsigned short port) {
      42             :     // Changes to already bound bgp listen port number is not supported.
      43          64 :     Endpoint local_endpoint = LocalEndpoint();
      44          64 :     if (local_endpoint != Endpoint()) {
      45          32 :         Endpoint new_endpoint(session_ip_, port);
      46          32 :         if (local_endpoint != new_endpoint) {
      47           0 :             BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
      48             :                 "Cannot change already bound " << local_endpoint <<
      49             :                 " to " << new_endpoint);
      50           0 :             return false;
      51             :         }
      52          32 :         return true;
      53             :     }
      54             : 
      55          32 :     LOG(DEBUG, "Starting Bgp Server at " << session_ip_ << ":" << port);
      56          32 :     bool r = TcpServer::Initialize(port, session_ip_);
      57          32 :     if (!r) {
      58           0 :         BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
      59             :                             "Cannot bind to bgp/tcp server ip:port " <<
      60             :                             session_ip_ << ":" << port);
      61             :     }
      62          32 :     return r;
      63             : }
      64             : 
      65             : //
      66             : // Start listening at the given ip:port.
      67             : //
      68           0 : bool BgpSessionManager::Initialize(unsigned short port, const IpAddress& ip) {
      69           0 :     session_ip_ = ip;
      70           0 :     return true;
      71             : }
      72             : 
      73             : //
      74             : // Called from BgpServer::DeleteActor's Shutdown method.
      75             : // Shutdown the TcpServer.
      76             : // Register an exit callback to the WorkQueues so that we can ask BgpServer
      77             : // to retry deletion when a WorkQueue becomes empty.
      78             : //
      79          32 : void BgpSessionManager::Shutdown() {
      80          32 :     CHECK_CONCURRENCY("bgp::Config");
      81          32 :     TcpServer::Shutdown();
      82          32 :     session_queue_.SetExitCallback(
      83             :         boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
      84          32 :     write_ready_queue_.SetExitCallback(
      85             :         boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
      86          32 : }
      87             : 
      88             : //
      89             : // Called when the BgpServer is being destroyed.
      90             : //
      91             : // The WorkQueues need to be shutdown as the last step to ensure that all
      92             : // entries get deleted. Note that there's no need to call DeleteSession on
      93             : // the sessions in the WorkQueues since ClearSessions does the same thing.
      94             : //
      95          32 : void BgpSessionManager::Terminate() {
      96          32 :     CHECK_CONCURRENCY("bgp::Config");
      97          32 :     server_ = NULL;
      98          32 :     ClearSessions();
      99          32 :     session_queue_.Shutdown();
     100          32 :     write_ready_queue_.Shutdown();
     101          32 : }
     102             : 
     103             : //
     104             : // Return true if all WorkQueues are empty.
     105             : //
     106          32 : bool BgpSessionManager::MayDelete() const {
     107          32 :     if (!session_queue_.IsQueueEmpty())
     108           0 :         return false;
     109          32 :     if (!write_ready_queue_.IsQueueEmpty())
     110           0 :         return false;
     111          32 :     return true;
     112             : }
     113             : 
     114             : //
     115             : // Add a BgpSession to the write ready WorkQueue.
     116             : // Take a reference to make sure that BgpSession doesn't get deleted before
     117             : // it's processed.
     118             : //
     119           0 : void BgpSessionManager::EnqueueWriteReady(BgpSession *session) {
     120           0 :     if (!server_ || server_->IsDeleted())
     121           0 :         return;
     122           0 :     write_ready_queue_.Enqueue(TcpSessionPtr(session));
     123             : }
     124             : 
     125             : //
     126             : // Handler for BgpSessions that are dequeued from the write ready WorkQueue.
     127             : //
     128             : // The BgpServer does not get destroyed if the WorkQueue is non-empty.
     129             : //
     130           0 : bool BgpSessionManager::ProcessWriteReady(TcpSessionPtr tcp_session) {
     131           0 :     BgpSession *session = static_cast<BgpSession *>(tcp_session.get());
     132           0 :     session->ProcessWriteReady();
     133           0 :     return true;
     134             : }
     135             : 
     136             : //
     137             : // Search for a matching BgpPeer.
     138             : // First look for a matching address in the master instance.
     139             : // Then look for a matching port in the EndpointPeerList in BgpServer.
     140             : //
     141          32 : BgpPeer *BgpSessionManager::FindPeer(Endpoint remote) {
     142          32 :     BgpPeer *peer = NULL;
     143             :     const RoutingInstance *instance =
     144          32 :         server_->routing_instance_mgr()->GetDefaultRoutingInstance();
     145          32 :     if (instance && !instance->deleted()) {
     146          32 :         peer = instance->peer_manager()->PeerLookup(remote);
     147             :     }
     148          32 :     if (!peer) {
     149           0 :         uint16_t port = BGPaaSUtils::DecodeBgpaasServicePort(remote.port(),
     150           0 :             server_->global_config()->bgpaas_port_start(),
     151           0 :             server_->global_config()->bgpaas_port_end()).first;
     152           0 :         peer = server_->FindPeer(TcpSession::Endpoint(Ip4Address(), port));
     153             :     }
     154          32 :     return peer;
     155             : }
     156             : 
     157             : //
     158             : // Create an active BgpSession.
     159             : //
     160          32 : TcpSession *BgpSessionManager::CreateSession() {
     161          32 :     TcpSession *session = TcpServer::CreateSession();
     162          32 :     Socket *socket = session->socket();
     163             : 
     164          32 :     boost::system::error_code ec;
     165          32 :     socket->open(boost::asio::ip::tcp::v4(), ec);
     166          32 :     if (ec || (ec = session->SetSocketOptions()) || socket_open_failure()) {
     167           0 :         BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
     168             :             "Failed to open bgp socket, error: " << ec.message());
     169           0 :         DeleteSession(session);
     170           0 :         return NULL;
     171             :     }
     172             : 
     173          32 :     if (session_ip_ != address::from_string(kDefaultBgpSessionIp, ec))  {
     174           0 :         tcp::endpoint localaddr(session_ip_, 0);
     175           0 :         socket->bind(localaddr, ec);
     176           0 :         if (ec) {
     177           0 :             BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
     178             :                 "Failed to bind bgp socket to:" << session_ip_ <<
     179             :                 ", error: " << ec.message());
     180           0 :             DeleteSession(session);
     181           0 :             return NULL;
     182             :         }
     183             :     }
     184          32 :     return session;
     185             : }
     186             : 
     187             : //
     188             : // Allocate a new BgpSession.
     189             : // Called via CreateSession or when the TcpServer accepts a passive session.
     190             : //
     191          64 : TcpSession *BgpSessionManager::AllocSession(Socket *socket) {
     192          64 :     TcpSession *session = new BgpSession(this, socket);
     193          63 :     return session;
     194             : }
     195             : 
     196             : //
     197             : // Accept incoming BgpSession and add to session WorkQueue for processing.
     198             : // This ensures that we don't try to access the BgpServer data structures
     199             : // from the IO thread while they are being modified from bgp::Config task.
     200             : //
     201             : // Stop accepting sessions after delete of the BgpServer gets triggered.
     202             : // Note that the BgpServer, and hence the BgpSessionManager will not get
     203             : // destroyed if the WorkQueue is non-empty.
     204             : //
     205          32 : bool BgpSessionManager::AcceptSession(TcpSession *tcp_session) {
     206          32 :     if (!server_ || server_->IsDeleted())
     207           0 :         return false;
     208          32 :     BgpSession *session = dynamic_cast<BgpSession *>(tcp_session);
     209          32 :     session->set_read_on_connect(false);
     210          32 :     session_queue_.Enqueue(session);
     211          32 :     return true;
     212             : }
     213             : 
     214             : //
     215             : // Handler for BgpSessions that are dequeued from the session WorkQueue.
     216             : //
     217             : // The BgpServer does not get destroyed if the WorkQueue is non-empty.
     218             : //
     219          32 : bool BgpSessionManager::ProcessSession(BgpSession *session) {
     220          32 :     CHECK_CONCURRENCY("bgp::Config");
     221             : 
     222          32 :     BgpPeer *peer = FindPeer(session->remote_endpoint());
     223             : 
     224             :     // Ignore if server is being deleted.
     225          32 :     if (!server_ || server_->IsDeleted()) {
     226           0 :         session->SendNotification(BgpProto::Notification::Cease,
     227             :                                   BgpProto::Notification::PeerDeconfigured);
     228           0 :         DeleteSession(session);
     229           0 :         return true;
     230             :     }
     231             : 
     232             :     // Ignore if this server is being held administratively down.
     233          32 :     if (server_->admin_down()) {
     234           0 :         session->SendNotification(BgpProto::Notification::Cease,
     235             :                                   BgpProto::Notification::AdminShutdown);
     236           0 :         DeleteSession(session);
     237           0 :         return true;
     238             :     }
     239             : 
     240             :     // Ignore if this peer is not configured or is being deleted.
     241          32 :     if (peer == NULL || peer->deleter()->IsDeleted()) {
     242           0 :         session->SendNotification(BgpProto::Notification::Cease,
     243             :                                   BgpProto::Notification::PeerDeconfigured);
     244           0 :         BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_TRACE,
     245             :                             "Remote end-point not found");
     246           0 :         DeleteSession(session);
     247           0 :         return true;
     248             :     }
     249             : 
     250             :     // Ignore if this peer is being held administratively down.
     251          32 :     if (peer->IsAdminDown()) {
     252           0 :         session->SendNotification(BgpProto::Notification::Cease,
     253             :                                   BgpProto::Notification::AdminShutdown);
     254           0 :         DeleteSession(session);
     255           0 :         return true;
     256             :     }
     257             : 
     258             :     // Ignore if the peer's prefix limit idle timer is running.
     259          32 :     if (peer->PrefixLimitIdleTimerRunning()) {
     260           0 :         session->SendNotification(BgpProto::Notification::Cease,
     261             :                                   BgpProto::Notification::MaxPrefixes);
     262           0 :         DeleteSession(session);
     263           0 :         return true;
     264             :     }
     265             : 
     266             :     // Ignore if this peer is being closed.
     267          32 :     if (peer->IsCloseInProgress()) {
     268           0 :         session->SendNotification(BgpProto::Notification::Cease,
     269             :                                   BgpProto::Notification::ConnectionRejected);
     270           0 :         DeleteSession(session);
     271           0 :         return true;
     272             :     }
     273             : 
     274          32 :     if (!peer->ProcessSession()) {
     275           0 :         session->SendNotification(BgpProto::Notification::Cease,
     276             :                                   BgpProto::Notification::ConnectionRejected);
     277           0 :         DeleteSession(session);
     278           0 :         return true;
     279             :     }
     280             : 
     281          32 :     peer->AcceptSession(session);
     282          32 :     return true;
     283             : }
     284             : 
     285             : //
     286             : // Exit callback for the session and write ready WorkQueues.
     287             : //
     288           0 : void BgpSessionManager::WorkQueueExitCallback(bool done) {
     289           0 :     server_->RetryDelete();
     290           0 : }
     291             : 
     292           0 : size_t BgpSessionManager::GetSessionQueueSize() const {
     293           0 :     return session_queue_.Length();
     294             : }
     295             : 
     296           0 : void BgpSessionManager::SetSessionQueueDisable(bool disabled) {
     297           0 :     session_queue_.set_disable(disabled);
     298           0 : }

Generated by: LCOV version 1.14