Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : */
4 : #include "bgp/routing-instance/rtarget_group_mgr.h"
5 :
6 : #include <boost/foreach.hpp>
7 :
8 : #include <utility>
9 :
10 : #include "base/map_util.h"
11 : #include "base/set_util.h"
12 : #include "base/task_annotations.h"
13 : #include "base/task_trigger.h"
14 : #include "bgp/bgp_config.h"
15 : #include "bgp/bgp_peer.h"
16 : #include "bgp/bgp_ribout.h"
17 : #include "bgp/bgp_server.h"
18 : #include "bgp/bgp_table.h"
19 : #include "bgp/routing-instance/routing_instance.h"
20 : #include "bgp/rtarget/rtarget_route.h"
21 :
22 : using std::pair;
23 :
24 63456 : void VpnRouteState::AddRouteTarget(RTargetGroupMgr *mgr, int part_id,
25 : BgpRoute *rt, RTargetList::const_iterator it) {
26 63456 : pair<RTargetList::iterator, bool> result;
27 63455 : result = list_.insert(*it);
28 63479 : assert(result.second);
29 63479 : RtGroup *rtgroup = mgr->LocateRtGroup(*it);
30 63488 : rtgroup->AddDepRoute(part_id, rt);
31 63483 : }
32 :
33 63439 : void VpnRouteState::DeleteRouteTarget(RTargetGroupMgr *mgr, int part_id,
34 : BgpRoute *rt, RTargetList::const_iterator it) {
35 63439 : RtGroup *rtgroup = mgr->GetRtGroup(*it);
36 63486 : rtgroup->RemoveDepRoute(part_id, rt);
37 63474 : mgr->RemoveRtGroup(*it);
38 63485 : list_.erase(it);
39 63484 : }
40 :
41 2272 : void RTargetState::AddInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
42 : RTargetRoute *rt, RtGroup::InterestedPeerList::const_iterator it) {
43 2272 : pair<RtGroup::InterestedPeerList::iterator, bool> result;
44 2272 : result = list_.insert(*it);
45 2272 : assert(result.second);
46 2272 : rtgroup->AddInterestedPeer(it->first, rt);
47 2272 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
48 2272 : }
49 :
50 2272 : void RTargetState::DeleteInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
51 : RTargetRoute *rt, RtGroup::InterestedPeerList::iterator it) {
52 2272 : rtgroup->RemoveInterestedPeer(it->first, rt);
53 2272 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
54 2272 : list_.erase(it);
55 2272 : }
56 :
57 418 : RTargetGroupMgr::RTargetGroupMgr(BgpServer *server) : server_(server),
58 836 : rtarget_route_trigger_(new TaskTrigger(
59 : boost::bind(&RTargetGroupMgr::ProcessRTargetRouteList, this),
60 836 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
61 836 : remove_rtgroup_trigger_(new TaskTrigger(
62 : boost::bind(&RTargetGroupMgr::ProcessRtGroupList, this),
63 836 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
64 418 : rtarget_trigger_lists_(DB::PartitionCount()),
65 1254 : master_instance_delete_ref_(this, NULL) {
66 2090 : for (int i = 0; i < DB::PartitionCount(); i++) {
67 5016 : rtarget_dep_triggers_.push_back(boost::shared_ptr<TaskTrigger>(new
68 : TaskTrigger(boost::bind(&RTargetGroupMgr::ProcessRouteTargetList,
69 : this, i),
70 3344 : TaskScheduler::GetInstance()->GetTaskId("db::DBTable"), i)));
71 : }
72 418 : }
73 :
74 7161 : void RTargetGroupMgr::RTargetPeerSync(BgpTable *table, RTargetRoute *rt,
75 : DBTableBase::ListenerId id, RTargetState *dbstate,
76 : const RtGroup::InterestedPeerList *future) {
77 7161 : CHECK_CONCURRENCY("bgp::RTFilter");
78 :
79 7161 : RouteTarget rtarget = rt->GetPrefix().rtarget();
80 7161 : RtGroup *rtgroup = LocateRtGroup(rtarget);
81 7161 : assert(rtgroup);
82 :
83 7161 : map_synchronize(dbstate->GetMutableList(), future,
84 : boost::bind(&RTargetState::AddInterestedPeer, dbstate, this, rtgroup,
85 : rt, _1),
86 : boost::bind(&RTargetState::DeleteInterestedPeer, dbstate, this, rtgroup,
87 : rt, _1));
88 :
89 7161 : if (dbstate->GetList()->empty()) {
90 4329 : rt->ClearState(table, id);
91 4329 : delete dbstate;
92 4329 : RemoveRtGroup(rtarget);
93 : }
94 7161 : }
95 :
96 7161 : void RTargetGroupMgr::BuildRTargetDistributionGraph(BgpTable *table,
97 : RTargetRoute *rt, DBTableBase::ListenerId id) {
98 7161 : CHECK_CONCURRENCY("bgp::RTFilter");
99 :
100 : RTargetState *dbstate =
101 7161 : static_cast<RTargetState *>(rt->GetState(table, id));
102 :
103 7161 : RtGroup::InterestedPeerList peer_list;
104 :
105 11933 : if (rt->IsDeleted() || !rt->BestPath() ||
106 4772 : !rt->BestPath()->IsFeasible()) {
107 2389 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
108 2389 : return;
109 : }
110 :
111 4772 : const BgpPath *best_ebgp_path = NULL;
112 11920 : for (Route::PathList::iterator it = rt->GetPathList().begin();
113 23840 : it != rt->GetPathList().end(); it++) {
114 7148 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
115 7148 : if (!path->IsFeasible())
116 0 : break;
117 7148 : if (!path->GetPeer() || path->GetPeer()->IsXmppPeer())
118 4316 : continue;
119 :
120 2832 : const BgpPeer *peer = static_cast<const BgpPeer *>(path->GetPeer());
121 2832 : if (peer->PeerType() == BgpProto::EBGP) {
122 2832 : if (!best_ebgp_path) {
123 2832 : best_ebgp_path = path;
124 0 : } else if (!best_ebgp_path->PathSameNeighborAs(*path)) {
125 0 : continue;
126 : }
127 : }
128 :
129 : std::pair<RtGroup::InterestedPeerList::iterator, bool> ret =
130 2832 : peer_list.insert(std::pair<const BgpPeer *,
131 5664 : RtGroup::RTargetRouteList>(peer, RtGroup::RTargetRouteList()));
132 2832 : assert(ret.second);
133 2832 : ret.first->second.insert(rt);
134 : }
135 :
136 4772 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
137 7161 : }
138 :
139 4854 : bool RTargetGroupMgr::ProcessRouteTargetList(int part_id) {
140 4854 : CHECK_CONCURRENCY("db::DBTable");
141 :
142 37996 : BOOST_FOREACH(const RouteTarget &rtarget, rtarget_trigger_lists_[part_id]) {
143 16525 : RtGroup *rtgroup = GetRtGroup(rtarget);
144 16724 : if (!rtgroup)
145 1712 : continue;
146 15012 : rtgroup->NotifyDepRoutes(part_id);
147 : }
148 :
149 4870 : rtarget_trigger_lists_[part_id].clear();
150 4869 : return true;
151 : }
152 :
153 4544 : void RTargetGroupMgr::AddRouteTargetToLists(const RouteTarget &rtarget) {
154 22720 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
155 18176 : rtarget_trigger_lists_[idx].insert(rtarget);
156 18176 : rtarget_dep_triggers_[idx]->Set();
157 : }
158 4544 : }
159 :
160 0 : void RTargetGroupMgr::DisableRouteTargetProcessing() {
161 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
162 0 : rtarget_dep_triggers_[idx]->set_disable();
163 : }
164 0 : }
165 :
166 0 : void RTargetGroupMgr::EnableRouteTargetProcessing() {
167 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
168 0 : rtarget_dep_triggers_[idx]->set_enable();
169 : }
170 0 : }
171 :
172 0 : bool RTargetGroupMgr::IsRouteTargetOnList(const RouteTarget &rtarget) const {
173 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
174 0 : if (rtarget_trigger_lists_[idx].find(rtarget) !=
175 0 : rtarget_trigger_lists_[idx].end()) {
176 0 : return true;
177 : }
178 : }
179 0 : return false;
180 : }
181 :
182 2559 : bool RTargetGroupMgr::ProcessRTargetRouteList() {
183 2559 : CHECK_CONCURRENCY("bgp::RTFilter");
184 :
185 2559 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
186 2559 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
187 2559 : BgpTable *table = master->GetTable(Address::RTARGET);
188 :
189 : // Get the Listener id
190 2559 : DBTableBase::ListenerId id = GetListenerId(table);
191 :
192 2559 : for (RTargetRouteTriggerList::iterator it = rtarget_route_list_.begin();
193 9720 : it != rtarget_route_list_.end(); it++) {
194 7161 : BuildRTargetDistributionGraph(table, *it, id);
195 : }
196 :
197 2559 : rtarget_route_list_.clear();
198 2559 : return true;
199 : }
200 :
201 0 : void RTargetGroupMgr::DisableRTargetRouteProcessing() {
202 0 : rtarget_route_trigger_->set_disable();
203 0 : }
204 :
205 0 : void RTargetGroupMgr::EnableRTargetRouteProcessing() {
206 0 : rtarget_route_trigger_->set_enable();
207 0 : }
208 :
209 0 : bool RTargetGroupMgr::IsRTargetRouteOnList(RTargetRoute *rt) const {
210 0 : return rtarget_route_list_.find(rt) != rtarget_route_list_.end();
211 : }
212 :
213 418 : void RTargetGroupMgr::Initialize() {
214 418 : assert(table_state_.empty());
215 418 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
216 418 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
217 418 : assert(master);
218 :
219 418 : master_instance_delete_ref_.Reset(master->deleter());
220 :
221 418 : RoutingInstance::RouteTableList const table_list = master->GetTables();
222 : DBTableBase::ListenerId id;
223 418 : RtGroupMgrTableState *ts = NULL;
224 418 : for (RoutingInstance::RouteTableList::const_iterator it =
225 4598 : table_list.begin(); it != table_list.end(); ++it) {
226 3762 : if (!it->second->IsVpnTable()) continue;
227 :
228 2090 : BgpTable *vpntable = it->second;
229 2090 : id = vpntable->Register(
230 : boost::bind(&RTargetGroupMgr::VpnRouteNotify, this, _1, _2),
231 : "RTargetGroupMgr");
232 2090 : ts = new RtGroupMgrTableState(vpntable, id);
233 2090 : table_state_.insert(std::make_pair(vpntable, ts));
234 : }
235 :
236 418 : BgpTable *rttable = master->GetTable(Address::RTARGET);
237 418 : id = rttable->Register(
238 : boost::bind(&RTargetGroupMgr::RTargetRouteNotify, this, _1, _2),
239 : "RTargetGroupMgr");
240 418 : ts = new RtGroupMgrTableState(rttable, id);
241 418 : table_state_.insert(std::make_pair(rttable, ts));
242 418 : }
243 :
244 418 : void RTargetGroupMgr::ManagedDelete() {
245 418 : if (rtgroup_map_.empty()) remove_rtgroup_trigger_->Set();
246 418 : }
247 :
248 : void
249 64222 : RTargetGroupMgr::RTargetDepSync(DBTablePartBase *root, BgpRoute *rt,
250 : DBTableBase::ListenerId id,
251 : VpnRouteState *dbstate,
252 : const VpnRouteState::RTargetList *future) {
253 64222 : CHECK_CONCURRENCY("db::DBTable");
254 :
255 64127 : BgpTable *table = static_cast<BgpTable *>(root->parent());
256 64127 : if (!dbstate) {
257 15881 : dbstate = new VpnRouteState();
258 15882 : rt->SetState(table, id, dbstate);
259 : }
260 :
261 64133 : int part_id = root->index();
262 64129 : set_synchronize(dbstate->GetMutableList(), future,
263 : boost::bind(
264 : &VpnRouteState::AddRouteTarget, dbstate, this, part_id, rt, _1),
265 : boost::bind(
266 : &VpnRouteState::DeleteRouteTarget, dbstate, this, part_id, rt, _1));
267 :
268 64112 : if (dbstate->GetList()->empty()) {
269 15888 : rt->ClearState(root->parent(), id);
270 15888 : delete dbstate;
271 : }
272 64182 : }
273 :
274 74449 : DBTableBase::ListenerId RTargetGroupMgr::GetListenerId(BgpTable *table) {
275 74449 : RtGroupMgrTableStateList::iterator loc = table_state_.find(table);
276 74438 : assert(loc != table_state_.end());
277 74429 : RtGroupMgrTableState *ts = loc->second;
278 74434 : DBTableBase::ListenerId id = ts->GetListenerId();
279 74431 : assert(id != DBTableBase::kInvalidId);
280 74431 : return id;
281 : }
282 :
283 64179 : bool RTargetGroupMgr::VpnRouteNotify(DBTablePartBase *root,
284 : DBEntryBase *entry) {
285 64179 : CHECK_CONCURRENCY("db::DBTable");
286 :
287 64163 : BgpTable *table = static_cast<BgpTable *>(root->parent());
288 64165 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
289 : // Get the Listener id
290 64165 : DBTableBase::ListenerId id = GetListenerId(table);
291 :
292 : // Get the dbstate
293 : VpnRouteState *dbstate =
294 64142 : static_cast<VpnRouteState *>(rt->GetState(table, id));
295 :
296 64287 : VpnRouteState::RTargetList list;
297 :
298 112591 : if (entry->IsDeleted() || !rt->BestPath() ||
299 48341 : !rt->BestPath()->IsFeasible()) {
300 15883 : if (!dbstate)
301 16 : return true;
302 15867 : RTargetDepSync(root, rt, id, dbstate, &list);
303 15871 : return true;
304 : }
305 :
306 48332 : const BgpPath *path = rt->BestPath();
307 48322 : const BgpAttr *attr = path->GetAttr();
308 48314 : const ExtCommunity *ext_community = attr->ext_community();
309 :
310 48310 : if (ext_community) {
311 : // Gather all Route Target
312 622516 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
313 : ext_community->communities()) {
314 286695 : if (ExtCommunity::is_route_target(comm)) {
315 192633 : list.insert(RouteTarget(comm));
316 : }
317 : }
318 : }
319 :
320 48356 : RTargetDepSync(root, rt, id, dbstate, &list);
321 48311 : return true;
322 64198 : }
323 :
324 7738 : bool RTargetGroupMgr::RTargetRouteNotify(DBTablePartBase *root,
325 : DBEntryBase *entry) {
326 7738 : CHECK_CONCURRENCY("db::DBTable");
327 :
328 7738 : BgpTable *table = static_cast<BgpTable *>(root->parent());
329 7738 : RTargetRoute *rt = static_cast<RTargetRoute *>(entry);
330 : // Get the Listener id
331 7738 : DBTableBase::ListenerId id = GetListenerId(table);
332 :
333 : // Get the dbstate
334 : RTargetState *dbstate =
335 7738 : static_cast<RTargetState *>(rt->GetState(table, id));
336 :
337 7738 : if (!dbstate) {
338 4648 : if (rt->IsDeleted()) return true;
339 4329 : dbstate = new RTargetState();
340 4329 : rt->SetState(table, id, dbstate);
341 : }
342 7419 : if (rtarget_route_list_.empty())
343 2559 : rtarget_route_trigger_->Set();
344 7419 : rtarget_route_list_.insert(rt);
345 7419 : return true;
346 : }
347 :
348 836 : RTargetGroupMgr::~RTargetGroupMgr() {
349 418 : assert(rtgroup_map_.empty());
350 836 : }
351 :
352 : // Search a RtGroup
353 364553 : RtGroup *RTargetGroupMgr::GetRtGroup(const RouteTarget &rt) {
354 364553 : std::scoped_lock lock(mutex_);
355 365054 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
356 364967 : if (loc != rtgroup_map_.end()) {
357 328913 : return loc->second;
358 : }
359 36042 : return NULL;
360 364942 : }
361 :
362 : // Search a RtGroup
363 242304 : RtGroup *RTargetGroupMgr::GetRtGroup(const ExtCommunity::ExtCommunityValue
364 : &community) {
365 242304 : RouteTarget rt(community);
366 484814 : return GetRtGroup(rt);
367 : }
368 :
369 78632 : RtGroup *RTargetGroupMgr::LocateRtGroup(const RouteTarget &rt) {
370 78632 : std::scoped_lock lock(mutex_);
371 78645 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
372 78641 : RtGroup *group = (loc != rtgroup_map_.end()) ? loc->second : NULL;
373 78642 : if (group == NULL) {
374 2044 : group = new RtGroup(rt);
375 2044 : rtgroup_map_.insert(rt, group);
376 : }
377 78649 : return group;
378 78642 : }
379 :
380 4544 : void RTargetGroupMgr::NotifyRtGroupUnlocked(const RouteTarget &rt) {
381 4544 : CHECK_CONCURRENCY("bgp::RTFilter", "bgp::Config", "bgp::ConfigHelper");
382 :
383 4544 : AddRouteTargetToLists(rt);
384 4544 : if (!rt.IsNull())
385 4544 : return;
386 :
387 0 : for (RtGroupMgrTableStateList::iterator it = table_state_.begin();
388 0 : it != table_state_.end(); ++it) {
389 0 : BgpTable *table = it->first;
390 0 : if (!table->IsVpnTable())
391 0 : continue;
392 0 : table->NotifyAllEntries();
393 : }
394 : }
395 :
396 0 : void RTargetGroupMgr::NotifyRtGroup(const RouteTarget &rt) {
397 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
398 0 : std::scoped_lock lock(mutex_);
399 0 : NotifyRtGroupUnlocked(rt);
400 0 : }
401 :
402 69602 : void RTargetGroupMgr::RemoveRtGroup(const RouteTarget &rt) {
403 69602 : std::scoped_lock lock(mutex_);
404 69614 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
405 69610 : RtGroup *rtgroup = (loc != rtgroup_map_.end()) ? loc->second : NULL;
406 69610 : assert(rtgroup);
407 :
408 69610 : rtgroup_remove_list_.insert(rtgroup);
409 69617 : remove_rtgroup_trigger_->Set();
410 69615 : }
411 :
412 34306 : void RTargetGroupMgr::GetRibOutInterestedPeers(RibOut *ribout,
413 : const ExtCommunity *ext_community,
414 : const RibPeerSet &peerset, RibPeerSet *new_peerset) {
415 34306 : RtGroupInterestedPeerSet peer_set;
416 34308 : RtGroup *null_rtgroup = GetRtGroup(RouteTarget::null_rtarget);
417 34351 : if (null_rtgroup) peer_set = null_rtgroup->GetInterestedPeers();
418 450565 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
419 : ext_community->communities()) {
420 208096 : if (ExtCommunity::is_route_target(comm)) {
421 137370 : RtGroup *rtgroup = GetRtGroup(comm);
422 137400 : if (!rtgroup) continue;
423 137400 : peer_set |= rtgroup->GetInterestedPeers();
424 : }
425 : }
426 34351 : RibOut::PeerIterator iter(ribout, peerset);
427 68696 : while (iter.HasNext()) {
428 34350 : int current_index = iter.index();
429 34350 : IPeerUpdate *peer = iter.Next();
430 34348 : BgpPeer *tmp = dynamic_cast<BgpPeer *>(peer);
431 34348 : assert(tmp);
432 34348 : if (tmp->IsFamilyNegotiated(Address::RTARGET)) {
433 34350 : if (!peer_set.test(tmp->GetIndex())) {
434 1030 : new_peerset->reset(current_index);
435 : }
436 : }
437 : }
438 34343 : }
439 :
440 687 : void RTargetGroupMgr::UnregisterTables() {
441 687 : CHECK_CONCURRENCY("bgp::RTFilter");
442 :
443 687 : if (rtgroup_map_.empty()) {
444 687 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
445 687 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
446 687 : if (master && master->deleted()) {
447 418 : for (RtGroupMgrTableStateList::iterator it =
448 3344 : table_state_.begin(), itnext; it != table_state_.end();
449 2508 : it = itnext) {
450 2508 : itnext = it;
451 2508 : itnext++;
452 2508 : RtGroupMgrTableState *ts = it->second;
453 2508 : DBTableBase::ListenerId id = ts->GetListenerId();
454 2508 : BgpTable *bgptable = it->first;
455 2508 : bgptable->Unregister(id);
456 2508 : table_state_.erase(it);
457 2508 : delete ts;
458 : }
459 418 : master_instance_delete_ref_.Reset(NULL);
460 : }
461 : }
462 687 : }
463 :
464 5742 : bool RTargetGroupMgr::ProcessRtGroupList() {
465 5742 : CHECK_CONCURRENCY("bgp::RTFilter");
466 45522 : BOOST_FOREACH(RtGroup *rtgroup, rtgroup_remove_list_) {
467 19890 : if (!rtgroup->MayDelete())
468 17846 : continue;
469 2044 : RouteTarget rt = rtgroup->rt();
470 2044 : rtgroup_map_.erase(rt);
471 : }
472 5742 : rtgroup_remove_list_.clear();
473 :
474 5742 : if (rtgroup_map_.empty()) UnregisterTables();
475 :
476 5742 : return true;
477 : }
478 :
479 0 : void RTargetGroupMgr::DisableRtGroupProcessing() {
480 0 : remove_rtgroup_trigger_->set_disable();
481 0 : }
482 :
483 0 : void RTargetGroupMgr::EnableRtGroupProcessing() {
484 0 : remove_rtgroup_trigger_->set_enable();
485 0 : }
486 :
487 0 : bool RTargetGroupMgr::IsRtGroupOnList(RtGroup *rtgroup) const {
488 0 : return rtgroup_remove_list_.find(rtgroup) != rtgroup_remove_list_.end();
489 : }
490 :
491 2508 : RtGroupMgrTableState::RtGroupMgrTableState(BgpTable *table,
492 2508 : DBTableBase::ListenerId id)
493 2508 : : id_(id), table_delete_ref_(this, table->deleter()) {
494 2508 : assert(table->deleter() != NULL);
495 2508 : }
496 :
497 2508 : RtGroupMgrTableState::~RtGroupMgrTableState() {
498 2508 : }
499 :
500 1092 : void RtGroupMgrTableState::ManagedDelete() {
501 1092 : }
|