use /etc/services for ctdb
[vlendec/samba-autobuild/.git] / ctdb / ib / ibwrapper_test.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Test the infiniband wrapper.
4  *
5  * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
6  *
7  * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #include <netdb.h>
32 #include <arpa/inet.h>
33 #include <malloc.h>
34 #include <assert.h>
35 #include <unistd.h>
36 #include <signal.h>
37 #include <sys/time.h>
38 #include <time.h>
39
40 #include "includes.h"
41 #include "lib/events/events.h"
42 #include "ib/ibwrapper.h"
43
44 struct ibwtest_ctx {
45         int     is_server;
46         char    *id; /* my id */
47
48         struct ibw_initattr *attrs;
49         int     nattrs;
50         char    *opts; /* option string */
51
52         struct sockaddr_in *addrs; /* dynamic array of dest addrs */
53         int     naddrs;
54
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 */
58
59         int     cnt;
60         int     nsent;
61
62         int     nmsg; /* number of messages to send (client) */
63
64         int     kill_me;
65         int     stopping;
66         int     error;
67         struct ibw_ctx  *ibwctx;
68
69         struct timeval  start_time, end_time;
70 };
71
72 struct ibwtest_conn {
73         char    *id;
74 };
75
76 enum testopcode {
77         TESTOP_SEND_ID = 1,
78         TESTOP_SEND_TEXT = 2,
79         TESTOP_SEND_RND = 3
80 };
81
82 int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
83 {
84         struct ibw_conn         *conn;
85         struct ibwtest_conn     *tconn = talloc_zero(tcx, struct ibwtest_conn);
86         int     i;
87
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);
92                         return -1;
93                 }
94         }
95         DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));
96
97         return 0;
98 }
99
100 int ibwtest_send_id(struct ibw_conn *conn)
101 {
102         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
103         char *buf;
104         void *key;
105         uint32_t        len;
106
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"));
111                 return -1;
112         }
113
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);
118
119         if (ibw_send(conn, buf, key, len)) {
120                 DEBUG(0, ("send_id: ibw_send error\n"));
121                 return -1;
122         }
123         tcx->nsent++;
124
125         return 0;
126 }
127
128 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
129 {
130         char *buf, *p;
131         void *key;
132         uint32_t len;
133
134         if (conn->state!=IBWC_CONNECTED)
135                 return 0; /* not yet up */
136
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");
140                 return -1;
141         }
142
143         *((uint32_t *)buf) = len;
144         p = buf;
145         p += sizeof(uint32_t);
146         p[0] = (char)TESTOP_SEND_TEXT;
147         p++;
148         strcpy(p, msg);
149
150         if (ibw_send(conn, buf, key, len)) {
151                 DEBUG(0, ("send_test_msg: ibw_send error\n"));
152                 return -1;
153         }
154         tcx->nsent++;
155
156         return 0;
157 }
158
159 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
160 {
161         uint32_t        i = size;
162         unsigned char   sum = 0;
163         unsigned char   value;
164         while(i) {
165                 i--;
166                 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
167                 buf[i] = value;
168                 sum += value;
169         }
170         return sum;
171 }
172
173 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
174 {
175         uint32_t        i = size;
176         unsigned char   sum = 0;
177
178         while(i) {
179                 i--;
180                 sum += buf[i];
181         }
182         return sum;
183 }
184
185 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
186 {
187         unsigned char *buf;
188         void    *key;
189         uint32_t        len;
190         unsigned char   sum;
191
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"));
195                 return -1;
196         }
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"));
203                 return -1;
204         }
205         tcx->nsent++;
206
207         return 0;
208 }
209
210 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
211 {
212         uint32_t        size;
213         int     i;
214
215         for(i=0; i<tcx->nmsg; i++)
216         {
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))
220                         return -1;
221         }
222         return 0;
223 }
224
225 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
226 {
227         int     rc;
228         struct ibw_conn *conn;
229
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);
233                         if (rc)
234                                 tcx->error = rc;
235                 }
236         }
237 }*/
238
239 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
240 {
241         struct ibwtest_ctx      *tcx = NULL; /* userdata */
242         struct ibwtest_conn     *tconn = NULL; /* userdata */
243
244         if (ctx) {
245                 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
246
247                 switch(ctx->state) {
248                 case IBWS_INIT:
249                         DEBUG(10, ("test IBWS_INIT\n"));
250                         break;
251                 case IBWS_READY:
252                         DEBUG(10, ("test IBWS_READY\n"));
253                         break;
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"));
259                         }
260                         break;
261                 case IBWS_STOPPED:
262                         DEBUG(10, ("test IBWS_STOPPED\n"));
263                         tcx->kill_me = 1; /* main loop can exit */
264                         break;
265                 case IBWS_ERROR:
266                         DEBUG(10, ("test IBWS_ERROR\n"));
267                         ibw_stop(tcx->ibwctx);
268                         break;
269                 default:
270                         assert(0);
271                         break;
272                 }
273         }
274
275         if (conn) {
276                 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
277                 switch(conn->state) {
278                 case IBWC_INIT:
279                         DEBUG(10, ("test IBWC_INIT\n"));
280                         break;
281                 case IBWC_CONNECTED:
282                         if (gettimeofday(&tcx->start_time, NULL)) {
283                                 DEBUG(0, ("gettimeofday error %d", errno));
284                                 return -1;
285                         }
286                         ibwtest_send_id(conn);
287                         break;
288                 case IBWC_DISCONNECTED:
289                         DEBUG(10, ("test IBWC_DISCONNECTED\n"));
290                         talloc_free(conn);
291                         break;
292                 case IBWC_ERROR:
293                         DEBUG(10, ("test IBWC_ERROR %s\n", ibw_getLastError()));
294                         break;
295                 default:
296                         assert(0);
297                         break;
298                 }
299         }
300         return 0;
301 }
302
303 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
304 {
305         struct ibwtest_conn *tconn;
306         enum testopcode op;
307         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
308         int     rc = 0;
309
310         assert(conn!=NULL);
311         assert(n>=sizeof(uint32_t)+1);
312         tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
313
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);
317         }
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));
321         }
322
323         if (tcx->is_server) {
324                 if (op==TESTOP_SEND_RND) {
325                         unsigned char sum;
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",
329                                 op,
330                                 n - sizeof(uint32_t) - 2,
331                                 (uint32_t)sum,
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);
337                                 goto error;
338                         }
339                 } else if (op!=TESTOP_SEND_ID) {
340                         char *buf2;
341                         void *key2;
342
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");
346                                 goto error;
347                         }
348                         memcpy(buf2, buf, n);
349                         if (ibw_send(conn, buf2, key2, n)) {
350                                 fprintf(stderr, "ibw_send error #2\n");
351                                 goto error;
352                         }
353                         tcx->nsent++;
354                 }
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);
359                 }
360
361                 if (tcx->nmsg) {
362                         char    msg[26];
363                         sprintf(msg, "hello world %d", tcx->nmsg--);
364                         rc = ibwtest_send_test_msg(tcx, conn, msg);
365                         if (tcx->nmsg==0) {
366                                 ibw_stop(tcx->ibwctx);
367                                 tcx->stopping = 1;
368                         }
369                 }
370         }
371
372         if (rc)
373                 tcx->error = rc;
374
375         return rc;
376 error:
377         return -1;
378 }
379
380 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te, 
381         struct timeval t, void *private_data)
382 {
383         struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
384         int     rc;
385
386         if (!tcx->is_server) {
387                 struct ibw_conn *conn;
388                 char    msg[50];
389
390                 /* fill it with something variable... */
391                 sprintf(msg, "hello world %d", tcx->cnt++);
392
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);
397                                 if (rc)
398                                         tcx->error = rc;
399                         }
400                 }
401         } /* else allow main loop run */
402 }
403
404 static struct ibwtest_ctx *testctx = NULL;
405
406 void ibwtest_sigint_handler(int sig)
407 {
408         DEBUG(0, ("got SIGINT\n"));
409         if (testctx) {
410                 if (testctx->ibwctx->state==IBWS_READY ||
411                         testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
412                         testctx->ibwctx->state==IBWS_ERROR)
413                 {
414                         if (testctx->stopping) {
415                                 DEBUG(10, ("forcing exit...\n"));
416                                 testctx->kill_me = 1;
417                         } else {
418                                 /* mostly expected case */
419                                 ibw_stop(testctx->ibwctx);
420                                 testctx->stopping = 1;
421                         }
422                 } else
423                         testctx->kill_me = 1;
424         }
425 }
426
427 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
428         struct ibw_initattr **pattrs, int *nattrs, char op)
429 {
430         int     i = 0, n = 1;
431         int     porcess_next = 1;
432         char    *p, *q;
433         struct ibw_initattr *attrs = NULL;
434
435         *pattrs = NULL;
436         for(p = optext; *p!='\0'; p++) {
437                 if (*p==',')
438                         n++;
439         }
440
441         attrs = (struct ibw_initattr *)talloc_size(tcx,
442                 n * sizeof(struct ibw_initattr));
443         for(p = optext; *p!='\0'; p++) {
444                 if (porcess_next) {
445                         attrs[i].name = p;
446                         q = strchr(p, ':');
447                         if (q==NULL) {
448                                 fprintf(stderr, "-%c format error\n", op);
449                                 return -1;
450                         }
451                         *q = '\0';
452                         attrs[i].value = q + 1;
453
454                         porcess_next = 0;
455                         i++;
456                         p = q; /* ++ at end */
457                 }
458                 if (*p==',') {
459                         *p = '\0'; /* ++ at end */
460                         porcess_next = 1;
461                 }
462         }
463         *pattrs = attrs;
464         *nattrs = n;
465
466         return 0;
467 }
468
469 static int ibwtest_get_address(const char *address, struct in_addr *addr)
470 {
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));
475                         return -1;
476                 }
477                 memcpy(addr, he->h_addr, he->h_length);
478         }
479         return 0;
480 }
481
482 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
483 {
484         int     i;
485         struct ibw_initattr     *attrs = NULL;
486         struct sockaddr_in      *p;
487         char    *tmp;
488
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))
492                 return -1;
493
494         tcx->addrs = talloc_size(tcx,
495                 tcx->naddrs * sizeof(struct sockaddr_in));
496         for(i=0; i<tcx->naddrs; i++) {
497                 p = tcx->addrs + i;
498                 p->sin_family = AF_INET;
499                 if (ibwtest_get_address(attrs[i].name, &p->sin_addr))
500                         return -1;
501                 p->sin_port = htons(atoi(attrs[i].value));
502         }
503
504         return 0;
505 }
506
507 int ibwtest_init_server(struct ibwtest_ctx *tcx)
508 {
509         if (tcx->naddrs!=1) {
510                 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
511                 return -1;
512         }
513
514         if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
515                 DEBUG(0, ("ERROR: ibw_bind failed\n"));
516                 return -1;
517         }
518         
519         if (ibw_listen(tcx->ibwctx, 1)) {
520                 DEBUG(0, ("ERROR: ibw_listen failed\n"));
521                 return -1;
522         }
523
524         /* continued at IBWS_READY */
525         return 0;
526 }
527
528 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
529 {
530         printf("Usage:\n");
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");
543 }
544
545 int main(int argc, char *argv[])
546 {
547         int     rc, op;
548         int     result = 1;
549         struct event_context *ev = NULL;
550         struct ibwtest_ctx *tcx = NULL;
551         float   usec;
552
553         tcx = talloc_zero(NULL, struct ibwtest_ctx);
554         memset(tcx, 0, sizeof(struct ibwtest_ctx));
555         tcx->nsec = 0;
556         tcx->nmsg = 1000;
557         LogLevel = 0;
558
559         /* here is the only case we can't avoid using global... */
560         testctx = tcx;
561         signal(SIGINT, ibwtest_sigint_handler);
562         srand((unsigned)time(NULL));
563
564         while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:a:")) != -1) {
565                 switch (op) {
566                 case 'i':
567                         tcx->id = talloc_strdup(tcx, optarg);
568                         break;
569                 case 'o':
570                         tcx->opts = talloc_strdup(tcx, optarg);
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;
601                 }
602         }
603         if (tcx->id==NULL) {
604                 ibwtest_usage(tcx, argv[0]);
605                 goto cleanup;
606         }
607
608         ev = event_context_init(NULL);
609         assert(ev);
610
611         tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
612                 tcx,
613                 ibwtest_connstate_handler,
614                 ibwtest_receive_handler,
615                 ev
616         );
617         if (!tcx->ibwctx)
618                 goto cleanup;
619
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;
626
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);
631                 }
632
633                 event_loop_once(ev);
634
635                 if (tcx->sleep_usec)
636                         usleep(tcx->sleep_usec);
637         }
638
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));
642                         goto cleanup;
643                 }
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);
648         }
649
650         if (!tcx->error)
651                 result = 0; /* everything OK */
652
653 cleanup:
654         if (tcx)
655                 talloc_free(tcx);
656         if (ev)
657                 talloc_free(ev);
658         DEBUG(0, ("exited with code %d\n", result));
659         return result;
660 }