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: 79 133 59.4 %
Date: 2026-11-16 02:06:09 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             :  * 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          20 : BgpSessionManager::BgpSessionManager(EventManager *evm, BgpServer *server)
      23             :     : TcpServer(evm),
      24          20 :       server_(server),
      25          20 :       session_queue_(
      26             :           TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
      27             :           boost::bind(&BgpSessionManager::ProcessSession, this, _1)),
      28          20 :       write_ready_queue_(
      29             :           TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
      30          40 :           boost::bind(&BgpSessionManager::ProcessWriteReady, this, _1)) {
      31             : 
      32          20 :       boost::system::error_code ec;
      33          20 :       session_ip_ = AddressFromString(kDefaultBgpSessionIp, &ec);
      34          20 : }
      35             : 
      36          40 : BgpSessionManager::~BgpSessionManager() {
      37          40 : }
      38             : 
      39             : //
      40             : // Start listening at the given port.
      41             : //
      42          40 : bool BgpSessionManager::Initialize(unsigned short port) {
      43             :     // Changes to already bound bgp listen port number is not supported.
      44          40 :     Endpoint local_endpoint = LocalEndpoint();
      45          40 :     if (local_endpoint != Endpoint()) {
      46          20 :         Endpoint new_endpoint(session_ip_, port);
      47          20 :         if (local_endpoint != new_endpoint) {
      48           0 :             BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
      49             :                 "Cannot change already bound " << local_endpoint <<
      50             :                 " to " << new_endpoint);
      51           0 :             return false;
      52             :         }
      53          20 :         return true;
      54             :     }
      55             : 
      56          20 :     LOG(DEBUG, "Starting Bgp Server at " << session_ip_ << ":" << port);
      57          20 :     bool r = TcpServer::Initialize(port, session_ip_);
      58          20 :     if (!r) {
      59           0 :         BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
      60             :                             "Cannot bind to bgp/tcp server ip:port " <<
      61             :                             session_ip_ << ":" << port);
      62             :     }
      63          20 :     return r;
      64             : }
      65             : 
      66             : //
      67             : // Start listening at the given ip:port.
      68             : //
      69           0 : bool BgpSessionManager::Initialize(unsigned short port, const IpAddress& ip) {
      70           0 :     session_ip_ = ip;
      71           0 :     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          20 : void BgpSessionManager::Shutdown() {
      81          20 :     CHECK_CONCURRENCY("bgp::Config");
      82          20 :     TcpServer::Shutdown();
      83          20 :     session_queue_.SetExitCallback(
      84             :         boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
      85          20 :     write_ready_queue_.SetExitCallback(
      86             :         boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
      87          20 : }
      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          20 : void BgpSessionManager::Terminate() {
      97          20 :     CHECK_CONCURRENCY("bgp::Config");
      98          20 :     server_ = NULL;
      99          20 :     ClearSessions();
     100          20 :     session_queue_.Shutdown();
     101          20 :     write_ready_queue_.Shutdown();
     102          20 : }
     103             : 
     104             : //
     105             : // Return true if all WorkQueues are empty.
     106             : //
     107          20 : bool BgpSessionManager::MayDelete() const {
     108          20 :     if (!session_queue_.IsQueueEmpty())
     109           0 :         return false;
     110          20 :     if (!write_ready_queue_.IsQueueEmpty())
     111           0 :         return false;
     112          20 :     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         148 : BgpPeer *BgpSessionManager::FindPeer(Endpoint remote) {
     143         148 :     BgpPeer *peer = NULL;
     144             :     const RoutingInstance *instance =
     145         148 :         server_->routing_instance_mgr()->GetDefaultRoutingInstance();
     146         148 :     if (instance && !instance->deleted()) {
     147         148 :         peer = instance->peer_manager()->PeerLookup(remote);
     148             :     }
     149         148 :     if (!peer) {
     150           0 :         uint16_t port = BGPaaSUtils::DecodeBgpaasServicePort(remote.port(),
     151           0 :             server_->global_config()->bgpaas_port_start(),
     152           0 :             server_->global_config()->bgpaas_port_end()).first;
     153           0 :         peer = server_->FindPeer(TcpSession::Endpoint(Ip4Address(), port));
     154             :     }
     155         148 :     return peer;
     156             : }
     157             : 
     158             : //
     159             : // Create an active BgpSession.
     160             : //
     161         148 : TcpSession *BgpSessionManager::CreateSession() {
     162         148 :     TcpSession *session = TcpServer::CreateSession();
     163         148 :     Socket *socket = session->socket();
     164             : 
     165         148 :     boost::system::error_code ec;
     166         148 :     socket->open(boost::asio::ip::tcp::v4(), ec);
     167         148 :     if (ec || (ec = session->SetSocketOptions()) || socket_open_failure()) {
     168           0 :         BGP_LOG_WARNING_STR(BgpSocket, BGP_LOG_FLAG_ALL,
     169             :             "Failed to open bgp socket, error: " << ec.message());
     170           0 :         DeleteSession(session);
     171           0 :         return NULL;
     172             :     }
     173             : 
     174         148 :     if (session_ip_ != address::from_string(kDefaultBgpSessionIp, ec))  {
     175           0 :         tcp::endpoint localaddr(session_ip_, 0);
     176           0 :         socket->bind(localaddr, ec);
     177           0 :         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         148 :     return session;
     186             : }
     187             : 
     188             : //
     189             : // Allocate a new BgpSession.
     190             : // Called via CreateSession or when the TcpServer accepts a passive session.
     191             : //
     192         296 : TcpSession *BgpSessionManager::AllocSession(Socket *socket) {
     193         296 :     TcpSession *session = new BgpSession(this, socket);
     194         293 :     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         148 : bool BgpSessionManager::AcceptSession(TcpSession *tcp_session) {
     207         148 :     if (!server_ || server_->IsDeleted())
     208           0 :         return false;
     209         148 :     BgpSession *session = dynamic_cast<BgpSession *>(tcp_session);
     210         148 :     session->set_read_on_connect(false);
     211         148 :     session_queue_.Enqueue(session);
     212         148 :     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         148 : bool BgpSessionManager::ProcessSession(BgpSession *session) {
     221         148 :     CHECK_CONCURRENCY("bgp::Config");
     222             : 
     223         148 :     BgpPeer *peer = FindPeer(session->remote_endpoint());
     224             : 
     225             :     // Ignore if server is being deleted.
     226         148 :     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         148 :     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         148 :     if (peer == NULL || peer->deleter()->IsDeleted()) {
     243           0 :         session->SendNotification(BgpProto::Notification::Cease,
     244             :                                   BgpProto::Notification::PeerDeconfigured);
     245           0 :         BGP_LOG_WARNING_STR(BgpConfig, BGP_LOG_FLAG_TRACE,
     246             :                             "Remote end-point not found");
     247           0 :         DeleteSession(session);
     248           0 :         return true;
     249             :     }
     250             : 
     251             :     // Ignore if this peer is being held administratively down.
     252         148 :     if (peer->IsAdminDown()) {
     253          80 :         session->SendNotification(BgpProto::Notification::Cease,
     254             :                                   BgpProto::Notification::AdminShutdown);
     255          80 :         DeleteSession(session);
     256          80 :         return true;
     257             :     }
     258             : 
     259             :     // Ignore if the peer's prefix limit idle timer is running.
     260          68 :     if (peer->PrefixLimitIdleTimerRunning()) {
     261           0 :         session->SendNotification(BgpProto::Notification::Cease,
     262             :                                   BgpProto::Notification::MaxPrefixes);
     263           0 :         DeleteSession(session);
     264           0 :         return true;
     265             :     }
     266             : 
     267             :     // Ignore if this peer is being closed.
     268          68 :     if (peer->IsCloseInProgress()) {
     269           8 :         session->SendNotification(BgpProto::Notification::Cease,
     270             :                                   BgpProto::Notification::ConnectionRejected);
     271           8 :         DeleteSession(session);
     272           8 :         return true;
     273             :     }
     274             : 
     275          60 :     if (!peer->ProcessSession()) {
     276           0 :         session->SendNotification(BgpProto::Notification::Cease,
     277             :                                   BgpProto::Notification::ConnectionRejected);
     278           0 :         DeleteSession(session);
     279           0 :         return true;
     280             :     }
     281             : 
     282          60 :     peer->AcceptSession(session);
     283          60 :     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           0 : void BgpSessionManager::SetSessionQueueDisable(bool disabled) {
     298           0 :     session_queue_.set_disable(disabled);
     299           0 : }

Generated by: LCOV version 1.14