-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.c
1244 lines (1132 loc) · 37.7 KB
/
client.c
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
/*
* af_ktls tool
*
* Copyright (C) 2016 Fridolin Pokorny <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <getopt.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <errno.h>
#include <gnutls/gnutls.h>
#include <malloc.h>
#include <alloca.h>
#include "ktls.h"
#include "connection.h"
#include "common.h"
#include "benchmark.h"
#include "xlibgnutls.h"
#include "action.h"
#include "client.h"
#include "server.h"
#include "verify.h"
#define TLS_MAX_PACKET_LENGTH ((size_t)(1 << 12))
#define DTLS_OVERHEAD ((size_t)(13 + 8 + 16))
#define TLS_OVERHEAD ((size_t)(5 + 8 + 16))
#define OPT_TLS 't'
#define OPT_DTLS 'd'
#define OPT_SERVER_HOST 3
#define OPT_SERVER_PORT 'p'
#define OPT_SRC_PORT 5
#define OPT_SENDFILE 6
#define OPT_SEND_KTLS_COUNT 7
#define OPT_SEND_GNUTLS_COUNT 8
#define OPT_PAYLOAD 9
#define OPT_SPLICE_COUNT 0x0A
#define OPT_SENDFILE_MTU 'm'
#define OPT_VERBOSE 'v'
#define OPT_HELP 'h'
#define OPT_SENDFILE_MMAP 0x0E
#define OPT_SEND_KTLS_TIME 0x0F
#define OPT_SEND_GNUTLS_TIME 0x10
#define OPT_SPLICE_TIME 0x11
#define OPT_SENDFILE_SIZE 0x12
#define OPT_SPLICE_FILE 0x13
#define OPT_SERVER_STORE 0x14
#define OPT_SPLICE_ECHO_COUNT 0x15
#define OPT_SPLICE_ECHO_TIME 0x16
#define OPT_SERVER_KTLS 0x17
#define OPT_JSON 'j'
#define OPT_DROP_CACHES 'c'
#define OPT_VERIFY_SENDPAGE 0x21
#define OPT_VERIFY_TRANSMISSION 0x22
#define OPT_VERIFY_SPLICE_READ 0x23
#define OPT_VERIFY_HANDLING 0x24
#define OPT_OUTPUT 'o'
#define OPT_SENDFILE_USER 0x26
#define OPT_SERVER_NO_ECHO 0x27
#define OPT_SERVER_MTU 0x28
#define OPT_RAW_SEND_TIME 0x29
#define OPT_SPLICE_SEND_RAW_TIME 0x2A
#define OPT_PLAIN_SENDFILE 0x2B
#define OPT_PLAIN_SENDFILE_USER 0x2C
#define OPT_PLAIN_SPLICE_EMU 0x2D
#define OPT_PLAIN_SENDFILE_MMAP 0x2E
#define OPT_TCP 0x2F
#define OPT_UDP 0x30
#define OPT_SHORT_OPTS "td\x03:p:\x05:\x06:\x07:\x08:\x09:\x0A:m:vh\x0E:\x0F:\x10:\x11:\x12\x13\x14:\x15:\x16:\x17\x18\x19jc\x21\x22\x23\x24\x25o:\x26:\x27\x28:\x29:\x2A:\x2B:\x2C:\x2D:\x2E:\x2F\x30"
static int thread_server_port = 0;
static struct option long_options[] = {
/* -t */{"tls", no_argument, 0, OPT_TLS},
/* -d */{"dtls", no_argument, 0, OPT_DTLS},
/* 0x3 */{"server-host", required_argument, 0, OPT_SERVER_HOST},
/* -p */{"server-port", required_argument, 0, OPT_SERVER_PORT},
/* 0x05 */{"src-port", required_argument, 0, OPT_SRC_PORT},
/* 0x06 */{"sendfile", required_argument, 0, OPT_SENDFILE},
/* 0x07 */{"send-ktls-count", required_argument, 0, OPT_SEND_KTLS_COUNT},
/* 0x08 */{"send-gnutls-count", required_argument, 0, OPT_SEND_GNUTLS_COUNT},
/* 0x09 */{"payload", required_argument, 0, OPT_PAYLOAD},
/* 0x0A */{"splice-count", required_argument, 0, OPT_SPLICE_COUNT},
/* -m */{"sendfile-mtu", required_argument, 0, OPT_SENDFILE_MTU},
/* -v */{"verbose", no_argument, 0, OPT_VERBOSE},
/* -h */{"help", no_argument, 0, OPT_HELP},
/* 0x0E */{"sendfile-mmap", required_argument, 0, OPT_SENDFILE_MMAP},
/* 0x0F */{"send-ktls-time", required_argument, 0, OPT_SEND_KTLS_TIME},
/* 0x10 */{"send-gnutls-time", required_argument, 0, OPT_SEND_GNUTLS_TIME},
/* 0x11 */{"splice-time", required_argument, 0, OPT_SPLICE_TIME},
/* 0x12 */{"sendfile-size", required_argument, 0, OPT_SENDFILE_SIZE},
/* 0x13 */{"splice-file", required_argument, 0, OPT_SPLICE_FILE},
/* 0x14 */{"server-store", required_argument, 0, OPT_SERVER_STORE},
/* 0x15 */{"splice-echo-count", required_argument, 0, OPT_SPLICE_ECHO_COUNT},
/* 0x16 */{"splice-echo-time", required_argument, 0, OPT_SPLICE_ECHO_TIME},
/* 0x17 */{"server-ktls", no_argument, 0, OPT_SERVER_KTLS},
/* -j */{"json", no_argument, 0, OPT_JSON},
/* -c */{"drop-caches", no_argument, 0, OPT_DROP_CACHES},
/* 0x21 */{"verify-sendpage", no_argument, 0, OPT_VERIFY_SENDPAGE},
/* 0x22 */{"verify-transmission", no_argument, 0, OPT_VERIFY_TRANSMISSION},
/* 0x23 */{"verify-splice-read", no_argument, 0, OPT_VERIFY_SPLICE_READ},
/* 0x24 */{"verify-handling", no_argument, 0, OPT_VERIFY_HANDLING},
/* -o */{"output", required_argument, 0, OPT_OUTPUT},
/* -o */{"sendfile-user", required_argument, 0, OPT_SENDFILE_USER},
/* 0x27 */{"server-no-echo", no_argument, 0, OPT_SERVER_NO_ECHO},
/* 0x28 */{"server-mtu", required_argument, 0, OPT_SERVER_MTU},
/* 0x29 */{"raw-send-time", required_argument, 0, OPT_RAW_SEND_TIME},
/* 0x2A */{"splice-send-raw-time", required_argument, 0, OPT_SPLICE_SEND_RAW_TIME},
/* 0x2B */{"plain-sendfile", required_argument, 0, OPT_PLAIN_SENDFILE},
/* 0x2C */{"plain-sendfile-user", required_argument, 0, OPT_PLAIN_SENDFILE_USER},
/* 0x2D */{"plain-splice-emu", required_argument, 0, OPT_PLAIN_SPLICE_EMU},
/* 0x2E */{"plain-sendfile-mmap", required_argument, 0, OPT_PLAIN_SENDFILE_MMAP},
/* 0x2F */{"tcp", no_argument, 0, OPT_TCP},
/* 0x30 */{"udp", no_argument, 0, OPT_UDP},
{0, 0, 0, 0}
};
static void print_help(char *progname) {
static const char *help_msg = \
"Usage: %s OPTIONS\n"
"Benchmark Gnu TLS and AL_TLS kernel implementation\n"
"\nOptions:\n\n"
"\t--tls|-t benchmark over TLS protocol\n"
"\t--dtls|-d benchmark over DTLS protocol; the default is TLS\n"
"\t--tcp benchmark over TCP protocol\n"
"\t--udp benchmark over UDP protocol; the default is TCP\n"
"\t--server-host|-h HOST specify destination host; if omitted, server is run in a thread\n"
"\t--server-port|-p PORT specify destination port; if omitted, 5557 is used if server-host specified,\n"
"\t otherwise port for thread server will be assigned\n"
"\t--src-port PORT specify source port to bind to; if omitted, assigned by OS\n"
"\t--payload SIZE specify TLS/DTLS and AF_KTLS packet payload;\n"
"\t applies for:\n"
"\t --splice-{time,count}\n"
"\t --send-gnutls-{time,count}\n"
"\t --send-ktls-{time,count}\n"
"\t--drop-caches|-c drop caches before each test (root needed)\n"
"\n"
"\t--server-store FILE store received content to a file; only if run with thread server\n"
"\t--server-ktls use AF_KTLS on server side as well; only if run with thread server\n"
"\n"
"\t--sendfile FILE perform sendfile(2) using AF_KTLS, send file FILE\n"
"\t--sendfile-mtu SIZE|-m SIZE specify sendfile(2) MTU\n"
"\t--sendfile-mmap FILE mmap(2) file FILE before sendfile(2)\n"
"\t--sendfile-size SIZE specify size of FILE for sendfile(2); otherwise the whole file is sent\n"
"\n"
"\t--send-ktls-count COUNT perform send(2) with zero content using AF_KTLS COUNT times\n"
"\t--send-ktls-time TIME perform send(2) with zero content using AF_KTLS TIME secs\n"
"\t--send-gnutls-count COUNT perform send(2) with zero content using Gnu TLS COUNT times\n"
"\t--send-gnutls-time TIME perform send(2) with zero content using Gnu TLS TIME secs\n"
"\t--raw-send-time TIME send raw to server, server will return encrypted\n"
"\n"
"\t--splice-count COUNT perform splice(2) COUNT times\n"
"\t--splice-time TIME perform splice(2) TIME secs\n"
"\t--splice-file FILE specify file to be used with --splice-{count,time}\n"
"\t--splice-echo-count COUNT perform echo splice(2) with server COUNT times\n"
"\t--splice-echo-time TIME perform echo splice(2) with server TIME secs\n"
"\t--splice-send-raw-time TIME perform splice(2) on AF_KTLS and send raw data\n"
"\n"
"\t--verify-sendpage verify tls_sendpage() kernel implementation\n"
"\t--verify-transmission verify tls_sendmsg(), tls_recvmsg() kernel implementation\n"
"\t--verify-splice-read verify tls_splice_read() kernel implementation\n"
"\t--verify-handling verify tls_setsockopt()/tls_getsockopt() kernel implementation\n"
"\n"
"\t--plain-sendfile FILE send file FILE unencrypted using sendfile(2)\n"
"\t--plain-sendfile-user FILE send file FILE unencrypted using read(2) and send(2)\n"
"\t--plain-splice-emu FILE send file FILE unencrypted using sendfile splice(2) emulation\n"
"\t--plain-sendfile-mmap FILE send file FILE unencrypted using mmap(2), read(2) and send(2)\n"
"\n"
"\t--output|-o set output file\n"
"\t--verbose|-v be verbose like an old lady at marketplace, can be used multiple times\n"
"\t--json|-j output in JSON instead of text\n"
"\t--help|-h print this help\n\n";
assert(progname);
printf(help_msg, progname);
}
static int parse_opts(struct client_opts *opts, int argc, char *argv[]) {
int c;
int idx = 0;
char *tmp_ptr = NULL;
bool protocol_seen = false;
bool no_tls_protocol_seen = false;
// assign default values at first
opts->tls = true;
opts->tcp = true;
opts->server_port = 5557;
opts->src_port = 0;
opts->sendfile = NULL;
opts->send_ktls_count = 0;
opts->send_gnutls_count = 0;
opts->payload_size = 1400;
opts->verbose_level = VERBOSE_LEVEL_SILENT;
opts->sendfile_mtu = 0;
opts->splice_count = 0;
opts->send_ktls_time = 0;
opts->send_gnutls_time = 0;
opts->splice_time = 0;
opts->sendfile_mmap = NULL;
opts->sendfile_size = 0;
opts->server_host = NULL;
opts->server_store = 0;
opts->splice_echo_count = 0;
opts->splice_echo_time = 0;
opts->server_ktls = false;
opts->json = false;
opts->drop_caches = false;
opts->verify = VERIFY_NONE;
opts->output = NULL;
opts->sendfile_user = NULL;
opts->server_no_echo = false;
opts->raw_send_time = 0;
opts->splice_send_raw_time = 0;
opts->plain_sendfile = NULL;
opts->plain_sendfile_user = NULL;
opts->plain_sendfile_mmap = NULL;
opts->plain_splice_emu = NULL;
// we will check for multiple occurrences for these, default values assigned
// later
opts->splice_file = NULL;
opts->server_mtu = 0;
for (;;) {
c = getopt_long (argc, argv, OPT_SHORT_OPTS, long_options, &idx);
if (c == -1) // we are done
break;
switch (c) {
case OPT_TLS:
if (protocol_seen) {
print_error("option '--dtls' is disjoint with --tls");
return 1;
}
if (no_tls_protocol_seen) {
print_error("option --dtls/--tls is disjoint with --udp/--tcp");
return 1;
}
protocol_seen = true;
opts->tls = true;
break;
case OPT_DTLS:
if (protocol_seen) {
print_error("option '--dtls' is disjoint with --tls");
return 1;
}
if (no_tls_protocol_seen) {
print_error("option --dtls/--tls is disjoint with --udp/--tcp");
return 1;
}
protocol_seen = true;
opts->tls = false;
break;
case OPT_TCP:
if (no_tls_protocol_seen && !opts->tcp) {
print_error("option --tcp is disjoint with --udp");
return 1;
}
if (protocol_seen) {
print_error("option --tcp/--udp is disjoint with --tls/--dtls");
return 1;
}
no_tls_protocol_seen = true;
opts->tcp = true;
break;
case OPT_UDP:
if (no_tls_protocol_seen && opts->tcp) {
print_error("option --udp is disjoint with --tcp");
return 1;
}
if (protocol_seen) {
print_error("option --tcp/--udp is disjoint with --tls/--dtls");
return 1;
}
no_tls_protocol_seen = true;
opts->tcp = false;
break;
case OPT_SERVER_HOST:
if (opts->server_host) {
print_error("multiple --server-host supplied");
return -1;
}
opts->server_host = optarg;
break;
case OPT_SERVER_PORT:
opts->server_port = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->server_port > 65535) {
print_error("unknown destination port '%s'", optarg);
return -1;
}
break;
case OPT_SRC_PORT:
opts->src_port = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->src_port > 65535) {
print_error("unknown source port '%s'", optarg);
return -1;
}
break;
case OPT_SENDFILE:
opts->sendfile = optarg;
break;
case OPT_SEND_KTLS_COUNT:
if (opts->send_ktls_count) {
print_error("multiple --send-ktls-count supplied");
return -1;
}
opts->send_ktls_count = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->send_ktls_count == 0) {
print_error("unknown send count '%s'", optarg);
return -1;
}
break;
case OPT_SEND_GNUTLS_COUNT:
if (opts->send_gnutls_count) {
print_error("multiple --send-gnutls-count supplied");
return -1;
}
opts->send_gnutls_count = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->send_gnutls_count == 0) {
print_error("unknown send count '%s'", optarg);
return -1;
}
break;
case OPT_PAYLOAD:
opts->payload_size = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->payload_size == 0) {
print_error("unknown payload size '%s'", optarg);
return -1;
}
break;
case OPT_SPLICE_COUNT:
opts->splice_count = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->splice_count == 0) {
print_error("unknown splice(2) count '%s'", optarg);
return -1;
}
break;
case OPT_SPLICE_SEND_RAW_TIME:
opts->splice_send_raw_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->splice_send_raw_time == 0) {
print_error("unknown splice(2) send raw time '%s'", optarg);
return -1;
}
break;
case OPT_SENDFILE_MTU:
opts->sendfile_mtu = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->sendfile_mtu == 0) {
print_error("unknown sendfile(2) MTU size '%s'", optarg);
return -1;
}
break;
case OPT_VERBOSE:
if (opts->verbose_level < VERBOSE_LEVEL_ALL)
opts->verbose_level++;
break;
case OPT_HELP:
print_help(argv[0]);
return -1;
break;
case OPT_SENDFILE_MMAP:
opts->sendfile_mmap = optarg;
break;
case OPT_SEND_KTLS_TIME:
if (opts->send_ktls_time) {
print_error("multiple --send-ktls-time supplied");
return -1;
}
opts->send_ktls_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->send_ktls_time == 0) {
print_error("unknown send time '%s'", optarg);
return -1;
}
break;
case OPT_SEND_GNUTLS_TIME:
if (opts->send_gnutls_time) {
print_error("multiple --send-gnutls-time supplied");
return -1;
}
opts->send_gnutls_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->send_gnutls_time == 0) {
print_error("unknown send time '%s'", optarg);
return -1;
}
break;
case OPT_SPLICE_TIME:
if (opts->splice_time) {
print_error("multiple --send-splice-time supplied");
return -1;
}
opts->splice_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->splice_time == 0) {
print_error("unknown send time '%s'", optarg);
return -1;
}
break;
case OPT_SENDFILE_SIZE:
if (opts->sendfile_size) {
print_error("multiple --sendfile-size supplied");
return -1;
}
opts->sendfile_size = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->sendfile_size == 0) {
print_error("unknown size of a file '%s'", optarg);
return -1;
}
break;
case OPT_SPLICE_FILE:
if (opts->splice_file) {
print_error("multiple --splice-file supplied");
return -1;
}
opts->splice_file = optarg;
break;
case OPT_SERVER_STORE:
if (opts->server_store) {
print_error("multiple --server-store supplied");
return -1;
}
opts->server_store = open(optarg, O_WRONLY|O_CREAT|O_TRUNC);
if (opts->server_store < 0) {
perror(optarg);
return -1;
}
break;
case OPT_SPLICE_ECHO_COUNT:
if (opts->splice_echo_count) {
print_error("multiple --splice-echo-count supplied");
return -1;
}
opts->splice_echo_count = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->splice_echo_count == 0) {
print_error("unknown count '%s'", optarg);
return -1;
}
break;
case OPT_SPLICE_ECHO_TIME:
if (opts->splice_echo_time) {
print_error("multiple --splice-echo-time supplied");
return -1;
}
opts->splice_echo_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->splice_echo_time == 0) {
print_error("unknown time '%s'", optarg);
return -1;
}
break;
case OPT_RAW_SEND_TIME:
if (opts->raw_send_time) {
print_error("multiple --raw-send-send supplied");
return -1;
}
opts->raw_send_time = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->raw_send_time == 0) {
print_error("unknown raw send time '%s'", optarg);
return -1;
}
break;
case OPT_PLAIN_SENDFILE:
if (opts->plain_sendfile) {
print_error("multiple --plain-sendfile supplied");
return -1;
}
opts->plain_sendfile = optarg;
break;
case OPT_PLAIN_SENDFILE_USER:
if (opts->plain_sendfile_user) {
print_error("multiple --plain-sendfile-user supplied");
return -1;
}
opts->plain_sendfile_user = optarg;
break;
case OPT_PLAIN_SPLICE_EMU:
if (opts->plain_splice_emu) {
print_error("multiple --plain-splice-emu supplied");
return -1;
}
opts->plain_splice_emu = optarg;
break;
case OPT_PLAIN_SENDFILE_MMAP:
if (opts->plain_sendfile_mmap) {
print_error("multiple --plain-sendfile-mmap supplied");
return -1;
}
opts->plain_sendfile_mmap = optarg;
break;
case OPT_SERVER_NO_ECHO:
opts->server_no_echo = true;
break;
case OPT_SERVER_KTLS:
opts->server_ktls = true;
break;
case OPT_JSON:
opts->json = true;
break;
case OPT_DROP_CACHES:
opts->drop_caches = true;
break;
case OPT_VERIFY_SENDPAGE:
opts->verify |= VERIFY_SENDPAGE;
break;
case OPT_VERIFY_TRANSMISSION:
opts->verify |= VERIFY_TRANSMISSION;
break;
case OPT_VERIFY_SPLICE_READ:
opts->verify |= VERIFY_SPLICE_READ;
break;
case OPT_VERIFY_HANDLING:
opts->verify |= VERIFY_HANDLING;
break;
case OPT_SENDFILE_USER:
if (opts->sendfile_user) {
print_error("multiple --sendfile-user supplied");
}
opts->sendfile_user = optarg;
break;
case OPT_OUTPUT:
if (opts->output) {
print_error("multiple --server-store supplied");
return -1;
}
opts->output = optarg;
break;
case '?':
print_help(argv[0]);
return -1;
break;
default:
/* should be unreachable */
print_error("Unhandled parameter");
assert(false);
}
}
/* no additional arguments allowed */
if (optind < argc) {
print_error("unknown argument supplied: %s", argv[optind]);
print_help(argv[0]);
return -1;
}
if (!opts->sendfile &&
!opts->send_gnutls_count &&
!opts->send_ktls_count &&
!opts->splice_count &&
!opts->send_gnutls_time &&
!opts->send_ktls_time &&
!opts->splice_echo_count &&
!opts->splice_echo_time &&
!opts->splice_time &&
!opts->sendfile_mmap &&
!opts->sendfile_user &&
!opts->raw_send_time &&
!opts->plain_sendfile &&
!opts->plain_sendfile_user &&
!opts->plain_sendfile_mmap &&
!opts->plain_splice_emu &&
!opts->splice_send_raw_time) {
if (!opts->verify) {
print_error("specify at least one benchamrking or verification option");
return -1;
}
if (opts->output) {
print_error("output file stores results of benchmarks, nothing to store");
return -1;
}
} else if (opts->verify) {
print_error("to verify implementation, run only verification without benchnarking options");
return -1;
}
if (opts->sendfile ||
opts->send_gnutls_count ||
opts->send_ktls_count ||
opts->splice_count ||
opts->send_gnutls_time ||
opts->send_ktls_time ||
opts->splice_echo_count ||
opts->splice_echo_time ||
opts->splice_time ||
opts->sendfile_mmap ||
opts->sendfile_user ||
opts->verify) {
if (opts->raw_send_time || opts->splice_send_raw_time) {
print_error("raw send tests can be run only as a standalone benchmark");
return -1;
}
if (opts->plain_sendfile ||
opts->plain_sendfile_mmap ||
opts->plain_sendfile_user ||
opts->plain_splice_emu) {
print_error("plain tests can be run only as a standalone benchmark");
return -1;
}
}
if (opts->verify && (opts->verify & (opts->verify - 1))) {
print_error("only one verification allowed per run");
return -1;
}
if (opts->sendfile_user && !opts->sendfile_mtu) {
print_error("--sendfile-user requires MTU specified with --sendfile-mtu");
return -1;
}
if (opts->sendfile_mmap && !opts->sendfile_mtu) {
print_error("--sendfile-mmap requires MTU specified with --sendfile-mtu");
return -1;
}
if (!opts->sendfile_mtu &&
(opts->plain_sendfile ||
opts->plain_sendfile_mmap ||
opts->plain_splice_emu ||
opts->plain_sendfile_user)) {
print_error("--sendfile-mtu required");
return -1;
}
if (opts->sendfile_mtu &&
!opts->sendfile &&
!opts->sendfile_user &&
!opts->splice_echo_count &&
!opts->splice_echo_time &&
!opts->plain_sendfile &&
!opts->plain_sendfile_mmap &&
!opts->plain_sendfile_user &&
!opts->splice_send_raw_time &&
!opts->plain_splice_emu) {
print_error("invalid use of --sendfile-mtu");
return -1;
}
if (!opts->splice_count && !opts->splice_time && opts->splice_file) {
print_error("--splice-file can be used only with --splice-{time,count}");
return -1;
}
if (opts->server_host && opts->server_ktls) {
print_error("--server-ktls can be used only with threaded server (no --server-host)");
return -1;
}
if (opts->server_host && opts->server_mtu) {
print_error("--server-mtu can be used only with threaded server (no --server-host)");
return -1;
} else {
opts->server_mtu = SERVER_MAX_MTU;
}
if (opts->server_host && opts->server_store) {
print_error("--server-store can be used only with threaded server (no --server-host)");
return -1;
}
if (opts->server_no_echo && opts->server_host) {
print_error("--server-no-echo can be used only with threaded server");
return -1;
}
if (opts->server_no_echo && (opts->splice_echo_count || opts->splice_echo_time)) {
print_error("--server-no-echo is not acceptable when a response from the server is needed"
" (--splice-echo-{time,count}");
return -1;
}
#ifdef BENCHMARK_RECV
if (opts->server_no_echo
&& (opts->send_gnutls_time || opts->send_gnutls_count ||
opts->send_ktls_time || opts->send_ktls_count)) {
print_error("cannot use --server-no-echo with send-{gnutls,ktls}-{count,time}"
" (or BENCHMARK_RECV should not be set");
return -1;
}
#endif
/*
* we will perform splice(2) on /dev/zero by default in order to avoid the
* impact of disk drive and FS
*/
if (opts->splice_count || opts->splice_time)
if (!opts->splice_file)
opts->splice_file = "/dev/zero";
return 0;
}
static void print_opts(const struct client_opts *opts) {
print_debug_client(opts, "protocol: %s", opts->tls ? "TLS" : "DTLS");
if (!opts->server_host) {
print_debug_client(opts, "server: thread server");
print_debug_client(opts, "server uses AF_KTLS: %s", opts->server_ktls ? "true" : "false");
print_debug_client(opts, "server lib: Gnu TLS");
print_debug_client(opts, "server mtu: %u", opts->server_mtu);
} else
print_debug_client(opts, "destination host: %s", opts->server_host);
print_debug_client(opts, "drop caches: %s", opts->drop_caches ? "true" : "false");
print_debug_client(opts, "destination port: %u", opts->server_port);
print_debug_client(opts, "source port: %u", opts->src_port);
if (opts->verify & VERIFY_SENDPAGE) {
print_debug_client(opts, "verifying tls_sendpage() in kernel");
return;
}
if (opts->verify & VERIFY_TRANSMISSION) {
print_debug_client(opts, "verifying tls_sendmsg()/tls_recvmsg() in kernel");
return;
}
if (opts->verify & VERIFY_SPLICE_READ) {
print_debug_client(opts, "verifying tls_splice_read() in kernel");
return;
}
print_debug_client(opts, "packet payload: %u", opts->payload_size);
if (opts->sendfile || opts->sendfile_user) {
if (opts->sendfile_mtu)
print_debug_client(opts, "sendfile(2) MTU: %u", opts->sendfile_mtu);
else
print_debug_client(opts, "sendfile(2) MTU: max");
}
if (opts->sendfile_mmap)
print_debug_client(opts, "mmap(2) send file %s", opts->sendfile_mmap);
print_debug_client(opts, "output type: %s", opts->json ? "JSON" : "text");
if (opts->raw_send_time)
print_debug_client(opts, "raw send time: %d", opts->raw_send_time);
if (opts->plain_sendfile)
print_debug_client(opts, "plain sendfile: %s", opts->plain_sendfile);
if (opts->plain_sendfile_user)
print_debug_client(opts, "plain user send file: %s", opts->plain_sendfile_user);
if (opts->plain_splice_emu)
print_debug_client(opts, "plain send file emulation using splice: %s", opts->plain_splice_emu);
if (opts->plain_sendfile_mmap)
print_debug_client(opts, "plain send file mmap(2): %s", opts->plain_splice_emu);
if (opts->raw_send_time)
print_debug_client(opts, "raw splice send raw time: %u", opts->splice_send_raw_time);
if (opts->server_store)
print_debug_client(opts, "server store file (fd): '%s'", opts->server_store);
if (opts->sendfile)
print_debug_client(opts, "using sendfile(2) on file '%s'", opts->sendfile);
if (opts->sendfile_user)
print_debug_client(opts, "sending file '%s'", opts->sendfile_user);
if (opts->send_ktls_count)
print_debug_client(opts, "using send(2) on AF_KTLS socket %u times", opts->send_ktls_count);
if (opts->send_ktls_time)
print_debug_client(opts, "using send(2) on AF_KTLS socket %u secs", opts->send_ktls_time);
if (opts->send_gnutls_count)
print_debug_client(opts, "using gnutls_record_send() %u times", opts->send_gnutls_count);
if (opts->send_gnutls_time)
print_debug_client(opts, "using gnutls_record_send() %u secs", opts->send_gnutls_time);
if (opts->splice_file)
print_debug_client(opts, "using splice(2) on file '%s'", opts->splice_file);
if (opts->splice_count)
print_debug_client(opts, "using splice(2) %u times", opts->splice_count);
if (opts->splice_time)
print_debug_client(opts, "using splice(2) %u secs", opts->splice_time);
if (opts->splice_echo_time)
print_debug_client(opts, "using splice(2) echo %u secs", opts->splice_echo_time);
if (opts->splice_echo_count)
print_debug_client(opts, "using splice(2) echo %u times", opts->splice_echo_count);
if (opts->server_no_echo)
print_debug_client(opts, "server is not echoing data messages", opts->splice_echo_count);
}
extern int do_drop_caches(void) {
int fd;
int ret;
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
if (fd < 0) {
perror("open:failed to drop caches");
return fd;
}
ret = write(fd, "3", 1);
if (ret < 0) {
perror("write:failed to drop caches");
return ret;
}
ret = close(fd);
if (ret < 0)
perror("close:failed to drop caches");
return ret;
}
static int do_plain_action(const struct client_opts *opts, int sd) {
int err;
if (opts->plain_sendfile) {
err = do_plain_sendfile(opts, sd);
if (err < 0) {
print_error("failed to do plain sendfile");
goto out;
}
DO_DROP_CACHES(opts);
}
if (opts->plain_sendfile_user) {
err = do_plain_sendfile_user(opts, sd);
if (err < 0) {
print_error("failed to do plain sendfile");
goto out;
}
DO_DROP_CACHES(opts);
}
if (opts->plain_sendfile_mmap) {
err = do_plain_sendfile_mmap(opts, sd);
if (err < 0) {
print_error("failed to do plain send file with mmap(2)");
goto out;
}
DO_DROP_CACHES(opts);
}
if (opts->plain_splice_emu) {
err = do_plain_splice_emu(opts, sd);
if (err < 0) {
print_error("failed to do splice(2) sendfile emulation");
goto out;
}
DO_DROP_CACHES(opts);
}
out:
return err;
}
static int do_action(const struct client_opts *opts, gnutls_session_t session, int udp_sd) {
int ksd = 0;
int err;
char *mem = NULL; //just a bunch of zeroed memory used as a source data
if (opts->send_ktls_count || opts->send_gnutls_count ||
opts->send_ktls_time || opts->send_gnutls_time ||
opts->splice_echo_time || opts->splice_echo_count ||
opts->raw_send_time || opts->splice_send_raw_time) {
err = posix_memalign((void **) &mem, 16, opts->payload_size);
memset(mem, 0, opts->payload_size);
if (err) {
perror("posix_memalign");
return err;
}
}
DO_DROP_CACHES(opts); // drop before first run to be fair
if (opts->send_gnutls_count) {
err = do_gnutls_send_count(opts, session, mem);
if (err < 0) {
print_error("failed to do Gnu TLS gnutls_record_send()");
goto action_error;
}
DO_DROP_CACHES(opts);
}
if (opts->send_gnutls_time) {
err = do_gnutls_send_time(opts, session, mem);
if (err < 0) {
print_error("failed to do Gnu TLS gnutls_record_send()");
goto action_error;
}
DO_DROP_CACHES(opts);
}
if (opts->sendfile_user) {
err = do_sendfile_user(opts, session);
if (err < 0) {
print_error("failed to do Gnu TLS send file with user space buffer");
goto action_error;
}
DO_DROP_CACHES(opts);
}
if (opts->sendfile_mmap) {
err = do_sendfile_mmap(opts, session);
if (err < 0) {
print_error("failed to do Gnu TLS send file with mmap(2)");
goto action_error;
}
DO_DROP_CACHES(opts);
}
if (opts->raw_send_time) {
err = do_raw_send_time(opts, session, udp_sd, mem);
if (err < 0) {
print_error("failed to do raw send");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->send_ktls_count || opts->sendfile || opts->splice_count
|| opts->send_ktls_time || opts->send_gnutls_time || opts->splice_time
|| opts->splice_echo_count || opts->splice_echo_time || opts->verify
|| opts->splice_send_raw_time) {
ksd = ktls_socket_init(session, udp_sd, opts->sendfile_mtu, opts->tls);
if (ksd < 0) {
print_error("failed to get AF_KTLS socket");
goto action_error;
}
DO_DROP_CACHES(opts);
}
if (opts->splice_send_raw_time) {
err = do_splice_send_raw_time(opts, udp_sd, ksd, mem);
if (err < 0) {
print_error("failed to do splice raw send");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->splice_count) {
err = do_splice_count(opts, ksd);
if (err < 0) {
print_error("failed to do splice(2)");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->splice_time) {
err = do_splice_time(opts, ksd);
if (err < 0) {
print_error("failed to do splice(2) time");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->splice_echo_time) {
err = do_splice_echo_time(opts, ksd, mem);
if (err < 0) {
print_error("failed to do splice(2) echo time");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->splice_echo_count) {
err = do_splice_echo_count(opts, ksd, mem);
if (err < 0) {
print_error("failed to do splice(2) echo count");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->sendfile) {
err = do_sendfile(opts, ksd);
if (err < 0) {
print_error("failed to do sendfile(2)");
goto action_error_ksd;
}
DO_DROP_CACHES(opts);
}
if (opts->send_ktls_count) {
err = do_send_count(opts, ksd, mem, session, 0);
if (err < 0) {
print_error("failed to do AF_ALG send() count");
goto action_error;
}
DO_DROP_CACHES(opts);
}