1    	#include <poll.h>
2    	
3    	#include "msg/async/net_handler.h"
4    	#include "RDMAStack.h"
5    	
6    	#define dout_subsys ceph_subsys_ms
7    	#undef dout_prefix
8    	#define dout_prefix *_dout << " RDMAIWARPServerSocketImpl "
9    	
10   	RDMAIWARPServerSocketImpl::RDMAIWARPServerSocketImpl(
11   	  CephContext *cct, shared_ptr<Infiniband>& ib,
12   	  shared_ptr<RDMADispatcher>& rdma_dispatcher, RDMAWorker *w,
13   	  entity_addr_t& a, unsigned addr_slot)
14   	  : RDMAServerSocketImpl(cct, ib, rdma_dispatcher, w, a, addr_slot)
15   	{
16   	}
17   	
18   	int RDMAIWARPServerSocketImpl::listen(entity_addr_t &sa,
19   					      const SocketOptions &opt)
20   	{
21   	  ldout(cct, 20) << __func__ << " bind to rdma point" << dendl;
22   	  cm_channel = rdma_create_event_channel();
23   	  rdma_create_id(cm_channel, &cm_id, NULL, RDMA_PS_TCP);
24   	  ldout(cct, 20) << __func__ << " successfully created cm id: " << cm_id << dendl;
25   	  int rc = rdma_bind_addr(cm_id, const_cast<struct sockaddr*>(sa.get_sockaddr()));
26   	  if (rc < 0) {
27   	    rc = -errno;
28   	    ldout(cct, 10) << __func__ << " unable to bind to " << sa.get_sockaddr()
29   	                   << " on port " << sa.get_port() << ": " << cpp_strerror(errno) << dendl;
30   	    goto err;
31   	  }
32   	  rc = rdma_listen(cm_id, 128);
33   	  if (rc < 0) {
34   	    rc = -errno;
35   	    ldout(cct, 10) << __func__ << " unable to listen to " << sa.get_sockaddr()
36   	                   << " on port " << sa.get_port() << ": " << cpp_strerror(errno) << dendl;
37   	    goto err;
38   	  }
39   	  server_setup_socket = cm_channel->fd;
40   	  rc = net.set_nonblock(server_setup_socket);
41   	  if (rc < 0) {
42   	    goto err;
43   	  }
44   	  ldout(cct, 20) << __func__ << " fd of cm_channel is " << server_setup_socket << dendl;
45   	  return 0;
46   	
47   	err:
48   	  server_setup_socket = -1;
49   	  rdma_destroy_id(cm_id);
50   	  rdma_destroy_event_channel(cm_channel);
51   	  return rc;
52   	}
53   	
54   	int RDMAIWARPServerSocketImpl::accept(ConnectedSocket *sock, const SocketOptions &opt,
55   	    entity_addr_t *out, Worker *w)
56   	{
(1) Event cond_true: Condition "should_gather", taking true branch.
57   	  ldout(cct, 15) << __func__ << dendl;
58   	
(2) Event cond_true: Condition "sock", taking true branch.
59   	  ceph_assert(sock);
60   	  struct pollfd pfd = {
61   	    .fd = cm_channel->fd,
62   	    .events = POLLIN,
63   	    .revents = 0,
64   	  };
65   	  int ret = poll(&pfd, 1, 0);
(3) Event cond_true: Condition "ret >= 0", taking true branch.
66   	  ceph_assert(ret >= 0);
(4) Event cond_false: Condition "!ret", taking false branch.
67   	  if (!ret)
(5) Event if_end: End of if statement.
68   	    return -EAGAIN;
69   	
70   	  struct rdma_cm_event *cm_event;
71   	  rdma_get_cm_event(cm_channel, &cm_event);
(6) Event cond_true: Condition "should_gather", taking true branch.
72   	  ldout(cct, 20) << __func__ << " event name: " << rdma_event_str(cm_event->event) << dendl;
73   	
74   	  struct rdma_cm_id *event_cm_id = cm_event->id;
75   	  struct rdma_event_channel *event_channel = rdma_create_event_channel();
76   	
(7) Event cond_false: Condition "this->net.set_nonblock(event_channel->fd) < 0", taking false branch.
77   	  if (net.set_nonblock(event_channel->fd) < 0) {
78   	      lderr(cct) << __func__ << " failed to switch event channel to non-block, close event channel " << dendl;
79   	      rdma_destroy_event_channel(event_channel);
80   	      rdma_ack_cm_event(cm_event);
81   	      return -errno;
(8) Event if_end: End of if statement.
82   	  }
83   	
84   	  rdma_migrate_id(event_cm_id, event_channel);
85   	
86   	  struct rdma_conn_param *remote_conn_param = &cm_event->param.conn;
87   	  struct rdma_conn_param local_conn_param;
88   	
89   	  RDMACMInfo info(event_cm_id, event_channel, remote_conn_param->qp_num);
90   	  RDMAIWARPConnectedSocketImpl* server =
91   	    new RDMAIWARPConnectedSocketImpl(cct, ib, dispatcher, dynamic_cast<RDMAWorker*>(w), &info);
92   	
93   	  memset(&local_conn_param, 0, sizeof(local_conn_param));
94   	  local_conn_param.qp_num = server->get_local_qpn();
95   	
(9) Event cond_false: Condition "rdma_accept(event_cm_id, &local_conn_param)", taking false branch.
96   	  if (rdma_accept(event_cm_id, &local_conn_param)) {
97   	    return -EAGAIN;
(10) Event if_end: End of if statement.
98   	  }
99   	  server->activate();
(11) Event cond_true: Condition "should_gather", taking true branch.
100  	  ldout(cct, 20) << __func__ << " accepted a new QP" << dendl;
101  	
102  	  rdma_ack_cm_event(cm_event);
103  	
104  	  std::unique_ptr<RDMAConnectedSocketImpl> csi(server);
105  	  *sock = ConnectedSocket(std::move(csi));
(12) Event alias: Assigning: "addr" = "&event_cm_id->route.addr.dst_addr". "addr" now points to element 0 of "event_cm_id->route.addr.dst_addr" (which consists of 1 16-byte elements).
Also see events: [overrun-buffer-val]
106  	  struct sockaddr *addr = &event_cm_id->route.addr.dst_addr;
(13) Event overrun-buffer-val: Overrunning struct type sockaddr of 16 bytes by passing it to a function which accesses it at byte offset 27. [details]
Also see events: [alias]
107  	  out->set_sockaddr(addr);
108  	
109  	  return 0;
110  	}
111  	
112  	void RDMAIWARPServerSocketImpl::abort_accept()
113  	{
114  	  if (server_setup_socket >= 0) {
115  	    rdma_destroy_id(cm_id);
116  	    rdma_destroy_event_channel(cm_channel);
117  	  }
118  	}
119