add a machinereadable version of ctdb stats/statistics
[metze/ctdb/wip.git] / ib / ibwrapper.c
index f3ef0c4c5cd750f5b0286fbc7c7e9f1568bc7514..f6e71687f324856d16868e61ce8e9f458d512822 100644 (file)
@@ -8,7 +8,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdlib.h>
@@ -38,8 +37,9 @@
 #include "lib/events/events.h"
 #include "ibwrapper.h"
 
+#include <infiniband/kern-abi.h>
+#include <rdma/rdma_cma_abi.h>
 #include <rdma/rdma_cma.h>
-#include "infiniband/sa-kern-abi.h"
 
 #include "ibwrapper_internal.h"
 #include "lib/util/dlinklist.h"
@@ -64,7 +64,7 @@ static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn
 {
        void *buf;
 
-       DEBUG(10, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
+       DEBUG(DEBUG_DEBUG, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
        buf = memalign(pctx->pagesize, n);
        if (!buf) {
                sprintf(ibw_lasterr, "couldn't allocate memory\n");
@@ -83,7 +83,7 @@ static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn
 
 static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
 {
-       DEBUG(10, ("ibw_free_mr(%u %u)\n", (uint32_t)*ppbuf, (uint32_t)*ppmr));
+       DEBUG(DEBUG_DEBUG, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
        if (*ppmr!=NULL) {
                ibv_dereg_mr(*ppmr);
                *ppmr = NULL;
@@ -102,7 +102,7 @@ static int ibw_init_memory(struct ibw_conn *conn)
        int     i;
        struct ibw_wr   *p;
 
-       DEBUG(10, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
        pconn->buf_send = ibw_alloc_mr(pctx, pconn,
                opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
        if (!pconn->buf_send) {
@@ -133,7 +133,7 @@ static int ibw_init_memory(struct ibw_conn *conn)
 
 static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
 {
-       DEBUG(10, ("ibw_ctx_priv_destruct(%u)\n", (uint32_t)pctx));
+       DEBUG(DEBUG_DEBUG, ("ibw_ctx_priv_destruct(%p)\n", pctx));
 
        /* destroy cm */
        if (pctx->cm_channel) {
@@ -155,70 +155,87 @@ static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
 
 static int ibw_ctx_destruct(struct ibw_ctx *ctx)
 {
-       DEBUG(10, ("ibw_ctx_destruct(%u)\n", (uint32_t)ctx));
+       DEBUG(DEBUG_DEBUG, ("ibw_ctx_destruct(%p)\n", ctx));
        return 0;
 }
 
 static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
 {
-       DEBUG(10, ("ibw_conn_priv_destruct(%u, cmid: %p)\n",
-               (uint32_t)pconn, pconn->cm_id));
-
-       /* free memory regions */
-       ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
-       ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
+       DEBUG(DEBUG_DEBUG, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
+               pconn, pconn->cm_id));
 
        /* pconn->wr_index is freed by talloc */
        /* pconn->wr_index[i] are freed by talloc */
 
        /* destroy verbs */
-       if (pconn->cm_id->qp) {
-               ibv_destroy_qp(pconn->cm_id->qp);
+       if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
+               rdma_destroy_qp(pconn->cm_id);
                pconn->cm_id->qp = NULL;
        }
-       if (pconn->cq) {
+
+       if (pconn->cq!=NULL) {
                ibv_destroy_cq(pconn->cq);
                pconn->cq = NULL;
        }
-       if (pconn->verbs_channel) {
+
+       if (pconn->verbs_channel!=NULL) {
                ibv_destroy_comp_channel(pconn->verbs_channel);
                pconn->verbs_channel = NULL;
        }
+
+       /* must be freed here because its order is important */
        if (pconn->verbs_channel_event) {
-               /* TODO: do we have to do this here? */
                talloc_free(pconn->verbs_channel_event);
                pconn->verbs_channel_event = NULL;
        }
+
+       /* free memory regions */
+       ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
+       ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);
+
        if (pconn->pd) {
                ibv_dealloc_pd(pconn->pd);
                pconn->pd = NULL;
+               DEBUG(DEBUG_DEBUG, ("pconn=%p pd deallocated\n", pconn));
        }
+
        if (pconn->cm_id) {
                rdma_destroy_id(pconn->cm_id);
                pconn->cm_id = NULL;
+               DEBUG(DEBUG_DEBUG, ("pconn=%p cm_id destroyed\n", pconn));
        }
+
+       return 0;
+}
+
+static int ibw_wr_destruct(struct ibw_wr *wr)
+{
+       if (wr->buf_large!=NULL)
+               ibw_free_mr(&wr->buf_large, &wr->mr_large);
        return 0;
 }
 
 static int ibw_conn_destruct(struct ibw_conn *conn)
 {
-       DEBUG(10, ("ibw_conn_destruct(%u)\n", (uint32_t)conn));
+       DEBUG(DEBUG_DEBUG, ("ibw_conn_destruct(%p)\n", conn));
        
        /* important here: ctx is a talloc _parent_ */
        DLIST_REMOVE(conn->ctx->conn_list, conn);
        return 0;
 }
 
-static struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx)
+struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
 {
        struct ibw_conn *conn;
        struct ibw_conn_priv *pconn;
 
-       conn = talloc_zero(ctx, struct ibw_conn);
+       assert(ctx!=NULL);
+
+       conn = talloc_zero(mem_ctx, struct ibw_conn);
        assert(conn!=NULL);
        talloc_set_destructor(conn, ibw_conn_destruct);
 
-       pconn = talloc_zero(ctx, struct ibw_conn_priv);
+       pconn = talloc_zero(conn, struct ibw_conn_priv);
        assert(pconn!=NULL);
        talloc_set_destructor(pconn, ibw_conn_priv_destruct);
 
@@ -238,7 +255,7 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn)
        struct ibv_qp_attr attr;
        int rc;
 
-       DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));
 
        /* init verbs */
        pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
@@ -246,9 +263,9 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn)
                sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
                return -1;
        }
-       DEBUG(10, ("created channel %p\n", pconn->verbs_channel));
+       DEBUG(DEBUG_DEBUG, ("created channel %p\n", pconn->verbs_channel));
 
-       pconn->verbs_channel_event = event_add_fd(pctx->ectx, conn,
+       pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
                pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);
 
        pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
@@ -256,7 +273,7 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn)
                sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
                return -1;
        }
-       DEBUG(10, ("created pd %p\n", pconn->pd));
+       DEBUG(DEBUG_DEBUG, ("created pd %p\n", pconn->pd));
 
        /* init mr */
        if (ibw_init_memory(conn))
@@ -320,7 +337,7 @@ static int ibw_refill_cq_recv(struct ibw_conn *conn)
        };
        struct ibv_recv_wr *bad_wr;
 
-       DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));
 
        list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
        wr.wr_id = pconn->recv_index;
@@ -329,7 +346,7 @@ static int ibw_refill_cq_recv(struct ibw_conn *conn)
        rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
        if (rc) {
                sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
+               DEBUG(DEBUG_ERR, (ibw_lasterr));
                return -2;
        }
 
@@ -353,7 +370,7 @@ static int ibw_fill_cq(struct ibw_conn *conn)
        };
        struct ibv_recv_wr *bad_wr;
 
-       DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));
 
        for(i = pctx->opts.max_recv_wr; i!=0; i--) {
                list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
@@ -363,7 +380,7 @@ static int ibw_fill_cq(struct ibw_conn *conn)
                rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
                if (rc) {
                        sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
-                       DEBUG(0, (ibw_lasterr));
+                       DEBUG(DEBUG_ERR, (ibw_lasterr));
                        return -2;
                }
        }
