Commit | Line | Data |
---|---|---|
a42e9d6c SH |
1 | /* |
2 | * tcpprobe - Observe the TCP flow with kprobes. | |
3 | * | |
4 | * The idea for this came from Werner Almesberger's umlsim | |
5 | * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
662ad4f8 | 9 | * the Free Software Foundation; either version 2 of the License. |
a42e9d6c SH |
10 | * |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
afd46503 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
a42e9d6c SH |
23 | #include <linux/kernel.h> |
24 | #include <linux/kprobes.h> | |
25 | #include <linux/socket.h> | |
26 | #include <linux/tcp.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
a42e9d6c SH |
28 | #include <linux/proc_fs.h> |
29 | #include <linux/module.h> | |
85795d64 SH |
30 | #include <linux/ktime.h> |
31 | #include <linux/time.h> | |
457c4cbc | 32 | #include <net/net_namespace.h> |
a42e9d6c SH |
33 | |
34 | #include <net/tcp.h> | |
35 | ||
65ebe634 | 36 | MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>"); |
a42e9d6c SH |
37 | MODULE_DESCRIPTION("TCP cwnd snooper"); |
38 | MODULE_LICENSE("GPL"); | |
662ad4f8 | 39 | MODULE_VERSION("1.1"); |
a42e9d6c | 40 | |
85795d64 | 41 | static int port __read_mostly = 0; |
a42e9d6c SH |
42 | MODULE_PARM_DESC(port, "Port to match (0=all)"); |
43 | module_param(port, int, 0); | |
44 | ||
f81074f8 | 45 | static unsigned int bufsize __read_mostly = 4096; |
662ad4f8 | 46 | MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)"); |
f81074f8 | 47 | module_param(bufsize, uint, 0); |
a42e9d6c | 48 | |
85795d64 SH |
49 | static int full __read_mostly; |
50 | MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)"); | |
51 | module_param(full, int, 0); | |
52 | ||
a42e9d6c SH |
53 | static const char procname[] = "tcpprobe"; |
54 | ||
662ad4f8 SH |
55 | struct tcp_log { |
56 | ktime_t tstamp; | |
f925d0a6 DB |
57 | union { |
58 | struct sockaddr raw; | |
59 | struct sockaddr_in v4; | |
60 | struct sockaddr_in6 v6; | |
61 | } src, dst; | |
662ad4f8 SH |
62 | u16 length; |
63 | u32 snd_nxt; | |
64 | u32 snd_una; | |
65 | u32 snd_wnd; | |
b4c1c1d0 | 66 | u32 rcv_wnd; |
662ad4f8 SH |
67 | u32 snd_cwnd; |
68 | u32 ssthresh; | |
69 | u32 srtt; | |
70 | }; | |
71 | ||
72 | static struct { | |
85795d64 | 73 | spinlock_t lock; |
a42e9d6c | 74 | wait_queue_head_t wait; |
85795d64 SH |
75 | ktime_t start; |
76 | u32 lastcwnd; | |
a42e9d6c | 77 | |
662ad4f8 SH |
78 | unsigned long head, tail; |
79 | struct tcp_log *log; | |
80 | } tcp_probe; | |
81 | ||
14a49e1f | 82 | |
662ad4f8 | 83 | static inline int tcp_probe_used(void) |
a42e9d6c | 84 | { |
f81074f8 | 85 | return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1); |
662ad4f8 SH |
86 | } |
87 | ||
88 | static inline int tcp_probe_avail(void) | |
89 | { | |
f81074f8 | 90 | return bufsize - tcp_probe_used() - 1; |
14a49e1f | 91 | } |
a42e9d6c | 92 | |
f925d0a6 DB |
93 | #define tcp_probe_copy_fl_to_si4(inet, si4, mem) \ |
94 | do { \ | |
95 | si4.sin_family = AF_INET; \ | |
96 | si4.sin_port = inet->inet_##mem##port; \ | |
97 | si4.sin_addr.s_addr = inet->inet_##mem##addr; \ | |
98 | } while (0) \ | |
99 | ||
100 | #if IS_ENABLED(CONFIG_IPV6) | |
101 | #define tcp_probe_copy_fl_to_si6(inet, si6, mem) \ | |
102 | do { \ | |
103 | struct ipv6_pinfo *pi6 = inet->pinet6; \ | |
104 | si6.sin6_family = AF_INET6; \ | |
105 | si6.sin6_port = inet->inet_##mem##port; \ | |
106 | si6.sin6_addr = pi6->mem##addr; \ | |
107 | si6.sin6_flowinfo = 0; /* No need here. */ \ | |
108 | si6.sin6_scope_id = 0; /* No need here. */ \ | |
109 | } while (0) | |
110 | #else | |
111 | #define tcp_probe_copy_fl_to_si6(fl, si6, mem) \ | |
112 | do { \ | |
113 | memset(&si6, 0, sizeof(si6)); \ | |
114 | } while (0) | |
115 | #endif | |
116 | ||
85795d64 SH |
117 | /* |
118 | * Hook inserted to be called before each receive packet. | |
119 | * Note: arguments must match tcp_rcv_established()! | |
120 | */ | |
121 | static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb, | |
d8cdeda6 | 122 | const struct tcphdr *th, unsigned int len) |
a42e9d6c SH |
123 | { |
124 | const struct tcp_sock *tp = tcp_sk(sk); | |
125 | const struct inet_sock *inet = inet_sk(sk); | |
126 | ||
85795d64 | 127 | /* Only update if port matches */ |
c720c7e8 | 128 | if ((port == 0 || ntohs(inet->inet_dport) == port || |
9d4fb27d JP |
129 | ntohs(inet->inet_sport) == port) && |
130 | (full || tp->snd_cwnd != tcp_probe.lastcwnd)) { | |
662ad4f8 SH |
131 | |
132 | spin_lock(&tcp_probe.lock); | |
133 | /* If log fills, just silently drop */ | |
134 | if (tcp_probe_avail() > 1) { | |
135 | struct tcp_log *p = tcp_probe.log + tcp_probe.head; | |
136 | ||
137 | p->tstamp = ktime_get(); | |
f925d0a6 DB |
138 | switch (sk->sk_family) { |
139 | case AF_INET: | |
140 | tcp_probe_copy_fl_to_si4(inet, p->src.v4, s); | |
141 | tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d); | |
142 | break; | |
143 | case AF_INET6: | |
144 | tcp_probe_copy_fl_to_si6(inet, p->src.v6, s); | |
145 | tcp_probe_copy_fl_to_si6(inet, p->dst.v6, d); | |
146 | break; | |
147 | default: | |
148 | BUG(); | |
149 | } | |
150 | ||
662ad4f8 SH |
151 | p->length = skb->len; |
152 | p->snd_nxt = tp->snd_nxt; | |
153 | p->snd_una = tp->snd_una; | |
154 | p->snd_cwnd = tp->snd_cwnd; | |
155 | p->snd_wnd = tp->snd_wnd; | |
b4c1c1d0 | 156 | p->rcv_wnd = tp->rcv_wnd; |
b3b0b681 | 157 | p->ssthresh = tcp_current_ssthresh(sk); |
662ad4f8 SH |
158 | p->srtt = tp->srtt >> 3; |
159 | ||
f81074f8 | 160 | tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1); |
662ad4f8 SH |
161 | } |
162 | tcp_probe.lastcwnd = tp->snd_cwnd; | |
163 | spin_unlock(&tcp_probe.lock); | |
164 | ||
165 | wake_up(&tcp_probe.wait); | |
a42e9d6c SH |
166 | } |
167 | ||
168 | jprobe_return(); | |
169 | return 0; | |
170 | } | |
171 | ||
662ad4f8 | 172 | static struct jprobe tcp_jprobe = { |
3a872d89 | 173 | .kp = { |
85795d64 | 174 | .symbol_name = "tcp_rcv_established", |
3a872d89 | 175 | }, |
9e367d85 | 176 | .entry = jtcp_rcv_established, |
a42e9d6c SH |
177 | }; |
178 | ||
5e73ea1a | 179 | static int tcpprobe_open(struct inode *inode, struct file *file) |
a42e9d6c | 180 | { |
662ad4f8 SH |
181 | /* Reset (empty) log */ |
182 | spin_lock_bh(&tcp_probe.lock); | |
183 | tcp_probe.head = tcp_probe.tail = 0; | |
184 | tcp_probe.start = ktime_get(); | |
185 | spin_unlock_bh(&tcp_probe.lock); | |
186 | ||
a42e9d6c SH |
187 | return 0; |
188 | } | |
189 | ||
662ad4f8 SH |
190 | static int tcpprobe_sprint(char *tbuf, int n) |
191 | { | |
192 | const struct tcp_log *p | |
f81074f8 | 193 | = tcp_probe.log + tcp_probe.tail; |
662ad4f8 SH |
194 | struct timespec tv |
195 | = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start)); | |
196 | ||
dda0b386 | 197 | return scnprintf(tbuf, n, |
f925d0a6 | 198 | "%lu.%09lu %pISpc %pISpc %d %#x %#x %u %u %u %u %u\n", |
662ad4f8 SH |
199 | (unsigned long) tv.tv_sec, |
200 | (unsigned long) tv.tv_nsec, | |
f925d0a6 | 201 | &p->src, &p->dst, p->length, p->snd_nxt, p->snd_una, |
b4c1c1d0 | 202 | p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, p->rcv_wnd); |
662ad4f8 SH |
203 | } |
204 | ||
a42e9d6c SH |
205 | static ssize_t tcpprobe_read(struct file *file, char __user *buf, |
206 | size_t len, loff_t *ppos) | |
207 | { | |
a2025b8b RK |
208 | int error = 0; |
209 | size_t cnt = 0; | |
a42e9d6c | 210 | |
a2025b8b | 211 | if (!buf) |
a42e9d6c SH |
212 | return -EINVAL; |
213 | ||
662ad4f8 | 214 | while (cnt < len) { |
dda0b386 | 215 | char tbuf[164]; |
662ad4f8 SH |
216 | int width; |
217 | ||
218 | /* Wait for data in buffer */ | |
219 | error = wait_event_interruptible(tcp_probe.wait, | |
220 | tcp_probe_used() > 0); | |
221 | if (error) | |
222 | break; | |
a42e9d6c | 223 | |
662ad4f8 SH |
224 | spin_lock_bh(&tcp_probe.lock); |
225 | if (tcp_probe.head == tcp_probe.tail) { | |
226 | /* multiple readers race? */ | |
227 | spin_unlock_bh(&tcp_probe.lock); | |
228 | continue; | |
229 | } | |
a42e9d6c | 230 | |
662ad4f8 | 231 | width = tcpprobe_sprint(tbuf, sizeof(tbuf)); |
a42e9d6c | 232 | |
8d390efd | 233 | if (cnt + width < len) |
f81074f8 | 234 | tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1); |
a42e9d6c | 235 | |
662ad4f8 SH |
236 | spin_unlock_bh(&tcp_probe.lock); |
237 | ||
238 | /* if record greater than space available | |
239 | return partial buffer (so far) */ | |
8d390efd | 240 | if (cnt + width >= len) |
662ad4f8 SH |
241 | break; |
242 | ||
8d390efd TQ |
243 | if (copy_to_user(buf + cnt, tbuf, width)) |
244 | return -EFAULT; | |
662ad4f8 SH |
245 | cnt += width; |
246 | } | |
a42e9d6c | 247 | |
662ad4f8 | 248 | return cnt == 0 ? error : cnt; |
a42e9d6c SH |
249 | } |
250 | ||
9a32144e | 251 | static const struct file_operations tcpprobe_fops = { |
a42e9d6c SH |
252 | .owner = THIS_MODULE, |
253 | .open = tcpprobe_open, | |
254 | .read = tcpprobe_read, | |
6038f373 | 255 | .llseek = noop_llseek, |
a42e9d6c SH |
256 | }; |
257 | ||
258 | static __init int tcpprobe_init(void) | |
259 | { | |
260 | int ret = -ENOMEM; | |
261 | ||
d8cdeda6 DB |
262 | /* Warning: if the function signature of tcp_rcv_established, |
263 | * has been changed, you also have to change the signature of | |
264 | * jtcp_rcv_established, otherwise you end up right here! | |
265 | */ | |
266 | BUILD_BUG_ON(__same_type(tcp_rcv_established, | |
267 | jtcp_rcv_established) == 0); | |
268 | ||
662ad4f8 SH |
269 | init_waitqueue_head(&tcp_probe.wait); |
270 | spin_lock_init(&tcp_probe.lock); | |
271 | ||
f81074f8 | 272 | if (bufsize == 0) |
662ad4f8 SH |
273 | return -EINVAL; |
274 | ||
f81074f8 | 275 | bufsize = roundup_pow_of_two(bufsize); |
3d8ea1fd | 276 | tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL); |
662ad4f8 SH |
277 | if (!tcp_probe.log) |
278 | goto err0; | |
a42e9d6c | 279 | |
d4beaa66 | 280 | if (!proc_create(procname, S_IRUSR, init_net.proc_net, &tcpprobe_fops)) |
a42e9d6c SH |
281 | goto err0; |
282 | ||
662ad4f8 | 283 | ret = register_jprobe(&tcp_jprobe); |
a42e9d6c SH |
284 | if (ret) |
285 | goto err1; | |
286 | ||
afd46503 | 287 | pr_info("probe registered (port=%d) bufsize=%u\n", port, bufsize); |
a42e9d6c SH |
288 | return 0; |
289 | err1: | |
ece31ffd | 290 | remove_proc_entry(procname, init_net.proc_net); |
a42e9d6c | 291 | err0: |
662ad4f8 | 292 | kfree(tcp_probe.log); |
a42e9d6c SH |
293 | return ret; |
294 | } | |
295 | module_init(tcpprobe_init); | |
296 | ||
297 | static __exit void tcpprobe_exit(void) | |
298 | { | |
ece31ffd | 299 | remove_proc_entry(procname, init_net.proc_net); |
662ad4f8 SH |
300 | unregister_jprobe(&tcp_jprobe); |
301 | kfree(tcp_probe.log); | |
a42e9d6c SH |
302 | } |
303 | module_exit(tcpprobe_exit); |