LCOV - code coverage report
Current view: top level - bgp/inet6vpn - inet6vpn_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 71 92 77.2 %
Date: 2026-10-05 02:12:29 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "bgp/inet6vpn/inet6vpn_table.h"
       6             : 
       7             : #include "bgp/ipeer.h"
       8             : #include "bgp/bgp_server.h"
       9             : #include "bgp/bgp_peer.h"
      10             : #include "bgp/bgp_update.h"
      11             : #include "bgp/inet6/inet6_table.h"
      12             : #include "bgp/routing-instance/routing_instance.h"
      13             : #include "net/rd.h"
      14             : 
      15         418 : Inet6VpnTable::Inet6VpnTable(DB *db, const std::string &name)
      16         418 :     : BgpTable(db, name) {
      17         418 : }
      18             : 
      19             : std::unique_ptr<DBEntry>
      20       17831 : Inet6VpnTable::AllocEntry(const DBRequestKey *key) const {
      21       17831 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      22       17831 :     return std::unique_ptr<DBEntry>(new Inet6VpnRoute(pfxkey->prefix));
      23             : }
      24             : 
      25             : std::unique_ptr<DBEntry>
      26           0 : Inet6VpnTable::AllocEntryStr(const std::string &key_str) const {
      27           0 :     Inet6VpnPrefix vpn_prefix = Inet6VpnPrefix::FromString(key_str);
      28           0 :     return std::unique_ptr<DBEntry> (new Inet6VpnRoute(vpn_prefix));
      29             : }
      30             : 
      31      184908 : size_t Inet6VpnTable::Hash(const DBEntry *entry) const {
      32      184908 :     const Inet6VpnRoute *vpn_route = static_cast<const Inet6VpnRoute *>(entry);
      33      184908 :     const Inet6VpnPrefix &vpn_prefix = vpn_route->GetPrefix();
      34      184903 :     Inet6Prefix prefix(vpn_prefix.addr(), vpn_prefix.prefixlen());
      35      184832 :     size_t value = Inet6Table::HashFunction(prefix);
      36      184749 :     return value % DB::PartitionCount();
      37             : }
      38             : 
      39        8064 : size_t Inet6VpnTable::Hash(const DBRequestKey *key) const {
      40        8064 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      41        8064 :     Inet6Prefix prefix(rkey->prefix.addr(), rkey->prefix.prefixlen());
      42        8064 :     size_t value = Inet6Table::HashFunction(prefix);
      43        8064 :     return value % DB::PartitionCount();
      44             : }
      45             : 
      46        8063 : BgpRoute *Inet6VpnTable::TableFind(DBTablePartition *rtp,
      47             :                                    const DBRequestKey *prefix) {
      48        8063 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      49        8063 :     Inet6VpnRoute vpn_route(pfxkey->prefix);
      50       16121 :     return static_cast<BgpRoute *>(rtp->Find(&vpn_route));
      51        8063 : }
      52             : 
      53         418 : DBTableBase *Inet6VpnTable::CreateTable(DB *db, const std::string &name) {
      54         418 :     Inet6VpnTable *table = new Inet6VpnTable(db, name);
      55         418 :     table->Init();
      56         418 :     return table;
      57             : }
      58             : 
      59        1024 : static RouteDistinguisher GenerateDistinguisher(
      60             :         const BgpTable *src_table, const BgpPath *src_path) {
      61        1024 :     const RouteDistinguisher &source_rd = src_path->GetAttr()->source_rd();
      62        1024 :     if (!src_path->GetPeer() || !(src_path->GetPeer()->IsRouterTypeBGPaaS())){
      63        1024 :         if (!source_rd.IsZero())
      64        1024 :         return source_rd;
      65             : 
      66           0 :         assert(!src_path->GetPeer() || !src_path->GetPeer()->IsXmppPeer());
      67           0 :         const RoutingInstance *src_instance = src_table->routing_instance();
      68           0 :         return *src_instance->GetRD();
      69             :     }
      70           0 :     uint16_t peer_port_id = 0;
      71           0 :     as_t asn_num = src_path->GetAttr()->neighbor_as();
      72             : 
      73           0 :     std::string ri_name = src_table->routing_instance()->name();
      74           0 :     for (const BgpPeer *peer = src_path->GetPeer()->server()->FindNextPeer(); peer != NULL;
      75           0 :          peer = src_path->GetPeer()->server()->FindNextPeer(peer->peer_name())) {
      76           0 :             if ((peer->peer_name().find(ri_name)!= std::string::npos)&&
      77           0 :             (peer->peer_address_string().find(src_path->GetPeer()->ToString())!= std::string::npos)){
      78           0 :                 peer_port_id = peer->peer_port();
      79           0 :                 asn_num = peer->peer_as();
      80           0 :                 break;
      81             :             }
      82             :     }
      83           0 :     RouteDistinguisher new_source_rd = RouteDistinguisher(true, asn_num, peer_port_id);
      84           0 :     return new_source_rd;
      85           0 : }
      86             : 
      87        1024 : BgpRoute *Inet6VpnTable::RouteReplicate(BgpServer *server, BgpTable *src_table,
      88             :         BgpRoute *source_rt, const BgpPath *src_path,
      89             :         ExtCommunityPtr community) {
      90        1024 :     assert(src_table->family()  == Address::INET6);
      91             : 
      92        1024 :     Inet6Route *src_rt = dynamic_cast<Inet6Route *>(source_rt);
      93        1024 :     assert(src_rt);
      94             : 
      95        1024 :     const RouteDistinguisher &rd = GenerateDistinguisher(src_table, src_path);
      96        1024 :     Inet6VpnPrefix vpn_prefix(rd, src_rt->GetPrefix().ip6_addr(),
      97        2048 :                               src_rt->GetPrefix().prefixlen());
      98             : 
      99        1024 :     Inet6VpnRoute vpn_route(vpn_prefix);
     100             :     DBTablePartition *partition =
     101        1024 :         static_cast<DBTablePartition *>(GetTablePartition(&vpn_route));
     102        1024 :     BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&vpn_route));
     103        1024 :     if (dest_route == NULL) {
     104         512 :         dest_route = new Inet6VpnRoute(vpn_prefix);
     105         512 :         partition->Add(dest_route);
     106             :     } else {
     107         512 :         dest_route->ClearDelete();
     108             :     }
     109             : 
     110        1024 :     BgpAttrDB *attr_db = server->attr_db();
     111             : 
     112             :     BgpAttrPtr new_attr =
     113             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     114        1024 :                                                         community);
     115        1024 :     new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
     116             : 
     117             :     // Check whether there's already a path with the given peer and path id.
     118             :     BgpPath *dest_path =
     119        1024 :         dest_route->FindSecondaryPath(source_rt, src_path->GetSource(),
     120             :                                       src_path->GetPeer(),
     121             :                                       src_path->GetPathId());
     122        1024 :     if (dest_path != NULL) {
     123        1024 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     124        1024 :             (src_path->GetFlags() != dest_path->GetFlags()) ||
     125           0 :             (src_path->GetLabel() != dest_path->GetLabel())) {
     126             :             // Update Attributes and notify (if needed)
     127         512 :             assert(dest_route->RemoveSecondaryPath(source_rt,
     128             :                                 src_path->GetSource(), src_path->GetPeer(),
     129             :                                 src_path->GetPathId()));
     130             :         } else {
     131           0 :             return dest_route;
     132             :         }
     133             :     }
     134             : 
     135             :     // Create replicated path and insert it on the route
     136             :     BgpSecondaryPath *replicated_path =
     137        1024 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     138        1024 :                              src_path->GetSource(), new_attr,
     139        1024 :                              src_path->GetFlags(), src_path->GetLabel());
     140        1024 :     replicated_path->SetReplicateInfo(src_table, source_rt);
     141        1024 :     dest_route->InsertPath(replicated_path);
     142             : 
     143             :     // Always trigger notification.
     144        1024 :     partition->Notify(dest_route);
     145             : 
     146        1024 :     return dest_route;
     147        1024 : }
     148             : 
     149       13597 : bool Inet6VpnTable::Export(RibOut *ribout, Route *route,
     150             :         const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     151       13597 :     BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     152       13597 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     153       13611 :     if (!uinfo) {
     154        6439 :         return false;
     155             :     }
     156        7172 :     uinfo_slist->push_front(*uinfo);
     157             : 
     158        7172 :     return true;
     159             : }
     160             : 
     161           3 : static void RegisterFactory() {
     162           3 :     DB::RegisterFactory("bgp.l3vpn-inet6.0", &Inet6VpnTable::CreateTable);
     163           3 : }
     164             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14