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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
38 #include "lib/events/events.h"
39 #include "ibwrapper.h"
41 #include <infiniband/kern-abi.h>
42 #include <rdma/rdma_cma_abi.h>
43 #include <rdma/rdma_cma.h>
45 #include "ibwrapper_internal.h"
46 #include "lib/util/dlinklist.h"
48 #define IBW_LASTERR_BUFSIZE 512
49 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
51 #define IBW_MAX_SEND_WR 256
52 #define IBW_MAX_RECV_WR 1024
53 #define IBW_RECV_BUFSIZE 256
54 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
56 static void ibw_event_handler_verbs(struct event_context *ev,
57 struct fd_event *fde, uint16_t flags, void *private_data);
58 static int ibw_fill_cq(struct ibw_conn *conn);
59 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
60 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
61 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
63 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
64 uint32_t n, struct ibv_mr **ppmr)
68 DEBUG(10, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
69 buf = memalign(pctx->pagesize, n);
71 sprintf(ibw_lasterr, "couldn't allocate memory\n");
75 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
77 sprintf(ibw_lasterr, "couldn't allocate mr\n");
85 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
87 DEBUG(10, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
98 static int ibw_init_memory(struct ibw_conn *conn)
100 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
101 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
102 struct ibw_opts *opts = &pctx->opts;
106 DEBUG(10, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
107 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
108 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
109 if (!pconn->buf_send) {
110 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
114 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
115 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
116 if (!pconn->buf_recv) {
117 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
121 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
122 assert(pconn->wr_index!=NULL);
124 for(i=0; i<opts->max_send_wr; i++) {
125 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
126 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
129 DLIST_ADD(pconn->wr_list_avail, p);
135 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
137 DEBUG(10, ("ibw_ctx_priv_destruct(%p)\n", pctx));
140 if (pctx->cm_channel) {
141 rdma_destroy_event_channel(pctx->cm_channel);
142 pctx->cm_channel = NULL;
144 if (pctx->cm_channel_event) {
145 /* TODO: do we have to do this here? */
146 talloc_free(pctx->cm_channel_event);
147 pctx->cm_channel_event = NULL;
150 rdma_destroy_id(pctx->cm_id);
157 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
159 DEBUG(10, ("ibw_ctx_destruct(%p)\n", ctx));
163 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
165 DEBUG(10, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
166 pconn, pconn->cm_id));
168 /* pconn->wr_index is freed by talloc */
169 /* pconn->wr_index[i] are freed by talloc */
172 if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
173 rdma_destroy_qp(pconn->cm_id);
174 pconn->cm_id->qp = NULL;
177 if (pconn->cq!=NULL) {
178 ibv_destroy_cq(pconn->cq);
182 if (pconn->verbs_channel!=NULL) {
183 ibv_destroy_comp_channel(pconn->verbs_channel);
184 pconn->verbs_channel = NULL;
187 /* must be freed here because its order is important */
188 if (pconn->verbs_channel_event) {
189 talloc_free(pconn->verbs_channel_event);
190 pconn->verbs_channel_event = NULL;
193 /* free memory regions */
194 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
195 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
198 ibv_dealloc_pd(pconn->pd);
200 DEBUG(10, ("pconn=%p pd deallocated\n", pconn));
204 rdma_destroy_id(pconn->cm_id);
206 DEBUG(10, ("pconn=%p cm_id destroyed\n", pconn));
212 static int ibw_wr_destruct(struct ibw_wr *wr)
214 if (wr->buf_large!=NULL)
215 ibw_free_mr(&wr->buf_large, &wr->mr_large);
219 static int ibw_conn_destruct(struct ibw_conn *conn)
221 DEBUG(10, ("ibw_conn_destruct(%p)\n", conn));
223 /* important here: ctx is a talloc _parent_ */
224 DLIST_REMOVE(conn->ctx->conn_list, conn);
228 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
230 struct ibw_conn *conn;
231 struct ibw_conn_priv *pconn;
235 conn = talloc_zero(mem_ctx, struct ibw_conn);
237 talloc_set_destructor(conn, ibw_conn_destruct);
239 pconn = talloc_zero(conn, struct ibw_conn_priv);
241 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
244 conn->internal = (void *)pconn;
246 DLIST_ADD(ctx->conn_list, conn);
251 static int ibw_setup_cq_qp(struct ibw_conn *conn)
253 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
254 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
255 struct ibv_qp_init_attr init_attr;
256 struct ibv_qp_attr attr;
259 DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
262 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
263 if (!pconn->verbs_channel) {
264 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
267 DEBUG(10, ("created channel %p\n", pconn->verbs_channel));
269 pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
270 pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
272 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
274 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
277 DEBUG(10, ("created pd %p\n", pconn->pd));
280 if (ibw_init_memory(conn))
284 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
285 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
286 conn, pconn->verbs_channel, 0);
287 if (pconn->cq==NULL) {
288 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
292 rc = ibv_req_notify_cq(pconn->cq, 0);
294 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
299 memset(&init_attr, 0, sizeof(init_attr));
300 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
301 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
302 init_attr.cap.max_recv_sge = 1;
303 init_attr.cap.max_send_sge = 1;
304 init_attr.qp_type = IBV_QPT_RC;
305 init_attr.send_cq = pconn->cq;
306 init_attr.recv_cq = pconn->cq;
308 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
310 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
313 /* elase result is in pconn->cm_id->qp */
315 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
317 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
321 return ibw_fill_cq(conn);
324 static int ibw_refill_cq_recv(struct ibw_conn *conn)
326 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
327 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
329 struct ibv_sge list = {
330 .addr = (uintptr_t) NULL, /* filled below */
331 .length = pctx->opts.recv_bufsize,
332 .lkey = pconn->mr_recv->lkey /* always the same */
334 struct ibv_recv_wr wr = {
335 .wr_id = 0, /* filled below */
339 struct ibv_recv_wr *bad_wr;
341 DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
343 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
344 wr.wr_id = pconn->recv_index;
345 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
347 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
349 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
350 DEBUG(0, (ibw_lasterr));
357 static int ibw_fill_cq(struct ibw_conn *conn)
359 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
360 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
362 struct ibv_sge list = {
363 .addr = (uintptr_t) NULL, /* filled below */
364 .length = pctx->opts.recv_bufsize,
365 .lkey = pconn->mr_recv->lkey /* always the same */
367 struct ibv_recv_wr wr = {
368 .wr_id = 0, /* filled below */
372 struct ibv_recv_wr *bad_wr;
374 DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
376 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
377 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
378 wr.wr_id = pconn->recv_index;
379 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
381 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
383 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
384 DEBUG(0, (ibw_lasterr));
392 static int ibw_manage_connect(struct ibw_conn *conn)
394 struct rdma_conn_param conn_param;
395 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
398 DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
400 if (ibw_setup_cq_qp(conn))
404 memset(&conn_param, 0, sizeof conn_param);
405 conn_param.responder_resources = 1;
406 conn_param.initiator_depth = 1;
407 conn_param.retry_count = 10;
409 rc = rdma_connect(pconn->cm_id, &conn_param);
411 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
416 static void ibw_event_handler_cm(struct event_context *ev,
417 struct fd_event *fde, uint16_t flags, void *private_data)
420 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
421 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
422 struct ibw_conn *conn = NULL;
423 struct ibw_conn_priv *pconn = NULL;
424 struct rdma_cm_id *cma_id = NULL;
425 struct rdma_cm_event *event = NULL;
429 rc = rdma_get_cm_event(pctx->cm_channel, &event);
431 ctx->state = IBWS_ERROR;
433 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
438 DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
439 (cma_id == pctx->cm_id) ? "parent" : "child"));
441 switch (event->event) {
442 case RDMA_CM_EVENT_ADDR_RESOLVED:
443 DEBUG(11, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
444 /* continuing from ibw_connect ... */
445 rc = rdma_resolve_route(cma_id, 2000);
447 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
450 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
453 case RDMA_CM_EVENT_ROUTE_RESOLVED:
454 DEBUG(11, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
455 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
456 assert(cma_id->context!=NULL);
457 conn = talloc_get_type(cma_id->context, struct ibw_conn);
459 rc = ibw_manage_connect(conn);
465 case RDMA_CM_EVENT_CONNECT_REQUEST:
466 DEBUG(11, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
467 ctx->state = IBWS_CONNECT_REQUEST;
468 conn = ibw_conn_new(ctx, ctx);
469 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
470 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
471 cma_id->context = (void *)conn;
472 DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));
474 if (ibw_setup_cq_qp(conn))
477 conn->state = IBWC_INIT;
478 pctx->connstate_func(ctx, conn);
480 /* continued at ibw_accept when invoked by the func above */
481 if (!pconn->is_accepted) {
482 rc = rdma_reject(cma_id, NULL, 0);
484 DEBUG(0, ("rdma_reject failed with rc=%d\n", rc));
486 DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
489 /* TODO: clarify whether if it's needed by upper layer: */
490 ctx->state = IBWS_READY;
491 pctx->connstate_func(ctx, NULL);
493 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
496 case RDMA_CM_EVENT_ESTABLISHED:
497 /* expected after ibw_accept and ibw_connect[not directly] */
498 DEBUG(1, ("ESTABLISHED (conn: %p)\n", cma_id->context));
499 conn = talloc_get_type(cma_id->context, struct ibw_conn);
500 assert(conn!=NULL); /* important assumption */
502 DEBUG(10, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
504 /* client conn is up */
505 conn->state = IBWC_CONNECTED;
507 /* both ctx and conn have changed */
508 pctx->connstate_func(ctx, conn);
511 case RDMA_CM_EVENT_ADDR_ERROR:
512 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
513 case RDMA_CM_EVENT_ROUTE_ERROR:
514 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
515 case RDMA_CM_EVENT_CONNECT_ERROR:
516 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
517 case RDMA_CM_EVENT_UNREACHABLE:
518 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
520 case RDMA_CM_EVENT_REJECTED:
521 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
522 DEBUG(1, ("cm event handler: %s", ibw_lasterr));
523 conn = talloc_get_type(cma_id->context, struct ibw_conn);
525 /* must be done BEFORE connstate */
526 if ((rc=rdma_ack_cm_event(event)))
527 DEBUG(0, ("reject/rdma_ack_cm_event failed with %d\n", rc));
528 event = NULL; /* not to touch cma_id or conn */
529 conn->state = IBWC_ERROR;
530 /* it should free the conn */
531 pctx->connstate_func(NULL, conn);
533 break; /* this is not strictly an error */
535 case RDMA_CM_EVENT_DISCONNECTED:
536 DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
537 if ((rc=rdma_ack_cm_event(event)))
538 DEBUG(0, ("disc/rdma_ack_cm_event failed with %d\n", rc));
539 event = NULL; /* don't ack more */
541 if (cma_id!=pctx->cm_id) {
542 DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
543 conn = talloc_get_type(cma_id->context, struct ibw_conn);
544 conn->state = IBWC_DISCONNECTED;
545 pctx->connstate_func(NULL, conn);
549 case RDMA_CM_EVENT_DEVICE_REMOVAL:
550 sprintf(ibw_lasterr, "cma detected device removal!\n");
554 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
558 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
559 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
565 DEBUG(0, ("cm event handler: %s", ibw_lasterr));
568 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
569 conn = talloc_get_type(cma_id->context, struct ibw_conn);
571 conn->state = IBWC_ERROR;
572 pctx->connstate_func(NULL, conn);
575 ctx->state = IBWS_ERROR;
576 pctx->connstate_func(ctx, NULL);
579 if ((rc=rdma_ack_cm_event(event))!=0) {
580 DEBUG(0, ("rdma_ack_cm_event failed with %d\n", rc));
587 static void ibw_event_handler_verbs(struct event_context *ev,
588 struct fd_event *fde, uint16_t flags, void *private_data)
590 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
591 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
592 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
596 struct ibv_cq *ev_cq;
599 DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
601 /* TODO: check whether if it's good to have more channels here... */
602 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
604 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
607 if (ev_cq != pconn->cq) {
608 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
611 rc = ibv_req_notify_cq(pconn->cq, 0);
613 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
617 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
619 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
620 wc.status, wc.opcode, rc);
626 DEBUG(10, ("send completion\n"));
627 if (ibw_wc_send(conn, &wc))
631 case IBV_WC_RDMA_WRITE:
632 DEBUG(10, ("rdma write completion\n"));
635 case IBV_WC_RDMA_READ:
636 DEBUG(10, ("rdma read completion\n"));
640 DEBUG(10, ("recv completion\n"));
641 if (ibw_wc_recv(conn, &wc))
646 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
651 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
655 ibv_ack_cq_events(pconn->cq, 1);
659 ibv_ack_cq_events(pconn->cq, 1);
661 DEBUG(0, (ibw_lasterr));
663 if (conn->state!=IBWC_ERROR) {
664 conn->state = IBWC_ERROR;
665 pctx->connstate_func(NULL, conn);
669 static int ibw_process_queue(struct ibw_conn *conn)
671 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
672 struct ibw_ctx_priv *pctx;
677 if (pconn->queue==NULL)
682 /* we must have at least 1 fragment to send */
683 assert(p->queued_ref_cnt>0);
686 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
687 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
689 assert(p->queued_msg!=NULL);
692 DEBUG(10, ("ibw_process_queue refcnt=%d msgsize=%u\n",
693 p->queued_ref_cnt, msg_size));
695 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
697 /* was this the last fragment? */
698 if (p->queued_ref_cnt) {
699 p->queued_msg += pctx->opts.recv_bufsize;
701 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
702 p->queued_msg = NULL;
708 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
710 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
711 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
715 DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
716 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
718 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
719 assert(wc->wr_id >= pctx->opts.max_recv_wr);
720 send_index = wc->wr_id - pctx->opts.max_recv_wr;
723 if (send_index < pctx->opts.max_send_wr) {
724 DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
725 p = pconn->wr_index[send_index];
726 if (p->buf_large!=NULL) {
728 /* awaiting more of it... */
731 ibw_free_mr(&p->buf_large, &p->mr_large);
732 DLIST_REMOVE(pconn->wr_list_used, p);
733 DLIST_ADD(pconn->wr_list_avail, p);
735 } else { /* nasty - but necessary */
736 DLIST_REMOVE(pconn->wr_list_used, p);
737 DLIST_ADD(pconn->wr_list_avail, p);
739 } else { /* "extra" request - not optimized */
740 DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
741 for(p=pconn->extra_sent; p!=NULL; p=p->next)
742 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
745 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
751 ibw_free_mr(&p->buf_large, &p->mr_large);
752 DLIST_REMOVE(pconn->extra_sent, p);
753 DLIST_ADD(pconn->extra_avail, p);
757 return ibw_process_queue(conn);
760 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
761 struct ibw_part *part, char **pp, uint32_t add_len, int info)
763 DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
764 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
766 /* allocate more if necessary - it's an "evergrowing" buffer... */
767 if (part->len + add_len > part->bufsize) {
768 if (part->buf==NULL) {
769 assert(part->len==0);
770 part->buf = talloc_size(pconn, add_len);
771 if (part->buf==NULL) {
772 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
776 part->bufsize = add_len;
778 part->buf = talloc_realloc_size(pconn,
779 part->buf, part->len + add_len);
780 if (part->buf==NULL) {
781 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
782 part->len, add_len, info);
786 part->bufsize = part->len + add_len;
790 memcpy(part->buf + part->len, *pp, add_len);
792 part->len += add_len;
793 part->to_read -= add_len;
798 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
799 struct ibw_part *part, uint32_t threshold)
801 DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
802 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
804 if (part->bufsize > threshold) {
805 DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
806 pconn->cm_id, part->bufsize, threshold));
807 talloc_free(part->buf);
808 part->buf = talloc_size(pconn, threshold);
809 if (part->buf==NULL) {
810 sprintf(ibw_lasterr, "talloc_size failed\n");
813 part->bufsize = threshold;
818 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
820 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
821 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
822 struct ibw_part *part = &pconn->part;
824 uint32_t remain = wc->byte_len;
826 DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
827 pconn->cm_id, (uint32_t)wc->wr_id, remain));
829 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
830 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
831 assert(wc->byte_len <= pctx->opts.recv_bufsize);
833 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
836 /* here always true: (part->len!=0 && part->to_read!=0) ||
837 (part->len==0 && part->to_read==0) */
838 if (part->len) { /* is there a partial msg to be continued? */
839 int read_len = (part->to_read<=remain) ? part->to_read : remain;
840 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
844 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
845 assert(part->len==sizeof(uint32_t));
846 /* set it again now... */
847 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
848 if (part->to_read<sizeof(uint32_t)) {
849 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
852 part->to_read -= sizeof(uint32_t); /* it's already read */
855 if (part->to_read==0) {
856 pctx->receive_func(conn, part->buf, part->len);
857 part->len = 0; /* tells not having partial data (any more) */
858 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
862 if (remain>=sizeof(uint32_t)) {
863 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
864 if (msglen<sizeof(uint32_t)) {
865 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
869 /* mostly awaited case: */
870 if (msglen<=remain) {
871 pctx->receive_func(conn, p, msglen);
875 part->to_read = msglen;
876 /* part->len is already 0 */
877 if (ibw_append_to_part(pconn, part, &p, remain, 422))
879 remain = 0; /* to be continued ... */
880 /* part->to_read > 0 here */
882 } else { /* edge case: */
883 part->to_read = sizeof(uint32_t);
884 /* part->len is already 0 */
885 if (ibw_append_to_part(pconn, part, &p, remain, 423))
888 /* part->to_read > 0 here */
891 } /* <remain> is always decreased at least by 1 */
893 if (ibw_refill_cq_recv(conn))
899 DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
903 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
906 const char *name, *value;
908 DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
910 opts->max_send_wr = IBW_MAX_SEND_WR;
911 opts->max_recv_wr = IBW_MAX_RECV_WR;
912 opts->recv_bufsize = IBW_RECV_BUFSIZE;
913 opts->recv_threshold = IBW_RECV_THRESHOLD;
915 for(i=0; i<nattr; i++) {
917 value = attr[i].value;
919 assert(name!=NULL && value!=NULL);
920 if (strcmp(name, "max_send_wr")==0)
921 opts->max_send_wr = atoi(value);
922 else if (strcmp(name, "max_recv_wr")==0)
923 opts->max_recv_wr = atoi(value);
924 else if (strcmp(name, "recv_bufsize")==0)
925 opts->recv_bufsize = atoi(value);
926 else if (strcmp(name, "recv_threshold")==0)
927 opts->recv_threshold = atoi(value);
929 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
936 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
938 ibw_connstate_fn_t ibw_connstate,
939 ibw_receive_fn_t ibw_receive,
940 struct event_context *ectx)
942 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
943 struct ibw_ctx_priv *pctx;
946 DEBUG(10, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
948 /* initialize basic data structures */
949 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
952 ibw_lasterr[0] = '\0';
953 talloc_set_destructor(ctx, ibw_ctx_destruct);
954 ctx->ctx_userdata = ctx_userdata;
956 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
957 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
958 ctx->internal = (void *)pctx;
961 pctx->connstate_func = ibw_connstate;
962 pctx->receive_func = ibw_receive;
966 /* process attributes */
967 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
971 pctx->cm_channel = rdma_create_event_channel();
972 if (!pctx->cm_channel) {
973 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
977 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
978 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
980 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
981 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
983 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
987 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
990 DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
992 pctx->pagesize = sysconf(_SC_PAGESIZE);
995 /* don't put code here */
997 DEBUG(0, (ibw_lasterr));
1005 int ibw_stop(struct ibw_ctx *ctx)
1007 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1010 DEBUG(10, ("ibw_stop\n"));
1012 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1013 if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
1014 if (ibw_disconnect(p))
1019 ctx->state = IBWS_STOPPED;
1020 pctx->connstate_func(ctx, NULL);
1025 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1027 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1030 DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
1031 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1032 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1034 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1035 DEBUG(0, (ibw_lasterr));
1038 DEBUG(10, ("rdma_bind_addr successful\n"));
1043 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1045 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1048 DEBUG(10, ("ibw_listen\n"));
1049 rc = rdma_listen(pctx->cm_id, backlog);
1051 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1052 DEBUG(0, (ibw_lasterr));
1059 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1061 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1062 struct rdma_conn_param conn_param;
1065 DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1066 conn->conn_userdata = conn_userdata;
1068 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1069 conn_param.responder_resources = 1;
1070 conn_param.initiator_depth = 1;
1071 rc = rdma_accept(pconn->cm_id, &conn_param);
1073 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1074 DEBUG(0, (ibw_lasterr));
1078 pconn->is_accepted = 1;
1080 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1085 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1087 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1088 struct ibw_conn_priv *pconn = NULL;
1093 conn->conn_userdata = conn_userdata;
1094 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1095 DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1096 ntohs(serv_addr->sin_port)));
1098 /* clean previous - probably half - initialization */
1099 if (ibw_conn_priv_destruct(pconn)) {
1100 DEBUG(0, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1105 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1106 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1108 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1112 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1116 DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1118 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1120 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1121 DEBUG(0, (ibw_lasterr));
1126 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1131 int ibw_disconnect(struct ibw_conn *conn)
1134 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1136 DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1138 assert(pconn!=NULL);
1140 switch(conn->state) {
1142 ibw_conn_priv_destruct(pconn); /* do this here right now */
1144 case IBWC_CONNECTED:
1145 rc = rdma_disconnect(pconn->cm_id);
1147 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1148 DEBUG(0, (ibw_lasterr));
1153 DEBUG(9, ("invalid state for disconnect: %d\n", conn->state));
1160 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1162 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1163 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1164 struct ibw_wr *p = pconn->wr_list_avail;
1167 DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1169 DLIST_REMOVE(pconn->wr_list_avail, p);
1170 DLIST_ADD(pconn->wr_list_used, p);
1172 if (len <= pctx->opts.recv_bufsize) {
1173 *buf = (void *)p->buf;
1175 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1176 if (p->buf_large==NULL) {
1177 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1180 *buf = (void *)p->buf_large;
1182 /* p->wr_id is already filled in ibw_init_memory */
1184 DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1186 p = pconn->extra_avail;
1188 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1189 talloc_set_destructor(p, ibw_wr_destruct);
1191 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1194 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1196 switch(pconn->extra_max) {
1197 case 1: DEBUG(2, ("warning: queue performed\n")); break;
1198 case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
1199 case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
1200 case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
1205 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1206 if (p->buf_large==NULL) {
1207 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1210 *buf = (void *)p->buf_large;
1212 DLIST_REMOVE(pconn->extra_avail, p);
1213 /* we don't have prepared index for this, so that
1214 * we will have to find this by wr_id later on */
1215 DLIST_ADD(pconn->extra_sent, p);
1222 DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1227 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1229 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1230 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1233 /* can we send it right now? */
1234 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1235 struct ibv_send_wr *bad_wr;
1236 struct ibv_sge list = {
1237 .addr = (uintptr_t)buf,
1239 .lkey = pconn->mr_send->lkey
1241 struct ibv_send_wr wr = {
1242 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1245 .opcode = IBV_WR_SEND,
1246 .send_flags = IBV_SEND_SIGNALED,
1249 if (p->buf_large==NULL) {
1250 DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1251 pconn->cm_id, (uint32_t)wr.wr_id, len));
1253 DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1254 pconn->cm_id, (uint32_t)wr.wr_id, len));
1255 list.lkey = p->mr_large->lkey;
1258 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1260 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1261 rc, pconn->wr_sent);
1268 } /* else put the request into our own queue: */
1270 DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1272 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1274 /* to be sent by ibw_wc_send */
1275 /* regardless "normal" or [a part of] "large" packet */
1276 if (!p->queued_ref_cnt) {
1277 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1278 qprev, qnext); /* TODO: optimize */
1279 p->queued_msg = buf;
1281 p->queued_ref_cnt++;
1282 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1286 DEBUG(0, (ibw_lasterr));
1290 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1292 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1293 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1296 assert(len>=sizeof(uint32_t));
1297 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1299 if (len > pctx->opts.recv_bufsize) {
1300 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1302 char *packet = (char *)buf;
1303 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1305 DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1306 pconn->cm_id, buf, len));
1308 /* single threaded => no race here: */
1309 assert(p->ref_cnt==0);
1310 while(rlen > recv_bufsize) {
1311 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1314 packet += recv_bufsize;
1315 rlen -= recv_bufsize;
1316 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1319 rc = ibw_send_packet(conn, packet, p, rlen);
1320 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1322 p->ref_cnt--; /* for the same handling */
1324 assert(p->ref_cnt==0);
1325 assert(p->queued_ref_cnt==0);
1327 rc = ibw_send_packet(conn, buf, p, len);
1332 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1334 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1335 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1336 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1342 if (p->buf_large!=NULL)
1343 ibw_free_mr(&p->buf_large, &p->mr_large);
1346 if (p->wr_id < pctx->opts.max_send_wr) {
1347 DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1348 DLIST_REMOVE(pconn->wr_list_used, p);
1349 DLIST_ADD(pconn->wr_list_avail, p);
1350 } else { /* "extra" packet */
1351 DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1352 DLIST_REMOVE(pconn->extra_sent, p);
1353 DLIST_ADD(pconn->extra_avail, p);
1359 const char *ibw_getLastError(void)