Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_BGP_ROUTING_INSTANCE_ROUTING_INSTANCE_H_ 6 : #define SRC_BGP_ROUTING_INSTANCE_ROUTING_INSTANCE_H_ 7 : 8 : 9 : #include <boost/asio/ip/tcp.hpp> 10 : #include <boost/dynamic_bitset.hpp> 11 : #include <boost/intrusive_ptr.hpp> 12 : #include <boost/iterator/iterator_facade.hpp> 13 : #include <boost/scoped_ptr.hpp> 14 : #include <tbb/spin_rw_mutex.h> 15 : 16 : #include <list> 17 : #include <map> 18 : #include <set> 19 : #include <string> 20 : #include <vector> 21 : #include <mutex> 22 : 23 : #include "base/bitset.h" 24 : #include "base/index_map.h" 25 : #include "base/lifetime.h" 26 : #include "base/address.h" 27 : #include "bgp/bgp_common.h" 28 : #include "bgp/routing-instance/iservice_chain_mgr.h" 29 : #include "bgp/rtarget/rtarget_address.h" 30 : #include "sandesh/sandesh_trace.h" 31 : 32 : class DBTable; 33 : class BgpAttr; 34 : class BgpInstanceConfig; 35 : class BgpNeighborConfig; 36 : class BgpPath; 37 : class BgpRoute; 38 : class BgpServer; 39 : class BgpTable; 40 : class IRouteAggregator; 41 : class IStaticRouteMgr; 42 : class RouteDistinguisher; 43 : class RoutingInstanceMgr; 44 : class RoutingInstanceInfo; 45 : class RoutingInstanceStatsData; 46 : class RoutingTableStats; 47 : class BgpNeighborResp; 48 : class ExtCommunity; 49 : class LifetimeActor; 50 : class PeerManager; 51 : class ShowRouteTable; 52 : class TaskTrigger; 53 : 54 : class RoutingInstance { 55 : public: 56 : typedef std::set<RouteTarget> RouteTargetList; 57 : typedef std::map<std::string, BgpTable *> RouteTableList; 58 : typedef std::map<Address::Family, BgpTable *> RouteTableFamilyList; 59 : 60 : RoutingInstance(std::string name, BgpServer *server, 61 : RoutingInstanceMgr *mgr, 62 : const BgpInstanceConfig *config); 63 : virtual ~RoutingInstance(); 64 : 65 38371 : RouteTableList &GetTables() { return vrf_tables_by_name_; } 66 17905 : const RouteTableList &GetTables() const { return vrf_tables_by_name_; } 67 : 68 : void ProcessRoutingPolicyConfig(); 69 : void UpdateRoutingPolicyConfig(); 70 : void ProcessServiceChainConfig(); 71 : void ProcessStaticRouteConfig(); 72 : void UpdateStaticRouteConfig(); 73 : void FlushStaticRouteConfig(); 74 : void UpdateAllStaticRoutes(); 75 : 76 : void ProcessRouteAggregationConfig(); 77 : void UpdateRouteAggregationConfig(); 78 : void FlushRouteAggregationConfig(); 79 : 80 : void ProcessConfig(); 81 : void UpdateConfig(const BgpInstanceConfig *config); 82 : void ClearConfig(); 83 : 84 : static std::string GetTableName(std::string instance_name, 85 : Address::Family fmly); 86 : static std::string GetVrfFromTableName(const std::string table); 87 : 88 : BgpTable *GetTable(Address::Family fmly); 89 : const BgpTable *GetTable(Address::Family fmly) const; 90 : 91 : void AddTable(BgpTable *tbl); 92 : 93 : void RemoveTable(BgpTable *tbl); 94 : 95 1773721 : const RouteTargetList &GetImportList() const { return import_; } 96 1989874 : const RouteTargetList &GetExportList() const { return export_; } 97 : bool HasExportTarget(const ExtCommunity *extcomm) const; 98 : 99 52550 : const RouteDistinguisher *GetRD() const { 100 52550 : return rd_.get(); 101 : } 102 : 103 : void TriggerTableDelete(BgpTable *table); 104 : void TableDeleteComplete(BgpTable *table); 105 : void DestroyDBTable(DBTable *table); 106 : 107 : bool MayDelete() const; 108 : void ManagedDelete(); 109 : LifetimeActor *deleter(); 110 : const LifetimeActor *deleter() const; 111 : bool deleted() const; 112 : 113 : void set_index(int index); 114 188384 : int index() const { return index_; } 115 38191 : bool always_subscribe() const { return always_subscribe_; } 116 6725741 : bool IsMasterRoutingInstance() const { 117 6725741 : return is_master_; 118 : } 119 : 120 3011920 : const std::string &name() const { return name_; } 121 : const std::string GetVirtualNetworkName() const; 122 : 123 442482 : const BgpInstanceConfig *config() const { return config_; } 124 : int virtual_network_index() const; 125 : bool virtual_network_allow_transit() const; 126 : bool virtual_network_pbb_evpn_enable() const; 127 : int vxlan_id() const; 128 : 129 231986 : const RoutingInstanceMgr *manager() const { return mgr_; } 130 207621 : RoutingInstanceMgr *manager() { return mgr_; } 131 : RoutingInstanceInfo GetDataCollection(const char *operation); 132 : 133 6336430 : BgpServer *server() { return server_; } 134 602348 : const BgpServer *server() const { return server_; } 135 : 136 : // Remove import and export route target 137 : // and Leave corresponding RtGroup 138 : void ClearRouteTarget(); 139 : 140 : void CreateNeighbors(); 141 : 142 206364 : IStaticRouteMgr *static_route_mgr(Address::Family family) { 143 206364 : if (family == Address::INET) 144 103216 : return inet_static_route_mgr_.get(); 145 103148 : if (family == Address::INET6) 146 103148 : return inet6_static_route_mgr_.get(); 147 0 : assert(false); 148 : return NULL; 149 : } 150 : 151 : IStaticRouteMgr *LocateStaticRouteMgr(Address::Family family); 152 : 153 1128217 : IRouteAggregator *route_aggregator(Address::Family family) const { 154 1128217 : if (family == Address::INET) 155 591096 : return inet_route_aggregator_.get(); 156 537121 : if (family == Address::INET6) 157 537241 : return inet6_route_aggregator_.get(); 158 0 : assert(false); 159 : return NULL; 160 : } 161 : 162 : IRouteAggregator *LocateRouteAggregator(Address::Family family); 163 : void DestroyRouteAggregator(Address::Family family); 164 : 165 : size_t peer_manager_size() const; 166 34089 : PeerManager *peer_manager() { return peer_manager_.get(); } 167 7331 : const PeerManager *peer_manager() const { return peer_manager_.get(); } 168 : PeerManager *LocatePeerManager(); 169 : 170 6823 : RoutingPolicyAttachList *routing_policies() { return &routing_policies_; } 171 447853 : const RoutingPolicyAttachList &routing_policies() const { 172 447853 : return routing_policies_; 173 : } 174 : SandeshTraceBufferPtr trace_buffer() const; 175 : 176 : void AddRoutingPolicy(RoutingPolicyPtr policy); 177 : 178 : bool IsServiceChainRoute(const BgpRoute *route) const; 179 : 180 : bool ProcessRoutingPolicy(const BgpRoute *route, BgpPath *path) const; 181 : 182 : // Check whether the route is aggregate route 183 : bool IsAggregateRoute(const BgpTable *table, const BgpRoute *route) const; 184 : 185 : // Check whether the route is contributing route to aggregate route 186 : bool IsContributingRoute(const BgpTable *table, 187 : const BgpRoute *route) const; 188 : 189 : int GetOriginVnForAggregateRoute(Address::Family family) const; 190 162615 : const std::string &mvpn_project_manager_network() const { 191 162615 : return mvpn_project_manager_network_; 192 : } 193 90252 : std::string &mvpn_project_manager_network() { 194 90252 : return mvpn_project_manager_network_; 195 : } 196 19564 : void set_mvpn_project_manager_network(std::string network) { 197 19564 : mvpn_project_manager_network_ = network; 198 19578 : } 199 2309 : const std::string &routing_instance_vxlan() const { 200 2309 : return routing_instance_vxlan_; 201 : } 202 17 : void set_routing_instance_vxlan(const std::string &routing_instance_vxlan) { 203 17 : routing_instance_vxlan_ = routing_instance_vxlan; 204 17 : } 205 : 206 : private: 207 : friend class RoutingInstanceMgr; 208 : class DeleteActor; 209 : 210 : void AddMvpnRTargetRoute(as_t asn); 211 : void DeleteMvpnRTargetRoute(as_t old_asn, Ip4Address old_ip); 212 : 213 : void AddRTargetRoute(as_t asn, const RouteTarget &rtarget); 214 : void DeleteRTargetRoute(as_t asn, const RouteTarget &rtarget); 215 : void InitAllRTargetRoutes(as_t asn); 216 : void FlushAllRTargetRoutes(as_t asn); 217 : void ProcessIdentifierUpdate(as_t asn); 218 : 219 : void AddRouteTarget(bool import, std::vector<std::string> *change_list, 220 : RouteTargetList::const_iterator it); 221 : void DeleteRouteTarget(bool import, std::vector<std::string> *change_list, 222 : RouteTargetList::iterator it); 223 : 224 : // Cleanup all the state prior to deletion. 225 : void Shutdown(); 226 : 227 : BgpTable *VpnTableCreate(Address::Family vpn_family); 228 : BgpTable *RTargetTableCreate(); 229 : BgpTable *VrfTableCreate(Address::Family vrf_family, 230 : Address::Family vpn_family); 231 : void ClearFamilyRouteTarget(Address::Family vrf_family, 232 : Address::Family vpn_family); 233 : 234 : std::string name_; 235 : int index_; 236 : std::unique_ptr<RouteDistinguisher> rd_; 237 : RouteTableList vrf_tables_by_name_; 238 : RouteTableFamilyList vrf_tables_by_family_; 239 : RouteTargetList import_; 240 : RouteTargetList export_; 241 : BgpServer *server_; 242 : RoutingInstanceMgr *mgr_; 243 : const BgpInstanceConfig *config_; 244 : bool is_master_; 245 : bool always_subscribe_; 246 : std::string virtual_network_; 247 : int virtual_network_index_; 248 : bool virtual_network_allow_transit_; 249 : bool virtual_network_pbb_evpn_enable_; 250 : int vxlan_id_; 251 : SandeshTraceBufferPtr trace_buffer_; 252 : boost::scoped_ptr<DeleteActor> deleter_; 253 : LifetimeRef<RoutingInstance> manager_delete_ref_; 254 : boost::scoped_ptr<IStaticRouteMgr> inet_static_route_mgr_; 255 : boost::scoped_ptr<IStaticRouteMgr> inet6_static_route_mgr_; 256 : boost::scoped_ptr<IRouteAggregator> inet_route_aggregator_; 257 : boost::scoped_ptr<IRouteAggregator> inet6_route_aggregator_; 258 : boost::scoped_ptr<PeerManager> peer_manager_; 259 : std::string mvpn_project_manager_network_; 260 : RoutingPolicyAttachList routing_policies_; 261 : std::string routing_instance_vxlan_; 262 : }; 263 : 264 : 265 : class RoutingInstanceSet : public BitSet { 266 : }; 267 : 268 : // 269 : // This class is responsible for life cycle management of RoutingInstances. 270 : // 271 : // In order to speed up creation of RoutingInstances at startup, multiple 272 : // bgp::ConfigHelper tasks running in parallel are used instead of creating 273 : // from the bgp::Config task directly. A hash of the RoutingInstance name 274 : // is calculated and the name is added to one of the instance_config_lists_. 275 : // Each list is processed independently in a bgp::ConfigHelper task. 276 : // 277 : // As RoutingInstance creation doesn't happen in-line from bgp::Config task, 278 : // creation of BgpPeers in non-master RoutingInstances needs to be deferred 279 : // till after RoutingInstance itself is created. This is handled by adding 280 : // the RoutingInstance name to neighbor_config_list_ and creating BgpPeers 281 : // from bgp::Config task. Assumption is that there will be a small number of 282 : // RoutingInstances with BgpPeers. 283 : // 284 : // As RoutingInstances can be created from parallel bgp::ConfigHelper tasks, 285 : // a mutex is used to serialize access to shared data structures such as the 286 : // instances_ map, target_map_ and vn_index_map_. 287 : // 288 : class RoutingInstanceMgr { 289 : public: 290 : typedef std::set<std::string> RoutingInstanceConfigList; 291 : typedef std::map<std::string, RoutingInstance *> RoutingInstanceList; 292 : typedef std::map<std::string, SandeshTraceBufferPtr> 293 : RoutingInstanceTraceBufferMap; 294 : typedef std::list<std::string> RoutingInstanceTraceBufferList; 295 : typedef RoutingInstanceList::iterator name_iterator; 296 : typedef RoutingInstanceList::const_iterator const_name_iterator; 297 : typedef RoutingInstanceList::iterator RoutingInstanceIterator; 298 : typedef std::multimap<RouteTarget, RoutingInstance *> InstanceTargetMap; 299 : typedef std::multimap<int, RoutingInstance *> VnIndexMap; 300 : 301 : typedef boost::function<void(std::string, int)> RoutingInstanceCb; 302 : typedef std::vector<RoutingInstanceCb> InstanceOpListenersList; 303 : typedef std::set<std::string> MvpnManagerNetworks; 304 : typedef std::map<std::string, MvpnManagerNetworks> 305 : MvpnProjectManagerNetworks; 306 : 307 : enum Operation { 308 : INSTANCE_ADD = 1, 309 : INSTANCE_UPDATE = 2, 310 : INSTANCE_DELETE = 3 311 : }; 312 : 313 : explicit RoutingInstanceMgr(BgpServer *server); 314 : virtual ~RoutingInstanceMgr(); 315 : 316 24805 : RoutingInstanceIterator begin() { 317 24805 : return instances_.begin(); 318 : } 319 : 320 89747 : RoutingInstanceIterator end() { 321 89747 : return instances_.end(); 322 : } 323 : 324 74 : name_iterator name_begin() { return instances_.begin(); } 325 4897 : name_iterator name_end() { return instances_.end(); } 326 767 : name_iterator name_lower_bound(const std::string &name) { 327 767 : return instances_.lower_bound(name); 328 : } 329 : const_name_iterator name_cbegin() { return instances_.begin(); } 330 9776 : const_name_iterator name_cend() { return instances_.end(); } 331 2557 : const_name_iterator name_clower_bound(const std::string &name) { 332 2557 : return instances_.lower_bound(name); 333 : } 334 : 335 : int RegisterInstanceOpCallback(RoutingInstanceCb cb); 336 : void NotifyInstanceOp(std::string name, Operation deleted); 337 : void UnregisterInstanceOpCallback(int id); 338 : 339 : const RoutingInstance *GetInstanceByTarget(const RouteTarget &target) const; 340 : std::string GetVirtualNetworkByVnIndex(int vn_index) const; 341 : int GetVnIndexByExtCommunity(const ExtCommunity *community) const; 342 : 343 : RoutingInstance *GetDefaultRoutingInstance(); 344 : const RoutingInstance *GetDefaultRoutingInstance() const; 345 : RoutingInstance *GetRoutingInstance(const std::string &name); 346 : const RoutingInstance *GetRoutingInstance(const std::string &name) const; 347 : RoutingInstance *GetRoutingInstanceLocked(const std::string &name); 348 : void InsertRoutingInstance(RoutingInstance *rtinstance); 349 : void LocateRoutingInstance(const BgpInstanceConfig *config); 350 : void LocateRoutingInstance(const std::string &name); 351 : RoutingInstance *CreateRoutingInstance(const BgpInstanceConfig *config); 352 : void UpdateRoutingInstance(RoutingInstance *rtinstance, 353 : const BgpInstanceConfig *config); 354 : virtual void DeleteRoutingInstance(const std::string &name); 355 : void DestroyRoutingInstance(RoutingInstance *rtinstance); 356 : 357 : // Trace buffer related functions 358 : SandeshTraceBufferPtr LocateTraceBuffer(const std::string &name); 359 : SandeshTraceBufferPtr GetTraceBuffer(const std::string &name); 360 : void DisableTraceBuffer(const std::string &name); 361 : bool HasRoutingInstanceActiveTraceBuf(const std::string &name) const; 362 : bool HasRoutingInstanceDormantTraceBuf(const std::string &name) const; 363 : SandeshTraceBufferPtr GetActiveTraceBuffer(const std::string &name) const; 364 : SandeshTraceBufferPtr GetDormantTraceBuffer(const std::string &name) const; 365 : size_t GetRoutingInstanceActiveTraceBufSize() const; 366 : size_t GetRoutingInstanceDormantTraceBufSize() const; 367 : size_t GetEnvRoutingInstanceDormantTraceBufferCapacity() const; 368 : size_t GetEnvRoutingInstanceDormantTraceBufferThreshold() const; 369 : 370 3 : size_t GetRoutingInstanceDormantTraceBufferCapacity() const { 371 3 : return dormant_trace_buf_size_; 372 : } 373 1 : size_t GetRoutingInstanceDormantTraceBufferThreshold() const { 374 1 : return trace_buf_threshold_; 375 : } 376 : 377 : bool deleted(); 378 : bool MayDelete() const; 379 : void ManagedDelete(); 380 : void Shutdown(); 381 : 382 : void CreateRoutingInstanceNeighbors(const BgpInstanceConfig *config); 383 : 384 25116 : size_t count() const { return instances_.size(); } 385 144062 : BgpServer *server() { return server_; } 386 : const BgpServer *server() const { return server_; } 387 : LifetimeActor *deleter(); 388 248721 : std::mutex &mutex() { return mutex_; } 389 : 390 259 : uint32_t deleted_count() const { return deleted_count_; } 391 51135 : void increment_deleted_count() { deleted_count_++; } 392 51135 : void decrement_deleted_count() { deleted_count_--; } 393 : 394 : bool CreateVirtualNetworkMapping(const std::string &virtual_network, 395 : const std::string &instance_name); 396 : bool DeleteVirtualNetworkMapping(const std::string &virtual_network, 397 : const std::string &instance_name); 398 : uint32_t SendTableStatsUve(); 399 : const MvpnProjectManagerNetworks &mvpn_project_managers() const { 400 : return mvpn_project_managers_; 401 : } 402 118978 : MvpnProjectManagerNetworks &mvpn_project_managers() { 403 118978 : return mvpn_project_managers_; 404 : } 405 : size_t GetMvpnProjectManagerCount(const std::string &network) const; 406 : static std::string GetPrimaryRoutingInstanceName(const string &name_in); 407 : 408 : private: 409 : friend class BgpConfigTest; 410 : friend class RoutingInstanceMgrTest; 411 : class DeleteActor; 412 : 413 : bool ProcessInstanceConfigList(int idx); 414 : bool ProcessNeighborConfigList(); 415 : 416 : void InstanceTargetAdd(RoutingInstance *rti); 417 : void InstanceTargetRemove(const RoutingInstance *rti); 418 : void InstanceVnIndexAdd(RoutingInstance *rti); 419 : void InstanceVnIndexRemove(const RoutingInstance *rti); 420 : 421 : const RoutingInstance *GetInstanceByVnIndex(int vn_index) const; 422 : int GetVnIndexByRouteTarget(const RouteTarget &rtarget) const; 423 : 424 : void ASNUpdateCallback(as_t old_asn, as_t old_local_asn); 425 : void IdentifierUpdateCallback(Ip4Address old_identifier); 426 : 427 : void DisableInstanceConfigListProcessing(); 428 : void EnableInstanceConfigListProcessing(); 429 : void DisableNeighborConfigListProcessing(); 430 : void EnableNeighborConfigListProcessing(); 431 : void SetTableStatsUve(Address::Family family, 432 : const std::map<std::string, RoutingTableStats> &stats_map, 433 : RoutingInstanceStatsData *instance_info) const; 434 : 435 : BgpServer *server_; 436 : mutable std::mutex mutex_; 437 : mutable tbb::spin_rw_mutex rw_mutex_; 438 : std::vector<RoutingInstanceConfigList> instance_config_lists_; 439 : std::vector<TaskTrigger *> instance_config_triggers_; 440 : RoutingInstanceConfigList neighbor_config_list_; 441 : boost::scoped_ptr<TaskTrigger> neighbor_config_trigger_; 442 : RoutingInstance *default_rtinstance_; 443 : RoutingInstanceList instances_; 444 : RoutingInstanceTraceBufferMap trace_buffer_active_; 445 : RoutingInstanceTraceBufferMap trace_buffer_dormant_; 446 : RoutingInstanceTraceBufferList trace_buffer_dormant_list_; 447 : InstanceTargetMap target_map_; 448 : VnIndexMap vn_index_map_; 449 : uint32_t deleted_count_; 450 : int asn_listener_id_; 451 : int identifier_listener_id_; 452 : size_t dormant_trace_buf_size_; 453 : size_t trace_buf_threshold_; 454 : boost::scoped_ptr<DeleteActor> deleter_; 455 : LifetimeRef<RoutingInstanceMgr> server_delete_ref_; 456 : boost::dynamic_bitset<> bmap_; // free list. 457 : InstanceOpListenersList callbacks_; 458 : 459 : // Map of virtual-network names to routing-instance names. 460 : typedef std::map<std::string, std::set<std::string> > VirtualNetworksMap; 461 : VirtualNetworksMap virtual_networks_; 462 : 463 : MvpnProjectManagerNetworks mvpn_project_managers_; 464 : }; 465 : 466 : #endif // SRC_BGP_ROUTING_INSTANCE_ROUTING_INSTANCE_H_