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 385937 : BgpSessionManager *session_manager() { return session_mgr_; } 103 83526 : BgpUpdateSender *update_sender() { return update_sender_.get(); } 104 994367 : LifetimeManager *lifetime_manager() { return lifetime_manager_.get(); } 105 223761 : BgpConfigManager *config_manager() { return config_mgr_.get(); } 106 : const BgpConfigManager *config_manager() const { return config_mgr_.get(); } 107 853330 : RoutingInstanceMgr *routing_instance_mgr() { return inst_mgr_.get(); } 108 15620 : const RoutingInstanceMgr *routing_instance_mgr() const { 109 15620 : return inst_mgr_.get(); 110 : } 111 58170 : RoutingPolicyMgr *routing_policy_mgr() { return policy_mgr_.get(); } 112 448445 : const RoutingPolicyMgr *routing_policy_mgr() const { 113 448445 : return policy_mgr_.get(); 114 : } 115 4957127 : RTargetGroupMgr *rtarget_group_mgr() { return rtarget_group_mgr_.get(); } 116 : const RTargetGroupMgr *rtarget_group_mgr() const { 117 : return rtarget_group_mgr_.get(); 118 : } 119 15766 : BgpConditionListener *condition_listener(Address::Family family) { 120 15766 : if (family == Address::INET) 121 11101 : return inet_condition_listener_.get(); 122 4665 : if (family == Address::INET6) 123 3195 : return inet6_condition_listener_.get(); 124 1470 : if (family == Address::EVPN) 125 1470 : return evpn_condition_listener_.get(); 126 0 : assert(false); 127 : return NULL; 128 : } 129 70818 : BgpConditionListener *condition_listener(SCAddress::Family family) { 130 70818 : if (family == SCAddress::INET) 131 19248 : return inet_condition_listener_.get(); 132 51570 : if (family == SCAddress::INET6) 133 19129 : return inet6_condition_listener_.get(); 134 32441 : if (family == SCAddress::EVPN) 135 16221 : return evpn_condition_listener_.get(); 136 16220 : if (family == SCAddress::EVPN6) 137 16219 : return evpn6_condition_listener_.get(); 138 1 : assert(false); 139 : return NULL; 140 : } 141 418985 : IServiceChainMgr *service_chain_mgr(SCAddress::Family family) { 142 418985 : if (family == SCAddress::INET) 143 104846 : return inet_service_chain_mgr_.get(); 144 314139 : if (family == SCAddress::INET6) 145 104827 : return inet6_service_chain_mgr_.get(); 146 209312 : if (family == SCAddress::EVPN) 147 104670 : return evpn_service_chain_mgr_.get(); 148 104642 : if (family == SCAddress::EVPN6) 149 104671 : return evpn6_service_chain_mgr_.get(); 150 0 : assert(false); 151 : return NULL; 152 : } 153 497136 : RoutePathReplicator *replicator(Address::Family family) { 154 497136 : if (family == Address::INETVPN) 155 99593 : return inetvpn_replicator_.get(); 156 397543 : if (family == Address::EVPN) 157 99395 : return evpn_replicator_.get(); 158 298148 : if (family == Address::ERMVPN) 159 99398 : return ermvpn_replicator_.get(); 160 198750 : if (family == Address::MVPN) 161 99399 : return mvpn_replicator_.get(); 162 99351 : if (family == Address::INET6VPN) 163 99416 : return inet6vpn_replicator_.get(); 164 0 : return NULL; 165 : } 166 5500 : const RoutePathReplicator *replicator(Address::Family family) const { 167 5500 : if (family == Address::INETVPN) 168 5244 : return inetvpn_replicator_.get(); 169 256 : if (family == Address::EVPN) 170 97 : return evpn_replicator_.get(); 171 159 : if (family == Address::ERMVPN) 172 17 : return ermvpn_replicator_.get(); 173 142 : if (family == Address::MVPN) 174 8 : return mvpn_replicator_.get(); 175 134 : if (family == Address::INET6VPN) 176 65 : return inet6vpn_replicator_.get(); 177 69 : return NULL; 178 : } 179 : 180 657567 : BgpMembershipManager *membership_mgr() { return membership_mgr_.get(); } 181 1 : const BgpMembershipManager *membership_mgr() const { 182 1 : return membership_mgr_.get(); 183 : } 184 : 185 363161 : AsPathDB *aspath_db() { return aspath_db_.get(); } 186 495 : AsPath4ByteDB *aspath_4byte_db() { return aspath_4byte_db_.get(); } 187 461 : As4PathDB *as4path_db() { return as4path_db_.get(); } 188 2558504 : BgpAttrDB *attr_db() { return attr_db_.get(); } 189 12995 : BgpOListDB *olist_db() { return olist_db_.get(); } 190 182 : ClusterListDB *cluster_list_db() { return cluster_list_db_.get(); } 191 276949 : CommunityDB *comm_db() { return comm_db_.get(); } 192 2196 : EdgeDiscoveryDB *edge_discovery_db() { return edge_discovery_db_.get(); } 193 3411 : EdgeForwardingDB *edge_forwarding_db() { return edge_forwarding_db_.get(); } 194 1690648 : ExtCommunityDB *extcomm_db() { return extcomm_db_.get(); } 195 201494 : LargeCommunityDB *largecomm_db() { return largecomm_db_.get(); } 196 18496 : OriginVnPathDB *ovnpath_db() { return ovnpath_db_.get(); } 197 17053 : PmsiTunnelDB *pmsi_tunnel_db() { return pmsi_tunnel_db_.get(); } 198 : 199 : bool IsDeleted() const; 200 : bool IsReadyForDeletion(); 201 : void RetryDelete(); 202 3246235 : 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 9639 : void set_destroyed() { destroyed_ = true; } 207 : 208 819453 : DB *database() { return &db_; } 209 : const std::string &localname() const; 210 27798 : bool admin_down() const { return admin_down_; } 211 331038 : uint32_t cluster_id() const { return cluster_id_; } 212 4 : void set_cluster_id(uint32_t id) { cluster_id_ = id; } 213 1590580 : as_t autonomous_system() const { return autonomous_system_; } 214 78 : void set_autonomous_system(as_t asn) { autonomous_system_ = asn; } 215 262809 : as_t local_autonomous_system() const { return local_autonomous_system_; } 216 685644 : uint32_t bgp_identifier() const { return bgp_identifier_.to_ulong(); } 217 6501 : uint32_t bgp_identifier_u32() const { return bgp_identifier_u32_; } 218 : 219 193 : std::string bgp_identifier_string() const { 220 193 : return bgp_identifier_.to_string(); 221 : } 222 : void UpdateBgpIdentifier(const Ip4Address &identifier); 223 36271 : 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 954 : uint32_t num_bgp_peer() const { return bgp_count_; } 231 177 : uint32_t num_deleting_bgp_peer() const { return deleting_count_; } 232 9455 : void increment_deleting_count() { deleting_count_++; } 233 9455 : void decrement_deleting_count() { 234 9455 : assert(deleting_count_ > 0); 235 9455 : deleting_count_--; 236 9455 : } 237 : 238 207 : uint32_t num_bgpaas_peer() const { return bgpaas_count_; } 239 177 : uint32_t num_deleting_bgpaas_peer() const { return deleting_bgpaas_count_; } 240 112 : void increment_deleting_bgpaas_count() { deleting_bgpaas_count_++; } 241 112 : void decrement_deleting_bgpaas_count() { 242 112 : assert(deleting_bgpaas_count_ > 0); 243 112 : deleting_bgpaas_count_--; 244 112 : } 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 5156 : void IncrementUpPeerCount() { 254 5156 : num_up_peer_++; 255 5156 : } 256 5154 : void DecrementUpPeerCount() { 257 5154 : assert(num_up_peer_); 258 5154 : num_up_peer_--; 259 5156 : } 260 4314 : uint32_t NumUpPeer() const { 261 4314 : return num_up_peer_; 262 : } 263 196 : void IncrementUpBgpaasPeerCount() { 264 196 : num_up_bgpaas_peer_++; 265 196 : } 266 196 : void DecrementUpBgpaasPeerCount() { 267 196 : assert(num_up_bgpaas_peer_); 268 196 : num_up_bgpaas_peer_--; 269 196 : } 270 177 : uint32_t NumUpBgpaasPeer() const { 271 177 : return num_up_bgpaas_peer_; 272 : } 273 : 274 : LifetimeActor *deleter(); 275 : boost::asio::io_context *ioservice(); 276 : 277 112 : 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 62559 : BgpGlobalSystemConfig *global_config() { return global_config_.get(); } 299 3037314 : const BgpGlobalSystemConfig *global_config() const { 300 3037314 : return global_config_.get(); 301 : } 302 8723 : 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 500770 : 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 2356833 : 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_