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: 64 92 69.6 %
Date: 2026-11-23 02:04:52 Functions: 6 11 54.5 %
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          68 : Inet6VpnTable::Inet6VpnTable(DB *db, const std::string &name)
      17          68 :     : BgpTable(db, name) {
      18          68 : }
      19             : 
      20             : std::unique_ptr<DBEntry>
      21           0 : Inet6VpnTable::AllocEntry(const DBRequestKey *key) const {
      22           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      23           0 :     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        2805 : size_t Inet6VpnTable::Hash(const DBEntry *entry) const {
      33        2805 :     const Inet6VpnRoute *vpn_route = static_cast<const Inet6VpnRoute *>(entry);
      34        2805 :     const Inet6VpnPrefix &vpn_prefix = vpn_route->GetPrefix();
      35        2805 :     Inet6Prefix prefix(vpn_prefix.addr(), vpn_prefix.prefixlen());
      36        2805 :     size_t value = Inet6Table::HashFunction(prefix);
      37        2805 :     return value % DB::PartitionCount();
      38             : }
      39             : 
      40           0 : size_t Inet6VpnTable::Hash(const DBRequestKey *key) const {
      41           0 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      42           0 :     Inet6Prefix prefix(rkey->prefix.addr(), rkey->prefix.prefixlen());
      43           0 :     size_t value = Inet6Table::HashFunction(prefix);
      44           0 :     return value % DB::PartitionCount();
      45             : }
      46             : 
      47           0 : BgpRoute *Inet6VpnTable::TableFind(DBTablePartition *rtp,
      48             :                                    const DBRequestKey *prefix) {
      49           0 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
      50           0 :     Inet6VpnRoute vpn_route(pfxkey->prefix);
      51           0 :     return static_cast<BgpRoute *>(rtp->Find(&vpn_route));
      52           0 : }
      53             : 
      54          68 : DBTableBase *Inet6VpnTable::CreateTable(DB *db, const std::string &name) {
      55          68 :     Inet6VpnTable *table = new Inet6VpnTable(db, name);
      56          68 :     table->Init();
      57          68 :     return table;
      58             : }
      59             : 
      60         609 : static RouteDistinguisher GenerateDistinguisher(
      61             :         const BgpTable *src_table, const BgpPath *src_path) {
      62         609 :     const RouteDistinguisher &source_rd = src_path->GetAttr()->source_rd();
      63         609 :     if (!src_path->GetPeer() || !(src_path->GetPeer()->IsRouterTypeBGPaaS())){
      64           0 :         if (!source_rd.IsZero())
      65           0 :         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         609 :     uint16_t peer_port_id = 0;
      72         609 :     as_t asn_num = src_path->GetAttr()->neighbor_as();
      73             : 
      74         609 :     std::string ri_name = src_table->routing_instance()->name();
      75        1218 :     for (const BgpPeer *peer = src_path->GetPeer()->server()->FindNextPeer(); peer != NULL;
      76         609 :          peer = src_path->GetPeer()->server()->FindNextPeer(peer->peer_name())) {
      77        1827 :             if ((peer->peer_name().find(ri_name)!= std::string::npos)&&
      78        1827 :             (peer->peer_address_string().find(src_path->GetPeer()->ToString())!= std::string::npos)){
      79         609 :                 peer_port_id = peer->peer_port();
      80         609 :                 asn_num = peer->peer_as();
      81         609 :                 break;
      82             :             }
      83             :     }
      84         609 :     RouteDistinguisher new_source_rd = RouteDistinguisher(true, asn_num, peer_port_id);
      85         609 :     return new_source_rd;
      86         609 : }
      87             : 
      88         609 : BgpRoute *Inet6VpnTable::RouteReplicate(BgpServer *server, BgpTable *src_table,
      89             :         BgpRoute *source_rt, const BgpPath *src_path,
      90             :         ExtCommunityPtr community) {
      91         609 :     assert(src_table->family()  == Address::INET6);
      92             : 
      93         609 :     Inet6Route *src_rt = dynamic_cast<Inet6Route *>(source_rt);
      94         609 :     assert(src_rt);
      95             : 
      96         609 :     const RouteDistinguisher &rd = GenerateDistinguisher(src_table, src_path);
      97         609 :     Inet6VpnPrefix vpn_prefix(rd, src_rt->GetPrefix().ip6_addr(),
      98        1218 :                               src_rt->GetPrefix().prefixlen());
      99             : 
     100         609 :     Inet6VpnRoute vpn_route(vpn_prefix);
     101             :     DBTablePartition *partition =
     102         609 :         static_cast<DBTablePartition *>(GetTablePartition(&vpn_route));
     103         609 :     BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&vpn_route));
     104         609 :     if (dest_route == NULL) {
     105         128 :         dest_route = new Inet6VpnRoute(vpn_prefix);
     106         128 :         partition->Add(dest_route);
     107             :     } else {
     108         481 :         dest_route->ClearDelete();
     109             :     }
     110             : 
     111         609 :     BgpAttrDB *attr_db = server->attr_db();
     112             : 
     113             :     BgpAttrPtr new_attr =
     114             :         server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(),
     115         609 :                                                         community);
     116         609 :     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         609 :         dest_route->FindSecondaryPath(source_rt, src_path->GetSource(),
     121             :                                       src_path->GetPeer(),
     122             :                                       src_path->GetPathId());
     123         609 :     if (dest_path != NULL) {
     124         898 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     125         898 :             (src_path->GetFlags() != dest_path->GetFlags()) ||
     126         257 :             (src_path->GetLabel() != dest_path->GetLabel())) {
     127             :             // Update Attributes and notify (if needed)
     128         192 :             assert(dest_route->RemoveSecondaryPath(source_rt,
     129             :                                 src_path->GetSource(), src_path->GetPeer(),
     130             :                                 src_path->GetPathId()));
     131             :         } else {
     132         257 :             return dest_route;
     133             :         }
     134             :     }
     135             : 
     136             :     // Create replicated path and insert it on the route
     137             :     BgpSecondaryPath *replicated_path =
     138         352 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     139         352 :                              src_path->GetSource(), new_attr,
     140         352 :                              src_path->GetFlags(), src_path->GetLabel());
     141         352 :     replicated_path->SetReplicateInfo(src_table, source_rt);
     142         352 :     dest_route->InsertPath(replicated_path);
     143             : 
     144             :     // Always trigger notification.
     145         352 :     partition->Notify(dest_route);
     146             : 
     147         352 :     return dest_route;
     148         609 : }
     149             : 
     150           0 : bool Inet6VpnTable::Export(RibOut *ribout, Route *route,
     151             :         const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     152           0 :     BgpRoute *bgp_route = static_cast<BgpRoute *> (route);
     153           0 :     UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset);
     154           0 :     if (!uinfo) {
     155           0 :         return false;
     156             :     }
     157           0 :     uinfo_slist->push_front(*uinfo);
     158             : 
     159           0 :     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