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: 0 757 0.0 %
Date: 2026-11-16 02:06:09 Functions: 0 127 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14