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 <rdma/rdma_cma.h>
42 #include "infiniband/sa-kern-abi.h"
44 #include "ibwrapper_internal.h"
45 #include "lib/util/dlinklist.h"
47 #define IBW_LASTERR_BUFSIZE 512
48 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
50 #define IBW_MAX_SEND_WR 256
51 #define IBW_MAX_RECV_WR 1024
52 #define IBW_RECV_BUFSIZE 256
53 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
55 static void ibw_event_handler_verbs(struct event_context *ev,
56 struct fd_event *fde, uint16_t flags, void *private_data);
57 static int ibw_fill_cq(struct ibw_conn *conn);
58 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
59 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
60 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
62 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
63 uint32_t n, struct ibv_mr **ppmr)
67 DEBUG(10, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
68 buf = memalign(pctx->pagesize, n);
70 sprintf(ibw_lasterr, "couldn't allocate memory\n");
74 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
76 sprintf(ibw_lasterr, "couldn't allocate mr\n");
84 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
86 DEBUG(10, ("ibw_free_mr(%u %u)\n", (uint32_t)*ppbuf, (uint32_t)*ppmr));
97 static int ibw_init_memory(struct ibw_conn *conn)
99 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
100 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
101 struct ibw_opts *opts = &pctx->opts;
105 DEBUG(10, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
106 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
107 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
108 if (!pconn->buf_send) {
109 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
113 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
114 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
115 if (!pconn->buf_recv) {
116 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
120 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
121 assert(pconn->wr_index!=NULL);
123 for(i=0; i<opts->max_send_wr; i++) {
124 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
125 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
128 DLIST_ADD(pconn->wr_list_avail, p);
134 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
136 DEBUG(10, ("ibw_ctx_priv_destruct(%u)\n", (uint32_t)pctx));
139 if (pctx->cm_channel) {
140 rdma_destroy_event_channel(pctx->cm_channel);
141 pctx->cm_channel = NULL;
143 if (pctx->cm_channel_event) {
144 /* TODO: do we have to do this here? */
145 talloc_free(pctx->cm_channel_event);
146 pctx->cm_channel_event = NULL;
149 rdma_destroy_id(pctx->cm_id);
156 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
158 DEBUG(10, ("ibw_ctx_destruct(%u)\n", (uint32_t)ctx));
162 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
164 DEBUG(10, ("ibw_conn_priv_destruct(%u, cmid: %p)\n",
165 (uint32_t)pconn, pconn->cm_id));
167 /* free memory regions */
168 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
169 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
171 /* pconn->wr_index is freed by talloc */
172 /* pconn->wr_index[i] are freed by talloc */
175 if (pconn->cm_id->qp) {
176 ibv_destroy_qp(pconn->cm_id->qp);
177 pconn->cm_id->qp = NULL;
180 ibv_destroy_cq(pconn->cq);
183 if (pconn->verbs_channel) {
184 ibv_destroy_comp_channel(pconn->verbs_channel);
185 pconn->verbs_channel = NULL;
187 if (pconn->verbs_channel_event) {
188 /* TODO: do we have to do this here? */
189 talloc_free(pconn->verbs_channel_event);
190 pconn->verbs_channel_event = NULL;
193 ibv_dealloc_pd(pconn->pd);
197 rdma_destroy_id(pconn->cm_id);
203 static int ibw_conn_destruct(struct ibw_conn *conn)
205 DEBUG(10, ("ibw_conn_destruct(%u)\n", (uint32_t)conn));
207 /* important here: ctx is a talloc _parent_ */
208 DLIST_REMOVE(conn->ctx->conn_list, conn);
212 static struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx)
214 struct ibw_conn *conn;
215 struct ibw_conn_priv *pconn;
217 conn = talloc_zero(ctx, struct ibw_conn);
219 talloc_set_destructor(conn, ibw_conn_destruct);
221 pconn = talloc_zero(ctx, struct ibw_conn_priv);
223 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
226 conn->internal = (void *)pconn;
228 DLIST_ADD(ctx->conn_list, conn);
233 static int ibw_setup_cq_qp(struct ibw_conn *conn)
235 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
236 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
237 struct ibv_qp_init_attr init_attr;
238 struct ibv_qp_attr attr;
241 DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
244 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
245 if (!pconn->verbs_channel) {
246 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
249 DEBUG(10, ("created channel %p\n", pconn->verbs_channel));
251 pconn->verbs_channel_event = event_add_fd(pctx->ectx, conn,
252 pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
254 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
256 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
259 DEBUG(10, ("created pd %p\n", pconn->pd));
262 if (ibw_init_memory(conn))
266 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
267 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
268 conn, pconn->verbs_channel, 0);
269 if (pconn->cq==NULL) {
270 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
274 rc = ibv_req_notify_cq(pconn->cq, 0);
276 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
281 memset(&init_attr, 0, sizeof(init_attr));
282 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
283 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
284 init_attr.cap.max_recv_sge = 1;
285 init_attr.cap.max_send_sge = 1;
286 init_attr.qp_type = IBV_QPT_RC;
287 init_attr.send_cq = pconn->cq;
288 init_attr.recv_cq = pconn->cq;
290 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
292 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
295 /* elase result is in pconn->cm_id->qp */
297 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
299 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
303 return ibw_fill_cq(conn);
306 static int ibw_refill_cq_recv(struct ibw_conn *conn)
308 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
309 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
311 struct ibv_sge list = {
312 .addr = (uintptr_t) NULL, /* filled below */
313 .length = pctx->opts.recv_bufsize,
314 .lkey = pconn->mr_recv->lkey /* always the same */
316 struct ibv_recv_wr wr = {
317 .wr_id = 0, /* filled below */
321 struct ibv_recv_wr *bad_wr;
323 DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
325 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
326 wr.wr_id = pconn->recv_index;
327 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
329 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
331 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
332 DEBUG(0, (ibw_lasterr));
339 static int ibw_fill_cq(struct ibw_conn *conn)
341 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
342 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
344 struct ibv_sge list = {
345 .addr = (uintptr_t) NULL, /* filled below */
346 .length = pctx->opts.recv_bufsize,
347 .lkey = pconn->mr_recv->lkey /* always the same */
349 struct ibv_recv_wr wr = {
350 .wr_id = 0, /* filled below */
354 struct ibv_recv_wr *bad_wr;
356 DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
358 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
359 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
360 wr.wr_id = pconn->recv_index;
361 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
363 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
365 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
366 DEBUG(0, (ibw_lasterr));
374 static int ibw_manage_connect(struct ibw_conn *conn, struct rdma_cm_id *cma_id)
376 struct rdma_conn_param conn_param;
379 DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", cma_id));
380 rc = ibw_setup_cq_qp(conn);
385 memset(&conn_param, 0, sizeof conn_param);
386 conn_param.responder_resources = 1;
387 conn_param.initiator_depth = 1;
388 conn_param.retry_count = 10;
390 rc = rdma_connect(cma_id, &conn_param);
392 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
397 static void ibw_event_handler_cm(struct event_context *ev,
398 struct fd_event *fde, uint16_t flags, void *private_data)
401 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
402 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
403 struct ibw_conn *conn = NULL;
404 struct ibw_conn_priv *pconn = NULL;
405 struct rdma_cm_id *cma_id = NULL;
406 struct rdma_cm_event *event = NULL;
410 rc = rdma_get_cm_event(pctx->cm_channel, &event);
412 ctx->state = IBWS_ERROR;
413 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
418 DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
419 (cma_id == pctx->cm_id) ? "parent" : "child"));
421 switch (event->event) {
422 case RDMA_CM_EVENT_ADDR_RESOLVED:
423 /* continuing from ibw_connect ... */
424 rc = rdma_resolve_route(cma_id, 2000);
426 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
429 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
432 case RDMA_CM_EVENT_ROUTE_RESOLVED:
433 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
434 assert(cma_id->context!=NULL);
435 conn = talloc_get_type(cma_id->context, struct ibw_conn);
437 rc = ibw_manage_connect(conn, cma_id);
443 case RDMA_CM_EVENT_CONNECT_REQUEST:
444 ctx->state = IBWS_CONNECT_REQUEST;
445 conn = ibw_conn_new(ctx);
446 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
447 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
448 cma_id->context = (void *)conn;
449 DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));
451 if (ibw_setup_cq_qp(conn))
454 conn->state = IBWC_INIT;
455 pctx->connstate_func(ctx, conn);
457 /* continued at ibw_accept when invoked by the func above */
458 if (!pconn->is_accepted) {
460 DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
463 /* TODO: clarify whether if it's needed by upper layer: */
464 ctx->state = IBWS_READY;
465 pctx->connstate_func(ctx, NULL);
467 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
470 case RDMA_CM_EVENT_ESTABLISHED:
471 /* expected after ibw_accept and ibw_connect[not directly] */
472 DEBUG(0, ("ESTABLISHED (conn: %p)\n", cma_id->context));
473 conn = talloc_get_type(cma_id->context, struct ibw_conn);
474 assert(conn!=NULL); /* important assumption */
476 /* client conn is up */
477 conn->state = IBWC_CONNECTED;
479 /* both ctx and conn have changed */
480 pctx->connstate_func(ctx, conn);
483 case RDMA_CM_EVENT_ADDR_ERROR:
484 case RDMA_CM_EVENT_ROUTE_ERROR:
485 case RDMA_CM_EVENT_CONNECT_ERROR:
486 case RDMA_CM_EVENT_UNREACHABLE:
487 case RDMA_CM_EVENT_REJECTED:
488 sprintf(ibw_lasterr, "cma event %d, error %d\n", event->event, event->status);
491 case RDMA_CM_EVENT_DISCONNECTED:
492 if (cma_id!=pctx->cm_id) {
493 DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
494 conn = talloc_get_type(cma_id->context, struct ibw_conn);
495 conn->state = IBWC_DISCONNECTED;
496 pctx->connstate_func(NULL, conn);
500 case RDMA_CM_EVENT_DEVICE_REMOVAL:
501 sprintf(ibw_lasterr, "cma detected device removal!\n");
505 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
509 if ((rc=rdma_ack_cm_event(event))) {
510 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
516 DEBUG(0, ("cm event handler: %s", ibw_lasterr));
517 if (cma_id!=pctx->cm_id) {
518 conn = talloc_get_type(cma_id->context, struct ibw_conn);
520 conn->state = IBWC_ERROR;
521 pctx->connstate_func(NULL, conn);
523 ctx->state = IBWS_ERROR;
524 pctx->connstate_func(ctx, NULL);
528 static void ibw_event_handler_verbs(struct event_context *ev,
529 struct fd_event *fde, uint16_t flags, void *private_data)
531 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
532 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
533 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
537 struct ibv_cq *ev_cq;
540 DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
542 /* TODO: check whether if it's good to have more channels here... */
543 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
545 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
548 if (ev_cq != pconn->cq) {
549 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
552 rc = ibv_req_notify_cq(pconn->cq, 0);
554 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
558 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
560 sprintf(ibw_lasterr, "cq completion failed status %d rc %d\n",
567 DEBUG(10, ("send completion\n"));
568 if (ibw_wc_send(conn, &wc))
572 case IBV_WC_RDMA_WRITE:
573 DEBUG(10, ("rdma write completion\n"));
576 case IBV_WC_RDMA_READ:
577 DEBUG(10, ("rdma read completion\n"));
581 DEBUG(10, ("recv completion\n"));
582 if (ibw_wc_recv(conn, &wc))
587 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
592 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
598 DEBUG(0, (ibw_lasterr));
599 conn->state = IBWC_ERROR;
600 pctx->connstate_func(NULL, conn);
603 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
605 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
606 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
611 DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
612 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
614 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
615 assert(wc->wr_id >= pctx->opts.max_recv_wr);
616 send_index = wc->wr_id - pctx->opts.max_recv_wr;
619 if (send_index < pctx->opts.max_send_wr) {
620 DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
621 p = pconn->wr_index[send_index];
622 if (p->buf_large!=NULL) {
624 /* awaiting more of it... */
627 ibw_free_mr(&p->buf_large, &p->mr_large);
628 DLIST_REMOVE(pconn->wr_list_used, p);
629 DLIST_ADD(pconn->wr_list_avail, p);
631 } else { /* nasty - but necessary */
632 DLIST_REMOVE(pconn->wr_list_used, p);
633 DLIST_ADD(pconn->wr_list_avail, p);
635 } else { /* "extra" request - not optimized */
636 DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
637 for(p=pconn->extra_sent; p!=NULL; p=p->next)
638 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
641 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
647 ibw_free_mr(&p->buf_large, &p->mr_large);
648 DLIST_REMOVE(pconn->extra_sent, p);
649 DLIST_ADD(pconn->extra_avail, p);
656 DEBUG(10, ("ibw_wc_send#queue %u\n", (int)wc->wr_id));
660 assert(p->queued_ref_cnt>0);
663 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
665 assert(p->queued_msg!=NULL);
667 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
668 if (p->queued_ref_cnt) {
669 p->queued_msg += pctx->opts.recv_bufsize;
671 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
672 p->queued_msg = NULL;
679 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
680 struct ibw_part *part, char **pp, uint32_t add_len, int info)
682 DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
683 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
685 /* allocate more if necessary - it's an "evergrowing" buffer... */
686 if (part->len + add_len > part->bufsize) {
687 if (part->buf==NULL) {
688 assert(part->len==0);
689 part->buf = talloc_size(pconn, add_len);
690 if (part->buf==NULL) {
691 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
695 part->bufsize = add_len;
697 part->buf = talloc_realloc_size(pconn,
698 part->buf, part->len + add_len);
699 if (part->buf==NULL) {
700 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
701 part->len, add_len, info);
705 part->bufsize = part->len + add_len;
709 memcpy(part->buf + part->len, *pp, add_len);
711 part->len += add_len;
712 part->to_read -= add_len;
717 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
718 struct ibw_part *part, uint32_t threshold)
720 DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
721 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
723 if (part->bufsize > threshold) {
724 DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
725 pconn->cm_id, part->bufsize, threshold));
726 talloc_free(part->buf);
727 part->buf = talloc_size(pconn, threshold);
728 if (part->buf==NULL) {
729 sprintf(ibw_lasterr, "talloc_size failed\n");
732 part->bufsize = threshold;
737 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
739 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
740 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
741 struct ibw_part *part = &pconn->part;
743 uint32_t remain = wc->byte_len;
745 DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
746 pconn->cm_id, (uint32_t)wc->wr_id, remain));
748 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
749 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
750 assert(wc->byte_len <= pctx->opts.recv_bufsize);
752 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
755 /* here always true: (part->len!=0 && part->to_read!=0) ||
756 (part->len==0 && part->to_read==0) */
757 if (part->len) { /* is there a partial msg to be continued? */
758 int read_len = (part->to_read<=remain) ? part->to_read : remain;
759 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
763 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
764 assert(part->len==sizeof(uint32_t));
765 /* set it again now... */
766 part->to_read = ntohl(*((uint32_t *)(part->buf)));
767 if (part->to_read<sizeof(uint32_t)) {
768 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
771 part->to_read -= sizeof(uint32_t); /* it's already read */
774 if (part->to_read==0) {
775 pctx->receive_func(conn, part->buf, part->len);
776 part->len = 0; /* tells not having partial data (any more) */
777 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
781 if (remain>=sizeof(uint32_t)) {
782 uint32_t msglen = ntohl(*(uint32_t *)p);
783 if (msglen<sizeof(uint32_t)) {
784 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
788 /* mostly awaited case: */
789 if (msglen<=remain) {
790 pctx->receive_func(conn, p, msglen);
794 part->to_read = msglen;
795 /* part->len is already 0 */
796 if (ibw_append_to_part(pconn, part, &p, remain, 422))
798 remain = 0; /* to be continued ... */
799 /* part->to_read > 0 here */
801 } else { /* edge case: */
802 part->to_read = sizeof(uint32_t);
803 /* part->len is already 0 */
804 if (ibw_append_to_part(pconn, part, &p, remain, 423))
807 /* part->to_read > 0 here */
810 } /* <remain> is always decreased at least by 1 */
812 if (ibw_refill_cq_recv(conn))
818 DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
822 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
825 const char *name, *value;
827 DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
829 opts->max_send_wr = IBW_MAX_SEND_WR;
830 opts->max_recv_wr = IBW_MAX_RECV_WR;
831 opts->recv_bufsize = IBW_RECV_BUFSIZE;
832 opts->recv_threshold = IBW_RECV_THRESHOLD;
834 for(i=0; i<nattr; i++) {
836 value = attr[i].value;
838 assert(name!=NULL && value!=NULL);
839 if (strcmp(name, "max_send_wr")==0)
840 opts->max_send_wr = atoi(value);
841 else if (strcmp(name, "max_recv_wr")==0)
842 opts->max_recv_wr = atoi(value);
843 else if (strcmp(name, "recv_bufsize")==0)
844 opts->recv_bufsize = atoi(value);
845 else if (strcmp(name, "recv_threshold")==0)
846 opts->recv_threshold = atoi(value);
848 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
855 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
857 ibw_connstate_fn_t ibw_connstate,
858 ibw_receive_fn_t ibw_receive,
859 struct event_context *ectx)
861 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
862 struct ibw_ctx_priv *pctx;
865 DEBUG(10, ("ibw_init(ctx_userdata: %u, ectx: %u)\n",
866 (uint32_t)ctx_userdata, (uint32_t)ectx));
868 /* initialize basic data structures */
869 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
872 ibw_lasterr[0] = '\0';
873 talloc_set_destructor(ctx, ibw_ctx_destruct);
874 ctx->ctx_userdata = ctx_userdata;
876 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
877 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
878 ctx->internal = (void *)pctx;
881 pctx->connstate_func = ibw_connstate;
882 pctx->receive_func = ibw_receive;
886 /* process attributes */
887 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
891 pctx->cm_channel = rdma_create_event_channel();
892 if (!pctx->cm_channel) {
893 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
897 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
898 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
900 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
903 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
906 DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
908 pctx->pagesize = sysconf(_SC_PAGESIZE);
911 /* don't put code here */
913 DEBUG(0, (ibw_lasterr));
921 int ibw_stop(struct ibw_ctx *ctx)
923 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
926 DEBUG(10, ("ibw_stop\n"));
928 for(p=ctx->conn_list; p!=NULL; p=p->next) {
929 if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
930 if (ibw_disconnect(p))
935 ctx->state = IBWS_STOPPED;
936 pctx->connstate_func(ctx, NULL);
941 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
943 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
946 DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
947 inet_ntoa(my_addr->sin_addr), my_addr->sin_port));
948 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
950 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
951 DEBUG(0, (ibw_lasterr));
954 DEBUG(10, ("rdma_bind_addr successful\n"));
959 int ibw_listen(struct ibw_ctx *ctx, int backlog)
961 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
964 DEBUG(10, ("ibw_listen\n"));
965 rc = rdma_listen(pctx->cm_id, backlog);
967 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
968 DEBUG(0, (ibw_lasterr));
975 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
977 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
978 struct rdma_conn_param conn_param;
981 DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
982 conn->conn_userdata = conn_userdata;
984 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
985 conn_param.responder_resources = 1;
986 conn_param.initiator_depth = 1;
987 rc = rdma_accept(pconn->cm_id, &conn_param);
989 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
990 DEBUG(0, (ibw_lasterr));
994 pconn->is_accepted = 1;
996 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1001 int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata)
1003 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1004 struct ibw_conn *conn = NULL;
1005 struct ibw_conn_priv *pconn = NULL;
1008 conn = ibw_conn_new(ctx);
1009 conn->conn_userdata = conn_userdata;
1010 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1011 DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr), serv_addr->sin_port));
1014 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1017 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1020 DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1022 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1024 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1025 DEBUG(0, (ibw_lasterr));
1029 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1034 int ibw_disconnect(struct ibw_conn *conn)
1037 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1039 DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1041 rc = rdma_disconnect(pconn->cm_id);
1043 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1044 DEBUG(0, (ibw_lasterr));
1051 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1053 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1054 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1055 struct ibw_wr *p = pconn->wr_list_avail;
1058 DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1060 DLIST_REMOVE(pconn->wr_list_avail, p);
1061 DLIST_ADD(pconn->wr_list_used, p);
1063 if (len <= pctx->opts.recv_bufsize) {
1064 *buf = (void *)p->buf;
1066 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1067 if (p->buf_large==NULL) {
1068 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1071 *buf = (void *)p->buf_large;
1073 /* p->wr_id is already filled in ibw_init_memory */
1075 DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1077 p = pconn->extra_avail;
1079 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1081 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1084 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1086 switch(pconn->extra_max) {
1087 case 1: DEBUG(2, ("warning: queue performed\n")); break;
1088 case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
1089 case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
1090 case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
1095 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1096 if (p->buf_large==NULL) {
1097 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1100 *buf = (void *)p->buf_large;
1102 DLIST_REMOVE(pconn->extra_avail, p);
1103 /* we don't have prepared index for this, so that
1104 * we will have to find this by wr_id later on */
1105 DLIST_ADD(pconn->extra_sent, p);
1112 DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1117 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1119 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1120 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1123 /* can we send it right now? */
1124 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1125 struct ibv_send_wr *bad_wr;
1126 struct ibv_sge list = {
1127 .addr = (uintptr_t)buf,
1129 .lkey = pconn->mr_send->lkey
1131 struct ibv_send_wr wr = {
1132 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1135 .opcode = IBV_WR_SEND,
1136 .send_flags = IBV_SEND_SIGNALED,
1139 if (p->buf_large==NULL) {
1140 DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1141 pconn->cm_id, (uint32_t)wr.wr_id, len));
1143 DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1144 pconn->cm_id, (uint32_t)wr.wr_id, len));
1145 list.lkey = p->mr_large->lkey;
1148 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1150 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1151 rc, pconn->wr_sent);
1158 } /* else put the request into our own queue: */
1160 DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1162 /* to be sent by ibw_wc_send */
1163 /* regardless "normal" or [a part of] "large" packet */
1164 if (!p->queued_ref_cnt) {
1165 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1166 qprev, qnext); /* TODO: optimize */
1167 p->queued_msg = buf;
1169 p->queued_ref_cnt++;
1170 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1174 DEBUG(0, (ibw_lasterr));
1178 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1180 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1181 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1184 assert(len>=sizeof(uint32_t));
1185 *((uint32_t *)buf) = htonl(len);
1187 if (len > pctx->opts.recv_bufsize) {
1188 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1190 char *packet = (char *)buf;
1191 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1193 DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1194 pconn->cm_id, buf, len));
1196 /* single threaded => no race here: */
1197 assert(p->ref_cnt==0);
1198 while(rlen > recv_bufsize) {
1199 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1202 packet += recv_bufsize;
1203 rlen -= recv_bufsize;
1204 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1207 rc = ibw_send_packet(conn, packet, p, rlen);
1208 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1210 p->ref_cnt--; /* for the same handling */
1212 assert(p->ref_cnt==0);
1213 assert(p->queued_ref_cnt==0);
1215 rc = ibw_send_packet(conn, buf, p, len);
1220 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1222 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1223 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1224 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1230 if (p->buf_large!=NULL)
1231 ibw_free_mr(&p->buf_large, &p->mr_large);
1234 if (p->wr_id < pctx->opts.max_send_wr) {
1235 DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1236 DLIST_REMOVE(pconn->wr_list_used, p);
1237 DLIST_ADD(pconn->wr_list_avail, p);
1238 } else { /* "extra" packet */
1239 DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1240 DLIST_REMOVE(pconn->extra_sent, p);
1241 DLIST_ADD(pconn->extra_avail, p);
1247 const char *ibw_getLastError(void)