s3:rpc_server: Initialize array
[Samba.git] / ctdb / ib / ibwrapper.c
blobcf4efa579e2ee93bbc0a28e5e45e04c411a288fc
1 /*
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/>.
23 #include "replace.h"
24 #include "system/network.h"
26 #include <assert.h>
27 #include <talloc.h>
28 #include <tevent.h>
30 #include "lib/util/dlinklist.h"
31 #include "lib/util/debug.h"
33 #include "common/logging.h"
35 #include <rdma/rdma_cma_abi.h>
36 #include <rdma/rdma_cma.h>
38 #include "ibwrapper.h"
39 #include "ibwrapper_internal.h"
41 #define IBW_LASTERR_BUFSIZE 512
42 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
44 #define IBW_MAX_SEND_WR 256
45 #define IBW_MAX_RECV_WR 1024
46 #define IBW_RECV_BUFSIZE 256
47 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
49 static void ibw_event_handler_verbs(struct tevent_context *ev,
50 struct tevent_fd *fde, uint16_t flags, void *private_data);
51 static int ibw_fill_cq(struct ibw_conn *conn);
52 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
53 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
54 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
56 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
57 uint32_t n, struct ibv_mr **ppmr)
59 void *buf;
61 DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
62 buf = memalign(pctx->pagesize, n);
63 if (!buf) {
64 sprintf(ibw_lasterr, "couldn't allocate memory\n");
65 return NULL;
68 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
69 if (!*ppmr) {
70 sprintf(ibw_lasterr, "couldn't allocate mr\n");
71 free(buf);
72 return NULL;
75 return buf;
78 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
80 DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
81 if (*ppmr!=NULL) {
82 ibv_dereg_mr(*ppmr);
83 *ppmr = NULL;
85 if (*ppbuf) {
86 free(*ppbuf);
87 *ppbuf = NULL;
91 static int ibw_init_memory(struct ibw_conn *conn)
93 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
94 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
95 struct ibw_opts *opts = &pctx->opts;
96 int i;
97 struct ibw_wr *p;
99 DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
100 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
101 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
102 if (!pconn->buf_send) {
103 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
104 return -1;
107 pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
108 opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
109 if (!pconn->buf_recv) {
110 sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
111 return -1;
114 pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
115 assert(pconn->wr_index!=NULL);
117 for(i=0; i<opts->max_send_wr; i++) {
118 p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
119 p->buf = pconn->buf_send + (i * opts->recv_bufsize);
120 p->wr_id = i;
122 DLIST_ADD(pconn->wr_list_avail, p);
125 return 0;
128 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
130 DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
133 * tevent_fd must be removed before the fd is closed
135 TALLOC_FREE(pctx->cm_channel_event);
137 /* destroy cm */
138 if (pctx->cm_channel) {
139 rdma_destroy_event_channel(pctx->cm_channel);
140 pctx->cm_channel = NULL;
142 if (pctx->cm_id) {
143 rdma_destroy_id(pctx->cm_id);
144 pctx->cm_id = NULL;
147 return 0;
150 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
152 DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
153 return 0;
156 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
158 DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
159 pconn, pconn->cm_id));
161 /* pconn->wr_index is freed by talloc */
162 /* pconn->wr_index[i] are freed by talloc */
165 * tevent_fd must be removed before the fd is closed
167 TALLOC_FREE(pconn->verbs_channel_event);
169 /* destroy verbs */
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);
177 pconn->cq = NULL;
180 if (pconn->verbs_channel!=NULL) {
181 ibv_destroy_comp_channel(pconn->verbs_channel);
182 pconn->verbs_channel = NULL;
185 /* free memory regions */
186 ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
187 ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
189 if (pconn->pd) {
190 ibv_dealloc_pd(pconn->pd);
191 pconn->pd = NULL;
192 DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
195 if (pconn->cm_id) {
196 rdma_destroy_id(pconn->cm_id);
197 pconn->cm_id = NULL;
198 DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
201 return 0;
204 static int ibw_wr_destruct(struct ibw_wr *wr)
206 if (wr->buf_large!=NULL)
207 ibw_free_mr(&wr->buf_large, &wr->mr_large);
208 return 0;
211 static int ibw_conn_destruct(struct ibw_conn *conn)
213 DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
215 /* important here: ctx is a talloc _parent_ */
216 DLIST_REMOVE(conn->ctx->conn_list, conn);
217 return 0;
220 struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
222 struct ibw_conn *conn;
223 struct ibw_conn_priv *pconn;
225 assert(ctx!=NULL);
227 conn = talloc_zero(mem_ctx, struct ibw_conn);
228 assert(conn!=NULL);
229 talloc_set_destructor(conn, ibw_conn_destruct);
231 pconn = talloc_zero(conn, struct ibw_conn_priv);
232 assert(pconn!=NULL);
233 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
235 conn->ctx = ctx;
236 conn->internal = (void *)pconn;
238 DLIST_ADD(ctx->conn_list, conn);
240 return conn;
243 static int ibw_setup_cq_qp(struct ibw_conn *conn)
245 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
246 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
247 struct ibv_qp_init_attr init_attr;
248 struct ibv_qp_attr attr;
249 int rc;
251 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
253 /* init verbs */
254 pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
255 if (!pconn->verbs_channel) {
256 sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
257 return -1;
259 DEBUG(DEBUG_DEBUG, ("created channel %p\n", pconn->verbs_channel));
261 pconn->verbs_channel_event = tevent_add_fd(pctx->ectx, NULL, /* not pconn or conn */
262 pconn->verbs_channel->fd, TEVENT_FD_READ, ibw_event_handler_verbs, conn);
264 pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
265 if (!pconn->pd) {
266 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
267 return -1;
269 DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
271 /* init mr */
272 if (ibw_init_memory(conn))
273 return -1;
275 /* init cq */
276 pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
277 pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
278 conn, pconn->verbs_channel, 0);
279 if (pconn->cq==NULL) {
280 sprintf(ibw_lasterr, "ibv_create_cq failed\n");
281 return -1;
284 rc = ibv_req_notify_cq(pconn->cq, 0);
285 if (rc) {
286 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
287 return rc;
290 /* init qp */
291 memset(&init_attr, 0, sizeof(init_attr));
292 init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
293 init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
294 init_attr.cap.max_recv_sge = 1;
295 init_attr.cap.max_send_sge = 1;
296 init_attr.qp_type = IBV_QPT_RC;
297 init_attr.send_cq = pconn->cq;
298 init_attr.recv_cq = pconn->cq;
300 rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
301 if (rc) {
302 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
303 return rc;
305 /* elase result is in pconn->cm_id->qp */
307 rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
308 if (rc) {
309 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
310 return rc;
313 return ibw_fill_cq(conn);
316 static int ibw_refill_cq_recv(struct ibw_conn *conn)
318 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
319 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
320 int rc;
321 struct ibv_sge list = {
322 .addr = (uintptr_t) NULL, /* filled below */
323 .length = pctx->opts.recv_bufsize,
324 .lkey = pconn->mr_recv->lkey /* always the same */
326 struct ibv_recv_wr wr = {
327 .wr_id = 0, /* filled below */
328 .sg_list = &list,
329 .num_sge = 1,
331 struct ibv_recv_wr *bad_wr;
333 DEBUG(DEBUG_DEBUG, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
335 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
336 wr.wr_id = pconn->recv_index;
337 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
339 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
340 if (rc) {
341 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
342 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
343 return -2;
346 return 0;
349 static int ibw_fill_cq(struct ibw_conn *conn)
351 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
352 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
353 int i, rc;
354 struct ibv_sge list = {
355 .addr = (uintptr_t) NULL, /* filled below */
356 .length = pctx->opts.recv_bufsize,
357 .lkey = pconn->mr_recv->lkey /* always the same */
359 struct ibv_recv_wr wr = {
360 .wr_id = 0, /* filled below */
361 .sg_list = &list,
362 .num_sge = 1,
364 struct ibv_recv_wr *bad_wr;
366 DEBUG(DEBUG_DEBUG, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
368 for(i = pctx->opts.max_recv_wr; i!=0; i--) {
369 list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
370 wr.wr_id = pconn->recv_index;
371 pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;
373 rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
374 if (rc) {
375 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
376 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
377 return -2;
381 return 0;
384 static int ibw_manage_connect(struct ibw_conn *conn)
386 struct rdma_conn_param conn_param;
387 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
388 int rc;
390 DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
392 if (ibw_setup_cq_qp(conn))
393 return -1;
395 /* cm connect */
396 memset(&conn_param, 0, sizeof conn_param);
397 conn_param.responder_resources = 1;
398 conn_param.initiator_depth = 1;
399 conn_param.retry_count = 10;
401 rc = rdma_connect(pconn->cm_id, &conn_param);
402 if (rc)
403 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
405 return rc;
408 static void ibw_event_handler_cm(struct tevent_context *ev,
409 struct tevent_fd *fde, uint16_t flags, void *private_data)
411 int rc;
412 struct ibw_ctx *ctx = talloc_get_type(private_data, struct ibw_ctx);
413 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
414 struct ibw_conn *conn = NULL;
415 struct ibw_conn_priv *pconn = NULL;
416 struct rdma_cm_id *cma_id = NULL;
417 struct rdma_cm_event *event = NULL;
419 assert(ctx!=NULL);
421 rc = rdma_get_cm_event(pctx->cm_channel, &event);
422 if (rc) {
423 ctx->state = IBWS_ERROR;
424 event = NULL;
425 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
426 goto error;
428 cma_id = event->id;
430 DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
431 (cma_id == pctx->cm_id) ? "parent" : "child"));
433 switch (event->event) {
434 case RDMA_CM_EVENT_ADDR_RESOLVED:
435 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
436 /* continuing from ibw_connect ... */
437 rc = rdma_resolve_route(cma_id, 2000);
438 if (rc) {
439 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
440 goto error;
442 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
443 break;
445 case RDMA_CM_EVENT_ROUTE_RESOLVED:
446 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
447 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
448 assert(cma_id->context!=NULL);
449 conn = talloc_get_type(cma_id->context, struct ibw_conn);
451 rc = ibw_manage_connect(conn);
452 if (rc)
453 goto error;
455 break;
457 case RDMA_CM_EVENT_CONNECT_REQUEST:
458 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
459 ctx->state = IBWS_CONNECT_REQUEST;
460 conn = ibw_conn_new(ctx, ctx);
461 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
462 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
463 cma_id->context = (void *)conn;
464 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
466 if (ibw_setup_cq_qp(conn))
467 goto error;
469 conn->state = IBWC_INIT;
470 pctx->connstate_func(ctx, conn);
472 /* continued at ibw_accept when invoked by the func above */
473 if (!pconn->is_accepted) {
474 rc = rdma_reject(cma_id, NULL, 0);
475 if (rc)
476 DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
477 talloc_free(conn);
478 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
481 /* TODO: clarify whether if it's needed by upper layer: */
482 ctx->state = IBWS_READY;
483 pctx->connstate_func(ctx, NULL);
485 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
486 break;
488 case RDMA_CM_EVENT_ESTABLISHED:
489 /* expected after ibw_accept and ibw_connect[not directly] */
490 DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
491 conn = talloc_get_type(cma_id->context, struct ibw_conn);
492 assert(conn!=NULL); /* important assumption */
494 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
496 /* client conn is up */
497 conn->state = IBWC_CONNECTED;
499 /* both ctx and conn have changed */
500 pctx->connstate_func(ctx, conn);
501 break;
503 case RDMA_CM_EVENT_ADDR_ERROR:
504 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
505 goto error;
506 case RDMA_CM_EVENT_ROUTE_ERROR:
507 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
508 goto error;
509 case RDMA_CM_EVENT_CONNECT_ERROR:
510 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
511 goto error;
512 case RDMA_CM_EVENT_UNREACHABLE:
513 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
514 goto error;
515 case RDMA_CM_EVENT_REJECTED:
516 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
517 DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
518 conn = talloc_get_type(cma_id->context, struct ibw_conn);
519 if (conn) {
520 /* must be done BEFORE connstate */
521 if ((rc=rdma_ack_cm_event(event)))
522 DEBUG(DEBUG_ERR, ("reject/rdma_ack_cm_event failed with %d\n", rc));
523 event = NULL; /* not to touch cma_id or conn */
524 conn->state = IBWC_ERROR;
525 /* it should free the conn */
526 pctx->connstate_func(NULL, conn);
528 break; /* this is not strictly an error */
530 case RDMA_CM_EVENT_DISCONNECTED:
531 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
532 if ((rc=rdma_ack_cm_event(event)))
533 DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
534 event = NULL; /* don't ack more */
536 if (cma_id!=pctx->cm_id) {
537 DEBUG(DEBUG_ERR, ("client DISCONNECT event cm_id=%p\n", cma_id));
538 conn = talloc_get_type(cma_id->context, struct ibw_conn);
539 conn->state = IBWC_DISCONNECTED;
540 pctx->connstate_func(NULL, conn);
542 break;
544 case RDMA_CM_EVENT_DEVICE_REMOVAL:
545 sprintf(ibw_lasterr, "cma detected device removal!\n");
546 goto error;
548 default:
549 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
550 goto error;
553 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
554 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
555 goto error;
558 return;
559 error:
560 DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
562 if (event!=NULL) {
563 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
564 conn = talloc_get_type(cma_id->context, struct ibw_conn);
565 if (conn) {
566 conn->state = IBWC_ERROR;
567 pctx->connstate_func(NULL, conn);
569 } else {
570 ctx->state = IBWS_ERROR;
571 pctx->connstate_func(ctx, NULL);
574 if ((rc=rdma_ack_cm_event(event))!=0) {
575 DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
579 return;
582 static void ibw_event_handler_verbs(struct tevent_context *ev,
583 struct tevent_fd *fde, uint16_t flags, void *private_data)
585 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
586 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
587 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
589 struct ibv_wc wc;
590 int rc;
591 struct ibv_cq *ev_cq;
592 void *ev_ctx;
594 DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
596 /* TODO: check whether if it's good to have more channels here... */
597 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
598 if (rc) {
599 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
600 goto error;
602 if (ev_cq != pconn->cq) {
603 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
604 goto error;
606 rc = ibv_req_notify_cq(pconn->cq, 0);
607 if (rc) {
608 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
609 goto error;
612 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
613 if (wc.status) {
614 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
615 wc.status, wc.opcode, rc);
616 goto error;
619 switch(wc.opcode) {
620 case IBV_WC_SEND:
621 DEBUG(DEBUG_DEBUG, ("send completion\n"));
622 if (ibw_wc_send(conn, &wc))
623 goto error;
624 break;
626 case IBV_WC_RDMA_WRITE:
627 DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
628 break;
630 case IBV_WC_RDMA_READ:
631 DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
632 break;
634 case IBV_WC_RECV:
635 DEBUG(DEBUG_DEBUG, ("recv completion\n"));
636 if (ibw_wc_recv(conn, &wc))
637 goto error;
638 break;
640 default:
641 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
642 goto error;
645 if (rc!=0) {
646 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
647 goto error;
650 ibv_ack_cq_events(pconn->cq, 1);
652 return;
653 error:
654 ibv_ack_cq_events(pconn->cq, 1);
656 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
658 if (conn->state!=IBWC_ERROR) {
659 conn->state = IBWC_ERROR;
660 pctx->connstate_func(NULL, conn);
664 static int ibw_process_queue(struct ibw_conn *conn)
666 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
667 struct ibw_ctx_priv *pctx;
668 struct ibw_wr *p;
669 int rc;
670 uint32_t msg_size;
672 if (pconn->queue==NULL)
673 return 0; /* NOP */
675 p = pconn->queue;
677 /* we must have at least 1 fragment to send */
678 assert(p->queued_ref_cnt>0);
679 p->queued_ref_cnt--;
681 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
682 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
684 assert(p->queued_msg!=NULL);
685 assert(msg_size!=0);
687 DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
688 p->queued_ref_cnt, msg_size));
690 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
692 /* was this the last fragment? */
693 if (p->queued_ref_cnt) {
694 p->queued_msg += pctx->opts.recv_bufsize;
695 } else {
696 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
697 p->queued_msg = NULL;
700 return rc;
703 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
705 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
706 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
707 struct ibw_wr *p;
708 int send_index;
710 DEBUG(DEBUG_DEBUG, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
711 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
713 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
714 assert(wc->wr_id >= pctx->opts.max_recv_wr);
715 send_index = wc->wr_id - pctx->opts.max_recv_wr;
716 pconn->wr_sent--;
718 if (send_index < pctx->opts.max_send_wr) {
719 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
720 p = pconn->wr_index[send_index];
721 if (p->buf_large!=NULL) {
722 if (p->ref_cnt) {
723 /* awaiting more of it... */
724 p->ref_cnt--;
725 } else {
726 ibw_free_mr(&p->buf_large, &p->mr_large);
727 DLIST_REMOVE(pconn->wr_list_used, p);
728 DLIST_ADD(pconn->wr_list_avail, p);
730 } else { /* nasty - but necessary */
731 DLIST_REMOVE(pconn->wr_list_used, p);
732 DLIST_ADD(pconn->wr_list_avail, p);
734 } else { /* "extra" request - not optimized */
735 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
736 for(p=pconn->extra_sent; p!=NULL; p=p->next)
737 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
738 break;
739 if (p==NULL) {
740 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
741 return -1;
743 if (p->ref_cnt) {
744 p->ref_cnt--;
745 } else {
746 ibw_free_mr(&p->buf_large, &p->mr_large);
747 DLIST_REMOVE(pconn->extra_sent, p);
748 DLIST_ADD(pconn->extra_avail, p);
752 return ibw_process_queue(conn);
755 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
756 struct ibw_part *part, char **pp, uint32_t add_len, int info)
758 DEBUG(DEBUG_DEBUG, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
759 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
761 /* allocate more if necessary - it's an "evergrowing" buffer... */
762 if (part->len + add_len > part->bufsize) {
763 if (part->buf==NULL) {
764 assert(part->len==0);
765 part->buf = talloc_size(pconn, add_len);
766 if (part->buf==NULL) {
767 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
768 add_len, info);
769 return -1;
771 part->bufsize = add_len;
772 } else {
773 part->buf = talloc_realloc_size(pconn,
774 part->buf, part->len + add_len);
775 if (part->buf==NULL) {
776 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
777 part->len, add_len, info);
778 return -1;
781 part->bufsize = part->len + add_len;
784 /* consume pp */
785 memcpy(part->buf + part->len, *pp, add_len);
786 *pp += add_len;
787 part->len += add_len;
788 part->to_read -= add_len;
790 return 0;
793 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
794 struct ibw_part *part, uint32_t threshold)
796 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
797 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
799 if (part->bufsize > threshold) {
800 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
801 pconn->cm_id, part->bufsize, threshold));
802 talloc_free(part->buf);
803 part->buf = talloc_size(pconn, threshold);
804 if (part->buf==NULL) {
805 sprintf(ibw_lasterr, "talloc_size failed\n");
806 return -1;
808 part->bufsize = threshold;
810 return 0;
813 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
815 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
816 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
817 struct ibw_part *part = &pconn->part;
818 char *p;
819 uint32_t remain = wc->byte_len;
821 DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
822 pconn->cm_id, (uint32_t)wc->wr_id, remain));
824 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
825 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
826 assert(wc->byte_len <= pctx->opts.recv_bufsize);
828 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
830 while(remain) {
831 /* here always true: (part->len!=0 && part->to_read!=0) ||
832 (part->len==0 && part->to_read==0) */
833 if (part->len) { /* is there a partial msg to be continued? */
834 int read_len = (part->to_read<=remain) ? part->to_read : remain;
835 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
836 goto error;
837 remain -= read_len;
839 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
840 assert(part->len==sizeof(uint32_t));
841 /* set it again now... */
842 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
843 if (part->to_read<sizeof(uint32_t)) {
844 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
845 goto error;
847 part->to_read -= sizeof(uint32_t); /* it's already read */
850 if (part->to_read==0) {
851 if (pctx->receive_func(conn, part->buf, part->len) != 0) {
852 goto error;
854 part->len = 0; /* tells not having partial data (any more) */
855 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
856 goto error;
858 } else {
859 if (remain>=sizeof(uint32_t)) {
860 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
861 if (msglen<sizeof(uint32_t)) {
862 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
863 goto error;
866 /* mostly awaited case: */
867 if (msglen<=remain) {
868 if (pctx->receive_func(conn, p, msglen) != 0) {
869 goto error;
871 p += msglen;
872 remain -= msglen;
873 } else {
874 part->to_read = msglen;
875 /* part->len is already 0 */
876 if (ibw_append_to_part(pconn, part, &p, remain, 422))
877 goto error;
878 remain = 0; /* to be continued ... */
879 /* part->to_read > 0 here */
881 } else { /* edge case: */
882 part->to_read = sizeof(uint32_t);
883 /* part->len is already 0 */
884 if (ibw_append_to_part(pconn, part, &p, remain, 423))
885 goto error;
886 remain = 0;
887 /* part->to_read > 0 here */
890 } /* <remain> is always decreased at least by 1 */
892 if (ibw_refill_cq_recv(conn))
893 goto error;
895 return 0;
897 error:
898 DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
899 return -1;
902 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
904 int i;
905 const char *name, *value;
907 DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
909 opts->max_send_wr = IBW_MAX_SEND_WR;
910 opts->max_recv_wr = IBW_MAX_RECV_WR;
911 opts->recv_bufsize = IBW_RECV_BUFSIZE;
912 opts->recv_threshold = IBW_RECV_THRESHOLD;
914 for(i=0; i<nattr; i++) {
915 name = attr[i].name;
916 value = attr[i].value;
918 assert(name!=NULL && value!=NULL);
919 if (strcmp(name, "max_send_wr")==0)
920 opts->max_send_wr = atoi(value);
921 else if (strcmp(name, "max_recv_wr")==0)
922 opts->max_recv_wr = atoi(value);
923 else if (strcmp(name, "recv_bufsize")==0)
924 opts->recv_bufsize = atoi(value);
925 else if (strcmp(name, "recv_threshold")==0)
926 opts->recv_threshold = atoi(value);
927 else {
928 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
929 return -1;
932 return 0;
935 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
936 void *ctx_userdata,
937 ibw_connstate_fn_t ibw_connstate,
938 ibw_receive_fn_t ibw_receive,
939 struct tevent_context *ectx)
941 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
942 struct ibw_ctx_priv *pctx;
943 int rc;
945 DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
947 /* initialize basic data structures */
948 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
950 assert(ctx!=NULL);
951 ibw_lasterr[0] = '\0';
952 talloc_set_destructor(ctx, ibw_ctx_destruct);
953 ctx->ctx_userdata = ctx_userdata;
955 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
956 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
957 ctx->internal = (void *)pctx;
958 assert(pctx!=NULL);
960 pctx->connstate_func = ibw_connstate;
961 pctx->receive_func = ibw_receive;
963 pctx->ectx = ectx;
965 /* process attributes */
966 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
967 goto cleanup;
969 /* init cm */
970 pctx->cm_channel = rdma_create_event_channel();
971 if (!pctx->cm_channel) {
972 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
973 goto cleanup;
976 pctx->cm_channel_event = tevent_add_fd(pctx->ectx, pctx,
977 pctx->cm_channel->fd, TEVENT_FD_READ, ibw_event_handler_cm, ctx);
979 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
980 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
981 #else
982 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
983 #endif
984 if (rc) {
985 rc = errno;
986 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
987 goto cleanup;
989 DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
991 pctx->pagesize = sysconf(_SC_PAGESIZE);
993 return ctx;
994 /* don't put code here */
995 cleanup:
996 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
998 if (ctx)
999 talloc_free(ctx);
1001 return NULL;
1004 int ibw_stop(struct ibw_ctx *ctx)
1006 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1007 struct ibw_conn *p;
1009 DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
1011 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1012 if (p->state==IBWC_ERROR || p->state==IBWC_CONNECTED) {
1013 if (ibw_disconnect(p))
1014 return -1;
1018 ctx->state = IBWS_STOPPED;
1019 pctx->connstate_func(ctx, NULL);
1021 return 0;
1024 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1026 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1027 int rc;
1029 DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
1030 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1031 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1032 if (rc) {
1033 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1034 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1035 return rc;
1037 DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
1039 return 0;
1042 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1044 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1045 int rc;
1047 DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
1048 rc = rdma_listen(pctx->cm_id, backlog);
1049 if (rc) {
1050 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1051 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1052 return rc;
1055 return 0;
1058 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1060 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1061 struct rdma_conn_param conn_param;
1062 int rc;
1064 DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1065 conn->conn_userdata = conn_userdata;
1067 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1068 conn_param.responder_resources = 1;
1069 conn_param.initiator_depth = 1;
1070 rc = rdma_accept(pconn->cm_id, &conn_param);
1071 if (rc) {
1072 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1073 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1074 return -1;;
1077 pconn->is_accepted = 1;
1079 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1081 return 0;
1084 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1086 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1087 struct ibw_conn_priv *pconn = NULL;
1088 int rc;
1090 assert(conn!=NULL);
1092 conn->conn_userdata = conn_userdata;
1093 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1094 DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1095 ntohs(serv_addr->sin_port)));
1097 /* clean previous - probably half - initialization */
1098 if (ibw_conn_priv_destruct(pconn)) {
1099 DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1100 return -1;
1103 /* init cm */
1104 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1105 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1106 #else
1107 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1108 #endif
1109 if (rc) {
1110 rc = errno;
1111 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1112 talloc_free(conn);
1113 return -1;
1115 DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1117 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1118 if (rc) {
1119 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1120 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1121 talloc_free(conn);
1122 return -1;
1125 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1127 return 0;
1130 int ibw_disconnect(struct ibw_conn *conn)
1132 int rc;
1133 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1135 DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1137 assert(pconn!=NULL);
1139 switch(conn->state) {
1140 case IBWC_ERROR:
1141 ibw_conn_priv_destruct(pconn); /* do this here right now */
1142 break;
1143 case IBWC_CONNECTED:
1144 rc = rdma_disconnect(pconn->cm_id);
1145 if (rc) {
1146 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1147 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1148 return rc;
1150 break;
1151 default:
1152 DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
1153 break;
1156 return 0;
1159 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1161 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1162 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1163 struct ibw_wr *p = pconn->wr_list_avail;
1165 if (p!=NULL) {
1166 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1168 DLIST_REMOVE(pconn->wr_list_avail, p);
1169 DLIST_ADD(pconn->wr_list_used, p);
1171 if (len <= pctx->opts.recv_bufsize) {
1172 *buf = (void *)p->buf;
1173 } else {
1174 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1175 if (p->buf_large==NULL) {
1176 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1177 goto error;
1179 *buf = (void *)p->buf_large;
1181 /* p->wr_id is already filled in ibw_init_memory */
1182 } else {
1183 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1184 /* not optimized */
1185 p = pconn->extra_avail;
1186 if (!p) {
1187 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1188 talloc_set_destructor(p, ibw_wr_destruct);
1189 if (p==NULL) {
1190 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1191 goto error;
1193 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1194 pconn->extra_max++;
1195 switch(pconn->extra_max) {
1196 case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
1197 case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
1198 case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
1199 case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
1200 default: break;
1204 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1205 if (p->buf_large==NULL) {
1206 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1207 goto error;
1209 *buf = (void *)p->buf_large;
1211 DLIST_REMOVE(pconn->extra_avail, p);
1212 /* we don't have prepared index for this, so that
1213 * we will have to find this by wr_id later on */
1214 DLIST_ADD(pconn->extra_sent, p);
1217 *key = (void *)p;
1219 return 0;
1220 error:
1221 DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1222 return -1;
1226 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1228 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1229 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1230 int rc;
1232 /* can we send it right now? */
1233 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1234 struct ibv_send_wr *bad_wr;
1235 struct ibv_sge list = {
1236 .addr = (uintptr_t)buf,
1237 .length = len,
1238 .lkey = pconn->mr_send->lkey
1240 struct ibv_send_wr wr = {
1241 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1242 .sg_list = &list,
1243 .num_sge = 1,
1244 .opcode = IBV_WR_SEND,
1245 .send_flags = IBV_SEND_SIGNALED,
1248 if (p->buf_large==NULL) {
1249 DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1250 pconn->cm_id, (uint32_t)wr.wr_id, len));
1251 } else {
1252 DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1253 pconn->cm_id, (uint32_t)wr.wr_id, len));
1254 list.lkey = p->mr_large->lkey;
1257 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1258 if (rc) {
1259 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1260 rc, pconn->wr_sent);
1261 goto error;
1264 pconn->wr_sent++;
1266 return rc;
1267 } /* else put the request into our own queue: */
1269 DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1271 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1273 /* to be sent by ibw_wc_send */
1274 /* regardless "normal" or [a part of] "large" packet */
1275 if (!p->queued_ref_cnt) {
1276 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1277 qprev, qnext); /* TODO: optimize */
1278 p->queued_msg = buf;
1280 p->queued_ref_cnt++;
1281 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1283 return 0;
1284 error:
1285 DEBUG(DEBUG_ERR, ("%s", ibw_lasterr));
1286 return -1;
1289 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1291 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1292 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1293 int rc;
1295 assert(len>=sizeof(uint32_t));
1296 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1298 if (len > pctx->opts.recv_bufsize) {
1299 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1300 int rlen = len;
1301 char *packet = (char *)buf;
1302 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1304 DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1305 pconn->cm_id, buf, len));
1307 /* single threaded => no race here: */
1308 assert(p->ref_cnt==0);
1309 while(rlen > recv_bufsize) {
1310 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1311 if (rc)
1312 return rc;
1313 packet += recv_bufsize;
1314 rlen -= recv_bufsize;
1315 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1317 if (rlen) {
1318 rc = ibw_send_packet(conn, packet, p, rlen);
1319 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1321 p->ref_cnt--; /* for the same handling */
1322 } else {
1323 assert(p->ref_cnt==0);
1324 assert(p->queued_ref_cnt==0);
1326 rc = ibw_send_packet(conn, buf, p, len);
1328 return rc;
1331 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1333 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1334 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1335 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1337 assert(p!=NULL);
1338 assert(buf!=NULL);
1339 assert(conn!=NULL);
1341 if (p->buf_large!=NULL)
1342 ibw_free_mr(&p->buf_large, &p->mr_large);
1344 /* parallel case */
1345 if (p->wr_id < pctx->opts.max_send_wr) {
1346 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1347 DLIST_REMOVE(pconn->wr_list_used, p);
1348 DLIST_ADD(pconn->wr_list_avail, p);
1349 } else { /* "extra" packet */
1350 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1351 DLIST_REMOVE(pconn->extra_sent, p);
1352 DLIST_ADD(pconn->extra_avail, p);
1355 return 0;
1358 const char *ibw_getLastError(void)
1360 return ibw_lasterr;