-
Notifications
You must be signed in to change notification settings - Fork 390
/
flow_entry.h
469 lines (415 loc) · 15.8 KB
/
flow_entry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
#ifndef __AGENT_PKT_FLOW_ENTRY_H__
#define __AGENT_PKT_FLOW_ENTRY_H__
#include <boost/uuid/uuid_io.hpp>
#include <boost/intrusive_ptr.hpp>
#include <tbb/atomic.h>
#include <tbb/mutex.h>
#include <base/util.h>
#include <net/address.h>
#include <db/db_table_walker.h>
#include <cmn/agent_cmn.h>
#include <oper/mirror_table.h>
#include <filter/traffic_action.h>
#include <filter/acl_entry.h>
#include <filter/acl.h>
#include <pkt/pkt_types.h>
#include <pkt/pkt_handler.h>
#include <pkt/pkt_init.h>
#include <pkt/pkt_flow_info.h>
#include <sandesh/sandesh_trace.h>
#include <oper/vn.h>
#include <oper/vm.h>
#include <oper/interface_common.h>
#include <oper/nexthop.h>
#include <oper/route_common.h>
#include <oper/sg.h>
#include <oper/vrf.h>
#include <filter/acl.h>
#include <sandesh/common/flow_types.h>
class FlowTableKSyncEntry;
class FlowEntry;
typedef boost::intrusive_ptr<FlowEntry> FlowEntryPtr;
struct FlowKey {
FlowKey() :
family(Address::UNSPEC), nh(0), src_addr(Ip4Address(0)),
dst_addr(Ip4Address(0)), protocol(0),
src_port(0), dst_port(0){
}
FlowKey(uint32_t nh_p, const Ip4Address &sip_p, const Ip4Address &dip_p,
uint8_t proto_p, uint16_t sport_p, uint16_t dport_p)
: family(Address::INET), nh(nh_p), src_addr(sip_p), dst_addr(dip_p),
protocol(proto_p), src_port(sport_p), dst_port(dport_p) {
}
FlowKey(uint32_t nh_p, const IpAddress &sip_p, const IpAddress &dip_p,
uint8_t proto_p, uint16_t sport_p, uint16_t dport_p)
: family(sip_p.is_v4() ? Address::INET : Address::INET6), nh(nh_p),
src_addr(sip_p), dst_addr(dip_p), protocol(proto_p), src_port(sport_p),
dst_port(dport_p) {
}
FlowKey(const FlowKey &key) :
family(key.family), nh(key.nh), src_addr(key.src_addr),
dst_addr(key.dst_addr), protocol(key.protocol), src_port(key.src_port),
dst_port(key.dst_port) {
}
// Comparator for the flow-entry
bool IsLess(const FlowKey &key) const {
if (family != key.family)
return family < key.family;
if (nh != key.nh)
return nh < key.nh;
if (src_addr != key.src_addr)
return src_addr < key.src_addr;
if (dst_addr != key.dst_addr)
return dst_addr < key.dst_addr;
if (protocol != key.protocol)
return protocol < key.protocol;
if (src_port != key.src_port)
return src_port < key.src_port;
return dst_port < key.dst_port;
}
void Reset() {
family = Address::UNSPEC;
nh = -1;
src_addr = Ip4Address(0);
dst_addr = Ip4Address(0);
protocol = -1;
src_port = -1;
dst_port = -1;
}
Address::Family family;
uint32_t nh;
IpAddress src_addr;
IpAddress dst_addr;
uint8_t protocol;
uint16_t src_port;
uint16_t dst_port;
};
struct FlowStats {
FlowStats() : setup_time(0), teardown_time(0), last_modified_time(0),
bytes(0), packets(0), intf_in(0), fip(0),
fip_vm_port_id(Interface::kInvalidIndex) {}
uint64_t setup_time;
uint64_t teardown_time;
uint64_t last_modified_time; //used for aging
uint64_t bytes;
uint64_t packets;
uint32_t intf_in;
// Following fields are required for FIP stats accounting
uint32_t fip;
uint32_t fip_vm_port_id;
};
typedef std::list<MatchAclParams> MatchAclParamsList;
struct MatchPolicy {
MatchPolicy():
m_acl_l(), policy_action(0), m_out_acl_l(), out_policy_action(0),
m_out_sg_acl_l(), out_sg_rule_present(false), out_sg_action(0),
m_sg_acl_l(), sg_rule_present(false), sg_action(0),
m_reverse_sg_acl_l(), reverse_sg_rule_present(false),
reverse_sg_action(0), m_reverse_out_sg_acl_l(),
reverse_out_sg_rule_present(false), reverse_out_sg_action(0),
m_mirror_acl_l(), mirror_action(0), m_out_mirror_acl_l(),
out_mirror_action(0), m_vrf_assign_acl_l(), vrf_assign_acl_action(0),
sg_action_summary(0), action_info() {
}
~MatchPolicy() {}
MatchAclParamsList m_acl_l;
uint32_t policy_action;
MatchAclParamsList m_out_acl_l;
uint32_t out_policy_action;
MatchAclParamsList m_out_sg_acl_l;
bool out_sg_rule_present;
uint32_t out_sg_action;
MatchAclParamsList m_sg_acl_l;
bool sg_rule_present;
uint32_t sg_action;
MatchAclParamsList m_reverse_sg_acl_l;
bool reverse_sg_rule_present;
uint32_t reverse_sg_action;
MatchAclParamsList m_reverse_out_sg_acl_l;
bool reverse_out_sg_rule_present;
uint32_t reverse_out_sg_action;
MatchAclParamsList m_mirror_acl_l;
uint32_t mirror_action;
MatchAclParamsList m_out_mirror_acl_l;
uint32_t out_mirror_action;
MatchAclParamsList m_vrf_assign_acl_l;
uint32_t vrf_assign_acl_action;
// Summary of SG actions
uint32_t sg_action_summary;
FlowAction action_info;
};
struct FlowData {
FlowData() :
smac(), dmac(), source_vn(""), dest_vn(""), source_sg_id_l(),
dest_sg_id_l(), flow_source_vrf(VrfEntry::kInvalidIndex),
flow_dest_vrf(VrfEntry::kInvalidIndex), match_p(), vn_entry(NULL),
intf_entry(NULL), in_vm_entry(NULL), out_vm_entry(NULL),
vrf(VrfEntry::kInvalidIndex),
mirror_vrf(VrfEntry::kInvalidIndex), dest_vrf(),
component_nh_idx((uint32_t)CompositeNH::kInvalidComponentNHIdx),
source_plen(0), dest_plen(0), drop_reason(0),
vrf_assign_evaluated(false), pending_recompute(false),
enable_rpf(true), l2_rpf_plen(Address::kMaxV4PrefixLen) {
}
MacAddress smac;
MacAddress dmac;
std::string source_vn;
std::string dest_vn;
SecurityGroupList source_sg_id_l;
SecurityGroupList dest_sg_id_l;
uint32_t flow_source_vrf;
uint32_t flow_dest_vrf;
MatchPolicy match_p;
VnEntryConstRef vn_entry;
InterfaceConstRef intf_entry;
VmEntryConstRef in_vm_entry;
VmEntryConstRef out_vm_entry;
NextHopConstRef nh;
uint32_t vrf;
uint32_t mirror_vrf;
uint32_t dest_vrf;
uint32_t component_nh_idx;
// Stats
uint8_t source_plen;
uint8_t dest_plen;
uint16_t drop_reason;
bool vrf_assign_evaluated;
bool pending_recompute;
uint32_t if_index_info;
TunnelInfo tunnel_info;
// map for references to the routes which were ignored due to more specific
// route this will be used to trigger flow re-compute to use more specific
// on route add. key for the map is vrf and data is prefix length
FlowRouteRefMap flow_source_plen_map;
FlowRouteRefMap flow_dest_plen_map;
bool enable_rpf;
uint8_t l2_rpf_plen;
};
class FlowEntry {
public:
enum FlowShortReason {
SHORT_UNKNOWN = 0,
SHORT_UNAVIALABLE_INTERFACE,
SHORT_IPV4_FWD_DIS,
SHORT_UNAVIALABLE_VRF,
SHORT_NO_SRC_ROUTE,
SHORT_NO_DST_ROUTE,
SHORT_AUDIT_ENTRY,
SHORT_VRF_CHANGE,
SHORT_NO_REVERSE_FLOW,
SHORT_REVERSE_FLOW_CHANGE,
SHORT_NAT_CHANGE,
SHORT_FLOW_LIMIT,
SHORT_LINKLOCAL_SRC_NAT,
SHORT_FAILED_VROUTER_INSTALL,
SHORT_MAX
};
enum FlowDropReason {
DROP_UNKNOWN = 0,
DROP_POLICY = SHORT_MAX,
DROP_OUT_POLICY,
DROP_SG,
DROP_OUT_SG,
DROP_REVERSE_SG,
DROP_REVERSE_OUT_SG
};
enum FlowPolicyState {
NOT_EVALUATED,
IMPLICIT_ALLOW, /* Due to No Acl rules */
IMPLICIT_DENY,
DEFAULT_GW_ICMP_OR_DNS, /* DNS/ICMP pkt to/from default gateway */
LINKLOCAL_FLOW, /* No policy applied for linklocal flow */
MULTICAST_FLOW, /* No policy applied for multicast flow */
NON_IP_FLOW, /* Flow due to bridging */
};
static const uint32_t kInvalidFlowHandle=0xFFFFFFFF;
static const uint8_t kMaxMirrorsPerFlow=0x2;
static const std::map<FlowPolicyState, const char*> FlowPolicyStateStr;
// Don't go beyond PCAP_END, pcap type is one byte
enum PcapType {
PCAP_CAPTURE_HOST = 1,
PCAP_FLAGS = 2,
PCAP_SOURCE_VN = 3,
PCAP_DEST_VN = 4,
PCAP_TLV_END = 255
};
enum FlowEntryFlags {
NatFlow = 1 << 0,
LocalFlow = 1 << 1,
ShortFlow = 1 << 2,
LinkLocalFlow = 1 << 3,
ReverseFlow = 1 << 4,
EcmpFlow = 1 << 5,
IngressDir = 1 << 6,
Trap = 1 << 7,
Multicast = 1 << 8,
// a local port bind is done (used as as src port for linklocal nat)
LinkLocalBindLocalSrcPort = 1 << 9,
TcpAckFlow = 1 << 10,
UnknownUnicastFlood = 1 << 11
};
FlowEntry(const FlowKey &k);
virtual ~FlowEntry();
void InitFwdFlow(const PktFlowInfo *info, const PktInfo *pkt,
const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl);
void InitRevFlow(const PktFlowInfo *info, const PktInfo *pkt,
const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl);
void InitAuditFlow(uint32_t flow_idx);
static void Init();
static AgentRoute *GetL2Route(const VrfEntry *entry, const MacAddress &mac);
static AgentRoute *GetUcRoute(const VrfEntry *entry, const IpAddress &addr);
static const SecurityGroupList &default_sg_list() {
return default_sg_list_;
}
// Flow accessor routines
int GetRefCount() { return refcount_; }
const FlowStats &stats() const { return stats_;}
const FlowKey &key() const { return key_;}
FlowData &data() { return data_;}
const FlowData &data() const { return data_;}
const uuid &flow_uuid() const { return flow_uuid_; }
const uuid &egress_uuid() const { return egress_uuid_; }
bool l3_flow() const { return l3_flow_; }
uint32_t flow_handle() const { return flow_handle_; }
void set_flow_handle(uint32_t flow_handle, FlowTable* table);
FlowEntry *reverse_flow_entry() { return reverse_flow_entry_.get(); }
const FlowEntry *reverse_flow_entry() const {
return reverse_flow_entry_.get();
}
void set_reverse_flow_entry(FlowEntry *reverse_flow_entry) {
reverse_flow_entry_ = reverse_flow_entry;
}
bool is_flags_set(const FlowEntryFlags &flags) const {
return (flags_ & flags);
}
void set_flags(const FlowEntryFlags &flags) { flags_ |= flags; }
void reset_flags(const FlowEntryFlags &flags) { flags_ &= ~flags; }
void set_source_sg_id_l(const SecurityGroupList &sg_l) {
data_.source_sg_id_l = sg_l;
}
void set_dest_sg_id_l(const SecurityGroupList &sg_l) {
data_.dest_sg_id_l = sg_l;
}
int linklocal_src_port() const { return linklocal_src_port_; }
int linklocal_src_port_fd() const { return linklocal_src_port_fd_; }
const std::string& acl_assigned_vrf() const;
uint32_t acl_assigned_vrf_index() const;
uint32_t reverse_flow_fip() const;
uint32_t reverse_flow_vmport_id() const;
void UpdateFipStatsInfo(uint32_t fip, uint32_t id);
const std::string &sg_rule_uuid() const { return sg_rule_uuid_; }
const std::string &nw_ace_uuid() const { return nw_ace_uuid_; }
const std::string &peer_vrouter() const { return peer_vrouter_; }
TunnelType tunnel_type() const { return tunnel_type_; }
uint16_t underlay_source_port() const { return underlay_source_port_; }
void set_underlay_source_port(uint16_t port) {
underlay_source_port_ = port;
}
bool underlay_sport_exported() const { return underlay_sport_exported_; }
void set_underlay_sport_exported(bool value) {
underlay_sport_exported_ = value;
}
uint16_t short_flow_reason() const { return short_flow_reason_; }
bool set_pending_recompute(bool value);
const MacAddress &smac() const { return data_.smac; }
const MacAddress &dmac() const { return data_.dmac; }
tbb::mutex &mutex() { return mutex_; }
FlowTableKSyncEntry *ksync_entry() const { return ksync_entry_; }
void set_ksync_entry(FlowTableKSyncEntry *ksync_entry) {
ksync_entry_ = ksync_entry;
}
const Interface *intf_entry() const { return data_.intf_entry.get(); }
const VnEntry *vn_entry() const { return data_.vn_entry.get(); }
const VmEntry *in_vm_entry() const { return data_.in_vm_entry.get(); }
const VmEntry *out_vm_entry() const { return data_.out_vm_entry.get(); }
const NextHop *nh() const { return data_.nh.get(); }
const MatchPolicy &match_p() const { return data_.match_p; }
bool ImplicitDenyFlow() const {
return ((data_.match_p.action_info.action &
(1 << TrafficAction::IMPLICIT_DENY)) ? true : false);
}
bool deleted() { return deleted_; }
// Flow action routines
bool ActionRecompute();
bool DoPolicy();
void MakeShortFlow(FlowShortReason reason);
void SetMirrorVrfFromAction();
void SetVrfAssignEntry();
void ComputeReflexiveAction();
uint32_t MatchAcl(const PacketHeader &hdr,
MatchAclParamsList &acl, bool add_implicit_deny,
bool add_implicit_allow, FlowPolicyInfo *info);
void ResetPolicy();
void ResetStats();
void FillFlowInfo(FlowInfo &info);
void GetPolicyInfo();
void GetPolicyInfo(const VnEntry *vn);
void GetVrfAssignAcl();
void SetMirrorVrf(const uint32_t id) {data_.mirror_vrf = id;}
void GetPolicy(const VnEntry *vn);
void GetNonLocalFlowSgList(const VmInterface *vm_port);
void GetLocalFlowSgList(const VmInterface *vm_port,
const VmInterface *reverse_vm_port);
void GetSgList(const Interface *intf);
void SetPacketHeader(PacketHeader *hdr);
void SetOutPacketHeader(PacketHeader *hdr);
void set_deleted(bool deleted) { deleted_ = deleted; }
void SetAclAction(std::vector<AclAction> &acl_action_l) const;
void UpdateReflexiveAction();
void SetAclFlowSandeshData(const AclDBEntry *acl,
FlowSandeshData &fe_sandesh_data) const;
private:
friend class FlowTable;
friend class FlowStatsCollector;
friend void intrusive_ptr_add_ref(FlowEntry *fe);
friend void intrusive_ptr_release(FlowEntry *fe);
bool SetRpfNH(FlowTable *ft, const AgentRoute *rt);
bool InitFlowCmn(const PktFlowInfo *info, const PktControlInfo *ctrl,
const PktControlInfo *rev_ctrl);
void GetSourceRouteInfo(const AgentRoute *rt);
void GetDestRouteInfo(const AgentRoute *rt);
void UpdateRpf();
FlowKey key_;
FlowData data_;
FlowStats stats_;
uuid flow_uuid_;
//egress_uuid is used only during flow-export and applicable only for
//local-flows
uuid egress_uuid_;
bool l3_flow_;
uint32_t flow_handle_;
FlowEntryPtr reverse_flow_entry_;
FlowTableKSyncEntry *ksync_entry_;
static tbb::atomic<int> alloc_count_;
bool deleted_;
uint32_t flags_;
uint16_t short_flow_reason_;
// linklocal port - used as nat src port, agent locally binds to this port
uint16_t linklocal_src_port_;
// fd of the socket used to locally bind in case of linklocal
int linklocal_src_port_fd_;
std::string sg_rule_uuid_;
std::string nw_ace_uuid_;
//IP address of the src vrouter for egress flows and dst vrouter for
//ingress flows. Used only during flow-export
std::string peer_vrouter_;
//Underlay IP protocol type. Used only during flow-export
TunnelType tunnel_type_;
//Underlay source port. 0 for local flows. Used during flow-export
uint16_t underlay_source_port_;
bool underlay_sport_exported_;
// atomic refcount
tbb::atomic<int> refcount_;
tbb::mutex mutex_;
static InetUnicastRouteEntry inet4_route_key_;
static InetUnicastRouteEntry inet6_route_key_;
static SecurityGroupList default_sg_list_;
};
void intrusive_ptr_add_ref(FlowEntry *fe);
void intrusive_ptr_release(FlowEntry *fe);
#endif // __AGENT_PKT_FLOW_ENTRY_H__