Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 : #include "bgp/routing-instance/rtarget_group_mgr.h"
6 :
7 : #include <boost/foreach.hpp>
8 :
9 : #include <utility>
10 :
11 : #include "base/map_util.h"
12 : #include "base/set_util.h"
13 : #include "base/task_annotations.h"
14 : #include "base/task_trigger.h"
15 : #include "bgp/bgp_config.h"
16 : #include "bgp/bgp_peer.h"
17 : #include "bgp/bgp_ribout.h"
18 : #include "bgp/bgp_server.h"
19 : #include "bgp/bgp_table.h"
20 : #include "bgp/routing-instance/routing_instance.h"
21 : #include "bgp/rtarget/rtarget_route.h"
22 :
23 : using std::pair;
24 :
25 499578 : void VpnRouteState::AddRouteTarget(RTargetGroupMgr *mgr, int part_id,
26 : BgpRoute *rt, RTargetList::const_iterator it) {
27 499578 : pair<RTargetList::iterator, bool> result;
28 499573 : result = list_.insert(*it);
29 499663 : assert(result.second);
30 499663 : RtGroup *rtgroup = mgr->LocateRtGroup(*it);
31 499680 : rtgroup->AddDepRoute(part_id, rt);
32 499673 : }
33 :
34 499480 : void VpnRouteState::DeleteRouteTarget(RTargetGroupMgr *mgr, int part_id,
35 : BgpRoute *rt, RTargetList::const_iterator it) {
36 499480 : RtGroup *rtgroup = mgr->GetRtGroup(*it);
37 499673 : rtgroup->RemoveDepRoute(part_id, rt);
38 499649 : mgr->RemoveRtGroup(*it);
39 499678 : list_.erase(it);
40 499664 : }
41 :
42 27064 : void RTargetState::AddInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
43 : RTargetRoute *rt, RtGroup::InterestedPeerList::const_iterator it) {
44 27064 : pair<RtGroup::InterestedPeerList::iterator, bool> result;
45 27064 : result = list_.insert(*it);
46 27064 : assert(result.second);
47 27064 : rtgroup->AddInterestedPeer(it->first, rt);
48 27064 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
49 27064 : }
50 :
51 27064 : void RTargetState::DeleteInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
52 : RTargetRoute *rt, RtGroup::InterestedPeerList::iterator it) {
53 27064 : rtgroup->RemoveInterestedPeer(it->first, rt);
54 27064 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
55 27064 : list_.erase(it);
56 27064 : }
57 :
58 9743 : RTargetGroupMgr::RTargetGroupMgr(BgpServer *server) : server_(server),
59 19486 : rtarget_route_trigger_(new TaskTrigger(
60 : boost::bind(&RTargetGroupMgr::ProcessRTargetRouteList, this),
61 19486 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
62 19486 : remove_rtgroup_trigger_(new TaskTrigger(
63 : boost::bind(&RTargetGroupMgr::ProcessRtGroupList, this),
64 19486 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
65 9743 : rtarget_trigger_lists_(DB::PartitionCount()),
66 29229 : master_instance_delete_ref_(this, NULL) {
67 48190 : for (int i = 0; i < DB::PartitionCount(); i++) {
68 115341 : rtarget_dep_triggers_.push_back(boost::shared_ptr<TaskTrigger>(new
69 : TaskTrigger(boost::bind(&RTargetGroupMgr::ProcessRouteTargetList,
70 : this, i),
71 76894 : TaskScheduler::GetInstance()->GetTaskId("db::DBTable"), i)));
72 : }
73 9743 : }
74 :
75 103024 : void RTargetGroupMgr::RTargetPeerSync(BgpTable *table, RTargetRoute *rt,
76 : DBTableBase::ListenerId id, RTargetState *dbstate,
77 : const RtGroup::InterestedPeerList *future) {
78 103024 : CHECK_CONCURRENCY("bgp::RTFilter");
79 :
80 103024 : RouteTarget rtarget = rt->GetPrefix().rtarget();
81 103024 : RtGroup *rtgroup = LocateRtGroup(rtarget);
82 103024 : assert(rtgroup);
83 :
84 103024 : map_synchronize(dbstate->GetMutableList(), future,
85 : boost::bind(&RTargetState::AddInterestedPeer, dbstate, this, rtgroup,
86 : rt, _1),
87 : boost::bind(&RTargetState::DeleteInterestedPeer, dbstate, this, rtgroup,
88 : rt, _1));
89 :
90 103024 : if (dbstate->GetList()->empty()) {
91 64789 : rt->ClearState(table, id);
92 64789 : delete dbstate;
93 64789 : RemoveRtGroup(rtarget);
94 : }
95 103024 : }
96 :
97 103024 : void RTargetGroupMgr::BuildRTargetDistributionGraph(BgpTable *table,
98 : RTargetRoute *rt, DBTableBase::ListenerId id) {
99 103024 : CHECK_CONCURRENCY("bgp::RTFilter");
100 :
101 : RTargetState *dbstate =
102 103024 : static_cast<RTargetState *>(rt->GetState(table, id));
103 :
104 103024 : RtGroup::InterestedPeerList peer_list;
105 :
106 179967 : if (rt->IsDeleted() || !rt->BestPath() ||
107 76943 : !rt->BestPath()->IsFeasible()) {
108 26081 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
109 26081 : return;
110 : }
111 :
112 76943 : const BgpPath *best_ebgp_path = NULL;
113 223411 : for (Route::PathList::iterator it = rt->GetPathList().begin();
114 446822 : it != rt->GetPathList().end(); it++) {
115 146468 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
116 146468 : if (!path->IsFeasible())
117 0 : break;
118 146468 : if (!path->GetPeer() || path->GetPeer()->IsXmppPeer())
119 106983 : continue;
120 :
121 39553 : const BgpPeer *peer = static_cast<const BgpPeer *>(path->GetPeer());
122 39553 : if (peer->PeerType() == BgpProto::EBGP) {
123 17209 : if (!best_ebgp_path) {
124 17037 : best_ebgp_path = path;
125 172 : } else if (!best_ebgp_path->PathSameNeighborAs(*path)) {
126 68 : continue;
127 : }
128 : }
129 :
130 : std::pair<RtGroup::InterestedPeerList::iterator, bool> ret =
131 39485 : peer_list.insert(std::pair<const BgpPeer *,
132 78970 : RtGroup::RTargetRouteList>(peer, RtGroup::RTargetRouteList()));
133 39485 : assert(ret.second);
134 39485 : ret.first->second.insert(rt);
135 : }
136 :
137 76943 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
138 103024 : }
139 :
140 162265 : bool RTargetGroupMgr::ProcessRouteTargetList(int part_id) {
141 162265 : CHECK_CONCURRENCY("db::DBTable");
142 :
143 758650 : BOOST_FOREACH(const RouteTarget &rtarget, rtarget_trigger_lists_[part_id]) {
144 297698 : RtGroup *rtgroup = GetRtGroup(rtarget);
145 299514 : if (!rtgroup)
146 27650 : continue;
147 271864 : rtgroup->NotifyDepRoutes(part_id);
148 : }
149 :
150 162375 : rtarget_trigger_lists_[part_id].clear();
151 162404 : return true;
152 : }
153 :
154 186463 : void RTargetGroupMgr::AddRouteTargetToLists(const RouteTarget &rtarget) {
155 915473 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
156 729010 : rtarget_trigger_lists_[idx].insert(rtarget);
157 729010 : rtarget_dep_triggers_[idx]->Set();
158 : }
159 186463 : }
160 :
161 8 : void RTargetGroupMgr::DisableRouteTargetProcessing() {
162 40 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
163 32 : rtarget_dep_triggers_[idx]->set_disable();
164 : }
165 8 : }
166 :
167 8 : void RTargetGroupMgr::EnableRouteTargetProcessing() {
168 40 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
169 32 : rtarget_dep_triggers_[idx]->set_enable();
170 : }
171 8 : }
172 :
173 20 : bool RTargetGroupMgr::IsRouteTargetOnList(const RouteTarget &rtarget) const {
174 56 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
175 47 : if (rtarget_trigger_lists_[idx].find(rtarget) !=
176 94 : rtarget_trigger_lists_[idx].end()) {
177 11 : return true;
178 : }
179 : }
180 9 : return false;
181 : }
182 :
183 41186 : bool RTargetGroupMgr::ProcessRTargetRouteList() {
184 41186 : CHECK_CONCURRENCY("bgp::RTFilter");
185 :
186 41186 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
187 41186 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
188 41186 : BgpTable *table = master->GetTable(Address::RTARGET);
189 :
190 : // Get the Listener id
191 41186 : DBTableBase::ListenerId id = GetListenerId(table);
192 :
193 41186 : for (RTargetRouteTriggerList::iterator it = rtarget_route_list_.begin();
194 144210 : it != rtarget_route_list_.end(); it++) {
195 103024 : BuildRTargetDistributionGraph(table, *it, id);
196 : }
197 :
198 41186 : rtarget_route_list_.clear();
199 41186 : return true;
200 : }
201 :
202 8 : void RTargetGroupMgr::DisableRTargetRouteProcessing() {
203 8 : rtarget_route_trigger_->set_disable();
204 8 : }
205 :
206 8 : void RTargetGroupMgr::EnableRTargetRouteProcessing() {
207 8 : rtarget_route_trigger_->set_enable();
208 8 : }
209 :
210 19 : bool RTargetGroupMgr::IsRTargetRouteOnList(RTargetRoute *rt) const {
211 19 : return rtarget_route_list_.find(rt) != rtarget_route_list_.end();
212 : }
213 :
214 6510 : void RTargetGroupMgr::Initialize() {
215 6510 : assert(table_state_.empty());
216 6510 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
217 6510 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
218 6510 : assert(master);
219 :
220 6510 : master_instance_delete_ref_.Reset(master->deleter());
221 :
222 6510 : RoutingInstance::RouteTableList const table_list = master->GetTables();
223 : DBTableBase::ListenerId id;
224 6510 : RtGroupMgrTableState *ts = NULL;
225 6510 : for (RoutingInstance::RouteTableList::const_iterator it =
226 71610 : table_list.begin(); it != table_list.end(); ++it) {
227 58590 : if (!it->second->IsVpnTable()) continue;
228 :
229 32550 : BgpTable *vpntable = it->second;
230 32550 : id = vpntable->Register(
231 : boost::bind(&RTargetGroupMgr::VpnRouteNotify, this, _1, _2),
232 : "RTargetGroupMgr");
233 32550 : ts = new RtGroupMgrTableState(vpntable, id);
234 32550 : table_state_.insert(std::make_pair(vpntable, ts));
235 : }
236 :
237 6510 : BgpTable *rttable = master->GetTable(Address::RTARGET);
238 6510 : id = rttable->Register(
239 : boost::bind(&RTargetGroupMgr::RTargetRouteNotify, this, _1, _2),
240 : "RTargetGroupMgr");
241 6510 : ts = new RtGroupMgrTableState(rttable, id);
242 6510 : table_state_.insert(std::make_pair(rttable, ts));
243 6510 : }
244 :
245 6510 : void RTargetGroupMgr::ManagedDelete() {
246 6510 : if (rtgroup_map_.empty()) remove_rtgroup_trigger_->Set();
247 6510 : }
248 :
249 : void
250 611796 : RTargetGroupMgr::RTargetDepSync(DBTablePartBase *root, BgpRoute *rt,
251 : DBTableBase::ListenerId id,
252 : VpnRouteState *dbstate,
253 : const VpnRouteState::RTargetList *future) {
254 611796 : CHECK_CONCURRENCY("db::DBTable");
255 :
256 611692 : BgpTable *table = static_cast<BgpTable *>(root->parent());
257 611692 : if (!dbstate) {
258 184744 : dbstate = new VpnRouteState();
259 184743 : rt->SetState(table, id, dbstate);
260 : }
261 :
262 611700 : int part_id = root->index();
263 611696 : set_synchronize(dbstate->GetMutableList(), future,
264 : boost::bind(
265 : &VpnRouteState::AddRouteTarget, dbstate, this, part_id, rt, _1),
266 : boost::bind(
267 : &VpnRouteState::DeleteRouteTarget, dbstate, this, part_id, rt, _1));
268 :
269 611729 : if (dbstate->GetList()->empty()) {
270 184755 : rt->ClearState(root->parent(), id);
271 184754 : delete dbstate;
272 : }
273 611723 : }
274 :
275 772371 : DBTableBase::ListenerId RTargetGroupMgr::GetListenerId(BgpTable *table) {
276 772371 : RtGroupMgrTableStateList::iterator loc = table_state_.find(table);
277 772273 : assert(loc != table_state_.end());
278 772249 : RtGroupMgrTableState *ts = loc->second;
279 772244 : DBTableBase::ListenerId id = ts->GetListenerId();
280 772240 : assert(id != DBTableBase::kInvalidId);
281 772240 : return id;
282 : }
283 :
284 611827 : bool RTargetGroupMgr::VpnRouteNotify(DBTablePartBase *root,
285 : DBEntryBase *entry) {
286 611827 : CHECK_CONCURRENCY("db::DBTable");
287 :
288 611803 : BgpTable *table = static_cast<BgpTable *>(root->parent());
289 611790 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
290 : // Get the Listener id
291 611790 : DBTableBase::ListenerId id = GetListenerId(table);
292 :
293 : // Get the dbstate
294 : VpnRouteState *dbstate =
295 611670 : static_cast<VpnRouteState *>(rt->GetState(table, id));
296 :
297 612022 : VpnRouteState::RTargetList list;
298 :
299 1038959 : if (entry->IsDeleted() || !rt->BestPath() ||
300 427045 : !rt->BestPath()->IsFeasible()) {
301 184834 : if (!dbstate)
302 113 : return true;
303 184721 : RTargetDepSync(root, rt, id, dbstate, &list);
304 184734 : return true;
305 : }
306 :
307 427012 : const BgpPath *path = rt->BestPath();
308 426977 : const BgpAttr *attr = path->GetAttr();
309 426966 : const ExtCommunity *ext_community = attr->ext_community();
310 :
311 426960 : if (ext_community) {
312 : // Gather all Route Target
313 7764028 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
314 : ext_community->communities()) {
315 3667643 : if (ExtCommunity::is_route_target(comm)) {
316 2960058 : list.insert(RouteTarget(comm));
317 : }
318 : }
319 : }
320 :
321 427076 : RTargetDepSync(root, rt, id, dbstate, &list);
322 426990 : return true;
323 611837 : }
324 :
325 119398 : bool RTargetGroupMgr::RTargetRouteNotify(DBTablePartBase *root,
326 : DBEntryBase *entry) {
327 119398 : CHECK_CONCURRENCY("db::DBTable");
328 :
329 119398 : BgpTable *table = static_cast<BgpTable *>(root->parent());
330 119398 : RTargetRoute *rt = static_cast<RTargetRoute *>(entry);
331 : // Get the Listener id
332 119398 : DBTableBase::ListenerId id = GetListenerId(table);
333 :
334 : // Get the dbstate
335 : RTargetState *dbstate =
336 119398 : static_cast<RTargetState *>(rt->GetState(table, id));
337 :
338 119398 : if (!dbstate) {
339 78954 : if (rt->IsDeleted()) return true;
340 64789 : dbstate = new RTargetState();
341 64789 : rt->SetState(table, id, dbstate);
342 : }
343 105233 : if (rtarget_route_list_.empty())
344 41186 : rtarget_route_trigger_->Set();
345 105233 : rtarget_route_list_.insert(rt);
346 105233 : return true;
347 : }
348 :
349 18648 : RTargetGroupMgr::~RTargetGroupMgr() {
350 9743 : assert(rtgroup_map_.empty());
351 18648 : }
352 :
353 : // Search a RtGroup
354 6454752 : RtGroup *RTargetGroupMgr::GetRtGroup(const RouteTarget &rt) {
355 6454752 : std::scoped_lock lock(mutex_);
356 6458537 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
357 6457789 : if (loc != rtgroup_map_.end()) {
358 6107958 : return loc->second;
359 : }
360 349724 : return NULL;
361 6457587 : }
362 :
363 : // Search a RtGroup
364 4408840 : RtGroup *RTargetGroupMgr::GetRtGroup(const ExtCommunity::ExtCommunityValue
365 : &community) {
366 4408840 : RouteTarget rt(community);
367 8818845 : return GetRtGroup(rt);
368 : }
369 :
370 1531166 : RtGroup *RTargetGroupMgr::LocateRtGroup(const RouteTarget &rt) {
371 1531166 : std::scoped_lock lock(mutex_);
372 1531323 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
373 1531318 : RtGroup *group = (loc != rtgroup_map_.end()) ? loc->second : NULL;
374 1531313 : if (group == NULL) {
375 108855 : group = new RtGroup(rt);
376 108854 : rtgroup_map_.insert(rt, group);
377 : }
378 1531321 : return group;
379 1531313 : }
380 :
381 186463 : void RTargetGroupMgr::NotifyRtGroupUnlocked(const RouteTarget &rt) {
382 186463 : CHECK_CONCURRENCY("bgp::RTFilter", "bgp::Config", "bgp::ConfigHelper");
383 :
384 186463 : AddRouteTargetToLists(rt);
385 186463 : if (!rt.IsNull())
386 186447 : return;
387 :
388 16 : for (RtGroupMgrTableStateList::iterator it = table_state_.begin();
389 112 : it != table_state_.end(); ++it) {
390 96 : BgpTable *table = it->first;
391 96 : if (!table->IsVpnTable())
392 16 : continue;
393 80 : table->NotifyAllEntries();
394 : }
395 : }
396 :
397 132335 : void RTargetGroupMgr::NotifyRtGroup(const RouteTarget &rt) {
398 132335 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
399 132334 : std::scoped_lock lock(mutex_);
400 132335 : NotifyRtGroupUnlocked(rt);
401 132335 : }
402 :
403 994389 : void RTargetGroupMgr::RemoveRtGroup(const RouteTarget &rt) {
404 994389 : std::scoped_lock lock(mutex_);
405 994417 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
406 994389 : RtGroup *rtgroup = (loc != rtgroup_map_.end()) ? loc->second : NULL;
407 994382 : assert(rtgroup);
408 :
409 994382 : rtgroup_remove_list_.insert(rtgroup);
410 994439 : remove_rtgroup_trigger_->Set();
411 994440 : }
412 :
413 320320 : void RTargetGroupMgr::GetRibOutInterestedPeers(RibOut *ribout,
414 : const ExtCommunity *ext_community,
415 : const RibPeerSet &peerset, RibPeerSet *new_peerset) {
416 320320 : RtGroupInterestedPeerSet peer_set;
417 320327 : RtGroup *null_rtgroup = GetRtGroup(RouteTarget::null_rtarget);
418 320560 : if (null_rtgroup) peer_set = null_rtgroup->GetInterestedPeers();
419 6137429 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
420 : ext_community->communities()) {
421 2908328 : if (ExtCommunity::is_route_target(comm)) {
422 2251046 : RtGroup *rtgroup = GetRtGroup(comm);
423 2251344 : if (!rtgroup) continue;
424 2251344 : peer_set |= rtgroup->GetInterestedPeers();
425 : }
426 : }
427 320548 : RibOut::PeerIterator iter(ribout, peerset);
428 644802 : while (iter.HasNext()) {
429 324255 : int current_index = iter.index();
430 324255 : IPeerUpdate *peer = iter.Next();
431 324247 : BgpPeer *tmp = dynamic_cast<BgpPeer *>(peer);
432 324247 : assert(tmp);
433 324247 : if (tmp->IsFamilyNegotiated(Address::RTARGET)) {
434 315649 : if (!peer_set.test(tmp->GetIndex())) {
435 22859 : new_peerset->reset(current_index);
436 : }
437 : }
438 : }
439 320512 : }
440 :
441 10781 : void RTargetGroupMgr::UnregisterTables() {
442 10781 : CHECK_CONCURRENCY("bgp::RTFilter");
443 :
444 10781 : if (rtgroup_map_.empty()) {
445 10781 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
446 10781 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
447 10781 : if (master && master->deleted()) {
448 7963 : for (RtGroupMgrTableStateList::iterator it =
449 54986 : table_state_.begin(), itnext; it != table_state_.end();
450 39060 : it = itnext) {
451 39060 : itnext = it;
452 39060 : itnext++;
453 39060 : RtGroupMgrTableState *ts = it->second;
454 39060 : DBTableBase::ListenerId id = ts->GetListenerId();
455 39060 : BgpTable *bgptable = it->first;
456 39060 : bgptable->Unregister(id);
457 39060 : table_state_.erase(it);
458 39060 : delete ts;
459 : }
460 7963 : master_instance_delete_ref_.Reset(NULL);
461 : }
462 : }
463 10781 : }
464 :
465 144162 : bool RTargetGroupMgr::ProcessRtGroupList() {
466 144162 : CHECK_CONCURRENCY("bgp::RTFilter");
467 797958 : BOOST_FOREACH(RtGroup *rtgroup, rtgroup_remove_list_) {
468 326898 : if (!rtgroup->MayDelete())
469 218043 : continue;
470 108855 : RouteTarget rt = rtgroup->rt();
471 108855 : rtgroup_map_.erase(rt);
472 : }
473 144162 : rtgroup_remove_list_.clear();
474 :
475 144162 : if (rtgroup_map_.empty()) UnregisterTables();
476 :
477 144162 : return true;
478 : }
479 :
480 3 : void RTargetGroupMgr::DisableRtGroupProcessing() {
481 3 : remove_rtgroup_trigger_->set_disable();
482 3 : }
483 :
484 3 : void RTargetGroupMgr::EnableRtGroupProcessing() {
485 3 : remove_rtgroup_trigger_->set_enable();
486 3 : }
487 :
488 12 : bool RTargetGroupMgr::IsRtGroupOnList(RtGroup *rtgroup) const {
489 12 : return rtgroup_remove_list_.find(rtgroup) != rtgroup_remove_list_.end();
490 : }
491 :
492 39060 : RtGroupMgrTableState::RtGroupMgrTableState(BgpTable *table,
493 39060 : DBTableBase::ListenerId id)
494 39060 : : id_(id), table_delete_ref_(this, table->deleter()) {
495 39060 : assert(table->deleter() != NULL);
496 39060 : }
497 :
498 39060 : RtGroupMgrTableState::~RtGroupMgrTableState() {
499 39060 : }
500 :
501 34500 : void RtGroupMgrTableState::ManagedDelete() {
502 34500 : }
|