@@ -371,14 +388,15 @@ static int ibw_fill_cq(struct ibw_conn *conn)
        return 0;
 }
 
-static int ibw_manage_connect(struct ibw_conn *conn, struct rdma_cm_id *cma_id)
+static int ibw_manage_connect(struct ibw_conn *conn)
 {
        struct rdma_conn_param conn_param;
+       struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
        int     rc;
 
-       DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", cma_id));
-       rc = ibw_setup_cq_qp(conn);
-       if (rc)
+       DEBUG(DEBUG_DEBUG, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));
+
+       if (ibw_setup_cq_qp(conn))
                return -1;
 
        /* cm connect */
@@ -387,7 +405,7 @@ static int ibw_manage_connect(struct ibw_conn *conn, struct rdma_cm_id *cma_id)
        conn_param.initiator_depth = 1;
        conn_param.retry_count = 10;
 
-       rc = rdma_connect(cma_id, &conn_param);
+       rc = rdma_connect(pconn->cm_id, &conn_param);
        if (rc)
                sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);
 
@@ -410,17 +428,18 @@ static void ibw_event_handler_cm(struct event_context *ev,
        rc = rdma_get_cm_event(pctx->cm_channel, &event);
        if (rc) {
                ctx->state = IBWS_ERROR;
+               event = NULL;
                sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
                goto error;
        }
        cma_id = event->id;
 
-       DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
+       DEBUG(DEBUG_DEBUG, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
                  (cma_id == pctx->cm_id) ? "parent" : "child"));
 
        switch (event->event) {
        case RDMA_CM_EVENT_ADDR_RESOLVED:
-               DEBUG(11, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
+               DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
                /* continuing from ibw_connect ... */
                rc = rdma_resolve_route(cma_id, 2000);
                if (rc) {
@@ -431,25 +450,25 @@ static void ibw_event_handler_cm(struct event_context *ev,
                break;
 
        case RDMA_CM_EVENT_ROUTE_RESOLVED:
-               DEBUG(11, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
+               DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
                /* after RDMA_CM_EVENT_ADDR_RESOLVED: */
                assert(cma_id->context!=NULL);
                conn = talloc_get_type(cma_id->context, struct ibw_conn);
 
-               rc = ibw_manage_connect(conn, cma_id);
+               rc = ibw_manage_connect(conn);
                if (rc)
                        goto error;
 
                break;
 
        case RDMA_CM_EVENT_CONNECT_REQUEST:
-               DEBUG(11, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
+               DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
                ctx->state = IBWS_CONNECT_REQUEST;
-               conn = ibw_conn_new(ctx);
+               conn = ibw_conn_new(ctx, ctx);
                pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
                pconn->cm_id = cma_id; /* !!! event will be freed but id not */
                cma_id->context = (void *)conn;
-               DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));
+               DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p\n", pconn->cm_id));
 
                if (ibw_setup_cq_qp(conn))
                        goto error;
@@ -459,8 +478,11 @@ static void ibw_event_handler_cm(struct event_context *ev,
 
                /* continued at ibw_accept when invoked by the func above */
                if (!pconn->is_accepted) {
+                       rc = rdma_reject(cma_id, NULL, 0);
+                       if (rc)
+                               DEBUG(DEBUG_ERR, ("rdma_reject failed with rc=%d\n", rc));
                        talloc_free(conn);
-                       DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
+                       DEBUG(DEBUG_DEBUG, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
                }
 
                /* TODO: clarify whether if it's needed by upper layer: */
@@ -472,10 +494,12 @@ static void ibw_event_handler_cm(struct event_context *ev,
 
        case RDMA_CM_EVENT_ESTABLISHED:
                /* expected after ibw_accept and ibw_connect[not directly] */
-               DEBUG(0, ("ESTABLISHED (conn: %p)\n", cma_id->context));
+               DEBUG(DEBUG_INFO, ("ESTABLISHED (conn: %p)\n", cma_id->context));
                conn = talloc_get_type(cma_id->context, struct ibw_conn);
                assert(conn!=NULL); /* important assumption */
 
+               DEBUG(DEBUG_DEBUG, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));
+
                /* client conn is up */
                conn->state = IBWC_CONNECTED;
 
@@ -485,24 +509,36 @@ static void ibw_event_handler_cm(struct event_context *ev,
 
        case RDMA_CM_EVENT_ADDR_ERROR:
                sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
-               goto error;
        case RDMA_CM_EVENT_ROUTE_ERROR:
                sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
-               goto error;
        case RDMA_CM_EVENT_CONNECT_ERROR:
                sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
-               goto error;
        case RDMA_CM_EVENT_UNREACHABLE:
                sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
                goto error;
        case RDMA_CM_EVENT_REJECTED:
                sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
-               goto error;
+               DEBUG(DEBUG_INFO, ("cm event handler: %s", ibw_lasterr));
+               conn = talloc_get_type(cma_id->context, struct ibw_conn);
+               if (conn) {
+                       /* must be done BEFORE connstate */
+                       if ((rc=rdma_ack_cm_event(event)))
+                               DEBUG(DEBUG_ERR, ("reject/rdma_ack_cm_event failed with %d\n", rc));
+                       event = NULL; /* not to touch cma_id or conn */
+                       conn->state = IBWC_ERROR;
+                       /* it should free the conn */
+                       pctx->connstate_func(NULL, conn);
+               }
+               break; /* this is not strictly an error */
 
        case RDMA_CM_EVENT_DISCONNECTED:
-               DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
+               DEBUG(DEBUG_DEBUG, ("RDMA_CM_EVENT_DISCONNECTED\n"));
+               if ((rc=rdma_ack_cm_event(event)))
+                       DEBUG(DEBUG_ERR, ("disc/rdma_ack_cm_event failed with %d\n", rc));
+               event = NULL; /* don't ack more */
+
                if (cma_id!=pctx->cm_id) {
-                       DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
+                       DEBUG(DEBUG_ERR, ("client DISCONNECT event cm_id=%p\n", cma_id));
                        conn = talloc_get_type(cma_id->context, struct ibw_conn);
                        conn->state = IBWC_DISCONNECTED;
                        pctx->connstate_func(NULL, conn);
@@ -518,23 +554,33 @@ static void ibw_event_handler_cm(struct event_context *ev,
                goto error;
        }
 
-       if ((rc=rdma_ack_cm_event(event))) {
+       if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
                sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
                goto error;
        }
 
        return;
 error:
-       DEBUG(0, ("cm event handler: %s", ibw_lasterr));
-       if (cma_id!=pctx->cm_id) {
-               conn = talloc_get_type(cma_id->context, struct ibw_conn);
-               if (conn)
-                       conn->state = IBWC_ERROR;
-               pctx->connstate_func(NULL, conn);
-       } else {
-               ctx->state = IBWS_ERROR;
-               pctx->connstate_func(ctx, NULL);
+       DEBUG(DEBUG_ERR, ("cm event handler: %s", ibw_lasterr));
+
+       if (event!=NULL) {
+               if (cma_id!=NULL && cma_id!=pctx->cm_id) {
+                       conn = talloc_get_type(cma_id->context, struct ibw_conn);
+                       if (conn) {
+                               conn->state = IBWC_ERROR;
+                               pctx->connstate_func(NULL, conn);
+                       }
+               } else {
+                       ctx->state = IBWS_ERROR;
+                       pctx->connstate_func(ctx, NULL);
+               }
+
+               if ((rc=rdma_ack_cm_event(event))!=0) {
+                       DEBUG(DEBUG_ERR, ("rdma_ack_cm_event failed with %d\n", rc));
+               }
        }
+
+       return;
 }
 
 static void ibw_event_handler_verbs(struct event_context *ev,
@@ -549,7 +595,7 @@ static void ibw_event_handler_verbs(struct event_context *ev,
        struct ibv_cq *ev_cq;
        void          *ev_ctx;
 
-       DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
+       DEBUG(DEBUG_DEBUG, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));
 
        /* TODO: check whether if it's good to have more channels here... */
        rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
@@ -569,28 +615,28 @@ static void ibw_event_handler_verbs(struct event_context *ev,
 
        while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
                if (wc.status) {
-                       sprintf(ibw_lasterr, "cq completion failed status %d rc %d\n",
-                               wc.status, rc);
+                       sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
+                               wc.status, wc.opcode, rc);
                        goto error;
                }
 
                switch(wc.opcode) {
                case IBV_WC_SEND:
-                       DEBUG(10, ("send completion\n"));
+                       DEBUG(DEBUG_DEBUG, ("send completion\n"));
                        if (ibw_wc_send(conn, &wc))
                                goto error;
                        break;
 
                case IBV_WC_RDMA_WRITE:
-                       DEBUG(10, ("rdma write completion\n"));
+                       DEBUG(DEBUG_DEBUG, ("rdma write completion\n"));
                        break;
        
                case IBV_WC_RDMA_READ:
-                       DEBUG(10, ("rdma read completion\n"));
+                       DEBUG(DEBUG_DEBUG, ("rdma read completion\n"));
                        break;
 
                case IBV_WC_RECV:
-                       DEBUG(10, ("recv completion\n"));
+                       DEBUG(DEBUG_DEBUG, ("recv completion\n"));
                        if (ibw_wc_recv(conn, &wc))
                                goto error;
                        break;
@@ -605,11 +651,57 @@ static void ibw_event_handler_verbs(struct event_context *ev,
                goto error;
        }
 
+       ibv_ack_cq_events(pconn->cq, 1);
+
        return;
 error:
-       DEBUG(0, (ibw_lasterr));
-       conn->state = IBWC_ERROR;
-       pctx->connstate_func(NULL, conn);
+       ibv_ack_cq_events(pconn->cq, 1);
+
+       DEBUG(DEBUG_ERR, (ibw_lasterr));
+       
+       if (conn->state!=IBWC_ERROR) {
+               conn->state = IBWC_ERROR;
+               pctx->connstate_func(NULL, conn);
+       }
+}
+
+static int ibw_process_queue(struct ibw_conn *conn)
+{
+       struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
+       struct ibw_ctx_priv *pctx;
+       struct ibw_wr   *p;
+       int     rc;
+       uint32_t        msg_size;
+
+       if (pconn->queue==NULL)
+               return 0; /* NOP */
+
+       p = pconn->queue;
+
+       /* we must have at least 1 fragment to send */
+       assert(p->queued_ref_cnt>0);
+       p->queued_ref_cnt--;
+
+       pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
+       msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
+
+       assert(p->queued_msg!=NULL);
+       assert(msg_size!=0);
+
+       DEBUG(DEBUG_DEBUG, ("ibw_process_queue refcnt=%d msgsize=%u\n",
+               p->queued_ref_cnt, msg_size));
+
+       rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
+
+       /* was this the last fragment? */
+       if (p->queued_ref_cnt) {
+               p->queued_msg += pctx->opts.recv_bufsize;
+       } else {
+               DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
+               p->queued_msg = NULL;
+       }
+
+       return rc;
 }
 
 static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
@@ -618,9 +710,8 @@ static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
        struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
        struct ibw_wr   *p;
        int     send_index;
-       int     rc = 0;
 
-       DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
+       DEBUG(DEBUG_DEBUG, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
                pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));
 
        assert(pconn->cm_id->qp->qp_num==wc->qp_num);
@@ -629,7 +720,7 @@ static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
        pconn->wr_sent--;
 
        if (send_index < pctx->opts.max_send_wr) {
-               DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
+               DEBUG(DEBUG_DEBUG, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
                p = pconn->wr_index[send_index];
                if (p->buf_large!=NULL) {
                        if (p->ref_cnt) {
@@ -645,7 +736,7 @@ static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
                        DLIST_ADD(pconn->wr_list_avail, p);
                }
        } else { /* "extra" request - not optimized */
-               DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
+               DEBUG(DEBUG_DEBUG, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
                for(p=pconn->extra_sent; p!=NULL; p=p->next)
                        if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
                                break;
@@ -662,36 +753,13 @@ static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
                }
        }
 
-       if (pconn->queue) {
-               uint32_t        msg_size;
-               
-               DEBUG(10, ("ibw_wc_send#queue %u\n", (int)wc->wr_id));
-               
-               p = pconn->queue;
-
-               assert(p->queued_ref_cnt>0);
-               p->queued_ref_cnt--;
-
-               msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;
-               assert(p->queued_msg!=NULL);
-               assert(msg_size!=0);
-               rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);
-               if (p->queued_ref_cnt) {
-                       p->queued_msg += pctx->opts.recv_bufsize;
-               } else {
-                       DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
-                       p->queued_msg = NULL;
-               }
-       }
-
-       return rc;
+       return ibw_process_queue(conn);
 }
 
 static int ibw_append_to_part(struct ibw_conn_priv *pconn,
        struct ibw_part *part, char **pp, uint32_t add_len, int info)
 {
-       DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
+       DEBUG(DEBUG_DEBUG, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
                pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));
 
        /* allocate more if necessary - it's an "evergrowing" buffer... */
@@ -729,11 +797,11 @@ static int ibw_append_to_part(struct ibw_conn_priv *pconn,
 static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
        struct ibw_part *part, uint32_t threshold)
 {
-       DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
+       DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
                pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));
 
        if (part->bufsize > threshold) {
-               DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
+               DEBUG(DEBUG_DEBUG, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
                        pconn->cm_id, part->bufsize, threshold));
                talloc_free(part->buf);
                part->buf = talloc_size(pconn, threshold);
@@ -754,7 +822,7 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
        char    *p;
        uint32_t        remain = wc->byte_len;
 
-       DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
+       DEBUG(DEBUG_DEBUG, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
                pconn->cm_id, (uint32_t)wc->wr_id, remain));
 
        assert(pconn->cm_id->qp->qp_num==wc->qp_num);
@@ -784,7 +852,9 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
                        }
 
                        if (part->to_read==0) {
-                               pctx->receive_func(conn, part->buf, part->len);
+                               if (pctx->receive_func(conn, part->buf, part->len) != 0) {
+                                       goto error;
+                               }
                                part->len = 0; /* tells not having partial data (any more) */
                                if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
                                        goto error;
@@ -799,7 +869,9 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
 
                                /* mostly awaited case: */
                                if (msglen<=remain) {
-                                       pctx->receive_func(conn, p, msglen);
+                                       if (pctx->receive_func(conn, p, msglen) != 0) {
+                                               goto error;
+                                       }
                                        p += msglen;
                                        remain -= msglen;
                                } else {
@@ -827,7 +899,7 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
        return 0;
 
 error:
-       DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
+       DEBUG(DEBUG_ERR, ("ibw_wc_recv error: %s", ibw_lasterr));
        return -1;
 }
 
@@ -836,7 +908,7 @@ static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct i
        int     i;
        const char *name, *value;
 
-       DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));
+       DEBUG(DEBUG_DEBUG, ("ibw_process_init_attrs: nattr: %d\n", nattr));
 
        opts->max_send_wr = IBW_MAX_SEND_WR;
        opts->max_recv_wr = IBW_MAX_RECV_WR;
@@ -874,8 +946,7 @@ struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
        struct ibw_ctx_priv *pctx;
        int     rc;
 
-       DEBUG(10, ("ibw_init(ctx_userdata: %u, ectx: %u)\n",
-               (uint32_t)ctx_userdata, (uint32_t)ectx));
+       DEBUG(DEBUG_DEBUG, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));
 
        /* initialize basic data structures */
        memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);
@@ -909,20 +980,24 @@ struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
        pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
                pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);
 
+#if RDMA_USER_CM_MAX_ABI_VERSION >= 2
        rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
+#else
+       rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
+#endif
        if (rc) {
                rc = errno;
                sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
                goto cleanup;
        }
-       DEBUG(10, ("created cm_id %p\n", pctx->cm_id));
+       DEBUG(DEBUG_DEBUG, ("created cm_id %p\n", pctx->cm_id));
 
        pctx->pagesize = sysconf(_SC_PAGESIZE);
 
        return ctx;
        /* don't put code here */
 cleanup:
-       DEBUG(0, (ibw_lasterr));
+       DEBUG(DEBUG_ERR, (ibw_lasterr));
 
        if (ctx)
                talloc_free(ctx);
@@ -935,7 +1010,7 @@ int ibw_stop(struct ibw_ctx *ctx)
        struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
        struct ibw_conn *p;
 
-       DEBUG(10, ("ibw_stop\n"));
+       DEBUG(DEBUG_DEBUG, ("ibw_stop\n"));
 
        for(p=ctx->conn_list; p!=NULL; p=p->next) {
                if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
@@ -955,15 +1030,15 @@ int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
        struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
        int     rc;
 
-       DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
+       DEBUG(DEBUG_DEBUG, ("ibw_bind: addr=%s, port=%u\n",
                inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
        rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
        if (rc) {
                sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
+               DEBUG(DEBUG_ERR, (ibw_lasterr));
                return rc;
        }
-       DEBUG(10, ("rdma_bind_addr successful\n"));
+       DEBUG(DEBUG_DEBUG, ("rdma_bind_addr successful\n"));
 
        return 0;
 }
@@ -973,11 +1048,11 @@ int ibw_listen(struct ibw_ctx *ctx, int backlog)
        struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
        int     rc;
 
-       DEBUG(10, ("ibw_listen\n"));
+       DEBUG(DEBUG_DEBUG, ("ibw_listen\n"));
        rc = rdma_listen(pctx->cm_id, backlog);
        if (rc) {
                sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
+               DEBUG(DEBUG_ERR, (ibw_lasterr));
                return rc;
        }
 
@@ -990,7 +1065,7 @@ int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
        struct rdma_conn_param  conn_param;
        int     rc;
 
-       DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_accept: cmid=%p\n", pconn->cm_id));
        conn->conn_userdata = conn_userdata;
 
        memset(&conn_param, 0, sizeof(struct rdma_conn_param));
@@ -999,7 +1074,7 @@ int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
        rc = rdma_accept(pconn->cm_id, &conn_param);
        if (rc) {
                sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
+               DEBUG(DEBUG_ERR, (ibw_lasterr));
                return -1;;
        }
 
@@ -1010,33 +1085,43 @@ int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
        return 0;
 }
 
-int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_userdata)
+int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
 {
-       struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
-       struct ibw_conn *conn = NULL;
+       struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
        struct ibw_conn_priv *pconn = NULL;
        int     rc;
 
-       conn = ibw_conn_new(ctx);
+       assert(conn!=NULL);
+
        conn->conn_userdata = conn_userdata;
        pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
-       DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
+       DEBUG(DEBUG_DEBUG, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
                ntohs(serv_addr->sin_port)));
 
+       /* clean previous - probably half - initialization */
+       if (ibw_conn_priv_destruct(pconn)) {
+               DEBUG(DEBUG_ERR, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
+               return -1;
+       }
+
        /* init cm */
+#if RDMA_USER_CM_MAX_ABI_VERSION >= 2
        rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
+#else
+       rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
+#endif
        if (rc) {
                rc = errno;
                sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
                talloc_free(conn);
                return -1;
        }
-       DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));
 
        rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
        if (rc) {
                sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
+               DEBUG(DEBUG_ERR, (ibw_lasterr));
                talloc_free(conn);
                return -1;
        }
