ctdb-tests: Remove dependency on log ringbuffer from missing IP test
[Samba.git] / ctdb / ib / ibwrapper.c
blob3daab3e3c72b75a0833b13189203f785e366c325
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 <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31 #include <arpa/inet.h>
32 #include <malloc.h>
33 #include <assert.h>
34 #include <unistd.h>
36 #include "includes.h"
37 #include "ibwrapper.h"
39 #include <infiniband/kern-abi.h>
40 #include <rdma/rdma_cma_abi.h>
41 #include <rdma/rdma_cma.h>
43 #include "ibwrapper_internal.h"
44 #include "lib/util/dlinklist.h"
46 #define IBW_LASTERR_BUFSIZE 512
47 static char ibw_lasterr[IBW_LASTERR_BUFSIZE];
49 #define IBW_MAX_SEND_WR 256
50 #define IBW_MAX_RECV_WR 1024
51 #define IBW_RECV_BUFSIZE 256
52 #define IBW_RECV_THRESHOLD (1 * 1024 * 1024)
54 static void ibw_event_handler_verbs(struct event_context *ev,
55 struct fd_event *fde, uint16_t flags, void *private_data);
56 static int ibw_fill_cq(struct ibw_conn *conn);
57 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
58 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
59 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);
61 static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
62 uint32_t n, struct ibv_mr **ppmr)
64 void *buf;
66 DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
67 buf = memalign(pctx->pagesize, n);
68 if (!buf) {
69 sprintf(ibw_lasterr, "couldn't allocate memory\n");
70 return NULL;
73 *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
74 if (!*ppmr) {
75 sprintf(ibw_lasterr, "couldn't allocate mr\n");
76 free(buf);
77 return NULL;
80 return buf;
83 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
85 DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
86 if (*ppmr!=NULL) {
87 ibv_dereg_mr(*ppmr);
88 *ppmr = NULL;
90 if (*ppbuf) {
91 free(*ppbuf);
92 *ppbuf = NULL;
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;
101 int i;
102 struct ibw_wr *p;
104 DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
105 pconn->buf_send = ibw_alloc_mr(pctx, pconn,
106 opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
107 if (!pconn->buf_send) {
108 sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
109 return -1;
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");
116 return -1;
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);
125 p->wr_id = i;
127 DLIST_ADD(pconn->wr_list_avail, p);
130 return 0;
133 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
135 DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
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_channel_event) {
143 /* TODO: do we have to do this here? */
144 talloc_free(pctx->cm_channel_event);
145 pctx->cm_channel_event = NULL;
147 if (pctx->cm_id) {
148 rdma_destroy_id(pctx->cm_id);
149 pctx->cm_id = NULL;
152 return 0;
155 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
157 DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
158 return 0;
161 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
163 DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
164 pconn, pconn->cm_id));
166 /* pconn->wr_index is freed by talloc */
167 /* pconn->wr_index[i] are freed by talloc */
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 /* 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);
195 if (pconn->pd) {
196 ibv_dealloc_pd(pconn->pd);
197 pconn->pd = NULL;
198 DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
201 if (pconn->cm_id) {
202 rdma_destroy_id(pconn->cm_id);
203 pconn->cm_id = NULL;
204 DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
207 return 0;
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);
214 return 0;
217 static int ibw_conn_destruct(struct ibw_conn *conn)
219 DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
221 /* important here: ctx is a talloc _parent_ */
222 DLIST_REMOVE(conn->ctx->conn_list, conn);
223 return 0;
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;
231 assert(ctx!=NULL);
233 conn = talloc_zero(mem_ctx, struct ibw_conn);
234 assert(conn!=NULL);
235 talloc_set_destructor(conn, ibw_conn_destruct);
237 pconn = talloc_zero(conn, struct ibw_conn_priv);
238 assert(pconn!=NULL);
239 talloc_set_destructor(pconn, ibw_conn_priv_destruct);
241 conn->ctx = ctx;
242 conn->internal = (void *)pconn;
244 DLIST_ADD(ctx->conn_list, conn);
246 return 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;
255 int rc;
257 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
259 /* init verbs */
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);
263 return -1;
265 DEBUG(DEBUG_DEBUG, ("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);
271 if (!pconn->pd) {
272 sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
273 return -1;
275 DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
277 /* init mr */
278 if (ibw_init_memory(conn))
279 return -1;
281 /* init cq */
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");
287 return -1;
290 rc = ibv_req_notify_cq(pconn->cq, 0);
291 if (rc) {
292 sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
293 return rc;
296 /* init qp */
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);
307 if (rc) {
308 sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
309 return 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);
314 if (rc) {
315 sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
316 return 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);
326 int rc;
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 */
334 .sg_list = &list,
335 .num_sge = 1,
337 struct ibv_recv_wr *bad_wr;
339 DEBUG(DEBUG_DEBUG, ("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);
346 if (rc) {
347 sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
348 DEBUG(DEBUG_ERR, (ibw_lasterr));
349 return -2;
352 return 0;
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);
359 int i, rc;
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 */
367 .sg_list = &list,
368 .num_sge = 1,
370 struct ibv_recv_wr *bad_wr;
372 DEBUG(DEBUG_DEBUG, ("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);
380 if (rc) {
381 sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
382 DEBUG(DEBUG_ERR, (ibw_lasterr));
383 return -2;
387 return 0;
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);
394 int rc;
396 DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
398 if (ibw_setup_cq_qp(conn))
399 return -1;
401 /* cm connect */
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);
408 if (rc)
409 sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
411 return rc;
414 static void ibw_event_handler_cm(struct event_context *ev,
415 struct fd_event *fde, uint16_t flags, void *private_data)
417 int rc;
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;
425 assert(ctx!=NULL);
427 rc = rdma_get_cm_event(pctx->cm_channel, &event);
428 if (rc) {
429 ctx->state = IBWS_ERROR;
430 event = NULL;
431 sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
432 goto error;
434 cma_id = event->id;
436 DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
437 (cma_id == pctx->cm_id) ? "parent" : "child"));
439 switch (event->event) {
440 case RDMA_CM_EVENT_ADDR_RESOLVED:
441 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
442 /* continuing from ibw_connect ... */
443 rc = rdma_resolve_route(cma_id, 2000);
444 if (rc) {
445 sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
446 goto error;
448 /* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
449 break;
451 case RDMA_CM_EVENT_ROUTE_RESOLVED:
452 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
453 /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
454 assert(cma_id->context!=NULL);
455 conn = talloc_get_type(cma_id->context, struct ibw_conn);
457 rc = ibw_manage_connect(conn);
458 if (rc)
459 goto error;
461 break;
463 case RDMA_CM_EVENT_CONNECT_REQUEST:
464 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
465 ctx->state = IBWS_CONNECT_REQUEST;
466 conn = ibw_conn_new(ctx, ctx);
467 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
468 pconn->cm_id = cma_id; /* !!! event will be freed but id not */
469 cma_id->context = (void *)conn;
470 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
472 if (ibw_setup_cq_qp(conn))
473 goto error;
475 conn->state = IBWC_INIT;
476 pctx->connstate_func(ctx, conn);
478 /* continued at ibw_accept when invoked by the func above */
479 if (!pconn->is_accepted) {
480 rc = rdma_reject(cma_id, NULL, 0);
481 if (rc)
482 DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
483 talloc_free(conn);
484 DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
487 /* TODO: clarify whether if it's needed by upper layer: */
488 ctx->state = IBWS_READY;
489 pctx->connstate_func(ctx, NULL);
491 /* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
492 break;
494 case RDMA_CM_EVENT_ESTABLISHED:
495 /* expected after ibw_accept and ibw_connect[not directly] */
496 DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
497 conn = talloc_get_type(cma_id->context, struct ibw_conn);
498 assert(conn!=NULL); /* important assumption */
500 DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
502 /* client conn is up */
503 conn->state = IBWC_CONNECTED;
505 /* both ctx and conn have changed */
506 pctx->connstate_func(ctx, conn);
507 break;
509 case RDMA_CM_EVENT_ADDR_ERROR:
510 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
511 case RDMA_CM_EVENT_ROUTE_ERROR:
512 sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
513 case RDMA_CM_EVENT_CONNECT_ERROR:
514 sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
515 case RDMA_CM_EVENT_UNREACHABLE:
516 sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
517 goto error;
518 case RDMA_CM_EVENT_REJECTED:
519 sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
520 DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
521 conn = talloc_get_type(cma_id->context, struct ibw_conn);
522 if (conn) {
523 /* must be done BEFORE connstate */
524 if ((rc=rdma_ack_cm_event(event)))
525 DEBUG(DEBUG_ERR, ("reject/rdma_ack_cm_event failed with %d\n", rc));
526 event = NULL; /* not to touch cma_id or conn */
527 conn->state = IBWC_ERROR;
528 /* it should free the conn */
529 pctx->connstate_func(NULL, conn);
531 break; /* this is not strictly an error */
533 case RDMA_CM_EVENT_DISCONNECTED:
534 DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
535 if ((rc=rdma_ack_cm_event(event)))
536 DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
537 event = NULL; /* don't ack more */
539 if (cma_id!=pctx->cm_id) {
540 DEBUG(DEBUG_ERR, ("client DISCONNECT event cm_id=%p\n", cma_id));
541 conn = talloc_get_type(cma_id->context, struct ibw_conn);
542 conn->state = IBWC_DISCONNECTED;
543 pctx->connstate_func(NULL, conn);
545 break;
547 case RDMA_CM_EVENT_DEVICE_REMOVAL:
548 sprintf(ibw_lasterr, "cma detected device removal!\n");
549 goto error;
551 default:
552 sprintf(ibw_lasterr, "unknown event %d\n", event->event);
553 goto error;
556 if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
557 sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
558 goto error;
561 return;
562 error:
563 DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
565 if (event!=NULL) {
566 if (cma_id!=NULL && cma_id!=pctx->cm_id) {
567 conn = talloc_get_type(cma_id->context, struct ibw_conn);
568 if (conn) {
569 conn->state = IBWC_ERROR;
570 pctx->connstate_func(NULL, conn);
572 } else {
573 ctx->state = IBWS_ERROR;
574 pctx->connstate_func(ctx, NULL);
577 if ((rc=rdma_ack_cm_event(event))!=0) {
578 DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
582 return;
585 static void ibw_event_handler_verbs(struct event_context *ev,
586 struct fd_event *fde, uint16_t flags, void *private_data)
588 struct ibw_conn *conn = talloc_get_type(private_data, struct ibw_conn);
589 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
590 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
592 struct ibv_wc wc;
593 int rc;
594 struct ibv_cq *ev_cq;
595 void *ev_ctx;
597 DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
599 /* TODO: check whether if it's good to have more channels here... */
600 rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
601 if (rc) {
602 sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
603 goto error;
605 if (ev_cq != pconn->cq) {
606 sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
607 goto error;
609 rc = ibv_req_notify_cq(pconn->cq, 0);
610 if (rc) {
611 sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
612 goto error;
615 while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
616 if (wc.status) {
617 sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
618 wc.status, wc.opcode, rc);
619 goto error;
622 switch(wc.opcode) {
623 case IBV_WC_SEND:
624 DEBUG(DEBUG_DEBUG, ("send completion\n"));
625 if (ibw_wc_send(conn, &wc))
626 goto error;
627 break;
629 case IBV_WC_RDMA_WRITE:
630 DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
631 break;
633 case IBV_WC_RDMA_READ:
634 DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
635 break;
637 case IBV_WC_RECV:
638 DEBUG(DEBUG_DEBUG, ("recv completion\n"));
639 if (ibw_wc_recv(conn, &wc))
640 goto error;
641 break;
643 default:
644 sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
645 goto error;
648 if (rc!=0) {
649 sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
650 goto error;
653 ibv_ack_cq_events(pconn->cq, 1);
655 return;
656 error:
657 ibv_ack_cq_events(pconn->cq, 1);
659 DEBUG(DEBUG_ERR, (ibw_lasterr));
661 if (conn->state!=IBWC_ERROR) {
662 conn->state = IBWC_ERROR;
663 pctx->connstate_func(NULL, conn);
667 static int ibw_process_queue(struct ibw_conn *conn)
669 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
670 struct ibw_ctx_priv *pctx;
671 struct ibw_wr *p;
672 int rc;
673 uint32_t msg_size;
675 if (pconn->queue==NULL)
676 return 0; /* NOP */
678 p = pconn->queue;
680 /* we must have at least 1 fragment to send */
681 assert(p->queued_ref_cnt>0);
682 p->queued_ref_cnt--;
684 pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
685 msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
687 assert(p->queued_msg!=NULL);
688 assert(msg_size!=0);
690 DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
691 p->queued_ref_cnt, msg_size));
693 rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
695 /* was this the last fragment? */
696 if (p->queued_ref_cnt) {
697 p->queued_msg += pctx->opts.recv_bufsize;
698 } else {
699 DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
700 p->queued_msg = NULL;
703 return rc;
706 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
708 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
709 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
710 struct ibw_wr *p;
711 int send_index;
713 DEBUG(DEBUG_DEBUG, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
714 pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
716 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
717 assert(wc->wr_id >= pctx->opts.max_recv_wr);
718 send_index = wc->wr_id - pctx->opts.max_recv_wr;
719 pconn->wr_sent--;
721 if (send_index < pctx->opts.max_send_wr) {
722 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
723 p = pconn->wr_index[send_index];
724 if (p->buf_large!=NULL) {
725 if (p->ref_cnt) {
726 /* awaiting more of it... */
727 p->ref_cnt--;
728 } else {
729 ibw_free_mr(&p->buf_large, &p->mr_large);
730 DLIST_REMOVE(pconn->wr_list_used, p);
731 DLIST_ADD(pconn->wr_list_avail, p);
733 } else { /* nasty - but necessary */
734 DLIST_REMOVE(pconn->wr_list_used, p);
735 DLIST_ADD(pconn->wr_list_avail, p);
737 } else { /* "extra" request - not optimized */
738 DEBUG(DEBUG_DEBUG, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
739 for(p=pconn->extra_sent; p!=NULL; p=p->next)
740 if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
741 break;
742 if (p==NULL) {
743 sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
744 return -1;
746 if (p->ref_cnt) {
747 p->ref_cnt--;
748 } else {
749 ibw_free_mr(&p->buf_large, &p->mr_large);
750 DLIST_REMOVE(pconn->extra_sent, p);
751 DLIST_ADD(pconn->extra_avail, p);
755 return ibw_process_queue(conn);
758 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
759 struct ibw_part *part, char **pp, uint32_t add_len, int info)
761 DEBUG(DEBUG_DEBUG, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
762 pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
764 /* allocate more if necessary - it's an "evergrowing" buffer... */
765 if (part->len + add_len > part->bufsize) {
766 if (part->buf==NULL) {
767 assert(part->len==0);
768 part->buf = talloc_size(pconn, add_len);
769 if (part->buf==NULL) {
770 sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
771 add_len, info);
772 return -1;
774 part->bufsize = add_len;
775 } else {
776 part->buf = talloc_realloc_size(pconn,
777 part->buf, part->len + add_len);
778 if (part->buf==NULL) {
779 sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
780 part->len, add_len, info);
781 return -1;
784 part->bufsize = part->len + add_len;
787 /* consume pp */
788 memcpy(part->buf + part->len, *pp, add_len);
789 *pp += add_len;
790 part->len += add_len;
791 part->to_read -= add_len;
793 return 0;
796 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
797 struct ibw_part *part, uint32_t threshold)
799 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
800 pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
802 if (part->bufsize > threshold) {
803 DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
804 pconn->cm_id, part->bufsize, threshold));
805 talloc_free(part->buf);
806 part->buf = talloc_size(pconn, threshold);
807 if (part->buf==NULL) {
808 sprintf(ibw_lasterr, "talloc_size failed\n");
809 return -1;
811 part->bufsize = threshold;
813 return 0;
816 static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
818 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
819 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
820 struct ibw_part *part = &pconn->part;
821 char *p;
822 uint32_t remain = wc->byte_len;
824 DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
825 pconn->cm_id, (uint32_t)wc->wr_id, remain));
827 assert(pconn->cm_id->qp->qp_num==wc->qp_num);
828 assert((int)wc->wr_id < pctx->opts.max_recv_wr);
829 assert(wc->byte_len <= pctx->opts.recv_bufsize);
831 p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);
833 while(remain) {
834 /* here always true: (part->len!=0 && part->to_read!=0) ||
835 (part->len==0 && part->to_read==0) */
836 if (part->len) { /* is there a partial msg to be continued? */
837 int read_len = (part->to_read<=remain) ? part->to_read : remain;
838 if (ibw_append_to_part(pconn, part, &p, read_len, 421))
839 goto error;
840 remain -= read_len;
842 if (part->len<=sizeof(uint32_t) && part->to_read==0) {
843 assert(part->len==sizeof(uint32_t));
844 /* set it again now... */
845 part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
846 if (part->to_read<sizeof(uint32_t)) {
847 sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
848 goto error;
850 part->to_read -= sizeof(uint32_t); /* it's already read */
853 if (part->to_read==0) {
854 if (pctx->receive_func(conn, part->buf, part->len) != 0) {
855 goto error;
857 part->len = 0; /* tells not having partial data (any more) */
858 if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
859 goto error;
861 } else {
862 if (remain>=sizeof(uint32_t)) {
863 uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
864 if (msglen<sizeof(uint32_t)) {
865 sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
866 goto error;
869 /* mostly awaited case: */
870 if (msglen<=remain) {
871 if (pctx->receive_func(conn, p, msglen) != 0) {
872 goto error;
874 p += msglen;
875 remain -= msglen;
876 } else {
877 part->to_read = msglen;
878 /* part->len is already 0 */
879 if (ibw_append_to_part(pconn, part, &p, remain, 422))
880 goto error;
881 remain = 0; /* to be continued ... */
882 /* part->to_read > 0 here */
884 } else { /* edge case: */
885 part->to_read = sizeof(uint32_t);
886 /* part->len is already 0 */
887 if (ibw_append_to_part(pconn, part, &p, remain, 423))
888 goto error;
889 remain = 0;
890 /* part->to_read > 0 here */
893 } /* <remain> is always decreased at least by 1 */
895 if (ibw_refill_cq_recv(conn))
896 goto error;
898 return 0;
900 error:
901 DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
902 return -1;
905 static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
907 int i;
908 const char *name, *value;
910 DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
912 opts->max_send_wr = IBW_MAX_SEND_WR;
913 opts->max_recv_wr = IBW_MAX_RECV_WR;
914 opts->recv_bufsize = IBW_RECV_BUFSIZE;
915 opts->recv_threshold = IBW_RECV_THRESHOLD;
917 for(i=0; i<nattr; i++) {
918 name = attr[i].name;
919 value = attr[i].value;
921 assert(name!=NULL && value!=NULL);
922 if (strcmp(name, "max_send_wr")==0)
923 opts->max_send_wr = atoi(value);
924 else if (strcmp(name, "max_recv_wr")==0)
925 opts->max_recv_wr = atoi(value);
926 else if (strcmp(name, "recv_bufsize")==0)
927 opts->recv_bufsize = atoi(value);
928 else if (strcmp(name, "recv_threshold")==0)
929 opts->recv_threshold = atoi(value);
930 else {
931 sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
932 return -1;
935 return 0;
938 struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
939 void *ctx_userdata,
940 ibw_connstate_fn_t ibw_connstate,
941 ibw_receive_fn_t ibw_receive,
942 struct event_context *ectx)
944 struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
945 struct ibw_ctx_priv *pctx;
946 int rc;
948 DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
950 /* initialize basic data structures */
951 memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
953 assert(ctx!=NULL);
954 ibw_lasterr[0] = '\0';
955 talloc_set_destructor(ctx, ibw_ctx_destruct);
956 ctx->ctx_userdata = ctx_userdata;
958 pctx = talloc_zero(ctx, struct ibw_ctx_priv);
959 talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
960 ctx->internal = (void *)pctx;
961 assert(pctx!=NULL);
963 pctx->connstate_func = ibw_connstate;
964 pctx->receive_func = ibw_receive;
966 pctx->ectx = ectx;
968 /* process attributes */
969 if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
970 goto cleanup;
972 /* init cm */
973 pctx->cm_channel = rdma_create_event_channel();
974 if (!pctx->cm_channel) {
975 sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
976 goto cleanup;
979 pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
980 pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
982 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
983 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
984 #else
985 rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
986 #endif
987 if (rc) {
988 rc = errno;
989 sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
990 goto cleanup;
992 DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
994 pctx->pagesize = sysconf(_SC_PAGESIZE);
996 return ctx;
997 /* don't put code here */
998 cleanup:
999 DEBUG(DEBUG_ERR, (ibw_lasterr));
1001 if (ctx)
1002 talloc_free(ctx);
1004 return NULL;
1007 int ibw_stop(struct ibw_ctx *ctx)
1009 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1010 struct ibw_conn *p;
1012 DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
1014 for(p=ctx->conn_list; p!=NULL; p=p->next) {
1015 if (p->state==IBWC_ERROR || p->state==IBWC_CONNECTED) {
1016 if (ibw_disconnect(p))
1017 return -1;
1021 ctx->state = IBWS_STOPPED;
1022 pctx->connstate_func(ctx, NULL);
1024 return 0;
1027 int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
1029 struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
1030 int rc;
1032 DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
1033 inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
1034 rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
1035 if (rc) {
1036 sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
1037 DEBUG(DEBUG_ERR, (ibw_lasterr));
1038 return rc;
1040 DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
1042 return 0;
1045 int ibw_listen(struct ibw_ctx *ctx, int backlog)
1047 struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
1048 int rc;
1050 DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
1051 rc = rdma_listen(pctx->cm_id, backlog);
1052 if (rc) {
1053 sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
1054 DEBUG(DEBUG_ERR, (ibw_lasterr));
1055 return rc;
1058 return 0;
1061 int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
1063 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1064 struct rdma_conn_param conn_param;
1065 int rc;
1067 DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
1068 conn->conn_userdata = conn_userdata;
1070 memset(&conn_param, 0, sizeof(struct rdma_conn_param));
1071 conn_param.responder_resources = 1;
1072 conn_param.initiator_depth = 1;
1073 rc = rdma_accept(pconn->cm_id, &conn_param);
1074 if (rc) {
1075 sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
1076 DEBUG(DEBUG_ERR, (ibw_lasterr));
1077 return -1;;
1080 pconn->is_accepted = 1;
1082 /* continued at RDMA_CM_EVENT_ESTABLISHED */
1084 return 0;
1087 int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
1089 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1090 struct ibw_conn_priv *pconn = NULL;
1091 int rc;
1093 assert(conn!=NULL);
1095 conn->conn_userdata = conn_userdata;
1096 pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1097 DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
1098 ntohs(serv_addr->sin_port)));
1100 /* clean previous - probably half - initialization */
1101 if (ibw_conn_priv_destruct(pconn)) {
1102 DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
1103 return -1;
1106 /* init cm */
1107 #if RDMA_USER_CM_MAX_ABI_VERSION >= 2
1108 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
1109 #else
1110 rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
1111 #endif
1112 if (rc) {
1113 rc = errno;
1114 sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
1115 talloc_free(conn);
1116 return -1;
1118 DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
1120 rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
1121 if (rc) {
1122 sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
1123 DEBUG(DEBUG_ERR, (ibw_lasterr));
1124 talloc_free(conn);
1125 return -1;
1128 /* continued at RDMA_CM_EVENT_ADDR_RESOLVED */
1130 return 0;
1133 int ibw_disconnect(struct ibw_conn *conn)
1135 int rc;
1136 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1138 DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
1140 assert(pconn!=NULL);
1142 switch(conn->state) {
1143 case IBWC_ERROR:
1144 ibw_conn_priv_destruct(pconn); /* do this here right now */
1145 break;
1146 case IBWC_CONNECTED:
1147 rc = rdma_disconnect(pconn->cm_id);
1148 if (rc) {
1149 sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
1150 DEBUG(DEBUG_ERR, (ibw_lasterr));
1151 return rc;
1153 break;
1154 default:
1155 DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
1156 break;
1159 return 0;
1162 int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
1164 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1165 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1166 struct ibw_wr *p = pconn->wr_list_avail;
1168 if (p!=NULL) {
1169 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
1171 DLIST_REMOVE(pconn->wr_list_avail, p);
1172 DLIST_ADD(pconn->wr_list_used, p);
1174 if (len <= pctx->opts.recv_bufsize) {
1175 *buf = (void *)p->buf;
1176 } else {
1177 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1178 if (p->buf_large==NULL) {
1179 sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
1180 goto error;
1182 *buf = (void *)p->buf_large;
1184 /* p->wr_id is already filled in ibw_init_memory */
1185 } else {
1186 DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
1187 /* not optimized */
1188 p = pconn->extra_avail;
1189 if (!p) {
1190 p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
1191 talloc_set_destructor(p, ibw_wr_destruct);
1192 if (p==NULL) {
1193 sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
1194 goto error;
1196 p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
1197 pconn->extra_max++;
1198 switch(pconn->extra_max) {
1199 case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
1200 case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
1201 case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
1202 case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
1203 default: break;
1207 p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
1208 if (p->buf_large==NULL) {
1209 sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
1210 goto error;
1212 *buf = (void *)p->buf_large;
1214 DLIST_REMOVE(pconn->extra_avail, p);
1215 /* we don't have prepared index for this, so that
1216 * we will have to find this by wr_id later on */
1217 DLIST_ADD(pconn->extra_sent, p);
1220 *key = (void *)p;
1222 return 0;
1223 error:
1224 DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
1225 return -1;
1229 static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
1231 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1232 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1233 int rc;
1235 /* can we send it right now? */
1236 if (pconn->wr_sent<pctx->opts.max_send_wr) {
1237 struct ibv_send_wr *bad_wr;
1238 struct ibv_sge list = {
1239 .addr = (uintptr_t)buf,
1240 .length = len,
1241 .lkey = pconn->mr_send->lkey
1243 struct ibv_send_wr wr = {
1244 .wr_id = p->wr_id + pctx->opts.max_recv_wr,
1245 .sg_list = &list,
1246 .num_sge = 1,
1247 .opcode = IBV_WR_SEND,
1248 .send_flags = IBV_SEND_SIGNALED,
1251 if (p->buf_large==NULL) {
1252 DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
1253 pconn->cm_id, (uint32_t)wr.wr_id, len));
1254 } else {
1255 DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
1256 pconn->cm_id, (uint32_t)wr.wr_id, len));
1257 list.lkey = p->mr_large->lkey;
1260 rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
1261 if (rc) {
1262 sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
1263 rc, pconn->wr_sent);
1264 goto error;
1267 pconn->wr_sent++;
1269 return rc;
1270 } /* else put the request into our own queue: */
1272 DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
1274 /* TODO: clarify how to continue when state==IBWC_STOPPED */
1276 /* to be sent by ibw_wc_send */
1277 /* regardless "normal" or [a part of] "large" packet */
1278 if (!p->queued_ref_cnt) {
1279 DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
1280 qprev, qnext); /* TODO: optimize */
1281 p->queued_msg = buf;
1283 p->queued_ref_cnt++;
1284 p->queued_rlen = len; /* last wins; see ibw_wc_send */
1286 return 0;
1287 error:
1288 DEBUG(DEBUG_ERR, (ibw_lasterr));
1289 return -1;
1292 int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
1294 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1295 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1296 int rc;
1298 assert(len>=sizeof(uint32_t));
1299 assert((*((uint32_t *)buf)==len)); /* TODO: htonl */
1301 if (len > pctx->opts.recv_bufsize) {
1302 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1303 int rlen = len;
1304 char *packet = (char *)buf;
1305 uint32_t recv_bufsize = pctx->opts.recv_bufsize;
1307 DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
1308 pconn->cm_id, buf, len));
1310 /* single threaded => no race here: */
1311 assert(p->ref_cnt==0);
1312 while(rlen > recv_bufsize) {
1313 rc = ibw_send_packet(conn, packet, p, recv_bufsize);
1314 if (rc)
1315 return rc;
1316 packet += recv_bufsize;
1317 rlen -= recv_bufsize;
1318 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1320 if (rlen) {
1321 rc = ibw_send_packet(conn, packet, p, rlen);
1322 p->ref_cnt++; /* not good to have it in ibw_send_packet */
1324 p->ref_cnt--; /* for the same handling */
1325 } else {
1326 assert(p->ref_cnt==0);
1327 assert(p->queued_ref_cnt==0);
1329 rc = ibw_send_packet(conn, buf, p, len);
1331 return rc;
1334 int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
1336 struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
1337 struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
1338 struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
1340 assert(p!=NULL);
1341 assert(buf!=NULL);
1342 assert(conn!=NULL);
1344 if (p->buf_large!=NULL)
1345 ibw_free_mr(&p->buf_large, &p->mr_large);
1347 /* parallel case */
1348 if (p->wr_id < pctx->opts.max_send_wr) {
1349 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
1350 DLIST_REMOVE(pconn->wr_list_used, p);
1351 DLIST_ADD(pconn->wr_list_avail, p);
1352 } else { /* "extra" packet */
1353 DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
1354 DLIST_REMOVE(pconn->extra_sent, p);
1355 DLIST_ADD(pconn->extra_avail, p);
1358 return 0;
1361 const char *ibw_getLastError(void)
1363 return ibw_lasterr;