This repository was archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathseqtest.c
More file actions
1317 lines (1180 loc) · 27.8 KB
/
seqtest.c
File metadata and controls
1317 lines (1180 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 Lucera Financial Infrastructures, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* This program is used to stress test TCP connections, verifying that
* ordering constraints are preserved across a connection. The intent
* is to validate correct function of a TCP proxy.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <math.h>
#include <pthread.h>
#define FLAG_REPLY (1u << 0)
#define FLAG_ERROR (1u << 1)
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
static int start_wait = 0;
static int start_ready = 0;
static pthread_cond_t waitcv;
static pthread_cond_t startcv;
static pthread_mutex_t startmx;
typedef struct sample {
uint64_t when;
uint64_t lat;
uint16_t ssz;
uint16_t rsz;
} sample_t;
#ifndef HAVE_STRLCPY
int
strlcpy(char *dst, const char *src, size_t dstsize)
{
int len;
char *end;
/* poor mans strlcpy, not as fast as a smarter implementation */
(void) strncpy(dst, src, dstsize);
if (dstsize > 0) {
dst[dstsize - 1] = '\0';
}
return (strlen(src));
}
#endif /* HAVE_STRLCPY */
/*
* Amazing - MacOS X doesn't have a standards conforming version
* of high resolution timers.
*/
#ifndef HAVE_GETHRTIME
#if defined(HAVE_CLOCK_GETTIME)
uint64_t
gethrtime(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
perror("clock_gettime");
exit(1);
}
return (ts.tv_nsec + (ts.tv_sec * 1000000000ull));
}
#elif defined(HAVE_GETTIMEOFDAY)
uint64_t
gethrtime(void)
{
struct timeval tv;
uint64_t nsec;
gettimeofday(&tv, NULL);
nsec = tv.tv_sec * 1000000000ull + tv.tv_usec * 1000ull;
return (nsec);
}
#else
#error "No way to determine high resolution time"
#endif
#endif /* HAVE_GETHRTIME */
/* We probably don't want to exchange messages in excess of this. */
uint32_t maxmsg = 8000;
int debug = 0;
/*
* Test header, used at the start of every message.
*/
typedef struct test_header {
uint64_t seqno;
uint64_t ts1; /* senders send time */
uint64_t ts2; /* repliers recv time */
uint64_t ts3; /* repliers send time */
uint32_t rdly; /* reply delay (ns) */
uint16_t ssz; /* send size */
uint16_t rsz; /* reply size */
} test_header_t;
/*
* Each thread in the sending system is driven by a single state.
* This allows us to set up the test, but otherwise each thread runs
* independent of the others, so we have no locks, nor races.
*/
typedef struct test {
int sock;
uint64_t sseqno;
uint64_t rseqno;
uint32_t rdly_min; /* reply delay (ns) */
uint32_t rdly_max; /* reply delay (ns) */
uint32_t sdly_min; /* interpacket send delay (ns) */
uint32_t sdly_max; /* interpacket send delay (ns) */
uint16_t ssz_min; /* send size min */
uint16_t ssz_max; /* send size max */
uint16_t rsz_min; /* reply size min */
uint16_t rsz_max; /* reply size max */
uint32_t rintvl; /* reply interval (0 = none) */
uint64_t count; /* num to exchange */
uint64_t replies; /* total replies */
uint32_t flags; /* flags */
pthread_t tid; /* pthread processing this test */
struct sockaddr *addr; /* address for the socket */
struct addrinfo *lai; /* local addr to bind (client only) */
socklen_t addrlen;
sample_t *samples;
} test_t;
/*
* randtime determines the number of nsec used for each call to random().
* The idea here is that we can use random() as a busy worker to spin. This
* will prevent it from being optimized away, and gives us some idea of the
* involved with each iteration.
*/
uint64_t
randtime(void)
{
uint64_t now, end;
static uint64_t rtime;
int i;
if (rtime < 1) {
now = gethrtime();
for (i = 0; i < 1U << 20; i++) {
random();
}
end = gethrtime();
rtime = (end - now) >> 20;
}
if (rtime < 1) {
rtime = 1;
}
return rtime;
}
int
cmpu64(const void *u1, const void *u2)
{
return (*(uint64_t *)u1 - *(uint64_t *)u2);
}
double
pctile(uint64_t *samples, size_t nsamples, double pctile)
{
double i, k;
i = (nsamples * pctile / 100.0);
k = ceil(i);
if (k != i) {
return (double)samples[(int)k-1];
}
k = floor(i);
return ((samples[(int)k-1]+samples[(int)k])/2.0);
}
/*
* ndelay waits a given number of nsec. It does this by sleeping for large
* values of nsec, but will spin when a smaller delay is required.
*/
void
ndelay(uint32_t nsec)
{
uint64_t now, end;
end = gethrtime() + nsec;
while ((now = gethrtime()) < end) {
if ((end - now) > 1000000) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = end - now;
/* we'll probably sleep too long, that's ok */
nanosleep(&ts, NULL);
continue;
}
/*
* Do some work, shouldn't take long, but this eases the pressure
* we put on gethrtime.
*/
random();
}
}
/*
* range returns a value chosen at random between a min and a max. The value
* is chosen using rand(), so this is not suitable for cryptographic purposes.
*/
uint32_t
range(uint32_t minval, uint32_t maxval)
{
uint32_t val = minval;
if (maxval > minval) {
val += (rand() % (maxval - minval));
}
return (val);
}
/*
* senderreceiver is a pthread worker that sends a single message and expects
* a reply.
*/
void *
senderreceiver(void *arg)
{
test_t *t = arg;
char *sbuf, *rbuf, *sptr, *rptr;
uint64_t stime, now, deltat;
uint32_t nbytes = 0;
int rv;
test_header_t *sh, *rh;
int i;
int good = 0;
int count;
sbuf = malloc(maxmsg);
rbuf = malloc(maxmsg);
pthread_mutex_lock(&startmx);
start_wait++;
pthread_cond_signal(&waitcv);
while (!start_ready) {
pthread_cond_wait(&startcv, &startmx);
}
pthread_mutex_unlock(&startmx);
rptr = rbuf;
count = t->count;
t->rintvl = 1;
if (count < 1) {
fprintf(stderr, "count must be at least 1\n");
exit(1);
}
for (i = 0; i < count; i++) {
uint16_t ssz, rsz;
uint32_t sdly, rdly;
sh = (void *)sbuf;
sptr = sbuf;
ssz = (uint16_t) range(t->ssz_min, t->ssz_max);
rsz = (uint16_t) range(t->rsz_min, t->rsz_max);
sdly = range(t->sdly_min, t->sdly_min);
rdly = range(t->rdly_min, t->rdly_min);
sh->ssz = ssz;
sh->rsz = (t->rintvl && ((i % t->rintvl) == 0)) ? rsz : 0;
sh->rdly = sh->rsz ? rdly : 0;
sh->seqno = t->sseqno++;
ndelay(sdly);
stime = gethrtime();
sh->ts3 = 0;
sh->ts2 = 0;
sh->ts1 = stime;
while (ssz > 0) {
rv = send(t->sock, sptr, ssz, 0);
if (rv < 0) {
perror("sender/send");
goto out;
}
ssz -= rv;
sptr += rv;
}
if (debug)
write(1, ">", 1);
rh = (void *)rbuf;
for (;;) {
size_t resid;
if (nbytes < sizeof (*rh)) {
/* suck in as much as we can */
resid = maxmsg - sizeof (*rh);
} else if (rh->rsz > maxmsg) {
fprintf(stderr, "h->rsz too big\n");
goto out;
} else if (nbytes < rh->rsz) {
resid = rh->rsz - nbytes;
} else {
break;
}
rv = recv(t->sock, rptr, resid, 0);
now = gethrtime();
if (rv < 0) {
perror("rcvr/recv");
goto out;
}
if (rv == 0) {
fprintf(stderr,
"sender: recv closed too soon "
"(%d rx, expected %d)\n", i, count);
goto out;
}
nbytes += rv;
rptr += rv;
}
assert(nbytes >= sizeof (*rh));
assert(nbytes >= rh->rsz);
if (rh->seqno != sh->seqno) {
fprintf(stderr,
"reply seqno out of order (%"
PRIu64 " != %" PRIu64 ")!!\n",
rh->seqno, sh->seqno);
goto out;
}
if (rh->ts3 < rh->ts2) {
fprintf(stderr, "negative packet processing cost\n");
goto out;
}
if (rh->ts1 != sh->ts1) {
fprintf(stderr, "mismatched timestamps: %" PRIu64
" != %" PRIu64 "\n", rh->ts1, sh->ts1);
goto out;
}
deltat = (now - rh->ts1) - (rh->ts3 - rh->ts2);
t->samples[t->rseqno].when = rh->ts1;
t->samples[t->rseqno].lat = deltat;
t->samples[t->rseqno].ssz = sh->ssz;
t->samples[t->rseqno].rsz = rh->rsz;
t->rseqno++;
/* if seqno dropped or duplicate, we expect many error msgs */
t->replies++;
if (debug)
write(1, "<", 1);
nbytes -= rh->rsz;
memmove(rbuf, rbuf + rh->rsz, nbytes);
rptr = rbuf + nbytes;
}
if (i < count) {
fprintf(stderr,
"only exchanged %d out of %d messages\n", i, count);
goto out;
}
good = 1;
out:
close(t->sock);
free(rbuf);
free(sbuf);
if (!good) {
exit(1);
}
return (NULL);
}
/*
* sender is a pthread worker that sends the initial messages.
*/
void *
sender(void *arg)
{
test_t *t = arg;
char *buf, *ptr;
uint64_t count = 0, stime;
int rv;
test_header_t *h;
int i;
buf = malloc(maxmsg);
count = t->count;
if (count < 1) {
fprintf(stderr, "count must be at least 1\n");
exit(1);
}
for (i = 0; i < count; i++) {
uint16_t ssz, rsz;
uint32_t sdly, rdly;
h = (void *)buf;
ptr = buf;
ssz = (uint16_t) range(t->ssz_min, t->ssz_max);
rsz = (uint16_t) range(t->rsz_min, t->rsz_max);
sdly = range(t->sdly_min, t->sdly_min);
rdly = range(t->rdly_min, t->rdly_min);
h->ssz = ssz;
h->rsz = (t->rintvl && ((i % t->rintvl) == 0)) ? rsz : 0;
h->rdly = h->rsz ? rdly : 0;
h->seqno = t->sseqno++;
ndelay(sdly);
stime = gethrtime();
h->ts3 = 0;
h->ts2 = 0;
h->ts1 = stime;
while (ssz > 0) {
rv = send(t->sock, ptr, ssz, 0);
if (rv < 0) {
perror("sender/send");
return (NULL);
}
ssz -= rv;
ptr += rv;
}
if (debug)
write(1, ">", 1);
}
free(buf);
return (NULL);
}
/*
* receiver is a pthread worker that receives any replies. It runs in the
* same process as sender.
*/
void *
receiver(void *arg)
{
test_t *t = arg;
char *buf, *ptr;
uint32_t exp;
uint32_t nbytes = 0;
uint64_t ltime = 0, now = 0, deltat = 0;
test_header_t *h;
int rv;
buf = malloc(maxmsg);
ptr = buf;
exp = t->rintvl ? t->count / t->rintvl : 0;
while (t->count == 0 || (exp > 0)) {
h = (void *)buf;
for (;;) {
size_t resid;
if (nbytes < sizeof (*h)) {
/* suck in as much as we can */
resid = maxmsg - sizeof (*h);
} else if (h->rsz > maxmsg) {
fprintf(stderr, "h->rsz too big\n");
goto out;
} else if (nbytes < h->rsz) {
resid = h->rsz - nbytes;
} else {
break;
}
rv = recv(t->sock, ptr, resid, 0);
now = gethrtime();
if (rv < 0) {
perror("rcvr/recv");
goto out;
}
if (rv == 0) {
fprintf(stderr, "receiver: recv closed too soon\n");
goto out;
}
nbytes += rv;
ptr += rv;
}
assert(nbytes >= sizeof (*h));
assert(nbytes >= h->rsz);
if (h->ts1 < ltime) {
fprintf(stderr, "ts1 backwards %" PRIu64
" < %" PRIu64 " !!\n",
h->ts1, ltime);
}
if (now < ltime) {
fprintf(stderr, "time-travelling packet\n");
}
if (h->ts3 < h->ts2) {
fprintf(stderr, "negative packet processing cost\n");
}
deltat = (now - h->ts1) - (h->ts3 - h->ts2);
ltime = h->ts1;
if (h->seqno != t->rseqno) {
fprintf(stderr,
"reply seqno out of order (%" PRIu64
" != %" PRIu64 ")!!\n",
h->seqno, t->rseqno);
}
t->samples[t->rseqno].when = h->ts1;
t->samples[t->rseqno].lat = deltat;
t->samples[t->rseqno].ssz = 0; /* probably of no use here */
t->samples[t->rseqno].rsz = 0;
t->rseqno++;
/* if seqno dropped or duplicate, we expect many error msgs */
/* XXX: we could check timestamps, figure latency, etc. */
t->replies++;
if (debug)
write(1, "<", 1);
nbytes -= h->rsz;
memmove(buf, buf + h->rsz, nbytes);
ptr = buf + nbytes;
if (exp > 0)
exp--;
}
out:
free(buf);
return (NULL);
}
/*
* replier is a pthread worker that services the initial sent messages,
* checking them for correctness and optionally sending a reply. Note that
* the nature of the reply is driven by the message received, rather than
* by the test. This allows this to run mostly configuration free.
*/
void *
replier(void *arg)
{
test_t *t = arg;
char *sbuf, *sptr;
char *rbuf, *rptr;
uint32_t nbytes = 0;
uint64_t ltime = 0, now = 0;
test_header_t *h;
uint32_t rdly;
uint16_t rsz, ssz;
int rv;
rbuf = malloc(maxmsg);
sbuf = malloc(maxmsg);
rptr = rbuf;
sptr = sbuf;
for (;;) {
h = (void *)rbuf;
for (;;) {
size_t resid;
if (nbytes < sizeof (*h)) {
/* suck in as much as we can */
resid = t->ssz_max - nbytes;
} else if (h->ssz > maxmsg) {
fprintf(stderr, "h->ssz too big\n");
goto out;
} else if (nbytes < h->ssz) {
resid = h->ssz - nbytes;
} else {
break;
}
rv = recv(t->sock, rptr, resid, 0);
now = gethrtime();
if (rv < 0) {
perror("replier/recv");
goto out;
}
if (rv == 0) {
goto out;
}
rptr += rv;
nbytes += rv;
}
if (debug)
write(1, "-", 1);
assert(nbytes >= sizeof (*h));
assert(nbytes >= h->ssz);
if (h->ts1 < ltime) {
fprintf(stderr, "replier: ts1 backwards!!\n");
}
ltime = h->ts1;
rdly = h->rdly;
rsz = h->rsz;
ssz = h->ssz;
if (h->seqno != t->sseqno++) {
fprintf(stderr, "reply seqno out of order!!\n");
}
/* if seqno dropped or duplicate, we expect many error msgs */
nbytes -= ssz;
memmove(rbuf, rptr, nbytes);
rptr = rbuf + nbytes;
if ((nbytes = rsz) == 0) {
continue;
}
ndelay(h->rdly);
h = (void *)sbuf;
sptr = (void *)sbuf;
h->seqno = t->rseqno++;
h->ssz = ssz;
h->rsz = rsz;
h->ts1 = ltime;
h->rdly = rdly;
h->ts2 = now;
h->ts3 = gethrtime();
while (nbytes) {
rv = send(t->sock, sptr, nbytes, 0);
if (rv < 0) {
perror("send");
goto out;
}
nbytes -= rv;
sptr += rv;
}
if (debug) {
write(1, "+", 1);
}
}
out:
close(t->sock);
free(sbuf);
free(rbuf);
free(arg);
return (NULL);
}
/*
* acceptor runs in the replier's process, and is reponsible for firing
* off a replier for each inbound connection.
*/
void *
acceptor(void *arg)
{
test_t *t = arg;
test_t *newt;
int s;
for (;;) {
socklen_t slen;
struct sockaddr_storage sa;
slen = sizeof (sa);
s = accept(t->sock, (void *)&sa, &slen);
if (s < 0) {
perror("accept");
close(t->sock);
return (NULL);
}
newt = malloc(sizeof (*newt));
memcpy(newt, t, sizeof (*newt));
newt->sock = s;
newt->tid = 0;
pthread_create(&newt->tid, NULL, replier, newt);
pthread_detach(newt->tid);
}
}
test_t *tests = NULL;
struct sockaddr **addrs = NULL;
int naddrs;
enum mode {
MODE_ASYNC_SEND = 0,
MODE_REPLIER,
MODE_SYNC_SEND
};
char *myopts[] = {
#define SMIN 0
"ssize_min",
#define SMAX 1
"ssize_max",
#define SSIZE 2
"ssize",
#define RMIN 3
"rsize_min",
#define RMAX 4
"rsize_max",
#define RSIZE 5
"rsize",
#define THREADS 6
"threads",
#define SDELAY 7
"sdelay",
#define RDELAY 8
"rdelay",
#define SDELAY_MIN 9
"sdelay_min",
#define SDELAY_MAX 10
"sdelay_max",
#define RDELAY_MIN 11
"rdelay_min",
#define RDELAY_MAX 12
"rdelay_max",
#define RINTERVAL 13
"rinterval",
#define COUNT 14
"count",
#define DUMPFILE 15
"dump",
NULL
};
void
check_ndelay(void)
{
/* some timing tests to make sure our implementation doesn't suck */
uint64_t start, finish;
printf("randtime is %llu\n", (unsigned long long)randtime());
start = gethrtime();
ndelay(1000000);
finish = gethrtime();
printf("ndelay 1 msec took %llu ns\n", (unsigned long long)(finish - start));
start = gethrtime();
ndelay(1000000000);
finish = gethrtime();
printf("ndelay 1 sec took %llu ns\n", (unsigned long long)(finish - start));
start = gethrtime();
sleep(1);
finish = gethrtime();
printf("sleep 1 sec took %llu ns\n", (unsigned long long)(finish - start));
start = gethrtime();
usleep(10000);
finish = gethrtime();
printf("usleep(10ms) took %llu ns\n", (unsigned long long)(finish - start));
}
/*
* Parse the local address from addrstr. If one exists, point
* *local_addr to it and update *addrstr to point to the rest of the
* address string. If no local address is found then set *local_addr
* to NULL and leave *addrstr unchanged.
*
* E.g.
*
* *addrstr = "192.168.1.115,192.168.1.119:6789"
*
* then:
*
* *local_addr = "192.168.1.115"
* *addstr = "192.168.1.119:6789"
*/
static void
parse_local_addr(char **addrstr, char **local_addr)
{
char *delim = strchr(*addrstr, ',');
if (delim != NULL) {
/*
* There is a local bind address specified. Split it
* off from the rest of the address string.
*/
*delim = '\0';
*local_addr = *addrstr;
*addrstr = delim + 1;
} else {
*local_addr = NULL;
}
}
/*
* Parse the host and port from addrstr. If the addrstr is well formed
* then *host will point to the host string and *port to port.
* Otherwise an error will print to stderr and the program will exit.
*
*
* This function is destrcutive to *addrstr. It is equal to *host upon
* return.
*
* E.g.
*
* *addrstr = "192.168.1.119:6789"
*
* then:
*
* *host = "192.168.1.119"
* *port = "6789"
* *addrstr = "192.168.1.119"
*/
static void
parse_addr(char **addrstr, char **host, char **port)
{
char *delim = strrchr(*addrstr, ':');
if (delim == NULL) {
fprintf(stderr, "no port found: %s\n", *addrstr);
exit(1);
}
/*
* Separate host from port.
*/
*delim = '\0';
*host = *addrstr;
*port = delim + 1;
/*
* Strip IPv6 brackets.
*/
if ((**host == '[') && ((*host)[strlen(*host) - 1] == ']')) {
(*host)[strlen(*host) - 1] = '\0';
(*host)++;
}
}
int
main(int argc, char **argv)
{
int c;
char *options, *optval;
uint16_t ssz_min, ssz_max, rsz_min, rsz_max;
uint32_t rdly_min, rdly_max;
uint32_t sdly_min, sdly_max;
uint32_t rintvl;
uint32_t nthreads;
uint32_t count;
enum mode mode;
int nais;
struct addrinfo **ais;
struct addrinfo **lais;
FILE *dumpfile = NULL;
uint64_t begin_time, finish_time;
int i;
ssz_min = ssz_max = rsz_min = rsz_max = sizeof (test_header_t);
rdly_min = rdly_max = 0;
sdly_min = sdly_max = 0;
rintvl = 1;
nthreads = 1;
count = 1;
mode = MODE_ASYNC_SEND;
/* initialize the timer */
(void) randtime();
while ((c = getopt(argc, argv, "o:srdS")) != EOF) {
switch (c) {
case 'd':
debug++;
break;
case 's':
mode = MODE_ASYNC_SEND;
break;
case 'S':
mode = MODE_SYNC_SEND;
break;
case 'r':
mode = MODE_REPLIER;
break;
case 'o':
options = optarg;
while (*options != '\0') {
switch (getsubopt(&options, myopts, &optval)) {
case SMIN:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
ssz_min = atoi(optval);
break;
case SMAX:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
ssz_max = atoi(optval);
break;
case SSIZE:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
ssz_max = ssz_min = atoi(optval);
break;
case RMIN:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rsz_min = atoi(optval);
break;
case RMAX:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rsz_max = atoi(optval);
break;
case RSIZE:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rsz_max = rsz_min = atoi(optval);
break;
case THREADS:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
nthreads = atoi(optval);
break;
case RDELAY_MIN:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rdly_min = atoi(optval);
break;
case RDELAY_MAX:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rdly_max = atoi(optval);
break;
case RDELAY:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rdly_min = rdly_max = atoi(optval);
break;
case SDELAY_MIN:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
sdly_min = atoi(optval);
break;
case SDELAY_MAX:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
sdly_max = atoi(optval);
break;
case SDELAY:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
sdly_min = sdly_max = atoi(optval);
break;
case RINTERVAL:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}
rintvl = atoi(optval);
break;
case COUNT:
if (optval == NULL) {
fprintf(stderr, "no value\n");
exit(1);
}