LCOV - code coverage report
Current view: top level - bgp/inet - inet_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 127 237 53.6 %
Date: 2026-11-16 02:06:09 Functions: 15 18 83.3 %
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/inet/inet_table.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include "base/task_annotations.h"
      11             : #include "bgp/bgp_server.h"
      12             : #include "bgp/bgp_update.h"
      13             : #include "bgp/extended-community/source_as.h"
      14             : #include "bgp/extended-community/vrf_route_import.h"
      15             : #include "bgp/l3vpn/inetvpn_route.h"
      16             : #include "bgp/l3vpn/inetvpn_table.h"
      17             : #include "bgp/routing-instance/path_resolver.h"
      18             : #include "bgp/routing-instance/routing_instance.h"
      19             : 
      20             : using std::unique_ptr;
      21             : using std::string;
      22             : 
      23         120 : InetTable::InetTable(DB *db, const string &name)
      24         120 :     : BgpTable(db, name) {
      25         120 :     family_ = (name.at(name.length()-1) == '3') ?
      26             :         Address::INETMPLS : Address::INET;
      27         120 : }
      28             : 
      29      343277 : size_t InetTable::HashFunction(const Ip4Prefix &prefix) {
      30      343277 :     return boost::hash_value(prefix.ip4_addr().to_ulong());
      31             : }
      32             : 
      33       16638 : unique_ptr<DBEntry> InetTable::AllocEntry(const DBRequestKey *key) const {
      34       16638 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      35       16638 :     return unique_ptr<DBEntry>(new InetRoute(pfxkey->prefix));
      36             : }
      37             : 
      38           0 : unique_ptr<DBEntry> InetTable::AllocEntryStr(const string &key_str) const {
      39           0 :     Ip4Prefix prefix = Ip4Prefix::FromString(key_str);
      40           0 :     return unique_ptr<DBEntry>(new InetRoute(prefix));
      41             : }
      42             : 
      43      243395 : size_t InetTable::Hash(const DBEntry *entry) const {
      44      243395 :     const InetRoute *rt_entry = static_cast<const InetRoute *>(entry);
      45      243395 :     size_t value = HashFunction(rt_entry->GetPrefix());
      46      242697 :     return value % DB::PartitionCount();
      47             : }
      48             : 
      49         704 : size_t InetTable::Hash(const DBRequestKey *key) const {
      50         704 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      51         704 :     size_t value = HashFunction(rkey->prefix);
      52         704 :     return value % DB::PartitionCount();
      53             : }
      54             : 
      55         704 : BgpRoute *InetTable::TableFind(DBTablePartition *rtp,
      56             :         const DBRequestKey *prefix) {
      57         704 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      58         704 :     InetRoute rt_key(pfxkey->prefix);
      59        1408 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
      60         704 : }
      61             : 
      62         120 : DBTableBase *InetTable::CreateTable(DB *db, const string &name) {
      63         120 :     InetTable *table = new InetTable(db, name);
      64         120 :     table->Init();
      65         120 :     return table;
      66             : }
      67             : 
      68       21171 : BgpRoute *InetTable::RouteReplicate(BgpServer *server,
      69             :         BgpTable *src_table, BgpRoute *src_rt, const BgpPath *path,
      70             :         ExtCommunityPtr community) {
      71       21171 :     InetRoute *inet= dynamic_cast<InetRoute *> (src_rt);
      72             : 
      73       21171 :     boost::scoped_ptr<Ip4Prefix> inet_prefix;
      74       21174 :     RouteDistinguisher rd;
      75             : 
      76       21175 :     if (inet) {
      77        2303 :         inet_prefix.reset(new Ip4Prefix(inet->GetPrefix().ip4_addr(),
      78        1151 :                                       inet->GetPrefix().prefixlen()));
      79             :     } else {
      80       20023 :         InetVpnRoute *inetvpn = dynamic_cast<InetVpnRoute *> (src_rt);
      81       20023 :         assert(inetvpn);
      82       20023 :         rd = inetvpn->GetPrefix().route_distinguisher();
      83       40045 :         inet_prefix.reset(new Ip4Prefix(inetvpn->GetPrefix().addr(),
      84       20022 :                                   inetvpn->GetPrefix().prefixlen()));
      85             :     }
      86             : 
      87       21171 :     InetRoute rt_key(*inet_prefix);
      88             :     DBTablePartition *rtp =
      89       21173 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
      90       21159 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
      91       21174 :     if (dest_route == NULL) {
      92       12799 :         dest_route = new InetRoute(rt_key.GetPrefix());
      93       12795 :         rtp->Add(dest_route);
      94             :     } else {
      95        8375 :         dest_route->ClearDelete();
      96             :     }
      97             : 
      98       21168 :     BgpAttrDB *attr_db = server->attr_db();
      99             :     BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(path->GetAttr(),
     100       21165 :                                                                 community);
     101             : 
     102             :     // Set the RD attr if route is replicated from vpn table
     103       21175 :     if (!inet) {
     104       20023 :         new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
     105             :     }
     106             : 
     107             :     // Check whether there's already a path with the given peer and path id.
     108       21175 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     109             :                                           path->GetSource(), path->GetPeer(),
     110             :                                           path->GetPathId());
     111       21172 :     uint32_t prev_flags = 0 , prev_label = 0;
     112       21172 :     const BgpAttr *prev_attr = NULL;
     113       21172 :     if (dest_path != NULL) {
     114        8372 :         prev_flags = dest_path->GetFlags();
     115        8372 :         prev_label = dest_path->GetLabel();
     116        8372 :         prev_attr = dest_path->GetOriginalAttr();
     117       16744 :         if ((new_attr != prev_attr) ||
     118       16744 :             (path->GetFlags() != prev_flags) ||
     119        7476 :             (path->GetLabel() != prev_label)) {
     120             :             // Update Attributes and notify (if needed)
     121         896 :             if (dest_path->NeedsResolution()) {
     122           0 :                 path_resolver()->StopPathResolution(rtp->index(), dest_path);
     123             :             }
     124         896 :             assert(dest_route->RemoveSecondaryPath(src_rt, path->GetSource(),
     125             :                         path->GetPeer(), path->GetPathId()));
     126             :         } else {
     127        7476 :             return dest_route;
     128             :         }
     129             :     }
     130             : 
     131             :     BgpSecondaryPath *replicated_path =
     132       13696 :         new BgpSecondaryPath(path->GetPeer(), path->GetPathId(),
     133       13696 :             path->GetSource(), new_attr, path->GetFlags(), path->GetLabel());
     134       13696 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     135             : 
     136             :     // For VPN to VRF replication, start path resolution if fast convergence is
     137             :     // enabled and update path flag to indicate need for resolution.
     138       13696 :     if (!inet && (server->IsNextHopCheckEnabled()) &&
     139           0 :         (replicated_path->GetSource() == BgpPath::BGP_XMPP)) {
     140           0 :         Address::Family family = replicated_path->GetAttr()->nexthop_family();
     141           0 :         RoutingInstanceMgr *mgr = server->routing_instance_mgr();
     142           0 :         RoutingInstance *master_ri = mgr->GetDefaultRoutingInstance();
     143           0 :         BgpTable *table = master_ri->GetTable(family);
     144           0 :         replicated_path->SetResolveNextHop();
     145           0 :         path_resolver()->StartPathResolution(dest_route, replicated_path,
     146             :                                              table);
     147             :     }
     148             : 
     149       13696 :     dest_route->InsertPath(replicated_path);
     150             : 
     151             :     // BgpRoute::InsertPath() triggers routing policies processing, which may
     152             :     // change the flags (namely, RoutingPolicyReject). So we check the flags
     153             :     // once again and only notify the partition if the new path is still
     154             :     // different after routing policies were applied.
     155       13690 :     bool notify = true;
     156       13690 :     if (dest_path != NULL) {
     157        1790 :         if ((replicated_path->GetOriginalAttr() == prev_attr) &&
     158        1790 :             (replicated_path->GetFlags() == prev_flags) &&
     159           0 :             (replicated_path->GetLabel() == prev_label))
     160           0 :             notify = false;
     161             :     }
     162             : 
     163             :     // Notify the route even if the best path may not have changed. For XMPP
     164             :     // peers, we support sending multiple ECMP next-hops for a single route.
     165             :     //
     166             :     // TODO(ananth): Can be optimized for changes that does not result in
     167             :     // any change to ECMP list.
     168       13690 :     if (notify)
     169       13691 :         rtp->Notify(dest_route);
     170             : 
     171       13691 :     return dest_route;
     172       21167 : }
     173             : 
     174       14124 : bool InetTable::Export(RibOut *ribout, Route *route, const RibPeerSet &peerset,
     175             :         UpdateInfoSList &uinfo_slist) {
     176       14124 :     BgpRoute *bgp_route = static_cast<BgpRoute *>(route);
     177             : 
     178       14124 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     179       14158 :     if (!uinfo)
     180           0 :         return false;
     181             : 
     182       14158 :     if (ribout->ExportPolicy().encoding == RibExportPolicy::BGP) {
     183           0 :         BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
     184           0 :         UpdateExtendedCommunity(&uinfo->roattr);
     185             : 
     186             :         // Strip OriginVnPath.
     187           0 :         if (uinfo->roattr.attr()->origin_vn_path()) {
     188             :             BgpAttrPtr new_attr = attr_db->ReplaceOriginVnPathAndLocate(
     189           0 :                 uinfo->roattr.attr(), NULL);
     190           0 :             uinfo->roattr.set_attr(this, new_attr);
     191           0 :         }
     192             :     }
     193       14157 :     uinfo_slist->push_front(*uinfo);
     194             : 
     195       14151 :     return true;
     196             : }
     197             : 
     198             : // Strip all extended-communities except OriginVN.
     199           0 : void InetTable::UpdateExtendedCommunity(RibOutAttr *roattr) {
     200           0 :     ExtCommunityPtr ext_commp = roattr->attr()->ext_community();
     201           0 :     if (!ext_commp)
     202           0 :         return;
     203             : 
     204             :     // Retrieve any origin_vn already present.
     205           0 :     ExtCommunity::ExtCommunityValue const *origin_vnp = NULL;
     206           0 :     BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     207             :                   ext_commp->communities()) {
     208           0 :         if (!ExtCommunity::is_origin_vn(comm))
     209           0 :             continue;
     210           0 :         origin_vnp = &comm;
     211           0 :         break;
     212             :     }
     213             : 
     214           0 :     BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
     215             : 
     216             :     // If there is no origin-vn, then remove all other extended communities.
     217           0 :     if (!origin_vnp) {
     218             :         BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
     219           0 :             roattr->attr(), NULL);
     220           0 :         roattr->set_attr(this, new_attr);
     221           0 :         return;
     222           0 :     }
     223             : 
     224             :     // Remove all communities other than OriginVN by replacing all of the
     225             :     // extended-community with just OriginVN.
     226           0 :     if (ext_commp->communities().size() > 1) {
     227           0 :         ExtCommunity::ExtCommunityList list;
     228           0 :         list.push_back(*origin_vnp);
     229             :         ExtCommunityDB *extcomm_db =
     230           0 :             routing_instance()->server()->extcomm_db();
     231           0 :         ext_commp = extcomm_db->AppendAndLocate(NULL, list);
     232             :         BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
     233           0 :             roattr->attr(), ext_commp);
     234           0 :         roattr->set_attr(this, new_attr);
     235           0 :     }
     236           0 : }
     237             : 
     238             : // Attach OriginVN extended-community from inetvpn path attribute if present
     239             : // into inet route path attribute.
     240           0 : BgpAttrPtr InetTable::UpdateAttributes(const BgpAttrPtr inetvpn_attrp,
     241             :                                        const BgpAttrPtr inet_attrp) {
     242           0 :     BgpServer *server = routing_instance()->server();
     243             : 
     244             :     // Check if origin-vn path attribute in inet.0 table path is identical to
     245             :     // what is in inetvpn table path.
     246           0 :     ExtCommunity::ExtCommunityValue const *inetvpn_rt_origin_vn = NULL;
     247           0 :     if (inetvpn_attrp && inetvpn_attrp->ext_community()) {
     248           0 :         BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     249             :                     inetvpn_attrp->ext_community()->communities()) {
     250           0 :             if (!ExtCommunity::is_origin_vn(comm))
     251           0 :                 continue;
     252           0 :             inetvpn_rt_origin_vn = &comm;
     253           0 :             break;
     254             :         }
     255             :     }
     256             : 
     257           0 :     ExtCommunity::ExtCommunityValue const *inet_rt_origin_vn = NULL;
     258           0 :     if (inet_attrp && inet_attrp->ext_community()) {
     259           0 :         BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
     260             :                     inet_attrp->ext_community()->communities()) {
     261           0 :             if (!ExtCommunity::is_origin_vn(comm))
     262           0 :                 continue;
     263           0 :             inet_rt_origin_vn = &comm;
     264           0 :             break;
     265             :         }
     266             :     }
     267             : 
     268             :     // Ignore if there is no change.
     269           0 :     if (inetvpn_rt_origin_vn == inet_rt_origin_vn)
     270           0 :         return inet_attrp;
     271             : 
     272             :     // Update/Delete inet route attributes with updated OriginVn community.
     273           0 :     ExtCommunityPtr new_ext_community;
     274           0 :     if (!inetvpn_rt_origin_vn) {
     275           0 :         new_ext_community = server->extcomm_db()->RemoveOriginVnAndLocate(
     276           0 :             inet_attrp->ext_community());
     277             :     } else {
     278           0 :         new_ext_community = server->extcomm_db()->ReplaceOriginVnAndLocate(
     279           0 :             inet_attrp->ext_community(), *inetvpn_rt_origin_vn);
     280             :     }
     281             : 
     282             :     return server->attr_db()->ReplaceExtCommunityAndLocate(inet_attrp.get(),
     283           0 :                                                            new_ext_community);
     284           0 : }
     285             : 
     286         320 : BgpAttrPtr InetTable::GetAttributes(BgpRoute *rt,
     287             :                                     BgpAttrPtr inet_attrp, const IPeer *peer) {
     288         320 :     CHECK_CONCURRENCY("db::DBTable");
     289             : 
     290         320 :     BgpAttrPtr attrp = GetFabricAttributes(rt, inet_attrp, peer);
     291         640 :     return GetMvpnAttributes(attrp);
     292         320 : }
     293             : 
     294         320 : BgpAttrPtr InetTable::GetMvpnAttributes(BgpAttrPtr attrp) {
     295         320 :     BgpServer *server = routing_instance()->server();
     296         320 :     if (server->mvpn_ipv4_enable()) {
     297             :         VrfRouteImport vit(server->bgp_identifier(),
     298           0 :                            routing_instance()->index());
     299             :         ExtCommunityPtr ext =
     300             :             server->extcomm_db()->ReplaceVrfRouteImportAndLocate(
     301           0 :                 attrp->ext_community(), vit.GetExtCommunity());
     302           0 :         SourceAs sas(server->autonomous_system(), 0);
     303           0 :         ext = server->extcomm_db()->ReplaceSourceASAndLocate(ext.get(),
     304           0 :                     sas.GetExtCommunity());
     305             :         BgpAttrPtr new_attr =
     306           0 :             server->attr_db()->ReplaceExtCommunityAndLocate(attrp.get(), ext);
     307           0 :         return new_attr;
     308           0 :     }
     309         320 :     return attrp;
     310             : }
     311             : 
     312             : // Given an inet prefix, update OriginVN with corresponding inetvpn route's
     313             : // path attribute.
     314         320 : BgpAttrPtr InetTable::GetFabricAttributes(BgpRoute *rt,
     315             :                                     BgpAttrPtr inet_attrp, const IPeer *peer) {
     316             : 
     317         320 :     if (!routing_instance()->IsMasterRoutingInstance())
     318         256 :         return inet_attrp;
     319          64 :     if (!inet_attrp || inet_attrp->source_rd().IsZero())
     320          64 :         return inet_attrp;
     321             : 
     322           0 :     const InetRoute *inet_rt = dynamic_cast<InetRoute *>(rt);
     323           0 :     const Ip4Prefix inet_prefix = inet_rt->GetPrefix();
     324           0 :     RequestKey inet_rt_key(inet_prefix, NULL);
     325           0 :     DBTablePartition *inet_partition = dynamic_cast<DBTablePartition *>(
     326           0 :         GetTablePartition(&inet_rt_key));
     327             : 
     328           0 :     InetVpnTable *inetvpn_table = dynamic_cast<InetVpnTable *>(
     329           0 :         routing_instance()->GetTable(Address::INETVPN));
     330           0 :     assert(inetvpn_table);
     331             :     InetVpnPrefix inetvpn_prefix(inet_attrp->source_rd(),
     332           0 :                                  inet_prefix.ip4_addr(),
     333           0 :                                  inet_prefix.prefixlen());
     334           0 :     InetVpnTable::RequestKey inetvpn_rt_key(inetvpn_prefix, NULL);
     335           0 :     DBTablePartition *inetvpn_partition = dynamic_cast<DBTablePartition *>(
     336           0 :         inetvpn_table->GetTablePartition(&inetvpn_rt_key));
     337             : 
     338             :     // Assert that the partition indicies are identical. This is a MUST
     339             :     // requirement as we need to peek into tables across different families.
     340           0 :     assert(inet_partition->index() == inetvpn_partition->index());
     341           0 :     InetVpnRoute *inetvpn_route = dynamic_cast<InetVpnRoute *>(
     342           0 :         inetvpn_table->TableFind(inetvpn_partition, &inetvpn_rt_key));
     343           0 :     if (!inetvpn_route)
     344           0 :         return inet_attrp;
     345           0 :     BgpPath *inetvpn_path = inetvpn_route->FindPath(peer, true);
     346           0 :     if (!inetvpn_path)
     347           0 :         return inet_attrp;
     348           0 :     return UpdateAttributes(inetvpn_path->GetAttr(), inet_attrp);
     349           0 : }
     350             : 
     351             : // Update inet route path attributes with OriginVN from corresponding inetvpn
     352             : // route path attribute.
     353         384 : void InetTable::UpdateRoute(const InetVpnPrefix &inetvpn_prefix,
     354             :                             const IPeer *peer, BgpAttrPtr inetvpn_attrp) {
     355         384 :     CHECK_CONCURRENCY("db::DBTable");
     356         384 :     assert(routing_instance()->IsMasterRoutingInstance());
     357             : 
     358             :     // Check if a route is present in inet.0 table for this prefix.
     359         384 :     Ip4Prefix inet_prefix(inetvpn_prefix.addr(), inetvpn_prefix.prefixlen());
     360         384 :     InetTable::RequestKey inet_rt_key(inet_prefix, NULL);
     361         384 :     DBTablePartition *inet_partition = dynamic_cast<DBTablePartition *>(
     362         384 :         GetTablePartition(&inet_rt_key));
     363             : 
     364         384 :     InetVpnTable *inetvpn_table = dynamic_cast<InetVpnTable *>(
     365         384 :         routing_instance()->GetTable(Address::INETVPN));
     366         384 :     assert(inetvpn_table);
     367         384 :     InetVpnTable::RequestKey inetvpn_rt_key(inetvpn_prefix, NULL);
     368             :     DBTablePartition *inetvpn_partition =
     369         384 :         static_cast<DBTablePartition *>(inetvpn_table->GetTablePartition(
     370             :             &inetvpn_rt_key));
     371         384 :     assert(inet_partition->index() == inetvpn_partition->index());
     372             : 
     373         384 :     InetRoute *inet_route = dynamic_cast<InetRoute *>(
     374         384 :         TableFind(inet_partition, &inet_rt_key));
     375         384 :     if (!inet_route)
     376         288 :         return;
     377             : 
     378          96 :     BgpPath *inet_path = inet_route->FindPath(peer);
     379          96 :     if (!inet_path)
     380           0 :         return;
     381          96 :     BgpAttrPtr inet_attrp = inet_path->GetAttr();
     382          96 :     if (!inet_attrp)
     383           0 :         return;
     384             : 
     385             :     // Bail if the RDs do not match.
     386          96 :     if (!(inet_attrp->source_rd() == inetvpn_prefix.route_distinguisher())) {
     387          96 :         return;
     388             :     }
     389           0 :     BgpAttrPtr new_inet_attrp = UpdateAttributes(inetvpn_attrp, inet_attrp);
     390           0 :     if (new_inet_attrp == inet_attrp)
     391           0 :         return;
     392             : 
     393             :     // Update route with OriginVN path attribute.
     394           0 :     inet_path->SetAttr(new_inet_attrp, inet_path->GetOriginalAttr());
     395           0 :     inet_route->Notify();
     396         864 : }
     397             : 
     398          80 : PathResolver *InetTable::CreatePathResolver() {
     399          80 :     if (routing_instance()->IsMasterRoutingInstance())
     400           0 :         return NULL;
     401          80 :     return (new PathResolver(this));
     402             : }
     403             : 
     404           1 : static void RegisterFactory() {
     405           1 :     DB::RegisterFactory("inet.0", &InetTable::CreateTable);
     406           1 :     DB::RegisterFactory("inet.3", &InetTable::CreateTable);
     407           1 : }
     408             : 
     409             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14