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 32735 : void VpnRouteState::AddRouteTarget(RTargetGroupMgr *mgr, int part_id,
26 : BgpRoute *rt, RTargetList::const_iterator it) {
27 32735 : pair<RTargetList::iterator, bool> result;
28 32734 : result = list_.insert(*it);
29 32758 : assert(result.second);
30 32758 : RtGroup *rtgroup = mgr->LocateRtGroup(*it);
31 32768 : rtgroup->AddDepRoute(part_id, rt);
32 32764 : }
33 :
34 32703 : void VpnRouteState::DeleteRouteTarget(RTargetGroupMgr *mgr, int part_id,
35 : BgpRoute *rt, RTargetList::const_iterator it) {
36 32703 : RtGroup *rtgroup = mgr->GetRtGroup(*it);
37 32764 : rtgroup->RemoveDepRoute(part_id, rt);
38 32753 : mgr->RemoveRtGroup(*it);
39 32766 : list_.erase(it);
40 32752 : }
41 :
42 1162 : void RTargetState::AddInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
43 : RTargetRoute *rt, RtGroup::InterestedPeerList::const_iterator it) {
44 1162 : pair<RtGroup::InterestedPeerList::iterator, bool> result;
45 1162 : result = list_.insert(*it);
46 1162 : assert(result.second);
47 1162 : rtgroup->AddInterestedPeer(it->first, rt);
48 1162 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
49 1162 : }
50 :
51 1162 : void RTargetState::DeleteInterestedPeer(RTargetGroupMgr *mgr, RtGroup *rtgroup,
52 : RTargetRoute *rt, RtGroup::InterestedPeerList::iterator it) {
53 1162 : rtgroup->RemoveInterestedPeer(it->first, rt);
54 1162 : mgr->NotifyRtGroupUnlocked(rtgroup->rt());
55 1162 : list_.erase(it);
56 1162 : }
57 :
58 20 : RTargetGroupMgr::RTargetGroupMgr(BgpServer *server) : server_(server),
59 40 : rtarget_route_trigger_(new TaskTrigger(
60 : boost::bind(&RTargetGroupMgr::ProcessRTargetRouteList, this),
61 40 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
62 40 : remove_rtgroup_trigger_(new TaskTrigger(
63 : boost::bind(&RTargetGroupMgr::ProcessRtGroupList, this),
64 40 : TaskScheduler::GetInstance()->GetTaskId("bgp::RTFilter"), 0)),
65 20 : rtarget_trigger_lists_(DB::PartitionCount()),
66 60 : master_instance_delete_ref_(this, NULL) {
67 100 : for (int i = 0; i < DB::PartitionCount(); i++) {
68 240 : rtarget_dep_triggers_.push_back(boost::shared_ptr<TaskTrigger>(new
69 : TaskTrigger(boost::bind(&RTargetGroupMgr::ProcessRouteTargetList,
70 : this, i),
71 160 : TaskScheduler::GetInstance()->GetTaskId("db::DBTable"), i)));
72 : }
73 20 : }
74 :
75 3466 : void RTargetGroupMgr::RTargetPeerSync(BgpTable *table, RTargetRoute *rt,
76 : DBTableBase::ListenerId id, RTargetState *dbstate,
77 : const RtGroup::InterestedPeerList *future) {
78 3466 : CHECK_CONCURRENCY("bgp::RTFilter");
79 :
80 3466 : RouteTarget rtarget = rt->GetPrefix().rtarget();
81 3466 : RtGroup *rtgroup = LocateRtGroup(rtarget);
82 3466 : assert(rtgroup);
83 :
84 3466 : 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 3466 : if (dbstate->GetList()->empty()) {
91 2100 : rt->ClearState(table, id);
92 2100 : delete dbstate;
93 2100 : RemoveRtGroup(rtarget);
94 : }
95 3466 : }
96 :
97 3466 : void RTargetGroupMgr::BuildRTargetDistributionGraph(BgpTable *table,
98 : RTargetRoute *rt, DBTableBase::ListenerId id) {
99 3466 : CHECK_CONCURRENCY("bgp::RTFilter");
100 :
101 : RTargetState *dbstate =
102 3466 : static_cast<RTargetState *>(rt->GetState(table, id));
103 :
104 3466 : RtGroup::InterestedPeerList peer_list;
105 :
106 5698 : if (rt->IsDeleted() || !rt->BestPath() ||
107 2232 : !rt->BestPath()->IsFeasible()) {
108 1234 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
109 1234 : return;
110 : }
111 :
112 2232 : const BgpPath *best_ebgp_path = NULL;
113 5652 : for (Route::PathList::iterator it = rt->GetPathList().begin();
114 11304 : it != rt->GetPathList().end(); it++) {
115 3420 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
116 3420 : if (!path->IsFeasible())
117 0 : break;
118 3420 : if (!path->GetPeer() || path->GetPeer()->IsXmppPeer())
119 2054 : continue;
120 :
121 1366 : const BgpPeer *peer = static_cast<const BgpPeer *>(path->GetPeer());
122 1366 : if (peer->PeerType() == BgpProto::EBGP) {
123 1366 : if (!best_ebgp_path) {
124 1366 : best_ebgp_path = path;
125 0 : } else if (!best_ebgp_path->PathSameNeighborAs(*path)) {
126 0 : continue;
127 : }
128 : }
129 :
130 : std::pair<RtGroup::InterestedPeerList::iterator, bool> ret =
131 1366 : peer_list.insert(std::pair<const BgpPeer *,
132 2732 : RtGroup::RTargetRouteList>(peer, RtGroup::RTargetRouteList()));
133 1366 : assert(ret.second);
134 1366 : ret.first->second.insert(rt);
135 : }
136 :
137 2232 : RTargetPeerSync(table, rt, id, dbstate, &peer_list);
138 3466 : }
139 :
140 2615 : bool RTargetGroupMgr::ProcessRouteTargetList(int part_id) {
141 2615 : CHECK_CONCURRENCY("db::DBTable");
142 :
143 19354 : BOOST_FOREACH(const RouteTarget &rtarget, rtarget_trigger_lists_[part_id]) {
144 8326 : RtGroup *rtgroup = GetRtGroup(rtarget);
145 8516 : if (!rtgroup)
146 1457 : continue;
147 7059 : rtgroup->NotifyDepRoutes(part_id);
148 : }
149 :
150 2629 : rtarget_trigger_lists_[part_id].clear();
151 2627 : return true;
152 : }
153 :
154 2324 : void RTargetGroupMgr::AddRouteTargetToLists(const RouteTarget &rtarget) {
155 11620 : for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
156 9296 : rtarget_trigger_lists_[idx].insert(rtarget);
157 9296 : rtarget_dep_triggers_[idx]->Set();
158 : }
159 2324 : }
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 1133 : bool RTargetGroupMgr::ProcessRTargetRouteList() {
184 1133 : CHECK_CONCURRENCY("bgp::RTFilter");
185 :
186 1133 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
187 1133 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
188 1133 : BgpTable *table = master->GetTable(Address::RTARGET);
189 :
190 : // Get the Listener id
191 1133 : DBTableBase::ListenerId id = GetListenerId(table);
192 :
193 1133 : for (RTargetRouteTriggerList::iterator it = rtarget_route_list_.begin();
194 4599 : it != rtarget_route_list_.end(); it++) {
195 3466 : BuildRTargetDistributionGraph(table, *it, id);
196 : }
197 :
198 1133 : rtarget_route_list_.clear();
199 1133 : 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 20 : void RTargetGroupMgr::Initialize() {
215 20 : assert(table_state_.empty());
216 20 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
217 20 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
218 20 : assert(master);
219 :
220 20 : master_instance_delete_ref_.Reset(master->deleter());
221 :
222 20 : RoutingInstance::RouteTableList const table_list = master->GetTables();
223 : DBTableBase::ListenerId id;
224 20 : RtGroupMgrTableState *ts = NULL;
225 20 : for (RoutingInstance::RouteTableList::const_iterator it =
226 220 : table_list.begin(); it != table_list.end(); ++it) {
227 180 : if (!it->second->IsVpnTable()) continue;
228 :
229 100 : BgpTable *vpntable = it->second;
230 100 : id = vpntable->Register(
231 : boost::bind(&RTargetGroupMgr::VpnRouteNotify, this, _1, _2),
232 : "RTargetGroupMgr");
233 100 : ts = new RtGroupMgrTableState(vpntable, id);
234 100 : table_state_.insert(std::make_pair(vpntable, ts));
235 : }
236 :
237 20 : BgpTable *rttable = master->GetTable(Address::RTARGET);
238 20 : id = rttable->Register(
239 : boost::bind(&RTargetGroupMgr::RTargetRouteNotify, this, _1, _2),
240 : "RTargetGroupMgr");
241 20 : ts = new RtGroupMgrTableState(rttable, id);
242 20 : table_state_.insert(std::make_pair(rttable, ts));
243 20 : }
244 :
245 20 : void RTargetGroupMgr::ManagedDelete() {
246 20 : if (rtgroup_map_.empty()) remove_rtgroup_trigger_->Set();
247 20 : }
248 :
249 : void
250 32088 : RTargetGroupMgr::RTargetDepSync(DBTablePartBase *root, BgpRoute *rt,
251 : DBTableBase::ListenerId id,
252 : VpnRouteState *dbstate,
253 : const VpnRouteState::RTargetList *future) {
254 32088 : CHECK_CONCURRENCY("db::DBTable");
255 :
256 31944 : BgpTable *table = static_cast<BgpTable *>(root->parent());
257 31944 : if (!dbstate) {
258 8186 : dbstate = new VpnRouteState();
259 8187 : rt->SetState(table, id, dbstate);
260 : }
261 :
262 31949 : int part_id = root->index();
263 31950 : 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 32050 : if (dbstate->GetList()->empty()) {
270 8191 : rt->ClearState(root->parent(), id);
271 8192 : delete dbstate;
272 : }
273 32046 : }
274 :
275 36888 : DBTableBase::ListenerId RTargetGroupMgr::GetListenerId(BgpTable *table) {
276 36888 : RtGroupMgrTableStateList::iterator loc = table_state_.find(table);
277 36870 : assert(loc != table_state_.end());
278 36858 : RtGroupMgrTableState *ts = loc->second;
279 36855 : DBTableBase::ListenerId id = ts->GetListenerId();
280 36856 : assert(id != DBTableBase::kInvalidId);
281 36856 : return id;
282 : }
283 :
284 32028 : bool RTargetGroupMgr::VpnRouteNotify(DBTablePartBase *root,
285 : DBEntryBase *entry) {
286 32028 : CHECK_CONCURRENCY("db::DBTable");
287 :
288 31984 : BgpTable *table = static_cast<BgpTable *>(root->parent());
289 31979 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
290 : // Get the Listener id
291 31979 : DBTableBase::ListenerId id = GetListenerId(table);
292 :
293 : // Get the dbstate
294 : VpnRouteState *dbstate =
295 31960 : static_cast<VpnRouteState *>(rt->GetState(table, id));
296 :
297 32162 : VpnRouteState::RTargetList list;
298 :
299 55993 : if (entry->IsDeleted() || !rt->BestPath() ||
300 23885 : !rt->BestPath()->IsFeasible()) {
301 8184 : if (!dbstate)
302 0 : return true;
303 8184 : RTargetDepSync(root, rt, id, dbstate, &list);
304 8191 : return true;
305 : }
306 :
307 23875 : const BgpPath *path = rt->BestPath();
308 23875 : const BgpAttr *attr = path->GetAttr();
309 23869 : const ExtCommunity *ext_community = attr->ext_community();
310 :
311 23865 : if (ext_community) {
312 : // Gather all Route Target
313 310992 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
314 : ext_community->communities()) {
315 143027 : if (ExtCommunity::is_route_target(comm)) {
316 94733 : list.insert(RouteTarget(comm));
317 : }
318 : }
319 : }
320 :
321 23903 : RTargetDepSync(root, rt, id, dbstate, &list);
322 23854 : return true;
323 32045 : }
324 :
325 3774 : bool RTargetGroupMgr::RTargetRouteNotify(DBTablePartBase *root,
326 : DBEntryBase *entry) {
327 3774 : CHECK_CONCURRENCY("db::DBTable");
328 :
329 3774 : BgpTable *table = static_cast<BgpTable *>(root->parent());
330 3774 : RTargetRoute *rt = static_cast<RTargetRoute *>(entry);
331 : // Get the Listener id
332 3774 : DBTableBase::ListenerId id = GetListenerId(table);
333 :
334 : // Get the dbstate
335 : RTargetState *dbstate =
336 3774 : static_cast<RTargetState *>(rt->GetState(table, id));
337 :
338 3774 : if (!dbstate) {
339 2228 : if (rt->IsDeleted()) return true;
340 2100 : dbstate = new RTargetState();
341 2100 : rt->SetState(table, id, dbstate);
342 : }
343 3646 : if (rtarget_route_list_.empty())
344 1133 : rtarget_route_trigger_->Set();
345 3646 : rtarget_route_list_.insert(rt);
346 3646 : return true;
347 : }
348 :
349 40 : RTargetGroupMgr::~RTargetGroupMgr() {
350 20 : assert(rtgroup_map_.empty());
351 40 : }
352 :
353 : // Search a RtGroup
354 188937 : RtGroup *RTargetGroupMgr::GetRtGroup(const RouteTarget &rt) {
355 188937 : std::scoped_lock lock(mutex_);
356 189464 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
357 189302 : if (loc != rtgroup_map_.end()) {
358 169222 : return loc->second;
359 : }
360 20085 : return NULL;
361 189297 : }
362 :
363 : // Search a RtGroup
364 125295 : RtGroup *RTargetGroupMgr::GetRtGroup(const ExtCommunity::ExtCommunityValue
365 : &community) {
366 125295 : RouteTarget rt(community);
367 250834 : return GetRtGroup(rt);
368 : }
369 :
370 40219 : RtGroup *RTargetGroupMgr::LocateRtGroup(const RouteTarget &rt) {
371 40219 : std::scoped_lock lock(mutex_);
372 40231 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
373 40224 : RtGroup *group = (loc != rtgroup_map_.end()) ? loc->second : NULL;
374 40225 : if (group == NULL) {
375 912 : group = new RtGroup(rt);
376 912 : rtgroup_map_.insert(rt, group);
377 : }
378 40234 : return group;
379 40225 : }
380 :
381 2324 : void RTargetGroupMgr::NotifyRtGroupUnlocked(const RouteTarget &rt) {
382 2324 : CHECK_CONCURRENCY("bgp::RTFilter", "bgp::Config", "bgp::ConfigHelper");
383 :
384 2324 : AddRouteTargetToLists(rt);
385 2324 : if (!rt.IsNull())
386 2324 : 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 35750 : void RTargetGroupMgr::RemoveRtGroup(const RouteTarget &rt) {
404 35750 : std::scoped_lock lock(mutex_);
405 35764 : RtGroupMap::iterator loc = rtgroup_map_.find(rt);
406 35751 : RtGroup *rtgroup = (loc != rtgroup_map_.end()) ? loc->second : NULL;
407 35753 : assert(rtgroup);
408 :
409 35753 : rtgroup_remove_list_.insert(rtgroup);
410 35767 : remove_rtgroup_trigger_->Set();
411 35766 : }
412 :
413 18623 : void RTargetGroupMgr::GetRibOutInterestedPeers(RibOut *ribout,
414 : const ExtCommunity *ext_community,
415 : const RibPeerSet &peerset, RibPeerSet *new_peerset) {
416 18623 : RtGroupInterestedPeerSet peer_set;
417 18626 : RtGroup *null_rtgroup = GetRtGroup(RouteTarget::null_rtarget);
418 18674 : if (null_rtgroup) peer_set = null_rtgroup->GetInterestedPeers();
419 245975 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
420 : ext_community->communities()) {
421 113631 : if (ExtCommunity::is_route_target(comm)) {
422 74647 : RtGroup *rtgroup = GetRtGroup(comm);
423 74695 : if (!rtgroup) continue;
424 74695 : peer_set |= rtgroup->GetInterestedPeers();
425 : }
426 : }
427 18673 : RibOut::PeerIterator iter(ribout, peerset);
428 37343 : while (iter.HasNext()) {
429 18673 : int current_index = iter.index();
430 18673 : IPeerUpdate *peer = iter.Next();
431 18672 : BgpPeer *tmp = dynamic_cast<BgpPeer *>(peer);
432 18672 : assert(tmp);
433 18672 : if (tmp->IsFamilyNegotiated(Address::RTARGET)) {
434 18673 : if (!peer_set.test(tmp->GetIndex())) {
435 1558 : new_peerset->reset(current_index);
436 : }
437 : }
438 : }
439 18665 : }
440 :
441 20 : void RTargetGroupMgr::UnregisterTables() {
442 20 : CHECK_CONCURRENCY("bgp::RTFilter");
443 :
444 20 : if (rtgroup_map_.empty()) {
445 20 : RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
446 20 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
447 20 : if (master && master->deleted()) {
448 20 : for (RtGroupMgrTableStateList::iterator it =
449 160 : table_state_.begin(), itnext; it != table_state_.end();
450 120 : it = itnext) {
451 120 : itnext = it;
452 120 : itnext++;
453 120 : RtGroupMgrTableState *ts = it->second;
454 120 : DBTableBase::ListenerId id = ts->GetListenerId();
455 120 : BgpTable *bgptable = it->first;
456 120 : bgptable->Unregister(id);
457 120 : table_state_.erase(it);
458 120 : delete ts;
459 : }
460 20 : master_instance_delete_ref_.Reset(NULL);
461 : }
462 : }
463 20 : }
464 :
465 2678 : bool RTargetGroupMgr::ProcessRtGroupList() {
466 2678 : CHECK_CONCURRENCY("bgp::RTFilter");
467 23282 : BOOST_FOREACH(RtGroup *rtgroup, rtgroup_remove_list_) {
468 10302 : if (!rtgroup->MayDelete())
469 9390 : continue;
470 912 : RouteTarget rt = rtgroup->rt();
471 912 : rtgroup_map_.erase(rt);
472 : }
473 2678 : rtgroup_remove_list_.clear();
474 :
475 2678 : if (rtgroup_map_.empty()) UnregisterTables();
476 :
477 2678 : 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 120 : RtGroupMgrTableState::RtGroupMgrTableState(BgpTable *table,
493 120 : DBTableBase::ListenerId id)
494 120 : : id_(id), table_delete_ref_(this, table->deleter()) {
495 120 : assert(table->deleter() != NULL);
496 120 : }
497 :
498 120 : RtGroupMgrTableState::~RtGroupMgrTableState() {
499 120 : }
500 :
501 120 : void RtGroupMgrTableState::ManagedDelete() {
502 120 : }
|