/
ribout_attributes_test.cc
327 lines (280 loc) · 10 KB
/
ribout_attributes_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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/test/task_test_util.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_ribout.h"
#include "bgp/bgp_server.h"
#include "bgp/extended-community/mac_mobility.h"
#include "bgp/inet/inet_route.h"
#include "control-node/control_node.h"
class BgpPeerMock : public IPeer {
public:
virtual std::string ToString() const { return "test-peer"; }
virtual std::string ToUVEKey() const { return "test-peer"; }
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 0; }
virtual const std::string GetStateName() const { return "UNKNOWN"; }
virtual void UpdateRefCount(int count) const { }
virtual tbb::atomic<int> GetRefCount() const {
tbb::atomic<int> count;
count = 0;
return count;
}
virtual void UpdatePrimaryPathCount(int count) const { }
virtual int GetPrimaryPathCount() const { return 0; }
private:
};
class RibOutAttributesTest : public ::testing::Test {
protected:
RibOutAttributesTest()
: server_(&evm_) {
}
void TearDown() {
server_.Shutdown();
task_util::WaitForIdle();
}
EventManager evm_;
BgpServer server_;
};
namespace {
TEST_F(RibOutAttributesTest, Paths) {
BgpPeerMock peer1;
BgpPeerMock peer2;
BgpPeerMock peer3;
BgpAttr *attr1;
BgpAttr *attr2;
BgpAttr *attr3;
BgpPath *path1;
BgpPath *path2;
BgpPath *path3;
IpAddress nexthop1(Ip4Address(0x01010101));
IpAddress nexthop2(Ip4Address(0x01010102));
IpAddress nexthop3(Ip4Address(0x01010103));
Ip4Prefix prefix;
AsPathSpec as_path;
BgpAttrSpec spec;
InetRoute route(prefix);
BgpAttrDB *db = server_.attr_db();
attr1 = new BgpAttr(db, spec);
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);
attr1->set_as_path(&as_path);
attr1->set_origin(BgpAttrOrigin::IGP);
attr1->set_med(5);
attr1->set_local_pref(10);
attr1->set_nexthop(nexthop1);
path1 = new BgpPath(&peer1, BgpPath::BGP_XMPP, attr1, 0, 1);
route.InsertPath(path1);
//
// Route has only one path
//
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(1, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
}
//
// Add another ECMP eligible path (same local-pref and as count)
//
attr2 = new BgpAttr(*attr1);
attr2->set_nexthop(nexthop2);
path2 = new BgpPath(&peer2, BgpPath::BGP_XMPP, attr2, 0, 2);
route.InsertPath(path2);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Add another path which is not ECMP eligible
//
attr3 = new BgpAttr(*attr1);
attr3->set_local_pref(5);
attr3->set_nexthop(nexthop3);
path3 = new BgpPath(&peer3, BgpPath::BGP_XMPP, attr3, 0, 3);
route.InsertPath(path3);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Remove ecmp path
//
route.RemovePath(&peer2);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(1, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
}
//
// Add ecmp path again
//
attr2 = new BgpAttr(*attr1);
attr2->set_nexthop(nexthop2);
path2 = new BgpPath(&peer2, BgpPath::BGP_XMPP, attr2, 0, 2);
route.InsertPath(path2);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Remove non ecmp path
//
route.RemovePath(&peer3);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Add another ECMP path with duplicate next-hop/label combination
//
attr3 = new BgpAttr(*attr1);
attr3->set_nexthop(nexthop2);
path3 = new BgpPath(&peer3, BgpPath::BGP_XMPP, attr3, 0, 2);
route.InsertPath(path3);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Remove duplicate ecmp eligible path, att1 and attr3 are left
//
route.RemovePath(&peer2);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_EQ(attr3->nexthop(), ribout_attr.nexthop_list().at(1).address());
}
//
// Remove the best path!
//
route.RemovePath(&peer1);
{
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(1, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr3->nexthop(), ribout_attr.nexthop_list().at(0).address());
}
//
// Remove all 4 paths that have been added so far
//
(void) route.RemovePath(&peer1);
(void) route.RemovePath(&peer2);
(void) route.RemovePath(&peer3);
}
// Higher sequence number wins.
TEST_F(RibOutAttributesTest, SequenceNumber1) {
Ip4Prefix prefix;
InetRoute route(prefix);
BgpAttrLocalPref local_pref1(100);
BgpAttrNextHop nexthop1(0x01010101);
ExtCommunitySpec extcomm_spec1;
MacMobility mm1(101);
extcomm_spec1.communities.push_back(mm1.GetExtCommunityValue());
BgpAttrSpec spec1;
spec1.push_back(&local_pref1);
spec1.push_back(&nexthop1);
spec1.push_back(&extcomm_spec1);
BgpAttrPtr attr1 = server_.attr_db()->Locate(spec1);
BgpPeerMock peer1;
BgpPath *path1 = new BgpPath(&peer1, BgpPath::BGP_XMPP, attr1, 0, 101);
BgpAttrLocalPref local_pref2(100);
BgpAttrNextHop nexthop2(0x01010102);
ExtCommunitySpec extcomm_spec2;
MacMobility mm2(102);
extcomm_spec2.communities.push_back(mm2.GetExtCommunityValue());
BgpAttrSpec spec2;
spec2.push_back(&local_pref2);
spec2.push_back(&nexthop2);
spec2.push_back(&extcomm_spec2);
BgpAttrPtr attr2 = server_.attr_db()->Locate(spec2);
BgpPeerMock peer2;
BgpPath *path2 = new BgpPath(&peer2, BgpPath::BGP_XMPP, attr2, 0, 102);
route.InsertPath(path1);
route.InsertPath(path2);
// RibOutAttr should have only path2.
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(1, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr2->nexthop(), ribout_attr.nexthop_list().at(0).address());
// Cleanup.
route.RemovePath(&peer1);
route.RemovePath(&peer2);
}
// Same sequence numbers result in ECMP.
TEST_F(RibOutAttributesTest, SequenceNumber2) {
Ip4Prefix prefix;
InetRoute route(prefix);
BgpAttrLocalPref local_pref1(100);
BgpAttrNextHop nexthop1(0x01010101);
ExtCommunitySpec extcomm_spec1;
MacMobility mm1(100);
extcomm_spec1.communities.push_back(mm1.GetExtCommunityValue());
BgpAttrSpec spec1;
spec1.push_back(&local_pref1);
spec1.push_back(&nexthop1);
spec1.push_back(&extcomm_spec1);
BgpAttrPtr attr1 = server_.attr_db()->Locate(spec1);
BgpPeerMock peer1;
BgpPath *path1 = new BgpPath(&peer1, BgpPath::BGP_XMPP, attr1, 0, 101);
BgpAttrLocalPref local_pref2(100);
BgpAttrNextHop nexthop2(0x01010102);
ExtCommunitySpec extcomm_spec2;
MacMobility mm2(100);
extcomm_spec2.communities.push_back(mm2.GetExtCommunityValue());
BgpAttrSpec spec2;
spec2.push_back(&local_pref2);
spec2.push_back(&nexthop2);
spec2.push_back(&extcomm_spec2);
BgpAttrPtr attr2 = server_.attr_db()->Locate(spec2);
BgpPeerMock peer2;
BgpPath *path2 = new BgpPath(&peer2, BgpPath::BGP_XMPP, attr2, 0, 102);
route.InsertPath(path1);
route.InsertPath(path2);
// RibOutAttr should have both paths.
RibOutAttr ribout_attr(&route, route.BestPath()->GetAttr(), true);
EXPECT_EQ(2, ribout_attr.nexthop_list().size());
EXPECT_EQ(attr1->nexthop(), ribout_attr.nexthop_list().at(0).address());
EXPECT_NE(ribout_attr.nexthop_list().at(0).address(),
ribout_attr.nexthop_list().at(1).address());
// Cleanup.
route.RemovePath(&peer1);
route.RemovePath(&peer2);
}
} // namespace
static void SetUp() {
bgp_log_test::init();
ControlNode::SetDefaultSchedulingPolicy();
}
static void TearDown() {
task_util::WaitForIdle();
TaskScheduler *scheduler = TaskScheduler::GetInstance();
scheduler->Terminate();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
SetUp();
int result = RUN_ALL_TESTS();
TearDown();
return result;
}