/
routing_instance.cc
1634 lines (1409 loc) · 54.8 KB
/
routing_instance.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/routing-instance/routing_instance.h"
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/functional/hash.hpp>
#include "base/set_util.h"
#include "base/task_annotations.h"
#include "base/task_trigger.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_server.h"
#include "bgp/routing-instance/iroute_aggregator.h"
#include "bgp/routing-instance/iservice_chain_mgr.h"
#include "bgp/routing-instance/istatic_route_mgr.h"
#include "bgp/routing-instance/peer_manager.h"
#include "bgp/routing-instance/routepath_replicator.h"
#include "bgp/routing-instance/routing_instance_log.h"
#include "bgp/routing-instance/routing_table_types.h"
#include "bgp/routing-policy/routing_policy.h"
#include "bgp/routing-instance/rtarget_group_mgr.h"
#include "bgp/rtarget/rtarget_route.h"
using boost::assign::list_of;
using boost::system::error_code;
using boost::tie;
using std::make_pair;
using std::set;
using std::string;
using std::vector;
SandeshTraceBufferPtr RoutingInstanceTraceBuf(
SandeshTraceBufferCreate(RTINSTANCE_TRACE_BUF, 1000));
class RoutingInstanceMgr::DeleteActor : public LifetimeActor {
public:
explicit DeleteActor(RoutingInstanceMgr *manager)
: LifetimeActor(manager->server_->lifetime_manager()),
manager_(manager) {
}
virtual bool MayDelete() const {
return manager_->MayDelete();
}
virtual void Shutdown() {
manager_->Shutdown();
}
virtual void Destroy() {
manager_->server_delete_ref_.Reset(NULL);
}
private:
RoutingInstanceMgr *manager_;
};
RoutingInstanceMgr::RoutingInstanceMgr(BgpServer *server) :
server_(server),
instance_config_lists_(
TaskScheduler::GetInstance()->HardwareThreadCount()),
default_rtinstance_(NULL),
deleted_count_(0),
asn_listener_id_(server->RegisterASNUpdateCallback(
boost::bind(&RoutingInstanceMgr::ASNUpdateCallback, this, _1, _2))),
identifier_listener_id_(server->RegisterIdentifierUpdateCallback(
boost::bind(&RoutingInstanceMgr::IdentifierUpdateCallback,
this, _1))),
deleter_(new DeleteActor(this)),
server_delete_ref_(this, server->deleter()) {
int task_id = TaskScheduler::GetInstance()->GetTaskId("bgp::ConfigHelper");
int hw_thread_count = TaskScheduler::GetInstance()->HardwareThreadCount();
for (int idx = 0; idx < hw_thread_count; ++idx) {
instance_config_triggers_.push_back(new TaskTrigger(boost::bind(
&RoutingInstanceMgr::ProcessInstanceConfigList, this, idx),
task_id, idx));
}
neighbor_config_trigger_.reset(new TaskTrigger(
boost::bind(&RoutingInstanceMgr::ProcessNeighborConfigList, this),
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0));
}
RoutingInstanceMgr::~RoutingInstanceMgr() {
assert(virtual_networks_.empty());
assert(deleted_count_ == 0);
server_->UnregisterASNUpdateCallback(asn_listener_id_);
server_->UnregisterIdentifierUpdateCallback(identifier_listener_id_);
STLDeleteValues(&instance_config_triggers_);
}
void RoutingInstanceMgr::ManagedDelete() {
deleter_->Delete();
}
void RoutingInstanceMgr::Shutdown() {
for (RoutingInstanceIterator it = begin(); it != end(); ++it) {
InstanceTargetRemove(it.operator->());
InstanceVnIndexRemove(it.operator->());
}
}
bool RoutingInstanceMgr::MayDelete() const {
if (!neighbor_config_list_.empty())
return false;
for (size_t idx = 0; idx < instance_config_lists_.size(); ++idx) {
if (!instance_config_lists_[idx].empty())
return false;
}
return true;
}
LifetimeActor *RoutingInstanceMgr::deleter() {
return deleter_.get();
}
bool RoutingInstanceMgr::deleted() {
return deleter()->IsDeleted();
}
//
// Go through all export targets for the RoutingInstance and add an entry for
// each one to the InstanceTargetMap.
//
// Ignore export targets that are not in the import list. This is done since
// export only targets are managed by user and the same export only target can
// be configured on multiple virtual networks.
//
void RoutingInstanceMgr::InstanceTargetAdd(RoutingInstance *rti) {
tbb::mutex::scoped_lock lock(mutex_);
for (RoutingInstance::RouteTargetList::const_iterator it =
rti->GetExportList().begin(); it != rti->GetExportList().end(); ++it) {
if (rti->GetImportList().find(*it) == rti->GetImportList().end())
continue;
target_map_.insert(make_pair(*it, rti));
}
}
//
// Go through all export targets for the RoutingInstance and remove the entry
// for each one from the InstanceTargetMap. Note that there may be multiple
// entries in the InstanceTargetMap for a given export target. Hence we need
// to make sure that we only remove the entry that matches the RoutingInstance.
//
// Ignore export targets that are not in the import list. They shouldn't be in
// in the map because of the same check in InstanceTargetAdd.
//
void RoutingInstanceMgr::InstanceTargetRemove(const RoutingInstance *rti) {
tbb::mutex::scoped_lock lock(mutex_);
for (RoutingInstance::RouteTargetList::const_iterator it =
rti->GetExportList().begin(); it != rti->GetExportList().end(); ++it) {
if (rti->GetImportList().find(*it) == rti->GetImportList().end())
continue;
for (InstanceTargetMap::iterator loc = target_map_.find(*it);
loc != target_map_.end() && loc->first == *it; ++loc) {
if (loc->second == rti) {
target_map_.erase(loc);
break;
}
}
}
}
//
// Lookup the RoutingInstance for the given RouteTarget.
//
const RoutingInstance *RoutingInstanceMgr::GetInstanceByTarget(
const RouteTarget &rtarget) const {
InstanceTargetMap::const_iterator loc = target_map_.find(rtarget);
if (loc == target_map_.end()) {
return NULL;
}
return loc->second;
}
//
// Add an entry for the vn index to the VnIndexMap.
//
void RoutingInstanceMgr::InstanceVnIndexAdd(RoutingInstance *rti) {
tbb::mutex::scoped_lock lock(mutex_);
if (rti->virtual_network_index())
vn_index_map_.insert(make_pair(rti->virtual_network_index(), rti));
}
//
// Remove the entry for the vn index from the VnIndexMap. Note that there may
// be multiple entries in the VnIndexMap for a given vn index target. Hence we
// need to make sure that we remove the entry that matches the RoutingInstance.
//
void RoutingInstanceMgr::InstanceVnIndexRemove(const RoutingInstance *rti) {
tbb::mutex::scoped_lock lock(mutex_);
if (!rti->virtual_network_index())
return;
int vn_index = rti->virtual_network_index();
for (VnIndexMap::iterator loc = vn_index_map_.find(vn_index);
loc != vn_index_map_.end() && loc->first == vn_index; ++loc) {
if (loc->second == rti) {
vn_index_map_.erase(loc);
break;
}
}
}
//
// Lookup the RoutingInstance for the given vn index.
//
const RoutingInstance *RoutingInstanceMgr::GetInstanceByVnIndex(
int vn_index) const {
VnIndexMap::const_iterator loc = vn_index_map_.find(vn_index);
if (loc == vn_index_map_.end())
return NULL;
return loc->second;
}
//
// Lookup the VN name for the given vn index.
//
string RoutingInstanceMgr::GetVirtualNetworkByVnIndex(
int vn_index) const {
const RoutingInstance *rti = GetInstanceByVnIndex(vn_index);
return rti ? rti->GetVirtualNetworkName() : "unresolved";
}
//
// Lookup the vn index for the given RouteTarget.
//
// Return 0 if the RouteTarget does not map to a RoutingInstance.
// Return -1 if the RouteTarget maps to multiple RoutingInstances
// that belong to different VNs.
//
int RoutingInstanceMgr::GetVnIndexByRouteTarget(
const RouteTarget &rtarget) const {
int vn_index = 0;
for (InstanceTargetMap::const_iterator loc = target_map_.find(rtarget);
loc != target_map_.end() && loc->first == rtarget; ++loc) {
int ri_vn_index = loc->second->virtual_network_index();
if (vn_index && ri_vn_index && ri_vn_index != vn_index)
return -1;
vn_index = ri_vn_index;
}
return vn_index;
}
//
// Derive the vn index from the route targets in the ExtCommunity.
//
// If the result is ambiguous i.e. we have a RouteTarget that maps
// to multiple vn indexes or we have multiple RouteTargets that map
// to different vn indexes, return 0.
//
int RoutingInstanceMgr::GetVnIndexByExtCommunity(
const ExtCommunity *ext_community) const {
int vn_index = 0;
BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
ext_community->communities()) {
if (!ExtCommunity::is_route_target(comm))
continue;
RouteTarget rtarget(comm);
int rtgt_vn_index = GetVnIndexByRouteTarget(rtarget);
if (rtgt_vn_index < 0 ||
(vn_index && rtgt_vn_index && rtgt_vn_index != vn_index)) {
vn_index = 0;
break;
} else if (rtgt_vn_index) {
vn_index = rtgt_vn_index;
}
}
return vn_index;
}
int
RoutingInstanceMgr::RegisterInstanceOpCallback(RoutingInstanceCb callback) {
tbb::mutex::scoped_lock lock(mutex_);
size_t i = bmap_.find_first();
if (i == bmap_.npos) {
i = callbacks_.size();
callbacks_.push_back(callback);
} else {
bmap_.reset(i);
if (bmap_.none()) {
bmap_.clear();
}
callbacks_[i] = callback;
}
return i;
}
void RoutingInstanceMgr::UnregisterInstanceOpCallback(int listener) {
tbb::mutex::scoped_lock lock(mutex_);
callbacks_[listener] = NULL;
if ((size_t) listener == callbacks_.size() - 1) {
while (!callbacks_.empty() && callbacks_.back() == NULL) {
callbacks_.pop_back();
}
if (bmap_.size() > callbacks_.size()) {
bmap_.resize(callbacks_.size());
}
} else {
if ((size_t) listener >= bmap_.size()) {
bmap_.resize(listener + 1);
}
bmap_.set(listener);
}
}
void RoutingInstanceMgr::NotifyInstanceOp(string name, Operation op) {
tbb::mutex::scoped_lock lock(mutex_);
for (InstanceOpListenersList::iterator iter = callbacks_.begin();
iter != callbacks_.end(); ++iter) {
if (*iter != NULL) {
RoutingInstanceCb cb = *iter;
(cb)(name, op);
}
}
}
bool RoutingInstanceMgr::ProcessInstanceConfigList(int idx) {
if (deleted()) {
deleter()->RetryDelete();
} else {
BOOST_FOREACH(const string &name, instance_config_lists_[idx]) {
const BgpInstanceConfig *config =
server()->config_manager()->FindInstance(name);
if (!config)
continue;
LocateRoutingInstance(config);
}
}
instance_config_lists_[idx].clear();
return true;
}
RoutingInstance *RoutingInstanceMgr::GetDefaultRoutingInstance() {
return default_rtinstance_;
}
const RoutingInstance *RoutingInstanceMgr::GetDefaultRoutingInstance() const {
return default_rtinstance_;
}
RoutingInstance *RoutingInstanceMgr::GetRoutingInstance(const string &name) {
return instances_.Find(name);
}
const RoutingInstance *RoutingInstanceMgr::GetRoutingInstance(
const string &name) const {
return instances_.Find(name);
}
RoutingInstance *RoutingInstanceMgr::GetRoutingInstanceLocked(
const string &name) {
tbb::mutex::scoped_lock lock(mutex_);
return instances_.Find(name);
}
void RoutingInstanceMgr::InsertRoutingInstance(RoutingInstance *rtinstance) {
tbb::mutex::scoped_lock lock(mutex_);
int index = instances_.Insert(rtinstance->config()->name(), rtinstance);
rtinstance->set_index(index);
}
void RoutingInstanceMgr::LocateRoutingInstance(
const BgpInstanceConfig *config) {
RoutingInstance *rtinstance = GetRoutingInstanceLocked(config->name());
if (rtinstance) {
if (rtinstance->deleted()) {
RTINSTANCE_LOG_WARNING_MESSAGE(server_,
RTINSTANCE_LOG_FLAG_ALL, rtinstance->GetVirtualNetworkName(),
config->name(),
"Instance recreated before pending deletion is complete");
} else {
UpdateRoutingInstance(rtinstance, config);
}
} else {
rtinstance = CreateRoutingInstance(config);
}
}
void RoutingInstanceMgr::LocateRoutingInstance(const string &name) {
static boost::hash<string> string_hash;
if (name == BgpConfigManager::kMasterInstance) {
const BgpInstanceConfig *config =
server()->config_manager()->FindInstance(name);
assert(config);
LocateRoutingInstance(config);
} else {
size_t idx = string_hash(name) % instance_config_lists_.size();
instance_config_lists_[idx].insert(name);
instance_config_triggers_[idx]->Set();
}
}
// Update VirtualNetwork to RoutingInstance name mapping.
bool RoutingInstanceMgr::CreateVirtualNetworkMapping(
const string &virtual_network, const string &instance_name) {
tbb::mutex::scoped_lock lock(mutex_);
VirtualNetworksMap::iterator iter;
bool inserted;
tie(iter, inserted) = virtual_networks_.insert(make_pair(virtual_network,
set<string>()));
assert(iter->second.insert(instance_name).second);
return true;
}
bool RoutingInstanceMgr::DeleteVirtualNetworkMapping(
const string &virtual_network, const string &instance_name) {
tbb::mutex::scoped_lock lock(mutex_);
VirtualNetworksMap::iterator iter = virtual_networks_.find(virtual_network);
assert(iter != virtual_networks_.end());
// Delete this instance from the set of virtual-network to instances map.
assert(iter->second.erase(instance_name) == 1);
RoutingInstanceStatsData instance_info;
bool mapping_deleted = iter->second.empty();
// Delete the mapping if the member set is empty.
if (mapping_deleted) {
virtual_networks_.erase(iter);
// Delete the virtual-network specific UVE.
instance_info.set_deleted(true);
} else {
// Delete this instance's uve, by deleting all table stats.
map<string, RoutingTableStats> stats_map;
stats_map.insert(make_pair(instance_name, RoutingTableStats()));
stats_map[instance_name].set_deleted(true);
// Delete all uve stats across all address families.
for (Address::Family i = Address::UNSPEC; i < Address::NUM_FAMILIES;
i = static_cast<Address::Family>(i + 1)) {
SetTableStatsUve(i, stats_map, &instance_info);
}
}
// Send delete uve.
RoutingInstanceStats::Send(instance_info);
return mapping_deleted;
}
void RoutingInstanceMgr::SetTableStatsUve(Address::Family family,
const map<string, RoutingTableStats> &stats_map,
RoutingInstanceStatsData *instance_info) const {
// By switching over all families, we get compiler to tell us in future,
// when ever familiy enum list gets modified.
switch (family) {
case Address::UNSPEC:
break;
case Address::INET:
instance_info->set_raw_ipv4_stats(stats_map);
break;
case Address::INET6:
instance_info->set_raw_ipv6_stats(stats_map);
break;
case Address::INETVPN:
instance_info->set_raw_inetvpn_stats(stats_map);
break;
case Address::INET6VPN:
instance_info->set_raw_inet6vpn_stats(stats_map);
break;
case Address::RTARGET:
instance_info->set_raw_rtarget_stats(stats_map);
break;
case Address::EVPN:
instance_info->set_raw_evpn_stats(stats_map);
break;
case Address::ERMVPN:
instance_info->set_raw_ermvpn_stats(stats_map);
break;
case Address::NUM_FAMILIES:
break;
}
}
uint32_t RoutingInstanceMgr::SendTableStatsUve() {
uint32_t out_q_depth = 0;
for (RoutingInstanceIterator rit = begin(); rit != end(); ++rit) {
RoutingInstanceStatsData instance_info;
map<string, RoutingTableStats> stats_map;
stats_map.insert(make_pair(rit->name(), RoutingTableStats()));
RoutingInstance::RouteTableList const rt_list = rit->GetTables();
// Prepare and send Statistics UVE for each routing-instance.
for (RoutingInstance::RouteTableList::const_iterator it =
rt_list.begin(); it != rt_list.end(); ++it) {
BgpTable *table = it->second;
size_t markers;
out_q_depth += table->GetPendingRiboutsCount(&markers);
stats_map[rit->name()].set_prefixes(table->Size());
stats_map[rit->name()].set_primary_paths(
table->GetPrimaryPathCount());
stats_map[rit->name()].set_secondary_paths(
table->GetSecondaryPathCount());
stats_map[rit->name()].set_infeasible_paths(
table->GetInfeasiblePathCount());
uint64_t total_paths = table->GetPrimaryPathCount() +
table->GetSecondaryPathCount() +
table->GetInfeasiblePathCount();
stats_map[rit->name()].set_total_paths(total_paths);
SetTableStatsUve(table->family(), stats_map, &instance_info);
}
// Set the primary key and trigger uve send.
instance_info.set_name(rit->GetVirtualNetworkName());
RoutingInstanceStats::Send(instance_info);
}
return out_q_depth;
}
RoutingInstance *RoutingInstanceMgr::CreateRoutingInstance(
const BgpInstanceConfig *config) {
RoutingInstance *rtinstance = BgpObjectFactory::Create<RoutingInstance>(
config->name(), server_, this, config);
if (config->name() == BgpConfigManager::kMasterInstance)
default_rtinstance_ = rtinstance;
rtinstance->ProcessConfig();
InsertRoutingInstance(rtinstance);
InstanceTargetAdd(rtinstance);
InstanceVnIndexAdd(rtinstance);
rtinstance->InitAllRTargetRoutes(server_->local_autonomous_system());
// Notify clients about routing instance create
NotifyInstanceOp(config->name(), INSTANCE_ADD);
vector<string> import_rt(config->import_list().begin(),
config->import_list().end());
vector<string> export_rt(config->export_list().begin(),
config->export_list().end());
RTINSTANCE_LOG(Create, rtinstance,
SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL,
import_rt, export_rt, rtinstance->virtual_network_index());
// Schedule creation of neighbors for this instance.
CreateRoutingInstanceNeighbors(config);
// Update instance to virtual-network mapping structures.
CreateVirtualNetworkMapping(rtinstance->GetVirtualNetworkName(),
rtinstance->name());
return rtinstance;
}
void RoutingInstanceMgr::UpdateRoutingInstance(RoutingInstance *rtinstance,
const BgpInstanceConfig *config) {
bool old_always_subscribe = rtinstance->always_subscribe();
// Note that InstanceTarget[Remove|Add] depend on the RouteTargetList in
// the RoutingInstance.
InstanceTargetRemove(rtinstance);
InstanceVnIndexRemove(rtinstance);
rtinstance->UpdateConfig(config);
InstanceTargetAdd(rtinstance);
InstanceVnIndexAdd(rtinstance);
if (old_always_subscribe != rtinstance->always_subscribe()) {
rtinstance->FlushAllRTargetRoutes(server_->local_autonomous_system());
rtinstance->InitAllRTargetRoutes(server_->local_autonomous_system());
}
// Notify clients about routing instance create
NotifyInstanceOp(config->name(), INSTANCE_UPDATE);
vector<string> import_rt(config->import_list().begin(),
config->import_list().end());
vector<string> export_rt(config->export_list().begin(),
config->export_list().end());
RTINSTANCE_LOG(Update, rtinstance,
SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL, import_rt, export_rt,
rtinstance->virtual_network_index());
}
//
// Concurrency: BGP Config task
//
// Trigger deletion of a particular routing-instance
//
// This involves several asynchronous steps such as deleting all tables in
// routing-instance (flush all notifications, registrations and user data).
//
void RoutingInstanceMgr::DeleteRoutingInstance(const string &name) {
CHECK_CONCURRENCY("bgp::Config");
RoutingInstance *rtinstance = GetRoutingInstance(name);
// Ignore if instance is not found as it might already have been deleted.
if (rtinstance && rtinstance->deleted()) {
RTINSTANCE_LOG_WARNING_MESSAGE(server_, RTINSTANCE_LOG_FLAG_ALL,
rtinstance->GetVirtualNetworkName(), name,
"Duplicate instance delete while pending deletion");
return;
} else if (!rtinstance) {
RTINSTANCE_LOG_WARNING_MESSAGE(server_, RTINSTANCE_LOG_FLAG_ALL,
name, name, "Instance not found during delete");
return;
}
InstanceVnIndexRemove(rtinstance);
InstanceTargetRemove(rtinstance);
rtinstance->ClearConfig();
RTINSTANCE_LOG(Delete, rtinstance,
SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL);
rtinstance->ProcessServiceChainConfig();
rtinstance->FlushStaticRouteConfig();
rtinstance->FlushRouteAggregationConfig();
NotifyInstanceOp(name, INSTANCE_DELETE);
rtinstance->ManagedDelete();
}
//
// Concurrency: Called from BGP config task manager
//
// Destroy a routing instance from the data structures
//
void RoutingInstanceMgr::DestroyRoutingInstance(RoutingInstance *rtinstance) {
CHECK_CONCURRENCY("bgp::Config");
RTINSTANCE_LOG(Destroy, rtinstance,
SandeshLevel::SYS_DEBUG, RTINSTANCE_LOG_FLAG_ALL);
DeleteVirtualNetworkMapping(rtinstance->GetVirtualNetworkName(),
rtinstance->name());
// Remove call here also deletes the instance.
const string name = rtinstance->name();
instances_.Remove(rtinstance->name(), rtinstance->index());
if (deleted())
return;
if (rtinstance == default_rtinstance_) {
default_rtinstance_ = NULL;
return;
}
const BgpInstanceConfig *config
= server()->config_manager()->FindInstance(name);
if (config) {
CreateRoutingInstance(config);
return;
}
}
void RoutingInstanceMgr::CreateRoutingInstanceNeighbors(
const BgpInstanceConfig *config) {
if (config->neighbor_list().empty())
return;
tbb::mutex::scoped_lock lock(mutex_);
neighbor_config_list_.insert(config->name());
neighbor_config_trigger_->Set();
}
bool RoutingInstanceMgr::ProcessNeighborConfigList() {
if (deleted()) {
deleter()->RetryDelete();
} else {
BOOST_FOREACH(const string &name, neighbor_config_list_) {
RoutingInstance *rtinstance = GetRoutingInstance(name);
if (!rtinstance || rtinstance->deleted())
continue;
rtinstance->CreateNeighbors();
}
}
neighbor_config_list_.clear();
return true;
}
void RoutingInstanceMgr::ASNUpdateCallback(as_t old_asn, as_t old_local_asn) {
if (server_->local_autonomous_system() == old_local_asn)
return;
for (RoutingInstanceIterator it = begin(); it != end(); ++it) {
it->FlushAllRTargetRoutes(old_local_asn);
it->InitAllRTargetRoutes(server_->local_autonomous_system());
}
}
void RoutingInstanceMgr::IdentifierUpdateCallback(Ip4Address old_identifier) {
for (RoutingInstanceIterator it = begin(); it != end(); ++it) {
it->FlushAllRTargetRoutes(server_->local_autonomous_system());
it->InitAllRTargetRoutes(server_->local_autonomous_system());
}
}
//
// Disable processing of all instance config lists.
// For testing only.
//
void RoutingInstanceMgr::DisableInstanceConfigListProcessing() {
for (size_t idx = 0; idx < instance_config_triggers_.size(); ++idx) {
instance_config_triggers_[idx]->set_disable();
}
}
//
// Enable processing of all instance config lists.
// For testing only.
//
void RoutingInstanceMgr::EnableInstanceConfigListProcessing() {
for (size_t idx = 0; idx < instance_config_triggers_.size(); ++idx) {
instance_config_triggers_[idx]->set_enable();
}
}
//
// Disable processing of neighbor config list.
// For testing only.
//
void RoutingInstanceMgr::DisableNeighborConfigListProcessing() {
neighbor_config_trigger_->set_disable();
}
//
// Enable processing of neighbor config list.
// For testing only.
//
void RoutingInstanceMgr::EnableNeighborConfigListProcessing() {
neighbor_config_trigger_->set_enable();
}
class RoutingInstance::DeleteActor : public LifetimeActor {
public:
DeleteActor(BgpServer *server, RoutingInstance *parent)
: LifetimeActor(server ? server->lifetime_manager() : NULL),
parent_(parent) {
}
virtual bool MayDelete() const {
return parent_->MayDelete();
}
virtual void Shutdown() {
parent_->mgr_->increment_deleted_count();
parent_->mgr_->NotifyInstanceOp(parent_->name(),
RoutingInstanceMgr::INSTANCE_DELETE);
parent_->Shutdown();
}
virtual void Destroy() {
parent_->mgr_->decrement_deleted_count();
parent_->mgr_->DestroyRoutingInstance(parent_);
}
private:
RoutingInstance *parent_;
};
RoutingInstance::RoutingInstance(string name, BgpServer *server,
RoutingInstanceMgr *mgr,
const BgpInstanceConfig *config)
: name_(name), index_(-1), server_(server), mgr_(mgr), config_(config),
is_master_(false), always_subscribe_(false), virtual_network_index_(0),
virtual_network_allow_transit_(false),
vxlan_id_(0),
deleter_(new DeleteActor(server, this)),
manager_delete_ref_(this, NULL) {
if (mgr) {
tbb::mutex::scoped_lock lock(mgr->mutex());
manager_delete_ref_.Reset(mgr->deleter());
}
}
RoutingInstance::~RoutingInstance() {
}
void RoutingInstance::CreateNeighbors() {
CHECK_CONCURRENCY("bgp::Config");
if (config_->neighbor_list().empty())
return;
PeerManager *peer_manager = LocatePeerManager();
BOOST_FOREACH(const string &name, config_->neighbor_list()) {
if (peer_manager->PeerLookup(name))
continue;
peer_manager->PeerResurrect(name);
}
}
void RoutingInstance::ProcessRoutingPolicyConfig() {
RoutingPolicyMgr *policy_mgr = server()->routing_policy_mgr();
BOOST_FOREACH(RoutingPolicyAttachInfo info,
config_->routing_policy_list()) {
RoutingPolicy *policy =
policy_mgr->GetRoutingPolicy(info.routing_policy_);
if (policy) {
routing_policies_.push_back(make_pair(policy,
policy->generation()));
}
}
}
//
// Update the routing policy on the instance
// 1. New policy is added
// 2. Policy order changed
// 3. Policy is removed
// 4. Existing policy gets updated(terms in the policy got modified)
// In any of the above cases, we call routing policy manager to re-evaluate the
// routes in the BgpTables of this routing instance with new set of policies
//
void RoutingInstance::UpdateRoutingPolicyConfig() {
CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
RoutingPolicyMgr *policy_mgr = server()->routing_policy_mgr();
if (policy_mgr->UpdateRoutingPolicyList(config_->routing_policy_list(),
routing_policies())) {
// Let RoutingPolicyMgr handle update of routing policy on the instance
policy_mgr->ApplyRoutingPolicy(this);
}
}
void RoutingInstance::ProcessServiceChainConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
const ServiceChainConfig *sc_config =
config_ ? config_->service_chain_info(family) : NULL;
IServiceChainMgr *manager = server_->service_chain_mgr(family);
if (sc_config && !sc_config->routing_instance.empty()) {
manager->LocateServiceChain(this, *sc_config);
} else {
manager->StopServiceChain(this);
}
}
}
void RoutingInstance::ProcessStaticRouteConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IStaticRouteMgr *manager = static_route_mgr(family);
if (!manager && !config_->static_routes(family).empty())
manager = LocateStaticRouteMgr(family);
if (manager)
manager->ProcessStaticRouteConfig();
}
}
void RoutingInstance::UpdateStaticRouteConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IStaticRouteMgr *manager = static_route_mgr(family);
if (!manager && !config_->static_routes(family).empty())
manager = LocateStaticRouteMgr(family);
if (manager)
manager->UpdateStaticRouteConfig();
}
}
void RoutingInstance::FlushStaticRouteConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IStaticRouteMgr *manager = static_route_mgr(family);
if (manager)
manager->FlushStaticRouteConfig();
}
}
void RoutingInstance::UpdateAllStaticRoutes() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IStaticRouteMgr *manager = static_route_mgr(family);
if (manager)
manager->UpdateAllRoutes();
}
}
void RoutingInstance::ProcessRouteAggregationConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IRouteAggregator *aggregator = route_aggregator(family);
if (!aggregator && !config_->aggregate_routes(family).empty())
aggregator = LocateRouteAggregator(family);
if (aggregator)
aggregator->ProcessAggregateRouteConfig();
}
}
void RoutingInstance::UpdateRouteAggregationConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IRouteAggregator *aggregator = route_aggregator(family);
if (!aggregator && !config_->aggregate_routes(family).empty())
aggregator = LocateRouteAggregator(family);
if (aggregator)
aggregator->UpdateAggregateRouteConfig();
}
}
void RoutingInstance::FlushRouteAggregationConfig() {
if (is_master_)
return;
vector<Address::Family> families = list_of(Address::INET)(Address::INET6);
BOOST_FOREACH(Address::Family family, families) {
IRouteAggregator *aggregator = route_aggregator(family);
if (aggregator)
aggregator->FlushAggregateRouteConfig();
}
}
void RoutingInstance::ProcessConfig() {
RoutingInstanceInfo info = GetDataCollection("");
// Initialize virtual network info.
virtual_network_ = config_->virtual_network();
virtual_network_index_ = config_->virtual_network_index();
virtual_network_allow_transit_ = config_->virtual_network_allow_transit();
vxlan_id_ = config_->vxlan_id();
// Always subscribe (using RTF) for RTs of PNF service chain instances.
always_subscribe_ = config_->has_pnf();
vector<string> import_rt, export_rt;
BOOST_FOREACH(string irt, config_->import_list()) {
import_.insert(RouteTarget::FromString(irt));
import_rt.push_back(irt);
}
BOOST_FOREACH(string ert, config_->export_list()) {
export_.insert(RouteTarget::FromString(ert));
export_rt.push_back(ert);
}
if (import_rt.size())
info.set_add_import_rt(import_rt);
if (export_rt.size())
info.set_add_export_rt(export_rt);
if (import_rt.size() || export_rt.size())
ROUTING_INSTANCE_COLLECTOR_INFO(info);
// Create BGP Table
if (name_ == BgpConfigManager::kMasterInstance) {
assert(mgr_->count() == 0);
is_master_ = true;
LocatePeerManager();
VpnTableCreate(Address::INETVPN);
VpnTableCreate(Address::INET6VPN);
VpnTableCreate(Address::ERMVPN);
VpnTableCreate(Address::EVPN);
RTargetTableCreate();
BgpTable *table_inet = static_cast<BgpTable *>(
server_->database()->CreateTable("inet.0"));
assert(table_inet);
AddTable(table_inet);
BgpTable *table_inet6 = static_cast<BgpTable *>(
server_->database()->CreateTable("inet6.0"));
assert(table_inet6);
AddTable(table_inet6);
} else {
// Create foo.family.0.
VrfTableCreate(Address::INET, Address::INETVPN);
VrfTableCreate(Address::INET6, Address::INET6VPN);
VrfTableCreate(Address::ERMVPN, Address::ERMVPN);
VrfTableCreate(Address::EVPN, Address::EVPN);
}
ProcessServiceChainConfig();
ProcessStaticRouteConfig();
ProcessRouteAggregationConfig();
ProcessRoutingPolicyConfig();
}