/
test_flowtable.cc
506 lines (417 loc) · 16 KB
/
test_flowtable.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/os.h"
#include "test/test_cmn_util.h"
#include "ksync/ksync_sock_user.h"
#define MAX_VNET 4
void RouterIdDepInit(Agent *agent) {
}
const std::string svn_name("svn");
const std::string dvn_name("dvn");
struct TestFlowKey {
uint32_t vrfid_;
const char *sip_;
const char *dip_;
uint8_t proto_;
uint16_t sport_;
uint16_t dport_;
const std::string *svn_;
const std::string *dvn_;
uint32_t ifindex_;
uint32_t vn_;
uint32_t vm_;
uint32_t nh_;
TestFlowKey(uint32_t vrf, const char *sip, const char *dip, uint8_t proto,
uint16_t sport, uint16_t dport, const std::string &svn,
const std::string &dvn, uint32_t ifindex, uint32_t vn,
uint32_t vm, uint32_t nh) :
vrfid_(vrf), sip_(sip), dip_(dip), proto_(proto), sport_(sport),
dport_(dport), svn_(&svn), dvn_(&dvn), ifindex_(ifindex), vn_(vn),
vm_(vm), nh_(nh) {
}
void InitFlowKey(FlowKey *key) const {
key->nh = nh_;
key->src_addr = Ip4Address::from_string(sip_);
key->dst_addr = Ip4Address::from_string(dip_);
key->protocol = proto_;
key->src_port = sport_;
key->dst_port = dport_;
key->family = key->src_addr.is_v4() ? Address::INET : Address::INET6;
}
};
#define vm_1_1_ip "1.1.1.1"
#define vm_1_2_ip "1.1.1.2"
#define vm_2_1_ip "2.2.2.2"
struct PortInfo tap1[] = {
{"tap-1-1", 1, vm_1_1_ip, "00:00:00:01:01:01", 1, 1},
};
struct PortInfo tap2[] = {
{"tap-1-2", 2, vm_1_2_ip, "00:00:00:01:01:02", 1, 2},
};
struct PortInfo tap3[] = {
{"tap-2-1", 3, vm_2_1_ip, "00:00:00:02:02:01", 2, 3},
};
int hash_id;
class SetupTask;
FlowEntry *FlowInit(TestFlowKey *t) {
FlowKey key;
t->InitFlowKey(&key);
FlowEntry *flow = Agent::GetInstance()->pkt()->flow_table()->Allocate(key);
boost::shared_ptr<PktInfo> pkt_info(new PktInfo(Agent::GetInstance(),
100, 0, 0));
PktFlowInfo info(pkt_info, Agent::GetInstance()->pkt()->flow_table());
PktInfo *pkt = pkt_info.get();
PktControlInfo ctrl;
ctrl.vn_ = VnGet(t->vn_);
ctrl.intf_ = VmPortGet(t->ifindex_);
ctrl.vm_ = VmGet(t->vm_);
flow->InitFwdFlow(&info, pkt, &ctrl, &ctrl);
return flow;
}
static void FlowAdd(FlowEntry *fwd, FlowEntry *rev) {
Agent::GetInstance()->pkt()->flow_table()->Add(fwd, rev);
}
class FlowTableTest : public ::testing::Test {
public:
bool FlowTableWait(int count) {
int i = 100000;
while (i > 0) {
i--;
if (Agent::GetInstance()->pkt()->flow_table()->Size() == (size_t) count) {
return true;
}
client->WaitForIdle();
usleep(100);
}
return (Agent::GetInstance()->pkt()->flow_table()->Size() == (size_t) count);
}
void FlushFlowTable() {
client->EnqueueFlowFlush();
EXPECT_TRUE(FlowTableWait(0));
}
void CreateLocalRoute(const char *vrf, const char *ip,
VmInterface *intf, int label) {
Ip4Address addr = Ip4Address::from_string(ip);
Agent::GetInstance()->fabric_inet4_unicast_table()->
AddLocalVmRouteReq(NULL, vrf, addr, 32, intf->GetUuid(),
intf->vn()->GetName(), label,
SecurityGroupList(), false, PathPreference(),
Ip4Address(0));
client->WaitForIdle();
EXPECT_TRUE(RouteFind(vrf, addr, 32));
}
void CreateRemoteRoute(const char *vrf, const char *remote_vm,
const char *serv, int label, const char *vn) {
Ip4Address addr = Ip4Address::from_string(remote_vm);
Ip4Address gw = Ip4Address::from_string(serv);
Inet4TunnelRouteAdd(NULL, vrf, addr, 32, gw, TunnelType::AllType(),
label, vn, SecurityGroupList(), PathPreference());
client->WaitForIdle();
EXPECT_TRUE(RouteFind(vrf, addr, 32));
}
void DeleteRoute(const char *vrf, const char *ip) {
Ip4Address addr = Ip4Address::from_string(ip);
Agent::GetInstance()->fabric_inet4_unicast_table()->DeleteReq(NULL, vrf, addr, 32, NULL);
client->WaitForIdle();
WAIT_FOR(1000, 1, (RouteFind(vrf, addr, 32) == false));
}
bool FindAcl(const std::list<MatchAclParams> &acl_list,
const AclDBEntry *acl) {
std::list<MatchAclParams>::const_iterator it;
bool found = false;
for (it = acl_list.begin(); it != acl_list.end(); it++) {
if (it->acl.get() == acl) {
found = true;
break;
}
}
return found;
}
bool ValidateFlow(TestFlowKey *t, TestFlowKey *rev, uint32_t action) {
bool ret = true;
FlowKey key;
t->InitFlowKey(&key);
FlowEntry *flow = Agent::GetInstance()->pkt()->flow_table()->Find(key);
EXPECT_TRUE(flow != NULL);
if (flow == NULL) {
return false;
}
FlowEntry *rflow = NULL;
if (rev) {
rev->InitFlowKey(&key);
rflow = Agent::GetInstance()->pkt()->flow_table()->Find(key);
WAIT_FOR(1000, 100, (flow->reverse_flow_entry() == rflow));
if (flow->reverse_flow_entry() != rflow) {
ret = false;
}
}
// Match action
EXPECT_TRUE((flow->match_p().action_info.action & action) != 0);
if ((flow->match_p().action_info.action & action) == 0)
ret = false;
// Match Policy from VN
const VnEntry *vn = flow->data().vn_entry.get();
if (vn && vn->GetAcl() &&
FindAcl(flow->match_p().m_acl_l, vn->GetAcl()) == false) {
EXPECT_STREQ("Policy not found in flow", "");
ret = false;
}
const VmInterface *intf = static_cast<const VmInterface *>
(flow->data().intf_entry.get());
if (intf) {
const VmInterface::SecurityGroupEntrySet sg_list = intf->sg_list().list_;
VmInterface::SecurityGroupEntrySet::const_iterator it;
for (it = sg_list.begin(); it != sg_list.end(); it++) {
if (it->sg_->GetEgressAcl() == NULL &&
it->sg_->GetIngressAcl() == NULL) {
continue;
}
if (FindAcl(flow->match_p().m_sg_acl_l,
(it->sg_->GetEgressAcl())) == false &&
FindAcl(flow->match_p().m_sg_acl_l,
(it->sg_->GetIngressAcl())) == false) {
EXPECT_STREQ("SG not found in flow", "");
ret = false;
}
}
}
if (rflow == NULL) {
return ret;
}
if (!flow->is_flags_set(FlowEntry::LocalFlow)) {
WAIT_FOR(1000, 100, (flow->match_p().m_out_acl_l.size() == 0U));
WAIT_FOR(1000, 100, (flow->match_p().m_out_sg_acl_l.size() == 0U));
if ((flow->match_p().m_out_acl_l.size() != 0) ||
(flow->match_p().m_out_sg_acl_l.size() != 0)) {
ret = false;
}
return ret;
}
vn = rflow->data().vn_entry.get();
if (vn && vn->GetAcl() &&
FindAcl(flow->match_p().m_out_acl_l, vn->GetAcl()) == false){
EXPECT_STREQ("Out Policy not found in flow", "");
ret = false;
}
intf = static_cast<const VmInterface *>
(rflow->data().intf_entry.get());
if (intf) {
const VmInterface::SecurityGroupEntrySet sg_list = intf->sg_list().list_;
VmInterface::SecurityGroupEntrySet::const_iterator it;
for (it = sg_list.begin(); it != sg_list.end(); it++) {
if (it->sg_->GetEgressAcl() == NULL &&
it->sg_->GetIngressAcl() == NULL) {
continue;
}
if (FindAcl(flow->match_p().m_out_sg_acl_l,
it->sg_->GetEgressAcl()) == false &&
FindAcl(flow->match_p().m_out_sg_acl_l,
it->sg_->GetIngressAcl()) == false) {
EXPECT_STREQ("Out SG not found in flow", "");
ret = false;
}
}
}
return ret;
}
static void FlowDel(const TestFlowKey *flow) {
FlowKey key;
flow->InitFlowKey(&key);
Agent::GetInstance()->pkt()->flow_table()->Delete(key, true);
client->WaitForIdle();
}
FlowEntry *FlowInit(TestFlowKey *t) {
FlowKey key;
t->InitFlowKey(&key);
FlowEntry *flow = Agent::GetInstance()->pkt()->flow_table()->Allocate(key);
boost::shared_ptr<PktInfo> pkt_info(new PktInfo(NULL, 0, 0, 0));
pkt_info->family = Address::INET;
PktFlowInfo info(pkt_info, Agent::GetInstance()->pkt()->flow_table());
PktInfo *pkt = pkt_info.get();
PktControlInfo ctrl;
ctrl.vn_ = VnGet(t->vn_);
ctrl.intf_ = VmPortGet(t->ifindex_);
ctrl.vm_ = VmGet(t->vm_);
flow->InitFwdFlow(&info, pkt, &ctrl, &ctrl);
return flow;
}
static void FlowAdd(FlowEntry *fwd, FlowEntry *rev) {
Agent::GetInstance()->pkt()->flow_table()->Add(fwd, rev);
client->WaitForIdle();
}
static void TestSetup(bool ksync_init) {
ksync_init_ = ksync_init;
if (ksync_init_) {
CreateTapInterfaces("flow", MAX_VNET, fd_table);
client->WaitForIdle();
}
hash_id = 1;
client->Reset();
CreateVmportEnv(tap1, 1, 1);
CreateVmportEnv(tap2, 1, 2);
CreateVmportEnv(tap3, 1, 3);
client->WaitForIdle();
EXPECT_TRUE(VmPortActive(tap1, 0));
EXPECT_TRUE(VmPortPolicyEnable(tap1, 0));
EXPECT_TRUE(VmPortActive(tap2, 0));
EXPECT_TRUE(VmPortPolicyEnable(tap2, 0));
EXPECT_TRUE(VmPortActive(tap3, 0));
EXPECT_TRUE(VmPortPolicyEnable(tap3, 0));
WAIT_FOR(1000, 100,
(7U == Agent::GetInstance()->interface_table()->Size()));
WAIT_FOR(1000, 100, (3U == Agent::GetInstance()->vm_table()->Size()));
WAIT_FOR(1000, 100,
(2U == Agent::GetInstance()->vn_table()->Size()));
WAIT_FOR(1000, 100,
(3U == Agent::GetInstance()->interface_config_table()->Size()));
vif1 = VmInterfaceGet(tap1[0].intf_id);
assert(vif1);
vif2 = VmInterfaceGet(tap2[0].intf_id);
assert(vif2);
vif3 = VmInterfaceGet(tap3[0].intf_id);
assert(vif3);
}
static void TestTearDown() {
client->Reset();
DeleteVmportEnv(tap1, 1, true, 1);
DeleteVmportEnv(tap2, 1, true, 2);
DeleteVmportEnv(tap3, 1, true, 3);
FlowTableTest::eth_itf = "eth0";
PhysicalInterface::DeleteReq(Agent::GetInstance()->interface_table(),
FlowTableTest::eth_itf);
client->WaitForIdle();
EXPECT_FALSE(VmPortFind(tap1, 0));
EXPECT_FALSE(VmPortFind(tap2, 0));
EXPECT_FALSE(VmPortFind(tap3, 0));
WAIT_FOR(1000, 100,
(Agent::GetInstance()->interface_table()->Size() == 3));
WAIT_FOR(1000, 100,
(Agent::GetInstance()->interface_config_table()->Size() == 0));
WAIT_FOR(1000, 100, (Agent::GetInstance()->vm_table()->Size() == 0));
WAIT_FOR(1000, 100, (Agent::GetInstance()->vn_table()->Size() == 0));
WAIT_FOR(1000, 100, (Agent::GetInstance()->acl_table()->Size() == 0));
if (ksync_init_) {
DeleteTapIntf(fd_table, MAX_VNET);
}
client->WaitForIdle();
}
static bool ksync_init_;
static int fd_table[MAX_VNET];
static VmInterface *vif1;
static VmInterface *vif2;
static VmInterface *vif3;
static std::string eth_itf;
protected:
virtual void SetUp();
virtual void TearDown() {
FlushFlowTable();
delete key1;
delete key1_r;
delete key2;
delete key2_r;
}
TestFlowKey *key1;
FlowEntry *flow1;
TestFlowKey *key1_r;
FlowEntry *flow1_r;
TestFlowKey *key2;
FlowEntry *flow2;
TestFlowKey *key2_r;
FlowEntry *flow2_r;
friend class SetupTask;
};
class SetupTask : public Task {
public:
SetupTask(FlowTableTest *test) : Task((TaskScheduler::GetInstance()->GetTaskId("Agent::FlowHandler")), -1), test_(test) {
}
virtual bool Run() {
test_->flow1 = FlowInit(test_->key1);
test_->flow1->set_flags(FlowEntry::LocalFlow);
test_->flow1_r = FlowInit(test_->key1_r);
test_->flow1_r->set_flags(FlowEntry::LocalFlow);
FlowAdd(test_->flow1, test_->flow1_r);
test_->flow2 = FlowInit(test_->key2);
test_->flow2->reset_flags(FlowEntry::LocalFlow);
test_->flow2_r = FlowInit(test_->key2_r);
test_->flow2_r->reset_flags(FlowEntry::LocalFlow);
FlowAdd(test_->flow2, test_->flow2_r);
return true;
}
private:
FlowTableTest *test_;
};
void
FlowTableTest::SetUp() {
WAIT_FOR(1000, 100,
(0U == Agent::GetInstance()->pkt()->flow_table()->Size()));
//Reset flow age
client->EnqueueFlowAge();
client->WaitForIdle();
key1 = new TestFlowKey(1, "1.1.1.1", "1.1.1.2", 1, 0, 0, svn_name,
dvn_name, 1, 1, 1, GetFlowKeyNH(1));
key1_r = new TestFlowKey(1, "1.1.1.2", "1.1.1.1", 1, 0, 0, dvn_name,
svn_name, 2, 1, 2, GetFlowKeyNH(2));
key2 = new TestFlowKey(1, "1.1.1.1", "1.1.1.3", 1, 0, 0, svn_name,
dvn_name, 1, 1, 1, GetFlowKeyNH(1));
key2_r = new TestFlowKey(1, "1.1.1.3", "1.1.1.1", 1, 0, 0, dvn_name,
svn_name, 2, 1, 2, GetFlowKeyNH(2));
TaskScheduler *scheduler = TaskScheduler::GetInstance();
SetupTask * task = new SetupTask(this);
scheduler->Enqueue(task);
client->WaitForIdle();
client->WaitForIdle();
}
bool FlowTableTest::ksync_init_;
int FlowTableTest::fd_table[MAX_VNET];
VmInterface *FlowTableTest::vif1;
VmInterface *FlowTableTest::vif2;
VmInterface *FlowTableTest::vif3;
std::string FlowTableTest::eth_itf;
TEST_F(FlowTableTest, FlowAdd_local_1) {
EXPECT_TRUE(ValidateFlow(key1, key1_r, (1 << TrafficAction::DROP)));
}
TEST_F(FlowTableTest, FlowAdd_non_local_1) {
EXPECT_TRUE(ValidateFlow(key2, key2_r, (1 << TrafficAction::DROP)));
}
TEST_F(FlowTableTest, RevFlowDelete_before_KSyncAck) {
FlushFlowTable();
KSyncSockTypeMap *sock = KSyncSockTypeMap::GetKSyncSockTypeMap();
sock->SetBlockMsgProcessing(true);
TaskScheduler *scheduler = TaskScheduler::GetInstance();
SetupTask * task = new SetupTask(this);
scheduler->Enqueue(task);
client->WaitForIdle();
client->EnqueueFlowFlush();
client->WaitForIdle();
sock->SetBlockMsgProcessing(false);
client->WaitForIdle();
EXPECT_TRUE(FlowTableWait(0));
task = new SetupTask(this);
scheduler->Enqueue(task);
client->WaitForIdle();
}
int main(int argc, char *argv[]) {
GETUSERARGS();
client = TestInit(init_file, ksync_init, true, true, true, (1000000 * 60 * 10),
AgentParam::kFlowStatsInterval, true, false);
if (vm.count("config")) {
FlowTableTest::eth_itf = Agent::GetInstance()->fabric_interface_name();
} else {
FlowTableTest::eth_itf = "eth0";
PhysicalInterface::CreateReq(Agent::GetInstance()->interface_table(),
FlowTableTest::eth_itf,
Agent::GetInstance()->fabric_vrf_name(),
PhysicalInterface::FABRIC,
PhysicalInterface::ETHERNET, false, nil_uuid(),
Ip4Address(0),
Interface::TRANSPORT_ETHERNET);
client->WaitForIdle();
}
FlowTableTest::TestSetup(ksync_init);
int ret = RUN_ALL_TESTS();
FlowTableTest::TestTearDown();
TestShutdown();
delete client;
return ret;
}