-
Notifications
You must be signed in to change notification settings - Fork 390
/
flow_entry.cc
2263 lines (2018 loc) · 76.9 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 <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)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");
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);
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;
pending_recompute = false;
if_index_info = 0;
tunnel_info.Reset();
flow_source_plen_map.clear();
flow_dest_plen_map.clear();
enable_rpf = true;
l2_rpf_plen = Address::kMaxV4PrefixLen;
vm_cfg_name = "";
bgp_as_a_service_port = 0;
ecmp_rpf_nh_ = 0;
acl_assigned_vrf_index_ = VrfEntry::kInvalidIndex;
}
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();
}
/////////////////////////////////////////////////////////////////////////////
// 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(), "") {
Reset();
alloc_count_.fetch_and_increment();
}
FlowEntry::~FlowEntry() {
assert(refcount_ == 0);
Reset();
alloc_count_.fetch_and_decrement();
}
void FlowEntry::Reset() {
uuid_ = flow_table_->rand_gen();
data_.Reset();
l3_flow_ = true;
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);
ksync_index_entry_ = std::auto_ptr<KSyncFlowIndexEntry>
(new KSyncFlowIndexEntry());
fsc_ = 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
void FlowEntry::Copy(FlowEntry *rhs, bool update) {
if (update) {
rhs->data_.in_vm_entry.FreeFd();
rhs->data_.out_vm_entry.FreeFd();
} 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_;
if (update == false) {
flow_handle_ = rhs->flow_handle_;
/* Flow Entry is being re-used. Generate a new UUID for it. */
// results is delete miss for previous uuid to stats collector
// with eviction disabled following is not required
// 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() == 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->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;
}
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_.ecmp_rpf_nh_ = 0;
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) {
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) {
SetRpfNH(flow_table_, ctrl->rt_);
}
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) {
SetRpfNH(flow_table_, ctrl->rt_);
}
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 = info->in_component_nh_idx;
if (info->trap_rev_flow) {
set_flags(FlowEntry::Trap);
} else {
reset_flags(FlowEntry::Trap);
}
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) {
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;
}
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);
}
bool FlowEntry::set_pending_recompute(bool value) {
if (data_.pending_recompute != value) {
data_.pending_recompute = value;
return true;
}
return false;
}
void FlowEntry::set_flow_handle(uint32_t flow_handle) {
if (flow_handle_ != flow_handle) {
assert(flow_handle_ == kInvalidFlowHandle);
flow_handle_ = flow_handle;
}
}
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();
return;
}
data_.acl_assigned_vrf_index_ = VrfEntry::kInvalidIndex;
}
uint32_t FlowEntry::acl_assigned_vrf_index() const {
return data_.acl_assigned_vrf_index_;
}
static bool ShouldDrop(uint32_t action) {
if (action & TrafficAction::DROP_FLAGS)
return true;
if (action & TrafficAction::IMPLICIT_DENY_FLAGS)
return true;
return false;
}
/////////////////////////////////////////////////////////////////////////////
// Flow entry fileds updation routines
/////////////////////////////////////////////////////////////////////////////
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";
}
// Get src-vn/sg-id/plen from route
// src-vn and sg-id are used for policy lookup
// plen is used to track the routes to use by flow_mgmt module
void FlowEntry::GetSourceRouteInfo(const AgentRoute *rt) {
const AgentPath *path = NULL;
if (rt) {
path = rt->GetActivePath();
}
if (path == NULL) {
data_.source_vn_list = FlowHandler::UnknownVnList();
data_.source_vn_match = FlowHandler::UnknownVn();
data_.source_sg_id_l = default_sg_list();
data_.source_plen = 0;
} else {
data_.source_vn_list = path->dest_vn_list();
if (path->dest_vn_list().size())
data_.source_vn_match = *path->dest_vn_list().begin();
data_.source_sg_id_l = path->sg_list();
data_.source_plen = rt->plen();
}
}
// Get dst-vn/sg-id/plen from route
// dst-vn and sg-id are used for policy lookup
// plen is used to track the routes to use by flow_mgmt module
void FlowEntry::GetDestRouteInfo(const AgentRoute *rt) {
const AgentPath *path = NULL;
if (rt) {
path = rt->GetActivePath();
}
if (path == NULL) {
data_.dest_vn_list = FlowHandler::UnknownVnList();
data_.dest_vn_match = FlowHandler::UnknownVn();
data_.dest_sg_id_l = default_sg_list();
data_.dest_plen = 0;
} else {
data_.dest_vn_list = path->dest_vn_list();
if (path->dest_vn_list().size())
data_.dest_vn_match = *path->dest_vn_list().begin();
data_.dest_sg_id_l = path->sg_list();
data_.dest_plen = rt->plen();
}
}
void FlowEntry::UpdateRpf() {
if (data_.vn_entry) {
data_.enable_rpf = data_.vn_entry->enable_rpf();
} else {
data_.enable_rpf = true;
}
}
//Given a NH take reference on the NH and set the RPF
bool FlowEntry::SetRpfNHState(FlowTable *ft, const NextHop *nh) {
if (data_.nh.get() && nh) {
if (data_.nh->GetType() != NextHop::COMPOSITE &&
nh->GetType() == NextHop::COMPOSITE) {
set_flags(FlowEntry::Trap);
}
}
if (data_.nh.get() != nh) {
data_.nh = nh;
return true;
}
return false;
}
bool FlowEntry::SetRpfNH(FlowTable *ft, const AgentRoute *rt) {
bool ret = false;
if (data().ecmp_rpf_nh_ != 0) {
//Set RPF NH based on reverese flow route
return ret;
}
if (!rt) {
return SetRpfNHState(ft, NULL);
}
//If l2 flow has a ip route entry present in
//layer 3 table, then use that for calculating
//rpf nexthop, else use layer 2 route entry(baremetal
//scenario where layer 3 route may not be present)
bool is_baremetal = false;
const VmInterface *vmi = dynamic_cast<const VmInterface *>(intf_entry());
if (vmi && vmi->vmi_type() == VmInterface::BAREMETAL) {
is_baremetal = true;
}
data_.l2_rpf_plen = Address::kMaxV4PrefixLen;
if (l3_flow() == false && is_baremetal == false) {
//For ingress flow, agent always sets
//rpf NH from layer 3 route entry
//In case of egress flow if route entry is present
//and its a host route entry use it for RPF NH
//For baremetal case since IP address may not be known
//agent uses layer 2 route entry
InetUnicastRouteEntry *ip_rt = static_cast<InetUnicastRouteEntry *>(
FlowEntry::GetUcRoute(rt->vrf(), key().src_addr));
if (ip_rt &&
ip_rt->GetActiveNextHop()->GetType() == NextHop::COMPOSITE) {
//L2 flow cant point to composite NH, set RPF NH based on
//layer 2 route irrespective prefix lenght of layer 3 route,
//this is to avoid packet drop in scenario where transition
//happened from non-ecmp to ECMP.
} else if (is_flags_set(FlowEntry::IngressDir) ||
(ip_rt && ip_rt->IsHostRoute())) {
rt = ip_rt;
if (rt) {
data_.l2_rpf_plen = rt->plen();
}
}
}
const NextHop *nh = NULL;
if (rt && rt->GetActiveNextHop()) {
nh = rt->GetActiveNextHop();
}
return SetRpfNHState(ft, nh);
}
bool FlowEntry::SetEcmpRpfNH(FlowTable *ft, uint32_t nh_id) {
if (!nh_id) {
return SetRpfNHState(ft, NULL);
}
const NextHop *nh = ft->agent()->nexthop_table()->FindNextHop(nh_id);
return SetRpfNHState(ft, nh);
}
VmInterfaceKey FlowEntry::InterfaceIdToKey(Agent *agent, uint32_t id) {
if (id != Interface::kInvalidIndex) {
Interface *itf = agent->interface_table()->FindInterface(id);
if (itf && (itf->type() == Interface::VM_INTERFACE)) {
return VmInterfaceKey(AgentKey::ADD_DEL_CHANGE, itf->GetUuid(),
itf->name());
}
}
return VmInterfaceKey(AgentKey::ADD_DEL_CHANGE, nil_uuid(), "");
}
uint32_t FlowEntry::InterfaceKeyToId(Agent *agent, const VmInterfaceKey &key) {
const Interface *intf = dynamic_cast<const Interface*>
(agent->interface_table()->FindActiveEntry(&key));
if (intf) {
return intf->id();
}
return Interface::kInvalidIndex;
}
const std::string FlowEntry::InterfaceIdToVmCfgName(Agent *agent, uint32_t id) {
if (id != Interface::kInvalidIndex) {
const VmInterface *itf = dynamic_cast <const VmInterface *>
(agent->interface_table()->FindInterface(id));
if (itf) {
const VmEntry *vm = itf->vm();
if (vm) {
return vm->GetCfgName();
}
}
}
return "";
}
/////////////////////////////////////////////////////////////////////////////
// Routines to compute ACL to be applied (including network-policy, SG and
// VRF-Assign Rules
/////////////////////////////////////////////////////////////////////////////
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();
}
// Rebuild all the policy rules to be applied
void FlowEntry::GetPolicyInfo(const VnEntry *vn, const FlowEntry *rflow) {
// Reset old values first
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, rflow);
// Get Sg list
GetSgList(data_.intf_entry.get());
//Get VRF translate ACL
GetVrfAssignAcl();
}
void FlowEntry::GetPolicyInfo() {
GetPolicyInfo(data_.vn_entry.get(), reverse_flow_entry());
}
void FlowEntry::GetPolicyInfo(const VnEntry *vn) {
GetPolicyInfo(vn, reverse_flow_entry());
}
void FlowEntry::GetPolicyInfo(const FlowEntry *rflow) {
GetPolicyInfo(data_.vn_entry.get(), rflow);
}