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/>.
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
31 #include <arpa/inet.h>
37 #include "lib/events/events.h"
38 #include "ibwrapper.h"
40 #include <infiniband/kern-abi.h>
41 #include <rdma/rdma_cma_abi.h>
42 #include <rdma/rdma_cma.h>
44 #include "ibwrapper_internal.h"
45 #include "lib/util/dlinklist.h"
47 #define IBW_LASTERR_BUFSIZE 512
48 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
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)
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);
62 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
63 uint32_t n, struct ibv_mr **ppmr)
67 DEBUG(10, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
68 buf = memalign(pctx->pagesize, n);
70 sprintf(ibw_lasterr, "couldn't allocate memory\n");
74 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
76 sprintf(ibw_lasterr, "couldn't allocate mr\n");
84 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
86 DEBUG(10, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
97 static int ibw_init_memory(struct ibw_conn *conn)
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;
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");
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");
120 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
121 assert(pconn->wr_index!=NULL);
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);
128 DLIST_ADD(pconn->wr_list_avail, p);
134 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
136 DEBUG(10, ("ibw_ctx_priv_destruct(%p)\n", pctx));
139 if (pctx->cm_channel) {
140 rdma_destroy_event_channel(pctx->cm_channel);
141 pctx->cm_channel = NULL;
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;
149 rdma_destroy_id(pctx->cm_id);
156 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
158 DEBUG(10, ("ibw_ctx_destruct(%p)\n", ctx));
162 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
164 DEBUG(10, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
165 pconn, pconn->cm_id));
167 /* pconn->wr_index is freed by talloc */
168 /* pconn->wr_index[i] are freed by talloc */
171 if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
172 rdma_destroy_qp(pconn->cm_id);
173 pconn->cm_id->qp = NULL;
176 if (pconn->cq!=NULL) {
177 ibv_destroy_cq(pconn->cq);
181 if (pconn->verbs_channel!=NULL) {
182 ibv_destroy_comp_channel(pconn->verbs_channel);
183 pconn->verbs_channel = NULL;
186 /* must be freed here because its order is important */
187 if (pconn->verbs_channel_event) {
188 talloc_free(pconn->verbs_channel_event);
189 pconn->verbs_channel_event = NULL;
192 /* free memory regions */
193 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
194 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
197 ibv_dealloc_pd(pconn->pd);
199 DEBUG(10, ("pconn=%p pd deallocated\n", pconn));
203 rdma_destroy_id(pconn->cm_id);
205 DEBUG(10, ("pconn=%p cm_id destroyed\n", pconn));
211 static int ibw_wr_destruct(struct ibw_wr *wr)
213 if (wr->buf_large!=NULL)
214 ibw_free_mr(&wr->buf_large, &wr->mr_large);
218 static int ibw_conn_destruct(struct ibw_conn *conn)
220 DEBUG(10, ("ibw_conn_destruct(%p)\n", conn));
222 /* important here: ctx is a talloc _parent_ */
223 DLIST_REMOVE(conn->ctx->conn_list, conn);
227 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
229 struct ibw_conn *conn;
230 struct ibw_conn_priv *pconn;
234 conn = talloc_zero(mem_ctx, struct ibw_conn);
236 talloc_set_destructor(conn, ibw_conn_destruct);
238 pconn = talloc_zero(conn, struct ibw_conn_priv);
240 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
243 conn->internal = (void *)pconn;
245 DLIST_ADD(ctx->conn_list, conn);
250 static int ibw_setup_cq_qp(struct ibw_conn *conn)
252 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
253 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
254 struct ibv_qp_init_attr init_attr;
255 struct ibv_qp_attr attr;
258 DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
261 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
262 if (!pconn->verbs_channel) {
263 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
266 DEBUG(10, ("created channel %p\n", pconn->verbs_channel));
268 pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
269 pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
271 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
273 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
276 DEBUG(10, ("created pd %p\n", pconn->pd));
279 if (ibw_init_memory(conn))
283 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
284 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
285 conn, pconn->verbs_channel, 0);
286 if (pconn->cq==NULL) {
287 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
291 rc = ibv_req_notify_cq(pconn->cq, 0);
293 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
298 memset(&init_attr, 0, sizeof(init_attr));
299 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
300 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
301 init_attr.cap.max_recv_sge = 1;
302 init_attr.cap.max_send_sge = 1;
303 init_attr.qp_type = IBV_QPT_RC;
304 init_attr.send_cq = pconn->cq;
305 init_attr.recv_cq = pconn->cq;
307 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
309 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
312 /* elase result is in pconn->cm_id->qp */
314 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
316 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
320 return ibw_fill_cq(conn);
323 static int ibw_refill_cq_recv(struct ibw_conn *conn)
325 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
326 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
328 struct ibv_sge list = {
329 .addr = (uintptr_t) NULL, /* filled below */
330 .length = pctx->opts.recv_bufsize,
331 .lkey = pconn->mr_recv->lkey /* always the same */
333 struct ibv_recv_wr wr = {
334 .wr_id = 0, /* filled below */
338 struct ibv_recv_wr *bad_wr;
340 DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
342 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
343 wr.wr_id = pconn->recv_index;
344 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
346 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
348 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
349 DEBUG(0, (ibw_lasterr));
356 static int ibw_fill_cq(struct ibw_conn *conn)
358 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
359 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
361 struct ibv_sge list = {
362 .addr = (uintptr_t) NULL, /* filled below */
363 .length = pctx->opts.recv_bufsize,
364 .lkey = pconn->mr_recv->lkey /* always the same */
366 struct ibv_recv_wr wr = {
367 .wr_id = 0, /* filled below */
371 struct ibv_recv_wr *bad_wr;
373 DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
375 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
376 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
377 wr.wr_id = pconn->recv_index;
378 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
380 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
382 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
383 DEBUG(0, (ibw_lasterr));
391 static int ibw_manage_connect(struct ibw_conn *conn)
393 struct rdma_conn_param conn_param;
394 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
397 DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
399 if (ibw_setup_cq_qp(conn))
403 memset(&conn_param, 0, sizeof conn_param);
404 conn_param.responder_resources = 1;
405 conn_param.initiator_depth = 1;
406 conn_param.retry_count = 10;
408 rc = rdma_connect(pconn->cm_id, &conn_param);
410 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
415 static void ibw_event_handler_cm(struct event_context *ev,
416 struct fd_event *fde, uint16_t flags, void *private_data)
419 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
420 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
421 struct ibw_conn *conn = NULL;
422 struct ibw_conn_priv *pconn = NULL;
423 struct rdma_cm_id *cma_id = NULL;
424 struct rdma_cm_event *event = NULL;
428 rc = rdma_get_cm_event(pctx->cm_channel, &event);
430 ctx->state = IBWS_ERROR;
432 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
437 DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
438 (cma_id == pctx->cm_id) ? "parent" : "child"));
440 switch (event->event) {
441 case RDMA_CM_EVENT_ADDR_RESOLVED:
442 DEBUG(11, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
443 /* continuing from ibw_connect ... */
444 rc = rdma_resolve_route(cma_id, 2000);
446 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
449 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
452 case RDMA_CM_EVENT_ROUTE_RESOLVED:
453 DEBUG(11, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
454 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
455 assert(cma_id->context!=NULL);
456 conn = talloc_get_type(cma_id->context, struct ibw_conn);
458 rc = ibw_manage_connect(conn);
464 case RDMA_CM_EVENT_CONNECT_REQUEST:
465 DEBUG(11, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
466 ctx->state = IBWS_CONNECT_REQUEST;
467 conn = ibw_conn_new(ctx, ctx);
468 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
469 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
470 cma_id->context = (void *)conn;
471 DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));
473 if (ibw_setup_cq_qp(conn))
476 conn->state = IBWC_INIT;
477 pctx->connstate_func(ctx, conn);
479 /* continued at ibw_accept when invoked by the func above */
480 if (!pconn->is_accepted) {
481 rc = rdma_reject(cma_id, NULL, 0);
483 DEBUG(0, ("rdma_reject failed with rc=%d\n", rc));
485 DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
488 /* TODO: clarify whether if it's needed by upper layer: */
489 ctx->state = IBWS_READY;
490 pctx->connstate_func(ctx, NULL);
492 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
495 case RDMA_CM_EVENT_ESTABLISHED:
496 /* expected after ibw_accept and ibw_connect[not directly] */
497 DEBUG(1, ("ESTABLISHED (conn: %p)\n", cma_id->context));
498 conn = talloc_get_type(cma_id->context, struct ibw_conn);
499 assert(conn!=NULL); /* important assumption */
501 DEBUG(10, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
503 /* client conn is up */
504 conn->state = IBWC_CONNECTED;
506 /* both ctx and conn have changed */
507 pctx->connstate_func(ctx, conn);
510 case RDMA_CM_EVENT_ADDR_ERROR:
511 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
512 case RDMA_CM_EVENT_ROUTE_ERROR:
513 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
514 case RDMA_CM_EVENT_CONNECT_ERROR:
515 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
516 case RDMA_CM_EVENT_UNREACHABLE:
517 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
519 case RDMA_CM_EVENT_REJECTED:
520 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
521 DEBUG(1, ("cm event handler: %s", ibw_lasterr));
522 conn = talloc_get_type(cma_id->context, struct ibw_conn);
524 /* must be done BEFORE connstate */
525 if ((rc=rdma_ack_cm_event(event)))
526 DEBUG(0, ("reject/rdma_ack_cm_event failed with %d\n", rc));
527 event = NULL; /* not to touch cma_id or conn */
528 conn->state = IBWC_ERROR;
529 /* it should free the conn */
530 pctx->connstate_func(NULL, conn);
532 break; /* this is not strictly an error */
534 case RDMA_CM_EVENT_DISCONNECTED:
535 DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
536 if ((rc=rdma_ack_cm_event(event)))
537 DEBUG(0, ("disc/rdma_ack_cm_event failed with %d\n", rc));
538 event = NULL; /* don't ack more */
540 if (cma_id!=pctx->cm_id) {
541 DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
542 conn = talloc_get_type(cma_id->context, struct ibw_conn);
543 conn->state = IBWC_DISCONNECTED;
544 pctx->connstate_func(NULL, conn);
548 case RDMA_CM_EVENT_DEVICE_REMOVAL:
549 sprintf(ibw_lasterr, "cma detected device removal!\n");
553 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
557 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
558 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
564 DEBUG(0, ("cm event handler: %s", ibw_lasterr));
567 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
568 conn = talloc_get_type(cma_id->context, struct ibw_conn);
570 conn->state = IBWC_ERROR;
571 pctx->connstate_func(NULL, conn);
574 ctx->state = IBWS_ERROR;
575 pctx->connstate_func(ctx, NULL);
578 if ((rc=rdma_ack_cm_event(event))!=0) {
579 DEBUG(0, ("rdma_ack_cm_event failed with %d\n", rc));
586 static void ibw_event_handler_verbs(struct event_context *ev,
587 struct fd_event *fde, uint16_t flags, void *private_data)
589 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
590 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
591 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
595 struct ibv_cq *ev_cq;
598 DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
600 /* TODO: check whether if it's good to have more channels here... */
601 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
603 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
606 if (ev_cq != pconn->cq) {
607 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
610 rc = ibv_req_notify_cq(pconn->cq, 0);
612 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
616 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
618 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
619 wc.status, wc.opcode, rc);
625 DEBUG(10, ("send completion\n"));
626 if (ibw_wc_send(conn, &wc))
630 case IBV_WC_RDMA_WRITE:
631 DEBUG(10, ("rdma write completion\n"));
634 case IBV_WC_RDMA_READ:
635 DEBUG(10, ("rdma read completion\n"));
639 DEBUG(10, ("recv completion\n"));
640 if (ibw_wc_recv(conn, &wc))
645 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
650 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
654 ibv_ack_cq_events(pconn->cq, 1);
658 ibv_ack_cq_events(pconn->cq, 1);
660 DEBUG(0, (ibw_lasterr));
662 if (conn->state!=IBWC_ERROR) {
663 conn->state = IBWC_ERROR;
664 pctx->connstate_func(NULL, conn);
668 static int ibw_process_queue(struct ibw_conn *conn)
670 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
671 struct ibw_ctx_priv *pctx;
676 if (pconn->queue==NULL)
681 /* we must have at least 1 fragment to send */
682 assert(p->queued_ref_cnt>0);
685 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
686 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
688 assert(p->queued_msg!=NULL);
691 DEBUG(10, ("ibw_process_queue refcnt=%d msgsize=%u\n",
692 p->queued_ref_cnt, msg_size));
694 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
696 /* was this the last fragment? */
697 if (p->queued_ref_cnt) {
698 p->queued_msg += pctx->opts.recv_bufsize;
700 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
701 p->queued_msg = NULL;
707 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
709 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
710 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
714 DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
715 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
717 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
718 assert(wc->wr_id >= pctx->opts.max_recv_wr);
719 send_index = wc->wr_id - pctx->opts.max_recv_wr;
722 if (send_index < pctx->opts.max_send_wr) {
723 DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
724 p = pconn->wr_index[send_index];
725 if (p->buf_large!=NULL) {
727 /* awaiting more of it... */
730 ibw_free_mr(&p->buf_large, &p->mr_large);
731 DLIST_REMOVE(pconn->wr_list_used, p);
732 DLIST_ADD(pconn->wr_list_avail, p);
734 } else { /* nasty - but necessary */
735 DLIST_REMOVE(pconn->wr_list_used, p);
736 DLIST_ADD(pconn->wr_list_avail, p);
738 } else { /* "extra" request - not optimized */
739 DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
740 for(p=pconn->extra_sent; p!=NULL; p=p->next)
741 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
744 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
750 ibw_free_mr(&p->buf_large, &p->mr_large);
751 DLIST_REMOVE(pconn->extra_sent, p);
752 DLIST_ADD(pconn->extra_avail, p);
756 return ibw_process_queue(conn);
759 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
760 struct ibw_part *part, char **pp, uint32_t add_len, int info)
762 DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
763 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
765 /* allocate more if necessary - it's an "evergrowing" buffer... */
766 if (part->len + add_len > part->bufsize) {
767 if (part->buf==NULL) {
768 assert(part->len==0);
769 part->buf = talloc_size(pconn, add_len);
770 if (part->buf==NULL) {
771 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
775 part->bufsize = add_len;
777 part->buf = talloc_realloc_size(pconn,
778 part->buf, part->len + add_len);
779 if (part->buf==NULL) {
780 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
781 part->len, add_len, info);
785 part->bufsize = part->len + add_len;
789 memcpy(part->buf + part->len, *pp, add_len);
791 part->len += add_len;
792 part->to_read -= add_len;
797 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
798 struct ibw_part *part, uint32_t threshold)
800 DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
801 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
803 if (part->bufsize > threshold) {
804 DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
805 pconn->cm_id, part->bufsize, threshold));
806 talloc_free(part->buf);
807 part->buf = talloc_size(pconn, threshold);
808 if (part->buf==NULL) {
809 sprintf(ibw_lasterr, "talloc_size failed\n");
812 part->bufsize = threshold;
817 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
819 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
820 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
821 struct ibw_part *part = &pconn->part;
823 uint32_t remain = wc->byte_len;
825 DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
826 pconn->cm_id, (uint32_t)wc->wr_id, remain));
828 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
829 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
830 assert(wc->byte_len <= pctx->opts.recv_bufsize);
832 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
835 /* here always true: (part->len!=0 && part->to_read!=0) ||
836 (part->len==0 && part->to_read==0) */
837 if (part->len) { /* is there a partial msg to be continued? */
838 int read_len = (part->to_read<=remain) ? part->to_read : remain;
839 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
843 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
844 assert(part->len==sizeof(uint32_t));
845 /* set it again now... */
846 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
847 if (part->to_read<sizeof(uint32_t)) {
848 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
851 part->to_read -= sizeof(uint32_t); /* it's already read */
854 if (part->to_read==0) {
855 pctx->receive_func(conn, part->buf, part->len);
856 part->len = 0; /* tells not having partial data (any more) */
857 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
861 if (remain>=sizeof(uint32_t)) {
862 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
863 if (msglen<sizeof(uint32_t)) {
864 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
868 /* mostly awaited case: */
869 if (msglen<=remain) {
870 pctx->receive_func(conn, p, msglen);
874 part->to_read = msglen;
875 /* part->len is already 0 */
876 if (ibw_append_to_part(pconn, part, &p, remain, 422))
878 remain = 0; /* to be continued ... */
879 /* part->to_read > 0 here */
881 } else { /* edge case: */
882 part->to_read = sizeof(uint32_t);
883 /* part->len is already 0 */
884 if (ibw_append_to_part(pconn, part, &p, remain, 423))
887 /* part->to_read > 0 here */
890 } /* <remain> is always decreased at least by 1 */
892 if (ibw_refill_cq_recv(conn))
898 DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
902 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
905 const char *name, *value;
907 DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
909 opts->max_send_wr = IBW_MAX_SEND_WR;
910 opts->max_recv_wr = IBW_MAX_RECV_WR;
911 opts->recv_bufsize = IBW_RECV_BUFSIZE;
912 opts->recv_threshold = IBW_RECV_THRESHOLD;
914 for(i=0; i<nattr; i++) {
916 value = attr[i].value;
918 assert(name!=NULL && value!=NULL);
919 if (strcmp(name, "max_send_wr")==0)
920 opts->max_send_wr = atoi(value);
921 else if (strcmp(name, "max_recv_wr")==0)
922 opts->max_recv_wr = atoi(value);
923 else if (strcmp(name, "recv_bufsize")==0)
924 opts->recv_bufsize = atoi(value);
925 else if (strcmp(name, "recv_threshold")==0)
926 opts->recv_threshold = atoi(value);
928 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
935 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
937 ibw_connstate_fn_t ibw_connstate,
938 ibw_receive_fn_t ibw_receive,
939 struct event_context *ectx)
941 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
942 struct ibw_ctx_priv *pctx;
945 DEBUG(10, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
947 /* initialize basic data structures */
948 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
951 ibw_lasterr[0] = '\0';
952 talloc_set_destructor(ctx, ibw_ctx_destruct);
953 ctx->ctx_userdata = ctx_userdata;
955 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
956 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
957 ctx->internal = (void *)pctx;
960 pctx->connstate_func = ibw_connstate;
961 pctx->receive_func = ibw_receive;
965 /* process attributes */
966 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
970 pctx->cm_channel = rdma_create_event_channel();
971 if (!pctx->cm_channel) {
972 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
976 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
977 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
979 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
980 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
982 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
986 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
989 DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
991 pctx->pagesize = sysconf(_SC_PAGESIZE);
994 /* don't put code here */
996 DEBUG(0, (ibw_lasterr));
1004 int ibw_stop(struct ibw_ctx *ctx)
1006 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1009 DEBUG(10, ("ibw_stop\n"));
1011 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1012 if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
1013 if (ibw_disconnect(p))
1018 ctx->state = IBWS_STOPPED;
1019 pctx->connstate_func(ctx, NULL);
1024 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1026 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1029 DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
1030 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1031 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1033 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1034 DEBUG(0, (ibw_lasterr));
1037 DEBUG(10, ("rdma_bind_addr successful\n"));
1042 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1044 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1047 DEBUG(10, ("ibw_listen\n"));
1048 rc = rdma_listen(pctx->cm_id, backlog);
1050 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1051 DEBUG(0, (ibw_lasterr));
1058 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1060 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1061 struct rdma_conn_param conn_param;
1064 DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1065 conn->conn_userdata = conn_userdata;
1067 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1068 conn_param.responder_resources = 1;
1069 conn_param.initiator_depth = 1;
1070 rc = rdma_accept(pconn->cm_id, &conn_param);
1072 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1073 DEBUG(0, (ibw_lasterr));
1077 pconn->is_accepted = 1;
1079 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1084 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1086 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1087 struct ibw_conn_priv *pconn = NULL;
1092 conn->conn_userdata = conn_userdata;
1093 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1094 DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1095 ntohs(serv_addr->sin_port)));
1097 /* clean previous - probably half - initialization */
1098 if (ibw_conn_priv_destruct(pconn)) {
1099 DEBUG(0, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1104 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1105 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1107 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1111 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1115 DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1117 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1119 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1120 DEBUG(0, (ibw_lasterr));
1125 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1130 int ibw_disconnect(struct ibw_conn *conn)
1133 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1135 DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1137 assert(pconn!=NULL);
1139 switch(conn->state) {
1141 ibw_conn_priv_destruct(pconn); /* do this here right now */
1143 case IBWC_CONNECTED:
1144 rc = rdma_disconnect(pconn->cm_id);
1146 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1147 DEBUG(0, (ibw_lasterr));
1152 DEBUG(9, ("invalid state for disconnect: %d\n", conn->state));
1159 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1161 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1162 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1163 struct ibw_wr *p = pconn->wr_list_avail;
1166 DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1168 DLIST_REMOVE(pconn->wr_list_avail, p);
1169 DLIST_ADD(pconn->wr_list_used, p);
1171 if (len <= pctx->opts.recv_bufsize) {
1172 *buf = (void *)p->buf;
1174 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1175 if (p->buf_large==NULL) {
1176 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1179 *buf = (void *)p->buf_large;
1181 /* p->wr_id is already filled in ibw_init_memory */
1183 DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1185 p = pconn->extra_avail;
1187 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1188 talloc_set_destructor(p, ibw_wr_destruct);
1190 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1193 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1195 switch(pconn->extra_max) {
1196 case 1: DEBUG(2, ("warning: queue performed\n")); break;
1197 case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
1198 case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
1199 case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
1204 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1205 if (p->buf_large==NULL) {
1206 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1209 *buf = (void *)p->buf_large;
1211 DLIST_REMOVE(pconn->extra_avail, p);
1212 /* we don't have prepared index for this, so that
1213 * we will have to find this by wr_id later on */
1214 DLIST_ADD(pconn->extra_sent, p);
1221 DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1226 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1228 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1229 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1232 /* can we send it right now? */
1233 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1234 struct ibv_send_wr *bad_wr;
1235 struct ibv_sge list = {
1236 .addr = (uintptr_t)buf,
1238 .lkey = pconn->mr_send->lkey
1240 struct ibv_send_wr wr = {
1241 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1244 .opcode = IBV_WR_SEND,
1245 .send_flags = IBV_SEND_SIGNALED,
1248 if (p->buf_large==NULL) {
1249 DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1250 pconn->cm_id, (uint32_t)wr.wr_id, len));
1252 DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1253 pconn->cm_id, (uint32_t)wr.wr_id, len));
1254 list.lkey = p->mr_large->lkey;
1257 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1259 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1260 rc, pconn->wr_sent);
1267 } /* else put the request into our own queue: */
1269 DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1271 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1273 /* to be sent by ibw_wc_send */
1274 /* regardless "normal" or [a part of] "large" packet */
1275 if (!p->queued_ref_cnt) {
1276 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1277 qprev, qnext); /* TODO: optimize */
1278 p->queued_msg = buf;
1280 p->queued_ref_cnt++;
1281 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1285 DEBUG(0, (ibw_lasterr));
1289 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1291 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1292 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1295 assert(len>=sizeof(uint32_t));
1296 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1298 if (len > pctx->opts.recv_bufsize) {
1299 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1301 char *packet = (char *)buf;
1302 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1304 DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1305 pconn->cm_id, buf, len));
1307 /* single threaded => no race here: */
1308 assert(p->ref_cnt==0);
1309 while(rlen > recv_bufsize) {
1310 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1313 packet += recv_bufsize;
1314 rlen -= recv_bufsize;
1315 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1318 rc = ibw_send_packet(conn, packet, p, rlen);
1319 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1321 p->ref_cnt--; /* for the same handling */
1323 assert(p->ref_cnt==0);
1324 assert(p->queued_ref_cnt==0);
1326 rc = ibw_send_packet(conn, buf, p, len);
1331 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1333 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1334 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1335 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1341 if (p->buf_large!=NULL)
1342 ibw_free_mr(&p->buf_large, &p->mr_large);
1345 if (p->wr_id < pctx->opts.max_send_wr) {
1346 DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1347 DLIST_REMOVE(pconn->wr_list_used, p);
1348 DLIST_ADD(pconn->wr_list_avail, p);
1349 } else { /* "extra" packet */
1350 DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1351 DLIST_REMOVE(pconn->extra_sent, p);
1352 DLIST_ADD(pconn->extra_avail, p);
1358 const char *ibw_getLastError(void)