LCOV - code coverage report
Current view: top level - bgp/inet6 - inet6_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 46 101 45.5 %
Date: 2026-11-23 02:04:52 Functions: 10 12 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/inet6/inet6_table.h"
       7             : 
       8             : #include "bgp/bgp_server.h"
       9             : #include "bgp/bgp_update.h"
      10             : #include "bgp/inet6vpn/inet6vpn_route.h"
      11             : #include "bgp/routing-instance/path_resolver.h"
      12             : #include "bgp/routing-instance/routing_instance.h"
      13             : 
      14         102 : Inet6Table::Inet6Table(DB *db, const std::string &name)
      15         102 :     : BgpTable(db, name) {
      16         102 : }
      17             : 
      18       41588 : size_t Inet6Table::HashFunction(const Inet6Prefix &prefix) {
      19       41588 :     const Ip6Address::bytes_type &addr_bytes = prefix.ToBytes();
      20       83182 :     return boost::hash_range(addr_bytes.begin(), addr_bytes.end());
      21             : }
      22             : 
      23        1688 : std::unique_ptr<DBEntry> Inet6Table::AllocEntry(const DBRequestKey *key) const {
      24        1688 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      25        1688 :     return std::unique_ptr<DBEntry> (new Inet6Route(rkey->prefix));
      26             : }
      27             : 
      28           0 : std::unique_ptr<DBEntry> Inet6Table::AllocEntryStr(
      29             :         const std::string &key_str) const {
      30           0 :     Inet6Prefix prefix = Inet6Prefix::FromString(key_str);
      31           0 :     return std::unique_ptr<DBEntry> (new Inet6Route(prefix));
      32             : }
      33             : 
      34       14425 : size_t Inet6Table::Hash(const DBEntry *entry) const {
      35       14425 :     const Inet6Route *route = static_cast<const Inet6Route *>(entry);
      36       14425 :     size_t value = HashFunction(route->GetPrefix());
      37       14426 :     return value % DB::PartitionCount();
      38             : }
      39             : 
      40        2428 : size_t Inet6Table::Hash(const DBRequestKey *key) const {
      41        2428 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      42        2428 :     size_t value = HashFunction(rkey->prefix);
      43        2428 :     return value % DB::PartitionCount();
      44             : }
      45             : 
      46        1404 : BgpRoute *Inet6Table::TableFind(DBTablePartition *partition,
      47             :                                 const DBRequestKey *key) {
      48        1404 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      49        1404 :     Inet6Route route(rkey->prefix);
      50        2808 :     return static_cast<BgpRoute *>(partition->Find(&route));
      51        1404 : }
      52             : 
      53         102 : DBTableBase *Inet6Table::CreateTable(DB *db, const std::string &name) {
      54         102 :     Inet6Table *table = new Inet6Table(db, name);
      55         102 :     table->Init();
      56         102 :     return table;
      57             : }
      58             : 
      59           0 : BgpRoute *Inet6Table::RouteReplicate(BgpServer *server, BgpTable *src_table,
      60             :         BgpRoute *src_rt, const BgpPath *path, ExtCommunityPtr community) {
      61           0 :     assert((src_table->family()  == Address::INET6) ||
      62             :            (src_table->family()  == Address::INET6VPN));
      63             : 
      64           0 :     Inet6Route *source = dynamic_cast<Inet6Route *>(src_rt);
      65             : 
      66           0 :     RouteDistinguisher rd;
      67             : 
      68           0 :     boost::scoped_ptr<Inet6Prefix> prefix;
      69           0 :     if (source) {
      70           0 :         prefix.reset(new Inet6Prefix(source->GetPrefix().ip6_addr(),
      71           0 :                                      source->GetPrefix().prefixlen()));
      72             :     } else {
      73           0 :         Inet6VpnRoute *vpn_route = dynamic_cast<Inet6VpnRoute *> (src_rt);
      74           0 :         assert(vpn_route);
      75           0 :         rd = vpn_route->GetPrefix().route_distinguisher();
      76           0 :         prefix.reset(new Inet6Prefix(vpn_route->GetPrefix().addr(),
      77           0 :                                      vpn_route->GetPrefix().prefixlen()));
      78             :     }
      79             : 
      80           0 :     Inet6Route route(*prefix);
      81             :     DBTablePartition *partition =
      82           0 :         static_cast<DBTablePartition *>(GetTablePartition(&route));
      83           0 :     BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&route));
      84           0 :     if (dest_route == NULL) {
      85           0 :         dest_route = new Inet6Route(route.GetPrefix());
      86           0 :         partition->Add(dest_route);
      87             :     } else {
      88           0 :         dest_route->ClearDelete();
      89             :     }
      90             : 
      91             :     // Replace the extended community with the one provided.
      92           0 :     BgpAttrDB *attr_db = server->attr_db();
      93             :     BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(path->GetAttr(),
      94           0 :                                                                 community);
      95             : 
      96           0 :     if (!source) {
      97           0 :         new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
      98             :     }
      99             : 
     100             :     // Check whether there's already a path with the given peer and path id.
     101             :     BgpPath *dest_path =
     102           0 :         dest_route->FindSecondaryPath(src_rt, path->GetSource(),
     103             :                                       path->GetPeer(), path->GetPathId());
     104           0 :     if (dest_path != NULL) {
     105           0 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     106           0 :             (path->GetFlags() != dest_path->GetFlags()) ||
     107           0 :             (path->GetLabel() != dest_path->GetLabel())) {
     108             :             // Update Attributes and notify (if needed)
     109           0 :             if (dest_path->NeedsResolution()) {
     110           0 :                 path_resolver()->StopPathResolution(partition->index(),
     111             :                                                     dest_path);
     112             :             }
     113           0 :             assert(dest_route->RemoveSecondaryPath(src_rt, path->GetSource(),
     114             :                 path->GetPeer(), path->GetPathId()));
     115             :         } else {
     116           0 :             return dest_route;
     117             :         }
     118             :     }
     119             : 
     120             :     BgpSecondaryPath *replicated_path =
     121           0 :         new BgpSecondaryPath(path->GetPeer(), path->GetPathId(),
     122           0 :             path->GetSource(), new_attr, path->GetFlags(), path->GetLabel());
     123           0 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     124             : 
     125             :     // For VPN to VRF replication, start path resolution if fast convergence is
     126             :     // enabled and update path flag to indicate need for resolution.
     127           0 :     if (!source && (server->IsNextHopCheckEnabled()) &&
     128           0 :         (replicated_path->GetSource() == BgpPath::BGP_XMPP)) {
     129           0 :         Address::Family family = replicated_path->GetAttr()->nexthop_family();
     130           0 :         RoutingInstanceMgr *mgr = server->routing_instance_mgr();
     131           0 :         RoutingInstance *master_ri = mgr->GetDefaultRoutingInstance();
     132           0 :         BgpTable *table = master_ri->GetTable(family);
     133           0 :         replicated_path->SetResolveNextHop();
     134           0 :         path_resolver()->StartPathResolution(dest_route, replicated_path,
     135             :                                              table);
     136             :     }
     137             : 
     138           0 :     dest_route->InsertPath(replicated_path);
     139             : 
     140             :     // Notify the route even if the best path may not have changed. For XMPP
     141             :     // peers, we support sending multiple ECMP next-hops for a single route.
     142             :     //
     143             :     // TODO(ananth): Can be optimized for changes that does not result in
     144             :     // any change to ECMP list
     145           0 :     partition->Notify(dest_route);
     146             : 
     147           0 :     return dest_route;
     148           0 : }
     149             : 
     150        3076 : bool Inet6Table::Export(RibOut *ribout, Route *route, const RibPeerSet &peerset,
     151             :         UpdateInfoSList &uinfo_slist) {
     152        3076 :     BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     153        3076 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     154        3078 :     if (!uinfo) {
     155        1259 :         return false;
     156             :     }
     157             : 
     158        1819 :     if (ribout->ExportPolicy().encoding == RibExportPolicy::BGP) {
     159        1274 :         BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
     160             :         // Strip ExtCommunity.
     161        1274 :         if (uinfo->roattr.attr()->ext_community()) {
     162             :             BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
     163         812 :                 uinfo->roattr.attr(), NULL);
     164         812 :             uinfo->roattr.set_attr(this, new_attr);
     165         812 :         }
     166             : 
     167             :         // Strip OriginVnPath.
     168        1274 :         if (uinfo->roattr.attr()->origin_vn_path()) {
     169             :             BgpAttrPtr new_attr = attr_db->ReplaceOriginVnPathAndLocate(
     170           0 :                 uinfo->roattr.attr(), NULL);
     171           0 :             uinfo->roattr.set_attr(this, new_attr);
     172           0 :         }
     173             :     }
     174        1819 :     uinfo_slist->push_front(*uinfo);
     175             : 
     176        1819 :     return true;
     177             : }
     178             : 
     179          34 : PathResolver *Inet6Table::CreatePathResolver() {
     180          34 :     if (routing_instance()->IsMasterRoutingInstance())
     181           0 :         return NULL;
     182          34 :     return (new PathResolver(this));
     183             : }
     184             : 
     185           1 : static void RegisterFactory() {
     186           1 :     DB::RegisterFactory("inet6.0", &Inet6Table::CreateTable);
     187           1 : }
     188             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14