/
bgp_table.cc
576 lines (485 loc) · 18.7 KB
/
bgp_table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_table.h"
#include <boost/foreach.hpp>
#include "base/task_annotations.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_ribout.h"
#include "bgp/bgp_ribout_updates.h"
#include "bgp/bgp_route.h"
#include "bgp/bgp_server.h"
#include "bgp/bgp_update_queue.h"
#include "bgp/routing-instance/iroute_aggregator.h"
#include "bgp/routing-instance/path_resolver.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/routing-instance/rtarget_group_mgr.h"
#include "net/community_type.h"
using std::map;
using std::make_pair;
using std::ostringstream;
using std::string;
class BgpTable::DeleteActor : public LifetimeActor {
public:
explicit DeleteActor(BgpTable *table)
: LifetimeActor(table->rtinstance_->server()->lifetime_manager()),
table_(table) {
}
virtual ~DeleteActor() {
}
virtual bool MayDelete() const {
return table_->MayDelete();
}
virtual void Shutdown() {
table_->Shutdown();
}
// Make sure that all notifications have been processed and all db
// state have been cleared for all partitions, before we inform the
// parent instance that this table deletion process is complete.
virtual void Destroy() {
table_->rtinstance_->DestroyDBTable(table_);
}
private:
BgpTable *table_;
};
BgpTable::BgpTable(DB *db, const string &name)
: RouteTable(db, name),
rtinstance_(NULL),
path_resolver_(NULL),
instance_delete_ref_(this, NULL) {
primary_path_count_ = 0;
secondary_path_count_ = 0;
infeasible_path_count_ = 0;
}
//
// Remove the table from the instance dependents before attempting to
// destroy the DeleteActor which can have its Delete() method be called
// via the reference.
//
BgpTable::~BgpTable() {
assert(path_resolver_ == NULL),
instance_delete_ref_.Reset(NULL);
}
void BgpTable::set_routing_instance(RoutingInstance *rtinstance) {
rtinstance_ = rtinstance;
assert(rtinstance);
deleter_.reset(new DeleteActor(this));
instance_delete_ref_.Reset(rtinstance->deleter());
path_resolver_ = CreatePathResolver();
if (IsRouteAggregationSupported())
rtinstance->route_aggregator(family())->Initialize();
}
BgpServer *BgpTable::server() {
return rtinstance_->server();
}
const BgpServer *BgpTable::server() const {
return rtinstance_->server();
}
//
// Find the RibOut for the given RibExportPolicy.
//
RibOut *BgpTable::RibOutFind(const RibExportPolicy &policy) {
RibOutMap::iterator loc = ribout_map_.find(policy);
return (loc != ribout_map_.end()) ? loc->second : NULL;
}
//
// Find or create the RibOut associated with the given RibExportPolicy.
// If a new RibOut is created, an entry for the pair gets added to the
// RibOutMap.
//
RibOut *BgpTable::RibOutLocate(SchedulingGroupManager *mgr,
const RibExportPolicy &policy) {
RibOutMap::iterator loc = ribout_map_.find(policy);
if (loc == ribout_map_.end()) {
RibOut *ribout = new RibOut(this, mgr, policy);
ribout_map_.insert(make_pair(policy, ribout));
return ribout;
}
return loc->second;
}
//
// Delete the entry corresponding to the given RibExportPolicy from the
// RibOutMap. Also deletes the RibOut itself.
//
void BgpTable::RibOutDelete(const RibExportPolicy &policy) {
RibOutMap::iterator loc = ribout_map_.find(policy);
assert(loc != ribout_map_.end());
delete loc->second;
ribout_map_.erase(loc);
}
UpdateInfo *BgpTable::GetUpdateInfo(RibOut *ribout, BgpRoute *route,
const RibPeerSet &peerset) {
const BgpPath *path = route->BestPath();
// Ignore if there is no best-path.
if (!path)
return NULL;
// Don't advertise infeasible paths.
if (!path->IsFeasible())
return NULL;
// Check whether the route is contributing route
if (IsRouteAggregationSupported() && IsContributingRoute(route))
return NULL;
// Needs to be outside the if block so it's not destroyed prematurely.
BgpAttrPtr attr_ptr;
const BgpAttr *attr = path->GetAttr();
RibPeerSet new_peerset = peerset;
// LocalPref, Med and AsPath manipulation is needed only if the RibOut
// has BGP encoding. Similarly, well-known communities do not apply if
// the encoding is not BGP.
if (ribout->IsEncodingBgp()) {
// Handle well-known communities.
if (attr->community() != NULL &&
attr->community()->communities().size()) {
BOOST_FOREACH(uint32_t value, attr->community()->communities()) {
if (value == CommunityType::NoAdvertise)
return NULL;
if ((ribout->peer_type() == BgpProto::EBGP) &&
((value == CommunityType::NoExport) ||
(value == CommunityType::NoExportSubconfed))) {
return NULL;
}
}
}
const IPeer *peer = path->GetPeer();
if (ribout->peer_type() == BgpProto::IBGP) {
// Split horizon check.
if (peer && peer->PeerType() == BgpProto::IBGP)
return NULL;
// Handle route-target filtering.
if (attr->ext_community() != NULL) {
server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
ribout, attr->ext_community(), peerset, &new_peerset);
if (new_peerset.empty())
return NULL;
}
BgpAttr *clone = new BgpAttr(*attr);
// Retain LocalPref value if set, else set default to 100.
if (clone->local_pref() == 0)
clone->set_local_pref(100);
// If the route is locally originated i.e. there's no AsPath,
// then generate a Nil AsPath i.e. one with 0 length. No need
// to modify the AsPath if it already exists since this is an
// iBGP RibOut.
if (clone->as_path() == NULL) {
AsPathSpec as_path;
clone->set_as_path(&as_path);
}
attr_ptr = clone->attr_db()->Locate(clone);
attr = attr_ptr.get();
} else if (ribout->peer_type() == BgpProto::EBGP) {
// Don't advertise routes from non-master instances if there's
// no nexthop. The ribout has to be for bgpaas-clients because
// that's the only case with bgp peers in non-master instance.
if (!rtinstance_->IsMasterRoutingInstance() &&
ribout->nexthop().is_unspecified()) {
return NULL;
}
// Sender side AS path loop check.
if (!ribout->as_override() && attr->as_path() &&
attr->as_path()->path().AsPathLoop(ribout->peer_as())) {
return NULL;
}
// Handle route-target filtering.
if (attr->ext_community() != NULL) {
server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
ribout, attr->ext_community(), peerset, &new_peerset);
if (new_peerset.empty())
return NULL;
}
BgpAttr *clone = new BgpAttr(*attr);
// Update nexthop.
if (!ribout->nexthop().is_unspecified())
clone->set_nexthop(ribout->nexthop());
// Reset LocalPref.
if (clone->local_pref())
clone->set_local_pref(0);
// Reset Med if the path did not originate from an xmpp peer.
// The AS path is NULL if the originating xmpp peer is locally
// connected. It's non-NULL but empty if the originating xmpp
// peer is connected to another bgp speaker in the iBGP mesh.
if (clone->med() && clone->as_path() && !clone->as_path()->empty())
clone->set_med(0);
as_t local_as =
clone->attr_db()->server()->local_autonomous_system();
// Override the peer AS with local AS in AsPath.
if (ribout->as_override() && clone->as_path() != NULL) {
const AsPathSpec &as_path = clone->as_path()->path();
AsPathSpec *as_path_ptr =
as_path.Replace(ribout->peer_as(), local_as);
clone->set_as_path(as_path_ptr);
delete as_path_ptr;
}
// Prepend the local AS to AsPath.
if (clone->as_path() != NULL) {
const AsPathSpec &as_path = clone->as_path()->path();
AsPathSpec *as_path_ptr = as_path.Add(local_as);
clone->set_as_path(as_path_ptr);
delete as_path_ptr;
} else {
AsPathSpec as_path;
AsPathSpec *as_path_ptr = as_path.Add(local_as);
clone->set_as_path(as_path_ptr);
delete as_path_ptr;
}
attr_ptr = clone->attr_db()->Locate(clone);
attr = attr_ptr.get();
}
}
UpdateInfo *uinfo = new UpdateInfo;
uinfo->target = new_peerset;
uinfo->roattr = RibOutAttr(route, attr, ribout->IsEncodingXmpp());
return uinfo;
}
// Bgp Path selection..
// Based Attribute weight
bool BgpTable::PathSelection(const Path &path1, const Path &path2) {
const BgpPath &l_path = dynamic_cast<const BgpPath &> (path1);
const BgpPath &r_path = dynamic_cast<const BgpPath &> (path2);
// Check the weight of Path
bool res = l_path.PathCompare(r_path, false) < 0;
return res;
}
bool BgpTable::InputCommon(DBTablePartBase *root, BgpRoute *rt, BgpPath *path,
const IPeer *peer, DBRequest *req,
DBRequest::DBOperation oper, BgpAttrPtr attrs,
uint32_t path_id, uint32_t flags, uint32_t label) {
bool notify_rt = false;
switch (oper) {
case DBRequest::DB_ENTRY_ADD_CHANGE: {
assert(rt);
// The entry may currently be marked as deleted.
rt->ClearDelete();
// Check whether peer already has a path.
if (path != NULL) {
if ((path->GetAttr() != attrs.get()) ||
(path->GetFlags() != flags) ||
(path->GetLabel() != label)) {
// Update Attributes and notify (if needed)
if (path->NeedsResolution())
path_resolver_->StopPathResolution(root->index(), path);
rt->DeletePath(path);
} else {
// Ignore duplicate update.
break;
}
}
BgpPath *new_path;
new_path =
new BgpPath(peer, path_id, BgpPath::BGP_XMPP, attrs, flags, label);
if (new_path->NeedsResolution()) {
Address::Family family = new_path->GetAttr()->nexthop_family();
BgpTable *table = rtinstance_->GetTable(family);
path_resolver_->StartPathResolution(root->index(), new_path, rt,
table);
}
rt->InsertPath(new_path);
notify_rt = true;
break;
}
case DBRequest::DB_ENTRY_DELETE: {
if (rt && !rt->IsDeleted()) {
BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
"Delete BGP path");
// Remove the Path from the route
if (path->NeedsResolution())
path_resolver_->StopPathResolution(root->index(), path);
rt->RemovePath(BgpPath::BGP_XMPP, peer, path_id);
notify_rt = true;
}
break;
}
default: {
assert(false);
break;
}
}
return notify_rt;
}
void BgpTable::Input(DBTablePartition *root, DBClient *client,
DBRequest *req) {
const IPeer *peer =
(static_cast<RequestKey *>(req->key.get()))->GetPeer();
// Skip if this peer is down
if ((req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) &&
peer && !peer->IsReady())
return;
BgpRoute *rt = TableFind(root, req->key.get());
RequestData *data = static_cast<RequestData *>(req->data.get());
BgpPath *path = NULL;
// First mark all paths from this request source as deleted.
// Apply all paths provided in this request data and add them. If path
// already exists, reset from it getting deleted. Finally walk the paths
// list again to purge any stale paths originated from this peer.
// Create rt if it is not already there for adds/updates.
if (!rt) {
if ((req->oper == DBRequest::DB_ENTRY_DELETE) ||
(req->oper == DBRequest::DB_ENTRY_NOTIFY))
return;
rt = static_cast<BgpRoute *>(Add(req));
static_cast<DBTablePartition *>(root)->Add(rt);
BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
"Insert new BGP path");
}
if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
root->Notify(rt);
return;
}
// Use a map to mark and sweep deleted paths, update the rest.
map<BgpPath *, bool> deleted_paths;
// Mark this peer's all paths as deleted.
for (Route::PathList::iterator it = rt->GetPathList().begin();
it != rt->GetPathList().end(); ++it) {
// Skip secondary paths.
if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) continue;
BgpPath *path = static_cast<BgpPath *>(it.operator->());
if (path->GetPeer() == peer &&
path->GetSource() == BgpPath::BGP_XMPP) {
deleted_paths.insert(make_pair(path, true));
}
}
int count = 0;
ExtCommunityDB *extcomm_db = rtinstance_->server()->extcomm_db();
BgpAttrPtr attr = data ? data->attrs() : NULL;
bool notify_rt = false;
// Process each of the paths sourced and create/update paths accordingly.
if (data) {
for (RequestData::NextHops::iterator iter = data->nexthops().begin(),
next = iter;
iter != data->nexthops().end(); iter = next, ++count) {
next++;
RequestData::NextHop nexthop = *iter;
// Don't support multi path with v6 nexthops for the time being.
uint32_t path_id = 0;
if (nexthop.address_.is_v4()) {
path_id = nexthop.address_.to_v4().to_ulong();
} else if (count > 0) {
break;
}
path = rt->FindPath(BgpPath::BGP_XMPP, peer, path_id);
if (path && req->oper != DBRequest::DB_ENTRY_DELETE)
deleted_paths.erase(path);
if (data->attrs() && count > 0) {
BgpAttr *clone = new BgpAttr(*data->attrs());
clone->set_ext_community(
extcomm_db->ReplaceTunnelEncapsulationAndLocate(
clone->ext_community(),
nexthop.tunnel_encapsulations_));
clone->set_nexthop(nexthop.address_);
clone->set_source_rd(nexthop.source_rd_);
attr = data->attrs()->attr_db()->Locate(clone);
}
notify_rt |= InputCommon(root, rt, path, peer, req, req->oper,
attr, path_id, nexthop.flags_,
nexthop.label_);
}
}
// Flush remaining paths that remain marked for deletion.
for (map<BgpPath *, bool>::iterator it = deleted_paths.begin();
it != deleted_paths.end(); it++) {
BgpPath *path = it->first;
notify_rt |= InputCommon(root, rt, path, peer, req,
DBRequest::DB_ENTRY_DELETE, NULL,
path->GetPathId(), 0, 0);
}
InputCommonPostProcess(root, rt, notify_rt);
}
void BgpTable::InputCommonPostProcess(DBTablePartBase *root,
BgpRoute *rt, bool notify_rt) {
if (!notify_rt)
return;
if (rt->front() == NULL)
root->Delete(rt);
else
root->Notify(rt);
}
bool BgpTable::MayDelete() const {
CHECK_CONCURRENCY("bgp::Config");
// Bail if the table has listeners.
if (HasListeners()) {
BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
"Paused table deletion due to pending listeners");
return false;
}
// Bail if the table has walkers.
if (HasWalkers()) {
BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
"Paused table deletion due to pending walkers");
return false;
}
// Bail if the table is not empty.
size_t size = Size();
if (size > 0) {
BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
"Paused table deletion due to " << size <<
" pending routes");
return false;
}
// Check the base class at the end so that we add custom checks
// before this if needed and to get more informative log message.
return DBTableBase::MayDelete();
}
void BgpTable::Shutdown() {
CHECK_CONCURRENCY("bgp::PeerMembership", "bgp::Config");
}
void BgpTable::ManagedDelete() {
BGP_LOG_TABLE(this, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL,
"Received request for table deletion");
deleter_->Delete();
}
//
// Retry deletion of the table if it is pending.
//
void BgpTable::RetryDelete() {
if (!deleter()->IsDeleted())
return;
deleter()->RetryDelete();
}
PathResolver *BgpTable::CreatePathResolver() {
return NULL;
}
void BgpTable::DestroyPathResolver() {
if (!path_resolver_)
return;
delete path_resolver_;
path_resolver_ = NULL;
}
size_t BgpTable::GetPendingRiboutsCount(size_t *markers) const {
CHECK_CONCURRENCY("bgp::ShowCommand", "bgp::Config");
size_t count = 0;
*markers = 0;
BOOST_FOREACH(const RibOutMap::value_type &i, ribout_map_) {
const RibOut *ribout = i.second;
if (ribout->updates()) {
BOOST_FOREACH(const UpdateQueue *queue,
ribout->updates()->queue_vec()) {
count += queue->size();
*markers += queue->marker_count();
}
}
}
return count;
}
LifetimeActor *BgpTable::deleter() {
return deleter_.get();
}
const LifetimeActor *BgpTable::deleter() const {
return deleter_.get();
}
void BgpTable::UpdatePathCount(const BgpPath *path, int count) {
if (dynamic_cast<const BgpSecondaryPath *>(path)) {
secondary_path_count_ += count;
} else {
primary_path_count_ += count;
}
if (!path->IsFeasible()) {
infeasible_path_count_ += count;
}
}
// Check whether the route is aggregate route
bool BgpTable::IsAggregateRoute(const BgpRoute *route) const {
return routing_instance()->IsAggregateRoute(this, route);
}
// Check whether the route is contributing route to aggregate route
bool BgpTable::IsContributingRoute(const BgpRoute *route) const {
return routing_instance()->IsContributingRoute(this, route);
}