-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_evpn.cc
662 lines (583 loc) · 19.6 KB
/
bgp_evpn.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_evpn.h"
#include <boost/foreach.hpp>
#include <algorithm>
#include <string>
#include "base/task_annotations.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_peer_types.h"
#include "bgp/bgp_server.h"
#include "bgp/bgp_update.h"
#include "bgp/evpn/evpn_table.h"
using std::sort;
using std::string;
using std::vector;
class EvpnManager::DeleteActor : public LifetimeActor {
public:
explicit DeleteActor(EvpnManager *evpn_manager)
: LifetimeActor(evpn_manager->server()->lifetime_manager()),
evpn_manager_(evpn_manager) {
}
virtual ~DeleteActor() {
}
virtual bool MayDelete() const {
return evpn_manager_->MayDelete();
}
virtual void Shutdown() {
evpn_manager_->Shutdown();
}
virtual void Destroy() {
evpn_manager_->table_->DestroyEvpnManager();
}
private:
EvpnManager *evpn_manager_;
};
//
// Constructor for EvpnMcastNode. The type indicates whether this is a local
// or remote EvpnMcastNode.
//
EvpnMcastNode::EvpnMcastNode(EvpnManagerPartition *partition,
EvpnRoute *route, uint8_t type)
: partition_(partition),
route_(route),
type_(type),
label_(0),
edge_replication_not_supported_(false),
assisted_replication_supported_(false),
assisted_replication_leaf_(false) {
UpdateAttributes(route);
}
//
// Destructor for EvpnMcastNode.
//
EvpnMcastNode::~EvpnMcastNode() {
}
//
// Update the label and attributes for a EvpnMcastNode.
// Return true if either of them changed.
//
bool EvpnMcastNode::UpdateAttributes(EvpnRoute *route) {
bool changed = false;
const BgpPath *path = route->BestPath();
if (path->GetLabel() != label_) {
label_ = path->GetLabel();
changed = true;
}
if (path->GetAttr() != attr_) {
attr_ = path->GetAttr();
changed = true;
}
const PmsiTunnel *pmsi_tunnel = attr_->pmsi_tunnel();
uint8_t ar_type =
pmsi_tunnel->tunnel_flags & PmsiTunnelSpec::AssistedReplicationType;
bool edge_replication_not_supported = false;
if ((pmsi_tunnel->tunnel_flags &
PmsiTunnelSpec::EdgeReplicationSupported) == 0) {
edge_replication_not_supported = true;
}
if (edge_replication_not_supported != edge_replication_not_supported_) {
edge_replication_not_supported_ = edge_replication_not_supported;
changed = true;
}
bool assisted_replication_supported = false;
if (ar_type == PmsiTunnelSpec::ARReplicator)
assisted_replication_supported = true;
if (assisted_replication_supported != assisted_replication_supported_) {
assisted_replication_supported_ = assisted_replication_supported;
changed = true;
}
bool assisted_replication_leaf = false;
if (ar_type == PmsiTunnelSpec::ARLeaf)
assisted_replication_leaf = true;
if (assisted_replication_leaf != assisted_replication_leaf_) {
assisted_replication_leaf_ = assisted_replication_leaf;
changed = true;
}
if (address_ != path->GetAttr()->nexthop().to_v4()) {
address_ = path->GetAttr()->nexthop().to_v4();
changed = true;
}
if (replicator_address_ != pmsi_tunnel->identifier) {
replicator_address_ = pmsi_tunnel->identifier;
changed = true;
}
return changed;
}
//
// Constructor for EvpnLocalMcastNode.
//
// Add an Inclusive Multicast route corresponding to Broadcast MAC route.
//
// Need to Notify the Broadcast MAC route so that the table Export method
// can run and build the OList. OList is not built till EvpnLocalMcastNode
// has been created.
//
EvpnLocalMcastNode::EvpnLocalMcastNode(EvpnManagerPartition *partition,
EvpnRoute *route)
: EvpnMcastNode(partition, route, EvpnMcastNode::LocalNode),
inclusive_mcast_route_(NULL) {
AddInclusiveMulticastRoute();
DBTablePartition *tbl_partition = partition_->GetTablePartition();
tbl_partition->Notify(route_);
}
//
// Destructor for EvpnLocalMcastNode.
//
EvpnLocalMcastNode::~EvpnLocalMcastNode() {
DeleteInclusiveMulticastRoute();
}
//
// Add Inclusive Multicast route for this EvpnLocalMcastNode.
// The attributes are based on the Broadcast MAC route.
//
void EvpnLocalMcastNode::AddInclusiveMulticastRoute() {
assert(!inclusive_mcast_route_);
if (label_ == 0)
return;
// Construct the prefix and route key.
const EvpnPrefix &mac_prefix = route_->GetPrefix();
EvpnPrefix prefix(mac_prefix.route_distinguisher(), mac_prefix.tag(),
address_);
EvpnRoute rt_key(prefix);
// Find or create the route.
DBTablePartition *tbl_partition = partition_->GetTablePartition();
EvpnRoute *route = static_cast<EvpnRoute *>(tbl_partition->Find(&rt_key));
if (!route) {
route = new EvpnRoute(prefix);
tbl_partition->Add(route);
} else {
route->ClearDelete();
}
// Add a path with source BgpPath::Local.
BgpPath *path = new BgpPath(BgpPath::Local, attr_, 0, label_);
route->InsertPath(path);
inclusive_mcast_route_ = route;
tbl_partition->Notify(inclusive_mcast_route_);
BGP_LOG_ROUTE(partition_->table(), static_cast<IPeer *>(NULL),
route, "Insert new Local path");
}
//
// Delete Inclusive Multicast route for this EvpnLocalMcastNode.
//
void EvpnLocalMcastNode::DeleteInclusiveMulticastRoute() {
if (!inclusive_mcast_route_)
return;
DBTablePartition *tbl_partition = partition_->GetTablePartition();
inclusive_mcast_route_->RemovePath(BgpPath::Local);
BGP_LOG_ROUTE(partition_->table(), static_cast<IPeer *>(NULL),
inclusive_mcast_route_, "Delete Local path");
if (!inclusive_mcast_route_->BestPath()) {
tbl_partition->Delete(inclusive_mcast_route_);
} else {
tbl_partition->Notify(inclusive_mcast_route_);
}
inclusive_mcast_route_ = NULL;
}
//
// Handle update of EvpnLocalMcastNode.
//
// We delete and add the Inclusive Multicast route to ensure that all the
// attributes are updated. An in-place update is not always possible since
// the vRouter address is part of the key for the Inclusive Multicast route.
//
void EvpnLocalMcastNode::TriggerUpdate() {
DeleteInclusiveMulticastRoute();
AddInclusiveMulticastRoute();
}
//
// Construct an UpdateInfo with the RibOutAttr that needs to be advertised to
// the IPeer for the EvpnRoute associated with this EvpnLocalMcastNode. This
// is used the Export method of the EvpnTable. It is expected that the caller
// fills in the target RibPeerSet in the UpdateInfo.
//
// The main functionality here is to build a per-IPeer BgpOList from the list
// of EvpnRemoteMcastNodes.
//
UpdateInfo *EvpnLocalMcastNode::GetUpdateInfo() {
CHECK_CONCURRENCY("db::DBTable");
// Nothing to send for a leaf as it already knows the replicator-address.
if (assisted_replication_leaf_)
return NULL;
// Go through list of EvpnRemoteMcastNodes and build the BgpOList.
BgpOListSpec olist_spec(BgpAttribute::OList);
BOOST_FOREACH(EvpnMcastNode *node, partition_->remote_mcast_node_list()) {
if (node->address() == address_)
continue;
if (node->assisted_replication_leaf())
continue;
if (!edge_replication_not_supported_ &&
!node->edge_replication_not_supported())
continue;
const ExtCommunity *extcomm = node->attr()->ext_community();
BgpOListElem elem(node->address(), node->label(),
extcomm ? extcomm->GetTunnelEncap() : vector<string>());
olist_spec.elements.push_back(elem);
}
// Go through list of leaf EvpnMcastNodes and build the leaf BgpOList.
BgpOListSpec leaf_olist_spec(BgpAttribute::LeafOList);
if (assisted_replication_supported_) {
BOOST_FOREACH(EvpnMcastNode *node, partition_->leaf_node_list()) {
if (node->replicator_address() != address_)
continue;
const ExtCommunity *extcomm = node->attr()->ext_community();
BgpOListElem elem(node->address(), node->label(),
extcomm ? extcomm->GetTunnelEncap() : vector<string>());
leaf_olist_spec.elements.push_back(elem);
}
}
// Bail if both BgpOLists are empty.
if (olist_spec.elements.empty() && leaf_olist_spec.elements.empty())
return NULL;
// Add BgpOList and leaf BgpOList to RibOutAttr for broadcast MAC route.
BgpAttrDB *attr_db = partition_->server()->attr_db();
BgpAttrPtr attr = attr_db->ReplaceOListAndLocate(attr_.get(), &olist_spec);
attr = attr_db->ReplaceLeafOListAndLocate(attr.get(), &leaf_olist_spec);
UpdateInfo *uinfo = new UpdateInfo;
uinfo->roattr =
RibOutAttr(partition_->table(), route_, attr.get(), 0, false);
return uinfo;
}
//
// Constructor for EvpnRemoteMcastNode.
//
EvpnRemoteMcastNode::EvpnRemoteMcastNode(EvpnManagerPartition *partition,
EvpnRoute *route)
: EvpnMcastNode(partition, route, EvpnMcastNode::RemoteNode) {
}
//
// Destructor for EvpnRemoteMcastNode.
//
EvpnRemoteMcastNode::~EvpnRemoteMcastNode() {
}
//
// Handle update of EvpnRemoteMcastNode.
//
void EvpnRemoteMcastNode::TriggerUpdate() {
}
//
// Constructor for EvpnManagerPartition.
//
EvpnManagerPartition::EvpnManagerPartition(EvpnManager *evpn_manager,
size_t part_id)
: evpn_manager_(evpn_manager), part_id_(part_id) {
}
//
// Destructor for EvpnManagerPartition.
//
EvpnManagerPartition::~EvpnManagerPartition() {
}
//
// Get the DBTablePartition for the EvpnTable for our partition id.
//
DBTablePartition *EvpnManagerPartition::GetTablePartition() {
return evpn_manager_->GetTablePartition(part_id_);
}
//
// Notify the Broadcast MAC route for the given EvpnMcastNode.
//
void EvpnManagerPartition::NotifyNodeRoute(EvpnMcastNode *node) {
DBTablePartition *tbl_partition = GetTablePartition();
tbl_partition->Notify(node->route());
}
//
// Go through all replicator EvpnMcastNodes and notify associated Broadcast
// MAC route.
//
void EvpnManagerPartition::NotifyReplicatorNodeRoutes() {
DBTablePartition *tbl_partition = GetTablePartition();
BOOST_FOREACH(EvpnMcastNode *node, replicator_node_list_) {
tbl_partition->Notify(node->route());
}
}
//
// Go through all ingress replication client EvpnMcastNodes and notify the
// associated Broadcast MAC route.
//
void EvpnManagerPartition::NotifyIrClientNodeRoutes(
bool exclude_edge_replication_supported) {
DBTablePartition *tbl_partition = GetTablePartition();
BOOST_FOREACH(EvpnMcastNode *node, ir_client_node_list_) {
if (exclude_edge_replication_supported &&
!node->edge_replication_not_supported()) {
continue;
}
tbl_partition->Notify(node->route());
}
}
//
// Add an EvpnMcastNode to the EvpnManagerPartition.
//
void EvpnManagerPartition::AddMcastNode(EvpnMcastNode *node) {
if (node->type() == EvpnMcastNode::LocalNode) {
local_mcast_node_list_.insert(node);
if (node->assisted_replication_supported())
replicator_node_list_.insert(node);
if (!node->assisted_replication_leaf())
ir_client_node_list_.insert(node);
NotifyNodeRoute(node);
} else {
remote_mcast_node_list_.insert(node);
if (node->assisted_replication_leaf()) {
leaf_node_list_.insert(node);
NotifyReplicatorNodeRoutes();
} else if (node->edge_replication_not_supported()) {
regular_node_list_.insert(node);
NotifyIrClientNodeRoutes(false);
} else if (!node->assisted_replication_leaf()) {
NotifyIrClientNodeRoutes(true);
}
}
}
//
// Delete an EvpnMcastNode from the EvpnManagerPartition.
//
void EvpnManagerPartition::DeleteMcastNode(EvpnMcastNode *node) {
if (node->type() == EvpnMcastNode::LocalNode) {
local_mcast_node_list_.erase(node);
replicator_node_list_.erase(node);
ir_client_node_list_.erase(node);
} else {
remote_mcast_node_list_.erase(node);
if (leaf_node_list_.erase(node) > 0) {
NotifyReplicatorNodeRoutes();
} else {
NotifyIrClientNodeRoutes(true);
}
if (regular_node_list_.erase(node) > 0)
NotifyIrClientNodeRoutes(false);
}
if (empty())
evpn_manager_->RetryDelete();
}
//
// Update an EvpnMcastNode in the EvpnManagerPartition.
// Need to remove/add EvpnMcastNode from the replicator, leaf and ir client
// lists as appropriate.
//
void EvpnManagerPartition::UpdateMcastNode(EvpnMcastNode *node) {
node->TriggerUpdate();
if (node->type() == EvpnMcastNode::LocalNode) {
replicator_node_list_.erase(node);
if (node->assisted_replication_supported())
replicator_node_list_.insert(node);
ir_client_node_list_.erase(node);
if (!node->assisted_replication_leaf())
ir_client_node_list_.insert(node);
NotifyNodeRoute(node);
} else {
bool was_leaf = leaf_node_list_.erase(node) > 0;
if (node->assisted_replication_leaf())
leaf_node_list_.insert(node);
if (was_leaf || node->assisted_replication_leaf())
NotifyReplicatorNodeRoutes();
if (!was_leaf || !node->assisted_replication_leaf())
NotifyIrClientNodeRoutes(true);
bool was_regular = regular_node_list_.erase(node) > 0;
if (node->edge_replication_not_supported())
regular_node_list_.insert(node);
if (was_regular || node->edge_replication_not_supported())
NotifyIrClientNodeRoutes(false);
}
}
//
// Return true if the EvpnManagerPartition is empty i.e. it has no local or
// remote EvpnMcastNodes.
//
bool EvpnManagerPartition::empty() const {
if (!local_mcast_node_list_.empty())
return false;
if (!remote_mcast_node_list_.empty())
return false;
assert(leaf_node_list_.empty());
assert(replicator_node_list_.empty());
assert(regular_node_list_.empty());
assert(ir_client_node_list_.empty());
return true;
}
//
// Return the BgpServer for the EvpnManagerPartition.
//
BgpServer *EvpnManagerPartition::server() {
return evpn_manager_->server();
}
//
// Return the EvpnTable for the EvpnManagerPartition.
//
const EvpnTable *EvpnManagerPartition::table() const {
return evpn_manager_->table();
}
//
// Constructor for EvpnManager.
//
EvpnManager::EvpnManager(EvpnTable *table)
: table_(table),
listener_id_(DBTable::kInvalidId),
table_delete_ref_(this, table->deleter()) {
deleter_.reset(new DeleteActor(this));
}
//
// Destructor for EvpnManager.
//
EvpnManager::~EvpnManager() {
}
//
// Initialize the EvpnManager. We allocate the EvpnManagerPartitions
// and register a DBListener for the EvpnTable.
//
void EvpnManager::Initialize() {
AllocPartitions();
listener_id_ = table_->Register(
boost::bind(&EvpnManager::RouteListener, this, _1, _2),
"EvpnManager");
}
//
// Terminate the EvpnManager. We free the EvpnManagerPartitions
// and unregister from the EvpnTable.
//
void EvpnManager::Terminate() {
table_->Unregister(listener_id_);
FreePartitions();
}
//
// Allocate the EvpnManagerPartitions.
//
void EvpnManager::AllocPartitions() {
for (int part_id = 0; part_id < DB::PartitionCount(); part_id++) {
partitions_.push_back(new EvpnManagerPartition(this, part_id));
}
}
//
// Free the EvpnManagerPartitions.
//
void EvpnManager::FreePartitions() {
STLDeleteValues(&partitions_);
}
//
// Get the DBTablePartition for the EvpnTable for given partition id.
//
DBTablePartition *EvpnManager::GetTablePartition(size_t part_id) {
return static_cast<DBTablePartition *>(table_->GetTablePartition(part_id));
}
//
// Construct export state for the given EvpnRoute. Note that the route
// only needs to be exported to the IPeer from which it was learnt.
//
UpdateInfo *EvpnManager::GetUpdateInfo(EvpnRoute *route) {
CHECK_CONCURRENCY("db::DBTable");
DBState *dbstate = route->GetState(table_, listener_id_);
EvpnLocalMcastNode *local_node =
dynamic_cast<EvpnLocalMcastNode *>(dbstate);
if (!local_node)
return NULL;
return local_node->GetUpdateInfo();
}
BgpServer *EvpnManager::server() {
return table_->server();
}
//
// DBListener callback handler for the EvpnTable.
//
void EvpnManager::RouteListener(DBTablePartBase *tpart, DBEntryBase *db_entry) {
CHECK_CONCURRENCY("db::DBTable");
EvpnManagerPartition *partition = partitions_[tpart->index()];
EvpnRoute *route = dynamic_cast<EvpnRoute *>(db_entry);
assert(route);
DBState *dbstate = route->GetState(table_, listener_id_);
if (!dbstate) {
// We have no previous DBState for this route.
// Bail if the route is not valid.
if (!route->IsValid())
return;
// Create a new EvpnMcastNode and associate it with the route.
EvpnMcastNode *node;
if (route->GetPrefix().type() == EvpnPrefix::MacAdvertisementRoute) {
node = new EvpnLocalMcastNode(partition, route);
} else {
node = new EvpnRemoteMcastNode(partition, route);
}
partition->AddMcastNode(node);
route->SetState(table_, listener_id_, node);
} else {
EvpnMcastNode *node = dynamic_cast<EvpnMcastNode *>(dbstate);
assert(node);
if (!route->IsValid()) {
// Delete the EvpnMcastNode associated with the route.
route->ClearState(table_, listener_id_);
partition->DeleteMcastNode(node);
delete node;
} else if (node->UpdateAttributes(route)) {
// Update the EvpnMcastNode associated with the route.
partition->UpdateMcastNode(node);
}
}
}
//
// Fill information for introspect command.
// Note that all IM routes are always in partition 0.
//
void EvpnManager::FillShowInfo(ShowEvpnTable *sevt) const {
CHECK_CONCURRENCY("db::DBTable");
vector<string> regular_nves;
vector<string> ar_replicators;
vector<ShowEvpnMcastLeaf> ar_leafs;
BOOST_FOREACH(const EvpnMcastNode *node,
partitions_[0]->remote_mcast_node_list()) {
if (node->assisted_replication_leaf()) {
ShowEvpnMcastLeaf leaf;
leaf.set_address(node->address().to_string());
leaf.set_replicator(node->replicator_address().to_string());
ar_leafs.push_back(leaf);
} else if (node->assisted_replication_supported()) {
ar_replicators.push_back(node->address().to_string());
} else if (node->edge_replication_not_supported()) {
regular_nves.push_back(node->address().to_string());
}
}
sort(regular_nves.begin(), regular_nves.end());
sort(ar_replicators.begin(), ar_replicators.end());
sort(ar_leafs.begin(), ar_leafs.end());
sevt->set_regular_nves(regular_nves);
sevt->set_ar_replicators(ar_replicators);
sevt->set_ar_leafs(ar_leafs);
}
//
// Check if the EvpnManager can be deleted. This can happen only if all the
// EvpnManagerPartitions are empty.
//
bool EvpnManager::MayDelete() const {
CHECK_CONCURRENCY("bgp::Config");
BOOST_FOREACH(const EvpnManagerPartition *parition, partitions_) {
if (!parition->empty())
return false;
}
return true;
}
//
// Initiate shutdown for the EvpnManager.
//
void EvpnManager::Shutdown() {
CHECK_CONCURRENCY("bgp::Config");
}
//
// Trigger deletion of the EvpnManager and propagate the delete to any
// dependents.
//
void EvpnManager::ManagedDelete() {
deleter_->Delete();
}
//
// Attempt to enqueue a delete for the EvpnManager.
//
void EvpnManager::RetryDelete() {
if (!deleter()->IsDeleted())
return;
deleter()->RetryDelete();
}
//
// Return the LifetimeActor for the EvpnManager.
//
LifetimeActor *EvpnManager::deleter() {
return deleter_.get();
}