Some minor changes before integrating ib...
[sfrench/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 ibwtest_conn     *pconn = talloc_zero(tcx, struct ibwtest_conn);
85         int     i;
86
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);
90                         return -1;
91                 }
92         }
93         DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));
94
95         return 0;
96 }
97
98 int ibwtest_send_id(struct ibw_conn *conn)
99 {
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;
104
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"));
109                 return -1;
110         }
111
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);
115
116         if (ibw_send(conn, buf, key, len)) {
117                 DEBUG(0, ("send_id: ibw_send error\n"));
118                 return -1;
119         }
120         tcx->nsent++;
121
122         return 0;
123 }
124
125 int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
126 {
127         char *buf, *p;
128         void *key;
129         uint32_t len;
130
131         if (conn->state!=IBWC_CONNECTED)
132                 return 0; /* not yet up */
133
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");
137                 return -1;
138         }
139
140         p = buf;
141         p += sizeof(uint32_t);
142         p[0] = (char)TESTOP_SEND_TEXT;
143         p++;
144         strcpy(p, msg);
145
146         if (ibw_send(conn, buf, key, len)) {
147                 DEBUG(0, ("send_test_msg: ibw_send error\n"));
148                 return -1;
149         }
150         tcx->nsent++;
151
152         return 0;
153 }
154
155 unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
156 {
157         uint32_t        i = size;
158         unsigned char   sum = 0;
159         unsigned char   value;
160         while(i) {
161                 i--;
162                 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
163                 buf[i] = value;
164                 sum += value;
165         }
166         return sum;
167 }
168
169 unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
170 {
171         uint32_t        i = size;
172         unsigned char   sum = 0;
173
174         while(i) {
175                 i--;
176                 sum += buf[i];
177         }
178         return sum;
179 }
180
181 int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
182 {
183         unsigned char *buf;
184         void    *key;
185         uint32_t        len;
186         unsigned char   sum;
187
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"));
191                 return -1;
192         }
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"));
198                 return -1;
199         }
200         tcx->nsent++;
201
202         return 0;
203 }
204
205 int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *conn)
206 {
207         uint32_t        size;
208         int     i;
209
210         for(i=0; i<tcx->nmsg; i++)
211         {
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))
215                         return -1;
216         }
217         return 0;
218 }
219
220 /*int ibwtest_do_varsize_scenario(ibwtest_ctx *tcx)
221 {
222         int     rc;
223         struct ibw_conn *conn;
224
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);
228                         if (rc)
229                                 tcx->error = rc;
230                 }
231         }
232 }*/
233
234 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
235 {
236         struct ibwtest_ctx      *tcx = NULL; /* userdata */
237         struct ibwtest_conn     *pconn = NULL; /* userdata */
238
239         if (ctx) {
240                 tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
241
242                 switch(ctx->state) {
243                 case IBWS_INIT:
244                         DEBUG(10, ("test IBWS_INIT\n"));
245                         break;
246                 case IBWS_READY:
247                         DEBUG(10, ("test IBWS_READY\n"));
248                         break;
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"));
254                         }
255                         break;
256                 case IBWS_STOPPED:
257                         DEBUG(10, ("test IBWS_STOPPED\n"));
258                         tcx->kill_me = 1; /* main loop can exit */
259                         break;
260                 case IBWS_ERROR:
261                         DEBUG(10, ("test IBWS_ERROR\n"));
262                         ibw_stop(tcx->ibwctx);
263                         break;
264                 default:
265                         assert(0);
266                         break;
267                 }
268         }
269
270         if (conn) {
271                 pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
272                 switch(conn->state) {
273                 case IBWC_INIT:
274                         DEBUG(10, ("test IBWC_INIT\n"));
275                         break;
276                 case IBWC_CONNECTED:
277                         if (gettimeofday(&tcx->start_time, NULL)) {
278                                 DEBUG(0, ("gettimeofday error %d", errno));
279                                 return -1;
280                         }
281                         ibwtest_send_id(conn);
282                         break;
283                 case IBWC_DISCONNECTED:
284                         DEBUG(10, ("test IBWC_DISCONNECTED\n"));
285                         talloc_free(conn);
286                         break;
287                 case IBWC_ERROR:
288                         DEBUG(10, ("test IBWC_ERROR\n"));
289                         break;
290                 default:
291                         assert(0);
292                         break;
293                 }
294         }
295         return 0;
296 }
297
298 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
299 {
300         struct ibwtest_conn *pconn;
301         enum testopcode op;
302         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
303         int     rc = 0;
304
305         assert(conn!=NULL);
306         assert(n>=sizeof(uint32_t)+1);
307         pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
308
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);
312         }
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));
316         }
317
318         if (tcx->is_server) {
319                 if (op==TESTOP_SEND_RND) {
320                         unsigned char sum;
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",
324                                 op,
325                                 n - sizeof(uint32_t) - 2,
326                                 (uint32_t)sum,
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);
332                                 return -3;
333                         }
334                 } else {
335                         char *buf2;
336                         void *key2;
337
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");
341                                 return -1;
342                         }
343                         memcpy(buf2, buf, n);
344                         if (ibw_send(conn, buf2, key2, n)) {
345                                 fprintf(stderr, "ibw_send error #2\n");
346                                 return -2;
347                         }
348                         tcx->nsent++;
349                 }
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);
354                 }
355
356                 if (tcx->nmsg) {
357                         char    msg[26];
358                         sprintf(msg, "hello world %d", tcx->nmsg--);
359                         rc = ibwtest_send_test_msg(tcx, conn, msg);
360                         if (tcx->nmsg==0) {
361                                 ibw_stop(tcx->ibwctx);
362                                 tcx->stopping = 1;
363                         }
364                 }
365         }
366
367         if (rc)
368                 tcx->error = rc;
369
370         return rc;
371 }
372
373 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te, 
374         struct timeval t, void *private)
375 {
376         struct ibwtest_ctx *tcx = talloc_get_type(private, struct ibwtest_ctx);
377         int     rc;
378
379         if (!tcx->is_server) {
380                 struct ibw_conn *conn;
381                 char    msg[50];
382
383                 /* fill it with something variable... */
384                 sprintf(msg, "hello world %d", tcx->cnt++);
385
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);
390                                 if (rc)
391                                         tcx->error = rc;
392                         }
393                 }
394         } /* else allow main loop run */
395 }
396
397 static struct ibwtest_ctx *testctx = NULL;
398
399 void ibwtest_sigint_handler(int sig)
400 {
401         DEBUG(0, ("got SIGINT\n"));
402         if (testctx) {
403                 if (testctx->ibwctx->state==IBWS_READY ||
404                         testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
405                         testctx->ibwctx->state==IBWS_ERROR)
406                 {
407                         if (testctx->stopping) {
408                                 DEBUG(10, ("forcing exit...\n"));
409                                 testctx->kill_me = 1;
410                         } else {
411                                 /* mostly expected case */
412                                 ibw_stop(testctx->ibwctx);
413                                 testctx->stopping = 1;
414                         }
415                 } else
416                         testctx->kill_me = 1;
417         }
418 }
419
420 int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
421         struct ibw_initattr **pattrs, int *nattrs, char op)
422 {
423         int     i = 0, n = 1;
424         int     porcess_next = 1;
425         char    *p, *q;
426         struct ibw_initattr *attrs = NULL;
427
428         *pattrs = NULL;
429         for(p = optext; *p!='\0'; p++) {
430                 if (*p==',')
431                         n++;
432         }
433
434         attrs = (struct ibw_initattr *)talloc_size(tcx,
435                 n * sizeof(struct ibw_initattr));
436         for(p = optext; *p!='\0'; p++) {
437                 if (porcess_next) {
438                         attrs[i].name = p;
439                         q = strchr(p, ':');
440                         if (q==NULL) {
441                                 fprintf(stderr, "-%c format error\n", op);
442                                 return -1;
443                         }
444                         *q = '\0';
445                         attrs[i].value = q + 1;
446
447                         porcess_next = 0;
448                         i++;
449                         p = q; /* ++ at end */
450                 }
451                 if (*p==',') {
452                         *p = '\0'; /* ++ at end */
453                         porcess_next = 1;
454                 }
455         }
456         *pattrs = attrs;
457         *nattrs = n;
458
459         return 0;
460 }
461
462 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
463 {
464         int     i;
465         struct ibw_initattr     *attrs = NULL;
466         struct sockaddr_in      *p;
467         char    *tmp;
468
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))
472                 return -1;
473
474         tcx->addrs = talloc_size(tcx,
475                 tcx->naddrs * sizeof(struct sockaddr_in));
476         for(i=0; i<tcx->naddrs; i++) {
477                 p = tcx->addrs + 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);
481         }
482
483         return 0;
484 }
485
486 int ibwtest_init_server(struct ibwtest_ctx *tcx)
487 {
488         if (tcx->naddrs!=1) {
489                 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
490                 return -1;
491         }
492
493         if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
494                 DEBUG(0, ("ERROR: ibw_bind failed\n"));
495                 return -1;
496         }
497         
498         if (ibw_listen(tcx->ibwctx, 1)) {
499                 DEBUG(0, ("ERROR: ibw_listen failed\n"));
500                 return -1;
501         }
502
503         /* continued at IBWS_READY */
504         return 0;
505 }
506
507 void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
508 {
509         printf("Usage:\n");
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");
521 }
522
523 int main(int argc, char *argv[])
524 {
525         int     rc, op;
526         int     result = 1;
527         struct event_context *ev = NULL;
528         struct ibwtest_ctx *tcx = NULL;
529         float   usec;
530
531         tcx = talloc_zero(NULL, struct ibwtest_ctx);
532         memset(tcx, 0, sizeof(struct ibwtest_ctx));
533         tcx->nsec = 0;
534         tcx->nmsg = 1000;
535
536         /* here is the only case we can't avoid using global... */
537         testctx = tcx;
538         signal(SIGINT, ibwtest_sigint_handler);
539         srand((unsigned)time(NULL));
540
541         while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:")) != -1) {
542                 switch (op) {
543                 case 'i':
544                         tcx->id = talloc_strdup(tcx, optarg);
545                         break;
546                 case 'o':
547                         tcx->opts = talloc_strdup(tcx, optarg);
548                         if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
549                                 &tcx->nattrs, op))
550                                 goto cleanup;
551                         break;
552                 case 'd':
553                         if (ibwtest_getdests(tcx, op))
554                                 goto cleanup;
555                         break;
556                 case 's':
557                         tcx->is_server = 1;
558                         break;
559                 case 't':
560                         tcx->nsec = (unsigned int)atoi(optarg);
561                         break;
562                 case 'n':
563                         tcx->nmsg = atoi(optarg);
564                         break;
565                 case 'l':
566                         tcx->sleep_usec = (unsigned int)atoi(optarg);
567                         break;
568                 case 'v':
569                         tcx->maxsize = (unsigned int)atoi(optarg);
570                         break;
571                 default:
572                         fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
573                         ibwtest_usage(tcx, argv[0]);
574                         goto cleanup;
575                 }
576         }
577         if (tcx->id==NULL) {
578                 ibwtest_usage(tcx, argv[0]);
579                 goto cleanup;
580         }
581
582         ev = event_context_init(NULL);
583         assert(ev);
584
585         tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
586                 tcx,
587                 ibwtest_connstate_handler,
588                 ibwtest_receive_handler,
589                 ev
590         );
591         if (!tcx->ibwctx)
592                 goto cleanup;
593
594         if (tcx->is_server)
595                 rc = ibwtest_init_server(tcx);
596         else
597                 rc = ibwtest_connect_everybody(tcx);
598         if (rc)
599                 goto cleanup;
600
601         while(!tcx->kill_me && !tcx->error) {
602                 if (tcx->nsec) {
603                         event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec),
604                                 ibwtest_timeout_handler, tcx);
605                 }
606
607                 event_loop_once(ev);
608
609                 if (tcx->sleep_usec)
610                         usleep(tcx->sleep_usec);
611         }
612
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));
616                         goto cleanup;
617                 }
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);
622         }
623
624         if (!tcx->error)
625                 result = 0; /* everything OK */
626
627 cleanup:
628         if (tcx)
629                 talloc_free(tcx);
630         if (ev)
631                 talloc_free(ev);
632         DEBUG(0, ("exited with code %d\n", result));
633         return result;
634 }