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 ibw_conn *conn;
85 struct ibwtest_conn *tconn = talloc_zero(tcx, struct ibwtest_conn);
88 for(i=0; i<tcx->naddrs; i++) {
89 conn = ibw_conn_new(tcx->ibwctx, tconn);
90 if (ibw_connect(conn, &tcx->addrs[i], tconn)) {
91 fprintf(stderr, "ibw_connect error at %d\n", i);
95 DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));
100 int ibwtest_send_id(struct ibw_conn *conn)
102 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
107 DEBUG(10, ("ibwtest_send_id\n"));
108 len = sizeof(uint32_t)+strlen(tcx->id)+2;
109 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
110 DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n"));
114 /* first sizeof(uint32_t) size bytes are for length */
115 *((uint32_t *)buf) = len;
116 buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
117 strcpy(buf+sizeof(uint32_t)+1, tcx->id);
119 if (ibw_send(conn, buf, key, len)) {
120 DEBUG(0, ("send_id: ibw_send error\n"));
128 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
134 if (conn->state!=IBWC_CONNECTED)
135 return 0; /* not yet up */
137 len = strlen(msg) + 2 + sizeof(uint32_t);
138 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
139 fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");
143 *((uint32_t *)buf) = len;
145 p += sizeof(uint32_t);
146 p[0] = (char)TESTOP_SEND_TEXT;
150 if (ibw_send(conn, buf, key, len)) {
151 DEBUG(0, ("send_test_msg: ibw_send error\n"));
159 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
162 unsigned char sum = 0;
166 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
173 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
176 unsigned char sum = 0;
185 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
192 len = sizeof(uint32_t) + 1 + size + 1;
193 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
194 DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n"));
197 *((uint32_t *)buf) = len;
198 buf[sizeof(uint32_t)] = TESTOP_SEND_RND;
199 sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);
200 buf[sizeof(uint32_t) + 1 + size] = sum;
201 if (ibw_send(conn, buf, key, len)) {
202 DEBUG(0, ("varsize/ibw_send failed\n"));
210 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
215 for(i=0; i<tcx->nmsg; i++)
217 //size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0)));
218 size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg));
219 if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size))
225 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
228 struct ibw_conn *conn;
230 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
231 if (conn->state==IBWC_CONNECTED) {
232 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
239 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
241 struct ibwtest_ctx *tcx = NULL; /* userdata */
242 struct ibwtest_conn *tconn = NULL; /* userdata */
245 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
249 DEBUG(10, ("test IBWS_INIT\n"));
252 DEBUG(10, ("test IBWS_READY\n"));
254 case IBWS_CONNECT_REQUEST:
255 DEBUG(10, ("test IBWS_CONNECT_REQUEST\n"));
256 tconn = talloc_zero(conn, struct ibwtest_conn);
257 if (ibw_accept(ctx, conn, tconn)) {
258 DEBUG(0, ("error accepting the connect request\n"));
262 DEBUG(10, ("test IBWS_STOPPED\n"));
263 tcx->kill_me = 1; /* main loop can exit */
266 DEBUG(10, ("test IBWS_ERROR\n"));
267 ibw_stop(tcx->ibwctx);
276 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
277 switch(conn->state) {
279 DEBUG(10, ("test IBWC_INIT\n"));
282 if (gettimeofday(&tcx->start_time, NULL)) {
283 DEBUG(0, ("gettimeofday error %d", errno));
286 ibwtest_send_id(conn);
288 case IBWC_DISCONNECTED:
289 DEBUG(10, ("test IBWC_DISCONNECTED\n"));
293 DEBUG(10, ("test IBWC_ERROR %s\n", ibw_getLastError()));
303 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
305 struct ibwtest_conn *tconn;
307 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
311 assert(n>=sizeof(uint32_t)+1);
312 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
314 op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];
315 if (op==TESTOP_SEND_ID) {
316 tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);
318 if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
319 DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op,
320 tconn->id ? tconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
323 if (tcx->is_server) {
324 if (op==TESTOP_SEND_RND) {
326 sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,
327 n - sizeof(uint32_t) - 2);
328 DEBUG(11, ("[%d]msg varsize %u/sum %u from %s\n",
330 n - sizeof(uint32_t) - 2,
332 tconn->id ? tconn->id : "NULL"));
333 if (sum!=((unsigned char *)buf)[n-1]) {
334 DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n",
335 (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
336 ibw_stop(tcx->ibwctx);
339 } else if (op!=TESTOP_SEND_ID) {
343 /* bounce message regardless what it is */
344 if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) {
345 fprintf(stderr, "ibw_alloc_send_buf error #2\n");
348 memcpy(buf2, buf, n);
349 if (ibw_send(conn, buf2, key2, n)) {
350 fprintf(stderr, "ibw_send error #2\n");
355 } else { /* client: */
356 if (op==TESTOP_SEND_ID && tcx->maxsize) {
357 /* send them in one blow */
358 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
363 sprintf(msg, "hello world %d", tcx->nmsg--);
364 rc = ibwtest_send_test_msg(tcx, conn, msg);
366 ibw_stop(tcx->ibwctx);
380 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te,
381 struct timeval t, void *private_data)
383 struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
386 if (!tcx->is_server) {
387 struct ibw_conn *conn;
390 /* fill it with something variable... */
391 sprintf(msg, "hello world %d", tcx->cnt++);
393 /* send something to everybody... */
394 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
395 if (conn->state==IBWC_CONNECTED) {
396 rc = ibwtest_send_test_msg(tcx, conn, msg);
401 } /* else allow main loop run */
404 static struct ibwtest_ctx *testctx = NULL;
406 void ibwtest_sigint_handler(int sig)
408 DEBUG(0, ("got SIGINT\n"));
410 if (testctx->ibwctx->state==IBWS_READY ||
411 testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
412 testctx->ibwctx->state==IBWS_ERROR)
414 if (testctx->stopping) {
415 DEBUG(10, ("forcing exit...\n"));
416 testctx->kill_me = 1;
418 /* mostly expected case */
419 ibw_stop(testctx->ibwctx);
420 testctx->stopping = 1;
423 testctx->kill_me = 1;
427 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
428 struct ibw_initattr **pattrs, int *nattrs, char op)
431 int porcess_next = 1;
433 struct ibw_initattr *attrs = NULL;
436 for(p = optext; *p!='\0'; p++) {
441 attrs = (struct ibw_initattr *)talloc_size(tcx,
442 n * sizeof(struct ibw_initattr));
443 for(p = optext; *p!='\0'; p++) {
448 fprintf(stderr, "-%c format error\n", op);
452 attrs[i].value = q + 1;
456 p = q; /* ++ at end */
459 *p = '\0'; /* ++ at end */
469 static int ibwtest_get_address(const char *address, struct in_addr *addr)
471 if (inet_pton(AF_INET, address, addr) <= 0) {
472 struct hostent *he = gethostbyname(address);
473 if (he == NULL || he->h_length > sizeof(*addr)) {
474 DEBUG(0, ("invalid nework address '%s'\n", address));
477 memcpy(addr, he->h_addr, he->h_length);
482 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
485 struct ibw_initattr *attrs = NULL;
486 struct sockaddr_in *p;
489 tmp = talloc_strdup(tcx, optarg);
490 /* hack to reuse the above ibw_initattr parser */
491 if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
494 tcx->addrs = talloc_size(tcx,
495 tcx->naddrs * sizeof(struct sockaddr_in));
496 for(i=0; i<tcx->naddrs; i++) {
498 p->sin_family = AF_INET;
499 if (ibwtest_get_address(attrs[i].name, &p->sin_addr))
501 p->sin_port = htons(atoi(attrs[i].value));
507 int ibwtest_init_server(struct ibwtest_ctx *tcx)
509 if (tcx->naddrs!=1) {
510 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
514 if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
515 DEBUG(0, ("ERROR: ibw_bind failed\n"));
519 if (ibw_listen(tcx->ibwctx, 1)) {
520 DEBUG(0, ("ERROR: ibw_listen failed\n"));
524 /* continued at IBWS_READY */
528 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
531 printf("\t%s -i <id> -o {name:value} -d {addr:port} -t nsec -s\n", name);
532 printf("\t-i <id> is a free text, acting as a server id, max 23 chars [mandatory]\n");
533 printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n");
534 printf("\t-a addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
535 printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec);
536 printf("\t\t send message periodically and endless when nsec is non-zero\n");
537 printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n");
538 printf("\t-n number of messages to send [default %d]\n", tcx->nmsg);
539 printf("\t-l usec time to sleep in the main loop [default %d]\n", tcx->sleep_usec);
540 printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize);
541 printf("\t-d LogLevel [default %d]\n", LogLevel);
542 printf("Press ctrl+C to stop the program.\n");
545 int main(int argc, char *argv[])
549 struct event_context *ev = NULL;
550 struct ibwtest_ctx *tcx = NULL;
553 tcx = talloc_zero(NULL, struct ibwtest_ctx);
554 memset(tcx, 0, sizeof(struct ibwtest_ctx));
559 /* here is the only case we can't avoid using global... */
561 signal(SIGINT, ibwtest_sigint_handler);
562 srand((unsigned)time(NULL));
564 while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:a:")) != -1) {
567 tcx->id = talloc_strdup(tcx, optarg);
570 tcx->opts = talloc_strdup(tcx, optarg);
571 if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
576 if (ibwtest_getdests(tcx, op))
583 tcx->nsec = (unsigned int)atoi(optarg);
586 tcx->nmsg = atoi(optarg);
589 tcx->sleep_usec = (unsigned int)atoi(optarg);
592 tcx->maxsize = (unsigned int)atoi(optarg);
595 LogLevel = atoi(optarg);
598 fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
599 ibwtest_usage(tcx, argv[0]);
604 ibwtest_usage(tcx, argv[0]);
608 ev = event_context_init(NULL);
611 tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
613 ibwtest_connstate_handler,
614 ibwtest_receive_handler,
621 rc = ibwtest_init_server(tcx);
623 rc = ibwtest_connect_everybody(tcx);
627 while(!tcx->kill_me && !tcx->error) {
629 event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec),
630 ibwtest_timeout_handler, tcx);
636 usleep(tcx->sleep_usec);
639 if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) {
640 if (gettimeofday(&tcx->end_time, NULL)) {
641 DEBUG(0, ("gettimeofday error %d\n", errno));
644 usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 +
645 (tcx->end_time.tv_usec - tcx->start_time.tv_usec);
646 printf("usec: %f, nmsg: %d, usec/nmsg: %f\n",
647 usec, tcx->nsent, usec/(float)tcx->nsent);
651 result = 0; /* everything OK */
658 DEBUG(0, ("exited with code %d\n", result));