/
test_kstate.cc
488 lines (421 loc) · 15.7 KB
/
test_kstate.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/os.h"
#include "test/test_init.h"
#include "test/test_cmn_util.h"
#include "kstate/test/test_kstate.h"
#include "oper/mirror_table.h"
#include "oper/tunnel_nh.h"
#include "xmpp/test/xmpp_test_util.h"
#include "ksync/ksync_sock_user.h"
#define vm1_ip "1.1.1.1"
#define vm2_ip "2.1.1.1"
#define vm3_ip "3.1.1.1"
#define vm4_ip "4.1.1.1"
#define vm5_ip "5.1.1.1"
#define SIP 0x01010101
#define DIP 0x01010102
#define SPORT 20130
#define DPORT 20131
#define MAX_VNET 1
int fd_table[MAX_VNET];
#define MAX_TEST_FD 5
#define MAX_TEST_MPLS 11
int test_fd[MAX_TEST_FD];
TestIfKState *TestIfKState::singleton_;
TestNHKState *TestNHKState::singleton_;
TestMplsKState *TestMplsKState::singleton_;
TestMirrorKState *TestMirrorKState::singleton_;
TestRouteKState *TestRouteKState::singleton_;
TestFlowKState *TestFlowKState::singleton_;
int TestKStateBase::handler_count_;
int TestKStateBase::fetched_count_;
struct PortInfo input[] = {
{"test0", 5, vm1_ip, "00:00:00:01:01:01", 3, 1},
{"test1", 6, vm2_ip, "00:00:00:02:02:02", 3, 2},
{"test2", 7, vm3_ip, "00:00:00:01:01:03", 3, 3},
{"test3", 8, vm4_ip, "00:00:00:02:02:04", 3, 4},
{"test4", 9, vm5_ip, "00:00:00:02:02:05", 3, 5},
};
std::string analyzer = "TestAnalyzer";
void RouterIdDepInit(Agent *agent) {
}
class KStateTest : public ::testing::Test {
public:
static void TestSetup(bool ksync_init) {
ksync_init_ = ksync_init;
//Physical interface name specified in default config file is vnet0.
//Create this if it is in ksync_init mode
if (ksync_init_) {
CreateTapInterfaces("vnet", MAX_VNET, fd_table);
client->WaitForIdle(2);
CreateTapInterfaces("test", MAX_TEST_FD, test_fd);
client->WaitForIdle(2);
}
//To disable flow aging set the flow age time to high value
Agent::GetInstance()->flow_stats_collector()->UpdateFlowAgeTime(1000000 * 60 * 10);
VxLanNetworkIdentifierMode(false);
client->WaitForIdle();
}
static void TestTearDown() {
if (ksync_init_) {
DeleteTapIntf(fd_table, MAX_VNET);
client->WaitForIdle(2);
DeleteTapIntf(test_fd, MAX_TEST_FD);
client->WaitForIdle(2);
}
}
void WaitForVrf(struct PortInfo *input, int idx, bool created) {
char vrf_name[80];
sprintf(vrf_name, "vrf%d", input[idx].vn_id);
WAIT_FOR(1000, 1000, (VrfFind(vrf_name) == created));
}
void CreateVmPorts(struct PortInfo *input, int count) {
CreateVmportEnv(input, count);
}
void CreatePorts(uint32_t if_count, uint32_t nh_count, uint32_t rt_count,
uint32_t num_ports = MAX_TEST_FD) {
int idx;
client->Reset();
CreateVmPorts(input, num_ports);
client->WaitForIdle(10);
for (uint32_t i = 0; i < num_ports; i++) {
idx = i;
WAIT_FOR(1000, 1000, (VmPortActive(input, idx) == true));
}
WAIT_FOR(1000, 1000, ((unsigned int)num_ports == Agent::GetInstance()->vm_table()->Size()));
WAIT_FOR(1000, 1000, (1 == Agent::GetInstance()->vn_table()->Size()));
WaitForVrf(input, 0, true);
if (if_count) {
unsigned int oper_if_count = num_ports + if_count;
WAIT_FOR(1000, 1000, ((oper_if_count) ==
Agent::GetInstance()->interface_table()->Size()));
}
//One EVPN label for VN
WAIT_FOR(1000, 1000, (((num_ports * 2) + 1 ==
(Agent::GetInstance()->mpls_table()->Size()))));
if (!ksync_init_) {
//One EVPN label for VN
WAIT_FOR(1000, 1000, ((num_ports * 2) + 1 ==
(uint32_t)(KSyncSockTypeMap::MplsCount())));
if (if_count) {
WAIT_FOR(1000, 1000, ((num_ports + if_count) ==
(uint32_t)KSyncSockTypeMap::IfCount()));
}
if (nh_count) {
//5 interface nexthops get created for each interface
//(l2 with policy, l2 without policy, l3 with policy, l3
// without policy and 1 multicast - mac as all f's)
//plus 4 Nexthops for each VRF (1 VRF NH and 3 Composite NHs
//i.e. TOR CNH, EVPN CNH, Fabric CNH)
WAIT_FOR(1000, 1000, ((nh_count + (num_ports * 5) + 4) ==
(uint32_t)KSyncSockTypeMap::NHCount()));
}
if (rt_count) {
WAIT_FOR(1000, 1000, ((rt_count + (num_ports * 2) + 1) ==
(uint32_t)KSyncSockTypeMap::RouteCount()));
}
}
}
void DeletePorts(int num_ports = MAX_TEST_FD) {
DeleteVmportEnv(input, num_ports, true);
client->WaitForIdle(10);
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->vm_table()->Size()));
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->vn_table()->Size()));
WaitForVrf(input, 0, false);
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->mpls_table()->Size()));
}
void CreatePortsWithPolicy() {
int idx;
client->Reset();
CreateVmportEnv(input, MAX_TEST_FD, 1);
client->WaitForIdle(10);
for (int i = 0; i < MAX_TEST_FD; i++) {
idx = i;
WAIT_FOR(1000, 1000, (VmPortActive(input, idx) == true));
WAIT_FOR(1000, 1000, (VmPortPolicyEnable(input, idx) == true));
}
WAIT_FOR(1000, 1000, (MAX_TEST_FD == Agent::GetInstance()->vm_table()->Size()));
WAIT_FOR(1000, 1000, (1 == Agent::GetInstance()->vn_table()->Size()));
WaitForVrf(input, 0, true);
WAIT_FOR(1000, 1000, ((MAX_TEST_FD + 3) == Agent::GetInstance()->interface_table()->Size()));
if (!ksync_init_) {
WAIT_FOR(1000, 1000, ((MAX_TEST_FD + 3) == KSyncSockTypeMap::IfCount()));
}
}
void DeletePortsWithPolicy() {
int idx;
client->Reset();
DeleteVmportEnv(input, MAX_TEST_FD, true, 1);
client->WaitForIdle(5);
client->PortDelNotifyWait(MAX_TEST_FD);
for (int i = 0; i < MAX_TEST_FD; i++) {
idx = i;
WAIT_FOR(1000, 1000, (VmPortFind(input, idx) == false));
}
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->vm_table()->Size()));
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->vn_table()->Size()));
WaitForVrf(input, 0, false);
WAIT_FOR(1000, 1000, (3 == Agent::GetInstance()->interface_table()->Size()));
}
void CreateMirrorEntry() {
Ip4Address sip(SIP);
Ip4Address dip(DIP);
//Create Mirror entry
MirrorTable::AddMirrorEntry(analyzer, Agent::GetInstance()->fabric_vrf_name(),
sip, SPORT, dip, DPORT);
client->WaitForIdle(2);
//Verify mirror NH is created
MirrorNHKey key(Agent::GetInstance()->fabric_vrf_name(), sip, SPORT, dip, DPORT);
WAIT_FOR(1000, 1000, (Agent::GetInstance()->nexthop_table()->FindActiveEntry(&key) != NULL));
}
void DeleteMirrorEntry() {
Ip4Address sip(SIP);
Ip4Address dip(DIP);
MirrorTable::DelMirrorEntry(analyzer);
client->WaitForIdle(2);
//Verify mirror NH is deleted
MirrorNHKey key(Agent::GetInstance()->fabric_vrf_name(), sip, SPORT, dip, DPORT);
WAIT_FOR(1000, 1000, (Agent::GetInstance()->nexthop_table()->FindActiveEntry(&key) == NULL));
}
void SetUp() {
agent_ = Agent::GetInstance();
}
Agent *agent_;
static bool ksync_init_;
};
bool KStateTest::ksync_init_;
TEST_F(KStateTest, IfDumpTest) {
int if_count = 0;
TestIfKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
if_count = TestKStateBase::fetched_count_;
LOG(DEBUG, "if count " << if_count);
CreatePorts(if_count, 0, 0);
TestIfKState::Init(-1, true, if_count + MAX_TEST_FD);
client->WaitForIdle();
client->KStateResponseWait(1);
DeletePorts();
}
TEST_F(KStateTest, IfGetTest) {
int if_count = 0;
TestIfKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
if_count = TestKStateBase::fetched_count_;
LOG(DEBUG, "if count " << if_count);
CreatePorts(if_count, 0, 0);
for (int i = 0; i < MAX_TEST_FD; i++) {
TestIfKState::Init(if_count + i);
client->WaitForIdle();
client->KStateResponseWait(1);
}
DeletePorts();
}
TEST_F(KStateTest, NHDumpTest) {
int nh_count = 0;
TestNHKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
nh_count = TestKStateBase::fetched_count_;
LOG(DEBUG, "nh count " << nh_count);
int max_ports = 2;
CreatePorts(0, nh_count, 0, max_ports);
//5 interface nexthops get created for each interface
//(l2 with policy, l2 without policy, l3 with policy, l3 without policy
// and 1 multicast - mac as all f's )
//plus 4 Nexthops for each VRF (1 VRF NH and 3 Composite NHs)
TestNHKState::Init(-1, true, nh_count + (max_ports * 5) + 4);
client->WaitForIdle();
client->KStateResponseWait(1);
DeletePorts(max_ports);
}
TEST_F(KStateTest, NHGetTest) {
int nh_count = 0;
TestNHKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
nh_count = TestKStateBase::fetched_count_;
LOG(DEBUG, "nh count " << nh_count);
CreatePorts(0, nh_count, 0);
int count = 0;
//Two interface nexthops get created for each interface (with and without policy)
for (int i = 0; count < (MAX_TEST_FD * 2); i++) {
uint32_t nh_id = nh_count + i;
/* Check whether nexthop-index is valid by checking it in oper db before doing
* Get from mock kernel */
NextHop *nh = Agent::GetInstance()->nexthop_table()->FindNextHop(nh_id);
if (nh) {
TestNHKState::Init(nh_id);
client->WaitForIdle();
client->KStateResponseWait(1);
count++;
}
}
DeletePorts();
}
TEST_F(KStateTest, MplsDumpTest) {
int mpls_count = 0;
TestMplsKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
mpls_count = TestKStateBase::fetched_count_;
CreatePorts(0, 0, 0);
CreatePorts(0, 0, 0);
TestMplsKState::Init(-1, true, mpls_count + MAX_TEST_MPLS);
client->WaitForIdle(3);
client->KStateResponseWait(1);
DeletePorts();
}
TEST_F(KStateTest, MplsGetTest) {
int mpls_count = 0;
TestMplsKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
mpls_count = TestKStateBase::fetched_count_;
uint32_t old_vn_count = agent_->vn_table()->Size();
CreatePorts(0, 0, 0);
uint32_t vn_count = agent_->vn_table()->Size() - old_vn_count;
for (int i = 0; i < MAX_TEST_FD; i++) {
//TODO Why this 8 needed?
TestMplsKState::Init(MplsTable::kStartLabel + mpls_count + vn_count +
i + 8);
client->WaitForIdle();
client->KStateResponseWait(1);
}
DeletePorts();
}
TEST_F(KStateTest, MirrorNHGetTest) {
unsigned int nh_count = 0;
TestNHKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
nh_count = TestKStateBase::fetched_count_;
LOG(DEBUG, "nh count " << nh_count);
//Create mirror entry
CreateMirrorEntry();
//Verify that NH table size is increased by 1
EXPECT_EQ(Agent::GetInstance()->nexthop_table()->Size(), nh_count + 1);
//Verify the get of Mirror NH
TestNHKState::Init(nh_count);
client->WaitForIdle();
client->KStateResponseWait(1);
DeleteMirrorEntry();
}
TEST_F(KStateTest, MirrorDumpTest) {
int mirror_count = 0;
TestMirrorKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
mirror_count = TestKStateBase::fetched_count_;
CreateMirrorEntry();
TestMirrorKState::Init(-1, true, mirror_count + 1);
client->WaitForIdle();
client->KStateResponseWait(1);
DeleteMirrorEntry();
}
TEST_F(KStateTest, MirrorGetTest) {
int mirror_count = 0;
TestMirrorKState::Init();
client->WaitForIdle();
client->KStateResponseWait(1);
mirror_count = TestKStateBase::fetched_count_;
CreateMirrorEntry();
TestMirrorKState::Init(mirror_count);
client->WaitForIdle();
client->KStateResponseWait(1);
DeleteMirrorEntry();
}
TEST_F(KStateTest, RouteDumpTest) {
if (!ksync_init_) {
int rt_count = 0, prev_rt_count;
TestRouteKState::Init(false);
client->WaitForIdle();
client->KStateResponseWait(1);
rt_count = 0;
prev_rt_count = KSyncSockTypeMap::RouteCount();
CreatePorts(0, 0, rt_count);
//Default
//Addition of 2 vm ports in a new VN (VRF) will result in the following routes
// 2 routes corresponding to the addresses of VM
// l2 broadcast
// 2 - v6 host route for new vrf addition
TestRouteKState::Init(true, prev_rt_count + (MAX_TEST_FD * 2) + 3);
client->WaitForIdle();
client->KStateResponseWait(1);
DeletePorts();
}
}
TEST_F(KStateTest, FlowDumpTest_invalid_index) {
/* send invalid index to flowkstate */
TestFlowKState::Init(true, -2, 0);
client->WaitForIdle();
client->KStateResponseWait(0);
}
TEST_F(KStateTest, DISABLED_FlowDumpTest) {
EXPECT_EQ(0U, Agent::GetInstance()->pkt()->flow_table()->Size());
TestFlowKState::Init(true, -1, 0);
client->WaitForIdle();
client->KStateResponseWait(0);
CreatePortsWithPolicy();
VmInterface *test0, *test1;
test0 = VmInterfaceGet(input[0].intf_id);
assert(test0);
test1 = VmInterfaceGet(input[1].intf_id);
assert(test1);
int hash_id = 1;
//Flow creation using IP packet
TxIpPacketUtil(test0->id(), vm1_ip, vm2_ip, 0, hash_id);
client->WaitForIdle(2);
EXPECT_TRUE(FlowGet("vrf3", vm1_ip, vm2_ip, 0, 0, 0, false,
"vn3", "vn3", hash_id++,
test0->flow_key_nh()->id()));
//Create flow in reverse direction
TxIpPacketUtil(test1->id(), vm2_ip, vm1_ip, 0, hash_id);
client->WaitForIdle(2);
EXPECT_TRUE(FlowGet("vrf3", vm2_ip, vm1_ip, 0, 0, 0, true,
"vn3", "vn3", hash_id++,
test1->flow_key_nh()->id(),
test0->flow_key_nh()->id()));
//Flow creation using TCP packet
TxTcpPacketUtil(test0->id(), vm1_ip, vm2_ip, 1000, 200,
hash_id);
client->WaitForIdle(2);
EXPECT_TRUE(FlowGet("vrf3", vm1_ip, vm2_ip, 6, 1000, 200, false,
"vn3", "vn3", hash_id++,
test0->flow_key_nh()->id()));
//Create flow in reverse direction and make sure it is linked to previous flow
TxTcpPacketUtil(test1->id(), vm2_ip, vm1_ip, 200, 1000,
hash_id);
client->WaitForIdle(2);
EXPECT_TRUE(FlowGet("vrf3", vm2_ip, vm1_ip, 6, 200, 1000, true,
"vn3", "vn3", hash_id++,
test1->flow_key_nh()->id(),
test0->flow_key_nh()->id()));
EXPECT_EQ(4U, Agent::GetInstance()->pkt()->flow_table()->Size());
TestFlowKState::Init(true, -1, 6);
client->WaitForIdle();
client->KStateResponseWait(1);
//cleanup
client->EnqueueFlowFlush();
client->WaitForIdle(2);
WAIT_FOR(1000, 1000, (0 == Agent::GetInstance()->pkt()->flow_table()->Size()));
DeletePortsWithPolicy();
}
int main(int argc, char *argv[]) {
int ret;
GETUSERARGS();
/* Supported only with non-ksync mode for now */
ksync_init = false;
client = TestInit(init_file, ksync_init, true, true);
KStateTest::TestSetup(ksync_init);
::testing::InitGoogleTest(&argc, argv);
ret = RUN_ALL_TESTS();
KStateTest::TestTearDown();
client->WaitForIdle();
TestShutdown();
delete client;
return ret;
}