LCOV - code coverage report
Current view: top level - bgp - bgp_mvpn.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 188 757 24.8 %
Date: 2026-09-21 02:11:39 Functions: 44 127 34.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "bgp/bgp_mvpn.h"
       6             : 
       7             : #include <utility>
       8             : 
       9             : #include <boost/foreach.hpp>
      10             : 
      11             : #include "base/task_annotations.h"
      12             : #include "bgp/ermvpn/ermvpn_route.h"
      13             : #include "bgp/ermvpn/ermvpn_table.h"
      14             : #include "bgp/extended-community/vrf_route_import.h"
      15             : #include "bgp/bgp_log.h"
      16             : #include "bgp/bgp_multicast.h"
      17             : #include "bgp/bgp_server.h"
      18             : #include "bgp/bgp_update.h"
      19             : #include "bgp/mvpn/mvpn_table.h"
      20             : #include "bgp/routing-instance/path_resolver.h"
      21             : #include "bgp/routing-instance/routing_instance.h"
      22             : #include "bgp/routing-instance/routing_instance_analytics_types.h"
      23             : #include "bgp/routing-instance/routing_instance_log.h"
      24             : #include "bgp/rtarget/rtarget_address.h"
      25             : #include "bgp/tunnel_encap/tunnel_encap.h"
      26             : 
      27             : using std::make_pair;
      28             : using std::ostringstream;
      29             : using std::pair;
      30             : using std::string;
      31             : using std::vector;
      32             : 
      33             : // A global MVPN state for a given <S.G> within a MvpnProjectManager.
      34           0 : MvpnState::MvpnState(const SG &sg, StatesMap *states, MvpnProjectManager *pm) :
      35           0 :         sg_(sg), global_ermvpn_tree_rt_(NULL), spmsi_rt_(NULL),
      36           0 :         source_active_rt_(NULL), states_(states), project_manager_(pm) {
      37           0 :     refcount_ = 0;
      38           0 : }
      39             : 
      40           0 : MvpnState::~MvpnState() {
      41           0 :     assert(!global_ermvpn_tree_rt_);
      42           0 :     assert(!spmsi_rt_);
      43           0 :     assert(!source_active_rt_);
      44           0 :     assert(spmsi_routes_received_.empty());
      45           0 :     assert(leafad_routes_attr_received_.empty());
      46           0 :     MVPN_TRACE(MvpnStateCreate, sg_.source.to_string(), sg_.group.to_string());
      47           0 : }
      48             : 
      49           0 : const ErmVpnTable *MvpnState::table() const {
      50           0 :     return project_manager_ ? project_manager_->table() : NULL;
      51             : }
      52             : 
      53             : // MvpnProjectManager is deleted when parent ErmVpnTable is deleted.
      54             : class MvpnProjectManager::DeleteActor : public LifetimeActor {
      55             : public:
      56          96 :     explicit DeleteActor(MvpnProjectManager *manager)
      57          96 :         : LifetimeActor(manager->table_->routing_instance()->server()->
      58          96 :                 lifetime_manager()), manager_(manager) {
      59          96 :     }
      60             : 
      61         192 :     virtual ~DeleteActor() {
      62         192 :     }
      63             : 
      64          96 :     virtual bool MayDelete() const {
      65          96 :         CHECK_CONCURRENCY("bgp::Config");
      66          96 :         return manager_->MayDelete();
      67             :     }
      68             : 
      69          96 :     virtual void Shutdown() {
      70          96 :     }
      71             : 
      72          96 :     virtual void Destroy() {
      73          96 :         manager_->table_->DestroyMvpnProjectManager();
      74          96 :     }
      75             : 
      76             : private:
      77             :     MvpnProjectManager *manager_;
      78             : };
      79             : 
      80             : // Create MvpnProjectManager object and take a lifetime reference to the
      81             : // parent ErmVpnTable object.
      82          96 : MvpnProjectManager::MvpnProjectManager(ErmVpnTable *table)
      83          96 :         : table_(table),
      84          96 :           listener_id_(DBTable::kInvalidId),
      85          96 :           table_delete_ref_(this, table->deleter()) {
      86          96 :     deleter_.reset(new DeleteActor(this));
      87          96 : }
      88             : 
      89         192 : MvpnProjectManager::~MvpnProjectManager() {
      90         192 : }
      91             : 
      92             : // MvpnProjectManager can be deleted only after all <S,G> MvpnState objects
      93             : // are deleted from the map.
      94          96 : bool MvpnProjectManager::MayDelete() const {
      95         288 :     BOOST_FOREACH(const MvpnProjectManagerPartition *partition, partitions_) {
      96          96 :         if (!partition->states().empty()) {
      97           0 :             MVPN_LOG(MvpnProjectManagerDelete,
      98             :                 "MvpnProjectManager::MayDelete() paused due to pending " +
      99             :                 integerToString(partition->states().size()) + " MvpnStates");
     100           0 :             return false;
     101             :         }
     102             :     }
     103          96 :     return true;
     104             : }
     105             : 
     106           0 : LifetimeActor *MvpnProjectManager::deleter() {
     107           0 :     return deleter_.get();
     108             : }
     109             : 
     110           0 : const LifetimeActor *MvpnProjectManager::deleter() const {
     111           0 :     return deleter_.get();
     112             : }
     113             : 
     114             : // Create MvpnProjectManagerPartitions and register with the ErmVpnTable to
     115             : // get route change notifications.
     116          96 : void MvpnProjectManager::Initialize() {
     117          96 :     if (!table_->server()->mvpn_ipv4_enable())
     118           0 :         return;
     119             : 
     120          96 :     AllocPartitions();
     121             : 
     122          95 :     listener_id_ = table_->Register(
     123             :         boost::bind(&MvpnProjectManager::RouteListener, this, _1, _2),
     124             :         "MvpnProjectManager");
     125          96 :     MVPN_LOG(MvpnProjectManagerCreate, "Initialized MvpnProjectManager");
     126             : }
     127             : 
     128          96 : void MvpnProjectManager::Terminate() {
     129          96 :     CHECK_CONCURRENCY("bgp::Config");
     130          96 :     table_->Unregister(listener_id_);
     131          96 :     listener_id_ = DBTable::kInvalidId;
     132          96 :     FreePartitions();
     133          96 :     MVPN_LOG(MvpnProjectManagerDelete, "Terminated MvpnProjectManager");
     134          96 : }
     135             : 
     136          96 : void MvpnProjectManager::AllocPartitions() {
     137         191 :     for (int part_id = 0; part_id < table_->PartitionCount(); part_id++)
     138          96 :         partitions_.push_back(new MvpnProjectManagerPartition(this, part_id));
     139          95 : }
     140             : 
     141          96 : void MvpnProjectManager::FreePartitions() {
     142         192 :     for (size_t part_id = 0; part_id < partitions_.size(); part_id++) {
     143          96 :         delete partitions_[part_id];
     144             :     }
     145          96 :     partitions_.clear();
     146          96 : }
     147             : 
     148           0 : MvpnProjectManagerPartition *MvpnProjectManager::GetPartition(int part_id) {
     149           0 :     return partitions_[part_id];
     150             : }
     151             : 
     152           0 : const MvpnProjectManagerPartition *MvpnProjectManager::GetPartition(
     153             :         int part_id) const {
     154           0 :     return partitions_[part_id];
     155             : }
     156             : 
     157          96 : void MvpnProjectManager::ManagedDelete() {
     158          96 :     deleter_->Delete();
     159          96 : }
     160             : 
     161           0 : bool MvpnProjectManager::deleted() const {
     162           0 :     return deleter_->IsDeleted();
     163             : }
     164             : 
     165           0 : MvpnStatePtr MvpnProjectManager::GetState(MvpnRoute *route) const {
     166           0 :     MvpnState::SG sg(route->GetPrefix().source(), route->GetPrefix().group());
     167           0 :     return GetPartition(route->get_table_partition()->index())->GetState(sg);
     168             : }
     169             : 
     170           0 : MvpnStatePtr MvpnProjectManager::GetState(MvpnRoute *route) {
     171           0 :     return static_cast<const MvpnProjectManager *>(this)->GetState(route);
     172             : }
     173             : 
     174           0 : MvpnStatePtr MvpnProjectManager::GetState(ErmVpnRoute *route) const {
     175           0 :     MvpnState::SG sg(route->GetPrefix().source(), route->GetPrefix().group());
     176           0 :     return GetPartition(route->get_table_partition()->index())->GetState(sg);
     177             : }
     178             : 
     179          96 : MvpnProjectManagerPartition::MvpnProjectManagerPartition(
     180          96 :         MvpnProjectManager *manager, int part_id)
     181          96 :     : manager_(manager), part_id_(part_id) {
     182          96 : }
     183             : 
     184         192 : MvpnProjectManagerPartition::~MvpnProjectManagerPartition() {
     185          96 :     assert(states_.empty());
     186         192 : }
     187             : 
     188           0 : MvpnStatePtr MvpnProjectManagerPartition::CreateState(const SG &sg) {
     189           0 :     MvpnStatePtr state(new MvpnState(sg, &states_, manager_));
     190           0 :     assert(states_.insert(make_pair(sg, state.get())).second);
     191           0 :     MVPN_TRACE(MvpnStateCreate, sg.source.to_string(), sg.group.to_string());
     192           0 :     return state;
     193           0 : }
     194             : 
     195           0 : MvpnStatePtr MvpnProjectManagerPartition::LocateState(const SG &sg) {
     196           0 :     MvpnStatePtr mvpn_state = GetState(sg);
     197           0 :     if (mvpn_state)
     198           0 :         return mvpn_state;
     199           0 :     mvpn_state = CreateState(sg);
     200           0 :     assert(mvpn_state);
     201           0 :     return mvpn_state;
     202           0 : }
     203             : 
     204           0 : MvpnStatePtr MvpnProjectManagerPartition::GetState(const SG &sg) const {
     205           0 :     MvpnState::StatesMap::const_iterator iter = states_.find(sg);
     206           0 :     return iter != states_.end() ?  iter->second : NULL;
     207             : }
     208             : 
     209           0 : MvpnStatePtr MvpnProjectManagerPartition::GetState(const SG &sg) {
     210           0 :     MvpnState::StatesMap::iterator iter = states_.find(sg);
     211           0 :     return iter != states_.end() ?  iter->second : NULL;
     212             : }
     213             : 
     214         384 : MvpnNeighbor::MvpnNeighbor() : source_as_(0) {
     215         384 : }
     216             : 
     217          96 : MvpnNeighbor::MvpnNeighbor(const RouteDistinguisher &rd,
     218          96 :                            const IpAddress &originator) :
     219          96 :         rd_(rd), originator_(originator), source_as_(0) {
     220          96 : }
     221             : 
     222           0 : const RouteDistinguisher &MvpnNeighbor::rd() const {
     223           0 :     return rd_;
     224             : }
     225             : 
     226           0 : uint32_t MvpnNeighbor::source_as() const {
     227           0 :     return source_as_;
     228             : }
     229             : 
     230           0 : const IpAddress &MvpnNeighbor::originator() const {
     231           0 :     return originator_;
     232             : }
     233             : 
     234           0 : bool MvpnNeighbor::operator==(const MvpnNeighbor &rhs) const {
     235           0 :     return rd_ == rhs.rd_ && originator_ == rhs.originator_ &&
     236           0 :            source_as_ == rhs.source_as_;
     237             : }
     238             : 
     239         384 : bool MvpnManager::FindNeighbor(const RouteDistinguisher &rd,
     240             :                                MvpnNeighbor *nbr) const {
     241         384 :     std::shared_lock<std::shared_mutex> lock(neighbors_mutex_);
     242         384 :     NeighborMap::const_iterator iter = neighbors_.find(rd);
     243         384 :     if (iter != neighbors_.end()) {
     244          96 :         *nbr = iter->second;
     245          96 :         return true;
     246             :     }
     247         288 :     return false;
     248         384 : }
     249             : 
     250           0 : const MvpnManager::NeighborMap &MvpnManager::neighbors() const {
     251             :     // Assert that lock cannot be taken now as it must have been taken already.
     252             :     // assert(!neighbors_mutex_.try_lock_read());
     253           0 :     return neighbors_;
     254             : }
     255             : 
     256           0 : size_t MvpnManager::neighbors_count() const {
     257           0 :     std::shared_lock<std::shared_mutex> lock(neighbors_mutex_);
     258           0 :     return neighbors_.size();
     259           0 : }
     260             : 
     261           0 : MvpnState::SG::SG(const Ip4Address &source, const Ip4Address &group) :
     262           0 :     source(IpAddress(source)), group(IpAddress(group)) {
     263           0 : }
     264             : 
     265           0 : MvpnState::SG::SG(const ErmVpnRoute *route) :
     266           0 :         source(route->GetPrefix().source()),
     267           0 :         group(route->GetPrefix().group()) {
     268           0 : }
     269             : 
     270           0 : MvpnState::SG::SG(const MvpnRoute *route) :
     271           0 :         source(route->GetPrefix().source()), group(route->GetPrefix().group()) {
     272           0 : }
     273             : 
     274           0 : MvpnState::SG::SG(const IpAddress &source, const IpAddress &group) :
     275           0 :     source(source), group(group) {
     276           0 : }
     277             : 
     278           0 : bool MvpnState::SG::operator<(const SG &other) const {
     279           0 :     if (source < other.source)
     280           0 :         return true;
     281           0 :     if (source > other.source)
     282           0 :         return false;
     283           0 :     if (group < other.group)
     284           0 :         return true;
     285           0 :     if (group > other.group)
     286           0 :         return false;
     287           0 :     return false;
     288             : }
     289             : 
     290           0 : const MvpnState::SG &MvpnState::sg() const {
     291           0 :     return sg_;
     292             : }
     293             : 
     294           0 : ErmVpnRoute *MvpnState::global_ermvpn_tree_rt() {
     295           0 :     return global_ermvpn_tree_rt_;
     296             : }
     297             : 
     298           0 : const ErmVpnRoute *MvpnState::global_ermvpn_tree_rt() const {
     299           0 :     return global_ermvpn_tree_rt_;
     300             : }
     301             : 
     302           0 : MvpnRoute *MvpnState::spmsi_rt() {
     303           0 :     return spmsi_rt_;
     304             : }
     305             : 
     306           0 : const MvpnRoute *MvpnState::spmsi_rt() const {
     307           0 :     return spmsi_rt_;
     308             : }
     309             : 
     310           0 : MvpnState::RoutesSet &MvpnState::spmsi_routes_received() {
     311           0 :     return spmsi_routes_received_;
     312             : }
     313             : 
     314           0 : const MvpnState::RoutesSet &MvpnState::spmsi_routes_received() const {
     315           0 :     return spmsi_routes_received_;
     316             : }
     317             : 
     318           0 : MvpnState::RoutesMap &MvpnState::leafad_routes_attr_received() {
     319           0 :     return leafad_routes_attr_received_;
     320             : }
     321             : 
     322           0 : const MvpnState::RoutesMap &MvpnState::leafad_routes_attr_received() const {
     323           0 :     return leafad_routes_attr_received_;
     324             : }
     325             : 
     326           0 : void MvpnState::set_global_ermvpn_tree_rt(ErmVpnRoute *global_ermvpn_tree_rt) {
     327           0 :     global_ermvpn_tree_rt_ = global_ermvpn_tree_rt;
     328           0 : }
     329             : 
     330           0 : void MvpnState::set_spmsi_rt(MvpnRoute *spmsi_rt) {
     331           0 :     spmsi_rt_ = spmsi_rt;
     332           0 : }
     333             : 
     334           0 : MvpnRoute *MvpnState::source_active_rt() {
     335           0 :     return source_active_rt_;
     336             : }
     337             : 
     338           0 : const MvpnRoute *MvpnState::source_active_rt() const {
     339           0 :     return source_active_rt_;
     340             : }
     341             : 
     342           0 : void MvpnState::set_source_active_rt(MvpnRoute *source_active_rt) {
     343           0 :     source_active_rt_ = source_active_rt;
     344           0 : }
     345             : 
     346           0 : MvpnDBState::MvpnDBState(MvpnStatePtr state) : state_(state) , route_(NULL) {
     347           0 : }
     348             : 
     349           0 : MvpnDBState::~MvpnDBState() {
     350           0 :     set_state(NULL);
     351           0 : }
     352             : 
     353           0 : MvpnStatePtr MvpnDBState::state() {
     354           0 :     return state_;
     355             : }
     356             : 
     357           0 : MvpnRoute *MvpnDBState::route() {
     358           0 :     return route_;
     359             : }
     360             : 
     361           0 : void MvpnDBState::set_route(MvpnRoute *route) {
     362           0 :     route_ = route;
     363           0 : }
     364             : 
     365           0 : void MvpnDBState::set_state(MvpnStatePtr state) {
     366           0 :     state_ = state;
     367           0 : }
     368             : 
     369             : class MvpnManager::DeleteActor : public LifetimeActor {
     370             : public:
     371          96 :     explicit DeleteActor(MvpnManager *manager)
     372          96 :         : LifetimeActor(manager->table_->routing_instance()->server()->
     373          96 :                 lifetime_manager()), manager_(manager) {
     374          96 :     }
     375         192 :     virtual ~DeleteActor() {
     376         192 :     }
     377             : 
     378          96 :     virtual bool MayDelete() const {
     379          96 :         CHECK_CONCURRENCY("bgp::Config");
     380          96 :         return manager_->MayDelete();
     381             :     }
     382             : 
     383          96 :     virtual void Shutdown() {
     384          96 :         if (manager_->table()->IsDeleted())
     385          96 :             return;
     386           0 :         manager_->table()->NotifyAllEntries();
     387             :     }
     388             : 
     389          96 :     virtual void Destroy() {
     390          96 :         manager_->table_->DestroyManager();
     391          96 :     }
     392             : 
     393             : private:
     394             :     MvpnManager *manager_;
     395             : };
     396             : 
     397          96 : MvpnManager::MvpnManager(MvpnTable *table, ErmVpnTable *ermvpn_table)
     398          96 :         : table_(table),
     399          96 :           ermvpn_table_(ermvpn_table),
     400          96 :           listener_id_(DBTable::kInvalidId),
     401          96 :           identifier_listener_id_(-1),
     402          96 :           table_delete_ref_(this, table->deleter()),
     403         192 :           ermvpn_table_delete_ref_(this, ermvpn_table->deleter()) {
     404          96 :     deleter_.reset(new DeleteActor(this));
     405          96 :     db_states_count_ = 0;
     406          96 : }
     407             : 
     408         192 : MvpnManager::~MvpnManager() {
     409         192 : }
     410             : 
     411          96 : MvpnTable *MvpnManager::table() {
     412          96 :     return table_;
     413             : }
     414             : 
     415           0 : const MvpnTable *MvpnManager::table() const {
     416           0 :     return table_;
     417             : }
     418             : 
     419           0 : int MvpnManager::listener_id() const {
     420           0 :     return listener_id_;
     421             : }
     422             : 
     423           0 : bool MvpnManager::deleted() const {
     424           0 :     return deleter_->IsDeleted();
     425             : }
     426             : 
     427           0 : const LifetimeActor *MvpnManager::deleter() const {
     428           0 :     return deleter_.get();
     429             : }
     430             : 
     431          96 : void MvpnManager::Terminate() {
     432          96 :     CHECK_CONCURRENCY("bgp::Config");
     433             : 
     434             :     // Delete locally originated type-1 route.
     435          96 :     MvpnRoute *type1_route = table_->FindType1ADRoute();
     436          96 :     if (type1_route) {
     437          96 :         BgpPath *path = type1_route->FindPath(BgpPath::Local, 0);
     438          96 :         if (path)
     439          96 :             type1_route->DeletePath(path);
     440          96 :         type1_route->NotifyOrDelete();
     441             :     }
     442             : 
     443          96 :     if (identifier_listener_id_ != -1) {
     444          96 :         table_->server()->UnregisterIdentifierUpdateCallback(
     445             :             identifier_listener_id_);
     446          96 :         identifier_listener_id_ = -1;
     447             :     }
     448          96 :     table_->Unregister(listener_id_);
     449          96 :     listener_id_ = DBTable::kInvalidId;
     450          96 :     FreePartitions();
     451          96 :     MVPN_LOG(MvpnManagerDelete, "Terminated MvpnManager");
     452          96 : }
     453             : 
     454         192 : void MvpnManager::ManagedDelete() {
     455         192 :     deleter_->Delete();
     456         192 : }
     457             : 
     458          96 : void MvpnManager::AllocPartitions() {
     459         192 :     for (int part_id = 0; part_id < table_->PartitionCount(); part_id++)
     460          96 :         partitions_.push_back(new MvpnManagerPartition(this, part_id));
     461          96 : }
     462             : 
     463          96 : void MvpnManager::FreePartitions() {
     464         192 :     for (size_t part_id = 0; part_id < partitions_.size(); part_id++) {
     465          96 :         delete partitions_[part_id];
     466             :     }
     467          96 :     partitions_.clear();
     468          96 : }
     469             : 
     470             : // MvpnManager can be deleted only after all associated DB States are cleared.
     471          96 : bool MvpnManager::MayDelete() const {
     472          96 :     if (!db_states_count_)
     473          96 :         return true;
     474           0 :     MVPN_LOG(MvpnManagerDelete,
     475             :              "MvpnManager::MayDelete() paused due to pending " +
     476             :              integerToString(db_states_count_) + " MvpnDBStates");
     477           0 :     return false;
     478             : }
     479             : 
     480             : // Set DB State and update count.
     481           0 : void MvpnManager::SetDBState(MvpnRoute *route, MvpnDBState *mvpn_dbstate) {
     482           0 :     route->SetState(table_, listener_id_, mvpn_dbstate);
     483           0 :     db_states_count_++;
     484           0 : }
     485             : 
     486             : // Create DB State and update count. If there is no DB State associated in the
     487             : // table, resume table deletion if the deletion was pending.
     488           0 : void MvpnManager::ClearDBState(MvpnRoute *route) {
     489           0 :     route->ClearState(table_, listener_id_);
     490           0 :     assert(db_states_count_);
     491           0 :     db_states_count_--;
     492             : 
     493             :     // Retry deletion now as there is no more attached db state in the table.
     494           0 :     if (!db_states_count_ && deleter_->IsDeleted())
     495           0 :         deleter_->RetryDelete();
     496           0 : }
     497             : 
     498           0 : MvpnTable *MvpnManagerPartition::table() {
     499           0 :     return manager_->table();
     500             : }
     501             : 
     502           0 : int MvpnManagerPartition::listener_id() const {
     503           0 :     return manager_->listener_id();
     504             : }
     505             : 
     506          96 : MvpnManagerPartition::MvpnManagerPartition(MvpnManager *manager, int part_id)
     507          96 :     : manager_(manager), part_id_(part_id) {
     508          96 : }
     509             : 
     510         192 : MvpnManagerPartition::~MvpnManagerPartition() {
     511         192 : }
     512             : 
     513             : MvpnProjectManagerPartition *
     514           0 : MvpnManagerPartition::GetProjectManagerPartition() {
     515           0 :     MvpnProjectManager *project_manager = manager_->GetProjectManager();
     516           0 :     return project_manager ? project_manager->GetPartition(part_id_) : NULL;
     517             : }
     518             : 
     519             : const MvpnProjectManagerPartition *
     520           0 : MvpnManagerPartition::GetProjectManagerPartition() const {
     521           0 :     MvpnProjectManager *project_manager = manager_->GetProjectManager();
     522           0 :     return project_manager ? project_manager->GetPartition(part_id_) : NULL;
     523             : }
     524             : 
     525           0 : MvpnProjectManager *MvpnManager::GetProjectManager() {
     526           0 :     return table_->GetProjectManager();
     527             : }
     528             : 
     529           0 : int MvpnProjectManager::listener_id() const {
     530           0 :     return listener_id_;
     531             : }
     532             : 
     533           0 : int MvpnProjectManagerPartition::listener_id() const {
     534           0 :     return manager_->listener_id();
     535             : }
     536             : 
     537           0 : MvpnStatePtr MvpnManagerPartition::LocateState(MvpnRoute *rt) {
     538             :     MvpnProjectManagerPartition *project_manager_partition =
     539           0 :         GetProjectManagerPartition();
     540           0 :     assert(project_manager_partition);
     541           0 :     MvpnState::SG sg = MvpnState::SG(rt->GetPrefix().sourceIpAddress(),
     542           0 :                                      rt->GetPrefix().groupIpAddress());
     543           0 :     return project_manager_partition->LocateState(sg);
     544             : }
     545             : 
     546           0 : MvpnStatePtr MvpnManagerPartition::GetState(MvpnRoute *rt) const {
     547             :     const MvpnProjectManagerPartition *project_manager_partition =
     548           0 :         GetProjectManagerPartition();
     549           0 :     if (!project_manager_partition)
     550           0 :         return NULL;
     551           0 :     MvpnState::SG sg = MvpnState::SG(rt->GetPrefix().sourceIpAddress(),
     552           0 :                                      rt->GetPrefix().groupIpAddress());
     553           0 :     return project_manager_partition->GetState(sg);
     554             : }
     555             : 
     556           0 : MvpnStatePtr MvpnManagerPartition::GetState(MvpnRoute *rt) {
     557           0 :     return static_cast<const MvpnManagerPartition *>(this)->GetState(rt);
     558             : }
     559             : 
     560          96 : ErmVpnTable *MvpnProjectManager::table() {
     561          96 :     return table_;
     562             : }
     563             : 
     564           0 : const ErmVpnTable *MvpnProjectManager::table() const {
     565           0 :     return table_;
     566             : }
     567             : 
     568           0 : ErmVpnTable *MvpnProjectManagerPartition::table() {
     569           0 :     return manager_->table();
     570             : }
     571             : 
     572           0 : const ErmVpnTable *MvpnProjectManagerPartition::table() const {
     573           0 :     return manager_->table();
     574             : }
     575             : 
     576           0 : void MvpnProjectManagerPartition::NotifyForestNode(
     577             :         const Ip4Address &source, const Ip4Address &group) {
     578           0 :     if (table()->tree_manager())
     579           0 :         table()->tree_manager()->NotifyForestNode(part_id_, source, group);
     580           0 : }
     581             : 
     582           0 : void MvpnManagerPartition::NotifyForestNode(
     583             :         const Ip4Address &source, const Ip4Address &group) {
     584           0 :     MvpnProjectManagerPartition *pm = GetProjectManagerPartition();
     585           0 :     if (pm)
     586           0 :         pm->NotifyForestNode(source, group);
     587           0 : }
     588             : 
     589           0 : bool MvpnProjectManagerPartition::GetForestNodePMSI(ErmVpnRoute *rt,
     590             :         uint32_t *label, Ip4Address *address, vector<string> *enc) const {
     591           0 :     if (!table()->tree_manager())
     592           0 :         return false;
     593           0 :     return table()->tree_manager()->GetForestNodePMSI(rt, label, address, enc);
     594             : }
     595             : 
     596           0 : bool MvpnManagerPartition::GetForestNodePMSI(ErmVpnRoute *rt, uint32_t *label,
     597             :         Ip4Address *address, vector<string> *encap) const {
     598           0 :     const MvpnProjectManagerPartition *pm = GetProjectManagerPartition();
     599           0 :     return pm ? pm->GetForestNodePMSI(rt, label, address, encap) : false;
     600             : }
     601             : 
     602             : ////////////////////////////////////////////////////////////////////////////////
     603             : 
     604             : // Initialize MvpnManager by allcating one MvpnManagerPartition for each DB
     605             : // partition, and register a route listener for the MvpnTable.
     606          96 : void MvpnManager::Initialize() {
     607          96 :     if (!table_->server()->mvpn_ipv4_enable())
     608           0 :         return;
     609             : 
     610          96 :     assert(!table_->IsMaster());
     611          96 :     AllocPartitions();
     612             : 
     613          96 :     listener_id_ = table_->Register(
     614             :         boost::bind(&MvpnManager::RouteListener, this, _1, _2),
     615             :         "MvpnManager");
     616             : 
     617          96 :     identifier_listener_id_ =
     618          96 :         table_->server()->RegisterIdentifierUpdateCallback(boost::bind(
     619             :             &MvpnManager::ReOriginateType1Route, this, _1));
     620          96 :     OriginateType1Route();
     621             : 
     622          96 :     MVPN_LOG(MvpnManagerCreate, "Initialized MvpnManager");
     623             : }
     624             : 
     625           0 : void MvpnManager::ReOriginateType1Route(const Ip4Address &old_identifier) {
     626             :     // Check if a path is already origianted. If so, delete it.
     627           0 :     MvpnRoute *route = table_->FindType1ADRoute(old_identifier);
     628           0 :     if (route) {
     629           0 :         BgpPath *path = route->FindPath(BgpPath::Local, 0);
     630           0 :         if (path) {
     631           0 :             route->DeletePath(path);
     632           0 :             route->NotifyOrDelete();
     633             :         }
     634             :     }
     635           0 :     OriginateType1Route();
     636           0 : }
     637             : 
     638          96 : void MvpnManager::OriginateType1Route() {
     639             :     // Originate Type1 Intra AS Auto-Discovery path.
     640          96 :     BgpServer *server = table_->server();
     641             : 
     642             :     // Check for the presence of valid identifier.
     643          96 :     if (!table_->server()->bgp_identifier())
     644           0 :         return;
     645          96 :     MvpnRoute *route = table_->LocateType1ADRoute();
     646          96 :     BgpAttrSpec attr_spec;
     647          96 :     BgpAttrNextHop nexthop(server->bgp_identifier());
     648          96 :     attr_spec.push_back(&nexthop);
     649          96 :     BgpAttrPtr attr = server->attr_db()->Locate(attr_spec);
     650          96 :     BgpPath *path = new BgpPath(NULL, 0, BgpPath::Local, attr, 0, 0, 0);
     651          96 :     route->InsertPath(path);
     652          96 :     route->Notify();
     653             : 
     654             :     // TODO(Ananth) Originate Type2 Inter AS Auto-Discovery Route.
     655          96 : }
     656             : 
     657             : // MvpnTable route listener callback function.
     658             : //
     659             : // Process changes (create/update/delete) to all different types of MvpnRoute.
     660         384 : void MvpnManager::RouteListener(DBTablePartBase *tpart, DBEntryBase *db_entry) {
     661         384 :     CHECK_CONCURRENCY("db::DBTable");
     662             : 
     663         384 :     MvpnRoute *route = dynamic_cast<MvpnRoute *>(db_entry);
     664         384 :     assert(route);
     665             : 
     666         384 :     MvpnManagerPartition *partition = partitions_[tpart->index()];
     667             : 
     668             :     // Process Type1 Intra-AS AD route.
     669         384 :     if (route->GetPrefix().type() == MvpnPrefix::IntraASPMSIADRoute) {
     670         384 :         ProcessType1ADRoute(route);
     671         384 :         return;
     672             :     }
     673             : 
     674             :     // TODO(Ananth) Inter-AS Multiast Site AD.
     675             : 
     676             :     // Process Type3 S-PMSI route.
     677           0 :     if (route->GetPrefix().type() == MvpnPrefix::SPMSIADRoute) {
     678           0 :         partition->ProcessType3SPMSIRoute(route);
     679           0 :         return;
     680             :     }
     681             : 
     682             :     // Process Type7 C-Join route.
     683           0 :     if (route->GetPrefix().type() == MvpnPrefix::SourceTreeJoinRoute) {
     684           0 :         partition->ProcessType7SourceTreeJoinRoute(route);
     685           0 :         return;
     686             :     }
     687             : 
     688             :     // Process Type5 Source Active route.
     689           0 :     if (route->GetPrefix().type() == MvpnPrefix::SourceActiveADRoute) {
     690           0 :         partition->ProcessType5SourceActiveRoute(route);
     691           0 :         return;
     692             :     }
     693             : 
     694             :     // Process Type4 LeafAD route.
     695           0 :     if (route->GetPrefix().type() == MvpnPrefix::LeafADRoute) {
     696           0 :         partition->ProcessType4LeafADRoute(route);
     697           0 :         return;
     698             :     }
     699             : }
     700             : 
     701             : // Update MVPN neighbor list with create/delete/update of auto-discovery routes.
     702             : //
     703             : // Protect access to neighbors_ map with a mutex as the same be 'read' off other
     704             : // DB tasks in parallel. (Type-1 and Type-2 do not carrry any <S,G> information)
     705         384 : void MvpnManager::ProcessType1ADRoute(MvpnRoute *route) {
     706         384 :     RouteDistinguisher rd = route->GetPrefix().route_distinguisher();
     707             : 
     708             :     // Check if an entry is already present.
     709         384 :     MvpnNeighbor old_neighbor;
     710         384 :     bool found = FindNeighbor(rd, &old_neighbor);
     711             : 
     712         384 :     if (!route->IsUsable()) {
     713          96 :         if (!found)
     714           0 :             return;
     715          96 :         std::unique_lock<std::shared_mutex> lock(neighbors_mutex_);
     716          96 :         MVPN_LOG(MvpnNeighborDelete, old_neighbor.rd().ToString(),
     717             :                  old_neighbor.originator().to_string(),
     718             :                  old_neighbor.source_as());
     719          96 :         neighbors_.erase(rd);
     720          96 :         return;
     721          96 :     }
     722             : 
     723             :     // Ignore primary paths.
     724         288 :     if (!route->BestPath()->IsReplicated())
     725         192 :         return;
     726             : 
     727          96 :     MvpnNeighbor neighbor(route->GetPrefix().route_distinguisher(),
     728         192 :                           route->GetPrefix().originator());
     729             : 
     730             :     // Ignore if there is no change.
     731          96 :     if (found && old_neighbor == neighbor)
     732           0 :         return;
     733             : 
     734          96 :     std::unique_lock<std::shared_mutex> lock(neighbors_mutex_);
     735          96 :     if (found)
     736           0 :         neighbors_.erase(rd);
     737          96 :     neighbors_.insert(make_pair(rd, neighbor));
     738          96 :     MVPN_LOG(MvpnNeighborCreate, neighbor.rd().ToString(),
     739             :              neighbor.originator().to_string(), neighbor.source_as());
     740          96 : }
     741             : 
     742             : // Check whether an ErmVpnRoute is locally originated GlobalTreeRoute.
     743           0 : bool MvpnProjectManagerPartition::IsUsableGlobalTreeRootRoute(
     744             :         ErmVpnRoute *ermvpn_route) const {
     745           0 :     if (!ermvpn_route || !ermvpn_route->IsUsable())
     746           0 :         return NULL;
     747           0 :     if (!table()->tree_manager())
     748           0 :         return false;
     749           0 :     ErmVpnRoute *global_rt = table()->tree_manager()->GetGlobalTreeRootRoute(
     750           0 :         ermvpn_route->GetPrefix().source(), ermvpn_route->GetPrefix().group());
     751           0 :     return (global_rt && global_rt == ermvpn_route);
     752             : }
     753             : 
     754             : // ErmVpnTable route listener callback function.
     755             : //
     756             : // Process changes (create/update/delete) to GlobalErmVpnRoute in vrf.ermvpn.0
     757           0 : void MvpnProjectManager::RouteListener(DBTablePartBase *tpart,
     758             :         DBEntryBase *db_entry) {
     759           0 :     CHECK_CONCURRENCY("db::DBTable");
     760           0 :     MvpnProjectManagerPartition *partition = GetPartition(tpart->index());
     761           0 :     partition->RouteListener(db_entry);
     762           0 : }
     763             : 
     764             : // Process changes to ErmVpnRoutes. We only care about changes to routes of
     765             : // type GlobalTreeRoute.
     766           0 : void MvpnProjectManagerPartition::RouteListener(DBEntryBase *db_entry) {
     767           0 :     ErmVpnRoute *ermvpn_route = dynamic_cast<ErmVpnRoute *>(db_entry);
     768           0 :     assert(ermvpn_route);
     769             : 
     770             :     // We only care about global tree routes for mvpn stitching.
     771           0 :     if (ermvpn_route->GetPrefix().type() != ErmVpnPrefix::GlobalTreeRoute)
     772           0 :         return;
     773             : 
     774           0 :     MvpnDBState *mvpn_dbstate = dynamic_cast<MvpnDBState *>(
     775           0 :         ermvpn_route->GetState(table(), listener_id()));
     776             : 
     777             :     // Handle GlobalTreeRoute route deletion.
     778           0 :     if (!IsUsableGlobalTreeRootRoute(ermvpn_route)) {
     779             :         // Ignore if there is no DB State associated with route.
     780           0 :         if (!mvpn_dbstate)
     781           0 :             return;
     782           0 :         MvpnStatePtr mvpn_state = mvpn_dbstate->state();
     783           0 :         mvpn_state->set_global_ermvpn_tree_rt(NULL);
     784             : 
     785             :         // Notify all received Type3 spmsi routes for PMSI re-computation.
     786             :         // Since usable global ermvpn is no longer available, any advertised
     787             :         // type-4 lead-ad routes must now be withdrawn.
     788           0 :         BOOST_FOREACH(MvpnRoute *route, mvpn_state->spmsi_routes_received()) {
     789           0 :             route->Notify();
     790             :         }
     791           0 :         ermvpn_route->ClearState(table(), listener_id());
     792           0 :         MVPN_ERMVPN_RT_LOG(ermvpn_route,
     793             :                            "Processed MVPN GlobalErmVpnRoute deletion");
     794           0 :         delete mvpn_dbstate;
     795           0 :         return;
     796           0 :     }
     797             : 
     798             :     // Set DB State in the route if not already done so before.
     799           0 :     MvpnStatePtr mvpn_state;
     800           0 :     if (!mvpn_dbstate) {
     801           0 :         MvpnState::SG sg(ermvpn_route);
     802           0 :         mvpn_state = LocateState(sg);
     803           0 :         mvpn_dbstate = new MvpnDBState(mvpn_state);
     804           0 :         ermvpn_route->SetState(table(), listener_id(), mvpn_dbstate);
     805             :     } else {
     806           0 :         mvpn_state = mvpn_dbstate->state();
     807             :     }
     808             : 
     809             :     // Note down current usable ermvpn route for stitching to mvpn.
     810           0 :     mvpn_dbstate->state()->set_global_ermvpn_tree_rt(ermvpn_route);
     811             : 
     812             :     // Notify all originated Type3 spmsi routes for PMSI re-computation.
     813           0 :     BOOST_FOREACH(MvpnRoute *route, mvpn_state->spmsi_routes_received()) {
     814           0 :         route->Notify();
     815             :     }
     816             : 
     817           0 :     MVPN_ERMVPN_RT_LOG(ermvpn_route,
     818             :                        "Processed MVPN GlobalErmVpnRoute creation");
     819           0 : }
     820             : 
     821             : // Process change to MVPN Type-5 SourceActive route.
     822           0 : void MvpnManagerPartition::ProcessType5SourceActiveRoute(MvpnRoute *rt) {
     823           0 :     MvpnDBState *mvpn_dbstate = dynamic_cast<MvpnDBState *>(rt->GetState(
     824           0 :                                     table(), listener_id()));
     825             : 
     826             :     // Process route change as delete if ProjectManager is not set.
     827           0 :     bool is_usable = rt->IsUsable() && table()->IsProjectManagerUsable();
     828           0 :     if (!is_usable) {
     829           0 :         if (!mvpn_dbstate)
     830           0 :             return;
     831             : 
     832             :         // Delete any associated type-3 s-pmsi route.
     833             :         MvpnRoute *spmsi_rt =
     834           0 :             mvpn_dbstate->state() ? mvpn_dbstate->state()->spmsi_rt() : NULL;
     835           0 :         if (spmsi_rt && spmsi_rt->IsUsable()) {
     836           0 :             BgpPath *path = spmsi_rt->FindPath(BgpPath::Local, 0);
     837           0 :             if (path)
     838           0 :                 spmsi_rt->DeletePath(path);
     839             :         }
     840             : 
     841           0 :         mvpn_dbstate->set_route(NULL);
     842           0 :         mvpn_dbstate->state()->set_source_active_rt(NULL);
     843           0 :         mvpn_dbstate->state()->set_spmsi_rt(NULL);
     844           0 :         if (spmsi_rt)
     845           0 :             spmsi_rt->NotifyOrDelete();
     846           0 :         manager_->ClearDBState(rt);
     847           0 :         MVPN_RT_LOG(rt, "Processed MVPN Source Active route deletion");
     848           0 :         delete mvpn_dbstate;
     849           0 :         return;
     850             :     }
     851             : 
     852           0 :     const BgpPath *path = rt->BestPath();
     853             :     // Here in the sender side, we only care about changes to the primary path.
     854           0 :     if (path->IsReplicated())
     855           0 :         return;
     856             : 
     857           0 :     MvpnStatePtr state = LocateState(rt);
     858           0 :     state->set_source_active_rt(rt);
     859             : 
     860             :     // Set DB State if not already done so.
     861           0 :     if (!mvpn_dbstate) {
     862           0 :         mvpn_dbstate = new MvpnDBState(state);
     863           0 :         manager_->SetDBState(rt, mvpn_dbstate);
     864             :     }
     865             : 
     866             :     // Check if there is any receiver interested. If not, do not originate
     867             :     // type-3 spmsi route. Also, we originate Type3 S-PMSI route only if there
     868             :     // is an imported secondary path for the join route (i.e when the join
     869             :     // route reached the sender)
     870           0 :     const MvpnRoute *join_rt = table()->FindType7SourceTreeJoinRoute(rt);
     871           0 :     if (!join_rt || !join_rt->IsUsable() ||
     872           0 :             !join_rt->BestPath()->IsReplicated()) {
     873             :         // Remove any type-3 spmsi path originated before.
     874           0 :         MvpnRoute *spmsi_rt = mvpn_dbstate->route();
     875           0 :         if (spmsi_rt) {
     876           0 :             assert(!state->spmsi_rt() || spmsi_rt == state->spmsi_rt());
     877           0 :             state->set_spmsi_rt(NULL);
     878           0 :             mvpn_dbstate->set_route(NULL);
     879           0 :             BgpPath *path = spmsi_rt->FindPath(BgpPath::Local, 0);
     880           0 :             if (path) {
     881           0 :                 MVPN_RT_LOG(spmsi_rt, "Deleted already originated SPMSI path");
     882           0 :                 spmsi_rt->DeletePath(path);
     883           0 :                 spmsi_rt->NotifyOrDelete();
     884             :             }
     885             :         }
     886           0 :         return;
     887             :     }
     888             : 
     889             :     // Originate Type-3 S-PMSI route to send towards the receivers.
     890           0 :     MvpnRoute *spmsi_rt = table()->LocateType3SPMSIRoute(join_rt);
     891           0 :     assert(spmsi_rt);
     892           0 :     state->set_spmsi_rt(spmsi_rt);
     893           0 :     if (!mvpn_dbstate->route()) {
     894           0 :         mvpn_dbstate->set_route(spmsi_rt);
     895             :     } else {
     896           0 :         assert(spmsi_rt == mvpn_dbstate->route());
     897           0 :         BgpPath *path = spmsi_rt->FindPath(BgpPath::Local, 0);
     898           0 :         assert(path);
     899             : 
     900             :         // Ignore if there is no change in the attributes.
     901           0 :         if (path->GetAttr() == rt->BestPath()->GetAttr())
     902           0 :             return;
     903           0 :         spmsi_rt->DeletePath(path);
     904             :     }
     905             : 
     906           0 :     PmsiTunnelSpec pmsi_spec;
     907           0 :     pmsi_spec.tunnel_flags = PmsiTunnelSpec::LeafInfoRequired;
     908           0 :     BgpAttrDB *attr_db = table()->server()->attr_db();
     909             :     BgpAttrPtr new_attrp = attr_db->ReplacePmsiTunnelAndLocate(
     910           0 :         rt->BestPath()->GetAttr(), &pmsi_spec);
     911             : 
     912             :     // Insert new path and notify.
     913             :     BgpPath *new_path = new BgpPath(NULL, 0, BgpPath::Local,
     914           0 :                                     new_attrp, 0, 0, 0);
     915           0 :     spmsi_rt->InsertPath(new_path);
     916           0 :     spmsi_rt->Notify();
     917           0 :     MVPN_RT_LOG(rt, "Processed MVPN Source Active route creation");
     918           0 : }
     919             : 
     920           0 : void MvpnManagerPartition::ProcessType7SourceTreeJoinRoute(MvpnRoute *join_rt) {
     921           0 :     MvpnDBState *mvpn_dbstate = dynamic_cast<MvpnDBState *>(
     922           0 :         join_rt->GetState(table(), listener_id()));
     923             : 
     924             :     // Process route change as delete if ProjectManager is not set.
     925           0 :     bool is_usable = join_rt->IsUsable() && table()->IsProjectManagerUsable();
     926           0 :     if (!is_usable) {
     927           0 :         if (!mvpn_dbstate)
     928           0 :             return;
     929             : 
     930             :         // Notify associatd source-active route so that any s-pmsi route if
     931             :         // originated before can be withdrawn as there is no more active join
     932             :         // route (receiver) for this <S,G>.
     933           0 :         if (mvpn_dbstate->state()->source_active_rt())
     934           0 :             mvpn_dbstate->state()->source_active_rt()->Notify();
     935           0 :         manager_->ClearDBState(join_rt);
     936           0 :         MVPN_RT_LOG(join_rt, "Processed Type 7 Join route deletion");
     937           0 :         delete mvpn_dbstate;
     938           0 :         return;
     939             :     }
     940             : 
     941             :     // We care only for imported secondary type-7 joins (at the sender).
     942           0 :     if (!join_rt->BestPath()->IsReplicated())
     943           0 :         return;
     944             : 
     945           0 :     MvpnStatePtr state = LocateState(join_rt);
     946           0 :     if (!mvpn_dbstate) {
     947           0 :         mvpn_dbstate = new MvpnDBState(state);
     948           0 :         manager_->SetDBState(join_rt, mvpn_dbstate);
     949             :     }
     950             : 
     951             :     // A join has been received or updated at the sender. Re-evaluate the
     952             :     // type5 source active, if one such route is present.
     953           0 :     if (state->source_active_rt()) {
     954           0 :         state->source_active_rt()->Notify();
     955           0 :         MVPN_RT_LOG(join_rt, "Processed Type 7 Join route creation and "
     956             :                     "notified Source Active route");
     957             :     } else {
     958           0 :         MVPN_RT_LOG(join_rt, "Processed Type 7 Join route creation");
     959             :     }
     960           0 : }
     961             : 
     962           0 : void MvpnManagerPartition::ProcessType4LeafADRoute(MvpnRoute *leaf_ad) {
     963           0 :     MvpnDBState *mvpn_dbstate = dynamic_cast<MvpnDBState *>(
     964           0 :         leaf_ad->GetState(table(), listener_id()));
     965             :     // Process route change as delete if ProjectManager is not set.
     966           0 :     bool is_usable = leaf_ad->IsUsable() && table()->IsProjectManagerUsable();
     967           0 :     if (!is_usable) {
     968           0 :         if (!mvpn_dbstate)
     969           0 :             return;
     970           0 :         assert(mvpn_dbstate->state()->leafad_routes_attr_received().
     971             :                 erase(leaf_ad));
     972           0 :         MvpnRoute *sa_active_rt = mvpn_dbstate->state()->source_active_rt();
     973             : 
     974             :         // Re-evaluate type5 route as secondary type4 leafad route is deleted.
     975             :         // olist needs to be updated and sent to the sender route agent.
     976           0 :         if (sa_active_rt && sa_active_rt->IsUsable()) {
     977           0 :             sa_active_rt->Notify();
     978           0 :             MVPN_RT_LOG(leaf_ad, "Processed Type 4 LeafAD route deletion"
     979             :                                  " and notified type5 source active route");
     980             :         } else {
     981           0 :             MVPN_RT_LOG(leaf_ad, "Processed Type 4 LeafAD route deletion");
     982             :         }
     983           0 :         manager_->ClearDBState(leaf_ad);
     984           0 :         delete mvpn_dbstate;
     985           0 :         return;
     986             :     }
     987             : 
     988           0 :     const BgpPath *path = leaf_ad->BestPath();
     989           0 :     if (!path->IsReplicated())
     990           0 :         return;
     991             : 
     992             :     // Secondary leaft-ad path has been imported.
     993           0 :     MvpnStatePtr state = LocateState(leaf_ad);
     994           0 :     if (!mvpn_dbstate) {
     995           0 :         mvpn_dbstate = new MvpnDBState(state);
     996           0 :         manager_->SetDBState(leaf_ad, mvpn_dbstate);
     997             :     }
     998             : 
     999             :     pair<MvpnState::RoutesMap::iterator, bool> result =
    1000           0 :         state->leafad_routes_attr_received().insert(make_pair(leaf_ad,
    1001           0 :                     leaf_ad->BestPath()->GetAttr()));
    1002             : 
    1003             :     // Overwrite the entry with new best path attributes if one already exists.
    1004           0 :     if (!result.second) {
    1005             :         // Ignore if there is no change in the best path's attributes.
    1006           0 :         if (result.first->second.get() == leaf_ad->BestPath()->GetAttr())
    1007           0 :             return;
    1008           0 :         result.first->second = leaf_ad->BestPath()->GetAttr();
    1009             :     }
    1010             : 
    1011             :     // Update the sender source-active route to update the olist.
    1012           0 :     MvpnRoute *sa_active_rt = mvpn_dbstate->state()->source_active_rt();
    1013           0 :     if (sa_active_rt && sa_active_rt->IsUsable()) {
    1014           0 :         sa_active_rt->Notify();
    1015           0 :         MVPN_RT_LOG(sa_active_rt, "Processed Type 4 Leaf AD route creation"
    1016             :                     " and Type-5 source active route was notified");
    1017             :     } else {
    1018           0 :         MVPN_RT_LOG(sa_active_rt, "Processed Type 4 Leaf AD route creation");
    1019             :     }
    1020           0 : }
    1021             : 
    1022             : // Process changes to Type3 S-PMSI routes by originating or deleting Type4 Leaf
    1023             : // AD paths as appropriate.
    1024           0 : void MvpnManagerPartition::ProcessType3SPMSIRoute(MvpnRoute *spmsi_rt) {
    1025             :     // Retrieve any state associcated with this S-PMSI route.
    1026           0 :     MvpnDBState *mvpn_dbstate = dynamic_cast<MvpnDBState *>(
    1027           0 :         spmsi_rt->GetState(table(), listener_id()));
    1028             : 
    1029           0 :     MvpnRoute *leaf_ad_route = NULL;
    1030             :     // Process route change as delete if ProjectManager is not set.
    1031           0 :     bool is_usable = spmsi_rt->IsUsable() && table()->IsProjectManagerUsable();
    1032           0 :     if (!is_usable) {
    1033           0 :         if (!mvpn_dbstate)
    1034           0 :             return;
    1035           0 :         MvpnStatePtr mvpn_state = GetState(spmsi_rt);
    1036           0 :         assert(mvpn_dbstate->state() == mvpn_state);
    1037             : 
    1038             :         // Check if a Type4 LeafAD path was already originated before for this
    1039             :         // S-PMSI path. If so, delete it as the S-PMSI path is no nonger usable.
    1040           0 :         leaf_ad_route = mvpn_dbstate->route();
    1041           0 :         if (leaf_ad_route) {
    1042           0 :             BgpPath *path = leaf_ad_route->FindPath(BgpPath::Local, 0);
    1043           0 :             if (path)
    1044           0 :                 leaf_ad_route->DeletePath(path);
    1045           0 :             mvpn_dbstate->set_route(NULL);
    1046             :         }
    1047             : 
    1048           0 :         assert(mvpn_state->spmsi_routes_received().erase(spmsi_rt));
    1049           0 :         manager_->ClearDBState(spmsi_rt);
    1050           0 :         delete mvpn_dbstate;
    1051           0 :         if (leaf_ad_route) {
    1052           0 :             leaf_ad_route->NotifyOrDelete();
    1053             : 
    1054             :             // Forest node route needs to be updated to delete the source
    1055             :             // address if advertised before.
    1056           0 :             NotifyForestNode(spmsi_rt->GetPrefix().source(),
    1057           0 :                              spmsi_rt->GetPrefix().group());
    1058           0 :             MVPN_RT_LOG(spmsi_rt, "Processed Type 3 S-PMSI route deletion"
    1059             :                         " and notified local ForestNode");
    1060             :         } else {
    1061           0 :             MVPN_RT_LOG(spmsi_rt, "Processed Type 3 S-PMSI route deletion");
    1062             :         }
    1063           0 :         return;
    1064           0 :     }
    1065             : 
    1066             :     // Ignore notifications of primary S-PMSI paths.
    1067           0 :     if (!spmsi_rt->BestPath()->IsReplicated())
    1068           0 :         return;
    1069             : 
    1070             :     // Don't send Type 4 route if there is no receiver in this vrf
    1071           0 :     const MvpnRoute *join_rt = table()->FindType7SourceTreeJoinRoute(spmsi_rt);
    1072           0 :     if (!join_rt || !join_rt->IsUsable())
    1073           0 :         return;
    1074             : 
    1075             :     // A valid S-PMSI path has been imported to a table. Originate a new
    1076             :     // LeafAD path, if GlobalErmVpnTreeRoute is available to stitch.
    1077             :     // TODO(Ananth) If LeafInfoRequired bit is not set in the S-PMSI route,
    1078             :     // then we do not need to originate a leaf ad route for this s-pmsi rt.
    1079           0 :     MvpnStatePtr mvpn_state = LocateState(spmsi_rt);
    1080           0 :     assert(mvpn_state);
    1081           0 :     if (!mvpn_dbstate) {
    1082           0 :         mvpn_dbstate = new MvpnDBState(mvpn_state);
    1083           0 :         manager_->SetDBState(spmsi_rt, mvpn_dbstate);
    1084           0 :         assert(mvpn_state->spmsi_routes_received().insert(spmsi_rt).second);
    1085             :     } else {
    1086           0 :         leaf_ad_route = mvpn_dbstate->route();
    1087             :     }
    1088             : 
    1089             :     // If LeafInfoRequired bit is not set, no need to process further
    1090           0 :     if (!spmsi_rt->BestPath()->GetAttr()->pmsi_tunnel() ||
    1091           0 :         (!(spmsi_rt->BestPath()->GetAttr()->pmsi_tunnel()->tunnel_flags() &
    1092             :                 PmsiTunnelSpec::LeafInfoRequired))) {
    1093           0 :             MVPN_RT_LOG(spmsi_rt, "No need to process Type 3 S-PMSI route as"
    1094             :                         " LeafInfoRequired bit is not set");
    1095           0 :             return;
    1096             :     }
    1097             : 
    1098           0 :     ErmVpnRoute *global_rt = mvpn_state->global_ermvpn_tree_rt();
    1099             :     uint32_t label;
    1100           0 :     Ip4Address address;
    1101           0 :     vector<string> tunnel_encaps;
    1102             :     bool pmsi_found =
    1103           0 :         GetForestNodePMSI(global_rt, &label, &address, &tunnel_encaps);
    1104             : 
    1105           0 :     if (!pmsi_found) {
    1106             :         // There is no ermvpn route available to stitch at this time. Remove any
    1107             :         // originated Type4 LeafAD route. DB State shall remain on the route as
    1108             :         // SPMSI route itself is still a usable route.
    1109           0 :         if (leaf_ad_route) {
    1110           0 :             BgpPath *path = leaf_ad_route->FindPath(BgpPath::Local, 0);
    1111           0 :             if (path)
    1112           0 :                 leaf_ad_route->DeletePath(path);
    1113           0 :             mvpn_dbstate->set_route(NULL);
    1114           0 :             leaf_ad_route->NotifyOrDelete();
    1115           0 :             NotifyForestNode(spmsi_rt->GetPrefix().source(),
    1116           0 :                              spmsi_rt->GetPrefix().group());
    1117           0 :             MVPN_RT_LOG(spmsi_rt, "Processed Type 3 S-PMSI route as deletion"
    1118             :                         " and notified local ForestNode due to missing PMSI");
    1119             :         }
    1120           0 :         return;
    1121             :     }
    1122             : 
    1123           0 :     if (!leaf_ad_route) {
    1124           0 :         leaf_ad_route = table()->LocateType4LeafADRoute(spmsi_rt);
    1125           0 :         mvpn_dbstate->set_route(leaf_ad_route);
    1126             :     }
    1127           0 :     BgpPath *old_path = leaf_ad_route->FindPath(BgpPath::Local, 0);
    1128             : 
    1129             :     // For LeafAD routes, rtarget is always <sender-router-id>:0.
    1130           0 :     BgpAttrPtr attrp = BgpAttrPtr(spmsi_rt->BestPath()->GetAttr());
    1131           0 :     ExtCommunity::ExtCommunityList rtarget;
    1132           0 :     rtarget.push_back(RouteTarget(spmsi_rt->GetPrefix().originator(), 0).
    1133           0 :                                   GetExtCommunity());
    1134           0 :     ExtCommunityPtr ext_community = table()->server()->extcomm_db()->
    1135           0 :             ReplaceRTargetAndLocate(attrp->ext_community(), rtarget);
    1136             : 
    1137           0 :     ExtCommunity::ExtCommunityList tunnel_encaps_list;
    1138           0 :     BOOST_FOREACH(string encap, tunnel_encaps) {
    1139           0 :         tunnel_encaps_list.push_back(TunnelEncap(encap).GetExtCommunity());
    1140           0 :     }
    1141             : 
    1142           0 :     ext_community = table()->server()->extcomm_db()->
    1143           0 :         ReplaceTunnelEncapsulationAndLocate(ext_community.get(),
    1144           0 :                 tunnel_encaps_list);
    1145             : 
    1146           0 :     attrp = table()->server()->attr_db()->ReplaceExtCommunityAndLocate(
    1147           0 :         attrp.get(), ext_community);
    1148             : 
    1149             :     // Retrieve PMSI tunnel attribute from the GlobalErmVpnTreeRoute.
    1150           0 :     PmsiTunnelSpec pmsi_spec;
    1151           0 :     pmsi_spec.tunnel_flags = 0;
    1152           0 :     pmsi_spec.tunnel_type = PmsiTunnelSpec::IngressReplication;
    1153           0 :     pmsi_spec.SetLabel(label, ext_community.get());
    1154           0 :     pmsi_spec.SetIdentifier(address);
    1155             : 
    1156             :     // Replicate the LeafAD path with appropriate PMSI tunnel info as part of
    1157             :     // the path attributes. Community should be route-target with root ingress
    1158             :     // PE router-id + 0 (Page 254).
    1159             :     BgpAttrPtr new_attrp =
    1160           0 :         table()->server()->attr_db()->ReplacePmsiTunnelAndLocate(attrp.get(),
    1161           0 :                                                                  &pmsi_spec);
    1162             : 
    1163             :     // Ignore if there is no change in the path attributes of already originated
    1164             :     // leaf ad path.
    1165           0 :     if (old_path && old_path->GetAttr() == new_attrp.get())
    1166           0 :         return;
    1167             : 
    1168           0 :     BgpPath *path = new BgpPath(NULL, 0, BgpPath::Local, new_attrp, 0, 0, 0);
    1169           0 :     if (old_path)
    1170           0 :         leaf_ad_route->DeletePath(old_path);
    1171           0 :     leaf_ad_route->InsertPath(path);
    1172           0 :     leaf_ad_route->NotifyOrDelete();
    1173           0 :     NotifyForestNode(spmsi_rt->GetPrefix().source(),
    1174           0 :                      spmsi_rt->GetPrefix().group());
    1175           0 :     MVPN_RT_LOG(spmsi_rt, "Processed Type 3 S-PMSI route creation");
    1176           0 : }
    1177             : 
    1178           0 : void MvpnManager::UpdateSecondaryTablesForReplication(MvpnRoute *mvpn_rt,
    1179             :         BgpTable::TableSet *secondary_tables) const {
    1180             :     // Find the right MvpnProjectManagerPartition based on the rt's partition.
    1181             :     const MvpnProjectManagerPartition *partition =
    1182           0 :         table()->GetProjectManagerPartition(mvpn_rt);
    1183           0 :     if (!partition)
    1184           0 :         return;
    1185             : 
    1186             :     // Retrieve MVPN state. Ignore if there is no state or if there is no usable
    1187             :     // Type3 SPMSI route 0associated with it (perhaps it was deleted already).
    1188           0 :     MvpnState::SG sg(mvpn_rt);
    1189           0 :     MvpnStatePtr state = partition->GetState(sg);
    1190           0 :     if (!state || !state->spmsi_rt() || !state->spmsi_rt()->IsUsable())
    1191           0 :         return;
    1192             : 
    1193             :     // Matching Type-3 S-PMSI route was found. Return its table.
    1194           0 :     BgpTable *table = dynamic_cast<BgpTable *>(
    1195           0 :         state->spmsi_rt()->get_table_partition()->parent());
    1196           0 :     assert(table);
    1197             : 
    1198             :     // Update table list to let replicator invoke RouteReplicate() for this
    1199             :     // LeafAD route for this table which has the corresponding Type3 SPMSI
    1200             :     // route. This was originated as the 'Sender' since receiver joined to
    1201             :     // the <C-S,G> group.
    1202           0 :     secondary_tables->insert(table);
    1203           0 :     MVPN_RT_LOG(mvpn_rt, "Updated tables for replication with table " +
    1204             :                 table->name());
    1205           0 : }
    1206             : 
    1207             : // Return source_address of the type-3 s-pmsi route used for rpf check in the
    1208             : // forest node.
    1209           0 : void MvpnProjectManager::GetMvpnSourceAddress(ErmVpnRoute *ermvpn_route,
    1210             :                                               Ip4Address *addrp) const {
    1211             :     // Bail if project manager is deleted.
    1212           0 :     if (deleter_->IsDeleted())
    1213           0 :         return;
    1214             : 
    1215             :     // Bail if there is no state for this <S,G>.
    1216           0 :     MvpnStatePtr state = GetState(ermvpn_route);
    1217           0 :     if (!state)
    1218           0 :         return;
    1219             : 
    1220             :     // Bail if there is no usable global_ermvpn_tree_rt.
    1221           0 :     if (!state->global_ermvpn_tree_rt() ||
    1222           0 :             !state->global_ermvpn_tree_rt()->IsUsable()) {
    1223           0 :         return;
    1224             :     }
    1225             : 
    1226             :     // Bail if there is no s-pmsi route received (no active sender)
    1227           0 :     if (state->spmsi_routes_received().empty())
    1228           0 :         return;
    1229             : 
    1230             :     // Use mvpn type3 spmsi route originator address as the source address.
    1231           0 :     *addrp = (*(state->spmsi_routes_received().begin()))->
    1232           0 :                 GetPrefix().originator();
    1233           0 :     MVPN_ERMVPN_RT_LOG(ermvpn_route, "Found Source Address for RPF Check " +
    1234             :                        addrp->to_string());
    1235           0 : }
    1236             : 
    1237           0 : UpdateInfo *MvpnProjectManager::GetType7UpdateInfo(MvpnRoute *route) {
    1238           0 :     BgpAttrPtr attr = route->BestPath()->GetAttr();
    1239           0 :     UpdateInfo *uinfo = new UpdateInfo;
    1240           0 :     uinfo->roattr = RibOutAttr(table(), route, attr.get(), 0, false, true);
    1241           0 :     return uinfo;
    1242           0 : }
    1243             : 
    1244           0 : UpdateInfo *MvpnProjectManager::GetUpdateInfo(MvpnRoute *route) {
    1245           0 :     assert((route->GetPrefix().type() == MvpnPrefix::SourceActiveADRoute) ||
    1246             :             (route->GetPrefix().type() == MvpnPrefix::SourceTreeJoinRoute));
    1247             : 
    1248           0 :     if (route->GetPrefix().type() == MvpnPrefix::SourceTreeJoinRoute)
    1249           0 :         return GetType7UpdateInfo(route);
    1250           0 :     MvpnStatePtr state = GetState(route);
    1251             : 
    1252             :     // If there is no imported leaf-ad route, then essentially there is no
    1253             :     // olist that can be formed. Route can be withdrawn if already advertised.
    1254           0 :     if (!state || state->leafad_routes_attr_received().empty())
    1255           0 :         return NULL;
    1256             : 
    1257             :     // Retrieve olist element from each of the imported type-4 leaf-ad route.
    1258           0 :     BgpOListSpec olist_spec(BgpAttribute::OList);
    1259           0 :     BOOST_FOREACH(MvpnState::RoutesMap::value_type &iter,
    1260             :                   state->leafad_routes_attr_received()) {
    1261           0 :         BgpAttrPtr attr = iter.second;
    1262           0 :         const PmsiTunnel *pmsi = attr->pmsi_tunnel();
    1263           0 :         if (!pmsi)
    1264           0 :             continue;
    1265           0 :         if (pmsi->tunnel_type() != PmsiTunnelSpec::IngressReplication)
    1266           0 :             continue;
    1267           0 :         const ExtCommunity *extcomm = attr->ext_community();
    1268           0 :         uint32_t label = attr->pmsi_tunnel()->GetLabel(extcomm);
    1269           0 :         if (!label)
    1270           0 :             continue;
    1271           0 :         BgpOListElem elem(pmsi->identifier(), label,
    1272           0 :             extcomm ? extcomm->GetTunnelEncap() : vector<string>());
    1273           0 :         olist_spec.elements.push_back(elem);
    1274           0 :         MVPN_RT_LOG(route, "Encoded olist " + pmsi->pmsi_tunnel().ToString());
    1275           0 :     }
    1276             : 
    1277           0 :     if (olist_spec.elements.empty())
    1278           0 :         return NULL;
    1279             : 
    1280           0 :     BgpAttrDB *attr_db = table()->server()->attr_db();
    1281             :     BgpAttrPtr attr = attr_db->ReplaceOListAndLocate(
    1282           0 :         route->BestPath()->GetAttr(), &olist_spec);
    1283           0 :     UpdateInfo *uinfo = new UpdateInfo;
    1284           0 :     uinfo->roattr = RibOutAttr(table(), route, attr.get(), 0, false, true);
    1285           0 :     return uinfo;
    1286           0 : }

Generated by: LCOV version 1.14