/
routepath_replicator_test.cc
2548 lines (2130 loc) · 99.3 KB
/
routepath_replicator_test.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/routepath_replicator.h"
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include "base/test/task_test_util.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_config_ifmap.h"
#include "bgp/bgp_config_parser.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_log.h"
#include "bgp/inet/inet_table.h"
#include "bgp/l3vpn/inetvpn_route.h"
#include "bgp/l3vpn/inetvpn_table.h"
#include "bgp/origin-vn/origin_vn.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/routing-instance/rtarget_group_mgr.h"
#include "bgp/test/bgp_server_test_util.h"
#include "bgp/test/bgp_test_util.h"
#include "control-node/control_node.h"
#include "db/db_graph.h"
#include "db/test/db_test_util.h"
#include "ifmap/ifmap_link_table.h"
#include "ifmap/ifmap_server_parser.h"
#include "ifmap/test/ifmap_test_util.h"
#include "io/event_manager.h"
#include "net/bgp_af.h"
#include "schema/bgp_schema_types.h"
#include "schema/vnc_cfg_types.h"
#include "testing/gunit.h"
using namespace std;
using boost::assign::list_of;
using boost::assign::map_list_of;
class BgpPeerMock : public IPeer {
public:
BgpPeerMock(const Ip4Address &address) : address_(address) { }
virtual ~BgpPeerMock() { }
virtual std::string ToString() const {
return address_.to_string();
}
virtual std::string ToUVEKey() const {
return address_.to_string();
}
virtual bool SendUpdate(const uint8_t *msg, size_t msgsize) {
return true;
}
virtual BgpServer *server() {
return NULL;
}
virtual IPeerClose *peer_close() {
return NULL;
}
virtual IPeerDebugStats *peer_stats() {
return NULL;
}
virtual const IPeerDebugStats *peer_stats() const {
return NULL;
}
virtual bool IsReady() const {
return true;
}
virtual bool IsXmppPeer() const { return false; }
virtual void Close() {
}
BgpProto::BgpPeerType PeerType() const {
return BgpProto::IBGP;
}
virtual uint32_t bgp_identifier() const {
return htonl(address_.to_ulong());
}
virtual const std::string GetStateName() const {
return "";
}
virtual void UpdateRefCount(int count) const { }
virtual tbb::atomic<int> GetRefCount() const {
tbb::atomic<int> count;
count = 0;
return count;
}
private:
Ip4Address address_;
};
#define VERIFY_EQ(expected, actual) \
TASK_UTIL_EXPECT_EQ(expected, actual)
static const char *bgp_server_config = "\
<config>\
<bgp-router name=\'localhost\'>\
<identifier>192.168.0.100</identifier>\
<address>192.168.0.100</address>\
<autonomous-system>64496</autonomous-system>\
</bgp-router>\
</config>\
";
class ReplicationTest : public ::testing::Test {
protected:
ReplicationTest() : bgp_server_(new BgpServer(&evm_)) {
IFMapLinkTable_Init(&config_db_, &config_graph_);
vnc_cfg_Server_ModuleInit(&config_db_, &config_graph_);
bgp_schema_Server_ModuleInit(&config_db_, &config_graph_);
}
~ReplicationTest() {
STLDeleteValues(&peers_);
}
virtual void SetUp() {
IFMapServerParser *parser = IFMapServerParser::GetInstance("schema");
vnc_cfg_ParserInit(parser);
bgp_schema_ParserInit(parser);
BgpIfmapConfigManager *config_manager =
static_cast<BgpIfmapConfigManager *>(
bgp_server_->config_manager());
config_manager->Initialize(&config_db_, &config_graph_, "localhost");
bgp_server_->rtarget_group_mgr()->Initialize();
BgpConfigParser bgp_parser(&config_db_);
bgp_parser.Parse(bgp_server_config);
task_util::WaitForIdle();
}
virtual void TearDown() {
task_util::WaitForIdle();
bgp_server_->Shutdown();
task_util::WaitForIdle();
db_util::Clear(&config_db_);
IFMapServerParser *parser = IFMapServerParser::GetInstance("schema");
parser->MetadataClear("schema");
}
void NetworkConfig(const vector<string> &instance_names,
const multimap<string, string> &connections) {
string netconf(
bgp_util::NetworkConfigGenerate(instance_names, connections));
IFMapServerParser *parser =
IFMapServerParser::GetInstance("schema");
parser->Receive(&config_db_, netconf.data(), netconf.length(), 0);
}
void DeleteRoutingInstance(const string &instance_name, const string &rt_name) {
ifmap_test_util::IFMapMsgUnlink(&config_db_, "routing-instance", instance_name,
"virtual-network", instance_name, "virtual-network-routing-instance");
ifmap_test_util::IFMapMsgUnlink(&config_db_, "routing-instance", instance_name,
"route-target", rt_name, "instance-target");
ifmap_test_util::IFMapMsgNodeDelete(
&config_db_, "virtual-network", instance_name);
ifmap_test_util::IFMapMsgNodeDelete(
&config_db_, "routing-instance", instance_name);
ifmap_test_util::IFMapMsgNodeDelete(
&config_db_, "route-target", rt_name);
task_util::WaitForIdle();
}
void VerifyTableNoExists(const string &table_name) {
TASK_UTIL_EXPECT_TRUE(
bgp_server_->database()->FindTable(table_name) == NULL);
}
void AddInetRoute(IPeer *peer, const string &instance_name,
const string &prefix, int localpref, string rd = "",
const vector<string> &rtarget_list = vector<string>()) {
boost::system::error_code error;
Ip4Prefix nlri = Ip4Prefix::FromString(prefix, &error);
EXPECT_FALSE(error);
DBRequest request;
request.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
request.key.reset(new InetTable::RequestKey(nlri, peer));
BgpAttrSpec attr_spec;
boost::scoped_ptr<BgpAttrLocalPref> local_pref(
new BgpAttrLocalPref(localpref));
attr_spec.push_back(local_pref.get());
BgpAttrSourceRd rd_spec(RouteDistinguisher::FromString(rd));
if (!rd.empty()) {
attr_spec.push_back(&rd_spec);
}
ExtCommunitySpec spec;
if (!rtarget_list.empty()) {
BOOST_FOREACH(string tgt, rtarget_list) {
RouteTarget rt(RouteTarget::FromString(tgt));
spec.communities.push_back(get_value(rt.GetExtCommunity().begin(), 8));
}
attr_spec.push_back(&spec);
}
BgpAttrPtr attr = bgp_server_->attr_db()->Locate(attr_spec);
request.data.reset(new BgpTable::RequestData(attr, 0, 0));
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(instance_name + ".inet.0"));
ASSERT_TRUE(table != NULL);
table->Enqueue(&request);
task_util::WaitForIdle();
}
void AddVPNRouteCommon(IPeer *peer, const string &prefix,
const BgpAttrSpec &attr_spec) {
boost::system::error_code error;
InetVpnPrefix nlri = InetVpnPrefix::FromString(prefix, &error);
EXPECT_FALSE(error);
DBRequest request;
request.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
request.key.reset(new InetVpnTable::RequestKey(nlri, peer));
BgpAttrPtr attr = bgp_server_->attr_db()->Locate(attr_spec);
request.data.reset(new BgpTable::RequestData(attr, 0, 0));
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable("bgp.l3vpn.0"));
ASSERT_TRUE(table != NULL);
table->Enqueue(&request);
task_util::WaitForIdle();
}
void AddVPNRoute(IPeer *peer, const string &prefix, int localpref,
const vector<string> &instance_names) {
BgpAttrSpec attr_spec;
boost::scoped_ptr<BgpAttrLocalPref> local_pref(
new BgpAttrLocalPref(localpref));
attr_spec.push_back(local_pref.get());
boost::scoped_ptr<ExtCommunitySpec> commspec;
commspec.reset(BuildInstanceListTargets(instance_names, &attr_spec));
AddVPNRouteCommon(peer, prefix, attr_spec);
task_util::WaitForIdle();
}
void AddVPNRouteWithTarget(IPeer *peer, const string &prefix, int localpref,
const string &target,
string origin_vn_str = string()) {
BgpAttrSpec attr_spec;
boost::scoped_ptr<BgpAttrLocalPref> local_pref(
new BgpAttrLocalPref(localpref));
attr_spec.push_back(local_pref.get());
boost::scoped_ptr<ExtCommunitySpec> commspec(new ExtCommunitySpec());
RouteTarget tgt = RouteTarget::FromString(target);
const ExtCommunity::ExtCommunityValue &extcomm =
tgt.GetExtCommunity();
uint64_t value = get_value(extcomm.data(), extcomm.size());
commspec->communities.push_back(value);
if (!origin_vn_str.empty()) {
OriginVn origin_vn = OriginVn::FromString(origin_vn_str);
commspec->communities.push_back(origin_vn.GetExtCommunityValue());
}
attr_spec.push_back(commspec.get());
AddVPNRouteCommon(peer, prefix, attr_spec);
task_util::WaitForIdle();
}
void DeleteVPNRoute(IPeer *peer, const string &prefix) {
boost::system::error_code error;
InetVpnPrefix nlri = InetVpnPrefix::FromString(prefix, &error);
EXPECT_FALSE(error);
DBRequest request;
request.oper = DBRequest::DB_ENTRY_DELETE;
request.key.reset(new InetVpnTable::RequestKey(nlri, peer));
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable("bgp.l3vpn.0"));
ASSERT_TRUE(table != NULL);
table->Enqueue(&request);
}
void DeleteInetRoute(IPeer *peer, const string &instance_name,
const string &prefix) {
boost::system::error_code error;
Ip4Prefix nlri = Ip4Prefix::FromString(prefix, &error);
EXPECT_FALSE(error);
DBRequest request;
request.oper = DBRequest::DB_ENTRY_DELETE;
request.key.reset(new InetTable::RequestKey(nlri, peer));
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(instance_name + ".inet.0"));
ASSERT_TRUE(table != NULL);
table->Enqueue(&request);
}
ExtCommunitySpec *BuildInstanceListTargets(
const vector<string> &instance_names, BgpAttrSpec *attr_spec) {
ExtCommunitySpec *commspec(new ExtCommunitySpec());
for (vector<string>::const_iterator iter = instance_names.begin();
iter != instance_names.end(); ++iter) {
RoutingInstance *rti =
bgp_server_->routing_instance_mgr()->GetRoutingInstance(*iter);
BOOST_FOREACH(RouteTarget tgt, rti->GetExportList()) {
const ExtCommunity::ExtCommunityValue &extcomm =
tgt.GetExtCommunity();
uint64_t value = get_value(extcomm.data(), extcomm.size());
commspec->communities.push_back(value);
}
}
if (!commspec->communities.empty()) {
attr_spec->push_back(commspec);
}
return commspec;
}
int RouteCount(const string &instance_name) const {
string tablename(instance_name);
tablename.append(".inet.0");
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(tablename));
EXPECT_TRUE(table != NULL);
if (table == NULL) {
return 0;
}
return table->Size();
}
BgpRoute *VPNRouteLookup(const string &prefix) {
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable("bgp.l3vpn.0"));
EXPECT_TRUE(table != NULL);
if (table == NULL) {
return NULL;
}
boost::system::error_code error;
InetVpnPrefix nlri = InetVpnPrefix::FromString(prefix, &error);
EXPECT_FALSE(error);
InetVpnTable::RequestKey key(nlri, NULL);
BgpRoute *rt = static_cast<BgpRoute *>(table->Find(&key));
return rt;
}
void VerifyVPNPathRouteTargets(const BgpPath *path,
const vector<string> &expected_targets) {
const BgpAttr *attr = path->GetAttr();
const ExtCommunity *ext_community = attr->ext_community();
set<string> actual_targets;
BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
ext_community->communities()) {
if (!ExtCommunity::is_route_target(comm))
continue;
RouteTarget rtarget(comm);
actual_targets.insert(rtarget.ToString());
}
EXPECT_EQ(actual_targets.size(), expected_targets.size());
BOOST_FOREACH(const string &target, expected_targets) {
EXPECT_TRUE(actual_targets.find(target) != actual_targets.end());
}
}
BgpRoute *InetRouteLookup(const string &instance_name, const string &prefix) {
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(instance_name + ".inet.0"));
EXPECT_TRUE(table != NULL);
if (table == NULL) {
return NULL;
}
boost::system::error_code error;
Ip4Prefix nlri = Ip4Prefix::FromString(prefix, &error);
EXPECT_FALSE(error);
InetTable::RequestKey key(nlri, NULL);
BgpRoute *rt = static_cast<BgpRoute *>(table->Find(&key));
return rt;
}
const RtReplicated *InetRouteReplicationState(const string &instance_name,
BgpRoute *rt) {
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(instance_name + ".inet.0"));
RoutePathReplicator *replicator =
bgp_server_->replicator(Address::INETVPN);
return replicator->GetReplicationState(table, rt);
}
const BgpRoute *GetVPNSecondary(const RtReplicated *rts) {
BOOST_FOREACH(const RtReplicated::SecondaryRouteInfo rinfo,
rts->GetList()) {
if (rinfo.rt_->Safi() == BgpAf::Vpn) {
return rinfo.rt_;
}
}
return NULL;
}
vector<string> GetInstanceRouteTargetList(const string &instance,
bool import = false) {
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance));
RoutingInstance *rti =
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance);
vector<string> target_list;
if (import) {
BOOST_FOREACH(RouteTarget tgt, rti->GetImportList()) {
target_list.push_back(tgt.ToString());
}
} else {
BOOST_FOREACH(RouteTarget tgt, rti->GetExportList()) {
target_list.push_back(tgt.ToString());
}
}
sort(target_list.begin(), target_list.end());
return target_list;
}
vector<string> GetInstanceImportRouteTargetList(const string &instance) {
return GetInstanceRouteTargetList(instance, true);
}
vector<string> GetInstanceExportRouteTargetList(const string &instance) {
return GetInstanceRouteTargetList(instance, false);
}
int GetInstanceOriginVnIndex(const string &instance) {
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance));
RoutingInstance *rti =
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance);
TASK_UTIL_EXPECT_NE(0, rti->virtual_network_index());
return rti->virtual_network_index();
}
void AddInstanceRouteTarget(const string &instance, const string &target,
bool do_import = true, bool do_export = true) {
assert(do_import || do_export);
autogen::InstanceTargetType *tgt_type = new autogen::InstanceTargetType;
if (do_import && do_export) {
tgt_type->import_export = "";
} else if (do_import) {
tgt_type->import_export = "import";
} else if (do_export) {
tgt_type->import_export = "export";
}
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance));
ifmap_test_util::IFMapMsgLink(&config_db_,
"routing-instance", instance,
"route-target", target, "instance-target", 0, tgt_type);
task_util::WaitForIdle();
}
void AddInstanceImportRouteTarget(const string &instance,
const string &target) {
AddInstanceRouteTarget(instance, target, true, false);
}
void AddInstanceExportRouteTarget(const string &instance,
const string &target) {
AddInstanceRouteTarget(instance, target, false, true);
}
void RemoveInstanceRouteTarget(const string instance, const string &target) {
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
bgp_server_->routing_instance_mgr()->GetRoutingInstance(instance));
ifmap_test_util::IFMapMsgUnlink(&config_db_,
"routing-instance", instance,
"route-target", target, "instance-target");
task_util::WaitForIdle();
}
int GetOriginVnIndexFromRoute(const BgpPath *path) {
const ExtCommunity *ext_comm = path->GetAttr()->ext_community();
assert(ext_comm);
BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
ext_comm->communities()) {
if (!ExtCommunity::is_origin_vn(comm))
continue;
OriginVn origin_vn(comm);
if (origin_vn.as_number() != 64496)
continue;
return origin_vn.vn_index();
}
return 0;
}
void DisableRouteTargetProcessing() {
bgp_server_->rtarget_group_mgr()->DisableRouteTargetProcessing();
}
void EnableRouteTargetProcessing() {
bgp_server_->rtarget_group_mgr()->EnableRouteTargetProcessing();
}
const TableState *LookupVpnTableState() {
RoutePathReplicator *replicator =
bgp_server_->replicator(Address::INETVPN);
RoutingInstanceMgr *mgr = bgp_server_->routing_instance_mgr();
RoutingInstance *master =
mgr->GetRoutingInstance(BgpConfigManager::kMasterInstance);
assert(master);
BgpTable *vpn_table = master->GetTable(Address::INETVPN);
const TableState *vpn_ts = replicator->FindTableState(vpn_table);
return vpn_ts;
}
void VerifyVpnTableStateExists(bool exists) {
if (exists)
TASK_UTIL_EXPECT_TRUE(LookupVpnTableState() != NULL);
else
TASK_UTIL_EXPECT_TRUE(LookupVpnTableState() == NULL);
}
const TableState *LookupTableState(const string &instance_name) {
RoutePathReplicator *replicator =
bgp_server_->replicator(Address::INETVPN);
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable(instance_name + ".inet.0"));
return replicator->FindTableState(table);
}
const TableState *VerifyVRFTableStateExists(const string &instance_name, bool exists) {
const TableState *ts = NULL;
if (exists)
TASK_UTIL_EXPECT_TRUE((ts = LookupTableState(instance_name)) != NULL);
else
TASK_UTIL_EXPECT_TRUE((ts = LookupTableState(instance_name)) == NULL);
return ts;
}
void VerifyVpnTableStateRouteCount(uint32_t count) {
const TableState *vpn_ts = LookupVpnTableState();
TASK_UTIL_EXPECT_EQ(count, (vpn_ts ? vpn_ts->route_count() : 0));
}
void DisableBulkSync() {
RoutePathReplicator *replicator =
bgp_server_->replicator(Address::INETVPN);
replicator->walk_trigger_->set_disable();
}
void EnableBulkSync() {
RoutePathReplicator *replicator =
bgp_server_->replicator(Address::INETVPN);
replicator->walk_trigger_->set_enable();
}
EventManager evm_;
DB config_db_;
DBGraph config_graph_;
boost::scoped_ptr<BgpServer> bgp_server_;
vector<BgpPeerMock *> peers_;
};
TEST_F(ReplicationTest, PathImport) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
// generate a notification for the route.
BgpRoute *rt = VPNRouteLookup("192.168.0.1:1:10.0.1.1/32");
ASSERT_TRUE(rt != NULL);
BgpTable *table = static_cast<BgpTable *>(
bgp_server_->database()->FindTable("bgp.l3vpn.0"));
table->Change(rt);
task_util::WaitForIdle();
// make sure that there are no changes.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, list_of("red"));
task_util::WaitForIdle();
// make sure that there are no changes.
VERIFY_EQ(1, rt->count());
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100,
list_of("green"));
task_util::WaitForIdle();
VERIFY_EQ(1, rt->count());
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(1, RouteCount("green"));
// Uninteresting route target.
AddVPNRouteWithTarget(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100,
"target:1:1");
task_util::WaitForIdle();
VERIFY_EQ(1, rt->count());
VERIFY_EQ(0, RouteCount("green"));
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(0, RouteCount("green"));
VERIFY_EQ(0, RouteCount("blue"));
}
TEST_F(ReplicationTest, NoExtCommunities) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
VERIFY_EQ(1, RouteCount("blue"));
// No extended communities
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, vector<string>());
task_util::WaitForIdle();
BgpRoute *rt = VPNRouteLookup("192.168.0.1:1:10.0.1.1/32");
VERIFY_EQ(1, rt->count());
VERIFY_EQ(0, RouteCount("blue"));
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
}
TEST_F(ReplicationTest, Delete) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
VERIFY_EQ(1, RouteCount("blue"));
// No extended communities
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
}
TEST_F(ReplicationTest, MultiplePaths) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.3", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.2.0.1:1:10.0.1.1/32", 100, list_of("blue"));
AddVPNRoute(peers_[1], "192.2.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
AddVPNRoute(peers_[2], "192.2.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
BgpRoute *rt = InetRouteLookup("blue", "10.0.1.1/32");
// Verify that all 3 paths are replicated
VERIFY_EQ(3, rt->count());
DeleteVPNRoute(peers_[0], "192.2.0.1:1:10.0.1.1/32");
DeleteVPNRoute(peers_[1], "192.2.0.1:1:10.0.1.1/32");
DeleteVPNRoute(peers_[2], "192.2.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
}
TEST_F(ReplicationTest, IdentifySecondary) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red")
("blue", "green");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.3", ec)));
AddInetRoute(peers_[0], "blue", "10.0.1.1/32", 100);
task_util::WaitForIdle();
// Replicated to both green and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
VERIFY_EQ(1, RouteCount("green"));
BgpRoute *rt_blue = InetRouteLookup("blue", "10.0.1.1/32");
BgpRoute *rt_red = InetRouteLookup("red", "10.0.1.1/32");
BgpRoute *rt_green = InetRouteLookup("green", "10.0.1.1/32");
VERIFY_EQ(1, rt_blue->count());
VERIFY_EQ(1, rt_red->count());
VERIFY_EQ(1, rt_green->count());
//
// Add 2 more paths in blue
//
AddInetRoute(peers_[1], "blue", "10.0.1.1/32", 100);
AddInetRoute(peers_[2], "blue", "10.0.1.1/32", 100);
task_util::WaitForIdle();
VERIFY_EQ(3, rt_blue->count());
// Verify that all three paths are replicated
VERIFY_EQ(3, rt_red->count());
// Verify that all three paths are replicated
VERIFY_EQ(3, rt_green->count());
AddInetRoute(peers_[0], "red", "10.0.1.1/32", 100);
#if 0
//
// Red path is better and is imported in blue. Now, Blue's best is the one
// imported from red. Hence its exported path into green should be gone by
// now
VERIFY_EQ(0, InetRouteLookup("green", "10.0.1.1/32"));
#endif
AddInetRoute(peers_[0], "green", "10.0.1.1/32", 100);
task_util::WaitForIdle();
rt_green = InetRouteLookup("green", "10.0.1.1/32");
// 2 replicated path and 3 paths sourced from peer
VERIFY_EQ(5, rt_blue->count());
// 3 replicated path and 1 path from the peer_[0]
VERIFY_EQ(4, rt_red->count());
// 3 replicated path and 1 path from the peer_[0]
VERIFY_EQ(4, rt_green->count());
DeleteInetRoute(peers_[0], "blue", "10.0.1.1/32");
task_util::WaitForIdle();
// 2 replicatd and 2 paths from peer
VERIFY_EQ(4, rt_blue->count());
// 2 replicatd and 1 path from peer[0]
VERIFY_EQ(3, rt_red->count());
// 2 replicatd and 1 path from peer[0]
VERIFY_EQ(3, rt_green->count());
DeleteInetRoute(peers_[1], "blue", "10.0.1.1/32");
DeleteInetRoute(peers_[2], "blue", "10.0.1.1/32");
task_util::WaitForIdle();
// 2 replicated paths
VERIFY_EQ(2, rt_blue->count());
// path from peer[0]
VERIFY_EQ(1, rt_red->count());
// path from peer[0]
VERIFY_EQ(1, rt_green->count());
DeleteInetRoute(peers_[0], "red", "10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, InetRouteLookup("red", "10.0.1.1/32"));
VERIFY_EQ(1, rt_green->count()); // peers_[0]
VERIFY_EQ(1, rt_blue->count()); // green(peers_[0])
DeleteInetRoute(peers_[0], "green", "10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("green"));
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(0, RouteCount("blue"));
}
TEST_F(ReplicationTest, MultiplePathsDiffRD) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 100, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
AddVPNRoute(peers_[1], "192.168.0.2:1:10.0.1.1/32", 100, list_of("red"));
task_util::WaitForIdle();
// Multiple paths should get imported.
VERIFY_EQ(1, RouteCount("blue"));
BgpRoute *rt = InetRouteLookup("blue", "10.0.1.1/32");
ASSERT_TRUE(rt != NULL);
VERIFY_EQ(2, rt->count());
VERIFY_EQ(1, RouteCount("red"));
DeleteVPNRoute(peers_[1], "192.168.0.2:1:10.0.1.1/32");
task_util::WaitForIdle();
rt = InetRouteLookup("blue", "10.0.1.1/32");
ASSERT_TRUE(rt != NULL);
VERIFY_EQ(1, rt->count());
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
}
TEST_F(ReplicationTest, PathChange) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 90, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
//
// Better path from peer1 which goes only to green
//
AddVPNRoute(peers_[1], "192.168.0.1:1:10.0.1.1/32", 100, list_of("green"));
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("green"));
VERIFY_EQ(0, RouteCount("red"));
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
DeleteVPNRoute(peers_[1], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(0, RouteCount("green"));
}
TEST_F(ReplicationTest, WithLocalRoute) {
// Add a route to "red" with the same prefix.
// Imported route becomes secondary.
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
// VPN route with target "blue".
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 80, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
AddInetRoute(peers_[1], "red", "10.0.1.1/32", 100);
task_util::WaitForIdle();
BgpRoute *rt = InetRouteLookup("red", "10.0.1.1/32");
ASSERT_TRUE(rt != NULL);
// Selected path is from peer[1]
VERIFY_EQ(peers_[1], rt->BestPath()->GetPeer());
const RtReplicated *rts = InetRouteReplicationState("red", rt);
ASSERT_TRUE(rts != NULL);
VERIFY_EQ(2, rts->GetList().size());
BOOST_FOREACH(const RtReplicated::SecondaryRouteInfo rinfo,
rts->GetList()) {
BGP_DEBUG_UT(rinfo.table_->name() << " " << rinfo.rt_->ToString());
}
// change VPN route
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 90, list_of("blue"));
task_util::WaitForIdle();
VERIFY_EQ(peers_[1], rt->BestPath()->GetPeer());
rts = InetRouteReplicationState("red", rt);
ASSERT_TRUE(rts != NULL);
VERIFY_EQ(2, rts->GetList().size());
// change the best path
AddVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32", 110, list_of("blue"));
task_util::WaitForIdle();
// Best path is from peer[0]
VERIFY_EQ(peers_[0], rt->BestPath()->GetPeer());
// Route from "red" is still replicated
rts = InetRouteReplicationState("red", rt);
ASSERT_TRUE(rts == NULL);
DeleteInetRoute(peers_[1], "red", "10.0.1.1/32");
DeleteVPNRoute(peers_[0], "192.168.0.1:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(0, RouteCount("green"));
}
TEST_F(ReplicationTest, ResurrectInetRoute) {
// Imported VPN route becomes secondary after inet route is added.
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.2", ec)));
// VPN route with same RD as exported inet route
AddVPNRoute(peers_[0], "192.168.0.100:1:10.0.1.1/32", 80, list_of("blue"));
task_util::WaitForIdle();
// Imported in both blue and red.
VERIFY_EQ(1, RouteCount("blue"));
VERIFY_EQ(1, RouteCount("red"));
AddInetRoute(peers_[1], "blue", "10.0.1.1/32", 100);
task_util::WaitForIdle();
BgpRoute *rt = InetRouteLookup("blue", "10.0.1.1/32");
ASSERT_TRUE(rt != NULL);
VERIFY_EQ(peers_[1], rt->BestPath()->GetPeer());
//
// Update local-pref inorder to make the path ecmp eligible
//
AddVPNRoute(peers_[0], "192.168.0.100:1:10.0.1.1/32", 100, list_of("blue"));
// Two paths.. One replicated from bgp.l3vpn.0 from peer[0]
// other one from blue.inet.0 from peer[1]
VERIFY_EQ(2, rt->count());
// Delete the INET route. This causes the VPN route to be imported to the
// VRF
DeleteInetRoute(peers_[1], "blue", "10.0.1.1/32");
task_util::WaitForIdle();
BgpRoute *rt_current = InetRouteLookup("blue", "10.0.1.1/32");
VERIFY_EQ(peers_[0], rt_current->BestPath()->GetPeer());
DeleteVPNRoute(peers_[0], "192.168.0.100:1:10.0.1.1/32");
task_util::WaitForIdle();
VERIFY_EQ(0, RouteCount("blue"));
VERIFY_EQ(0, RouteCount("red"));
VERIFY_EQ(0, RouteCount("green"));
}
TEST_F(ReplicationTest, LocalRouteFirst) {
vector<string> instance_names = list_of("blue")("red")("green");
multimap<string, string> connections = map_list_of("blue", "red");
NetworkConfig(instance_names, connections);
task_util::WaitForIdle();
boost::system::error_code ec;
peers_.push_back(
new BgpPeerMock(Ip4Address::from_string("192.168.0.1", ec)));
peers_.push_back(