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