LCOV - code coverage report
Current view: top level - bgp - bgp_evpn.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 122 980 12.4 %
Date: 2026-11-16 02:06:09 Functions: 24 117 20.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/bgp_evpn.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <algorithm>
      11             : #include <string>
      12             : 
      13             : #include "base/set_util.h"
      14             : #include "base/task_annotations.h"
      15             : #include "bgp/bgp_log.h"
      16             : #include "bgp/bgp_multicast.h"
      17             : #include "bgp/bgp_peer_types.h"
      18             : #include "bgp/bgp_server.h"
      19             : #include "bgp/bgp_update.h"
      20             : #include "bgp/ermvpn/ermvpn_route.h"
      21             : #include "bgp/ermvpn/ermvpn_table.h"
      22             : #include "bgp/evpn/evpn_table.h"
      23             : #include "bgp/extended-community/multicast_flags.h"
      24             : #include "bgp/routing-instance/routing_instance.h"
      25             : #include "bgp/routing-instance/routing_instance_analytics_types.h"
      26             : #include "bgp/routing-instance/routing_instance_log.h"
      27             : 
      28             : using std::pair;
      29             : using std::set;
      30             : using std::sort;
      31             : using std::string;
      32             : using std::vector;
      33             : 
      34             : // A global MVPN state for a given <S.G> within a EvpnProjectManager.
      35           0 : EvpnState::EvpnState(const SG &sg, StatesMap *states, EvpnManager *manager) :
      36           0 :         sg_(sg), global_ermvpn_tree_rt_(NULL), states_(states),
      37           0 :         manager_(manager) {
      38           0 :     refcount_ = 0;
      39           0 : }
      40             : 
      41           0 : EvpnState::~EvpnState() {
      42           0 :     assert(!global_ermvpn_tree_rt_);
      43           0 :     EVPN_TRACE(EvpnStateCreate, sg_.source.to_string(), sg_.group.to_string());
      44           0 : }
      45             : 
      46           0 : const ErmVpnTable *EvpnState::table() const {
      47           0 :     return manager_ ? manager_->ermvpn_table() : NULL;
      48             : }
      49             : 
      50             : class EvpnManager::DeleteActor : public LifetimeActor {
      51             : public:
      52          80 :     explicit DeleteActor(EvpnManager *evpn_manager)
      53          80 :         : LifetimeActor(evpn_manager->server()->lifetime_manager()),
      54          80 :           evpn_manager_(evpn_manager) {
      55          80 :     }
      56         160 :     virtual ~DeleteActor() {
      57         160 :     }
      58             : 
      59          80 :     virtual bool MayDelete() const {
      60          80 :         CHECK_CONCURRENCY("bgp::Config");
      61          80 :         return evpn_manager_->MayDelete();
      62             :     }
      63             : 
      64          80 :     virtual void Shutdown() {
      65          80 :         evpn_manager_->Shutdown();
      66          80 :     }
      67             : 
      68          80 :     virtual void Destroy() {
      69          80 :         evpn_manager_->table_->DestroyEvpnManager();
      70          80 :     }
      71             : 
      72             : private:
      73             :     EvpnManager *evpn_manager_;
      74             : };
      75             : 
      76             : //
      77             : // Constructor for EvpnMcastNode. The type indicates whether this is a local
      78             : // or remote EvpnMcastNode.
      79             : //
      80           0 : EvpnMcastNode::EvpnMcastNode(EvpnManagerPartition *partition,
      81           0 :     EvpnRoute *route, uint8_t type)
      82           0 :     : partition_(partition),
      83           0 :       state_(NULL),
      84           0 :       route_(route),
      85           0 :       type_(type),
      86           0 :       label_(0),
      87           0 :       edge_replication_not_supported_(false),
      88           0 :       assisted_replication_supported_(false),
      89           0 :       assisted_replication_leaf_(false) {
      90           0 :     UpdateAttributes(route);
      91           0 : }
      92             : 
      93           0 : EvpnMcastNode::EvpnMcastNode(EvpnManagerPartition *partition,
      94           0 :     EvpnRoute *route, uint8_t type, EvpnStatePtr state)
      95           0 :     : partition_(partition),
      96           0 :       state_(state),
      97           0 :       route_(route),
      98           0 :       type_(type),
      99           0 :       label_(0),
     100           0 :       edge_replication_not_supported_(false),
     101           0 :       assisted_replication_supported_(false),
     102           0 :       assisted_replication_leaf_(false) {
     103           0 :     if (route)
     104           0 :         UpdateAttributes(route);
     105           0 : }
     106             : 
     107             : //
     108             : // Destructor for EvpnMcastNode.
     109             : //
     110           0 : EvpnMcastNode::~EvpnMcastNode() {
     111           0 :     set_state(NULL);
     112           0 : }
     113             : 
     114             : //
     115             : // Update the label and attributes for a EvpnMcastNode.
     116             : // Return true if either of them changed.
     117             : //
     118           0 : bool EvpnMcastNode::UpdateAttributes(EvpnRoute *route) {
     119           0 :     bool changed = false;
     120           0 :     const BgpPath *path = route->BestPath();
     121           0 :     if (path->GetLabel() != label_) {
     122           0 :         label_ = path->GetLabel();
     123           0 :         changed = true;
     124             :     }
     125           0 :     if (path->GetAttr() != attr_) {
     126           0 :         attr_ = path->GetAttr();
     127           0 :         changed = true;
     128             :     }
     129             : 
     130           0 :     const PmsiTunnel *pmsi_tunnel = attr_->pmsi_tunnel();
     131           0 :     uint8_t ar_type = 0;
     132           0 :     bool edge_replication_not_supported = false;
     133           0 :     if (pmsi_tunnel) {
     134           0 :         ar_type = pmsi_tunnel->tunnel_flags() &
     135             :                   PmsiTunnelSpec::AssistedReplicationType;
     136           0 :         if ((pmsi_tunnel->tunnel_flags() &
     137           0 :              PmsiTunnelSpec::EdgeReplicationSupported) == 0) {
     138           0 :             edge_replication_not_supported = true;
     139             :         }
     140           0 :         if (replicator_address_ != pmsi_tunnel->identifier()) {
     141           0 :             replicator_address_ = pmsi_tunnel->identifier();
     142           0 :             changed = true;
     143             :         }
     144             :     }
     145             : 
     146           0 :     if (edge_replication_not_supported != edge_replication_not_supported_) {
     147           0 :         edge_replication_not_supported_ = edge_replication_not_supported;
     148           0 :         changed = true;
     149             :     }
     150             : 
     151           0 :     bool assisted_replication_supported = false;
     152           0 :     if (ar_type == PmsiTunnelSpec::ARReplicator)
     153           0 :         assisted_replication_supported = true;
     154           0 :     if (assisted_replication_supported != assisted_replication_supported_) {
     155           0 :         assisted_replication_supported_ = assisted_replication_supported;
     156           0 :         changed = true;
     157             :     }
     158             : 
     159           0 :     bool assisted_replication_leaf = false;
     160           0 :     if (ar_type == PmsiTunnelSpec::ARLeaf)
     161           0 :         assisted_replication_leaf = true;
     162           0 :     if (assisted_replication_leaf != assisted_replication_leaf_) {
     163           0 :         assisted_replication_leaf_ = assisted_replication_leaf;
     164           0 :         changed = true;
     165             :     }
     166             : 
     167           0 :     if (address_ != path->GetAttr()->nexthop().to_v4()) {
     168           0 :         address_ = path->GetAttr()->nexthop().to_v4();
     169           0 :         changed = true;
     170             :     }
     171             : 
     172           0 :     return changed;
     173             : }
     174             : 
     175             : //
     176             : // Constructor for EvpnLocalMcastNode.
     177             : //
     178             : // Add an Inclusive Multicast route corresponding to Broadcast MAC route.
     179             : //
     180             : // Need to Notify the Broadcast MAC route so that the table Export method
     181             : // can run and build the OList. OList is not built till EvpnLocalMcastNode
     182             : // has been created.
     183             : //
     184           0 : EvpnLocalMcastNode::EvpnLocalMcastNode(EvpnManagerPartition *partition,
     185           0 :     EvpnRoute *route)
     186             :     : EvpnMcastNode(partition, route, EvpnMcastNode::LocalNode),
     187           0 :       inclusive_mcast_route_(NULL) {
     188           0 :     AddInclusiveMulticastRoute();
     189           0 :     DBTablePartition *tbl_partition = partition_->GetTablePartition();
     190           0 :     tbl_partition->Notify(route_);
     191           0 : }
     192             : 
     193           0 : EvpnLocalMcastNode::EvpnLocalMcastNode(EvpnManagerPartition *partition,
     194           0 :     EvpnRoute *route, EvpnStatePtr state)
     195             :     : EvpnMcastNode(partition, route, EvpnMcastNode::LocalNode, state),
     196           0 :       inclusive_mcast_route_(NULL) {
     197           0 :     if (!route)
     198           0 :         return;
     199           0 :     AddInclusiveMulticastRoute();
     200           0 :     DBTablePartition *tbl_partition = partition_->GetTablePartition();
     201           0 :     tbl_partition->Notify(route_);
     202           0 : }
     203             : 
     204             : //
     205             : // Destructor for EvpnLocalMcastNode.
     206             : //
     207           0 : EvpnLocalMcastNode::~EvpnLocalMcastNode() {
     208           0 :     DeleteInclusiveMulticastRoute();
     209           0 : }
     210             : 
     211             : //
     212             : // Add Inclusive Multicast route for this EvpnLocalMcastNode.
     213             : // The attributes are based on the Broadcast MAC route.
     214             : //
     215           0 : void EvpnLocalMcastNode::AddInclusiveMulticastRoute() {
     216             :     // No need to create IMET route if group is specified
     217           0 :     if (!route_->GetPrefix().group().is_unspecified())
     218           0 :         return;
     219           0 :     assert(!inclusive_mcast_route_);
     220           0 :     if (label_ == 0)
     221           0 :         return;
     222             : 
     223             :     // Construct the prefix and route key.
     224             :     // Build the RD using the TOR IP address, not the TOR agent IP address.
     225             :     // This ensures that the MAC broadcast route from the primary and backup
     226             :     // TOR Agents results in the same Inclusive Multicast prefix.
     227           0 :     const EvpnPrefix &mac_prefix = route_->GetPrefix();
     228             :     RouteDistinguisher rd(
     229           0 :         address_.to_ulong(), mac_prefix.route_distinguisher().GetVrfId());
     230           0 :     EvpnPrefix prefix(rd, mac_prefix.tag(), address_);
     231           0 :     EvpnRoute rt_key(prefix);
     232             : 
     233             :     // Find or create the route.
     234           0 :     DBTablePartition *tbl_partition = partition_->GetTablePartition();
     235           0 :     EvpnRoute *route = static_cast<EvpnRoute *>(tbl_partition->Find(&rt_key));
     236           0 :     if (!route) {
     237           0 :         route = new EvpnRoute(prefix);
     238           0 :         tbl_partition->Add(route);
     239             :     } else {
     240           0 :         route->ClearDelete();
     241             :     }
     242             :     // Add MulticastFlags community to specify that it supports SMET route
     243           0 :     ExtCommunity::ExtCommunityList mcastFlags;
     244           0 :     mcastFlags.push_back(MulticastFlags().GetExtCommunity());
     245           0 :     ExtCommunityPtr ext_community = partition_->server()->extcomm_db()->
     246           0 :             ReplaceMFlagsAndLocate(attr_->ext_community(), mcastFlags);
     247           0 :     attr_ = partition_->server()->attr_db()->ReplaceExtCommunityAndLocate(
     248           0 :         attr_.get(), ext_community);
     249             : 
     250             :     // Add a path with source BgpPath::Local and the peer address as path_id.
     251           0 :     uint32_t path_id = mac_prefix.route_distinguisher().GetAddress();
     252           0 :     BgpPath *path = new BgpPath(path_id, BgpPath::Local, attr_, 0, label_);
     253           0 :     route->InsertPath(path);
     254           0 :     inclusive_mcast_route_ = route;
     255           0 :     tbl_partition->Notify(inclusive_mcast_route_);
     256           0 :     BGP_LOG_ROUTE(partition_->table(), static_cast<IPeer *>(NULL),
     257             :         route, "Insert new Local path");
     258           0 : }
     259             : 
     260             : //
     261             : // Delete Inclusive Multicast route for this EvpnLocalMcastNode.
     262             : //
     263           0 : void EvpnLocalMcastNode::DeleteInclusiveMulticastRoute() {
     264           0 :     if (!inclusive_mcast_route_)
     265           0 :         return;
     266             : 
     267           0 :     const EvpnPrefix &mac_prefix = route_->GetPrefix();
     268           0 :     uint32_t path_id = mac_prefix.route_distinguisher().GetAddress();
     269           0 :     DBTablePartition *tbl_partition = partition_->GetTablePartition();
     270           0 :     inclusive_mcast_route_->RemovePath(BgpPath::Local, path_id);
     271           0 :     BGP_LOG_ROUTE(partition_->table(), static_cast<IPeer *>(NULL),
     272             :         inclusive_mcast_route_, "Delete Local path");
     273             : 
     274           0 :     if (!inclusive_mcast_route_->HasPaths()) {
     275           0 :         tbl_partition->Delete(inclusive_mcast_route_);
     276             :     } else {
     277           0 :         tbl_partition->Notify(inclusive_mcast_route_);
     278             :     }
     279           0 :     inclusive_mcast_route_ = NULL;
     280             : }
     281             : 
     282             : //
     283             : // Handle update of EvpnLocalMcastNode.
     284             : //
     285             : // We delete and add the Inclusive Multicast route to ensure that all the
     286             : // attributes are updated. An in-place update is not always possible since
     287             : // the vRouter address is part of the key for the Inclusive Multicast route.
     288             : //
     289           0 : void EvpnLocalMcastNode::TriggerUpdate() {
     290           0 :     DeleteInclusiveMulticastRoute();
     291           0 :     AddInclusiveMulticastRoute();
     292           0 : }
     293             : 
     294             : //
     295             : // Construct an UpdateInfo with the RibOutAttr that needs to be advertised to
     296             : // the IPeer for the EvpnRoute associated with this EvpnLocalMcastNode. This
     297             : // is used the Export method of the EvpnTable. It is expected that the caller
     298             : // fills in the target RibPeerSet in the UpdateInfo.
     299             : //
     300             : // The main functionality here is to build a per-IPeer BgpOList from the list
     301             : // of EvpnRemoteMcastNodes.
     302             : //
     303           0 : UpdateInfo *EvpnLocalMcastNode::GetUpdateInfo(EvpnRoute *route) {
     304           0 :     CHECK_CONCURRENCY("db::DBTable");
     305             : 
     306             :     // Nothing to send for a leaf as it already knows the replicator-address.
     307           0 :     if (assisted_replication_leaf_)
     308           0 :         return NULL;
     309             : 
     310           0 :     const RoutingInstance *rti = partition_->table()->routing_instance();
     311           0 :     bool pbb_evpn_enable = rti->virtual_network_pbb_evpn_enable();
     312           0 :     uint32_t local_ethernet_tag = route_->GetPrefix().tag();
     313             : 
     314           0 :     EvpnState::SG sg = EvpnState::SG(route->GetPrefix().source(),
     315           0 :                                      route->GetPrefix().group());
     316             :     // Go through list of EvpnRemoteMcastNodes and build the BgpOList.
     317           0 :     BgpOListSpec olist_spec(BgpAttribute::OList);
     318           0 :     if (partition_->remote_mcast_node_list()->count(sg)) {
     319           0 :         set<EvpnMcastNode*> nodes = (*partition_->remote_mcast_node_list())[sg];
     320           0 :         BOOST_FOREACH(EvpnMcastNode *node, nodes) {
     321           0 :             if (node->address() == address_)
     322           0 :                 continue;
     323           0 :             if (node->assisted_replication_leaf())
     324           0 :                 continue;
     325           0 :             if (!edge_replication_not_supported_ &&
     326           0 :                 !node->edge_replication_not_supported())
     327           0 :                 continue;
     328           0 :             uint32_t remote_ethernet_tag = node->route()->GetPrefix().tag();
     329             : 
     330           0 :             if (pbb_evpn_enable && remote_ethernet_tag &&
     331             :                 (local_ethernet_tag != remote_ethernet_tag))
     332           0 :                 continue;
     333             : 
     334           0 :             const ExtCommunity *extcomm = node->attr()->ext_community();
     335           0 :             BgpOListElem elem(node->address(), node->label(),
     336           0 :                     extcomm ? extcomm->GetTunnelEncap() : vector<string>());
     337           0 :             olist_spec.elements.push_back(elem);
     338           0 :         }
     339           0 :     }
     340             : 
     341             :     // Go through list of leaf EvpnMcastNodes and build the leaf BgpOList.
     342           0 :     BgpOListSpec leaf_olist_spec(BgpAttribute::LeafOList);
     343           0 :     if (assisted_replication_supported_) {
     344           0 :         if (partition_->leaf_node_list()->count(sg)) {
     345           0 :             set<EvpnMcastNode*> nodes = (*partition_->leaf_node_list())[sg];
     346           0 :             BOOST_FOREACH(EvpnMcastNode *node, nodes) {
     347           0 :                 if (node->replicator_address() != address_)
     348           0 :                     continue;
     349             : 
     350           0 :                 const ExtCommunity *extcomm = node->attr()->ext_community();
     351           0 :                 BgpOListElem elem(node->address(), node->label(),
     352           0 :                         extcomm ? extcomm->GetTunnelEncap() : vector<string>());
     353           0 :                 leaf_olist_spec.elements.push_back(elem);
     354           0 :             }
     355           0 :         }
     356             :     }
     357             : 
     358             :     // Bail if both BgpOLists are empty.
     359           0 :     if (olist_spec.elements.empty() && leaf_olist_spec.elements.empty())
     360           0 :         return NULL;
     361             : 
     362             :     // Add BgpOList and leaf BgpOList to RibOutAttr for broadcast MAC route.
     363           0 :     BgpAttrDB *attr_db = partition_->server()->attr_db();
     364           0 :     BgpAttrPtr attr = attr_db->ReplaceOListAndLocate(attr_.get(), &olist_spec);
     365           0 :     attr = attr_db->ReplaceLeafOListAndLocate(attr.get(), &leaf_olist_spec);
     366             : 
     367           0 :     UpdateInfo *uinfo = new UpdateInfo;
     368             :     uinfo->roattr =
     369           0 :         RibOutAttr(partition_->table(), route_, attr.get(), 0, false, true);
     370           0 :     return uinfo;
     371           0 : }
     372             : 
     373             : //
     374             : // Constructor for EvpnRemoteMcastNode.
     375             : //
     376           0 : EvpnRemoteMcastNode::EvpnRemoteMcastNode(EvpnManagerPartition *partition,
     377           0 :     EvpnRoute *route)
     378           0 :     : EvpnMcastNode(partition, route, EvpnMcastNode::RemoteNode) {
     379           0 : }
     380             : 
     381           0 : EvpnRemoteMcastNode::EvpnRemoteMcastNode(EvpnManagerPartition *partition,
     382           0 :     EvpnRoute *route, EvpnStatePtr state)
     383           0 :     : EvpnMcastNode(partition, route, EvpnMcastNode::RemoteNode, state) {
     384           0 : }
     385             : 
     386             : //
     387             : // Destructor for EvpnRemoteMcastNode.
     388             : //
     389           0 : EvpnRemoteMcastNode::~EvpnRemoteMcastNode() {
     390           0 : }
     391             : 
     392             : //
     393             : // Handle update of EvpnRemoteMcastNode.
     394             : //
     395           0 : void EvpnRemoteMcastNode::TriggerUpdate() {
     396           0 : }
     397             : 
     398             : //
     399             : // Constructor for EvpnSegment.
     400             : //
     401           0 : EvpnSegment::EvpnSegment(EvpnManager *manager, const EthernetSegmentId &esi)
     402           0 :   : evpn_manager_(manager),
     403           0 :     esi_(esi),
     404           0 :     esi_ad_route_(NULL),
     405           0 :     single_active_(true),
     406           0 :     route_lists_(DB::PartitionCount()) {
     407           0 : }
     408             : 
     409             : //
     410             : // Destructor for EvpnSegment.
     411             : //
     412           0 : EvpnSegment::~EvpnSegment() {
     413           0 :     assert(!esi_ad_route_);
     414           0 :     assert(pe_list_.empty());
     415           0 : }
     416             : 
     417             : //
     418             : // Add the given MAC route as a dependent of this EvpnSegment.
     419             : //
     420           0 : void EvpnSegment::AddMacRoute(size_t part_id, EvpnRoute *route) {
     421           0 :     pair<RouteList::iterator, bool> ret = route_lists_[part_id].insert(route);
     422           0 :     assert(ret.second);
     423           0 : }
     424             : 
     425             : //
     426             : // Delete the given MAC route as a dependent of this EvpnSegment.
     427             : // Trigger deletion of the EvpnSegment if there are no dependent
     428             : // routes in the partition.
     429             : //
     430           0 : void EvpnSegment::DeleteMacRoute(size_t part_id, EvpnRoute *route) {
     431           0 :     size_t count = route_lists_[part_id].erase(route);
     432           0 :     assert(count == 1);
     433           0 :     if (route_lists_[part_id].empty())
     434           0 :         evpn_manager_->TriggerSegmentDelete(this);
     435           0 : }
     436             : 
     437             : //
     438             : // Trigger an update of all dependent MAC routes for this EvpnSegment.
     439             : // Note that the bgp::EvpnSegment task is mutually exclusive with the
     440             : // db::DBTable task.
     441             : //
     442           0 : void EvpnSegment::TriggerMacRouteUpdate() {
     443           0 :     CHECK_CONCURRENCY("bgp::EvpnSegment");
     444             : 
     445           0 :     for (size_t part_id = 0; part_id < route_lists_.size(); ++part_id) {
     446           0 :         EvpnManagerPartition *partition = evpn_manager_->GetPartition(part_id);
     447           0 :         BOOST_FOREACH(EvpnRoute *route, route_lists_[part_id]) {
     448           0 :             partition->TriggerMacRouteUpdate(route);
     449             :         }
     450             :     }
     451           0 : }
     452             : 
     453             : //
     454             : // Update the PE list for this EvpnSegment. This should be called when
     455             : // the AutoDisocvery route is updated.
     456             : // Return true if there's a change in the PE list i.e. if an entry is
     457             : // added, deleted or updated.
     458             : //
     459           0 : bool EvpnSegment::UpdatePeList() {
     460           0 :     CHECK_CONCURRENCY("bgp::EvpnSegment");
     461             : 
     462             :     // Mark all entries as invalid.
     463           0 :     for (RemotePeList::iterator it = pe_list_.begin();
     464           0 :         it != pe_list_.end(); ++it) {
     465           0 :         it->esi_valid = false;
     466             :     }
     467             : 
     468             :     // Go through all paths for the route and refresh/update existing entries
     469             :     // or add new ones as necessary. Remember that there was a change in the
     470             :     // list if any element changed or is added/deleted.
     471           0 :     bool changed = false;
     472           0 :     for (Route::PathList::const_iterator path_it =
     473           0 :          esi_ad_route_->GetPathList().begin();
     474           0 :          path_it != esi_ad_route_->GetPathList().end(); ++path_it) {
     475             :         const BgpPath *path =
     476           0 :             static_cast<const BgpPath *>(path_it.operator->());
     477           0 :         const BgpAttr *attr = path->GetAttr();
     478             : 
     479             :         // Skip non-VXLAN paths for now.
     480           0 :         const ExtCommunity *extcomm = attr->ext_community();
     481           0 :         if (!extcomm || !extcomm->ContainsTunnelEncapVxlan())
     482           0 :             continue;
     483             : 
     484             :         // Go through existing pe list and try to find the RemotePe.
     485           0 :         bool found = false;
     486           0 :         RemotePe remote_pe(path);
     487           0 :         for (RemotePeList::iterator it = pe_list_.begin();
     488           0 :             it != pe_list_.end(); ++it) {
     489             :             // Skip if the nexthop doesn't match.
     490           0 :             if (it->attr->nexthop() != remote_pe.attr->nexthop())
     491           0 :                 continue;
     492             : 
     493             :             // We're done if there are multiple paths for the same remote PE
     494             :             // e.g. when a pair of route reflectors is being used.
     495           0 :             if (it->esi_valid) {
     496           0 :                 found = true;
     497           0 :                 break;
     498             :             }
     499             : 
     500             :             // Check if we have a match.
     501           0 :             if (*it == remote_pe) {
     502           0 :                 it->esi_valid = true;
     503           0 :                 found = true;
     504           0 :                 break;
     505             :             }
     506             :         }
     507             : 
     508             :         // Add a new entry to the pe list if we didn't find the RemotePe.
     509           0 :         if (!found) {
     510           0 :             pe_list_.push_back(remote_pe);
     511           0 :             changed = true;
     512             :         }
     513           0 :     }
     514             : 
     515             :     // Erase invalid entries from the list.
     516           0 :     for (RemotePeList::iterator it = pe_list_.begin(), next = it;
     517           0 :         it != pe_list_.end(); it = next) {
     518           0 :         ++next;
     519           0 :         if (!it->esi_valid) {
     520           0 :             pe_list_.erase(it);
     521           0 :             changed = true;
     522             :         }
     523             :     }
     524             : 
     525             :     // Update the single active status of the EvpnSegment itself.
     526           0 :     single_active_ = false;
     527           0 :     for (RemotePeList::iterator it = pe_list_.begin();
     528           0 :         it != pe_list_.end(); ++it) {
     529           0 :         if (it->single_active) {
     530           0 :             single_active_ = true;
     531           0 :             break;
     532             :         }
     533             :     }
     534             : 
     535           0 :     return changed;
     536             : }
     537             : 
     538             : //
     539             : // Return true if it's safe to delete this EvpnSegment.
     540             : //
     541           0 : bool EvpnSegment::MayDelete() const {
     542           0 :     CHECK_CONCURRENCY("bgp::EvpnSegment");
     543             : 
     544             :     // Bail if we have state set on the per-ESI AD route.
     545           0 :     if (esi_ad_route_)
     546           0 :         return false;
     547             : 
     548             :     // Bail if the dependent route list for any partition is not empty.
     549           0 :     for (size_t part_id = 0; part_id < route_lists_.size(); ++part_id) {
     550           0 :         if (!route_lists_[part_id].empty())
     551           0 :             return false;
     552             :     }
     553             : 
     554           0 :     return true;
     555             : }
     556             : 
     557             : //
     558             : // Constructor for EvpnSegment::RemotePe.
     559             : //
     560           0 : EvpnSegment::RemotePe::RemotePe(const BgpPath *path)
     561           0 :   : esi_valid(true),
     562           0 :     single_active(path->GetAttr()->evpn_single_active()),
     563           0 :     peer(path->GetPeer()),
     564           0 :     attr(path->GetAttr()),
     565           0 :     flags(path->GetFlags()),
     566           0 :     src(path->GetSource()) {
     567           0 : }
     568             : 
     569             : //
     570             : // Equality operator for EvpnSegment::RemotePe.
     571             : // Do not compare esi_valid and single_active fields since they are
     572             : // derived.
     573             : //
     574           0 : bool EvpnSegment::RemotePe::operator==(const RemotePe &rhs) const {
     575           0 :     if (peer != rhs.peer)
     576           0 :         return false;
     577           0 :     if (attr != rhs.attr)
     578           0 :         return false;
     579           0 :     if (flags != rhs.flags)
     580           0 :         return false;
     581           0 :     if (src != rhs.src)
     582           0 :         return false;
     583           0 :     return true;
     584             : }
     585             : 
     586             : //
     587             : // Constructor for EvpnMacState.
     588             : //
     589           0 : EvpnMacState::EvpnMacState(EvpnManager *evpn_manager, EvpnRoute *route)
     590           0 :   : evpn_manager_(evpn_manager), route_(route), segment_(NULL) {
     591           0 : }
     592             : 
     593             : //
     594             : // Destructor for EvpnMacState.
     595             : //
     596           0 : EvpnMacState::~EvpnMacState() {
     597           0 :     assert(segment_ == NULL);
     598           0 :     assert(aliased_path_list_.empty());
     599           0 : }
     600             : 
     601             : //
     602             : // Add the BgpPath specified by the iterator to the aliased path list.
     603             : // Also inserts the BgpPath to the BgpRoute.
     604             : //
     605           0 : void EvpnMacState::AddAliasedPath(AliasedPathList::const_iterator it) {
     606           0 :     BgpPath *path = *it;
     607           0 :     const IPeer *peer = path->GetPeer();
     608           0 :     aliased_path_list_.insert(path);
     609           0 :     route_->InsertPath(path);
     610           0 :     BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
     611             :         "Added aliased path " << route_->ToString() <<
     612             :         " peer " << (peer ? peer->ToString() : "None") <<
     613             :         " nexthop " << path->GetAttr()->nexthop().to_string() <<
     614             :         " label " << path->GetLabel() <<
     615             :         " in table " << evpn_manager_->table()->name());
     616           0 : }
     617             : 
     618             : //
     619             : // Delete the BgpPath specified by the iterator from the aliased path list.
     620             : // Also deletes the BgpPath from the BgpRoute.
     621             : //
     622           0 : void EvpnMacState::DeleteAliasedPath(AliasedPathList::const_iterator it) {
     623           0 :     BgpPath *path = *it;
     624           0 :     const IPeer *peer = path->GetPeer();
     625           0 :     BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
     626             :         "Deleted aliased path " << route_->ToString() <<
     627             :         " peer " << (peer ? peer->ToString() : "None") <<
     628             :         " nexthop " << path->GetAttr()->nexthop().to_string() <<
     629             :         " label " << path->GetLabel() <<
     630             :         " in table " << evpn_manager_->table()->name());
     631           0 :     route_->DeletePath(path);
     632           0 :     aliased_path_list_.erase(it);
     633           0 : }
     634             : 
     635             : //
     636             : // Find or create the matching aliased BgpPath.
     637             : //
     638           0 : BgpPath *EvpnMacState::LocateAliasedPath(
     639             :     const EvpnSegment::RemotePe *remote_pe, uint32_t label) {
     640           0 :     const IPeer *peer = remote_pe->peer;
     641           0 :     const BgpAttr *attr = remote_pe->attr.get();
     642           0 :     uint32_t flags = remote_pe->flags | BgpPath::AliasedPath;
     643           0 :     BgpPath::PathSource src = remote_pe->src;
     644           0 :     for (AliasedPathList::iterator it = aliased_path_list_.begin();
     645           0 :         it != aliased_path_list_.end(); ++it) {
     646           0 :         BgpPath *path = *it;
     647           0 :         if (path->GetPeer() == peer &&
     648           0 :             path->GetAttr() == attr &&
     649           0 :             path->GetFlags() == flags &&
     650           0 :             path->GetLabel() == label) {
     651           0 :             return path;
     652             :         }
     653             :     }
     654             : 
     655           0 :     return (new BgpPath(peer, src, attr, flags, label));
     656             : }
     657             : 
     658             : //
     659             : // Update aliased BgpPaths for the EvpnRoute based on the remote PEs
     660             : // for the EvpnSegment.
     661             : // Return true if the list of aliased paths is modified.
     662             : //
     663           0 : bool EvpnMacState::ProcessMacRouteAliasing() {
     664           0 :     CHECK_CONCURRENCY("db::DBTable");
     665             : 
     666           0 :     const BgpPath *path = route_->BestPath();
     667             : 
     668             :     // Find the remote PE entry that corresponds to the primary path.
     669           0 :     bool found_primary_pe = false;
     670           0 :     EvpnSegment::const_iterator it;
     671           0 :     if (path && segment_ && !segment_->single_active())
     672           0 :         it = segment_->begin();
     673           0 :     for (; path && segment_ && !segment_->single_active() &&
     674           0 :          it != segment_->end(); ++it) {
     675           0 :         if (path->GetAttr()->nexthop() != it->attr->nexthop())
     676           0 :             continue;
     677           0 :         found_primary_pe = true;
     678           0 :         break;
     679             :     }
     680             : 
     681             :     // Go through the remote PE list for the EvpnSegment and build the
     682             :     // list of future aliased paths.
     683           0 :     AliasedPathList future_aliased_path_list;
     684           0 :     if (found_primary_pe && path && segment_ && !segment_->single_active())
     685           0 :         it = segment_->begin();
     686           0 :     for (; found_primary_pe && path && segment_ &&
     687           0 :          !segment_->single_active() && it != segment_->end(); ++it) {
     688             :         // Skip if there's a BGP_XMPP path with the remote PE as nexthop.
     689           0 :         if (route_->FindPath(it->attr->nexthop()))
     690           0 :             continue;
     691             : 
     692             :         // Locate the aliased path using attributes for the remote PE and
     693             :         // label from the primary path.
     694             :         BgpPath *aliased_path =
     695           0 :             LocateAliasedPath(it.operator->(), path->GetLabel());
     696           0 :         future_aliased_path_list.insert(aliased_path);
     697             :     }
     698             : 
     699             :     // Reconcile the current and future aliased paths and notify/delete the
     700             :     // route as appropriate.
     701           0 :     bool modified = set_synchronize(&aliased_path_list_,
     702             :         &future_aliased_path_list,
     703             :         boost::bind(&EvpnMacState::AddAliasedPath, this, _1),
     704             :         boost::bind(&EvpnMacState::DeleteAliasedPath, this, _1));
     705           0 :     return modified;
     706           0 : }
     707             : 
     708             : //
     709             : // Constructor for EvpnManagerPartition.
     710             : //
     711         320 : EvpnManagerPartition::EvpnManagerPartition(EvpnManager *evpn_manager,
     712         320 :     size_t part_id)
     713         320 :     : evpn_manager_(evpn_manager),
     714         320 :       part_id_(part_id),
     715         640 :       mac_update_trigger_(new TaskTrigger(
     716             :           boost::bind(&EvpnManagerPartition::ProcessMacUpdateList, this),
     717         640 :           TaskScheduler::GetInstance()->GetTaskId("db::DBTable"),
     718         640 :           part_id)) {
     719         320 :     table_partition_ = evpn_manager_->GetTablePartition(part_id);
     720         320 : }
     721             : 
     722             : //
     723             : // Destructor for EvpnManagerPartition.
     724             : //
     725         320 : EvpnManagerPartition::~EvpnManagerPartition() {
     726         320 :     assert(local_mcast_node_list_.empty());
     727         320 :     assert(remote_mcast_node_list_.empty());
     728         320 :     assert(mac_update_list_.empty());
     729         320 : }
     730             : 
     731             : //
     732             : // Get the DBTablePartition for the EvpnTable for our partition id.
     733             : //
     734           0 : DBTablePartition *EvpnManagerPartition::GetTablePartition() {
     735           0 :     return evpn_manager_->GetTablePartition(part_id_);
     736             : }
     737             : 
     738             : //
     739             : // Notify the Broadcast MAC route for the given EvpnMcastNode.
     740             : //
     741           0 : void EvpnManagerPartition::NotifyNodeRoute(EvpnMcastNode *node) {
     742           0 :     DBTablePartition *tbl_partition = GetTablePartition();
     743           0 :     tbl_partition->Notify(node->route());
     744           0 : }
     745             : 
     746             : //
     747             : // Go through all replicator EvpnMcastNodes and notify associated Broadcast
     748             : // MAC route.
     749             : //
     750           0 : void EvpnManagerPartition::NotifyReplicatorNodeRoutes() {
     751           0 :     DBTablePartition *tbl_partition = GetTablePartition();
     752           0 :     EvpnMcastNodeList::const_iterator it = replicator_node_list_.begin();
     753           0 :     for (; it != replicator_node_list_.end(); it++) {
     754           0 :         BOOST_FOREACH(EvpnMcastNode *node, it->second)
     755           0 :             tbl_partition->Notify(node->route());
     756             :     }
     757           0 : }
     758             : 
     759             : //
     760             : // Go through all ingress replication client EvpnMcastNodes and notify the
     761             : // associated Broadcast MAC route.
     762             : //
     763           0 : void EvpnManagerPartition::NotifyIrClientNodeRoutes(
     764             :     bool exclude_edge_replication_supported) {
     765           0 :     DBTablePartition *tbl_partition = GetTablePartition();
     766           0 :     EvpnMcastNodeList::const_iterator it = ir_client_node_list_.begin();
     767           0 :     for (; it != ir_client_node_list_.end(); it++) {
     768           0 :         BOOST_FOREACH(EvpnMcastNode *node, it->second) {
     769           0 :             if (exclude_edge_replication_supported &&
     770           0 :                 !node->edge_replication_not_supported()) {
     771           0 :                 continue;
     772             :             }
     773           0 :             tbl_partition->Notify(node->route());
     774             :         }
     775             :     }
     776           0 : }
     777             : 
     778             : //
     779             : // Add an EvpnMcastNode to the EvpnManagerPartition.
     780             : //
     781           0 : void EvpnManagerPartition::AddMcastNode(EvpnMcastNode *node, EvpnRoute *rt) {
     782           0 :     EvpnState::SG sg = EvpnState::SG(rt->GetPrefix().source(),
     783           0 :                                      rt->GetPrefix().group());
     784           0 :     if (node->type() == EvpnMcastNode::LocalNode) {
     785           0 :         local_mcast_node_list_[sg].insert(node);
     786           0 :         if (node->assisted_replication_supported())
     787           0 :             replicator_node_list_[sg].insert(node);
     788           0 :         if (!node->assisted_replication_leaf())
     789           0 :             ir_client_node_list_[sg].insert(node);
     790           0 :         NotifyNodeRoute(node);
     791             :     } else {
     792           0 :         remote_mcast_node_list_[sg].insert(node);
     793           0 :         if (node->assisted_replication_leaf()) {
     794           0 :             leaf_node_list_[sg].insert(node);
     795           0 :             NotifyReplicatorNodeRoutes();
     796           0 :         } else if (node->edge_replication_not_supported()) {
     797           0 :             regular_node_list_[sg].insert(node);
     798           0 :             NotifyIrClientNodeRoutes(false);
     799           0 :         } else if (!node->assisted_replication_leaf()) {
     800           0 :             NotifyIrClientNodeRoutes(true);
     801             :         }
     802             :     }
     803           0 : }
     804             : 
     805             : //
     806             : // Delete an EvpnMcastNode from the EvpnManagerPartition.
     807             : //
     808           0 : bool EvpnManagerPartition::RemoveMcastNodeFromList(EvpnState::SG &sg,
     809             :                                                    EvpnMcastNode *node,
     810             :                                                    EvpnMcastNodeList *list) {
     811           0 :     size_t deleted = 0;
     812           0 :     if (list->count(sg)) {
     813           0 :         deleted = (*list)[sg].erase(node);
     814           0 :         if ((*list)[sg].size() == 0)
     815           0 :             list->erase(sg);
     816             :     }
     817           0 :     return (deleted > 0);
     818             : }
     819             : 
     820             : //
     821             : // Delete an EvpnMcastNode from the EvpnManagerPartition.
     822             : //
     823           0 : void EvpnManagerPartition::DeleteMcastNode(EvpnMcastNode *node,
     824             :                                            EvpnRoute * rt) {
     825           0 :     EvpnState::SG sg = EvpnState::SG(rt->GetPrefix().source(),
     826           0 :                                      rt->GetPrefix().group());
     827           0 :     if (node->type() == EvpnMcastNode::LocalNode) {
     828           0 :         RemoveMcastNodeFromList(sg, node, &local_mcast_node_list_);
     829           0 :         RemoveMcastNodeFromList(sg, node, &replicator_node_list_);
     830           0 :         RemoveMcastNodeFromList(sg, node, &ir_client_node_list_);
     831             :     } else {
     832           0 :         RemoveMcastNodeFromList(sg, node, &remote_mcast_node_list_);
     833           0 :         if (RemoveMcastNodeFromList(sg, node, &leaf_node_list_)) {
     834           0 :             NotifyReplicatorNodeRoutes();
     835             :         } else {
     836           0 :             NotifyIrClientNodeRoutes(true);
     837             :         }
     838           0 :         if (RemoveMcastNodeFromList(sg, node, &regular_node_list_)) {
     839           0 :             NotifyIrClientNodeRoutes(false);
     840             :         }
     841             :     }
     842           0 :     if (empty())
     843           0 :         evpn_manager_->RetryDelete();
     844           0 : }
     845             : 
     846             : //
     847             : // Update an EvpnMcastNode in the EvpnManagerPartition.
     848             : // Need to remove/add EvpnMcastNode from the replicator, leaf and ir client
     849             : // lists as appropriate.
     850             : //
     851           0 : void EvpnManagerPartition::UpdateMcastNode(EvpnMcastNode *node, EvpnRoute *rt) {
     852           0 :     node->TriggerUpdate();
     853           0 :     EvpnState::SG sg = EvpnState::SG(rt->GetPrefix().source(),
     854           0 :                                      rt->GetPrefix().group());
     855           0 :     if (node->type() == EvpnMcastNode::LocalNode) {
     856           0 :         RemoveMcastNodeFromList(sg, node, &replicator_node_list_);
     857           0 :         if (node->assisted_replication_supported())
     858           0 :             replicator_node_list_[sg].insert(node);
     859           0 :         RemoveMcastNodeFromList(sg, node, &ir_client_node_list_);
     860           0 :         if (!node->assisted_replication_leaf())
     861           0 :             ir_client_node_list_[sg].insert(node);
     862           0 :         NotifyNodeRoute(node);
     863             :     } else {
     864           0 :         bool was_leaf = RemoveMcastNodeFromList(sg, node, &leaf_node_list_);
     865           0 :         if (node->assisted_replication_leaf())
     866           0 :             leaf_node_list_[sg].insert(node);
     867           0 :         if (was_leaf || node->assisted_replication_leaf())
     868           0 :             NotifyReplicatorNodeRoutes();
     869           0 :         if (!was_leaf || !node->assisted_replication_leaf())
     870           0 :             NotifyIrClientNodeRoutes(true);
     871           0 :         bool was_regular = RemoveMcastNodeFromList(
     872             :                                    sg, node, &regular_node_list_);
     873           0 :         if (node->edge_replication_not_supported())
     874           0 :             regular_node_list_[sg].insert(node);
     875           0 :         if (was_regular || node->edge_replication_not_supported())
     876           0 :             NotifyIrClientNodeRoutes(false);
     877             :     }
     878           0 : }
     879             : 
     880             : //
     881             : // Add the given MAC route to the update list.
     882             : // This method gets called either when the MAC route itself changes or when
     883             : // the remote PE list for the EvpnSegment of the MAC route gets updated.
     884             : //
     885           0 : void EvpnManagerPartition::TriggerMacRouteUpdate(EvpnRoute *route) {
     886           0 :     CHECK_CONCURRENCY("db::DBTable", "bgp::EvpnSegment");
     887             : 
     888           0 :     mac_update_list_.insert(route);
     889           0 :     mac_update_trigger_->Set();
     890           0 : }
     891             : 
     892             : //
     893             : // Process the MAC route update list for this EvpnManagerPartition.
     894             : //
     895           0 : bool EvpnManagerPartition::ProcessMacUpdateList() {
     896           0 :     CHECK_CONCURRENCY("db::DBTable");
     897             : 
     898           0 :     BgpTable *table = evpn_manager_->table();
     899           0 :     int listener_id = evpn_manager_->listener_id();
     900             : 
     901           0 :     BOOST_FOREACH(EvpnRoute *route, mac_update_list_) {
     902             :         // Skip if the route is on the change list. We will get another
     903             :         // chance to process it after the MacAdvertisement listener sees
     904             :         // it and changes the EvpnMacState to point to the updated value
     905             :         // of EvpnSegment.
     906           0 :         if (route->is_onlist())
     907           0 :             continue;
     908             : 
     909             :         EvpnMacState *mac_state =
     910           0 :             dynamic_cast<EvpnMacState *>(route->GetState(table, listener_id));
     911           0 :         assert(mac_state);
     912           0 :         bool modified = mac_state->ProcessMacRouteAliasing();
     913           0 :         if (route->HasPaths()) {
     914           0 :             if (!mac_state->segment()) {
     915           0 :                 route->ClearState(table, listener_id);
     916           0 :                 delete mac_state;
     917             :             }
     918           0 :             if (modified) {
     919           0 :                 table_partition_->Notify(route);
     920             :             }
     921             :         } else {
     922           0 :             route->ClearState(table, listener_id);
     923           0 :             table_partition_->Delete(route);
     924           0 :             delete mac_state;
     925             :         }
     926             :     }
     927             : 
     928           0 :     mac_update_list_.clear();
     929           0 :     evpn_manager_->RetryDelete();
     930           0 :     return true;
     931             : }
     932             : 
     933           0 : bool EvpnManagerPartition::GetForestNodeAddress(ErmVpnRoute *rt,
     934             :                                                  Ip4Address *address) const {
     935           0 :     if (!evpn_manager_->ermvpn_table()->tree_manager())
     936           0 :         return false;
     937             :     uint32_t label;
     938           0 :     vector<string> te;
     939           0 :     return evpn_manager_->ermvpn_table()->tree_manager()->GetForestNodePMSI(
     940           0 :                                            rt, &label, address, &te);
     941           0 : }
     942             : 
     943           0 : EvpnStatePtr EvpnManagerPartition::GetState(const SG &sg) const {
     944           0 :     EvpnState::StatesMap::const_iterator iter = states_.find(sg);
     945           0 :     return iter != states_.end() ?  iter->second : NULL;
     946             : }
     947             : 
     948           0 : EvpnStatePtr EvpnManagerPartition::GetState(const SG &sg) {
     949           0 :     EvpnState::StatesMap::iterator iter = states_.find(sg);
     950           0 :     return iter != states_.end() ?  iter->second : NULL;
     951             : }
     952             : 
     953           0 : EvpnStatePtr EvpnManagerPartition::GetState(EvpnRoute *rt) {
     954           0 :     EvpnState::SG sg = EvpnState::SG(rt->GetPrefix().source(),
     955           0 :                                      rt->GetPrefix().group());
     956           0 :     return GetState(sg);
     957             : }
     958             : 
     959           0 : EvpnStatePtr EvpnManagerPartition::CreateState(const SG &sg) {
     960           0 :     EvpnStatePtr state(new EvpnState(sg, &states_, evpn_manager_));
     961           0 :     assert(states_.insert(make_pair(sg, state.get())).second);
     962           0 :     EVPN_TRACE(EvpnStateCreate, sg.source.to_string(), sg.group.to_string());
     963           0 :     return state;
     964           0 : }
     965             : 
     966           0 : EvpnStatePtr EvpnManagerPartition::LocateState(const SG &sg) {
     967           0 :     EvpnStatePtr evpn_state = GetState(sg);
     968           0 :     if (evpn_state)
     969           0 :         return evpn_state;
     970           0 :     evpn_state = CreateState(sg);
     971           0 :     assert(evpn_state);
     972           0 :     return evpn_state;
     973           0 : }
     974             : 
     975           0 : EvpnStatePtr EvpnManagerPartition::LocateState(EvpnRoute *rt) {
     976           0 :     EvpnState::SG sg = EvpnState::SG(rt->GetPrefix().source(),
     977           0 :                                      rt->GetPrefix().group());
     978           0 :     return LocateState(sg);
     979             : }
     980             : 
     981             : //
     982             : // Disable processing of the update list.
     983             : // For testing only.
     984             : //
     985           0 : void EvpnManagerPartition::DisableMacUpdateProcessing() {
     986           0 :     mac_update_trigger_->set_disable();
     987           0 : }
     988             : 
     989             : //
     990             : // Enable processing of the update list.
     991             : // For testing only.
     992             : //
     993           0 : void EvpnManagerPartition::EnableMacUpdateProcessing() {
     994           0 :     mac_update_trigger_->set_enable();
     995           0 : }
     996             : 
     997             : //
     998             : // Return true if the EvpnManagerPartition is empty i.e. it has no local
     999             : // or remote EvpnMcastNodes and no MAC routes that need to be updated.
    1000             : //
    1001         320 : bool EvpnManagerPartition::empty() const {
    1002         320 :     if (!local_mcast_node_list_.empty())
    1003           0 :         return false;
    1004         320 :     if (!remote_mcast_node_list_.empty())
    1005           0 :         return false;
    1006         320 :     if (!mac_update_list_.empty())
    1007           0 :         return false;
    1008         320 :     assert(leaf_node_list_.empty());
    1009         320 :     assert(replicator_node_list_.empty());
    1010         320 :     assert(regular_node_list_.empty());
    1011         320 :     assert(ir_client_node_list_.empty());
    1012         320 :     return true;
    1013             : }
    1014             : 
    1015             : //
    1016             : // Return the BgpServer for the EvpnManagerPartition.
    1017             : //
    1018           0 : BgpServer *EvpnManagerPartition::server() {
    1019           0 :     return evpn_manager_->server();
    1020             : }
    1021             : 
    1022             : //
    1023             : // Return the EvpnTable for the EvpnManagerPartition.
    1024             : //
    1025           0 : const EvpnTable *EvpnManagerPartition::table() const {
    1026           0 :     return evpn_manager_->table();
    1027             : }
    1028             : 
    1029             : //
    1030             : // Constructor for EvpnManager.
    1031             : //
    1032          80 : EvpnManager::EvpnManager(EvpnTable *table)
    1033          80 :     : table_(table),
    1034          80 :       listener_id_(DBTable::kInvalidId),
    1035         160 :       segment_delete_trigger_(new TaskTrigger(
    1036             :           boost::bind(&EvpnManager::ProcessSegmentDeleteSet, this),
    1037         160 :           TaskScheduler::GetInstance()->GetTaskId("bgp::EvpnSegment"), 0)),
    1038         160 :       segment_update_trigger_(new TaskTrigger(
    1039             :           boost::bind(&EvpnManager::ProcessSegmentUpdateSet, this),
    1040         160 :           TaskScheduler::GetInstance()->GetTaskId("bgp::EvpnSegment"), 0)),
    1041         160 :       table_delete_ref_(this, table->deleter()) {
    1042          80 :     deleter_.reset(new DeleteActor(this));
    1043          80 :     db_states_count_ = 0;
    1044          80 : }
    1045             : 
    1046             : //
    1047             : // Destructor for EvpnManager.
    1048             : //
    1049         160 : EvpnManager::~EvpnManager() {
    1050          80 :     assert(segment_map_.empty());
    1051          80 :     assert(segment_delete_set_.empty());
    1052          80 :     assert(segment_update_set_.empty());
    1053         160 : }
    1054             : 
    1055             : //
    1056             : // Initialize the EvpnManager. We allocate the EvpnManagerPartitions
    1057             : // and register a DBListener for the EvpnTable.
    1058             : //
    1059          80 : void EvpnManager::Initialize() {
    1060          80 :     AllocPartitions();
    1061          80 :     listener_id_ = table_->Register(
    1062             :         boost::bind(&EvpnManager::RouteListener, this, _1, _2),
    1063             :         "EvpnManager");
    1064          80 :     ermvpn_table_ = dynamic_cast<ErmVpnTable *>(
    1065          80 :         table_->routing_instance()->GetTable(Address::ERMVPN));
    1066          80 :     if (ermvpn_table_)
    1067          80 :         ermvpn_listener_id_ = ermvpn_table_->Register(
    1068             :             boost::bind(&EvpnManager::ErmVpnRouteListener, this, _1, _2),
    1069             :             "EvpnManager");
    1070          80 : }
    1071             : 
    1072             : //
    1073             : // Terminate the EvpnManager. We free the EvpnManagerPartitions
    1074             : // and unregister from the EvpnTable.
    1075             : //
    1076          80 : void EvpnManager::Terminate() {
    1077          80 :     CHECK_CONCURRENCY("bgp::Config");
    1078          80 :     table_->Unregister(listener_id_);
    1079          80 :     listener_id_ = DBTable::kInvalidId;
    1080          80 :     if (ermvpn_listener_id_ != DBTable::kInvalidId) {
    1081          80 :         ermvpn_table_->Unregister(ermvpn_listener_id_);
    1082          80 :         ermvpn_listener_id_ = DBTable::kInvalidId;
    1083             :     }
    1084          80 :     FreePartitions();
    1085          80 : }
    1086             : 
    1087             : //
    1088             : // Allocate the EvpnManagerPartitions.
    1089             : //
    1090          80 : void EvpnManager::AllocPartitions() {
    1091         400 :     for (int part_id = 0; part_id < DB::PartitionCount(); part_id++) {
    1092         320 :         partitions_.push_back(new EvpnManagerPartition(this, part_id));
    1093             :     }
    1094          80 : }
    1095             : 
    1096             : //
    1097             : // Free the EvpnManagerPartitions.
    1098             : //
    1099          80 : void EvpnManager::FreePartitions() {
    1100          80 :     STLDeleteValues(&partitions_);
    1101          80 : }
    1102             : 
    1103             : //
    1104             : // Disable processing of the update lists in all partitions.
    1105             : // For testing only.
    1106             : //
    1107           0 : void EvpnManager::DisableMacUpdateProcessing() {
    1108           0 :     for (int part_id = 0; part_id < DB::PartitionCount(); part_id++) {
    1109           0 :         partitions_[part_id]->DisableMacUpdateProcessing();
    1110             :     }
    1111           0 : }
    1112             : 
    1113             : //
    1114             : // Enable processing of the update lists in all partitions.
    1115             : // For testing only.
    1116             : //
    1117           0 : void EvpnManager::EnableMacUpdateProcessing() {
    1118           0 :     for (int part_id = 0; part_id < DB::PartitionCount(); part_id++) {
    1119           0 :         partitions_[part_id]->EnableMacUpdateProcessing();
    1120             :     }
    1121           0 : }
    1122             : 
    1123             : //
    1124             : // Get the EvpnManagerPartition for the given partition id.
    1125             : //
    1126         640 : EvpnManagerPartition *EvpnManager::GetPartition(size_t part_id) {
    1127         640 :     return partitions_[part_id];
    1128             : }
    1129             : 
    1130             : //
    1131             : // Get the DBTablePartition for the EvpnTable for given partition id.
    1132             : //
    1133         320 : DBTablePartition *EvpnManager::GetTablePartition(size_t part_id) {
    1134         320 :     return static_cast<DBTablePartition *>(table_->GetTablePartition(part_id));
    1135             : }
    1136             : 
    1137             : //
    1138             : // Construct export state for the given EvpnRoute. Note that the route
    1139             : // only needs to be exported to the IPeer from which it was learnt.
    1140             : //
    1141           0 : UpdateInfo *EvpnManager::GetUpdateInfo(EvpnRoute *route) {
    1142           0 :     CHECK_CONCURRENCY("db::DBTable");
    1143             : 
    1144           0 :     DBState *dbstate = route->GetState(table_, listener_id_);
    1145             :     EvpnLocalMcastNode *local_node =
    1146           0 :         dynamic_cast<EvpnLocalMcastNode *>(dbstate);
    1147             : 
    1148           0 :     if (!local_node)
    1149           0 :         return NULL;
    1150             : 
    1151           0 :     return local_node->GetUpdateInfo(route);
    1152             : }
    1153             : 
    1154          80 : BgpServer *EvpnManager::server() {
    1155          80 :     return table_->server();
    1156             : }
    1157             : 
    1158             : //
    1159             : // Find or create the EvpnSegment for the given EthernetSegmentId.
    1160             : //
    1161           0 : EvpnSegment *EvpnManager::LocateSegment(const EthernetSegmentId &esi) {
    1162           0 :     assert(!esi.IsZero());
    1163           0 :     tbb::spin_rw_mutex::scoped_lock write_lock(segment_rw_mutex_, true);
    1164           0 :     SegmentMap::iterator loc = segment_map_.find(esi);
    1165           0 :     EvpnSegment *segment = (loc != segment_map_.end()) ? loc->second : NULL;
    1166           0 :     if (!segment) {
    1167           0 :         segment = new EvpnSegment(this, esi);
    1168           0 :         segment_map_.insert(esi, segment);
    1169             :     }
    1170           0 :     return segment;
    1171           0 : }
    1172             : 
    1173             : //
    1174             : // Find the EvpnSegment for the given EthernetSegmentId.
    1175             : //
    1176           0 : EvpnSegment *EvpnManager::FindSegment(const EthernetSegmentId &esi) {
    1177           0 :     assert(!esi.IsZero());
    1178           0 :     tbb::spin_rw_mutex::scoped_lock read_lock(segment_rw_mutex_, false);
    1179           0 :     SegmentMap::iterator loc = segment_map_.find(esi);
    1180           0 :     return (loc != segment_map_.end()) ? loc->second : NULL;
    1181           0 : }
    1182             : 
    1183             : //
    1184             : // Trigger deletion of the given EvpnSegment.
    1185             : // The EvpnSegment is added to a set of EvpnSegments that can potentially
    1186             : // be deleted. This method can be invoked from multiple db::DBTable tasks
    1187             : // in parallel when a MAC routes are removed from the dependency list in an
    1188             : // EvpnSegment. Hence we ensure exclusive access using a write lock.
    1189             : //
    1190             : // The list is processed from the context of bgp::EvpnSegment task which is
    1191             : // mutually exclusive with db::DBTable task.
    1192             : //
    1193           0 : void EvpnManager::TriggerSegmentDelete(EvpnSegment *segment) {
    1194           0 :     CHECK_CONCURRENCY("db::DBTable", "bgp::EvpnSegment");
    1195             : 
    1196           0 :     tbb::spin_rw_mutex::scoped_lock write_lock(segment_rw_mutex_, true);
    1197           0 :     segment_delete_set_.insert(segment);
    1198           0 :     segment_delete_trigger_->Set();
    1199           0 : }
    1200             : 
    1201             : //
    1202             : // Process the set of EvpnSegments that can potentially be deleted.
    1203             : // Remove the EvpnSegment from the map and destroy if it's fine to
    1204             : // to delete the EvpnSegment.
    1205             : //
    1206           0 : bool EvpnManager::ProcessSegmentDeleteSet() {
    1207           0 :     CHECK_CONCURRENCY("bgp::EvpnSegment");
    1208             : 
    1209           0 :     BOOST_FOREACH(EvpnSegment *segment, segment_delete_set_) {
    1210           0 :         if (segment->MayDelete()) {
    1211           0 :             segment_update_set_.erase(segment);
    1212           0 :             EthernetSegmentId esi = segment->esi();
    1213           0 :             segment_map_.erase(esi);
    1214             :         }
    1215             :     }
    1216           0 :     segment_delete_set_.clear();
    1217           0 :     RetryDelete();
    1218           0 :     return true;
    1219             : }
    1220             : 
    1221             : //
    1222             : // Disable processing of the delete list.
    1223             : // For testing only.
    1224             : //
    1225           0 : void EvpnManager::DisableSegmentDeleteProcessing() {
    1226           0 :     segment_delete_trigger_->set_disable();
    1227           0 : }
    1228             : 
    1229             : //
    1230             : // Enable processing of the delete list.
    1231             : // For testing only.
    1232             : //
    1233           0 : void EvpnManager::EnableSegmentDeleteProcessing() {
    1234           0 :     segment_delete_trigger_->set_enable();
    1235           0 : }
    1236             : 
    1237             : //
    1238             : // Trigger update of the given EvpnSegment.
    1239             : // The EvpnSegment is added to a set of EvpnSegments for which updates
    1240             : // need triggered. This method is called in the context of db::DBTable
    1241             : // task and a task instance of 0 since all AutoDisocvery routes always
    1242             : // get sharded to partition 0.
    1243             : //
    1244             : // The set is processed in the context of bgp::EvpnSegment task, which
    1245             : // is mutually exclusive with db::DBTable task.
    1246             : //
    1247           0 : void EvpnManager::TriggerSegmentUpdate(EvpnSegment *segment) {
    1248           0 :     CHECK_CONCURRENCY("db::DBTable");
    1249             : 
    1250           0 :     segment_update_set_.insert(segment);
    1251           0 :     segment_update_trigger_->Set();
    1252           0 : }
    1253             : 
    1254             : //
    1255             : // Process the set of EvpnSegments that need to be updated.
    1256             : //
    1257             : // Go through each EvpnSegment and update it's PE list. Trigger updates
    1258             : // of all it's dependent MAC routes if there's a change in the PE list.
    1259             : //
    1260           0 : bool EvpnManager::ProcessSegmentUpdateSet() {
    1261           0 :     CHECK_CONCURRENCY("bgp::EvpnSegment");
    1262             : 
    1263           0 :     BOOST_FOREACH(EvpnSegment *segment, segment_update_set_) {
    1264           0 :         EvpnRoute *esi_ad_route = segment->esi_ad_route();
    1265           0 :         bool changed = segment->UpdatePeList();
    1266           0 :         if (changed)
    1267           0 :             segment->TriggerMacRouteUpdate();
    1268           0 :         if (!esi_ad_route->IsValid()) {
    1269           0 :             segment->clear_esi_ad_route();
    1270           0 :             esi_ad_route->ClearState(table_, listener_id_);
    1271           0 :             TriggerSegmentDelete(segment);
    1272             :         }
    1273             :     }
    1274           0 :     segment_update_set_.clear();
    1275           0 :     RetryDelete();
    1276           0 :     return true;
    1277             : }
    1278             : 
    1279             : //
    1280             : // Disable processing of the update list.
    1281             : // For testing only.
    1282             : //
    1283           0 : void EvpnManager::DisableSegmentUpdateProcessing() {
    1284           0 :     segment_update_trigger_->set_disable();
    1285           0 : }
    1286             : 
    1287             : //
    1288             : // Enable processing of the update list.
    1289             : // For testing only.
    1290             : //
    1291           0 : void EvpnManager::EnableSegmentUpdateProcessing() {
    1292           0 :     segment_update_trigger_->set_enable();
    1293           0 : }
    1294             : 
    1295             : //
    1296             : // DBListener callback handler for AutoDisocvery routes in the EvpnTable.
    1297             : //
    1298           0 : void EvpnManager::AutoDiscoveryRouteListener(EvpnRoute *route) {
    1299           0 :     CHECK_CONCURRENCY("db::DBTable");
    1300             : 
    1301           0 :     DBState *dbstate = route->GetState(table_, listener_id_);
    1302           0 :     if (!dbstate) {
    1303             :         // We have no previous DBState for this route.
    1304             :         // Bail if the route is not valid.
    1305           0 :         if (!route->IsValid())
    1306           0 :             return;
    1307             : 
    1308             :         // Locate the EvpnSegment and associate it with the route.
    1309             :         // Trigger an update of EvpnSegment so that it's PE list
    1310             :         // gets updated.
    1311           0 :         EvpnSegment *segment = LocateSegment(route->GetPrefix().esi());
    1312           0 :         segment->set_esi_ad_route(route);
    1313           0 :         route->SetState(table_, listener_id_, segment);
    1314           0 :         TriggerSegmentUpdate(segment);
    1315             :     } else {
    1316             :         // Trigger an update of EvpnSegment so that it's PE list
    1317             :         // gets updated.
    1318           0 :         EvpnSegment *segment = dynamic_cast<EvpnSegment *>(dbstate);
    1319           0 :         assert(segment);
    1320           0 :         TriggerSegmentUpdate(segment);
    1321             :     }
    1322             : }
    1323             : 
    1324             : //
    1325             : // DBListener callback handler for MacAdvertisement routes in the EvpnTable.
    1326             : //
    1327         640 : void EvpnManager::MacAdvertisementRouteListener(
    1328             :     EvpnManagerPartition *partition, EvpnRoute *route) {
    1329         640 :     CHECK_CONCURRENCY("db::DBTable");
    1330             : 
    1331         640 :     DBState *dbstate = route->GetState(table_, listener_id_);
    1332         640 :     if (!dbstate) {
    1333             :         // We have no previous DBState for this route.
    1334             :         // Bail if the route is not valid or if it doesn't have an ESI.
    1335         640 :         if (!route->IsValid())
    1336         256 :             return;
    1337         384 :         const BgpPath *path = route->BestPath();
    1338         384 :         if (path->GetAttr()->esi().IsZero())
    1339         384 :             return;
    1340             : 
    1341             :         // Create a new EvpnMacState and associate it with the route.
    1342           0 :         EvpnMacState *mac_state = new EvpnMacState(this, route);
    1343           0 :         route->SetState(table_, listener_id_, mac_state);
    1344             : 
    1345             :         // Locate the EvpnSegment and add the MAC route as a dependent
    1346             :         // of the EvpnSegment.
    1347           0 :         EvpnSegment *segment = LocateSegment(path->GetAttr()->esi());
    1348           0 :         mac_state->set_segment(segment);
    1349           0 :         segment->AddMacRoute(partition->part_id(), route);
    1350           0 :         partition->TriggerMacRouteUpdate(route);
    1351             :     } else {
    1352           0 :         EvpnMacState *mac_state = dynamic_cast<EvpnMacState *>(dbstate);
    1353           0 :         assert(mac_state);
    1354           0 :         EvpnSegment *segment = mac_state->segment();
    1355             : 
    1356             :         // Handle change in the ESI and update the dependency on the
    1357             :         // EvpnSegment as appropriate.
    1358             :         // Note that the DBState on the EvpnRoute doesn't get cleared
    1359             :         // here. That only happens when aliasing for the route is being
    1360             :         // processed.
    1361           0 :         const BgpPath *path = route->BestPath();
    1362           0 :         if (!route->IsValid() || (path && path->GetAttr()->esi().IsZero())) {
    1363           0 :             if (segment) {
    1364           0 :                 segment->DeleteMacRoute(partition->part_id(), route);
    1365           0 :                 mac_state->clear_segment();
    1366             :             }
    1367           0 :         } else if (!segment && !path->GetAttr()->esi().IsZero()) {
    1368           0 :             segment = LocateSegment(path->GetAttr()->esi());
    1369           0 :             mac_state->set_segment(segment);
    1370           0 :             segment->AddMacRoute(partition->part_id(), route);
    1371           0 :         } else if (segment && segment->esi() != path->GetAttr()->esi()) {
    1372           0 :             segment->DeleteMacRoute(partition->part_id(), route);
    1373           0 :             mac_state->clear_segment();
    1374           0 :             segment = LocateSegment(path->GetAttr()->esi());
    1375           0 :             mac_state->set_segment(segment);
    1376           0 :             segment->AddMacRoute(partition->part_id(), route);
    1377             :         }
    1378           0 :         partition->TriggerMacRouteUpdate(route);
    1379             :     }
    1380             : }
    1381             : 
    1382             : //
    1383             : // DBListener callback handler for InclusiveMulticast routes in the EvpnTable.
    1384             : //
    1385           0 : void EvpnManager::InclusiveMulticastRouteListener(
    1386             :     EvpnManagerPartition *partition, EvpnRoute *route) {
    1387           0 :     CHECK_CONCURRENCY("db::DBTable");
    1388             : 
    1389           0 :     DBState *dbstate = route->GetState(table_, listener_id_);
    1390           0 :     if (!dbstate) {
    1391             :         // We have no previous DBState for this route.
    1392             :         // Bail if the route is not valid.
    1393           0 :         if (!route->IsValid())
    1394           0 :             return;
    1395             : 
    1396             :         // Create a new EvpnMcastNode and associate it with the route.
    1397             :         EvpnMcastNode *node;
    1398           0 :         if (route->GetPrefix().type() == EvpnPrefix::MacAdvertisementRoute) {
    1399           0 :             node = new EvpnLocalMcastNode(partition, route);
    1400             :         } else {
    1401           0 :             node = new EvpnRemoteMcastNode(partition, route);
    1402             :         }
    1403           0 :         partition->AddMcastNode(node, route);
    1404           0 :         route->SetState(table_, listener_id_, node);
    1405             :     } else {
    1406           0 :         EvpnMcastNode *node = dynamic_cast<EvpnMcastNode *>(dbstate);
    1407           0 :         assert(node);
    1408             : 
    1409           0 :         if (!route->IsValid()) {
    1410             :             // Delete the EvpnMcastNode associated with the route.
    1411           0 :             route->ClearState(table_, listener_id_);
    1412           0 :             partition->DeleteMcastNode(node, route);
    1413           0 :             delete node;
    1414           0 :         } else if (node->UpdateAttributes(route)) {
    1415             :             // Update the EvpnMcastNode associated with the route.
    1416           0 :             partition->UpdateMcastNode(node, route);
    1417             :         }
    1418             :     }
    1419             : }
    1420             : 
    1421             : //
    1422             : // DBListener callback handler for SelectiveMulticast routes in the EvpnTable.
    1423             : //
    1424           0 : void EvpnManager::SelectiveMulticastRouteListener(
    1425             :     EvpnManagerPartition *partition, EvpnRoute *route) {
    1426             : 
    1427           0 :     CHECK_CONCURRENCY("db::DBTable");
    1428           0 :     EvpnMcastNode *dbstate = dynamic_cast<EvpnMcastNode *>(
    1429           0 :             route->GetState(table_, listener_id_));
    1430           0 :     bool is_usable = route->IsUsable();
    1431           0 :     bool is_deleted = route->IsDeleted();
    1432           0 :     bool checkErmvpnRoute = false;
    1433           0 :     if (route->BestPath()) {
    1434           0 :         checkErmvpnRoute = route->BestPath()->GetFlags() &
    1435             :             BgpPath::CheckGlobalErmVpnRoute;
    1436             :     }
    1437           0 :     if ((!is_usable && !checkErmvpnRoute) || is_deleted) {
    1438           0 :         if (!dbstate)
    1439           0 :             return;
    1440             : 
    1441           0 :         EvpnStatePtr evpn_state = partition->GetState(route);
    1442           0 :         if (evpn_state)
    1443           0 :             evpn_state->smet_routes().erase(route);
    1444           0 :         EVPN_RT_LOG(route, "Processed Smet route deletion");
    1445           0 :         ClearDBState(route);
    1446           0 :         partition->NotifyForestNode(route, ermvpn_table());
    1447           0 :         partition->DeleteMcastNode(dbstate, route);
    1448           0 :         delete dbstate;
    1449           0 :         return;
    1450           0 :     }
    1451             : 
    1452           0 :     EvpnStatePtr evpn_state = partition->LocateState(route);
    1453           0 :     assert(evpn_state);
    1454           0 :     evpn_state->smet_routes().insert(route);
    1455           0 :     BgpPath *path = const_cast<BgpPath *>(route->BestPath());
    1456           0 :     if (path && path->CheckErmVpn()) {
    1457           0 :         ErmVpnRoute *global_rt = evpn_state->global_ermvpn_tree_rt();
    1458           0 :         Ip4Address address;
    1459           0 :         bool nh_found = partition->GetForestNodeAddress(global_rt, &address);
    1460           0 :         if (nh_found) {
    1461           0 :             BgpAttrPtr attr = path->GetAttr();
    1462             :             BgpAttrPtr new_attr = partition->server()->attr_db()->
    1463           0 :                                   ReplaceNexthopAndLocate(attr.get(), address);
    1464           0 :             path->SetAttr(new_attr, attr);
    1465           0 :             path->ResetCheckErmVpn();
    1466           0 :             partition->NotifyForestNode(route, ermvpn_table());
    1467           0 :         }
    1468             :     }
    1469             : 
    1470           0 :     if (!dbstate) {
    1471             :         // Create a new DBState and associate it with the route.
    1472             :         EvpnMcastNode *node;
    1473           0 :         if (route->GetPrefix().ip_address() ==
    1474           0 :                 Ip4Address(table_->server()->bgp_identifier())) {
    1475           0 :             node = new EvpnLocalMcastNode(partition, route, evpn_state);
    1476             :         } else {
    1477           0 :             node = new EvpnRemoteMcastNode(partition, route, evpn_state);
    1478             :         }
    1479           0 :         SetDBState(route, node);
    1480           0 :         partition->AddMcastNode(node, route);
    1481           0 :         EVPN_RT_LOG(route, "Processed Smet route creation");
    1482             :     }
    1483           0 : }
    1484             : 
    1485           0 : void EvpnManagerPartition::NotifyForestNode(EvpnRoute *route,
    1486             :                                             ErmVpnTable *table) {
    1487           0 :     const Ip4Address &source = route->GetPrefix().source().to_v4();
    1488           0 :     const Ip4Address &group = route->GetPrefix().group().to_v4();
    1489           0 :     if (table->tree_manager())
    1490           0 :         table->tree_manager()->NotifyForestNode(part_id_, source, group);
    1491           0 : }
    1492             : 
    1493             : // Set DB State and update count.
    1494           0 : void EvpnManager::SetDBState(EvpnRoute *route, EvpnMcastNode *dbstate) {
    1495           0 :     route->SetState(table_, listener_id_, dbstate);
    1496           0 :     db_states_count_++;
    1497           0 : }
    1498             : 
    1499             : // Create DB State and update count. If there is no DB State associated in the
    1500             : // table, resume table deletion if the deletion was pending.
    1501           0 : void EvpnManager::ClearDBState(EvpnRoute *route) {
    1502           0 :     route->ClearState(table_, listener_id_);
    1503           0 :     assert(db_states_count_);
    1504           0 :     db_states_count_--;
    1505             : 
    1506             :     // Retry deletion now as there is no more attached db state in the table.
    1507           0 :     if (!db_states_count_ && deleter_->IsDeleted())
    1508           0 :         deleter_->RetryDelete();
    1509           0 : }
    1510             : 
    1511             : // Check whether an ErmVpnRoute is locally originated GlobalTreeRoute.
    1512           0 : bool EvpnManager::IsUsableGlobalTreeRootRoute(
    1513             :         ErmVpnRoute *ermvpn_route) const {
    1514           0 :     if (!ermvpn_route || !ermvpn_route->IsUsable())
    1515           0 :         return false;
    1516           0 :     if (!ermvpn_table()->tree_manager())
    1517           0 :         return false;
    1518           0 :     if (ermvpn_table()->tree_manager()->begin() ==
    1519           0 :             ermvpn_table()->tree_manager()->end())
    1520           0 :         return false;
    1521           0 :     ErmVpnRoute *global_rt = ermvpn_table()->tree_manager()->
    1522           0 :                                GetGlobalTreeRootRoute(
    1523           0 :                                ermvpn_route->GetPrefix().source(),
    1524           0 :                                ermvpn_route->GetPrefix().group());
    1525           0 :     return (global_rt && global_rt == ermvpn_route);
    1526             : }
    1527             : 
    1528             : // ErmVpnTable route listener callback function.
    1529             : //
    1530             : // Process changes (create/update/delete) to GlobalErmVpnRoute in vrf.ermvpn.0
    1531           0 : void EvpnManager::ErmVpnRouteListener(DBTablePartBase *tpart,
    1532             :                                       DBEntryBase *db_entry) {
    1533           0 :     CHECK_CONCURRENCY("db::DBTable");
    1534             : 
    1535           0 :     ErmVpnRoute *ermvpn_route = dynamic_cast<ErmVpnRoute *>(db_entry);
    1536           0 :     assert(ermvpn_route);
    1537             : 
    1538             :     // We only care about global tree routes for evpn stitching.
    1539           0 :     if (ermvpn_route->GetPrefix().type() != ErmVpnPrefix::GlobalTreeRoute) {
    1540           0 :         return;
    1541             :     }
    1542             : 
    1543           0 :     EvpnMcastNode *dbstate = dynamic_cast<EvpnMcastNode *>(
    1544           0 :         ermvpn_route->GetState(ermvpn_table(), ermvpn_listener_id()));
    1545             : 
    1546             :     // Handle GlobalTreeRoute route deletion.
    1547           0 :     if (!IsUsableGlobalTreeRootRoute(ermvpn_route)) {
    1548             :         // Ignore if there is no DB State associated with route.
    1549           0 :         if (!dbstate)
    1550           0 :             return;
    1551           0 :         EvpnStatePtr evpn_state = dbstate->state();
    1552           0 :         evpn_state->set_global_ermvpn_tree_rt(NULL);
    1553             : 
    1554             :         // Notify all received smet routes for PMSI re-computation.
    1555             :         // Since usable global ermvpn is no longer available, any advertised
    1556             :         // smet routes must now be withdrawn.
    1557           0 :         BOOST_FOREACH(EvpnRoute *route, evpn_state->smet_routes()) {
    1558           0 :             BgpPath *path = const_cast<BgpPath *>(route->BestPath());
    1559           0 :             if (path && (path->GetPathId() == 0)) {
    1560           0 :                 path->SetCheckErmVpn();
    1561             :             }
    1562           0 :             route->Notify();
    1563             :         }
    1564           0 :         ermvpn_route->ClearState(ermvpn_table(), ermvpn_listener_id());
    1565           0 :         EVPN_ERMVPN_RT_LOG(ermvpn_route,
    1566             :                            "Processed EVPN GlobalErmVpnRoute deletion");
    1567           0 :         delete dbstate;
    1568           0 :         return;
    1569           0 :     }
    1570             : 
    1571             :     // Set DB State in the route if not already done so before.
    1572           0 :     EvpnManagerPartition *partition = GetPartition(tpart->index());
    1573           0 :     EvpnStatePtr evpn_state;
    1574           0 :     if (!dbstate) {
    1575           0 :         EvpnState::SG sg(ermvpn_route);
    1576           0 :         evpn_state = partition->LocateState(sg);
    1577           0 :         dbstate = new EvpnLocalMcastNode(partition, NULL, evpn_state);
    1578           0 :         ermvpn_route->SetState(ermvpn_table(), ermvpn_listener_id(), dbstate);
    1579             :     } else {
    1580           0 :         evpn_state = dbstate->state();
    1581             :     }
    1582             : 
    1583             :     // Note down current usable ermvpn route for stitching to evpn.
    1584           0 :     dbstate->state()->set_global_ermvpn_tree_rt(ermvpn_route);
    1585             : 
    1586             :     // Notify all originated Type6 routes.
    1587           0 :     BOOST_FOREACH(EvpnRoute *route, evpn_state->smet_routes()) {
    1588           0 :         route->Notify();
    1589             :     }
    1590           0 :     EVPN_ERMVPN_RT_LOG(ermvpn_route,
    1591             :                        "Processed EVPN GlobalErmVpnRoute creation");
    1592           0 : }
    1593             : 
    1594             : //
    1595             : // DBListener callback handler for the EvpnTable.
    1596             : //
    1597         640 : void EvpnManager::RouteListener(DBTablePartBase *tpart, DBEntryBase *db_entry) {
    1598         640 :     CHECK_CONCURRENCY("db::DBTable");
    1599             : 
    1600         640 :     EvpnManagerPartition *partition = GetPartition(tpart->index());
    1601         640 :     EvpnRoute *route = dynamic_cast<EvpnRoute *>(db_entry);
    1602         640 :     assert(route);
    1603             : 
    1604         640 :     switch (route->GetPrefix().type()) {
    1605           0 :     case EvpnPrefix::AutoDiscoveryRoute:
    1606           0 :         AutoDiscoveryRouteListener(route);
    1607           0 :         break;
    1608         640 :     case EvpnPrefix::MacAdvertisementRoute:
    1609         640 :         if (route->GetPrefix().mac_addr().IsBroadcast()) {
    1610           0 :             InclusiveMulticastRouteListener(partition, route);
    1611             :         } else {
    1612         640 :             MacAdvertisementRouteListener(partition, route);
    1613             :         }
    1614         640 :         break;
    1615           0 :     case EvpnPrefix::InclusiveMulticastRoute:
    1616           0 :         InclusiveMulticastRouteListener(partition, route);
    1617           0 :         break;
    1618           0 :     case EvpnPrefix::SelectiveMulticastRoute:
    1619           0 :         SelectiveMulticastRouteListener(partition, route);
    1620           0 :         break;
    1621           0 :     default:
    1622           0 :         break;
    1623             :     }
    1624         640 : }
    1625             : 
    1626             : //
    1627             : // Fill information for introspect command.
    1628             : // Note that all IM routes are always in partition 0.
    1629             : //
    1630           0 : void EvpnManager::FillShowInfo(ShowEvpnTable *sevt) const {
    1631           0 :     CHECK_CONCURRENCY("db::DBTable");
    1632             : 
    1633           0 :     vector<string> regular_nves;
    1634           0 :     vector<string> ar_replicators;
    1635           0 :     vector<ShowEvpnMcastLeaf> ar_leafs;
    1636             :     EvpnManagerPartition::EvpnMcastNodeList::const_iterator it =
    1637           0 :                       partitions_[0]->remote_mcast_node_list()->begin();
    1638           0 :     for (; it != partitions_[0]->remote_mcast_node_list()->end(); it++) {
    1639           0 :         BOOST_FOREACH(const EvpnMcastNode *node, it->second) {
    1640           0 :             if (node->assisted_replication_leaf()) {
    1641           0 :                 ShowEvpnMcastLeaf leaf;
    1642           0 :                 leaf.set_address(node->address().to_string());
    1643           0 :                 leaf.set_replicator(node->replicator_address().to_string());
    1644           0 :                 ar_leafs.push_back(leaf);
    1645           0 :             } else if (node->assisted_replication_supported()) {
    1646           0 :                 ar_replicators.push_back(node->address().to_string());
    1647           0 :             } else if (node->edge_replication_not_supported()) {
    1648           0 :                 regular_nves.push_back(node->address().to_string());
    1649             :             }
    1650             :         }
    1651             :     }
    1652             : 
    1653           0 :     sort(regular_nves.begin(), regular_nves.end());
    1654           0 :     sort(ar_replicators.begin(), ar_replicators.end());
    1655           0 :     sort(ar_leafs.begin(), ar_leafs.end());
    1656           0 :     sevt->set_regular_nves(regular_nves);
    1657           0 :     sevt->set_ar_replicators(ar_replicators);
    1658           0 :     sevt->set_ar_leafs(ar_leafs);
    1659           0 : }
    1660             : 
    1661             : //
    1662             : // Check if the EvpnManager can be deleted. This can happen only if all the
    1663             : // EvpnManagerPartitions are empty.
    1664             : //
    1665          80 : bool EvpnManager::MayDelete() const {
    1666          80 :     CHECK_CONCURRENCY("bgp::Config");
    1667             : 
    1668          80 :     if (!segment_map_.empty())
    1669           0 :         return false;
    1670             : 
    1671          80 :     if (!segment_update_set_.empty())
    1672           0 :         return false;
    1673          80 :     if (segment_update_trigger_->IsSet())
    1674           0 :         return false;
    1675             : 
    1676          80 :     if (!segment_delete_set_.empty())
    1677           0 :         return false;
    1678          80 :     if (segment_delete_trigger_->IsSet())
    1679           0 :         return false;
    1680             : 
    1681         720 :     BOOST_FOREACH(const EvpnManagerPartition *partition, partitions_) {
    1682         320 :         if (!partition->empty())
    1683           0 :             return false;
    1684         320 :         if (!partition->states().empty())
    1685           0 :             return false;
    1686             :     }
    1687          80 :     if (db_states_count_)
    1688           0 :         return false;
    1689          80 :     return true;
    1690             : }
    1691             : 
    1692             : //
    1693             : // Initiate shutdown for the EvpnManager.
    1694             : //
    1695          80 : void EvpnManager::Shutdown() {
    1696          80 :     CHECK_CONCURRENCY("bgp::Config");
    1697          80 : }
    1698             : 
    1699             : //
    1700             : // Trigger deletion of the EvpnManager and propagate the delete to any
    1701             : // dependents.
    1702             : //
    1703          80 : void EvpnManager::ManagedDelete() {
    1704          80 :     deleter_->Delete();
    1705          80 : }
    1706             : 
    1707             : //
    1708             : // Attempt to enqueue a delete for the EvpnManager.
    1709             : //
    1710           0 : void EvpnManager::RetryDelete() {
    1711           0 :     if (!deleter()->IsDeleted())
    1712           0 :         return;
    1713           0 :     deleter()->RetryDelete();
    1714             : }
    1715             : 
    1716             : //
    1717             : // Return the LifetimeActor for the EvpnManager.
    1718             : //
    1719           0 : LifetimeActor *EvpnManager::deleter() {
    1720           0 :     return deleter_.get();
    1721             : }
    1722             : 
    1723           0 : EvpnState::SG::SG(const Ip4Address &source, const Ip4Address &group) :
    1724           0 :     source(IpAddress(source)), group(IpAddress(group)) {
    1725           0 : }
    1726             : 
    1727           0 : EvpnState::SG::SG(const ErmVpnRoute *route) :
    1728           0 :         source(route->GetPrefix().source()),
    1729           0 :         group(route->GetPrefix().group()) {
    1730           0 : }
    1731             : 
    1732           0 : EvpnState::SG::SG(const EvpnRoute *route) :
    1733           0 :         source(route->GetPrefix().source()), group(route->GetPrefix().group()) {
    1734           0 : }
    1735             : 
    1736           0 : EvpnState::SG::SG(const IpAddress &source, const IpAddress &group) :
    1737           0 :     source(source), group(group) {
    1738           0 : }
    1739             : 
    1740           0 : bool EvpnState::SG::operator<(const SG &other) const {
    1741           0 :     if (source < other.source)
    1742           0 :         return true;
    1743           0 :     if (source > other.source)
    1744           0 :         return false;
    1745           0 :     if (group < other.group)
    1746           0 :         return true;
    1747           0 :     if (group > other.group)
    1748           0 :         return false;
    1749           0 :     return false;
    1750             : }
    1751             : 
    1752           0 : const EvpnState::SG &EvpnState::sg() const {
    1753           0 :     return sg_;
    1754             : }
    1755             : 
    1756           0 : void EvpnState::set_global_ermvpn_tree_rt(ErmVpnRoute *global_ermvpn_tree_rt) {
    1757           0 :     global_ermvpn_tree_rt_ = global_ermvpn_tree_rt;
    1758           0 : }
    1759             : 
    1760           0 : ErmVpnRoute *EvpnState::global_ermvpn_tree_rt() {
    1761           0 :     return global_ermvpn_tree_rt_;
    1762             : }
    1763             : 
    1764           0 : const ErmVpnRoute *EvpnState::global_ermvpn_tree_rt() const {
    1765           0 :     return global_ermvpn_tree_rt_;
    1766             : }

Generated by: LCOV version 1.14