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: 32 147 21.8 %
Date: 2026-11-16 02:06:09 Functions: 8 23 34.8 %
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           0 : size_t ErmVpnTable::HashFunction(const ErmVpnPrefix &prefix) const {
      22           0 :     return boost::hash_value(prefix.group().to_ulong());
      23             : }
      24             : 
      25         100 : ErmVpnTable::ErmVpnTable(DB *db, const string &name)
      26         100 :     : BgpTable(db, name), tree_manager_(NULL), mvpn_project_manager_(NULL) {
      27         100 : }
      28             : 
      29           0 : unique_ptr<DBEntry> ErmVpnTable::AllocEntry(
      30             :     const DBRequestKey *key) const {
      31           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      32           0 :     return unique_ptr<DBEntry> (new ErmVpnRoute(pfxkey->prefix));
      33             : }
      34             : 
      35             : 
      36           0 : unique_ptr<DBEntry> ErmVpnTable::AllocEntryStr(
      37             :     const string &key_str) const {
      38           0 :     ErmVpnPrefix prefix = ErmVpnPrefix::FromString(key_str);
      39           0 :     return unique_ptr<DBEntry> (new ErmVpnRoute(prefix));
      40             : }
      41             : 
      42           0 : size_t ErmVpnTable::Hash(const DBEntry *entry) const {
      43           0 :     const ErmVpnRoute *rt_entry = static_cast<const ErmVpnRoute *>(entry);
      44           0 :     const ErmVpnPrefix &ermvpnprefix = rt_entry->GetPrefix();
      45           0 :     size_t value = ErmVpnTable::HashFunction(ermvpnprefix);
      46           0 :     return value % kPartitionCount;
      47             : }
      48             : 
      49           0 : size_t ErmVpnTable::Hash(const Ip4Address &group) const {
      50           0 :     return boost::hash_value(group.to_ulong()) % kPartitionCount;
      51             : }
      52             : 
      53           0 : size_t ErmVpnTable::Hash(const DBRequestKey *key) const {
      54           0 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      55           0 :     Ip4Prefix prefix(rkey->prefix.group(), 32);
      56           0 :     size_t value = InetTable::HashFunction(prefix);
      57           0 :     return value % kPartitionCount;
      58             : }
      59             : 
      60           0 : BgpRoute *ErmVpnTable::TableFind(DBTablePartition *rtp,
      61             :     const DBRequestKey *prefix) {
      62           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      63           0 :     ErmVpnRoute rt_key(pfxkey->prefix);
      64           0 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
      65           0 : }
      66             : 
      67         100 : DBTableBase *ErmVpnTable::CreateTable(DB *db, const string &name) {
      68         100 :     ErmVpnTable *table = new ErmVpnTable(db, name);
      69         100 :     table->Init();
      70         100 :     return table;
      71             : }
      72             : 
      73           0 : BgpRoute *ErmVpnTable::RouteReplicate(BgpServer *server,
      74             :         BgpTable *src_table, BgpRoute *src_rt, const BgpPath *src_path,
      75             :         ExtCommunityPtr community) {
      76           0 :     assert(src_table->family() == Address::ERMVPN);
      77             : 
      78           0 :     ErmVpnRoute *mroute = dynamic_cast<ErmVpnRoute *>(src_rt);
      79           0 :     assert(mroute);
      80             : 
      81             :     // Native routes are not replicated to other VRFs or to the VPN table.
      82           0 :     if (mroute->GetPrefix().type() == ErmVpnPrefix::NativeRoute)
      83           0 :         return NULL;
      84             : 
      85           0 :     if (!IsMaster()) {
      86             :         // Don't replicate to a VRF from other VRF tables.
      87           0 :         ErmVpnTable *src_ermvpn_table = dynamic_cast<ErmVpnTable *>(src_table);
      88           0 :         if (!src_ermvpn_table->IsMaster())
      89           0 :             return NULL;
      90             : 
      91             :         // Don't replicate to VRF from the VPN table if OriginVn doesn't match.
      92           0 :         if (!community->ContainsOriginVn(server->autonomous_system(),
      93           0 :                     routing_instance()->virtual_network_index()))
      94           0 :             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           0 :     ErmVpnPrefix mprefix(mroute->GetPrefix());
     101           0 :     if (IsMaster()) {
     102           0 :         mprefix.set_route_distinguisher(src_path->GetAttr()->source_rd());
     103             :     } else {
     104           0 :         mprefix.set_route_distinguisher(RouteDistinguisher::kZeroRd);
     105             :     }
     106           0 :     ErmVpnRoute rt_key(mprefix);
     107             : 
     108             :     // Find or create the route.
     109             :     DBTablePartition *rtp =
     110           0 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
     111           0 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
     112           0 :     if (dest_route == NULL) {
     113           0 :         dest_route = new ErmVpnRoute(mprefix);
     114           0 :         rtp->Add(dest_route);
     115             :     } else {
     116           0 :         dest_route->ClearDelete();
     117             :     }
     118             : 
     119             :     BgpAttrPtr new_attr =
     120             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     121           0 :                                                         community);
     122             : 
     123             :     // Check whether peer already has a path.
     124           0 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     125             :             src_path->GetSource(), src_path->GetPeer(),
     126             :             src_path->GetPathId());
     127           0 :     if (dest_path != NULL) {
     128           0 :         if (new_attr != dest_path->GetOriginalAttr() ||
     129           0 :             src_path->GetFlags() != dest_path->GetFlags()) {
     130           0 :             bool success = dest_route->RemoveSecondaryPath(src_rt,
     131             :                 src_path->GetSource(), src_path->GetPeer(),
     132             :                 src_path->GetPathId());
     133           0 :             assert(success);
     134             :         } else {
     135           0 :             return dest_route;
     136             :         }
     137             :     }
     138             : 
     139             :     // Create replicated path and insert it on the route.
     140             :     BgpSecondaryPath *replicated_path =
     141           0 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     142           0 :                              src_path->GetSource(), new_attr,
     143           0 :                              src_path->GetFlags(), src_path->GetLabel());
     144           0 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     145           0 :     dest_route->InsertPath(replicated_path);
     146           0 :     rtp->Notify(dest_route);
     147             : 
     148           0 :     return dest_route;
     149           0 : }
     150             : 
     151           0 : bool ErmVpnTable::Export(RibOut *ribout, Route *route,
     152             :     const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     153           0 :     if (ribout->IsEncodingBgp()) {
     154           0 :         BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     155           0 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     156           0 :         if (!uinfo)
     157           0 :             return false;
     158           0 :         uinfo_slist->push_front(*uinfo);
     159           0 :         return true;
     160             :     }
     161             : 
     162           0 :     ErmVpnRoute *ermvpn_route = dynamic_cast<ErmVpnRoute *>(route);
     163           0 :     if (ermvpn_route->GetPrefix().type() != ErmVpnPrefix::NativeRoute &&
     164           0 :             ermvpn_route->GetPrefix().type() != ErmVpnPrefix::GlobalTreeRoute)
     165           0 :         return false;
     166             : 
     167           0 :     if (!tree_manager_ || tree_manager_->deleter()->IsDeleted())
     168           0 :         return false;
     169             : 
     170           0 :     const IPeer *peer = ermvpn_route->BestPath()->GetPeer();
     171           0 :     if (!peer || !ribout->IsRegistered(const_cast<IPeer *>(peer)))
     172           0 :         return false;
     173             : 
     174           0 :     size_t peerbit = ribout->GetPeerIndex(const_cast<IPeer *>(peer));
     175           0 :     if (!peerset.test(peerbit))
     176           0 :         return false;
     177             : 
     178           0 :     UpdateInfo *uinfo = tree_manager_->GetUpdateInfo(ermvpn_route);
     179           0 :     if (!uinfo)
     180           0 :         return false;
     181             : 
     182           0 :     uinfo->target.set(peerbit);
     183           0 :     uinfo_slist->push_front(*uinfo);
     184           0 :     return true;
     185             : }
     186             : 
     187         100 : void ErmVpnTable::CreateTreeManager() {
     188             :     // Don't create the McastTreeManager for the VPN table.
     189         100 :     if (IsMaster() && !server()->mvpn_ipv4_enable())
     190          20 :         return;
     191          80 :     assert(!tree_manager_);
     192          80 :     tree_manager_ = BgpStaticObjectFactory::Create<McastTreeManager>(this);
     193          80 :     tree_manager_->Initialize();
     194             : }
     195             : 
     196          80 : void ErmVpnTable::DestroyTreeManager() {
     197          80 :     assert(tree_manager_);
     198          80 :     tree_manager_->Terminate();
     199          80 :     delete tree_manager_;
     200          80 :     tree_manager_ = NULL;
     201          80 : }
     202             : 
     203           0 : McastTreeManager *ErmVpnTable::GetTreeManager() {
     204           0 :     return tree_manager_;
     205             : }
     206             : 
     207           0 : const McastTreeManager *ErmVpnTable::GetTreeManager() const {
     208           0 :     return tree_manager_;
     209             : }
     210             : 
     211         100 : void ErmVpnTable::set_routing_instance(RoutingInstance *rtinstance) {
     212         100 :     BgpTable::set_routing_instance(rtinstance);
     213         100 :     CreateTreeManager();
     214         100 :     CreateMvpnProjectManager();
     215         100 : }
     216             : 
     217         100 : void ErmVpnTable::CreateMvpnProjectManager() {
     218             :     // Don't create the MvpnProjectManager for the master table.
     219         100 :     if (!server()->mvpn_ipv4_enable() || IsMaster())
     220         100 :         return;
     221           0 :     assert(!mvpn_project_manager_);
     222           0 :     mvpn_project_manager_ = BgpStaticObjectFactory::Create<MvpnProjectManager>(this);
     223           0 :     mvpn_project_manager_->Initialize();
     224             : }
     225             : 
     226           0 : void ErmVpnTable::DestroyMvpnProjectManager() {
     227           0 :     assert(mvpn_project_manager_);
     228           0 :     mvpn_project_manager_->Terminate();
     229           0 :     delete mvpn_project_manager_;
     230           0 :     mvpn_project_manager_ = NULL;
     231           0 : }
     232             : 
     233        3553 : bool ErmVpnTable::IsMaster() const {
     234        3553 :     return routing_instance()->IsMasterRoutingInstance();
     235             : }
     236             : 
     237             : // Find or create the route.
     238           0 : ErmVpnRoute *ErmVpnTable::FindRoute(const ErmVpnPrefix &prefix) {
     239           0 :     ErmVpnRoute rt_key(prefix);
     240             :     DBTablePartition *rtp = static_cast<DBTablePartition *>(
     241           0 :         GetTablePartition(&rt_key));
     242           0 :     return static_cast<ErmVpnRoute *>(rtp->Find(&rt_key));
     243           0 : }
     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           0 : void ErmVpnTable::GetMvpnSourceAddress(ErmVpnRoute *ermvpn_rt,
     251             :                                        Ip4Address *address) const {
     252           0 :     if (mvpn_project_manager_)
     253           0 :         mvpn_project_manager_->GetMvpnSourceAddress(ermvpn_rt, address);
     254           0 : }
     255             : 
     256           1 : static void RegisterFactory() {
     257           1 :     DB::RegisterFactory("ermvpn.0", &ErmVpnTable::CreateTable);
     258           1 : }
     259             : 
     260             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14