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/routepath_replicator.h"
7 :
8 : #include <boost/foreach.hpp>
9 :
10 : #include <utility>
11 :
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_log.h"
17 : #include "bgp/bgp_route.h"
18 : #include "bgp/bgp_server.h"
19 : #include "bgp/origin-vn/origin_vn.h"
20 : #include "bgp/routing-instance/path_resolver.h"
21 : #include "bgp/routing-instance/routing_instance.h"
22 : #include "bgp/routing-instance/rtarget_group_mgr.h"
23 : #include "bgp/routing-instance/routing_instance_analytics_types.h"
24 :
25 : using std::ostringstream;
26 : using std::make_pair;
27 : using std::pair;
28 : using std::string;
29 : using std::vector;
30 :
31 : //
32 : // RoutePathReplication trace macro. Optionally logs the server name as well for
33 : // easier debugging in multi server unit tests
34 : //
35 : #define RPR_TRACE(obj, ...) \
36 : do { \
37 : if (LoggingDisabled()) break; \
38 : bgp_log_test::LogServerName(server()); \
39 : Rpr##obj##Log::Send("RoutingInstance", \
40 : SandeshLevel::SYS_DEBUG, __FILE__, __LINE__, __VA_ARGS__); \
41 : Rpr##obj::TraceMsg(trace_buf_, __FILE__, __LINE__, __VA_ARGS__); \
42 : } while (false)
43 :
44 : #define RPR_TRACE_ONLY(obj, ...) \
45 : do { \
46 : if (LoggingDisabled()) break; \
47 : bgp_log_test::LogServerName(server()); \
48 : Rpr##obj::TraceMsg(trace_buf_, __FILE__, __LINE__, __VA_ARGS__); \
49 : } while (false)
50 :
51 : class TableState::DeleteActor : public LifetimeActor {
52 : public:
53 254235 : explicit DeleteActor(TableState *ts)
54 254235 : : LifetimeActor(ts->replicator()->server()->lifetime_manager()),
55 254235 : ts_(ts) {
56 254235 : }
57 :
58 254314 : virtual bool MayDelete() const {
59 254314 : return ts_->MayDelete();
60 : }
61 :
62 254235 : virtual void Shutdown() {
63 254235 : }
64 :
65 254235 : virtual void Destroy() {
66 254235 : ts_->replicator()->DeleteTableState(ts_->table());
67 254235 : }
68 :
69 : private:
70 : TableState *ts_;
71 : };
72 :
73 254233 : TableState::TableState(RoutePathReplicator *replicator, BgpTable *table)
74 254233 : : replicator_(replicator),
75 254233 : table_(table),
76 254233 : listener_id_(DBTableBase::kInvalidId),
77 254233 : deleter_(new DeleteActor(this)),
78 254233 : table_delete_ref_(this, table->deleter()),
79 254229 : export_target_gen_(0) {
80 254227 : assert(table->deleter() != NULL);
81 254226 : }
82 :
83 254235 : TableState::~TableState() {
84 254235 : if (walk_ref() != NULL) {
85 22606 : table()->ReleaseWalker(walk_ref());
86 : }
87 254235 : }
88 :
89 254235 : void TableState::ManagedDelete() {
90 254235 : deleter()->Delete();
91 254235 : }
92 :
93 0 : bool TableState::deleted() const {
94 0 : return deleter()->IsDeleted();
95 : }
96 :
97 909078 : LifetimeActor *TableState::deleter() {
98 909078 : return deleter_.get();
99 : }
100 :
101 0 : const LifetimeActor *TableState::deleter() const {
102 0 : return deleter_.get();
103 : }
104 :
105 254314 : bool TableState::MayDelete() const {
106 531261 : if (list_.empty() && !route_count() &&
107 276947 : ((walk_ref() == NULL) || !walk_ref()->walk_is_active()))
108 254235 : return true;
109 79 : return false;
110 : }
111 :
112 635975 : void TableState::RetryDelete() {
113 635975 : if (!deleter()->IsDeleted())
114 617076 : return;
115 18871 : deleter()->RetryDelete();
116 : }
117 :
118 668273 : void TableState::AddGroup(RtGroup *group) {
119 668273 : list_.insert(group);
120 668388 : }
121 :
122 668410 : void TableState::RemoveGroup(RtGroup *group) {
123 668410 : list_.erase(group);
124 668410 : }
125 :
126 429580 : const RtGroup *TableState::FindGroup(RtGroup *group) const {
127 429580 : GroupList::const_iterator it = list_.find(group);
128 429554 : return (it != list_.end() ? *it : NULL);
129 : }
130 :
131 254325 : uint32_t TableState::route_count() const {
132 254325 : return table_->GetDBStateCount(listener_id());
133 : }
134 :
135 1052229 : RtReplicated::RtReplicated(RoutePathReplicator *replicator)
136 1052539 : : replicator_(replicator),
137 1052229 : export_target_gen_(0) {
138 1052508 : }
139 :
140 517565 : void RtReplicated::AddRouteInfo(BgpTable *table, BgpRoute *rt,
141 : ReplicatedRtPathList::const_iterator it) {
142 517565 : pair<ReplicatedRtPathList::iterator, bool> result;
143 517565 : result = replicate_list_.insert(*it);
144 517578 : assert(result.second);
145 517578 : }
146 :
147 517142 : void RtReplicated::DeleteRouteInfo(BgpTable *table, BgpRoute *rt,
148 : ReplicatedRtPathList::const_iterator it) {
149 517142 : replicator_->DeleteSecondaryPath(table, rt, *it);
150 517390 : replicate_list_.erase(it);
151 517454 : }
152 :
153 : //
154 : // Return the list of secondary table names for the given primary path.
155 : // We go through all SecondaryRouteInfos and skip the ones that don't
156 : // match the primary path.
157 : //
158 281 : vector<string> RtReplicated::GetTableNameList(const BgpPath *path) const {
159 281 : vector<string> table_list;
160 1367 : BOOST_FOREACH(const SecondaryRouteInfo &rinfo, replicate_list_) {
161 543 : if (rinfo.peer_ != path->GetPeer())
162 0 : continue;
163 543 : if (rinfo.path_id_ != path->GetPathId())
164 16 : continue;
165 527 : if (rinfo.src_ != path->GetSource())
166 0 : continue;
167 527 : table_list.push_back(rinfo.table_->name());
168 : }
169 281 : return table_list;
170 0 : }
171 :
172 48715 : RoutePathReplicator::RoutePathReplicator(BgpServer *server,
173 48715 : Address::Family family)
174 48715 : : server_(server),
175 48715 : family_(family),
176 48715 : vpn_table_(NULL),
177 48715 : trace_buf_(SandeshTraceBufferCreate("RoutePathReplicator", 500)) {
178 48715 : }
179 :
180 97430 : RoutePathReplicator::~RoutePathReplicator() {
181 48715 : assert(table_state_list_.empty());
182 97430 : }
183 :
184 41140 : void RoutePathReplicator::Initialize() {
185 41140 : assert(!vpn_table_);
186 41140 : RoutingInstanceMgr *mgr = server_->routing_instance_mgr();
187 41140 : assert(mgr);
188 41140 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
189 41140 : assert(master);
190 41140 : vpn_table_ = master->GetTable(family_);
191 41140 : assert(vpn_table_);
192 41140 : assert(AddTableState(vpn_table_));
193 41140 : }
194 :
195 709529 : TableState *RoutePathReplicator::AddTableState(BgpTable *table,
196 : RtGroup *group) {
197 709529 : assert(table->IsVpnTable() || group);
198 :
199 709511 : TableStateList::iterator loc = table_state_list_.find(table);
200 709307 : if (loc == table_state_list_.end()) {
201 254230 : TableState *ts = new TableState(this, table);
202 254226 : DBTableBase::ListenerId id = table->Register(
203 : boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
204 : "RoutePathReplicator");
205 254221 : ts->set_listener_id(id);
206 254216 : if (group)
207 213076 : ts->AddGroup(group);
208 254232 : table_state_list_.insert(make_pair(table, ts));
209 254233 : RPR_TRACE(RegTable, table->name());
210 254230 : return ts;
211 : } else {
212 455201 : assert(group);
213 455201 : TableState *ts = loc->second;
214 455205 : ts->AddGroup(group);
215 455309 : return ts;
216 : }
217 : }
218 :
219 668409 : void RoutePathReplicator::RemoveTableState(BgpTable *table, RtGroup *group) {
220 668409 : TableState *ts = FindTableState(table);
221 668410 : assert(ts);
222 668410 : ts->RemoveGroup(group);
223 668410 : }
224 :
225 254235 : void RoutePathReplicator::DeleteTableState(BgpTable *table) {
226 254235 : TableState *ts = FindTableState(table);
227 254235 : assert(ts);
228 254235 : RPR_TRACE(UnregTable, table->name());
229 254235 : table->Unregister(ts->listener_id());
230 254235 : table_state_list_.erase(table);
231 254235 : delete ts;
232 254235 : }
233 :
234 2097187 : TableState *RoutePathReplicator::FindTableState(BgpTable *table) {
235 2097187 : TableStateList::iterator loc = table_state_list_.find(table);
236 2097139 : return (loc != table_state_list_.end() ? loc->second : NULL);
237 : }
238 :
239 5647 : const TableState *RoutePathReplicator::FindTableState(
240 : const BgpTable *table) const {
241 : TableStateList::const_iterator loc =
242 5647 : table_state_list_.find(const_cast<BgpTable *>(table));
243 5644 : return (loc != table_state_list_.end() ? loc->second : NULL);
244 : }
245 :
246 : void
247 51325 : RoutePathReplicator::RequestWalk(BgpTable *table) {
248 51325 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
249 51325 : TableState *ts = FindTableState(table);
250 51325 : assert(ts);
251 51325 : if (!ts->walk_ref()) {
252 : DBTable::DBTableWalkRef walk_ref = table->AllocWalker(
253 : boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
254 45212 : boost::bind(&RoutePathReplicator::BulkReplicationDone, this, _2));
255 22606 : table->WalkTable(walk_ref);
256 22606 : ts->set_walk_ref(walk_ref);
257 22606 : } else {
258 28719 : table->WalkAgain(ts->walk_ref());
259 : }
260 51325 : }
261 :
262 : void
263 24593 : RoutePathReplicator::BulkReplicationDone(DBTableBase *dbtable) {
264 24593 : CHECK_CONCURRENCY("db::Walker");
265 24593 : BgpTable *table = static_cast<BgpTable *>(dbtable);
266 24593 : RPR_TRACE(WalkDone, table->name());
267 24593 : TableState *ts = FindTableState(table);
268 24593 : ts->RetryDelete();
269 24593 : }
270 :
271 430016 : void RoutePathReplicator::JoinVpnTable(RtGroup *group) {
272 430016 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
273 430020 : TableState *vpn_ts = FindTableState(vpn_table_);
274 429956 : if (!vpn_ts || vpn_ts->FindGroup(group))
275 370 : return;
276 429544 : RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), true);
277 429665 : group->AddImportTable(family(), vpn_table_);
278 429770 : RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), false);
279 429767 : group->AddExportTable(family(), vpn_table_);
280 429775 : AddTableState(vpn_table_, group);
281 : }
282 :
283 429963 : void RoutePathReplicator::LeaveVpnTable(RtGroup *group) {
284 429963 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
285 429963 : TableState *vpn_ts = FindTableState(vpn_table_);
286 429964 : if (!vpn_ts)
287 185 : return;
288 429779 : RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), true);
289 429779 : group->RemoveImportTable(family(), vpn_table_);
290 429780 : RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), false);
291 429780 : group->RemoveExportTable(family(), vpn_table_);
292 429779 : RemoveTableState(vpn_table_, group);
293 : }
294 :
295 : //
296 : // Add a given BgpTable to RtGroup of given RouteTarget.
297 : // It will create a new RtGroup if none exists.
298 : // In case of export RouteTarget, create TableState if it doesn't exist.
299 : //
300 928040 : void RoutePathReplicator::Join(BgpTable *table, const RouteTarget &rt,
301 : bool import) {
302 928040 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
303 :
304 927965 : std::scoped_lock lock(mutex_);
305 928522 : RPR_TRACE(TableJoin, table->name(), rt.ToString(), import);
306 :
307 928526 : bool first = false;
308 928526 : RtGroup *group = server()->rtarget_group_mgr()->LocateRtGroup(rt);
309 928625 : if (import) {
310 689995 : first = group->AddImportTable(family(), table);
311 689998 : if (group->HasDepRoutes())
312 110 : server()->rtarget_group_mgr()->NotifyRtGroup(rt);
313 689936 : if (family_ == Address::INETVPN)
314 137997 : server_->NotifyAllStaticRoutes();
315 1338034 : BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
316 324041 : if (sec_table->IsVpnTable() || sec_table->empty())
317 322820 : continue;
318 1205 : RequestWalk(sec_table);
319 : }
320 : } else {
321 238630 : first = group->AddExportTable(family(), table);
322 238630 : TableState *ts = AddTableState(table, group);
323 : // Export targets changed, so the attributes of the paths replicated
324 : // earlier are stale. Bump before the walk, so that RouteListener does
325 : // not take the optimized path for them.
326 238622 : ts->bump_export_target_gen();
327 238628 : if (!table->empty()) {
328 59 : RequestWalk(table);
329 : }
330 : }
331 :
332 : // Join the vpn table when group is created.
333 928273 : if (first)
334 430007 : JoinVpnTable(group);
335 928409 : }
336 :
337 : //
338 : // Remove a BgpTable from RtGroup of given RouteTarget.
339 : // If the last group is going away, the RtGroup will be removed
340 : // In case of export RouteTarget, trigger remove of TableState appropriate.
341 : //
342 928629 : void RoutePathReplicator::Leave(BgpTable *table, const RouteTarget &rt,
343 : bool import) {
344 928629 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
345 :
346 928629 : std::scoped_lock lock(mutex_);
347 928629 : RtGroup *group = server()->rtarget_group_mgr()->GetRtGroup(rt);
348 928630 : assert(group);
349 928630 : RPR_TRACE(TableLeave, table->name(), rt.ToString(), import);
350 :
351 928630 : if (import) {
352 690000 : group->RemoveImportTable(family(), table);
353 689990 : if (group->HasDepRoutes())
354 132225 : server()->rtarget_group_mgr()->NotifyRtGroup(rt);
355 689991 : if (family_ == Address::INETVPN)
356 138000 : server_->NotifyAllStaticRoutes();
357 2674041 : BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
358 992024 : if (sec_table->IsVpnTable() || sec_table->empty())
359 965148 : continue;
360 26878 : RequestWalk(sec_table);
361 : }
362 : } else {
363 238630 : group->RemoveExportTable(family(), table);
364 238630 : RemoveTableState(table, group);
365 : // The paths replicated earlier still carry the target being removed.
366 238630 : FindTableState(table)->bump_export_target_gen();
367 238630 : if (!table->empty()) {
368 23155 : RequestWalk(table);
369 : }
370 : }
371 :
372 : // Leave the vpn table when the last VRF has left the group.
373 928625 : if (!group->HasVrfTables(family())) {
374 429963 : LeaveVpnTable(group);
375 429965 : server()->rtarget_group_mgr()->RemoveRtGroup(rt);
376 : }
377 928627 : }
378 :
379 1443206 : void RoutePathReplicator::DBStateSync(BgpTable *table, TableState *ts,
380 : BgpRoute *rt, RtReplicated *dbstate,
381 : const RtReplicated::ReplicatedRtPathList *future) {
382 1443206 : set_synchronize(dbstate->GetMutableList(), future,
383 : boost::bind(&RtReplicated::AddRouteInfo, dbstate, table, rt, _1),
384 : boost::bind(&RtReplicated::DeleteRouteInfo, dbstate, table, rt, _1));
385 :
386 1443118 : if (dbstate->GetList().empty()) {
387 1052635 : rt->ClearState(table, ts->listener_id());
388 1052844 : delete dbstate;
389 1052751 : if (table->GetDBStateCount(ts->listener_id()) == 0)
390 611383 : ts->RetryDelete();
391 : }
392 1443336 : }
393 :
394 227580 : static ExtCommunityPtr UpdateOriginVn(BgpServer *server,
395 : const ExtCommunity *ext_community,
396 : int vn_index) {
397 227580 : as_t asn = server->autonomous_system();
398 227582 : ExtCommunityPtr extcomm_ptr;
399 227582 : if (vn_index) {
400 53754 : if (asn > AS2_MAX && vn_index > 0xffff) {
401 0 : OriginVn origin_vn(asn, AS_TRANS);
402 0 : extcomm_ptr = server->extcomm_db()->ReplaceOriginVnAndLocate(
403 0 : ext_community, origin_vn.GetExtCommunity());
404 0 : OriginVn origin_vn2(AS_TRANS, vn_index);
405 0 : extcomm_ptr = server->extcomm_db()->AppendAndLocate(
406 0 : extcomm_ptr.get(), origin_vn2.GetExtCommunity());
407 0 : } else {
408 53754 : OriginVn origin_vn(asn, vn_index);
409 161263 : extcomm_ptr = server->extcomm_db()->ReplaceOriginVnAndLocate(
410 107510 : ext_community, origin_vn.GetExtCommunity());
411 53756 : return extcomm_ptr;
412 : }
413 : }
414 173828 : extcomm_ptr = server->extcomm_db()->RemoveOriginVnAndLocate(ext_community);
415 173850 : return extcomm_ptr;
416 0 : }
417 :
418 : //
419 : // Update the ExtCommunity with the RouteTargets from the export list
420 : // and the OriginVn. The OriginVn is derived from the RouteTargets in
421 : // vpn routes.
422 : //
423 519926 : static ExtCommunityPtr UpdateExtCommunity(BgpServer *server,
424 : const RoutingInstance *rtinstance, const ExtCommunity *ext_community,
425 : const ExtCommunity::ExtCommunityList &export_list) {
426 : // Add RouteTargets exported by the instance for a non-master instance.
427 519926 : ExtCommunityPtr extcomm_ptr;
428 519926 : if (!rtinstance->IsMasterRoutingInstance()) {
429 : extcomm_ptr =
430 322431 : server->extcomm_db()->AppendAndLocate(ext_community, export_list);
431 322482 : return extcomm_ptr;
432 : }
433 :
434 : // Bail if we have a vpn route without extended communities.
435 197497 : if (!ext_community)
436 1053 : return ExtCommunityPtr(NULL);
437 :
438 : // Nothing to do if we already have the OriginVn community with our AS
439 : // or with a vn index from the global range.
440 3338936 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
441 : ext_community->communities()) {
442 1587749 : if (!ExtCommunity::is_origin_vn(comm))
443 1571246 : continue;
444 18473 : OriginVn origin_vn(comm);
445 36964 : if (!origin_vn.IsGlobal() &&
446 18482 : origin_vn.as_number() != server->autonomous_system()) {
447 1961 : continue;
448 : }
449 16521 : return ExtCommunityPtr(ext_community);
450 : }
451 :
452 : // Add the OriginVn if we have a valid vn index.
453 : int vn_index =
454 179958 : server->routing_instance_mgr()->GetVnIndexByExtCommunity(ext_community);
455 180021 : return UpdateOriginVn(server, ext_community, vn_index);
456 520100 : }
457 :
458 : //
459 : // Concurrency: Called in the context of the DB partition task.
460 : //
461 : // This function handles
462 : // 1. Table Notification for path replication
463 : // 2. Table walk for import/export of new targets
464 : //
465 : // Replicate a path (clone the BgpPath) to secondary BgpTables based on the
466 : // export targets of the primary BgpTable.
467 : // If primary table is a VRF table attach it's export targets to replicated
468 : // path in the VPN table.
469 : //
470 2003231 : bool RoutePathReplicator::RouteListener(TableState *ts,
471 : DBTablePartBase *root, DBEntryBase *entry) {
472 2003231 : CHECK_CONCURRENCY("db::DBTable");
473 :
474 2003058 : BgpTable *table = static_cast<BgpTable *>(root->parent());
475 2003027 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
476 2003027 : const RoutingInstance *rtinstance = table->routing_instance();
477 :
478 2003022 : DBTableBase::ListenerId id = ts->listener_id();
479 2002999 : assert(id != DBTableBase::kInvalidId);
480 :
481 : // Get the DBState.
482 : RtReplicated *dbstate =
483 2002999 : static_cast<RtReplicated *>(rt->GetState(table, id));
484 2004308 : RtReplicated::ReplicatedRtPathList replicated_path_list;
485 :
486 : //Flag to track if any change happenned to route in last 30 minutes
487 2003948 : bool route_unchanged = false;
488 : //List of tables that needs replication of route
489 2003948 : RtGroup::RtGroupMemberList addedTables;
490 : //List of tables that doesn't need replication of route
491 2003459 : RtGroup::RtGroupMemberList deletedTables;
492 : //List of tables having the latest changes in route
493 2003087 : RtGroup::RtGroupMemberList previousTables;
494 : //Threshold used for optimised replication
495 2002890 : uint64_t optimizationThresholdTime = 30ULL * 60 * 1000000;
496 2002890 : uint64_t start = UTCTimestampUsec();
497 : //Read once: a change racing with this walk must not be recorded as
498 : //applied. The walk it requests runs after this one and sees the mismatch.
499 2004293 : uint64_t export_target_gen = ts->export_target_gen();
500 :
501 : //Optimize only if last route change occurred 30 mins before and the
502 : //export targets of the primary table are unchanged since the route was
503 : //last replicated - the optimized path skips every destination table that
504 : //is in both the old and the new set, which for a route in a VRF always
505 : //includes the VPN table, leaving it with the previous export targets.
506 : //We update the route_unchanged flag to true
507 2004332 : if ( (start - rt->last_update_at() > optimizationThresholdTime) &&
508 2004250 : dbstate != NULL &&
509 120 : dbstate->export_target_gen() == export_target_gen)
510 : {
511 118 : RPR_TRACE(RouteListener, "Route change occurred before threshold \
512 : time, optimising replication");
513 118 : route_unchanged = true;
514 : }
515 :
516 : //
517 : // Cleanup if the route is not usable.
518 : // If route aggregation is enabled, contributing route/more specific route
519 : // for a aggregate route will NOT be replicated to destination table
520 : //
521 2466807 : if (!rt->IsUsable() || (table->IsRouteAggregationSupported() &&
522 462816 : !rtinstance->deleted() &&
523 459847 : table->IsContributingRoute(rt))) {
524 717417 : if (!dbstate) {
525 560841 : return true;
526 : }
527 156576 : DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
528 156694 : return true;
529 : }
530 :
531 : // Create and set new DBState on the route. This will get cleaned up via
532 : // via the call to DBStateSync if we don't need to replicate the route to
533 : // any tables.
534 1286249 : if (dbstate == NULL) {
535 1052532 : dbstate = new RtReplicated(this);
536 1052504 : rt->SetState(table, id, dbstate);
537 : }
538 :
539 : // Get the export route target list from the routing instance.
540 1286461 : ExtCommunity::ExtCommunityList export_list;
541 1286466 : if (!rtinstance->IsMasterRoutingInstance()) {
542 5800624 : BOOST_FOREACH(RouteTarget rtarget, rtinstance->GetExportList()) {
543 2492904 : export_list.push_back(rtarget.GetExtCommunity());
544 : }
545 : }
546 :
547 : // Updating previousTables with the list of tables having the information
548 : // about the route.
549 1286243 : if (route_unchanged){
550 118 : replicated_path_list = dbstate->GetList();
551 372 : BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
552 : replicated_path_list){
553 127 : previousTables.insert(path.table_);
554 : }
555 : }
556 :
557 : // Replicate all feasible and non-replicated paths.
558 1286243 : for (Route::PathList::iterator it = rt->GetPathList().begin();
559 6123098 : it != rt->GetPathList().end(); ++it) {
560 1781244 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
561 :
562 : // Skip if the source peer is down.
563 2629690 : if (!path->IsStale() && !path->IsLlgrStale() && path->GetPeer() &&
564 848127 : !path->GetPeer()->IsReady())
565 1286964 : continue;
566 :
567 : // No need to replicate aliased or replicated paths.
568 1671302 : if (path->IsReplicated() || path->IsAliased())
569 1145155 : continue;
570 :
571 : // Do not replicate non-ecmp paths.
572 526134 : if (rt->BestPath()->PathCompare(*path, true))
573 6180 : break;
574 :
575 : // Do not replicate if nexthop is not hitched by ermvpn tree.
576 519928 : if (path->CheckErmVpn())
577 0 : break;
578 :
579 519926 : const BgpAttr *attr = path->GetAttr();
580 519923 : const ExtCommunity *ext_community = attr->ext_community();
581 :
582 : ExtCommunityPtr extcomm_ptr =
583 519924 : UpdateExtCommunity(server(), rtinstance, ext_community, export_list);
584 520100 : ext_community = extcomm_ptr.get();
585 520100 : if (!ext_community)
586 1053 : continue;
587 :
588 : // Go through all extended communities.
589 : //
590 : // Get the vn_index from the OriginVn extended community.
591 : // For each RouteTarget extended community, get the list of tables
592 : // to which we need to replicate the path.
593 519047 : int vn_index = 0;
594 519047 : RtGroup::RtGroupMemberList secondary_tables;
595 5916226 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
596 : ext_community->communities()) {
597 2698564 : if (ExtCommunity::is_origin_vn(comm)) {
598 79512 : OriginVn origin_vn(comm);
599 79512 : vn_index = origin_vn.vn_index();
600 2619007 : } else if (ExtCommunity::is_route_target(comm)) {
601 : RtGroup *group =
602 2158365 : server()->rtarget_group_mgr()->GetRtGroup(comm);
603 2158739 : if (!group)
604 603799 : continue;
605 : RtGroup::RtGroupMemberList import_list =
606 2156965 : group->GetImportTables(family());
607 2156735 : if (import_list.empty())
608 602025 : continue;
609 1554700 : secondary_tables.insert(import_list.begin(), import_list.end());
610 2156544 : }
611 : }
612 :
613 : // Update with family specific secondary tables.
614 519028 : table->UpdateSecondaryTablesForReplication(rt, &secondary_tables);
615 :
616 : // Skip if we don't need to replicate the path to any tables.
617 519022 : if (secondary_tables.empty())
618 30522 : continue;
619 :
620 : // Add OriginVn when replicating self-originated routes from a VRF.
621 409195 : if (!vn_index && !rtinstance->IsMasterRoutingInstance() &&
622 897699 : path->IsVrfOriginated() && rtinstance->virtual_network_index()) {
623 47193 : vn_index = rtinstance->virtual_network_index();
624 47193 : extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(), vn_index);
625 : }
626 :
627 488505 : if (route_unchanged){
628 120 : addedTables = RtGroup::RtGroupMemberList();
629 : // Finding the difference between sets, secondary_tables and
630 : // previousTables
631 120 : std::set_difference(secondary_tables.begin(), secondary_tables.end(),
632 : previousTables.begin(), previousTables.end(),
633 : std::inserter(addedTables, addedTables.end()));
634 120 : std::set_difference(previousTables.begin(), previousTables.end(),
635 : secondary_tables.begin(), secondary_tables.end(),
636 : std::inserter(deletedTables, deletedTables.end()));
637 398 : BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
638 : dbstate->GetList()){
639 139 : if (deletedTables.find(path.table_)!=deletedTables.end()) {
640 1 : replicated_path_list.erase(path);
641 : }
642 : }
643 : //Updating the secondary tables with the difference.
644 120 : secondary_tables = addedTables;
645 :
646 : }
647 :
648 :
649 : // Replicate path to all destination tables.
650 3754698 : BOOST_FOREACH(BgpTable *dest, secondary_tables) {
651 : // Skip if destination is same as source table.
652 1633044 : if (dest == table)
653 670501 : continue;
654 :
655 1144656 : const RoutingInstance *dest_rtinstance = dest->routing_instance();
656 1144650 : ExtCommunityPtr new_extcomm_ptr = extcomm_ptr;
657 :
658 : // If the origin vn is unresolved, see if route has a RouteTarget
659 : // that's in the set of export RouteTargets for the dest instance.
660 : // If so, we set the origin vn for the replicated route to be the
661 : // vn for the dest instance.
662 1145332 : if (!vn_index && dest_rtinstance->virtual_network_index() &&
663 468 : dest_rtinstance->HasExportTarget(ext_community)) {
664 368 : int dest_vn_index = dest_rtinstance->virtual_network_index();
665 736 : new_extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(),
666 368 : dest_vn_index);
667 : }
668 :
669 : // Replicate the route to the destination table. The destination
670 : // table may decide to not replicate based on it's own policy e.g.
671 : // multicast routes are never leaked across routing-instances.
672 1144851 : BgpRoute *replicated_rt = dest->RouteReplicate(
673 : server_, table, rt, path, new_extcomm_ptr);
674 1144874 : if (!replicated_rt)
675 182116 : continue;
676 :
677 : // Add information about the secondary path to the replicated path
678 : // list.
679 962740 : RtReplicated::SecondaryRouteInfo rtinfo(dest, path->GetPeer(),
680 1925492 : path->GetPathId(), path->GetSource(), replicated_rt);
681 962712 : pair<RtReplicated::ReplicatedRtPathList::iterator, bool> result;
682 962717 : result = replicated_path_list.insert(rtinfo);
683 : // Assert if the insertion to replication path list fails
684 962740 : if (!route_unchanged)
685 962739 : assert(result.second);
686 962740 : RPR_TRACE_ONLY(Replicate, table->name(), rt->ToString(),
687 : path->ToString(),
688 : BgpPath::PathIdString(path->GetPathId()),
689 : dest->name(), replicated_rt->ToString());
690 1144884 : }
691 550623 : }
692 :
693 : // Record what this replication was done with, for the next walk.
694 1286262 : dbstate->set_export_target_gen(export_target_gen);
695 :
696 : // Update the DBState to reflect the new list of secondary paths. The
697 : // DBState will get cleared if the list is empty.
698 1286566 : DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
699 1286699 : return true;
700 2004234 : }
701 176 : const RtReplicated *RoutePathReplicator::GetReplicationState(
702 : BgpTable *table, BgpRoute *rt) const {
703 176 : const TableState *ts = FindTableState(table);
704 176 : if (!ts)
705 0 : return NULL;
706 : RtReplicated *dbstate =
707 176 : static_cast<RtReplicated *>(rt->GetState(table, ts->listener_id()));
708 176 : return dbstate;
709 : }
710 :
711 : //
712 : // Return the list of secondary table names for the given primary path.
713 : //
714 5472 : vector<string> RoutePathReplicator::GetReplicatedTableNameList(
715 : const BgpTable *table, const BgpRoute *rt, const BgpPath *path) const {
716 5472 : const TableState *ts = FindTableState(table);
717 5468 : if (!ts)
718 5166 : return vector<string>();
719 : const RtReplicated *dbstate = static_cast<const RtReplicated *>(
720 302 : rt->GetState(table, ts->listener_id()));
721 302 : if (!dbstate)
722 21 : return vector<string>();
723 281 : return dbstate->GetTableNameList(path);
724 : }
725 :
726 517127 : void RoutePathReplicator::DeleteSecondaryPath(BgpTable *table, BgpRoute *rt,
727 : const RtReplicated::SecondaryRouteInfo &rtinfo) {
728 517127 : BgpRoute *rt_secondary = rtinfo.rt_;
729 517127 : BgpTable *secondary_table = rtinfo.table_;
730 517127 : const IPeer *peer = rtinfo.peer_;
731 517127 : uint32_t path_id = rtinfo.path_id_;
732 517127 : BgpPath::PathSource src = rtinfo.src_;
733 :
734 : DBTablePartBase *partition =
735 517127 : secondary_table->GetTablePartition(rt_secondary);
736 517032 : BgpPath *secondary_path = rt_secondary->FindSecondaryPath(rt, src,
737 : peer, path_id);
738 517185 : if (secondary_path && secondary_path->NeedsResolution()) {
739 0 : secondary_table->path_resolver()->StopPathResolution(partition->index(),
740 : secondary_path);
741 : }
742 517171 : assert(rt_secondary->RemoveSecondaryPath(rt, src, peer, path_id));
743 517474 : if (rt_secondary->count() == 0) {
744 493465 : RPR_TRACE_ONLY(Flush, secondary_table->name(), rt_secondary->ToString(),
745 : peer ? peer->ToString() : "Nil",
746 : BgpPath::PathIdString(path_id), table->name(),
747 : rt->ToString(), "Delete");
748 493589 : partition->Delete(rt_secondary);
749 : } else {
750 23880 : partition->Notify(rt_secondary);
751 23882 : RPR_TRACE_ONLY(Flush, secondary_table->name(), rt_secondary->ToString(),
752 : peer ? peer->ToString() : "Nil",
753 : BgpPath::PathIdString(path_id), table->name(),
754 : rt->ToString(), "Path update");
755 : }
756 517447 : }
757 :
758 0 : string RtReplicated::SecondaryRouteInfo::ToString() const {
759 0 : ostringstream out;
760 0 : out << table_->name() << "(" << table_ << ")" << ":" <<
761 0 : peer_->ToString() << "(" << peer_ << ")" << ":" <<
762 0 : rt_->ToString() << "(" << rt_ << ")";
763 0 : return out.str();
764 0 : }
|