@@ -1051,13 +1136,25 @@ int ibw_disconnect(struct ibw_conn *conn)
        int     rc;
        struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
 
-       DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
+       DEBUG(DEBUG_DEBUG, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));
 
-       rc = rdma_disconnect(pconn->cm_id);
-       if (rc) {
-               sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
-               DEBUG(0, (ibw_lasterr));
-               return rc;
+       assert(pconn!=NULL);
+
+       switch(conn->state) {
+       case IBWC_ERROR:
+               ibw_conn_priv_destruct(pconn); /* do this here right now */
+               break;
+       case IBWC_CONNECTED:
+               rc = rdma_disconnect(pconn->cm_id);
+               if (rc) {
+                       sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
+                       DEBUG(DEBUG_ERR, (ibw_lasterr));
+                       return rc;
+               }
+               break;
+       default:
+               DEBUG(DEBUG_DEBUG, ("invalid state for disconnect: %d\n", conn->state));
+               break;
        }
 
        return 0;
@@ -1070,7 +1167,7 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t l
        struct ibw_wr *p = pconn->wr_list_avail;
 
        if (p!=NULL) {
-               DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
+               DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));
 
                DLIST_REMOVE(pconn->wr_list_avail, p);
                DLIST_ADD(pconn->wr_list_used, p);
