2 * Unix SMB/CIFS implementation.
3 * Wrap Infiniband calls.
5 * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
7 * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
30 #include "lib/util/dlinklist.h"
31 #include "lib/util/debug.h"
33 #include "common/logging.h"
35 #include <infiniband/kern-abi.h>
36 #include <rdma/rdma_cma_abi.h>
37 #include <rdma/rdma_cma.h>
39 #include "ibwrapper.h"
40 #include "ibwrapper_internal.h"
42 #define IBW_LASTERR_BUFSIZE 512
43 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
45 #define IBW_MAX_SEND_WR 16
46 #define IBW_MAX_RECV_WR 16
47 #define IBW_RECV_BUFSIZE 64
48 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
50 static void ibw_event_handler_verbs(struct tevent_context *ev,
51 struct tevent_fd *fde, uint16_t flags, void *private_data);
52 static int ibw_fill_cq(struct ibw_conn *conn);
53 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
54 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
55 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
57 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
58 uint32_t n, struct ibv_mr **ppmr)
62 DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
63 buf = memalign(pctx->pagesize, n);
65 sprintf(ibw_lasterr, "couldn't allocate memory\n");
69 DEBUG(DEBUG_DEBUG, ("2 ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
70 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
72 sprintf(ibw_lasterr, "couldn't allocate mr\n");
77 DEBUG(DEBUG_DEBUG, ("3 ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
81 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
83 DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
94 static int ibw_init_memory(struct ibw_conn *conn)
96 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
97 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
98 struct ibw_opts *opts = &pctx->opts;
102 DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
103 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
104 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
105 if (!pconn->buf_send) {
106 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
110 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
111 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
112 if (!pconn->buf_send) {
113 sprintf(ibw_lasterr, "METZEcouldn't allocate work send buf\n");
117 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
118 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
119 if (!pconn->buf_recv) {
120 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
124 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
125 assert(pconn->wr_index!=NULL);
127 for(i=0; i<opts->max_send_wr; i++) {
128 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
129 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
132 DLIST_ADD(pconn->wr_list_avail, p);
138 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
140 DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
143 * tevent_fd must be removed before the fd is closed
145 TALLOC_FREE(pctx->cm_channel_event);
148 if (pctx->cm_channel) {
149 rdma_destroy_event_channel(pctx->cm_channel);
150 pctx->cm_channel = NULL;
153 rdma_destroy_id(pctx->cm_id);
160 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
162 DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
166 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
168 DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
169 pconn, pconn->cm_id));
171 /* pconn->wr_index is freed by talloc */
172 /* pconn->wr_index[i] are freed by talloc */
175 * tevent_fd must be removed before the fd is closed
177 TALLOC_FREE(pconn->verbs_channel_event);
180 if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
181 rdma_destroy_qp(pconn->cm_id);
182 pconn->cm_id->qp = NULL;
185 if (pconn->cq!=NULL) {
186 ibv_destroy_cq(pconn->cq);
190 if (pconn->verbs_channel!=NULL) {
191 ibv_destroy_comp_channel(pconn->verbs_channel);
192 pconn->verbs_channel = NULL;
195 /* free memory regions */
196 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
197 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
200 ibv_dealloc_pd(pconn->pd);
202 DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
206 rdma_destroy_id(pconn->cm_id);
208 DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
214 static int ibw_wr_destruct(struct ibw_wr *wr)
216 if (wr->buf_large!=NULL)
217 ibw_free_mr(&wr->buf_large, &wr->mr_large);
221 static int ibw_conn_destruct(struct ibw_conn *conn)
223 DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
225 /* important here: ctx is a talloc _parent_ */
226 DLIST_REMOVE(conn->ctx->conn_list, conn);
230 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
232 struct ibw_conn *conn;
233 struct ibw_conn_priv *pconn;
237 conn = talloc_zero(mem_ctx, struct ibw_conn);
239 talloc_set_destructor(conn, ibw_conn_destruct);
241 pconn = talloc_zero(conn, struct ibw_conn_priv);
243 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
246 conn->internal = (void *)pconn;
248 DLIST_ADD(ctx->conn_list, conn);
253 static int ibw_setup_cq_qp(struct ibw_conn *conn)
255 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
256 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
257 struct ibv_qp_init_attr init_attr;
258 struct ibv_qp_attr attr;
261 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
264 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
265 if (!pconn->verbs_channel) {
266 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
269 DEBUG(DEBUG_DEBUG, ("created channel %p\n", pconn->verbs_channel));
271 pconn->verbs_channel_event = tevent_add_fd(pctx->ectx, NULL, /* not pconn or conn */
272 pconn->verbs_channel->fd, TEVENT_FD_READ, ibw_event_handler_verbs, conn);
274 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
276 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
279 DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
282 if (ibw_init_memory(conn))
286 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
287 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
288 conn, pconn->verbs_channel, 0);
289 if (pconn->cq==NULL) {
290 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
294 rc = ibv_req_notify_cq(pconn->cq, 0);
296 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
301 memset(&init_attr, 0, sizeof(init_attr));
302 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
303 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
304 init_attr.cap.max_recv_sge = 1;
305 init_attr.cap.max_send_sge = 1;
306 init_attr.qp_type = IBV_QPT_RC;
307 init_attr.send_cq = pconn->cq;
308 init_attr.recv_cq = pconn->cq;
310 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
312 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
315 /* elase result is in pconn->cm_id->qp */
317 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
319 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
323 return ibw_fill_cq(conn);
326 static int ibw_refill_cq_recv(struct ibw_conn *conn)
328 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
329 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
331 struct ibv_sge list = {
332 .addr = (uintptr_t) NULL, /* filled below */
333 .length = pctx->opts.recv_bufsize,
334 .lkey = pconn->mr_recv->lkey /* always the same */
336 struct ibv_recv_wr wr = {
337 .wr_id = 0, /* filled below */
341 struct ibv_recv_wr *bad_wr;
343 DEBUG(DEBUG_DEBUG, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
345 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
346 wr.wr_id = pconn->recv_index;
347 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
349 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
351 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
352 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
359 static int ibw_fill_cq(struct ibw_conn *conn)
361 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
362 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
364 struct ibv_sge list = {
365 .addr = (uintptr_t) NULL, /* filled below */
366 .length = pctx->opts.recv_bufsize,
367 .lkey = pconn->mr_recv->lkey /* always the same */
369 struct ibv_recv_wr wr = {
370 .wr_id = 0, /* filled below */
374 struct ibv_recv_wr *bad_wr;
376 DEBUG(DEBUG_DEBUG, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
378 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
379 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
380 wr.wr_id = pconn->recv_index;
381 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
383 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
385 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
386 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
394 static int ibw_manage_connect(struct ibw_conn *conn)
396 struct rdma_conn_param conn_param;
397 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
400 DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
402 if (ibw_setup_cq_qp(conn))
406 memset(&conn_param, 0, sizeof conn_param);
407 conn_param.responder_resources = 1;
408 conn_param.initiator_depth = 1;
409 conn_param.retry_count = 10;
411 rc = rdma_connect(pconn->cm_id, &conn_param);
413 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
418 static void ibw_event_handler_cm(struct tevent_context *ev,
419 struct tevent_fd *fde, uint16_t flags, void *private_data)
422 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
423 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
424 struct ibw_conn *conn = NULL;
425 struct ibw_conn_priv *pconn = NULL;
426 struct rdma_cm_id *cma_id = NULL;
427 struct rdma_cm_event *event = NULL;
431 rc = rdma_get_cm_event(pctx->cm_channel, &event);
433 ctx->state = IBWS_ERROR;
435 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
440 DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
441 (cma_id == pctx->cm_id) ? "parent" : "child"));
443 switch (event->event) {
444 case RDMA_CM_EVENT_ADDR_RESOLVED:
445 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
446 /* continuing from ibw_connect ... */
447 rc = rdma_resolve_route(cma_id, 2000);
449 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
452 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
455 case RDMA_CM_EVENT_ROUTE_RESOLVED:
456 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
457 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
458 assert(cma_id->context!=NULL);
459 conn = talloc_get_type(cma_id->context, struct ibw_conn);
461 rc = ibw_manage_connect(conn);
467 case RDMA_CM_EVENT_CONNECT_REQUEST:
468 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
469 ctx->state = IBWS_CONNECT_REQUEST;
470 conn = ibw_conn_new(ctx, ctx);
471 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
472 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
473 cma_id->context = (void *)conn;
474 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
476 if (ibw_setup_cq_qp(conn))
479 conn->state = IBWC_INIT;
480 pctx->connstate_func(ctx, conn);
482 /* continued at ibw_accept when invoked by the func above */
483 if (!pconn->is_accepted) {
484 rc = rdma_reject(cma_id, NULL, 0);
486 DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
488 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
491 /* TODO: clarify whether if it's needed by upper layer: */
492 ctx->state = IBWS_READY;
493 pctx->connstate_func(ctx, NULL);
495 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
498 case RDMA_CM_EVENT_ESTABLISHED:
499 /* expected after ibw_accept and ibw_connect[not directly] */
500 DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
501 conn = talloc_get_type(cma_id->context, struct ibw_conn);
502 assert(conn!=NULL); /* important assumption */
504 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
506 /* client conn is up */
507 conn->state = IBWC_CONNECTED;
509 /* both ctx and conn have changed */
510 pctx->connstate_func(ctx, conn);
513 case RDMA_CM_EVENT_ADDR_ERROR:
514 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
516 case RDMA_CM_EVENT_ROUTE_ERROR:
517 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
519 case RDMA_CM_EVENT_CONNECT_ERROR:
520 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
522 case RDMA_CM_EVENT_UNREACHABLE:
523 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
525 case RDMA_CM_EVENT_REJECTED:
526 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
527 DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
528 conn = talloc_get_type(cma_id->context, struct ibw_conn);
530 /* must be done BEFORE connstate */
531 if ((rc=rdma_ack_cm_event(event)))
532 DEBUG(DEBUG_ERR, ("reject/rdma_ack_cm_event failed with %d\n", rc));
533 event = NULL; /* not to touch cma_id or conn */
534 conn->state = IBWC_ERROR;
535 /* it should free the conn */
536 pctx->connstate_func(NULL, conn);
538 break; /* this is not strictly an error */
540 case RDMA_CM_EVENT_DISCONNECTED:
541 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
542 if ((rc=rdma_ack_cm_event(event)))
543 DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
544 event = NULL; /* don't ack more */
546 if (cma_id!=pctx->cm_id) {
547 DEBUG(DEBUG_ERR, ("client DISCONNECT event cm_id=%p\n", cma_id));
548 conn = talloc_get_type(cma_id->context, struct ibw_conn);
549 conn->state = IBWC_DISCONNECTED;
550 pctx->connstate_func(NULL, conn);
554 case RDMA_CM_EVENT_DEVICE_REMOVAL:
555 sprintf(ibw_lasterr, "cma detected device removal!\n");
559 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
563 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
564 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
570 DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
573 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
574 conn = talloc_get_type(cma_id->context, struct ibw_conn);
576 conn->state = IBWC_ERROR;
577 pctx->connstate_func(NULL, conn);
580 ctx->state = IBWS_ERROR;
581 pctx->connstate_func(ctx, NULL);
584 if ((rc=rdma_ack_cm_event(event))!=0) {
585 DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
592 static void ibw_event_handler_verbs(struct tevent_context *ev,
593 struct tevent_fd *fde, uint16_t flags, void *private_data)
595 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
596 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
597 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
601 struct ibv_cq *ev_cq;
604 DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
606 /* TODO: check whether if it's good to have more channels here... */
607 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
609 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
612 if (ev_cq != pconn->cq) {
613 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
616 rc = ibv_req_notify_cq(pconn->cq, 0);
618 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
622 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
624 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
625 wc.status, wc.opcode, rc);
631 DEBUG(DEBUG_DEBUG, ("send completion\n"));
632 if (ibw_wc_send(conn, &wc))
636 case IBV_WC_RDMA_WRITE:
637 DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
640 case IBV_WC_RDMA_READ:
641 DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
645 DEBUG(DEBUG_DEBUG, ("recv completion\n"));
646 if (ibw_wc_recv(conn, &wc))
651 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
656 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
660 ibv_ack_cq_events(pconn->cq, 1);
664 ibv_ack_cq_events(pconn->cq, 1);
666 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
668 if (conn->state!=IBWC_ERROR) {
669 conn->state = IBWC_ERROR;
670 pctx->connstate_func(NULL, conn);
674 static int ibw_process_queue(struct ibw_conn *conn)
676 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
677 struct ibw_ctx_priv *pctx;
682 if (pconn->queue==NULL)
687 /* we must have at least 1 fragment to send */
688 assert(p->queued_ref_cnt>0);
691 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
692 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
694 assert(p->queued_msg!=NULL);
697 DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
698 p->queued_ref_cnt, msg_size));
700 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
702 /* was this the last fragment? */
703 if (p->queued_ref_cnt) {
704 p->queued_msg += pctx->opts.recv_bufsize;
706 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
707 p->queued_msg = NULL;
713 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
715 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
716 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
720 DEBUG(DEBUG_DEBUG, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
721 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
723 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
724 assert(wc->wr_id >= pctx->opts.max_recv_wr);
725 send_index = wc->wr_id - pctx->opts.max_recv_wr;
728 if (send_index < pctx->opts.max_send_wr) {
729 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
730 p = pconn->wr_index[send_index];
731 if (p->buf_large!=NULL) {
733 /* awaiting more of it... */
736 ibw_free_mr(&p->buf_large, &p->mr_large);
737 DLIST_REMOVE(pconn->wr_list_used, p);
738 DLIST_ADD(pconn->wr_list_avail, p);
740 } else { /* nasty - but necessary */
741 DLIST_REMOVE(pconn->wr_list_used, p);
742 DLIST_ADD(pconn->wr_list_avail, p);
744 } else { /* "extra" request - not optimized */
745 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
746 for(p=pconn->extra_sent; p!=NULL; p=p->next)
747 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
750 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
756 ibw_free_mr(&p->buf_large, &p->mr_large);
757 DLIST_REMOVE(pconn->extra_sent, p);
758 DLIST_ADD(pconn->extra_avail, p);
762 return ibw_process_queue(conn);
765 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
766 struct ibw_part *part, char **pp, uint32_t add_len, int info)
768 DEBUG(DEBUG_DEBUG, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
769 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
771 /* allocate more if necessary - it's an "evergrowing" buffer... */
772 if (part->len + add_len > part->bufsize) {
773 if (part->buf==NULL) {
774 assert(part->len==0);
775 part->buf = talloc_size(pconn, add_len);
776 if (part->buf==NULL) {
777 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
781 part->bufsize = add_len;
783 part->buf = talloc_realloc_size(pconn,
784 part->buf, part->len + add_len);
785 if (part->buf==NULL) {
786 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
787 part->len, add_len, info);
791 part->bufsize = part->len + add_len;
795 memcpy(part->buf + part->len, *pp, add_len);
797 part->len += add_len;
798 part->to_read -= add_len;
803 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
804 struct ibw_part *part, uint32_t threshold)
806 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
807 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
809 if (part->bufsize > threshold) {
810 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
811 pconn->cm_id, part->bufsize, threshold));
812 talloc_free(part->buf);
813 part->buf = talloc_size(pconn, threshold);
814 if (part->buf==NULL) {
815 sprintf(ibw_lasterr, "talloc_size failed\n");
818 part->bufsize = threshold;
823 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
825 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
826 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
827 struct ibw_part *part = &pconn->part;
829 uint32_t remain = wc->byte_len;
831 DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
832 pconn->cm_id, (uint32_t)wc->wr_id, remain));
834 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
835 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
836 assert(wc->byte_len <= pctx->opts.recv_bufsize);
838 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
841 /* here always true: (part->len!=0 && part->to_read!=0) ||
842 (part->len==0 && part->to_read==0) */
843 if (part->len) { /* is there a partial msg to be continued? */
844 int read_len = (part->to_read<=remain) ? part->to_read : remain;
845 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
849 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
850 assert(part->len==sizeof(uint32_t));
851 /* set it again now... */
852 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
853 if (part->to_read<sizeof(uint32_t)) {
854 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
857 part->to_read -= sizeof(uint32_t); /* it's already read */
860 if (part->to_read==0) {
861 if (pctx->receive_func(conn, part->buf, part->len) != 0) {
864 part->len = 0; /* tells not having partial data (any more) */
865 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
869 if (remain>=sizeof(uint32_t)) {
870 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
871 if (msglen<sizeof(uint32_t)) {
872 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
876 /* mostly awaited case: */
877 if (msglen<=remain) {
878 if (pctx->receive_func(conn, p, msglen) != 0) {
884 part->to_read = msglen;
885 /* part->len is already 0 */
886 if (ibw_append_to_part(pconn, part, &p, remain, 422))
888 remain = 0; /* to be continued ... */
889 /* part->to_read > 0 here */
891 } else { /* edge case: */
892 part->to_read = sizeof(uint32_t);
893 /* part->len is already 0 */
894 if (ibw_append_to_part(pconn, part, &p, remain, 423))
897 /* part->to_read > 0 here */
900 } /* <remain> is always decreased at least by 1 */
902 if (ibw_refill_cq_recv(conn))
908 DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
912 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
915 const char *name, *value;
917 DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
919 opts->max_send_wr = IBW_MAX_SEND_WR;
920 opts->max_recv_wr = IBW_MAX_RECV_WR;
921 opts->recv_bufsize = IBW_RECV_BUFSIZE;
922 opts->recv_threshold = IBW_RECV_THRESHOLD;
924 for(i=0; i<nattr; i++) {
926 value = attr[i].value;
928 assert(name!=NULL && value!=NULL);
929 if (strcmp(name, "max_send_wr")==0)
930 opts->max_send_wr = atoi(value);
931 else if (strcmp(name, "max_recv_wr")==0)
932 opts->max_recv_wr = atoi(value);
933 else if (strcmp(name, "recv_bufsize")==0)
934 opts->recv_bufsize = atoi(value);
935 else if (strcmp(name, "recv_threshold")==0)
936 opts->recv_threshold = atoi(value);
938 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
945 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
947 ibw_connstate_fn_t ibw_connstate,
948 ibw_receive_fn_t ibw_receive,
949 struct tevent_context *ectx)
951 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
952 struct ibw_ctx_priv *pctx;
955 DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
957 /* initialize basic data structures */
958 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
961 ibw_lasterr[0] = '\0';
962 talloc_set_destructor(ctx, ibw_ctx_destruct);
963 ctx->ctx_userdata = ctx_userdata;
965 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
966 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
967 ctx->internal = (void *)pctx;
970 pctx->connstate_func = ibw_connstate;
971 pctx->receive_func = ibw_receive;
975 /* process attributes */
976 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
980 pctx->cm_channel = rdma_create_event_channel();
981 if (!pctx->cm_channel) {
982 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
986 pctx->cm_channel_event = tevent_add_fd(pctx->ectx, pctx,
987 pctx->cm_channel->fd, TEVENT_FD_READ, ibw_event_handler_cm, ctx);
989 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
990 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
992 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
996 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
999 DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
1001 pctx->pagesize = sysconf(_SC_PAGESIZE);
1004 /* don't put code here */
1006 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1014 int ibw_stop(struct ibw_ctx *ctx)
1016 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1019 DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
1021 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1022 if (p->state==IBWC_ERROR || p->state==IBWC_CONNECTED) {
1023 if (ibw_disconnect(p))
1028 ctx->state = IBWS_STOPPED;
1029 pctx->connstate_func(ctx, NULL);
1034 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1036 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1039 DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
1040 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1041 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1043 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1044 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1047 DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
1052 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1054 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1057 DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
1058 rc = rdma_listen(pctx->cm_id, backlog);
1060 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1061 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1068 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1070 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1071 struct rdma_conn_param conn_param;
1074 DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1075 conn->conn_userdata = conn_userdata;
1077 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1078 conn_param.responder_resources = 1;
1079 conn_param.initiator_depth = 1;
1080 rc = rdma_accept(pconn->cm_id, &conn_param);
1082 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1083 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1087 pconn->is_accepted = 1;
1089 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1094 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1096 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1097 struct ibw_conn_priv *pconn = NULL;
1102 conn->conn_userdata = conn_userdata;
1103 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1104 DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1105 ntohs(serv_addr->sin_port)));
1107 /* clean previous - probably half - initialization */
1108 if (ibw_conn_priv_destruct(pconn)) {
1109 DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1114 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1115 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1117 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1121 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1125 DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1127 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1129 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1130 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1135 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1140 int ibw_disconnect(struct ibw_conn *conn)
1143 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1145 DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1147 assert(pconn!=NULL);
1149 switch(conn->state) {
1151 ibw_conn_priv_destruct(pconn); /* do this here right now */
1153 case IBWC_CONNECTED:
1154 rc = rdma_disconnect(pconn->cm_id);
1156 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1157 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1162 DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
1169 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1171 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1172 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1173 struct ibw_wr *p = pconn->wr_list_avail;
1176 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1178 DLIST_REMOVE(pconn->wr_list_avail, p);
1179 DLIST_ADD(pconn->wr_list_used, p);
1181 if (len <= pctx->opts.recv_bufsize) {
1182 *buf = (void *)p->buf;
1184 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1185 if (p->buf_large==NULL) {
1186 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1189 *buf = (void *)p->buf_large;
1191 /* p->wr_id is already filled in ibw_init_memory */
1193 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1195 p = pconn->extra_avail;
1197 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1198 talloc_set_destructor(p, ibw_wr_destruct);
1200 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1203 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1205 switch(pconn->extra_max) {
1206 case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
1207 case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
1208 case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
1209 case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
1214 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1215 if (p->buf_large==NULL) {
1216 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1219 *buf = (void *)p->buf_large;
1221 DLIST_REMOVE(pconn->extra_avail, p);
1222 /* we don't have prepared index for this, so that
1223 * we will have to find this by wr_id later on */
1224 DLIST_ADD(pconn->extra_sent, p);
1231 DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1236 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1238 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1239 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1242 /* can we send it right now? */
1243 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1244 struct ibv_send_wr *bad_wr;
1245 struct ibv_sge list = {
1246 .addr = (uintptr_t)buf,
1248 .lkey = pconn->mr_send->lkey
1250 struct ibv_send_wr wr = {
1251 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1254 .opcode = IBV_WR_SEND,
1255 .send_flags = IBV_SEND_SIGNALED,
1258 if (p->buf_large==NULL) {
1259 DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1260 pconn->cm_id, (uint32_t)wr.wr_id, len));
1262 DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1263 pconn->cm_id, (uint32_t)wr.wr_id, len));
1264 list.lkey = p->mr_large->lkey;
1267 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1269 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1270 rc, pconn->wr_sent);
1277 } /* else put the request into our own queue: */
1279 DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1281 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1283 /* to be sent by ibw_wc_send */
1284 /* regardless "normal" or [a part of] "large" packet */
1285 if (!p->queued_ref_cnt) {
1286 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1287 qprev, qnext); /* TODO: optimize */
1288 p->queued_msg = buf;
1290 p->queued_ref_cnt++;
1291 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1295 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1299 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1301 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1302 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1305 assert(len>=sizeof(uint32_t));
1306 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1308 if (len > pctx->opts.recv_bufsize) {
1309 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1311 char *packet = (char *)buf;
1312 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1314 DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1315 pconn->cm_id, buf, len));
1317 /* single threaded => no race here: */
1318 assert(p->ref_cnt==0);
1319 while(rlen > recv_bufsize) {
1320 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1323 packet += recv_bufsize;
1324 rlen -= recv_bufsize;
1325 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1328 rc = ibw_send_packet(conn, packet, p, rlen);
1329 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1331 p->ref_cnt--; /* for the same handling */
1333 assert(p->ref_cnt==0);
1334 assert(p->queued_ref_cnt==0);
1336 rc = ibw_send_packet(conn, buf, p, len);
1341 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1343 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1344 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1345 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1351 if (p->buf_large!=NULL)
1352 ibw_free_mr(&p->buf_large, &p->mr_large);
1355 if (p->wr_id < pctx->opts.max_send_wr) {
1356 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1357 DLIST_REMOVE(pconn->wr_list_used, p);
1358 DLIST_ADD(pconn->wr_list_avail, p);
1359 } else { /* "extra" packet */
1360 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1361 DLIST_REMOVE(pconn->extra_sent, p);
1362 DLIST_ADD(pconn->extra_avail, p);
1368 const char *ibw_getLastError(void)