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