OpenSDN source code
ksync_sock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef ctrlplane_ksync_sock_h
7 #define ctrlplane_ksync_sock_h
8 
9 #include <atomic>
10 #include <vector>
11 #include <mutex>
12 
13 #include <boost/asio.hpp>
14 #include <boost/asio/buffer.hpp>
15 
16 #include <boost/asio/netlink_protocol.hpp>
17 #include <boost/asio/netlink_endpoint.hpp>
18 
19 #include <base/queue_task.h>
20 #include <sandesh/common/vns_constants.h>
21 #include <sandesh/common/vns_types.h>
22 #include <io/tcp_session.h>
23 #include <vr_types.h>
24 #include <nl_util.h>
25 #include "ksync_entry.h"
26 #include "ksync_tx_queue.h"
27 
28 #define KSYNC_DEFAULT_MSG_SIZE 4096
29 #define KSYNC_DEFAULT_Q_ID_SEQ 0x00000001
30 #define KSYNC_ACK_WAIT_THRESHOLD 200
31 #define KSYNC_SOCK_RECV_BUFF_SIZE (256 * 1024)
32 #define KSYNC_BMC_ARR_SIZE 1024
33 
34 class KSyncEntry;
35 class KSyncIoContext;
37 struct nl_client;
39 
40 typedef std::vector<boost::asio::mutable_buffers_1> KSyncBufferList;
41 
42 uint32_t GetNetlinkSeqno(char *data);
43 bool NetlinkMsgDone(char *data);
44 bool ValidateNetlink(char *data);
45 void GetNetlinkPayload(char *data, char **buf, uint32_t *buf_len);
46 void InitNetlink(nl_client *client);
47 void ResetNetlink(nl_client *client);
48 void UpdateNetlink(nl_client *client, uint32_t len, uint32_t seq_no);
49 void DecodeSandeshMessages(char *buf, uint32_t buf_len, SandeshContext *sandesh_context,
50  uint32_t alignment);
51 
52 /* Base class to hold sandesh context information which is passed to
53  * Sandesh decode
54  */
56 public:
58  virtual ~AgentSandeshContext() { }
59 
60  virtual void IfMsgHandler(vr_interface_req *req) = 0;
61  virtual void NHMsgHandler(vr_nexthop_req *req) = 0;
62  virtual void RouteMsgHandler(vr_route_req *req) = 0;
63  virtual void MplsMsgHandler(vr_mpls_req *req) = 0;
64  virtual int VrResponseMsgHandler(vr_response *resp) = 0;
65  virtual void MirrorMsgHandler(vr_mirror_req *req) = 0;
66  virtual void FlowMsgHandler(vr_flow_req *req) = 0;
67  virtual void FlowResponseHandler(vr_flow_response *req) { assert(0); }
68  virtual void FlowTableInfoHandler(vr_flow_table_data *r) { assert(0); }
69  virtual void BridgeTableInfoHandler(vr_bridge_table_data *r) { assert(0);}
70  virtual void VrfAssignMsgHandler(vr_vrf_assign_req *req) = 0;
71  virtual void VrfMsgHandler(vr_vrf_req *req) = 0;
72  virtual void VrfStatsMsgHandler(vr_vrf_stats_req *req) = 0;
73  virtual void DropStatsMsgHandler(vr_drop_stats_req *req) = 0;
74  virtual void VxLanMsgHandler(vr_vxlan_req *req) = 0;
75  virtual void VrouterHugePageHandler(vr_hugepage_config *req) {}
76  virtual void VrouterOpsMsgHandler(vrouter_ops *req) = 0;
77  virtual void QosConfigMsgHandler(vr_qos_map_req *req) = 0;
78  virtual void ForwardingClassMsgHandler(vr_fc_map_req *req) = 0;
79  virtual void SetErrno(int err) {errno_ = err;}
80 
81  int GetErrno() const {return errno_;}
82  void set_ksync_io_ctx(const KSyncIoContext *ioc) {ksync_io_ctx_ = ioc;}
83  const KSyncIoContext *ksync_io_ctx() const {return ksync_io_ctx_;}
84 private:
85  int errno_;
87 };
88 
89 
90 /* Base class for context management. Used while sending and
91  * receiving data via ksync socket
92  */
93 class IoContext {
94 public:
95  // Type of IoContext. The work-queue used for processing response is based
96  // on this
97  // IOC_UVE : Used for UVEs
98  // IOC_KSYNC : Used for KSync objects
99  enum Type {
102  MAX_WORK_QUEUES // This should always be last
103  };
104  // Work-queue neames used for the ksync receive work-queues
105  static const char *io_wq_names[MAX_WORK_QUEUES];
106 
108  sandesh_context_(NULL), msg_(NULL), msg_len_(0), seqno_(0),
109  type_(IOC_KSYNC), index_(0), rx_buffer1_(NULL), rx_buffer2_(NULL) {
110  }
111  IoContext(char *msg, uint32_t len, uint32_t seq, AgentSandeshContext *ctx,
112  Type type) :
113  sandesh_context_(ctx), msg_(msg), msg_len_(len), seqno_(seq),
114  type_(type), index_(0), rx_buffer1_(NULL), rx_buffer2_(NULL) {
115  }
116  IoContext(char *msg, uint32_t len, uint32_t seq, AgentSandeshContext *ctx,
117  Type type, uint32_t index) :
118  sandesh_context_(ctx), msg_(msg), msg_len_(len), seqno_(seq),
119  type_(type), index_(index), rx_buffer1_(NULL), rx_buffer2_(NULL) {
120  }
121  virtual ~IoContext() {
122  if (msg_ != NULL)
123  free(msg_);
124  assert(rx_buffer1_ == NULL);
125  assert(rx_buffer2_ == NULL);
126  }
127 
128  bool operator<(const IoContext &rhs) const {
129  return seqno_ < rhs.seqno_;
130  }
131 
132  virtual void Handler() {}
133  virtual void ErrorHandler(int err) {}
134 
136  Type type() { return type_; }
137 
138  void SetSeqno(uint32_t seqno) {seqno_ = seqno;}
139  uint32_t GetSeqno() const {return seqno_;}
140  char *GetMsg() const { return msg_; }
141  uint32_t GetMsgLen() const { return msg_len_; }
142  char *rx_buffer1() const { return rx_buffer1_; }
143  void reset_rx_buffer1() { rx_buffer1_ = NULL; }
144  char *rx_buffer2() { return rx_buffer2_; }
145  void reset_rx_buffer2() { rx_buffer2_ = NULL; }
146  uint32_t index() const { return index_; }
147 
148  boost::intrusive::list_member_hook<> node_;
149 
150 protected:
152 
153 private:
154  char *msg_;
155  uint32_t msg_len_;
156  uint32_t seqno_;
158  uint32_t index_;
159  // Buffers allocated to read the ksync responses for this IoContext.
160  // As an optimization, KSync Tx Queue will use these buffers to minimize
161  // computation in KSync Tx Queue context.
162  char *rx_buffer1_;
163  char *rx_buffer2_;
164 
165  friend class KSyncSock;
166 };
167 
168 /* IoContext tied to KSyncEntry */
169 class KSyncIoContext : public IoContext {
170 public:
171  KSyncIoContext(KSyncSock *sock, KSyncEntry *sync_entry, int msg_len,
172  char *msg, KSyncEntry::KSyncEvent event);
173  virtual ~KSyncIoContext() {
174  }
175 
176  virtual void Handler();
177 
178  void ErrorHandler(int err);
179  KSyncEntry *GetKSyncEntry() const {return entry_;}
181 private:
186 };
187 
188 /*
189  * KSync implementation of bunching messages.
190  *
191  * Message bunching has two parts,
192  * Encoding messages
193  * KSyncTxQueue is responsible to bunch KSync requests into single message
194  *
195  * KSyncTxQueue uses KSyncBulkMsgContext to bunch KSync events.
196  * The IoContext for bunched KSync events are stored as list inside
197  * KSyncBulkMessageContext
198  *
199  * KSync Events are bunched if pass following constraints,
200  * - Number of KSync events is less than max_bulk_msg_count_
201  * - Total size of buffer for events is less than max_bulk_buf_size_
202  * - Type of IoContext is same.
203  * When type of IoContext is same, it also ensures that KSync Responses
204  * are processed in same WorkQueue.
205  * - Each IoContext type has multiple work-queues. Flows are sprayed across
206  * the work-queues based on flow-table index. Entries are bunched only if
207  * they belong to same work-queue.
208  * This also ensures that flows from a flow-table partition are processed
209  * in single KSync Response Work-Queue
210  * - The input queue for KSyncTxQueue is not empty.
211  *
212  * Decoding messages
213  * KSync response are processed in context of KSyncSock::receive_work_queue_.
214  * Each IoContext type has its own set of receive work-queue.
215  *
216  * Bulk decoder works on following assumption,
217  * - Expects KSync Responses for each IoContext in KSyncBulkMsgContext
218  * - Response for each IoContext can itself be more than one Sandesh Response.
219  * - The sequence of response is same as sequence of IoContext entries
220  * - Response for each IoContext starts with VrResponseMsg followed
221  * optionally by other response. Hence, decoder moves to next IoContext
222  * after getting VrResponse message
223  *
224  * class KSyncBulkSandeshContext is used to decode the Sandesh Responses
225  * and move the IoContext on getting VrResponse
226  */
227 typedef boost::intrusive::member_hook<IoContext,
228  boost::intrusive::list_member_hook<>,
230 typedef boost::intrusive::list<IoContext, KSyncSockNode> IoContextList;
231 
233 public:
234  const static unsigned kMaxRxBufferCount = 64;
235  KSyncBulkMsgContext(IoContext::Type type, uint32_t index);
238 
239  void Insert(IoContext *ioc);
240  void Data(KSyncBufferList *iovec);
242  return io_context_type_;
243  }
244  void AddReceiveBuffer(char *buff);
245  char *GetReceiveBuffer();
246  uint32_t work_queue_index() const { return work_queue_index_; }
247  void set_seqno(uint32_t seq) { seqno_ = seq; }
248  uint32_t seqno() { return seqno_; }
249 private:
251  // List of IoContext to be processed in this context
253  // Type of message
255  // Index of work-queue
257  // List of rx-buffers
258  // The buffers are taken from IoContext added to the list above
259  // If IoContext does not have buffer, then it dyamically allocates
260  // buffer
262  // Index of next buffer to process
264 
266  // Following fields are used for decode processing
268  // Number of responses already processed
270  // Iterator to IoContext being processed
271  IoContextList::iterator io_context_list_it_;
272  uint32_t seqno_;
273 };
274 
276 public:
278  virtual ~KSyncBulkSandeshContext();
279 
280  void IfMsgHandler(vr_interface_req *req);
281  void NHMsgHandler(vr_nexthop_req *req);
282  void RouteMsgHandler(vr_route_req *req);
283  void MplsMsgHandler(vr_mpls_req *req);
284  int VrResponseMsgHandler(vr_response *resp);
285  void MirrorMsgHandler(vr_mirror_req *req);
286  void FlowMsgHandler(vr_flow_req *req);
287  void FlowResponseHandler(vr_flow_response *req);
288  void VrfAssignMsgHandler(vr_vrf_assign_req *req);
289  void VrfMsgHandler(vr_vrf_req *req);
290  void VrfStatsMsgHandler(vr_vrf_stats_req *req);
291  void DropStatsMsgHandler(vr_drop_stats_req *req);
292  void VxLanMsgHandler(vr_vxlan_req *req);
293  void VrouterOpsMsgHandler(vrouter_ops *req);
294  void QosConfigMsgHandler(vr_qos_map_req *req);
295  void ForwardingClassMsgHandler(vr_fc_map_req *req);
296  void SetErrno(int err);
297 
298  bool Decoder(char *buff, uint32_t buff_len, uint32_t alignment, bool more);
301  bulk_msg_context_ = bulk_context;
302  }
303 
304  void IoContextStart();
305  void IoContextDone();
306 
307 private:
310 };
311 
312 class KSyncSock {
313 public:
314  // Number of flow receive queues
315  const static int kRxWorkQueueCount = 2;
316  const static int kMsgGrowSize = 16;
317  const static unsigned kBufLen = (4*1024);
318 
319  // Number of messages that can be bunched together
320  const static unsigned kMaxBulkMsgCount = 16;
321  // Sequence number to denote invalid builk-context
322  const static unsigned kInvalidBulkSeqNo = 0xFFFFFFFF;
323 
324  typedef std::map<uint32_t, KSyncBulkMsgContext> WaitTree;
325  typedef std::pair<uint32_t, KSyncBulkMsgContext> WaitTreePair;
326  typedef boost::function<void(const boost::system::error_code &, size_t)>
328 
329  // Request structure in the KSync Response Queue
330  struct KSyncRxData {
331  // buffer having KSync response
332  char *buff_;
333  // bulk context for decoding response
335 
336  KSyncRxData() : buff_(NULL), bulk_msg_context_(NULL) { }
337  KSyncRxData(const KSyncRxData &rhs) :
339  }
340  KSyncRxData(char *buff, KSyncBulkMsgContext *ctxt) :
341  buff_(buff), bulk_msg_context_(ctxt) {
342  }
343  };
345  // structure for ksyncrprocess Rx queue
351  entry_(entry), event_(event) {
352  }
353  };
355 
356  KSyncSock();
357  virtual ~KSyncSock();
358 
359  // Virtual methods
360  virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt) = 0;
361  virtual bool Decoder(char *data, AgentSandeshContext *ctxt) = 0;
362 
363  // Write a KSyncEntry to kernel
364  void SendAsync(KSyncEntry *entry, int msg_len, char *msg,
365  KSyncEntry::KSyncEvent event);
366  std::size_t BlockingSend(char *msg, int msg_len);
367  bool BlockingRecv();
368  void GenericSend(IoContext *ctx);
369  uint32_t AllocSeqNo(IoContext::Type type);
370  uint32_t AllocSeqNo(IoContext::Type type, uint32_t instance);
372  KSyncReceiveQueue *GetReceiveQueue(uint32_t seqno);
373 
374  // Bulk Messaging methods
375  KSyncBulkMsgContext *LocateBulkContext(uint32_t seqno,
376  IoContext::Type io_context_type,
377  uint32_t work_queue_index);
378  int SendBulkMessage(KSyncBulkMsgContext *bulk_context, uint32_t seqno);
379  bool TryAddToBulk(KSyncBulkMsgContext *bulk_context, IoContext *ioc);
380  void OnEmptyQueue(bool done);
381  int tx_count() const { return tx_count_; }
382 
383  // Start Ksync Asio operations
384  static void Start(bool read_inline);
385  static void Shutdown();
386 
387  // Partition to KSyncSock mapping
388  static KSyncSock *Get(DBTablePartBase *partition);
389  static KSyncSock *Get(int partition_id);
390 
391  static uint32_t GetPid() {return pid_;};
393  static void SetNetlinkFamilyId(int id);
394 
397  }
398  static void SetAgentSandeshContext(AgentSandeshContext *ctx, uint32_t idx) {
399  agent_sandesh_ctx_[idx] = ctx;
400  }
401 
402  const KSyncTxQueue *send_queue() const { return &send_queue_; }
403  const KSyncReceiveQueue *get_receive_work_queue(uint16_t index) const {
404  return ksync_rx_queue[index];
405  }
406  // Allocate a recieve work-queue
407 
409  uint32_t task_id, uint32_t instance,
410  const char *name);
411 
412  uint32_t WaitTreeSize() const;
413  void SetSeqno(uint32_t seq);
414  void SetMeasureQueueDelay(bool val);
417  // API to enqueue ksync events to rx process work queue
419 protected:
420  static void Init(bool use_work_queue, const std::string &cpu_pin_policy);
421  static void SetSockTableEntry(KSyncSock *sock);
422  bool ValidateAndEnqueue(char *data, KSyncBulkMsgContext *context);
424  void ProcessDataInline(char *data);
425 
426  std::mutex mutex_;
427  nl_client *nl_client_;
428  // Tree of all IoContext pending ksync response
433 
434  // Information maintained for bulk processing
435 
436  // Max messages in one bulk context
438  // Max buffer size in one bulk context
440 
441  // Sequence number of first message in bulk context. Entry in WaitTree is
442  // added based on this sequence number
443  uint32_t bulk_seq_no_;
444  // Current buffer size in bulk context
445  uint32_t bulk_buf_size_;
446  // Current message count in bulk context
447  uint32_t bulk_msg_count_;
448 
449  uint32_t bmca_prod_;
450  uint32_t bmca_cons_;
452 
453 private:
454  friend class KSyncTxQueue;
455  virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb) = 0;
456  virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
457  HandlerCb cb) = 0;
458  virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no) = 0;
459  virtual void Receive(boost::asio::mutable_buffers_1) = 0;
460  virtual uint32_t GetSeqno(char *data) = 0;
461  virtual bool IsMoreData(char *data) = 0;
462  virtual bool Validate(char *data) = 0;
463 
464  // Read handler registered with boost::asio. Demux done based on seqno_
465  void ReadHandler(const boost::system::error_code& error,
466  size_t bytes_transferred);
467 
468  // Write handler registered with boost::asio. Demux done based on seqno_
469  void WriteHandler(const boost::system::error_code& error,
470  size_t bytes_transferred);
471 
472  bool ProcessKernelData(KSyncBulkSandeshContext *ksync_context,
473  const KSyncRxData &data);
474  bool ProcessRxData(KSyncRxQueueData data);
475  bool SendAsyncImpl(IoContext *ioc);
476  bool SendAsyncStart() {
477  std::scoped_lock lock(mutex_);
478  return (wait_tree_.size() <= KSYNC_ACK_WAIT_THRESHOLD);
479  }
480 
481 private:
482  char *rx_buff_;
483  std::atomic<uint32_t> seqno_;
484  std::atomic<uint32_t> uve_seqno_;
485  // Read ksync responses inline
486  // The IoContext WaitTree is not used when response is read-inline
493 
494  // Debug stats
498 
499  // IO context can defer ksync event processing
500  // by defering them to this work queue, this queue gets
501  // processed in Agent::KSync context
503  static std::unique_ptr<KSyncSock> sock_;
504  static pid_t pid_;
506  // AgentSandeshContext used for KSync response handling
507  // AgentSandeshContext used for decode is picked based on work-queue index
508  // Picking AgentSandeshContext based on work-queue index also makes it
509  // thread safe
511  static std::atomic<bool> shutdown_;
512 
514 };
515 
516 //netlink socket class for interacting with kernel
517 class KSyncSockNetlink : public KSyncSock {
518 public:
519  KSyncSockNetlink(boost::asio::io_context &ios, int protocol);
520  virtual ~KSyncSockNetlink();
521 
522  virtual uint32_t GetSeqno(char *data);
523  virtual bool IsMoreData(char *data);
524  virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt);
525  virtual bool Decoder(char *data, AgentSandeshContext *ctxt);
526  virtual bool Validate(char *data);
527  virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb);
528  virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
529  HandlerCb cb);
530  virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no);
531  virtual void Receive(boost::asio::mutable_buffers_1);
532 
533  static void NetlinkDecoder(char *data, SandeshContext *ctxt);
534  static void NetlinkBulkDecoder(char *data, SandeshContext *ctxt, bool more);
535  static void Init(boost::asio::io_context &ios, int protocol, bool use_work_queue,
536  const std::string &cpu_pin_policy);
537 private:
538  boost::asio::netlink::raw::socket sock_;
539 };
540 
541 //udp socket class for interacting with user vrouter
542 class KSyncSockUdp : public KSyncSock {
543 public:
544  KSyncSockUdp(boost::asio::io_context &ios, int port);
545  virtual ~KSyncSockUdp() { }
546 
547  virtual uint32_t GetSeqno(char *data);
548  virtual bool IsMoreData(char *data);
549  virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt);
550  virtual bool Decoder(char *data, AgentSandeshContext *ctxt);
551  virtual bool Validate(char *data);
552  virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb);
553  virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
554  HandlerCb cb);
555  virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no);
556  virtual void Receive(boost::asio::mutable_buffers_1);
557 
558  static void Init(boost::asio::io_context &ios, int port,
559  const std::string &cpu_pin_policy);
560 private:
561  boost::asio::ip::udp::socket sock_;
562  boost::asio::ip::udp::endpoint server_ep_;
563 };
564 
565 //Unix domain socket class for interacting with user vrouter
566 #define KSYNC_AGENT_VROUTER_SOCK_PATH "/var/run/vrouter/dpdk_netlink"
567 class KSyncSockUds : public KSyncSock {
568 public:
569  KSyncSockUds(boost::asio::io_context &ios);
570  virtual ~KSyncSockUds() {
571  delete rx_buff_;
572  delete rx_buff_q_;
573  }
574 
575  virtual uint32_t GetSeqno(char *data);
576  virtual bool IsMoreData(char *data);
577  virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt);
578  virtual bool Decoder(char *data, AgentSandeshContext *ctxt);
579  virtual bool Validate(char *data);
580  virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb);
581  virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
582  HandlerCb cb);
583  virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no);
584  virtual void Receive(boost::asio::mutable_buffers_1);
585  virtual bool Run(void);
586 
587  static void Init(boost::asio::io_context &ios,
588  const std::string &cpu_pin_policy,
589  const std::string &sockpathvr="");
590 private:
591  boost::asio::local::stream_protocol::socket sock_;
592  boost::asio::local::stream_protocol::endpoint server_ep_;
593  char *rx_buff_;
594  char *rx_buff_q_;
595  size_t remain_;
596  int socket_;
598  static string sockpath_;
599 };
600 
602 public:
605 
606 protected:
607  virtual int MsgLength(Buffer buffer, int offset);
608 
609  virtual const int GetHeaderLenSize() {
610  return sizeof(struct nlmsghdr);
611  }
612 
613  virtual const int GetMaxMessageSize() {
614  return kMaxMessageSize;
615  }
616 
617 private:
618  static const int kMaxMessageSize = 4096;
619 };
620 
622 public:
624  bool async_ready = false);
625  virtual ~KSyncSockTcpSession();
626 protected:
627  virtual void OnRead(Buffer buffer);
628 private:
630 };
631 
632 class KSyncSockTcp : public KSyncSock, public TcpServer {
633 public:
634  KSyncSockTcp(EventManager *evm, boost::asio::ip::address ip_addr,
635  int port);
636  virtual ~KSyncSockTcp() { }
637 
638  virtual uint32_t GetSeqno(char *data);
639  virtual bool IsMoreData(char *data);
640  virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt);
641  virtual bool Decoder(char *data, AgentSandeshContext *ctxt);
642  virtual bool Validate(char *data);
643  virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb);
644  virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no,
645  HandlerCb cb);
646  virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no);
647  virtual void Receive(boost::asio::mutable_buffers_1);
648  virtual TcpSession *AllocSession(Socket *socket);
649  virtual bool Run(void);
650 
651  bool ReceiveMsg(const u_int8_t *msg, size_t size);
652  void OnSessionEvent(TcpSession *session, TcpSession::Event event);
653  bool connect_complete() const {
654  return connect_complete_;
655  }
656  void AsyncReadStart();
657 
658  static void Init(EventManager *evm,
659  boost::asio::ip::address ip_addr, int port,
660  const std::string &cpu_pin_policy);
661 private:
664  boost::asio::ip::tcp::endpoint server_ep_;
665  boost::asio::ip::tcp::socket *tcp_socket_;
667  char *rx_buff_;
669  size_t remain_;
670 };
671 #endif // ctrlplane_ksync_sock_h
void set_ksync_io_ctx(const KSyncIoContext *ioc)
Definition: ksync_sock.h:82
virtual void MplsMsgHandler(vr_mpls_req *req)=0
virtual void VrfAssignMsgHandler(vr_vrf_assign_req *req)=0
int GetErrno() const
Definition: ksync_sock.h:81
virtual void VrfMsgHandler(vr_vrf_req *req)=0
virtual ~AgentSandeshContext()
Definition: ksync_sock.h:58
virtual void MirrorMsgHandler(vr_mirror_req *req)=0
virtual void FlowTableInfoHandler(vr_flow_table_data *r)
Definition: ksync_sock.h:68
const KSyncIoContext * ksync_io_ctx_
Definition: ksync_sock.h:86
virtual void NHMsgHandler(vr_nexthop_req *req)=0
const KSyncIoContext * ksync_io_ctx() const
Definition: ksync_sock.h:83
virtual void QosConfigMsgHandler(vr_qos_map_req *req)=0
virtual void BridgeTableInfoHandler(vr_bridge_table_data *r)
Definition: ksync_sock.h:69
virtual void VrouterOpsMsgHandler(vrouter_ops *req)=0
virtual void VrouterHugePageHandler(vr_hugepage_config *req)
Definition: ksync_sock.h:75
virtual void SetErrno(int err)
Definition: ksync_sock.h:79
virtual void FlowResponseHandler(vr_flow_response *req)
Definition: ksync_sock.h:67
virtual void IfMsgHandler(vr_interface_req *req)=0
virtual void FlowMsgHandler(vr_flow_req *req)=0
virtual void ForwardingClassMsgHandler(vr_fc_map_req *req)=0
virtual void RouteMsgHandler(vr_route_req *req)=0
virtual void DropStatsMsgHandler(vr_drop_stats_req *req)=0
virtual void VrfStatsMsgHandler(vr_vrf_stats_req *req)=0
virtual void VxLanMsgHandler(vr_vxlan_req *req)=0
virtual int VrResponseMsgHandler(vr_response *resp)=0
uint32_t seqno_
Definition: ksync_sock.h:156
AgentSandeshContext * GetSandeshContext()
Definition: ksync_sock.h:135
char * msg_
Definition: ksync_sock.h:154
char * rx_buffer1() const
Definition: ksync_sock.h:142
IoContext(char *msg, uint32_t len, uint32_t seq, AgentSandeshContext *ctx, Type type)
Definition: ksync_sock.h:111
AgentSandeshContext * sandesh_context_
Definition: ksync_sock.h:151
virtual void ErrorHandler(int err)
Definition: ksync_sock.h:133
uint32_t index() const
Definition: ksync_sock.h:146
uint32_t msg_len_
Definition: ksync_sock.h:155
char * rx_buffer1_
Definition: ksync_sock.h:162
Type type_
Definition: ksync_sock.h:157
static const char * io_wq_names[MAX_WORK_QUEUES]
Definition: ksync_sock.h:105
virtual void Handler()
Definition: ksync_sock.h:132
uint32_t index_
Definition: ksync_sock.h:158
@ MAX_WORK_QUEUES
Definition: ksync_sock.h:102
void reset_rx_buffer2()
Definition: ksync_sock.h:145
uint32_t GetSeqno() const
Definition: ksync_sock.h:139
uint32_t GetMsgLen() const
Definition: ksync_sock.h:141
char * rx_buffer2()
Definition: ksync_sock.h:144
char * GetMsg() const
Definition: ksync_sock.h:140
IoContext(char *msg, uint32_t len, uint32_t seq, AgentSandeshContext *ctx, Type type, uint32_t index)
Definition: ksync_sock.h:116
void reset_rx_buffer1()
Definition: ksync_sock.h:143
Type type()
Definition: ksync_sock.h:136
virtual ~IoContext()
Definition: ksync_sock.h:121
void SetSeqno(uint32_t seqno)
Definition: ksync_sock.h:138
char * rx_buffer2_
Definition: ksync_sock.h:163
boost::intrusive::list_member_hook node_
Definition: ksync_sock.h:148
bool operator<(const IoContext &rhs) const
Definition: ksync_sock.h:128
void set_seqno(uint32_t seq)
Definition: ksync_sock.h:247
uint32_t rx_buffer_index_
Definition: ksync_sock.h:263
uint32_t work_queue_index_
Definition: ksync_sock.h:256
KSyncBulkMsgContext(IoContext::Type type, uint32_t index)
Definition: ksync_sock.cc:1054
void Insert(IoContext *ioc)
Definition: ksync_sock.cc:1093
void Data(KSyncBufferList *iovec)
Definition: ksync_sock.cc:1098
char * GetReceiveBuffer()
Definition: ksync_sock.cc:1081
IoContext::Type io_context_type_
Definition: ksync_sock.h:254
char * rx_buffers_[kMaxRxBufferCount]
Definition: ksync_sock.h:261
uint32_t vr_response_count_
Definition: ksync_sock.h:269
static const unsigned kMaxRxBufferCount
Definition: ksync_sock.h:234
IoContextList::iterator io_context_list_it_
Definition: ksync_sock.h:271
IoContextList io_context_list_
Definition: ksync_sock.h:252
IoContext::Type io_context_type() const
Definition: ksync_sock.h:241
uint32_t work_queue_index() const
Definition: ksync_sock.h:246
uint32_t seqno()
Definition: ksync_sock.h:248
void AddReceiveBuffer(char *buff)
Definition: ksync_sock.cc:1088
void set_bulk_message_context(KSyncBulkMsgContext *bulk_context)
Definition: ksync_sock.h:300
void VxLanMsgHandler(vr_vxlan_req *req)
Definition: ksync_sock.cc:1041
void IfMsgHandler(vr_interface_req *req)
Definition: ksync_sock.cc:949
void DropStatsMsgHandler(vr_drop_stats_req *req)
Definition: ksync_sock.cc:1036
bool Decoder(char *buff, uint32_t buff_len, uint32_t alignment, bool more)
Definition: ksync_sock.cc:921
void ForwardingClassMsgHandler(vr_fc_map_req *req)
Definition: ksync_sock.cc:974
void VrfAssignMsgHandler(vr_vrf_assign_req *req)
Definition: ksync_sock.cc:1021
void NHMsgHandler(vr_nexthop_req *req)
Definition: ksync_sock.cc:954
KSyncBulkMsgContext * bulk_msg_context_
Definition: ksync_sock.h:308
void FlowResponseHandler(vr_flow_response *req)
Definition: ksync_sock.cc:1016
virtual ~KSyncBulkSandeshContext()
Definition: ksync_sock.cc:894
DISALLOW_COPY_AND_ASSIGN(KSyncBulkSandeshContext)
void VrfStatsMsgHandler(vr_vrf_stats_req *req)
Definition: ksync_sock.cc:1031
void VrouterOpsMsgHandler(vrouter_ops *req)
Definition: ksync_sock.cc:1046
void RouteMsgHandler(vr_route_req *req)
Definition: ksync_sock.cc:959
void QosConfigMsgHandler(vr_qos_map_req *req)
Definition: ksync_sock.cc:969
void SetErrno(int err)
Definition: ksync_sock.cc:939
int VrResponseMsgHandler(vr_response *resp)
Definition: ksync_sock.cc:981
void MplsMsgHandler(vr_mpls_req *req)
Definition: ksync_sock.cc:964
AgentSandeshContext * GetSandeshContext()
Definition: ksync_sock.cc:944
void VrfMsgHandler(vr_vrf_req *req)
Definition: ksync_sock.cc:1026
void MirrorMsgHandler(vr_mirror_req *req)
Definition: ksync_sock.cc:1006
void FlowMsgHandler(vr_flow_req *req)
Definition: ksync_sock.cc:1011
KSyncEntry * entry_
Definition: ksync_sock.h:182
void ErrorHandler(int err)
Definition: ksync_sock.cc:884
KSyncEntry::KSyncEvent event() const
Definition: ksync_sock.h:180
AgentSandeshContext * agent_sandesh_ctx_
Definition: ksync_sock.h:184
KSyncSock * sock_
Definition: ksync_sock.h:185
virtual ~KSyncIoContext()
Definition: ksync_sock.h:173
KSyncIoContext(KSyncSock *sock, KSyncEntry *sync_entry, int msg_len, char *msg, KSyncEntry::KSyncEvent event)
Definition: ksync_sock.cc:870
virtual void Handler()
Definition: ksync_sock.cc:880
KSyncEntry::KSyncEvent event_
Definition: ksync_sock.h:183
KSyncEntry * GetKSyncEntry() const
Definition: ksync_sock.h:179
virtual ~KSyncSockTcpSessionReader()
Definition: ksync_sock.h:604
virtual const int GetMaxMessageSize()
Definition: ksync_sock.h:613
static const int kMaxMessageSize
Definition: ksync_sock.h:618
virtual const int GetHeaderLenSize()
Definition: ksync_sock.h:609
virtual int MsgLength(Buffer buffer, int offset)
KSyncSockTcpSessionReader(TcpSession *session, ReceiveCallback callback)
KSyncSockTcpSessionReader * reader_
Definition: ksync_sock.h:629
virtual void OnRead(Buffer buffer)
KSyncSockTcpSession(TcpServer *server, Socket *sock, bool async_ready=false)
virtual ~KSyncSockTcpSession()
virtual bool IsMoreData(char *data)
bool connect_complete() const
Definition: ksync_sock.h:653
boost::asio::ip::tcp::endpoint server_ep_
Definition: ksync_sock.h:664
EventManager * evm_
Definition: ksync_sock.h:662
virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb)
TcpSession * session_
Definition: ksync_sock.h:663
virtual void Receive(boost::asio::mutable_buffers_1)
virtual uint32_t GetSeqno(char *data)
void AsyncReadStart()
bool ReceiveMsg(const u_int8_t *msg, size_t size)
KSyncSockTcp(EventManager *evm, boost::asio::ip::address ip_addr, int port)
virtual bool Run(void)
boost::asio::ip::tcp::socket * tcp_socket_
Definition: ksync_sock.h:665
size_t remain_
Definition: ksync_sock.h:669
virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no)
void OnSessionEvent(TcpSession *session, TcpSession::Event event)
char * rx_buff_rem_
Definition: ksync_sock.h:668
virtual bool Validate(char *data)
virtual ~KSyncSockTcp()
Definition: ksync_sock.h:636
virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt)
bool connect_complete_
Definition: ksync_sock.h:666
char * rx_buff_
Definition: ksync_sock.h:667
virtual TcpSession * AllocSession(Socket *socket)
virtual bool Decoder(char *data, AgentSandeshContext *ctxt)
static void Init(EventManager *evm, boost::asio::ip::address ip_addr, int port, const std::string &cpu_pin_policy)
virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no, HandlerCb cb)
virtual void Receive(boost::asio::mutable_buffers_1)
Definition: ksync_sock.cc:835
virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb)
Definition: ksync_sock.cc:830
static void Init(boost::asio::io_context &ios, int port, const std::string &cpu_pin_policy)
Definition: ksync_sock.cc:771
virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt)
Definition: ksync_sock.cc:793
KSyncSockUdp(boost::asio::io_context &ios, int port)
Definition: ksync_sock.cc:766
virtual bool IsMoreData(char *data)
Definition: ksync_sock.cc:782
boost::asio::ip::udp::endpoint server_ep_
Definition: ksync_sock.h:562
virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no, HandlerCb cb)
Definition: ksync_sock.cc:801
virtual bool Validate(char *data)
Definition: ksync_sock.cc:826
boost::asio::ip::udp::socket sock_
Definition: ksync_sock.h:561
virtual ~KSyncSockUdp()
Definition: ksync_sock.h:545
virtual uint32_t GetSeqno(char *data)
Definition: ksync_sock.cc:777
virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no)
Definition: ksync_sock.cc:814
virtual bool Decoder(char *data, AgentSandeshContext *ctxt)
Definition: ksync_sock.cc:788
virtual bool IsMoreData(char *data)
static string sockpath_
Definition: ksync_sock.h:598
char * rx_buff_
Definition: ksync_sock.h:593
virtual ~KSyncSockUds()
Definition: ksync_sock.h:570
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)
WorkQueue< KSyncRxData > KSyncReceiveQueue
Definition: ksync_sock.h:344
static std::unique_ptr< KSyncSock > sock_
Definition: ksync_sock.h:503
static AgentSandeshContext * agent_sandesh_ctx_[kRxWorkQueueCount]
Definition: ksync_sock.h:510
uint32_t bmca_prod_
Definition: ksync_sock.h:449
static AgentSandeshContext * GetAgentSandeshContext(uint32_t type)
Definition: ksync_sock.h:395
static pid_t pid_
Definition: ksync_sock.h:504
static const unsigned kInvalidBulkSeqNo
Definition: ksync_sock.h:322
std::size_t BlockingSend(char *msg, int msg_len)
Definition: ksync_sock.cc:450
KSyncReceiveQueue * ksync_rx_queue[kRxWorkQueueCount]
Definition: ksync_sock.h:432
bool ProcessKernelData(KSyncBulkSandeshContext *ksync_context, const KSyncRxData &data)
Definition: ksync_sock.cc:392
void reset_use_wait_tree()
Definition: ksync_sock.h:415
DISALLOW_COPY_AND_ASSIGN(KSyncSock)
static uint32_t GetPid()
Definition: ksync_sock.h:391
virtual bool Validate(char *data)=0
void EnqueueRxProcessData(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
Definition: ksync_sock.cc:328
KSyncBulkSandeshContext ksync_bulk_sandesh_context_[kRxWorkQueueCount]
Definition: ksync_sock.h:491
void set_process_data_inline()
Definition: ksync_sock.h:416
std::atomic< uint32_t > seqno_
Definition: ksync_sock.h:483
virtual uint32_t GetSeqno(char *data)=0
std::mutex mutex_
Definition: ksync_sock.h:426
virtual void Receive(boost::asio::mutable_buffers_1)=0
std::pair< uint32_t, KSyncBulkMsgContext > WaitTreePair
Definition: ksync_sock.h:325
uint32_t bulk_seq_no_
Definition: ksync_sock.h:443
std::atomic< uint32_t > uve_seqno_
Definition: ksync_sock.h:484
KSyncBulkMsgContext * LocateBulkContext(uint32_t seqno, IoContext::Type io_context_type, uint32_t work_queue_index)
Definition: ksync_sock.cc:550
void OnEmptyQueue(bool done)
Definition: ksync_sock.cc:487
uint32_t max_bulk_buf_size_
Definition: ksync_sock.h:439
const KSyncReceiveQueue * get_receive_work_queue(uint16_t index) const
Definition: ksync_sock.h:403
virtual bool Decoder(char *data, AgentSandeshContext *ctxt)=0
static void Init(bool use_work_queue, const std::string &cpu_pin_policy)
Definition: ksync_sock.cc:232
virtual void AsyncSendTo(KSyncBufferList *iovec, uint32_t seq_no, HandlerCb cb)=0
void ProcessDataInline(char *data)
Definition: ksync_sock.cc:840
bool read_inline_
Definition: ksync_sock.h:487
static void Shutdown()
Definition: ksync_sock.cc:226
void ReadHandler(const boost::system::error_code &error, size_t bytes_transferred)
Definition: ksync_sock.cc:370
bool ValidateAndEnqueue(char *data, KSyncBulkMsgContext *context)
Definition: ksync_sock.cc:355
void GenericSend(IoContext *ctx)
Definition: ksync_sock.cc:457
int SendBulkMessage(KSyncBulkMsgContext *bulk_context, uint32_t seqno)
Definition: ksync_sock.cc:509
bool SendAsyncImpl(IoContext *ioc)
Definition: ksync_sock.cc:628
static void Start(bool read_inline)
Definition: ksync_sock.cc:258
uint32_t WaitTreeSize() const
Definition: ksync_sock.cc:280
void SendAsync(KSyncEntry *entry, int msg_len, char *msg, KSyncEntry::KSyncEvent event)
Definition: ksync_sock.cc:461
uint32_t bulk_buf_size_
Definition: ksync_sock.h:445
WorkQueue< KSyncRxQueueData > KSyncRxWorkQueue
Definition: ksync_sock.h:354
static const int kRxWorkQueueCount
Definition: ksync_sock.h:315
virtual ~KSyncSock()
Definition: ksync_sock.cc:204
KSyncReceiveQueue * AllocQueue(KSyncBulkSandeshContext ctxt[], uint32_t task_id, uint32_t instance, const char *name)
Definition: ksync_sock.cc:239
bool BlockingRecv()
Definition: ksync_sock.cc:425
static void SetSockTableEntry(KSyncSock *sock)
Definition: ksync_sock.cc:270
KSyncBulkSandeshContext uve_bulk_sandesh_context_[kRxWorkQueueCount]
Definition: ksync_sock.h:492
KSyncRxWorkQueue rx_process_queue_
Definition: ksync_sock.h:502
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 void SetAgentSandeshContext(AgentSandeshContext *ctx, uint32_t idx)
Definition: ksync_sock.h:398
static const unsigned kBufLen
Definition: ksync_sock.h:317
void WriteHandler(const boost::system::error_code &error, size_t bytes_transferred)
Definition: ksync_sock.cc:475
void SetMeasureQueueDelay(bool val)
Definition: ksync_sock.cc:251
bool TryAddToBulk(KSyncBulkMsgContext *bulk_context, IoContext *ioc)
Definition: ksync_sock.cc:599
char * rx_buff_
Definition: ksync_sock.h:482
KSyncReceiveQueue * GetReceiveQueue(IoContext::Type type, uint32_t instance)
Definition: ksync_sock.cc:309
static const int kMsgGrowSize
Definition: ksync_sock.h:316
virtual std::size_t SendTo(KSyncBufferList *iovec, uint32_t seq_no)=0
uint32_t AllocSeqNo(IoContext::Type type)
Definition: ksync_sock.cc:305
WaitTree wait_tree_
Definition: ksync_sock.h:429
std::map< uint32_t, KSyncBulkMsgContext > WaitTree
Definition: ksync_sock.h:324
int tx_count() const
Definition: ksync_sock.h:381
uint32_t bulk_msg_count_
Definition: ksync_sock.h:447
nl_client * nl_client_
Definition: ksync_sock.h:427
void SetSeqno(uint32_t seq)
Definition: ksync_sock.cc:284
KSyncTxQueue send_queue_
Definition: ksync_sock.h:430
static KSyncSock * Get(DBTablePartBase *partition)
Definition: ksync_sock.cc:346
bool use_wait_tree_
Definition: ksync_sock.h:489
virtual bool BulkDecoder(char *data, KSyncBulkSandeshContext *ctxt)=0
KSyncReceiveQueue * uve_rx_queue[kRxWorkQueueCount]
Definition: ksync_sock.h:431
virtual void AsyncReceive(boost::asio::mutable_buffers_1, HandlerCb)=0
uint32_t max_bulk_msg_count_
Definition: ksync_sock.h:437
KSyncBulkSandeshContext * GetBulkSandeshContext(uint32_t seqno)
Definition: ksync_sock.cc:337
static const unsigned kMaxBulkMsgCount
Definition: ksync_sock.h:320
const KSyncTxQueue * send_queue() const
Definition: ksync_sock.h:402
int err_count_
Definition: ksync_sock.h:497
bool SendAsyncStart()
Definition: ksync_sock.h:476
KSyncBulkMsgContext * bulk_mctx_arr_[KSYNC_BMC_ARR_SIZE]
Definition: ksync_sock.h:451
static int vnsw_netlink_family_id_
Definition: ksync_sock.h:505
int tx_count_
Definition: ksync_sock.h:495
KSyncBulkMsgContext * bulk_msg_context_
Definition: ksync_sock.h:488
bool ProcessRxData(KSyncRxQueueData data)
Definition: ksync_sock.cc:331
uint32_t bmca_cons_
Definition: ksync_sock.h:450
virtual bool IsMoreData(char *data)=0
bool process_data_inline_
Definition: ksync_sock.h:490
int ack_count_
Definition: ksync_sock.h:496
static std::atomic< bool > shutdown_
Definition: ksync_sock.h:511
static int GetNetlinkFamilyId()
Definition: ksync_sock.h:392
boost::function< bool(const uint8_t *, size_t)> ReceiveCallback
Definition: tcp_session.h:308
boost::asio::const_buffer Buffer
Definition: tcp_session.h:307
boost::asio::ip::tcp::socket Socket
Definition: tcp_server.h:32
TcpServer * server()
Definition: tcp_session.h:85
boost::asio::const_buffer Buffer
Definition: tcp_session.h:61
boost::asio::ip::tcp::socket Socket
Definition: tcp_session.h:57
static EventManager evm
@ INVALID
Definition: globals.h:150
bool NetlinkMsgDone(char *data)
Definition: ksync_sock.cc:68
bool ValidateNetlink(char *data)
Definition: ksync_sock.cc:74
boost::intrusive::list< IoContext, KSyncSockNode > IoContextList
Definition: ksync_sock.h:230
std::vector< boost::asio::mutable_buffers_1 > KSyncBufferList
Definition: ksync_sock.h:38
void GetNetlinkPayload(char *data, char **buf, uint32_t *buf_len)
Definition: ksync_sock.cc:119
boost::intrusive::member_hook< IoContext, boost::intrusive::list_member_hook<>, &IoContext::node_ > KSyncSockNode
Definition: ksync_sock.h:229
void UpdateNetlink(nl_client *client, uint32_t len, uint32_t seq_no)
Definition: ksync_sock.cc:146
#define KSYNC_BMC_ARR_SIZE
Definition: ksync_sock.h:32
void InitNetlink(nl_client *client)
Definition: ksync_sock.cc:132
#define KSYNC_ACK_WAIT_THRESHOLD
Definition: ksync_sock.h:30
uint32_t GetNetlinkSeqno(char *data)
Definition: ksync_sock.cc:63
void ResetNetlink(nl_client *client)
Definition: ksync_sock.cc:139
void DecodeSandeshMessages(char *buf, uint32_t buf_len, SandeshContext *sandesh_context, uint32_t alignment)
Definition: ksync_sock.cc:153
uint8_t type
Definition: load_balance.h:2
KSyncBulkMsgContext * bulk_msg_context_
Definition: ksync_sock.h:334
KSyncRxData(char *buff, KSyncBulkMsgContext *ctxt)
Definition: ksync_sock.h:340
KSyncRxData(const KSyncRxData &rhs)
Definition: ksync_sock.h:337
KSyncRxQueueData(KSyncEntry *entry, KSyncEntry::KSyncEvent event)
Definition: ksync_sock.h:350
KSyncEntry::KSyncEvent event_
Definition: ksync_sock.h:348