- ctdb/ib minor bugfixes (error case)
[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 <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                 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
432                 goto error;
433         }
434         cma_id = event->id;
435
436         DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
437                   (cma_id == pctx->cm_id) ? "parent" : "child"));
438
439         switch (event->event) {
440         case RDMA_CM_EVENT_ADDR_RESOLVED:
441                 DEBUG(11, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
442                 /* continuing from ibw_connect ... */
443                 rc = rdma_resolve_route(cma_id, 2000);
444                 if (rc) {
445                         sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
446                         goto error;
447                 }
448                 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
449                 break;
450
451         case RDMA_CM_EVENT_ROUTE_RESOLVED:
452                 DEBUG(11, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
453                 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
454                 assert(cma_id->context!=NULL);
455                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
456
457                 rc = ibw_manage_connect(conn);
458                 if (rc)
459                         goto error;
460
461                 break;
462
463         case RDMA_CM_EVENT_CONNECT_REQUEST:
464                 DEBUG(11, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
465                 ctx->state = IBWS_CONNECT_REQUEST;
466                 conn = ibw_conn_new(ctx, ctx);
467                 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
468                 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
469                 cma_id->context = (void *)conn;
470                 DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));
471
472                 if (ibw_setup_cq_qp(conn))
473                         goto error;
474
475                 conn->state = IBWC_INIT;
476                 pctx->connstate_func(ctx, conn);
477
478                 /* continued at ibw_accept when invoked by the func above */
479                 if (!pconn->is_accepted) {
480                         rc = rdma_reject(cma_id, NULL, 0);
481                         if (rc)
482                                 DEBUG(0, ("rdma_reject failed with rc=%d\n", rc));
483                         talloc_free(conn);
484                         DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
485                 }
486
487                 /* TODO: clarify whether if it's needed by upper layer: */
488                 ctx->state = IBWS_READY;
489                 pctx->connstate_func(ctx, NULL);
490
491                 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
492                 break;
493
494         case RDMA_CM_EVENT_ESTABLISHED:
495                 /* expected after ibw_accept and ibw_connect[not directly] */
496                 DEBUG(0, ("ESTABLISHED (conn: %p)\n", cma_id->context));
497                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
498                 assert(conn!=NULL); /* important assumption */
499
500                 DEBUG(10, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
501
502                 /* client conn is up */
503                 conn->state = IBWC_CONNECTED;
504
505                 /* both ctx and conn have changed */
506                 pctx->connstate_func(ctx, conn);
507                 break;
508
509         case RDMA_CM_EVENT_ADDR_ERROR:
510                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
511         case RDMA_CM_EVENT_ROUTE_ERROR:
512                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
513         case RDMA_CM_EVENT_CONNECT_ERROR:
514                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
515         case RDMA_CM_EVENT_UNREACHABLE:
516                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
517         case RDMA_CM_EVENT_REJECTED:
518                 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
519                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
520                 if (conn) {
521                         if ((rc=rdma_ack_cm_event(event)))
522                                 DEBUG(0, ("reject/rdma_ack_cm_event failed with %d\n", rc));
523                         event = NULL;
524                         pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
525                         ibw_conn_priv_destruct(pconn);
526                 }
527                 goto error;
528
529         case RDMA_CM_EVENT_DISCONNECTED:
530                 DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
531                 if ((rc=rdma_ack_cm_event(event)))
532                         DEBUG(0, ("disc/rdma_ack_cm_event failed with %d\n", rc));
533                 event = NULL; /* don't ack more */
534
535                 if (cma_id!=pctx->cm_id) {
536                         DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
537                         conn = talloc_get_type(cma_id->context, struct ibw_conn);
538                         conn->state = IBWC_DISCONNECTED;
539                         pctx->connstate_func(NULL, conn);
540                 }
541                 break;
542
543         case RDMA_CM_EVENT_DEVICE_REMOVAL:
544                 sprintf(ibw_lasterr, "cma detected device removal!\n");
545                 goto error;
546
547         default:
548                 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
549                 goto error;
550         }
551
552         if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
553                 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
554                 goto error;
555         }
556
557         return;
558 error:
559         if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
560                 DEBUG(0, ("rdma_ack_cm_event failed with %d\n", rc));
561         }
562
563         DEBUG(0, ("cm event handler: %s", ibw_lasterr));
564
565         if (cma_id!=pctx->cm_id) {
566                 conn = talloc_get_type(cma_id->context, struct ibw_conn);
567                 if (conn) {
568                         conn->state = IBWC_ERROR;
569                         pctx->connstate_func(NULL, conn);
570                 }
571         } else {
572                 ctx->state = IBWS_ERROR;
573                 pctx->connstate_func(ctx, NULL);
574         }
575 }
576
577 static void ibw_event_handler_verbs(struct event_context *ev,
578         struct fd_event *fde, uint16_t flags, void *private_data)
579 {
580         struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
581         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
582         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
583
584         struct ibv_wc wc;
585         int rc;
586         struct ibv_cq *ev_cq;
587         void          *ev_ctx;
588
589         DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
590
591         /* TODO: check whether if it's good to have more channels here... */
592         rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
593         if (rc) {
594                 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
595                 goto error;
596         }
597         if (ev_cq != pconn->cq) {
598                 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
599                 goto error;
600         }
601         rc = ibv_req_notify_cq(pconn->cq, 0);
602         if (rc) {
603                 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
604                 goto error;
605         }
606
607         while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
608                 if (wc.status) {
609                         sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
610                                 wc.status, wc.opcode, rc);
611                         goto error;
612                 }
613
614                 switch(wc.opcode) {
615                 case IBV_WC_SEND:
616                         DEBUG(10, ("send completion\n"));
617                         if (ibw_wc_send(conn, &wc))
618                                 goto error;
619                         break;
620
621                 case IBV_WC_RDMA_WRITE:
622                         DEBUG(10, ("rdma write completion\n"));
623                         break;
624         
625                 case IBV_WC_RDMA_READ:
626                         DEBUG(10, ("rdma read completion\n"));
627                         break;
628
629                 case IBV_WC_RECV:
630                         DEBUG(10, ("recv completion\n"));
631                         if (ibw_wc_recv(conn, &wc))
632                                 goto error;
633                         break;
634
635                 default:
636                         sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
637                         goto error;
638                 }
639         }
640         if (rc!=0) {
641                 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
642                 goto error;
643         }
644
645         ibv_ack_cq_events(pconn->cq, 1);
646
647         return;
648 error:
649         ibv_ack_cq_events(pconn->cq, 1);
650
651         DEBUG(0, (ibw_lasterr));
652         
653         if (conn->state!=IBWC_ERROR) {
654                 conn->state = IBWC_ERROR;
655                 pctx->connstate_func(NULL, conn);
656         }
657 }
658
659 static int ibw_process_queue(struct ibw_conn *conn)
660 {
661         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
662         struct ibw_ctx_priv *pctx;
663         struct ibw_wr   *p;
664         int     rc;
665         uint32_t        msg_size;
666
667         if (pconn->queue==NULL)
668                 return 0; /* NOP */
669
670         p = pconn->queue;
671
672         /* we must have at least 1 fragment to send */
673         assert(p->queued_ref_cnt>0);
674         p->queued_ref_cnt--;
675
676         pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
677         msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
678
679         assert(p->queued_msg!=NULL);
680         assert(msg_size!=0);
681
682         DEBUG(10, ("ibw_process_queue refcnt=%d msgsize=%u\n",
683                 p->queued_ref_cnt, msg_size));
684
685         rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
686
687         /* was this the last fragment? */
688         if (p->queued_ref_cnt) {
689                 p->queued_msg += pctx->opts.recv_bufsize;
690         } else {
691                 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
692                 p->queued_msg = NULL;
693         }
694
695         return rc;
696 }
697
698 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
699 {
700         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
701         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
702         struct ibw_wr   *p;
703         int     send_index;
704
705         DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
706                 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
707
708         assert(pconn->cm_id->qp->qp_num==wc->qp_num);
709         assert(wc->wr_id >= pctx->opts.max_recv_wr);
710         send_index = wc->wr_id - pctx->opts.max_recv_wr;
711         pconn->wr_sent--;
712
713         if (send_index < pctx->opts.max_send_wr) {
714                 DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
715                 p = pconn->wr_index[send_index];
716                 if (p->buf_large!=NULL) {
717                         if (p->ref_cnt) {
718                                 /* awaiting more of it... */
719                                 p->ref_cnt--;
720                         } else {
721                                 ibw_free_mr(&p->buf_large, &p->mr_large);
722                                 DLIST_REMOVE(pconn->wr_list_used, p);
723                                 DLIST_ADD(pconn->wr_list_avail, p);
724                         }
725                 } else { /* nasty - but necessary */
726                         DLIST_REMOVE(pconn->wr_list_used, p);
727                         DLIST_ADD(pconn->wr_list_avail, p);
728                 }
729         } else { /* "extra" request - not optimized */
730                 DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
731                 for(p=pconn->extra_sent; p!=NULL; p=p->next)
732                         if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
733                                 break;
734                 if (p==NULL) {
735                         sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
736                                 return -1;
737                 }
738                 if (p->ref_cnt) {
739                         p->ref_cnt--;
740                 } else {
741                         ibw_free_mr(&p->buf_large, &p->mr_large);
742                         DLIST_REMOVE(pconn->extra_sent, p);
743                         DLIST_ADD(pconn->extra_avail, p);
744                 }
745         }
746
747         return ibw_process_queue(conn);
748 }
749
750 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
751         struct ibw_part *part, char **pp, uint32_t add_len, int info)
752 {
753         DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
754                 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
755
756         /* allocate more if necessary - it's an "evergrowing" buffer... */
757         if (part->len + add_len > part->bufsize) {
758                 if (part->buf==NULL) {
759                         assert(part->len==0);
760                         part->buf = talloc_size(pconn, add_len);
761                         if (part->buf==NULL) {
762                                 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
763                                         add_len, info);
764                                 return -1;
765                         }
766                         part->bufsize = add_len;
767                 } else {
768                         part->buf = talloc_realloc_size(pconn,
769                                 part->buf, part->len + add_len);
770                         if (part->buf==NULL) {
771                                 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
772                                         part->len, add_len, info);
773                                 return -1;
774                         }
775                 }
776                 part->bufsize = part->len + add_len;
777         }
778
779         /* consume pp */
780         memcpy(part->buf + part->len, *pp, add_len);
781         *pp += add_len;
782         part->len += add_len;
783         part->to_read -= add_len;
784
785         return 0;
786 }
787
788 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
789         struct ibw_part *part, uint32_t threshold)
790 {
791         DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
792                 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
793
794         if (part->bufsize > threshold) {
795                 DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
796                         pconn->cm_id, part->bufsize, threshold));
797                 talloc_free(part->buf);
798                 part->buf = talloc_size(pconn, threshold);
799                 if (part->buf==NULL) {
800                         sprintf(ibw_lasterr, "talloc_size failed\n");
801                         return -1;
802                 }
803                 part->bufsize = threshold;
804         }
805         return 0;
806 }
807
808 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
809 {
810         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
811         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
812         struct ibw_part *part = &pconn->part;
813         char    *p;
814         uint32_t        remain = wc->byte_len;
815
816         DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
817                 pconn->cm_id, (uint32_t)wc->wr_id, remain));
818
819         assert(pconn->cm_id->qp->qp_num==wc->qp_num);
820         assert((int)wc->wr_id < pctx->opts.max_recv_wr);
821         assert(wc->byte_len <= pctx->opts.recv_bufsize);
822
823         p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
824
825         while(remain) {
826                 /* here always true: (part->len!=0 && part->to_read!=0) ||
827                         (part->len==0 && part->to_read==0) */
828                 if (part->len) { /* is there a partial msg to be continued? */
829                         int read_len = (part->to_read<=remain) ? part->to_read : remain;
830                         if (ibw_append_to_part(pconn, part, &p, read_len, 421))
831                                 goto error;
832                         remain -= read_len;
833
834                         if (part->len<=sizeof(uint32_t) && part->to_read==0) {
835                                 assert(part->len==sizeof(uint32_t));
836                                 /* set it again now... */
837                                 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
838                                 if (part->to_read<sizeof(uint32_t)) {
839                                         sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
840                                         goto error;
841                                 }
842                                 part->to_read -= sizeof(uint32_t); /* it's already read */
843                         }
844
845                         if (part->to_read==0) {
846                                 pctx->receive_func(conn, part->buf, part->len);
847                                 part->len = 0; /* tells not having partial data (any more) */
848                                 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
849                                         goto error;
850                         }
851                 } else {
852                         if (remain>=sizeof(uint32_t)) {
853                                 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
854                                 if (msglen<sizeof(uint32_t)) {
855                                         sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
856                                         goto error;
857                                 }
858
859                                 /* mostly awaited case: */
860                                 if (msglen<=remain) {
861                                         pctx->receive_func(conn, p, msglen);
862                                         p += msglen;
863                                         remain -= msglen;
864                                 } else {
865                                         part->to_read = msglen;
866                                         /* part->len is already 0 */
867                                         if (ibw_append_to_part(pconn, part, &p, remain, 422))
868                                                 goto error;
869                                         remain = 0; /* to be continued ... */
870                                         /* part->to_read > 0 here */
871                                 }
872                         } else { /* edge case: */
873                                 part->to_read = sizeof(uint32_t);
874                                 /* part->len is already 0 */
875                                 if (ibw_append_to_part(pconn, part, &p, remain, 423))
876                                         goto error;
877                                 remain = 0;
878                                 /* part->to_read > 0 here */
879                         }
880                 }
881         } /* <remain> is always decreased at least by 1 */
882
883         if (ibw_refill_cq_recv(conn))
884                 goto error;
885
886         return 0;
887
888 error:
889         DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
890         return -1;
891 }
892
893 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
894 {
895         int     i;
896         const char *name, *value;
897
898         DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
899
900         opts->max_send_wr = IBW_MAX_SEND_WR;
901         opts->max_recv_wr = IBW_MAX_RECV_WR;
902         opts->recv_bufsize = IBW_RECV_BUFSIZE;
903         opts->recv_threshold = IBW_RECV_THRESHOLD;
904
905         for(i=0; i<nattr; i++) {
906                 name = attr[i].name;
907                 value = attr[i].value;
908
909                 assert(name!=NULL && value!=NULL);
910                 if (strcmp(name, "max_send_wr")==0)
911                         opts->max_send_wr = atoi(value);
912                 else if (strcmp(name, "max_recv_wr")==0)
913                         opts->max_recv_wr = atoi(value);
914                 else if (strcmp(name, "recv_bufsize")==0)
915                         opts->recv_bufsize = atoi(value);
916                 else if (strcmp(name, "recv_threshold")==0)
917                         opts->recv_threshold = atoi(value);
918                 else {
919                         sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
920                         return -1;
921                 }
922         }
923         return 0;
924 }
925
926 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
927         void *ctx_userdata,
928         ibw_connstate_fn_t ibw_connstate,
929         ibw_receive_fn_t ibw_receive,
930         struct event_context *ectx)
931 {
932         struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
933         struct ibw_ctx_priv *pctx;
934         int     rc;
935
936         DEBUG(10, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
937
938         /* initialize basic data structures */
939         memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
940
941         assert(ctx!=NULL);
942         ibw_lasterr[0] = '\0';
943         talloc_set_destructor(ctx, ibw_ctx_destruct);
944         ctx->ctx_userdata = ctx_userdata;
945
946         pctx = talloc_zero(ctx, struct ibw_ctx_priv);
947         talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
948         ctx->internal = (void *)pctx;
949         assert(pctx!=NULL);
950
951         pctx->connstate_func = ibw_connstate;
952         pctx->receive_func = ibw_receive;
953
954         pctx->ectx = ectx;
955
956         /* process attributes */
957         if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
958                 goto cleanup;
959
960         /* init cm */
961         pctx->cm_channel = rdma_create_event_channel();
962         if (!pctx->cm_channel) {
963                 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
964                 goto cleanup;
965         }
966
967         pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
968                 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
969
970         rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
971         if (rc) {
972                 rc = errno;
973                 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
974                 goto cleanup;
975         }
976         DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
977
978         pctx->pagesize = sysconf(_SC_PAGESIZE);
979
980         return ctx;
981         /* don't put code here */
982 cleanup:
983         DEBUG(0, (ibw_lasterr));
984
985         if (ctx)
986                 talloc_free(ctx);
987
988         return NULL;
989 }
990
991 int ibw_stop(struct ibw_ctx *ctx)
992 {
993         struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
994         struct ibw_conn *p;
995
996         DEBUG(10, ("ibw_stop\n"));
997
998         for(p=ctx->conn_list; p!=NULL; p=p->next) {
999                 if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
1000                         if (ibw_disconnect(p))
1001                                 return -1;
1002                 }
1003         }
1004
1005         ctx->state = IBWS_STOPPED;
1006         pctx->connstate_func(ctx, NULL);
1007
1008         return 0;
1009 }
1010
1011 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1012 {
1013         struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1014         int     rc;
1015
1016         DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
1017                 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1018         rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1019         if (rc) {
1020                 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1021                 DEBUG(0, (ibw_lasterr));
1022                 return rc;
1023         }
1024         DEBUG(10, ("rdma_bind_addr successful\n"));
1025
1026         return 0;
1027 }
1028
1029 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1030 {
1031         struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1032         int     rc;
1033
1034         DEBUG(10, ("ibw_listen\n"));
1035         rc = rdma_listen(pctx->cm_id, backlog);
1036         if (rc) {
1037                 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1038                 DEBUG(0, (ibw_lasterr));
1039                 return rc;
1040         }
1041
1042         return 0;
1043 }
1044
1045 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1046 {
1047         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1048         struct rdma_conn_param  conn_param;
1049         int     rc;
1050
1051         DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1052         conn->conn_userdata = conn_userdata;
1053
1054         memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1055         conn_param.responder_resources = 1;
1056         conn_param.initiator_depth = 1;
1057         rc = rdma_accept(pconn->cm_id, &conn_param);
1058         if (rc) {
1059                 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1060                 DEBUG(0, (ibw_lasterr));
1061                 return -1;;
1062         }
1063
1064         pconn->is_accepted = 1;
1065
1066         /* continued at RDMA_CM_EVENT_ESTABLISHED */
1067
1068         return 0;
1069 }
1070
1071 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1072 {
1073         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1074         struct ibw_conn_priv *pconn = NULL;
1075         int     rc;
1076
1077         assert(conn!=NULL);
1078
1079         conn->conn_userdata = conn_userdata;
1080         pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1081         DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1082                 ntohs(serv_addr->sin_port)));
1083
1084         /* clean previous - probably half - initialization */
1085         if (ibw_conn_priv_destruct(pconn)) {
1086                 DEBUG(0, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1087                 return -1;
1088         }
1089
1090         /* init cm */
1091         rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1092         if (rc) {
1093                 rc = errno;
1094                 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1095                 talloc_free(conn);
1096                 return -1;
1097         }
1098         DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1099
1100         rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1101         if (rc) {
1102                 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1103                 DEBUG(0, (ibw_lasterr));
1104                 talloc_free(conn);
1105                 return -1;
1106         }
1107
1108         /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1109
1110         return 0;
1111 }
1112
1113 int ibw_disconnect(struct ibw_conn *conn)
1114 {
1115         int     rc;
1116         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1117
1118         DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1119
1120         assert(pconn!=NULL);
1121
1122         switch(conn->state) {
1123         case IBWC_ERROR:
1124                 ibw_conn_priv_destruct(pconn); /* do this here right now */
1125                 break;
1126         case IBWC_CONNECTED:
1127                 rc = rdma_disconnect(pconn->cm_id);
1128                 if (rc) {
1129                         sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1130                         DEBUG(0, (ibw_lasterr));
1131                         return rc;
1132                 }
1133                 break;
1134         default:
1135                 DEBUG(9, ("invalid state for disconnect: %d\n", conn->state));
1136                 break;
1137         }
1138
1139         return 0;
1140 }
1141
1142 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1143 {
1144         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1145         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1146         struct ibw_wr *p = pconn->wr_list_avail;
1147
1148         if (p!=NULL) {
1149                 DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1150
1151                 DLIST_REMOVE(pconn->wr_list_avail, p);
1152                 DLIST_ADD(pconn->wr_list_used, p);
1153
1154                 if (len <= pctx->opts.recv_bufsize) {
1155                         *buf = (void *)p->buf;
1156                 } else {
1157                         p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1158                         if (p->buf_large==NULL) {
1159                                 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1160                                 goto error;
1161                         }
1162                         *buf = (void *)p->buf_large;
1163                 }
1164                 /* p->wr_id is already filled in ibw_init_memory */
1165         } else {
1166                 DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1167                 /* not optimized */
1168                 p = pconn->extra_avail;
1169                 if (!p) {
1170                         p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1171                         talloc_set_destructor(p, ibw_wr_destruct);
1172                         if (p==NULL) {
1173                                 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1174                                 goto error;
1175                         }
1176                         p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1177                         pconn->extra_max++;
1178                         switch(pconn->extra_max) {
1179                                 case 1: DEBUG(2, ("warning: queue performed\n")); break;
1180                                 case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
1181                                 case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
1182                                 case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
1183                                 default: break;
1184                         }
1185                 }
1186
1187                 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1188                 if (p->buf_large==NULL) {
1189                         sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1190                         goto error;
1191                 }
1192                 *buf = (void *)p->buf_large;
1193
1194                 DLIST_REMOVE(pconn->extra_avail, p);
1195                 /* we don't have prepared index for this, so that
1196                  * we will have to find this by wr_id later on */
1197                 DLIST_ADD(pconn->extra_sent, p);
1198         }
1199
1200         *key = (void *)p;
1201
1202         return 0;
1203 error:
1204         DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1205         return -1;
1206 }
1207
1208
1209 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1210 {
1211         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1212         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1213         int     rc;
1214
1215         /* can we send it right now? */
1216         if (pconn->wr_sent<pctx->opts.max_send_wr) {
1217                 struct ibv_send_wr *bad_wr;
1218                 struct ibv_sge list = {
1219                         .addr   = (uintptr_t)buf,
1220                         .length = len,
1221                         .lkey   = pconn->mr_send->lkey
1222                 };
1223                 struct ibv_send_wr wr = {
1224                         .wr_id      = p->wr_id + pctx->opts.max_recv_wr,
1225                         .sg_list    = &list,
1226                         .num_sge    = 1,
1227                         .opcode     = IBV_WR_SEND,
1228                         .send_flags = IBV_SEND_SIGNALED,
1229                 };
1230
1231                 if (p->buf_large==NULL) {
1232                         DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1233                                 pconn->cm_id, (uint32_t)wr.wr_id, len));
1234                 } else {
1235                         DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1236                                 pconn->cm_id, (uint32_t)wr.wr_id, len));
1237                         list.lkey = p->mr_large->lkey;
1238                 }
1239
1240                 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1241                 if (rc) {
1242                         sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1243                                 rc, pconn->wr_sent);
1244                         goto error;
1245                 }
1246
1247                 pconn->wr_sent++;
1248
1249                 return rc;
1250         } /* else put the request into our own queue: */
1251
1252         DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1253
1254         /* TODO: clarify how to continue when state==IBWC_STOPPED */
1255
1256         /* to be sent by ibw_wc_send */
1257         /* regardless "normal" or [a part of] "large" packet */
1258         if (!p->queued_ref_cnt) {
1259                 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1260                         qprev, qnext); /* TODO: optimize */
1261                 p->queued_msg = buf;
1262         }
1263         p->queued_ref_cnt++;
1264         p->queued_rlen = len; /* last wins; see ibw_wc_send */
1265
1266         return 0;
1267 error:
1268         DEBUG(0, (ibw_lasterr));
1269         return -1;
1270 }
1271
1272 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1273 {
1274         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1275         struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1276         int     rc;
1277
1278         assert(len>=sizeof(uint32_t));
1279         assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1280
1281         if (len > pctx->opts.recv_bufsize) {
1282                 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1283                 int     rlen = len;
1284                 char    *packet = (char *)buf;
1285                 uint32_t        recv_bufsize = pctx->opts.recv_bufsize;
1286
1287                 DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1288                         pconn->cm_id, buf, len));
1289
1290                 /* single threaded => no race here: */
1291                 assert(p->ref_cnt==0);
1292                 while(rlen > recv_bufsize) {
1293                         rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1294                         if (rc)
1295                                 return rc;
1296                         packet += recv_bufsize;
1297                         rlen -= recv_bufsize;
1298                         p->ref_cnt++; /* not good to have it in ibw_send_packet */
1299                 }
1300                 if (rlen) {
1301                         rc = ibw_send_packet(conn, packet, p, rlen);
1302                         p->ref_cnt++; /* not good to have it in ibw_send_packet */
1303                 }
1304                 p->ref_cnt--; /* for the same handling */
1305         } else {
1306                 assert(p->ref_cnt==0);
1307                 assert(p->queued_ref_cnt==0);
1308
1309                 rc = ibw_send_packet(conn, buf, p, len);
1310         }
1311         return rc;
1312 }
1313
1314 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1315 {
1316         struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1317         struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1318         struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1319
1320         assert(p!=NULL);
1321         assert(buf!=NULL);
1322         assert(conn!=NULL);
1323
1324         if (p->buf_large!=NULL)
1325                 ibw_free_mr(&p->buf_large, &p->mr_large);
1326
1327         /* parallel case */
1328         if (p->wr_id < pctx->opts.max_send_wr) {
1329                 DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1330                 DLIST_REMOVE(pconn->wr_list_used, p);
1331                 DLIST_ADD(pconn->wr_list_avail, p);
1332         } else { /* "extra" packet */
1333                 DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1334                 DLIST_REMOVE(pconn->extra_sent, p);
1335                 DLIST_ADD(pconn->extra_avail, p);
1336         }
1337
1338         return 0;
1339 }
1340
1341 const char *ibw_getLastError(void)
1342 {
1343         return ibw_lasterr;
1344 }