-
Notifications
You must be signed in to change notification settings - Fork 390
/
test_flow_util.h
527 lines (463 loc) · 16.6 KB
/
test_flow_util.h
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef __testflow__
#define __testflow__
#include "test_pkt_util.h"
class TestFlowPkt {
public:
//Ingress flow
TestFlowPkt(Address::Family family, const std::string &sip,
const std::string &dip, uint16_t proto, uint32_t sport,
uint32_t dport, std::string vrf, uint32_t ifindex) :
family_(family), sip_(sip), dip_(dip), proto_(proto), sport_(sport),
dport_(dport), ifindex_(ifindex), mpls_(0), hash_(0),
allow_wait_for_idle_(true) {
vrf_ =
Agent::GetInstance()->vrf_table()->FindVrfFromName(vrf)->vrf_id();
if (ifindex_) {
nh_id_ =
InterfaceTable::GetInstance()->
FindInterface(ifindex_)->flow_key_nh()->id();
} else {
nh_id_ = GetActiveLabel(MplsLabel::VPORT_NH, mpls_)->nexthop()->id();
}
};
//Ingress flow
TestFlowPkt(Address::Family family, const std::string &sip,
const std::string &dip, uint16_t proto, uint32_t sport,
uint32_t dport, const std::string &vrf, uint32_t ifindex,
uint32_t hash):
family_(family), sip_(sip), dip_(dip), proto_(proto),sport_(sport),
dport_(dport), ifindex_(ifindex), mpls_(0), hash_(hash),
allow_wait_for_idle_(true) {
vrf_ =
Agent::GetInstance()->vrf_table()->FindVrfFromName(vrf)->vrf_id();
if (ifindex_) {
nh_id_ =
InterfaceTable::GetInstance()->
FindInterface(ifindex_)->flow_key_nh()->id();
} else {
nh_id_ = GetActiveLabel(MplsLabel::VPORT_NH, mpls_)->nexthop()->id();
}
};
//Egress flow
TestFlowPkt(Address::Family family, const std::string &sip,
const std::string &dip, uint16_t proto, uint32_t sport,
uint32_t dport, const std::string &vrf, const std::string &osip,
uint32_t mpls) :
family_(family), sip_(sip), dip_(dip), proto_(proto), sport_(sport),
dport_(dport), ifindex_(0), mpls_(mpls), outer_sip_(osip), hash_(0),
allow_wait_for_idle_(true) {
vrf_ =
Agent::GetInstance()->vrf_table()->FindVrfFromName(vrf)->vrf_id();
if (ifindex_) {
nh_id_ =
InterfaceTable::GetInstance()->
FindInterface(ifindex_)->flow_key_nh()->id();
} else {
nh_id_ = GetActiveLabel(MplsLabel::VPORT_NH, mpls_)->nexthop()->id();
}
};
//Egress flow
TestFlowPkt(Address::Family family, const std::string &sip,
const std::string &dip, uint16_t proto, uint32_t sport,
uint32_t dport, std::string vrf, std::string osip,
uint32_t mpls, uint32_t hash) :
family_(family), sip_(sip), dip_(dip), proto_(proto), sport_(sport),
dport_(dport), ifindex_(0), mpls_(mpls), hash_(hash),
allow_wait_for_idle_(true) {
vrf_ =
Agent::GetInstance()->vrf_table()->FindVrfFromName(vrf)->vrf_id();
if (ifindex_) {
nh_id_ =
InterfaceTable::GetInstance()->
FindInterface(ifindex_)->flow_key_nh()->id();
} else {
nh_id_ = GetActiveLabel(MplsLabel::VPORT_NH, mpls_)->nexthop()->id();
}
};
void SendIngressFlow() {
if (!hash_) {
hash_ = rand() % 1000;
}
switch(proto_) {
case IPPROTO_TCP:
if (family_ == Address::INET) {
TxTcpPacket(ifindex_, sip_.c_str(), dip_.c_str(), sport_,
dport_, false, hash_, vrf_);
} else {
TxTcp6Packet(ifindex_, sip_.c_str(), dip_.c_str(), sport_,
dport_, false, hash_, vrf_);
}
break;
case IPPROTO_UDP:
if (family_ == Address::INET) {
TxUdpPacket(ifindex_, sip_.c_str(), dip_.c_str(), sport_,
dport_, hash_, vrf_);
} else {
TxUdp6Packet(ifindex_, sip_.c_str(), dip_.c_str(), sport_,
dport_, hash_, vrf_);
}
break;
default:
if (family_ == Address::INET) {
TxIpPacket(ifindex_, sip_.c_str(), dip_.c_str(), proto_, hash_);
} else {
TxIp6Packet(ifindex_, sip_.c_str(), dip_.c_str(), proto_,
hash_);
}
break;
}
};
void SendEgressFlow() {
if (!hash_) {
hash_ = rand() % 65535;
}
std::string self_server = Agent::GetInstance()->router_id().to_string();
//Populate ethernet interface id
uint32_t eth_intf_id =
EthInterfaceGet(Agent::GetInstance()->
fabric_interface_name().c_str())->id();
switch(proto_) {
case IPPROTO_TCP:
if (family_ == Address::INET) {
TxTcpMplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), sport_, dport_, false, hash_);
} else {
TxTcp6MplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), sport_, dport_, false, hash_);
}
break;
case IPPROTO_UDP:
if (family_ == Address::INET) {
TxUdpMplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), sport_, dport_, hash_);
} else {
TxUdp6MplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), sport_, dport_, hash_);
}
break;
default:
if (family_ == Address::INET) {
TxIpMplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), proto_, hash_);
} else {
TxIp6MplsPacket(eth_intf_id, outer_sip_.c_str(),
self_server.c_str(), mpls_, sip_.c_str(),
dip_.c_str(), proto_, hash_);
}
break;
}
};
bool FlowStatus(bool active) {
FlowEntry *fe = FlowGet(vrf_, sip_, dip_, proto_, sport_, dport_, nh_id_);
if (fe == NULL || fe->deleted()) {
return !active;
}
return active;
}
FlowEntry* Send() {
if (ifindex_) {
SendIngressFlow();
} else if (mpls_) {
SendEgressFlow();
} else {
assert(0);
}
if (allow_wait_for_idle_) {
client->WaitForIdle();
}
WAIT_FOR(1000, 3000, FlowStatus(true));
//Get flow
FlowEntry *fe = FlowGet(vrf_, sip_, dip_, proto_, sport_, dport_,
nh_id_);
EXPECT_TRUE(fe != NULL);
return fe;
};
void Delete() {
FlowKey key;
key.nh = nh_id_;
key.src_addr = IpAddress::from_string(sip_);
key.dst_addr = IpAddress::from_string(dip_);
key.src_port = sport_;
key.dst_port = dport_;
key.protocol = proto_;
key.family = Address::INET;
if (Agent::GetInstance()->pkt()->flow_table()->Find(key) == NULL) {
return;
}
TaskScheduler *scheduler = TaskScheduler::GetInstance();
FlowDeleteTask * task = new FlowDeleteTask(key);
scheduler->Enqueue(task);
WAIT_FOR(1000, 3000, FlowStatus(false));
};
const FlowEntry *FlowFetch() {
FlowEntry *fe = FlowGet(vrf_, sip_, dip_, proto_, sport_, dport_, nh_id_);
return fe;
}
void set_allow_wait_for_idle(bool allow) {
allow_wait_for_idle_ = allow;
}
private:
class FlowDeleteTask : public Task {
public:
FlowDeleteTask(const FlowKey &key) :
Task(TaskScheduler::GetInstance()->GetTaskId("Agent::FlowHandler"), -1),
key_(key) {}
virtual bool Run() {
Agent::GetInstance()->pkt()->flow_table()->Delete(key_, true);
return true;
}
private:
FlowKey key_;
};
Address::Family family_;
std::string sip_;
std::string dip_;
uint16_t proto_;
uint32_t sport_;
uint32_t dport_;
uint32_t vrf_;
uint32_t ifindex_;
uint32_t mpls_;
std::string outer_sip_;
uint32_t hash_;
bool allow_wait_for_idle_;
uint32_t nh_id_;
};
class FlowVerify {
public:
virtual ~FlowVerify() {};
virtual void Verify(FlowEntry *fe) = 0;
};
class VerifyVn : public FlowVerify {
public:
VerifyVn(std::string src_vn, std::string dest_vn):
src_vn_(src_vn), dest_vn_(dest_vn) {};
virtual ~VerifyVn() {};
virtual void Verify(FlowEntry *fe) {
EXPECT_TRUE(fe->data().source_vn == src_vn_);
EXPECT_TRUE(fe->data().dest_vn == dest_vn_);
if (true) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(rev->data().source_vn == dest_vn_);
EXPECT_TRUE(rev->data().dest_vn == src_vn_);
}
};
private:
std::string src_vn_;
std::string dest_vn_;
};
class ShortFlow : public FlowVerify {
public:
ShortFlow() {}
virtual ~ShortFlow() {};
virtual void Verify(FlowEntry *fe) {
EXPECT_TRUE(fe->is_flags_set(FlowEntry::ShortFlow) == true);
EXPECT_TRUE((fe->data().match_p.action_info.action & (1 << TrafficAction::DROP)) != 0);
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev->is_flags_set(FlowEntry::ShortFlow) == true);
EXPECT_TRUE((rev->data().match_p.action_info.action & (1 << TrafficAction::DROP)) != 0);
}
private:
};
class VerifyFlowAction : public FlowVerify {
public:
VerifyFlowAction(TrafficAction::Action action):action_(action) {}
virtual ~VerifyFlowAction() {}
virtual void Verify(FlowEntry *fe) {
EXPECT_TRUE((fe->data().match_p.action_info.action & (1 << action_)) != 0);
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE((rev->data().match_p.action_info.action & (1 << action_)) != 0);
}
private:
TrafficAction::Action action_;
};
class VerifyVrf : public FlowVerify {
public:
VerifyVrf(std::string src_vrf, std::string dest_vrf):
src_vrf_(src_vrf), dest_vrf_(dest_vrf) {};
virtual ~VerifyVrf() {};
void Verify(FlowEntry *fe) {
const VrfEntry *src_vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromName(src_vrf_);
EXPECT_TRUE(src_vrf != NULL);
const VrfEntry *dest_vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromName(dest_vrf_);
EXPECT_TRUE(dest_vrf != NULL);
EXPECT_TRUE(fe->data().vrf == src_vrf->vrf_id());
if (true) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(rev->data().vrf == dest_vrf->vrf_id());
}
};
private:
std::string src_vrf_;
std::string dest_vrf_;
};
class VerifyDestVrf : public FlowVerify {
public:
VerifyDestVrf(std::string src_vrf, std::string dest_vrf):
src_vrf_(src_vrf), dest_vrf_(dest_vrf) {};
virtual ~VerifyDestVrf() {};
void Verify(FlowEntry *fe) {
const VrfEntry *src_vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromName(src_vrf_);
EXPECT_TRUE(src_vrf != NULL);
const VrfEntry *dest_vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromName(dest_vrf_);
EXPECT_TRUE(dest_vrf != NULL);
EXPECT_TRUE(fe->data().flow_source_vrf == dest_vrf->vrf_id());
if (true) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(rev->data().flow_source_vrf == src_vrf->vrf_id());
}
};
private:
std::string src_vrf_;
std::string dest_vrf_;
};
struct VerifyNat : public FlowVerify {
public:
VerifyNat(std::string nat_sip, std::string nat_dip, uint32_t proto,
uint32_t nat_sport, uint32_t nat_dport):
nat_sip_(nat_sip), nat_dip_(nat_dip), nat_sport_(nat_sport),
nat_dport_(nat_dport) { };
virtual ~VerifyNat() { };
void Verify(FlowEntry *fe) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(rev->key().src_addr == Ip4Address::from_string(nat_sip_));
EXPECT_TRUE(rev->key().dst_addr == Ip4Address::from_string(nat_dip_));
EXPECT_TRUE(rev->key().src_port == nat_sport_);
EXPECT_TRUE(rev->key().dst_port == nat_dport_ ||
rev->key().dst_port == fe->linklocal_src_port());
};
private:
std::string nat_sip_;
std::string nat_dip_;
uint32_t nat_sport_;
uint32_t nat_dport_;
};
class VerifyEcmp : public FlowVerify {
public:
VerifyEcmp(bool fwd_ecmp, bool rev_ecmp):
fwd_flow_is_ecmp_(fwd_ecmp), rev_flow_is_ecmp_(rev_ecmp) { };
virtual ~VerifyEcmp() { };
void Verify(FlowEntry *fe) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
if (fwd_flow_is_ecmp_) {
EXPECT_TRUE(fe->is_flags_set(FlowEntry::EcmpFlow) == true);
EXPECT_TRUE(fe->data().component_nh_idx != (uint32_t) -1);
} else {
EXPECT_TRUE(fe->is_flags_set(FlowEntry::EcmpFlow) == false);
EXPECT_TRUE(fe->data().component_nh_idx == (uint32_t) -1);
}
if (rev_flow_is_ecmp_) {
EXPECT_TRUE(rev->is_flags_set(FlowEntry::EcmpFlow) == true);
EXPECT_TRUE(rev->data().component_nh_idx != (uint32_t) -1);
} else {
EXPECT_TRUE(rev->is_flags_set(FlowEntry::EcmpFlow) == false);
EXPECT_TRUE(rev->data().component_nh_idx == (uint32_t) -1);
}
};
private:
bool fwd_flow_is_ecmp_;
bool rev_flow_is_ecmp_;
};
class VerifyAction : public FlowVerify {
public:
VerifyAction(uint32_t action, uint32_t rev_action):
action_(action), rev_action_(rev_action) { };
virtual ~VerifyAction() { };
void Verify(FlowEntry *fe) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(fe->match_p().action_info.action == action_);
EXPECT_TRUE(rev->match_p().action_info.action == rev_action_);
};
private:
uint32_t action_;
uint32_t rev_action_;
};
class VerifyRpf : public FlowVerify {
public:
VerifyRpf(uint32_t forward_flow_nh, uint32_t reverse_flow_nh):
forward_flow_rpf_nh_(forward_flow_nh),
reverse_flow_rpf_nh_(reverse_flow_nh) {};
virtual ~VerifyRpf() {};
void Verify(FlowEntry *fe) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(fe->data().nh_state_.get()->nh()->id() ==
forward_flow_rpf_nh_);
EXPECT_TRUE(rev->data().nh_state_.get()->nh()->id() ==
reverse_flow_rpf_nh_);
}
private:
uint32_t forward_flow_rpf_nh_;
uint32_t reverse_flow_rpf_nh_;
};
class VerifyRpfEnable : public FlowVerify {
public:
VerifyRpfEnable(bool fwd_rpf_enable, bool rev_rpf_enable):
fwd_rpf_enable_(fwd_rpf_enable),
rev_rpf_enable_(rev_rpf_enable){};
virtual ~VerifyRpfEnable() {};
void Verify(FlowEntry *fe) {
FlowEntry *rev = fe->reverse_flow_entry();
EXPECT_TRUE(rev != NULL);
EXPECT_TRUE(fe->data().enable_rpf == fwd_rpf_enable_);
EXPECT_TRUE(rev->data().enable_rpf == rev_rpf_enable_);
}
private:
bool fwd_rpf_enable_;
bool rev_rpf_enable_;
};
struct TestFlow {
~TestFlow() {
for (int i = 0; i < 10; i++) {
if (action_[i]) {
delete action_[i];
}
}
};
void Verify(FlowEntry *fe) {
for (int i = 0; i < 10; i++) {
if (action_[i]) {
action_[i]->Verify(fe);
}
}
};
FlowEntry* Send() {
return pkt_.Send();
};
void Delete() {
pkt_.Delete();
};
TestFlowPkt pkt_;
FlowVerify* action_[10];
};
void CreateFlow(TestFlow *tflow, uint32_t count) {
for (uint32_t i = 0; i < count; i++) {
FlowEntry *fe = tflow->Send();
tflow->Verify(fe);
tflow = tflow + 1;
}
}
void DeleteFlow(TestFlow *tflow, uint32_t count) {
for (uint32_t i = 0; i < count; i++) {
tflow->Delete();
}
}
#endif