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