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

Generated by: LCOV version 1.14