blob: 760165a0800c317884015f7997b3106aa7ad8562 [file] [log] [blame]
Stephen Hemmingera42e9d62006-06-05 17:30:32 -07001/*
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
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/kernel.h>
23#include <linux/kprobes.h>
24#include <linux/socket.h>
25#include <linux/tcp.h>
26#include <linux/proc_fs.h>
27#include <linux/module.h>
28#include <linux/kfifo.h>
Stephen Hemminger85795d62007-03-24 21:35:33 -070029#include <linux/ktime.h>
30#include <linux/time.h>
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070031#include <linux/vmalloc.h>
32
33#include <net/tcp.h>
34
Stephen Hemminger65ebe6342007-01-23 11:38:57 -080035MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070036MODULE_DESCRIPTION("TCP cwnd snooper");
37MODULE_LICENSE("GPL");
38
Stephen Hemminger85795d62007-03-24 21:35:33 -070039static int port __read_mostly = 0;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070040MODULE_PARM_DESC(port, "Port to match (0=all)");
41module_param(port, int, 0);
42
Stephen Hemminger85795d62007-03-24 21:35:33 -070043static int bufsize __read_mostly = 64*1024;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070044MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
45module_param(bufsize, int, 0);
46
Stephen Hemminger85795d62007-03-24 21:35:33 -070047static int full __read_mostly;
48MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
49module_param(full, int, 0);
50
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070051static const char procname[] = "tcpprobe";
52
53struct {
Stephen Hemminger85795d62007-03-24 21:35:33 -070054 struct kfifo *fifo;
55 spinlock_t lock;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070056 wait_queue_head_t wait;
Stephen Hemminger85795d62007-03-24 21:35:33 -070057 ktime_t start;
58 u32 lastcwnd;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070059} tcpw;
60
Stephen Hemminger85795d62007-03-24 21:35:33 -070061/*
62 * Print to log with timestamps.
63 * FIXME: causes an extra copy
64 */
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070065static void printl(const char *fmt, ...)
66{
67 va_list args;
68 int len;
Stephen Hemminger85795d62007-03-24 21:35:33 -070069 struct timespec tv;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070070 char tbuf[256];
71
72 va_start(args, fmt);
Stephen Hemminger85795d62007-03-24 21:35:33 -070073 /* want monotonic time since start of tcp_probe */
74 tv = ktime_to_timespec(ktime_sub(ktime_get(), tcpw.start));
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070075
Stephen Hemminger85795d62007-03-24 21:35:33 -070076 len = sprintf(tbuf, "%lu.%09lu ",
77 (unsigned long) tv.tv_sec, (unsigned long) tv.tv_nsec);
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070078 len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
79 va_end(args);
80
81 kfifo_put(tcpw.fifo, tbuf, len);
82 wake_up(&tcpw.wait);
Stephen Hemminger67403752007-05-29 13:24:51 -070083} __attribute__ ((format (printf, 1, 2)));
84
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070085
Stephen Hemminger85795d62007-03-24 21:35:33 -070086/*
87 * Hook inserted to be called before each receive packet.
88 * Note: arguments must match tcp_rcv_established()!
89 */
90static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
91 struct tcphdr *th, unsigned len)
Stephen Hemmingera42e9d62006-06-05 17:30:32 -070092{
93 const struct tcp_sock *tp = tcp_sk(sk);
94 const struct inet_sock *inet = inet_sk(sk);
95
Stephen Hemminger85795d62007-03-24 21:35:33 -070096 /* Only update if port matches */
97 if ((port == 0 || ntohs(inet->dport) == port || ntohs(inet->sport) == port)
98 && (full || tp->snd_cwnd != tcpw.lastcwnd)) {
Sangtae Ha63313492007-05-29 13:24:11 -070099 printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u %u\n",
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700100 NIPQUAD(inet->saddr), ntohs(inet->sport),
101 NIPQUAD(inet->daddr), ntohs(inet->dport),
Stephen Hemminger85795d62007-03-24 21:35:33 -0700102 skb->len, tp->snd_nxt, tp->snd_una,
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700103 tp->snd_cwnd, tcp_current_ssthresh(sk),
Stephen Hemminger85795d62007-03-24 21:35:33 -0700104 tp->snd_wnd, tp->srtt >> 3);
105 tcpw.lastcwnd = tp->snd_cwnd;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700106 }
107
108 jprobe_return();
109 return 0;
110}
111
Stephen Hemminger85795d62007-03-24 21:35:33 -0700112static struct jprobe tcp_probe = {
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700113 .kp = {
Stephen Hemminger85795d62007-03-24 21:35:33 -0700114 .symbol_name = "tcp_rcv_established",
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700115 },
Stephen Hemminger85795d62007-03-24 21:35:33 -0700116 .entry = JPROBE_ENTRY(jtcp_rcv_established),
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700117};
118
119
120static int tcpprobe_open(struct inode * inode, struct file * file)
121{
122 kfifo_reset(tcpw.fifo);
Stephen Hemminger85795d62007-03-24 21:35:33 -0700123 tcpw.start = ktime_get();
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700124 return 0;
125}
126
127static ssize_t tcpprobe_read(struct file *file, char __user *buf,
128 size_t len, loff_t *ppos)
129{
James Morris118075b2006-07-30 20:21:45 -0700130 int error = 0, cnt = 0;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700131 unsigned char *tbuf;
132
133 if (!buf || len < 0)
134 return -EINVAL;
135
136 if (len == 0)
137 return 0;
138
139 tbuf = vmalloc(len);
140 if (!tbuf)
141 return -ENOMEM;
142
143 error = wait_event_interruptible(tcpw.wait,
144 __kfifo_len(tcpw.fifo) != 0);
145 if (error)
David S. Miller18b6fe62006-08-13 18:05:09 -0700146 goto out_free;
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700147
148 cnt = kfifo_get(tcpw.fifo, tbuf, len);
149 error = copy_to_user(buf, tbuf, cnt);
150
David S. Miller18b6fe62006-08-13 18:05:09 -0700151out_free:
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700152 vfree(tbuf);
153
154 return error ? error : cnt;
155}
156
Arjan van de Ven9a321442007-02-12 00:55:35 -0800157static const struct file_operations tcpprobe_fops = {
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700158 .owner = THIS_MODULE,
159 .open = tcpprobe_open,
160 .read = tcpprobe_read,
161};
162
163static __init int tcpprobe_init(void)
164{
165 int ret = -ENOMEM;
166
167 init_waitqueue_head(&tcpw.wait);
168 spin_lock_init(&tcpw.lock);
169 tcpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &tcpw.lock);
Akinobu Mitaac16ca62006-11-22 20:26:11 -0800170 if (IS_ERR(tcpw.fifo))
171 return PTR_ERR(tcpw.fifo);
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700172
173 if (!proc_net_fops_create(procname, S_IRUSR, &tcpprobe_fops))
174 goto err0;
175
Stephen Hemminger85795d62007-03-24 21:35:33 -0700176 ret = register_jprobe(&tcp_probe);
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700177 if (ret)
178 goto err1;
179
180 pr_info("TCP watch registered (port=%d)\n", port);
181 return 0;
182 err1:
183 proc_net_remove(procname);
184 err0:
185 kfifo_free(tcpw.fifo);
186 return ret;
187}
188module_init(tcpprobe_init);
189
190static __exit void tcpprobe_exit(void)
191{
192 kfifo_free(tcpw.fifo);
193 proc_net_remove(procname);
Stephen Hemminger85795d62007-03-24 21:35:33 -0700194 unregister_jprobe(&tcp_probe);
Stephen Hemmingera42e9d62006-06-05 17:30:32 -0700195
196}
197module_exit(tcpprobe_exit);