-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_ribout_updates_test.h
706 lines (603 loc) · 22.8 KB
/
bgp_ribout_updates_test.h
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef ctrlplane_bgp_ribout_updates_test_h
#define ctrlplane_bgp_ribout_updates_test_h
#include "bgp/bgp_ribout_updates.h"
#include <boost/foreach.hpp>
#include <string>
#include <vector>
#include "base/task.h"
#include "base/task_annotations.h"
#include "base/util.h"
#include "base/test/task_test_util.h"
#include "control-node/control_node.h"
#include "bgp/bgp_attr.h"
#include "bgp/bgp_export.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_message_builder.h"
#include "bgp/bgp_peer.h"
#include "bgp/bgp_server.h"
#include "bgp/bgp_update.h"
#include "bgp/bgp_update_queue.h"
#include "bgp/bgp_update_sender.h"
#include "bgp/inet/inet_table.h"
#include "bgp/inet/inet_route.h"
#include "db/db.h"
#include "io/test/event_manager_test.h"
#include "testing/gunit.h"
static inline void SchedulerStop() {
TaskScheduler::GetInstance()->Stop();
}
static inline void SchedulerStart() {
TaskScheduler::GetInstance()->Start();
}
static int gbl_index;
static const int kPeerCount = 4;
static const int kRouteCount = 12;
static const int kAttrCount = 12;
class BgpTestPeer : public IPeerUpdate {
public:
BgpTestPeer() : index_(gbl_index++), count_(0), send_block_(false) {
std::ostringstream repr;
repr << "Peer" << index_;
to_str_ = repr.str();
}
virtual ~BgpTestPeer() { }
virtual const std::string &ToString() const { return to_str_; }
virtual bool SendUpdate(const uint8_t *msg, size_t msgsize) {
count_++;
send_block_ = block_set_.find(count_) != block_set_.end();
return !send_block_;
}
void set_block_at(int step) {
block_set_.insert(step);
}
void clear_blocks() {
block_set_.clear();
}
int update_count() const { return count_; }
void clear_update_count() { count_ = 0; }
bool send_block() const { return send_block_; }
void clear_send_block() { send_block_ = false; }
virtual bool send_ready() const { return !send_block_; }
private:
int index_;
std::set<int> block_set_;
int count_;
bool send_block_;
std::string to_str_;
};
class BgpMessageMock : public BgpMessage {
public:
BgpMessageMock() : route_count_(0) { }
virtual ~BgpMessageMock() { }
bool Start(const RibOut *ribout, bool cache_routes,
const RibOutAttr *roattr, const BgpRoute *route) {
msg_count_++;
route_count_ = 1;
return true;
}
virtual bool AddRoute(const BgpRoute *route, const RibOutAttr* attr) {
return (++route_count_ == 1000 ? false : true);
}
virtual void Finish() {
}
virtual const uint8_t *GetData(IPeerUpdate *peer, size_t *lenp,
const string **msg_str) {
return NULL;
}
static int msg_count() { return msg_count_; }
static void clear_msg_count() { msg_count_ = 0; }
private:
static int msg_count_;
int route_count_;
};
int BgpMessageMock::msg_count_;
class BgpMessageBuilderMock : public BgpMessageBuilder {
public:
virtual Message *Create() const {
return new BgpMessageMock;
}
};
class RibOutUpdatesTest : public ::testing::Test {
protected:
enum Step {
STEP_0 = 0,
STEP_1,
STEP_2,
STEP_3,
STEP_4,
STEP_5,
STEP_6,
STEP_7,
STEP_8,
STEP_9,
};
enum Count {
COUNT_0 = 0,
COUNT_1,
COUNT_2,
COUNT_3,
COUNT_4,
COUNT_5,
COUNT_6,
COUNT_7,
COUNT_8,
COUNT_9,
};
RibOutUpdatesTest()
: server_(&evm_),
sender_(server_.update_sender()),
spartition_(sender_->partition(0)),
table_(&db_, "inet.0"),
ribout_(table_.RibOutLocate(sender_, RibExportPolicy())),
dflt_prefix_(Ip4Prefix::FromString("0/0")),
dflt_rt_(dflt_prefix_) {
table_.Init();
dflt_rt_.set_onlist();
export_ = ribout_->bgp_export();
updates_ = ribout_->updates(0);
}
virtual void SetUp() {
SchedulerStop();
gbl_index = 0;
for (int idx = 0; idx < kPeerCount; idx++) {
CreatePeer();
RibOutRegister(ribout_, peers_[idx]);
}
for (int idx = 0; idx < kRouteCount; idx++) {
CreateRoute(idx);
}
CreateAttrs();
}
virtual void TearDown() {
server_.Shutdown();
DrainAndDeleteDBState();
SchedulerStart();
task_util::WaitForIdle();
BOOST_FOREACH(BgpTestPeer *peer, peers_) {
RibOutUnregister(ribout_, peer);
}
STLDeleteValues(&peers_);
STLDeleteValues(&routes_);
}
void RibOutRegister(RibOut *ribout, IPeerUpdate *peer) {
ConcurrencyScope scope("bgp::PeerMembership");
ribout->Register(peer);
ASSERT_TRUE(spartition_->PeerIsRegistered(peer));
}
void RibOutUnregister(RibOut *ribout, IPeerUpdate *peer) {
ConcurrencyScope scope("bgp::PeerMembership");
ribout->Deactivate(peer);
ribout->Unregister(peer);
ASSERT_FALSE(spartition_->PeerIsRegistered(peer));
}
void CreateAttrs() {
BgpAttr *attribute;
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(101);
attrA_ = server_.attr_db()->Locate(attribute);
roattrA_.set_attr(&table_, attrA_);
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(102);
attrB_ = server_.attr_db()->Locate(attribute);
roattrB_.set_attr(&table_, attrB_);
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(103);
attrC_ = server_.attr_db()->Locate(attribute);
roattrC_.set_attr(&table_, attrC_);
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(199);
attrZ_ = server_.attr_db()->Locate(attribute);
roattrZ_.set_attr(&table_, attrZ_);
for (int i = 0; i < kAttrCount; i++) {
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(1000 + i);
attr_.push_back(server_.attr_db()->Locate(attribute));
}
for (int i = 0; i < kAttrCount; i++) {
attribute = new BgpAttr(server_.attr_db());
attribute->set_med(2000 + i);
alt_attr_.push_back(server_.attr_db()->Locate(attribute));
}
}
void CreatePeer() {
BgpTestPeer *peer = new BgpTestPeer();
peers_.push_back(peer);
}
void CreateRoute(int idx) {
ASSERT_TRUE(idx <= 65535);
std::ostringstream repr;
repr << "10." << idx/256 << "." << idx%256 << "/24";
Ip4Prefix prefix = Ip4Prefix::FromString(repr.str());
InetRoute *route = new InetRoute(prefix);
route->set_onlist();
routes_.push_back(route);
}
void BuildPeerSet(RibPeerSet &peerset, int start_idx, int end_idx) {
peerset.clear();
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
peerset.set(ribout_->GetPeerIndex(peers_[idx]));
}
}
void PrependUpdateInfo(UpdateInfoSList &uinfo_slist, BgpAttrPtr attrX,
int label, int start_idx, int end_idx) {
RibPeerSet target;
BuildPeerSet(target, start_idx, end_idx);
RibOutAttr roattr(&table_, attrX.get(), label);
UpdateInfo *uinfo = new UpdateInfo(target, roattr);
uinfo_slist->push_front(*uinfo);
}
void PrependUpdateInfo(UpdateInfoSList &uinfo_slist, BgpAttrPtr attrX,
int start_idx, int end_idx) {
PrependUpdateInfo(uinfo_slist, attrX, 0, start_idx, end_idx);
}
void BuildUpdateInfo(UpdateInfoSList &uinfo_slist,
std::vector<BgpAttrPtr> &attrvec, int label,
int start_idx, int end_idx) {
for (int idx = start_idx; idx <= end_idx; idx++) {
RibPeerSet target;
target.set(ribout_->GetPeerIndex(peers_[idx]));
RibOutAttr roattr(&table_, attrvec[idx].get(), label);
UpdateInfo *uinfo = new UpdateInfo(target, roattr);
uinfo_slist->push_front(*uinfo);
}
}
void BuildUpdateInfo(UpdateInfoSList &uinfo_slist,
std::vector<BgpAttrPtr> &attrvec, int start_idx, int end_idx) {
BuildUpdateInfo(uinfo_slist, attrvec, 0, start_idx, end_idx);
}
void CloneUpdateInfo(UpdateInfoSList &src_uinfo_slist,
UpdateInfoSList &dst_uinfo_slist) {
for (UpdateInfoSList::List::iterator iter = src_uinfo_slist->begin();
iter != src_uinfo_slist->end(); iter++) {
UpdateInfo *uinfo = new UpdateInfo(iter->target, iter->roattr);
dst_uinfo_slist->push_front(*uinfo);
}
}
void FlushUpdateInfo(UpdateInfoSList &uinfo_slist) {
uinfo_slist->clear_and_dispose(UpdateInfoDisposer());
}
void SetPeerBlock(int idx, Step step) {
ASSERT_TRUE(step >= STEP_1);
ASSERT_TRUE(idx < (int) peers_.size());
peers_[idx]->set_block_at(step);
}
void SetPeerBlock(int start_idx, int end_idx, Step step) {
ASSERT_TRUE(step >= STEP_1);
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
peers_[idx]->set_block_at(step);
}
}
void SetEvenPeerBlock(int start_idx, int end_idx, Step step) {
ASSERT_TRUE(step >= STEP_1);
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if (idx % 2 == 0)
peers_[idx]->set_block_at(step);
}
}
void SetOddPeerBlock(int start_idx, int end_idx, Step step) {
ASSERT_TRUE(step >= STEP_1);
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if (idx % 2 == 1)
peers_[idx]->set_block_at(step);
}
}
void SetPeerBlockNow(int start_idx, int end_idx) {
ConcurrencyScope scope("bgp::SendUpdate");
RibPeerSet blocked;
for (int idx = start_idx; idx <= end_idx; idx++) {
blocked.set(ribout_->GetPeerIndex(peers_[idx]));
}
spartition_->SetSendBlocked(ribout_, RibOutUpdates::QUPDATE, blocked);
}
void SetPeerUnblockNow(int idx) {
ConcurrencyScope scope("bgp::SendReadyTask");
ASSERT_TRUE(idx < (int) peers_.size());
peers_[idx]->clear_send_block();
spartition_->PeerSendReady(peers_[idx]);
}
void SetPeerUnblockNow(int start_idx, int end_idx) {
ConcurrencyScope scope("bgp::SendReadyTask");
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
peers_[idx]->clear_send_block();
spartition_->PeerSendReady(peers_[idx]);
}
}
void VerifyOddEvenPeerBlock(int start_idx, int end_idx,
bool even, bool odd, bool blocked) {
ConcurrencyScope scope("bgp::PeerMembership");
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if ((idx % 2 == 0) == even || (idx % 2 == 1) == odd)
EXPECT_EQ(blocked, !spartition_->PeerIsSendReady(peers_[idx]));
}
}
void VerifyPeerBlock(int idx, bool blocked) {
VerifyOddEvenPeerBlock(idx, idx, true, true, blocked);
}
void VerifyPeerBlock(int start_idx, int end_idx, bool blocked) {
VerifyOddEvenPeerBlock(start_idx, end_idx, true, true, blocked);
}
void VerifyEvenPeerBlock(int start_idx, int end_idx, bool blocked) {
VerifyOddEvenPeerBlock(start_idx, end_idx, true, false, blocked);
}
void VerifyOddPeerBlock(int start_idx, int end_idx, bool blocked) {
VerifyOddEvenPeerBlock(start_idx, end_idx, false, true, blocked);
}
void VerifyOddEvenPeerInSync(int start_idx, int end_idx,
bool even, bool odd, bool in_sync) {
ConcurrencyScope scope("bgp::PeerMembership");
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if ((idx % 2 == 0) == even || (idx % 2 == 1) == odd)
EXPECT_EQ(in_sync, spartition_->PeerInSync(peers_[idx]));
}
}
void VerifyPeerInSync(int idx, bool in_sync) {
VerifyOddEvenPeerInSync(idx, idx, true, true, in_sync);
}
void VerifyPeerInSync(int start_idx, int end_idx, bool in_sync) {
VerifyOddEvenPeerInSync(start_idx, end_idx, true, true, in_sync);
}
void VerifyEvenPeerInSync(int start_idx, int end_idx, bool in_sync) {
VerifyOddEvenPeerInSync(start_idx, end_idx, true, false, in_sync);
}
void VerifyOddPeerInSync(int start_idx, int end_idx, bool in_sync) {
VerifyOddEvenPeerInSync(start_idx, end_idx, false, true, in_sync);
}
void ClearAllPeerBlocks(int start_idx, int end_idx) {
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
peers_[idx]->clear_blocks();
}
}
void ClearPeerCount(int start_idx, int end_idx) {
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
peers_[idx]->clear_update_count();
}
}
RouteUpdate *BuildRouteUpdate(BgpRoute *route, UpdateInfoSList &uinfo_slist,
int qid = RibOutUpdates::QUPDATE) {
ConcurrencyScope scope("db::DBTable");
RouteUpdate *rt_update = new RouteUpdate(route, qid);
rt_update->SetUpdateInfo(uinfo_slist);
updates_->Enqueue(route, rt_update);
return rt_update;
}
void EnqueueDefaultRoute() {
UpdateInfoSList uinfo_slist;
PrependUpdateInfo(uinfo_slist, attrZ_, 0, (int) peers_.size()-1);
BuildRouteUpdate(&dflt_rt_, uinfo_slist);
}
void VerifyDefaultRoute() {
RouteState *rstate = ExpectRouteState(&dflt_rt_);
VerifyHistory(rstate, attrZ_, 0, (int) peers_.size()-1);
}
void ExpectNullDBState(BgpRoute *route) {
DBState *dbstate = route->GetState(&table_, ribout_->listener_id());
EXPECT_TRUE(dbstate == NULL);
}
RouteState *ExpectRouteState(BgpRoute *route) {
DBState *dbstate = route->GetState(&table_, ribout_->listener_id());
RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
EXPECT_TRUE(rstate != NULL);
return rstate;
}
RouteUpdate *ExpectRouteUpdate(BgpRoute *route,
int qid = RibOutUpdates::QUPDATE) {
DBState *dbstate = route->GetState(&table_, ribout_->listener_id());
RouteUpdate *rt_update = dynamic_cast<RouteUpdate *>(dbstate);
EXPECT_TRUE(rt_update != NULL);
EXPECT_EQ(qid, rt_update->queue_id());
return rt_update;
}
void VerifyUpdates(RouteUpdate *rt_update, BgpAttrPtr attrX,
int start_idx, int end_idx, int count = 1) {
RibPeerSet uinfo_peerset;
BuildPeerSet(uinfo_peerset, start_idx, end_idx);
EXPECT_TRUE(rt_update != NULL);
EXPECT_EQ(count, rt_update->Updates()->size());
RibOutAttr roattrX(&table_, attrX.get(), 0);
const UpdateInfo *uinfo = rt_update->FindUpdateInfo(roattrX);
EXPECT_TRUE(uinfo != NULL);
EXPECT_TRUE(uinfo->target == uinfo_peerset);
}
void VerifyUpdates(RouteUpdate *rt_update, std::vector<BgpAttrPtr> &attrvec,
int start_idx, int end_idx) {
EXPECT_EQ(end_idx-start_idx+1, rt_update->Updates()->size());
for (int idx = start_idx; idx <= end_idx; idx++) {
RibOutAttr roattrX(&table_, attrvec[idx].get(), 0);
const UpdateInfo *uinfo = rt_update->FindUpdateInfo(roattrX);
EXPECT_TRUE(uinfo != NULL);
RibPeerSet target;
target.set(ribout_->GetPeerIndex(peers_[idx]));
EXPECT_TRUE(uinfo->target == target);
}
}
void VerifyHistory(RouteState *rstate, RibOutAttr &roattrX,
int start_idx, int end_idx, int count = 1) {
RibPeerSet adv_peerset;
BuildPeerSet(adv_peerset, start_idx, end_idx);
EXPECT_EQ(count, rstate->Advertised()->size());
const AdvertiseInfo *ainfo = rstate->FindHistory(roattrX);
EXPECT_TRUE(ainfo != NULL);
EXPECT_TRUE(ainfo->bitset == adv_peerset);
}
void VerifyHistory(RouteState *rstate, BgpAttrPtr attrX,
int start_idx, int end_idx, int count = 1) {
RibOutAttr roattrX(&table_, attrX.get(), 0);
VerifyHistory(rstate, roattrX, start_idx, end_idx, count);
}
void VerifyHistory(RouteState *rstate, std::vector<BgpAttrPtr> &attrvec,
int label, int start_idx, int end_idx) {
EXPECT_EQ(end_idx-start_idx+1, rstate->Advertised()->size());
for (int idx = start_idx; idx <= end_idx; idx++) {
RibOutAttr roattrX(&table_, attrvec[idx].get(), label);
const AdvertiseInfo *ainfo = rstate->FindHistory(roattrX);
EXPECT_TRUE(ainfo != NULL);
RibPeerSet bitset;
bitset.set(ribout_->GetPeerIndex(peers_[idx]));
EXPECT_TRUE(ainfo->bitset == bitset);
}
}
void VerifyHistory(RouteState *rstate, std::vector<BgpAttrPtr> &attrvec,
int start_idx, int end_idx) {
VerifyHistory(rstate, attrvec, 0, start_idx, end_idx);
}
void VerifyHistory(RouteUpdate *rt_update) {
EXPECT_EQ(0, rt_update->History()->size());
}
void VerifyHistory(RouteUpdate *rt_update, BgpAttrPtr attrX,
int start_idx, int end_idx, int count = 1) {
RibPeerSet adv_peerset;
BuildPeerSet(adv_peerset, start_idx, end_idx);
EXPECT_EQ(count, rt_update->History()->size());
RibOutAttr roattrX(&table_, attrX.get(), 0);
const AdvertiseInfo *ainfo = rt_update->FindHistory(roattrX);
EXPECT_TRUE(ainfo != NULL);
EXPECT_TRUE(ainfo->bitset == adv_peerset);
}
void VerifyHistory(RouteUpdate *rt_update, std::vector<BgpAttrPtr> &attrvec,
int start_idx, int end_idx) {
EXPECT_EQ(end_idx-start_idx+1, rt_update->History()->size());
for (int idx = start_idx; idx <= end_idx; idx++) {
RibOutAttr roattrX(&table_, attrvec[idx].get(), 0);
const AdvertiseInfo *ainfo = rt_update->FindHistory(roattrX);
EXPECT_TRUE(ainfo != NULL);
RibPeerSet bitset;
bitset.set(ribout_->GetPeerIndex(peers_[idx]));
EXPECT_TRUE(ainfo->bitset == bitset);
}
}
void VerifyUpdateCount(int idx, Count count) {
ASSERT_TRUE(idx < (int) peers_.size());
EXPECT_EQ(count, peers_[idx]->update_count());
}
void VerifyUpdateCount(int start_idx, int end_idx, Count count) {
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
EXPECT_EQ(count, peers_[idx]->update_count());
}
}
void VerifyEvenUpdateCount(int start_idx, int end_idx, Count count) {
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if (idx % 2 == 0)
EXPECT_EQ(count, peers_[idx]->update_count());
}
}
void VerifyOddUpdateCount(int start_idx, int end_idx, Count count) {
ASSERT_TRUE(start_idx <= end_idx);
ASSERT_TRUE(end_idx < (int) peers_.size());
for (int idx = start_idx; idx <= end_idx; idx++) {
if (idx % 2 == 1)
EXPECT_EQ(count, peers_[idx]->update_count());
}
}
void VerifyMessageCount(int count) {
EXPECT_EQ(count, BgpMessageMock::msg_count());
}
void ClearMessageCount() {
BgpMessageMock::clear_msg_count();
}
void DeleteRouteState(BgpRoute *route) {
DBState *dbstate = route->GetState(&table_, ribout_->listener_id());
if (!dbstate) return;
RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
EXPECT_TRUE(rstate != NULL);
route->ClearState(&table_, ribout_->listener_id());
delete rstate;
}
void DeleteRouteState(int start_idx, int end_idx) {
for (int idx = start_idx; idx <= end_idx; idx++) {
BgpRoute *route = routes_[idx];
DBState *dbstate = route->GetState(&table_, ribout_->listener_id());
if (!dbstate) continue;
RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
EXPECT_TRUE(rstate != NULL);
route->ClearState(&table_, ribout_->listener_id());
delete rstate;
}
}
void CheckInvariants() {
for (int qid = RibOutUpdates::QFIRST; qid < RibOutUpdates::QCOUNT;
qid++) {
UpdateQueue *queue = updates_->queue_vec_[qid];
queue->CheckInvariants();
}
}
void CheckTerminationInvariants() {
for (int qid = RibOutUpdates::QFIRST; qid < RibOutUpdates::QCOUNT;
qid++) {
UpdateQueue *queue = updates_->queue_vec_[qid];
queue->CheckInvariants();
ASSERT_EQ(1, queue->queue_.size());
}
}
void DrainAndDeleteDBState() {
BGP_DEBUG_UT("Cleaning up for next iteration");
CheckInvariants();
ClearAllPeerBlocks(0, (int) peers_.size()-1);
SetPeerUnblockNow(0, (int) peers_.size()-1);
UpdateAllPeers();
UpdateRibOut();
ClearPeerCount(0, (int) peers_.size()-1);
ClearMessageCount();
DeleteRouteState(0, (int) routes_.size()-1);
DeleteRouteState(&dflt_rt_);
CheckTerminationInvariants();
}
void UpdateRibOut(int qid = RibOutUpdates::QUPDATE) {
ConcurrencyScope scope("bgp::SendUpdate");
spartition_->UpdateRibOut(ribout_, qid);
}
void UpdatePeer(BgpTestPeer *peer) {
ConcurrencyScope scope("bgp::SendUpdate");
spartition_->UpdatePeer(peer);
}
void UpdateAllPeers() {
for (int idx = 0; idx < (int) peers_.size(); idx++) {
UpdatePeer(peers_[idx]);
}
}
EventManager evm_;
BgpServer server_;
DB db_;
BgpUpdateSender *sender_;
BgpSenderPartition *spartition_;
InetTable table_;
RibOut *ribout_;
BgpExport *export_;
RibOutUpdates *updates_;
Ip4Prefix dflt_prefix_;
InetRoute dflt_rt_;
std::vector<BgpTestPeer *> peers_;
std::vector<BgpRoute *> routes_;
std::vector<BgpAttrPtr> attr_;
std::vector<BgpAttrPtr> alt_attr_;
BgpAttrPtr attrA_, attrB_, attrC_, attrZ_;
BgpAttrPtr attr_null_;
RibOutAttr roattrA_, roattrB_, roattrC_, roattrZ_;
RibOutAttr roattr_null_;
};
#endif