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

Generated by: LCOV version 1.14