OpenSDN source code
ksync_sock_uds.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "ksync_sock.h"
7 
8 #include <boost/asio.hpp>
9 #include <boost/bind/bind.hpp>
10 #include <errno.h>
11 
12 class AgentParam;
13 
14 using namespace boost::asio;
15 using namespace boost::placeholders;
16 
18 // KSyncSockUds routines
20 //Unix domain socket class for interacting with dpdk vrouter
21 
22 class KSyncSockUdsReadTask : public Task {
23 public:
25  Task(scheduler->GetTaskId("Ksync::KSyncSockUdsRead"), 0), queue_(queue) {
26  }
28  }
29 
30  bool Run() {
31  queue_->Run();
32  return true;
33  }
34  std::string Description() const { return "KSyncSockUdsRead"; }
35 private:
37 
38 };
39 
40 
41 
43 
44 KSyncSockUds::KSyncSockUds(boost::asio::io_context &ios) :
45  sock_(ios),
46  server_ep_(sockpath_),
47  rx_buff_(NULL),
48  rx_buff_q_(NULL),
49  remain_(0),
50  socket_(0),
51  connected_(false) {
52  boost::system::error_code ec;
55 retry:;
56  sock_.connect(server_ep_, ec);
57  if (ec) {
58  sleep(1);
59  goto retry;
60  }
61  socket_ = sock_.native_handle();
62  connected_ = true;
63  rx_buff_ = new char[10*kBufLen];
64  rx_buff_q_ = new char[10*kBufLen];
65 }
66 
69  char *ret_buff = new char[1024*kBufLen];
70  boost::system::error_code ec;
71 
72  // Read data from the socket and append it to the existing
73  // unprocessed data in the local buffer.
74  while (1) {
75  char *bufp = rx_buff_;
76  struct nlmsghdr *nlh = NULL;
77  struct nlmsghdr tnlh;
78  size_t offset = 0;
79  int ret_val;
80  size_t bytes_transferred = 0;
81  bytes_transferred = ret_val = recv(socket_, rx_buff_, 10*kBufLen, 0);
82  if (ret_val == 0) {
83  // connection reset by peer
84  // close socket and exit
85  sock_.close(ec);
86  LOG(INFO, " dpdk vrouter is down, exiting.. errno:" << errno);
87  exit(0);
88  }
89  if (ret_val < 0) {
90  if (errno != EAGAIN) {
91  sock_.close(ec);
92  connected_ = false;
93 retry:;
94  sock_.connect(server_ep_, ec);
95  if (ec) {
96  sleep(1);
97  goto retry;
98  }
99  socket_ = sock_.native_handle();
100  connected_ = true;
101  }
102  continue;
103  }
104 
105  if (remain_ != 0) {
106  if (remain_ < sizeof(struct nlmsghdr)) {
107  memcpy((char *)&tnlh, rx_buff_q_, remain_);
108  memcpy(((char *)&tnlh) + remain_, rx_buff_,
109  (sizeof(struct nlmsghdr) - remain_));
110  nlh = &tnlh;
111  } else {
112  nlh = (struct nlmsghdr *)rx_buff_q_;
113  }
114  if (remain_ > nlh->nlmsg_len)
115  assert(0);
116  memcpy(ret_buff, rx_buff_q_, remain_);
117  memcpy(ret_buff+remain_, rx_buff_, nlh->nlmsg_len - remain_);
118  bufp += (nlh->nlmsg_len - remain_);
119  ctxt->SetErrno(0);
120  ProcessDataInline(ret_buff);
121  offset = nlh->nlmsg_len - remain_;
122  }
123  while (offset < bytes_transferred) {
124  if ((bytes_transferred - offset) >= (sizeof(struct nlmsghdr))) {
125  nlh = (struct nlmsghdr *)(rx_buff_ + offset);
126  if ((bytes_transferred - offset) >= nlh->nlmsg_len) {
127  memcpy(ret_buff, rx_buff_ + offset, nlh->nlmsg_len);
128  ctxt->SetErrno(0);
129  ProcessDataInline(ret_buff);
130  offset += nlh->nlmsg_len;
131  } else {
132  break;
133  }
134  } else {
135  break;
136  }
137  }
138  memcpy(rx_buff_q_, rx_buff_ + offset, bytes_transferred - offset);
139  remain_ = bytes_transferred - offset;
140  }
141  return true;
142 }
143 
144 void KSyncSockUds::Init(io_service &ios, const std::string &cpu_pin_policy,
145  const std::string &sockpathvr) {
146  sockpath_ = sockpathvr;
148  SetNetlinkFamilyId(10);
149  KSyncSock::Init(false, cpu_pin_policy);
150 }
151 
152 uint32_t KSyncSockUds::GetSeqno(char *data) {
153  return GetNetlinkSeqno(data);
154 }
155 
156 bool KSyncSockUds::IsMoreData(char *data) {
157  return NetlinkMsgDone(data);
158 }
159 
160 bool KSyncSockUds::Decoder(char *data, AgentSandeshContext *context) {
161  KSyncSockNetlink::NetlinkDecoder(data, context);
162  return true;
163 }
164 
166  KSyncBulkSandeshContext *bulk_sandesh_context) {
167  // Get sandesh buffer and buffer-length
168  uint32_t buf_len = 0;
169  char *buf = NULL;
170  GetNetlinkPayload(data, &buf, &buf_len);
171  return bulk_sandesh_context->Decoder(buf, buf_len, NLA_ALIGNTO, IsMoreData(data));
172 }
173 
174 void KSyncSockUds::AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
175  HandlerCb cb) {
176  if (connected_ == true)
177  SendTo(iovec, seq_no);
178 }
179 
180 size_t KSyncSockUds::SendTo(KSyncBufferList *iovec, uint32_t seq_no) {
181  size_t len = 0, ret;
182  struct msghdr msg;
183  struct iovec iov[max_bulk_msg_count_*2];
184  int i;
185 
186  memset(&msg, 0, sizeof(msg));
187  msg.msg_iov = iov;
188 
190  int offset = nl_client_->cl_buf_offset;
192 
193  KSyncBufferList::iterator it = iovec->begin();
194  iovec->insert(it, buffer((char *)nl_client_->cl_buf, offset));
195 
196  int count = iovec->size();
197  for(i = 0; i < count; i++) {
198  mutable_buffers_1 buf = iovec->at(i);
199  size_t buf_size = boost::asio::buffer_size(buf);
200  void* cbuf = boost::asio::buffer_cast<void*>(buf);
201  len += buf_size;
202  iov[i].iov_base = cbuf;
203  iov[i].iov_len = buf_size;
204  }
205 
206  msg.msg_iovlen = i;
207  ret = sendmsg(socket_, &msg, 0);
208  if (ret != len) {
209  LOG(ERROR, "sendmsg failure " << ret << "len " << len);
210  }
211  return len;
212 }
213 
214 bool KSyncSockUds::Validate(char *data) {
215  return true;
216 }
217 
218 void KSyncSockUds::AsyncReceive(mutable_buffers_1 buf, HandlerCb cb) {
219  static int started = 0;
220  if (!started) {
222  //Receive is handled in a separate Run() thread
223  KSyncSockUdsReadTask *task = new KSyncSockUdsReadTask(scheduler, this);
224  scheduler->Enqueue(task);
225  started = 1;
226  }
227 }
228 
229 void KSyncSockUds::Receive(mutable_buffers_1 buf) {
230  boost::system::error_code ec;
231  uint32_t bytes_read = 0;
232  const struct nlmsghdr *nlh = NULL;
233 
234  char *netlink_header(buffer_cast<char *>(buf));
235 
236  while (bytes_read < sizeof(struct nlmsghdr)) {
237  char *buffer = netlink_header + bytes_read;
238  bytes_read += recv(socket_, buffer, sizeof(struct nlmsghdr) - bytes_read, 0);
239  //Data read is lesser than netlink header
240  //continue reading
241  if (bytes_read == sizeof(struct nlmsghdr)) {
242  nlh = buffer_cast<struct nlmsghdr *>(buf);
243  }
244  }
245 
246  if (nlh->nlmsg_type == NLMSG_ERROR) {
247  LOG(ERROR, "Netlink error for seqno " << nlh->nlmsg_seq
248  << " len " << nlh->nlmsg_len);
249  assert(0);
250  }
251 
252  bytes_read = 0;
253  uint32_t payload_size = nlh->nlmsg_len - sizeof(struct nlmsghdr);
254  char *data(buffer_cast<char *>(buf + sizeof(struct nlmsghdr)));
255 
256  while (bytes_read < payload_size) {
257  char *buffer = data + bytes_read;
258  bytes_read += recv(socket_, buffer, payload_size - bytes_read, 0);
259  }
260 }
virtual void SetErrno(int err)
Definition: ksync_sock.h:79
bool Decoder(char *buff, uint32_t buff_len, uint32_t alignment, bool more)
Definition: ksync_sock.cc:921
KSyncSockUdsReadTask(TaskScheduler *scheduler, KSyncSockUds *queue)
bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
std::string Description() const
Gives a description of the task.
KSyncSockUds * queue_
virtual bool IsMoreData(char *data)
static string sockpath_
Definition: ksync_sock.h:598
char * rx_buff_
Definition: ksync_sock.h:593
static void Init(boost::asio::io_context &ios, const std::string &cpu_pin_policy, const std::string &sockpathvr="")
size_t remain_
Definition: ksync_sock.h:595
KSyncSockUds(boost::asio::io_context &ios)
virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no, HandlerCb cb)
virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no)
boost::asio::local::stream_protocol::endpoint server_ep_
Definition: ksync_sock.h:592
boost::asio::local::stream_protocol::socket sock_
Definition: ksync_sock.h:591
virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt)
virtual void Receive(boost::asio::mutable_buffers_1)
virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb)
virtual bool Validate(char *data)
virtual bool Decoder(char *data, AgentSandeshContext *ctxt)
virtual uint32_t GetSeqno(char *data)
char * rx_buff_q_
Definition: ksync_sock.h:594
virtual bool Run(void)
static AgentSandeshContext * GetAgentSandeshContext(uint32_t type)
Definition: ksync_sock.h:395
void reset_use_wait_tree()
Definition: ksync_sock.h:415
void set_process_data_inline()
Definition: ksync_sock.h:416
static void Init(bool use_work_queue, const std::string &cpu_pin_policy)
Definition: ksync_sock.cc:232
void ProcessDataInline(char *data)
Definition: ksync_sock.cc:840
uint32_t bulk_buf_size_
Definition: ksync_sock.h:445
static void SetSockTableEntry(KSyncSock *sock)
Definition: ksync_sock.cc:270
static void SetNetlinkFamilyId(int id)
Definition: ksync_sock.cc:275
boost::function< void(const boost::system::error_code &, size_t)> HandlerCb
Definition: ksync_sock.h:327
static const unsigned kBufLen
Definition: ksync_sock.h:317
nl_client * nl_client_
Definition: ksync_sock.h:427
uint32_t max_bulk_msg_count_
Definition: ksync_sock.h:437
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
bool NetlinkMsgDone(char *data)
Definition: ksync_sock.cc:68
void GetNetlinkPayload(char *data, char **buf, uint32_t *buf_len)
Definition: ksync_sock.cc:119
void UpdateNetlink(nl_client *client, uint32_t len, uint32_t seq_no)
Definition: ksync_sock.cc:146
uint32_t GetNetlinkSeqno(char *data)
Definition: ksync_sock.cc:63
void ResetNetlink(nl_client *client)
Definition: ksync_sock.cc:139
#define KSYNC_AGENT_VROUTER_SOCK_PATH
Definition: ksync_sock.h:566
std::vector< boost::asio::mutable_buffers_1 > KSyncBufferList
Definition: ksync_sock.h:38
#define LOG(_Level, _Msg)
Definition: logging.h:35
Definition: task_int.h:11