/
flow_entry.cc
2725 lines (2433 loc) · 92.6 KB
/
flow_entry.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) 2015 Juniper Networks, Inc. All rights reserved.
*/
#include <vector>
#include <bitset>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <base/os.h>
#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 <net/address_util.h>
#include <pkt/flow_table.h>
#include <vrouter/flow_stats/flow_stats_collector.h>
#include <vrouter/ksync/ksync_init.h>
#include <vrouter/ksync/ksync_flow_index_manager.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 <cmn/agent_stats.h>
#include <oper/route_common.h>
#include <oper/vrf.h>
#include <oper/vm.h>
#include <oper/sg.h>
#include <oper/qos_config.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/flow_mgmt.h>
#include <pkt/flow_event.h>
#include <pkt/flow_entry.h>
const std::map<FlowEntry::FlowPolicyState, const char*>
FlowEntry::FlowPolicyStateStr = boost::assign::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")
(BGPROUTERSERVICE_FLOW, "00000000-0000-0000-0000-000000000007");
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 flow Interface unavialable")
((uint16_t)SHORT_IPV4_FWD_DIS, "Short flow Ipv4 forwarding disabled")
((uint16_t)SHORT_UNAVIALABLE_VRF,
"Short flow VRF unavailable")
((uint16_t)SHORT_NO_SRC_ROUTE, "Short flow No Source route")
((uint16_t)SHORT_NO_DST_ROUTE, "Short flow No Destination route")
((uint16_t)SHORT_AUDIT_ENTRY, "Short flow Audit Entry")
((uint16_t)SHORT_VRF_CHANGE, "Short flow VRF CHANGE")
((uint16_t)SHORT_NO_REVERSE_FLOW, "Short flow No Reverse flow")
((uint16_t)SHORT_REVERSE_FLOW_CHANGE,
"Short flow Reverse flow change")
((uint16_t)SHORT_NAT_CHANGE, "Short flow NAT Changed")
((uint16_t)SHORT_FLOW_LIMIT, "Short flow Flow Limit Reached")
((uint16_t)SHORT_LINKLOCAL_SRC_NAT,
"Short flow Linklocal source NAT failed")
((uint16_t)SHORT_FAILED_VROUTER_INSTALL,
"Short flow vrouter install failed")
((uint16_t)SHORT_INVALID_L2_FLOW, "Short flow invalid L2 flow")
((uint16_t)SHORT_FLOW_ON_TSN, "Short flow TSN flow")
((uint16_t)SHORT_NO_MIRROR_ENTRY, "Short flow No mirror entry ")
((uint16_t)SHORT_SAME_FLOW_RFLOW_KEY,"Short flow same flow and rflow")
((uint16_t)DROP_POLICY, "Flow drop Policy")
((uint16_t)DROP_OUT_POLICY, "Flow drop Out Policy")
((uint16_t)DROP_SG, "Flow drop SG")
((uint16_t)DROP_OUT_SG, "Flow drop OUT SG")
((uint16_t)DROP_REVERSE_SG, "Flow drop REVERSE SG")
((uint16_t)DROP_REVERSE_OUT_SG, "Flow drop REVERSE OUT SG")
((uint16_t)SHORT_NO_SRC_ROUTE_L2RPF,
"Short flow No Source route for RPF NH");
tbb::atomic<int> FlowEntry::alloc_count_;
SecurityGroupList FlowEntry::default_sg_list_;
/////////////////////////////////////////////////////////////////////////////
// VmFlowRef
/////////////////////////////////////////////////////////////////////////////
const int VmFlowRef::kInvalidFd;
VmFlowRef::VmFlowRef() :
vm_(NULL), fd_(kInvalidFd), port_(0), flow_(NULL) {
}
VmFlowRef::VmFlowRef(const VmFlowRef &rhs) {
// UPDATE on linklocal flows is not supported. So, fd_ should be invalid
assert(fd_ == VmFlowRef::kInvalidFd);
assert(rhs.fd_ == VmFlowRef::kInvalidFd);
SetVm(rhs.vm_.get());
}
VmFlowRef:: ~VmFlowRef() {
Reset(true);
}
void VmFlowRef::Init(FlowEntry *flow) {
flow_ = flow;
}
void VmFlowRef::operator=(const VmFlowRef &rhs) {
assert(rhs.fd_ == VmFlowRef::kInvalidFd);
assert(rhs.port_ == 0);
// For linklocal flows, we should have called Move already. It would
// reset vm_. Validate it
if (fd_ != VmFlowRef::kInvalidFd)
assert(rhs.vm_.get() == NULL);
}
// Move is called from Copy() routine when flow is evicted by vrouter and a
// new flow-add is received by agent. Use the fd_ and port_ from new flow
// since reverse flow will be setup based on these
void VmFlowRef::Move(VmFlowRef *rhs) {
// Release the old values
Reset(false);
fd_ = rhs->fd_;
port_ = rhs->port_;
SetVm(rhs->vm_.get());
// Ownership for fd_ is transferred. Reset RHS fields
// Reset VM first before resetting fd_
rhs->SetVm(NULL);
rhs->fd_ = VmFlowRef::kInvalidFd;
rhs->port_ = 0;
}
void VmFlowRef::Reset(bool reset_flow) {
FreeRef();
FreeFd();
vm_.reset(NULL);
if (reset_flow)
flow_ = NULL;
}
void VmFlowRef::FreeRef() {
if (vm_.get() == NULL)
return;
vm_->update_flow_count(-1);
if (fd_ != kInvalidFd) {
vm_->update_linklocal_flow_count(-1);
}
}
void VmFlowRef::FreeFd() {
if (fd_ == kInvalidFd) {
assert(port_ == 0);
return;
}
FlowProto *proto = flow_->flow_table()->agent()->pkt()->get_flow_proto();
proto->update_linklocal_flow_count(-1);
flow_->flow_table()->DelLinkLocalFlowInfo(fd_);
close(fd_);
fd_ = kInvalidFd;
port_ = 0;
}
void VmFlowRef::SetVm(const VmEntry *vm) {
if (vm == vm_.get())
return;
FreeRef();
vm_.reset(vm);
if (vm == NULL)
return;
// update per-vm flow accounting
vm->update_flow_count(1);
if (fd_ != kInvalidFd) {
vm_->update_linklocal_flow_count(1);
}
return;
}
bool VmFlowRef::AllocateFd(Agent *agent, uint8_t l3_proto) {
if (fd_ != kInvalidFd)
return true;
port_ = 0;
// Short flows are always dropped. Dont allocate FD for short flow
if (flow_->IsShortFlow())
return false;
if (l3_proto == IPPROTO_TCP) {
fd_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
} else if (l3_proto == IPPROTO_UDP) {
fd_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
if (fd_ == kInvalidFd) {
return false;
}
// Update agent accounting info
agent->pkt()->get_flow_proto()->update_linklocal_flow_count(1);
flow_->flow_table()->AddLinkLocalFlowInfo(fd_, flow_->flow_handle(),
flow_->key(), UTCTimestampUsec());
// allow the socket to be reused upon close
int optval = 1;
setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
if (bind(fd_, (struct sockaddr*) &address, sizeof(address)) < 0) {
FreeFd();
return false;
}
struct sockaddr_in bound_to;
socklen_t len = sizeof(bound_to);
if (getsockname(fd_, (struct sockaddr*) &bound_to, &len) < 0) {
FreeFd();
return false;
}
port_ = ntohs(bound_to.sin_port);
return true;
}
/////////////////////////////////////////////////////////////////////////////
// FlowData constructor/destructor
/////////////////////////////////////////////////////////////////////////////
FlowData::FlowData() {
Reset();
}
FlowData::~FlowData() {
}
void FlowData::Reset() {
smac = MacAddress();
dmac = MacAddress();
source_vn_list.clear();
source_vn_match = "";
dest_vn_match = "";
dest_vn_list.clear();
source_sg_id_l.clear();
dest_sg_id_l.clear();
flow_source_vrf = VrfEntry::kInvalidIndex;
flow_dest_vrf = VrfEntry::kInvalidIndex;
match_p.Reset();
vn_entry.reset(NULL);
intf_entry.reset(NULL);
in_vm_entry.Reset(true);
out_vm_entry.Reset(true);
src_ip_nh.reset(NULL);
vrf = VrfEntry::kInvalidIndex;
mirror_vrf = VrfEntry::kInvalidIndex;
dest_vrf = 0;
component_nh_idx = (uint32_t)CompositeNH::kInvalidComponentNHIdx;
source_plen = 0;
dest_plen = 0;
drop_reason = 0;
vrf_assign_evaluated = false;
if_index_info = 0;
tunnel_info.Reset();
flow_source_plen_map.clear();
flow_dest_plen_map.clear();
enable_rpf = true;
rpf_nh.reset(NULL);
rpf_plen = Address::kMaxV4PrefixLen;
rpf_vrf = VrfEntry::kInvalidIndex;
vm_cfg_name = "";
bgp_as_a_service_port = 0;
acl_assigned_vrf_index_ = VrfEntry::kInvalidIndex;
qos_config_idx = AgentQosConfigTable::kInvalidIndex;
ttl = 0;
}
static std::vector<std::string> MakeList(const VnListType &ilist) {
std::vector<std::string> olist;
for (VnListType::const_iterator it = ilist.begin();
it != ilist.end(); ++it) {
olist.push_back(*it);
}
return olist;
}
std::vector<std::string> FlowData::SourceVnList() const {
return MakeList(source_vn_list);
}
std::vector<std::string> FlowData::DestinationVnList() const {
return MakeList(dest_vn_list);
}
/////////////////////////////////////////////////////////////////////////////
// MatchPolicy constructor/destructor
/////////////////////////////////////////////////////////////////////////////
MatchPolicy::MatchPolicy() {
Reset();
}
MatchPolicy::~MatchPolicy() {
}
void MatchPolicy::Reset() {
m_acl_l.clear();
policy_action = 0;
m_out_acl_l.clear();
out_policy_action = 0;
m_out_sg_acl_l.clear();
out_sg_rule_present = false;
out_sg_action = 0;
m_sg_acl_l.clear();
sg_rule_present = false;
sg_action = 0;
m_reverse_sg_acl_l.clear();
reverse_sg_rule_present = false;
reverse_sg_action = 0;
m_reverse_out_sg_acl_l.clear();
reverse_out_sg_rule_present = false;
reverse_out_sg_action = 0;
m_mirror_acl_l.clear();
mirror_action = 0;
m_out_mirror_acl_l.clear();
out_mirror_action = 0;
m_vrf_assign_acl_l.clear();
vrf_assign_acl_action = 0;
sg_action_summary = 0;
action_info.Clear();
}
/////////////////////////////////////////////////////////////////////////////
// FlowEventLog constructor/destructor
/////////////////////////////////////////////////////////////////////////////
FlowEventLog::FlowEventLog() : time_(0), event_(EVENT_MAX),
flow_handle_(FlowEntry::kInvalidFlowHandle), flow_gen_id_(0),
ksync_entry_(NULL), hash_id_(FlowEntry::kInvalidFlowHandle), gen_id_(0),
vrouter_flow_handle_(FlowEntry::kInvalidFlowHandle), vrouter_gen_id_(0) {
}
FlowEventLog::~FlowEventLog() {
}
/////////////////////////////////////////////////////////////////////////////
// FlowEntry constructor/destructor
/////////////////////////////////////////////////////////////////////////////
FlowEntry::FlowEntry(FlowTable *flow_table) :
flow_table_(flow_table), flags_(0),
tunnel_type_(TunnelType::INVALID),
fip_vmi_(AgentKey::ADD_DEL_CHANGE, nil_uuid(), ""),
flow_mgmt_request_(NULL), flow_mgmt_info_() {
// ksync entry is set to NULL only on constructor and on flow delete
// it should not have any other explicit set to NULL
ksync_entry_ = NULL;
Reset();
alloc_count_.fetch_and_increment();
}
FlowEntry::~FlowEntry() {
assert(refcount_ == 0);
Reset();
alloc_count_.fetch_and_decrement();
}
void FlowEntry::Reset() {
assert(ksync_entry_ == NULL);
uuid_ = flow_table_->rand_gen();
egress_uuid_ = flow_table_->rand_gen();
data_.Reset();
l3_flow_ = true;
gen_id_ = 0;
flow_handle_ = kInvalidFlowHandle;
reverse_flow_entry_ = NULL;
deleted_ = false;
flags_ = 0;
short_flow_reason_ = SHORT_UNKNOWN;
peer_vrouter_ = "";
tunnel_type_ = TunnelType::INVALID;
on_tree_ = false;
fip_ = 0;
fip_vmi_ = VmInterfaceKey(AgentKey::ADD_DEL_CHANGE, nil_uuid(), "");
refcount_ = 0;
nw_ace_uuid_ = FlowPolicyStateStr.at(NOT_EVALUATED);
sg_rule_uuid_= FlowPolicyStateStr.at(NOT_EVALUATED);
fsc_ = NULL;
trace_ = false;
event_logs_.reset();
event_log_index_ = 0;
last_event_ = FlowEvent::INVALID;
flow_retry_attempts_ = 0;
is_flow_on_unresolved_list = false;
pending_actions_.Reset();
assert(flow_mgmt_request_ == NULL);
assert(flow_mgmt_info_.get() == NULL);
}
void FlowEntry::Reset(const FlowKey &k) {
Reset();
key_ = k;
}
void FlowEntry::Init() {
alloc_count_ = 0;
}
FlowEntry *FlowEntry::Allocate(const FlowKey &key, FlowTable *flow_table) {
// flow_table will be NULL for some UT cases
FlowEntry *flow;
if (flow_table == NULL) {
flow = new FlowEntry(flow_table);
flow->Reset(key);
} else {
flow = flow_table->free_list()->Allocate(key);
}
flow->data_.in_vm_entry.Init(flow);
flow->data_.out_vm_entry.Init(flow);
return flow;
}
// selectively copy fields from RHS
// When flow is being updated, rhs will be new flow allocated in PktFlowInfo
void FlowEntry::Copy(FlowEntry *rhs, bool update) {
if (update) {
rhs->data_.in_vm_entry.Reset(false);
rhs->data_.out_vm_entry.Reset(false);
} else {
// The operator= below will call VmFlowRef operator=. In case of flow
// eviction, we want to move ownership from rhs to lhs. However rhs is
// const ref in operator so, invode Move API to transfer ownership
data_.in_vm_entry.Move(&rhs->data_.in_vm_entry);
data_.out_vm_entry.Move(&rhs->data_.out_vm_entry);
}
data_ = rhs->data_;
flags_ = rhs->flags_;
short_flow_reason_ = rhs->short_flow_reason_;
sg_rule_uuid_ = rhs->sg_rule_uuid_;
nw_ace_uuid_ = rhs->nw_ace_uuid_;
peer_vrouter_ = rhs->peer_vrouter_;
tunnel_type_ = rhs->tunnel_type_;
fip_ = rhs->fip_;
fip_vmi_ = rhs->fip_vmi_;
last_event_ = rhs->last_event_;
flow_retry_attempts_ = rhs->flow_retry_attempts_;
trace_ = rhs->trace_;
if (update == false) {
gen_id_ = rhs->gen_id_;
flow_handle_ = rhs->flow_handle_;
/* Flow Entry is being re-used. Generate a new UUID for it. */
uuid_ = flow_table_->rand_gen();
egress_uuid_ = flow_table_->rand_gen();
}
}
/////////////////////////////////////////////////////////////////////////////
// Routines to initialize FlowEntry from PktControlInfo
/////////////////////////////////////////////////////////////////////////////
void intrusive_ptr_add_ref(FlowEntry *fe) {
fe->refcount_.fetch_and_increment();
}
void intrusive_ptr_release(FlowEntry *fe) {
FlowTable *flow_table = fe->flow_table();
int prev = fe->refcount_.fetch_and_decrement();
if (prev == 1) {
if (fe->on_tree()) {
if (flow_table->ConcurrencyCheck(flow_table->flow_task_id())
== false) {
FlowEntryPtr ref(fe);
FlowProto *proto=flow_table->agent()->pkt()->get_flow_proto();
proto->ForceEnqueueFreeFlowReference(ref);
return;
}
FlowTable::FlowEntryMap::iterator it =
flow_table->flow_entry_map_.find(fe->key());
assert(it != flow_table->flow_entry_map_.end());
flow_table->flow_entry_map_.erase(it);
flow_table->agent()->stats()->decr_flow_count();
}
flow_table->free_list()->Free(fe);
}
}
bool FlowEntry::InitFlowCmn(const PktFlowInfo *info, const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl,
FlowEntry *rflow) {
reverse_flow_entry_ = rflow;
reset_flags(FlowEntry::ReverseFlow);
peer_vrouter_ = info->peer_vrouter;
tunnel_type_ = info->tunnel_type;
if (info->linklocal_flow) {
set_flags(FlowEntry::LinkLocalFlow);
} else {
reset_flags(FlowEntry::LinkLocalFlow);
}
if (info->nat_done) {
set_flags(FlowEntry::NatFlow);
} else {
reset_flags(FlowEntry::NatFlow);
}
if (info->short_flow) {
set_flags(FlowEntry::ShortFlow);
short_flow_reason_ = info->short_flow_reason;
} else {
reset_flags(FlowEntry::ShortFlow);
short_flow_reason_ = SHORT_UNKNOWN;
}
if (info->local_flow) {
set_flags(FlowEntry::LocalFlow);
} else {
reset_flags(FlowEntry::LocalFlow);
}
if (info->tcp_ack) {
set_flags(FlowEntry::TcpAckFlow);
} else {
reset_flags(FlowEntry::TcpAckFlow);
}
if (info->bgp_router_service_flow) {
set_flags(FlowEntry::BgpRouterService);
data_.bgp_as_a_service_port = info->nat_sport;
} else {
reset_flags(FlowEntry::BgpRouterService);
data_.bgp_as_a_service_port = 0;
}
if (info->alias_ip_flow) {
set_flags(FlowEntry::AliasIpFlow);
} else {
reset_flags(FlowEntry::AliasIpFlow);
}
data_.intf_entry = ctrl->intf_ ? ctrl->intf_ : rev_ctrl->intf_;
data_.vn_entry = ctrl->vn_ ? ctrl->vn_ : rev_ctrl->vn_;
data_.in_vm_entry.SetVm(ctrl->vm_);
data_.out_vm_entry.SetVm(rev_ctrl->vm_);
l3_flow_ = info->l3_flow;
data_.acl_assigned_vrf_index_ = VrfEntry::kInvalidIndex;
return true;
}
void FlowEntry::InitFwdFlow(const PktFlowInfo *info, const PktInfo *pkt,
const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl,
FlowEntry *rflow, Agent *agent) {
gen_id_ = pkt->GetAgentHdr().cmd_param_5;
flow_handle_ = pkt->GetAgentHdr().cmd_param;
if (InitFlowCmn(info, ctrl, rev_ctrl, rflow) == false) {
return;
}
if (info->linklocal_bind_local_port) {
set_flags(FlowEntry::LinkLocalBindLocalSrcPort);
} else {
reset_flags(FlowEntry::LinkLocalBindLocalSrcPort);
}
uint32_t intf_in = pkt->GetAgentHdr().ifindex;
data_.vm_cfg_name = InterfaceIdToVmCfgName(agent, intf_in);
if (info->ingress) {
set_flags(FlowEntry::IngressDir);
} else {
reset_flags(FlowEntry::IngressDir);
}
if (ctrl->rt_ != NULL) {
RpfInit(ctrl->rt_);
}
if (info->bgp_router_service_flow) {
if (info->ttl == 1) {
data_.ttl = BGP_SERVICE_TTL_FWD_FLOW;
}
}
data_.flow_source_vrf = info->flow_source_vrf;
data_.flow_dest_vrf = info->flow_dest_vrf;
data_.flow_source_plen_map = info->flow_source_plen_map;
data_.flow_dest_plen_map = info->flow_dest_plen_map;
data_.dest_vrf = info->dest_vrf;
data_.vrf = pkt->vrf;
data_.if_index_info = pkt->agent_hdr.ifindex;
data_.tunnel_info = pkt->tunnel;
if (info->ecmp) {
set_flags(FlowEntry::EcmpFlow);
} else {
reset_flags(FlowEntry::EcmpFlow);
}
data_.component_nh_idx = info->out_component_nh_idx;
reset_flags(FlowEntry::Trap);
if (ctrl->rt_ && ctrl->rt_->is_multicast()) {
set_flags(FlowEntry::Multicast);
}
if (rev_ctrl->rt_ && rev_ctrl->rt_->is_multicast()) {
set_flags(FlowEntry::Multicast);
}
reset_flags(FlowEntry::UnknownUnicastFlood);
if (info->flood_unknown_unicast) {
set_flags(FlowEntry::UnknownUnicastFlood);
if (info->ingress) {
GetSourceRouteInfo(ctrl->rt_);
} else {
GetSourceRouteInfo(rev_ctrl->rt_);
}
data_.dest_vn_list = data_.source_vn_list;
} else {
GetSourceRouteInfo(ctrl->rt_);
GetDestRouteInfo(rev_ctrl->rt_);
}
data_.smac = pkt->smac;
data_.dmac = pkt->dmac;
}
void FlowEntry::InitRevFlow(const PktFlowInfo *info, const PktInfo *pkt,
const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl,
FlowEntry *rflow, Agent *agent) {
uint32_t intf_in;
if (InitFlowCmn(info, ctrl, rev_ctrl, rflow) == false) {
return;
}
set_flags(FlowEntry::ReverseFlow);
if (ctrl->intf_) {
intf_in = ctrl->intf_->id();
} else {
intf_in = Interface::kInvalidIndex;
}
data_.vm_cfg_name = InterfaceIdToVmCfgName(agent, intf_in);
// Compute reverse flow fields
reset_flags(FlowEntry::IngressDir);
if (ctrl->intf_) {
if (info->ComputeDirection(ctrl->intf_)) {
set_flags(FlowEntry::IngressDir);
} else {
reset_flags(FlowEntry::IngressDir);
}
}
if (ctrl->rt_ != NULL) {
RpfInit(ctrl->rt_);
}
if (info->bgp_router_service_flow) {
if (info->ttl == 1) {
data_.ttl = BGP_SERVICE_TTL_REV_FLOW;
}
}
data_.flow_source_vrf = info->flow_dest_vrf;
data_.flow_dest_vrf = info->flow_source_vrf;
data_.flow_source_plen_map = info->flow_dest_plen_map;
data_.flow_dest_plen_map = info->flow_source_plen_map;
data_.vrf = info->dest_vrf;
if (!info->nat_done) {
data_.dest_vrf = info->flow_source_vrf;
} else {
data_.dest_vrf = info->nat_dest_vrf;
}
if (info->ecmp) {
set_flags(FlowEntry::EcmpFlow);
} else {
reset_flags(FlowEntry::EcmpFlow);
}
data_.component_nh_idx = CompositeNH::kInvalidComponentNHIdx;
reset_flags(FlowEntry::UnknownUnicastFlood);
if (info->flood_unknown_unicast) {
set_flags(FlowEntry::UnknownUnicastFlood);
if (info->ingress) {
GetSourceRouteInfo(rev_ctrl->rt_);
} else {
GetSourceRouteInfo(ctrl->rt_);
}
//Set source VN and dest VN to be same
//since flooding happens only for layer2 routes
//SG id would be left empty, user who wants
//unknown unicast to happen should modify the
//SG to allow such traffic
data_.dest_vn_list = data_.source_vn_list;
} else {
GetSourceRouteInfo(ctrl->rt_);
GetDestRouteInfo(rev_ctrl->rt_);
}
data_.smac = pkt->dmac;
data_.dmac = pkt->smac;
}
void FlowEntry::InitAuditFlow(uint32_t flow_idx, uint8_t gen_id) {
gen_id_ = gen_id;
flow_handle_ = flow_idx;
set_flags(FlowEntry::ShortFlow);
short_flow_reason_ = SHORT_AUDIT_ENTRY;
data_.source_vn_list = FlowHandler::UnknownVnList();
data_.dest_vn_list = FlowHandler::UnknownVnList();
data_.source_sg_id_l = default_sg_list();
data_.dest_sg_id_l = default_sg_list();
}
/////////////////////////////////////////////////////////////////////////////
// Utility routines
/////////////////////////////////////////////////////////////////////////////
// Find L2 Route for the MAC address.
AgentRoute *FlowEntry::GetL2Route(const VrfEntry *vrf,
const MacAddress &mac) {
BridgeAgentRouteTable *table = static_cast<BridgeAgentRouteTable *>
(vrf->GetBridgeRouteTable());
return table->FindRouteNoLock(mac);
}
AgentRoute *FlowEntry::GetUcRoute(const VrfEntry *entry,
const IpAddress &addr) {
AgentRoute *rt = NULL;
if (addr.is_v4()) {
InetUnicastRouteEntry key(NULL, addr, 32, false);
rt = entry->GetUcRoute(key);
} else {
InetUnicastRouteEntry key(NULL, addr, 128, false);
rt = entry->GetUcRoute(key);
}
if (rt != NULL && rt->IsRPFInvalid()) {
return NULL;
}
return rt;
}
AgentRoute *FlowEntry::GetEvpnRoute(const VrfEntry *vrf,
const MacAddress &mac,
const IpAddress &ip,
uint32_t ethernet_tag) {
EvpnAgentRouteTable *table = static_cast<EvpnAgentRouteTable *>(
vrf->GetEvpnRouteTable());
return table->FindRouteNoLock(mac, ip, ethernet_tag);
}
uint32_t FlowEntry::reverse_flow_fip() const {
FlowEntry *rflow = reverse_flow_entry_.get();
if (rflow) {
return rflow->fip();
}
return 0;
}
VmInterfaceKey FlowEntry::reverse_flow_vmi() const {
FlowEntry *rflow = reverse_flow_entry_.get();
if (rflow) {
return rflow->fip_vmi();
}
return VmInterfaceKey(AgentKey::ADD_DEL_CHANGE, nil_uuid(), "");
}
void FlowEntry::UpdateFipStatsInfo(uint32_t fip, uint32_t id, Agent *agent) {
fip_ = fip;
fip_vmi_ = InterfaceIdToKey(agent, id);
}
void FlowEntry::set_flow_handle(uint32_t flow_handle, uint8_t gen_id) {
if (flow_handle_ != flow_handle) {
assert(flow_handle_ == kInvalidFlowHandle);
flow_handle_ = flow_handle;
}
gen_id_ = gen_id;
}
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 *>(
flow_table()->agent()->vrf_table()->FindActiveEntry(&vrf_key));
if (vrf) {
data_.acl_assigned_vrf_index_ = vrf->vrf_id();
bool set_dest_vrf = true;
if (is_flags_set(FlowEntry::NatFlow) &&
reverse_flow_entry() &&
key().dst_addr != reverse_flow_entry()->key().src_addr) {
//Packet is getting DNATed, VRF assign ACL action
//is applied on floating-ip VN and the destination VRF should
//be retained as interface VRF
set_dest_vrf = false;
}
if (set_dest_vrf) {
data_.dest_vrf = 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::RevFlowDepInfo(RevFlowDepParams *params) {
params->sip_ = key().src_addr;
FlowEntry *rev_flow = reverse_flow_entry();
if (rev_flow) {
params->rev_uuid_ = rev_flow->uuid();
params->vm_cfg_name_ = rev_flow->data().vm_cfg_name;
params->sg_uuid_ = rev_flow->sg_rule_uuid();
params->rev_egress_uuid_ = rev_flow->egress_uuid();
if (rev_flow->intf_entry()) {
params->vmi_uuid_ = UuidToString(rev_flow->intf_entry()->GetUuid());
}
if (key().family != Address::INET) {
return;
}
if (is_flags_set(FlowEntry::NatFlow) &&
is_flags_set(FlowEntry::IngressDir)) {
const FlowKey *nat_key = &rev_flow->key();
if (key().src_addr != nat_key->dst_addr) {
params->sip_ = nat_key->dst_addr;
}
}
}
}
static bool ShouldDrop(uint32_t action) {
if (action & TrafficAction::DROP_FLAGS)
return true;
if (action & TrafficAction::IMPLICIT_DENY_FLAGS)
return true;
return false;
}
/////////////////////////////////////////////////////////////////////////////
// Flow RPF
//
// VRouter enforces RPF check based on RPF-NH programmed in the flow. The RPF
// NH can be of two types,
// - Unicast NH :
// In this case, VRouter matches with incoming interface/tunnel-src with
// RPF NH the flow
//
// - ECMP NH :
// In this case, VRouter picks an ECMP component-nh and matches with incoming
// interface/tunnel-src. The index for component-nh is got from reverse flow.
//
// RPF-NH cases
// ------------
// 1. Baremetals
// Agent is not aware of IP address assigned for baremetals. So, RPF check
// for baremetals is based on the L2-Route
//
// 2. Layer-2 Flows
// If agent know the Inet route for source-ip in packet, RPF is based on
// the Inet route for source-ip. There are some exceptions for this rule,
// - If Inet route for source-ip is ECMP, then RPF is based on layer-2 route
// - If Inet route for source-ip is not-host route, then RPF is based on
// layer-2 routes
//
// If packet is from BMS (egress flow), its possible that agent does not
// know IP address for BMS. In such case, RPF is based on L2-Route
//
// 3. Layer-3 Flows from VMI
// RPF check will be based on the InterfaceNH for VMI
//
// 4. Layer-3 Flows from Fabric with unicast-NH
// The unicast-nh is used as RPF-NH
//
// 5. Layer-3 Flows from Fabric with composite-NH
// VRouter picks NH from flow and the ecmp-index from reverse flow.
//
// The ecmp-index in reverse is computed based on route for dest-ip in
// VRF got post VRF translation if any
//
// Note, the RPF must be picked post VRF translation since order of members
// in Composite-NH may vary across VRF
//
// Route Tracking
// --------------
// Flow Management should track the route for ip1 in vrf4 to update RPF-NH
//
// RPF Computation
// ---------------
// RPF computation happens in two stages
// 1. FlowEntry creation (RpfInit):
// Called during FlowEntry init. Computes src_ip_nh for flow.
//
// For layer-2 flows, RPF-NH Is same as src_ip_nh
// For Non-ECMP layer-3 flows, RPF-NH is same as src_ip_nh
// For ECMP layer-3 flows, RPF-NH must be computed only after VRF
// translation is computed for reverse flow.
//
// 2. Post ACL processing (RpfUpdate):
// Post ACL processing, all VRF translation rules are identified.
// The RPF-NH is computed in this method.
/////////////////////////////////////////////////////////////////////////////
// Utility method to set rpf_vrf and rpf_plen fields
static void SetRpfFieldsInternal(FlowEntry *flow, const AgentRoute *rt) {
// If there is no route, we should track default route
if (rt == NULL) {
flow->data().rpf_vrf = flow->data().vrf;
flow->data().rpf_plen = 0;
return;
}
if (dynamic_cast<const InetUnicastRouteEntry *>(rt)) {
flow->data().rpf_vrf = rt->vrf()->vrf_id();
flow->data().rpf_plen = rt->plen();
return;
}
// Route is not INET. Dont track any route
flow->data().rpf_vrf = VrfEntry::kInvalidIndex;
flow->data().rpf_plen = 0;
return;
}
// Utility method to set src_ip_nh fields
void FlowEntry::RpfSetSrcIpNhFields(const AgentRoute *rt,
const NextHop *src_ip_nh) {
data_.src_ip_nh.reset(src_ip_nh);
SetRpfFieldsInternal(this, rt);
return;
}
void FlowEntry::RpfSetRpfNhFields(const NextHop *rpf_nh) {
data_.rpf_nh.reset(rpf_nh);
}
// Utility method to set rpf_nh fields
void FlowEntry::RpfSetRpfNhFields(const AgentRoute *rt, const NextHop *rpf_nh) {
data_.rpf_nh.reset(rpf_nh);
SetRpfFieldsInternal(this, rt);
return;
}
// This method is called when flow is initialized. The RPF-NH cannot be
// computed at this stage since we dont know if reverse flow has VRF
// translation or not.
// This method only sets src_ip_nh field
//
// In case of layer-3 flow "rt" is inet route for source-ip in source-vrf
// In case of layer-2 flow "rt" is l2 route for smac in source-vrf
void FlowEntry::RpfInit(const AgentRoute *rt) {
// Set src_ip_nh based on rt first
RpfSetSrcIpNhFields(rt, rt->GetActiveNextHop());
// RPF enabled?
bool rpf_enable = true;
if (data_.vn_entry && data_.vn_entry->enable_rpf() == false)
rpf_enable = false;
// The src_ip_nh can change below only for l2 flows
// For l3-flow, rt will already be a INET route
if (l3_flow())
return;
// For layer-2 flows, we use l2-route for RPF in following cases
// 1. Interface is of type BAREMETAL (ToR/TSN only)
//
// 2. ECMP is not supported for l2-flows. If src-ip lookup resulted in
// ECMP-NH fallback to the original l2-route
//
// 3. In case of OVS, ToR will export layer-2 route and MX will export a
// layer-3 subnet route covering all the ToRs. In this case, when ToR
// send layer-2 packet the layer-3 route will point to MX and RPF fails.
// Assuming MX only gives subnet-route, use inet-route only if its
// host-route
// 4. Its an egress flow and there is no route for IP address
const VmInterface *vmi =
dynamic_cast<const VmInterface *>(intf_entry());
if (vmi && vmi->vmi_type() == VmInterface::BAREMETAL) {
return;
}
const InetUnicastRouteEntry *src_ip_rt =
static_cast<InetUnicastRouteEntry *>
(FlowEntry::GetUcRoute(rt->vrf(), key().src_addr));
if (src_ip_rt == NULL) {
// For egress flow, with no l3-route then do rpf based on l2-route
// For ingress flow, with no l3-route, make it short flow