pidl/wscript: remove --with-perl-* options
[Samba.git] / ctdb / ib / ibwrapper_test.c
blob48162c328a657af3575608bfb6d84df40d494841
1 /*
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 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31 #include <arpa/inet.h>
32 #include <malloc.h>
33 #include <assert.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <sys/time.h>
37 #include <time.h>
39 #include "includes.h"
40 #include "ib/ibwrapper.h"
42 struct ibwtest_ctx {
43 int is_server;
44 char *id; /* my id */
46 struct ibw_initattr *attrs;
47 int nattrs;
48 char *opts; /* option string */
50 struct sockaddr_in *addrs; /* dynamic array of dest addrs */
51 int naddrs;
53 unsigned int nsec; /* delta times between messages in nanosec */
54 unsigned int sleep_usec; /* microsecs to sleep in the main loop to emulate overloading */
55 uint32_t maxsize; /* maximum variable message size */
57 int cnt;
58 int nsent;
60 int nmsg; /* number of messages to send (client) */
62 int kill_me;
63 int stopping;
64 int error;
65 struct ibw_ctx *ibwctx;
67 struct timeval start_time, end_time;
70 struct ibwtest_conn {
71 char *id;
74 enum testopcode {
75 TESTOP_SEND_ID = 1,
76 TESTOP_SEND_TEXT = 2,
77 TESTOP_SEND_RND = 3
80 int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
82 struct ibw_conn *conn;
83 struct ibwtest_conn *tconn = talloc_zero(tcx, struct ibwtest_conn);
84 int i;
86 for(i=0; i<tcx->naddrs; i++) {
87 conn = ibw_conn_new(tcx->ibwctx, tconn);
88 if (ibw_connect(conn, &tcx->addrs[i], tconn)) {
89 fprintf(stderr, "ibw_connect error at %d\n", i);
90 return -1;
93 DEBUG(DEBUG_DEBUG, ("sent %d connect request...\n", tcx->naddrs));
95 return 0;
98 int ibwtest_send_id(struct ibw_conn *conn)
100 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
101 char *buf;
102 void *key;
103 uint32_t len;
105 DEBUG(DEBUG_DEBUG, ("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(DEBUG_ERR, ("send_id: ibw_alloc_send_buf failed\n"));
109 return -1;
112 /* first sizeof(uint32_t) size bytes are for length */
113 *((uint32_t *)buf) = len;
114 buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
115 strcpy(buf+sizeof(uint32_t)+1, tcx->id);
117 if (ibw_send(conn, buf, key, len)) {
118 DEBUG(DEBUG_ERR, ("send_id: ibw_send error\n"));
119 return -1;
121 tcx->nsent++;
123 return 0;
126 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
128 char *buf, *p;
129 void *key;
130 uint32_t len;
132 if (conn->state!=IBWC_CONNECTED)
133 return 0; /* not yet up */
135 len = strlen(msg) + 2 + sizeof(uint32_t);
136 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
137 fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");
138 return -1;
141 *((uint32_t *)buf) = len;
142 p = buf;
143 p += sizeof(uint32_t);
144 p[0] = (char)TESTOP_SEND_TEXT;
145 p++;
146 strcpy(p, msg);
148 if (ibw_send(conn, buf, key, len)) {
149 DEBUG(DEBUG_ERR, ("send_test_msg: ibw_send error\n"));
150 return -1;
152 tcx->nsent++;
154 return 0;
157 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
159 uint32_t i = size;
160 unsigned char sum = 0;
161 unsigned char value;
162 while(i) {
163 i--;
164 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
165 buf[i] = value;
166 sum += value;
168 return sum;
171 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
173 uint32_t i = size;
174 unsigned char sum = 0;
176 while(i) {
177 i--;
178 sum += buf[i];
180 return sum;
183 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
185 unsigned char *buf;
186 void *key;
187 uint32_t len;
188 unsigned char sum;
190 len = sizeof(uint32_t) + 1 + size + 1;
191 if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
192 DEBUG(DEBUG_ERR, ("varsize/ibw_alloc_send_buf failed\n"));
193 return -1;
195 *((uint32_t *)buf) = len;
196 buf[sizeof(uint32_t)] = TESTOP_SEND_RND;
197 sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);
198 buf[sizeof(uint32_t) + 1 + size] = sum;
199 if (ibw_send(conn, buf, key, len)) {
200 DEBUG(DEBUG_ERR, ("varsize/ibw_send failed\n"));
201 return -1;
203 tcx->nsent++;
205 return 0;
208 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
210 uint32_t size;
211 int i;
213 for(i=0; i<tcx->nmsg; i++)
215 //size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0)));
216 size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg));
217 if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size))
218 return -1;
220 return 0;
223 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
225 int rc;
226 struct ibw_conn *conn;
228 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
229 if (conn->state==IBWC_CONNECTED) {
230 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
231 if (rc)
232 tcx->error = rc;
237 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
239 struct ibwtest_ctx *tcx = NULL; /* userdata */
240 struct ibwtest_conn *tconn = NULL; /* userdata */
242 if (ctx) {
243 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
245 switch(ctx->state) {
246 case IBWS_INIT:
247 DEBUG(DEBUG_DEBUG, ("test IBWS_INIT\n"));
248 break;
249 case IBWS_READY:
250 DEBUG(DEBUG_DEBUG, ("test IBWS_READY\n"));
251 break;
252 case IBWS_CONNECT_REQUEST:
253 DEBUG(DEBUG_DEBUG, ("test IBWS_CONNECT_REQUEST\n"));
254 tconn = talloc_zero(conn, struct ibwtest_conn);
255 if (ibw_accept(ctx, conn, tconn)) {
256 DEBUG(DEBUG_ERR, ("error accepting the connect request\n"));
258 break;
259 case IBWS_STOPPED:
260 DEBUG(DEBUG_DEBUG, ("test IBWS_STOPPED\n"));
261 tcx->kill_me = 1; /* main loop can exit */
262 break;
263 case IBWS_ERROR:
264 DEBUG(DEBUG_DEBUG, ("test IBWS_ERROR\n"));
265 ibw_stop(tcx->ibwctx);
266 break;
267 default:
268 assert(0);
269 break;
273 if (conn) {
274 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
275 switch(conn->state) {
276 case IBWC_INIT:
277 DEBUG(DEBUG_DEBUG, ("test IBWC_INIT\n"));
278 break;
279 case IBWC_CONNECTED:
280 if (gettimeofday(&tcx->start_time, NULL)) {
281 DEBUG(DEBUG_ERR, ("gettimeofday error %d", errno));
282 return -1;
284 ibwtest_send_id(conn);
285 break;
286 case IBWC_DISCONNECTED:
287 DEBUG(DEBUG_DEBUG, ("test IBWC_DISCONNECTED\n"));
288 talloc_free(conn);
289 break;
290 case IBWC_ERROR:
291 DEBUG(DEBUG_DEBUG, ("test IBWC_ERROR %s\n", ibw_getLastError()));
292 break;
293 default:
294 assert(0);
295 break;
298 return 0;
301 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
303 struct ibwtest_conn *tconn;
304 enum testopcode op;
305 struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
306 int rc = 0;
308 assert(conn!=NULL);
309 assert(n>=sizeof(uint32_t)+1);
310 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
312 op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];
313 if (op==TESTOP_SEND_ID) {
314 tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);
316 if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
317 DEBUG(DEBUG_DEBUG, ("[%d]msg from %s: \"%s\"(%d)\n", op,
318 tconn->id ? tconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
321 if (tcx->is_server) {
322 if (op==TESTOP_SEND_RND) {
323 unsigned char sum;
324 sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,
325 n - sizeof(uint32_t) - 2);
326 DEBUG(DEBUG_DEBUG, ("[%d]msg varsize %u/sum %u from %s\n",
328 (uint32_t)(n - sizeof(uint32_t) - 2),
329 (uint32_t)sum,
330 tconn->id ? tconn->id : "NULL"));
331 if (sum!=((unsigned char *)buf)[n-1]) {
332 DEBUG(DEBUG_ERR, ("ERROR: checksum mismatch %u!=%u\n",
333 (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
334 ibw_stop(tcx->ibwctx);
335 goto error;
337 } else if (op!=TESTOP_SEND_ID) {
338 char *buf2;
339 void *key2;
341 /* bounce message regardless what it is */
342 if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) {
343 fprintf(stderr, "ibw_alloc_send_buf error #2\n");
344 goto error;
346 memcpy(buf2, buf, n);
347 if (ibw_send(conn, buf2, key2, n)) {
348 fprintf(stderr, "ibw_send error #2\n");
349 goto error;
351 tcx->nsent++;
353 } else { /* client: */
354 if (op==TESTOP_SEND_ID && tcx->maxsize) {
355 /* send them in one blow */
356 rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
359 if (tcx->nmsg) {
360 char msg[26];
361 sprintf(msg, "hello world %d", tcx->nmsg--);
362 rc = ibwtest_send_test_msg(tcx, conn, msg);
363 if (tcx->nmsg==0) {
364 ibw_stop(tcx->ibwctx);
365 tcx->stopping = 1;
370 if (rc)
371 tcx->error = rc;
373 return rc;
374 error:
375 return -1;
378 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te,
379 struct timeval t, void *private_data)
381 struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
382 int rc;
384 if (!tcx->is_server) {
385 struct ibw_conn *conn;
386 char msg[50];
388 /* fill it with something variable... */
389 sprintf(msg, "hello world %d", tcx->cnt++);
391 /* send something to everybody... */
392 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
393 if (conn->state==IBWC_CONNECTED) {
394 rc = ibwtest_send_test_msg(tcx, conn, msg);
395 if (rc)
396 tcx->error = rc;
399 } /* else allow main loop run */
402 static struct ibwtest_ctx *testctx = NULL;
404 void ibwtest_sigint_handler(int sig)
406 DEBUG(DEBUG_ERR, ("got SIGINT\n"));
407 if (testctx) {
408 if (testctx->ibwctx->state==IBWS_READY ||
409 testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
410 testctx->ibwctx->state==IBWS_ERROR)
412 if (testctx->stopping) {
413 DEBUG(DEBUG_DEBUG, ("forcing exit...\n"));
414 testctx->kill_me = 1;
415 } else {
416 /* mostly expected case */
417 ibw_stop(testctx->ibwctx);
418 testctx->stopping = 1;
420 } else
421 testctx->kill_me = 1;
425 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
426 struct ibw_initattr **pattrs, int *nattrs, char op)
428 int i = 0, n = 1;
429 int porcess_next = 1;
430 char *p, *q;
431 struct ibw_initattr *attrs = NULL;
433 *pattrs = NULL;
434 for(p = optext; *p!='\0'; p++) {
435 if (*p==',')
436 n++;
439 attrs = (struct ibw_initattr *)talloc_size(tcx,
440 n * sizeof(struct ibw_initattr));
441 for(p = optext; *p!='\0'; p++) {
442 if (porcess_next) {
443 attrs[i].name = p;
444 q = strchr(p, ':');
445 if (q==NULL) {
446 fprintf(stderr, "-%c format error\n", op);
447 return -1;
449 *q = '\0';
450 attrs[i].value = q + 1;
452 porcess_next = 0;
453 i++;
454 p = q; /* ++ at end */
456 if (*p==',') {
457 *p = '\0'; /* ++ at end */
458 porcess_next = 1;
461 *pattrs = attrs;
462 *nattrs = n;
464 return 0;
467 static int ibwtest_get_address(const char *address, struct in_addr *addr)
469 if (inet_pton(AF_INET, address, addr) <= 0) {
470 struct hostent *he = gethostbyname(address);
471 if (he == NULL || he->h_length > sizeof(*addr)) {
472 DEBUG(DEBUG_ERR, ("invalid nework address '%s'\n", address));
473 return -1;
475 memcpy(addr, he->h_addr, he->h_length);
477 return 0;
480 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
482 int i;
483 struct ibw_initattr *attrs = NULL;
484 struct sockaddr_in *p;
485 char *tmp;
487 tmp = talloc_strdup(tcx, optarg);
488 if (tmp == NULL) return -1;
489 /* hack to reuse the above ibw_initattr parser */
490 if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
491 return -1;
493 tcx->addrs = talloc_size(tcx,
494 tcx->naddrs * sizeof(struct sockaddr_in));
495 for(i=0; i<tcx->naddrs; i++) {
496 p = tcx->addrs + i;
497 p->sin_family = AF_INET;
498 if (ibwtest_get_address(attrs[i].name, &p->sin_addr))
499 return -1;
500 p->sin_port = htons(atoi(attrs[i].value));
503 return 0;
506 int ibwtest_init_server(struct ibwtest_ctx *tcx)
508 if (tcx->naddrs!=1) {
509 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
510 return -1;
513 if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
514 DEBUG(DEBUG_ERR, ("ERROR: ibw_bind failed\n"));
515 return -1;
518 if (ibw_listen(tcx->ibwctx, 1)) {
519 DEBUG(DEBUG_ERR, ("ERROR: ibw_listen failed\n"));
520 return -1;
523 /* continued at IBWS_READY */
524 return 0;
527 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
529 printf("Usage:\n");
530 printf("\t%s -i <id> -o {name:value} -d {addr:port} -t nsec -s\n", name);
531 printf("\t-i <id> is a free text, acting as a server id, max 23 chars [mandatory]\n");
532 printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n");
533 printf("\t-a addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
534 printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec);
535 printf("\t\t send message periodically and endless when nsec is non-zero\n");
536 printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n");
537 printf("\t-n number of messages to send [default %d]\n", tcx->nmsg);
538 printf("\t-l usec time to sleep in the main loop [default %d]\n", tcx->sleep_usec);
539 printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize);
540 printf("\t-d LogLevel [default %d]\n", LogLevel);
541 printf("Press ctrl+C to stop the program.\n");
544 int main(int argc, char *argv[])
546 int rc, op;
547 int result = 1;
548 struct tevent_context *ev = NULL;
549 struct ibwtest_ctx *tcx = NULL;
550 float usec;
552 tcx = talloc_zero(NULL, struct ibwtest_ctx);
553 memset(tcx, 0, sizeof(struct ibwtest_ctx));
554 tcx->nsec = 0;
555 tcx->nmsg = 1000;
556 LogLevel = 0;
558 /* here is the only case we can't avoid using global... */
559 testctx = tcx;
560 signal(SIGINT, ibwtest_sigint_handler);
561 srand((unsigned)time(NULL));
563 while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:a:")) != -1) {
564 switch (op) {
565 case 'i':
566 tcx->id = talloc_strdup(tcx, optarg);
567 break;
568 case 'o':
569 tcx->opts = talloc_strdup(tcx, optarg);
570 if (tcx->opts) goto cleanup;
571 if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
572 &tcx->nattrs, op))
573 goto cleanup;
574 break;
575 case 'a':
576 if (ibwtest_getdests(tcx, op))
577 goto cleanup;
578 break;
579 case 's':
580 tcx->is_server = 1;
581 break;
582 case 't':
583 tcx->nsec = (unsigned int)atoi(optarg);
584 break;
585 case 'n':
586 tcx->nmsg = atoi(optarg);
587 break;
588 case 'l':
589 tcx->sleep_usec = (unsigned int)atoi(optarg);
590 break;
591 case 'v':
592 tcx->maxsize = (unsigned int)atoi(optarg);
593 break;
594 case 'd':
595 LogLevel = atoi(optarg);
596 break;
597 default:
598 fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
599 ibwtest_usage(tcx, argv[0]);
600 goto cleanup;
603 if (tcx->id==NULL) {
604 ibwtest_usage(tcx, argv[0]);
605 goto cleanup;
608 ev = event_context_init(NULL);
609 assert(ev);
611 tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
612 tcx,
613 ibwtest_connstate_handler,
614 ibwtest_receive_handler,
617 if (!tcx->ibwctx)
618 goto cleanup;
620 if (tcx->is_server)
621 rc = ibwtest_init_server(tcx);
622 else
623 rc = ibwtest_connect_everybody(tcx);
624 if (rc)
625 goto cleanup;
627 while(!tcx->kill_me && !tcx->error) {
628 if (tcx->nsec) {
629 event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec),
630 ibwtest_timeout_handler, tcx);
633 event_loop_once(ev);
635 if (tcx->sleep_usec)
636 usleep(tcx->sleep_usec);
639 if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) {
640 if (gettimeofday(&tcx->end_time, NULL)) {
641 DEBUG(DEBUG_ERR, ("gettimeofday error %d\n", errno));
642 goto cleanup;
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);
650 if (!tcx->error)
651 result = 0; /* everything OK */
653 cleanup:
654 if (tcx)
655 talloc_free(tcx);
656 if (ev)
657 talloc_free(ev);
658 DEBUG(DEBUG_ERR, ("exited with code %d\n", result));
659 return result;