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: 70 145 48.3 %
Date: 2026-09-21 02:11:39 Functions: 17 27 63.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14