LCOV - code coverage report
Current view: top level - bgp/mvpn - mvpn_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 20 403 5.0 %
Date: 2026-11-16 02:06:09 Functions: 6 42 14.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/mvpn/mvpn_table.h"
       7             : 
       8             : #include <utility>
       9             : #include <boost/foreach.hpp>
      10             : 
      11             : #include "base/task_annotations.h"
      12             : #include "bgp/ermvpn/ermvpn_table.h"
      13             : #include "bgp/extended-community/source_as.h"
      14             : #include "bgp/ipeer.h"
      15             : #include "bgp/bgp_factory.h"
      16             : #include "bgp/bgp_log.h"
      17             : #include "bgp/bgp_multicast.h"
      18             : #include "bgp/bgp_mvpn.h"
      19             : #include "bgp/bgp_server.h"
      20             : #include "bgp/bgp_update.h"
      21             : #include "bgp/inet/inet_table.h"
      22             : #include "bgp/origin-vn/origin_vn.h"
      23             : #include "bgp/routing-instance/path_resolver.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::unique_ptr;
      29             : using std::pair;
      30             : using std::string;
      31             : using std::set;
      32             : 
      33           0 : size_t MvpnTable::HashFunction(const MvpnPrefix &prefix) const {
      34           0 :     if ((prefix.type() == MvpnPrefix::IntraASPMSIADRoute) ||
      35           0 :            (prefix.type() == MvpnPrefix::LeafADRoute)) {
      36           0 :         uint32_t data = prefix.originator().to_ulong();
      37           0 :         return boost::hash_value(data);
      38             :     }
      39           0 :     if (prefix.type() == MvpnPrefix::InterASPMSIADRoute) {
      40           0 :         uint32_t data = prefix.asn();
      41           0 :         return boost::hash_value(data);
      42             :     }
      43           0 :     return boost::hash_value(prefix.group().to_ulong());
      44             : }
      45             : 
      46         100 : MvpnTable::MvpnTable(DB *db, const string &name)
      47         100 :     : BgpTable(db, name), manager_(NULL) {
      48         100 : }
      49             : 
      50          80 : PathResolver *MvpnTable::CreatePathResolver() {
      51          80 :     if (routing_instance()->IsMasterRoutingInstance())
      52           0 :         return NULL;
      53          80 :     PathResolver * path_resolver = new PathResolver(this);
      54          80 :     path_resolver->set_nexthop_longest_match(true);
      55          80 :     return path_resolver;
      56             : }
      57             : 
      58           0 : unique_ptr<DBEntry> MvpnTable::AllocEntry(
      59             :     const DBRequestKey *key) const {
      60           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      61           0 :     return unique_ptr<DBEntry> (new MvpnRoute(pfxkey->prefix));
      62             : }
      63             : 
      64           0 : unique_ptr<DBEntry> MvpnTable::AllocEntryStr(
      65             :     const string &key_str) const {
      66           0 :     MvpnPrefix prefix = MvpnPrefix::FromString(key_str);
      67           0 :     return unique_ptr<DBEntry> (new MvpnRoute(prefix));
      68           0 : }
      69             : 
      70           0 : size_t MvpnTable::Hash(const DBEntry *entry) const {
      71           0 :     const MvpnRoute *rt_entry = static_cast<const MvpnRoute *>(entry);
      72           0 :     const MvpnPrefix &mvpnprefix = rt_entry->GetPrefix();
      73           0 :     size_t value = MvpnTable::HashFunction(mvpnprefix);
      74           0 :     return value % kPartitionCount;
      75             : }
      76             : 
      77           0 : size_t MvpnTable::Hash(const DBRequestKey *key) const {
      78           0 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      79           0 :     Ip4Prefix prefix(rkey->prefix.group(), 32);
      80           0 :     size_t value = InetTable::HashFunction(prefix);
      81           0 :     return value % kPartitionCount;
      82             : }
      83             : 
      84           0 : BgpRoute *MvpnTable::TableFind(DBTablePartition *rtp,
      85             :     const DBRequestKey *prefix) {
      86           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      87           0 :     MvpnRoute rt_key(pfxkey->prefix);
      88           0 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
      89           0 : }
      90             : 
      91         100 : DBTableBase *MvpnTable::CreateTable(DB *db, const string &name) {
      92         100 :     MvpnTable *table = new MvpnTable(db, name);
      93         100 :     table->Init();
      94         100 :     return table;
      95             : }
      96             : 
      97           0 : void MvpnTable::CreateManager() {
      98           0 :     if (manager_)
      99           0 :         return;
     100             : 
     101             :     // Don't create the MvpnManager if ProjectManager is not present.
     102           0 :     MvpnProjectManager *pm = GetProjectManager();
     103           0 :     if (!pm)
     104           0 :         return;
     105           0 :     manager_ = BgpStaticObjectFactory::Create<MvpnManager>(this, pm->table());
     106           0 :     manager_->Initialize();
     107             : 
     108             :     // Notify all routes in the table for further evaluation.
     109           0 :     NotifyAllEntries();
     110             : 
     111             :     // TODO(Ananth): Should we also notify routes in the bgp.mvpn.0 table ?
     112             : }
     113             : 
     114           0 : void MvpnTable::DestroyManager() {
     115           0 :     if (!manager_)
     116           0 :         return;
     117           0 :     if (IsDeleted())
     118           0 :         DeleteMvpnManager();
     119           0 :     manager_->Terminate();
     120           0 :     delete manager_;
     121           0 :     manager_ = NULL;
     122             : }
     123             : 
     124         100 : void MvpnTable::CreateMvpnManagers() {
     125         100 :     if (!server()->mvpn_ipv4_enable())
     126         100 :         return;
     127           0 :     RoutingInstance *rtinstance = routing_instance();
     128           0 :     std::scoped_lock lock(rtinstance->manager()->mutex());
     129             : 
     130             :     // Don't create the MvpnManager for the VPN table.
     131           0 :     if (!rtinstance->IsMasterRoutingInstance() &&
     132           0 :             !rtinstance->mvpn_project_manager_network().empty()) {
     133             :         pair<MvpnProjectManagerNetworks::iterator, bool> ret =
     134           0 :             rtinstance->manager()->mvpn_project_managers().insert(make_pair(
     135           0 :                 rtinstance->mvpn_project_manager_network(), set<string>()));
     136           0 :         ret.first->second.insert(rtinstance->name());
     137             : 
     138             :         // Initialize MVPN Manager.
     139           0 :         CreateManager();
     140             :     }
     141             : 
     142             :     MvpnProjectManagerNetworks::iterator iter =
     143           0 :         rtinstance->manager()->mvpn_project_managers().find(
     144           0 :             rtinstance->name());
     145           0 :     if (iter == rtinstance->manager()->mvpn_project_managers().end())
     146           0 :         return;
     147             : 
     148           0 :     BOOST_FOREACH(const string &mvpn_network, iter->second) {
     149             :         RoutingInstance *rti =
     150           0 :             rtinstance->manager()->GetRoutingInstance(mvpn_network);
     151           0 :         if (!rti || rti->deleted())
     152           0 :             continue;
     153             :         MvpnTable *table =
     154           0 :             dynamic_cast<MvpnTable *>(rti->GetTable(Address::MVPN));
     155           0 :         if (!table || table->IsDeleted())
     156           0 :             continue;
     157           0 :         table->CreateManager();
     158             :     }
     159           0 : }
     160             : 
     161           0 : void MvpnTable::DeleteMvpnManager() {
     162           0 :     if (!server()->mvpn_ipv4_enable())
     163           0 :         return;
     164           0 :     if (routing_instance()->mvpn_project_manager_network().empty())
     165           0 :         return;
     166           0 :     std::scoped_lock lock(routing_instance()->manager()->mutex());
     167             :     MvpnProjectManagerNetworks::iterator iter =
     168           0 :         routing_instance()->manager()->mvpn_project_managers().find(
     169           0 :             routing_instance()->mvpn_project_manager_network());
     170           0 :     if (iter != routing_instance()->manager()->mvpn_project_managers().end()) {
     171           0 :         iter->second.erase(routing_instance()->name());
     172           0 :         if (iter->second.empty())
     173           0 :             routing_instance()->manager()->mvpn_project_managers().erase(iter);
     174             :     }
     175           0 : }
     176             : 
     177             : // Call the const version to avoid code duplication.
     178           0 : MvpnProjectManager *MvpnTable::GetProjectManager() {
     179             :     return const_cast<MvpnProjectManager *>(
     180           0 :         static_cast<const MvpnTable *>(this)->GetProjectManager());
     181             : }
     182             : 
     183             : // Get MvpnProjectManager object for this Mvpn. Each MVPN network is associated
     184             : // with a parent project maanger network via configuration. MvpnProjectManager
     185             : // is retrieved from this parent network RoutingInstance's ErmVpnTable.
     186           0 : const MvpnProjectManager *MvpnTable::GetProjectManager() const {
     187           0 :     std::string pm_network = routing_instance()->mvpn_project_manager_network();
     188           0 :     if (pm_network.empty())
     189           0 :         return NULL;
     190             :     const RoutingInstance *rtinstance =
     191           0 :         routing_instance()->manager()->GetRoutingInstance(pm_network);
     192           0 :     if (!rtinstance)
     193           0 :         return NULL;
     194           0 :     const ErmVpnTable *table = dynamic_cast<const ErmVpnTable *>(
     195           0 :         rtinstance->GetTable(Address::ERMVPN));
     196           0 :     if (!table)
     197           0 :         return NULL;
     198           0 :     return table->mvpn_project_manager();
     199           0 : }
     200             : 
     201           0 : bool MvpnTable::IsProjectManagerUsable() const {
     202           0 :     std::string pm_network = routing_instance()->mvpn_project_manager_network();
     203           0 :     if (pm_network.empty())
     204           0 :         return false;
     205             :     const RoutingInstance *rtinstance =
     206           0 :         routing_instance()->manager()->GetRoutingInstance(pm_network);
     207           0 :     if (!rtinstance || rtinstance->deleted())
     208           0 :         return false;
     209           0 :     const ErmVpnTable *table = dynamic_cast<const ErmVpnTable *>(
     210           0 :         rtinstance->GetTable(Address::ERMVPN));
     211           0 :     if (!table || table->IsDeleted())
     212           0 :         return false;
     213             : 
     214           0 :     if (!table->mvpn_project_manager() ||
     215           0 :             table->mvpn_project_manager()->deleter()->IsDeleted()) {
     216           0 :         return false;
     217             :     }
     218           0 :     return true;
     219           0 : }
     220             : 
     221             : // Return the MvpnProjectManagerPartition for this route using the same DB
     222             : // partition index as of the route.
     223           0 : const MvpnProjectManagerPartition *MvpnTable::GetProjectManagerPartition(
     224             :         BgpRoute *route) const {
     225           0 :     const MvpnProjectManager *manager = GetProjectManager();
     226           0 :     if (!manager)
     227           0 :         return NULL;
     228           0 :     int part_id = route->get_table_partition()->index();
     229           0 :     return manager->GetPartition(part_id);
     230             : }
     231             : 
     232             : // Override virtual method to retrive target table for MVPN routes. For now,
     233             : // only Type-4 LeafAD routes require special treatment, as they always come
     234             : // with the same route target <router-id>:0. Hence, if normal rtf selection
     235             : // mode is used, every table with MVPN enalbled would have to be notified for
     236             : // replication. Instead, find the table based on the correspondong S-PMSI route.
     237             : // This route can be retrieved from the MVPN state of the <S-G> maintained in
     238             : // the MvpnProjectManagerPartition object.
     239           0 : void MvpnTable::UpdateSecondaryTablesForReplication(BgpRoute *rt,
     240             :         TableSet *secondary_tables) {
     241           0 :     MvpnRoute *mvpn_rt = dynamic_cast<MvpnRoute *>(rt);
     242           0 :     assert(mvpn_rt);
     243             : 
     244             :     // Special table lookup is required only for the Type4 LeafAD routes.
     245           0 :     if (mvpn_rt->GetPrefix().type() != MvpnPrefix::LeafADRoute)
     246           0 :         return;
     247             : 
     248             :     // Find Type-3 S-PMSI route from the Type-4 prefix route.
     249           0 :     MvpnPrefix spmsi_prefix;
     250           0 :     spmsi_prefix.SetSPMSIPrefixFromLeafADPrefix(mvpn_rt->GetPrefix());
     251           0 :     const MvpnRoute *spmsi_rt = FindRoute(spmsi_prefix);
     252           0 :     if (!spmsi_rt || !spmsi_rt->IsUsable())
     253           0 :         return;
     254           0 :     if (!spmsi_rt->BestPath()->IsReplicated())
     255           0 :         return;
     256             : 
     257           0 :     const BgpTable *table = dynamic_cast<const BgpSecondaryPath *>(
     258           0 :                                 spmsi_rt->BestPath())->src_table();
     259           0 :     const MvpnTable *mvpn_table = dynamic_cast<const MvpnTable *>(table);
     260           0 :     if (!mvpn_table || mvpn_table->IsMaster() || !mvpn_table->manager())
     261           0 :         return;
     262           0 :     mvpn_table->manager()->UpdateSecondaryTablesForReplication(
     263             :                               mvpn_rt, secondary_tables);
     264           0 : }
     265             : 
     266             : // Find or create the route.
     267           0 : MvpnRoute *MvpnTable::FindRoute(const MvpnPrefix &prefix) {
     268           0 :     MvpnRoute rt_key(prefix);
     269             :     DBTablePartition *rtp = static_cast<DBTablePartition *>(
     270           0 :         GetTablePartition(&rt_key));
     271           0 :     return dynamic_cast<MvpnRoute *>(rtp->Find(&rt_key));
     272           0 : }
     273             : 
     274           0 : const MvpnRoute *MvpnTable::FindRoute(const MvpnPrefix &prefix) const {
     275           0 :     MvpnRoute rt_key(prefix);
     276             :     const DBTablePartition *rtp = static_cast<const DBTablePartition *>(
     277           0 :         GetTablePartition(&rt_key));
     278           0 :     return dynamic_cast<const MvpnRoute *>(rtp->Find(&rt_key));
     279           0 : }
     280             : 
     281             : // Find or create the route.
     282           0 : MvpnRoute *MvpnTable::LocateRoute(const MvpnPrefix &prefix) {
     283           0 :     MvpnRoute rt_key(prefix);
     284             :     DBTablePartition *rtp = static_cast<DBTablePartition *>(
     285           0 :         GetTablePartition(&rt_key));
     286           0 :     MvpnRoute *dest_route = dynamic_cast<MvpnRoute *>(rtp->Find(&rt_key));
     287           0 :     if (dest_route == NULL) {
     288           0 :         dest_route = new MvpnRoute(prefix);
     289           0 :         rtp->Add(dest_route);
     290             :     } else {
     291           0 :         dest_route->ClearDelete();
     292             :     }
     293           0 :     return dest_route;
     294           0 : }
     295             : 
     296           0 : MvpnPrefix MvpnTable::CreateType4LeafADRoutePrefix(const MvpnRoute *type3_rt) {
     297           0 :     assert(type3_rt->GetPrefix().type() == MvpnPrefix::SPMSIADRoute);
     298           0 :     const Ip4Address originator_ip(server()->bgp_identifier());
     299           0 :     MvpnPrefix prefix(MvpnPrefix::LeafADRoute, originator_ip);
     300           0 :     prefix.SetLeafADPrefixFromSPMSIPrefix(type3_rt->GetPrefix());
     301           0 :     return prefix;
     302           0 : }
     303             : 
     304           0 : MvpnRoute *MvpnTable::LocateType4LeafADRoute(const MvpnRoute *type3_spmsi_rt) {
     305           0 :     MvpnPrefix prefix = CreateType4LeafADRoutePrefix(type3_spmsi_rt);
     306           0 :     return LocateRoute(prefix);
     307           0 : }
     308             : 
     309           0 : MvpnPrefix MvpnTable::CreateType3SPMSIRoutePrefix(const MvpnRoute *type7_rt) {
     310           0 :     assert(type7_rt->GetPrefix().type() == MvpnPrefix::SourceTreeJoinRoute);
     311           0 :     const RouteDistinguisher rd = type7_rt->GetPrefix().route_distinguisher();
     312           0 :     Ip4Address source = type7_rt->GetPrefix().source();
     313           0 :     Ip4Address group = type7_rt->GetPrefix().group();
     314           0 :     const Ip4Address originator_ip(server()->bgp_identifier());
     315             :     MvpnPrefix prefix(MvpnPrefix::SPMSIADRoute, rd, originator_ip,
     316           0 :             group, source);
     317           0 :     return prefix;
     318             : }
     319             : 
     320           0 : MvpnPrefix MvpnTable::CreateLocalType7Prefix(MvpnRoute *rt) const {
     321           0 :     const RouteDistinguisher rd =  RouteDistinguisher::kZeroRd;
     322           0 :     Ip4Address source = rt->GetPrefix().source();
     323           0 :     Ip4Address group = rt->GetPrefix().group();
     324             :     MvpnPrefix prefix(MvpnPrefix::SourceTreeJoinRoute, rd,
     325           0 :                       0, group, source);
     326           0 :     return prefix;
     327             : }
     328             : 
     329           0 : MvpnPrefix MvpnTable::CreateType7SourceTreeJoinRoutePrefix(
     330             :         MvpnRoute *rt) const {
     331             :     // get the source-rd from attributes as we store type-5 route with zero-rd
     332           0 :     const BgpAttr *attr = rt->BestPath()->GetAttr();
     333           0 :     assert(attr);
     334           0 :     assert(!attr->source_rd().IsZero());
     335           0 :     const RouteDistinguisher rd =  attr->source_rd();
     336           0 :     Ip4Address source = rt->GetPrefix().source();
     337           0 :     Ip4Address group = rt->GetPrefix().group();
     338             :     MvpnPrefix prefix(MvpnPrefix::SourceTreeJoinRoute, rd,
     339           0 :                       server()->autonomous_system(), group, source);
     340           0 :     return prefix;
     341             : }
     342             : 
     343           0 : MvpnRoute *MvpnTable::LocateType3SPMSIRoute(const MvpnRoute *type7_rt) {
     344           0 :     MvpnPrefix prefix = CreateType3SPMSIRoutePrefix(type7_rt);
     345           0 :     return LocateRoute(prefix);
     346           0 : }
     347             : 
     348           0 : MvpnPrefix MvpnTable::CreateType2ADRoutePrefix() {
     349           0 :     const RouteDistinguisher *rd = routing_instance()->GetRD();
     350             :     MvpnPrefix prefix(MvpnPrefix::InterASPMSIADRoute, *rd,
     351           0 :             server()->autonomous_system());
     352           0 :     return prefix;
     353             : }
     354             : 
     355           0 : MvpnPrefix MvpnTable::CreateType1ADRoutePrefix(
     356             :         const Ip4Address &originator_ip) {
     357           0 :     const RouteDistinguisher rd(originator_ip.to_ulong(),
     358           0 :                                 routing_instance()->index());
     359           0 :     MvpnPrefix prefix(MvpnPrefix::IntraASPMSIADRoute, rd, originator_ip);
     360           0 :     return prefix;
     361             : }
     362             : 
     363           0 : MvpnPrefix MvpnTable::CreateType1ADRoutePrefix() {
     364           0 :     return CreateType1ADRoutePrefix(Ip4Address(server()->bgp_identifier()));
     365             : }
     366             : 
     367           0 : MvpnRoute *MvpnTable::LocateType1ADRoute() {
     368           0 :     MvpnPrefix prefix = CreateType1ADRoutePrefix();
     369           0 :     return LocateRoute(prefix);
     370           0 : }
     371             : 
     372           0 : MvpnRoute *MvpnTable::FindType1ADRoute(const Ip4Address &originator_ip) {
     373           0 :     MvpnPrefix prefix = CreateType1ADRoutePrefix(originator_ip);
     374           0 :     return FindRoute(prefix);
     375           0 : }
     376             : 
     377           0 : MvpnRoute *MvpnTable::FindType1ADRoute() {
     378           0 :     Ip4Address originator_ip(server()->bgp_identifier());
     379           0 :     return FindType1ADRoute(Ip4Address(server()->bgp_identifier()));
     380             : }
     381             : 
     382           0 : const MvpnRoute *MvpnTable::FindType7SourceTreeJoinRoute(MvpnRoute *rt) const {
     383           0 :     MvpnPrefix prefix;
     384           0 :     if (rt->GetPrefix().type() == MvpnPrefix::SourceActiveADRoute)
     385           0 :         prefix = CreateType7SourceTreeJoinRoutePrefix(rt);
     386           0 :     if (rt->GetPrefix().type() == MvpnPrefix::SPMSIADRoute) {
     387           0 :         prefix = CreateLocalType7Prefix(rt);
     388             :     }
     389           0 :     return FindRoute(prefix);
     390           0 : }
     391             : 
     392           0 : BgpRoute *MvpnTable::RouteReplicate(BgpServer *server, BgpTable *stable,
     393             :         BgpRoute *rt, const BgpPath *src_path, ExtCommunityPtr community) {
     394           0 :     MvpnTable *src_table = dynamic_cast<MvpnTable *>(stable);
     395           0 :     assert(src_table);
     396           0 :     MvpnRoute *src_rt = dynamic_cast<MvpnRoute *>(rt);
     397           0 :     assert(src_rt);
     398             : 
     399           0 :     if (!server->mvpn_ipv4_enable()) {
     400           0 :         return ReplicatePath(server, src_rt->GetPrefix(), src_table, src_rt,
     401           0 :                              src_path, community);
     402             :     }
     403             : 
     404             :     // Replicate Type7 C-Join route.
     405           0 :     if (src_rt->GetPrefix().type() == MvpnPrefix::SourceTreeJoinRoute) {
     406           0 :         return ReplicateType7SourceTreeJoin(server, src_table, src_rt,
     407           0 :                                             src_path, community);
     408             :     }
     409             : 
     410           0 :     if (!IsMaster()) {
     411             :         // For type-4 paths, only replicate if there is a type-3 primary path
     412             :         // present in the table.
     413           0 :         if (src_rt->GetPrefix().type() == MvpnPrefix::LeafADRoute) {
     414           0 :             MvpnProjectManager *pm = GetProjectManager();
     415           0 :             if (!pm)
     416           0 :                 return NULL;
     417           0 :             MvpnStatePtr mvpn_state = pm->GetState(src_rt);
     418           0 :             if (!mvpn_state || !mvpn_state->spmsi_rt() ||
     419           0 :                     !mvpn_state->spmsi_rt()->IsUsable()) {
     420           0 :                 return NULL;
     421             :             }
     422           0 :             if (mvpn_state->spmsi_rt()->table() != this)
     423           0 :                 return NULL;
     424           0 :         }
     425             :     }
     426             : 
     427             :     // Replicate all other types.
     428           0 :     return ReplicatePath(server, src_rt->GetPrefix(), src_table, src_rt,
     429           0 :                          src_path, community);
     430             : }
     431             : 
     432           0 : BgpRoute *MvpnTable::ReplicateType7SourceTreeJoin(BgpServer *server,
     433             :     MvpnTable *src_table, MvpnRoute *src_rt, const BgpPath *src_path,
     434             :     ExtCommunityPtr ext_community) {
     435             : 
     436             :     // Only replicate if route has a target that matches this table's auto
     437             :     // created route target (vit).
     438           0 :     if (!IsMaster()) {
     439           0 :         RouteTarget vit(Ip4Address(server->bgp_identifier()),
     440           0 :                                    routing_instance()->index());
     441           0 :         bool vit_found = false;
     442           0 :         BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     443             :                       ext_community->communities()) {
     444           0 :             if (ExtCommunity::is_route_target(comm)) {
     445           0 :                 RouteTarget rtarget(comm);
     446           0 :                 if (rtarget == vit) {
     447           0 :                     vit_found = true;
     448           0 :                     break;
     449             :                 }
     450             :             }
     451             :         }
     452             : 
     453           0 :         if (!vit_found) {
     454           0 :             MVPN_RT_LOG(src_rt, "Route was not replicated as rt-import "
     455             :                         "extended-community was not found");
     456           0 :             return NULL;
     457             :         }
     458             :     }
     459             : 
     460             :     // If replicating from Master table, no special checks are required.
     461           0 :     if (src_table->IsMaster()) {
     462           0 :         return ReplicatePath(server, src_rt->GetPrefix(), src_table, src_rt,
     463           0 :                              src_path, ext_community);
     464             :     }
     465             : 
     466             :     // This is the case when routes are replicated either to Master or to other
     467             :     // vrf.mvpn.0 as identified the route targets. In either case, basic idea
     468             :     // is to target the replicated path directly to vrf where sender resides.
     469             :     //
     470             :     // Route-target of the target vrf is derived from the Vrf Import Target of
     471             :     // the route the source resolves to. Resolver code would have already
     472             :     // computed this and encoded inside source-rd. Also source-as to encode in
     473             :     // the RD is also encoded as part of the SourceAS extended community.
     474           0 :     const BgpAttr *attr = src_path->GetAttr();
     475           0 :     if (!attr)
     476           0 :         return NULL;
     477             : 
     478             :     // Do not resplicate if the source is not resolvable.
     479           0 :     if (attr->source_rd().IsZero()) {
     480           0 :         MVPN_RT_LOG(src_rt, "Route was not replicated as source_rd is zero");
     481           0 :         return NULL;
     482             :     }
     483             : 
     484             :     // Find source-as extended-community. If not present, do not replicate
     485           0 :     bool source_as_found = false;
     486           0 :     SourceAs source_as;
     487           0 :     BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &value,
     488             :                   attr->ext_community()->communities()) {
     489           0 :         if (ExtCommunity::is_source_as(value)) {
     490           0 :             source_as_found = true;
     491           0 :             source_as = SourceAs(value);
     492           0 :             break;
     493             :         }
     494             :     }
     495             : 
     496           0 :     if (!source_as_found) {
     497           0 :         MVPN_RT_LOG(src_rt, "Route was not replicated as source_as is zero");
     498           0 :         return NULL;
     499             :     }
     500             : 
     501             :     // No need to send SourceAS with this mvpn route. This is only sent along
     502             :     // with the unicast routes.
     503             :     ext_community =
     504           0 :         server->extcomm_db()->RemoveSourceASAndLocate(ext_community.get());
     505             : 
     506             :     // Replicate path using source route's<C-S,G>, source_rd and asn as encoded
     507             :     // in the source-as attribute.
     508             :     MvpnPrefix prefix(MvpnPrefix::SourceTreeJoinRoute, attr->source_rd(),
     509           0 :                       source_as.GetAsn(), src_rt->GetPrefix().group(),
     510           0 :                       src_rt->GetPrefix().source());
     511             : 
     512             :     // Replicate the path with the computed prefix and attributes.
     513           0 :     return ReplicatePath(server, prefix, src_table, src_rt, src_path,
     514           0 :                          ext_community);
     515           0 : }
     516             : 
     517           0 : BgpRoute *MvpnTable::ReplicatePath(BgpServer *server, const MvpnPrefix &mprefix,
     518             :         MvpnTable *src_table, MvpnRoute *src_rt, const BgpPath *src_path,
     519             :         ExtCommunityPtr comm) {
     520           0 :     MvpnRoute rt_key(mprefix);
     521             : 
     522             :     // Find or create the route.
     523             :     DBTablePartition *rtp =
     524           0 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
     525           0 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
     526           0 :     if (dest_route == NULL) {
     527           0 :         dest_route = new MvpnRoute(mprefix);
     528           0 :         rtp->Add(dest_route);
     529             :     } else {
     530           0 :         dest_route->ClearDelete();
     531             :     }
     532             : 
     533             :     BgpAttrPtr new_attr =
     534             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     535           0 :                                                         comm.get());
     536             :     // Replace Nexthop with controller address, MX rejects the route if
     537             :     // nexthop is same as neighbor address, needed for Type-7 routes
     538           0 :     new_attr = server->attr_db()->ReplaceNexthopAndLocate(new_attr.get(),
     539           0 :                                   Ip4Address(server->bgp_identifier()));
     540             :     // Need to strip off route targets other than sender-ip:0
     541           0 :     if (src_rt->GetPrefix().type() == MvpnPrefix::LeafADRoute) {
     542           0 :         ExtCommunity::ExtCommunityList rtarget;
     543           0 :         Ip4Address ip = src_rt->GetPrefix().GetType3OriginatorFromType4Route();
     544           0 :         RouteTarget leaf_ad_rtarget(ip, 0);
     545           0 :         BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &value,
     546             :                       comm->communities()) {
     547           0 :             if (ExtCommunity::is_route_target(value)) {
     548           0 :                 if (leaf_ad_rtarget == RouteTarget(value)) {
     549           0 :                     rtarget.push_back(value);
     550           0 :                     break;
     551             :                 }
     552             :             }
     553             :         }
     554             : 
     555           0 :         if (rtarget.size() == 1) {
     556             :             ExtCommunityPtr ext_community = server->extcomm_db()->
     557           0 :                 ReplaceRTargetAndLocate(new_attr->ext_community(), rtarget);
     558           0 :             new_attr = server->attr_db()->ReplaceExtCommunityAndLocate(
     559           0 :                 src_path->GetAttr(), ext_community.get());
     560           0 :         } else {
     561           0 :             MVPN_RT_LOG(src_rt,
     562             :                         "Could not find <originator>:0 route-target community");
     563             :         }
     564           0 :     }
     565             : 
     566             :     // Check whether peer already has a path.
     567           0 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     568             :             src_path->GetSource(), src_path->GetPeer(),
     569             :             src_path->GetPathId());
     570           0 :     if (dest_path != NULL) {
     571           0 :         if (new_attr != dest_path->GetOriginalAttr() ||
     572           0 :             src_path->GetFlags() != dest_path->GetFlags()) {
     573           0 :             bool success = dest_route->RemoveSecondaryPath(src_rt,
     574             :                 src_path->GetSource(), src_path->GetPeer(),
     575             :                 src_path->GetPathId());
     576           0 :             assert(success);
     577             :         } else {
     578           0 :             return dest_route;
     579             :         }
     580             :     }
     581             : 
     582             :     // Create replicated path and insert it on the route.
     583             :     BgpSecondaryPath *replicated_path =
     584           0 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     585           0 :                              src_path->GetSource(), new_attr,
     586           0 :                              src_path->GetFlags(), src_path->GetLabel());
     587           0 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     588           0 :     dest_route->InsertPath(replicated_path);
     589           0 :     rtp->Notify(dest_route);
     590           0 :     MVPN_RT_LOG(src_rt, "Route was successfully replicated");
     591           0 :     return dest_route;
     592           0 : }
     593             : 
     594           0 : bool MvpnTable::Export(RibOut *ribout, Route *route,
     595             :     const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     596           0 :     MvpnRoute *mvpn_route = dynamic_cast<MvpnRoute *>(route);
     597             : 
     598           0 :     if (ribout->IsEncodingXmpp()) {
     599           0 :         UpdateInfo *uinfo = GetMvpnUpdateInfo(ribout, mvpn_route, peerset);
     600           0 :         if (!uinfo)
     601           0 :             return false;
     602           0 :         uinfo_slist->push_front(*uinfo);
     603           0 :         return true;
     604             :     }
     605           0 :     BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     606             : 
     607           0 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     608           0 :     if (!uinfo) {
     609           0 :         MVPN_RT_LOG(mvpn_route, "Route was exported as update_info could not "
     610             :                     "be computed");
     611           0 :         return false;
     612             :     }
     613           0 :     uinfo_slist->push_front(*uinfo);
     614           0 :     return true;
     615             : }
     616             : 
     617           0 : void MvpnTable::GetPeerSet(RibOut *ribout, MvpnRoute *route,
     618             :         const RibPeerSet &peerset, RibPeerSet *new_peerset) {
     619           0 :     RibOut::PeerIterator iter(ribout, peerset);
     620           0 :     while (iter.HasNext()) {
     621           0 :         int current_index = iter.index();
     622           0 :         IPeer *peer = dynamic_cast<IPeer *>(iter.Next());
     623           0 :         assert(peer);
     624           0 :         for (Route::PathList::const_iterator it = route->GetPathList().begin();
     625           0 :              it != route->GetPathList().end(); ++it) {
     626           0 :             const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
     627           0 :             if (path->IsFeasible() && peer == path->GetPeer()) {
     628           0 :                 new_peerset->set(current_index);
     629           0 :                 break;
     630             :             }
     631             :         }
     632             :     }
     633           0 : }
     634             : 
     635           0 : UpdateInfo *MvpnTable::GetMvpnUpdateInfo(RibOut *ribout, MvpnRoute *route,
     636             :     const RibPeerSet &peerset) {
     637           0 :     if ((route->GetPrefix().type() != MvpnPrefix::SourceActiveADRoute) &&
     638           0 :         (route->GetPrefix().type() != MvpnPrefix::SourceTreeJoinRoute))
     639           0 :         return NULL;
     640           0 :     if (!route->IsUsable())
     641           0 :         return NULL;
     642             : 
     643           0 :     if (route->BestPath()->IsReplicated())
     644           0 :         return NULL;
     645             : 
     646           0 :     MvpnProjectManager *pm = GetProjectManager();
     647           0 :     if (!pm) {
     648           0 :         MVPN_RT_LOG(route, "Route was exported as ProjectManager was "
     649             :                     "not found");
     650           0 :         return NULL;
     651             :     }
     652             : 
     653           0 :     RibPeerSet new_peerset;
     654           0 :     GetPeerSet(ribout, route, peerset, &new_peerset);
     655             : 
     656           0 :     if (new_peerset.empty())
     657           0 :         return NULL;
     658             : 
     659           0 :     UpdateInfo *uinfo = pm->GetUpdateInfo(route);
     660           0 :     if (uinfo)
     661           0 :         uinfo->target = new_peerset;
     662           0 :     return uinfo;
     663           0 : }
     664             : 
     665        3459 : bool MvpnTable::IsMaster() const {
     666        3459 :     return routing_instance()->IsMasterRoutingInstance();
     667             : }
     668             : 
     669           1 : static void RegisterFactory() {
     670           1 :     DB::RegisterFactory("mvpn.0", &MvpnTable::CreateTable);
     671           1 : }
     672             : 
     673             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14