-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_peer_close_test.cc
1155 lines (962 loc) · 34.8 KB
/
bgp_peer_close_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 <boost/algorithm/string.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <list>
#include "base/logging.h"
#include "base/task.h"
#include "base/test/addr_test_util.h"
#include "base/test/task_test_util.h"
#include "base/util.h"
#include "io/event_manager.h"
#include "bgp/bgp_attr.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_debug.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_path.h"
#include "bgp/bgp_peer_close.h"
#include "bgp/bgp_peer_membership.h"
#include "bgp/bgp_proto.h"
#include "bgp/bgp_ribout.h"
#include "bgp/bgp_server.h"
#include "bgp/bgp_session_manager.h"
#include "bgp/bgp_xmpp_channel.h"
#include "bgp/inet/inet_route.h"
#include "bgp/inet/inet_table.h"
#include "bgp/l3vpn/inetvpn_table.h"
#include "bgp/routing-instance/peer_manager.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/test/bgp_server_test_util.h"
#include "bgp/tunnel_encap/tunnel_encap.h"
#include "bgp/xmpp_message_builder.h"
#include "control-node/control_node.h"
#include "control-node/test/network_agent_mock.h"
#include "io/test/event_manager_test.h"
#include "db/db.h"
#include "net/bgp_af.h"
#include "schema/xmpp_unicast_types.h"
#include "testing/gunit.h"
#include "xmpp/xmpp_client.h"
#include "xmpp/xmpp_init.h"
#include "xmpp/xmpp_server.h"
#include "xmpp/xmpp_state_machine.h"
#define XMPP_CONTROL_SERV "bgp.contrail.com"
#define PUBSUB_NODE_ADDR "bgp-node.contrail.com"
using namespace std;
using namespace boost;
using namespace boost::asio;
using namespace boost::assign;
using namespace boost::program_options;
using boost::any_cast;
using ::testing::TestWithParam;
using ::testing::Bool;
using ::testing::ValuesIn;
using ::testing::Combine;
static vector<int> n_instances = boost::assign::list_of(1);
static vector<int> n_routes = boost::assign::list_of(1);
static vector<int> n_peers = boost::assign::list_of(1);
static vector<int> n_agents = boost::assign::list_of(1);
static vector<int> n_targets = boost::assign::list_of(1);
static vector<bool> xmpp_close_from_control_node =
boost::assign::list_of(false);
static char **gargv;
static int gargc;
static int n_db_walker_wait_usecs = 0;
static int wait_for_idle = 30; // Seconds
static void process_command_line_args(int argc, char **argv) {
static bool cmd_line_processed;
if (cmd_line_processed) return;
cmd_line_processed = true;
int instances = 1, routes = 1, peers = 1, agents = 1, targets = 1;
bool close_from_control_node = false;
bool cmd_line_arg_set = false;
// Declare the supported options.
options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("nroutes", value<int>(), "set number of routes")
("npeers", value<int>(), "set number of bgp peers")
("nagents", value<int>(), "set number of xmpp agents")
("ninstances", value<int>(), "set number of routing instances")
("ntargets", value<int>(), "set number of route targets")
("db-walker-wait-usecs", value<int>(), "set usecs delay in walker cb")
("wait-for-idle-time", value<int>(),
"task_util::WaitForIdle() wait time, 0 to disable")
("close-from-control-node", bool_switch(&close_from_control_node),
"Initiate xmpp session close from control-node")
;
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(1);
}
if (close_from_control_node) {
cmd_line_arg_set = true;
}
if (vm.count("ninstances")) {
instances = vm["ninstances"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("nroutes")) {
routes = vm["nroutes"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("npeers")) {
peers = vm["npeers"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("nagents")) {
agents = vm["nagents"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("ntargets")) {
targets = vm["ntargets"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("db-walker-wait-usecs")) {
n_db_walker_wait_usecs = vm["db-walker-wait-usecs"].as<int>();
cmd_line_arg_set = true;
}
if (vm.count("wait-for-idle-time")) {
wait_for_idle = vm["wait-for-idle-time"].as<int>();
cmd_line_arg_set = true;
}
if (cmd_line_arg_set) {
n_instances.clear();
n_instances.push_back(instances);
n_routes.clear();
n_routes.push_back(routes);
n_peers.clear();
n_peers.push_back(peers);
n_targets.clear();
n_targets.push_back(targets);
n_agents.clear();
n_agents.push_back(agents);
xmpp_close_from_control_node.clear();
xmpp_close_from_control_node.push_back(close_from_control_node);
}
}
static vector<int> GetInstanceParameters() {
process_command_line_args(gargc, gargv);
return n_instances;
}
static vector<int> GetAgentParameters() {
process_command_line_args(gargc, gargv);
return n_agents;
}
static vector<int> GetRouteParameters() {
process_command_line_args(gargc, gargv);
return n_routes;
}
static vector<int> GetPeerParameters() {
process_command_line_args(gargc, gargv);
return n_peers;
}
static vector<int> GetTargetParameters() {
process_command_line_args(gargc, gargv);
return n_targets;
}
static void WaitForIdle() {
if (wait_for_idle) {
usleep(10);
task_util::WaitForIdle(wait_for_idle);
}
}
class PeerCloseManagerTest : public PeerCloseManager {
public:
explicit PeerCloseManagerTest(IPeer *peer) : PeerCloseManager(peer) { }
~PeerCloseManagerTest() { }
//
// Do not start the timer in test, as we right away call it in line from
// within the tests
//
void StartStaleTimer() { }
};
class BgpNullPeer {
public:
BgpNullPeer(BgpServerTest *server, const BgpInstanceConfig *instance_config,
string name, RoutingInstance *rtinstance, int peer_id)
: name_(name), peer_id_(peer_id), config_(new BgpNeighborConfig) {
for (int i = 0; i < 20; i++) {
ribout_creation_complete_.push_back(false);
}
config_->set_name(name);
config_->set_instance_name(rtinstance->name());
config_->set_peer_address(IpAddress::from_string("127.0.0.1"));
config_->set_peer_as(65412);
config_->set_port(peer_id);
peer_ = static_cast<BgpPeerTest *>
(rtinstance->peer_manager()->PeerLocate(server, config_.get()));
WaitForIdle();
}
BgpPeerTest *peer() { return peer_; }
int peer_id() { return peer_id_; }
bool ribout_creation_complete(Address::Family family) {
return ribout_creation_complete_[family];
}
void ribout_creation_complete(Address::Family family, bool complete) {
ribout_creation_complete_[family] = complete;
}
string name_;
int peer_id_;
BgpPeerTest *peer_;
private:
auto_ptr<BgpNeighborConfig> config_;
std::vector<bool> ribout_creation_complete_;
};
class BgpXmppChannelManagerMock : public BgpXmppChannelManager {
public:
BgpXmppChannelManagerMock(XmppServer *x, BgpServer *b) :
BgpXmppChannelManager(x, b), channel_(NULL) { }
virtual BgpXmppChannel *CreateChannel(XmppChannel *channel) {
channel_ = new BgpXmppChannel(channel, bgp_server_, this);
return channel_;
}
BgpXmppChannel *channel_;
};
typedef std::tr1::tuple<int, int, int, int, int, bool> TestParams;
class BgpPeerCloseTest : public ::testing::TestWithParam<TestParams> {
public:
void CreateRibsDone(IPeer *ipeer, BgpTable *table, BgpNullPeer *npeer) {
npeer->ribout_creation_complete(table->family(), true);
}
void DeleteRoutingInstance(RoutingInstance *rtinstance);
bool IsPeerCloseGraceful(bool graceful) { return graceful; }
void SetPeerCloseGraceful(bool graceful) {
server_->GetIsPeerCloseGraceful_fnc_ =
boost::bind(&BgpPeerCloseTest::IsPeerCloseGraceful, this,
graceful);
xmpp_server_->GetIsPeerCloseGraceful_fnc_ =
boost::bind(&BgpPeerCloseTest::IsPeerCloseGraceful, this,
graceful);
}
protected:
BgpPeerCloseTest() : thread_(&evm_) { }
virtual void SetUp() {
server_.reset(new BgpServerTest(&evm_, "test"));
xmpp_server_ = new XmppServerTest(&evm_, XMPP_CONTROL_SERV);
channel_manager_.reset(new BgpXmppChannelManagerMock(
xmpp_server_, server_.get()));
master_cfg_.reset(BgpTestUtil::CreateBgpInstanceConfig(
BgpConfigManager::kMasterInstance, "", ""));
rtinstance_ = static_cast<RoutingInstance *>(
server_->routing_instance_mgr()->GetRoutingInstance(
BgpConfigManager::kMasterInstance));
n_families_ = 2;
familes_.push_back(Address::INET);
familes_.push_back(Address::INETVPN);
server_->session_manager()->Initialize(0);
xmpp_server_->Initialize(0, false);
thread_.Start();
}
void AgentCleanup() {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
agent->Delete();
}
}
void Cleanup() {
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
delete npeer;
}
WaitForIdle();
}
virtual void TearDown() {
SetPeerCloseGraceful(false);
xmpp_server_->Shutdown();
WaitForIdle();
if (n_agents_) {
TASK_UTIL_EXPECT_EQ(0, xmpp_server_->ConnectionCount());
}
AgentCleanup();
channel_manager_.reset();
WaitForIdle();
TcpServerManager::DeleteServer(xmpp_server_);
xmpp_server_ = NULL;
server_->Shutdown();
WaitForIdle();
Cleanup();
evm_.Shutdown();
thread_.Join();
task_util::WaitForIdle();
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
delete agent;
}
}
void Configure() {
server_->Configure(GetConfig().c_str());
WaitForIdle();
VerifyRoutingInstances();
}
XmppChannelConfig *CreateXmppChannelCfg(const char *address, int port,
const string &from,
const string &to,
bool isClient) {
XmppChannelConfig *cfg = new XmppChannelConfig(isClient);
boost::system::error_code ec;
cfg->endpoint.address(ip::address::from_string(address, ec));
cfg->endpoint.port(port);
cfg->ToAddr = to;
cfg->FromAddr = from;
if (!isClient) cfg->NodeAddr = PUBSUB_NODE_ADDR;
return cfg;
}
string GetConfig();
BgpAttr *CreatePathAttr();
void AddRoutes(BgpTable *table, BgpNullPeer *npeer);
ExtCommunitySpec *CreateRouteTargets();
void AddAllRoutes();
void AddPeersWithRoutes(const BgpInstanceConfig *instance_config);
void AddXmppPeersWithRoutes();
void CreateAgents();
void Subscribe();
void UnSubscribe();
void AddXmppRoutes();
void DeleteXmppRoutes();
void VerifyXmppRoutes(int routes);
void DeleteAllRoutingInstances();
void VerifyRoutingInstances();
void XmppPeerClose();
void CallStaleTimer(bool);
void InitParams();
void VerifyPeer(BgpServerTest *server, RoutingInstance *rtinstance,
BgpNullPeer *npeer, BgpPeerTest *peer);
void VerifyPeers();
void VerifyNoPeers();
void VerifyRoutes(int count);
void VerifyRibOutCreationCompletion();
void VerifyXmppRouteNextHops();
bool SendUpdate(BgpPeerTest *peer, const uint8_t *msg, size_t msgsize);
bool IsReady(bool ready);
bool MpNlriAllowed(BgpPeerTest *peer, uint16_t afi, uint8_t safi);
EventManager evm_;
ServerThread thread_;
boost::scoped_ptr<BgpServerTest> server_;
XmppServerTest *xmpp_server_;
boost::scoped_ptr<BgpXmppChannelManagerMock> channel_manager_;
scoped_ptr<BgpInstanceConfigTest> master_cfg_;
RoutingInstance *rtinstance_;
std::list<BgpNullPeer *> peers_;
std::list<test::NetworkAgentMock *> xmpp_agents_;
std::list<BgpXmppChannel *> xmpp_peers_;
int n_families_;
std::vector<Address::Family> familes_;
int n_instances_;
int n_peers_;
int n_routes_;
int n_agents_;
int n_targets_;
bool xmpp_close_from_control_node_;
};
void BgpPeerCloseTest::VerifyPeer(BgpServerTest *server,
RoutingInstance *rtinstance,
BgpNullPeer *npeer, BgpPeerTest *peer) {
EXPECT_EQ((server)->FindPeer(rtinstance->name().c_str(), npeer->name_),
peer);
}
void BgpPeerCloseTest::VerifyPeers() {
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
VerifyPeer(server_.get(), rtinstance_, npeer, npeer->peer());
}
}
void BgpPeerCloseTest::VerifyNoPeers() {
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
VerifyPeer(server_.get(), rtinstance_, npeer,
static_cast<BgpPeerTest *>(NULL));
}
}
void BgpPeerCloseTest::VerifyRoutes(int count) {
if (!n_peers_) return;
for (int i = 0; i < n_families_; i++) {
BgpTable *tb = rtinstance_->GetTable(familes_[i]);
if (count && n_agents_ && n_peers_ &&
familes_[i] == Address::INETVPN) {
BGP_VERIFY_ROUTE_COUNT(tb, (n_instances_ + 1) * count);
} else {
BGP_VERIFY_ROUTE_COUNT(tb, count);
}
}
}
void BgpPeerCloseTest::VerifyRibOutCreationCompletion() {
WaitForIdle();
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
for (int i = 0; i < n_families_; i++) {
EXPECT_TRUE(npeer->ribout_creation_complete(familes_[i]));
}
}
}
string BgpPeerCloseTest::GetConfig() {
ostringstream out;
out <<
"<config>\
<bgp-router name=\'A\'>\
<identifier>192.168.0.1</identifier>\
<address>127.0.0.1</address>\
<port>" << server_->session_manager()->GetPort() << "</port>\
<session to=\'B\'>\
<address-families>\
<family>inet-vpn</family>\
<family>e-vpn</family>\
<family>erm-vpn</family>\
<family>route-target</family>\
</address-families>\
</session>\
</bgp-router>\
";
for (int i = 1; i <= n_instances_; i++) {
out << "<routing-instance name='instance" << i << "'>\n";
for (int j = 1; j <= n_targets_; j++) {
out << " <vrf-target>target:1:" << j << "</vrf-target>\n";
}
out << "</routing-instance>\n";
}
out << "</config>";
BGP_DEBUG_UT("Applying config" << out.str());
return out.str();
}
bool BgpPeerCloseTest::SendUpdate(BgpPeerTest *peer, const uint8_t *msg,
size_t msgsize) {
return true;
}
bool BgpPeerCloseTest::IsReady(bool ready) {
return ready;
}
bool BgpPeerCloseTest::MpNlriAllowed(BgpPeerTest *peer, uint16_t afi,
uint8_t safi) {
if (afi == BgpAf::IPv4 && safi == BgpAf::Unicast) {
return true;
}
if (afi == BgpAf::IPv4 && safi == BgpAf::Vpn) {
return true;
}
if (afi == BgpAf::L2Vpn && safi == BgpAf::EVpn) {
return true;
}
return false;
}
BgpAttr *BgpPeerCloseTest::CreatePathAttr() {
BgpAttrSpec attr_spec;
BgpAttrDB *db = server_->attr_db();
BgpAttr *attr = new BgpAttr(db, attr_spec);
attr->set_origin(BgpAttrOrigin::IGP);
attr->set_med(5);
attr->set_local_pref(10);
AsPathSpec as_path;
AsPathSpec::PathSegment *ps = new AsPathSpec::PathSegment;
ps->path_segment_type = AsPathSpec::PathSegment::AS_SEQUENCE;
ps->path_segment.push_back(20);
ps->path_segment.push_back(30);
as_path.path_segments.push_back(ps);
attr->set_as_path(&as_path);
return attr;
}
ExtCommunitySpec *BgpPeerCloseTest::CreateRouteTargets() {
auto_ptr<ExtCommunitySpec> commspec(new ExtCommunitySpec());
for (int i = 1; i <= n_targets_; i++) {
RouteTarget tgt = RouteTarget::FromString(
"target:1:" + boost::lexical_cast<string>(i));
const ExtCommunity::ExtCommunityValue &extcomm =
tgt.GetExtCommunity();
uint64_t value = get_value(extcomm.data(), extcomm.size());
commspec->communities.push_back(value);
}
if (commspec->communities.empty()) return NULL;
return commspec.release();
}
void BgpPeerCloseTest::AddRoutes(BgpTable *table, BgpNullPeer *npeer) {
DBRequest req;
boost::scoped_ptr<ExtCommunitySpec> commspec;
boost::scoped_ptr<BgpAttrLocalPref> local_pref;
IPeer *peer = npeer->peer();
#if 0
InetVpnPrefix vpn_prefix(InetVpnPrefix::FromString(
"123:456:192.168.255.0/32"));
#endif
Ip4Prefix prefix(Ip4Prefix::FromString("192.168.255.0/24"));
InetVpnPrefix vpn_prefix(InetVpnPrefix::FromString("123:456:10." +
boost::lexical_cast<string>(npeer->peer_id()) + ".1.1/32"));
for (int rt = 0; rt < n_routes_; rt++,
prefix = task_util::Ip4PrefixIncrement(prefix),
vpn_prefix = task_util::InetVpnPrefixIncrement(vpn_prefix)) {
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
int localpref = 1 + (std::rand() % (n_peers_ + n_agents_));
localpref = 100; // Default preference
BgpAttrSpec attr_spec;
local_pref.reset(new BgpAttrLocalPref(localpref));
attr_spec.push_back(local_pref.get());
BgpAttrNextHop nexthop(0x7f010000 + npeer->peer_id());
attr_spec.push_back(&nexthop);
TunnelEncap tun_encap(std::string("gre"));
switch (table->family()) {
case Address::INET:
commspec.reset(new ExtCommunitySpec());
commspec->communities.push_back(get_value(tun_encap.GetExtCommunity().begin(), 8));
attr_spec.push_back(commspec.get());
req.key.reset(new InetTable::RequestKey(prefix, peer));
break;
case Address::INETVPN:
req.key.reset(new InetVpnTable::RequestKey(vpn_prefix, peer));
commspec.reset(CreateRouteTargets());
if (!commspec.get()) {
commspec.reset(new ExtCommunitySpec());
}
commspec->communities.push_back(get_value(tun_encap.GetExtCommunity().begin(), 8));
attr_spec.push_back(commspec.get());
break;
default:
assert(0);
break;
}
uint32_t label = 20000 + rt;
BgpAttrPtr attr = server_->attr_db()->Locate(attr_spec);
req.data.reset(new InetTable::RequestData(attr, 0, label));
table->Enqueue(&req);
}
}
void BgpPeerCloseTest::AddAllRoutes() {
RibExportPolicy policy(BgpProto::IBGP, RibExportPolicy::BGP, 1, 0);
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
for (int i = 0; i < n_families_; i++) {
BgpTable *table = rtinstance_->GetTable(familes_[i]);
server_->membership_mgr()->Register(npeer->peer(), table, policy,
-1, boost::bind(&BgpPeerCloseTest::CreateRibsDone, this, _1,
_2, npeer));
// Add routes to RibIn
AddRoutes(table, npeer);
}
npeer->peer()->set_vpn_tables_registered(true);
}
WaitForIdle();
}
void BgpPeerCloseTest::AddXmppPeersWithRoutes() {
if (!n_agents_) return;
CreateAgents();
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
TASK_UTIL_EXPECT_EQ(true, agent->IsEstablished());
}
WaitForIdle();
Subscribe();
VerifyXmppRoutes(0);
AddXmppRoutes();
WaitForIdle();
VerifyXmppRoutes(n_instances_ * n_routes_);
}
void BgpPeerCloseTest::CreateAgents() {
Ip4Prefix prefix(Ip4Prefix::FromString("127.0.0.1/32"));
for (int i = 0; i < n_agents_; i++) {
// create an XMPP client in server A
xmpp_agents_.push_back(new test::NetworkAgentMock(&evm_,
"agent" + boost::lexical_cast<string>(i) +
"@vnsw.contrailsystems.com",
xmpp_server_->GetPort(),
prefix.ip4_addr().to_string()));
WaitForIdle();
TASK_UTIL_EXPECT_NE_MSG(static_cast<BgpXmppChannel *>(NULL),
channel_manager_->channel_,
"Waiting for channel_manager_->channel_ to be set");
xmpp_peers_.push_back(channel_manager_->channel_);
channel_manager_->channel_ = NULL;
prefix = task_util::Ip4PrefixIncrement(prefix);
}
}
void BgpPeerCloseTest::Subscribe() {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
agent->Subscribe(BgpConfigManager::kMasterInstance, -1);
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
agent->Subscribe(instance_name, i);
}
}
WaitForIdle();
}
void BgpPeerCloseTest::UnSubscribe() {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
agent->Unsubscribe(BgpConfigManager::kMasterInstance);
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
agent->Unsubscribe(instance_name);
}
}
VerifyXmppRoutes(0);
WaitForIdle();
}
void BgpPeerCloseTest::AddXmppRoutes() {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
Ip4Prefix prefix(Ip4Prefix::FromString(
"10." + boost::lexical_cast<string>(i) + ".1.1/32"));
for (int rt = 0; rt < n_routes_; rt++,
prefix = task_util::Ip4PrefixIncrement(prefix)) {
agent->AddRoute(instance_name, prefix.ToString());
}
}
}
}
void BgpPeerCloseTest::DeleteXmppRoutes() {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
Ip4Prefix prefix(Ip4Prefix::FromString(
"10." + boost::lexical_cast<string>(i) + ".1.1/32"));
for (int rt = 0; rt < n_routes_; rt++,
prefix = task_util::Ip4PrefixIncrement(prefix)) {
agent->DeleteRoute(instance_name, prefix.ToString());
}
}
}
WaitForIdle();
// VerifyXmppRoutes(0);
}
void BgpPeerCloseTest::VerifyXmppRoutes(int routes) {
if (!n_agents_) return;
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
TASK_UTIL_EXPECT_EQ_MSG(routes, agent->RouteCount(instance_name),
"Wait for routes in " + instance_name);
ASSERT_TRUE(agent->RouteCount(instance_name) == routes);
}
}
WaitForIdle();
}
void BgpPeerCloseTest::VerifyXmppRouteNextHops() {
if (!n_agents_ || n_agents_ != n_peers_) return;
int agent_id = 0;
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
agent_id++;
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
TASK_UTIL_EXPECT_EQ_MSG(n_routes_, agent->RouteCount(instance_name),
"Wait for routes in " + instance_name);
for (int rt = 0; rt < n_routes_; rt++) {
string prefix =
"10." + boost::lexical_cast<string>(agent_id) + ".1.1/32";
//
// We expect two next-hops, one from the agent and one from
// the bgp peer
//
const test::NetworkAgentMock::RouteEntry *route;
TASK_UTIL_EXPECT_TRUE(
(route = agent->RouteLookup(instance_name, prefix)) &&
route->entry.next_hops.next_hop.size() == 2);
}
}
}
WaitForIdle();
}
void BgpPeerCloseTest::DeleteAllRoutingInstances() {
ostringstream out;
out << "<delete>";
for (int i = 1; i <= n_instances_; i++) {
out << "<routing-instance name='instance" << i << "'>\n";
for (int j = 1; j <= n_targets_; j++) {
out << " <vrf-target>target:1:" << j << "</vrf-target>\n";
}
out << "</routing-instance>\n";
}
out << "</delete>";
server_->Configure(out.str().c_str());
WaitForIdle();
}
void BgpPeerCloseTest::VerifyRoutingInstances() {
for (int i = 1; i <= n_instances_; i++) {
string instance_name = "instance" + boost::lexical_cast<string>(i);
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
server_->routing_instance_mgr()->\
GetRoutingInstance(instance_name));
}
//
// Verify 'default' master routing-instance
//
TASK_UTIL_EXPECT_NE(static_cast<RoutingInstance *>(NULL),
server_->routing_instance_mgr()->GetRoutingInstance(
BgpConfigManager::kMasterInstance));
}
void BgpPeerCloseTest::AddPeersWithRoutes(
const BgpInstanceConfig *instance_config) {
Configure();
SetPeerCloseGraceful(false);
//
// Add XmppPeers with routes as well
//
AddXmppPeersWithRoutes();
for (int p = 1; p <= n_peers_; p++) {
ostringstream oss;
oss << "NullPeer" << p;
BgpNullPeer *npeer =
new BgpNullPeer(server_.get(), instance_config, oss.str(),
rtinstance_, p);
VerifyPeer(server_.get(), rtinstance_, npeer, npeer->peer());
// Override certain default routines to customize behavior that we want
// in this test.
npeer->peer()->SendUpdate_fnc_ =
boost::bind(&BgpPeerCloseTest::SendUpdate, this, npeer->peer(),
_1, _2);
npeer->peer()->MpNlriAllowed_fnc_ =
boost::bind(&BgpPeerCloseTest::MpNlriAllowed, this, npeer->peer(),
_1, _2);
npeer->peer()->IsReady_fnc_ =
boost::bind(&BgpPeerCloseTest::IsReady, this, true);
peers_.push_back(npeer);
}
AddAllRoutes();
VerifyXmppRouteNextHops();
}
void BgpPeerCloseTest::CallStaleTimer(bool bgp_peers_ready) {
// Invoke stale timer callbacks as evm is not running in this unit test
BOOST_FOREACH(BgpNullPeer *peer, peers_) {
peer->peer()->IsReady_fnc_ =
boost::bind(&BgpPeerCloseTest::IsReady, this, bgp_peers_ready);
peer->peer()->peer_close()->close_manager()->StaleTimerCallback();
}
BOOST_FOREACH(BgpXmppChannel *peer, xmpp_peers_) {
peer->Peer()->peer_close()->close_manager()->StaleTimerCallback();
}
WaitForIdle();
}
void BgpPeerCloseTest::XmppPeerClose() {
if (xmpp_close_from_control_node_) {
BOOST_FOREACH(BgpXmppChannel *peer, xmpp_peers_) {
peer->Peer()->Close();
}
} else {
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
// agent->client()->Shutdown();
agent->SessionDown();
}
}
WaitForIdle();
BOOST_FOREACH(test::NetworkAgentMock *agent, xmpp_agents_) {
TASK_UTIL_EXPECT_TRUE(!agent->IsEstablished());
}
}
void BgpPeerCloseTest::InitParams() {
n_instances_ = ::std::tr1::get<0>(GetParam());
n_routes_ = ::std::tr1::get<1>(GetParam());
n_peers_ = ::std::tr1::get<2>(GetParam());
n_agents_ = ::std::tr1::get<3>(GetParam());
n_targets_ = ::std::tr1::get<4>(GetParam());
xmpp_close_from_control_node_ = ::std::tr1::get<5>(GetParam());
}
// Peer flaps
//
// Run all tests with n peers, n routes, and address-families combined
//
// 1. Close with RibIn delete
// 2. Close with RibIn Stale, followed by RibIn Delete
// 3. Close with RibIn Stale, followed by RibIn Sweep
//
// Config deletions
//
// 1. Peer(s) deletion from config
// 2. Routing Instance(s) deletion from config
// 3. Delete entire bgp routing config
//
//
// Config modifications
//
// 1. Modify router-id
// 2. Toggle graceful restart options for address families selectively
// 3. Modify graceful restart timer values for address families selectively
// 4. Neighbor inbound policy channge (In future..)
//
// 1. Repeated close in each of the above case before the close is complete
// 2. Repeated close in each of the above case after the close is complete (?)
TEST_P(BgpPeerCloseTest, ClosePeers) {
SCOPED_TRACE(__FUNCTION__);
InitParams();
AddPeersWithRoutes(master_cfg_.get());
VerifyPeers();
VerifyRoutes(n_routes_);
VerifyRibOutCreationCompletion();
// BgpShow::Instance(server_.get());
// Trigger ribin deletes
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
npeer->peer()->SetAdminState(true);
}
XmppPeerClose();
// Assert that all ribins for all families have been deleted correctly
VerifyPeers();
VerifyRoutes(0);
}
TEST_P(BgpPeerCloseTest, DeletePeers) {
SCOPED_TRACE(__FUNCTION__);
InitParams();
AddPeersWithRoutes(master_cfg_.get());
WaitForIdle();
VerifyPeers();
VerifyRoutes(n_routes_);
VerifyRibOutCreationCompletion();
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
npeer->peer()->ManagedDelete();
}
XmppPeerClose();
// Assert that all ribins have been deleted correctly
VerifyNoPeers();
VerifyRoutes(0);
}
TEST_P(BgpPeerCloseTest, DeleteXmppRoutes) {
SCOPED_TRACE(__FUNCTION__);
InitParams();
AddPeersWithRoutes(master_cfg_.get());
WaitForIdle();
VerifyPeers();
VerifyRoutes(n_routes_);
VerifyRibOutCreationCompletion();
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
npeer->peer()->ManagedDelete();
}
DeleteXmppRoutes();
// Assert that all ribins have been deleted correctly
VerifyNoPeers();
VerifyRoutes(0);
}
TEST_P(BgpPeerCloseTest, Unsubscribe) {
SCOPED_TRACE(__FUNCTION__);
InitParams();
AddPeersWithRoutes(master_cfg_.get());
WaitForIdle();
VerifyPeers();
VerifyRoutes(n_routes_);
VerifyRibOutCreationCompletion();
BOOST_FOREACH(BgpNullPeer *npeer, peers_) {
npeer->peer()->ManagedDelete();
}
UnSubscribe();
// Assert that all ribins have been deleted correctly
VerifyNoPeers();
VerifyRoutes(0);
}
TEST_P(BgpPeerCloseTest, DeleteRoutingInstances) {
SCOPED_TRACE(__FUNCTION__);