LCOV - code coverage report
Current view: top level - bgp/ermvpn - ermvpn_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 144 147 98.0 %
Date: 2026-11-30 02:00:48 Functions: 22 23 95.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/ermvpn/ermvpn_table.h"
       7             : 
       8             : #include "bgp/ipeer.h"
       9             : #include "bgp/bgp_factory.h"
      10             : #include "bgp/bgp_multicast.h"
      11             : #include "bgp/bgp_mvpn.h"
      12             : #include "bgp/bgp_server.h"
      13             : #include "bgp/bgp_update.h"
      14             : #include "bgp/inet/inet_table.h"
      15             : #include "bgp/origin-vn/origin_vn.h"
      16             : #include "bgp/routing-instance/routing_instance.h"
      17             : 
      18             : using std::unique_ptr;
      19             : using std::string;
      20             : 
      21      252976 : size_t ErmVpnTable::HashFunction(const ErmVpnPrefix &prefix) const {
      22      252976 :     return boost::hash_value(prefix.group().to_ulong());
      23             : }
      24             : 
      25       51139 : ErmVpnTable::ErmVpnTable(DB *db, const string &name)
      26       51139 :     : BgpTable(db, name), tree_manager_(NULL), mvpn_project_manager_(NULL) {
      27       51139 : }
      28             : 
      29        7199 : unique_ptr<DBEntry> ErmVpnTable::AllocEntry(
      30             :     const DBRequestKey *key) const {
      31        7199 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      32        7199 :     return unique_ptr<DBEntry> (new ErmVpnRoute(pfxkey->prefix));
      33             : }
      34             : 
      35             : 
      36         103 : unique_ptr<DBEntry> ErmVpnTable::AllocEntryStr(
      37             :     const string &key_str) const {
      38         103 :     ErmVpnPrefix prefix = ErmVpnPrefix::FromString(key_str);
      39         206 :     return unique_ptr<DBEntry> (new ErmVpnRoute(prefix));
      40             : }
      41             : 
      42      252978 : size_t ErmVpnTable::Hash(const DBEntry *entry) const {
      43      252978 :     const ErmVpnRoute *rt_entry = static_cast<const ErmVpnRoute *>(entry);
      44      252978 :     const ErmVpnPrefix &ermvpnprefix = rt_entry->GetPrefix();
      45      252977 :     size_t value = ErmVpnTable::HashFunction(ermvpnprefix);
      46      252969 :     return value % kPartitionCount;
      47             : }
      48             : 
      49        3256 : size_t ErmVpnTable::Hash(const Ip4Address &group) const {
      50        3256 :     return boost::hash_value(group.to_ulong()) % kPartitionCount;
      51             : }
      52             : 
      53       14799 : size_t ErmVpnTable::Hash(const DBRequestKey *key) const {
      54       14799 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      55       14799 :     Ip4Prefix prefix(rkey->prefix.group(), 32);
      56       14799 :     size_t value = InetTable::HashFunction(prefix);
      57       14799 :     return value % kPartitionCount;
      58             : }
      59             : 
      60       14051 : BgpRoute *ErmVpnTable::TableFind(DBTablePartition *rtp,
      61             :     const DBRequestKey *prefix) {
      62       14051 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      63       14051 :     ErmVpnRoute rt_key(pfxkey->prefix);
      64       28102 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
      65       14051 : }
      66             : 
      67       51133 : DBTableBase *ErmVpnTable::CreateTable(DB *db, const string &name) {
      68       51133 :     ErmVpnTable *table = new ErmVpnTable(db, name);
      69       51139 :     table->Init();
      70       51139 :     return table;
      71             : }
      72             : 
      73       14296 : BgpRoute *ErmVpnTable::RouteReplicate(BgpServer *server,
      74             :         BgpTable *src_table, BgpRoute *src_rt, const BgpPath *src_path,
      75             :         ExtCommunityPtr community) {
      76       14296 :     assert(src_table->family() == Address::ERMVPN);
      77             : 
      78       14296 :     ErmVpnRoute *mroute = dynamic_cast<ErmVpnRoute *>(src_rt);
      79       14296 :     assert(mroute);
      80             : 
      81             :     // Native routes are not replicated to other VRFs or to the VPN table.
      82       14296 :     if (mroute->GetPrefix().type() == ErmVpnPrefix::NativeRoute)
      83        3607 :         return NULL;
      84             : 
      85       10689 :     if (!IsMaster()) {
      86             :         // Don't replicate to a VRF from other VRF tables.
      87        3674 :         ErmVpnTable *src_ermvpn_table = dynamic_cast<ErmVpnTable *>(src_table);
      88        3674 :         if (!src_ermvpn_table->IsMaster())
      89         177 :             return NULL;
      90             : 
      91             :         // Don't replicate to VRF from the VPN table if OriginVn doesn't match.
      92        6994 :         if (!community->ContainsOriginVn(server->autonomous_system(),
      93        3497 :                     routing_instance()->virtual_network_index()))
      94         109 :             return NULL;
      95             :     }
      96             : 
      97             :     // RD is always zero in the VRF.  When replicating to the VPN table, we
      98             :     // pick up the RD from the SourceRD attribute. The SourceRD is always set
      99             :     // for Local and Global routes that the multicast code adds to a VRF.
     100       10403 :     ErmVpnPrefix mprefix(mroute->GetPrefix());
     101       10403 :     if (IsMaster()) {
     102        7015 :         mprefix.set_route_distinguisher(src_path->GetAttr()->source_rd());
     103             :     } else {
     104        3388 :         mprefix.set_route_distinguisher(RouteDistinguisher::kZeroRd);
     105             :     }
     106       10403 :     ErmVpnRoute rt_key(mprefix);
     107             : 
     108             :     // Find or create the route.
     109             :     DBTablePartition *rtp =
     110       10403 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
     111       10403 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
     112       10403 :     if (dest_route == NULL) {
     113        3452 :         dest_route = new ErmVpnRoute(mprefix);
     114        3452 :         rtp->Add(dest_route);
     115             :     } else {
     116        6951 :         dest_route->ClearDelete();
     117             :     }
     118             : 
     119             :     BgpAttrPtr new_attr =
     120             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     121       10403 :                                                         community);
     122             : 
     123             :     // Check whether peer already has a path.
     124       10403 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     125             :             src_path->GetSource(), src_path->GetPeer(),
     126             :             src_path->GetPathId());
     127       10403 :     if (dest_path != NULL) {
     128        7564 :         if (new_attr != dest_path->GetOriginalAttr() ||
     129        2686 :             src_path->GetFlags() != dest_path->GetFlags()) {
     130        2192 :             bool success = dest_route->RemoveSecondaryPath(src_rt,
     131             :                 src_path->GetSource(), src_path->GetPeer(),
     132             :                 src_path->GetPathId());
     133        2192 :             assert(success);
     134             :         } else {
     135        2686 :             return dest_route;
     136             :         }
     137             :     }
     138             : 
     139             :     // Create replicated path and insert it on the route.
     140             :     BgpSecondaryPath *replicated_path =
     141        7717 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     142        7717 :                              src_path->GetSource(), new_attr,
     143        7717 :                              src_path->GetFlags(), src_path->GetLabel());
     144        7717 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     145        7717 :     dest_route->InsertPath(replicated_path);
     146        7717 :     rtp->Notify(dest_route);
     147             : 
     148        7717 :     return dest_route;
     149       10403 : }
     150             : 
     151       18466 : bool ErmVpnTable::Export(RibOut *ribout, Route *route,
     152             :     const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     153       18466 :     if (ribout->IsEncodingBgp()) {
     154        7353 :         BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     155        7353 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     156        7353 :         if (!uinfo)
     157        3878 :             return false;
     158        3475 :         uinfo_slist->push_front(*uinfo);
     159        3475 :         return true;
     160             :     }
     161             : 
     162       11113 :     ErmVpnRoute *ermvpn_route = dynamic_cast<ErmVpnRoute *>(route);
     163       18927 :     if (ermvpn_route->GetPrefix().type() != ErmVpnPrefix::NativeRoute &&
     164        7814 :             ermvpn_route->GetPrefix().type() != ErmVpnPrefix::GlobalTreeRoute)
     165        4817 :         return false;
     166             : 
     167        6296 :     if (!tree_manager_ || tree_manager_->deleter()->IsDeleted())
     168           0 :         return false;
     169             : 
     170        6296 :     const IPeer *peer = ermvpn_route->BestPath()->GetPeer();
     171        6296 :     if (!peer || !ribout->IsRegistered(const_cast<IPeer *>(peer)))
     172        2997 :         return false;
     173             : 
     174        3299 :     size_t peerbit = ribout->GetPeerIndex(const_cast<IPeer *>(peer));
     175        3299 :     if (!peerset.test(peerbit))
     176         136 :         return false;
     177             : 
     178        3163 :     UpdateInfo *uinfo = tree_manager_->GetUpdateInfo(ermvpn_route);
     179        3163 :     if (!uinfo)
     180         645 :         return false;
     181             : 
     182        2518 :     uinfo->target.set(peerbit);
     183        2518 :     uinfo_slist->push_front(*uinfo);
     184        2518 :     return true;
     185             : }
     186             : 
     187       51139 : void ErmVpnTable::CreateTreeManager() {
     188             :     // Don't create the McastTreeManager for the VPN table.
     189       51139 :     if (IsMaster() && !server()->mvpn_ipv4_enable())
     190        8228 :         return;
     191       42911 :     assert(!tree_manager_);
     192       42911 :     tree_manager_ = BgpStaticObjectFactory::Create<McastTreeManager>(this);
     193       42911 :     tree_manager_->Initialize();
     194             : }
     195             : 
     196       42911 : void ErmVpnTable::DestroyTreeManager() {
     197       42911 :     assert(tree_manager_);
     198       42911 :     tree_manager_->Terminate();
     199       42911 :     delete tree_manager_;
     200       42911 :     tree_manager_ = NULL;
     201       42911 : }
     202             : 
     203           4 : McastTreeManager *ErmVpnTable::GetTreeManager() {
     204           4 :     return tree_manager_;
     205             : }
     206             : 
     207         210 : const McastTreeManager *ErmVpnTable::GetTreeManager() const {
     208         210 :     return tree_manager_;
     209             : }
     210             : 
     211       51136 : void ErmVpnTable::set_routing_instance(RoutingInstance *rtinstance) {
     212       51136 :     BgpTable::set_routing_instance(rtinstance);
     213       51139 :     CreateTreeManager();
     214       51138 :     CreateMvpnProjectManager();
     215       51138 : }
     216             : 
     217       51138 : void ErmVpnTable::CreateMvpnProjectManager() {
     218             :     // Don't create the MvpnProjectManager for the master table.
     219       51138 :     if (!server()->mvpn_ipv4_enable() || IsMaster())
     220       28575 :         return;
     221       22563 :     assert(!mvpn_project_manager_);
     222       22563 :     mvpn_project_manager_ = BgpStaticObjectFactory::Create<MvpnProjectManager>(this);
     223       22563 :     mvpn_project_manager_->Initialize();
     224             : }
     225             : 
     226       22563 : void ErmVpnTable::DestroyMvpnProjectManager() {
     227       22563 :     assert(mvpn_project_manager_);
     228       22563 :     mvpn_project_manager_->Terminate();
     229       22563 :     delete mvpn_project_manager_;
     230       22563 :     mvpn_project_manager_ = NULL;
     231       22563 : }
     232             : 
     233      763892 : bool ErmVpnTable::IsMaster() const {
     234      763892 :     return routing_instance()->IsMasterRoutingInstance();
     235             : }
     236             : 
     237             : // Find or create the route.
     238       33129 : ErmVpnRoute *ErmVpnTable::FindRoute(const ErmVpnPrefix &prefix) {
     239       33129 :     ErmVpnRoute rt_key(prefix);
     240             :     DBTablePartition *rtp = static_cast<DBTablePartition *>(
     241       33129 :         GetTablePartition(&rt_key));
     242       66258 :     return static_cast<ErmVpnRoute *>(rtp->Find(&rt_key));
     243       33129 : }
     244             : 
     245           0 : const ErmVpnRoute *ErmVpnTable::FindRoute(const ErmVpnPrefix &prefix) const {
     246             :     return const_cast<ErmVpnRoute *>(
     247           0 :         static_cast<const ErmVpnTable *>(this)->FindRoute(prefix));
     248             : }
     249             : 
     250        1251 : void ErmVpnTable::GetMvpnSourceAddress(ErmVpnRoute *ermvpn_rt,
     251             :                                        Ip4Address *address) const {
     252        1251 :     if (mvpn_project_manager_)
     253         387 :         mvpn_project_manager_->GetMvpnSourceAddress(ermvpn_rt, address);
     254        1251 : }
     255             : 
     256         159 : static void RegisterFactory() {
     257         159 :     DB::RegisterFactory("ermvpn.0", &ErmVpnTable::CreateTable);
     258         159 : }
     259             : 
     260             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14