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

Generated by: LCOV version 1.14