5 #include <sys/socket.h>
6 #include <netinet/in.h>
10 #include <infiniband/verbs.h>
11 #include <rdma/rdma_cma.h>
13 typedef struct Rdmasrv Rdmasrv
;
15 struct rdma_cm_id
*listen_id
;
16 struct rdma_event_channel
*event_channel
;
17 struct sockaddr_in addr
;
22 extern Nptrans
*np_rdmatrans_create(struct rdma_cm_id
*cmid
, int q_depth
, int msize
);
24 static void rdmasrv_start(Npsrv
*srv
);
25 static void rdmasrv_shutdown(Npsrv
*srv
);
26 static void rdmasrv_destroy(Npsrv
*srv
);
27 static void *rdmasrv_listenproc(void *a
);
30 np_rdmasrv_create(int nwthreads
, int *port
)
36 rdma
= malloc(sizeof(struct Rdmasrv
));
40 rdma
->event_channel
= rdma_create_event_channel();
41 if (!rdma
->event_channel
) {
42 np_werror("Can't create event channel", EIO
);
46 ret
= rdma_create_id(rdma
->event_channel
, &rdma
->listen_id
,
49 np_werror("Can't create an id", ret
);
53 rdma
->addr
.sin_family
= AF_INET
;
54 rdma
->addr
.sin_port
= htons(*port
);
55 rdma
->addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
56 ret
= rdma_bind_addr(rdma
->listen_id
, (struct sockaddr
*)&rdma
->addr
);
58 np_werror("Can't bind", ret
);
62 srv
= np_srv_create(nwthreads
);
64 srv
->start
= rdmasrv_start
;
65 srv
->shutdown
= rdmasrv_shutdown
;
66 srv
->destroy
= rdmasrv_destroy
;
76 rdmasrv_start(Npsrv
*srv
)
82 n
= rdma_listen(rdma
->listen_id
, 1);
88 n
= pthread_create(&rdma
->thread
, NULL
, rdmasrv_listenproc
, srv
);
94 rdmasrv_shutdown(Npsrv
*srv
)
101 rdma_destroy_id(rdma
->listen_id
);
103 rdma
->listen_id
= NULL
;
107 rdmasrv_destroy(Npsrv
*srv
)
113 rdmasrv_shutdown(srv
);
114 pthread_join(rdma
->thread
, &ret
);
120 rdmasrv_listenproc(void *a
)
127 struct rdma_cm_event
*event
;
128 struct rdma_cm_id
*cmid
;
129 enum rdma_cm_event_type etype
;
133 while (!rdma
->shutdown
) {
134 ret
= rdma_get_cm_event(rdma
->event_channel
, &event
);
138 cmid
= (struct rdma_cm_id
*)event
->id
;
139 etype
= event
->event
;
140 rdma_ack_cm_event(event
);
143 case RDMA_CM_EVENT_CONNECT_REQUEST
:
144 printf("Connection request\n");
145 trans
= np_rdmatrans_create(cmid
, srv
->nwthread
, srv
->msize
);
147 conn
= np_conn_create(srv
, trans
);
148 cmid
->context
= conn
;
149 np_srv_add_conn(srv
, conn
);
153 case RDMA_CM_EVENT_ESTABLISHED
:
154 printf("Connection established\n");
157 case RDMA_CM_EVENT_DISCONNECTED
:
158 printf("Connection shutting down\n");
159 conn
= cmid
->context
;
160 np_conn_shutdown(conn
);
164 fprintf(stderr
, "event %d received waiting for a connect request\n",
171 fprintf(stderr
, "shutting down the server with error %d\n", ret
);