LCOV - code coverage report
Current view: top level - bgp - bgp_multicast.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 570 582 97.9 %
Date: 2026-06-18 01:51:13 Functions: 78 80 97.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "bgp/bgp_multicast.h"
       6             : 
       7             : #include <boost/bind/bind.hpp>
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include "base/string_util.h"
      11             : #include "base/task_annotations.h"
      12             : #include "bgp/bgp_log.h"
      13             : #include "bgp/bgp_mvpn.h"
      14             : #include "bgp/bgp_server.h"
      15             : #include "bgp/bgp_update.h"
      16             : #include "bgp/ermvpn/ermvpn_table.h"
      17             : #include "bgp/mvpn/mvpn_route.h"
      18             : #include "bgp/routing-instance/routing_instance.h"
      19             : #include "bgp/routing-instance/routing_instance_analytics_types.h"
      20             : #include "bgp/routing-instance/routing_instance_log.h"
      21             : #include "bgp/tunnel_encap/tunnel_encap.h"
      22             : 
      23             : using std::string;
      24             : using std::vector;
      25             : using namespace boost::placeholders;
      26             : 
      27             : class McastTreeManager::DeleteActor : public LifetimeActor {
      28             : public:
      29       42902 :     explicit DeleteActor(McastTreeManager *tree_manager)
      30       42902 :         : LifetimeActor(tree_manager->table_->routing_instance()->server()->
      31             :                 lifetime_manager()),
      32       42902 :           tree_manager_(tree_manager) {
      33       42907 :     }
      34       85814 :     virtual ~DeleteActor() {
      35       85814 :     }
      36             : 
      37       42907 :     virtual bool MayDelete() const {
      38       42907 :         return tree_manager_->MayDelete();
      39             :     }
      40             : 
      41       42907 :     virtual void Shutdown() {
      42       42907 :         tree_manager_->Shutdown();
      43       42907 :     }
      44             : 
      45       42907 :     virtual void Destroy() {
      46       42907 :         tree_manager_->table_->DestroyTreeManager();
      47       42907 :     }
      48             : 
      49             : private:
      50             :     McastTreeManager *tree_manager_;
      51             : };
      52             : 
      53             : //
      54             : // Constructor for McastForwarder.  The level is determined by the route type.
      55             : // We get the address of the forwarder and the label_block from the attributes
      56             : // of the active path.  The LabelBLockPtr needs to be copied so that we can
      57             : // release the label when processing a delete notification - we won't have the
      58             : // path at that point.
      59             : //
      60             : // The RD will be zero for BGP learnt routes and the RouterId will be zero for
      61             : // XMPP learnt routes.
      62             : //
      63       10745 : McastForwarder::McastForwarder(McastSGEntry *sg_entry, ErmVpnRoute *route)
      64       10745 :     : sg_entry_(sg_entry),
      65       10745 :       route_(route),
      66       10745 :       global_tree_route_(NULL),
      67       10745 :       label_(0),
      68       10745 :       address_(0),
      69       10745 :       rd_(route->GetPrefix().route_distinguisher()),
      70       21490 :       router_id_(route->GetPrefix().router_id()) {
      71       10745 :     const BgpPath *path = route->BestPath();
      72       10745 :     const BgpAttr *attr = path->GetAttr();
      73             : 
      74       10745 :     if (route_->GetPrefix().type() == ErmVpnPrefix::NativeRoute) {
      75        5582 :         level_ = McastTreeManager::LevelNative;
      76        5582 :         address_ = attr->nexthop().to_v4();
      77        5582 :         label_block_ = attr->label_block();
      78             :     } else {
      79        5163 :         level_ = McastTreeManager::LevelLocal;
      80        5163 :         const EdgeDiscovery::Edge *edge = attr->edge_discovery()->edge_list[0];
      81        5163 :         address_ = edge->address;
      82        5163 :         label_block_ = edge->label_block;
      83             :     }
      84             : 
      85       10745 :     if (path->GetAttr()->ext_community())
      86        5613 :         encap_ = path->GetAttr()->ext_community()->GetTunnelEncap();
      87       10745 : }
      88             : 
      89             : //
      90             : // Destructor for McastForwarder. Flushes forward and reverse links to and
      91             : // from other McastForwarders.
      92             : //
      93       12668 : McastForwarder::~McastForwarder() {
      94       10745 :     DeleteGlobalTreeRoute();
      95       10745 :     FlushLinks();
      96       10745 :     ReleaseLabel();
      97       12668 : }
      98             : 
      99             : //
     100             : // Update the McastForwarder based on information in the ErmVpnRoute.
     101             : // Return true if something changed.
     102             : //
     103        8822 : bool McastForwarder::Update(ErmVpnRoute *route) {
     104        8822 :     McastForwarder forwarder(sg_entry_, route);
     105             : 
     106        8822 :     bool changed = false;
     107        8822 :     if (label_block_ != forwarder.label_block_) {
     108        1106 :         ReleaseLabel();
     109        1106 :         label_block_ = forwarder.label_block_;
     110        1106 :         changed = true;
     111             :     }
     112        8822 :     if (address_ != forwarder.address_) {
     113         515 :         address_ = forwarder.address_;
     114         515 :         changed = true;
     115             :     }
     116        8822 :     if (encap_ != forwarder.encap_) {
     117          13 :         encap_ = forwarder.encap_;
     118          13 :         changed = true;
     119             :     }
     120             : 
     121        8822 :     return changed;
     122        8822 : }
     123             : 
     124             : //
     125             : // Printable string for McastForwarder.
     126             : //
     127        2792 : std::string McastForwarder::ToString() const {
     128        2792 :     if (level_ == McastTreeManager::LevelNative) {
     129        5584 :         return rd_.ToString() + " -> " + integerToString(label_);
     130             :     } else {
     131           0 :         return router_id_.to_string() + " -> " + integerToString(label_);
     132             :     }
     133             : }
     134             : 
     135             : //
     136             : // Find a link to the given McastForwarder.
     137             : //
     138        4820 : McastForwarder *McastForwarder::FindLink(McastForwarder *forwarder) {
     139        4820 :     for (McastForwarderList::iterator it = tree_links_.begin();
     140        8172 :          it != tree_links_.end(); ++it) {
     141        3352 :         if (*it == forwarder) return forwarder;
     142             :     }
     143        4820 :     return NULL;
     144             : }
     145             : 
     146             : //
     147             : // Add a link to the given McastForwarder.
     148             : //
     149        4820 : void McastForwarder::AddLink(McastForwarder *forwarder) {
     150        4820 :     assert(!FindLink(forwarder));
     151        4820 :     tree_links_.push_back(forwarder);
     152        4820 : }
     153             : 
     154             : //
     155             : // Remove a link to the given McastForwarder.
     156             : //
     157        2410 : void McastForwarder::RemoveLink(McastForwarder *forwarder) {
     158        2410 :     for (McastForwarderList::iterator it = tree_links_.begin();
     159        2562 :          it != tree_links_.end(); ++it) {
     160        2562 :         if (*it == forwarder) {
     161        2410 :             tree_links_.erase(it);
     162        2410 :             return;
     163             :         }
     164             :     }
     165             : }
     166             : 
     167             : //
     168             : // Flush all links from this McastForwarder.  Takes care of removing the
     169             : // reverse links as well.
     170             : //
     171       17004 : void McastForwarder::FlushLinks() {
     172       17004 :     for (McastForwarderList::iterator it = tree_links_.begin();
     173       19414 :          it != tree_links_.end(); ++it) {
     174        2410 :         (*it)->RemoveLink(this);
     175             :     }
     176       17004 :     tree_links_.clear();
     177       17004 : }
     178             : 
     179             : //
     180             : // Allocate a label for this McastForwarder.  The label gets allocated from
     181             : // the LabelBlock corresponding to the label range advertised by the peer.
     182             : // This is used when updating the distribution tree for the McastSGEntry to
     183             : // this McastForwarder belongs.
     184             : //
     185        4266 : void McastForwarder::AllocateLabel() {
     186        4266 :     label_ = label_block_->AllocateLabel();
     187        4266 : }
     188             : 
     189             : //
     190             : // Release the label, if any, for this McastForwarder. This is required when
     191             : // updating the distribution tree for the McastSGEntry to which we belong.
     192             : //
     193       18110 : void McastForwarder::ReleaseLabel() {
     194       18110 :     if (label_ != 0) {
     195        4255 :         label_block_->ReleaseLabel(label_);
     196        4255 :         label_ = 0;
     197             :     }
     198       18110 : }
     199             : 
     200             : //
     201             : // Add the GlobalTreeRoute for this McastForwarder. The GlobalTreeRoute is
     202             : // used by the tree builder to tell the associated control-node about the
     203             : // forwarding edges for Native McastForwarders attached it.
     204             : //
     205        3466 : void McastForwarder::AddGlobalTreeRoute() {
     206        3466 :     assert(level_ == McastTreeManager::LevelLocal);
     207        3466 :     assert(!global_tree_route_);
     208             : 
     209             :     // Bail if there's no label allocated.
     210        3466 :     if (label_ == 0)
     211        2095 :         return;
     212             : 
     213             :     // Bail if we can't build a source RD.
     214        1473 :     if (sg_entry_->GetSourceRd().IsZero())
     215         102 :         return;
     216             : 
     217             :     // Construct the prefix and route key.
     218        1371 :     BgpTable *table = static_cast<BgpTable *>(route_->get_table());
     219             :     ErmVpnPrefix prefix(ErmVpnPrefix::GlobalTreeRoute,
     220        1371 :         RouteDistinguisher::kZeroRd, router_id_,
     221        1371 :         sg_entry_->group(), sg_entry_->source());
     222        1371 :     ErmVpnRoute rt_key(prefix);
     223             : 
     224             :     // Find or create the route.
     225        1371 :     McastManagerPartition *partition = sg_entry_->partition();
     226             :     DBTablePartition *tbl_partition =
     227        1371 :         static_cast<DBTablePartition *>(partition->GetTablePartition());
     228             :     ErmVpnRoute *route =
     229        1371 :         static_cast<ErmVpnRoute *>(tbl_partition->Find(&rt_key));
     230        1371 :     if (!route) {
     231         416 :         route = new ErmVpnRoute(prefix);
     232         416 :         tbl_partition->Add(route);
     233             :     } else {
     234         955 :         route->ClearDelete();
     235             :     }
     236             : 
     237             :     // Build the attributes.  Need to go through the tree links to build the
     238             :     // EdgeForwardingSpec.
     239        1371 :     BgpServer *server = table->routing_instance()->server();
     240        1371 :     BgpAttrSpec attr_spec;
     241        1371 :     BgpAttrNextHop nexthop(server->bgp_identifier());
     242        1371 :     attr_spec.push_back(&nexthop);
     243        1371 :     BgpAttrSourceRd source_rd(sg_entry_->GetSourceRd());
     244        1371 :     attr_spec.push_back(&source_rd);
     245        1371 :     EdgeForwardingSpec efspec;
     246        1371 :     for (McastForwarderList::const_iterator it = tree_links_.begin();
     247        2359 :          it != tree_links_.end(); ++it) {
     248         988 :         EdgeForwardingSpec::Edge *edge = new EdgeForwardingSpec::Edge;
     249         988 :         edge->SetInboundIp4Address(address_);
     250         988 :         edge->inbound_label = label_;
     251         988 :         edge->SetOutboundIp4Address((*it)->address());
     252         988 :         edge->outbound_label = (*it)->label();
     253         988 :         efspec.edge_list.push_back(edge);
     254             :     }
     255        1371 :     attr_spec.push_back(&efspec);
     256             :     // Add tunnel encaps for remote nodes
     257        1371 :     ExtCommunitySpec ext;
     258        1371 :     ext.AddTunnelEncaps(encap_);
     259        1371 :     if (!ext.communities.empty())
     260         133 :         attr_spec.push_back(&ext);
     261        1371 :     BgpAttrPtr attr = server->attr_db()->Locate(attr_spec);
     262             : 
     263             :     // Add a path with source BgpPath::Local.
     264        1371 :     BgpPath *path = new BgpPath(0, BgpPath::Local, attr);
     265        1371 :     route->InsertPath(path);
     266        1371 :     tbl_partition->Notify(route);
     267        1371 :     global_tree_route_ = route;
     268        1371 : }
     269             : 
     270             : //
     271             : // Delete the GlobalTreeRoute for this McastForwarder.
     272             : //
     273       14211 : void McastForwarder::DeleteGlobalTreeRoute() {
     274       14211 :     if (!global_tree_route_)
     275       12840 :         return;
     276             : 
     277        1371 :     McastManagerPartition *partition = sg_entry_->partition();
     278             :     DBTablePartition *tbl_partition =
     279        1371 :         static_cast<DBTablePartition *>(partition->GetTablePartition());
     280        1371 :     global_tree_route_->RemovePath(BgpPath::Local);
     281             : 
     282        1371 :     if (!global_tree_route_->HasPaths()) {
     283        1327 :         tbl_partition->Delete(global_tree_route_);
     284             :     } else {
     285          44 :         tbl_partition->Notify(global_tree_route_);
     286             :     }
     287        1371 :     global_tree_route_ = NULL;
     288             : }
     289             : 
     290             : //
     291             : // Append list of BgpOListElems from the Local tree to the BgpOListSpec. The
     292             : // list is built based on the tree links in this McastForwarder.
     293             : //
     294        4215 : void McastForwarder::AddLocalOListElems(BgpOListSpec *olist_spec) {
     295        4215 :     assert(level_ == McastTreeManager::LevelNative);
     296             : 
     297        4215 :     for (McastForwarderList::const_iterator it = tree_links_.begin();
     298        7837 :          it != tree_links_.end(); ++it) {
     299        3622 :         BgpOListElem elem((*it)->address(), (*it)->label(), (*it)->encap());
     300        3622 :         olist_spec->elements.push_back(elem);
     301        3622 :     }
     302        4215 : }
     303             : 
     304             : //
     305             : // Append list of BgpOListElems from the Global tree to the BgpOListSpec. The
     306             : // list is built based on EdgeForwarding attribute in the GlobalTreeRoute.
     307             : //
     308        4215 : void McastForwarder::AddGlobalOListElems(BgpOListSpec *olist_spec) {
     309        4215 :     assert(level_ == McastTreeManager::LevelNative);
     310             : 
     311             :     // Bail if this is not the forest node for the Local tree.
     312        4215 :     if (!sg_entry_->IsForestNode(this))
     313        2472 :         return;
     314             : 
     315        2241 :     const ErmVpnRoute *route = sg_entry_->tree_result_route();
     316        2241 :     if (!route)
     317         488 :         return;
     318             : 
     319        1753 :     const BgpPath *path = route->BestPath();
     320        1753 :     if (!path)
     321          10 :         return;
     322        1743 :     const BgpAttr *attr = path->GetAttr();
     323        1743 :     vector<string> encaps;
     324        1743 :     if (attr && attr->ext_community())
     325         585 :         encaps = attr->ext_community()->GetTunnelEncap();
     326             : 
     327             :     // Go through each forwarding edge and add it to the list.
     328        1743 :     const EdgeForwarding *eforwarding = path->GetAttr()->edge_forwarding();
     329        1743 :     for (EdgeForwarding::EdgeList::const_iterator it =
     330        4675 :          eforwarding->edge_list.begin(); it != eforwarding->edge_list.end();
     331        1189 :          ++it) {
     332        1189 :         const EdgeForwarding::Edge *edge = *it;
     333        1189 :         if (edge->inbound_address == address_) {
     334        1008 :             BgpOListElem elem(edge->outbound_address, edge->outbound_label,
     335        1008 :                               encaps);
     336        1008 :             olist_spec->elements.push_back(elem);
     337        1008 :         }
     338             :     }
     339        1743 : }
     340             : 
     341             : //
     342             : // Construct an UpdateInfo with the RibOutAttr that needs to be advertised to
     343             : // the IPeer for the ErmVpnRoute associated with this McastForwarder. This is
     344             : // used as Export method of the ErmVpnTable.  It is expected that the caller
     345             : // fills in the target RibPeerSet in the UpdateInfo.
     346             : //
     347             : // The main functionality here is to transform the McastForwarderList for the
     348             : // distribution tree and the EdgeForwarding attribute from the GlobalTreeRoute
     349             : // into a BgpOList.
     350             : //
     351        4215 : UpdateInfo *McastForwarder::GetUpdateInfo(ErmVpnTable *table) {
     352        4215 :     CHECK_CONCURRENCY("db::DBTable");
     353             : 
     354        4215 :     assert(level_ == McastTreeManager::LevelNative);
     355             : 
     356        4215 :     BgpOListSpec olist_spec(BgpAttribute::OList);
     357        4215 :     AddLocalOListElems(&olist_spec);
     358        4215 :     AddGlobalOListElems(&olist_spec);
     359             : 
     360             :     // Bail if there is no label allocated.
     361        4215 :     if (label_ == 0)
     362         648 :         return NULL;
     363             : 
     364        3567 :     BgpAttrSpec attr_spec;
     365        3567 :     attr_spec.push_back(&olist_spec);
     366        3567 :     BgpAttrPtr attr = table->server()->attr_db()->Locate(attr_spec);
     367             : 
     368        3567 :     UpdateInfo *uinfo = new UpdateInfo;
     369        3567 :     uinfo->roattr = RibOutAttr(table, route_, attr.get(), label_, true, true);
     370        5783 :     if (route_ && sg_entry_->IsForestNode(this) &&
     371        2216 :             sg_entry_->IsTreeBuilder(McastTreeManager::LevelLocal)) {
     372        1300 :         table->GetMvpnSourceAddress(route_, uinfo->roattr.source_address());
     373             :     }
     374        3567 :     return uinfo;
     375        4215 : }
     376             : 
     377             : //
     378             : // Constructor for McastSGEntry.
     379             : //
     380       25103 : McastSGEntry::McastSGEntry(McastManagerPartition *partition,
     381       25103 :         Ip4Address group, Ip4Address source)
     382       25103 :     : partition_(partition),
     383       25103 :       group_(group),
     384       25103 :       source_(source),
     385       25103 :       forest_node_(NULL),
     386       25103 :       local_tree_route_(NULL),
     387       25103 :       tree_result_route_(NULL),
     388       50206 :       on_work_queue_(false) {
     389       75309 :     for (int level = McastTreeManager::LevelFirst;
     390       75309 :          level < McastTreeManager::LevelCount; ++level) {
     391       50206 :         ForwarderSet *forwarders = new ForwarderSet;
     392       50206 :         forwarder_sets_.push_back(forwarders);
     393       50206 :         update_needed_.push_back(false);
     394             :     }
     395       25103 : }
     396             : 
     397             : //
     398             : // Destructor for McastSGEntry.
     399             : //
     400       25506 : McastSGEntry::~McastSGEntry() {
     401       25103 :     STLDeleteValues(&forwarder_sets_);
     402       25506 : }
     403             : 
     404             : //
     405             : // Printable string for McastSGEntry.
     406             : //
     407          56 : std::string McastSGEntry::ToString() const {
     408         112 :     return group_.to_string() + "," + source_.to_string();
     409             : }
     410             : 
     411             : //
     412             : // Add the given McastForwarder under this McastSGEntry and trigger update
     413             : // of the distribution tree.
     414             : //
     415        1923 : void McastSGEntry::AddForwarder(McastForwarder *forwarder) {
     416        1923 :     uint8_t level = forwarder->level();
     417        1923 :     forwarder_sets_[level]->insert(forwarder);
     418        1923 :     update_needed_[level] = true;
     419        1923 :     partition_->EnqueueSGEntry(this);
     420        1923 : }
     421             : 
     422             : //
     423             : // Handle change for the given McastForwarder under this McastSGEntry. Trigger
     424             : // update of the distribution tree.
     425             : //
     426             : // Note that this method only handles the change = the caller determines that
     427             : // there has been a change.
     428             : //
     429        1161 : void McastSGEntry::ChangeForwarder(McastForwarder *forwarder) {
     430        1161 :     uint8_t level = forwarder->level();
     431        1161 :     update_needed_[level] = true;
     432        1161 :     partition_->EnqueueSGEntry(this);
     433        1161 : }
     434             : 
     435             : //
     436             : // Delete the given McastForwarder from this McastSGEntry and trigger update
     437             : // of the distribution tree.
     438             : //
     439        1923 : void McastSGEntry::DeleteForwarder(McastForwarder *forwarder) {
     440        1923 :     if (forwarder == forest_node_)
     441         408 :         forest_node_ = NULL;
     442        1923 :     uint8_t level = forwarder->level();
     443        1923 :     forwarder_sets_[level]->erase(forwarder);
     444        1923 :     update_needed_[level] = true;
     445        1923 :     partition_->EnqueueSGEntry(this);
     446        1923 : }
     447             : 
     448             : //
     449             : // Get the SourceRD to be used when adding [Local|Global]TreeRoutes.  This
     450             : // SourceRD gets used as the RD when the ErmVpnRoute is replicated from the
     451             : // VRF table to the VPN table.
     452             : //
     453             : // We simply use the RD for the forest node.
     454             : //
     455        3743 : const RouteDistinguisher &McastSGEntry::GetSourceRd() const {
     456        3743 :     if (!forest_node_)
     457         102 :         return RouteDistinguisher::kZeroRd;
     458        3641 :     return forest_node_->route()->GetPrefix().route_distinguisher();
     459             : }
     460             : 
     461             : //
     462             : // Add the LocalTreeRoute for this McastSGEntry.  This route advertises a set
     463             : // of candidate edges from McastForwarders attached to this control-node that
     464             : // can be used by the tree builder to build the higher level tree.  We simply
     465             : // advertise edges McastTreeManager::kDegree - 1 edges from the forest node.
     466             : //
     467             : // We advertise kDegree-1 candidate edges via the EdgeDiscovery attribute. All
     468             : // the edges are for the forest node for the tree of native McastForwarders.
     469             : // The label block for each edge in the EdgeDiscovery attribute is of size 1 -
     470             : // this is label that has been allocated for the forest node.  Using a single
     471             : // label is acceptable because the tree builder algorithm does not change the
     472             : // relative order of nodes in the tree.
     473             : //
     474        1247 : void McastSGEntry::AddLocalTreeRoute() {
     475        1247 :     assert(!forest_node_);
     476        1247 :     assert(!local_tree_route_);
     477             : 
     478             :     // Select last usable leaf in the distribution tree as the forest node.
     479             :     // A leaf is considered usable if it has a valid label i.e. it has not
     480             :     // run out of labels.
     481        1247 :     uint8_t level = McastTreeManager::LevelNative;
     482        1247 :     ForwarderSet *forwarders = forwarder_sets_[level];
     483        1247 :     for (ForwarderSet::reverse_iterator rit = forwarders->rbegin();
     484        1256 :          rit != forwarders->rend(); ++rit) {
     485         908 :         McastForwarder *forwarder = *rit;
     486         908 :         if (forwarder->label()) {
     487         899 :             forest_node_ = forwarder;
     488         899 :             break;
     489             :         }
     490             :     }
     491             : 
     492             :     // Bail if we couldn't designate a forest node.
     493        1247 :     if (!forest_node_)
     494         348 :         return;
     495             : 
     496             :     // Construct the prefix and route key.
     497         899 :     BgpServer *server = partition_->server();
     498         899 :     Ip4Address router_id(server->bgp_identifier());
     499             :     ErmVpnPrefix prefix(ErmVpnPrefix::LocalTreeRoute,
     500         899 :         RouteDistinguisher::kZeroRd, router_id, group_, source_);
     501         899 :     ErmVpnRoute rt_key(prefix);
     502             : 
     503             :     // Find or create the route.
     504             :     DBTablePartition *tbl_partition =
     505         899 :         static_cast<DBTablePartition *>(partition_->GetTablePartition());
     506             :     ErmVpnRoute *route =
     507         899 :         static_cast<ErmVpnRoute *>(tbl_partition->Find(&rt_key));
     508         899 :     if (!route) {
     509         347 :         route = new ErmVpnRoute(prefix);
     510         347 :         tbl_partition->Add(route);
     511             :     } else {
     512         552 :         route->ClearDelete();
     513             :     }
     514             : 
     515             :     // Build the attributes.
     516         899 :     BgpAttrSpec attr_spec;
     517         899 :     BgpAttrNextHop nexthop(server->bgp_identifier());
     518         899 :     attr_spec.push_back(&nexthop);
     519         899 :     BgpAttrSourceRd source_rd(GetSourceRd());
     520         899 :     attr_spec.push_back(&source_rd);
     521         899 :     EdgeDiscoverySpec edspec;
     522        3596 :     for (int idx = 1; idx <= McastTreeManager::kDegree - 1; ++idx) {
     523        2697 :         EdgeDiscoverySpec::Edge *edge = new EdgeDiscoverySpec::Edge;
     524        2697 :         edge->SetIp4Address(forest_node_->address());
     525        2697 :         edge->SetLabels(forest_node_->label(), forest_node_->label());
     526        2697 :         edspec.edge_list.push_back(edge);
     527             :     }
     528         899 :     attr_spec.push_back(&edspec);
     529             :     // Add tunnel encaps for remote nodes
     530         899 :     ExtCommunitySpec ext;
     531         899 :     ext.AddTunnelEncaps(forest_node_->encap());
     532         899 :     if (!ext.communities.empty())
     533         135 :         attr_spec.push_back(&ext);
     534         899 :     BgpAttrPtr attr = server->attr_db()->Locate(attr_spec);
     535             : 
     536             :     // Add a path with source BgpPath::Local.
     537         899 :     BgpPath *path = new BgpPath(0, BgpPath::Local, attr);
     538         899 :     route->InsertPath(path);
     539         899 :     tbl_partition->Notify(route);
     540         899 :     local_tree_route_ = route;
     541         899 : }
     542             : 
     543             : //
     544             : // Delete the LocalTreeRoute for this McastSGEntry.
     545             : //
     546        1247 : void McastSGEntry::DeleteLocalTreeRoute() {
     547        1247 :     if (!local_tree_route_)
     548         348 :         return;
     549             : 
     550         899 :     forest_node_ = NULL;
     551             :     DBTablePartition *tbl_partition =
     552         899 :         static_cast<DBTablePartition *>(partition_->GetTablePartition());
     553         899 :     local_tree_route_->RemovePath(BgpPath::Local);
     554         899 :     if (!local_tree_route_->HasPaths()) {
     555         899 :         tbl_partition->Delete(local_tree_route_);
     556             :     } else {
     557           0 :         tbl_partition->Notify(local_tree_route_);
     558             :     }
     559         899 :     local_tree_route_ = NULL;
     560             : }
     561             : 
     562             : //
     563             : // Update the LocalTreeRoute for this McastSGEntry if RouterId has changed.
     564             : //
     565         723 : void McastSGEntry::UpdateLocalTreeRoute() {
     566         723 :     if (!local_tree_route_)
     567         723 :         return;
     568             : 
     569             :     // Bail if the RouterId hasn't changed.
     570         611 :     const BgpServer *server = partition_->server();
     571         611 :     Ip4Address router_id = local_tree_route_->GetPrefix().router_id();
     572         611 :     if (router_id.to_ulong() == server->bgp_identifier())
     573         611 :         return;
     574             : 
     575             :     // Add and delete the route.
     576           0 :     DeleteLocalTreeRoute();
     577           0 :     AddLocalTreeRoute();
     578             : }
     579             : 
     580             : //
     581             : // Update relevant [Local|Global]TreeRoutes for the McastSGEntry.
     582             : //
     583        3526 : void McastSGEntry::UpdateRoutes(uint8_t level) {
     584        3526 :     if (level == McastTreeManager::LevelNative) {
     585        1247 :         DeleteLocalTreeRoute();
     586        1247 :         AddLocalTreeRoute();
     587             :     } else {
     588        2279 :         ForwarderSet *forwarders = forwarder_sets_[level];
     589        2279 :         for (ForwarderSet::iterator it = forwarders->begin();
     590        5745 :              it != forwarders->end(); ++it) {
     591        3466 :             (*it)->DeleteGlobalTreeRoute();
     592        3466 :             (*it)->AddGlobalTreeRoute();
     593             :         }
     594             :     }
     595        3526 : }
     596             : 
     597        3353 : ErmVpnRoute *McastSGEntry::GetGlobalTreeRootRoute() const {
     598        3353 :     if (!IsTreeBuilder(McastTreeManager::LevelLocal))
     599        1631 :         return NULL;
     600        1722 :     ForwarderSet *forwarders = forwarder_sets_[McastTreeManager::LevelLocal];
     601        1722 :     assert(!forwarders->empty());
     602        1722 :     ForwarderSet::const_iterator it = forwarders->begin();
     603        1722 :     return (*it)->global_tree_route();
     604             : }
     605             : 
     606             : //
     607             : // Implement tree builder election.
     608             : //
     609        9099 : bool McastSGEntry::IsTreeBuilder(uint8_t level) const {
     610        9099 :     if (level == McastTreeManager::LevelNative)
     611        1249 :         return true;
     612             : 
     613        7850 :     const ForwarderSet *forwarders = forwarder_sets_[level];
     614        7850 :     ForwarderSet::const_iterator it = forwarders->begin();
     615        7850 :     if (it == forwarders->end())
     616         679 :         return false;
     617             : 
     618        7171 :     Ip4Address router_id(partition_->server()->bgp_identifier());
     619        7171 :     if ((*it)->router_id() != router_id)
     620        3201 :         return false;
     621             : 
     622        3970 :     return true;
     623             : }
     624             : 
     625             : //
     626             : //
     627             : // Update specified distribution tree for the McastSGEntry.  We traverse all
     628             : // McastForwarders in sorted order and arrange them in breadth first fashion
     629             : // in a k-ary tree.  Building the tree in this manner guarantees that we get
     630             : // the same tree for a given set of forwarders, independent of the order in
     631             : // in which they joined. This predictability is deemed to be more important
     632             : // than other criteria such as minimizing disruption of traffic, minimizing
     633             : // the cost/weight of the tree etc.
     634             : //
     635        6790 : void McastSGEntry::UpdateTree(uint8_t level) {
     636        6790 :     CHECK_CONCURRENCY("db::DBTable");
     637             : 
     638        6790 :     if (!update_needed_[level])
     639        4597 :         return;
     640        3526 :     update_needed_[level] = false;
     641             : 
     642             :     int degree;
     643        3526 :     if (level == McastTreeManager::LevelNative) {
     644        1247 :         degree = McastTreeManager::kDegree;
     645             :     } else {
     646        2279 :         degree = McastTreeManager::kDegree - 1;
     647             :     }
     648             : 
     649             :     // First get rid of the previous distribution tree and enqueue all the
     650             :     // associated ErmVpnRoutes for notification. Note that DBListeners will
     651             :     // not get invoked until after this routine is done.
     652        3526 :     ForwarderSet *forwarders = forwarder_sets_[level];
     653        3526 :     for (ForwarderSet::iterator it = forwarders->begin();
     654        9785 :          it != forwarders->end(); ++it) {
     655        6259 :        (*it)->FlushLinks();
     656        6259 :        (*it)->ReleaseLabel();
     657        6259 :        partition_->GetTablePartition()->Notify((*it)->route());
     658             :     }
     659             : 
     660             :     // Bail if we're not the tree builder.
     661        3526 :     if (!IsTreeBuilder(level)) {
     662        1333 :         UpdateRoutes(level);
     663        1333 :         return;
     664             :     }
     665             : 
     666             :     // Create a vector of pointers to the McastForwarders in sorted order.
     667             :     // We do this because std::set doesn't support random access iterators.
     668             :     // Skip if we can't allocate a label for the McastForwarder.
     669        2193 :     McastForwarderList vec;
     670        2193 :     vec.reserve(forwarders->size());
     671        2193 :     for (ForwarderSet::iterator it = forwarders->begin();
     672        6459 :          it != forwarders->end(); ++it) {
     673        4266 :         McastForwarder *forwarder = *it;
     674        4266 :         forwarder->AllocateLabel();
     675        4266 :         if (!forwarder->label())
     676          11 :             continue;
     677        4255 :         vec.push_back(forwarder);
     678             :     }
     679             : 
     680             :     // Go through each McastForwarder in the vector and link it to it's parent
     681             :     // McastForwarder in the k-ary tree. We also add a link from the parent to
     682             :     // the entry in question.
     683        6448 :     for (McastForwarderList::iterator it = vec.begin(); it != vec.end(); ++it) {
     684        4255 :         int idx = it - vec.begin();
     685        4255 :         if (idx == 0)
     686        1845 :             continue;
     687             : 
     688        2410 :         int parent_idx = (idx - 1) / degree;
     689        2410 :         McastForwarderList::iterator parent_it = vec.begin() + parent_idx;
     690        2410 :         assert(parent_it != vec.end());
     691        2410 :         McastForwarder *forwarder = *it;
     692        2410 :         McastForwarder *parent_forwarder = *parent_it;
     693        2410 :         forwarder->AddLink(parent_forwarder);
     694        2410 :         parent_forwarder->AddLink(forwarder);
     695             :     }
     696             : 
     697             :     // Update [Local|Global]TreeRoutes.
     698        2193 :     UpdateRoutes(level);
     699        2193 : }
     700             : 
     701             : //
     702             : // Update distribution trees for both levels.
     703             : //
     704        3395 : void McastSGEntry::UpdateTree() {
     705       10185 :     for (uint8_t level = McastTreeManager::LevelFirst;
     706       10185 :          level < McastTreeManager::LevelCount; ++level) {
     707        6790 :         UpdateTree(level);
     708             :     }
     709        3395 : }
     710             : 
     711             : //
     712             : // Trigger notification of the ErmVpnRoute associated with the McastForwarder
     713             : // that is the forest node. This is used to trigger a rebuild of the BgpOlist
     714             : // when the GlobalTreeRoute is updated.
     715             : //
     716        2086 : void McastSGEntry::NotifyForestNode() {
     717        2086 :     if (!forest_node_)
     718         433 :         return;
     719        1653 :     partition_->GetTablePartition()->Notify(forest_node_->route());
     720             : }
     721             : 
     722         174 : bool McastSGEntry::GetForestNodePMSI(uint32_t *label, Ip4Address *address,
     723             :         vector<string> *tunnel_encap) const {
     724         174 :     if (!forest_node_)
     725           0 :         return false;
     726         174 :     *label = forest_node_->label();
     727         174 :     *address = forest_node_->address();
     728         174 :     *tunnel_encap = forest_node_->encap();
     729         174 :     return true;
     730             : }
     731             : 
     732        7782 : bool McastSGEntry::IsForestNode(McastForwarder *forwarder) {
     733        7782 :     return (forwarder == forest_node_);
     734             : }
     735             : 
     736        3395 : bool McastSGEntry::empty() const {
     737        3395 :     if (local_tree_route_ || tree_result_route_)
     738        2713 :         return false;
     739         682 :     if (!forwarder_sets_[McastTreeManager::LevelNative]->empty())
     740           4 :         return false;
     741         678 :     if (!forwarder_sets_[McastTreeManager::LevelLocal]->empty())
     742         275 :         return false;
     743         403 :     return true;
     744             : }
     745             : 
     746             : //
     747             : // Constructor for McastManagerPartition.
     748             : //
     749       42873 : McastManagerPartition::McastManagerPartition(McastTreeManager *tree_manager,
     750       42873 :         size_t part_id)
     751       42873 :     : tree_manager_(tree_manager),
     752       42873 :       part_id_(part_id),
     753       42873 :       update_count_(0),
     754       85746 :       work_queue_(TaskScheduler::GetInstance()->GetTaskId("db::DBTable"),
     755       42873 :               part_id_,
     756       42873 :               boost::bind(&McastManagerPartition::ProcessSGEntry, this, _1)) {
     757       42874 : }
     758             : 
     759             : //
     760             : // Destructor for McastManagerPartition.
     761             : //
     762       42874 : McastManagerPartition::~McastManagerPartition() {
     763       42874 :     work_queue_.Shutdown();
     764       42874 : }
     765             : 
     766             : // Find the McastSGEntry for the given group and source.
     767       21173 : McastSGEntry *McastManagerPartition::FindSGEntry(
     768             :         const Ip4Address &group, const Ip4Address &source) {
     769             :     return const_cast<McastSGEntry *>(
     770       21173 :         static_cast<const McastManagerPartition *>(this)->FindSGEntry(group,
     771       21173 :                                                                       source));
     772             : }
     773             : 
     774             : //
     775             : // Find the McastSGEntry for the given group and source.
     776             : //
     777       24700 : const McastSGEntry *McastManagerPartition::FindSGEntry(
     778             :         const Ip4Address &group, const Ip4Address &source) const {
     779             :     McastSGEntry temp_sg_entry(const_cast<McastManagerPartition *>(this),
     780       24700 :                                group, source);
     781       24700 :     SGList::const_iterator it = sg_list_.find(&temp_sg_entry);
     782       49400 :     return (it != sg_list_.end() ? *it : NULL);
     783       24700 : }
     784             : 
     785             : //
     786             : // Find or create the McastSGEntry for the given group and source.
     787             : //
     788        2365 : McastSGEntry *McastManagerPartition::LocateSGEntry(
     789             :         Ip4Address group, Ip4Address source) {
     790        2365 :     McastSGEntry *sg_entry = FindSGEntry(group, source);
     791        2365 :     if (!sg_entry) {
     792         403 :         sg_entry = new McastSGEntry(this, group, source);
     793         403 :         sg_list_.insert(sg_entry);
     794             :     }
     795        2365 :     return sg_entry;
     796             : }
     797             : 
     798        3353 : ErmVpnRoute *McastManagerPartition::GetGlobalTreeRootRoute(
     799             :         const Ip4Address &source, const Ip4Address &group) const {
     800        3353 :     const McastSGEntry *sg = FindSGEntry(group, source);
     801        3353 :     return sg ? sg->GetGlobalTreeRootRoute() : NULL;
     802             : }
     803             : 
     804        7980 : void McastManagerPartition::NotifyForestNode(
     805             :         const Ip4Address &source, const Ip4Address &group) {
     806        7980 :     McastSGEntry *sg = FindSGEntry(group, source);
     807        7980 :     if (sg)
     808         312 :         sg->NotifyForestNode();
     809        7980 : }
     810             : 
     811         174 : bool McastManagerPartition::GetForestNodePMSI(ErmVpnRoute *rt, uint32_t *label,
     812             :         Ip4Address *address, vector<string> *encap) const {
     813         174 :     const McastSGEntry *sg = FindSGEntry(rt->GetPrefix().group(),
     814         174 :                                          rt->GetPrefix().source());
     815         174 :     return sg ? sg->GetForestNodePMSI(label, address, encap) : false;
     816             : }
     817             : 
     818             : //
     819             : // Enqueue the given McastSGEntry on the WorkQueue if it's not already on it.
     820             : //
     821        5449 : void McastManagerPartition::EnqueueSGEntry(McastSGEntry *sg_entry) {
     822        5449 :     if (sg_entry->on_work_queue())
     823        2054 :         return;
     824        3395 :     work_queue_.Enqueue(sg_entry);
     825        3395 :     sg_entry->set_on_work_queue();
     826             : }
     827             : 
     828             : //
     829             : // Callback for the WorkQueue. Updates distribution trees for the McastSGEntry.
     830             : // Also gets rid of the McastSGEntry if it is eligible to be deleted.
     831             : //
     832        3395 : bool McastManagerPartition::ProcessSGEntry(McastSGEntry *sg_entry) {
     833        3395 :     CHECK_CONCURRENCY("db::DBTable");
     834             : 
     835        3395 :     sg_entry->clear_on_work_queue();
     836        3395 :     sg_entry->UpdateTree();
     837        3395 :     update_count_++;
     838             : 
     839        3395 :     if (sg_entry->empty()) {
     840         403 :         sg_list_.erase(sg_entry);
     841         403 :         delete sg_entry;
     842             :     }
     843             : 
     844        3395 :     if (sg_list_.empty())
     845         288 :         tree_manager_->RetryDelete();
     846             : 
     847        3395 :     return true;
     848             : }
     849             : 
     850             : //
     851             : // Get the DBTablePartBase for the ErmVpnTable for our partition id.
     852             : //
     853       12452 : DBTablePartBase *McastManagerPartition::GetTablePartition() {
     854       12452 :     return tree_manager_->GetTablePartition(part_id_);
     855             : }
     856             : 
     857           0 : const RoutingInstance *McastManagerPartition::routing_instance() const {
     858           0 :     return tree_manager_->table()->routing_instance();
     859             : }
     860             : 
     861        8681 : BgpServer *McastManagerPartition::server() {
     862        8681 :     return tree_manager_->table()->server();
     863             : }
     864             : 
     865           0 : const BgpServer *McastManagerPartition::server() const {
     866           0 :     return tree_manager_->table()->server();
     867             : }
     868             : 
     869             : //
     870             : // Constructor for McastTreeManager.
     871             : //
     872       42907 : McastTreeManager::McastTreeManager(ErmVpnTable *table)
     873       42907 :     : table_(table),
     874       42907 :       listener_id_(DBTable::kInvalidId),
     875       42907 :       table_delete_ref_(this, table->deleter()) {
     876       42907 :     deleter_.reset(new DeleteActor(this));
     877       42907 : }
     878             : 
     879             : //
     880             : // Destructor for McastTreeManager.
     881             : //
     882       66863 : McastTreeManager::~McastTreeManager() {
     883       66863 : }
     884             : 
     885             : //
     886             : // Initialize the McastTreeManager. We allocate the McastManagerPartitions
     887             : // and register a DBListener for the ErmVpnTable.
     888             : //
     889       42871 : void McastTreeManager::Initialize() {
     890       42871 :     AllocPartitions();
     891       42872 :     listener_id_ = table_->Register(
     892             :         boost::bind(&McastTreeManager::RouteListener, this, _1, _2),
     893             :         "McastTreeManager");
     894       42874 : }
     895             : 
     896             : //
     897             : // Terminate the McastTreeManager. We free the McastManagerPartitions
     898             : // and unregister from the ErmVpnTable.
     899             : //
     900       42874 : void McastTreeManager::Terminate() {
     901       42874 :     table_->Unregister(listener_id_);
     902       42874 :     FreePartitions();
     903       42874 : }
     904             : 
     905             : //
     906             : // Allocate the McastManagerPartitions.
     907             : //
     908       42874 : void McastTreeManager::AllocPartitions() {
     909       85746 :     for (int part_id = 0; part_id < table_->PartitionCount(); part_id++) {
     910       42874 :         partitions_.push_back(new McastManagerPartition(this, part_id));
     911             :     }
     912       42872 : }
     913             : 
     914             : //
     915             : // Free the McastManagerPartitions.
     916             : //
     917       42874 : void McastTreeManager::FreePartitions() {
     918       85748 :     for (size_t part_id = 0; part_id < partitions_.size(); part_id++) {
     919       42874 :         delete partitions_[part_id];
     920             :     }
     921       42874 :     partitions_.clear();
     922       42874 : }
     923             : 
     924        7982 : McastManagerPartition *McastTreeManager::GetPartition(int part_id) {
     925        7982 :     return partitions_[part_id];
     926             : }
     927             : 
     928        3527 : const McastManagerPartition *McastTreeManager::GetPartition(int part_id) const {
     929        3527 :     return partitions_[part_id];
     930             : }
     931             : 
     932             : //
     933             : // Get the DBTablePartBase for the ErmVpnTable for given partition id.
     934             : //
     935       12452 : DBTablePartBase *McastTreeManager::GetTablePartition(size_t part_id) {
     936       12452 :     return table_->GetTablePartition(part_id);
     937             : }
     938             : 
     939             : //
     940             : // Construct export state for the given ErmVpnRoute. Note that the route
     941             : // only needs to be exported to the IPeer from which it was learnt.
     942             : //
     943        3245 : UpdateInfo *McastTreeManager::GetUpdateInfo(ErmVpnRoute *route) {
     944        3245 :     CHECK_CONCURRENCY("db::DBTable");
     945             : 
     946        3245 :     DBState *dbstate = route->GetState(table_, listener_id_);
     947        3245 :     McastForwarder *forwarder = dynamic_cast<McastForwarder *>(dbstate);
     948             : 
     949        3245 :     if (!forwarder)
     950           0 :         return NULL;
     951             : 
     952        3245 :     return forwarder->GetUpdateInfo(table_);
     953             : }
     954             : 
     955             : //
     956             : // DBListener callback handler for Native and Local routes in the ErmVpnTable.
     957             : // It creates, updates or deletes the associated McastForwarder as appropriate.
     958             : //
     959             : // Creates a McastSGEntry if one doesn't already exist. However, McastSGEntrys
     960             : // don't get deleted from here.  They only get deleted from WorkQueue callback
     961             : // routine i.e. McastManagerPartition::ProcessSGEntry.
     962             : //
     963       12689 : void McastTreeManager::TreeNodeListener(McastManagerPartition *partition,
     964             :         ErmVpnRoute *route) {
     965       12689 :     CHECK_CONCURRENCY("db::DBTable");
     966             : 
     967       12689 :     DBState *dbstate = route->GetState(table_, listener_id_);
     968       12689 :     if (!dbstate) {
     969             :         // We have no previous DBState for this route.
     970             :         // Bail if the route is not valid.
     971        1944 :         if (!route->IsValid())
     972          21 :             return;
     973             : 
     974             :         // Create a new McastForwarder and associate it with the route.
     975        1923 :         McastSGEntry *sg_entry = partition->LocateSGEntry(
     976        1923 :             route->GetPrefix().group(), route->GetPrefix().source());
     977        1923 :         McastForwarder *forwarder = new McastForwarder(sg_entry, route);
     978        1923 :         sg_entry->AddForwarder(forwarder);
     979        1923 :         route->SetState(table_, listener_id_, forwarder);
     980             : 
     981             :         // Update local tree route if our RouterId has changed. Ideally,
     982             :         // we should trigger an update of all local trees routes when we
     983             :         // detect a change in RouterId. Instead, we currently check and
     984             :         // update the local route when we detect a new local route from
     985             :         // another node.
     986        1923 :         if (route->GetPrefix().type() == ErmVpnPrefix::LocalTreeRoute)
     987         723 :             sg_entry->UpdateLocalTreeRoute();
     988             :     } else {
     989       10745 :         McastSGEntry *sg_entry = partition->FindSGEntry(
     990       10745 :             route->GetPrefix().group(), route->GetPrefix().source());
     991       10745 :         assert(sg_entry);
     992       10745 :         McastForwarder *forwarder = dynamic_cast<McastForwarder *>(dbstate);
     993       10745 :         assert(forwarder);
     994             : 
     995       10745 :         if (!route->IsValid()) {
     996             :             // Delete the McastForwarder associated with the route.
     997        1923 :             route->ClearState(table_, listener_id_);
     998        1923 :             sg_entry->DeleteForwarder(forwarder);
     999        1923 :             delete forwarder;
    1000        8822 :         } else if (forwarder->Update(route)) {
    1001             :             // Trigger update of the distribution tree.
    1002        1161 :             sg_entry->ChangeForwarder(forwarder);
    1003             :         }
    1004             :     }
    1005             : }
    1006             : 
    1007             : //
    1008             : // DBListener callback handler for GlobalTreeRoutes in the ErmVpnTable. It
    1009             : // updates the tree_result_route_ and triggers re-evaluation of the forest
    1010             : // node McastForwarder's BgpOlist.
    1011             : //
    1012        9553 : void McastTreeManager::TreeResultListener(McastManagerPartition *partition,
    1013             :         ErmVpnRoute *route) {
    1014        9553 :     CHECK_CONCURRENCY("db::DBTable");
    1015             : 
    1016        9553 :     DBState *dbstate = route->GetState(table_, listener_id_);
    1017        9553 :     if (!dbstate) {
    1018             :         // We have no previous DBState for this route.
    1019             :         // Bail if the route is not valid.
    1020        8221 :         if (!route->IsValid())
    1021        6002 :             return;
    1022             : 
    1023             :         // Ignore GlobalTreeRoute if it's not applicable to this control-node.
    1024        2219 :         BgpServer *server = table_->routing_instance()->server();
    1025        2219 :         if (route->GetPrefix().router_id().to_ulong() !=
    1026        2219 :             server->bgp_identifier())
    1027        1777 :             return;
    1028             : 
    1029         442 :         McastSGEntry *sg_entry = partition->LocateSGEntry(
    1030         442 :             route->GetPrefix().group(), route->GetPrefix().source());
    1031         442 :         route->SetState(table_, listener_id_, sg_entry);
    1032         442 :         sg_entry->set_tree_result_route(route);
    1033         442 :         sg_entry->NotifyForestNode();
    1034             :     } else {
    1035        1332 :         McastSGEntry *sg_entry = dynamic_cast<McastSGEntry *>(dbstate);
    1036        1332 :         assert(sg_entry);
    1037             : 
    1038        1332 :         if (!route->IsValid()) {
    1039         442 :             sg_entry->clear_tree_result_route();
    1040         442 :             route->ClearState(table_, listener_id_);
    1041         442 :             partition->EnqueueSGEntry(sg_entry);
    1042             :         }
    1043        1332 :         sg_entry->NotifyForestNode();
    1044             :     }
    1045             : }
    1046             : 
    1047             : //
    1048             : // DBListener callback handler for the ErmVpnTable. GlobalTreeRoutes provide
    1049             : // result information and hence are handled differently than Native and Local
    1050             : // routes, which result in update of a McastForwarder.
    1051             : //
    1052       22242 : void McastTreeManager::RouteListener(
    1053             :         DBTablePartBase *tpart, DBEntryBase *db_entry) {
    1054       22242 :     CHECK_CONCURRENCY("db::DBTable");
    1055             : 
    1056       22242 :     McastManagerPartition *partition = partitions_[tpart->index()];
    1057       22242 :     ErmVpnRoute *route = dynamic_cast<ErmVpnRoute *>(db_entry);
    1058       22242 :     if (route->GetPrefix().type() == ErmVpnPrefix::GlobalTreeRoute) {
    1059        9553 :         TreeResultListener(partition, route);
    1060             :     } else {
    1061       12689 :         TreeNodeListener(partition, route);
    1062             :     }
    1063       22242 : }
    1064             : 
    1065             : 
    1066             : //
    1067             : // Check if the McastTreeManager can be deleted. This can happen only if all
    1068             : // the McastManagerPartitions are empty.
    1069             : //
    1070       42907 : bool McastTreeManager::MayDelete() const {
    1071       42907 :     CHECK_CONCURRENCY("bgp::Config");
    1072             : 
    1073       42907 :     for (PartitionList::const_iterator it = partitions_.begin();
    1074       85781 :          it != partitions_.end(); ++it) {
    1075       42874 :         if (!(*it)->empty())
    1076           0 :             return false;
    1077             :     }
    1078             : 
    1079       42907 :     return true;
    1080             : }
    1081             : 
    1082             : //
    1083             : // Initiate shutdown for the McastTreeManager.
    1084             : //
    1085       42907 : void McastTreeManager::Shutdown() {
    1086       42907 :     CHECK_CONCURRENCY("bgp::Config");
    1087       42907 : }
    1088             : 
    1089             : //
    1090             : // Trigger deletion of the McastTreeManager and propagate the delete to any
    1091             : // dependents.
    1092             : //
    1093       42907 : void McastTreeManager::ManagedDelete() {
    1094       42907 :     deleter_->Delete();
    1095       42907 : }
    1096             : 
    1097             : //
    1098             : // Attempt to enqueue a delete for the McastTreeManager.
    1099             : //
    1100         288 : void McastTreeManager::RetryDelete() {
    1101         288 :     if (!deleter()->IsDeleted())
    1102         288 :         return;
    1103           0 :     deleter()->RetryDelete();
    1104             : }
    1105             : 
    1106             : //
    1107             : // Return the LifetimeActor for the McastTreeManager.
    1108             : //
    1109        6670 : LifetimeActor *McastTreeManager::deleter() {
    1110        6670 :     return deleter_.get();
    1111             : }
    1112             : 
    1113             : //
    1114             : // Return the LifetimeActor for the McastTreeManager.
    1115             : // Const version.
    1116             : //
    1117         190 : const LifetimeActor *McastTreeManager::deleter() const {
    1118         190 :     return deleter_.get();
    1119             : }
    1120             : 
    1121             : //
    1122             : // Return true if the McastTreeManager is deleted.
    1123             : //
    1124         190 : bool McastTreeManager::deleted() const {
    1125         190 :     return deleter_->IsDeleted();
    1126             : }
    1127             : 
    1128        3353 : ErmVpnRoute *McastTreeManager::GetGlobalTreeRootRoute(
    1129             :     const Ip4Address &source, const Ip4Address &group) const {
    1130        3353 :     const McastManagerPartition *partition = GetPartition(table_->Hash(group));
    1131        3353 :     return partition->GetGlobalTreeRootRoute(source, group);
    1132             : }
    1133             : 
    1134        7980 : void McastTreeManager::NotifyForestNode(int part_id, const Ip4Address &source,
    1135             :                                         const Ip4Address &group) {
    1136        7980 :     McastManagerPartition *partition = GetPartition(part_id);
    1137        7980 :     partition->NotifyForestNode(source, group);
    1138        7980 : }
    1139             : 
    1140         239 : bool McastTreeManager::GetForestNodePMSI(ErmVpnRoute *rt, uint32_t *label,
    1141             :         Ip4Address *address, vector<string> *encap) const {
    1142         239 :     if (!rt || !rt->IsUsable())
    1143          65 :         return false;
    1144             :     const McastManagerPartition *partition =
    1145         174 :         GetPartition(rt->get_table_partition()->index());
    1146         174 :     return partition->GetForestNodePMSI(rt, label, address, encap);
    1147             : }

Generated by: LCOV version 1.14