@@ -1087,11 +1184,12 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t l
                }
                /* p->wr_id is already filled in ibw_init_memory */
        } else {
-               DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
+               DEBUG(DEBUG_DEBUG, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
                /* not optimized */
                p = pconn->extra_avail;
                if (!p) {
                        p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
+                       talloc_set_destructor(p, ibw_wr_destruct);
                        if (p==NULL) {
                                sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
                                goto error;
@@ -1099,10 +1197,10 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t l
                        p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
                        pconn->extra_max++;
                        switch(pconn->extra_max) {
-                               case 1: DEBUG(2, ("warning: queue performed\n")); break;
-                               case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
-                               case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
-                               case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
+                               case 1: DEBUG(DEBUG_INFO, ("warning: queue performed\n")); break;
+                               case 10: DEBUG(DEBUG_INFO, ("warning: queue reached 10\n")); break;
+                               case 100: DEBUG(DEBUG_INFO, ("warning: queue reached 100\n")); break;
+                               case 1000: DEBUG(DEBUG_INFO, ("warning: queue reached 1000\n")); break;
                                default: break;
                        }
                }
@@ -1124,7 +1222,7 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t l
 
        return 0;
 error:
-       DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
+       DEBUG(DEBUG_ERR, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
        return -1;
 }
 
@@ -1152,10 +1250,10 @@ static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, u
                };
 
                if (p->buf_large==NULL) {
-                       DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
+                       DEBUG(DEBUG_DEBUG, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
                                pconn->cm_id, (uint32_t)wr.wr_id, len));
                } else {
-                       DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
+                       DEBUG(DEBUG_DEBUG, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
                                pconn->cm_id, (uint32_t)wr.wr_id, len));
                        list.lkey = p->mr_large->lkey;
                }
