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