-
Notifications
You must be signed in to change notification settings - Fork 5
/
netbench.pl
executable file
·421 lines (375 loc) · 12.2 KB
/
netbench.pl
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
#!/usr/bin/perl
# Copyright (c) 2022-2024 Alexander Bluhm <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
use Cwd;
use Errno;
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
use File::Basename;
use Getopt::Std;
use List::Util qw(sum);
my @alltestmodes = qw(
tcpbench tcpsplice tcpcopy
udpbench udpsplice udpcopy
);
my %opts;
getopts('A:a:B:b:C:c:d:f:i:l:m:N:P:s:t:v', \%opts) or do {
print STDERR <<"EOF";
usage: netbench.pl [-v] [-A address] -a address [-B bitrate] [-b bufsize]
[-C pseudo] [-c client] [-d delay] [-f frames] [-i idle] [-l length]
[-m mmsglen] [-P packetrate] [-p pseudo] [-s server] [-t timeout]
[test ...]
-A address IP address of relay
-a address IP address for packet destination
-B bitrate bits per seconds send rate
-b bufsize set size of send and receive buffer
-C pseudo pseudo network device changes packet length
-c client connect via ssh to start packet generator
-d delay wait for setup before sending
-f frames calculate udp payload to fragment packet into frames
-i idle idle timeout before receiving stops
-m mmsglen number of mmsghdr for sendmmsg or recvmmsg
-N repeat run instances in parallel with incremented address
-P packet packets per seconds send rate
-l length set length of udp payload
-s sever connect via ssh to start packet consumer
-t timeout send duration and receive timeout, default 1
-v verbose
test ... test mode: @alltestmodes
EOF
exit(2);
};
my $addr = $opts{a}
or die "IP address required";
$addr =~ /^([0-9]+\.[0-9.]+|[0-9a-fA-F:]+)$/
or die "Address must be IPv4 or IPv6";
my $relay_addr = $opts{A};
!$relay_addr || $relay_addr =~ /^([0-9]+\.[0-9.]+|[0-9a-fA-F:]+)$/
or die "Relay address must be IPv4 or IPv6";
my $client_ssh = $opts{c};
my $server_ssh = $opts{s};
my $pseudo = $opts{C};
@ARGV or die "No test mode specified";
my %testmode;
foreach my $mode (@ARGV) {
grep { $_ eq $mode } @alltestmodes
or die "Unknown test mode '$mode'";
$testmode{$mode} = 1;
}
foreach my $mode (@alltestmodes) {
die "Test mode '$mode' must be used solely"
if $testmode{$mode} && keys %testmode != 1;
}
$testmode{tcp} = 1
if $testmode{tcpbench} || $testmode{tcpsplice} || $testmode{tcpcopy};
$testmode{udp} = 1
if $testmode{udpbench} || $testmode{udpsplice} || $testmode{udpcopy};
$testmode{splice} = 1
if $testmode{tcpsplice} || $testmode{udpsplice};
$testmode{copy} = 1
if $testmode{tcpcopy} || $testmode{udpcopy};
my $dir = dirname($0);
chdir($dir)
or die "Change directory to '$dir' failed: $!";
my $netbenchdir = getcwd();
$| = 1;
my $paylen = $opts{l};
if (defined($opts{f})) {
!defined($paylen)
or die "Use either -f frames or -l lenght";
$opts{f} =~ /\d+$/
or die "Frames must be number";
my $mtu = 1500;
if ($pseudo) {
if ($pseudo eq "gif") {
$mtu = 1480;
} elsif ($pseudo eq "gif6") {
$mtu = 1460;
} elsif ($pseudo eq "gre") {
$mtu = 1472;
}
}
if ($addr =~ /:/) {
if ($opts{f} <= 1) {
# ether frame minus ip6 header
$paylen = ($mtu - 40) * $opts{f};
} else {
# ether frame minus ip6 header minus fragment header minus round
$paylen = ($mtu - 40 - 8 - 4) * $opts{f};
}
} else {
# ether frame minus ip header
$paylen = ($mtu - 20) * $opts{f};
}
# minus udp header
$paylen = $paylen < 8 ? 0 : $paylen - 8;
if ($addr =~ /:/) {
# maximux ip6 payload length
$paylen = 2**16 - 1 - 8 if $paylen > 2**16 - 1 - 8;
} else {
# maximum ip packet length
$paylen = 2**16 - 1 - 20 - 8 if $paylen > 2**16 - 1 - 20 - 8;
}
}
my (@servers, @clients, @relays);
my %server = (
name => "server",
ssh => $server_ssh,
addr => $addr,
);
start_server_tcp(\%server) if $testmode{tcp};
start_server_udp(\%server) if $testmode{udp};
my (%relay, $client_addr, $client_port);
if ($testmode{splice} || $testmode{copy}) {
%relay = (
name => "relay",
listen => $relay_addr,
connect => $server{addr},
port => $server{port},
);
start_relay(\%relay);
($client_addr, $client_port) = (@relay{qw(addr port)});
} else {
($client_addr, $client_port) = (@server{qw(addr port)});
}
my %client = (
name => "client",
ssh => $client_ssh,
addr => $client_addr,
port => $client_port,
);
push @servers, \%server;
push @relays, \%relay if %relay;
push @clients, \%client;
start_client_tcp(\%client) if $testmode{tcp};
start_client_udp(\%client) if $testmode{udp};
set_nonblock($_) foreach (@clients, @relays, @servers);
collect_output(@clients, @relays, @servers);
print_status_tcp() if $testmode{tcp};
print_status_udp() if $testmode{udp};
# timeout always fails with error
if ($testmode{tcp}) {
delete $_->{status} foreach (@servers);
}
my @failed = map { $_->{status} ? $_->{name} : () }
(@clients, @relays, @servers);
die "Process '@failed' failed" if @failed;
exit;
sub start_server_tcp {
my ($proc) = @_;
$proc->{port} = "12345";
my $timeout = 3;
$timeout += $opts{t} if defined($opts{t});
my @cmd = ('tcpbench', '-s');
unshift @cmd, ('timeout', $timeout) if $opts{t};
push @cmd, "-b$proc->{addr}";
push @cmd, "-p$proc->{port}";
push @cmd, "-S$opts{b}" if defined($opts{b});
unshift @cmd, ('ssh', '-nT', $proc->{ssh}) if $proc->{ssh};
$proc->{cmd} = \@cmd;
$proc->{conn} = "recv";
open_pipe($proc);
}
sub start_client_tcp {
my ($proc) = @_;
my @cmd = ('tcpbench');
push @cmd, "-n$opts{N}" if defined($opts{N});
push @cmd, "-p$proc->{port}";
push @cmd, "-S$opts{b}" if defined($opts{b});
push @cmd, "-t$opts{t}" if defined($opts{t});
push @cmd, $proc->{addr};
unshift @cmd, ('ssh', '-nT', $proc->{ssh}) if $proc->{ssh};
$proc->{cmd} = \@cmd;
$proc->{conn} = "send";
open_pipe($proc);
}
sub start_server_udp {
my ($proc) = @_;
my $timeout = 3;
$timeout += $opts{d} if defined($opts{d});
$timeout += $opts{t} if defined($opts{t});
my @cmd = ('udpbench');
push @cmd, "-B$opts{B}" if defined($opts{B});
push @cmd, "-b$opts{b}" if defined($opts{b});
push @cmd, "-C$opts{C}" if defined($opts{C});
push @cmd, "-d$opts{d}" if defined($opts{d});
push @cmd, "-i$opts{i}" if defined($opts{i});
push @cmd, "-l$paylen" if defined($paylen);
push @cmd, "-m$opts{m}" if defined($opts{m});
push @cmd, "-N$opts{N}" if defined($opts{N});
push @cmd, "-P$opts{P}" if defined($opts{P});
push @cmd, '-p0';
push @cmd, "-t$timeout" if defined($opts{t});
push @cmd, ('recv', $proc->{addr});
unshift @cmd, ('ssh', '-nT', $proc->{ssh}) if $proc->{ssh};
$proc->{cmd} = \@cmd;
open_pipe($proc, "sockname", $opts{N});
}
sub start_client_udp {
my ($proc) = @_;
my @cmd = ('udpbench');
push @cmd, "-B$opts{B}" if defined($opts{B});
push @cmd, "-b$opts{b}" if defined($opts{b});
push @cmd, "-C$opts{C}" if defined($opts{C});
push @cmd, "-d$opts{d}" if defined($opts{d});
push @cmd, "-i$opts{i}" if defined($opts{i});
push @cmd, "-l$paylen" if defined($paylen);
push @cmd, "-m$opts{m}" if defined($opts{m});
push @cmd, "-N$opts{N}" if defined($opts{N});
push @cmd, "-P$opts{P}" if defined($opts{P});
push @cmd, "-p$proc->{port}";
push @cmd, "-t$opts{t}" if defined($opts{t});
push @cmd, ('send', $proc->{addr});
unshift @cmd, ('ssh', '-nT', $proc->{ssh}) if $proc->{ssh};
$proc->{cmd} = \@cmd;
open_pipe($proc, "sockname", $opts{N});
}
sub start_relay {
my ($proc) = @_;
my $timeout = 2;
$timeout += $opts{d} if defined($opts{d});
$timeout += $opts{t} if defined($opts{t});
my @cmd = ('splicebench');
push @cmd, '-c' if $testmode{copy};
push @cmd, '-u' if $testmode{udp};
push @cmd, "-b$opts{b}" if defined($opts{b});
push @cmd, "-i$opts{i}" if defined($opts{i});
push @cmd, "-N$opts{N}" if defined($opts{N}) && $testmode{udp};
push @cmd, "-n$opts{N}" if defined($opts{N}) && $testmode{tcp};
push @cmd, "-t$timeout" if defined($opts{t});
push @cmd, "[$proc->{listen}]:0";
push @cmd, "[$proc->{connect}]:$proc->{port}";
unshift @cmd, ('ssh', '-nT', $proc->{ssh}) if $proc->{ssh};
$proc->{cmd} = \@cmd;
open_pipe($proc, "listen sockname", $testmode{udp} && $opts{N});
}
sub open_pipe {
my ($proc, $sockname, $num) = @_;
print "command: @{$proc->{cmd}}\n" if $opts{v};
$proc->{pid} = open(my $fh, '-|', @{$proc->{cmd}})
or die "Open pipe from proc $proc->{name} '@{$proc->{cmd}}' failed: $!";
$proc->{fh} = $fh;
local $_;
while (<$fh>) {
print if $opts{v};
last unless $sockname;
if (/^$sockname: ([0-9.a-fA-F:]+) ([0-9]+)/) {
$proc->{addr} ||= $1;
$proc->{port} = $2;
last unless $num && --$num;
}
}
unless (defined) {
close($fh) or die $! ?
"Close pipe from proc $proc->{name} '@{$proc->{cmd}}' failed: $!" :
"Proc $proc->{name} Client '@{$proc->{cmd}}' failed: $?";
delete $proc->{fh};
delete $proc->{pid};
}
}
sub set_nonblock {
my ($proc) = @_;
my $flags = fcntl($proc->{fh}, F_GETFL, 0)
or die "Proc $proc->{name} fcntl F_GETFL failed: $!\n";
fcntl($proc->{fh}, F_SETFL, $flags | O_NONBLOCK)
or die "Proc $proc->{name} fcntl F_SETFL O_NONBLOCK failed: $!\n";
}
sub collect_output {
my @procs = @_;
while (my @fhs = map { $_->{fh} || () } @procs) {
if ($testmode{tcp}) {
my @pids = map { $_->{pid} || () } @clients;
unless (@pids) {
@pids = map { $_->{pid} || () } @servers;
kill 'TERM', @pids;
}
}
my $rin = '';
vec($rin, fileno($_), 1) = 1 foreach @fhs;
my $nfound = select(my $rout = $rin, undef, undef, undef);
defined($nfound)
or die "Select failed: $!";
$nfound
or die "Select timeout";
read_output($_, $rout) foreach @procs;
}
}
my %status;
sub read_output {
my ($proc, $rout) = @_;
$proc->{fh} && vec($rout, fileno($proc->{fh}), 1)
or return;
undef $!;
local $_;
while (readline($proc->{fh})) {
print if $opts{v};
# handle short reads
$_ = $proc->{prev}. $_ if defined($proc->{prev});
$proc->{prev} = /\n$/ ? undef : $_;
print if !$opts{v} && /^(send|recv|Conn):.*\n/;
if (/^(send|recv):.*
\spackets\s([0-9]+),.*\sether\s([0-9]+),.*
\sbegin\s([0-9.]+),.*\send\s([0-9.]+),.*/x) {
$status{$1}{etherlen} = ($status{$1}{etherlen} || 0) + $2 * $3;
$status{$1}{begin} = $4
if !$status{$1}{begin} || $4 < $status{$1}{begin};
$status{$1}{end} = $5
if !$status{$1}{end} || $status{$1}{end} < $5;;
}
if (/^Conn:\s+\d+\s+([kmgt]?)bps:\s+([\d.]+)\s/i) {
my $value = $2;
my $unit = lc($1);
if ($unit eq '') {
} elsif ($unit eq 'k') {
$value *= 1000;
} elsif ($unit eq 'm') {
$value *= 1000*1000;
} elsif ($unit eq 'g') {
$value *= 1000*1000*1000;
} elsif ($unit eq 't') {
$value *= 1000*1000*1000*1000;
} else {
die "Unit '$1' unknown";
}
push @{$status{$proc->{conn}}{bits} ||= []}, $value;
}
}
unless ($!{EWOULDBLOCK}) {
unless (close($proc->{fh})) {
die "Close pipe from proc $proc->{name} '@{$proc->{cmd}}' ".
"failed: $!" if $!;
warn "Proc $proc->{name} '@{$proc->{cmd}}' failed: $?";
$proc->{status} = $?;
}
delete $proc->{fh};
delete $proc->{pid};
}
}
sub print_status_tcp {
foreach (qw(send recv)) {
my @values = @{$status{$_}{bits}};
printf("%sall: bit/s %g\n",
$_, sum(@values) / scalar(@values));
}
}
sub print_status_udp {
foreach (qw(send recv)) {
printf("%sall: etherlen %d, begin %f, end %f, duration %f, bit/s %g\n",
$_, $status{$_}{etherlen}, $status{$_}{begin}, $status{$_}{end},
$status{$_}{end} - $status{$_}{begin},
$status{$_}{etherlen} * 8 /
($status{$_}{end} - $status{$_}{begin}));
}
}