diff options
-rw-r--r-- | ctdb/.bzrignore | 2 | ||||
-rw-r--r-- | ctdb/ib/config.m4 | 2 | ||||
-rw-r--r-- | ctdb/ib/ibw_ctdb.c | 105 | ||||
-rw-r--r-- | ctdb/ib/ibw_ctdb.h | 25 | ||||
-rw-r--r-- | ctdb/ib/ibw_ctdb_init.c | 190 | ||||
-rw-r--r-- | ctdb/ib/ibwrapper.c | 464 | ||||
-rw-r--r-- | ctdb/ib/ibwrapper.h | 11 | ||||
-rw-r--r-- | ctdb/ib/ibwrapper_internal.h | 50 | ||||
-rw-r--r-- | ctdb/include/includes.h | 4 | ||||
-rw-r--r-- | ctdb/tests/ibwrapper_test.c | 326 |
10 files changed, 962 insertions, 217 deletions
diff --git a/ctdb/.bzrignore b/ctdb/.bzrignore index 5d04ceb19e0..80886fdfe1b 100644 --- a/ctdb/.bzrignore +++ b/ctdb/.bzrignore @@ -1,12 +1,10 @@ config.status Makefile bin -common config.log push.sh ctdb_test config.cache configure -configure config.h config.h.in diff --git a/ctdb/ib/config.m4 b/ctdb/ib/config.m4 index e84b7448a76..9d95ea7a5ab 100644 --- a/ctdb/ib/config.m4 +++ b/ctdb/ib/config.m4 @@ -7,7 +7,7 @@ if eval "test x$enable_infiniband = xyes"; then AC_DEFINE(USE_INFINIBAND,1,[Use infiniband]) HAVE_INFINIBAND=yes - INFINIBAND_WRAPPER_OBJ="ib/ibwrapper.o" + INFINIBAND_WRAPPER_OBJ="ib/ibwrapper.o ib/ibw_ctdb.o ib/ibw_ctdb_init.o" INFINIBAND_LIBS="-lrdmacm -libverbs" INFINIBAND_BINS="bin/ibwrapper_test" diff --git a/ctdb/ib/ibw_ctdb.c b/ctdb/ib/ibw_ctdb.c new file mode 100644 index 00000000000..4379cb75086 --- /dev/null +++ b/ctdb/ib/ibw_ctdb.c @@ -0,0 +1,105 @@ +/* + * Unix SMB/CIFS implementation. + * Join infiniband wrapper and ctdb. + * + * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006 + * + * Major code contributions by Peter Somogyi <psomogyi@gamax.hu> + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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. + */ + +#include "includes.h" +#include "lib/events/events.h" +#include <system/network.h> +#include <assert.h> +#include "ctdb_private.h" +#include "ibwrapper.h" +#include "ibw_ctdb.h" + +int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn) +{ + if (ctx!=NULL) { + /* ctx->state changed */ + switch(ctx->state) { + case IBWS_INIT: /* ctx start - after ibw_init */ + break; + case IBWS_READY: /* after ibw_bind & ibw_listen */ + break; + case IBWS_CONNECT_REQUEST: /* after [IBWS_READY + incoming request] */ + /* => [(ibw_accept)IBWS_READY | (ibw_disconnect)STOPPED | ERROR] */ + if (ibw_accept(ctx, conn, NULL)) { + DEBUG(0, ("connstate_handler/ibw_accept failed\n")); + return -1; + } /* else continue in IBWC_CONNECTED */ + break; + case IBWS_STOPPED: /* normal stop <= ibw_disconnect+(IBWS_READY | IBWS_CONNECT_REQUEST) */ + /* TODO: have a CTDB upcall for which CTDB should wait in a (final) loop */ + break; + case IBWS_ERROR: /* abnormal state; ibw_stop must be called after this */ + break; + default: + assert(0); + break; + } + } + + if (conn!=NULL) { + /* conn->state changed */ + switch(conn->state) { + case IBWC_INIT: /* conn start - internal state */ + break; + case IBWC_CONNECTED: { /* after ibw_accept or ibw_connect */ + struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node); + if (node!=NULL) /* after ibw_connect */ + node->ctdb->upcalls->node_connected(node); + else { /* after ibw_accept */ + /* NOP in CTDB case */ + } + } break; + case IBWC_DISCONNECTED: /* after ibw_disconnect */ + /* TODO: have a CTDB upcall */ + break; + case IBWC_ERROR: { + struct ctdb_node *node = talloc_get_type(conn->conn_userdata, struct ctdb_node); + if (node!=NULL) + node->ctdb->upcalls->node_connected(node); + } break; + default: + assert(0); + break; + } + } + + return 0; +} + +int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n) +{ + struct ctdb_context *ctdb = talloc_get_type(conn->ctx->ctx_userdata, struct ctdb_context); + + assert(ctdb!=NULL); + assert(conn->state==IBWC_CONNECTED); + + /* TODO: shall I short-circuit this in ibwrapper? */ + /* maybe when everything go fine... */ + + /* TODO2: !!! here I can provide conn->conn_userdata (with no perf. penalty) - + * as struct ctdb_node in case the connection + * has been built up by ibw_connect !!! */ + ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf, (uint32_t)n); + + return 0; +} diff --git a/ctdb/ib/ibw_ctdb.h b/ctdb/ib/ibw_ctdb.h new file mode 100644 index 00000000000..c43aca7df15 --- /dev/null +++ b/ctdb/ib/ibw_ctdb.h @@ -0,0 +1,25 @@ +/* + * Unix SMB/CIFS implementation. + * Join infiniband wrapper and ctdb. + * + * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006 + * + * Major code contributions by Peter Somogyi <psomogyi@gamax.hu> + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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. + */ + +int ctdb_ibw_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn); +int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n); diff --git a/ctdb/ib/ibw_ctdb_init.c b/ctdb/ib/ibw_ctdb_init.c new file mode 100644 index 00000000000..0d4f01d7635 --- /dev/null +++ b/ctdb/ib/ibw_ctdb_init.c @@ -0,0 +1,190 @@ +/* + * Unix SMB/CIFS implementation. + * Join infiniband wrapper and ctdb. + * + * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006 + * + * Major code contributions by Peter Somogyi <psomogyi@gamax.hu> + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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. + */ + +#include "includes.h" +#include "lib/events/events.h" +#include <system/network.h> +#include <assert.h> +#include "ctdb_private.h" +#include "ibwrapper.h" +#include "ibw_ctdb.h" + +/* not nice; temporary workaround for the current implementation... */ +static void *last_key = NULL; + +static int ctdb_ibw_listen(struct ctdb_context *ctdb, int backlog) +{ + struct ibw_ctx *ictx = talloc_get_type(ctdb->private, struct ibw_ctx); + struct sockaddr_in my_addr; + + assert(ictx!=NULL); + memset(&my_addr, 0, sizeof(struct sockaddr_in)); + my_addr.sin_port = htons(ctdb->address.port); + my_addr.sin_family = PF_INET; + inet_pton(AF_INET, ctdb->address.address, &my_addr.sin_addr); + + if (ibw_bind(ictx, &my_addr)) { + DEBUG(0, ("ctdb_ibw_listen: ibw_bind failed\n")); + return -1; + } + + if (ibw_listen(ictx, backlog)) { + DEBUG(0, ("ctdb_ibw_listen: ibw_listen failed\n")); + return -1; + } + + return 0; +} + +int ctdb_ibw_node_connect(struct ibw_ctx *ictx, struct ctdb_node *node) +{ + struct sockaddr_in sock_out; + + inet_pton(AF_INET, node->address.address, &sock_out.sin_addr); + sock_out.sin_port = htons(node->address.port); + sock_out.sin_family = PF_INET; + + if (ibw_connect(ictx, &sock_out, node)) { + DEBUG(0, ("ctdb_ibw_node_connect: ibw_connect failed\n")); + return -1; + } + + /* continues at ibw_ctdb.c/IBWC_CONNECTED in good case */ + return 0; +} + +/* + * Start infiniband + */ +static int ctdb_ibw_start(struct ctdb_context *ctdb) +{ + struct ibw_ctx *ictx = talloc_get_type(ctdb->private, struct ibw_ctx); + int i; + + /* listen on our own address */ + if (ctdb_ibw_listen(ctdb, 10)) /* TODO: backlog as param */ + return -1; + + /* everything async here */ + for (i=0;i<ctdb->num_nodes;i++) { + struct ctdb_node *node = ctdb->nodes[i]; + if (!(ctdb->flags & CTDB_FLAG_SELF_CONNECT) && + ctdb_same_address(&ctdb->address, &node->address)) + continue; + ctdb_ibw_node_connect(ictx, node); + } + + return 0; +} + + +/* + * initialise ibw portion of a ctdb node + */ +static int ctdb_ibw_add_node(struct ctdb_node *node) +{ + /* TODO: clarify whether is this necessary for us ? + - why not enough doing such thing internally at connect time ? */ + return 0; +} + +static int ctdb_ibw_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t length) +{ + struct ibw_conn *conn = talloc_get_type(node->private, struct ibw_conn); + int rc; + + rc = ibw_send(conn, data, last_key, length); + last_key = NULL; + + return rc; +} + +#ifdef __NOTDEF__ +/* + * transport packet allocator - allows transport to control memory for packets + */ +static void *ctdb_ibw_allocate_pkt(struct ctdb_node *node, size_t size) +{ + struct ibw_conn *conn = NULL; + void *buf = NULL; + + if (ibw_alloc_send_buf(conn, &buf, &last_key, size)) + return NULL; + + return buf; +} + +static void ctdb_ibw_dealloc_pkt(struct ctdb_node *node, void *data) +{ + if (last_key) { + struct ibw_conn *conn = talloc_get_type(node->private, struct ibw_conn); + + assert(conn!=NULL); + ibw_cancel_send_buf(conn, data, last_key); + } /* else ibw_send is already using it and will free it after completion */ +} + +static int ctdb_ibw_stop(struct ctdb_context *cctx) +{ + struct ibw_ctx *ictx = talloc_get_type(cctx->private, struct ibw_ctx); + + assert(ictx!=NULL); + return ibw_stop(ictx); +} + +#endif /* __NOTDEF__ */ + +static const struct ctdb_methods ctdb_ibw_methods = { + .start = ctdb_ibw_start, + .add_node = ctdb_ibw_add_node, + .queue_pkt = ctdb_ibw_queue_pkt, +// .allocate_pkt = ctdb_ibw_allocate_pkt, + +// .dealloc_pkt = ctdb_ibw_dealloc_pkt, +// .stop = ctdb_ibw_stop +}; + +/* + * initialise ibw portion of ctdb + */ +int ctdb_ibw_init(struct ctdb_context *ctdb) +{ + struct ibw_ctx *ictx; + + ictx = ibw_init( + NULL, //struct ibw_initattr *attr, /* TODO */ + 0, //int nattr, /* TODO */ + ctdb, + ctdb_ibw_connstate_handler, + ctdb_ibw_receive_handler, + ctdb->ev); + + if (ictx==NULL) { + DEBUG(0, ("ctdb_ibw_init: ibw_init failed\n")); + return -1; + } + + ctdb->methods = &ctdb_ibw_methods; + ctdb->private = ictx; + return 0; +} diff --git a/ctdb/ib/ibwrapper.c b/ctdb/ib/ibwrapper.c index dec183f900a..2f8dd256f91 100644 --- a/ctdb/ib/ibwrapper.c +++ b/ctdb/ib/ibwrapper.c @@ -39,6 +39,7 @@ #include "ibwrapper.h" #include <rdma/rdma_cma.h> +#include "infiniband/sa-kern-abi.h" #include "ibwrapper_internal.h" #include "lib/util/dlinklist.h" @@ -46,23 +47,31 @@ #define IBW_LASTERR_BUFSIZE 512 static char ibw_lasterr[IBW_LASTERR_BUFSIZE]; +#define IBW_MAX_SEND_WR 256 +#define IBW_MAX_RECV_WR 1024 +#define IBW_RECV_BUFSIZE 256 +#define IBW_RECV_THRESHOLD (1 * 1024 * 1024) + static void ibw_event_handler_verbs(struct event_context *ev, struct fd_event *fde, uint16_t flags, void *private_data); static int ibw_fill_cq(struct ibw_conn *conn); -static inline int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc); -static inline int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc); +static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc); +static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc); +static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len); static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn, - int n, struct ibv_mr **ppmr) + uint32_t n, struct ibv_mr **ppmr) { void *buf; + + DEBUG(10, ("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"); return NULL; } - *ppmr = ibv_reg_mr(pctx->pd, buf, n, IBV_ACCESS_LOCAL_WRITE); + *ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE); if (!*ppmr) { sprintf(ibw_lasterr, "couldn't allocate mr\n"); free(buf); @@ -74,6 +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)); if (*ppmr!=NULL) { ibv_dereg_mr(*ppmr); *ppmr = NULL; @@ -92,8 +102,9 @@ 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)); pconn->buf_send = ibw_alloc_mr(pctx, pconn, - opts->max_send_wr * opts->avg_send_size, &pconn->mr_send); + opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send); if (!pconn->buf_send) { sprintf(ibw_lasterr, "couldn't allocate work send buf\n"); return -1; @@ -111,8 +122,8 @@ static int ibw_init_memory(struct ibw_conn *conn) for(i=0; i<opts->max_send_wr; i++) { p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr); - p->msg = pconn->buf_send + (i * opts->avg_send_size); - p->wr_id = i + opts->max_recv_wr; + p->buf = pconn->buf_send + (i * opts->recv_bufsize); + p->wr_id = i; DLIST_ADD(pconn->wr_list_avail, p); } @@ -122,10 +133,7 @@ static int ibw_init_memory(struct ibw_conn *conn) static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx) { - if (pctx->pd) { - ibv_dealloc_pd(pctx->pd); - pctx->pd = NULL; - } + DEBUG(10, ("ibw_ctx_priv_destruct(%u)\n", (uint32_t)pctx)); /* destroy cm */ if (pctx->cm_channel) { @@ -147,11 +155,15 @@ 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)); 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); @@ -177,6 +189,10 @@ static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn) talloc_free(pconn->verbs_channel_event); pconn->verbs_channel_event = NULL; } + if (pconn->pd) { + ibv_dealloc_pd(pconn->pd); + pconn->pd = NULL; + } if (pconn->cm_id) { rdma_destroy_id(pconn->cm_id); pconn->cm_id = NULL; @@ -186,6 +202,8 @@ static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn) static int ibw_conn_destruct(struct ibw_conn *conn) { + DEBUG(10, ("ibw_conn_destruct(%u)\n", (uint32_t)conn)); + /* important here: ctx is a talloc _parent_ */ DLIST_REMOVE(conn->ctx->conn_list, conn); return 0; @@ -205,6 +223,7 @@ static struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx) talloc_set_destructor(pconn, ibw_conn_priv_destruct); conn->ctx = ctx; + conn->internal = (void *)pconn; DLIST_ADD(ctx->conn_list, conn); @@ -216,11 +235,10 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn) struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); struct ibv_qp_init_attr init_attr; + struct ibv_qp_attr attr; int rc; - /* init mr */ - if (ibw_init_memory(conn)) - return -1; + DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id)); /* init verbs */ pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs); @@ -233,6 +251,17 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn) pconn->verbs_channel_event = event_add_fd(pctx->ectx, conn, pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn); + pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs); + if (!pconn->pd) { + sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno); + return -1; + } + DEBUG(10, ("created pd %p\n", pconn->pd)); + + /* init mr */ + if (ibw_init_memory(conn)) + return -1; + /* init cq */ pconn->cq = ibv_create_cq(pconn->cm_id->verbs, pctx->opts.max_recv_wr + pctx->opts.max_send_wr, @@ -258,13 +287,19 @@ static int ibw_setup_cq_qp(struct ibw_conn *conn) init_attr.send_cq = pconn->cq; init_attr.recv_cq = pconn->cq; - rc = rdma_create_qp(pconn->cm_id, pctx->pd, &init_attr); + rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr); if (rc) { sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc); return rc; } /* elase result is in pconn->cm_id->qp */ + rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr); + if (rc) { + sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc); + return rc; + } + return ibw_fill_cq(conn); } @@ -274,24 +309,26 @@ static int ibw_refill_cq_recv(struct ibw_conn *conn) struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); int rc; struct ibv_sge list = { - .addr = (uintptr_t) NULL, + .addr = (uintptr_t) NULL, /* filled below */ .length = pctx->opts.recv_bufsize, - .lkey = pconn->mr_recv->lkey + .lkey = pconn->mr_recv->lkey /* always the same */ }; struct ibv_recv_wr wr = { - .wr_id = 0, + .wr_id = 0, /* filled below */ .sg_list = &list, .num_sge = 1, }; struct ibv_recv_wr *bad_wr; + DEBUG(10, ("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; pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr; rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr); if (rc) { - sprintf(ibw_lasterr, "ibv_post_recv failed with %d\n", rc); + sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc); DEBUG(0, (ibw_lasterr)); return -2; } @@ -305,17 +342,19 @@ static int ibw_fill_cq(struct ibw_conn *conn) struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); int i, rc; struct ibv_sge list = { - .addr = (uintptr_t) NULL, + .addr = (uintptr_t) NULL, /* filled below */ .length = pctx->opts.recv_bufsize, - .lkey = pconn->mr_recv->lkey + .lkey = pconn->mr_recv->lkey /* always the same */ }; struct ibv_recv_wr wr = { - .wr_id = 0, + .wr_id = 0, /* filled below */ .sg_list = &list, .num_sge = 1, }; struct ibv_recv_wr *bad_wr; + DEBUG(10, ("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; wr.wr_id = pconn->recv_index; @@ -323,7 +362,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, "ibv_post_recv failed with %d\n", rc); + sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc); DEBUG(0, (ibw_lasterr)); return -2; } @@ -337,6 +376,7 @@ static int ibw_manage_connect(struct ibw_conn *conn, struct rdma_cm_id *cma_id) struct rdma_conn_param conn_param; int rc; + DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", cma_id)); rc = ibw_setup_cq_qp(conn); if (rc) return -1; @@ -408,6 +448,9 @@ static void ibw_event_handler_cm(struct event_context *ev, cma_id->context = (void *)conn; DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id)); + if (ibw_setup_cq_qp(conn)) + goto error; + conn->state = IBWC_INIT; pctx->connstate_func(ctx, conn); @@ -415,9 +458,6 @@ static void ibw_event_handler_cm(struct event_context *ev, if (!pconn->is_accepted) { talloc_free(conn); DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id)); - } else { - if (ibw_setup_cq_qp(conn)) - goto error; } /* TODO: clarify whether if it's needed by upper layer: */ @@ -429,7 +469,7 @@ 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: %u)\n", (unsigned int)cma_id->context)); + DEBUG(0, ("ESTABLISHED (conn: %p)\n", cma_id->context)); conn = talloc_get_type(cma_id->context, struct ibw_conn); assert(conn!=NULL); /* important assumption */ @@ -450,21 +490,10 @@ static void ibw_event_handler_cm(struct event_context *ev, case RDMA_CM_EVENT_DISCONNECTED: if (cma_id!=pctx->cm_id) { - DEBUG(0, ("client DISCONNECT event\n")); + DEBUG(0, ("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); - - talloc_free(conn); - - /* if we are the last... */ - if (ctx->conn_list==NULL) - rdma_disconnect(pctx->cm_id); - } else { - DEBUG(0, ("server DISCONNECT event\n")); - ctx->state = IBWS_STOPPED; /* ??? TODO: try it... */ - /* talloc_free(ctx) should be called within or after this func */ - pctx->connstate_func(ctx, NULL); } break; @@ -508,6 +537,8 @@ 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)); + /* TODO: check whether if it's good to have more channels here... */ rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx); if (rc) { @@ -515,8 +546,7 @@ static void ibw_event_handler_verbs(struct event_context *ev, goto error; } if (ev_cq != pconn->cq) { - sprintf(ibw_lasterr, "ev_cq(%u) != pconn->cq(%u)\n", - (unsigned int)ev_cq, (unsigned int)pconn->cq); + sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq); goto error; } rc = ibv_req_notify_cq(pconn->cq, 0); @@ -527,8 +557,8 @@ 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\n", - wc.status); + sprintf(ibw_lasterr, "cq completion failed status %d rc %d\n", + wc.status, rc); goto error; } @@ -570,75 +600,93 @@ error: pctx->connstate_func(NULL, conn); } -static inline int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc) +static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc) { struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); 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", + pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len)); assert(pconn->cm_id->qp->qp_num==wc->qp_num); - assert(wc->wr_id > pctx->opts.max_recv_wr); + assert(wc->wr_id >= pctx->opts.max_recv_wr); send_index = wc->wr_id - pctx->opts.max_recv_wr; + pconn->wr_sent--; if (send_index < pctx->opts.max_send_wr) { - DEBUG(10, ("ibw_wc_send#1 %u", (int)wc->wr_id)); + DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id)); p = pconn->wr_index[send_index]; - if (p->msg_large) - ibw_free_mr(&p->msg_large, &p->mr_large); - DLIST_REMOVE(pconn->wr_list_used, p); - DLIST_ADD(pconn->wr_list_avail, p); - } else { - DEBUG(10, ("ibw_wc_send#2 %u", (int)wc->wr_id)); - for(p=pconn->queue_sent; p!=NULL; p=p->next) - if (p->wr_id==(int)wc->wr_id) + if (p->buf_large!=NULL) { + if (p->ref_cnt) { + /* awaiting more of it... */ + p->ref_cnt--; + } else { + ibw_free_mr(&p->buf_large, &p->mr_large); + DLIST_REMOVE(pconn->wr_list_used, p); + DLIST_ADD(pconn->wr_list_avail, p); + } + } else { /* nasty - but necessary */ + DLIST_REMOVE(pconn->wr_list_used, p); + DLIST_ADD(pconn->wr_list_avail, p); + } + } else { /* "extra" request - not optimized */ + DEBUG(10, ("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; if (p==NULL) { sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id); return -1; } - ibw_free_mr(&p->msg_large, &p->mr_large); - DLIST_REMOVE(pconn->queue_sent, p); - DLIST_ADD(pconn->queue_avail, p); + if (p->ref_cnt) { + p->ref_cnt--; + } else { + ibw_free_mr(&p->buf_large, &p->mr_large); + DLIST_REMOVE(pconn->extra_sent, p); + DLIST_ADD(pconn->extra_avail, p); + } } if (pconn->queue) { - struct ibv_sge list = { - .addr = (uintptr_t) NULL, - .length = *(uint32_t *)(p->msg_large), - .lkey = 0 - }; - struct ibv_send_wr wr = { - .wr_id = p->wr_id + pctx->opts.max_recv_wr, - .sg_list = &list, - .num_sge = 1, - .opcode = IBV_WR_SEND, - .send_flags = IBV_SEND_SIGNALED, - }; - struct ibv_send_wr *bad_wr; - int rc; - + uint32_t msg_size; + + DEBUG(10, ("ibw_wc_send#queue %u\n", (int)wc->wr_id)); + p = pconn->queue; - DLIST_REMOVE(pconn->queue, p); - DLIST_ADD(pconn->queue_sent, p); - rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr); - if (rc) { - sprintf(ibw_lasterr, "ibv_post_send failed with %d\n", rc); - return -1; + + 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 0; + return rc; } -static inline int ibw_append_to_part(void *memctx, struct ibw_part *part, - char **pp, uint32_t add_len, int info) +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", + pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info)); + /* allocate more if necessary - it's an "evergrowing" buffer... */ if (part->len + add_len > part->bufsize) { if (part->buf==NULL) { assert(part->len==0); - part->buf = talloc_size(memctx, add_len); + part->buf = talloc_size(pconn, add_len); if (part->buf==NULL) { sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n", add_len, info); @@ -646,7 +694,7 @@ static inline int ibw_append_to_part(void *memctx, struct ibw_part *part, } part->bufsize = add_len; } else { - part->buf = talloc_realloc_size(memctx, + part->buf = talloc_realloc_size(pconn, part->buf, part->len + add_len); if (part->buf==NULL) { sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n", @@ -666,11 +714,17 @@ static inline int ibw_append_to_part(void *memctx, struct ibw_part *part, return 0; } -static inline int ibw_wc_mem_threshold(void *memctx, struct ibw_part *part, uint32_t threshold) +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", + 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", + pconn->cm_id, part->bufsize, threshold)); talloc_free(part->buf); - part->buf = talloc_size(memctx, threshold); + part->buf = talloc_size(pconn, threshold); if (part->buf==NULL) { sprintf(ibw_lasterr, "talloc_size failed\n"); return -1; @@ -680,13 +734,16 @@ static inline int ibw_wc_mem_threshold(void *memctx, struct ibw_part *part, uint return 0; } -static inline int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc) +static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc) { struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); struct ibw_part *part = &pconn->part; char *p; - uint32_t remain; + uint32_t remain = wc->byte_len; + + DEBUG(10, ("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); assert((int)wc->wr_id < pctx->opts.max_recv_wr); @@ -694,7 +751,6 @@ static inline int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc) p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize); - remain = wc->byte_len; while(remain) { /* here always true: (part->len!=0 && part->to_read!=0) || (part->len==0 && part->to_read==0) */ @@ -707,7 +763,7 @@ static inline int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc) if (part->len<=sizeof(uint32_t) && part->to_read==0) { assert(part->len==sizeof(uint32_t)); /* set it again now... */ - part->to_read = *((uint32_t *)(part->buf)); + part->to_read = ntohl(*((uint32_t *)(part->buf))); if (part->to_read<sizeof(uint32_t)) { sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read); goto error; @@ -723,7 +779,7 @@ static inline int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc) } } else { if (remain>=sizeof(uint32_t)) { - uint32_t msglen = *(uint32_t *)p; + uint32_t msglen = ntohl(*(uint32_t *)p); if (msglen<sizeof(uint32_t)) { sprintf(ibw_lasterr, "got msglen=%u\n", msglen); goto error; @@ -768,11 +824,12 @@ static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct i int i; const char *name, *value; - opts->max_send_wr = 256; - opts->max_recv_wr = 1024; - opts->avg_send_size = 1024; - opts->recv_bufsize = 256; - opts->recv_threshold = 1 * 1024 * 1024; + DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr)); + + opts->max_send_wr = IBW_MAX_SEND_WR; + opts->max_recv_wr = IBW_MAX_RECV_WR; + opts->recv_bufsize = IBW_RECV_BUFSIZE; + opts->recv_threshold = IBW_RECV_THRESHOLD; for(i=0; i<nattr; i++) { name = attr[i].name; @@ -783,8 +840,6 @@ static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct i opts->max_send_wr = atoi(value); else if (strcmp(name, "max_recv_wr")==0) opts->max_recv_wr = atoi(value); - else if (strcmp(name, "avg_send_size")==0) - opts->avg_send_size = atoi(value); else if (strcmp(name, "recv_bufsize")==0) opts->recv_bufsize = atoi(value); else if (strcmp(name, "recv_threshold")==0) @@ -807,6 +862,9 @@ 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)); + /* initialize basic data structures */ memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE); @@ -847,14 +905,6 @@ struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr, } DEBUG(10, ("created cm_id %p\n", pctx->cm_id)); - /* init verbs */ - pctx->pd = ibv_alloc_pd(pctx->cm_id->verbs); - if (!pctx->pd) { - sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno); - goto cleanup; - } - DEBUG(10, ("created pd %p\n", pctx->pd)); - pctx->pagesize = sysconf(_SC_PAGESIZE); return ctx; @@ -870,8 +920,11 @@ cleanup: 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")); + for(p=ctx->conn_list; p!=NULL; p=p->next) { if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) { if (ibw_disconnect(p)) @@ -879,6 +932,9 @@ int ibw_stop(struct ibw_ctx *ctx) } } + ctx->state = IBWS_STOPPED; + pctx->connstate_func(ctx, NULL); + return 0; } @@ -887,6 +943,8 @@ 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", + inet_ntoa(my_addr->sin_addr), 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); @@ -903,13 +961,13 @@ 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, ("rdma_listen...\n")); + DEBUG(10, ("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)); return rc; - } + } return 0; } @@ -920,6 +978,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)); conn->conn_userdata = conn_userdata; memset(&conn_param, 0, sizeof(struct rdma_conn_param)); @@ -949,15 +1008,18 @@ int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_u conn = ibw_conn_new(ctx); 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), serv_addr->sin_port)); + /* init cm */ rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP); if (rc) { rc = errno; - sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc); + sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc); return rc; } + DEBUG(10, ("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); + 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)); @@ -972,52 +1034,56 @@ int ibw_connect(struct ibw_ctx *ctx, struct sockaddr_in *serv_addr, void *conn_u int ibw_disconnect(struct ibw_conn *conn) { int rc; - struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); + struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); + + DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id)); - rc = rdma_disconnect(pctx->cm_id); + rc = rdma_disconnect(pconn->cm_id); if (rc) { - sprintf(ibw_lasterr, "ibw_disconnect failed with %d", rc); + sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc); DEBUG(0, (ibw_lasterr)); return rc; } - /* continued at RDMA_CM_EVENT_DISCONNECTED */ - return 0; } -int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n) +int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len) { struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); struct ibw_wr *p = pconn->wr_list_avail; - if (p) { + if (p!=NULL) { + DEBUG(10, ("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); - if (n + sizeof(long) <= pctx->opts.avg_send_size) { - *buf = (void *)(p->msg + sizeof(long)); + if (len <= pctx->opts.recv_bufsize) { + *buf = (void *)p->buf; } else { - p->msg_large = ibw_alloc_mr(pctx, pconn, n + sizeof(long), &p->mr_large); - if (!p->msg_large) { + p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large); + if (p->buf_large==NULL) { sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n"); goto error; } - *buf = (void *)(p->msg_large + sizeof(long)); + *buf = (void *)p->buf_large; } + /* 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)); /* not optimized */ - p = pconn->queue_avail; + p = pconn->extra_avail; if (!p) { - p = pconn->queue_avail = talloc_zero(pconn, struct ibw_wr); + p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr); if (p==NULL) { - sprintf(ibw_lasterr, "talloc_zero failed (qmax: %u)", pconn->queue_max); + sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max); goto error; } - p->wr_id = pconn->queue_max + pctx->opts.max_send_wr; - pconn->queue_max++; - switch(pconn->queue_max) { + 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; @@ -1025,36 +1091,42 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n) default: break; } } - DLIST_REMOVE(pconn->queue_avail, p); - p->msg_large = ibw_alloc_mr(pctx, pconn, n + sizeof(long), &p->mr_large); - if (!p->msg_large) { - sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed"); + p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large); + if (p->buf_large==NULL) { + sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n"); goto error; } - *buf = (void *)(p->msg_large + sizeof(long)); + *buf = (void *)p->buf_large; + + DLIST_REMOVE(pconn->extra_avail, p); + /* we don't have prepared index for this, so that + * we will have to find this by wr_id later on */ + DLIST_ADD(pconn->extra_sent, p); } *key = (void *)p; return 0; error: - DEBUG(0, ("ibw_alloc_send_buf error: %s\n", ibw_lasterr)); + DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr)); return -1; } -int ibw_send(struct ibw_conn *conn, void *buf, void *key, int n) +static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len) { struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); - struct ibw_wr *p = talloc_get_type(key, struct ibw_wr); + int rc; - if (p->msg!=NULL) { + /* can we send it right now? */ + if (pconn->wr_sent<pctx->opts.max_send_wr) { + struct ibv_send_wr *bad_wr; struct ibv_sge list = { - .addr = (uintptr_t) NULL, - .length = n, - .lkey = 0 + .addr = (uintptr_t)buf, + .length = len, + .lkey = pconn->mr_send->lkey }; struct ibv_send_wr wr = { .wr_id = p->wr_id + pctx->opts.max_recv_wr, @@ -1063,29 +1135,111 @@ int ibw_send(struct ibw_conn *conn, void *buf, void *key, int n) .opcode = IBV_WR_SEND, .send_flags = IBV_SEND_SIGNALED, }; - struct ibv_send_wr *bad_wr; - if (n + sizeof(uint32_t)<=pctx->opts.avg_send_size) { - assert((p->msg + sizeof(long))==(char *)buf); - list.lkey = pconn->mr_send->lkey; - list.addr = (uintptr_t) p->msg; - - *((uint32_t *)p->msg) = htonl(n); + if (p->buf_large==NULL) { + DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n", + pconn->cm_id, (uint32_t)wr.wr_id, len)); } else { - assert((p->msg_large + sizeof(long))==(char *)buf); - assert(p->mr_large!=NULL); + DEBUG(10, ("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; - list.addr = (uintptr_t) p->msg_large; - - *((uint32_t *)p->msg_large) = htonl(n); } - return ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr); - } /* else: */ - *((uint32_t *)p->msg_large) = htonl(n); + rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr); + if (rc) { + sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n", + rc, pconn->wr_sent); + goto error; + } + + pconn->wr_sent++; + + return rc; + } /* else put the request into our own queue: */ + + DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len)); /* to be sent by ibw_wc_send */ - DLIST_ADD_END(pconn->queue, p, struct ibw_wr *); /* TODO: optimize */ + /* regardless "normal" or [a part of] "large" packet */ + if (!p->queued_ref_cnt) { + DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *, + qprev, qnext); /* TODO: optimize */ + p->queued_msg = buf; + } + p->queued_ref_cnt++; + p->queued_rlen = len; /* last wins; see ibw_wc_send */ + + return 0; +error: + DEBUG(0, (ibw_lasterr)); + return -1; +} + +int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len) +{ + struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); + struct ibw_wr *p = talloc_get_type(key, struct ibw_wr); + int rc; + + assert(len>=sizeof(uint32_t)); + *((uint32_t *)buf) = htonl(len); + + if (len > pctx->opts.recv_bufsize) { + struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); + int rlen = 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", + pconn->cm_id, buf, len)); + + /* single threaded => no race here: */ + assert(p->ref_cnt==0); + while(rlen > recv_bufsize) { + rc = ibw_send_packet(conn, packet, p, recv_bufsize); + if (rc) + return rc; + packet += recv_bufsize; + rlen -= recv_bufsize; + p->ref_cnt++; /* not good to have it in ibw_send_packet */ + } + if (rlen) { + rc = ibw_send_packet(conn, packet, p, rlen); + p->ref_cnt++; /* not good to have it in ibw_send_packet */ + } + p->ref_cnt--; /* for the same handling */ + } else { + assert(p->ref_cnt==0); + assert(p->queued_ref_cnt==0); + + rc = ibw_send_packet(conn, buf, p, len); + } + return rc; +} + +int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key) +{ + struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv); + struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv); + struct ibw_wr *p = talloc_get_type(key, struct ibw_wr); + + assert(p!=NULL); + assert(buf!=NULL); + assert(conn!=NULL); + + if (p->buf_large!=NULL) + ibw_free_mr(&p->buf_large, &p->mr_large); + + /* parallel case */ + if (p->wr_id < pctx->opts.max_send_wr) { + DEBUG(10, ("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)); + DLIST_REMOVE(pconn->extra_sent, p); + DLIST_ADD(pconn->extra_avail, p); + } return 0; } diff --git a/ctdb/ib/ibwrapper.h b/ctdb/ib/ibwrapper.h index 8934e68d9fb..52018cb7cb5 100644 --- a/ctdb/ib/ibwrapper.h +++ b/ctdb/ib/ibwrapper.h @@ -185,7 +185,7 @@ int ibw_disconnect(struct ibw_conn *conn); * * Returns 0 on success. */ -int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n); +int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len); /* * Send the message in one @@ -195,7 +195,14 @@ int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, int n); * * You mustn't use (buf, key) any more for sending. */ -int ibw_send(struct ibw_conn *conn, void *buf, void *key, int n); +int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len); + +/* + * Call this after ibw_alloc_send_buf + * when you won't call ibw_send for (buf, key) + * You mustn't use (buf, key) any more. + */ +int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key); /* * Retrieves the last error diff --git a/ctdb/ib/ibwrapper_internal.h b/ctdb/ib/ibwrapper_internal.h index 687a5797681..9c6bfab519e 100644 --- a/ctdb/ib/ibwrapper_internal.h +++ b/ctdb/ib/ibwrapper_internal.h @@ -24,19 +24,25 @@ struct ibw_opts { uint32_t max_send_wr; uint32_t max_recv_wr; - uint32_t avg_send_size; uint32_t recv_bufsize; uint32_t recv_threshold; }; struct ibw_wr { - char *msg; /* initialized in ibw_init_memory once per connection */ + char *buf; /* initialized in ibw_init_memory once per connection */ int wr_id; /* position in wr_index list; also used as wr id */ - char *msg_large; /* allocated specially for "large" message */ + char *buf_large; /* allocated specially for "large" message */ struct ibv_mr *mr_large; + int ref_cnt; /* reference count for ibw_wc_send to know when to release */ + + char *queued_msg; /* set at ibw_send - can be different than above */ + int queued_ref_cnt; /* instead of adding the same to the queue again */ + uint32_t queued_rlen; /* last wins when queued_ref_cnt>0; or simple msg size */ struct ibw_wr *next, *prev; /* in wr_list_avail or wr_list_used */ + /* or extra_sent or extra_avail */ + struct ibw_wr *qnext, *qprev; /* in queue */ }; struct ibw_ctx_priv { @@ -49,8 +55,6 @@ struct ibw_ctx_priv { struct rdma_event_channel *cm_channel; struct fd_event *cm_channel_event; - struct ibv_pd *pd; - ibw_connstate_fn_t connstate_func; /* see ibw_init */ ibw_receive_fn_t receive_func; /* see ibw_init */ @@ -69,6 +73,7 @@ struct ibw_conn_priv { struct fd_event *verbs_channel_event; struct rdma_cm_id *cm_id; /* client's cm id */ + struct ibv_pd *pd; int is_accepted; struct ibv_cq *cq; /* qp is in cm_id */ @@ -78,11 +83,13 @@ struct ibw_conn_priv { struct ibw_wr *wr_list_avail; struct ibw_wr *wr_list_used; struct ibw_wr **wr_index; /* array[0..(qsize-1)] of (ibw_wr *) */ + int wr_sent; /* # of send wrs in the CQ */ + + struct ibw_wr *extra_sent; + struct ibw_wr *extra_avail; + int extra_max; /* max wr_id in the queue */ struct ibw_wr *queue; - struct ibw_wr *queue_sent; - struct ibw_wr *queue_avail; - int queue_max; /* max wr_id in the queue */ /* buf_recv is a ring buffer */ char *buf_recv; /* max_recv_wr * avg_recv_size */ @@ -91,3 +98,30 @@ struct ibw_conn_priv { struct ibw_part part; }; +/* remove an element from a list - element doesn't have to be in list. */ +#define DLIST_REMOVE2(list, p, prev, next) \ +do { \ + if ((p) == (list)) { \ + (list) = (p)->next; \ + if (list) (list)->prev = NULL; \ + } else { \ + if ((p)->prev) (p)->prev->next = (p)->next; \ + if ((p)->next) (p)->next->prev = (p)->prev; \ + } \ + if ((p) != (list)) (p)->next = (p)->prev = NULL; \ +} while (0) + +/* hook into the end of the list - needs a tmp pointer */ +#define DLIST_ADD_END2(list, p, type, prev, next) \ +do { \ + if (!(list)) { \ + (list) = (p); \ + (p)->next = (p)->prev = NULL; \ + } else { \ + type tmp; \ + for (tmp = (list); tmp->next; tmp = tmp->next) ; \ + tmp->next = (p); \ + (p)->next = NULL; \ + (p)->prev = tmp; \ + } \ +} while (0) diff --git a/ctdb/include/includes.h b/ctdb/include/includes.h index b160e1f739c..199f416b20a 100644 --- a/ctdb/include/includes.h +++ b/ctdb/include/includes.h @@ -12,7 +12,9 @@ typedef bool BOOL; #define True 1 #define False 0 -#define DEBUG(lvl, x) printf x +#define LogLevel 0 + +#define DEBUG(lvl, x) if ((lvl) <= LogLevel) (printf x) #define _PUBLIC_ diff --git a/ctdb/tests/ibwrapper_test.c b/ctdb/tests/ibwrapper_test.c index c00d0a747cf..46672ceda95 100644 --- a/ctdb/tests/ibwrapper_test.c +++ b/ctdb/tests/ibwrapper_test.c @@ -34,6 +34,8 @@ #include <assert.h> #include <unistd.h> #include <signal.h> +#include <sys/time.h> +#include <time.h> #include "includes.h" #include "lib/events/events.h" @@ -50,12 +52,20 @@ struct ibwtest_ctx { struct sockaddr_in *addrs; /* dynamic array of dest addrs */ int naddrs; - unsigned int nsec; /* nanosleep between messages */ + unsigned int nsec; /* delta times between messages in nanosec */ + unsigned int sleep_nsec; /* nanosleep in the main loop to emulate overloading */ + uint32_t maxsize; /* maximum variable message size */ int cnt; + int nmsg; /* number of messages to send (client) */ + int kill_me; + int stopping; + int error; struct ibw_ctx *ibwctx; + + struct timeval start_time, end_time; }; struct ibwtest_conn { @@ -64,9 +74,30 @@ struct ibwtest_conn { enum testopcode { TESTOP_SEND_ID = 1, - TESTOP_SEND_DATA = 2 + TESTOP_SEND_TEXT = 2, + TESTOP_SEND_RND = 3 }; +int ibwtest_nanosleep(unsigned int sleep_nsec) +{ +// int nanosleep(const struct timespec *req, struct timespec *rem); + struct timespec req, rem; + + memset(&req, 0, sizeof(struct timespec)); + memset(&rem, 0, sizeof(struct timespec)); + req.tv_sec = 0; + req.tv_nsec = sleep_nsec; + + while (nanosleep(&req, &rem) < 0) { + if (errno != EINTR) { + DEBUG(0, ("nanosleep ERROR: %d\n", errno)); + return -1; + } + memcpy(&req, &rem, sizeof(struct timespec)); + } + return 0; +} + int ibwtest_connect_everybody(struct ibwtest_ctx *tcx) { struct ibwtest_conn *pconn = talloc_zero(tcx, struct ibwtest_conn); @@ -85,20 +116,23 @@ int ibwtest_connect_everybody(struct ibwtest_ctx *tcx) int ibwtest_send_id(struct ibw_conn *conn) { + struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx); char *buf; void *key; - struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx); + uint32_t len; - DEBUG(10, ("test IBWC_CONNECTED\n")); - if (ibw_alloc_send_buf(conn, (void **)&buf, &key, strlen(tcx->id)+2)) { + DEBUG(10, ("ibwtest_send_id\n")); + len = sizeof(uint32_t)+strlen(tcx->id)+2; + if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) { DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n")); return -1; } - buf[0] = (char)TESTOP_SEND_ID; - strcpy(buf+1, tcx->id); + /* first sizeof(uint32_t) size bytes are for length */ + buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID; + strcpy(buf+sizeof(uint32_t)+1, tcx->id); - if (ibw_send(conn, buf, key, strlen(buf+1)+2)) { + if (ibw_send(conn, buf, key, len)) { DEBUG(0, ("send_id: ibw_send error\n")); return -1; } @@ -107,24 +141,109 @@ int ibwtest_send_id(struct ibw_conn *conn) int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg) { - char *buf; + char *buf, *p; void *key; + uint32_t len; - if (ibw_alloc_send_buf(conn, (void **)&buf, &key, strlen(msg)+2)) { + if (conn->state!=IBWC_CONNECTED) + return 0; /* not yet up */ + + len = strlen(msg) + 2 + sizeof(uint32_t); + if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) { fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n"); return -1; } - buf[0] = (char)TESTOP_SEND_DATA; - strcpy(buf+1, msg); - - if (ibw_send(conn, buf, key, strlen(buf+1)+2)) { + p = buf; + p += sizeof(uint32_t); + p[0] = (char)TESTOP_SEND_TEXT; + p++; + strcpy(p, msg); + + if (ibw_send(conn, buf, key, len)) { DEBUG(0, ("send_test_msg: ibw_send error\n")); return -1; } return 0; } +unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size) +{ + uint32_t i = size; + unsigned char sum = 0; + unsigned char value; + while(i) { + i--; + value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0))); + buf[i] = value; + sum += value; + } + return sum; +} + +unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size) +{ + uint32_t i = size; + unsigned char sum = 0; + + while(i) { + i--; + sum += buf[i]; + } + return sum; +} + +int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size) +{ + unsigned char *buf; + void *key; + uint32_t len; + unsigned char sum; + + len = sizeof(uint32_t) + 1 + size + 1; + if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) { + DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n")); + return -1; + } + buf[sizeof(uint32_t)] = TESTOP_SEND_RND; + sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size); + buf[sizeof(uint32_t) + 1 + size] = sum; + if (ibw_send(conn, buf, key, len)) { + DEBUG(0, ("varsize/ibw_send failed\n")); + return -1; + } + return 0; +} + +int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn) +{ + uint32_t size; + int i; + + for(i=0; i<tcx->nmsg; i++) + { + //size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0))); + size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg)); + if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size)) + return -1; + } + return 0; +} + +/*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx) +{ + int rc; + struct ibw_conn *conn; + + for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) { + if (conn->state==IBWC_CONNECTED) { + rc = ibwtest_do_varsize_scenario_conn(tcx, conn); + if (rc) + tcx->error = rc; + } + } +}*/ + int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn) { struct ibwtest_ctx *tcx = NULL; /* userdata */ @@ -149,8 +268,6 @@ int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn) break; case IBWS_STOPPED: DEBUG(10, ("test IBWS_STOPPED\n")); - talloc_free(tcx->ibwctx); - DEBUG(10, ("talloc_free(tcx->ibwctx) DONE\n")); tcx->kill_me = 1; /* main loop can exit */ break; case IBWS_ERROR: @@ -170,10 +287,15 @@ int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn) DEBUG(10, ("test IBWC_INIT\n")); break; case IBWC_CONNECTED: + if (gettimeofday(&tcx->start_time, NULL)) { + DEBUG(0, ("gettimeofday error %d", errno)); + return -1; + } ibwtest_send_id(conn); break; case IBWC_DISCONNECTED: DEBUG(10, ("test IBWC_DISCONNECTED\n")); + talloc_free(conn); break; case IBWC_ERROR: DEBUG(10, ("test IBWC_ERROR\n")); @@ -191,58 +313,120 @@ int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n) struct ibwtest_conn *pconn; enum testopcode op; struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx); + int rc = 0; assert(conn!=NULL); + assert(n>=sizeof(uint32_t)+1); pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn); - op = (enum testopcode)((char *)buf)[0]; - DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op, - pconn->id ? pconn->id : NULL, ((char *)buf)+1, n)); + op = (enum testopcode)((char *)buf)[sizeof(uint32_t)]; + if (op==TESTOP_SEND_ID) { + pconn->id = talloc_strdup(pconn, ((char *)buf)+sizeof(uint32_t)+1); + } + if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) { + DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op, + pconn->id ? pconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n)); + } if (tcx->is_server) { - char *buf2; - void *key2; - /* bounce message */ - if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) { - fprintf(stderr, "ibw_alloc_send_buf error #2\n"); - return -1; + if (op==TESTOP_SEND_RND) { + unsigned char sum; + sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1, + n - sizeof(uint32_t) - 2); + DEBUG(11, ("[%d]msg varsize %u/sum %u from %s\n", + op, + n - sizeof(uint32_t) - 2, + (uint32_t)sum, + pconn->id ? pconn->id : "NULL")); + if (sum!=((unsigned char *)buf)[n-1]) { + DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n", + (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1])); + ibw_stop(tcx->ibwctx); + return -3; + } + } else { + char *buf2; + void *key2; + + /* bounce message regardless what it is */ + if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) { + fprintf(stderr, "ibw_alloc_send_buf error #2\n"); + return -1; + } + memcpy(buf2, buf, n); + if (ibw_send(conn, buf2, key2, n)) { + fprintf(stderr, "ibw_send error #2\n"); + return -2; + } } - memcpy(buf2, buf, n); - if (ibw_send(conn, buf2, key2, n)) { - fprintf(stderr, "ibw_send error #2\n"); - return -2; + } else { /* client: */ + if (op==TESTOP_SEND_ID && tcx->maxsize) { + /* send them in one blow */ + rc = ibwtest_do_varsize_scenario_conn(tcx, conn); + } + + if (tcx->nmsg) { + char msg[26]; + sprintf(msg, "hello world %d", tcx->nmsg--); + rc = ibwtest_send_test_msg(tcx, conn, msg); + if (tcx->nmsg==0) { + ibw_stop(tcx->ibwctx); + tcx->stopping = 1; + } } } - return 0; + if (rc) + tcx->error = rc; + + return rc; } void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te, struct timeval t, void *private) { struct ibwtest_ctx *tcx = talloc_get_type(private, struct ibwtest_ctx); + int rc; if (!tcx->is_server) { - struct ibw_conn *p; + struct ibw_conn *conn; char msg[50]; /* fill it with something variable... */ sprintf(msg, "hello world %d", tcx->cnt++); /* send something to everybody... */ - for(p=tcx->ibwctx->conn_list; p!=NULL; p=p->next) { - ibwtest_send_test_msg(tcx, p, msg); + for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) { + if (conn->state==IBWC_CONNECTED) { + rc = ibwtest_send_test_msg(tcx, conn, msg); + if (rc) + tcx->error = rc; + } } } /* else allow main loop run */ } static struct ibwtest_ctx *testctx = NULL; -void ibwtest_sigquit_handler(int sig) +void ibwtest_sigint_handler(int sig) { - DEBUG(0, ("got SIGQUIT\n")); - if (testctx) - ibw_stop(testctx->ibwctx); + DEBUG(0, ("got SIGINT\n")); + if (testctx) { + if (testctx->ibwctx->state==IBWS_READY || + testctx->ibwctx->state==IBWS_CONNECT_REQUEST || + testctx->ibwctx->state==IBWS_ERROR) + { + if (testctx->stopping) { + DEBUG(10, ("forcing exit...\n")); + testctx->kill_me = 1; + } else { + /* mostly expected case */ + ibw_stop(testctx->ibwctx); + testctx->stopping = 1; + } + } else + testctx->kill_me = 1; + } } int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext, @@ -274,9 +458,10 @@ int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext, porcess_next = 0; i++; + p = q; /* ++ at end */ } if (*p==',') { - *p = '\0'; + *p = '\0'; /* ++ at end */ porcess_next = 1; } } @@ -302,9 +487,9 @@ int ibwtest_getdests(struct ibwtest_ctx *tcx, char op) tcx->naddrs * sizeof(struct sockaddr_in)); for(i=0; i<tcx->naddrs; i++) { p = tcx->addrs + i; + p->sin_family = AF_INET; p->sin_addr.s_addr = inet_addr(attrs[i].name); p->sin_port = atoi(attrs[i].value); - p->sin_family = AF_INET; } return 0; @@ -313,7 +498,7 @@ int ibwtest_getdests(struct ibwtest_ctx *tcx, char op) int ibwtest_init_server(struct ibwtest_ctx *tcx) { if (tcx->naddrs!=1) { - fprintf(stderr, "incorrecr number of addrs(%d!=1)\n", tcx->naddrs); + fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs); return -1; } @@ -321,6 +506,11 @@ int ibwtest_init_server(struct ibwtest_ctx *tcx) DEBUG(0, ("ERROR: ibw_bind failed\n")); return -1; } + + if (ibw_listen(tcx->ibwctx, 1)) { + DEBUG(0, ("ERROR: ibw_listen failed\n")); + return -1; + } /* continued at IBWS_READY */ return 0; @@ -334,26 +524,33 @@ void ibwtest_usage(struct ibwtest_ctx *tcx, char *name) printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n"); printf("\t-d addr1:port1,addr2:port2,... is a list of destination ip addresses\n"); printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec); + printf("\t\t send message periodically and endless when nsec is non-zero\n"); printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n"); + printf("\t-n number of messages to send [default %d]\n", tcx->nmsg); + printf("\t-l nsec time to sleep in the main loop [default %d]\n", tcx->sleep_nsec); + printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize); printf("Press ctrl+C to stop the program.\n"); } int main(int argc, char *argv[]) { int rc, op; - int result = 1; + int result = 1, nmsg; struct event_context *ev = NULL; struct ibwtest_ctx *tcx = NULL; + float usec; tcx = talloc_zero(NULL, struct ibwtest_ctx); memset(tcx, 0, sizeof(struct ibwtest_ctx)); - tcx->nsec = 1000; + tcx->nsec = 0; + tcx->nmsg = 1000; /* here is the only case we can't avoid using global... */ testctx = tcx; - signal(SIGQUIT, ibwtest_sigquit_handler); + signal(SIGINT, ibwtest_sigint_handler); + srand((unsigned)time(NULL)); - while ((op=getopt(argc, argv, "i:o:d:m:s")) != -1) { + while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:")) != -1) { switch (op) { case 'i': tcx->id = talloc_strdup(tcx, optarg); @@ -371,6 +568,18 @@ int main(int argc, char *argv[]) case 's': tcx->is_server = 1; break; + case 't': + tcx->nsec = (unsigned int)atoi(optarg); + break; + case 'n': + tcx->nmsg = atoi(optarg); + break; + case 'l': + tcx->sleep_nsec = (unsigned int)atoi(optarg); + break; + case 'v': + tcx->maxsize = (unsigned int)atoi(optarg); + break; default: fprintf(stderr, "ERROR: unknown option -%c\n", (char)op); ibwtest_usage(tcx, argv[0]); @@ -381,6 +590,7 @@ int main(int argc, char *argv[]) ibwtest_usage(tcx, argv[0]); goto cleanup; } + nmsg = tcx->nmsg; ev = event_context_init(NULL); assert(ev); @@ -401,13 +611,33 @@ int main(int argc, char *argv[]) if (rc) goto cleanup; - while(!tcx->kill_me) { - event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec), - ibwtest_timeout_handler, tcx); + while(!tcx->kill_me && !tcx->error) { + if (tcx->nsec) { + event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec), + ibwtest_timeout_handler, tcx); + } + event_loop_once(ev); + + if (tcx->sleep_nsec) { + if (ibwtest_nanosleep(tcx->sleep_nsec)) + goto cleanup; + } + } + + if (!tcx->is_server && nmsg!=0 && !tcx->error) { + if (gettimeofday(&tcx->end_time, NULL)) { + DEBUG(0, ("gettimeofday error %d\n", errno)); + goto cleanup; + } + usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 + + (tcx->end_time.tv_usec - tcx->start_time.tv_usec); + printf("usec: %f, nmsg: %d, usec/nmsg: %f\n", + usec, nmsg, usec/(float)nmsg); } - result = 0; /* everything OK */ + if (!tcx->error) + result = 0; /* everything OK */ cleanup: if (tcx) |