01e17ab233cb17ccc0f89c5459c95d77bb49134a
[metze/samba/wip.git] / ctdb / ib / ibwrapper.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Wrap Infiniband calls.
4  *
5  * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
6  *
7  * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "replace.h"
24 #include "system/network.h"
25
26 #include <assert.h>
27 #include <talloc.h>
28 #include <tevent.h>
29
30 #include "lib/util/dlinklist.h"
31 #include "lib/util/debug.h"
32
33 #include "common/logging.h"
34
35 #include <infiniband/kern-abi.h>
36 #include <rdma/rdma_cma_abi.h>
37 #include <rdma/rdma_cma.h>
38
39 #include "ibwrapper.h"
40 #include "ibwrapper_internal.h"
41
42 #define IBW_LASTERR_BUFSIZE 512
43 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
44
45 #define IBW_MAX_SEND_WR 16
46 #define IBW_MAX_RECV_WR 16
47 #define IBW_RECV_BUFSIZE 64
48 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
49
50 static void ibw_event_handler_verbs(struct tevent_context *ev,
51         struct tevent_fd *fde, uint16_t flags, void *private_data);
52 static int ibw_fill_cq(struct ibw_conn *conn);
53 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
54 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
55 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
56
57 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
58         uint32_t n, struct ibv_mr **ppmr)
59 {
60         void *buf;
61
62         DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
63         buf = memalign(pctx->pagesize, n);
64         if (!buf) {
65                 sprintf(ibw_lasterr, "couldn't allocate memory\n");
66                 return NULL;
67         }
68
69         DEBUG(DEBUG_DEBUG, ("2 ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
70         *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
71         if (!*ppmr) {
72                 sprintf(ibw_lasterr, "couldn't allocate mr\n");
73                 free(buf);
74                 return NULL;
75         }
76
77         DEBUG(DEBUG_DEBUG, ("3 ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
78         return buf;
79 }
80
81 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
82 {
83         DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
84         if (*ppmr!=NULL) {
85                 ibv_dereg_mr(*ppmr);
86                 *ppmr = NULL;
87         }
88         if (*ppbuf) {
89                 free(*ppbuf);
90                 *ppbuf = NULL;
91         }
92 }
93
94 static int ibw_init_memory(struct ibw_conn *conn)
95 {
96         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
97         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
98         struct ibw_opts *opts = &pctx->opts;
99         int     i;
100         struct ibw_wr   *p;
101
102         DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
103         pconn->buf_send = ibw_alloc_mr(pctx, pconn,
104                 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
105         if (!pconn->buf_send) {
106                 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
107                 return -1;
108         }
109
110         pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
111                 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
112         if (!pconn->buf_send) {
113                 sprintf(ibw_lasterr, "METZEcouldn't allocate work send buf\n");
114                 return -1;
115         }
116
117         pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
118                 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
119         if (!pconn->buf_recv) {
120                 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
121                 return -1;
122         }
123
124         pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
125         assert(pconn->wr_index!=NULL);
126
127         for(i=0; i<opts->max_send_wr; i++) {
128                 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
129                 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
130                 p->wr_id = i;
131
132                 DLIST_ADD(pconn->wr_list_avail, p);
133         }
134
135         return 0;
136 }
137
138 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
139 {
140         DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
141
142         /*
143          * tevent_fd must be removed before the fd is closed
144          */
145         TALLOC_FREE(pctx->cm_channel_event);
146
147         /* destroy cm */
148         if (pctx->cm_channel) {
149                 rdma_destroy_event_channel(pctx->cm_channel);
150                 pctx->cm_channel = NULL;
151         }
152         if (pctx->cm_id) {
153                 rdma_destroy_id(pctx->cm_id);
154                 pctx->cm_id = NULL;
155         }
156
157         return 0;
158 }
159
160 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
161 {
162         DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
163         return 0;
164 }
165
166 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
167 {
168         DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
169                 pconn, pconn->cm_id));
170
171         /* pconn->wr_index is freed by talloc */
172         /* pconn->wr_index[i] are freed by talloc */
173
174         /*
175          * tevent_fd must be removed before the fd is closed
176          */
177         TALLOC_FREE(pconn->verbs_channel_event);
178
179         /* destroy verbs */
180         if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
181                 rdma_destroy_qp(pconn->cm_id);
182                 pconn->cm_id->qp = NULL;
183         }
184
185         if (pconn->cq!=NULL) {
186                 ibv_destroy_cq(pconn->cq);
187                 pconn->cq = NULL;
188         }
189
190         if (pconn->verbs_channel!=NULL) {
191                 ibv_destroy_comp_channel(pconn->verbs_channel);
192                 pconn->verbs_channel = NULL;
193         }
194
195         /* free memory regions */
196         ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
197         ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
198
199         if (pconn->pd) {
200                 ibv_dealloc_pd(pconn->pd);
201                 pconn->pd = NULL;
202                 DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
203         }
204
205         if (pconn->cm_id) {
206                 rdma_destroy_id(pconn->cm_id);
207                 pconn->cm_id = NULL;
208                 DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
209         }
210
211         return 0;
212 }
213
214 static int ibw_wr_destruct(struct ibw_wr *wr)
215 {
216         if (wr->buf_large!=NULL)
217                 ibw_free_mr(&wr->buf_large, &wr->mr_large);
218         return 0;
219 }
220
221 static int ibw_conn_destruct(struct ibw_conn *conn)
222 {
223         DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
224         
225         /* important here: ctx is a talloc _parent_ */
226         DLIST_REMOVE(conn->ctx->conn_list, conn);
227         return 0;
228 }
229
230 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
231 {
232         struct ibw_conn *conn;
233         struct ibw_conn_priv *pconn;
234
235         assert(ctx!=NULL);
236
237         conn = talloc_zero(mem_ctx, struct ibw_conn);
238         assert(conn!=NULL);
239         talloc_set_destructor(conn, ibw_conn_destruct);
240
241         pconn = talloc_zero(conn, struct ibw_conn_priv);
242         assert(pconn!=NULL);
243         talloc_set_destructor(pconn, ibw_conn_priv_destruct);
244
245         conn->ctx = ctx;
246         conn->internal = (void *)pconn;
247
248         DLIST_ADD(ctx->conn_list, conn);
249
250         return conn;
251 }
252
253 static int ibw_setup_cq_qp(struct ibw_conn *conn)
254 {
255         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
256         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
257         struct ibv_qp_init_attr init_attr;
258         struct ibv_qp_attr attr;
259         int rc;
260
261         DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
262
263         /* init verbs */
264         pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
265         if (!pconn->verbs_channel) {
266                 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
267                 return -1;
268         }
269         DEBUG(DEBUG_DEBUG, ("created channel %p\n", pconn->verbs_channel));
270
271         pconn->verbs_channel_event = tevent_add_fd(pctx->ectx, NULL, /* not pconn or conn */
272                 pconn->verbs_channel->fd, TEVENT_FD_READ, ibw_event_handler_verbs, conn);
273
274         pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
275         if (!pconn->pd) {
276                 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
277                 return -1;
278         }
279         DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
280
281         /* init mr */
282         if (ibw_init_memory(conn))
283                 return -1;
284
285         /* init cq */
286         pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
287                 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
288                 conn, pconn->verbs_channel, 0);
289         if (pconn->cq==NULL) {
290                 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
291                 return -1;
292         }
293
294         rc = ibv_req_notify_cq(pconn->cq, 0);
295         if (rc) {
296                 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
297                 return rc;
298         }
299
300         /* init qp */
301         memset(&init_attr, 0, sizeof(init_attr));
302         init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
303         init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
304         init_attr.cap.max_recv_sge = 1;
305         init_attr.cap.max_send_sge = 1;
306         init_attr.qp_type = IBV_QPT_RC;
307         init_attr.send_cq = pconn->cq;
308         init_attr.recv_cq = pconn->cq;
309
310         rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
311         if (rc) {
312                 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
313                 return rc;
314         }
315         /* elase result is in pconn->cm_id->qp */
316
317         rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
318         if (rc) {
319                 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
320                 return rc;
321         }
322
323         return ibw_fill_cq(conn);
324 }
325
326 static int ibw_refill_cq_recv(struct ibw_conn *conn)
327 {
328         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
329         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
330         int     rc;
331         struct ibv_sge list = {
332                 .addr   = (uintptr_t) NULL, /* filled below */
333                 .length = pctx->opts.recv_bufsize,
334                 .lkey   = pconn->mr_recv->lkey /* always the same */
335         };
336         struct ibv_recv_wr wr = {
337                 .wr_id      = 0, /* filled below */
338                 .sg_list    = &list,
339                 .num_sge    = 1,
340         };
341         struct ibv_recv_wr *bad_wr;
342
343         DEBUG(DEBUG_DEBUG, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
344
345         list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
346         wr.wr_id = pconn->recv_index;
347         pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
348
349         rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
350         if (rc) {
351                 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
352                 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
353                 return -2;
354         }
355
356         return 0;
357 }
358
359 static int ibw_fill_cq(struct ibw_conn *conn)
360 {
361         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
362         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
363         int     i, rc;
364         struct ibv_sge list = {
365                 .addr   = (uintptr_t) NULL, /* filled below */
366                 .length = pctx->opts.recv_bufsize,
367                 .lkey   = pconn->mr_recv->lkey /* always the same */
368         };
369         struct ibv_recv_wr wr = {
370                 .wr_id      = 0, /* filled below */
371                 .sg_list    = &list,
372                 .num_sge    = 1,
373         };
374         struct ibv_recv_wr *bad_wr;
375
376         DEBUG(DEBUG_DEBUG, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
377
378         for(i = pctx->opts.max_recv_wr; i!=0; i--) {
379                 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
380                 wr.wr_id = pconn->recv_index;
381                 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
382
383                 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
384                 if (rc) {
385                         sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
386                         DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
387                         return -2;
388                 }
389         }
390
391         return 0;
392 }
393
394 static int ibw_manage_connect(struct ibw_conn *conn)
395 {
396         struct rdma_conn_param conn_param;
397         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
398         int     rc;
399
400         DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
401
402         if (ibw_setup_cq_qp(conn))
403                 return -1;
404
405         /* cm connect */
406         memset(&conn_param, 0, sizeof conn_param);
407         conn_param.responder_resources = 1;
408         conn_param.initiator_depth = 1;
409         conn_param.retry_count = 10;
410
411         rc = rdma_connect(pconn->cm_id, &conn_param);
412         if (rc)
413                 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
414
415         return rc;
416 }
417
418 static void ibw_event_handler_cm(struct tevent_context *ev,
419         struct tevent_fd *fde, uint16_t flags, void *private_data)
420 {
421         int     rc;
422         struct ibw_ctx  *ctx = talloc_get_type(private_data, struct ibw_ctx);
423         struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
424         struct ibw_conn *conn = NULL;
425         struct ibw_conn_priv *pconn = NULL;
426         struct rdma_cm_id *cma_id = NULL;
427         struct rdma_cm_event *event = NULL;
428
429         assert(ctx!=NULL);
430
431         rc = rdma_get_cm_event(pctx->cm_channel, &event);
432         if (rc) {
433                 ctx->state = IBWS_ERROR;
434                 event = NULL;
435                 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
436                 goto error;
437         }
438         cma_id = event->id;
439
440         DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
441                   (cma_id == pctx->cm_id) ? "parent" : "child"));
442
443         switch (event->event) {
444         case RDMA_CM_EVENT_ADDR_RESOLVED:
445                 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
446                 /* continuing from ibw_connect ... */
447                 rc = rdma_resolve_route(cma_id, 2000);
448                 if (rc) {
449                         sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
450                         goto error;
451                 }
452                 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
453                 break;
454
455         case RDMA_CM_EVENT_ROUTE_RESOLVED:
456                 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
457                 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
458                 assert(cma_id->context!=NULL);
459                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
460
461                 rc = ibw_manage_connect(conn);
462                 if (rc)
463                         goto error;
464
465                 break;
466
467         case RDMA_CM_EVENT_CONNECT_REQUEST:
468                 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
469                 ctx->state = IBWS_CONNECT_REQUEST;
470                 conn = ibw_conn_new(ctx, ctx);
471                 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
472                 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
473                 cma_id->context = (void *)conn;
474                 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
475
476                 if (ibw_setup_cq_qp(conn))
477                         goto error;
478
479                 conn->state = IBWC_INIT;
480                 pctx->connstate_func(ctx, conn);
481
482                 /* continued at ibw_accept when invoked by the func above */
483                 if (!pconn->is_accepted) {
484                         rc = rdma_reject(cma_id, NULL, 0);
485                         if (rc)
486                                 DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
487                         talloc_free(conn);
488                         DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
489                 }
490
491                 /* TODO: clarify whether if it's needed by upper layer: */
492                 ctx->state = IBWS_READY;
493                 pctx->connstate_func(ctx, NULL);
494
495                 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
496                 break;
497
498         case RDMA_CM_EVENT_ESTABLISHED:
499                 /* expected after ibw_accept and ibw_connect[not directly] */
500                 DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
501                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
502                 assert(conn!=NULL); /* important assumption */
503
504                 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
505
506                 /* client conn is up */
507                 conn->state = IBWC_CONNECTED;
508
509                 /* both ctx and conn have changed */
510                 pctx->connstate_func(ctx, conn);
511                 break;
512
513         case RDMA_CM_EVENT_ADDR_ERROR:
514                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
515                 goto error;
516         case RDMA_CM_EVENT_ROUTE_ERROR:
517                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
518                 goto error;
519         case RDMA_CM_EVENT_CONNECT_ERROR:
520                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
521                 goto error;
522         case RDMA_CM_EVENT_UNREACHABLE:
523                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
524                 goto error;
525         case RDMA_CM_EVENT_REJECTED:
526                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
527                 DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
528                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
529                 if (conn) {
530                         /* must be done BEFORE connstate */
531                         if ((rc=rdma_ack_cm_event(event)))
532                                 DEBUG(DEBUG_ERR, ("reject/rdma_ack_cm_event failed with %d\n", rc));
533                         event = NULL; /* not to touch cma_id or conn */
534                         conn->state = IBWC_ERROR;
535                         /* it should free the conn */
536                         pctx->connstate_func(NULL, conn);
537                 }
538                 break; /* this is not strictly an error */
539
540         case RDMA_CM_EVENT_DISCONNECTED:
541                 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
542                 if ((rc=rdma_ack_cm_event(event)))
543                         DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
544                 event = NULL; /* don't ack more */
545
546                 if (cma_id!=pctx->cm_id) {
547                         DEBUG(DEBUG_ERR, ("client DISCONNECT event cm_id=%p\n", cma_id));
548                         conn = talloc_get_type(cma_id->context, struct ibw_conn);
549                         conn->state = IBWC_DISCONNECTED;
550                         pctx->connstate_func(NULL, conn);
551                 }
552                 break;
553
554         case RDMA_CM_EVENT_DEVICE_REMOVAL:
555                 sprintf(ibw_lasterr, "cma detected device removal!\n");
556                 goto error;
557
558         default:
559                 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
560                 goto error;
561         }
562
563         if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
564                 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
565                 goto error;
566         }
567
568         return;
569 error:
570         DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
571
572         if (event!=NULL) {
573                 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
574                         conn = talloc_get_type(cma_id->context, struct ibw_conn);
575                         if (conn) {
576                                 conn->state = IBWC_ERROR;
577                                 pctx->connstate_func(NULL, conn);
578                         }
579                 } else {
580                         ctx->state = IBWS_ERROR;
581                         pctx->connstate_func(ctx, NULL);
582                 }
583
584                 if ((rc=rdma_ack_cm_event(event))!=0) {
585                         DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
586                 }
587         }
588
589         return;
590 }
591
592 static void ibw_event_handler_verbs(struct tevent_context *ev,
593         struct tevent_fd *fde, uint16_t flags, void *private_data)
594 {
595         struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
596         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
597         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
598
599         struct ibv_wc wc;
600         int rc;
601         struct ibv_cq *ev_cq;
602         void          *ev_ctx;
603
604         DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
605
606         /* TODO: check whether if it's good to have more channels here... */
607         rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
608         if (rc) {
609                 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
610                 goto error;
611         }
612         if (ev_cq != pconn->cq) {
613                 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
614                 goto error;
615         }
616         rc = ibv_req_notify_cq(pconn->cq, 0);
617         if (rc) {
618                 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
619                 goto error;
620         }
621
622         while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
623                 if (wc.status) {
624                         sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
625                                 wc.status, wc.opcode, rc);
626                         goto error;
627                 }
628
629                 switch(wc.opcode) {
630                 case IBV_WC_SEND:
631                         DEBUG(DEBUG_DEBUG, ("send completion\n"));
632                         if (ibw_wc_send(conn, &wc))
633                                 goto error;
634                         break;
635
636                 case IBV_WC_RDMA_WRITE:
637                         DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
638                         break;
639         
640                 case IBV_WC_RDMA_READ:
641                         DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
642                         break;
643
644                 case IBV_WC_RECV:
645                         DEBUG(DEBUG_DEBUG, ("recv completion\n"));
646                         if (ibw_wc_recv(conn, &wc))
647                                 goto error;
648                         break;
649
650                 default:
651                         sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
652                         goto error;
653                 }
654         }
655         if (rc!=0) {
656                 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
657                 goto error;
658         }
659
660         ibv_ack_cq_events(pconn->cq, 1);
661
662         return;
663 error:
664         ibv_ack_cq_events(pconn->cq, 1);
665
666         DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
667         
668         if (conn->state!=IBWC_ERROR) {
669                 conn->state = IBWC_ERROR;
670                 pctx->connstate_func(NULL, conn);
671         }
672 }
673
674 static int ibw_process_queue(struct ibw_conn *conn)
675 {
676         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
677         struct ibw_ctx_priv *pctx;
678         struct ibw_wr   *p;
679         int     rc;
680         uint32_t        msg_size;
681
682         if (pconn->queue==NULL)
683                 return 0; /* NOP */
684
685         p = pconn->queue;
686
687         /* we must have at least 1 fragment to send */
688         assert(p->queued_ref_cnt>0);
689         p->queued_ref_cnt--;
690
691         pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
692         msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
693
694         assert(p->queued_msg!=NULL);
695         assert(msg_size!=0);
696
697         DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
698                 p->queued_ref_cnt, msg_size));
699
700         rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
701
702         /* was this the last fragment? */
703         if (p->queued_ref_cnt) {
704                 p->queued_msg += pctx->opts.recv_bufsize;
705         } else {
706                 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
707                 p->queued_msg = NULL;
708         }
709
710         return rc;
711 }
712
713 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
714 {
715         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
716         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
717         struct ibw_wr   *p;
718         int     send_index;
719
720         DEBUG(DEBUG_DEBUG, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
721                 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
722
723         assert(pconn->cm_id->qp->qp_num==wc->qp_num);
724         assert(wc->wr_id >= pctx->opts.max_recv_wr);
725         send_index = wc->wr_id - pctx->opts.max_recv_wr;
726         pconn->wr_sent--;
727
728         if (send_index < pctx->opts.max_send_wr) {
729                 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
730                 p = pconn->wr_index[send_index];
731                 if (p->buf_large!=NULL) {
732                         if (p->ref_cnt) {
733                                 /* awaiting more of it... */
734                                 p->ref_cnt--;
735                         } else {
736                                 ibw_free_mr(&p->buf_large, &p->mr_large);
737                                 DLIST_REMOVE(pconn->wr_list_used, p);
738                                 DLIST_ADD(pconn->wr_list_avail, p);
739                         }
740                 } else { /* nasty - but necessary */
741                         DLIST_REMOVE(pconn->wr_list_used, p);
742                         DLIST_ADD(pconn->wr_list_avail, p);
743                 }
744         } else { /* "extra" request - not optimized */
745                 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
746                 for(p=pconn->extra_sent; p!=NULL; p=p->next)
747                         if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
748                                 break;
749                 if (p==NULL) {
750                         sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
751                                 return -1;
752                 }
753                 if (p->ref_cnt) {
754                         p->ref_cnt--;
755                 } else {
756                         ibw_free_mr(&p->buf_large, &p->mr_large);
757                         DLIST_REMOVE(pconn->extra_sent, p);
758                         DLIST_ADD(pconn->extra_avail, p);
759                 }
760         }
761
762         return ibw_process_queue(conn);
763 }
764
765 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
766         struct ibw_part *part, char **pp, uint32_t add_len, int info)
767 {
768         DEBUG(DEBUG_DEBUG, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
769                 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
770
771         /* allocate more if necessary - it's an "evergrowing" buffer... */
772         if (part->len + add_len > part->bufsize) {
773                 if (part->buf==NULL) {
774                         assert(part->len==0);
775                         part->buf = talloc_size(pconn, add_len);
776                         if (part->buf==NULL) {
777                                 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
778                                         add_len, info);
779                                 return -1;
780                         }
781                         part->bufsize = add_len;
782                 } else {
783                         part->buf = talloc_realloc_size(pconn,
784                                 part->buf, part->len + add_len);
785                         if (part->buf==NULL) {
786                                 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
787                                         part->len, add_len, info);
788                                 return -1;
789                         }
790                 }
791                 part->bufsize = part->len + add_len;
792         }
793
794         /* consume pp */
795         memcpy(part->buf + part->len, *pp, add_len);
796         *pp += add_len;
797         part->len += add_len;
798         part->to_read -= add_len;
799
800         return 0;
801 }
802
803 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
804         struct ibw_part *part, uint32_t threshold)
805 {
806         DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
807                 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
808
809         if (part->bufsize > threshold) {
810                 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
811                         pconn->cm_id, part->bufsize, threshold));
812                 talloc_free(part->buf);
813                 part->buf = talloc_size(pconn, threshold);
814                 if (part->buf==NULL) {
815                         sprintf(ibw_lasterr, "talloc_size failed\n");
816                         return -1;
817                 }
818                 part->bufsize = threshold;
819         }
820         return 0;
821 }
822
823 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
824 {
825         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
826         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
827         struct ibw_part *part = &pconn->part;
828         char    *p;
829         uint32_t        remain = wc->byte_len;
830
831         DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
832                 pconn->cm_id, (uint32_t)wc->wr_id, remain));
833
834         assert(pconn->cm_id->qp->qp_num==wc->qp_num);
835         assert((int)wc->wr_id < pctx->opts.max_recv_wr);
836         assert(wc->byte_len <= pctx->opts.recv_bufsize);
837
838         p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
839
840         while(remain) {
841                 /* here always true: (part->len!=0 && part->to_read!=0) ||
842                         (part->len==0 && part->to_read==0) */
843                 if (part->len) { /* is there a partial msg to be continued? */
844                         int read_len = (part->to_read<=remain) ? part->to_read : remain;
845                         if (ibw_append_to_part(pconn, part, &p, read_len, 421))
846                                 goto error;
847                         remain -= read_len;
848
849                         if (part->len<=sizeof(uint32_t) && part->to_read==0) {
850                                 assert(part->len==sizeof(uint32_t));
851                                 /* set it again now... */
852                                 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
853                                 if (part->to_read<sizeof(uint32_t)) {
854                                         sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
855                                         goto error;
856                                 }
857                                 part->to_read -= sizeof(uint32_t); /* it's already read */
858                         }
859
860                         if (part->to_read==0) {
861                                 if (pctx->receive_func(conn, part->buf, part->len) != 0) {
862                                         goto error;
863                                 }
864                                 part->len = 0; /* tells not having partial data (any more) */
865                                 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
866                                         goto error;
867                         }
868                 } else {
869                         if (remain>=sizeof(uint32_t)) {
870                                 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
871                                 if (msglen<sizeof(uint32_t)) {
872                                         sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
873                                         goto error;
874                                 }
875
876                                 /* mostly awaited case: */
877                                 if (msglen<=remain) {
878                                         if (pctx->receive_func(conn, p, msglen) != 0) {
879                                                 goto error;
880                                         }
881                                         p += msglen;
882                                         remain -= msglen;
883                                 } else {
884                                         part->to_read = msglen;
885                                         /* part->len is already 0 */
886                                         if (ibw_append_to_part(pconn, part, &p, remain, 422))
887                                                 goto error;
888                                         remain = 0; /* to be continued ... */
889                                         /* part->to_read > 0 here */
890                                 }
891                         } else { /* edge case: */
892                                 part->to_read = sizeof(uint32_t);
893                                 /* part->len is already 0 */
894                                 if (ibw_append_to_part(pconn, part, &p, remain, 423))
895                                         goto error;
896                                 remain = 0;
897                                 /* part->to_read > 0 here */
898                         }
899                 }
900         } /* <remain> is always decreased at least by 1 */
901
902         if (ibw_refill_cq_recv(conn))
903                 goto error;
904
905         return 0;
906
907 error:
908         DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
909         return -1;
910 }
911
912 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
913 {
914         int     i;
915         const char *name, *value;
916
917         DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
918
919         opts->max_send_wr = IBW_MAX_SEND_WR;
920         opts->max_recv_wr = IBW_MAX_RECV_WR;
921         opts->recv_bufsize = IBW_RECV_BUFSIZE;
922         opts->recv_threshold = IBW_RECV_THRESHOLD;
923
924         for(i=0; i<nattr; i++) {
925                 name = attr[i].name;
926                 value = attr[i].value;
927
928                 assert(name!=NULL && value!=NULL);
929                 if (strcmp(name, "max_send_wr")==0)
930                         opts->max_send_wr = atoi(value);
931                 else if (strcmp(name, "max_recv_wr")==0)
932                         opts->max_recv_wr = atoi(value);
933                 else if (strcmp(name, "recv_bufsize")==0)
934                         opts->recv_bufsize = atoi(value);
935                 else if (strcmp(name, "recv_threshold")==0)
936                         opts->recv_threshold = atoi(value);
937                 else {
938                         sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
939                         return -1;
940                 }
941         }
942         return 0;
943 }
944
945 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
946         void *ctx_userdata,
947         ibw_connstate_fn_t ibw_connstate,
948         ibw_receive_fn_t ibw_receive,
949         struct tevent_context *ectx)
950 {
951         struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
952         struct ibw_ctx_priv *pctx;
953         int     rc;
954
955         DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
956
957         /* initialize basic data structures */
958         memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
959
960         assert(ctx!=NULL);
961         ibw_lasterr[0] = '\0';
962         talloc_set_destructor(ctx, ibw_ctx_destruct);
963         ctx->ctx_userdata = ctx_userdata;
964
965         pctx = talloc_zero(ctx, struct ibw_ctx_priv);
966         talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
967         ctx->internal = (void *)pctx;
968         assert(pctx!=NULL);
969
970         pctx->connstate_func = ibw_connstate;
971         pctx->receive_func = ibw_receive;
972
973         pctx->ectx = ectx;
974
975         /* process attributes */
976         if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
977                 goto cleanup;
978
979         /* init cm */
980         pctx->cm_channel = rdma_create_event_channel();
981         if (!pctx->cm_channel) {
982                 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
983                 goto cleanup;
984         }
985
986         pctx->cm_channel_event = tevent_add_fd(pctx->ectx, pctx,
987                 pctx->cm_channel->fd, TEVENT_FD_READ, ibw_event_handler_cm, ctx);
988
989 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
990         rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
991 #else
992         rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
993 #endif
994         if (rc) {
995                 rc = errno;
996                 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
997                 goto cleanup;
998         }
999         DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
1000
1001         pctx->pagesize = sysconf(_SC_PAGESIZE);
1002
1003         return ctx;
1004         /* don't put code here */
1005 cleanup:
1006         DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1007
1008         if (ctx)
1009                 talloc_free(ctx);
1010
1011         return NULL;
1012 }
1013
1014 int ibw_stop(struct ibw_ctx *ctx)
1015 {
1016         struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1017         struct ibw_conn *p;
1018
1019         DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
1020
1021         for(p=ctx->conn_list; p!=NULL; p=p->next) {
1022                 if (p->state==IBWC_ERROR || p->state==IBWC_CONNECTED) {
1023                         if (ibw_disconnect(p))
1024                                 return -1;
1025                 }
1026         }
1027
1028         ctx->state = IBWS_STOPPED;
1029         pctx->connstate_func(ctx, NULL);
1030
1031         return 0;
1032 }
1033
1034 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1035 {
1036         struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1037         int     rc;
1038
1039         DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
1040                 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1041         rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1042         if (rc) {
1043                 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1044                 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1045                 return rc;
1046         }
1047         DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
1048
1049         return 0;
1050 }
1051
1052 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1053 {
1054         struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1055         int     rc;
1056
1057         DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
1058         rc = rdma_listen(pctx->cm_id, backlog);
1059         if (rc) {
1060                 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1061                 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1062                 return rc;
1063         }
1064
1065         return 0;
1066 }
1067
1068 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1069 {
1070         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1071         struct rdma_conn_param  conn_param;
1072         int     rc;
1073
1074         DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1075         conn->conn_userdata = conn_userdata;
1076
1077         memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1078         conn_param.responder_resources = 1;
1079         conn_param.initiator_depth = 1;
1080         rc = rdma_accept(pconn->cm_id, &conn_param);
1081         if (rc) {
1082                 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1083                 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1084                 return -1;;
1085         }
1086
1087         pconn->is_accepted = 1;
1088
1089         /* continued at RDMA_CM_EVENT_ESTABLISHED */
1090
1091         return 0;
1092 }
1093
1094 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1095 {
1096         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1097         struct ibw_conn_priv *pconn = NULL;
1098         int     rc;
1099
1100         assert(conn!=NULL);
1101
1102         conn->conn_userdata = conn_userdata;
1103         pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1104         DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1105                 ntohs(serv_addr->sin_port)));
1106
1107         /* clean previous - probably half - initialization */
1108         if (ibw_conn_priv_destruct(pconn)) {
1109                 DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1110                 return -1;
1111         }
1112
1113         /* init cm */
1114 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1115         rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1116 #else
1117         rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1118 #endif
1119         if (rc) {
1120                 rc = errno;
1121                 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1122                 talloc_free(conn);
1123                 return -1;
1124         }
1125         DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1126
1127         rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1128         if (rc) {
1129                 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1130                 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1131                 talloc_free(conn);
1132                 return -1;
1133         }
1134
1135         /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1136
1137         return 0;
1138 }
1139
1140 int ibw_disconnect(struct ibw_conn *conn)
1141 {
1142         int     rc;
1143         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1144
1145         DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1146
1147         assert(pconn!=NULL);
1148
1149         switch(conn->state) {
1150         case IBWC_ERROR:
1151                 ibw_conn_priv_destruct(pconn); /* do this here right now */
1152                 break;
1153         case IBWC_CONNECTED:
1154                 rc = rdma_disconnect(pconn->cm_id);
1155                 if (rc) {
1156                         sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1157                         DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1158                         return rc;
1159                 }
1160                 break;
1161         default:
1162                 DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
1163                 break;
1164         }
1165
1166         return 0;
1167 }
1168
1169 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1170 {
1171         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1172         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1173         struct ibw_wr *p = pconn->wr_list_avail;
1174
1175         if (p!=NULL) {
1176                 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1177
1178                 DLIST_REMOVE(pconn->wr_list_avail, p);
1179                 DLIST_ADD(pconn->wr_list_used, p);
1180
1181                 if (len <= pctx->opts.recv_bufsize) {
1182                         *buf = (void *)p->buf;
1183                 } else {
1184                         p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1185                         if (p->buf_large==NULL) {
1186                                 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1187                                 goto error;
1188                         }
1189                         *buf = (void *)p->buf_large;
1190                 }
1191                 /* p->wr_id is already filled in ibw_init_memory */
1192         } else {
1193                 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1194                 /* not optimized */
1195                 p = pconn->extra_avail;
1196                 if (!p) {
1197                         p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1198                         talloc_set_destructor(p, ibw_wr_destruct);
1199                         if (p==NULL) {
1200                                 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1201                                 goto error;
1202                         }
1203                         p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1204                         pconn->extra_max++;
1205                         switch(pconn->extra_max) {
1206                                 case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
1207                                 case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
1208                                 case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
1209                                 case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
1210                                 default: break;
1211                         }
1212                 }
1213
1214                 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1215                 if (p->buf_large==NULL) {
1216                         sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1217                         goto error;
1218                 }
1219                 *buf = (void *)p->buf_large;
1220
1221                 DLIST_REMOVE(pconn->extra_avail, p);
1222                 /* we don't have prepared index for this, so that
1223                  * we will have to find this by wr_id later on */
1224                 DLIST_ADD(pconn->extra_sent, p);
1225         }
1226
1227         *key = (void *)p;
1228
1229         return 0;
1230 error:
1231         DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1232         return -1;
1233 }
1234
1235
1236 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1237 {
1238         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1239         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1240         int     rc;
1241
1242         /* can we send it right now? */
1243         if (pconn->wr_sent<pctx->opts.max_send_wr) {
1244                 struct ibv_send_wr *bad_wr;
1245                 struct ibv_sge list = {
1246                         .addr   = (uintptr_t)buf,
1247                         .length = len,
1248                         .lkey   = pconn->mr_send->lkey
1249                 };
1250                 struct ibv_send_wr wr = {
1251                         .wr_id      = p->wr_id + pctx->opts.max_recv_wr,
1252                         .sg_list    = &list,
1253                         .num_sge    = 1,
1254                         .opcode     = IBV_WR_SEND,
1255                         .send_flags = IBV_SEND_SIGNALED,
1256                 };
1257
1258                 if (p->buf_large==NULL) {
1259                         DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1260                                 pconn->cm_id, (uint32_t)wr.wr_id, len));
1261                 } else {
1262                         DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1263                                 pconn->cm_id, (uint32_t)wr.wr_id, len));
1264                         list.lkey = p->mr_large->lkey;
1265                 }
1266
1267                 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1268                 if (rc) {
1269                         sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1270                                 rc, pconn->wr_sent);
1271                         goto error;
1272                 }
1273
1274                 pconn->wr_sent++;
1275
1276                 return rc;
1277         } /* else put the request into our own queue: */
1278
1279         DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1280
1281         /* TODO: clarify how to continue when state==IBWC_STOPPED */
1282
1283         /* to be sent by ibw_wc_send */
1284         /* regardless "normal" or [a part of] "large" packet */
1285         if (!p->queued_ref_cnt) {
1286                 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1287                         qprev, qnext); /* TODO: optimize */
1288                 p->queued_msg = buf;
1289         }
1290         p->queued_ref_cnt++;
1291         p->queued_rlen = len; /* last wins; see ibw_wc_send */
1292
1293         return 0;
1294 error:
1295         DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1296         return -1;
1297 }
1298
1299 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1300 {
1301         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1302         struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1303         int     rc;
1304
1305         assert(len>=sizeof(uint32_t));
1306         assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1307
1308         if (len > pctx->opts.recv_bufsize) {
1309                 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1310                 int     rlen = len;
1311                 char    *packet = (char *)buf;
1312                 uint32_t        recv_bufsize = pctx->opts.recv_bufsize;
1313
1314                 DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1315                         pconn->cm_id, buf, len));
1316
1317                 /* single threaded => no race here: */
1318                 assert(p->ref_cnt==0);
1319                 while(rlen > recv_bufsize) {
1320                         rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1321                         if (rc)
1322                                 return rc;
1323                         packet += recv_bufsize;
1324                         rlen -= recv_bufsize;
1325                         p->ref_cnt++; /* not good to have it in ibw_send_packet */
1326                 }
1327                 if (rlen) {
1328                         rc = ibw_send_packet(conn, packet, p, rlen);
1329                         p->ref_cnt++; /* not good to have it in ibw_send_packet */
1330                 }
1331                 p->ref_cnt--; /* for the same handling */
1332         } else {
1333                 assert(p->ref_cnt==0);
1334                 assert(p->queued_ref_cnt==0);
1335
1336                 rc = ibw_send_packet(conn, buf, p, len);
1337         }
1338         return rc;
1339 }
1340
1341 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1342 {
1343         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1344         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1345         struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1346
1347         assert(p!=NULL);
1348         assert(buf!=NULL);
1349         assert(conn!=NULL);
1350
1351         if (p->buf_large!=NULL)
1352                 ibw_free_mr(&p->buf_large, &p->mr_large);
1353
1354         /* parallel case */
1355         if (p->wr_id < pctx->opts.max_send_wr) {
1356                 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1357                 DLIST_REMOVE(pconn->wr_list_used, p);
1358                 DLIST_ADD(pconn->wr_list_avail, p);
1359         } else { /* "extra" packet */
1360                 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1361                 DLIST_REMOVE(pconn->extra_sent, p);
1362                 DLIST_ADD(pconn->extra_avail, p);
1363         }
1364
1365         return 0;
1366 }
1367
1368 const char *ibw_getLastError(void)
1369 {
1370         return ibw_lasterr;
1371 }