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