-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_membership.cc
1304 lines (1172 loc) · 39.2 KB
/
bgp_membership.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) 2016 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_membership.h"
#include <boost/foreach.hpp>
#include "base/task_annotations.h"
#include "base/task_trigger.h"
#include "bgp/bgp_export.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_peer_close.h"
#include "bgp/bgp_peer_types.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/scheduling_group.h"
#include "bgp/routing-instance/routing_instance.h"
using std::list;
using std::make_pair;
using std::string;
using std::vector;
//
// Constructor for BgpMembershipManager.
//
BgpMembershipManager::BgpMembershipManager(BgpServer *server)
: server_(server),
walker_(new Walker(this)),
event_queue_(new WorkQueue<Event *>(
TaskScheduler::GetInstance()->GetTaskId("bgp::PeerMembership"), 0,
boost::bind(&BgpMembershipManager::EventCallback, this, _1))) {
current_jobs_count_ = 0;
total_jobs_count_ = 0;
}
//
// Destructor for BgpMembershipManager.
//
BgpMembershipManager::~BgpMembershipManager() {
assert(current_jobs_count_ == 0);
assert(rib_state_map_.empty());
assert(peer_state_map_.empty());
}
int BgpMembershipManager::RegisterPeerRegistrationCallback(
PeerRegistrationCallback callback) {
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
size_t id = registration_bmap_.find_first();
if (id == registration_bmap_.npos) {
id = registration_callbacks_.size();
registration_callbacks_.push_back(callback);
} else {
registration_bmap_.reset(id);
if (registration_bmap_.none()) {
registration_bmap_.clear();
}
registration_callbacks_[id] = callback;
}
return id;
}
void BgpMembershipManager::UnregisterPeerRegistrationCallback(int id) {
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
registration_callbacks_[id] = NULL;
if ((size_t) id == registration_callbacks_.size() - 1) {
while (!registration_callbacks_.empty() &&
registration_callbacks_.back() == NULL) {
registration_callbacks_.pop_back();
}
if (registration_bmap_.size() > registration_callbacks_.size()) {
registration_bmap_.resize(registration_callbacks_.size());
}
} else {
if ((size_t) id >= registration_bmap_.size()) {
registration_bmap_.resize(id + 1);
}
registration_bmap_.set(id);
}
}
void BgpMembershipManager::NotifyPeerRegistration(IPeer *peer, BgpTable *table,
bool unregister) {
CHECK_CONCURRENCY("bgp::PeerMembership");
if (!peer->IsXmppPeer())
return;
for (PeerRegistrationListenerList::iterator iter =
registration_callbacks_.begin();
iter != registration_callbacks_.end(); ++iter) {
if (*iter != NULL) {
PeerRegistrationCallback callback = *iter;
(callback)(peer, table, unregister);
}
}
}
//
// Register the IPeer to the BgpTable.
// Post a REGISTER_RIB event to deal with concurrency issues with RibOut.
//
void BgpMembershipManager::Register(IPeer *peer, BgpTable *table,
const RibExportPolicy &policy, int instance_id) {
CHECK_CONCURRENCY("bgp::Config", "bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
current_jobs_count_++;
total_jobs_count_++;
PeerRibState *prs = LocatePeerRibState(peer, table);
assert(prs->action() == NONE);
assert(!prs->ribout_registered());
prs->set_ribin_registered(true);
prs->set_action(RIBOUT_ADD);
Event *event = new Event(REGISTER_RIB, peer, table, policy, instance_id);
EnqueueEvent(event);
}
//
// Synchronously register the IPeer to the BgpTable for RIBIN.
//
void BgpMembershipManager::RegisterRibIn(IPeer *peer, BgpTable *table) {
CHECK_CONCURRENCY("bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
PeerRibState *prs = LocatePeerRibState(peer, table);
assert(prs->action() == NONE);
assert(!prs->ribin_registered() ||
peer->peer_close()->close_manager()->IsInGracefulRestartTimerWait());
assert(!prs->ribout_registered());
prs->set_ribin_registered(true);
}
//
// Unregister the IPeer from the BgpTable.
// Post an UNREGISTER_RIB event to deal with concurrency issues with RibOut.
//
void BgpMembershipManager::Unregister(IPeer *peer, BgpTable *table) {
CHECK_CONCURRENCY("bgp::Config", "bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
current_jobs_count_++;
total_jobs_count_++;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == NONE);
assert(prs->ribin_registered());
if (!prs->ribout_registered()) {
UnregisterRibInUnlocked(prs);
return;
}
prs->set_action(RIBIN_DELETE_RIBOUT_DELETE);
prs->set_ribin_registered(false);
prs->set_instance_id(-1);
prs->set_subscription_gen_id(0);
Event *event = new Event(UNREGISTER_RIB, peer, table);
EnqueueEvent(event);
}
//
// Unregister the IPeer from the BgpTable for RIBIN.
//
void BgpMembershipManager::UnregisterRibIn(IPeer *peer, BgpTable *table) {
CHECK_CONCURRENCY("bgp::Config", "bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
current_jobs_count_++;
total_jobs_count_++;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == NONE);
assert(prs->ribin_registered() && !prs->ribout_registered());
UnregisterRibInUnlocked(prs);
}
//
// Common routine to handle unregister of IPeer from Table for RIBIN.
//
void BgpMembershipManager::UnregisterRibInUnlocked(PeerRibState *prs) {
prs->set_ribin_registered(false);
prs->set_instance_id(-1);
prs->set_subscription_gen_id(0);
prs->set_action(RIBIN_DELETE);
prs->UnregisterRibIn();
BGP_LOG_PEER_TABLE(prs->peer(), SandeshLevel::SYS_DEBUG,
BGP_LOG_FLAG_SYSLOG, prs->table(),
"Unregister table requested for action " << prs->action());
}
//
// Unregister the IPeer from the BgpTable.
// Post an UNREGISTER_RIB event to deal with concurrency issues with RibOut.
// The action is set to RIBIN_WALK_RIBOUT_DELETE.
// This API is to be used when handling graceful restart of the peer.
//
void BgpMembershipManager::UnregisterRibOut(IPeer *peer, BgpTable *table) {
CHECK_CONCURRENCY("bgp::Config", "bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
current_jobs_count_++;
total_jobs_count_++;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == NONE);
assert(prs->ribin_registered());
assert(prs->ribout_registered());
prs->set_instance_id(-1);
prs->set_subscription_gen_id(0);
prs->set_action(RIBIN_WALK_RIBOUT_DELETE);
Event *event = new Event(UNREGISTER_RIB, peer, table);
EnqueueEvent(event);
}
//
// Trigger a walk of IPeer's RIBIN for the BgpTable.
// This API can be used when sweeping paths as part of graceful restart.
// It can also be used in future when re-evaluating import policy for a peer.
//
void BgpMembershipManager::WalkRibIn(IPeer *peer, BgpTable *table) {
CHECK_CONCURRENCY("bgp::Config", "bgp::StateMachine", "xmpp::StateMachine");
tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
current_jobs_count_++;
total_jobs_count_++;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == NONE);
assert(prs->ribin_registered());
prs->set_action(RIBIN_WALK);
prs->WalkRibIn();
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Walk table requested for action " << prs->action());
}
//
// Fill in the registration info of the IPeer for the BgpTable.
// Return true if the IPeer is registered with the BgpTable, false otherwise.
//
bool BgpMembershipManager::GetRegistrationInfo(
const IPeer *peer, const BgpTable *table,
int *instance_id, uint64_t *subscription_gen_id) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
const PeerRibState *prs = FindPeerRibState(peer, table);
if (!prs)
return false;
if (instance_id)
*instance_id = prs->instance_id();
if (subscription_gen_id)
*subscription_gen_id = prs->subscription_gen_id();
return true;
}
//
// Update the registration info of the IPeer for the BgpTable.
//
void BgpMembershipManager::SetRegistrationInfo(
const IPeer *peer, const BgpTable *table,
int instance_id, uint64_t subscription_gen_id) {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
PeerRibState *prs = FindPeerRibState(peer, table);
if (!prs)
return;
prs->set_instance_id(instance_id);
prs->set_subscription_gen_id(subscription_gen_id);
}
//
// Return true if the IPeer is registered to the BgpTable.
//
bool BgpMembershipManager::IsRegistered(const IPeer *peer,
const BgpTable *table) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
const PeerRibState *prs = FindPeerRibState(peer, table);
return (prs && prs->ribin_registered() && prs->ribout_registered());
}
//
// Return true if the IPeer is registered to the BgpTable for RibIn.
//
bool BgpMembershipManager::IsRibInRegistered(const IPeer *peer,
const BgpTable *table) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
const PeerRibState *prs = FindPeerRibState(peer, table);
return (prs && prs->ribin_registered());
}
//
// Return true if the IPeer is registered to the BgpTable for RibOut.
//
bool BgpMembershipManager::IsRibOutRegistered(const IPeer *peer,
const BgpTable *table) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
const PeerRibState *prs = FindPeerRibState(peer, table);
return (prs && prs->ribout_registered());
}
//
// Return RibOut's output queue depth.
//
uint32_t BgpMembershipManager::GetRibOutQueueDepth(const IPeer *peer,
const BgpTable *table) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
const PeerRibState *prs = FindPeerRibState(peer, table);
if (!prs || !prs->ribout_registered())
return 0;
RibOut *ribout = prs->ribout();
assert(ribout);
uint32_t out_q_depth = 0;
if (ribout->updates()) {
BOOST_FOREACH(const UpdateQueue *queue,
ribout->updates()->queue_vec()) {
out_q_depth += queue->size();
}
}
return out_q_depth;
}
//
// Fill in the list of registered BgpTables for given IPeer.
//
void BgpMembershipManager::GetRegisteredRibs(const IPeer *peer,
list<BgpTable *> *table_list) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
table_list->clear();
const PeerState *ps = FindPeerState(peer);
if (ps)
ps->GetRegisteredRibs(table_list);
}
//
//
// Fill membership introspect information for a BgpTable.
//
void BgpMembershipManager::FillRoutingInstanceTableInfo(
ShowRoutingInstanceTable *srit, const BgpTable *table) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
BgpTable *nc_table = const_cast<BgpTable *>(table);
const RibState *rs = FindRibState(nc_table);
if (rs)
rs->FillRoutingInstanceTableInfo(srit);
}
//
// Fill membership introspect information for an IPeer.
//
void BgpMembershipManager::FillPeerMembershipInfo(const IPeer *peer,
BgpNeighborResp *resp) const {
tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
assert(resp->get_routing_tables().empty());
IPeer *nc_peer = const_cast<IPeer *>(peer);
const SchedulingGroupManager *sg_mgr = server_->scheduling_group_manager();
const SchedulingGroup *sg = sg_mgr->PeerGroup(nc_peer);
if (sg) {
resp->set_send_state(
sg->PeerInSync(nc_peer) ? "in sync" : "not in sync");
} else {
resp->set_send_state("not advertising");
}
const PeerState *ps = FindPeerState(nc_peer);
if (ps)
ps->FillPeerMembershipInfo(resp);
}
//
// Return true if no pending work in the BgpMembershipManager itself and
// in the Walker.
//
bool BgpMembershipManager::IsQueueEmpty() const {
return (event_queue_->IsQueueEmpty() && walker_->IsQueueEmpty());
}
//
// Return number of PeerRibStates.
//
size_t BgpMembershipManager::GetMembershipCount() const {
size_t count = 0;
for (PeerStateMap::const_iterator loc = peer_state_map_.begin();
loc != peer_state_map_.end(); ++loc) {
const PeerState *ps = loc->second;
count += ps->GetMembershipCount();
}
return count;
}
//
// Find or create the PeerState for given IPeer.
//
BgpMembershipManager::PeerState *BgpMembershipManager::LocatePeerState(
IPeer *peer) {
PeerStateMap::iterator loc = peer_state_map_.find(peer);
if (loc == peer_state_map_.end()) {
PeerState *ps = new PeerState(this, peer);
peer_state_map_.insert(make_pair(peer, ps));
return ps;
} else {
return loc->second;
}
}
//
// Find the PeerState for given IPeer.
//
BgpMembershipManager::PeerState *BgpMembershipManager::FindPeerState(
const IPeer *peer) {
PeerStateMap::iterator loc = peer_state_map_.find(peer);
return (loc != peer_state_map_.end() ? loc->second : NULL);
}
//
// Find the PeerState for given IPeer.
// Const version.
//
const BgpMembershipManager::PeerState *BgpMembershipManager::FindPeerState(
const IPeer *peer) const {
PeerStateMap::const_iterator loc = peer_state_map_.find(peer);
return (loc != peer_state_map_.end() ? loc->second : NULL);
}
//
// Destroy the given PeerState.
//
void BgpMembershipManager::DestroyPeerState(PeerState *ps) {
peer_state_map_.erase(ps->peer());
delete ps;
}
//
// Find or create the RibState for given BgpTable.
//
BgpMembershipManager::RibState *BgpMembershipManager::LocateRibState(
BgpTable *table) {
RibStateMap::iterator loc = rib_state_map_.find(table);
if (loc == rib_state_map_.end()) {
RibState *rs = new RibState(this, table);
rib_state_map_.insert(make_pair(table, rs));
return rs;
} else {
return loc->second;
}
}
//
// Find the RibState for given BgpTable.
//
BgpMembershipManager::RibState *BgpMembershipManager::FindRibState(
const BgpTable *table) {
RibStateMap::iterator loc = rib_state_map_.find(table);
return (loc != rib_state_map_.end() ? loc->second : NULL);
}
//
// Find the RibState for given BgpTable.
// Const version.
//
const BgpMembershipManager::RibState *BgpMembershipManager::FindRibState(
const BgpTable *table) const {
RibStateMap::const_iterator loc = rib_state_map_.find(table);
return (loc != rib_state_map_.end() ? loc->second : NULL);
}
//
// Destroy the given RibState.
//
void BgpMembershipManager::DestroyRibState(RibState *rs) {
rib_state_map_.erase(rs->table());
delete rs;
}
//
// Request the Walker to schedule a table walk for the given RibState.
// Note that the Walker accumulates requests and starts walks asynchronously.
//
void BgpMembershipManager::EnqueueRibState(RibState *rs) {
walker_->Enqueue(rs);
}
//
// Find or create the PeerRibState for given (IPeer, BgpTable).
//
BgpMembershipManager::PeerRibState *BgpMembershipManager::LocatePeerRibState(
IPeer *peer, BgpTable *table) {
PeerState *ps = LocatePeerState(peer);
RibState *rs = LocateRibState(table);
PeerRibState *prs = ps->LocatePeerRibState(rs);
rs->InsertPeerRibState(prs);
return prs;
}
//
// Find the PeerRibState for given (IPeer, BgpTable).
//
BgpMembershipManager::PeerRibState *BgpMembershipManager::FindPeerRibState(
const IPeer *peer, const BgpTable *table) {
PeerState *ps = FindPeerState(peer);
RibState *rs = FindRibState(table);
return (ps && rs ? ps->FindPeerRibState(rs) : NULL);
}
//
// Find the PeerRibState for given (IPeer, BgpTable).
// Const version.
//
const BgpMembershipManager::PeerRibState *
BgpMembershipManager::FindPeerRibState(
const IPeer *peer, const BgpTable *table) const {
const PeerState *ps = FindPeerState(peer);
const RibState *rs = FindRibState(table);
return (ps && rs ? ps->FindPeerRibState(rs) : NULL);
}
//
// Destroy the given PeerRibState.
// Also destroy the PeerState and/or RibState if they are no longer required.
//
void BgpMembershipManager::DestroyPeerRibState(PeerRibState *prs) {
PeerState *ps = prs->peer_state();
RibState *rs = prs->rib_state();
if (ps->RemovePeerRibState(prs))
DestroyPeerState(ps);
if (rs->RemovePeerRibState(prs))
DestroyRibState(rs);
delete prs;
}
//
// Process REGISTER_RIB event.
//
void BgpMembershipManager::ProcessRegisterRibEvent(Event *event) {
IPeer *peer = event->peer;
BgpTable *table = event->table;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == RIBOUT_ADD);
assert(prs->ribin_registered());
prs->set_instance_id(event->instance_id);
// Notify completion right away if the table is marked for deletion.
if (table->IsDeleted()) {
prs->clear_action();
peer->MembershipRequestCallback(table);
current_jobs_count_--;
return;
}
prs->RegisterRibOut(event->policy);
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Register table requested for action " << prs->action());
}
//
// Process REGISTER_RIB_COMPLETE event.
//
void BgpMembershipManager::ProcessRegisterRibCompleteEvent(Event *event) {
IPeer *peer = event->peer;
BgpTable *table = event->table;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs && prs->action() == RIBOUT_ADD);
assert(prs->ribin_registered());
assert(prs->ribout_registered());
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Register table completed for action " << prs->action());
prs->clear_action();
peer->MembershipRequestCallback(table);
NotifyPeerRegistration(peer, table, false);
current_jobs_count_--;
}
//
// Process UNREGISTER_RIB event.
//
void BgpMembershipManager::ProcessUnregisterRibEvent(Event *event) {
IPeer *peer = event->peer;
BgpTable *table = event->table;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs);
assert(prs->action() == RIBIN_DELETE_RIBOUT_DELETE ||
prs->action() == RIBIN_WALK_RIBOUT_DELETE);
if (prs->action() == RIBIN_DELETE_RIBOUT_DELETE)
assert(!prs->ribin_registered());
if (prs->action() == RIBIN_WALK_RIBOUT_DELETE)
assert(prs->ribin_registered());
assert(prs->ribout_registered());
prs->DeactivateRibOut();
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Unregister table requested for action " << prs->action());
}
//
// Process UNREGISTER_RIB_COMPLETE event.
//
void BgpMembershipManager::ProcessUnregisterRibCompleteEvent(Event *event) {
IPeer *peer = event->peer;
BgpTable *table = event->table;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs);
assert(prs->action() == RIBIN_DELETE_RIBOUT_DELETE ||
prs->action() == RIBIN_WALK_RIBOUT_DELETE);
if (prs->action() == RIBIN_DELETE_RIBOUT_DELETE)
assert(!prs->ribin_registered());
if (prs->action() == RIBIN_WALK_RIBOUT_DELETE)
assert(prs->ribin_registered());
prs->UnregisterRibOut();
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Unregister table completed for action " << prs->action());
prs->clear_action();
if (!prs->ribin_registered() && !prs->ribout_registered())
DestroyPeerRibState(prs);
peer->MembershipRequestCallback(table);
NotifyPeerRegistration(peer, table, true);
current_jobs_count_--;
}
//
// Process WALK_RIB_COMPLETE event.
//
void BgpMembershipManager::ProcessWalkRibCompleteEvent(Event *event) {
IPeer *peer = event->peer;
BgpTable *table = event->table;
PeerRibState *prs = FindPeerRibState(peer, table);
assert(prs);
assert(prs->action() == RIBIN_WALK || prs->action() == RIBIN_DELETE);
if (prs->action() == RIBIN_WALK) {
assert(prs->ribin_registered());
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Walk table completed for action " << prs->action());
} else {
assert(!prs->ribin_registered());
BGP_LOG_PEER_TABLE(peer, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_SYSLOG,
table, "Unregister table completed for action " << prs->action());
}
prs->clear_action();
if (!prs->ribin_registered() && !prs->ribout_registered())
DestroyPeerRibState(prs);
peer->MembershipRequestCallback(table);
current_jobs_count_--;
}
//
// Internal handler for an Event.
// Exists so that test code can override it.
//
bool BgpMembershipManager::EventCallbackInternal(Event *event) {
switch (event->event_type) {
case REGISTER_RIB:
ProcessRegisterRibEvent(event);
break;
case REGISTER_RIB_COMPLETE:
ProcessRegisterRibCompleteEvent(event);
break;
case UNREGISTER_RIB:
ProcessUnregisterRibEvent(event);
break;
case UNREGISTER_RIB_COMPLETE:
ProcessUnregisterRibCompleteEvent(event);
break;
case WALK_RIB_COMPLETE:
ProcessWalkRibCompleteEvent(event);
break;
default:
assert(false);
break;
}
delete event;
return true;
}
//
// Handler for an Event.
//
bool BgpMembershipManager::EventCallback(Event *event) {
CHECK_CONCURRENCY("bgp::PeerMembership");
return EventCallbackInternal(event);
}
//
// Constructor.
//`
BgpMembershipManager::Event::Event(EventType event_type, IPeer *peer,
BgpTable *table)
: event_type(event_type),
peer(peer),
table(table),
instance_id(-1) {
}
//
// Constructor.
//`
BgpMembershipManager::Event::Event(EventType event_type, IPeer *peer,
BgpTable *table, const RibExportPolicy &policy, int instance_id)
: event_type(event_type),
peer(peer),
table(table),
policy(policy),
instance_id(instance_id) {
}
//
// Constructor.
//`
BgpMembershipManager::PeerState::PeerState(BgpMembershipManager *manager,
IPeer *peer)
: manager_(manager),
peer_(peer) {
}
//
// Destructor.
//`
BgpMembershipManager::PeerState::~PeerState() {
assert(rib_map_.empty());
}
//
// Find or create the PeerRibState for given RibState.
//
BgpMembershipManager::PeerRibState *
BgpMembershipManager::PeerState::LocatePeerRibState(RibState *rs) {
PeerRibStateMap::iterator loc = rib_map_.find(rs);
if (loc == rib_map_.end()) {
PeerRibState *prs = new PeerRibState(manager_, this, rs);
rib_map_.insert(make_pair(rs, prs));
return prs;
} else {
return loc->second;
}
}
//
// Find the PeerRibState for given RibState.
//
BgpMembershipManager::PeerRibState *
BgpMembershipManager::PeerState::FindPeerRibState(const RibState *rs) {
PeerRibStateMap::iterator loc = rib_map_.find(rs);
return (loc != rib_map_.end() ? loc->second : NULL);
}
//
// Find the PeerRibState for given RibState.
// Const version.
//
const BgpMembershipManager::PeerRibState *
BgpMembershipManager::PeerState::FindPeerRibState(const RibState *rs) const {
PeerRibStateMap::const_iterator loc = rib_map_.find(rs);
return (loc != rib_map_.end() ? loc->second : NULL);
}
//
// Remove given PeerRibState from PeerRibStateMap.
// Return true if the PeerState itself can we deleted.
//
bool BgpMembershipManager::PeerState::RemovePeerRibState(PeerRibState *prs) {
PeerRibStateMap::iterator loc = rib_map_.find(prs->rib_state());
if (loc != rib_map_.end())
rib_map_.erase(loc);
return rib_map_.empty();
}
//
// Fill in the list of registered BgpTables.
//
void BgpMembershipManager::PeerState::GetRegisteredRibs(
list<BgpTable *> *table_list) const {
for (PeerRibStateMap::const_iterator loc = rib_map_.begin();
loc != rib_map_.end(); ++loc) {
const RibState *rs = loc->first;
table_list->push_back(rs->table());
}
}
//
// Fill introspect information.
//
void BgpMembershipManager::PeerState::FillPeerMembershipInfo(
BgpNeighborResp *resp) const {
vector<BgpNeighborRoutingTable> table_list;
for (PeerRibStateMap::const_iterator loc = rib_map_.begin();
loc != rib_map_.end(); ++loc) {
const RibState *rs = loc->first;
BgpNeighborRoutingTable table;
table.set_name(rs->table()->name());
table.set_current_state("subscribed");
table_list.push_back(table);
}
resp->set_routing_tables(table_list);
}
//
// Constructor.
//
BgpMembershipManager::RibState::RibState(BgpMembershipManager *manager,
BgpTable *table)
: manager_(manager),
table_(table),
request_count_(0),
walk_count_(0) {
}
//
// Destructor.
//
BgpMembershipManager::RibState::~RibState() {
assert(peer_rib_list_.empty());
assert(pending_peer_rib_list_.empty());
}
//
// Enqueue given PeerRibState into the pending PeerRibStateList.
//
void BgpMembershipManager::RibState::EnqueuePeerRibState(PeerRibState *prs) {
request_count_++;
pending_peer_rib_list_.insert(prs);
manager_->EnqueueRibState(this);
}
//
// Clear the pending PeerRibStateList.
//
void BgpMembershipManager::RibState::ClearPeerRibStateList() {
pending_peer_rib_list_.clear();
}
//
// Insert given PeerRibState into the regular PeerRibStateList.
//
void BgpMembershipManager::RibState::InsertPeerRibState(PeerRibState *prs) {
peer_rib_list_.insert(prs);
}
//
// Remove given PeerRibState from the regular PeerRibStateList.
//
bool BgpMembershipManager::RibState::RemovePeerRibState(PeerRibState *prs) {
peer_rib_list_.erase(prs);
return peer_rib_list_.empty();
}
//
// Fill introspect information.
//
void BgpMembershipManager::RibState::FillRoutingInstanceTableInfo(
ShowRoutingInstanceTable *srit) const {
ShowTableMembershipInfo stmi;
stmi.set_requests(request_count_);
stmi.set_walks(walk_count_);
vector<ShowMembershipPeerInfo> peers;
for (PeerRibList::const_iterator it = peer_rib_list_.begin();
it != peer_rib_list_.end(); ++it) {
const PeerRibState *prs = *it;
ShowMembershipPeerInfo smpi;
prs->FillMembershipInfo(&smpi);
peers.push_back(smpi);
}
stmi.set_peers(peers);
srit->set_membership(stmi);
}
//
// Constructor.
//
BgpMembershipManager::PeerRibState::PeerRibState(BgpMembershipManager *manager,
PeerState *ps, RibState *rs)
: manager_(manager),
ps_(ps),
rs_(rs),
ribout_(NULL),
ribout_index_(-1),
action_(BgpMembershipManager::NONE),
ribin_registered_(false),
ribout_registered_(false),
instance_id_(-1),
subscription_gen_id_(0),
table_delete_ref_(this, rs->table()->deleter()) {
}
//
// Destructor.
//
BgpMembershipManager::PeerRibState::~PeerRibState() {
assert(!ribout_);
assert(ribout_index_ == -1);
assert(action_ == BgpMembershipManager::NONE);
assert(!ribin_registered_);
assert(!ribout_registered_);
assert(instance_id_ == -1);
assert(subscription_gen_id_ == 0);
}
//
// Create RibOut for this PeerRibState and registers the RibOut as a listener
// for the BgpTable.
//
// Register the IPeer to the RibOut. Also join the IPeer to the UPDATE and
// BULK queues for the RibOutUpdates associated with the RibOut.
// This PeerRibState is added to the pending PeerRibStateList of RibState
// so that Join processing is handled when walking the BgpTable.
//
void BgpMembershipManager::PeerRibState::RegisterRibOut(
const RibExportPolicy &policy) {
CHECK_CONCURRENCY("bgp::PeerMembership");
BgpServer *server = manager_->server();
SchedulingGroupManager *sgm = server->scheduling_group_manager();
ribout_ = rs_->table()->RibOutLocate(sgm, policy);
ribout_->RegisterListener();
ribout_->Register(ps_->peer());
ribout_index_ = ribout_->GetPeerIndex(ps_->peer());
RibOutUpdates *updates = ribout_->updates();
SchedulingGroup *sg = ribout_->GetSchedulingGroup();
assert(sg);
if (updates->QueueJoin(RibOutUpdates::QUPDATE, ribout_index_))
sg->RibOutActive(ribout_, RibOutUpdates::QUPDATE);
if (updates->QueueJoin(RibOutUpdates::QBULK, ribout_index_))
sg->RibOutActive(ribout_, RibOutUpdates::QBULK);
ribout_registered_ = true;
rs_->EnqueuePeerRibState(this);
}
//
// Deactivate the IPeer in the RibOut.
// This ensures that the IPeer will stop exporting routes from now onwards.
//
// Note that this is called before Leave processing for the IPeer is started.
//
void BgpMembershipManager::PeerRibState::DeactivateRibOut() {
CHECK_CONCURRENCY("bgp::PeerMembership");
ribout_->Deactivate(ps_->peer());
rs_->EnqueuePeerRibState(this);
}
//
// Unregister the IPeer from the BgpTable.
// First leave the IPeer from the UPDATE and BULK queues for the RibOutUpdates
// associated with the RibOut.
// Then unregister the IPeer from the RibOut, which may result in deletion
// of the RibOut itself.
//
// Note that this is called only after Leave processing for the IPeer has been
// completed.
//
void BgpMembershipManager::PeerRibState::UnregisterRibOut() {
CHECK_CONCURRENCY("bgp::PeerMembership");
assert(ribout_);
assert(ribout_index_ != -1);
RibOutUpdates *updates = ribout_->updates();
updates->QueueLeave(RibOutUpdates::QUPDATE, ribout_index_);
updates->QueueLeave(RibOutUpdates::QBULK, ribout_index_);
ribout_->Unregister(ps_->peer());
ribout_ = NULL;
ribout_index_ = -1;
ribout_registered_ = false;
}
//
// Unregister the RibIn for the IPeer.
//
void BgpMembershipManager::PeerRibState::UnregisterRibIn() {
rs_->EnqueuePeerRibState(this);
}
//
// Walk the RibIn for the IPeer.
//
void BgpMembershipManager::PeerRibState::WalkRibIn() {
rs_->EnqueuePeerRibState(this);
}
//
// Fill introspect information.
//
void BgpMembershipManager::PeerRibState::FillMembershipInfo(
ShowMembershipPeerInfo *smpi) const {
smpi->set_peer(ps_->peer()->ToString());
smpi->set_ribin_registered(ribin_registered_);
smpi->set_ribout_registered(ribout_registered_);
smpi->set_instance_id(instance_id_);
smpi->set_generation_id(subscription_gen_id_);
}
//
// Constructor.
//
BgpMembershipManager::Walker::Walker(BgpMembershipManager *manager)
: manager_(manager),
trigger_(new TaskTrigger(
boost::bind(&BgpMembershipManager::Walker::WalkTrigger, this),
TaskScheduler::GetInstance()->GetTaskId("bgp::PeerMembership"), 0)),
postpone_walk_(false),