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

Generated by: LCOV version 1.14