-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtcp-active-syn-ack-time
executable file
·113 lines (86 loc) · 2.33 KB
/
tcp-active-syn-ack-time
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
#! /usr/bin/env perl
# Copyright (C) YangBingwu (detailyang)
use 5.006001;
use strict;
use warnings;
use Getopt::Std qw( getopts );
sub usage();
my %opts;
getopts("ht:p:d", \%opts) or die usage();
if ($opts{h}) {
print usage();
exit;
}
my $stap_args = $opts{a} || '';
my $port = $opts{p} or die "No Port Specified by the -p option\n";
if ($port !~ /^\d+$/) {
die "Bad -p options value \"$port\": not look like a port"
}
my $time = $opts{t} || 5000;
if ($time !~ /^\d+$/) {
die "Bad -t options value \"$time\": not look like time"
}
my $preamble = <<_EOC_;
probe begin {
printf("Collecting tcp dport ($port)...syn-ack time\\n\\n")
}
_EOC_
chop $preamble;
my $stap_src = <<_EOC_;
$preamble
global collect%, intervals%
probe kernel.function("tcp_transmit_skb") {
t = gettimeofday_us()
tcp_state = tcp_ts_get_info_state(\$sk)
tcp_state_str = tcp_sockstate_str(tcp_state)
saddr = format_ipaddr(__ip_sock_saddr(\$sk), %{AF_INET%})
daddr = format_ipaddr(__ip_sock_daddr(\$sk), %{AF_INET%})
sport = __tcp_sock_sport(\$sk)
dport = __tcp_sock_dport(\$sk)
if (dport == $port) {
if (tcp_state_str == "TCP_SYN_SENT") {
collect[saddr, sport, daddr, dport] = t
}
if (tcp_state_str == "TCP_ESTABLISHED") {
syn_sent_time = collect[saddr, sport, daddr, dport]
if (syn_sent_time) {
intervals <<< t - syn_sent_time
}
delete collect[saddr, sport, daddr, dport]
}
}
}
probe timer.ms($time) {
if (\@count(intervals) > 0) {
printf("dport:$port min:%dus, max:%dus avg:%dus, cnt:%d\\r\\n",
\@min(intervals), \@max(intervals), \@avg(intervals), \@count(intervals)
)
print(\@hist_log(intervals))
} else {
printf("none\\r\\n")
}
exit()
}
_EOC_
if ($opts{d}) {
print $stap_src;
exit;
}
open my $in, "|stap -g --skip-badvars $stap_args -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage() {
return <<'_EOC_';
Usage:
tcp-active-syn-ack-time [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-t <time>(ms) Time(ms) to execute.
-d Dump out the systemtap script source.
-h Print this usage.
-p <port> Specify the tcp dport.
Examples:
tcp-active-syn-ack-time -p 80 -t 5000
_EOC_
}