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: 25 145 17.2 %
Date: 2026-09-28 02:13:17 Functions: 6 27 22.2 %
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         268 : RtGroup::RtGroup(const RouteTarget &rt)
      25         268 :     : rt_(rt), dep_(RTargetDepRouteList(DB::PartitionCount())) {
      26             :     vector<Address::Family> vpn_family_list = list_of
      27         268 :         (Address::INETVPN)(Address::INET6VPN)(Address::ERMVPN)(Address::EVPN)
      28         268 :         (Address::MVPN);
      29        2948 :     BOOST_FOREACH(Address::Family vpn_family, vpn_family_list) {
      30        1340 :         import_[vpn_family] = RtGroupMemberList();
      31        1340 :         export_[vpn_family] = RtGroupMemberList();
      32             :     }
      33         268 : }
      34             : 
      35         268 : bool RtGroup::MayDelete() const {
      36         268 :     return !HasImportExportTables() && !HasInterestedPeers() && !HasDepRoutes();
      37             : }
      38             : 
      39           0 : const RtGroup::RtGroupMemberList &RtGroup::GetImportTables(
      40             :     Address::Family family) const {
      41           0 :     return import_.at(family);
      42             : }
      43             : 
      44           0 : const RtGroup::RtGroupMemberList &RtGroup::GetExportTables(
      45             :     Address::Family family) const {
      46           0 :     return export_.at(family);
      47             : }
      48             : 
      49           0 : bool RtGroup::AddImportTable(Address::Family family, BgpTable *tbl) {
      50           0 :     bool first = import_[family].empty();
      51           0 :     import_[family].insert(tbl);
      52           0 :     return first;
      53             : }
      54             : 
      55           0 : bool RtGroup::AddExportTable(Address::Family family, BgpTable *tbl) {
      56           0 :     bool first = export_[family].empty();
      57           0 :     export_[family].insert(tbl);
      58           0 :     return first;
      59             : }
      60             : 
      61           0 : bool RtGroup::RemoveImportTable(Address::Family family, BgpTable *tbl) {
      62           0 :     import_[family].erase(tbl);
      63           0 :     return import_[family].empty();
      64             : }
      65             : 
      66           0 : bool RtGroup::RemoveExportTable(Address::Family family, BgpTable *tbl) {
      67           0 :     export_[family].erase(tbl);
      68           0 :     return export_[family].empty();
      69             : }
      70             : 
      71         268 : bool RtGroup::HasImportExportTables() const {
      72        2948 :     BOOST_FOREACH(const RtGroupMembers::value_type &family_members, import_) {
      73        1340 :         if (!family_members.second.empty())
      74           0 :             return true;
      75             :     }
      76        2948 :     BOOST_FOREACH(const RtGroupMembers::value_type &family_members, export_) {
      77        1340 :         if (!family_members.second.empty())
      78           0 :             return true;
      79             :     }
      80         268 :     return false;
      81             : }
      82             : 
      83           0 : bool RtGroup::HasVrfTables(Address::Family family) const {
      84             :     const BgpTable *table;
      85             : 
      86           0 :     const RtGroupMemberList &import_list = import_.at(family);
      87           0 :     if (import_list.size() > 1)
      88           0 :         return true;
      89           0 :     table = *import_list.begin();
      90           0 :     if (table && !table->IsVpnTable())
      91           0 :         return true;
      92             : 
      93           0 :     const RtGroupMemberList &export_list = export_.at(family);
      94           0 :     if (export_list.size() > 1)
      95           0 :         return true;
      96           0 :     table = *export_list.begin();
      97           0 :     if (table && !table->IsVpnTable())
      98           0 :         return true;
      99             : 
     100           0 :     return false;
     101             : }
     102             : 
     103         268 : const RouteTarget &RtGroup::rt() {
     104         268 :     return rt_;
     105             : }
     106             : 
     107           0 : void RtGroup::AddDepRoute(int part_id, BgpRoute *rt) {
     108           0 :     dep_[part_id].insert(rt);
     109           0 : }
     110             : 
     111           0 : void RtGroup::RemoveDepRoute(int part_id, BgpRoute *rt) {
     112           0 :     dep_[part_id].erase(rt);
     113           0 : }
     114             : 
     115           0 : void RtGroup::NotifyDepRoutes(int part_id) {
     116           0 :     BOOST_FOREACH(BgpRoute *route, dep_[part_id]) {
     117           0 :         DBTablePartBase *dbpart = route->get_table_partition();
     118           0 :         dbpart->Notify(route);
     119             :     }
     120           0 : }
     121             : 
     122         268 : bool RtGroup::HasDepRoutes() const {
     123         268 :     for (RTargetDepRouteList::const_iterator it = dep_.begin();
     124        1340 :          it != dep_.end(); ++it) {
     125        1072 :         if (!it->empty()) {
     126           0 :             return true;
     127             :         }
     128             :     }
     129         268 :     return false;
     130             : }
     131             : 
     132           0 : const RtGroupInterestedPeerSet& RtGroup::GetInterestedPeers() const {
     133           0 :     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         268 : bool RtGroup::HasInterestedPeers() const {
     159         268 :     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