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 912 : RtGroup::RtGroup(const RouteTarget &rt) 26 912 : : rt_(rt), dep_(RTargetDepRouteList(DB::PartitionCount())) { 27 : vector<Address::Family> vpn_family_list = list_of 28 912 : (Address::INETVPN)(Address::INET6VPN)(Address::ERMVPN)(Address::EVPN) 29 912 : (Address::MVPN); 30 10032 : BOOST_FOREACH(Address::Family vpn_family, vpn_family_list) { 31 4560 : import_[vpn_family] = RtGroupMemberList(); 32 4560 : export_[vpn_family] = RtGroupMemberList(); 33 : } 34 912 : } 35 : 36 10302 : bool RtGroup::MayDelete() const { 37 10302 : return !HasImportExportTables() && !HasInterestedPeers() && !HasDepRoutes(); 38 : } 39 : 40 50838 : const RtGroup::RtGroupMemberList &RtGroup::GetImportTables( 41 : Address::Family family) const { 42 50838 : return import_.at(family); 43 : } 44 : 45 4799 : const RtGroup::RtGroupMemberList &RtGroup::GetExportTables( 46 : Address::Family family) const { 47 4799 : return export_.at(family); 48 : } 49 : 50 3299 : bool RtGroup::AddImportTable(Address::Family family, BgpTable *tbl) { 51 3299 : bool first = import_[family].empty(); 52 3296 : import_[family].insert(tbl); 53 3300 : return first; 54 : } 55 : 56 2500 : bool RtGroup::AddExportTable(Address::Family family, BgpTable *tbl) { 57 2500 : bool first = export_[family].empty(); 58 2499 : export_[family].insert(tbl); 59 2500 : return first; 60 : } 61 : 62 3300 : bool RtGroup::RemoveImportTable(Address::Family family, BgpTable *tbl) { 63 3300 : import_[family].erase(tbl); 64 3300 : return import_[family].empty(); 65 : } 66 : 67 2500 : bool RtGroup::RemoveExportTable(Address::Family family, BgpTable *tbl) { 68 2500 : export_[family].erase(tbl); 69 2500 : return export_[family].empty(); 70 : } 71 : 72 10302 : bool RtGroup::HasImportExportTables() const { 73 19422 : BOOST_FOREACH(const RtGroupMembers::value_type &family_members, import_) { 74 13950 : if (!family_members.second.empty()) 75 9390 : return true; 76 : } 77 10032 : BOOST_FOREACH(const RtGroupMembers::value_type &family_members, export_) { 78 4560 : if (!family_members.second.empty()) 79 0 : return true; 80 : } 81 912 : return false; 82 : } 83 : 84 4000 : bool RtGroup::HasVrfTables(Address::Family family) const { 85 : const BgpTable *table; 86 : 87 4000 : const RtGroupMemberList &import_list = import_.at(family); 88 4000 : if (import_list.size() > 1) 89 2700 : return true; 90 1300 : table = *import_list.begin(); 91 1300 : if (table && !table->IsVpnTable()) 92 0 : return true; 93 : 94 1300 : const RtGroupMemberList &export_list = export_.at(family); 95 1300 : if (export_list.size() > 1) 96 400 : return true; 97 900 : table = *export_list.begin(); 98 900 : if (table && !table->IsVpnTable()) 99 0 : return true; 100 : 101 900 : return false; 102 : } 103 : 104 10436 : const RouteTarget &RtGroup::rt() { 105 10436 : return rt_; 106 : } 107 : 108 32767 : void RtGroup::AddDepRoute(int part_id, BgpRoute *rt) { 109 32767 : dep_[part_id].insert(rt); 110 32762 : } 111 : 112 32760 : void RtGroup::RemoveDepRoute(int part_id, BgpRoute *rt) { 113 32760 : dep_[part_id].erase(rt); 114 32754 : } 115 : 116 7059 : void RtGroup::NotifyDepRoutes(int part_id) { 117 75917 : BOOST_FOREACH(BgpRoute *route, dep_[part_id]) { 118 34157 : DBTablePartBase *dbpart = route->get_table_partition(); 119 34022 : dbpart->Notify(route); 120 : } 121 6993 : } 122 : 123 5712 : bool RtGroup::HasDepRoutes() const { 124 5712 : for (RTargetDepRouteList::const_iterator it = dep_.begin(); 125 28548 : it != dep_.end(); ++it) { 126 22837 : if (!it->empty()) { 127 0 : return true; 128 : } 129 : } 130 5709 : return false; 131 : } 132 : 133 74694 : const RtGroupInterestedPeerSet& RtGroup::GetInterestedPeers() const { 134 74694 : return interested_peers_; 135 : } 136 : 137 1162 : void RtGroup::AddInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) { 138 1162 : InterestedPeerList::iterator it = peer_list_.find(peer); 139 1162 : if (it == peer_list_.end()) { 140 874 : it = peer_list_.insert(peer_list_.begin(), 141 1748 : pair<const BgpPeer *, RTargetRouteList>(peer, RTargetRouteList())); 142 874 : assert(peer->GetIndex() >= 0); 143 874 : interested_peers_.set(peer->GetIndex()); 144 : } 145 1162 : it->second.insert(rt); 146 1162 : } 147 : 148 1162 : void RtGroup::RemoveInterestedPeer(const BgpPeer *peer, RTargetRoute *rt) { 149 1162 : InterestedPeerList::iterator it = peer_list_.find(peer); 150 1162 : if (it == peer_list_.end()) return; 151 1162 : it->second.erase(rt); 152 1162 : if (it->second.empty()) { 153 874 : peer_list_.erase(peer); 154 874 : assert(peer->GetIndex() >= 0); 155 874 : interested_peers_.reset(peer->GetIndex()); 156 : } 157 : } 158 : 159 912 : bool RtGroup::HasInterestedPeers() const { 160 912 : 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 : }