OpenSDN source code
ksync_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>
32 
33 #include <vr_types.h>
34 #include <nl_util.h>
35 #include <vr_flow.h>
36 #include <ini_parser.h>
37 #include <vr_genetlink.h>
38 
39 #include "ksync_init.h"
40 #include "ksync_flow_memory.h"
41 #include "sandesh_ksync.h"
42 #include "init/agent_param.h"
43 
44 using namespace boost::asio::ip;
45 static const int kTestFlowTableSize = 131072 * sizeof(vr_flow_entry);
46 
47 KSyncMemory::KSyncMemory(KSync *ksync, uint32_t minor_id) :
48  ksync_(ksync),
49  table_path_(),
50  major_devid_(0),
51  minor_devid_(minor_id),
52  table_size_(0),
53  table_entries_count_(0),
54  audit_timer_(TimerManager::CreateTimer
55  (*(ksync->agent()->event_manager())->io_service(),
56  " Audit Timer",
57  ksync->agent()->task_scheduler()->GetTaskId(kTaskFlowAudit),
58  0)),
59  audit_timeout_(0),
60  audit_yield_(0),
61  audit_interval_(0),
62  audit_idx_(0),
63  audit_list_() {
64 }
65 
68 }
69 
73  uint32_t table_count = table_entries_count_;
74  // Compute number of entries to visit per timer interval so that complete
75  // table can be visited in kAuditSweepTime
76  uint32_t timer_per_sec = 1000 / kAuditYieldTimer;
77  uint32_t timer_per_sweep = kAuditSweepTime * timer_per_sec;
78  audit_yield_ = table_count / timer_per_sweep;
83 
85  boost::bind(&KSyncMemory::AuditProcess, this));
86 }
87 
88 void KSyncMemory::Mmap(bool unlink_node, void *khpmem, bool kernel_mode) {
89  // In case of non hugepage kernel mode,
90  // Remove the existing /dev/ file first. We will add it again in vr_table_map
91  if (!khpmem && unlink_node) {
92  const char *error_msg = vr_table_unlink(table_path_.c_str());
93  if (error_msg) {
94  LOG(ERROR, "Error unmapping KSync memory: " << error_msg);
95  assert(0);
96  }
97  }
98  parse_ini_file();
99 
100  // Kernel hugepage present
101  if (khpmem) {
102  table_ = khpmem;
103  } else {
104  // DPDK or kernel without hugepage support
105  const char *table_str;
106  if (kernel_mode) {
107  table_str = NULL;
108  } else {
109  table_str = table_path_.c_str();
110  }
111  const char *mmap_error_msg = vr_table_map(major_devid_, minor_devid_, table_str,
112  table_size_, &table_);
113  if (mmap_error_msg) {
114  LOG(ERROR, "Error mapping KSync memory. Device: " << table_path_ << "; " << mmap_error_msg);
115  assert(0);
116  }
117  LOG(INFO, "Mem mapped dev file:" << table_path_.c_str() << " to addr:" << table_ << "\n");
118  }
119 
121  SetTableSize();
122 }
123 
125  struct nl_client *cl;
126  int attr_len;
127  int encode_len, ret;
128 
129  assert((cl = nl_register_client()) != NULL);
130 
131  assert(nl_socket(cl, AF_NETLINK, SOCK_DGRAM, NETLINK_GENERIC) > 0);
132  assert(nl_connect(cl, 0, 0, 0) == 0);
133 
134  assert(vrouter_obtain_family_id(cl) > 0);
135 
136  assert(nl_build_nlh(cl, cl->cl_genl_family_id, NLM_F_REQUEST) == 0);
137  assert(nl_build_genlh(cl, SANDESH_REQUEST, 0) == 0);
138 
139  attr_len = nl_get_attr_hdr_size();
140 
141  encode_len = EncodeReq(cl, attr_len);
142  nl_build_attr(cl, encode_len, NL_ATTR_VR_MESSAGE_PROTOCOL);
143  nl_update_nlh(cl);
144 
145  if ((ret = nl_sendmsg(cl)) < 0) {
146  LOG(DEBUG, "Error requesting Table message. Error : " << ret);
147  assert(0);
148  }
149 
150  while ((ret = nl_recvmsg(cl)) > 0) {
153  }
154  nl_free_client(cl);
155  return table_size_;
156 }
157 
158 // Steps to map table entry
159 // In case of non huge pages
160 // - Query the table parameters from kernel
161 // - Create device /dev/ with major-num and minor-num
162 // - Map device memory
163 // In case of huge pages
164 // - Just use the huge page memory which is initialized
165 // and passed to this function
166 void KSyncMemory::InitMem(void *hpmem) {
168  Mmap(true, hpmem, true);
169  return;
170 }
171 
173  assert(0);
174 }
175 
177  assert(0);
178 }
179 
181  // Get current time
182  uint64_t t = UTCTimestampUsec();
183 
184  while (!audit_list_.empty()) {
185  AuditEntry list_entry = audit_list_.front();
186  // audit_list_ is sorted on last time of insertion in the list
187  // So, break on finding first entry that cannot be aged
188  if ((t - list_entry.timeout) < audit_timeout_) {
189  /* Wait for audit_timeout_ to create short for the entry */
190  break;
191  }
192  uint32_t idx = list_entry.audit_idx;
193  uint32_t gen_id = list_entry.audit_gen_id;
194  audit_list_.pop_front();
196  CreateProtoAuditEntry(idx, gen_id);
197  }
198 
199  uint32_t count = 0;
200  uint8_t gen_id;
201  assert(audit_yield_);
202  while (count < audit_yield_) {
203  if (IsInactiveEntry(audit_idx_, gen_id)) {
205  audit_list_.push_back(AuditEntry(audit_idx_, gen_id, t));
206  }
207 
208  count++;
209  audit_idx_++;
212  audit_idx_ = 0;
213  }
214  }
215  return true;
216 }
217 
219  struct nl_client *cl;
220  int attr_len;
221  int encode_len;
222 
223  assert((cl = nl_register_client()) != NULL);
224  cl->cl_genl_family_id = KSyncSock::GetNetlinkFamilyId();
225  assert(nl_build_nlh(cl, cl->cl_genl_family_id, NLM_F_REQUEST) == 0);
226  assert(nl_build_genlh(cl, SANDESH_REQUEST, 0) == 0);
227 
228  attr_len = nl_get_attr_hdr_size();
229  encode_len = EncodeReq(cl, attr_len);
230  nl_build_attr(cl, encode_len, NL_ATTR_VR_MESSAGE_PROTOCOL);
231  nl_update_nlh(cl);
232  string ksync_agent_vrouter_sock_path = KSYNC_AGENT_VROUTER_SOCK_PATH;
233  ksync_agent_vrouter_sock_path =
235  ksync_->agent()->params()->cat_ksocketdir() +
236  "dpdk_netlink" : ksync_agent_vrouter_sock_path;
237 
238 #ifdef AGENT_VROUTER_TCP
239  tcp::socket socket(*(ksync_->agent()->event_manager()->io_service()));
240  tcp::endpoint endpoint(ksync_->agent()->vrouter_server_ip(),
242 #else
243  boost::asio::local::stream_protocol::socket
244  socket(*(ksync_->agent()->event_manager()->io_service()));
245  boost::asio::local::stream_protocol::endpoint
246  endpoint(ksync_agent_vrouter_sock_path);
247 #endif
248  boost::system::error_code ec;
249  socket.connect(endpoint, ec);
250  if (ec) {
251  assert(0);
252  }
253 
254  socket.send(boost::asio::buffer(cl->cl_buf, cl->cl_buf_offset), 0, ec);
255  if (ec) {
256  assert(0);
257  }
258 
259  uint32_t len_read = 0;
260  uint32_t data_len = sizeof(struct nlmsghdr);
261  while (len_read < data_len) {
262  len_read = socket.read_some(boost::asio::buffer(cl->cl_buf + len_read,
263  cl->cl_buf_len), ec);
264  if (ec) {
265  assert(0);
266  }
267 
268  if (len_read > sizeof(struct nlmsghdr)) {
269  const struct nlmsghdr *nlh =
270  (const struct nlmsghdr *)((cl->cl_buf));
271  data_len = nlh->nlmsg_len;
272  }
273  }
274 
277  nl_free_client(cl);
278 }
279 
281  GetTableSize();
282  Mmap(false, NULL, false);
283 }
#define kTaskFlowAudit
Definition: agent.h:329
bool cat_is_agent_mocked() const
Definition: agent_param.h:568
std::string cat_ksocketdir() const
Definition: agent_param.h:572
const uint32_t vrouter_server_port() const
Definition: agent.h:976
AgentParam * params() const
Definition: agent.h:1227
EventManager * event_manager() const
Definition: agent.h:1106
const Ip4Address vrouter_server_ip() const
Definition: agent.h:970
boost::asio::io_context * io_service()
Definition: event_manager.h:43
virtual ~KSyncMemory()
Definition: ksync_memory.cc:66
virtual void IncrementHoldFlowCounter()
Definition: ksync_memory.h:48
bool AuditProcess()
uint32_t audit_interval_
Definition: ksync_memory.h:87
void InitMem(void *hpmem)
virtual void DecrementHoldFlowCounter()
Definition: ksync_memory.h:47
void MapSharedMemory()
virtual void InitTest()
static const uint32_t kAuditTimeout
Definition: ksync_memory.h:23
int major_devid_
Definition: ksync_memory.h:76
uint32_t audit_idx_
Definition: ksync_memory.h:88
uint32_t table_entries_count_
Definition: ksync_memory.h:81
virtual bool IsInactiveEntry(uint32_t idx, uint8_t &gen_id)=0
uint32_t audit_yield_
Definition: ksync_memory.h:86
static const uint32_t kAuditYieldTimer
Definition: ksync_memory.h:21
KSync * ksync_
Definition: ksync_memory.h:71
virtual void Shutdown()
virtual int get_entry_size()=0
virtual int EncodeReq(nl_client *nl, uint32_t attr_len)=0
std::string table_path_
Definition: ksync_memory.h:74
std::list< AuditEntry > audit_list_
Definition: ksync_memory.h:89
void Mmap(bool unlink, void *khpmem, bool kernel_mode)
Definition: ksync_memory.cc:88
static const uint32_t kAuditYieldMax
Definition: ksync_memory.h:25
Timer * audit_timer_
Definition: ksync_memory.h:83
static const uint32_t kAuditSweepTime
Definition: ksync_memory.h:19
uint32_t audit_timeout_
Definition: ksync_memory.h:85
virtual void Init()
Definition: ksync_memory.cc:70
int GetKernelTableSize()
static const uint32_t kAuditYieldMin
Definition: ksync_memory.h:27
int minor_devid_
Definition: ksync_memory.h:77
KSyncMemory(KSync *ksync, uint32_t minor_id)
Definition: ksync_memory.cc:47
virtual void CreateProtoAuditEntry(uint32_t index, uint8_t gen_id)=0
virtual void UpdateAgentHoldFlowCounter()
Definition: ksync_memory.h:49
void * table_
Definition: ksync_memory.h:72
void GetTableSize()
virtual void SetTableSize()
Definition: ksync_memory.h:44
static AgentSandeshContext * GetAgentSandeshContext(uint32_t type)
Definition: ksync_sock.h:395
static int GetNetlinkFamilyId()
Definition: ksync_sock.h:392
Agent * agent() const
Definition: ksync_init.h:40
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
static const int kTestFlowTableSize
Definition: ksync_memory.cc:45
#define KSYNC_AGENT_VROUTER_SOCK_PATH
Definition: ksync_sock.h:566
#define LOG(_Level, _Msg)
Definition: logging.h:35
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14