@@ -1172,7 +1270,9 @@ static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, u
                return rc;
        } /* else put the request into our own queue: */
 
-       DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
+       DEBUG(DEBUG_DEBUG, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));
+
+       /* TODO: clarify how to continue when state==IBWC_STOPPED */
 
        /* to be sent by ibw_wc_send */
        /* regardless "normal" or [a part of] "large" packet */
@@ -1186,7 +1286,7 @@ static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, u
 
        return 0;
 error:
-       DEBUG(0, (ibw_lasterr));
+       DEBUG(DEBUG_ERR, (ibw_lasterr));
        return -1;
 }
 
@@ -1205,7 +1305,7 @@ int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
                char    *packet = (char *)buf;
                uint32_t        recv_bufsize = pctx->opts.recv_bufsize;
 
-               DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
+               DEBUG(DEBUG_DEBUG, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
                        pconn->cm_id, buf, len));
 
                /* single threaded => no race here: */
@@ -1247,11 +1347,11 @@ int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
 
        /* parallel case */
        if (p->wr_id < pctx->opts.max_send_wr) {
-               DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
+               DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
                DLIST_REMOVE(pconn->wr_list_used, p);
                DLIST_ADD(pconn->wr_list_avail, p);
        } else { /* "extra" packet */
-               DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
+               DEBUG(DEBUG_DEBUG, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
                DLIST_REMOVE(pconn->extra_sent, p);
                DLIST_ADD(pconn->extra_avail, p);
        }