2 * Unix SMB/CIFS implementation.
3 * Test the infiniband wrapper.
5 * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
7 * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
41 #include "lib/events/events.h"
42 #include "ib/ibwrapper.h"
48 struct ibw_initattr *attrs;
50 char *opts; /* option string */
52 struct sockaddr_in *addrs; /* dynamic array of dest addrs */
55 unsigned int nsec; /* delta times between messages in nanosec */
56 unsigned int sleep_usec; /* microsecs to sleep in the main loop to emulate overloading */
57 uint32_t maxsize; /* maximum variable message size */
62 int nmsg; /* number of messages to send (client) */
67 struct ibw_ctx *ibwctx;
69 struct timeval start_time, end_time;
82 int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
84 struct ibwtest_conn *pconn = talloc_zero(tcx, struct ibwtest_conn);
87 for(i=0; i<tcx->naddrs; i++) {
88 if (ibw_connect(tcx->ibwctx, &tcx->addrs[i], pconn)) {
89 fprintf(stderr, "ibw_connect error at %d\n", i);
93 DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));
98 int ibwtest_send_id(struct ibw_conn *conn)
100 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
105 DEBUG(10, ("ibwtest_send_id\n"));
106 len = sizeof(uint32_t)+strlen(tcx->id)+2;
107 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
108 DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n"));
112 /* first sizeof(uint32_t) size bytes are for length */
113 buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
114 strcpy(buf+sizeof(uint32_t)+1, tcx->id);
116 if (ibw_send(conn, buf, key, len)) {
117 DEBUG(0, ("send_id: ibw_send error\n"));
125 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
131 if (conn->state!=IBWC_CONNECTED)
132 return 0; /* not yet up */
134 len = strlen(msg) + 2 + sizeof(uint32_t);
135 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
136 fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");
141 p += sizeof(uint32_t);
142 p[0] = (char)TESTOP_SEND_TEXT;
146 if (ibw_send(conn, buf, key, len)) {
147 DEBUG(0, ("send_test_msg: ibw_send error\n"));
155 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
158 unsigned char sum = 0;
162 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
169 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
172 unsigned char sum = 0;
181 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
188 len = sizeof(uint32_t) + 1 + size + 1;
189 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
190 DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n"));
193 buf[sizeof(uint32_t)] = TESTOP_SEND_RND;
194 sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);
195 buf[sizeof(uint32_t) + 1 + size] = sum;
196 if (ibw_send(conn, buf, key, len)) {
197 DEBUG(0, ("varsize/ibw_send failed\n"));
205 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
210 for(i=0; i<tcx->nmsg; i++)
212 //size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0)));
213 size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg));
214 if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size))
220 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
223 struct ibw_conn *conn;
225 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
226 if (conn->state==IBWC_CONNECTED) {
227 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
234 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
236 struct ibwtest_ctx *tcx = NULL; /* userdata */
237 struct ibwtest_conn *pconn = NULL; /* userdata */
240 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
244 DEBUG(10, ("test IBWS_INIT\n"));
247 DEBUG(10, ("test IBWS_READY\n"));
249 case IBWS_CONNECT_REQUEST:
250 DEBUG(10, ("test IBWS_CONNECT_REQUEST\n"));
251 pconn = talloc_zero(conn, struct ibwtest_conn);
252 if (ibw_accept(ctx, conn, pconn)) {
253 DEBUG(0, ("error accepting the connect request\n"));
257 DEBUG(10, ("test IBWS_STOPPED\n"));
258 tcx->kill_me = 1; /* main loop can exit */
261 DEBUG(10, ("test IBWS_ERROR\n"));
262 ibw_stop(tcx->ibwctx);
271 pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
272 switch(conn->state) {
274 DEBUG(10, ("test IBWC_INIT\n"));
277 if (gettimeofday(&tcx->start_time, NULL)) {
278 DEBUG(0, ("gettimeofday error %d", errno));
281 ibwtest_send_id(conn);
283 case IBWC_DISCONNECTED:
284 DEBUG(10, ("test IBWC_DISCONNECTED\n"));
288 DEBUG(10, ("test IBWC_ERROR\n"));
298 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
300 struct ibwtest_conn *pconn;
302 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
306 assert(n>=sizeof(uint32_t)+1);
307 pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
309 op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];
310 if (op==TESTOP_SEND_ID) {
311 pconn->id = talloc_strdup(pconn, ((char *)buf)+sizeof(uint32_t)+1);
313 if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
314 DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op,
315 pconn->id ? pconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
318 if (tcx->is_server) {
319 if (op==TESTOP_SEND_RND) {
321 sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,
322 n - sizeof(uint32_t) - 2);
323 DEBUG(11, ("[%d]msg varsize %u/sum %u from %s\n",
325 n - sizeof(uint32_t) - 2,
327 pconn->id ? pconn->id : "NULL"));
328 if (sum!=((unsigned char *)buf)[n-1]) {
329 DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n",
330 (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
331 ibw_stop(tcx->ibwctx);
338 /* bounce message regardless what it is */
339 if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) {
340 fprintf(stderr, "ibw_alloc_send_buf error #2\n");
343 memcpy(buf2, buf, n);
344 if (ibw_send(conn, buf2, key2, n)) {
345 fprintf(stderr, "ibw_send error #2\n");
350 } else { /* client: */
351 if (op==TESTOP_SEND_ID && tcx->maxsize) {
352 /* send them in one blow */
353 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
358 sprintf(msg, "hello world %d", tcx->nmsg--);
359 rc = ibwtest_send_test_msg(tcx, conn, msg);
361 ibw_stop(tcx->ibwctx);
373 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te,
374 struct timeval t, void *private)
376 struct ibwtest_ctx *tcx = talloc_get_type(private, struct ibwtest_ctx);
379 if (!tcx->is_server) {
380 struct ibw_conn *conn;
383 /* fill it with something variable... */
384 sprintf(msg, "hello world %d", tcx->cnt++);
386 /* send something to everybody... */
387 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
388 if (conn->state==IBWC_CONNECTED) {
389 rc = ibwtest_send_test_msg(tcx, conn, msg);
394 } /* else allow main loop run */
397 static struct ibwtest_ctx *testctx = NULL;
399 void ibwtest_sigint_handler(int sig)
401 DEBUG(0, ("got SIGINT\n"));
403 if (testctx->ibwctx->state==IBWS_READY ||
404 testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
405 testctx->ibwctx->state==IBWS_ERROR)
407 if (testctx->stopping) {
408 DEBUG(10, ("forcing exit...\n"));
409 testctx->kill_me = 1;
411 /* mostly expected case */
412 ibw_stop(testctx->ibwctx);
413 testctx->stopping = 1;
416 testctx->kill_me = 1;
420 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
421 struct ibw_initattr **pattrs, int *nattrs, char op)
424 int porcess_next = 1;
426 struct ibw_initattr *attrs = NULL;
429 for(p = optext; *p!='\0'; p++) {
434 attrs = (struct ibw_initattr *)talloc_size(tcx,
435 n * sizeof(struct ibw_initattr));
436 for(p = optext; *p!='\0'; p++) {
441 fprintf(stderr, "-%c format error\n", op);
445 attrs[i].value = q + 1;
449 p = q; /* ++ at end */
452 *p = '\0'; /* ++ at end */
462 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
465 struct ibw_initattr *attrs = NULL;
466 struct sockaddr_in *p;
469 tmp = talloc_strdup(tcx, optarg);
470 /* hack to reuse the above ibw_initattr parser */
471 if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
474 tcx->addrs = talloc_size(tcx,
475 tcx->naddrs * sizeof(struct sockaddr_in));
476 for(i=0; i<tcx->naddrs; i++) {
478 p->sin_family = AF_INET;
479 p->sin_addr.s_addr = inet_addr(attrs[i].name);
480 p->sin_port = atoi(attrs[i].value);
486 int ibwtest_init_server(struct ibwtest_ctx *tcx)
488 if (tcx->naddrs!=1) {
489 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
493 if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
494 DEBUG(0, ("ERROR: ibw_bind failed\n"));
498 if (ibw_listen(tcx->ibwctx, 1)) {
499 DEBUG(0, ("ERROR: ibw_listen failed\n"));
503 /* continued at IBWS_READY */
507 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
510 printf("\t%s -i <id> -o {name:value} -d {addr:port} -t nsec -s\n", name);
511 printf("\t-i <id> is a free text, acting as a server id, max 23 chars [mandatory]\n");
512 printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n");
513 printf("\t-d addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
514 printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec);
515 printf("\t\t send message periodically and endless when nsec is non-zero\n");
516 printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n");
517 printf("\t-n number of messages to send [default %d]\n", tcx->nmsg);
518 printf("\t-l usec time to sleep in the main loop [default %d]\n", tcx->sleep_usec);
519 printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize);
520 printf("Press ctrl+C to stop the program.\n");
523 int main(int argc, char *argv[])
527 struct event_context *ev = NULL;
528 struct ibwtest_ctx *tcx = NULL;
531 tcx = talloc_zero(NULL, struct ibwtest_ctx);
532 memset(tcx, 0, sizeof(struct ibwtest_ctx));
536 /* here is the only case we can't avoid using global... */
538 signal(SIGINT, ibwtest_sigint_handler);
539 srand((unsigned)time(NULL));
541 while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:")) != -1) {
544 tcx->id = talloc_strdup(tcx, optarg);
547 tcx->opts = talloc_strdup(tcx, optarg);
548 if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
553 if (ibwtest_getdests(tcx, op))
560 tcx->nsec = (unsigned int)atoi(optarg);
563 tcx->nmsg = atoi(optarg);
566 tcx->sleep_usec = (unsigned int)atoi(optarg);
569 tcx->maxsize = (unsigned int)atoi(optarg);
572 fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
573 ibwtest_usage(tcx, argv[0]);
578 ibwtest_usage(tcx, argv[0]);
582 ev = event_context_init(NULL);
585 tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
587 ibwtest_connstate_handler,
588 ibwtest_receive_handler,
595 rc = ibwtest_init_server(tcx);
597 rc = ibwtest_connect_everybody(tcx);
601 while(!tcx->kill_me && !tcx->error) {
603 event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec),
604 ibwtest_timeout_handler, tcx);
610 usleep(tcx->sleep_usec);
613 if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) {
614 if (gettimeofday(&tcx->end_time, NULL)) {
615 DEBUG(0, ("gettimeofday error %d\n", errno));
618 usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 +
619 (tcx->end_time.tv_usec - tcx->start_time.tv_usec);
620 printf("usec: %f, nmsg: %d, usec/nmsg: %f\n",
621 usec, tcx->nsent, usec/(float)tcx->nsent);
625 result = 0; /* everything OK */
632 DEBUG(0, ("exited with code %d\n", result));