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