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 <rdma/rdma_cma.h>
41 #include "infiniband/sa-kern-abi.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(10, ("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(10, ("ibw_free_mr(%u %u)\n", (uint32_t)*ppbuf, (uint32_t)*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(10, ("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(10, ("ibw_ctx_priv_destruct(%u)\n", (uint32_t)pctx));
138 if (pctx->cm_channel) {
139 rdma_destroy_event_channel(pctx->cm_channel);
140 pctx->cm_channel = NULL;
142 if (pctx->cm_channel_event) {
143 /* TODO: do we have to do this here? */
144 talloc_free(pctx->cm_channel_event);
145 pctx->cm_channel_event = NULL;
148 rdma_destroy_id(pctx->cm_id);
155 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
157 DEBUG(10, ("ibw_ctx_destruct(%u)\n", (uint32_t)ctx));
161 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
163 DEBUG(10, ("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 if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
171 rdma_destroy_qp(pconn->cm_id);
172 pconn->cm_id->qp = NULL;
175 if (pconn->cq!=NULL) {
176 ibv_destroy_cq(pconn->cq);
180 if (pconn->verbs_channel!=NULL) {
181 ibv_destroy_comp_channel(pconn->verbs_channel);
182 pconn->verbs_channel = NULL;
185 /* must be freed here because its order is important */
186 if (pconn->verbs_channel_event) {
187 talloc_free(pconn->verbs_channel_event);
188 pconn->verbs_channel_event = NULL;
191 /* free memory regions */
192 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
193 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
196 ibv_dealloc_pd(pconn->pd);
198 DEBUG(10, ("pconn=%p pd deallocated\n", pconn));
202 rdma_destroy_id(pconn->cm_id);
204 DEBUG(10, ("pconn=%p cm_id destroyed\n", pconn));
210 static int ibw_wr_destruct(struct ibw_wr *wr)
212 if (wr->buf_large!=NULL)
213 ibw_free_mr(&wr->buf_large, &wr->mr_large);
217 static int ibw_conn_destruct(struct ibw_conn *conn)
219 DEBUG(10, ("ibw_conn_destruct(%u)\n", (uint32_t)conn));
221 /* important here: ctx is a talloc _parent_ */
222 DLIST_REMOVE(conn->ctx->conn_list, conn);
226 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
228 struct ibw_conn *conn;
229 struct ibw_conn_priv *pconn;
233 conn = talloc_zero(mem_ctx, struct ibw_conn);
235 talloc_set_destructor(conn, ibw_conn_destruct);
237 pconn = talloc_zero(conn, struct ibw_conn_priv);
239 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
242 conn->internal = (void *)pconn;
244 DLIST_ADD(ctx->conn_list, conn);
249 static int ibw_setup_cq_qp(struct ibw_conn *conn)
251 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
252 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
253 struct ibv_qp_init_attr init_attr;
254 struct ibv_qp_attr attr;
257 DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
260 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
261 if (!pconn->verbs_channel) {
262 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
265 DEBUG(10, ("created channel %p\n", pconn->verbs_channel));
267 pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
268 pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
270 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
272 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
275 DEBUG(10, ("created pd %p\n", pconn->pd));
278 if (ibw_init_memory(conn))
282 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
283 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
284 conn, pconn->verbs_channel, 0);
285 if (pconn->cq==NULL) {
286 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
290 rc = ibv_req_notify_cq(pconn->cq, 0);
292 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
297 memset(&init_attr, 0, sizeof(init_attr));
298 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
299 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
300 init_attr.cap.max_recv_sge = 1;
301 init_attr.cap.max_send_sge = 1;
302 init_attr.qp_type = IBV_QPT_RC;
303 init_attr.send_cq = pconn->cq;
304 init_attr.recv_cq = pconn->cq;
306 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
308 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
311 /* elase result is in pconn->cm_id->qp */
313 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
315 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
319 return ibw_fill_cq(conn);
322 static int ibw_refill_cq_recv(struct ibw_conn *conn)
324 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
325 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
327 struct ibv_sge list = {
328 .addr = (uintptr_t) NULL, /* filled below */
329 .length = pctx->opts.recv_bufsize,
330 .lkey = pconn->mr_recv->lkey /* always the same */
332 struct ibv_recv_wr wr = {
333 .wr_id = 0, /* filled below */
337 struct ibv_recv_wr *bad_wr;
339 DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
341 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
342 wr.wr_id = pconn->recv_index;
343 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
345 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
347 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
348 DEBUG(0, (ibw_lasterr));
355 static int ibw_fill_cq(struct ibw_conn *conn)
357 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
358 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
360 struct ibv_sge list = {
361 .addr = (uintptr_t) NULL, /* filled below */
362 .length = pctx->opts.recv_bufsize,
363 .lkey = pconn->mr_recv->lkey /* always the same */
365 struct ibv_recv_wr wr = {
366 .wr_id = 0, /* filled below */
370 struct ibv_recv_wr *bad_wr;
372 DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
374 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
375 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
376 wr.wr_id = pconn->recv_index;
377 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
379 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
381 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
382 DEBUG(0, (ibw_lasterr));
390 static int ibw_manage_connect(struct ibw_conn *conn)
392 struct rdma_conn_param conn_param;
393 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
396 DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
398 if (ibw_setup_cq_qp(conn))
402 memset(&conn_param, 0, sizeof conn_param);
403 conn_param.responder_resources = 1;
404 conn_param.initiator_depth = 1;
405 conn_param.retry_count = 10;
407 rc = rdma_connect(pconn->cm_id, &conn_param);
409 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
414 static void ibw_event_handler_cm(struct event_context *ev,
415 struct fd_event *fde, uint16_t flags, void *private_data)
418 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
419 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
420 struct ibw_conn *conn = NULL;
421 struct ibw_conn_priv *pconn = NULL;
422 struct rdma_cm_id *cma_id = NULL;
423 struct rdma_cm_event *event = NULL;
427 rc = rdma_get_cm_event(pctx->cm_channel, &event);
429 ctx->state = IBWS_ERROR;
430 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
435 DEBUG(10, ("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(11, ("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(11, ("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(11, ("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(10, ("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(0, ("rdma_reject failed with rc=%d\n", rc));
483 DEBUG(10, ("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(0, ("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(10, ("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);
516 case RDMA_CM_EVENT_REJECTED:
517 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
518 conn = talloc_get_type(cma_id->context, struct ibw_conn);
520 if ((rc=rdma_ack_cm_event(event)))
521 DEBUG(0, ("reject/rdma_ack_cm_event failed with %d\n", rc));
523 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
524 ibw_conn_priv_destruct(pconn);
528 case RDMA_CM_EVENT_DISCONNECTED:
529 DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
530 if ((rc=rdma_ack_cm_event(event)))
531 DEBUG(0, ("disc/rdma_ack_cm_event failed with %d\n", rc));
532 event = NULL; /* don't ack more */
534 if (cma_id!=pctx->cm_id) {
535 DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
536 conn = talloc_get_type(cma_id->context, struct ibw_conn);
537 conn->state = IBWC_DISCONNECTED;
538 pctx->connstate_func(NULL, conn);
542 case RDMA_CM_EVENT_DEVICE_REMOVAL:
543 sprintf(ibw_lasterr, "cma detected device removal!\n");
547 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
551 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
552 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
558 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
559 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
563 DEBUG(0, ("cm event handler: %s", ibw_lasterr));
565 if (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);
571 ctx->state = IBWS_ERROR;
572 pctx->connstate_func(ctx, NULL);
576 static void ibw_event_handler_verbs(struct event_context *ev,
577 struct fd_event *fde, uint16_t flags, void *private_data)
579 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
580 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
581 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
585 struct ibv_cq *ev_cq;
588 DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
590 /* TODO: check whether if it's good to have more channels here... */
591 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
593 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
596 if (ev_cq != pconn->cq) {
597 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
600 rc = ibv_req_notify_cq(pconn->cq, 0);
602 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
606 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
608 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
609 wc.status, wc.opcode, rc);
615 DEBUG(10, ("send completion\n"));
616 if (ibw_wc_send(conn, &wc))
620 case IBV_WC_RDMA_WRITE:
621 DEBUG(10, ("rdma write completion\n"));
624 case IBV_WC_RDMA_READ:
625 DEBUG(10, ("rdma read completion\n"));
629 DEBUG(10, ("recv completion\n"));
630 if (ibw_wc_recv(conn, &wc))
635 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
640 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
644 ibv_ack_cq_events(pconn->cq, 1);
648 ibv_ack_cq_events(pconn->cq, 1);
650 DEBUG(0, (ibw_lasterr));
652 if (conn->state!=IBWC_ERROR) {
653 conn->state = IBWC_ERROR;
654 pctx->connstate_func(NULL, conn);
658 static int ibw_process_queue(struct ibw_conn *conn)
660 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
661 struct ibw_ctx_priv *pctx;
666 if (pconn->queue==NULL)
671 /* we must have at least 1 fragment to send */
672 assert(p->queued_ref_cnt>0);
675 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
676 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
678 assert(p->queued_msg!=NULL);
681 DEBUG(10, ("ibw_process_queue refcnt=%d msgsize=%u\n",
682 p->queued_ref_cnt, msg_size));
684 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
686 /* was this the last fragment? */
687 if (p->queued_ref_cnt) {
688 p->queued_msg += pctx->opts.recv_bufsize;
690 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
691 p->queued_msg = NULL;
697 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
699 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
700 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
704 DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
705 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
707 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
708 assert(wc->wr_id >= pctx->opts.max_recv_wr);
709 send_index = wc->wr_id - pctx->opts.max_recv_wr;
712 if (send_index < pctx->opts.max_send_wr) {
713 DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
714 p = pconn->wr_index[send_index];
715 if (p->buf_large!=NULL) {
717 /* awaiting more of it... */
720 ibw_free_mr(&p->buf_large, &p->mr_large);
721 DLIST_REMOVE(pconn->wr_list_used, p);
722 DLIST_ADD(pconn->wr_list_avail, p);
724 } else { /* nasty - but necessary */
725 DLIST_REMOVE(pconn->wr_list_used, p);
726 DLIST_ADD(pconn->wr_list_avail, p);
728 } else { /* "extra" request - not optimized */
729 DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
730 for(p=pconn->extra_sent; p!=NULL; p=p->next)
731 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
734 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
740 ibw_free_mr(&p->buf_large, &p->mr_large);
741 DLIST_REMOVE(pconn->extra_sent, p);
742 DLIST_ADD(pconn->extra_avail, p);
746 return ibw_process_queue(conn);
749 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
750 struct ibw_part *part, char **pp, uint32_t add_len, int info)
752 DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
753 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
755 /* allocate more if necessary - it's an "evergrowing" buffer... */
756 if (part->len + add_len > part->bufsize) {
757 if (part->buf==NULL) {
758 assert(part->len==0);
759 part->buf = talloc_size(pconn, add_len);
760 if (part->buf==NULL) {
761 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
765 part->bufsize = add_len;
767 part->buf = talloc_realloc_size(pconn,
768 part->buf, part->len + add_len);
769 if (part->buf==NULL) {
770 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
771 part->len, add_len, info);
775 part->bufsize = part->len + add_len;
779 memcpy(part->buf + part->len, *pp, add_len);
781 part->len += add_len;
782 part->to_read -= add_len;
787 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
788 struct ibw_part *part, uint32_t threshold)
790 DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
791 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
793 if (part->bufsize > threshold) {
794 DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
795 pconn->cm_id, part->bufsize, threshold));
796 talloc_free(part->buf);
797 part->buf = talloc_size(pconn, threshold);
798 if (part->buf==NULL) {
799 sprintf(ibw_lasterr, "talloc_size failed\n");
802 part->bufsize = threshold;
807 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
809 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
810 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
811 struct ibw_part *part = &pconn->part;
813 uint32_t remain = wc->byte_len;
815 DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
816 pconn->cm_id, (uint32_t)wc->wr_id, remain));
818 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
819 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
820 assert(wc->byte_len <= pctx->opts.recv_bufsize);
822 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
825 /* here always true: (part->len!=0 && part->to_read!=0) ||
826 (part->len==0 && part->to_read==0) */
827 if (part->len) { /* is there a partial msg to be continued? */
828 int read_len = (part->to_read<=remain) ? part->to_read : remain;
829 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
833 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
834 assert(part->len==sizeof(uint32_t));
835 /* set it again now... */
836 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
837 if (part->to_read<sizeof(uint32_t)) {
838 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
841 part->to_read -= sizeof(uint32_t); /* it's already read */
844 if (part->to_read==0) {
845 pctx->receive_func(conn, part->buf, part->len);
846 part->len = 0; /* tells not having partial data (any more) */
847 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
851 if (remain>=sizeof(uint32_t)) {
852 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
853 if (msglen<sizeof(uint32_t)) {
854 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
858 /* mostly awaited case: */
859 if (msglen<=remain) {
860 pctx->receive_func(conn, p, msglen);
864 part->to_read = msglen;
865 /* part->len is already 0 */
866 if (ibw_append_to_part(pconn, part, &p, remain, 422))
868 remain = 0; /* to be continued ... */
869 /* part->to_read > 0 here */
871 } else { /* edge case: */
872 part->to_read = sizeof(uint32_t);
873 /* part->len is already 0 */
874 if (ibw_append_to_part(pconn, part, &p, remain, 423))
877 /* part->to_read > 0 here */
880 } /* <remain> is always decreased at least by 1 */
882 if (ibw_refill_cq_recv(conn))
888 DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
892 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
895 const char *name, *value;
897 DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
899 opts->max_send_wr = IBW_MAX_SEND_WR;
900 opts->max_recv_wr = IBW_MAX_RECV_WR;
901 opts->recv_bufsize = IBW_RECV_BUFSIZE;
902 opts->recv_threshold = IBW_RECV_THRESHOLD;
904 for(i=0; i<nattr; i++) {
906 value = attr[i].value;
908 assert(name!=NULL && value!=NULL);
909 if (strcmp(name, "max_send_wr")==0)
910 opts->max_send_wr = atoi(value);
911 else if (strcmp(name, "max_recv_wr")==0)
912 opts->max_recv_wr = atoi(value);
913 else if (strcmp(name, "recv_bufsize")==0)
914 opts->recv_bufsize = atoi(value);
915 else if (strcmp(name, "recv_threshold")==0)
916 opts->recv_threshold = atoi(value);
918 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
925 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
927 ibw_connstate_fn_t ibw_connstate,
928 ibw_receive_fn_t ibw_receive,
929 struct event_context *ectx)
931 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
932 struct ibw_ctx_priv *pctx;
935 DEBUG(10, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
937 /* initialize basic data structures */
938 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
941 ibw_lasterr[0] = '\0';
942 talloc_set_destructor(ctx, ibw_ctx_destruct);
943 ctx->ctx_userdata = ctx_userdata;
945 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
946 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
947 ctx->internal = (void *)pctx;
950 pctx->connstate_func = ibw_connstate;
951 pctx->receive_func = ibw_receive;
955 /* process attributes */
956 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
960 pctx->cm_channel = rdma_create_event_channel();
961 if (!pctx->cm_channel) {
962 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
966 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
967 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
969 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
972 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
975 DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
977 pctx->pagesize = sysconf(_SC_PAGESIZE);
980 /* don't put code here */
982 DEBUG(0, (ibw_lasterr));
990 int ibw_stop(struct ibw_ctx *ctx)
992 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
995 DEBUG(10, ("ibw_stop\n"));
997 for(p=ctx->conn_list; p!=NULL; p=p->next) {
998 if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
999 if (ibw_disconnect(p))
1004 ctx->state = IBWS_STOPPED;
1005 pctx->connstate_func(ctx, NULL);
1010 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1012 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1015 DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
1016 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1017 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1019 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1020 DEBUG(0, (ibw_lasterr));
1023 DEBUG(10, ("rdma_bind_addr successful\n"));
1028 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1030 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1033 DEBUG(10, ("ibw_listen\n"));
1034 rc = rdma_listen(pctx->cm_id, backlog);
1036 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1037 DEBUG(0, (ibw_lasterr));
1044 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1046 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1047 struct rdma_conn_param conn_param;
1050 DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1051 conn->conn_userdata = conn_userdata;
1053 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1054 conn_param.responder_resources = 1;
1055 conn_param.initiator_depth = 1;
1056 rc = rdma_accept(pconn->cm_id, &conn_param);
1058 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1059 DEBUG(0, (ibw_lasterr));
1063 pconn->is_accepted = 1;
1065 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1070 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1072 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1073 struct ibw_conn_priv *pconn = NULL;
1078 conn->conn_userdata = conn_userdata;
1079 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1080 DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1081 ntohs(serv_addr->sin_port)));
1083 /* clean previous - probably half - initialization */
1084 if (ibw_conn_priv_destruct(pconn)) {
1085 DEBUG(0, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1090 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1093 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1097 DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1099 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1101 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1102 DEBUG(0, (ibw_lasterr));
1107 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1112 int ibw_disconnect(struct ibw_conn *conn)
1115 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1117 DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1119 assert(pconn!=NULL);
1121 switch(conn->state) {
1123 ibw_conn_priv_destruct(pconn); /* do this here right now */
1125 case IBWC_CONNECTED:
1126 rc = rdma_disconnect(pconn->cm_id);
1128 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1129 DEBUG(0, (ibw_lasterr));
1134 DEBUG(9, ("invalid state for disconnect: %d\n", conn->state));
1141 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1143 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1144 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1145 struct ibw_wr *p = pconn->wr_list_avail;
1148 DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1150 DLIST_REMOVE(pconn->wr_list_avail, p);
1151 DLIST_ADD(pconn->wr_list_used, p);
1153 if (len <= pctx->opts.recv_bufsize) {
1154 *buf = (void *)p->buf;
1156 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1157 if (p->buf_large==NULL) {
1158 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1161 *buf = (void *)p->buf_large;
1163 /* p->wr_id is already filled in ibw_init_memory */
1165 DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1167 p = pconn->extra_avail;
1169 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1170 talloc_set_destructor(p, ibw_wr_destruct);
1172 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1175 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1177 switch(pconn->extra_max) {
1178 case 1: DEBUG(2, ("warning: queue performed\n")); break;
1179 case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
1180 case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
1181 case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
1186 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1187 if (p->buf_large==NULL) {
1188 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1191 *buf = (void *)p->buf_large;
1193 DLIST_REMOVE(pconn->extra_avail, p);
1194 /* we don't have prepared index for this, so that
1195 * we will have to find this by wr_id later on */
1196 DLIST_ADD(pconn->extra_sent, p);
1203 DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1208 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1210 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1211 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1214 /* can we send it right now? */
1215 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1216 struct ibv_send_wr *bad_wr;
1217 struct ibv_sge list = {
1218 .addr = (uintptr_t)buf,
1220 .lkey = pconn->mr_send->lkey
1222 struct ibv_send_wr wr = {
1223 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1226 .opcode = IBV_WR_SEND,
1227 .send_flags = IBV_SEND_SIGNALED,
1230 if (p->buf_large==NULL) {
1231 DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1232 pconn->cm_id, (uint32_t)wr.wr_id, len));
1234 DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1235 pconn->cm_id, (uint32_t)wr.wr_id, len));
1236 list.lkey = p->mr_large->lkey;
1239 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1241 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1242 rc, pconn->wr_sent);
1249 } /* else put the request into our own queue: */
1251 DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1253 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1255 /* to be sent by ibw_wc_send */
1256 /* regardless "normal" or [a part of] "large" packet */
1257 if (!p->queued_ref_cnt) {
1258 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1259 qprev, qnext); /* TODO: optimize */
1260 p->queued_msg = buf;
1262 p->queued_ref_cnt++;
1263 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1267 DEBUG(0, (ibw_lasterr));
1271 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1273 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1274 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1277 assert(len>=sizeof(uint32_t));
1278 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1280 if (len > pctx->opts.recv_bufsize) {
1281 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1283 char *packet = (char *)buf;
1284 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1286 DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1287 pconn->cm_id, buf, len));
1289 /* single threaded => no race here: */
1290 assert(p->ref_cnt==0);
1291 while(rlen > recv_bufsize) {
1292 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1295 packet += recv_bufsize;
1296 rlen -= recv_bufsize;
1297 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1300 rc = ibw_send_packet(conn, packet, p, rlen);
1301 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1303 p->ref_cnt--; /* for the same handling */
1305 assert(p->ref_cnt==0);
1306 assert(p->queued_ref_cnt==0);
1308 rc = ibw_send_packet(conn, buf, p, len);
1313 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1315 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1316 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1317 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1323 if (p->buf_large!=NULL)
1324 ibw_free_mr(&p->buf_large, &p->mr_large);
1327 if (p->wr_id < pctx->opts.max_send_wr) {
1328 DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1329 DLIST_REMOVE(pconn->wr_list_used, p);
1330 DLIST_ADD(pconn->wr_list_avail, p);
1331 } else { /* "extra" packet */
1332 DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1333 DLIST_REMOVE(pconn->extra_sent, p);
1334 DLIST_ADD(pconn->extra_avail, p);
1340 const char *ibw_getLastError(void)