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 "ibwrapper.h"
39 #include <infiniband/kern-abi.h>
40 #include <rdma/rdma_cma_abi.h>
41 #include <rdma/rdma_cma.h>
43 #include "ibwrapper_internal.h"
44 #include "lib/util/dlinklist.h"
46 #define IBW_LASTERR_BUFSIZE 512
47 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
49 #define IBW_MAX_SEND_WR 256
50 #define IBW_MAX_RECV_WR 1024
51 #define IBW_RECV_BUFSIZE 256
52 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
54 static void ibw_event_handler_verbs(struct event_context *ev,
55 struct fd_event *fde, uint16_t flags, void *private_data);
56 static int ibw_fill_cq(struct ibw_conn *conn);
57 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
58 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
59 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
61 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
62 uint32_t n, struct ibv_mr **ppmr)
66 DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
67 buf = memalign(pctx->pagesize, n);
69 sprintf(ibw_lasterr, "couldn't allocate memory\n");
73 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
75 sprintf(ibw_lasterr, "couldn't allocate mr\n");
83 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
85 DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
96 static int ibw_init_memory(struct ibw_conn *conn)
98 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
99 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
100 struct ibw_opts *opts = &pctx->opts;
104 DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
105 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
106 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
107 if (!pconn->buf_send) {
108 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
112 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
113 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
114 if (!pconn->buf_recv) {
115 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
119 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
120 assert(pconn->wr_index!=NULL);
122 for(i=0; i<opts->max_send_wr; i++) {
123 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
124 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
127 DLIST_ADD(pconn->wr_list_avail, p);
133 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
135 DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
138 * tevent_fd must be removed before the fd is closed
140 TALLOC_FREE(pctx->cm_channel_event);
143 if (pctx->cm_channel) {
144 rdma_destroy_event_channel(pctx->cm_channel);
145 pctx->cm_channel = NULL;
148 rdma_destroy_id(pctx->cm_id);
155 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
157 DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
161 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
163 DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
164 pconn, pconn->cm_id));
166 /* pconn->wr_index is freed by talloc */
167 /* pconn->wr_index[i] are freed by talloc */
170 * tevent_fd must be removed before the fd is closed
172 TALLOC_FREE(pconn->verbs_channel_event);
175 if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
176 rdma_destroy_qp(pconn->cm_id);
177 pconn->cm_id->qp = NULL;
180 if (pconn->cq!=NULL) {
181 ibv_destroy_cq(pconn->cq);
185 if (pconn->verbs_channel!=NULL) {
186 ibv_destroy_comp_channel(pconn->verbs_channel);
187 pconn->verbs_channel = NULL;
190 /* free memory regions */
191 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
192 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
195 ibv_dealloc_pd(pconn->pd);
197 DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
201 rdma_destroy_id(pconn->cm_id);
203 DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
209 static int ibw_wr_destruct(struct ibw_wr *wr)
211 if (wr->buf_large!=NULL)
212 ibw_free_mr(&wr->buf_large, &wr->mr_large);
216 static int ibw_conn_destruct(struct ibw_conn *conn)
218 DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
220 /* important here: ctx is a talloc _parent_ */
221 DLIST_REMOVE(conn->ctx->conn_list, conn);
225 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
227 struct ibw_conn *conn;
228 struct ibw_conn_priv *pconn;
232 conn = talloc_zero(mem_ctx, struct ibw_conn);
234 talloc_set_destructor(conn, ibw_conn_destruct);
236 pconn = talloc_zero(conn, struct ibw_conn_priv);
238 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
241 conn->internal = (void *)pconn;
243 DLIST_ADD(ctx->conn_list, conn);
248 static int ibw_setup_cq_qp(struct ibw_conn *conn)
250 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
251 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
252 struct ibv_qp_init_attr init_attr;
253 struct ibv_qp_attr attr;
256 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
259 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
260 if (!pconn->verbs_channel) {
261 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
264 DEBUG(DEBUG_DEBUG, ("created channel %p\n", pconn->verbs_channel));
266 pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
267 pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
269 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
271 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
274 DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
277 if (ibw_init_memory(conn))
281 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
282 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
283 conn, pconn->verbs_channel, 0);
284 if (pconn->cq==NULL) {
285 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
289 rc = ibv_req_notify_cq(pconn->cq, 0);
291 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
296 memset(&init_attr, 0, sizeof(init_attr));
297 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
298 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
299 init_attr.cap.max_recv_sge = 1;
300 init_attr.cap.max_send_sge = 1;
301 init_attr.qp_type = IBV_QPT_RC;
302 init_attr.send_cq = pconn->cq;
303 init_attr.recv_cq = pconn->cq;
305 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
307 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
310 /* elase result is in pconn->cm_id->qp */
312 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
314 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
318 return ibw_fill_cq(conn);
321 static int ibw_refill_cq_recv(struct ibw_conn *conn)
323 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
324 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
326 struct ibv_sge list = {
327 .addr = (uintptr_t) NULL, /* filled below */
328 .length = pctx->opts.recv_bufsize,
329 .lkey = pconn->mr_recv->lkey /* always the same */
331 struct ibv_recv_wr wr = {
332 .wr_id = 0, /* filled below */
336 struct ibv_recv_wr *bad_wr;
338 DEBUG(DEBUG_DEBUG, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
340 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
341 wr.wr_id = pconn->recv_index;
342 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
344 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
346 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
347 DEBUG(DEBUG_ERR, (ibw_lasterr));
354 static int ibw_fill_cq(struct ibw_conn *conn)
356 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
357 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
359 struct ibv_sge list = {
360 .addr = (uintptr_t) NULL, /* filled below */
361 .length = pctx->opts.recv_bufsize,
362 .lkey = pconn->mr_recv->lkey /* always the same */
364 struct ibv_recv_wr wr = {
365 .wr_id = 0, /* filled below */
369 struct ibv_recv_wr *bad_wr;
371 DEBUG(DEBUG_DEBUG, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
373 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
374 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
375 wr.wr_id = pconn->recv_index;
376 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
378 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
380 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
381 DEBUG(DEBUG_ERR, (ibw_lasterr));
389 static int ibw_manage_connect(struct ibw_conn *conn)
391 struct rdma_conn_param conn_param;
392 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
395 DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
397 if (ibw_setup_cq_qp(conn))
401 memset(&conn_param, 0, sizeof conn_param);
402 conn_param.responder_resources = 1;
403 conn_param.initiator_depth = 1;
404 conn_param.retry_count = 10;
406 rc = rdma_connect(pconn->cm_id, &conn_param);
408 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
413 static void ibw_event_handler_cm(struct event_context *ev,
414 struct fd_event *fde, uint16_t flags, void *private_data)
417 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
418 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
419 struct ibw_conn *conn = NULL;
420 struct ibw_conn_priv *pconn = NULL;
421 struct rdma_cm_id *cma_id = NULL;
422 struct rdma_cm_event *event = NULL;
426 rc = rdma_get_cm_event(pctx->cm_channel, &event);
428 ctx->state = IBWS_ERROR;
430 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
435 DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
436 (cma_id == pctx->cm_id) ? "parent" : "child"));
438 switch (event->event) {
439 case RDMA_CM_EVENT_ADDR_RESOLVED:
440 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
441 /* continuing from ibw_connect ... */
442 rc = rdma_resolve_route(cma_id, 2000);
444 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
447 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
450 case RDMA_CM_EVENT_ROUTE_RESOLVED:
451 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
452 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
453 assert(cma_id->context!=NULL);
454 conn = talloc_get_type(cma_id->context, struct ibw_conn);
456 rc = ibw_manage_connect(conn);
462 case RDMA_CM_EVENT_CONNECT_REQUEST:
463 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
464 ctx->state = IBWS_CONNECT_REQUEST;
465 conn = ibw_conn_new(ctx, ctx);
466 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
467 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
468 cma_id->context = (void *)conn;
469 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
471 if (ibw_setup_cq_qp(conn))
474 conn->state = IBWC_INIT;
475 pctx->connstate_func(ctx, conn);
477 /* continued at ibw_accept when invoked by the func above */
478 if (!pconn->is_accepted) {
479 rc = rdma_reject(cma_id, NULL, 0);
481 DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
483 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
486 /* TODO: clarify whether if it's needed by upper layer: */
487 ctx->state = IBWS_READY;
488 pctx->connstate_func(ctx, NULL);
490 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
493 case RDMA_CM_EVENT_ESTABLISHED:
494 /* expected after ibw_accept and ibw_connect[not directly] */
495 DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
496 conn = talloc_get_type(cma_id->context, struct ibw_conn);
497 assert(conn!=NULL); /* important assumption */
499 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
501 /* client conn is up */
502 conn->state = IBWC_CONNECTED;
504 /* both ctx and conn have changed */
505 pctx->connstate_func(ctx, conn);
508 case RDMA_CM_EVENT_ADDR_ERROR:
509 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
510 case RDMA_CM_EVENT_ROUTE_ERROR:
511 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
512 case RDMA_CM_EVENT_CONNECT_ERROR:
513 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
514 case RDMA_CM_EVENT_UNREACHABLE:
515 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
517 case RDMA_CM_EVENT_REJECTED:
518 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
519 DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
520 conn = talloc_get_type(cma_id->context, struct ibw_conn);
522 /* must be done BEFORE connstate */
523 if ((rc=rdma_ack_cm_event(event)))
524 DEBUG(DEBUG_ERR, ("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);
530 break; /* this is not strictly an error */
532 case RDMA_CM_EVENT_DISCONNECTED:
533 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
534 if ((rc=rdma_ack_cm_event(event)))
535 DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
536 event = NULL; /* don't ack more */
538 if (cma_id!=pctx->cm_id) {
539 DEBUG(DEBUG_ERR, ("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);
546 case RDMA_CM_EVENT_DEVICE_REMOVAL:
547 sprintf(ibw_lasterr, "cma detected device removal!\n");
551 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
555 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
556 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
562 DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
565 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
566 conn = talloc_get_type(cma_id->context, struct ibw_conn);
568 conn->state = IBWC_ERROR;
569 pctx->connstate_func(NULL, conn);
572 ctx->state = IBWS_ERROR;
573 pctx->connstate_func(ctx, NULL);
576 if ((rc=rdma_ack_cm_event(event))!=0) {
577 DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
584 static void ibw_event_handler_verbs(struct event_context *ev,
585 struct fd_event *fde, uint16_t flags, void *private_data)
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);
593 struct ibv_cq *ev_cq;
596 DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
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);
601 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
604 if (ev_cq != pconn->cq) {
605 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
608 rc = ibv_req_notify_cq(pconn->cq, 0);
610 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
614 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
616 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
617 wc.status, wc.opcode, rc);
623 DEBUG(DEBUG_DEBUG, ("send completion\n"));
624 if (ibw_wc_send(conn, &wc))
628 case IBV_WC_RDMA_WRITE:
629 DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
632 case IBV_WC_RDMA_READ:
633 DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
637 DEBUG(DEBUG_DEBUG, ("recv completion\n"));
638 if (ibw_wc_recv(conn, &wc))
643 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
648 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
652 ibv_ack_cq_events(pconn->cq, 1);
656 ibv_ack_cq_events(pconn->cq, 1);
658 DEBUG(DEBUG_ERR, (ibw_lasterr));
660 if (conn->state!=IBWC_ERROR) {
661 conn->state = IBWC_ERROR;
662 pctx->connstate_func(NULL, conn);
666 static int ibw_process_queue(struct ibw_conn *conn)
668 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
669 struct ibw_ctx_priv *pctx;
674 if (pconn->queue==NULL)
679 /* we must have at least 1 fragment to send */
680 assert(p->queued_ref_cnt>0);
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;
686 assert(p->queued_msg!=NULL);
689 DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
690 p->queued_ref_cnt, msg_size));
692 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
694 /* was this the last fragment? */
695 if (p->queued_ref_cnt) {
696 p->queued_msg += pctx->opts.recv_bufsize;
698 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
699 p->queued_msg = NULL;
705 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
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);
712 DEBUG(DEBUG_DEBUG, ("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));
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;
720 if (send_index < pctx->opts.max_send_wr) {
721 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
722 p = pconn->wr_index[send_index];
723 if (p->buf_large!=NULL) {
725 /* awaiting more of it... */
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);
732 } else { /* nasty - but necessary */
733 DLIST_REMOVE(pconn->wr_list_used, p);
734 DLIST_ADD(pconn->wr_list_avail, p);
736 } else { /* "extra" request - not optimized */
737 DEBUG(DEBUG_DEBUG, ("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)
742 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
748 ibw_free_mr(&p->buf_large, &p->mr_large);
749 DLIST_REMOVE(pconn->extra_sent, p);
750 DLIST_ADD(pconn->extra_avail, p);
754 return ibw_process_queue(conn);
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)
760 DEBUG(DEBUG_DEBUG, ("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));
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",
773 part->bufsize = add_len;
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);
783 part->bufsize = part->len + add_len;
787 memcpy(part->buf + part->len, *pp, add_len);
789 part->len += add_len;
790 part->to_read -= add_len;
795 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
796 struct ibw_part *part, uint32_t threshold)
798 DEBUG(DEBUG_DEBUG, ("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));
801 if (part->bufsize > threshold) {
802 DEBUG(DEBUG_DEBUG, ("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");
810 part->bufsize = threshold;
815 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
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;
821 uint32_t remain = wc->byte_len;
823 DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
824 pconn->cm_id, (uint32_t)wc->wr_id, remain));
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);
830 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
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))
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);
849 part->to_read -= sizeof(uint32_t); /* it's already read */
852 if (part->to_read==0) {
853 if (pctx->receive_func(conn, part->buf, part->len) != 0) {
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 if (pctx->receive_func(conn, p, msglen) != 0) {
876 part->to_read = msglen;
877 /* part->len is already 0 */
878 if (ibw_append_to_part(pconn, part, &p, remain, 422))
880 remain = 0; /* to be continued ... */
881 /* part->to_read > 0 here */
883 } else { /* edge case: */
884 part->to_read = sizeof(uint32_t);
885 /* part->len is already 0 */
886 if (ibw_append_to_part(pconn, part, &p, remain, 423))
889 /* part->to_read > 0 here */
892 } /* <remain> is always decreased at least by 1 */
894 if (ibw_refill_cq_recv(conn))
900 DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
904 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
907 const char *name, *value;
909 DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
911 opts->max_send_wr = IBW_MAX_SEND_WR;
912 opts->max_recv_wr = IBW_MAX_RECV_WR;
913 opts->recv_bufsize = IBW_RECV_BUFSIZE;
914 opts->recv_threshold = IBW_RECV_THRESHOLD;
916 for(i=0; i<nattr; i++) {
918 value = attr[i].value;
920 assert(name!=NULL && value!=NULL);
921 if (strcmp(name, "max_send_wr")==0)
922 opts->max_send_wr = atoi(value);
923 else if (strcmp(name, "max_recv_wr")==0)
924 opts->max_recv_wr = atoi(value);
925 else if (strcmp(name, "recv_bufsize")==0)
926 opts->recv_bufsize = atoi(value);
927 else if (strcmp(name, "recv_threshold")==0)
928 opts->recv_threshold = atoi(value);
930 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
937 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
939 ibw_connstate_fn_t ibw_connstate,
940 ibw_receive_fn_t ibw_receive,
941 struct event_context *ectx)
943 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
944 struct ibw_ctx_priv *pctx;
947 DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
949 /* initialize basic data structures */
950 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
953 ibw_lasterr[0] = '\0';
954 talloc_set_destructor(ctx, ibw_ctx_destruct);
955 ctx->ctx_userdata = ctx_userdata;
957 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
958 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
959 ctx->internal = (void *)pctx;
962 pctx->connstate_func = ibw_connstate;
963 pctx->receive_func = ibw_receive;
967 /* process attributes */
968 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
972 pctx->cm_channel = rdma_create_event_channel();
973 if (!pctx->cm_channel) {
974 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
978 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
979 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
981 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
982 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
984 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
988 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
991 DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
993 pctx->pagesize = sysconf(_SC_PAGESIZE);
996 /* don't put code here */
998 DEBUG(DEBUG_ERR, (ibw_lasterr));
1006 int ibw_stop(struct ibw_ctx *ctx)
1008 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1011 DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
1013 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1014 if (p->state==IBWC_ERROR || p->state==IBWC_CONNECTED) {
1015 if (ibw_disconnect(p))
1020 ctx->state = IBWS_STOPPED;
1021 pctx->connstate_func(ctx, NULL);
1026 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1028 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1031 DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
1032 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1033 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1035 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1036 DEBUG(DEBUG_ERR, (ibw_lasterr));
1039 DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
1044 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1046 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1049 DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
1050 rc = rdma_listen(pctx->cm_id, backlog);
1052 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1053 DEBUG(DEBUG_ERR, (ibw_lasterr));
1060 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1062 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1063 struct rdma_conn_param conn_param;
1066 DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1067 conn->conn_userdata = conn_userdata;
1069 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1070 conn_param.responder_resources = 1;
1071 conn_param.initiator_depth = 1;
1072 rc = rdma_accept(pconn->cm_id, &conn_param);
1074 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1075 DEBUG(DEBUG_ERR, (ibw_lasterr));
1079 pconn->is_accepted = 1;
1081 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1086 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1088 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1089 struct ibw_conn_priv *pconn = NULL;
1094 conn->conn_userdata = conn_userdata;
1095 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1096 DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1097 ntohs(serv_addr->sin_port)));
1099 /* clean previous - probably half - initialization */
1100 if (ibw_conn_priv_destruct(pconn)) {
1101 DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1106 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1107 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1109 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1113 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1117 DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1119 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1121 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1122 DEBUG(DEBUG_ERR, (ibw_lasterr));
1127 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1132 int ibw_disconnect(struct ibw_conn *conn)
1135 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1137 DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1139 assert(pconn!=NULL);
1141 switch(conn->state) {
1143 ibw_conn_priv_destruct(pconn); /* do this here right now */
1145 case IBWC_CONNECTED:
1146 rc = rdma_disconnect(pconn->cm_id);
1148 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1149 DEBUG(DEBUG_ERR, (ibw_lasterr));
1154 DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
1161 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1163 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1164 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1165 struct ibw_wr *p = pconn->wr_list_avail;
1168 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1170 DLIST_REMOVE(pconn->wr_list_avail, p);
1171 DLIST_ADD(pconn->wr_list_used, p);
1173 if (len <= pctx->opts.recv_bufsize) {
1174 *buf = (void *)p->buf;
1176 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1177 if (p->buf_large==NULL) {
1178 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1181 *buf = (void *)p->buf_large;
1183 /* p->wr_id is already filled in ibw_init_memory */
1185 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1187 p = pconn->extra_avail;
1189 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1190 talloc_set_destructor(p, ibw_wr_destruct);
1192 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1195 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1197 switch(pconn->extra_max) {
1198 case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
1199 case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
1200 case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
1201 case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
1206 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1207 if (p->buf_large==NULL) {
1208 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1211 *buf = (void *)p->buf_large;
1213 DLIST_REMOVE(pconn->extra_avail, p);
1214 /* we don't have prepared index for this, so that
1215 * we will have to find this by wr_id later on */
1216 DLIST_ADD(pconn->extra_sent, p);
1223 DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1228 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1230 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1231 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1234 /* can we send it right now? */
1235 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1236 struct ibv_send_wr *bad_wr;
1237 struct ibv_sge list = {
1238 .addr = (uintptr_t)buf,
1240 .lkey = pconn->mr_send->lkey
1242 struct ibv_send_wr wr = {
1243 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1246 .opcode = IBV_WR_SEND,
1247 .send_flags = IBV_SEND_SIGNALED,
1250 if (p->buf_large==NULL) {
1251 DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1252 pconn->cm_id, (uint32_t)wr.wr_id, len));
1254 DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1255 pconn->cm_id, (uint32_t)wr.wr_id, len));
1256 list.lkey = p->mr_large->lkey;
1259 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1261 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1262 rc, pconn->wr_sent);
1269 } /* else put the request into our own queue: */
1271 DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1273 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1275 /* to be sent by ibw_wc_send */
1276 /* regardless "normal" or [a part of] "large" packet */
1277 if (!p->queued_ref_cnt) {
1278 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1279 qprev, qnext); /* TODO: optimize */
1280 p->queued_msg = buf;
1282 p->queued_ref_cnt++;
1283 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1287 DEBUG(DEBUG_ERR, (ibw_lasterr));
1291 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1293 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1294 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1297 assert(len>=sizeof(uint32_t));
1298 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1300 if (len > pctx->opts.recv_bufsize) {
1301 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1303 char *packet = (char *)buf;
1304 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1306 DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1307 pconn->cm_id, buf, len));
1309 /* single threaded => no race here: */
1310 assert(p->ref_cnt==0);
1311 while(rlen > recv_bufsize) {
1312 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1315 packet += recv_bufsize;
1316 rlen -= recv_bufsize;
1317 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1320 rc = ibw_send_packet(conn, packet, p, rlen);
1321 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1323 p->ref_cnt--; /* for the same handling */
1325 assert(p->ref_cnt==0);
1326 assert(p->queued_ref_cnt==0);
1328 rc = ibw_send_packet(conn, buf, p, len);
1333 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1335 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1336 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1337 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1343 if (p->buf_large!=NULL)
1344 ibw_free_mr(&p->buf_large, &p->mr_large);
1347 if (p->wr_id < pctx->opts.max_send_wr) {
1348 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1349 DLIST_REMOVE(pconn->wr_list_used, p);
1350 DLIST_ADD(pconn->wr_list_avail, p);
1351 } else { /* "extra" packet */
1352 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1353 DLIST_REMOVE(pconn->extra_sent, p);
1354 DLIST_ADD(pconn->extra_avail, p);
1360 const char *ibw_getLastError(void)