/
test_pkt_flow.cc
3742 lines (3285 loc) · 132 KB
/
test_pkt_flow.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 "base/os.h"
#include <algorithm>
#include "test/test_cmn_util.h"
#include "test_flow_util.h"
#include "ksync/ksync_sock_user.h"
#include "oper/tunnel_nh.h"
#include "pkt/flow_table.h"
#define MAX_VNET 4
void RouterIdDepInit(Agent *agent) {
}
struct TestFlowKey {
uint32_t vrfid_;
const char *sip_;
const char *dip_;
uint8_t proto_;
uint16_t sport_;
uint16_t dport_;
};
#define vm1_ip "11.1.1.1"
#define ip6_vm1_ip "::11.1.1.1"
#define vm2_ip "11.1.1.2"
#define ip6_vm2_ip "::11.1.1.2"
#define vm3_ip "12.1.1.1"
#define ip6_vm3_ip "::12.1.1.1"
#define vm4_ip "13.1.1.1"
#define ip6_vm4_ip "::13.1.1.1"
#define vm5_ip "14.1.1.1"
#define ip6_vm5_ip "::14.1.1.1"
#define vm1_fip "14.1.1.100"
#define ip6_vm1_fip "::14.1.1.100"
#define vm1_fip2 "14.1.1.101"
#define ip6_vm1_fip2 "::14.1.1.101"
#define vm2_fip "14.1.1.100"
#define ip6_vm2_fip "::14.1.1.100"
#define remote_vm1_ip "11.1.1.3"
#define ip6_remote_vm1_ip "::11.1.1.3"
#define remote_vm3_ip "12.1.1.3"
#define ip6_remote_vm3_ip "::12.1.1.3"
#define remote_vm4_ip "13.1.1.2"
#define ip6_remote_vm4_ip "::13.1.1.2"
#define remote_router_ip "10.1.1.2"
#define ip6_remote_router_ip "::10.1.1.2"
#define vhost_ip_addr "10.1.2.1"
#define ip6_vhost_ip_addr "::10.1.2.1"
#define linklocal_ip "169.254.1.10"
#define ip6_linklocal_ip "::169.254.1.10"
#define linklocal_port 4000
#define fabric_port 8000
#define vm_a_ip "16.1.1.1"
#define vm_b_ip "16.1.1.2"
int fd_table[MAX_VNET];
InetInterface *vhost;
struct PortInfo input[] = {
{"flow0", 6, vm1_ip, "00:00:00:01:01:01", 5, 1},
{"flow1", 7, vm2_ip, "00:00:00:01:01:02", 5, 2},
{"flow2", 8, vm3_ip, "00:00:00:01:01:03", 5, 3},
};
struct PortInfo input2[] = {
{"flow3", 9, vm4_ip, "00:00:00:01:01:04", 3, 4},
};
struct PortInfo input3[] = {
{"flow4", 10, vm5_ip, "00:00:00:01:01:06", 4, 5},
};
struct PortInfo input4[] = {
{"flow5", 3, vm_a_ip, "00:00:00:01:01:07", 6, 6},
{"flow6", 4, vm_b_ip, "00:00:00:01:01:08", 6, 7},
};
typedef enum {
INGRESS = 0,
EGRESS = 1,
BIDIRECTION = 2
} AclDirection;
int hash_id;
VmInterface *flow0;
VmInterface *flow1;
VmInterface *flow2;
VmInterface *flow3;
VmInterface *flow4;
VmInterface *flow5;
VmInterface *flow6;
std::string eth_itf;
static void NHNotify(DBTablePartBase *partition, DBEntryBase *entry) {
}
class FlowTest : public ::testing::Test {
public:
FlowTest() : peer_(NULL), agent_(Agent::GetInstance()) {
boost::scoped_ptr<InetInterfaceKey> key(new InetInterfaceKey("vhost0"));
vhost = static_cast<InetInterface *>(Agent::GetInstance()->
interface_table()->FindActiveEntry(key.get()));
}
bool FlowTableWait(size_t count) {
int i = 1000;
while (i > 0) {
i--;
if (agent()->pkt()->flow_table()->Size() == count) {
break;
}
client->WaitForIdle();
usleep(1);
}
return (agent()->pkt()->flow_table()->Size() == count);
}
void FlushFlowTable() {
client->EnqueueFlowFlush();
client->WaitForIdle();
EXPECT_EQ(0U, agent()->pkt()->flow_table()->Size());
}
void CreateLocalRoute(const char *vrf, const char *ip,
VmInterface *intf, int label) {
Ip4Address addr = Ip4Address::from_string(ip);
agent()->fabric_inet4_unicast_table()->
AddLocalVmRouteReq(peer_, 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(peer_, vrf, addr, 32, gw, TunnelType::MplsType(), label, vn,
SecurityGroupList(), PathPreference());
client->WaitForIdle(2);
WAIT_FOR(1000, 500, (RouteFind(vrf, addr, 32) == true));
}
void DeleteRoute(const char *vrf, const char *ip) {
Ip4Address addr = Ip4Address::from_string(ip);
agent()->fabric_inet4_unicast_table()->DeleteReq(peer_,
vrf, addr, 32, NULL);
client->WaitForIdle();
WAIT_FOR(1000, 1, (RouteFind(vrf, addr, 32) == false));
}
void DeleteRemoteRoute(const char *vrf, const char *ip) {
Ip4Address addr = Ip4Address::from_string(ip);
agent()->fabric_inet4_unicast_table()->DeleteReq(peer_,
vrf, addr, 32, NULL);
client->WaitForIdle();
WAIT_FOR(1000, 1, (RouteFind(vrf, addr, 32) == false));
}
static void RunFlowAudit() {
FlowTableKSyncObject *ksync_obj =
Agent::GetInstance()->ksync()->flowtable_ksync_obj();
ksync_obj->AuditProcess();
ksync_obj->AuditProcess();
}
static bool KFlowHoldAdd(uint32_t hash_id, int vrf, const char *sip,
const char *dip, int proto, int sport, int dport,
int nh_id) {
FlowTableKSyncObject *ksync_obj =
Agent::GetInstance()->ksync()->flowtable_ksync_obj();
if (hash_id >= ksync_obj->flow_table_entries_count()) {
return false;
}
if (ksync_init_) {
return false;
}
vr_flow_entry *vr_flow = KSyncSockTypeMap::GetFlowEntry(hash_id);
vr_flow_req req;
req.set_fr_index(hash_id);
IpAddress saddr = IpAddress::from_string(sip);
IpAddress daddr = IpAddress::from_string(dip);
req.set_fr_flow_ip(ksync_obj->IpToVector(saddr, daddr, Address::INET));
req.set_fr_flow_proto(proto);
req.set_fr_flow_sport(htons(sport));
req.set_fr_flow_dport(htons(dport));
req.set_fr_flow_vrf(vrf);
req.set_fr_flow_nh_id(nh_id);
vr_flow->fe_action = VR_FLOW_ACTION_HOLD;
KSyncSockTypeMap::SetFlowEntry(&req, true);
return true;
}
static void KFlowPurgeHold() {
if (ksync_init_) {
return;
}
FlowTableKSyncObject *ksync_obj =
Agent::GetInstance()->ksync()->flowtable_ksync_obj();
for (size_t count = 0; count < ksync_obj->flow_table_entries_count();
count++) {
vr_flow_entry *vr_flow = KSyncSockTypeMap::GetFlowEntry(count);
vr_flow->fe_action = VR_FLOW_ACTION_DROP;
vr_flow_req req;
req.set_fr_index(hash_id);
KSyncSockTypeMap::SetFlowEntry(&req, false);
}
return;
}
static void FlowAdd(int hash_id, int vrf, const char *sip, const char *dip,
int proto, int sport, int dport, const char *nat_sip,
const char *nat_dip, int nat_vrf) {
boost::shared_ptr<PktInfo> pkt_1(new PktInfo(Agent::GetInstance(),
100, 0, 0));
PktFlowInfo flow_info_1(pkt_1, Agent::GetInstance()->pkt()->flow_table());
PktFlowInfo *flow_info = &flow_info_1;
MatchPolicy policy;
string svn = "svn";
string dvn = "dvn";
PktInfo *pkt = pkt_1.get();
pkt->vrf = vrf;
pkt->ip_saddr = Ip4Address(ntohl(inet_addr(sip)));
pkt->ip_daddr = Ip4Address(ntohl(inet_addr(dip)));
pkt->ip_proto = proto;
pkt->sport = sport;
pkt->dport = dport;
policy.action_info.action = (1 << TrafficAction::PASS);
flow_info->nat_vrf = nat_vrf;
if (nat_sip) {
flow_info->nat_ip_saddr = Ip4Address(ntohl(inet_addr(nat_sip)));
} else {
flow_info->nat_ip_saddr = pkt->ip_saddr;
}
if (nat_dip) {
flow_info->nat_ip_daddr = Ip4Address(ntohl(inet_addr(nat_dip)));
} else {
flow_info->nat_ip_daddr = pkt->ip_daddr;
}
flow_info->nat_sport = sport;
flow_info->nat_dport = dport;
if (pkt->ip_saddr != flow_info->nat_ip_saddr ||
pkt->ip_daddr != flow_info->nat_ip_daddr) {
flow_info->nat_done = true;
} else {
flow_info->nat_done = false;
}
pkt->agent_hdr.cmd_param = hash_id;
PktControlInfo in;
PktControlInfo out;
flow_info->Add(pkt, &in, &out);
//agent()->pkt()->flow_table()->Add(pkt, flow_info);
client->WaitForIdle();
}
static int GetFlowPassCount(int total_flows, int age_time_usecs) {
int age_time_millisec = age_time_usecs / 1000;
int default_age_time_millisec = FlowStatsCollector::FlowAgeTime / 1000;
int max_flows = (FlowStatsCollector::MaxFlows * age_time_millisec) / default_age_time_millisec;
int flow_multiplier = (max_flows * FlowStatsCollector::FlowStatsMinInterval)/age_time_millisec;
int flow_timer_interval = std::min((age_time_millisec * flow_multiplier)/total_flows, 1000);
int flow_count_per_pass = std::max((flow_timer_interval * total_flows)/age_time_millisec, 100);
int ret = total_flows / flow_count_per_pass;
if (total_flows % flow_count_per_pass) {
ret++;
}
return ret;
}
static void TestTearDown() {
client->Reset();
if (ksync_init_) {
DeleteTapIntf(fd_table, MAX_VNET);
}
client->WaitForIdle();
}
static void TestSetup(bool ksync_init) {
ksync_init_ = ksync_init;
if (ksync_init_) {
CreateTapInterfaces("flow", MAX_VNET, fd_table);
client->WaitForIdle();
}
}
void CheckSandeshResponse(Sandesh *sandesh, uint32_t flows) {
if (memcmp(sandesh->Name(), "FlowRecordsResp",
strlen("FlowRecordsResp")) == 0) {
FlowRecordsResp *resp = static_cast<FlowRecordsResp *>(sandesh);
EXPECT_TRUE(resp->get_flow_list().size() == flows);
} else if (memcmp(sandesh->Name(), "FlowRecordResp",
strlen("FlowRecordResp")) == 0) {
FlowRecordResp *resp = static_cast<FlowRecordResp *>(sandesh);
EXPECT_TRUE(resp->get_record().sip == vm1_ip);
EXPECT_TRUE(resp->get_record().dip == vm2_ip);
EXPECT_TRUE(resp->get_record().src_port == 1000);
EXPECT_TRUE(resp->get_record().dst_port == 200);
EXPECT_TRUE(resp->get_record().protocol == IPPROTO_TCP);
}
}
std::string AddAclXmlString(const char *name, int id, int proto,
const char *action, const char* uuid) {
char buff[10240];
sprintf(buff,
"<?xml version=\"1.0\"?>\n"
"<config>\n"
" <update>\n"
" <node type=\"access-control-list\">\n"
" <name>%s</name>\n"
" <id-perms>\n"
" <permissions>\n"
" <owner></owner>\n"
" <owner_access>0</owner_access>\n"
" <group></group>\n"
" <group_access>0</group_access>\n"
" <other_access>0</other_access>\n"
" </permissions>\n"
" <uuid>\n"
" <uuid-mslong>0</uuid-mslong>\n"
" <uuid-lslong>%d</uuid-lslong>\n"
" </uuid>\n"
" </id-perms>\n"
" <access-control-list-entries>\n"
" <dynamic>false</dynamic>\n"
" <acl-rule>\n"
" <match-condition>\n"
" <src-address>\n"
" <virtual-network> any </virtual-network>\n"
" </src-address>\n"
" <protocol>%d</protocol>\n"
" <src-port>\n"
" <start-port> 0 </start-port>\n"
" <end-port> 10000 </end-port>\n"
" </src-port>\n"
" <dst-address>\n"
" <virtual-network> any </virtual-network>\n"
" </dst-address>\n"
" <dst-port>\n"
" <start-port> 0 </start-port>\n"
" <end-port> 10000 </end-port>\n"
" </dst-port>\n"
" </match-condition>\n"
" <action-list>\n"
" <simple-action>\n"
" %s\n"
" </simple-action>\n"
" </action-list>\n"
" <rule-uuid>%s</rule-uuid>\n"
" </acl-rule>\n"
" <acl-rule>\n"
" <match-condition>\n"
" <src-address>\n"
" <virtual-network> vn6 </virtual-network>\n"
" </src-address>\n"
" <protocol>any</protocol>\n"
" <src-port>\n"
" <start-port> 0 </start-port>\n"
" <end-port> 60000 </end-port>\n"
" </src-port>\n"
" <dst-address>\n"
" <virtual-network> vn6 </virtual-network>\n"
" </dst-address>\n"
" <dst-port>\n"
" <start-port> 0 </start-port>\n"
" <end-port> 60000 </end-port>\n"
" </dst-port>\n"
" </match-condition>\n"
" <action-list>\n"
" <simple-action>\n"
" deny\n"
" </simple-action>\n"
" </action-list>\n"
" <rule-uuid>fe6a4dcb-dde4-48e6-8957-856a7aacb2e1</rule-uuid>\n"
" </acl-rule>\n"
" </access-control-list-entries>\n"
" </node>\n"
" </update>\n"
"</config>\n", name, id, proto, action, uuid);
string s(buff);
return s;
}
void AddAclEntry(const char *name, int id, int proto,
const char *action, const char *uuid_str) {
std::string s = AddAclXmlString(name, id, proto, action, uuid_str);
pugi::xml_document xdoc_;
pugi::xml_parse_result result = xdoc_.load(s.c_str());
EXPECT_TRUE(result);
Agent::GetInstance()->ifmap_parser()->ConfigParse(xdoc_.first_child(), 0);
client->WaitForIdle();
}
void AddSgEntry(const char *sg_name, const char *name, int id,
int proto, const char *action, AclDirection direction,
const char *uuid1, const char* uuid2) {
AddSg(sg_name, id);
char acl_name[1024];
uint16_t max_len = sizeof(acl_name) - 1;
strncpy(acl_name, name, max_len);
switch (direction) {
case INGRESS:
strncat(acl_name, "ingress-access-control-list", max_len);
AddAclEntry(acl_name, id, proto, action, uuid1);
AddLink("security-group", sg_name, "access-control-list", acl_name);
break;
case EGRESS:
strncat(acl_name, "egress-access-control-list", max_len);
AddAclEntry(acl_name, id, proto, action, uuid1);
AddLink("security-group", sg_name, "access-control-list", acl_name);
break;
case BIDIRECTION:
strncat(acl_name, "egress-access-control-list", max_len);
AddAclEntry(acl_name, id, proto, action, uuid1);
AddLink("security-group", sg_name, "access-control-list", acl_name);
strncpy(acl_name, name, max_len);
strncat(acl_name, "ingress-access-control-list", max_len);
AddAclEntry(acl_name, id, proto, action, uuid2);
AddLink("security-group", sg_name, "access-control-list", acl_name);
break;
}
}
void FlowSetup() {
CreateVmportEnv(input4, 2, 0);
client->WaitForIdle(5);
flow5 = VmInterfaceGet(input4[0].intf_id);
assert(flow5);
flow6 = VmInterfaceGet(input4[1].intf_id);
assert(flow6);
}
void FlowTeardown() {
client->Reset();
DeleteVmportEnv(input4, 2, true, 0);
client->WaitForIdle(5);
client->PortDelNotifyWait(2);
}
protected:
virtual void SetUp() {
unsigned int vn_count = 0;
EXPECT_EQ(0U, agent()->pkt()->flow_table()->Size());
hash_id = 1;
client->Reset();
CreateVmportEnv(input, 3, 1);
client->WaitForIdle(5);
vn_count++;
EXPECT_TRUE(VmPortActive(input, 0));
EXPECT_TRUE(VmPortActive(input, 1));
EXPECT_TRUE(VmPortActive(input, 2));
EXPECT_TRUE(VmPortPolicyEnable(input, 0));
EXPECT_TRUE(VmPortPolicyEnable(input, 1));
EXPECT_TRUE(VmPortPolicyEnable(input, 2));
EXPECT_EQ(7U, agent()->interface_table()->Size());
EXPECT_EQ(3U, agent()->vm_table()->Size());
EXPECT_EQ(vn_count, agent()->vn_table()->Size());
EXPECT_EQ(3U, agent()->interface_config_table()->Size());
flow0 = VmInterfaceGet(input[0].intf_id);
assert(flow0);
flow1 = VmInterfaceGet(input[1].intf_id);
assert(flow1);
flow2 = VmInterfaceGet(input[2].intf_id);
assert(flow2);
/* Create interface flow3 in vn3 , vm4. Associate vn3 with acl2 */
client->Reset();
CreateVmportEnv(input2, 1, 2);
client->WaitForIdle(5);
vn_count++;
EXPECT_TRUE(VmPortActive(input2, 0));
EXPECT_TRUE(VmPortPolicyEnable(input2, 0));
EXPECT_EQ(8U, agent()->interface_table()->Size());
EXPECT_EQ(4U, agent()->vm_table()->Size());
EXPECT_EQ(vn_count, agent()->vn_table()->Size());
EXPECT_EQ(4U, agent()->interface_config_table()->Size());
EXPECT_EQ(2U, agent()->acl_table()->Size());
flow3 = VmInterfaceGet(input2[0].intf_id);
assert(flow3);
/* Create interface flow4 in default-project:vn4 */
client->Reset();
CreateVmportFIpEnv(input3, 1);
client->WaitForIdle(5);
vn_count++;
EXPECT_TRUE(VmPortActive(input3, 0));
EXPECT_EQ(9U, agent()->interface_table()->Size());
EXPECT_EQ(5U, agent()->vm_table()->Size());
EXPECT_EQ(vn_count, agent()->vn_table()->Size());
EXPECT_EQ(5U, agent()->interface_config_table()->Size());
flow4 = VmInterfaceGet(input3[0].intf_id);
assert(flow4);
// Configure Floating-IP
AddFloatingIpPool("fip-pool1", 1);
AddFloatingIp("fip1", 1, "14.1.1.100");
AddFloatingIp("fip2", 1, "14.1.1.101");
AddLink("floating-ip", "fip1", "floating-ip-pool", "fip-pool1");
AddLink("floating-ip", "fip2", "floating-ip-pool", "fip-pool1");
AddLink("floating-ip-pool", "fip-pool1", "virtual-network",
"default-project:vn4");
AddLink("virtual-machine-interface", "flow0", "floating-ip", "fip1");
client->WaitForIdle();
boost::system::error_code ec;
peer_ = CreateBgpPeer(Ip4Address::from_string("0.0.0.1", ec),
"xmpp channel");
Ip4Address gw_ip = Ip4Address::from_string("11.1.1.254");
//Add a gateway route pointing to pkt0
VrfEntry *vrf = VrfGet("vrf5");
static_cast<InetUnicastAgentRouteTable *>(
vrf->GetInet4UnicastRouteTable())->AddHostRoute("vrf5",
gw_ip, 32, "vn5");
client->WaitForIdle();
}
virtual void TearDown() {
FlushFlowTable();
client->Reset();
Ip4Address gw_ip = Ip4Address::from_string("11.1.1.254");
Agent::GetInstance()->fabric_inet4_unicast_table()->DeleteReq(
Agent::GetInstance()->local_peer(), "vrf5", gw_ip, 32, NULL);
client->WaitForIdle();
DeleteVmportEnv(input, 3, true, 1);
client->WaitForIdle(3);
client->PortDelNotifyWait(3);
EXPECT_FALSE(VmPortFind(input, 0));
EXPECT_FALSE(VmPortFind(input, 1));
EXPECT_FALSE(VmPortFind(input, 2));
EXPECT_EQ(6U, agent()->interface_table()->Size());
EXPECT_EQ(2U, agent()->interface_config_table()->Size());
client->Reset();
DeleteVmportEnv(input2, 1, true, 2);
client->WaitForIdle(3);
client->PortDelNotifyWait(1);
EXPECT_EQ(5U, agent()->interface_table()->Size());
EXPECT_EQ(1U, agent()->interface_config_table()->Size());
EXPECT_FALSE(VmPortFind(input2, 0));
client->Reset();
DeleteVmportFIpEnv(input3, 1, true);
client->WaitForIdle(3);
client->PortDelNotifyWait(1);
EXPECT_EQ(4U, agent()->interface_table()->Size());
EXPECT_EQ(0U, agent()->interface_config_table()->Size());
EXPECT_FALSE(VmPortFind(input3, 0));
EXPECT_EQ(0U, agent()->vm_table()->Size());
EXPECT_EQ(0U, agent()->vn_table()->Size());
EXPECT_EQ(0U, agent()->acl_table()->Size());
DeleteBgpPeer(peer_);
}
Agent *agent() {return agent_;}
private:
static bool ksync_init_;
BgpPeer *peer_;
Agent *agent_;
};
bool FlowTest::ksync_init_;
//Ingress flow test (VMport to VMport - Same VN)
//Flow creation using IP and TCP packets
TEST_F(FlowTest, FlowAdd_1) {
TestFlow flow[] = {
//Add a ICMP forward and reverse flow
{ TestFlowPkt(Address::INET, vm1_ip, vm2_ip, 1, 0, 0, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
{ TestFlowPkt(Address::INET, vm2_ip, vm1_ip, 1, 0, 0, "vrf5",
flow1->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
//Add a TCP forward and reverse flow
{ TestFlowPkt(Address::INET, vm1_ip, vm2_ip, IPPROTO_TCP, 1000, 200,
"vrf5", flow0->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
{ TestFlowPkt(Address::INET, vm2_ip, vm1_ip, IPPROTO_TCP, 200, 1000,
"vrf5", flow1->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
}
};
CreateFlow(flow, 4);
EXPECT_EQ(4U, agent()->pkt()->flow_table()->Size());
FetchAllFlowRecords *all_flow_records_sandesh = new FetchAllFlowRecords();
Sandesh::set_response_callback(boost::bind(&FlowTest::CheckSandeshResponse,
this, _1, 4));
all_flow_records_sandesh->HandleRequest();
client->WaitForIdle();
all_flow_records_sandesh->Release();
FetchFlowRecord *flow_record_sandesh = new FetchFlowRecord();
flow_record_sandesh->set_nh(GetFlowKeyNH(input[0].intf_id));
flow_record_sandesh->set_sip(vm1_ip);
flow_record_sandesh->set_dip(vm2_ip);
flow_record_sandesh->set_src_port(1000);
flow_record_sandesh->set_dst_port(200);
flow_record_sandesh->set_protocol(IPPROTO_TCP);
flow_record_sandesh->HandleRequest();
client->WaitForIdle();
flow_record_sandesh->Release();
//Verify the ingress and egress flow counts
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(4U, in_count);
EXPECT_EQ(4U, out_count);
}
TEST_F(FlowTest, Ip6_FlowAdd_1) {
TestFlow flow[] = {
//Add a ICMP forward and reverse flow
{ TestFlowPkt(Address::INET6, ip6_vm1_ip, ip6_vm2_ip, IPPROTO_ICMPV6,
0, 0, "vrf5", flow0->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
{ TestFlowPkt(Address::INET6, ip6_vm2_ip, ip6_vm1_ip, IPPROTO_ICMPV6,
0, 0, "vrf5", flow1->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
//Add a TCP forward and reverse flow
{ TestFlowPkt(Address::INET6, ip6_vm1_ip, ip6_vm2_ip, IPPROTO_TCP,
1000, 200, "vrf5", flow0->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
{ TestFlowPkt(Address::INET6, ip6_vm2_ip, ip6_vm1_ip, IPPROTO_TCP,
200, 1000, "vrf5", flow1->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
}
};
CreateFlow(flow, 4);
EXPECT_EQ(4U, agent()->pkt()->flow_table()->Size());
FetchAllFlowRecords *all_flow_records_sandesh = new FetchAllFlowRecords();
Sandesh::set_response_callback(boost::bind(&FlowTest::CheckSandeshResponse,
this, _1, 4));
all_flow_records_sandesh->HandleRequest();
client->WaitForIdle();
all_flow_records_sandesh->Release();
FetchFlowRecord *flow_record_sandesh = new FetchFlowRecord();
flow_record_sandesh->set_nh(GetFlowKeyNH(input[0].intf_id));
flow_record_sandesh->set_sip(ip6_vm1_ip);
flow_record_sandesh->set_dip(ip6_vm2_ip);
flow_record_sandesh->set_src_port(1000);
flow_record_sandesh->set_dst_port(200);
flow_record_sandesh->set_protocol(IPPROTO_TCP);
flow_record_sandesh->HandleRequest();
client->WaitForIdle();
flow_record_sandesh->Release();
//Verify the ingress and egress flow counts
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(4U, in_count);
EXPECT_EQ(4U, out_count);
}
//Egress flow test (IP fabric to VMPort - Same VN)
//Flow creation using GRE packets
TEST_F(FlowTest, FlowAdd_2) {
EXPECT_EQ(0U, agent()->pkt()->flow_table()->Size());
//Create PHYSICAL interface to receive GRE packets on it.
PhysicalInterfaceKey key(eth_itf);
Interface *intf = static_cast<Interface *>
(agent()->interface_table()->FindActiveEntry(&key));
EXPECT_TRUE(intf != NULL);
//Create remote VM route. This will be used to figure out destination VN for
//flow
CreateRemoteRoute("vrf5", remote_vm1_ip, remote_router_ip, 30, "vn5");
client->WaitForIdle();
CreateRemoteRoute("vrf5", remote_vm3_ip, remote_router_ip, 32, "vn5");
client->WaitForIdle();
TestFlow flow[] = {
//Send an ICMP flow from remote VM to local VM
{
TestFlowPkt(Address::INET, remote_vm1_ip, vm1_ip, 1, 0, 0, "vrf5",
remote_router_ip, flow0->label()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
//Send a ICMP reply from local to remote VM
{
TestFlowPkt(Address::INET, vm1_ip, remote_vm1_ip, 1, 0, 0, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
//Send a TCP flow from remote VM to local VM
{
TestFlowPkt(Address::INET, remote_vm3_ip, vm3_ip, IPPROTO_TCP, 1001, 1002,
"vrf5", remote_router_ip, flow2->label()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
},
//Send a TCP reply from local VM to remote VM
{
TestFlowPkt(Address::INET, vm3_ip, remote_vm3_ip, IPPROTO_TCP, 1002, 1001,
"vrf5", flow2->id()),
{
new VerifyVn("vn5", "vn5"),
new VerifyVrf("vrf5", "vrf5")
}
}
};
CreateFlow(flow, 4);
EXPECT_EQ(4U, agent()->pkt()->flow_table()->Size());
//Verify ingress and egress flow count
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//1. Remove remote VM routes
DeleteRemoteRoute("vrf5", remote_vm1_ip);
DeleteRemoteRoute("vrf5", remote_vm3_ip);
client->WaitForIdle();
}
//Ingress flow test (VMport to VMport - Different VNs)
//Flow creation using IP and TCP packets
TEST_F(FlowTest, FlowAdd_3) {
/* Add Local VM route of vrf3 to vrf5 */
CreateLocalRoute("vrf5", vm4_ip, flow3, 19);
/* Add Local VM route of vrf5 to vrf3 */
CreateLocalRoute("vrf3", vm1_ip, flow0, 16);
TestFlow flow[] = {
//Send a ICMP request from local VM in vn5 to local VM in vn3
{
TestFlowPkt(Address::INET, vm1_ip, vm4_ip, 1, 0, 0, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn3"),
}
},
//Send an ICMP reply from local VM in vn3 to local VM in vn5
{
TestFlowPkt(Address::INET, vm4_ip, vm1_ip, 1, 0, 0, "vrf3",
flow3->id()),
{
new VerifyVn("vn3", "vn5"),
}
},
//Send a TCP packet from local VM in vn5 to local VM in vn3
{
TestFlowPkt(Address::INET, vm1_ip, vm4_ip, IPPROTO_TCP, 200, 300, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn3"),
}
},
//Send an TCP packet from local VM in vn3 to local VM in vn5
{
TestFlowPkt(Address::INET, vm4_ip, vm1_ip, IPPROTO_TCP, 300, 200, "vrf3",
flow3->id()),
{
new VerifyVn("vn3", "vn5"),
}
}
};
CreateFlow(flow, 4);
client->WaitForIdle();
//Verify ingress and egress flow count of VN "vn5"
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//Verify ingress and egress flow count of VN "vn3"
fe = flow[1].pkt_.FlowFetch();
vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//1. Remove remote VM routes
DeleteRoute("vrf5", vm4_ip);
DeleteRoute("vrf3", vm1_ip);
client->WaitForIdle();
}
//Egress flow test (IP fabric to VMport - Different VNs)
//Flow creation using GRE packets
TEST_F(FlowTest, FlowAdd_4) {
/* Add remote VN route to vrf5 */
CreateRemoteRoute("vrf5", remote_vm4_ip, remote_router_ip, 8, "vn3");
Ip4Address rid1 = agent()->router_id();
std::string router_ip_str = rid1.to_string();
TestFlow flow[] = {
//Send an ICMP flow from remote VM in vn3 to local VM in vn5
{
TestFlowPkt(Address::INET, remote_vm4_ip, vm1_ip, 1, 0, 0, "vrf5",
remote_router_ip, 16),
{
new VerifyVn("vn3", "vn5"),
}
},
//Send a ICMP reply from local VM in vn5 to remote VM in vn3
{
TestFlowPkt(Address::INET, vm1_ip, remote_vm4_ip, 1, 0, 0, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn3"),
}
},
//Send a TCP flow from remote VM in vn3 to local VM in vn5
{
TestFlowPkt(Address::INET, remote_vm4_ip, vm1_ip, IPPROTO_TCP, 1006, 1007,
"vrf5", remote_router_ip, 16),
{
new VerifyVn("vn3", "vn5"),
}
},
//Send a TCP reply from local VM in vn5 to remote VM in vn3
{
TestFlowPkt(Address::INET, vm1_ip, remote_vm4_ip, IPPROTO_TCP, 1007, 1006,
"vrf5", flow0->id()),
{
new VerifyVn("vn5", "vn3"),
}
}
};
CreateFlow(flow, 4);
client->WaitForIdle();
//Verify ingress and egress flow count of VN "vn5"
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//Verify ingress and egress flow count of VN "vn3"
fe = flow[1].pkt_.FlowFetch();
vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//1. Remove remote VM routes
DeleteRemoteRoute("vrf5", remote_vm4_ip);
client->WaitForIdle();
}
//Duplicate Ingress flow test (VMport to VMport - Same VN)
//Flow creation using IP
TEST_F(FlowTest, FlowAdd_5) {
TestFlow flow[] = {
//Send an ICMP flow from remote VM in vn3 to local VM in vn5
{
TestFlowPkt(Address::INET, vm1_ip, vm2_ip, 1, 0, 0, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn5"),
}
}
};
CreateFlow(flow, 1);
EXPECT_EQ(2U, agent()->pkt()->flow_table()->Size());
//Verify ingress and egress flow count of VN "vn5"
uint32_t in_count, out_count;
const FlowEntry *fe = flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//Send duplicate flow creation request
CreateFlow(flow, 1);
EXPECT_EQ(2U, agent()->pkt()->flow_table()->Size());
//Verify ingress and egress flow count for VN "vn5" does not change
fe = flow[0].pkt_.FlowFetch();
vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
}
//Duplicate Ingress flow test for flow having reverse flow (VMport to VMport - Same VN)
//Flow creation using TCP packets
TEST_F(FlowTest, FlowAdd_6) {
TestFlow fwd_flow[] = {
{
TestFlowPkt(Address::INET, vm1_ip, vm2_ip, IPPROTO_TCP, 1000, 200, "vrf5",
flow0->id()),
{
new VerifyVn("vn5", "vn5"),
}
}
};
TestFlow rev_flow[] = {
{
TestFlowPkt(Address::INET, vm2_ip, vm1_ip, IPPROTO_TCP, 200, 1000, "vrf5",
flow1->id()),
{
new VerifyVn("vn5", "vn5"),
}
}
};
CreateFlow(fwd_flow, 1);
EXPECT_EQ(2U, agent()->pkt()->flow_table()->Size());
//Verify ingress and egress flow count of VN "vn5"
uint32_t in_count, out_count;
const FlowEntry *fe = fwd_flow[0].pkt_.FlowFetch();
const VnEntry *vn = fe->data().vn_entry.get();
agent()->pkt()->flow_table()->VnFlowCounters(vn, &in_count, &out_count);
EXPECT_EQ(2U, in_count);
EXPECT_EQ(2U, out_count);
//Send request for reverse flow
CreateFlow(rev_flow, 1);
//Send request for reverse flow again
CreateFlow(rev_flow, 1);
//Send request for forward flow again