LCOV - code coverage report
Current view: top level - bgp/routing-instance - routepath_replicator.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 301 416 72.4 %
Date: 2026-11-23 02:04:52 Functions: 34 43 79.1 %
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             : #include "bgp/routing-instance/routepath_replicator.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <utility>
      11             : 
      12             : #include "base/set_util.h"
      13             : #include "base/task_annotations.h"
      14             : #include "base/task_trigger.h"
      15             : #include "bgp/bgp_config.h"
      16             : #include "bgp/bgp_log.h"
      17             : #include "bgp/bgp_route.h"
      18             : #include "bgp/bgp_server.h"
      19             : #include "bgp/origin-vn/origin_vn.h"
      20             : #include "bgp/routing-instance/path_resolver.h"
      21             : #include "bgp/routing-instance/routing_instance.h"
      22             : #include "bgp/routing-instance/rtarget_group_mgr.h"
      23             : #include "bgp/routing-instance/routing_instance_analytics_types.h"
      24             : 
      25             : using std::ostringstream;
      26             : using std::make_pair;
      27             : using std::pair;
      28             : using std::string;
      29             : using std::vector;
      30             : 
      31             : //
      32             : // RoutePathReplication trace macro. Optionally logs the server name as well for
      33             : // easier debugging in multi server unit tests
      34             : //
      35             : #define RPR_TRACE(obj, ...)                                                    \
      36             : do {                                                                           \
      37             :     if (LoggingDisabled()) break;                                              \
      38             :     bgp_log_test::LogServerName(server());                                     \
      39             :     Rpr##obj##Log::Send("RoutingInstance",                                     \
      40             :             SandeshLevel::SYS_DEBUG, __FILE__, __LINE__, __VA_ARGS__);         \
      41             :     Rpr##obj::TraceMsg(trace_buf_, __FILE__, __LINE__, __VA_ARGS__);           \
      42             : } while (false)
      43             : 
      44             : #define RPR_TRACE_ONLY(obj, ...)                                               \
      45             : do {                                                                           \
      46             :     if (LoggingDisabled()) break;                                              \
      47             :     bgp_log_test::LogServerName(server());                                     \
      48             :     Rpr##obj::TraceMsg(trace_buf_, __FILE__, __LINE__, __VA_ARGS__);           \
      49             : } while (false)
      50             : 
      51             : class TableState::DeleteActor : public LifetimeActor {
      52             : public:
      53         510 :     explicit DeleteActor(TableState *ts)
      54         510 :         : LifetimeActor(ts->replicator()->server()->lifetime_manager()),
      55         510 :           ts_(ts) {
      56         510 :     }
      57             : 
      58         510 :     virtual bool MayDelete() const {
      59         510 :         return ts_->MayDelete();
      60             :     }
      61             : 
      62         510 :     virtual void Shutdown() {
      63         510 :     }
      64             : 
      65         510 :     virtual void Destroy() {
      66         510 :         ts_->replicator()->DeleteTableState(ts_->table());
      67         510 :     }
      68             : 
      69             : private:
      70             :     TableState *ts_;
      71             : };
      72             : 
      73         510 : TableState::TableState(RoutePathReplicator *replicator, BgpTable *table)
      74         510 :     : replicator_(replicator),
      75         510 :       table_(table),
      76         510 :       listener_id_(DBTableBase::kInvalidId),
      77         510 :       deleter_(new DeleteActor(this)),
      78         510 :       table_delete_ref_(this, table->deleter()),
      79         510 :       export_target_gen_(0) {
      80         510 :     assert(table->deleter() != NULL);
      81         510 : }
      82             : 
      83         510 : TableState::~TableState() {
      84         510 :     if (walk_ref() != NULL) {
      85           0 :         table()->ReleaseWalker(walk_ref());
      86             :     }
      87         510 : }
      88             : 
      89         510 : void TableState::ManagedDelete() {
      90         510 :     deleter()->Delete();
      91         510 : }
      92             : 
      93           0 : bool TableState::deleted() const {
      94           0 :     return deleter()->IsDeleted();
      95             : }
      96             : 
      97        4374 : LifetimeActor *TableState::deleter() {
      98        4374 :     return deleter_.get();
      99             : }
     100             : 
     101           0 : const LifetimeActor *TableState::deleter() const {
     102           0 :     return deleter_.get();
     103             : }
     104             : 
     105         510 : bool TableState::MayDelete() const {
     106        1020 :     if (list_.empty() && !route_count() &&
     107         510 :         ((walk_ref() == NULL) || !walk_ref()->walk_is_active()))
     108         510 :         return true;
     109           0 :     return false;
     110             : }
     111             : 
     112        3864 : void TableState::RetryDelete() {
     113        3864 :     if (!deleter()->IsDeleted())
     114        3864 :         return;
     115           0 :     deleter()->RetryDelete();
     116             : }
     117             : 
     118         510 : void TableState::AddGroup(RtGroup *group) {
     119         510 :     list_.insert(group);
     120         510 : }
     121             : 
     122         510 : void TableState::RemoveGroup(RtGroup *group) {
     123         510 :     list_.erase(group);
     124         510 : }
     125             : 
     126         340 : const RtGroup *TableState::FindGroup(RtGroup *group) const {
     127         340 :     GroupList::const_iterator it = list_.find(group);
     128         340 :     return (it != list_.end() ? *it : NULL);
     129             : }
     130             : 
     131         510 : uint32_t TableState::route_count() const {
     132         510 :     return table_->GetDBStateCount(listener_id());
     133             : }
     134             : 
     135        4934 : RtReplicated::RtReplicated(RoutePathReplicator *replicator)
     136        4934 :     : replicator_(replicator),
     137        4934 :       export_target_gen_(0) {
     138        4934 : }
     139             : 
     140        1040 : void RtReplicated::AddRouteInfo(BgpTable *table, BgpRoute *rt,
     141             :     ReplicatedRtPathList::const_iterator it) {
     142        1040 :     pair<ReplicatedRtPathList::iterator, bool> result;
     143        1040 :     result = replicate_list_.insert(*it);
     144        1040 :     assert(result.second);
     145        1040 : }
     146             : 
     147        1040 : void RtReplicated::DeleteRouteInfo(BgpTable *table, BgpRoute *rt,
     148             :     ReplicatedRtPathList::const_iterator it) {
     149        1040 :     replicator_->DeleteSecondaryPath(table, rt, *it);
     150        1040 :     replicate_list_.erase(it);
     151        1040 : }
     152             : 
     153             : //
     154             : // Return the list of secondary table names for the given primary path.
     155             : // We go through all SecondaryRouteInfos and skip the ones that don't
     156             : // match the primary path.
     157             : //
     158           0 : vector<string> RtReplicated::GetTableNameList(const BgpPath *path) const {
     159           0 :     vector<string> table_list;
     160           0 :     BOOST_FOREACH(const SecondaryRouteInfo &rinfo, replicate_list_) {
     161           0 :         if (rinfo.peer_ != path->GetPeer())
     162           0 :             continue;
     163           0 :         if (rinfo.path_id_ != path->GetPathId())
     164           0 :             continue;
     165           0 :         if (rinfo.src_ != path->GetSource())
     166           0 :             continue;
     167           0 :         table_list.push_back(rinfo.table_->name());
     168             :     }
     169           0 :     return table_list;
     170           0 : }
     171             : 
     172         340 : RoutePathReplicator::RoutePathReplicator(BgpServer *server,
     173         340 :     Address::Family family)
     174         340 :     : server_(server),
     175         340 :       family_(family),
     176         340 :       vpn_table_(NULL),
     177         340 :       trace_buf_(SandeshTraceBufferCreate("RoutePathReplicator", 500)) {
     178         340 : }
     179             : 
     180         680 : RoutePathReplicator::~RoutePathReplicator() {
     181         340 :     assert(table_state_list_.empty());
     182         680 : }
     183             : 
     184         340 : void RoutePathReplicator::Initialize() {
     185         340 :     assert(!vpn_table_);
     186         340 :     RoutingInstanceMgr *mgr = server_->routing_instance_mgr();
     187         340 :     assert(mgr);
     188         340 :     RoutingInstance *master = mgr->GetDefaultRoutingInstance();
     189         340 :     assert(master);
     190         340 :     vpn_table_ = master->GetTable(family_);
     191         340 :     assert(vpn_table_);
     192         340 :     assert(AddTableState(vpn_table_));
     193         340 : }
     194             : 
     195         850 : TableState *RoutePathReplicator::AddTableState(BgpTable *table,
     196             :     RtGroup *group) {
     197         850 :     assert(table->IsVpnTable() || group);
     198             : 
     199         850 :     TableStateList::iterator loc = table_state_list_.find(table);
     200         850 :     if (loc == table_state_list_.end()) {
     201         510 :         TableState *ts = new TableState(this, table);
     202         510 :         DBTableBase::ListenerId id = table->Register(
     203             :             boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
     204             :             "RoutePathReplicator");
     205         510 :         ts->set_listener_id(id);
     206         510 :         if (group)
     207         170 :             ts->AddGroup(group);
     208         510 :         table_state_list_.insert(make_pair(table, ts));
     209         510 :         RPR_TRACE(RegTable, table->name());
     210         510 :         return ts;
     211             :     } else {
     212         340 :         assert(group);
     213         340 :         TableState *ts = loc->second;
     214         340 :         ts->AddGroup(group);
     215         340 :         return ts;
     216             :     }
     217             : }
     218             : 
     219         510 : void RoutePathReplicator::RemoveTableState(BgpTable *table, RtGroup *group) {
     220         510 :     TableState *ts = FindTableState(table);
     221         510 :     assert(ts);
     222         510 :     ts->RemoveGroup(group);
     223         510 : }
     224             : 
     225         510 : void RoutePathReplicator::DeleteTableState(BgpTable *table) {
     226         510 :     TableState *ts = FindTableState(table);
     227         510 :     assert(ts);
     228         510 :     RPR_TRACE(UnregTable, table->name());
     229         510 :     table->Unregister(ts->listener_id());
     230         510 :     table_state_list_.erase(table);
     231         510 :     delete ts;
     232         510 : }
     233             : 
     234        1870 : TableState *RoutePathReplicator::FindTableState(BgpTable *table) {
     235        1870 :     TableStateList::iterator loc = table_state_list_.find(table);
     236        1870 :     return (loc != table_state_list_.end() ? loc->second : NULL);
     237             : }
     238             : 
     239           0 : const TableState *RoutePathReplicator::FindTableState(
     240             :     const BgpTable *table) const {
     241             :     TableStateList::const_iterator loc =
     242           0 :         table_state_list_.find(const_cast<BgpTable *>(table));
     243           0 :     return (loc != table_state_list_.end() ? loc->second : NULL);
     244             : }
     245             : 
     246             : void
     247           0 : RoutePathReplicator::RequestWalk(BgpTable *table) {
     248           0 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
     249           0 :     TableState *ts = FindTableState(table);
     250           0 :     assert(ts);
     251           0 :     if (!ts->walk_ref()) {
     252             :         DBTable::DBTableWalkRef walk_ref = table->AllocWalker(
     253             :           boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
     254           0 :           boost::bind(&RoutePathReplicator::BulkReplicationDone, this, _2));
     255           0 :         table->WalkTable(walk_ref);
     256           0 :         ts->set_walk_ref(walk_ref);
     257           0 :     } else {
     258           0 :         table->WalkAgain(ts->walk_ref());
     259             :     }
     260           0 : }
     261             : 
     262             : void
     263           0 : RoutePathReplicator::BulkReplicationDone(DBTableBase *dbtable) {
     264           0 :     CHECK_CONCURRENCY("db::Walker");
     265           0 :     BgpTable *table = static_cast<BgpTable *>(dbtable);
     266           0 :     RPR_TRACE(WalkDone, table->name());
     267           0 :     TableState *ts = FindTableState(table);
     268           0 :     ts->RetryDelete();
     269           0 : }
     270             : 
     271         340 : void RoutePathReplicator::JoinVpnTable(RtGroup *group) {
     272         340 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
     273         340 :     TableState *vpn_ts = FindTableState(vpn_table_);
     274         340 :     if (!vpn_ts || vpn_ts->FindGroup(group))
     275           0 :         return;
     276         340 :     RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), true);
     277         340 :     group->AddImportTable(family(), vpn_table_);
     278         340 :     RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), false);
     279         340 :     group->AddExportTable(family(), vpn_table_);
     280         340 :     AddTableState(vpn_table_, group);
     281             : }
     282             : 
     283         340 : void RoutePathReplicator::LeaveVpnTable(RtGroup *group) {
     284         340 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
     285         340 :     TableState *vpn_ts = FindTableState(vpn_table_);
     286         340 :     if (!vpn_ts)
     287           0 :         return;
     288         340 :     RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), true);
     289         340 :     group->RemoveImportTable(family(), vpn_table_);
     290         340 :     RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), false);
     291         340 :     group->RemoveExportTable(family(), vpn_table_);
     292         340 :     RemoveTableState(vpn_table_, group);
     293             : }
     294             : 
     295             : //
     296             : // Add a given BgpTable to RtGroup of given RouteTarget.
     297             : // It will create a new RtGroup if none exists.
     298             : // In case of export RouteTarget, create TableState if it doesn't exist.
     299             : //
     300         510 : void RoutePathReplicator::Join(BgpTable *table, const RouteTarget &rt,
     301             :                                bool import) {
     302         510 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
     303             : 
     304         510 :     std::scoped_lock lock(mutex_);
     305         510 :     RPR_TRACE(TableJoin, table->name(), rt.ToString(), import);
     306             : 
     307         510 :     bool first = false;
     308         510 :     RtGroup *group = server()->rtarget_group_mgr()->LocateRtGroup(rt);
     309         510 :     if (import) {
     310         340 :         first = group->AddImportTable(family(), table);
     311         340 :         if (group->HasDepRoutes())
     312           0 :             server()->rtarget_group_mgr()->NotifyRtGroup(rt);
     313         340 :         if (family_ == Address::INETVPN)
     314          68 :             server_->NotifyAllStaticRoutes();
     315         340 :         BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
     316           0 :             if (sec_table->IsVpnTable() || sec_table->empty())
     317           0 :                 continue;
     318           0 :             RequestWalk(sec_table);
     319             :         }
     320             :     } else {
     321         170 :         first = group->AddExportTable(family(), table);
     322         170 :         TableState *ts = AddTableState(table, group);
     323             :         // Export targets changed, so the attributes of the paths replicated
     324             :         // earlier are stale. Bump before the walk, so that RouteListener does
     325             :         // not take the optimized path for them.
     326         170 :         ts->bump_export_target_gen();
     327         170 :         if (!table->empty()) {
     328           0 :             RequestWalk(table);
     329             :         }
     330             :     }
     331             : 
     332             :     // Join the vpn table when group is created.
     333         510 :     if (first)
     334         340 :         JoinVpnTable(group);
     335         510 : }
     336             : 
     337             : //
     338             : // Remove a BgpTable from RtGroup of given RouteTarget.
     339             : // If the last group is going away, the RtGroup will be removed
     340             : // In case of export RouteTarget, trigger remove of TableState appropriate.
     341             : //
     342         510 : void RoutePathReplicator::Leave(BgpTable *table, const RouteTarget &rt,
     343             :                                 bool import) {
     344         510 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
     345             : 
     346         510 :     std::scoped_lock lock(mutex_);
     347         510 :     RtGroup *group = server()->rtarget_group_mgr()->GetRtGroup(rt);
     348         510 :     assert(group);
     349         510 :     RPR_TRACE(TableLeave, table->name(), rt.ToString(), import);
     350             : 
     351         510 :     if (import) {
     352         340 :         group->RemoveImportTable(family(), table);
     353         340 :         if (group->HasDepRoutes())
     354           0 :             server()->rtarget_group_mgr()->NotifyRtGroup(rt);
     355         340 :         if (family_ == Address::INETVPN)
     356          68 :             server_->NotifyAllStaticRoutes();
     357        1360 :         BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
     358         510 :             if (sec_table->IsVpnTable() || sec_table->empty())
     359         510 :                 continue;
     360           0 :             RequestWalk(sec_table);
     361             :         }
     362             :     } else {
     363         170 :         group->RemoveExportTable(family(), table);
     364         170 :         RemoveTableState(table, group);
     365             :         // The paths replicated earlier still carry the target being removed.
     366         170 :         FindTableState(table)->bump_export_target_gen();
     367         170 :         if (!table->empty()) {
     368           0 :             RequestWalk(table);
     369             :         }
     370             :     }
     371             : 
     372             :     // Leave the vpn table when the last VRF has left the group.
     373         510 :     if (!group->HasVrfTables(family())) {
     374         340 :         LeaveVpnTable(group);
     375         340 :         server()->rtarget_group_mgr()->RemoveRtGroup(rt);
     376             :     }
     377         510 : }
     378             : 
     379        7389 : void RoutePathReplicator::DBStateSync(BgpTable *table, TableState *ts,
     380             :     BgpRoute *rt, RtReplicated *dbstate,
     381             :     const RtReplicated::ReplicatedRtPathList *future) {
     382        7389 :     set_synchronize(dbstate->GetMutableList(), future,
     383             :         boost::bind(&RtReplicated::AddRouteInfo, dbstate, table, rt, _1),
     384             :         boost::bind(&RtReplicated::DeleteRouteInfo, dbstate, table, rt, _1));
     385             : 
     386        7388 :     if (dbstate->GetList().empty()) {
     387        4935 :         rt->ClearState(table, ts->listener_id());
     388        4935 :         delete dbstate;
     389        4935 :         if (table->GetDBStateCount(ts->listener_id()) == 0)
     390        3864 :             ts->RetryDelete();
     391             :     }
     392        7387 : }
     393             : 
     394        2271 : static ExtCommunityPtr UpdateOriginVn(BgpServer *server,
     395             :                                       const ExtCommunity *ext_community,
     396             :                                       int vn_index) {
     397        2271 :     as_t asn = server->autonomous_system();
     398        2271 :     ExtCommunityPtr extcomm_ptr;
     399        2271 :     if (vn_index) {
     400           0 :         if (asn > AS2_MAX && vn_index > 0xffff) {
     401           0 :             OriginVn origin_vn(asn, AS_TRANS);
     402           0 :             extcomm_ptr = server->extcomm_db()->ReplaceOriginVnAndLocate(
     403           0 :                 ext_community, origin_vn.GetExtCommunity());
     404           0 :             OriginVn origin_vn2(AS_TRANS, vn_index);
     405           0 :             extcomm_ptr = server->extcomm_db()->AppendAndLocate(
     406           0 :                 extcomm_ptr.get(), origin_vn2.GetExtCommunity());
     407           0 :         } else {
     408           0 :             OriginVn origin_vn(asn, vn_index);
     409           0 :             extcomm_ptr = server->extcomm_db()->ReplaceOriginVnAndLocate(
     410           0 :                 ext_community, origin_vn.GetExtCommunity());
     411           0 :             return extcomm_ptr;
     412             :         }
     413             :     }
     414        2271 :     extcomm_ptr = server->extcomm_db()->RemoveOriginVnAndLocate(ext_community);
     415        2276 :     return extcomm_ptr;
     416           0 : }
     417             : 
     418             : //
     419             : // Update the ExtCommunity with the RouteTargets from the export list
     420             : // and the OriginVn. The OriginVn is derived from the RouteTargets in
     421             : // vpn routes.
     422             : //
     423        4977 : static ExtCommunityPtr UpdateExtCommunity(BgpServer *server,
     424             :         const RoutingInstance *rtinstance, const ExtCommunity *ext_community,
     425             :         const ExtCommunity::ExtCommunityList &export_list) {
     426             :     // Add RouteTargets exported by the instance for a non-master instance.
     427        4977 :     ExtCommunityPtr extcomm_ptr;
     428        4977 :     if (!rtinstance->IsMasterRoutingInstance()) {
     429             :         extcomm_ptr =
     430        2705 :             server->extcomm_db()->AppendAndLocate(ext_community, export_list);
     431        2708 :         return extcomm_ptr;
     432             :     }
     433             : 
     434             :     // Bail if we have a vpn route without extended communities.
     435        2272 :     if (!ext_community)
     436           0 :         return ExtCommunityPtr(NULL);
     437             : 
     438             :     // Nothing to do if we already have the OriginVn community with our AS
     439             :     // or with a vn index from the global range.
     440       21870 :     BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     441             :                   ext_community->communities()) {
     442        9801 :         if (!ExtCommunity::is_origin_vn(comm))
     443        9799 :             continue;
     444           0 :         OriginVn origin_vn(comm);
     445           0 :         if (!origin_vn.IsGlobal() &&
     446           0 :             origin_vn.as_number() != server->autonomous_system()) {
     447           0 :             continue;
     448             :         }
     449           0 :         return ExtCommunityPtr(ext_community);
     450             :     }
     451             : 
     452             :     // Add the OriginVn if we have a valid vn index.
     453             :     int vn_index =
     454        2272 :         server->routing_instance_mgr()->GetVnIndexByExtCommunity(ext_community);
     455        2271 :     return UpdateOriginVn(server, ext_community, vn_index);
     456        4984 : }
     457             : 
     458             : //
     459             : // Concurrency: Called in the context of the DB partition task.
     460             : //
     461             : // This function handles
     462             : //   1. Table Notification for path replication
     463             : //   2. Table walk for import/export of new targets
     464             : //
     465             : // Replicate a path (clone the BgpPath) to secondary BgpTables based on the
     466             : // export targets of the primary BgpTable.
     467             : // If primary table is a VRF table attach it's export targets to replicated
     468             : // path in the VPN table.
     469             : //
     470        9845 : bool RoutePathReplicator::RouteListener(TableState *ts,
     471             :     DBTablePartBase *root, DBEntryBase *entry) {
     472        9845 :     CHECK_CONCURRENCY("db::DBTable");
     473             : 
     474        9841 :     BgpTable *table = static_cast<BgpTable *>(root->parent());
     475        9841 :     BgpRoute *rt = static_cast<BgpRoute *>(entry);
     476        9841 :     const RoutingInstance *rtinstance = table->routing_instance();
     477             : 
     478        9841 :     DBTableBase::ListenerId id = ts->listener_id();
     479        9840 :     assert(id != DBTableBase::kInvalidId);
     480             : 
     481             :     // Get the DBState.
     482             :     RtReplicated *dbstate =
     483        9840 :         static_cast<RtReplicated *>(rt->GetState(table, id));
     484        9847 :     RtReplicated::ReplicatedRtPathList replicated_path_list;
     485             : 
     486             :     //Flag to track if any change happenned to route in last 30 minutes
     487        9847 :     bool route_unchanged = false;
     488             :     //List of tables that needs replication of route
     489        9847 :     RtGroup::RtGroupMemberList addedTables;
     490             :     //List of tables that doesn't need replication of route
     491        9842 :     RtGroup::RtGroupMemberList deletedTables;
     492             :     //List of tables having the latest changes in route
     493        9843 :     RtGroup::RtGroupMemberList previousTables;
     494             :     //Threshold used for optimised replication
     495        9843 :     uint64_t optimizationThresholdTime = 30ULL * 60 * 1000000;
     496        9843 :     uint64_t start = UTCTimestampUsec();
     497             :     //Read once: a change racing with this walk must not be recorded as
     498             :     //applied. The walk it requests runs after this one and sees the mismatch.
     499        9846 :     uint64_t export_target_gen = ts->export_target_gen();
     500             : 
     501             :     //Optimize only if last route change occurred 30 mins before and the
     502             :     //export targets of the primary table are unchanged since the route was
     503             :     //last replicated - the optimized path skips every destination table that
     504             :     //is in both the old and the new set, which for a route in a VRF always
     505             :     //includes the VPN table, leaving it with the previous export targets.
     506             :     //We update the route_unchanged flag to true
     507        9846 :     if ( (start - rt->last_update_at() > optimizationThresholdTime) &&
     508        9846 :           dbstate != NULL &&
     509           0 :           dbstate->export_target_gen() == export_target_gen)
     510             :     {
     511           0 :         RPR_TRACE(RouteListener, "Route change occurred before threshold \
     512             :         time, optimising replication");
     513           0 :         route_unchanged = true;
     514             :     }
     515             : 
     516             :     //
     517             :     // Cleanup if the route is not usable.
     518             :     // If route aggregation is enabled, contributing route/more specific route
     519             :     // for a aggregate route will NOT be replicated to destination table
     520             :     //
     521       11117 :     if (!rt->IsUsable() || (table->IsRouteAggregationSupported() &&
     522        1271 :                             !rtinstance->deleted() &&
     523        1271 :                             table->IsContributingRoute(rt))) {
     524        3226 :         if (!dbstate) {
     525        2458 :             return true;
     526             :         }
     527         768 :         DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
     528         768 :         return true;
     529             :     }
     530             : 
     531             :     // Create and set new DBState on the route.  This will get cleaned up via
     532             :     // via the call to DBStateSync if we don't need to replicate the route to
     533             :     // any tables.
     534        6618 :     if (dbstate == NULL) {
     535        4934 :         dbstate = new RtReplicated(this);
     536        4934 :         rt->SetState(table, id, dbstate);
     537             :     }
     538             : 
     539             :     // Get the export route target list from the routing instance.
     540        6621 :     ExtCommunity::ExtCommunityList export_list;
     541        6617 :     if (!rtinstance->IsMasterRoutingInstance()) {
     542        7784 :         BOOST_FOREACH(RouteTarget rtarget, rtinstance->GetExportList()) {
     543        2596 :             export_list.push_back(rtarget.GetExtCommunity());
     544             :         }
     545             :     }
     546             : 
     547             :     // Updating previousTables with the list of tables having the information
     548             :     // about the route.
     549        6615 :     if (route_unchanged){
     550           0 :         replicated_path_list = dbstate->GetList();
     551           0 :         BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
     552             :                        replicated_path_list){
     553           0 :             previousTables.insert(path.table_);
     554             :         }
     555             :     }
     556             : 
     557             :     // Replicate all feasible and non-replicated paths.
     558        6615 :     for (Route::PathList::iterator it = rt->GetPathList().begin();
     559       27236 :         it != rt->GetPathList().end(); ++it) {
     560        9083 :         BgpPath *path = static_cast<BgpPath *>(it.operator->());
     561             : 
     562             :         // Skip if the source peer is down.
     563       15204 :         if (!path->IsStale() && !path->IsLlgrStale() && path->GetPeer() &&
     564        6121 :             !path->GetPeer()->IsReady())
     565        4297 :             continue;
     566             : 
     567             :         // No need to replicate aliased or replicated paths.
     568        8940 :         if (path->IsReplicated() || path->IsAliased())
     569        1876 :             continue;
     570             : 
     571             :         // Do not replicate non-ecmp paths.
     572        7063 :         if (rt->BestPath()->PathCompare(*path, true))
     573        2084 :             break;
     574             : 
     575             :         // Do not replicate if nexthop is not hitched by ermvpn tree.
     576        4977 :         if (path->CheckErmVpn())
     577           0 :             break;
     578             : 
     579        4977 :         const BgpAttr *attr = path->GetAttr();
     580        4977 :         const ExtCommunity *ext_community = attr->ext_community();
     581             : 
     582             :         ExtCommunityPtr extcomm_ptr =
     583        4977 :           UpdateExtCommunity(server(), rtinstance, ext_community, export_list);
     584        4984 :         ext_community = extcomm_ptr.get();
     585        4984 :         if (!ext_community)
     586           0 :             continue;
     587             : 
     588             :         // Go through all extended communities.
     589             :         //
     590             :         // Get the vn_index from the OriginVn extended community.
     591             :         // For each RouteTarget extended community, get the list of tables
     592             :         // to which we need to replicate the path.
     593        4984 :         int vn_index = 0;
     594        4984 :         RtGroup::RtGroupMemberList secondary_tables;
     595       43916 :         BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     596             :                       ext_community->communities()) {
     597       19466 :             if (ExtCommunity::is_origin_vn(comm)) {
     598           0 :                 OriginVn origin_vn(comm);
     599           0 :                 vn_index = origin_vn.vn_index();
     600       19464 :             } else if (ExtCommunity::is_route_target(comm)) {
     601             :                 RtGroup *group =
     602        4984 :                     server()->rtarget_group_mgr()->GetRtGroup(comm);
     603        4985 :                 if (!group)
     604        2277 :                     continue;
     605             :                 RtGroup::RtGroupMemberList import_list =
     606        4953 :                     group->GetImportTables(family());
     607        4953 :                 if (import_list.empty())
     608        2245 :                     continue;
     609        2708 :                 secondary_tables.insert(import_list.begin(), import_list.end());
     610        4953 :             }
     611             :         }
     612             : 
     613             :         // Update with family specific secondary tables.
     614        4984 :         table->UpdateSecondaryTablesForReplication(rt, &secondary_tables);
     615             : 
     616             :         // Skip if we don't need to replicate the path to any tables.
     617        4984 :         if (secondary_tables.empty())
     618        2276 :             continue;
     619             : 
     620             :         // Add OriginVn when replicating self-originated routes from a VRF.
     621        2708 :         if (!vn_index && !rtinstance->IsMasterRoutingInstance() &&
     622        5416 :             path->IsVrfOriginated() && rtinstance->virtual_network_index()) {
     623           0 :             vn_index = rtinstance->virtual_network_index();
     624           0 :             extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(), vn_index);
     625             :         }
     626             : 
     627        2708 :         if (route_unchanged){
     628           0 :             addedTables = RtGroup::RtGroupMemberList();
     629             :             // Finding the difference between sets, secondary_tables and
     630             :             // previousTables
     631           0 :             std::set_difference(secondary_tables.begin(), secondary_tables.end(),
     632             :                                 previousTables.begin(), previousTables.end(),
     633             :                                 std::inserter(addedTables, addedTables.end()));
     634           0 :             std::set_difference(previousTables.begin(), previousTables.end(),
     635             :                                 secondary_tables.begin(), secondary_tables.end(),
     636             :                                 std::inserter(deletedTables, deletedTables.end()));
     637           0 :             BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
     638             :                        dbstate->GetList()){
     639           0 :                 if (deletedTables.find(path.table_)!=deletedTables.end()) {
     640           0 :                     replicated_path_list.erase(path);
     641             :                 }
     642             :             }
     643             :             //Updating the secondary tables with the difference.
     644           0 :             secondary_tables = addedTables;
     645             : 
     646             :         }
     647             : 
     648             : 
     649             :         // Replicate path to all destination tables.
     650       13540 :         BOOST_FOREACH(BgpTable *dest, secondary_tables) {
     651             :             // Skip if destination is same as source table.
     652        5416 :             if (dest == table)
     653        2708 :                 continue;
     654             : 
     655        2708 :             const RoutingInstance *dest_rtinstance = dest->routing_instance();
     656        2708 :             ExtCommunityPtr new_extcomm_ptr = extcomm_ptr;
     657             : 
     658             :             // If the origin vn is unresolved, see if route has a RouteTarget
     659             :             // that's in the set of export RouteTargets for the dest instance.
     660             :             // If so, we set the origin vn for the replicated route to be the
     661             :             // vn for the dest instance.
     662        2708 :             if (!vn_index && dest_rtinstance->virtual_network_index() &&
     663           0 :                 dest_rtinstance->HasExportTarget(ext_community)) {
     664           0 :                 int dest_vn_index = dest_rtinstance->virtual_network_index();
     665           0 :                 new_extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(),
     666           0 :                                                  dest_vn_index);
     667             :             }
     668             : 
     669             :             // Replicate the route to the destination table.  The destination
     670             :             // table may decide to not replicate based on it's own policy e.g.
     671             :             // multicast routes are never leaked across routing-instances.
     672        2708 :             BgpRoute *replicated_rt = dest->RouteReplicate(
     673             :                 server_, table, rt, path, new_extcomm_ptr);
     674        2708 :             if (!replicated_rt)
     675           0 :                 continue;
     676             : 
     677             :             // Add information about the secondary path to the replicated path
     678             :             // list.
     679        2708 :             RtReplicated::SecondaryRouteInfo rtinfo(dest, path->GetPeer(),
     680        5416 :                 path->GetPathId(), path->GetSource(), replicated_rt);
     681        2708 :             pair<RtReplicated::ReplicatedRtPathList::iterator, bool> result;
     682        2708 :             result = replicated_path_list.insert(rtinfo);
     683             :             // Assert if the insertion to replication path list fails
     684        2708 :             if (!route_unchanged)
     685        2708 :                 assert(result.second);
     686        2708 :             RPR_TRACE_ONLY(Replicate, table->name(), rt->ToString(),
     687             :                            path->ToString(),
     688             :                            BgpPath::PathIdString(path->GetPathId()),
     689             :                            dest->name(), replicated_rt->ToString());
     690        2708 :         }
     691        7260 :     }
     692             : 
     693             :     // Record what this replication was done with, for the next walk.
     694        6618 :     dbstate->set_export_target_gen(export_target_gen);
     695             : 
     696             :     // Update the DBState to reflect the new list of secondary paths. The
     697             :     // DBState will get cleared if the list is empty.
     698        6621 :     DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
     699        6619 :     return true;
     700        9845 : }
     701           0 : const RtReplicated *RoutePathReplicator::GetReplicationState(
     702             :         BgpTable *table, BgpRoute *rt) const {
     703           0 :     const TableState *ts = FindTableState(table);
     704           0 :     if (!ts)
     705           0 :         return NULL;
     706             :     RtReplicated *dbstate =
     707           0 :         static_cast<RtReplicated *>(rt->GetState(table, ts->listener_id()));
     708           0 :     return dbstate;
     709             : }
     710             : 
     711             : //
     712             : // Return the list of secondary table names for the given primary path.
     713             : //
     714           0 : vector<string> RoutePathReplicator::GetReplicatedTableNameList(
     715             :     const BgpTable *table, const BgpRoute *rt, const BgpPath *path) const {
     716           0 :     const TableState *ts = FindTableState(table);
     717           0 :     if (!ts)
     718           0 :         return vector<string>();
     719             :     const RtReplicated *dbstate = static_cast<const RtReplicated *>(
     720           0 :         rt->GetState(table, ts->listener_id()));
     721           0 :     if (!dbstate)
     722           0 :         return vector<string>();
     723           0 :     return dbstate->GetTableNameList(path);
     724             : }
     725             : 
     726        1040 : void RoutePathReplicator::DeleteSecondaryPath(BgpTable *table, BgpRoute *rt,
     727             :     const RtReplicated::SecondaryRouteInfo &rtinfo) {
     728        1040 :     BgpRoute *rt_secondary = rtinfo.rt_;
     729        1040 :     BgpTable *secondary_table = rtinfo.table_;
     730        1040 :     const IPeer *peer = rtinfo.peer_;
     731        1040 :     uint32_t path_id = rtinfo.path_id_;
     732        1040 :     BgpPath::PathSource src = rtinfo.src_;
     733             : 
     734             :     DBTablePartBase *partition =
     735        1040 :         secondary_table->GetTablePartition(rt_secondary);
     736        1040 :     BgpPath *secondary_path = rt_secondary->FindSecondaryPath(rt, src,
     737             :                                                               peer, path_id);
     738        1040 :     if (secondary_path && secondary_path->NeedsResolution()) {
     739           0 :        secondary_table->path_resolver()->StopPathResolution(partition->index(),
     740             :                                                        secondary_path);
     741             :     }
     742        1040 :     assert(rt_secondary->RemoveSecondaryPath(rt, src, peer, path_id));
     743        1040 :     if (rt_secondary->count() == 0) {
     744         912 :         RPR_TRACE_ONLY(Flush, secondary_table->name(), rt_secondary->ToString(),
     745             :                        peer ? peer->ToString() : "Nil",
     746             :                        BgpPath::PathIdString(path_id), table->name(),
     747             :                        rt->ToString(), "Delete");
     748         912 :         partition->Delete(rt_secondary);
     749             :     } else {
     750         128 :         partition->Notify(rt_secondary);
     751         128 :         RPR_TRACE_ONLY(Flush, secondary_table->name(), rt_secondary->ToString(),
     752             :                        peer ? peer->ToString() : "Nil",
     753             :                        BgpPath::PathIdString(path_id), table->name(),
     754             :                        rt->ToString(), "Path update");
     755             :     }
     756        1040 : }
     757             : 
     758           0 : string RtReplicated::SecondaryRouteInfo::ToString() const {
     759           0 :     ostringstream out;
     760           0 :     out << table_->name() << "(" << table_ << ")" << ":" <<
     761           0 :         peer_->ToString() << "(" << peer_ << ")" << ":" <<
     762           0 :         rt_->ToString() << "(" << rt_ << ")";
     763           0 :     return out.str();
     764           0 : }

Generated by: LCOV version 1.14