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 1384 : void VpnRouteState::AddRouteTarget(RTargetGroupMgr *mgr, int part_id,
26 : BgpRoute *rt, RTargetList::const_iterator it) {
27 1384 : pair<RTargetList::iterator, bool> result;
28 1384 : result = list_.insert(*it);
29 1384 : assert(result.second);
30 1384 : RtGroup *rtgroup = mgr->LocateRtGroup(*it);
31 1384 : rtgroup->AddDepRoute(part_id, rt);
32 1384 : }
33 :
34 1384 : void VpnRouteState::DeleteRouteTarget(RTargetGroupMgr *mgr, int part_id,
35 : BgpRoute *rt, RTargetList::const_iterator it) {
36 1384 : RtGroup *rtgroup = mgr->GetRtGroup(*it);
37 1384 : rtgroup->RemoveDepRoute(part_id, rt);
38 1384 : mgr->RemoveRtGroup(*it);
39 1384 : list_.erase(it);
40 1384 : }
41 :
42 99 : void RTargetState::AddInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
43 : RTargetRoute *rt, RtGroup::InterestedPeerList::const_iterator it) {
44 99 : pair<RtGroup::InterestedPeerList::iterator, bool> result;
45 99 : result = list_.insert(*it);
46 99 : assert(result.second);
47 99 : rtgroup->AddInterestedPeer(it->first, rt);
48 99 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
49 99 : }
50 :
51 99 : void RTargetState::DeleteInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
52 : RTargetRoute *rt, RtGroup::InterestedPeerList::iterator it) {
53 99 : rtgroup->RemoveInterestedPeer(it->first, rt);
54 99 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
55 99 : list_.erase(it);
56 99 : }
57 :
58 68 : RTargetGroupMgr::RTargetGroupMgr(BgpServer *server) : server_(server),
59 136 : rtarget_route_trigger_(new TaskTrigger(
60 : boost::bind(&RTargetGroupMgr::ProcessRTargetRouteList, this),
61 136 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
62 136 : remove_rtgroup_trigger_(new TaskTrigger(
63 : boost::bind(&RTargetGroupMgr::ProcessRtGroupList, this),
64 136 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
65 68 : rtarget_trigger_lists_(DB::PartitionCount()),
66 204 : master_instance_delete_ref_(this, NULL) {
67 340 : for (int i = 0; i < DB::PartitionCount(); i++) {
68 816 : rtarget_dep_triggers_.push_back(boost::shared_ptr<TaskTrigger>(new
69 : TaskTrigger(boost::bind(&RTargetGroupMgr::ProcessRouteTargetList,
70 : this, i),
71 544 : TaskScheduler::GetInstance()->GetTaskId("db::DBTable"), i)));
72 : }
73 68 : }
74 :
75 347 : void RTargetGroupMgr::RTargetPeerSync(BgpTable *table, RTargetRoute *rt,
76 : DBTableBase::ListenerId id, RTargetState *dbstate,
77 : const RtGroup::InterestedPeerList *future) {
78 347 : CHECK_CONCURRENCY("bgp::RTFilter");
79 :
80 347 : RouteTarget rtarget = rt->GetPrefix().rtarget();
81 347 : RtGroup *rtgroup = LocateRtGroup(rtarget);
82 347 : assert(rtgroup);
83 :
84 347 : 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 347 : if (dbstate->GetList()->empty()) {
91 179 : rt->ClearState(table, id);
92 179 : delete dbstate;
93 179 : RemoveRtGroup(rtarget);
94 : }
95 347 : }
96 :
97 347 : void RTargetGroupMgr::BuildRTargetDistributionGraph(BgpTable *table,
98 : RTargetRoute *rt, DBTableBase::ListenerId id) {
99 347 : CHECK_CONCURRENCY("bgp::RTFilter");
100 :
101 : RTargetState *dbstate =
102 347 : static_cast<RTargetState *>(rt->GetState(table, id));
103 :
104 347 : RtGroup::InterestedPeerList peer_list;
105 :
106 694 : if (rt->IsDeleted() || !rt->BestPath() ||
107 347 : !rt->BestPath()->IsFeasible()) {
108 0 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
109 0 : return;
110 : }
111 :
112 347 : const BgpPath *best_ebgp_path = NULL;
113 886 : for (Route::PathList::iterator it = rt->GetPathList().begin();
114 1772 : it != rt->GetPathList().end(); it++) {
115 539 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
116 539 : if (!path->IsFeasible())
117 0 : break;
118 539 : if (!path->GetPeer() || path->GetPeer()->IsXmppPeer())
119 302 : continue;
120 :
121 237 : const BgpPeer *peer = static_cast<const BgpPeer *>(path->GetPeer());
122 237 : if (peer->PeerType() == BgpProto::EBGP) {
123 132 : if (!best_ebgp_path) {
124 90 : best_ebgp_path = path;
125 42 : } else if (!best_ebgp_path->PathSameNeighborAs(*path)) {
126 0 : continue;
127 : }
128 : }
129 :
130 : std::pair<RtGroup::InterestedPeerList::iterator, bool> ret =
131 237 : peer_list.insert(std::pair<const BgpPeer *,
132 474 : RtGroup::RTargetRouteList>(peer, RtGroup::RTargetRouteList()));
133 237 : assert(ret.second);
134 237 : ret.first->second.insert(rt);
135 : }
136 :
137 347 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
138 347 : }
139 :
140 257 : bool RTargetGroupMgr::ProcessRouteTargetList(int part_id) {
141 257 : CHECK_CONCURRENCY("db::DBTable");
142 :
143 1775 : BOOST_FOREACH(const RouteTarget &rtarget, rtarget_trigger_lists_[part_id]) {
144 749 : RtGroup *rtgroup = GetRtGroup(rtarget);
145 780 : if (!rtgroup)
146 0 : continue;
147 780 : rtgroup->NotifyDepRoutes(part_id);
148 : }
149 :
150 260 : rtarget_trigger_lists_[part_id].clear();
151 260 : return true;
152 : }
153 :
154 198 : void RTargetGroupMgr::AddRouteTargetToLists(const RouteTarget &rtarget) {
155 990 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
156 792 : rtarget_trigger_lists_[idx].insert(rtarget);
157 792 : rtarget_dep_triggers_[idx]->Set();
158 : }
159 198 : }
160 :
161 0 : void RTargetGroupMgr::DisableRouteTargetProcessing() {
162 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
163 0 : rtarget_dep_triggers_[idx]->set_disable();
164 : }
165 0 : }
166 :
167 0 : void RTargetGroupMgr::EnableRouteTargetProcessing() {
168 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
169 0 : rtarget_dep_triggers_[idx]->set_enable();
170 : }
171 0 : }
172 :
173 0 : bool RTargetGroupMgr::IsRouteTargetOnList(const RouteTarget &rtarget) const {
174 0 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
175 0 : if (rtarget_trigger_lists_[idx].find(rtarget) !=
176 0 : rtarget_trigger_lists_[idx].end()) {
177 0 : return true;
178 : }
179 : }
180 0 : return false;
181 : }
182 :
183 166 : bool RTargetGroupMgr::ProcessRTargetRouteList() {
184 166 : CHECK_CONCURRENCY("bgp::RTFilter");
185 :
186 166 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
187 166 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
188 166 : BgpTable *table = master->GetTable(Address::RTARGET);
189 :
190 : // Get the Listener id
191 166 : DBTableBase::ListenerId id = GetListenerId(table);
192 :
193 166 : for (RTargetRouteTriggerList::iterator it = rtarget_route_list_.begin();
194 513 : it != rtarget_route_list_.end(); it++) {
195 347 : BuildRTargetDistributionGraph(table, *it, id);
196 : }
197 :
198 166 : rtarget_route_list_.clear();
199 166 : return true;
200 : }
201 :
202 0 : void RTargetGroupMgr::DisableRTargetRouteProcessing() {
203 0 : rtarget_route_trigger_->set_disable();
204 0 : }
205 :
206 0 : void RTargetGroupMgr::EnableRTargetRouteProcessing() {
207 0 : rtarget_route_trigger_->set_enable();
208 0 : }
209 :
210 0 : bool RTargetGroupMgr::IsRTargetRouteOnList(RTargetRoute *rt) const {
211 0 : return rtarget_route_list_.find(rt) != rtarget_route_list_.end();
212 : }
213 :
214 68 : void RTargetGroupMgr::Initialize() {
215 68 : assert(table_state_.empty());
216 68 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
217 68 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
218 68 : assert(master);
219 :
220 68 : master_instance_delete_ref_.Reset(master->deleter());
221 :
222 68 : RoutingInstance::RouteTableList const table_list = master->GetTables();
223 : DBTableBase::ListenerId id;
224 68 : RtGroupMgrTableState *ts = NULL;
225 68 : for (RoutingInstance::RouteTableList::const_iterator it =
226 748 : table_list.begin(); it != table_list.end(); ++it) {
227 612 : if (!it->second->IsVpnTable()) continue;
228 :
229 340 : BgpTable *vpntable = it->second;
230 340 : id = vpntable->Register(
231 : boost::bind(&RTargetGroupMgr::VpnRouteNotify, this, _1, _2),
232 : "RTargetGroupMgr");
233 340 : ts = new RtGroupMgrTableState(vpntable, id);
234 340 : table_state_.insert(std::make_pair(vpntable, ts));
235 : }
236 :
237 68 : BgpTable *rttable = master->GetTable(Address::RTARGET);
238 68 : id = rttable->Register(
239 : boost::bind(&RTargetGroupMgr::RTargetRouteNotify, this, _1, _2),
240 : "RTargetGroupMgr");
241 68 : ts = new RtGroupMgrTableState(rttable, id);
242 68 : table_state_.insert(std::make_pair(rttable, ts));
243 68 : }
244 :
245 68 : void RTargetGroupMgr::ManagedDelete() {
246 68 : if (rtgroup_map_.empty()) remove_rtgroup_trigger_->Set();
247 68 : }
248 :
249 : void
250 5400 : RTargetGroupMgr::RTargetDepSync(DBTablePartBase *root, BgpRoute *rt,
251 : DBTableBase::ListenerId id,
252 : VpnRouteState *dbstate,
253 : const VpnRouteState::RTargetList *future) {
254 5400 : CHECK_CONCURRENCY("db::DBTable");
255 :
256 5402 : BgpTable *table = static_cast<BgpTable *>(root->parent());
257 5401 : if (!dbstate) {
258 1384 : dbstate = new VpnRouteState();
259 1384 : rt->SetState(table, id, dbstate);
260 : }
261 :
262 5401 : int part_id = root->index();
263 5400 : 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 5398 : if (dbstate->GetList()->empty()) {
270 1384 : rt->ClearState(root->parent(), id);
271 1384 : delete dbstate;
272 : }
273 5400 : }
274 :
275 6021 : DBTableBase::ListenerId RTargetGroupMgr::GetListenerId(BgpTable *table) {
276 6021 : RtGroupMgrTableStateList::iterator loc = table_state_.find(table);
277 6019 : assert(loc != table_state_.end());
278 6019 : RtGroupMgrTableState *ts = loc->second;
279 6018 : DBTableBase::ListenerId id = ts->GetListenerId();
280 6018 : assert(id != DBTableBase::kInvalidId);
281 6018 : return id;
282 : }
283 :
284 5407 : bool RTargetGroupMgr::VpnRouteNotify(DBTablePartBase *root,
285 : DBEntryBase *entry) {
286 5407 : CHECK_CONCURRENCY("db::DBTable");
287 :
288 5405 : BgpTable *table = static_cast<BgpTable *>(root->parent());
289 5405 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
290 : // Get the Listener id
291 5405 : DBTableBase::ListenerId id = GetListenerId(table);
292 :
293 : // Get the dbstate
294 : VpnRouteState *dbstate =
295 5401 : static_cast<VpnRouteState *>(rt->GetState(table, id));
296 :
297 5409 : VpnRouteState::RTargetList list;
298 :
299 9432 : if (entry->IsDeleted() || !rt->BestPath() ||
300 4023 : !rt->BestPath()->IsFeasible()) {
301 1384 : if (!dbstate)
302 0 : return true;
303 1384 : RTargetDepSync(root, rt, id, dbstate, &list);
304 1384 : return true;
305 : }
306 :
307 4023 : const BgpPath *path = rt->BestPath();
308 4024 : const BgpAttr *attr = path->GetAttr();
309 4024 : const ExtCommunity *ext_community = attr->ext_community();
310 :
311 4023 : if (ext_community) {
312 : // Gather all Route Target
313 36145 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
314 : ext_community->communities()) {
315 16060 : if (ExtCommunity::is_route_target(comm)) {
316 4018 : list.insert(RouteTarget(comm));
317 : }
318 : }
319 : }
320 :
321 4017 : RTargetDepSync(root, rt, id, dbstate, &list);
322 4016 : return true;
323 5400 : }
324 :
325 451 : bool RTargetGroupMgr::RTargetRouteNotify(DBTablePartBase *root,
326 : DBEntryBase *entry) {
327 451 : CHECK_CONCURRENCY("db::DBTable");
328 :
329 451 : BgpTable *table = static_cast<BgpTable *>(root->parent());
330 451 : RTargetRoute *rt = static_cast<RTargetRoute *>(entry);
331 : // Get the Listener id
332 451 : DBTableBase::ListenerId id = GetListenerId(table);
333 :
334 : // Get the dbstate
335 : RTargetState *dbstate =
336 451 : static_cast<RTargetState *>(rt->GetState(table, id));
337 :
338 451 : if (!dbstate) {
339 280 : if (rt->IsDeleted()) return true;
340 179 : dbstate = new RTargetState();
341 179 : rt->SetState(table, id, dbstate);
342 : }
343 350 : if (rtarget_route_list_.empty())
344 166 : rtarget_route_trigger_->Set();
345 350 : rtarget_route_list_.insert(rt);
346 350 : return true;
347 : }
348 :
349 136 : RTargetGroupMgr::~RTargetGroupMgr() {
350 68 : assert(rtgroup_map_.empty());
351 136 : }
352 :
353 : // Search a RtGroup
354 10706 : RtGroup *RTargetGroupMgr::GetRtGroup(const RouteTarget &rt) {
355 10706 : std::scoped_lock lock(mutex_);
356 10749 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
357 10749 : if (loc != rtgroup_map_.end()) {
358 9172 : return loc->second;
359 : }
360 1577 : return NULL;
361 10749 : }
362 :
363 : // Search a RtGroup
364 6529 : RtGroup *RTargetGroupMgr::GetRtGroup(const ExtCommunity::ExtCommunityValue
365 : &community) {
366 6529 : RouteTarget rt(community);
367 13058 : return GetRtGroup(rt);
368 : }
369 :
370 2241 : RtGroup *RTargetGroupMgr::LocateRtGroup(const RouteTarget &rt) {
371 2241 : std::scoped_lock lock(mutex_);
372 2241 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
373 2241 : RtGroup *group = (loc != rtgroup_map_.end()) ? loc->second : NULL;
374 2241 : if (group == NULL) {
375 184 : group = new RtGroup(rt);
376 184 : rtgroup_map_.insert(rt, group);
377 : }
378 2241 : return group;
379 2241 : }
380 :
381 198 : void RTargetGroupMgr::NotifyRtGroupUnlocked(const RouteTarget &rt) {
382 198 : CHECK_CONCURRENCY("bgp::RTFilter", "bgp::Config", "bgp::ConfigHelper");
383 :
384 198 : AddRouteTargetToLists(rt);
385 198 : if (!rt.IsNull())
386 198 : return;
387 :
388 0 : for (RtGroupMgrTableStateList::iterator it = table_state_.begin();
389 0 : it != table_state_.end(); ++it) {
390 0 : BgpTable *table = it->first;
391 0 : if (!table->IsVpnTable())
392 0 : continue;
393 0 : table->NotifyAllEntries();
394 : }
395 : }
396 :
397 0 : void RTargetGroupMgr::NotifyRtGroup(const RouteTarget &rt) {
398 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
399 0 : std::scoped_lock lock(mutex_);
400 0 : NotifyRtGroupUnlocked(rt);
401 0 : }
402 :
403 1903 : void RTargetGroupMgr::RemoveRtGroup(const RouteTarget &rt) {
404 1903 : std::scoped_lock lock(mutex_);
405 1903 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
406 1903 : RtGroup *rtgroup = (loc != rtgroup_map_.end()) ? loc->second : NULL;
407 1903 : assert(rtgroup);
408 :
409 1903 : rtgroup_remove_list_.insert(rtgroup);
410 1903 : remove_rtgroup_trigger_->Set();
411 1903 : }
412 :
413 1540 : void RTargetGroupMgr::GetRibOutInterestedPeers(RibOut *ribout,
414 : const ExtCommunity *ext_community,
415 : const RibPeerSet &peerset, RibPeerSet *new_peerset) {
416 1540 : RtGroupInterestedPeerSet peer_set;
417 1536 : RtGroup *null_rtgroup = GetRtGroup(RouteTarget::null_rtarget);
418 1545 : if (null_rtgroup) peer_set = null_rtgroup->GetInterestedPeers();
419 14806 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
420 : ext_community->communities()) {
421 6630 : if (ExtCommunity::is_route_target(comm)) {
422 1545 : RtGroup *rtgroup = GetRtGroup(comm);
423 1545 : if (!rtgroup) continue;
424 1545 : peer_set |= rtgroup->GetInterestedPeers();
425 : }
426 : }
427 1545 : RibOut::PeerIterator iter(ribout, peerset);
428 3090 : while (iter.HasNext()) {
429 1545 : int current_index = iter.index();
430 1545 : IPeerUpdate *peer = iter.Next();
431 1545 : BgpPeer *tmp = dynamic_cast<BgpPeer *>(peer);
432 1545 : assert(tmp);
433 1545 : if (tmp->IsFamilyNegotiated(Address::RTARGET)) {
434 0 : if (!peer_set.test(tmp->GetIndex())) {
435 0 : new_peerset->reset(current_index);
436 : }
437 : }
438 : }
439 1544 : }
440 :
441 167 : void RTargetGroupMgr::UnregisterTables() {
442 167 : CHECK_CONCURRENCY("bgp::RTFilter");
443 :
444 167 : if (rtgroup_map_.empty()) {
445 167 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
446 167 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
447 167 : if (master && master->deleted()) {
448 68 : for (RtGroupMgrTableStateList::iterator it =
449 544 : table_state_.begin(), itnext; it != table_state_.end();
450 408 : it = itnext) {
451 408 : itnext = it;
452 408 : itnext++;
453 408 : RtGroupMgrTableState *ts = it->second;
454 408 : DBTableBase::ListenerId id = ts->GetListenerId();
455 408 : BgpTable *bgptable = it->first;
456 408 : bgptable->Unregister(id);
457 408 : table_state_.erase(it);
458 408 : delete ts;
459 : }
460 68 : master_instance_delete_ref_.Reset(NULL);
461 : }
462 : }
463 167 : }
464 :
465 1159 : bool RTargetGroupMgr::ProcessRtGroupList() {
466 1159 : CHECK_CONCURRENCY("bgp::RTFilter");
467 3927 : BOOST_FOREACH(RtGroup *rtgroup, rtgroup_remove_list_) {
468 1384 : if (!rtgroup->MayDelete())
469 1200 : continue;
470 184 : RouteTarget rt = rtgroup->rt();
471 184 : rtgroup_map_.erase(rt);
472 : }
473 1159 : rtgroup_remove_list_.clear();
474 :
475 1159 : if (rtgroup_map_.empty()) UnregisterTables();
476 :
477 1159 : return true;
478 : }
479 :
480 0 : void RTargetGroupMgr::DisableRtGroupProcessing() {
481 0 : remove_rtgroup_trigger_->set_disable();
482 0 : }
483 :
484 0 : void RTargetGroupMgr::EnableRtGroupProcessing() {
485 0 : remove_rtgroup_trigger_->set_enable();
486 0 : }
487 :
488 0 : bool RTargetGroupMgr::IsRtGroupOnList(RtGroup *rtgroup) const {
489 0 : return rtgroup_remove_list_.find(rtgroup) != rtgroup_remove_list_.end();
490 : }
491 :
492 408 : RtGroupMgrTableState::RtGroupMgrTableState(BgpTable *table,
493 408 : DBTableBase::ListenerId id)
494 408 : : id_(id), table_delete_ref_(this, table->deleter()) {
495 408 : assert(table->deleter() != NULL);
496 408 : }
497 :
498 408 : RtGroupMgrTableState::~RtGroupMgrTableState() {
499 408 : }
500 :
501 102 : void RtGroupMgrTableState::ManagedDelete() {
502 102 : }
|