-
Notifications
You must be signed in to change notification settings - Fork 390
/
flow_table.cc
3971 lines (3529 loc) · 134 KB
/
flow_table.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 <vector>
#include <bitset>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/unordered_map.hpp>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_trace.h>
#include <pkt/flow_table.h>
#include <vrouter/flow_stats/flow_stats_collector.h>
#include <vrouter/ksync/ksync_init.h>
#include <ksync/ksync_entry.h>
#include <vrouter/ksync/flowtable_ksync.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "base/os.h"
#include "route/route.h"
#include "cmn/agent_cmn.h"
#include "oper/interface_common.h"
#include "oper/nexthop.h"
#include "init/agent_param.h"
#include "cmn/agent_cmn.h"
#include "oper/route_common.h"
#include "oper/vrf.h"
#include "oper/vm.h"
#include "oper/sg.h"
#include "filter/packet_header.h"
#include "filter/acl.h"
#include "pkt/proto.h"
#include "pkt/proto_handler.h"
#include "pkt/pkt_handler.h"
#include "pkt/flow_proto.h"
#include "pkt/pkt_types.h"
#include "pkt/pkt_sandesh_flow.h"
#include "pkt/agent_stats.h"
#include "uve/agent_uve.h"
#include "uve/vm_uve_table.h"
#include "uve/vn_uve_table.h"
#include "uve/vrouter_uve_entry.h"
using boost::assign::map_list_of;
const std::map<FlowEntry::FlowPolicyState, const char*>
FlowEntry::FlowPolicyStateStr = map_list_of
(NOT_EVALUATED, "00000000-0000-0000-0000-000000000000")
(IMPLICIT_ALLOW, "00000000-0000-0000-0000-000000000001")
(IMPLICIT_DENY, "00000000-0000-0000-0000-000000000002")
(DEFAULT_GW_ICMP_OR_DNS, "00000000-0000-0000-0000-000000000003")
(LINKLOCAL_FLOW, "00000000-0000-0000-0000-000000000004")
(MULTICAST_FLOW, "00000000-0000-0000-0000-000000000005")
(NON_IP_FLOW, "00000000-0000-0000-0000-000000000006");
const std::map<uint16_t, const char*>
FlowEntry::FlowDropReasonStr = boost::assign::map_list_of
((uint16_t)DROP_UNKNOWN, "UNKNOWN")
((uint16_t)SHORT_UNAVIALABLE_INTERFACE,
"SHORT_UNAVIALABLE_INTERFACE")
((uint16_t)SHORT_IPV4_FWD_DIS, "SHORT_IPV4_FWD_DIS")
((uint16_t)SHORT_UNAVIALABLE_VRF,
"SHORT_UNAVIALABLE_VRF")
((uint16_t)SHORT_NO_SRC_ROUTE, "SHORT_NO_SRC_ROUTE")
((uint16_t)SHORT_NO_DST_ROUTE, "SHORT_NO_DST_ROUTE")
((uint16_t)SHORT_AUDIT_ENTRY, "SHORT_AUDIT_ENTRY")
((uint16_t)SHORT_VRF_CHANGE, "SHORT_VRF_CHANGE")
((uint16_t)SHORT_NO_REVERSE_FLOW, "SHORT_NO_REVERSE_FLOW")
((uint16_t)SHORT_REVERSE_FLOW_CHANGE,
"SHORT_REVERSE_FLOW_CHANGE")
((uint16_t)SHORT_NAT_CHANGE, "SHORT_NAT_CHANGE")
((uint16_t)SHORT_FLOW_LIMIT, "SHORT_FLOW_LIMIT")
((uint16_t)SHORT_LINKLOCAL_SRC_NAT,
"SHORT_LINKLOCAL_SRC_NAT")
((uint16_t)SHORT_FAILED_VROUTER_INSTALL,
"SHORT_FAILED_VROUTER_INST")
((uint16_t)SHORT_INVALID_L2_FLOW, "SHORT_INVALID_L2_FLOW")
((uint16_t)DROP_POLICY, "DROP_POLICY")
((uint16_t)DROP_OUT_POLICY, "DROP_OUT_POLICY")
((uint16_t)DROP_SG, "DROP_SG")
((uint16_t)DROP_OUT_SG, "DROP_OUT_SG")
((uint16_t)DROP_REVERSE_SG, "DROP_REVERSE_SG")
((uint16_t)DROP_REVERSE_OUT_SG, "DROP_REVERSE_OUT_SG");
boost::uuids::random_generator FlowTable::rand_gen_ = boost::uuids::random_generator();
tbb::atomic<int> FlowEntry::alloc_count_;
SecurityGroupList FlowTable::default_sg_list_;
static bool ShouldDrop(uint32_t action) {
if ((action & TrafficAction::DROP_FLAGS) || (action & TrafficAction::IMPLICIT_DENY_FLAGS))
return true;
return false;
}
static void SetAclListAceId(const AclDBEntry *acl, const std::list<MatchAclParams> &acl_l,
std::vector<AceId> &ace_l) {
std::list<MatchAclParams>::const_iterator ma_it;
for (ma_it = acl_l.begin();
ma_it != acl_l.end();
++ma_it) {
if ((*ma_it).acl != acl) {
continue;
}
AclEntryIDList::const_iterator ait;
for (ait = (*ma_it).ace_id_list.begin();
ait != (*ma_it).ace_id_list.end(); ++ ait) {
AceId ace_id;
ace_id.id = *ait;
ace_l.push_back(ace_id);
}
}
}
FlowEntry::FlowEntry(const FlowKey &k) :
key_(k), data_(), stats_(), l3_flow_(true),
flow_handle_(kInvalidFlowHandle),
ksync_entry_(NULL), deleted_(false), flags_(0),
short_flow_reason_(SHORT_UNKNOWN),
linklocal_src_port_(),
linklocal_src_port_fd_(PktFlowInfo::kLinkLocalInvalidFd),
peer_vrouter_(), tunnel_type_(TunnelType::INVALID),
underlay_source_port_(0), underlay_sport_exported_(false) {
flow_uuid_ = FlowTable::rand_gen_();
egress_uuid_ = FlowTable::rand_gen_();
refcount_ = 0;
nw_ace_uuid_ = FlowPolicyStateStr.at(NOT_EVALUATED);
sg_rule_uuid_= FlowPolicyStateStr.at(NOT_EVALUATED);
alloc_count_.fetch_and_increment();
}
FlowEntry::~FlowEntry() {
if (is_flags_set(FlowEntry::LinkLocalBindLocalSrcPort) &&
(linklocal_src_port_fd_ == PktFlowInfo::kLinkLocalInvalidFd ||
!linklocal_src_port_)) {
LOG(DEBUG, "Linklocal Flow Inconsistency fd = " <<
linklocal_src_port_fd_ << " port = " << linklocal_src_port_ <<
" flow index = " << flow_handle_ << " source = " <<
key_.src_addr.to_string() << " dest = " <<
key_.dst_addr.to_string() << " protocol = " << key_.protocol <<
" sport = " << key_.src_port << " dport = " << key_.dst_port);
}
if (linklocal_src_port_fd_ != PktFlowInfo::kLinkLocalInvalidFd) {
close(linklocal_src_port_fd_);
Agent::GetInstance()->pkt()->flow_table()->
DelLinkLocalFlowInfo(linklocal_src_port_fd_);
}
alloc_count_.fetch_and_decrement();
}
std::string FlowEntry::DropReasonStr(uint16_t reason) {
std::map<uint16_t, const char*>::const_iterator it =
FlowDropReasonStr.find(reason);
if (it != FlowDropReasonStr.end()) {
return string(it->second);
}
return "UNKNOWN";
}
void FlowEntry::GetSourceRouteInfo(const AgentRoute *rt) {
const AgentPath *path = NULL;
if (rt) {
path = rt->GetActivePath();
}
if (path == NULL) {
data_.source_vn = FlowHandler::UnknownVn();
data_.source_sg_id_l = FlowTable::default_sg_list();
data_.source_plen = 0;
} else {
data_.source_vn = path->dest_vn_name();
data_.source_sg_id_l = path->sg_list();
data_.source_plen = rt->plen();
}
}
void FlowEntry::GetDestRouteInfo(const AgentRoute *rt) {
const AgentPath *path = NULL;
if (rt) {
path = rt->GetActivePath();
}
if (path == NULL) {
data_.dest_vn = FlowHandler::UnknownVn();
data_.dest_sg_id_l = FlowTable::default_sg_list();
data_.dest_plen = 0;
} else {
data_.dest_vn = path->dest_vn_name();
data_.dest_sg_id_l = path->sg_list();
data_.dest_plen = rt->plen();
}
}
uint32_t FlowEntry::MatchAcl(const PacketHeader &hdr,
std::list<MatchAclParams> &acl,
bool add_implicit_deny, bool add_implicit_allow,
FlowPolicyInfo *info) {
PktHandler *pkt_handler = Agent::GetInstance()->pkt()->pkt_handler();
// If there are no ACL to match, make it pass
if (acl.size() == 0 && add_implicit_allow) {
if (info) {
/* We are setting UUIDs for linklocal and multicast flows here,
* because even if we move this to the place where acl association
* is being skipped, we still need checks for linklocal and
* multicast flows here to avoid its value being overwritten with
* IMPLICIT_ALLOW
*/
if (is_flags_set(FlowEntry::LinkLocalFlow)) {
info->uuid = FlowPolicyStateStr.at(LINKLOCAL_FLOW);
} else if (is_flags_set(FlowEntry::Multicast)) {
info->uuid = FlowPolicyStateStr.at(MULTICAST_FLOW);
} else {
/* We need to make sure that info is not already populated
* before setting it to IMPLICIT_ALLOW. This is required
* because info could earlier be set by previous call to
* MatchAcl. We should note here that same 'info' var is passed
* for MatchAcl calls with in_acl and out_acl
*/
if (!info->terminal && !info->other) {
info->uuid = FlowPolicyStateStr.at(IMPLICIT_ALLOW);
}
}
}
return (1 << TrafficAction::PASS);
}
// PASS default GW traffic, if it is ICMP or DNS
if ((hdr.protocol == IPPROTO_ICMP ||
(hdr.protocol == IPPROTO_UDP &&
(hdr.src_port == DNS_SERVER_PORT ||
hdr.dst_port == DNS_SERVER_PORT))) &&
(pkt_handler->IsGwPacket(data_.intf_entry.get(), hdr.dst_ip) ||
pkt_handler->IsGwPacket(data_.intf_entry.get(), hdr.src_ip))) {
if (info) {
info->uuid = FlowPolicyStateStr.at(DEFAULT_GW_ICMP_OR_DNS);
}
return (1 << TrafficAction::PASS);
}
uint32_t action = 0;
for (std::list<MatchAclParams>::iterator it = acl.begin();
it != acl.end(); ++it) {
if (it->acl.get() == NULL) {
continue;
}
if (it->acl->PacketMatch(hdr, *it, info)) {
action |= it->action_info.action;
if (it->action_info.action & (1 << TrafficAction::MIRROR)) {
data_.match_p.action_info.mirror_l.insert
(data_.match_p.action_info.mirror_l.end(),
it->action_info.mirror_l.begin(),
it->action_info.mirror_l.end());
}
if (it->terminal_rule) {
break;
}
}
}
// If no acl matched, make it imlicit deny
if (action == 0 && add_implicit_deny) {
action = (1 << TrafficAction::DENY) |
(1 << TrafficAction::IMPLICIT_DENY);
if (info) {
info->uuid = FlowPolicyStateStr.at(IMPLICIT_DENY);
info->drop = true;
}
}
return action;
}
void FlowEntry::ResetStats() {
stats_.bytes = 0;
stats_.packets = 0;
}
// Recompute FlowEntry action
bool FlowEntry::ActionRecompute() {
uint32_t action = 0;
action = data_.match_p.policy_action | data_.match_p.out_policy_action |
data_.match_p.sg_action_summary |
data_.match_p.mirror_action | data_.match_p.out_mirror_action;
//Only VRF assign acl, can specify action to
//translate VRF. VRF translate action specified
//by egress VN ACL or ingress VN ACL should be ignored
action &= ~(1 << TrafficAction::VRF_TRANSLATE);
action |= data_.match_p.vrf_assign_acl_action;
if (action & (1 << TrafficAction::VRF_TRANSLATE) &&
data_.match_p.action_info.vrf_translate_action_.ignore_acl() == true) {
//In case of multi inline service chain, match condition generated on
//each of service instance interface takes higher priority than
//network ACL. Match condition on the interface would have ignore acl flag
//set to avoid applying two ACL for vrf translation
action = data_.match_p.vrf_assign_acl_action |
data_.match_p.sg_action_summary | data_.match_p.mirror_action |
data_.match_p.out_mirror_action;
//Pick mirror action from network ACL
if (data_.match_p.policy_action & (1 << TrafficAction::MIRROR) ||
data_.match_p.out_policy_action & (1 << TrafficAction::MIRROR)) {
action |= (1 << TrafficAction::MIRROR);
}
}
// Force short flows to DROP
if (is_flags_set(FlowEntry::ShortFlow)) {
action |= (1 << TrafficAction::DENY);
}
// check for conflicting actions and remove allowed action
if (ShouldDrop(action)) {
action = (action & ~TrafficAction::DROP_FLAGS & ~TrafficAction::PASS_FLAGS);
action |= (1 << TrafficAction::DENY);
if (is_flags_set(FlowEntry::ShortFlow)) {
data_.drop_reason = short_flow_reason_;
} else if (ShouldDrop(data_.match_p.policy_action)) {
data_.drop_reason = DROP_POLICY;
} else if (ShouldDrop(data_.match_p.out_policy_action)){
data_.drop_reason = DROP_OUT_POLICY;
} else if (ShouldDrop(data_.match_p.sg_action)){
data_.drop_reason = DROP_SG;
} else if (ShouldDrop(data_.match_p.out_sg_action)){
data_.drop_reason = DROP_OUT_SG;
} else if (ShouldDrop(data_.match_p.reverse_sg_action)){
data_.drop_reason = DROP_REVERSE_SG;
} else if (ShouldDrop(data_.match_p.reverse_out_sg_action)){
data_.drop_reason = DROP_REVERSE_OUT_SG;
} else {
data_.drop_reason = DROP_UNKNOWN;
}
}
if (action & (1 << TrafficAction::TRAP)) {
action = (1 << TrafficAction::TRAP);
}
if (action != data_.match_p.action_info.action) {
data_.match_p.action_info.action = action;
return true;
}
return false;
}
void FlowEntry::SetPacketHeader(PacketHeader *hdr) {
hdr->vrf = data_.vrf;
hdr->src_ip = key_.src_addr;
hdr->dst_ip = key_.dst_addr;
hdr->protocol = key_.protocol;
if (hdr->protocol == IPPROTO_UDP || hdr->protocol == IPPROTO_TCP) {
hdr->src_port = key_.src_port;
hdr->dst_port = key_.dst_port;
} else {
hdr->src_port = 0;
hdr->dst_port = 0;
}
hdr->src_policy_id = &(data_.source_vn);
hdr->dst_policy_id = &(data_.dest_vn);
hdr->src_sg_id_l = &(data_.source_sg_id_l);
hdr->dst_sg_id_l = &(data_.dest_sg_id_l);
}
// In case of NAT flows, the key fields can change.
void FlowEntry::SetOutPacketHeader(PacketHeader *hdr) {
FlowEntry *rflow = reverse_flow_entry();
if (rflow == NULL)
return;
hdr->vrf = rflow->data().vrf;
hdr->src_ip = rflow->key().dst_addr;
hdr->dst_ip = rflow->key().src_addr;
hdr->protocol = rflow->key().protocol;
if (hdr->protocol == IPPROTO_UDP || hdr->protocol == IPPROTO_TCP) {
hdr->src_port = rflow->key().dst_port;
hdr->dst_port = rflow->key().src_port;
} else {
hdr->src_port = 0;
hdr->dst_port = 0;
}
hdr->src_policy_id = &(rflow->data().dest_vn);
hdr->dst_policy_id = &(rflow->data().source_vn);
hdr->src_sg_id_l = &(rflow->data().dest_sg_id_l);
hdr->dst_sg_id_l = &(rflow->data().source_sg_id_l);
}
void FlowEntry::UpdateRpf() {
if (data_.vn_entry) {
data_.enable_rpf = data_.vn_entry->enable_rpf();
} else {
data_.enable_rpf = true;
}
}
// Apply Policy and SG rules for a flow.
//
// Special case of local flows:
// For local-flows, both VM are on same compute and we need to apply SG from
// both the ports. m_sg_acl_l will contain ACL for port in forward flow and
// m_out_sg_acl_l will have ACL from other port
//
// If forward flow goes thru NAT, the key for matching ACL in
// m_out_sg_acl_l can potentially change. The routine SetOutPacketHeader
// takes care of forming header after NAT
//
// Rules applied are based on flow type
// Non-Local Forward Flow
// Network Policy.
// Out-Network Policy will be empty
// SG
// Out-SG will be empty
// Non-Local Reverse Flow
// Network Policy.
// Out-Network Policy will be empty
// SG and out-SG from forward flow
// Local Forward Flow
// Network Policy.
// Out-Network Policy
// SG
// Out-SG
// Local Reverse Flow
// Network Policy.
// Out-Network Policy
// SG and out-SG from forward flow
bool FlowEntry::DoPolicy() {
data_.match_p.action_info.Clear();
data_.match_p.policy_action = 0;
data_.match_p.out_policy_action = 0;
data_.match_p.sg_action = 0;
data_.match_p.out_sg_action = 0;
data_.match_p.reverse_sg_action = 0;
data_.match_p.reverse_out_sg_action = 0;
data_.match_p.mirror_action = 0;
data_.match_p.out_mirror_action = 0;
data_.match_p.sg_action_summary = 0;
const string value = FlowPolicyStateStr.at(NOT_EVALUATED);
FlowPolicyInfo nw_acl_info(value), sg_acl_info(value);
FlowPolicyInfo rev_sg_acl_info(value);
FlowEntry *rflow = reverse_flow_entry();
PacketHeader hdr;
SetPacketHeader(&hdr);
//Calculate VRF assign entry, and ignore acl is set
//skip network and SG acl action is set
data_.match_p.vrf_assign_acl_action =
MatchAcl(hdr, data_.match_p.m_vrf_assign_acl_l, false, true, NULL);
// Mirror is valid even if packet is to be dropped. So, apply it first
data_.match_p.mirror_action = MatchAcl(hdr, data_.match_p.m_mirror_acl_l,
false, true, NULL);
// Apply out-policy. Valid only for local-flow
data_.match_p.out_mirror_action = MatchAcl(hdr,
data_.match_p.m_out_mirror_acl_l, false, true, NULL);
// Apply network policy
data_.match_p.policy_action = MatchAcl(hdr, data_.match_p.m_acl_l, true,
true, &nw_acl_info);
if (ShouldDrop(data_.match_p.policy_action)) {
goto done;
}
data_.match_p.out_policy_action = MatchAcl(hdr, data_.match_p.m_out_acl_l,
true, true, &nw_acl_info);
if (ShouldDrop(data_.match_p.policy_action)) {
goto done;
}
// Apply security-group
if (!is_flags_set(FlowEntry::ReverseFlow)) {
data_.match_p.sg_action = MatchAcl(hdr, data_.match_p.m_sg_acl_l, true,
!data_.match_p.sg_rule_present,
&sg_acl_info);
PacketHeader out_hdr;
if (ShouldDrop(data_.match_p.sg_action) == false && rflow) {
// Key fields for lookup in out-acl can potentially change in case
// of NAT. Form ACL lookup based on post-NAT fields
SetOutPacketHeader(&out_hdr);
data_.match_p.out_sg_action =
MatchAcl(out_hdr, data_.match_p.m_out_sg_acl_l, true,
!data_.match_p.out_sg_rule_present, &sg_acl_info);
}
// For TCP-ACK packet, we allow packet if either forward or reverse
// flow says allow. So, continue matching reverse flow even if forward
// flow says drop
if (is_flags_set(FlowEntry::TcpAckFlow) && rflow) {
rflow->SetPacketHeader(&hdr);
data_.match_p.reverse_sg_action =
MatchAcl(hdr, data_.match_p.m_reverse_sg_acl_l, true,
!data_.match_p.reverse_sg_rule_present,
&rev_sg_acl_info);
if (ShouldDrop(data_.match_p.reverse_sg_action) == false) {
// Key fields for lookup in out-acl can potentially change in
// case of NAT. Form ACL lookup based on post-NAT fields
rflow->SetOutPacketHeader(&out_hdr);
data_.match_p.reverse_out_sg_action =
MatchAcl(out_hdr, data_.match_p.m_reverse_out_sg_acl_l, true,
!data_.match_p.reverse_out_sg_rule_present,
&rev_sg_acl_info);
}
}
// Compute summary SG action.
// For Non-TCP-ACK Flows
// DROP if any of sg_action, sg_out_action, reverse_sg_action or
// reverse_out_sg_action says DROP
// Only sg_acl_info which is derived from data_.match_p.m_sg_acl_l
// and data_.match_p.m_out_sg_acl_l will be populated. Pick the
// UUID specified by sg_acl_info for flow's SG rule UUID
// For TCP-ACK flows
// ALLOW if either ((sg_action && sg_out_action) ||
// (reverse_sg_action & reverse_out_sg_action)) ALLOW
// For flow's SG rule UUID use the following rules
// --If both sg_acl_info and rev_sg_acl_info has drop set, pick the
// UUID from sg_acl_info.
// --If either of sg_acl_info or rev_sg_acl_info does not have drop
// set, pick the UUID from the one which does not have drop set.
// --If both of them does not have drop set, pick it up from
// sg_acl_info
//
data_.match_p.sg_action_summary = 0;
if (!is_flags_set(FlowEntry::TcpAckFlow)) {
data_.match_p.sg_action_summary =
data_.match_p.sg_action |
data_.match_p.out_sg_action |
data_.match_p.reverse_sg_action |
data_.match_p.reverse_out_sg_action;
sg_rule_uuid_ = sg_acl_info.uuid;
} else {
if (ShouldDrop(data_.match_p.sg_action |
data_.match_p.out_sg_action)
&&
ShouldDrop(data_.match_p.reverse_sg_action |
data_.match_p.reverse_out_sg_action)) {
data_.match_p.sg_action_summary = (1 << TrafficAction::DENY);
sg_rule_uuid_ = sg_acl_info.uuid;
} else {
data_.match_p.sg_action_summary = (1 << TrafficAction::PASS);
if (!ShouldDrop(data_.match_p.sg_action |
data_.match_p.out_sg_action)) {
sg_rule_uuid_ = sg_acl_info.uuid;
} else if (!ShouldDrop(data_.match_p.reverse_sg_action |
data_.match_p.reverse_out_sg_action)) {
sg_rule_uuid_ = rev_sg_acl_info.uuid;
}
}
}
} else {
// SG is reflexive ACL. For reverse-flow, copy SG action from
// forward flow
UpdateReflexiveAction();
}
done:
nw_ace_uuid_ = nw_acl_info.uuid;
// Set mirror vrf after evaluation of actions
SetMirrorVrfFromAction();
//Set VRF assign action
SetVrfAssignEntry();
// Summarize the actions based on lookups above
ActionRecompute();
return true;
}
void FlowEntry::SetVrfAssignEntry() {
if (!(data_.match_p.vrf_assign_acl_action &
(1 << TrafficAction::VRF_TRANSLATE))) {
data_.vrf_assign_evaluated = true;
return;
}
std::string vrf_assigned_name =
data_.match_p.action_info.vrf_translate_action_.vrf_name();
std::list<MatchAclParams>::const_iterator acl_it;
for (acl_it = match_p().m_vrf_assign_acl_l.begin();
acl_it != match_p().m_vrf_assign_acl_l.end();
++acl_it) {
std::string vrf = acl_it->action_info.vrf_translate_action_.vrf_name();
data_.match_p.action_info.vrf_translate_action_.set_vrf_name(vrf);
//Check if VRF assign acl says, network ACL and SG action
//to be ignored
bool ignore_acl = acl_it->action_info.vrf_translate_action_.ignore_acl();
data_.match_p.action_info.vrf_translate_action_.set_ignore_acl(ignore_acl);
}
if (data_.vrf_assign_evaluated && vrf_assigned_name !=
data_.match_p.action_info.vrf_translate_action_.vrf_name()) {
MakeShortFlow(SHORT_VRF_CHANGE);
}
set_acl_assigned_vrf_index();
if (acl_assigned_vrf_index() == 0) {
MakeShortFlow(SHORT_VRF_CHANGE);
}
data_.vrf_assign_evaluated = true;
}
// SetMirrorVrfFromAction
// For this flow check for mirror action from dynamic ACLs or policy mirroring
// assign the vrf from its Virtual Nework that ACL is used
// If it is a local flow and out mirror action or policy is set
// assign the vrf of the reverse flow, since ACL came from the reverse flow
void FlowEntry::SetMirrorVrfFromAction() {
if (data_.match_p.mirror_action & (1 << TrafficAction::MIRROR) ||
data_.match_p.policy_action & (1 << TrafficAction::MIRROR)) {
const VnEntry *vn = vn_entry();
if (vn && vn->GetVrf()) {
SetMirrorVrf(vn->GetVrf()->vrf_id());
}
}
if (data_.match_p.out_mirror_action & (1 << TrafficAction::MIRROR) ||
data_.match_p.out_policy_action & (1 << TrafficAction::MIRROR)) {
FlowEntry *rflow = reverse_flow_entry_.get();
if (rflow) {
const VnEntry *rvn = rflow->vn_entry();
if (rvn && rvn->GetVrf()) {
SetMirrorVrf(rvn->GetVrf()->vrf_id());
}
}
}
}
// Ingress-ACL/Egress-ACL in interface with VM as reference point.
// Ingress : Packet to VM
// Egress : Packet from VM
// The direction stored in flow is defined with vrouter as reference point
// Ingress : Packet to Vrouter from VM
// Egress : Packet from Vrouter to VM
//
// Function takes care of copying right rules
static bool CopySgEntries(const VmInterface *vm_port, bool ingress_acl,
std::list<MatchAclParams> &list) {
bool ret = false;
for (VmInterface::SecurityGroupEntrySet::const_iterator it =
vm_port->sg_list().list_.begin();
it != vm_port->sg_list().list_.end(); ++it) {
if (it->sg_ == NULL)
continue;
if (it->sg_->IsAclSet()) {
ret = true;
}
MatchAclParams acl;
// As per definition above,
// get EgressACL if flow direction is Ingress
// get IngressACL if flow direction is Egress
if (ingress_acl) {
acl.acl = it->sg_->GetEgressAcl();
} else {
acl.acl = it->sg_->GetIngressAcl();
}
if (acl.acl)
list.push_back(acl);
}
return ret;
}
void FlowEntry::GetLocalFlowSgList(const VmInterface *vm_port,
const VmInterface *reverse_vm_port) {
// Get SG-Rule for the forward flow
data_.match_p.sg_rule_present = CopySgEntries(vm_port, true,
data_.match_p.m_sg_acl_l);
// For local flow, we need to simulate SG lookup at both ends.
// Assume packet is from VM-A to VM-B.
// If we apply Ingress-ACL from VM-A, then apply Egress-ACL from VM-B
// If we apply Egress-ACL from VM-A, then apply Ingress-ACL from VM-B
if (reverse_vm_port) {
data_.match_p.out_sg_rule_present =
CopySgEntries(reverse_vm_port, false, data_.match_p.m_out_sg_acl_l);
}
if (!is_flags_set(FlowEntry::TcpAckFlow)) {
return;
}
// TCP ACK workaround:
// Ideally TCP State machine should be run to age TCP flows
// Temporary workaound in place of state machine. For TCP ACK packets allow
// the flow if either forward or reverse flow is allowed
// Copy the SG rules to be applied for reverse flow
data_.match_p.reverse_out_sg_rule_present =
CopySgEntries(vm_port, false,
data_.match_p.m_reverse_out_sg_acl_l);
if (reverse_vm_port) {
data_.match_p.reverse_sg_rule_present =
CopySgEntries(reverse_vm_port, true,
data_.match_p.m_reverse_sg_acl_l);
}
}
void FlowEntry::GetNonLocalFlowSgList(const VmInterface *vm_port) {
// Get SG-Rule for the forward flow
bool ingress = is_flags_set(FlowEntry::IngressDir);
data_.match_p.sg_rule_present = CopySgEntries(vm_port, ingress,
data_.match_p.m_sg_acl_l);
data_.match_p.out_sg_rule_present = false;
if (!is_flags_set(FlowEntry::TcpAckFlow)) {
return;
}
// TCP ACK workaround:
// Ideally TCP State machine should be run to age TCP flows
// Temporary workaound in place of state machine. For TCP ACK packets allow
// the flow if either forward or reverse flow is allowed
// Copy the SG rules to be applied for reverse flow
data_.match_p.reverse_out_sg_rule_present =
CopySgEntries(vm_port, !ingress,
data_.match_p.m_reverse_out_sg_acl_l);
data_.match_p.reverse_sg_rule_present = false;
}
void FlowEntry::GetSgList(const Interface *intf) {
// Dont apply network-policy for linklocal and multicast flows
if (is_flags_set(FlowEntry::LinkLocalFlow) ||
is_flags_set(FlowEntry::Multicast)) {
return;
}
// SG ACL's are reflexive. Skip SG for reverse flow
if (is_flags_set(FlowEntry::ReverseFlow)) {
return;
}
// Get virtual-machine port for forward flow
const VmInterface *vm_port = NULL;
if (intf != NULL) {
if (intf->type() == Interface::VM_INTERFACE) {
vm_port = static_cast<const VmInterface *>(intf);
}
}
if (vm_port == NULL) {
return;
}
// Get virtual-machine port for reverse flow
FlowEntry *rflow = reverse_flow_entry();
const VmInterface *reverse_vm_port = NULL;
if (rflow != NULL) {
if (rflow->data().intf_entry.get() != NULL) {
if (rflow->data().intf_entry->type() == Interface::VM_INTERFACE) {
reverse_vm_port = static_cast<const VmInterface *>
(rflow->data().intf_entry.get());
}
}
}
// Get SG-Rules
if (is_flags_set(FlowEntry::LocalFlow)) {
GetLocalFlowSgList(vm_port, reverse_vm_port);
} else {
GetNonLocalFlowSgList(vm_port);
}
}
void FlowEntry::ResetPolicy() {
/* Reset acl list*/
data_.match_p.m_acl_l.clear();
data_.match_p.m_out_acl_l.clear();
data_.match_p.m_mirror_acl_l.clear();
data_.match_p.m_out_mirror_acl_l.clear();
/* Reset sg acl list*/
data_.match_p.sg_rule_present = false;
data_.match_p.m_sg_acl_l.clear();
data_.match_p.out_sg_rule_present = false;
data_.match_p.m_out_sg_acl_l.clear();
data_.match_p.reverse_sg_rule_present = false;
data_.match_p.m_reverse_sg_acl_l.clear();
data_.match_p.reverse_out_sg_rule_present = false;
data_.match_p.m_reverse_out_sg_acl_l.clear();
data_.match_p.m_vrf_assign_acl_l.clear();
}
void FlowEntry::GetPolicy(const VnEntry *vn) {
if (vn == NULL)
return;
MatchAclParams acl;
// Get Mirror configuration first
if (vn->GetMirrorAcl()) {
acl.acl = vn->GetMirrorAcl();
data_.match_p.m_mirror_acl_l.push_back(acl);
}
if (vn->GetMirrorCfgAcl()) {
acl.acl = vn->GetMirrorCfgAcl();
data_.match_p.m_mirror_acl_l.push_back(acl);
}
// Dont apply network-policy for linklocal and subnet broadcast flow
if (is_flags_set(FlowEntry::LinkLocalFlow) ||
is_flags_set(FlowEntry::Multicast)) {
return;
}
if (vn->GetAcl()) {
acl.acl = vn->GetAcl();
data_.match_p.m_acl_l.push_back(acl);
}
const VnEntry *rvn = NULL;
FlowEntry *rflow = reverse_flow_entry_.get();
// For local flows, we have to apply NW Policy from out-vn also
if (!is_flags_set(FlowEntry::LocalFlow) || rflow == NULL) {
// Not local flow
return;
}
rvn = rflow->vn_entry();
if (rvn == NULL) {
return;
}
if (rvn->GetAcl()) {
acl.acl = rvn->GetAcl();
data_.match_p.m_out_acl_l.push_back(acl);
}
if (rvn->GetMirrorAcl()) {
acl.acl = rvn->GetMirrorAcl();
data_.match_p.m_out_mirror_acl_l.push_back(acl);
}
if (rvn->GetMirrorCfgAcl()) {
acl.acl = rvn->GetMirrorCfgAcl();
data_.match_p.m_out_mirror_acl_l.push_back(acl);
}
}
void FlowEntry::GetVrfAssignAcl() {
if (data_.intf_entry == NULL) {
return;
}
if (data_.intf_entry->type() != Interface::VM_INTERFACE) {
return;
}
if (is_flags_set(FlowEntry::LinkLocalFlow) ||
is_flags_set(FlowEntry::Multicast)) {
return;
}
const VmInterface *intf =
static_cast<const VmInterface *>(data_.intf_entry.get());
//If interface has a VRF assign rule, choose the acl and match the
//packet, else get the acl attached to VN and try matching the packet to
//network acl
const AclDBEntry* acl = NULL;
if (is_flags_set(FlowEntry::NatFlow) == false) {
acl = intf->vrf_assign_acl();
}
if (acl == NULL) {
acl = data_.vn_entry.get()->GetAcl();
}
if (!acl) {
return;
}
MatchAclParams m_acl;
m_acl.acl = acl;
data_.match_p.m_vrf_assign_acl_l.push_back(m_acl);
}
const std::string& FlowEntry::acl_assigned_vrf() const {
return data_.match_p.action_info.vrf_translate_action_.vrf_name();
}
void FlowEntry::set_acl_assigned_vrf_index() {
VrfKey vrf_key(data_.match_p.action_info.vrf_translate_action_.vrf_name());
const VrfEntry *vrf = static_cast<const VrfEntry *>(
Agent::GetInstance()->vrf_table()->FindActiveEntry(&vrf_key));
if (vrf) {
data_.acl_assigned_vrf_index_ = vrf->vrf_id();
return;
}
data_.acl_assigned_vrf_index_ = VrfEntry::kInvalidIndex;
}
uint32_t FlowEntry::acl_assigned_vrf_index() const {
return data_.acl_assigned_vrf_index_;
}
void FlowEntry::UpdateKSync(FlowTable* table, bool update) {
FlowInfo flow_info;
FillFlowInfo(flow_info);
if (deleted_ == false && stats_.last_modified_time != stats_.setup_time) {
/*
* Export flow only if it has been modified after setup. Flow export
* for setup happens during stats updation.
*/
FlowStatsManager *sm = table->agent()->flow_stats_manager();
sm->FlowExportEvent(this);
}
FlowTableKSyncObject *ksync_obj =
Agent::GetInstance()->ksync()->flowtable_ksync_obj();
if (ksync_entry_ == NULL) {
FLOW_TRACE(Trace, "Add", flow_info);
FlowTableKSyncEntry key(ksync_obj, this, flow_handle_);
ksync_entry_ =
static_cast<FlowTableKSyncEntry *>(ksync_obj->Create(&key));
if (deleted_) {
/*
* Create and delete a KSync Entry when update ksync entry is
* triggered for a deleted flow entry.
* This happens when Reverse flow deleted is deleted before
* getting an ACK from vrouter.
*/
ksync_obj->Delete(ksync_entry_);
ksync_entry_ = NULL;
}
} else {
if (flow_handle_ != ksync_entry_->hash_id()) {
/*
* if flow handle changes delete the previous record from
* vrouter and install new
*/
ksync_obj->Delete(ksync_entry_);
FlowTableKSyncEntry key(ksync_obj, this, flow_handle_);
ksync_entry_ =
static_cast<FlowTableKSyncEntry *>(ksync_obj->Create(&key));
} else {
ksync_obj->Change(ksync_entry_);
}
}
}
void FlowEntry::MakeShortFlow(FlowShortReason reason) {
if (!is_flags_set(FlowEntry::ShortFlow)) {
set_flags(FlowEntry::ShortFlow);
short_flow_reason_ = reason;
}
if (reverse_flow_entry_ &&
!reverse_flow_entry_->is_flags_set(FlowEntry::ShortFlow)) {
reverse_flow_entry_->set_flags(FlowEntry::ShortFlow);
reverse_flow_entry_->short_flow_reason_ = reason;
}
}
void FlowEntry::GetPolicyInfo(const VnEntry *vn) {
// Default make it false
ResetPolicy();
// Short flows means there is some information missing for the flow. Skip
// getting policy information for short flow. When the information is
// complete, GetPolicyInfo is called again
if (is_flags_set(FlowEntry::ShortFlow)) {
return;
}
// ACL supported on VMPORT interfaces only
if (data_.intf_entry == NULL)
return;
if (data_.intf_entry->type() != Interface::VM_INTERFACE)
return;
// Get Network policy/mirror cfg policy/mirror policies
GetPolicy(vn);
// Get Sg list
GetSgList(data_.intf_entry.get());
//Get VRF translate ACL
GetVrfAssignAcl();
}
void FlowEntry::GetPolicyInfo() {
GetPolicyInfo(data_.vn_entry.get());
}
void FlowTable::AddLinkLocalFlowInfo(int fd, uint32_t index, const FlowKey &key,
const uint64_t timestamp) {
LinkLocalFlowInfoMap::iterator it = linklocal_flow_info_map_.find(fd);
if (it == linklocal_flow_info_map_.end()) {
linklocal_flow_info_map_.insert(
LinkLocalFlowInfoPair(fd, LinkLocalFlowInfo(index, key, timestamp)));
} else {
it->second.flow_index = index;
it->second.flow_key = key;