LCOV - code coverage report
Current view: top level - bgp - bgp_server.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 136 140 97.1 %
Date: 2026-06-11 01:56:02 Functions: 68 68 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #ifndef SRC_BGP_BGP_SERVER_H_
       6             : #define SRC_BGP_BGP_SERVER_H_
       7             : 
       8             : #include <tbb/spin_rw_mutex.h>
       9             : #include <boost/asio/ip/tcp.hpp>
      10             : #include <boost/dynamic_bitset.hpp>
      11             : #include <boost/scoped_ptr.hpp>
      12             : #include <boost/bind/bind.hpp>
      13             : #include "io/event_manager.h"
      14             : 
      15             : #include <map>
      16             : #include <set>
      17             : #include <string>
      18             : #include <vector>
      19             : #include <atomic>
      20             : 
      21             : #include "bgp/bgp_common.h"
      22             : #include "db/db.h"
      23             : #include "io/tcp_server.h"
      24             : #include "io/tcp_session.h"
      25             : #include "base/address.h"
      26             : #include "bgp/routing-instance/iservice_chain_mgr.h"
      27             : 
      28             : using namespace boost::placeholders;
      29             : 
      30             : class AsPathDB;
      31             : class AsPath4ByteDB;
      32             : class As4PathDB;
      33             : class BgpAttrDB;
      34             : class BgpConditionListener;
      35             : class BgpConfigManager;
      36             : class BgpGlobalSystemConfig;
      37             : class BgpGlobalQosConfig;
      38             : class BgpMembershipManager;
      39             : class BgpOListDB;
      40             : class BgpPeer;
      41             : class BgpRouterState;
      42             : class BgpSessionManager;
      43             : class BgpUpdateSender;
      44             : class ClusterListDB;
      45             : class CommunityDB;
      46             : class EdgeDiscoveryDB;
      47             : class EdgeForwardingDB;
      48             : class ExtCommunityDB;
      49             : class LargeCommunityDB;
      50             : class IServiceChainMgr;
      51             : class IStaticRouteMgr;
      52             : class LifetimeActor;
      53             : class LifetimeManager;
      54             : class OriginVnPathDB;
      55             : class PmsiTunnelDB;
      56             : class RoutePathReplicator;
      57             : class RoutingInstanceMgr;
      58             : class RoutingPolicyMgr;
      59             : class RTargetGroupMgr;
      60             : class XmppServer;
      61             : 
      62             : class BgpServer {
      63             : public:
      64             :     typedef boost::function<void()> AdminDownCb;
      65             :     typedef boost::function<void(uint8_t)> DSCPUpdateCb;
      66             :     typedef boost::function<void(as_t, as_t)> ASNUpdateCb;
      67             :     typedef boost::function<void(Ip4Address)> IdentifierUpdateCb;
      68             :     typedef boost::function<void(BgpPeer *)> VisitorFn;
      69             :     typedef std::set<IStaticRouteMgr *> StaticRouteMgrList;
      70             : 
      71             :     explicit BgpServer(EventManager *evm);
      72             :     virtual ~BgpServer();
      73             : 
      74             :     static void Initialize();
      75             :     static void Terminate();
      76             : 
      77             :     virtual std::string ToString() const;
      78             : 
      79             :     uint16_t GetGracefulRestartTime() const;
      80             :     uint32_t GetLongLivedGracefulRestartTime() const;
      81             :     bool IsFastConvergenceEnabled() const;
      82             :     uint16_t GetXmppHoldTime() const;
      83             :     bool IsNextHopCheckEnabled() const;
      84             :     uint32_t GetEndOfRibReceiveTime() const;
      85             :     uint32_t GetEndOfRibSendTime() const;
      86             : 
      87             :     int RegisterPeer(BgpPeer *peer);
      88             :     void UnregisterPeer(BgpPeer *peer);
      89             :     BgpPeer *FindPeer(const std::string &name);
      90             :     BgpPeer *FindNextPeer(const std::string &name = std::string());
      91             :     void InsertPeer(TcpSession::Endpoint remote, BgpPeer *peer);
      92             :     void RemovePeer(TcpSession::Endpoint remote, BgpPeer *peer);
      93             :     virtual BgpPeer *FindPeer(TcpSession::Endpoint remote) const;
      94             :     BgpPeer *FindExactPeer(const BgpPeer *peer) const;
      95             : 
      96             :     void Shutdown();
      97             : 
      98             :     void VisitBgpPeers(BgpServer::VisitorFn) const;
      99             : 
     100             :     // accessors
     101      386751 :     BgpSessionManager *session_manager() { return session_mgr_; }
     102       83521 :     BgpUpdateSender *update_sender() { return update_sender_.get(); }
     103      994490 :     LifetimeManager *lifetime_manager() { return lifetime_manager_.get(); }
     104      224038 :     BgpConfigManager *config_manager() { return config_mgr_.get(); }
     105             :     const BgpConfigManager *config_manager() const { return config_mgr_.get(); }
     106      866203 :     RoutingInstanceMgr *routing_instance_mgr() { return inst_mgr_.get(); }
     107       15676 :     const RoutingInstanceMgr *routing_instance_mgr() const {
     108       15676 :         return inst_mgr_.get();
     109             :     }
     110       58413 :     RoutingPolicyMgr *routing_policy_mgr() { return policy_mgr_.get(); }
     111      447666 :     const RoutingPolicyMgr *routing_policy_mgr() const {
     112      447666 :         return policy_mgr_.get();
     113             :     }
     114     4911058 :     RTargetGroupMgr *rtarget_group_mgr() { return rtarget_group_mgr_.get(); }
     115             :     const RTargetGroupMgr *rtarget_group_mgr() const {
     116             :         return rtarget_group_mgr_.get();
     117             :     }
     118       15791 :     BgpConditionListener *condition_listener(Address::Family family) {
     119       15791 :         if (family == Address::INET)
     120       11113 :             return inet_condition_listener_.get();
     121        4678 :         if (family == Address::INET6)
     122        3194 :             return inet6_condition_listener_.get();
     123        1484 :         if (family == Address::EVPN)
     124        1485 :             return evpn_condition_listener_.get();
     125           0 :         assert(false);
     126             :         return NULL;
     127             :     }
     128       70805 :     BgpConditionListener *condition_listener(SCAddress::Family family) {
     129       70805 :         if (family == SCAddress::INET)
     130       19257 :             return inet_condition_listener_.get();
     131       51548 :         if (family == SCAddress::INET6)
     132       19099 :             return inet6_condition_listener_.get();
     133       32449 :         if (family == SCAddress::EVPN)
     134       16225 :             return evpn_condition_listener_.get();
     135       16224 :         if (family == SCAddress::EVPN6)
     136       16224 :             return evpn6_condition_listener_.get();
     137           0 :         assert(false);
     138             :         return NULL;
     139             :     }
     140      419953 :     IServiceChainMgr *service_chain_mgr(SCAddress::Family family) {
     141      419953 :         if (family == SCAddress::INET)
     142      105088 :             return inet_service_chain_mgr_.get();
     143      314865 :         if (family == SCAddress::INET6)
     144      105067 :             return inet6_service_chain_mgr_.get();
     145      209798 :         if (family == SCAddress::EVPN)
     146      104910 :             return evpn_service_chain_mgr_.get();
     147      104888 :         if (family == SCAddress::EVPN6)
     148      104911 :             return evpn6_service_chain_mgr_.get();
     149           0 :         assert(false);
     150             :         return NULL;
     151             :     }
     152      498681 :     RoutePathReplicator *replicator(Address::Family family) {
     153      498681 :         if (family == Address::INETVPN)
     154       99902 :             return inetvpn_replicator_.get();
     155      398779 :         if (family == Address::EVPN)
     156       99703 :             return evpn_replicator_.get();
     157      299076 :         if (family == Address::ERMVPN)
     158       99703 :             return ermvpn_replicator_.get();
     159      199373 :         if (family == Address::MVPN)
     160       99702 :             return mvpn_replicator_.get();
     161       99671 :         if (family == Address::INET6VPN)
     162       99723 :             return inet6vpn_replicator_.get();
     163           0 :         return NULL;
     164             :     }
     165        5512 :     const RoutePathReplicator *replicator(Address::Family family) const {
     166        5512 :         if (family == Address::INETVPN)
     167        5246 :             return inetvpn_replicator_.get();
     168         266 :         if (family == Address::EVPN)
     169          97 :             return evpn_replicator_.get();
     170         169 :         if (family == Address::ERMVPN)
     171          17 :             return ermvpn_replicator_.get();
     172         152 :         if (family == Address::MVPN)
     173           8 :             return mvpn_replicator_.get();
     174         144 :         if (family == Address::INET6VPN)
     175          67 :             return inet6vpn_replicator_.get();
     176          77 :         return NULL;
     177             :     }
     178             : 
     179      677940 :     BgpMembershipManager *membership_mgr() { return membership_mgr_.get(); }
     180           1 :     const BgpMembershipManager *membership_mgr() const {
     181           1 :         return membership_mgr_.get();
     182             :     }
     183             : 
     184      359418 :     AsPathDB *aspath_db() { return aspath_db_.get(); }
     185         485 :     AsPath4ByteDB *aspath_4byte_db() { return aspath_4byte_db_.get(); }
     186         470 :     As4PathDB *as4path_db() { return as4path_db_.get(); }
     187     2537657 :     BgpAttrDB *attr_db() { return attr_db_.get(); }
     188       13296 :     BgpOListDB *olist_db() { return olist_db_.get(); }
     189         182 :     ClusterListDB *cluster_list_db() { return cluster_list_db_.get(); }
     190      273359 :     CommunityDB *comm_db() { return comm_db_.get(); }
     191        2336 :     EdgeDiscoveryDB *edge_discovery_db() { return edge_discovery_db_.get(); }
     192        3598 :     EdgeForwardingDB *edge_forwarding_db() { return edge_forwarding_db_.get(); }
     193     1682946 :     ExtCommunityDB *extcomm_db() { return extcomm_db_.get(); }
     194      201114 :     LargeCommunityDB *largecomm_db() { return largecomm_db_.get(); }
     195       18410 :     OriginVnPathDB *ovnpath_db() { return ovnpath_db_.get(); }
     196       17061 :     PmsiTunnelDB *pmsi_tunnel_db() { return pmsi_tunnel_db_.get(); }
     197             : 
     198             :     bool IsDeleted() const;
     199             :     bool IsReadyForDeletion();
     200             :     void RetryDelete();
     201     3261821 :     bool logging_disabled() const { return logging_disabled_; }
     202         208 :     void set_logging_disabled(bool flag) { logging_disabled_ = flag; }
     203             : 
     204           4 :     bool destroyed() const { return destroyed_; }
     205        9639 :     void set_destroyed() { destroyed_  = true; }
     206             : 
     207      819629 :     DB *database() { return &db_; }
     208             :     const std::string &localname() const;
     209       27948 :     bool admin_down() const { return admin_down_; }
     210      331048 :     uint32_t cluster_id() const { return cluster_id_; }
     211           4 :     void set_cluster_id(uint32_t id) { cluster_id_ = id; }
     212     1583084 :     as_t autonomous_system() const { return autonomous_system_; }
     213          78 :     void set_autonomous_system(as_t asn) { autonomous_system_ = asn; }
     214      263009 :     as_t local_autonomous_system() const { return local_autonomous_system_; }
     215      685954 :     uint32_t bgp_identifier() const { return bgp_identifier_.to_ulong(); }
     216        6508 :     uint32_t bgp_identifier_u32() const { return bgp_identifier_u32_; }
     217             : 
     218         275 :     std::string bgp_identifier_string() const {
     219         275 :         return bgp_identifier_.to_string();
     220             :     }
     221             :     void UpdateBgpIdentifier(const Ip4Address &identifier);
     222       36619 :     uint32_t hold_time() const { return hold_time_; }
     223             :     bool HasSelfConfiguration() const;
     224             : 
     225             :     // Status
     226             :     uint32_t num_routing_instance() const;
     227             :     uint32_t num_deleted_routing_instance() const;
     228             : 
     229        1036 :     uint32_t num_bgp_peer() const { return bgp_count_; }
     230         259 :     uint32_t num_deleting_bgp_peer() const { return deleting_count_; }
     231        9457 :     void increment_deleting_count() { deleting_count_++; }
     232        9457 :     void decrement_deleting_count() {
     233        9457 :         assert(deleting_count_ > 0);
     234        9457 :         deleting_count_--;
     235        9457 :     }
     236             : 
     237         289 :     uint32_t num_bgpaas_peer() const { return bgpaas_count_; }
     238         259 :     uint32_t num_deleting_bgpaas_peer() const { return deleting_bgpaas_count_; }
     239         114 :     void increment_deleting_bgpaas_count() { deleting_bgpaas_count_++; }
     240         114 :     void decrement_deleting_bgpaas_count() {
     241         114 :         assert(deleting_bgpaas_count_ > 0);
     242         114 :         deleting_bgpaas_count_--;
     243         114 :     }
     244             : 
     245             :     uint32_t get_output_queue_depth() const;
     246             : 
     247             :     uint32_t num_service_chains() const;
     248             :     uint32_t num_down_service_chains() const;
     249             :     uint32_t num_static_routes() const;
     250             :     uint32_t num_down_static_routes() const;
     251             : 
     252        5111 :     void IncrementUpPeerCount() {
     253        5111 :         num_up_peer_++;
     254        5112 :     }
     255        5111 :     void DecrementUpPeerCount() {
     256        5111 :         assert(num_up_peer_);
     257        5111 :         num_up_peer_--;
     258        5112 :     }
     259        4370 :     uint32_t NumUpPeer() const {
     260        4370 :         return num_up_peer_;
     261             :     }
     262         196 :     void IncrementUpBgpaasPeerCount() {
     263         196 :         num_up_bgpaas_peer_++;
     264         196 :     }
     265         196 :     void DecrementUpBgpaasPeerCount() {
     266         196 :         assert(num_up_bgpaas_peer_);
     267         196 :         num_up_bgpaas_peer_--;
     268         196 :     }
     269         259 :     uint32_t NumUpBgpaasPeer() const {
     270         259 :         return num_up_bgpaas_peer_;
     271             :     }
     272             : 
     273             :     LifetimeActor *deleter();
     274             :     boost::asio::io_context *ioservice();
     275             : 
     276         112 :     void increment_message_build_error() const { ++message_build_error_; }
     277           5 :     uint64_t message_build_error() const { return message_build_error_; }
     278             : 
     279             :     int RegisterAdminDownCallback(AdminDownCb callback);
     280             :     void UnregisterAdminDownCallback(int listener);
     281             :     void NotifyAdminDown();
     282             :     int RegisterASNUpdateCallback(ASNUpdateCb callback);
     283             :     void UnregisterASNUpdateCallback(int listener);
     284             :     void NotifyASNUpdate(as_t old_asn, as_t old_local_asn);
     285             :     int RegisterIdentifierUpdateCallback(IdentifierUpdateCb callback);
     286             :     void UnregisterIdentifierUpdateCallback(int listener);
     287             :     void NotifyIdentifierUpdate(Ip4Address old_identifier);
     288             :     int RegisterDSCPUpdateCallback(DSCPUpdateCb cb);
     289             :     void UnregisterDSCPUpdateCallback(int listener);
     290             :     void NotifyDSCPUpdate(int new_dscp_value);
     291             : 
     292             :     void InsertStaticRouteMgr(IStaticRouteMgr *srt_manager);
     293             :     void RemoveStaticRouteMgr(IStaticRouteMgr *srt_manager);
     294             :     void NotifyAllStaticRoutes();
     295             :     uint32_t GetStaticRouteCount() const;
     296             :     uint32_t GetDownStaticRouteCount() const;
     297       63368 :     BgpGlobalSystemConfig *global_config() { return global_config_.get(); }
     298     3029418 :     const BgpGlobalSystemConfig *global_config() const {
     299     3029418 :         return global_config_.get();
     300             :     }
     301        8453 :     BgpGlobalQosConfig *global_qos() { return global_qos_.get(); }
     302             :     bool gr_helper_disable() const { return gr_helper_disable_; }
     303         208 :     void set_gr_helper_disable(bool gr_helper_disable) {
     304         208 :         gr_helper_disable_ = gr_helper_disable;
     305         208 :     }
     306             :     bool IsGRHelperModeEnabled() const;
     307             :     bool CollectStats(BgpRouterState *state, bool first) const;
     308             :     bool IsServerStartingUp() const;
     309             :     time_t GetRTargetTableLastUpdatedTimeStamp() const;
     310      500539 :     bool mvpn_ipv4_enable() const { return mvpn_ipv4_enable_; }
     311        1271 :     void set_mvpn_ipv4_enable(bool flag) { mvpn_ipv4_enable_ = flag; }
     312             :     bool enable_4byte_as() const;
     313             :     void set_enable_4byte_as(bool flag);
     314     2352981 :     bool ignore_aspath() const { return ignore_aspath_; }
     315           1 :     void set_ignore_aspath(bool flag) { ignore_aspath_ = flag; }
     316             : 
     317             : private:
     318             :     class ConfigUpdater;
     319             :     class DeleteActor;
     320             :     friend class BgpServerTest;
     321             :     friend class BgpServerUnitTest;
     322             :     typedef std::map<std::string, BgpPeer *> BgpPeerList;
     323             :     typedef std::vector<AdminDownCb> AdminDownListenersList;
     324             :     typedef std::vector<ASNUpdateCb> ASNUpdateListenersList;
     325             :     typedef std::vector<IdentifierUpdateCb> IdentifierUpdateListenersList;
     326             :     typedef std::vector<DSCPUpdateCb> DSCPUpdateListenersList;
     327             :     typedef std::multimap<TcpSession::Endpoint, BgpPeer *> EndpointPeerList;
     328             : 
     329             :     void RoutingInstanceMgrDeletionComplete(RoutingInstanceMgr *mgr);
     330             :     void FillPeerStats(const BgpPeer *peer) const;
     331             :     void ClearBgpaaSPeers();
     332             : 
     333             :     // base config variables
     334             :     tbb::spin_rw_mutex rw_mutex_;
     335             :     bool admin_down_;
     336             :     AdminDownListenersList admin_down_listeners_;
     337             :     boost::dynamic_bitset<> admin_down_bmap_;  // free list.
     338             :     uint32_t cluster_id_;
     339             :     as_t autonomous_system_;
     340             :     as_t local_autonomous_system_;
     341             :     ASNUpdateListenersList asn_listeners_;
     342             :     boost::dynamic_bitset<> asn_bmap_;     // free list.
     343             :     Ip4Address bgp_identifier_;
     344             :     uint32_t bgp_identifier_u32_;
     345             :     IdentifierUpdateListenersList id_listeners_;
     346             :     boost::dynamic_bitset<> id_bmap_;      // free list.
     347             :     DSCPUpdateListenersList dscp_listeners_;
     348             :     boost::dynamic_bitset<> dscp_bmap_;  // free list.
     349             :     uint32_t hold_time_;
     350             :     bool gr_helper_disable_;
     351             :     StaticRouteMgrList srt_manager_list_;
     352             : 
     353             :     DB db_;
     354             :     boost::dynamic_bitset<> peer_bmap_;
     355             :     std::atomic<uint32_t> bgp_count_;
     356             :     std::atomic<uint32_t> num_up_peer_;
     357             :     std::atomic<uint32_t> deleting_count_;
     358             :     std::atomic<uint32_t> bgpaas_count_;
     359             :     std::atomic<uint32_t> num_up_bgpaas_peer_;
     360             :     std::atomic<uint32_t> deleting_bgpaas_count_;
     361             :     BgpPeerList peer_list_;
     362             :     EndpointPeerList endpoint_peer_list_;
     363             : 
     364             :     boost::scoped_ptr<LifetimeManager> lifetime_manager_;
     365             :     boost::scoped_ptr<DeleteActor> deleter_;
     366             :     bool destroyed_;
     367             :     bool logging_disabled_;
     368             :     bool mvpn_ipv4_enable_;
     369             :     bool ignore_aspath_;
     370             : 
     371             :     // databases
     372             :     boost::scoped_ptr<AsPathDB> aspath_db_;
     373             :     boost::scoped_ptr<AsPath4ByteDB> aspath_4byte_db_;
     374             :     boost::scoped_ptr<As4PathDB> as4path_db_;
     375             :     boost::scoped_ptr<BgpOListDB> olist_db_;
     376             :     boost::scoped_ptr<ClusterListDB> cluster_list_db_;
     377             :     boost::scoped_ptr<CommunityDB> comm_db_;
     378             :     boost::scoped_ptr<EdgeDiscoveryDB> edge_discovery_db_;
     379             :     boost::scoped_ptr<EdgeForwardingDB> edge_forwarding_db_;
     380             :     boost::scoped_ptr<ExtCommunityDB> extcomm_db_;
     381             :     boost::scoped_ptr<LargeCommunityDB> largecomm_db_;
     382             :     boost::scoped_ptr<OriginVnPathDB> ovnpath_db_;
     383             :     boost::scoped_ptr<PmsiTunnelDB> pmsi_tunnel_db_;
     384             :     boost::scoped_ptr<BgpAttrDB> attr_db_;
     385             : 
     386             :     // sessions and state managers
     387             :     BgpSessionManager *session_mgr_;
     388             :     boost::scoped_ptr<BgpUpdateSender> update_sender_;
     389             :     boost::scoped_ptr<RoutingInstanceMgr> inst_mgr_;
     390             :     boost::scoped_ptr<RoutingPolicyMgr> policy_mgr_;
     391             :     boost::scoped_ptr<RTargetGroupMgr> rtarget_group_mgr_;
     392             :     boost::scoped_ptr<BgpMembershipManager> membership_mgr_;
     393             :     boost::scoped_ptr<BgpConditionListener> inet_condition_listener_;
     394             :     boost::scoped_ptr<BgpConditionListener> inet6_condition_listener_;
     395             :     boost::scoped_ptr<BgpConditionListener> evpn_condition_listener_;
     396             :     boost::scoped_ptr<BgpConditionListener> evpn6_condition_listener_;
     397             :     boost::scoped_ptr<RoutePathReplicator> inetvpn_replicator_;
     398             :     boost::scoped_ptr<RoutePathReplicator> ermvpn_replicator_;
     399             :     boost::scoped_ptr<RoutePathReplicator> mvpn_replicator_;
     400             :     boost::scoped_ptr<RoutePathReplicator> evpn_replicator_;
     401             :     boost::scoped_ptr<RoutePathReplicator> inet6vpn_replicator_;
     402             :     boost::scoped_ptr<IServiceChainMgr> inet_service_chain_mgr_;
     403             :     boost::scoped_ptr<IServiceChainMgr> inet6_service_chain_mgr_;
     404             :     boost::scoped_ptr<IServiceChainMgr> evpn_service_chain_mgr_;
     405             :     boost::scoped_ptr<IServiceChainMgr> evpn6_service_chain_mgr_;
     406             : 
     407             :     // configuration
     408             :     boost::scoped_ptr<BgpGlobalSystemConfig> global_config_;
     409             :     boost::scoped_ptr<BgpGlobalQosConfig> global_qos_;
     410             :     boost::scoped_ptr<BgpConfigManager> config_mgr_;
     411             :     boost::scoped_ptr<ConfigUpdater> updater_;
     412             : 
     413             :     mutable std::atomic<uint64_t> message_build_error_;
     414             : 
     415             :     DISALLOW_COPY_AND_ASSIGN(BgpServer);
     416             : };
     417             : 
     418             : #endif  // SRC_BGP_BGP_SERVER_H_

Generated by: LCOV version 1.14