python:tests: Store keys as bytes rather than as lists of ints
[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 "replace.h"
24 #include "system/network.h"
25 #include "system/time.h"
26 #include "system/wait.h"
27
28 #include <assert.h>
29 #include <talloc.h>
30 #include <tevent.h>
31
32 #include "lib/util/time.h"
33 #include "lib/util/debug.h"
34
35 #include "common/logging.h"
36
37 #include "ib/ibwrapper.h"
38
39 struct ibwtest_ctx {
40         int     is_server;
41         char    *id; /* my id */
42
43         struct ibw_initattr *attrs;
44         int     nattrs;
45         char    *opts; /* option string */
46
47         struct sockaddr_in *addrs; /* dynamic array of dest addrs */
48         int     naddrs;
49
50         unsigned int    nsec; /* delta times between messages in nanosec */
51         unsigned int    sleep_usec; /* microsecs to sleep in the main loop to emulate overloading */
52         uint32_t        maxsize; /* maximum variable message size */
53
54         int     cnt;
55         int     nsent;
56
57         int     nmsg; /* number of messages to send (client) */
58
59         int     kill_me;
60         int     stopping;
61         int     error;
62         struct ibw_ctx  *ibwctx;
63
64         struct timeval  start_time, end_time;
65 };
66
67 struct ibwtest_conn {
68         char    *id;
69 };
70
71 enum testopcode {
72         TESTOP_SEND_ID = 1,
73         TESTOP_SEND_TEXT = 2,
74         TESTOP_SEND_RND = 3
75 };
76
77 static int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
78 {
79         struct ibw_conn         *conn;
80         struct ibwtest_conn     *tconn = talloc_zero(tcx, struct ibwtest_conn);
81         int     i;
82
83         for(i=0; i<tcx->naddrs; i++) {
84                 conn = ibw_conn_new(tcx->ibwctx, tconn);
85                 if (ibw_connect(conn, &tcx->addrs[i], tconn)) {
86                         fprintf(stderr, "ibw_connect error at %d\n", i);
87                         return -1;
88                 }
89         }
90         DEBUG(DEBUG_DEBUG, ("sent %d connect request...\n", tcx->naddrs));
91
92         return 0;
93 }
94
95 static int ibwtest_send_id(struct ibw_conn *conn)
96 {
97         struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
98         char *buf;
99         void *key;
100         uint32_t        len;
101
102         DEBUG(DEBUG_DEBUG, ("ibwtest_send_id\n"));
103         len = sizeof(uint32_t)+strlen(tcx->id)+2;
104         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
105                 DEBUG(DEBUG_ERR, ("send_id: ibw_alloc_send_buf failed\n"));
106                 return -1;
107         }
108
109         /* first sizeof(uint32_t) size bytes are for length */
110         *((uint32_t *)buf) = len;
111         buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
112         strcpy(buf+sizeof(uint32_t)+1, tcx->id);
113
114         if (ibw_send(conn, buf, key, len)) {
115                 DEBUG(DEBUG_ERR, ("send_id: ibw_send error\n"));
116                 return -1;
117         }
118         tcx->nsent++;
119
120         return 0;
121 }
122
123 static int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const char *msg)
124 {
125         char *buf, *p;
126         void *key;
127         uint32_t len;
128
129         if (conn->state!=IBWC_CONNECTED)
130                 return 0; /* not yet up */
131
132         len = strlen(msg) + 2 + sizeof(uint32_t);
133         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
134                 fprintf(stderr, "send_test_msg: ibw_alloc_send_buf failed\n");
135                 return -1;
136         }
137
138         *((uint32_t *)buf) = len;
139         p = buf;
140         p += sizeof(uint32_t);
141         p[0] = (char)TESTOP_SEND_TEXT;
142         p++;
143         strcpy(p, msg);
144
145         if (ibw_send(conn, buf, key, len)) {
146                 DEBUG(DEBUG_ERR, ("send_test_msg: ibw_send error\n"));
147                 return -1;
148         }
149         tcx->nsent++;
150
151         return 0;
152 }
153
154 static unsigned char ibwtest_fill_random(unsigned char *buf, uint32_t size)
155 {
156         uint32_t        i = size;
157         unsigned char   sum = 0;
158         unsigned char   value;
159         while(i) {
160                 i--;
161                 value = (unsigned char)(256.0 * (rand() / (RAND_MAX + 1.0)));
162                 buf[i] = value;
163                 sum += value;
164         }
165         return sum;
166 }
167
168 static unsigned char ibwtest_get_sum(unsigned char *buf, uint32_t size)
169 {
170         uint32_t        i = size;
171         unsigned char   sum = 0;
172
173         while(i) {
174                 i--;
175                 sum += buf[i];
176         }
177         return sum;
178 }
179
180 static int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_conn *conn, uint32_t size)
181 {
182         unsigned char *buf;
183         void    *key;
184         uint32_t        len;
185         unsigned char   sum;
186
187         len = sizeof(uint32_t) + 1 + size + 1;
188         if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
189                 DEBUG(DEBUG_ERR, ("varsize/ibw_alloc_send_buf failed\n"));
190                 return -1;
191         }
192         *((uint32_t *)buf) = len;
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(DEBUG_ERR, ("varsize/ibw_send failed\n"));
198                 return -1;
199         }
200         tcx->nsent++;
201
202         return 0;
203 }
204
205 static 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 static int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
235 {
236         struct ibwtest_ctx      *tcx = NULL; /* userdata */
237         struct ibwtest_conn     *tconn = 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(DEBUG_DEBUG, ("test IBWS_INIT\n"));
245                         break;
246                 case IBWS_READY:
247                         DEBUG(DEBUG_DEBUG, ("test IBWS_READY\n"));
248                         break;
249                 case IBWS_CONNECT_REQUEST:
250                         DEBUG(DEBUG_DEBUG, ("test IBWS_CONNECT_REQUEST\n"));
251                         tconn = talloc_zero(conn, struct ibwtest_conn);
252                         if (ibw_accept(ctx, conn, tconn)) {
253                                 DEBUG(DEBUG_ERR, ("error accepting the connect request\n"));
254                         }
255                         break;
256                 case IBWS_STOPPED:
257                         DEBUG(DEBUG_DEBUG, ("test IBWS_STOPPED\n"));
258                         tcx->kill_me = 1; /* main loop can exit */
259                         break;
260                 case IBWS_ERROR:
261                         DEBUG(DEBUG_DEBUG, ("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                 tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
272                 switch(conn->state) {
273                 case IBWC_INIT:
274                         DEBUG(DEBUG_DEBUG, ("test IBWC_INIT\n"));
275                         break;
276                 case IBWC_CONNECTED:
277                         if (gettimeofday(&tcx->start_time, NULL)) {
278                                 DEBUG(DEBUG_ERR, ("gettimeofday error %d", errno));
279                                 return -1;
280                         }
281                         ibwtest_send_id(conn);
282                         break;
283                 case IBWC_DISCONNECTED:
284                         DEBUG(DEBUG_DEBUG, ("test IBWC_DISCONNECTED\n"));
285                         talloc_free(conn);
286                         break;
287                 case IBWC_ERROR:
288                         DEBUG(DEBUG_DEBUG, ("test IBWC_ERROR %s\n", ibw_getLastError()));
289                         break;
290                 default:
291                         assert(0);
292                         break;
293                 }
294         }
295         return 0;
296 }
297
298 static int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
299 {
300         struct ibwtest_conn *tconn;
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         tconn = 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                 tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);
312         }
313         if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
314                 DEBUG(DEBUG_DEBUG, ("[%d]msg from %s: \"%s\"(%d)\n", op,
315                         tconn->id ? tconn->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(DEBUG_DEBUG, ("[%d]msg varsize %u/sum %u from %s\n",
324                                 op,
325                                 (uint32_t)(n - sizeof(uint32_t) - 2),
326                                 (uint32_t)sum,
327                                 tconn->id ? tconn->id : "NULL"));
328                         if (sum!=((unsigned char *)buf)[n-1]) {
329                                 DEBUG(DEBUG_ERR, ("ERROR: checksum mismatch %u!=%u\n",
330                                         (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
331                                 ibw_stop(tcx->ibwctx);
332                                 goto error;
333                         }
334                 } else if (op!=TESTOP_SEND_ID) {
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                                 goto error;
342                         }
343                         memcpy(buf2, buf, n);
344                         if (ibw_send(conn, buf2, key2, n)) {
345                                 fprintf(stderr, "ibw_send error #2\n");
346                                 goto error;
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 error:
372         return -1;
373 }
374
375 static void ibwtest_timeout_handler(struct tevent_context *ev,
376                                     struct tevent_timer *te,
377                                     struct timeval t, void *private_data)
378 {
379         struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
380         int     rc;
381
382         if (!tcx->is_server) {
383                 struct ibw_conn *conn;
384                 char    msg[50];
385
386                 /* fill it with something variable... */
387                 sprintf(msg, "hello world %d", tcx->cnt++);
388
389                 /* send something to everybody... */
390                 for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) {
391                         if (conn->state==IBWC_CONNECTED) {
392                                 rc = ibwtest_send_test_msg(tcx, conn, msg);
393                                 if (rc)
394                                         tcx->error = rc;
395                         }
396                 }
397         } /* else allow main loop run */
398 }
399
400 static struct ibwtest_ctx *testctx = NULL;
401
402 static void ibwtest_sigint_handler(int sig)
403 {
404         DEBUG(DEBUG_ERR, ("got SIGINT\n"));
405         if (testctx) {
406                 if (testctx->ibwctx->state==IBWS_READY ||
407                         testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
408                         testctx->ibwctx->state==IBWS_ERROR)
409                 {
410                         if (testctx->stopping) {
411                                 DEBUG(DEBUG_DEBUG, ("forcing exit...\n"));
412                                 testctx->kill_me = 1;
413                         } else {
414                                 /* mostly expected case */
415                                 ibw_stop(testctx->ibwctx);
416                                 testctx->stopping = 1;
417                         }
418                 } else
419                         testctx->kill_me = 1;
420         }
421 }
422
423 static int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
424         struct ibw_initattr **pattrs, int *nattrs, char op)
425 {
426         int     i = 0, n = 1;
427         int     porcess_next = 1;
428         char    *p, *q;
429         struct ibw_initattr *attrs = NULL;
430
431         *pattrs = NULL;
432         for(p = optext; *p!='\0'; p++) {
433                 if (*p==',')
434                         n++;
435         }
436
437         attrs = (struct ibw_initattr *)talloc_size(tcx,
438                 n * sizeof(struct ibw_initattr));
439         for(p = optext; *p!='\0'; p++) {
440                 if (porcess_next) {
441                         attrs[i].name = p;
442                         q = strchr(p, ':');
443                         if (q==NULL) {
444                                 fprintf(stderr, "-%c format error\n", op);
445                                 return -1;
446                         }
447                         *q = '\0';
448                         attrs[i].value = q + 1;
449
450                         porcess_next = 0;
451                         i++;
452                         p = q; /* ++ at end */
453                 }
454                 if (*p==',') {
455                         *p = '\0'; /* ++ at end */
456                         porcess_next = 1;
457                 }
458         }
459         *pattrs = attrs;
460         *nattrs = n;
461
462         return 0;
463 }
464
465 static int ibwtest_get_address(const char *address, struct in_addr *addr)
466 {
467         if (inet_pton(AF_INET, address, addr) <= 0) {
468                 struct hostent *he = gethostbyname(address);
469                 if (he == NULL || he->h_length > sizeof(*addr)) {
470                         DEBUG(DEBUG_ERR, ("invalid network address '%s'\n", address));
471                         return -1;
472                 }
473                 memcpy(addr, he->h_addr, he->h_length);
474         }
475         return 0;
476 }
477
478 static int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
479 {
480         int     i;
481         struct ibw_initattr     *attrs = NULL;
482         struct sockaddr_in      *p;
483         char    *tmp;
484
485         tmp = talloc_strdup(tcx, optarg);
486         if (tmp == NULL) return -1;
487         /* hack to reuse the above ibw_initattr parser */
488         if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
489                 return -1;
490
491         tcx->addrs = talloc_size(tcx,
492                 tcx->naddrs * sizeof(struct sockaddr_in));
493         for(i=0; i<tcx->naddrs; i++) {
494                 p = tcx->addrs + i;
495                 p->sin_family = AF_INET;
496                 if (ibwtest_get_address(attrs[i].name, &p->sin_addr))
497                         return -1;
498                 p->sin_port = htons(atoi(attrs[i].value));
499         }
500
501         return 0;
502 }
503
504 static int ibwtest_init_server(struct ibwtest_ctx *tcx)
505 {
506         if (tcx->naddrs!=1) {
507                 fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs);
508                 return -1;
509         }
510
511         if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
512                 DEBUG(DEBUG_ERR, ("ERROR: ibw_bind failed\n"));
513                 return -1;
514         }
515         
516         if (ibw_listen(tcx->ibwctx, 1)) {
517                 DEBUG(DEBUG_ERR, ("ERROR: ibw_listen failed\n"));
518                 return -1;
519         }
520
521         /* continued at IBWS_READY */
522         return 0;
523 }
524
525 static void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
526 {
527         printf("Usage:\n");
528         printf("\t%s -i <id> -o {name:value} -d {addr:port} -t nsec -s\n", name);
529         printf("\t-i <id> is a free text, acting as a server id, max 23 chars [mandatory]\n");
530         printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n");
531         printf("\t-a addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
532         printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec);
533         printf("\t\t send message periodically and endless when nsec is non-zero\n");
534         printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n");
535         printf("\t-n number of messages to send [default %d]\n", tcx->nmsg);
536         printf("\t-l usec time to sleep in the main loop [default %d]\n", tcx->sleep_usec);
537         printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize);
538         printf("\t-d LogLevel [default %d]\n", DEBUGLEVEL);
539         printf("Press ctrl+C to stop the program.\n");
540 }
541
542 int main(int argc, char *argv[])
543 {
544         int     rc, op;
545         int     result = 1;
546         struct tevent_context *ev = NULL;
547         struct ibwtest_ctx *tcx = NULL;
548         float   usec;
549
550         tcx = talloc_zero(NULL, struct ibwtest_ctx);
551         memset(tcx, 0, sizeof(struct ibwtest_ctx));
552         tcx->nsec = 0;
553         tcx->nmsg = 1000;
554         debuglevel_set(0);
555
556         /* here is the only case we can't avoid using global... */
557         testctx = tcx;
558         signal(SIGINT, ibwtest_sigint_handler);
559         srand((unsigned)time(NULL));
560
561         while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:a:")) != -1) {
562                 switch (op) {
563                 case 'i':
564                         tcx->id = talloc_strdup(tcx, optarg);
565                         break;
566                 case 'o':
567                         tcx->opts = talloc_strdup(tcx, optarg);
568                         if (tcx->opts) goto cleanup;
569                         if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
570                                 &tcx->nattrs, op))
571                                 goto cleanup;
572                         break;
573                 case 'a':
574                         if (ibwtest_getdests(tcx, op))
575                                 goto cleanup;
576                         break;
577                 case 's':
578                         tcx->is_server = 1;
579                         break;
580                 case 't':
581                         tcx->nsec = (unsigned int)atoi(optarg);
582                         break;
583                 case 'n':
584                         tcx->nmsg = atoi(optarg);
585                         break;
586                 case 'l':
587                         tcx->sleep_usec = (unsigned int)atoi(optarg);
588                         break;
589                 case 'v':
590                         tcx->maxsize = (unsigned int)atoi(optarg);
591                         break;
592                 case 'd':
593                         debuglevel_set(atoi(optarg));
594                         break;
595                 default:
596                         fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
597                         ibwtest_usage(tcx, argv[0]);
598                         goto cleanup;
599                 }
600         }
601         if (tcx->id==NULL) {
602                 ibwtest_usage(tcx, argv[0]);
603                 goto cleanup;
604         }
605
606         ev = tevent_context_init(NULL);
607         assert(ev);
608
609         tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs,
610                 tcx,
611                 ibwtest_connstate_handler,
612                 ibwtest_receive_handler,
613                 ev
614         );
615         if (!tcx->ibwctx)
616                 goto cleanup;
617
618         if (tcx->is_server)
619                 rc = ibwtest_init_server(tcx);
620         else
621                 rc = ibwtest_connect_everybody(tcx);
622         if (rc)
623                 goto cleanup;
624
625         while(!tcx->kill_me && !tcx->error) {
626                 if (tcx->nsec) {
627                         tevent_add_timer(ev, tcx,
628                                          timeval_current_ofs(0, tcx->nsec),
629                                          ibwtest_timeout_handler, tcx);
630                 }
631
632                 tevent_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(DEBUG_ERR, ("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(DEBUG_ERR, ("exited with code %d\n", result));
658         return result;
659 }