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 184 : RtGroup::RtGroup(const RouteTarget &rt) 26 184 : : rt_(rt), dep_(RTargetDepRouteList(DB::PartitionCount())) { 27 : vector<Address::Family> vpn_family_list = list_of 28 184 : (Address::INETVPN)(Address::INET6VPN)(Address::ERMVPN)(Address::EVPN) 29 184 : (Address::MVPN); 30 2024 : BOOST_FOREACH(Address::Family vpn_family, vpn_family_list) { 31 920 : import_[vpn_family] = RtGroupMemberList(); 32 920 : export_[vpn_family] = RtGroupMemberList(); 33 : } 34 184 : } 35 : 36 1384 : bool RtGroup::MayDelete() const { 37 1384 : return !HasImportExportTables() && !HasInterestedPeers() && !HasDepRoutes(); 38 : } 39 : 40 4953 : const RtGroup::RtGroupMemberList &RtGroup::GetImportTables( 41 : Address::Family family) const { 42 4953 : return import_.at(family); 43 : } 44 : 45 680 : const RtGroup::RtGroupMemberList &RtGroup::GetExportTables( 46 : Address::Family family) const { 47 680 : return export_.at(family); 48 : } 49 : 50 680 : bool RtGroup::AddImportTable(Address::Family family, BgpTable *tbl) { 51 680 : bool first = import_[family].empty(); 52 680 : import_[family].insert(tbl); 53 680 : return first; 54 : } 55 : 56 510 : bool RtGroup::AddExportTable(Address::Family family, BgpTable *tbl) { 57 510 : bool first = export_[family].empty(); 58 510 : export_[family].insert(tbl); 59 510 : return first; 60 : } 61 : 62 680 : bool RtGroup::RemoveImportTable(Address::Family family, BgpTable *tbl) { 63 680 : import_[family].erase(tbl); 64 680 : return import_[family].empty(); 65 : } 66 : 67 510 : bool RtGroup::RemoveExportTable(Address::Family family, BgpTable *tbl) { 68 510 : export_[family].erase(tbl); 69 510 : return export_[family].empty(); 70 : } 71 : 72 1384 : bool RtGroup::HasImportExportTables() const { 73 6834 : BOOST_FOREACH(const RtGroupMembers::value_type &family_members, import_) { 74 3564 : if (!family_members.second.empty()) 75 839 : return true; 76 : } 77 5995 : BOOST_FOREACH(const RtGroupMembers::value_type &family_members, export_) { 78 2725 : if (!family_members.second.empty()) 79 0 : return true; 80 : } 81 545 : return false; 82 : } 83 : 84 510 : bool RtGroup::HasVrfTables(Address::Family family) const { 85 : const BgpTable *table; 86 : 87 510 : const RtGroupMemberList &import_list = import_.at(family); 88 510 : if (import_list.size() > 1) 89 0 : return true; 90 510 : table = *import_list.begin(); 91 510 : if (table && !table->IsVpnTable()) 92 0 : return true; 93 : 94 510 : const RtGroupMemberList &export_list = export_.at(family); 95 510 : if (export_list.size() > 1) 96 170 : return true; 97 340 : table = *export_list.begin(); 98 340 : if (table && !table->IsVpnTable()) 99 0 : return true; 100 : 101 340 : return false; 102 : } 103 : 104 3102 : const RouteTarget &RtGroup::rt() { 105 3102 : return rt_; 106 : } 107 : 108 1384 : void RtGroup::AddDepRoute(int part_id, BgpRoute *rt) { 109 1384 : dep_[part_id].insert(rt); 110 1384 : } 111 : 112 1384 : void RtGroup::RemoveDepRoute(int part_id, BgpRoute *rt) { 113 1384 : dep_[part_id].erase(rt); 114 1384 : } 115 : 116 780 : void RtGroup::NotifyDepRoutes(int part_id) { 117 780 : BOOST_FOREACH(BgpRoute *route, dep_[part_id]) { 118 0 : DBTablePartBase *dbpart = route->get_table_partition(); 119 0 : dbpart->Notify(route); 120 : } 121 774 : } 122 : 123 1225 : bool RtGroup::HasDepRoutes() const { 124 1225 : for (RTargetDepRouteList::const_iterator it = dep_.begin(); 125 5001 : it != dep_.end(); ++it) { 126 4137 : if (!it->empty()) { 127 361 : return true; 128 : } 129 : } 130 864 : return false; 131 : } 132 : 133 1545 : const RtGroupInterestedPeerSet& RtGroup::GetInterestedPeers() const { 134 1545 : return interested_peers_; 135 : } 136 : 137 99 : void RtGroup::AddInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) { 138 99 : InterestedPeerList::iterator it = peer_list_.find(peer); 139 99 : if (it == peer_list_.end()) { 140 99 : it = peer_list_.insert(peer_list_.begin(), 141 198 : pair<const BgpPeer *, RTargetRouteList>(peer, RTargetRouteList())); 142 99 : assert(peer->GetIndex() >= 0); 143 99 : interested_peers_.set(peer->GetIndex()); 144 : } 145 99 : it->second.insert(rt); 146 99 : } 147 : 148 99 : void RtGroup::RemoveInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) { 149 99 : InterestedPeerList::iterator it = peer_list_.find(peer); 150 99 : if (it == peer_list_.end()) return; 151 99 : it->second.erase(rt); 152 99 : if (it->second.empty()) { 153 99 : peer_list_.erase(peer); 154 99 : assert(peer->GetIndex() >= 0); 155 99 : interested_peers_.reset(peer->GetIndex()); 156 : } 157 : } 158 : 159 545 : bool RtGroup::HasInterestedPeers() const { 160 545 : return !peer_list_.empty(); 161 : } 162 : 163 0 : bool RtGroup::HasInterestedPeer(const string &name) const { 164 0 : BOOST_FOREACH(const InterestedPeerList::value_type &peer, peer_list_) { 165 0 : if (peer.first->peer_basename() == name) 166 0 : return true; 167 : } 168 0 : return false; 169 : } 170 : 171 0 : void RtGroup::FillMemberTables(const RtGroupMembers &rt_members, 172 : vector<ShowRtGroupMemberTableList> *member_list) const { 173 0 : BOOST_FOREACH(const RtGroupMembers::value_type &rt_tables, rt_members) { 174 0 : ShowRtGroupMemberTableList member; 175 0 : vector<string> table_names; 176 0 : BOOST_FOREACH(BgpTable *table, rt_tables.second) { 177 0 : table_names.push_back(table->name()); 178 : } 179 0 : member.set_family(Address::FamilyToString(rt_tables.first)); 180 0 : member.set_tables(table_names); 181 0 : member_list->push_back(member); 182 0 : } 183 0 : } 184 : 185 0 : void RtGroup::FillInterestedPeers(vector<string> *interested_peers) const { 186 0 : BOOST_FOREACH(const InterestedPeerList::value_type &peer, peer_list_) { 187 0 : interested_peers->push_back(peer.first->peer_basename()); 188 : } 189 0 : } 190 : 191 0 : void RtGroup::FillDependentRoutes(vector<string> *rtlist) const { 192 0 : for (RTargetDepRouteList::const_iterator dep_it = dep_.begin(); 193 0 : dep_it != dep_.end(); ++dep_it) { 194 0 : for (RouteList::const_iterator dep_rt_it = dep_it->begin(); 195 0 : dep_rt_it != dep_it->end(); ++dep_rt_it) { 196 0 : rtlist->push_back((*dep_rt_it)->ToString()); 197 : } 198 : } 199 0 : } 200 : 201 0 : void RtGroup::FillShowInfoCommon(ShowRtGroupInfo *info, 202 : bool fill_peers, bool fill_routes) const { 203 0 : info->set_rtarget(ToString()); 204 : 205 0 : vector<ShowRtGroupMemberTableList> import_members; 206 0 : FillMemberTables(import_, &import_members); 207 0 : info->set_import_members(import_members); 208 0 : vector<ShowRtGroupMemberTableList> export_members; 209 0 : FillMemberTables(export_, &export_members); 210 0 : info->set_export_members(export_members); 211 : 212 0 : if (fill_peers) { 213 0 : vector<string> interested_peers; 214 0 : FillInterestedPeers(&interested_peers); 215 0 : info->set_peers_interested(interested_peers); 216 0 : } 217 0 : if (fill_routes) { 218 0 : vector<string> rtlist; 219 0 : FillDependentRoutes(&rtlist); 220 0 : info->set_dep_route(rtlist); 221 0 : } 222 0 : } 223 : 224 0 : void RtGroup::FillShowInfo(ShowRtGroupInfo *info) const { 225 0 : FillShowInfoCommon(info, true, true); 226 0 : } 227 : 228 0 : void RtGroup::FillShowSummaryInfo(ShowRtGroupInfo *info) const { 229 0 : FillShowInfoCommon(info, true, false); 230 0 : } 231 : 232 0 : void RtGroup::FillShowPeerInfo(ShowRtGroupInfo *info) const { 233 0 : FillShowInfoCommon(info, false, true); 234 0 : }