added debug constants to allow for better mapping to syslog levels
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
21  */
22
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>
38
39 #include "includes.h"
40 #include "lib/events/events.h"
41 #include "ib/ibwrapper.h"
42
43 struct ibwtest_ctx {
44         int     is_server;
45         char    *id; /* my id */
46
47         struct ibw_initattr *attrs;
48         int     nattrs;
49         char    *opts; /* option string */
50
51         struct sockaddr_in *addrs; /* dynamic array of dest addrs */
52         int     naddrs;
53
54         unsigned int    nsec; /* delta times between messages in nanosec */
55         unsigned int    sleep_usec; /* microsecs to sleep in the main loop to emulate overloading */
56         uint32_t        maxsize; /* maximum variable message size */
57
58         int     cnt;
59         int     nsent;
60
61         int     nmsg; /* number of messages to send (client) */
62
63         int     kill_me;
64         int     stopping;
65         int     error;
66         struct ibw_ctx  *ibwctx;
67
68         struct timeval  start_time, end_time;
69 };
70
71 struct ibwtest_conn {
72         char    *id;
73 };
74
75 enum testopcode {
76         TESTOP_SEND_ID = 1,
77         TESTOP_SEND_TEXT = 2,
78         TESTOP_SEND_RND = 3
79 };
80
81 int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
82 {
83         struct ibw_conn         *conn;
84         struct ibwtest_conn     *tconn = talloc_zero(tcx, struct ibwtest_conn);
85         int     i;
86
87         for(i=0; i<tcx->naddrs; i++) {
88                 conn = ibw_conn_new(tcx->ibwctx, tconn);
89                 if (ibw_connect(conn, &tcx->addrs[i], tconn)) {
90                         fprintf(stderr, "ibw_connect error at %d\n", i);
91                         return -1;
92                 }
93         }
94         DEBUG(DEBUG_DEBUG, ("sent %d connect request...\n", tcx->naddrs));
95
96         return 0;
97 }
98
99 int ibwtest_send_id(struct ibw_conn *conn)
100 {
101         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
102         char *buf;
103         void *key;
104         uint32_t        len;
105
106         DEBUG(DEBUG_DEBUG, ("ibwtest_send_id\n"));
107         len = sizeof(uint32_t)+strlen(tcx->id)+2;
108         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
109                 DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n"));
110                 return -1;
111         }
112
113         /* first sizeof(uint32_t) size bytes are for length */
114         *((uint32_t *)buf) = len;
115         buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
116         strcpy(buf+sizeof(uint32_t)+1, tcx->id);
117
118         if (ibw_send(conn, buf, key, len)) {
119                 DEBUG(0, ("send_id: ibw_send error\n"));
120                 return -1;
121         }
122         tcx->nsent++;
123
124         return 0;
125 }
126
127 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
128 {
129         char *buf, *p;
130         void *key;
131         uint32_t len;
132
133         if (conn->state!=IBWC_CONNECTED)
134                 return 0; /* not yet up */
135
136         len = strlen(msg) + 2 + sizeof(uint32_t);
137         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
138                 fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");
139                 return -1;
140         }
141
142         *((uint32_t *)buf) = len;
143         p = buf;
144         p += sizeof(uint32_t);
145         p[0] = (char)TESTOP_SEND_TEXT;
146         p++;
147         strcpy(p, msg);
148
149         if (ibw_send(conn, buf, key, len)) {
150                 DEBUG(0, ("send_test_msg: ibw_send error\n"));
151                 return -1;
152         }
153         tcx->nsent++;
154
155         return 0;
156 }
157
158 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
159 {
160         uint32_t        i = size;
161         unsigned char   sum = 0;
162         unsigned char   value;
163         while(i) {
164                 i--;
165                 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
166                 buf[i] = value;
167                 sum += value;
168         }
169         return sum;
170 }
171
172 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
173 {
174         uint32_t        i = size;
175         unsigned char   sum = 0;
176
177         while(i) {
178                 i--;
179                 sum += buf[i];
180         }
181         return sum;
182 }
183
184 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
185 {
186         unsigned char *buf;
187         void    *key;
188         uint32_t        len;
189         unsigned char   sum;
190
191         len = sizeof(uint32_t) + 1 + size + 1;
192         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
193                 DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n"));
194                 return -1;
195         }
196         *((uint32_t *)buf) = len;
197         buf[sizeof(uint32_t)] = TESTOP_SEND_RND;
198         sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);
199         buf[sizeof(uint32_t) + 1 + size] = sum;
200         if (ibw_send(conn, buf, key, len)) {
201                 DEBUG(0, ("varsize/ibw_send failed\n"));
202                 return -1;
203         }
204         tcx->nsent++;
205
206         return 0;
207 }
208
209 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
210 {
211         uint32_t        size;
212         int     i;
213
214         for(i=0; i<tcx->nmsg; i++)
215         {
216                 //size = (uint32_t)((float)(tcx->maxsize) * (rand() / (RAND_MAX + 1.0)));
217                 size = (uint32_t)((float)(tcx->maxsize) * ((float)(i+1)/(float)tcx->nmsg));
218                 if (ibwtest_do_varsize_scenario_conn_size(tcx, conn, size))
219                         return -1;
220         }
221         return 0;
222 }
223
224 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
225 {
226         int     rc;
227         struct ibw_conn *conn;
228
229         for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
230                 if (conn->state==IBWC_CONNECTED) {
231                         rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
232                         if (rc)
233                                 tcx->error = rc;
234                 }
235         }
236 }*/
237
238 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
239 {
240         struct ibwtest_ctx      *tcx = NULL; /* userdata */
241         struct ibwtest_conn     *tconn = NULL; /* userdata */
242
243         if (ctx) {
244                 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
245
246                 switch(ctx->state) {
247                 case IBWS_INIT:
248                         DEBUG(DEBUG_DEBUG, ("test IBWS_INIT\n"));
249                         break;
250                 case IBWS_READY:
251                         DEBUG(DEBUG_DEBUG, ("test IBWS_READY\n"));
252                         break;
253                 case IBWS_CONNECT_REQUEST:
254                         DEBUG(DEBUG_DEBUG, ("test IBWS_CONNECT_REQUEST\n"));
255                         tconn = talloc_zero(conn, struct ibwtest_conn);
256                         if (ibw_accept(ctx, conn, tconn)) {
257                                 DEBUG(0, ("error accepting the connect request\n"));
258                         }
259                         break;
260                 case IBWS_STOPPED:
261                         DEBUG(DEBUG_DEBUG, ("test IBWS_STOPPED\n"));
262                         tcx->kill_me = 1; /* main loop can exit */
263                         break;
264                 case IBWS_ERROR:
265                         DEBUG(DEBUG_DEBUG, ("test IBWS_ERROR\n"));
266                         ibw_stop(tcx->ibwctx);
267                         break;
268                 default:
269                         assert(0);
270                         break;
271                 }
272         }
273
274         if (conn) {
275                 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
276                 switch(conn->state) {
277                 case IBWC_INIT:
278                         DEBUG(DEBUG_DEBUG, ("test IBWC_INIT\n"));
279                         break;
280                 case IBWC_CONNECTED:
281                         if (gettimeofday(&tcx->start_time, NULL)) {
282                                 DEBUG(0, ("gettimeofday error %d", errno));
283                                 return -1;
284                         }
285                         ibwtest_send_id(conn);
286                         break;
287                 case IBWC_DISCONNECTED:
288                         DEBUG(DEBUG_DEBUG, ("test IBWC_DISCONNECTED\n"));
289                         talloc_free(conn);
290                         break;
291                 case IBWC_ERROR:
292                         DEBUG(DEBUG_DEBUG, ("test IBWC_ERROR %s\n", ibw_getLastError()));
293                         break;
294                 default:
295                         assert(0);
296                         break;
297                 }
298         }
299         return 0;
300 }
301
302 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
303 {
304         struct ibwtest_conn *tconn;
305         enum testopcode op;
306         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
307         int     rc = 0;
308
309         assert(conn!=NULL);
310         assert(n>=sizeof(uint32_t)+1);
311         tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
312
313         op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];
314         if (op==TESTOP_SEND_ID) {
315                 tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);
316         }
317         if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
318                 DEBUG(DEBUG_DEBUG, ("[%d]msg from %s: \"%s\"(%d)\n", op,
319                         tconn->id ? tconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
320         }
321
322         if (tcx->is_server) {
323                 if (op==TESTOP_SEND_RND) {
324                         unsigned char sum;
325                         sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,
326                                 n - sizeof(uint32_t) - 2);
327                         DEBUG(DEBUG_DEBUG, ("[%d]msg varsize %u/sum %u from %s\n",
328                                 op,
329                                 n - sizeof(uint32_t) - 2,
330                                 (uint32_t)sum,
331                                 tconn->id ? tconn->id : "NULL"));
332                         if (sum!=((unsigned char *)buf)[n-1]) {
333                                 DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n",
334                                         (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
335                                 ibw_stop(tcx->ibwctx);
336                                 goto error;
337                         }
338                 } else if (op!=TESTOP_SEND_ID) {
339                         char *buf2;
340                         void *key2;
341
342                         /* bounce message regardless what it is */
343                         if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) {
344                                 fprintf(stderr, "ibw_alloc_send_buf error #2\n");
345                                 goto error;
346                         }
347                         memcpy(buf2, buf, n);
348                         if (ibw_send(conn, buf2, key2, n)) {
349                                 fprintf(stderr, "ibw_send error #2\n");
350                                 goto error;
351                         }
352                         tcx->nsent++;
353                 }
354         } else { /* client: */
355                 if (op==TESTOP_SEND_ID && tcx->maxsize) {
356                         /* send them in one blow */
357                         rc = ibwtest_do_varsize_scenario_conn(tcx, conn);
358                 }
359
360                 if (tcx->nmsg) {
361                         char    msg[26];
362                         sprintf(msg, "hello world %d", tcx->nmsg--);
363                         rc = ibwtest_send_test_msg(tcx, conn, msg);
364                         if (tcx->nmsg==0) {
365                                 ibw_stop(tcx->ibwctx);
366                                 tcx->stopping = 1;
367                         }
368                 }
369         }
370
371         if (rc)
372                 tcx->error = rc;
373
374         return rc;
375 error:
376         return -1;
377 }
378
379 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te, 
380         struct timeval t, void *private_data)
381 {
382         struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
383         int     rc;
384
385         if (!tcx->is_server) {
386                 struct ibw_conn *conn;
387                 char    msg[50];
388
389                 /* fill it with something variable... */
390                 sprintf(msg, "hello world %d", tcx->cnt++);
391
392                 /* send something to everybody... */
393                 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
394                         if (conn->state==IBWC_CONNECTED) {
395                                 rc = ibwtest_send_test_msg(tcx, conn, msg);
396                                 if (rc)
397                                         tcx->error = rc;
398                         }
399                 }
400         } /* else allow main loop run */
401 }
402
403 static struct ibwtest_ctx *testctx = NULL;
404
405 void ibwtest_sigint_handler(int sig)
406 {
407         DEBUG(0, ("got SIGINT\n"));
408         if (testctx) {
409                 if (testctx->ibwctx->state==IBWS_READY ||
410                         testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
411                         testctx->ibwctx->state==IBWS_ERROR)
412                 {
413                         if (testctx->stopping) {
414                                 DEBUG(DEBUG_DEBUG, ("forcing exit...\n"));
415                                 testctx->kill_me = 1;
416                         } else {
417                                 /* mostly expected case */
418                                 ibw_stop(testctx->ibwctx);
419                                 testctx->stopping = 1;
420                         }
421                 } else
422                         testctx->kill_me = 1;
423         }
424 }
425
426 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
427         struct ibw_initattr **pattrs, int *nattrs, char op)
428 {
429         int     i = 0, n = 1;
430         int     porcess_next = 1;
431         char    *p, *q;
432         struct ibw_initattr *attrs = NULL;
433
434         *pattrs = NULL;
435         for(p = optext; *p!='\0'; p++) {
436                 if (*p==',')
437                         n++;
438         }
439
440         attrs = (struct ibw_initattr *)talloc_size(tcx,
441                 n * sizeof(struct ibw_initattr));
442         for(p = optext; *p!='\0'; p++) {
443                 if (porcess_next) {
444                         attrs[i].name = p;
445                         q = strchr(p, ':');
446                         if (q==NULL) {
447                                 fprintf(stderr, "-%c format error\n", op);
448                                 return -1;
449                         }
450                         *q = '\0';
451                         attrs[i].value = q + 1;
452
453                         porcess_next = 0;
454                         i++;
455                         p = q; /* ++ at end */
456                 }
457                 if (*p==',') {
458                         *p = '\0'; /* ++ at end */
459                         porcess_next = 1;
460                 }
461         }
462         *pattrs = attrs;
463         *nattrs = n;
464
465         return 0;
466 }
467
468 static int ibwtest_get_address(const char *address, struct in_addr *addr)
469 {
470         if (inet_pton(AF_INET, address, addr) <= 0) {
471                 struct hostent *he = gethostbyname(address);
472                 if (he == NULL || he->h_length > sizeof(*addr)) {
473                         DEBUG(0, ("invalid nework address '%s'\n", address));
474                         return -1;
475                 }
476                 memcpy(addr, he->h_addr, he->h_length);
477         }
478         return 0;
479 }
480
481 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
482 {
483         int     i;
484         struct ibw_initattr     *attrs = NULL;
485         struct sockaddr_in      *p;
486         char    *tmp;
487
488         tmp = talloc_strdup(tcx, optarg);
489         /* hack to reuse the above ibw_initattr parser */
490         if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
491                 return -1;
492
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));
501         }
502
503         return 0;
504 }
505
506 int ibwtest_init_server(struct ibwtest_ctx *tcx)
507 {
508         if (tcx->naddrs!=1) {
509                 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
510                 return -1;
511         }
512
513         if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
514                 DEBUG(0, ("ERROR: ibw_bind failed\n"));
515                 return -1;
516         }
517         
518         if (ibw_listen(tcx->ibwctx, 1)) {
519                 DEBUG(0, ("ERROR: ibw_listen failed\n"));
520                 return -1;
521         }
522
523         /* continued at IBWS_READY */
524         return 0;
525 }
526
527 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
528 {
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");
542 }
543
544 int main(int argc, char *argv[])
545 {
546         int     rc, op;
547         int     result = 1;
548         struct event_context *ev = NULL;
549         struct ibwtest_ctx *tcx = NULL;
550         float   usec;
551
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;
557
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));
562
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 (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
571                                 &tcx->nattrs, op))
572                                 goto cleanup;
573                         break;
574                 case 'a':
575                         if (ibwtest_getdests(tcx, op))
576                                 goto cleanup;
577                         break;
578                 case 's':
579                         tcx->is_server = 1;
580                         break;
581                 case 't':
582                         tcx->nsec = (unsigned int)atoi(optarg);
583                         break;
584                 case 'n':
585                         tcx->nmsg = atoi(optarg);
586                         break;
587                 case 'l':
588                         tcx->sleep_usec = (unsigned int)atoi(optarg);
589                         break;
590                 case 'v':
591                         tcx->maxsize = (unsigned int)atoi(optarg);
592                         break;
593                 case 'd':
594                         LogLevel = atoi(optarg);
595                         break;
596                 default:
597                         fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
598                         ibwtest_usage(tcx, argv[0]);
599                         goto cleanup;
600                 }
601         }
602         if (tcx->id==NULL) {
603                 ibwtest_usage(tcx, argv[0]);
604                 goto cleanup;
605         }
606
607         ev = event_context_init(NULL);
608         assert(ev);
609
610         tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
611                 tcx,
612                 ibwtest_connstate_handler,
613                 ibwtest_receive_handler,
614                 ev
615         );
616         if (!tcx->ibwctx)
617                 goto cleanup;
618
619         if (tcx->is_server)
620                 rc = ibwtest_init_server(tcx);
621         else
622                 rc = ibwtest_connect_everybody(tcx);
623         if (rc)
624                 goto cleanup;
625
626         while(!tcx->kill_me && !tcx->error) {
627                 if (tcx->nsec) {
628                         event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec),
629                                 ibwtest_timeout_handler, tcx);
630                 }
631
632                 event_loop_once(ev);
633
634                 if (tcx->sleep_usec)
635                         usleep(tcx->sleep_usec);
636         }
637
638         if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) {
639                 if (gettimeofday(&tcx->end_time, NULL)) {
640                         DEBUG(0, ("gettimeofday error %d\n", errno));
641                         goto cleanup;
642                 }
643                 usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 +
644                                 (tcx->end_time.tv_usec - tcx->start_time.tv_usec);
645                 printf("usec: %f, nmsg: %d, usec/nmsg: %f\n",
646                         usec, tcx->nsent, usec/(float)tcx->nsent);
647         }
648
649         if (!tcx->error)
650                 result = 0; /* everything OK */
651
652 cleanup:
653         if (tcx)
654                 talloc_free(tcx);
655         if (ev)
656                 talloc_free(ev);
657         DEBUG(0, ("exited with code %d\n", result));
658         return result;
659 }