OpenSDN source code
ksync_flow_memory.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <sys/socket.h>
7 #if defined(__linux__)
8 #include <linux/netlink.h>
9 #endif
10 #include <fcntl.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <sys/ipc.h>
14 #include <sys/shm.h>
15 #include <asm/types.h>
16 #include <boost/asio.hpp>
17 
18 #include <base/timer.h>
19 #include <base/task_trigger.h>
20 #include <base/address_util.h>
21 #include <cmn/agent_cmn.h>
22 #include <services/services_init.h>
23 #include <uve/stats_collector.h>
25 #include <pkt/flow_proto.h>
26 #include <ksync/ksync_index.h>
27 #include <ksync/ksync_entry.h>
28 #include <ksync/ksync_object.h>
29 #include <ksync/ksync_netlink.h>
30 #include <ksync/ksync_sock.h>
31 #include <ksync/ksync_sock_user.h>
33 
34 #include <vr_types.h>
35 #include <nl_util.h>
36 #include <vr_flow.h>
37 #include <vr_genetlink.h>
38 
39 #include "ksync_init.h"
40 #include "ksync_flow_memory.h"
41 #include "sandesh_ksync.h"
42 
43 using namespace boost::asio::ip;
44 static const int kTestFlowTableSize = 131072 * sizeof(vr_flow_entry);
45 
46 KSyncFlowMemory::KSyncFlowMemory(KSync *ksync, uint32_t minor_id) :
47  KSyncMemory(ksync, minor_id) {
48  table_path_ = FLOW_TABLE_DEV;
50 }
51 
54  proto->Register(boost::bind(&KSyncFlowMemory::GetFlowKey, this, _1, _2, _3));
55 
57 }
58 
59 int KSyncFlowMemory::EncodeReq(struct nl_client *cl, uint32_t attr_len) {
60  int encode_len, error;
61 
62  vr_flow_table_data info;
63  info.set_ftable_op(flow_op::FLOW_TABLE_GET);
64  info.set_ftable_size(0);
65  info.set_ftable_dev(0);
66  info.set_ftable_file_path("");
67  info.set_ftable_processed(0);
68  info.set_ftable_hold_oflows(0);
69  info.set_ftable_added(0);
70  info.set_ftable_cpus(0);
71  info.set_ftable_created(0);
72  info.set_ftable_oflow_entries(0);
73  encode_len = info.WriteBinary(nl_get_buf_ptr(cl) + attr_len,
74  nl_get_buf_len(cl), &error);
75  return encode_len;
76 }
77 
79  return sizeof(vr_flow_entry);
80 }
81 
84  flow_table_ = static_cast<vr_flow_entry *>(table_);
85 }
86 
87 void KSyncFlowMemory::CreateProtoAuditEntry(uint32_t idx, uint8_t gen_id) {
88  const vr_flow_entry *ventry = GetKernelFlowEntry(idx, false);
89  // Audit and remove entry if its still in HOLD state
90  if (ventry && ventry->fe_gen_id == gen_id &&
91  ventry->fe_action == VR_FLOW_ACTION_HOLD) {
92  IpAddress sip, dip;
93  VrFlowToIp(ventry, &sip, &dip);
94  FlowKey key(ventry->fe_key.flow_nh_id, sip, dip,
95  ventry->fe_key.flow_proto,
96  ntohs(ventry->fe_key.flow_sport),
97  ntohs(ventry->fe_key.flow_dport));
98 
99  FlowProto *proto = ksync_->agent()->pkt()->get_flow_proto();
100  proto->CreateAuditEntry(key, idx, gen_id);
101  }
102 }
103 
106  return;
107 }
108 
111  return;
112 }
113 
116  hold_flow_counter_ = 0;
117  return;
118 }
119 
120 bool KSyncFlowMemory::IsInactiveEntry(uint32_t audit_idx, uint8_t &gen_id) {
121  const vr_flow_entry *vflow_entry =
122  GetKernelFlowEntry(audit_idx, false);
123  if (vflow_entry && vflow_entry->fe_action == VR_FLOW_ACTION_HOLD) {
124  gen_id = vflow_entry->fe_gen_id;
125  return true;
126  }
127  return false;
128 }
129 
130 void KSyncFlowMemory::VrFlowToIp(const vr_flow_entry *kflow, IpAddress *sip,
131  IpAddress *dip) {
132  if (kflow->fe_key.flow_family == AF_INET) {
133  *sip = Ip4Address(ntohl(kflow->fe_key.key_u.ip4_key.ip4_sip));
134  *dip = Ip4Address(ntohl(kflow->fe_key.key_u.ip4_key.ip4_dip));
135  } else {
136  const unsigned char *k_sip = kflow->fe_key.key_u.ip6_key.ip6_sip;
137  const unsigned char *k_dip = kflow->fe_key.key_u.ip6_key.ip6_dip;
138  Ip6Address::bytes_type sbytes;
139  Ip6Address::bytes_type dbytes;
140  for (int i = 0; i < 16; i++) {
141  sbytes[i] = k_sip[i];
142  dbytes[i] = k_dip[i];
143  }
144  *sip = Ip6Address(sbytes);
145  *dip = Ip6Address(dbytes);
146  }
147 }
148 
149 void KSyncFlowMemory::KFlow2FlowKey(const vr_flow_entry *kflow,
150  FlowKey *key) const {
151  key->nh = kflow->fe_key.flow4_nh_id;
152  Address::Family family = (kflow->fe_key.flow_family == AF_INET)?
154  VrFlowToIp(kflow, &key->src_addr, &key->dst_addr);
155  key->src_port = ntohs(kflow->fe_key.flow4_sport);
156  key->dst_port = ntohs(kflow->fe_key.flow4_dport);
157  key->protocol = kflow->fe_key.flow4_proto;
158  key->family = family;
159 }
160 
161 const vr_flow_entry *KSyncFlowMemory::GetValidKFlowEntry(const FlowKey &key,
162  uint32_t idx,
163  uint8_t gen_id) const {
164  const vr_flow_entry *kflow = GetKernelFlowEntry(idx, false);
165  if (!kflow) {
166  return NULL;
167  }
168  if (key.protocol == IPPROTO_TCP) {
169  FlowKey rhs;
170  KFlow2FlowKey(kflow, &rhs);
171  if (!key.IsEqual(rhs)) {
172  return NULL;
173  }
174  if (kflow->fe_gen_id != gen_id) {
175  return NULL;
176  }
177  }
178  return kflow;
179 }
180 
182  (uint32_t idx, bool ignore_active_status) const {
183  if (idx == FlowEntry::kInvalidFlowHandle) {
184  return NULL;
185  }
186 
187  if (idx >= table_entries_count_) {
188  /* if index is outside the range of flow table entries return NULL */
189  return NULL;
190  }
191 
192  if (ignore_active_status) {
193  return &flow_table_[idx];
194  }
195 
196  if (flow_table_[idx].fe_flags & VR_FLOW_FLAG_ACTIVE) {
197  return &flow_table_[idx];
198  }
199  return NULL;
200 }
201 
202 bool KSyncFlowMemory::GetFlowKey(uint32_t index, FlowKey *key, bool *is_nat_flow) {
203  const vr_flow_entry *kflow = GetKernelFlowEntry(index, false);
204  if (!kflow) {
205  return false;
206  }
207  key->nh = kflow->fe_key.flow4_nh_id;
208  Address::Family family = (kflow->fe_key.flow_family == AF_INET)?
210  VrFlowToIp(kflow, &key->src_addr, &key->dst_addr);
211  key->src_port = ntohs(kflow->fe_key.flow4_sport);
212  key->dst_port = ntohs(kflow->fe_key.flow4_dport);
213  key->protocol = kflow->fe_key.flow4_proto;
214  key->family = family;
215  if (kflow->fe_action == VR_FLOW_ACTION_NAT) {
216  *is_nat_flow = true;
217  } else {
218  *is_nat_flow = false;
219  }
220  return true;
221 }
222 
223 bool KSyncFlowMemory::IsEvictionMarked(const vr_flow_entry *entry,
224  uint16_t flags) const {
225  if (!entry) {
226  return false;
227  }
228  if (flags & VR_FLOW_FLAG_EVICTED) {
229  return true;
230  }
231  return false;
232 }
233 
234 const vr_flow_entry *KSyncFlowMemory::GetKFlowStats(const FlowKey &key,
235  uint32_t idx,
236  uint8_t gen_id,
237  vr_flow_stats *stat) const {
238  const vr_flow_entry *kflow = GetValidKFlowEntry(key, idx, gen_id);
239  if (!kflow) {
240  return NULL;
241  }
242  *stat = kflow->fe_stats;
243  kflow = GetValidKFlowEntry(key, idx, gen_id);
244  return kflow;
245 }
246 
247 void KSyncFlowMemory::ReadFlowInfo(const vr_flow_entry *kflow,
248  vr_flow_stats *stat, KFlowData *info) const {
249  *stat = kflow->fe_stats;
250  info->underlay_src_port = kflow->fe_udp_src_port;
251  info->tcp_flags = kflow->fe_tcp_flags;
252  info->flags = kflow->fe_flags;
253 }
254 
255 const vr_flow_entry *KSyncFlowMemory::GetKFlowStatsAndInfo(const FlowKey &key,
256  uint32_t idx,
257  uint8_t gen_id,
258  vr_flow_stats *stats,
259  KFlowData *info)
260  const {
261  const vr_flow_entry *kflow = GetKernelFlowEntry(idx, false);
262  if (!kflow) {
263  return NULL;
264  }
265  if (key.protocol == IPPROTO_TCP) {
266  FlowKey rhs;
267  KFlow2FlowKey(kflow, &rhs);
268  if (!key.IsEqual(rhs)) {
269  return NULL;
270  }
271 
272  ReadFlowInfo(kflow, stats, info);
273 
274  if (kflow->fe_gen_id != gen_id) {
275  return NULL;
276  }
277  } else {
278  ReadFlowInfo(kflow, stats, info);
279  }
280  return kflow;
281 }
282 
285  memset(table_, 0, kTestFlowTableSize);
288  audit_timeout_ = 100 * 1000; // timout immediately.
289  SetTableSize();
290 }
291 
294 }
295 
296 void vr_flow_req::Process(SandeshContext *context) {
297  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
298  ioc->FlowMsgHandler(this);
299 }
300 
301 void vr_flow_response::Process(SandeshContext *context) {
302  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
303  ioc->FlowResponseHandler(this);
304 }
305 
306 void vr_flow_table_data::Process(SandeshContext *context) {
307  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
308  ioc->FlowTableInfoHandler(this);
309 }
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:16
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
Family
Definition: address.h:25
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
virtual void FlowTableInfoHandler(vr_flow_table_data *r)
Definition: ksync_sock.h:68
virtual void FlowResponseHandler(vr_flow_response *req)
Definition: ksync_sock.h:67
virtual void FlowMsgHandler(vr_flow_req *req)=0
void update_hold_flow_count(uint32_t value)
Definition: agent_stats.h:97
ServicesModule * services() const
Definition: agent.cc:977
void set_flow_table_size(uint32_t count)
Definition: agent.cc:1123
AgentStats * stats() const
Definition: agent.cc:885
PktModule * pkt() const
Definition: agent.cc:969
static const uint32_t kInvalidFlowHandle
Definition: flow_entry.h:522
void CreateAuditEntry(const FlowKey &key, uint32_t flow_handle, uint8_t gen_id)
Definition: flow_proto.cc:620
void Register(FlowIndexToKeyFn fn)
static void VrFlowToIp(const vr_flow_entry *kflow, IpAddress *sip, IpAddress *dip)
virtual void Shutdown()
void UpdateAgentHoldFlowCounter()
const vr_flow_entry * GetKernelFlowEntry(uint32_t idx, bool ignore_active_status) const
KSyncFlowMemory(KSync *ksync, uint32_t minor_id)
virtual void Init()
bool GetFlowKey(uint32_t index, FlowKey *key, bool *is_nat_flow)
void KFlow2FlowKey(const vr_flow_entry *entry, FlowKey *key) const
virtual void CreateProtoAuditEntry(uint32_t index, uint8_t gen_id)
virtual int get_entry_size()
virtual bool IsInactiveEntry(uint32_t idx, uint8_t &gen_id)
virtual void SetTableSize()
void ReadFlowInfo(const vr_flow_entry *k_flow, vr_flow_stats *stats, KFlowData *info) const
const vr_flow_entry * flow_table_
const vr_flow_entry * GetKFlowStats(const FlowKey &key, uint32_t idx, uint8_t gen_id, vr_flow_stats *stats) const
virtual int EncodeReq(nl_client *nl, uint32_t attr_len)
const vr_flow_entry * GetKFlowStatsAndInfo(const FlowKey &key, uint32_t idx, uint8_t gen_id, vr_flow_stats *stats, KFlowData *info) const
const vr_flow_entry * GetValidKFlowEntry(const FlowKey &key, uint32_t idx, uint8_t gen_id) const
bool IsEvictionMarked(const vr_flow_entry *entry, uint16_t flags) const
uint32_t hold_flow_counter_
virtual void InitTest()
uint32_t table_entries_count_
Definition: ksync_memory.h:81
uint32_t audit_yield_
Definition: ksync_memory.h:86
KSync * ksync_
Definition: ksync_memory.h:71
std::string table_path_
Definition: ksync_memory.h:74
uint32_t audit_timeout_
Definition: ksync_memory.h:85
virtual void Init()
Definition: ksync_memory.cc:70
void * table_
Definition: ksync_memory.h:72
static void FlowMmapFree()
static vr_flow_entry * FlowMmapAlloc(int size)
Agent * agent() const
Definition: ksync_init.h:40
FlowProto * get_flow_proto() const
Definition: pkt_init.h:44
IcmpErrorProto * icmp_error_proto() const
Definition: services_init.h:39
static const int kTestFlowTableSize
bool IsEqual(const FlowKey &key) const
Definition: flow_entry.h:177
uint8_t protocol
Definition: flow_entry.h:216
IpAddress src_addr
Definition: flow_entry.h:214
IpAddress dst_addr
Definition: flow_entry.h:215
uint16_t src_port
Definition: flow_entry.h:217
Address::Family family
Definition: flow_entry.h:212
uint32_t nh
Definition: flow_entry.h:213
uint16_t dst_port
Definition: flow_entry.h:218
uint16_t tcp_flags
uint16_t underlay_src_port