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 500 : explicit DeleteActor(TableState *ts)
54 500 : : LifetimeActor(ts->replicator()->server()->lifetime_manager()),
55 500 : ts_(ts) {
56 500 : }
57 :
58 500 : virtual bool MayDelete() const {
59 500 : return ts_->MayDelete();
60 : }
61 :
62 500 : virtual void Shutdown() {
63 500 : }
64 :
65 500 : virtual void Destroy() {
66 500 : ts_->replicator()->DeleteTableState(ts_->table());
67 500 : }
68 :
69 : private:
70 : TableState *ts_;
71 : };
72 :
73 500 : TableState::TableState(RoutePathReplicator *replicator, BgpTable *table)
74 500 : : replicator_(replicator),
75 500 : table_(table),
76 500 : listener_id_(DBTableBase::kInvalidId),
77 500 : deleter_(new DeleteActor(this)),
78 500 : table_delete_ref_(this, table->deleter()),
79 500 : export_target_gen_(0) {
80 500 : assert(table->deleter() != NULL);
81 500 : }
82 :
83 500 : TableState::~TableState() {
84 500 : if (walk_ref() != NULL) {
85 0 : table()->ReleaseWalker(walk_ref());
86 : }
87 500 : }
88 :
89 500 : void TableState::ManagedDelete() {
90 500 : deleter()->Delete();
91 500 : }
92 :
93 0 : bool TableState::deleted() const {
94 0 : return deleter()->IsDeleted();
95 : }
96 :
97 33365 : LifetimeActor *TableState::deleter() {
98 33365 : return deleter_.get();
99 : }
100 :
101 0 : const LifetimeActor *TableState::deleter() const {
102 0 : return deleter_.get();
103 : }
104 :
105 500 : bool TableState::MayDelete() const {
106 1000 : if (list_.empty() && !route_count() &&
107 500 : ((walk_ref() == NULL) || !walk_ref()->walk_is_active()))
108 500 : return true;
109 0 : return false;
110 : }
111 :
112 32867 : void TableState::RetryDelete() {
113 32867 : if (!deleter()->IsDeleted())
114 32849 : return;
115 0 : deleter()->RetryDelete();
116 : }
117 :
118 2498 : void TableState::AddGroup(RtGroup *group) {
119 2498 : list_.insert(group);
120 2500 : }
121 :
122 2500 : void TableState::RemoveGroup(RtGroup *group) {
123 2500 : list_.erase(group);
124 2500 : }
125 :
126 899 : const RtGroup *TableState::FindGroup(RtGroup *group) const {
127 899 : GroupList::const_iterator it = list_.find(group);
128 898 : return (it != list_.end() ? *it : NULL);
129 : }
130 :
131 500 : uint32_t TableState::route_count() const {
132 500 : return table_->GetDBStateCount(listener_id());
133 : }
134 :
135 46938 : RtReplicated::RtReplicated(RoutePathReplicator *replicator)
136 46936 : : replicator_(replicator),
137 46938 : export_target_gen_(0) {
138 46898 : }
139 :
140 26365 : void RtReplicated::AddRouteInfo(BgpTable *table, BgpRoute *rt,
141 : ReplicatedRtPathList::const_iterator it) {
142 26365 : pair<ReplicatedRtPathList::iterator, bool> result;
143 26365 : result = replicate_list_.insert(*it);
144 26366 : assert(result.second);
145 26366 : }
146 :
147 26315 : void RtReplicated::DeleteRouteInfo(BgpTable *table, BgpRoute *rt,
148 : ReplicatedRtPathList::const_iterator it) {
149 26315 : replicator_->DeleteSecondaryPath(table, rt, *it);
150 26363 : replicate_list_.erase(it);
151 26360 : }
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 0 : vector<string> RtReplicated::GetTableNameList(const BgpPath *path) const {
159 0 : vector<string> table_list;
160 0 : BOOST_FOREACH(const SecondaryRouteInfo &rinfo, replicate_list_) {
161 0 : if (rinfo.peer_ != path->GetPeer())
162 0 : continue;
163 0 : if (rinfo.path_id_ != path->GetPathId())
164 0 : continue;
165 0 : if (rinfo.src_ != path->GetSource())
166 0 : continue;
167 0 : table_list.push_back(rinfo.table_->name());
168 : }
169 0 : return table_list;
170 0 : }
171 :
172 100 : RoutePathReplicator::RoutePathReplicator(BgpServer *server,
173 100 : Address::Family family)
174 100 : : server_(server),
175 100 : family_(family),
176 100 : vpn_table_(NULL),
177 100 : trace_buf_(SandeshTraceBufferCreate("RoutePathReplicator", 500)) {
178 100 : }
179 :
180 200 : RoutePathReplicator::~RoutePathReplicator() {
181 100 : assert(table_state_list_.empty());
182 200 : }
183 :
184 100 : void RoutePathReplicator::Initialize() {
185 100 : assert(!vpn_table_);
186 100 : RoutingInstanceMgr *mgr = server_->routing_instance_mgr();
187 100 : assert(mgr);
188 100 : RoutingInstance *master = mgr->GetDefaultRoutingInstance();
189 100 : assert(master);
190 100 : vpn_table_ = master->GetTable(family_);
191 100 : assert(vpn_table_);
192 100 : assert(AddTableState(vpn_table_));
193 100 : }
194 :
195 2600 : TableState *RoutePathReplicator::AddTableState(BgpTable *table,
196 : RtGroup *group) {
197 2600 : assert(table->IsVpnTable() || group);
198 :
199 2600 : TableStateList::iterator loc = table_state_list_.find(table);
200 2600 : if (loc == table_state_list_.end()) {
201 500 : TableState *ts = new TableState(this, table);
202 500 : DBTableBase::ListenerId id = table->Register(
203 : boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
204 : "RoutePathReplicator");
205 500 : ts->set_listener_id(id);
206 500 : if (group)
207 400 : ts->AddGroup(group);
208 500 : table_state_list_.insert(make_pair(table, ts));
209 500 : RPR_TRACE(RegTable, table->name());
210 500 : return ts;
211 : } else {
212 2099 : assert(group);
213 2099 : TableState *ts = loc->second;
214 2099 : ts->AddGroup(group);
215 2100 : return ts;
216 : }
217 : }
218 :
219 2500 : void RoutePathReplicator::RemoveTableState(BgpTable *table, RtGroup *group) {
220 2500 : TableState *ts = FindTableState(table);
221 2500 : assert(ts);
222 2500 : ts->RemoveGroup(group);
223 2500 : }
224 :
225 500 : void RoutePathReplicator::DeleteTableState(BgpTable *table) {
226 500 : TableState *ts = FindTableState(table);
227 500 : assert(ts);
228 500 : RPR_TRACE(UnregTable, table->name());
229 500 : table->Unregister(ts->listener_id());
230 500 : table_state_list_.erase(table);
231 500 : delete ts;
232 500 : }
233 :
234 6399 : TableState *RoutePathReplicator::FindTableState(BgpTable *table) {
235 6399 : TableStateList::iterator loc = table_state_list_.find(table);
236 6399 : return (loc != table_state_list_.end() ? loc->second : NULL);
237 : }
238 :
239 0 : const TableState *RoutePathReplicator::FindTableState(
240 : const BgpTable *table) const {
241 : TableStateList::const_iterator loc =
242 0 : table_state_list_.find(const_cast<BgpTable *>(table));
243 0 : return (loc != table_state_list_.end() ? loc->second : NULL);
244 : }
245 :
246 : void
247 0 : RoutePathReplicator::RequestWalk(BgpTable *table) {
248 0 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
249 0 : TableState *ts = FindTableState(table);
250 0 : assert(ts);
251 0 : if (!ts->walk_ref()) {
252 : DBTable::DBTableWalkRef walk_ref = table->AllocWalker(
253 : boost::bind(&RoutePathReplicator::RouteListener, this, ts, _1, _2),
254 0 : boost::bind(&RoutePathReplicator::BulkReplicationDone, this, _2));
255 0 : table->WalkTable(walk_ref);
256 0 : ts->set_walk_ref(walk_ref);
257 0 : } else {
258 0 : table->WalkAgain(ts->walk_ref());
259 : }
260 0 : }
261 :
262 : void
263 0 : RoutePathReplicator::BulkReplicationDone(DBTableBase *dbtable) {
264 0 : CHECK_CONCURRENCY("db::Walker");
265 0 : BgpTable *table = static_cast<BgpTable *>(dbtable);
266 0 : RPR_TRACE(WalkDone, table->name());
267 0 : TableState *ts = FindTableState(table);
268 0 : ts->RetryDelete();
269 0 : }
270 :
271 898 : void RoutePathReplicator::JoinVpnTable(RtGroup *group) {
272 898 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
273 899 : TableState *vpn_ts = FindTableState(vpn_table_);
274 899 : if (!vpn_ts || vpn_ts->FindGroup(group))
275 0 : return;
276 898 : RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), true);
277 900 : group->AddImportTable(family(), vpn_table_);
278 900 : RPR_TRACE(TableJoin, vpn_table_->name(), group->rt().ToString(), false);
279 900 : group->AddExportTable(family(), vpn_table_);
280 900 : AddTableState(vpn_table_, group);
281 : }
282 :
283 900 : void RoutePathReplicator::LeaveVpnTable(RtGroup *group) {
284 900 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
285 900 : TableState *vpn_ts = FindTableState(vpn_table_);
286 900 : if (!vpn_ts)
287 0 : return;
288 900 : RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), true);
289 900 : group->RemoveImportTable(family(), vpn_table_);
290 900 : RPR_TRACE(TableLeave, vpn_table_->name(), group->rt().ToString(), false);
291 900 : group->RemoveExportTable(family(), vpn_table_);
292 900 : 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 3999 : void RoutePathReplicator::Join(BgpTable *table, const RouteTarget &rt,
301 : bool import) {
302 3999 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
303 :
304 3991 : std::scoped_lock lock(mutex_);
305 4000 : RPR_TRACE(TableJoin, table->name(), rt.ToString(), import);
306 :
307 4000 : bool first = false;
308 4000 : RtGroup *group = server()->rtarget_group_mgr()->LocateRtGroup(rt);
309 4000 : if (import) {
310 2400 : first = group->AddImportTable(family(), table);
311 2400 : if (group->HasDepRoutes())
312 0 : server()->rtarget_group_mgr()->NotifyRtGroup(rt);
313 2398 : if (family_ == Address::INETVPN)
314 479 : server_->NotifyAllStaticRoutes();
315 9951 : BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
316 3776 : if (sec_table->IsVpnTable() || sec_table->empty())
317 3776 : continue;
318 0 : RequestWalk(sec_table);
319 : }
320 : } else {
321 1600 : first = group->AddExportTable(family(), table);
322 1600 : 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 1600 : ts->bump_export_target_gen();
327 1600 : if (!table->empty()) {
328 0 : RequestWalk(table);
329 : }
330 : }
331 :
332 : // Join the vpn table when group is created.
333 3998 : if (first)
334 898 : JoinVpnTable(group);
335 4000 : }
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 4000 : void RoutePathReplicator::Leave(BgpTable *table, const RouteTarget &rt,
343 : bool import) {
344 4000 : CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
345 :
346 4000 : std::scoped_lock lock(mutex_);
347 4000 : RtGroup *group = server()->rtarget_group_mgr()->GetRtGroup(rt);
348 4000 : assert(group);
349 4000 : RPR_TRACE(TableLeave, table->name(), rt.ToString(), import);
350 :
351 4000 : if (import) {
352 2400 : group->RemoveImportTable(family(), table);
353 2400 : if (group->HasDepRoutes())
354 0 : server()->rtarget_group_mgr()->NotifyRtGroup(rt);
355 2400 : if (family_ == Address::INETVPN)
356 480 : server_->NotifyAllStaticRoutes();
357 15200 : BOOST_FOREACH(BgpTable *sec_table, group->GetExportTables(family())) {
358 6400 : if (sec_table->IsVpnTable() || sec_table->empty())
359 6400 : continue;
360 0 : RequestWalk(sec_table);
361 : }
362 : } else {
363 1600 : group->RemoveExportTable(family(), table);
364 1600 : RemoveTableState(table, group);
365 : // The paths replicated earlier still carry the target being removed.
366 1600 : FindTableState(table)->bump_export_target_gen();
367 1600 : if (!table->empty()) {
368 0 : RequestWalk(table);
369 : }
370 : }
371 :
372 : // Leave the vpn table when the last VRF has left the group.
373 4000 : if (!group->HasVrfTables(family())) {
374 900 : LeaveVpnTable(group);
375 900 : server()->rtarget_group_mgr()->RemoveRtGroup(rt);
376 : }
377 4000 : }
378 :
379 57855 : void RoutePathReplicator::DBStateSync(BgpTable *table, TableState *ts,
380 : BgpRoute *rt, RtReplicated *dbstate,
381 : const RtReplicated::ReplicatedRtPathList *future) {
382 57855 : 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 57839 : if (dbstate->GetList().empty()) {
387 47029 : rt->ClearState(table, ts->listener_id());
388 47079 : delete dbstate;
389 47026 : if (table->GetDBStateCount(ts->listener_id()) == 0)
390 32870 : ts->RetryDelete();
391 : }
392 57872 : }
393 :
394 11548 : static ExtCommunityPtr UpdateOriginVn(BgpServer *server,
395 : const ExtCommunity *ext_community,
396 : int vn_index) {
397 11548 : as_t asn = server->autonomous_system();
398 11548 : ExtCommunityPtr extcomm_ptr;
399 11548 : if (vn_index) {
400 0 : 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 0 : OriginVn origin_vn(asn, vn_index);
409 0 : extcomm_ptr = server->extcomm_db()->ReplaceOriginVnAndLocate(
410 0 : ext_community, origin_vn.GetExtCommunity());
411 0 : return extcomm_ptr;
412 : }
413 : }
414 11548 : extcomm_ptr = server->extcomm_db()->RemoveOriginVnAndLocate(ext_community);
415 11558 : 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 12684 : 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 12684 : ExtCommunityPtr extcomm_ptr;
428 12684 : if (!rtinstance->IsMasterRoutingInstance()) {
429 : extcomm_ptr =
430 1151 : server->extcomm_db()->AppendAndLocate(ext_community, export_list);
431 1152 : return extcomm_ptr;
432 : }
433 :
434 : // Bail if we have a vpn route without extended communities.
435 11533 : if (!ext_community)
436 0 : 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 146367 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
441 : ext_community->communities()) {
442 67420 : if (!ExtCommunity::is_origin_vn(comm))
443 67417 : continue;
444 0 : OriginVn origin_vn(comm);
445 0 : if (!origin_vn.IsGlobal() &&
446 0 : origin_vn.as_number() != server->autonomous_system()) {
447 0 : continue;
448 : }
449 0 : return ExtCommunityPtr(ext_community);
450 : }
451 :
452 : // Add the OriginVn if we have a valid vn index.
453 : int vn_index =
454 11537 : server->routing_instance_mgr()->GetVnIndexByExtCommunity(ext_community);
455 11548 : return UpdateOriginVn(server, ext_community, vn_index);
456 12710 : }
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 86786 : bool RoutePathReplicator::RouteListener(TableState *ts,
471 : DBTablePartBase *root, DBEntryBase *entry) {
472 86786 : CHECK_CONCURRENCY("db::DBTable");
473 :
474 86460 : BgpTable *table = static_cast<BgpTable *>(root->parent());
475 86454 : BgpRoute *rt = static_cast<BgpRoute *>(entry);
476 86454 : const RoutingInstance *rtinstance = table->routing_instance();
477 :
478 86476 : DBTableBase::ListenerId id = ts->listener_id();
479 86471 : assert(id != DBTableBase::kInvalidId);
480 :
481 : // Get the DBState.
482 : RtReplicated *dbstate =
483 86471 : static_cast<RtReplicated *>(rt->GetState(table, id));
484 87026 : RtReplicated::ReplicatedRtPathList replicated_path_list;
485 :
486 : //Flag to track if any change happenned to route in last 30 minutes
487 86860 : bool route_unchanged = false;
488 : //List of tables that needs replication of route
489 86860 : RtGroup::RtGroupMemberList addedTables;
490 : //List of tables that doesn't need replication of route
491 86697 : RtGroup::RtGroupMemberList deletedTables;
492 : //List of tables having the latest changes in route
493 86616 : RtGroup::RtGroupMemberList previousTables;
494 : //Threshold used for optimised replication
495 86569 : uint64_t optimizationThresholdTime = 30ULL * 60 * 1000000;
496 86569 : 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 87041 : 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 86988 : if ( (start - rt->last_update_at() > optimizationThresholdTime) &&
508 86967 : dbstate != NULL &&
509 0 : dbstate->export_target_gen() == export_target_gen)
510 : {
511 0 : RPR_TRACE(RouteListener, "Route change occurred before threshold \
512 : time, optimising replication");
513 0 : 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 115061 : if (!rt->IsUsable() || (table->IsRouteAggregationSupported() &&
522 28111 : !rtinstance->deleted() &&
523 28105 : table->IsContributingRoute(rt))) {
524 34484 : if (!dbstate) {
525 29103 : return true;
526 : }
527 5381 : DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
528 5390 : 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 52339 : if (dbstate == NULL) {
535 46947 : dbstate = new RtReplicated(this);
536 46894 : rt->SetState(table, id, dbstate);
537 : }
538 :
539 : // Get the export route target list from the routing instance.
540 52443 : ExtCommunity::ExtCommunityList export_list;
541 52398 : if (!rtinstance->IsMasterRoutingInstance()) {
542 255853 : BOOST_FOREACH(RouteTarget rtarget, rtinstance->GetExportList()) {
543 113532 : 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 52339 : if (route_unchanged){
550 0 : replicated_path_list = dbstate->GetList();
551 0 : BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
552 : replicated_path_list){
553 0 : previousTables.insert(path.table_);
554 : }
555 : }
556 :
557 : // Replicate all feasible and non-replicated paths.
558 52339 : for (Route::PathList::iterator it = rt->GetPathList().begin();
559 209555 : it != rt->GetPathList().end(); ++it) {
560 52326 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
561 :
562 : // Skip if the source peer is down.
563 97788 : if (!path->IsStale() && !path->IsLlgrStale() && path->GetPeer() &&
564 45307 : !path->GetPeer()->IsReady())
565 39782 : continue;
566 :
567 : // No need to replicate aliased or replicated paths.
568 41206 : if (path->IsReplicated() || path->IsAliased())
569 28509 : continue;
570 :
571 : // Do not replicate non-ecmp paths.
572 12696 : if (rt->BestPath()->PathCompare(*path, true))
573 0 : break;
574 :
575 : // Do not replicate if nexthop is not hitched by ermvpn tree.
576 12686 : if (path->CheckErmVpn())
577 0 : break;
578 :
579 12686 : const BgpAttr *attr = path->GetAttr();
580 12686 : const ExtCommunity *ext_community = attr->ext_community();
581 :
582 : ExtCommunityPtr extcomm_ptr =
583 12685 : UpdateExtCommunity(server(), rtinstance, ext_community, export_list);
584 12710 : ext_community = extcomm_ptr.get();
585 12710 : if (!ext_community)
586 0 : 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 12710 : int vn_index = 0;
594 12710 : RtGroup::RtGroupMemberList secondary_tables;
595 162882 : BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
596 : ext_community->communities()) {
597 75087 : if (ExtCommunity::is_origin_vn(comm)) {
598 0 : OriginVn origin_vn(comm);
599 0 : vn_index = origin_vn.vn_index();
600 75072 : } else if (ExtCommunity::is_route_target(comm)) {
601 : RtGroup *group =
602 50722 : server()->rtarget_group_mgr()->GetRtGroup(comm);
603 50841 : if (!group)
604 0 : continue;
605 : RtGroup::RtGroupMemberList import_list =
606 50841 : group->GetImportTables(family());
607 50778 : if (import_list.empty())
608 0 : continue;
609 50776 : secondary_tables.insert(import_list.begin(), import_list.end());
610 50704 : }
611 : }
612 :
613 : // Update with family specific secondary tables.
614 12703 : table->UpdateSecondaryTablesForReplication(rt, &secondary_tables);
615 :
616 : // Skip if we don't need to replicate the path to any tables.
617 12702 : if (secondary_tables.empty())
618 0 : continue;
619 :
620 : // Add OriginVn when replicating self-originated routes from a VRF.
621 12703 : if (!vn_index && !rtinstance->IsMasterRoutingInstance() &&
622 25404 : path->IsVrfOriginated() && rtinstance->virtual_network_index()) {
623 0 : vn_index = rtinstance->virtual_network_index();
624 0 : extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(), vn_index);
625 : }
626 :
627 12702 : if (route_unchanged){
628 0 : addedTables = RtGroup::RtGroupMemberList();
629 : // Finding the difference between sets, secondary_tables and
630 : // previousTables
631 0 : std::set_difference(secondary_tables.begin(), secondary_tables.end(),
632 : previousTables.begin(), previousTables.end(),
633 : std::inserter(addedTables, addedTables.end()));
634 0 : std::set_difference(previousTables.begin(), previousTables.end(),
635 : secondary_tables.begin(), secondary_tables.end(),
636 : std::inserter(deletedTables, deletedTables.end()));
637 0 : BOOST_FOREACH (RtReplicated::SecondaryRouteInfo path,
638 : dbstate->GetList()){
639 0 : if (deletedTables.find(path.table_)!=deletedTables.end()) {
640 0 : replicated_path_list.erase(path);
641 : }
642 : }
643 : //Updating the secondary tables with the difference.
644 0 : secondary_tables = addedTables;
645 :
646 : }
647 :
648 :
649 : // Replicate path to all destination tables.
650 139716 : BOOST_FOREACH(BgpTable *dest, secondary_tables) {
651 : // Skip if destination is same as source table.
652 63497 : if (dest == table)
653 21399 : continue;
654 :
655 50800 : const RoutingInstance *dest_rtinstance = dest->routing_instance();
656 50799 : 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 50842 : if (!vn_index && dest_rtinstance->virtual_network_index() &&
663 0 : dest_rtinstance->HasExportTarget(ext_community)) {
664 0 : int dest_vn_index = dest_rtinstance->virtual_network_index();
665 0 : new_extcomm_ptr = UpdateOriginVn(server_, extcomm_ptr.get(),
666 0 : 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 50839 : BgpRoute *replicated_rt = dest->RouteReplicate(
673 : server_, table, rt, path, new_extcomm_ptr);
674 50840 : if (!replicated_rt)
675 8704 : continue;
676 :
677 : // Add information about the secondary path to the replicated path
678 : // list.
679 42132 : RtReplicated::SecondaryRouteInfo rtinfo(dest, path->GetPeer(),
680 84266 : path->GetPathId(), path->GetSource(), replicated_rt);
681 42128 : pair<RtReplicated::ReplicatedRtPathList::iterator, bool> result;
682 42120 : result = replicated_path_list.insert(rtinfo);
683 : // Assert if the insertion to replication path list fails
684 42128 : if (!route_unchanged)
685 42128 : assert(result.second);
686 42128 : RPR_TRACE_ONLY(Replicate, table->name(), rt->ToString(),
687 : path->ToString(),
688 : BgpPath::PathIdString(path->GetPathId()),
689 : dest->name(), replicated_rt->ToString());
690 50843 : }
691 12710 : }
692 :
693 : // Record what this replication was done with, for the next walk.
694 52387 : 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 52463 : DBStateSync(table, ts, rt, dbstate, &replicated_path_list);
699 52493 : return true;
700 86986 : }
701 0 : const RtReplicated *RoutePathReplicator::GetReplicationState(
702 : BgpTable *table, BgpRoute *rt) const {
703 0 : const TableState *ts = FindTableState(table);
704 0 : if (!ts)
705 0 : return NULL;
706 : RtReplicated *dbstate =
707 0 : static_cast<RtReplicated *>(rt->GetState(table, ts->listener_id()));
708 0 : return dbstate;
709 : }
710 :
711 : //
712 : // Return the list of secondary table names for the given primary path.
713 : //
714 0 : vector<string> RoutePathReplicator::GetReplicatedTableNameList(
715 : const BgpTable *table, const BgpRoute *rt, const BgpPath *path) const {
716 0 : const TableState *ts = FindTableState(table);
717 0 : if (!ts)
718 0 : return vector<string>();
719 : const RtReplicated *dbstate = static_cast<const RtReplicated *>(
720 0 : rt->GetState(table, ts->listener_id()));
721 0 : if (!dbstate)
722 0 : return vector<string>();
723 0 : return dbstate->GetTableNameList(path);
724 : }
725 :
726 26310 : void RoutePathReplicator::DeleteSecondaryPath(BgpTable *table, BgpRoute *rt,
727 : const RtReplicated::SecondaryRouteInfo &rtinfo) {
728 26310 : BgpRoute *rt_secondary = rtinfo.rt_;
729 26310 : BgpTable *secondary_table = rtinfo.table_;
730 26310 : const IPeer *peer = rtinfo.peer_;
731 26310 : uint32_t path_id = rtinfo.path_id_;
732 26310 : BgpPath::PathSource src = rtinfo.src_;
733 :
734 : DBTablePartBase *partition =
735 26310 : secondary_table->GetTablePartition(rt_secondary);
736 26299 : BgpPath *secondary_path = rt_secondary->FindSecondaryPath(rt, src,
737 : peer, path_id);
738 26304 : if (secondary_path && secondary_path->NeedsResolution()) {
739 0 : secondary_table->path_resolver()->StopPathResolution(partition->index(),
740 : secondary_path);
741 : }
742 26304 : assert(rt_secondary->RemoveSecondaryPath(rt, src, peer, path_id));
743 26345 : if (rt_secondary->count() == 0) {
744 26320 : 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 26366 : partition->Delete(rt_secondary);
749 : } else {
750 0 : partition->Notify(rt_secondary);
751 0 : 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 26365 : }
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 : }
|