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: 10 92 10.9 %
Date: 2026-09-21 02:11:39 Functions: 3 11 27.3 %
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          32 : Inet6VpnTable::Inet6VpnTable(DB *db, const std::string &name)
      16          32 :     : BgpTable(db, name) {
      17          32 : }
      18             : 
      19             : std::unique_ptr<DBEntry>
      20           0 : Inet6VpnTable::AllocEntry(const DBRequestKey *key) const {
      21           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      22           0 :     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           0 : size_t Inet6VpnTable::Hash(const DBEntry *entry) const {
      32           0 :     const Inet6VpnRoute *vpn_route = static_cast<const Inet6VpnRoute *>(entry);
      33           0 :     const Inet6VpnPrefix &vpn_prefix = vpn_route->GetPrefix();
      34           0 :     Inet6Prefix prefix(vpn_prefix.addr(), vpn_prefix.prefixlen());
      35           0 :     size_t value = Inet6Table::HashFunction(prefix);
      36           0 :     return value % DB::PartitionCount();
      37             : }
      38             : 
      39           0 : size_t Inet6VpnTable::Hash(const DBRequestKey *key) const {
      40           0 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      41           0 :     Inet6Prefix prefix(rkey->prefix.addr(), rkey->prefix.prefixlen());
      42           0 :     size_t value = Inet6Table::HashFunction(prefix);
      43           0 :     return value % DB::PartitionCount();
      44             : }
      45             : 
      46           0 : BgpRoute *Inet6VpnTable::TableFind(DBTablePartition *rtp,
      47             :                                    const DBRequestKey *prefix) {
      48           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      49           0 :     Inet6VpnRoute vpn_route(pfxkey->prefix);
      50           0 :     return static_cast<BgpRoute *>(rtp->Find(&vpn_route));
      51           0 : }
      52             : 
      53          32 : DBTableBase *Inet6VpnTable::CreateTable(DB *db, const std::string &name) {
      54          32 :     Inet6VpnTable *table = new Inet6VpnTable(db, name);
      55          32 :     table->Init();
      56          32 :     return table;
      57             : }
      58             : 
      59           0 : static RouteDistinguisher GenerateDistinguisher(
      60             :         const BgpTable *src_table, const BgpPath *src_path) {
      61           0 :     const RouteDistinguisher &source_rd = src_path->GetAttr()->source_rd();
      62           0 :     if (!src_path->GetPeer() || !(src_path->GetPeer()->IsRouterTypeBGPaaS())){
      63           0 :         if (!source_rd.IsZero())
      64           0 :         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           0 : BgpRoute *Inet6VpnTable::RouteReplicate(BgpServer *server, BgpTable *src_table,
      88             :         BgpRoute *source_rt, const BgpPath *src_path,
      89             :         ExtCommunityPtr community) {
      90           0 :     assert(src_table->family()  == Address::INET6);
      91             : 
      92           0 :     Inet6Route *src_rt = dynamic_cast<Inet6Route *>(source_rt);
      93           0 :     assert(src_rt);
      94             : 
      95           0 :     const RouteDistinguisher &rd = GenerateDistinguisher(src_table, src_path);
      96           0 :     Inet6VpnPrefix vpn_prefix(rd, src_rt->GetPrefix().ip6_addr(),
      97           0 :                               src_rt->GetPrefix().prefixlen());
      98             : 
      99           0 :     Inet6VpnRoute vpn_route(vpn_prefix);
     100             :     DBTablePartition *partition =
     101           0 :         static_cast<DBTablePartition *>(GetTablePartition(&vpn_route));
     102           0 :     BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&vpn_route));
     103           0 :     if (dest_route == NULL) {
     104           0 :         dest_route = new Inet6VpnRoute(vpn_prefix);
     105           0 :         partition->Add(dest_route);
     106             :     } else {
     107           0 :         dest_route->ClearDelete();
     108             :     }
     109             : 
     110           0 :     BgpAttrDB *attr_db = server->attr_db();
     111             : 
     112             :     BgpAttrPtr new_attr =
     113             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     114           0 :                                                         community);
     115           0 :     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           0 :         dest_route->FindSecondaryPath(source_rt, src_path->GetSource(),
     120             :                                       src_path->GetPeer(),
     121             :                                       src_path->GetPathId());
     122           0 :     if (dest_path != NULL) {
     123           0 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     124           0 :             (src_path->GetFlags() != dest_path->GetFlags()) ||
     125           0 :             (src_path->GetLabel() != dest_path->GetLabel())) {
     126             :             // Update Attributes and notify (if needed)
     127           0 :             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           0 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     138           0 :                              src_path->GetSource(), new_attr,
     139           0 :                              src_path->GetFlags(), src_path->GetLabel());
     140           0 :     replicated_path->SetReplicateInfo(src_table, source_rt);
     141           0 :     dest_route->InsertPath(replicated_path);
     142             : 
     143             :     // Always trigger notification.
     144           0 :     partition->Notify(dest_route);
     145             : 
     146           0 :     return dest_route;
     147           0 : }
     148             : 
     149           0 : bool Inet6VpnTable::Export(RibOut *ribout, Route *route,
     150             :         const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     151           0 :     BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     152           0 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     153           0 :     if (!uinfo) {
     154           0 :         return false;
     155             :     }
     156           0 :     uinfo_slist->push_front(*uinfo);
     157             : 
     158           0 :     return true;
     159             : }
     160             : 
     161           1 : static void RegisterFactory() {
     162           1 :     DB::RegisterFactory("bgp.l3vpn-inet6.0", &Inet6VpnTable::CreateTable);
     163           1 : }
     164             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14