LCOV - code coverage report
Current view: top level - bgp/routing-instance - rtarget_group.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 143 145 98.6 %
Date: 2026-11-09 02:10:49 Functions: 27 27 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/routing-instance/rtarget_group.h"
       7             : 
       8             : #include <boost/assign/list_of.hpp>
       9             : #include <boost/foreach.hpp>
      10             : 
      11             : #include <utility>
      12             : 
      13             : #include "sandesh/sandesh_trace.h"
      14             : #include "bgp/bgp_peer.h"
      15             : #include "bgp/bgp_peer_types.h"
      16             : #include "bgp/bgp_table.h"
      17             : #include "bgp/rtarget/rtarget_route.h"
      18             : #include "db/db.h"
      19             : 
      20             : using boost::assign::list_of;
      21             : using std::pair;
      22             : using std::string;
      23             : using std::vector;
      24             : 
      25      108909 : RtGroup::RtGroup(const RouteTarget &rt)
      26      108909 :     : rt_(rt), dep_(RTargetDepRouteList(DB::PartitionCount())) {
      27             :     vector<Address::Family> vpn_family_list = list_of
      28      108909 :         (Address::INETVPN)(Address::INET6VPN)(Address::ERMVPN)(Address::EVPN)
      29      108909 :         (Address::MVPN);
      30     1197985 :     BOOST_FOREACH(Address::Family vpn_family, vpn_family_list) {
      31      544539 :         import_[vpn_family] = RtGroupMemberList();
      32      544538 :         export_[vpn_family] = RtGroupMemberList();
      33             :     }
      34      108909 : }
      35             : 
      36      324520 : bool RtGroup::MayDelete() const {
      37      324520 :     return !HasImportExportTables() && !HasInterestedPeers() && !HasDepRoutes();
      38             : }
      39             : 
      40     2172059 : const RtGroup::RtGroupMemberList &RtGroup::GetImportTables(
      41             :     Address::Family family) const {
      42     2172059 :     return import_.at(family);
      43             : }
      44             : 
      45     1379813 : const RtGroup::RtGroupMemberList &RtGroup::GetExportTables(
      46             :     Address::Family family) const {
      47     1379813 :     return export_.at(family);
      48             : }
      49             : 
      50     1119525 : bool RtGroup::AddImportTable(Address::Family family, BgpTable *tbl) {
      51     1119525 :     bool first = import_[family].empty();
      52     1119108 :     import_[family].insert(tbl);
      53     1119731 :     return first;
      54             : }
      55             : 
      56      668350 : bool RtGroup::AddExportTable(Address::Family family, BgpTable *tbl) {
      57      668350 :     bool first = export_[family].empty();
      58      668148 :     export_[family].insert(tbl);
      59      668412 :     return first;
      60             : }
      61             : 
      62     1119785 : bool RtGroup::RemoveImportTable(Address::Family family, BgpTable *tbl) {
      63     1119785 :     import_[family].erase(tbl);
      64     1119780 :     return import_[family].empty();
      65             : }
      66             : 
      67      668438 : bool RtGroup::RemoveExportTable(Address::Family family, BgpTable *tbl) {
      68      668438 :     export_[family].erase(tbl);
      69      668439 :     return export_[family].empty();
      70             : }
      71             : 
      72      324520 : bool RtGroup::HasImportExportTables() const {
      73     1686380 :     BOOST_FOREACH(const RtGroupMembers::value_type &family_members, import_) {
      74      869264 :         if (!family_members.second.empty())
      75      188334 :             return true;
      76             :     }
      77     1498046 :     BOOST_FOREACH(const RtGroupMembers::value_type &family_members, export_) {
      78      680930 :         if (!family_members.second.empty())
      79           0 :             return true;
      80             :     }
      81      136186 :     return false;
      82             : }
      83             : 
      84      928654 : bool RtGroup::HasVrfTables(Address::Family family) const {
      85             :     const BgpTable *table;
      86             : 
      87      928654 :     const RtGroupMemberList &import_list = import_.at(family);
      88      928653 :     if (import_list.size() > 1)
      89      308100 :         return true;
      90      620553 :     table = *import_list.begin();
      91      620553 :     if (table && !table->IsVpnTable())
      92           0 :         return true;
      93             : 
      94      620552 :     const RtGroupMemberList &export_list = export_.at(family);
      95      620551 :     if (export_list.size() > 1)
      96      190400 :         return true;
      97      430151 :     table = *export_list.begin();
      98      430151 :     if (table && !table->IsVpnTable())
      99         185 :         return true;
     100             : 
     101      429966 :     return false;
     102             : }
     103             : 
     104     2215770 : const RouteTarget &RtGroup::rt() {
     105     2215770 :     return rt_;
     106             : }
     107             : 
     108      499941 : void RtGroup::AddDepRoute(int part_id, BgpRoute *rt) {
     109      499941 :     dep_[part_id].insert(rt);
     110      499945 : }
     111             : 
     112      499958 : void RtGroup::RemoveDepRoute(int part_id, BgpRoute *rt) {
     113      499958 :     dep_[part_id].erase(rt);
     114      499911 : }
     115             : 
     116      271189 : void RtGroup::NotifyDepRoutes(int part_id) {
     117     1334263 :     BOOST_FOREACH(BgpRoute *route, dep_[part_id]) {
     118      529849 :         DBTablePartBase *dbpart = route->get_table_partition();
     119      530600 :         dbpart->Notify(route);
     120             :     }
     121      270682 : }
     122             : 
     123     1514760 : bool RtGroup::HasDepRoutes() const {
     124     1514760 :     for (RTargetDepRouteList::const_iterator it = dep_.begin();
     125     6887790 :          it != dep_.end(); ++it) {
     126     5531348 :         if (!it->empty()) {
     127      158251 :             return true;
     128             :         }
     129             :     }
     130     1356315 :     return false;
     131             : }
     132             : 
     133     2258137 : const RtGroupInterestedPeerSet& RtGroup::GetInterestedPeers() const {
     134     2258137 :     return interested_peers_;
     135             : }
     136             : 
     137       27076 : void RtGroup::AddInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) {
     138       27076 :     InterestedPeerList::iterator it = peer_list_.find(peer);
     139       27076 :     if (it == peer_list_.end()) {
     140       23558 :         it = peer_list_.insert(peer_list_.begin(),
     141       47116 :             pair<const BgpPeer *, RTargetRouteList>(peer, RTargetRouteList()));
     142       23558 :         assert(peer->GetIndex() >= 0);
     143       23558 :         interested_peers_.set(peer->GetIndex());
     144             :     }
     145       27076 :     it->second.insert(rt);
     146       27076 : }
     147             : 
     148       27076 : void RtGroup::RemoveInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) {
     149       27076 :     InterestedPeerList::iterator it = peer_list_.find(peer);
     150       27076 :     if (it == peer_list_.end()) return;
     151       27076 :     it->second.erase(rt);
     152       27076 :     if (it->second.empty()) {
     153       23558 :         peer_list_.erase(peer);
     154       23558 :         assert(peer->GetIndex() >= 0);
     155       23558 :         interested_peers_.reset(peer->GetIndex());
     156             :     }
     157             : }
     158             : 
     159      136186 : bool RtGroup::HasInterestedPeers() const {
     160      136186 :     return !peer_list_.empty();
     161             : }
     162             : 
     163         106 : bool RtGroup::HasInterestedPeer(const string &name) const {
     164         242 :     BOOST_FOREACH(const InterestedPeerList::value_type &peer, peer_list_) {
     165          92 :         if (peer.first->peer_basename() == name)
     166          24 :             return true;
     167             :     }
     168          82 :     return false;
     169             : }
     170             : 
     171        3942 : void RtGroup::FillMemberTables(const RtGroupMembers &rt_members,
     172             :     vector<ShowRtGroupMemberTableList> *member_list) const {
     173       43362 :     BOOST_FOREACH(const RtGroupMembers::value_type &rt_tables, rt_members) {
     174       19710 :         ShowRtGroupMemberTableList member;
     175       19710 :         vector<string> table_names;
     176       93870 :         BOOST_FOREACH(BgpTable *table, rt_tables.second) {
     177       37080 :             table_names.push_back(table->name());
     178             :         }
     179       19710 :         member.set_family(Address::FamilyToString(rt_tables.first));
     180       19710 :         member.set_tables(table_names);
     181       19710 :         member_list->push_back(member);
     182       19710 :     }
     183        3942 : }
     184             : 
     185        1947 : void RtGroup::FillInterestedPeers(vector<string> *interested_peers) const {
     186        2155 :     BOOST_FOREACH(const InterestedPeerList::value_type &peer, peer_list_) {
     187         104 :         interested_peers->push_back(peer.first->peer_basename());
     188             :     }
     189        1947 : }
     190             : 
     191        1909 : void RtGroup::FillDependentRoutes(vector<string> *rtlist) const {
     192        1909 :     for (RTargetDepRouteList::const_iterator dep_it = dep_.begin();
     193        9545 :          dep_it != dep_.end(); ++dep_it) {
     194        7636 :         for (RouteList::const_iterator dep_rt_it = dep_it->begin();
     195        7655 :              dep_rt_it != dep_it->end(); ++dep_rt_it) {
     196          19 :             rtlist->push_back((*dep_rt_it)->ToString());
     197             :         }
     198             :     }
     199        1909 : }
     200             : 
     201        1971 : void RtGroup::FillShowInfoCommon(ShowRtGroupInfo *info,
     202             :     bool fill_peers, bool fill_routes) const {
     203        1971 :     info->set_rtarget(ToString());
     204             : 
     205        1971 :     vector<ShowRtGroupMemberTableList> import_members;
     206        1971 :     FillMemberTables(import_, &import_members);
     207        1971 :     info->set_import_members(import_members);
     208        1971 :     vector<ShowRtGroupMemberTableList> export_members;
     209        1971 :     FillMemberTables(export_, &export_members);
     210        1971 :     info->set_export_members(export_members);
     211             : 
     212        1971 :     if (fill_peers) {
     213        1947 :         vector<string> interested_peers;
     214        1947 :         FillInterestedPeers(&interested_peers);
     215        1947 :         info->set_peers_interested(interested_peers);
     216        1947 :     }
     217        1971 :     if (fill_routes) {
     218        1909 :         vector<string> rtlist;
     219        1909 :         FillDependentRoutes(&rtlist);
     220        1909 :         info->set_dep_route(rtlist);
     221        1909 :     }
     222        1971 : }
     223             : 
     224        1885 : void RtGroup::FillShowInfo(ShowRtGroupInfo *info) const {
     225        1885 :     FillShowInfoCommon(info, true, true);
     226        1885 : }
     227             : 
     228          62 : void RtGroup::FillShowSummaryInfo(ShowRtGroupInfo *info) const {
     229          62 :     FillShowInfoCommon(info, true, false);
     230          62 : }
     231             : 
     232          24 : void RtGroup::FillShowPeerInfo(ShowRtGroupInfo *info) const {
     233          24 :     FillShowInfoCommon(info, false, true);
     234          24 : }

Generated by: LCOV version 1.14