]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/tcp_probe.c
[TCP] tcp_probe: Attach printf attribute properly to printl().
[net-next-2.6.git] / net / ipv4 / tcp_probe.c
CommitLineData
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
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>
85795d64
SH
29#include <linux/ktime.h>
30#include <linux/time.h>
a42e9d6c
SH
31#include <linux/vmalloc.h>
32
33#include <net/tcp.h>
34
65ebe634 35MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
a42e9d6c
SH
36MODULE_DESCRIPTION("TCP cwnd snooper");
37MODULE_LICENSE("GPL");
38
85795d64 39static int port __read_mostly = 0;
a42e9d6c
SH
40MODULE_PARM_DESC(port, "Port to match (0=all)");
41module_param(port, int, 0);
42
85795d64 43static int bufsize __read_mostly = 64*1024;
a42e9d6c
SH
44MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
45module_param(bufsize, int, 0);
46
85795d64
SH
47static 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
a42e9d6c
SH
51static const char procname[] = "tcpprobe";
52
53struct {
85795d64
SH
54 struct kfifo *fifo;
55 spinlock_t lock;
a42e9d6c 56 wait_queue_head_t wait;
85795d64
SH
57 ktime_t start;
58 u32 lastcwnd;
a42e9d6c
SH
59} tcpw;
60
85795d64
SH
61/*
62 * Print to log with timestamps.
63 * FIXME: causes an extra copy
64 */
14a49e1f
DM
65static void printl(const char *fmt, ...)
66 __attribute__ ((format (printf, 1, 2)));
67
a42e9d6c
SH
68static void printl(const char *fmt, ...)
69{
70 va_list args;
71 int len;
85795d64 72 struct timespec tv;
a42e9d6c
SH
73 char tbuf[256];
74
75 va_start(args, fmt);
85795d64
SH
76 /* want monotonic time since start of tcp_probe */
77 tv = ktime_to_timespec(ktime_sub(ktime_get(), tcpw.start));
a42e9d6c 78
85795d64
SH
79 len = sprintf(tbuf, "%lu.%09lu ",
80 (unsigned long) tv.tv_sec, (unsigned long) tv.tv_nsec);
a42e9d6c
SH
81 len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
82 va_end(args);
83
84 kfifo_put(tcpw.fifo, tbuf, len);
85 wake_up(&tcpw.wait);
14a49e1f 86}
a42e9d6c 87
85795d64
SH
88/*
89 * Hook inserted to be called before each receive packet.
90 * Note: arguments must match tcp_rcv_established()!
91 */
92static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
93 struct tcphdr *th, unsigned len)
a42e9d6c
SH
94{
95 const struct tcp_sock *tp = tcp_sk(sk);
96 const struct inet_sock *inet = inet_sk(sk);
97
85795d64
SH
98 /* Only update if port matches */
99 if ((port == 0 || ntohs(inet->dport) == port || ntohs(inet->sport) == port)
100 && (full || tp->snd_cwnd != tcpw.lastcwnd)) {
63313494 101 printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u %u\n",
a42e9d6c
SH
102 NIPQUAD(inet->saddr), ntohs(inet->sport),
103 NIPQUAD(inet->daddr), ntohs(inet->dport),
85795d64 104 skb->len, tp->snd_nxt, tp->snd_una,
a42e9d6c 105 tp->snd_cwnd, tcp_current_ssthresh(sk),
85795d64
SH
106 tp->snd_wnd, tp->srtt >> 3);
107 tcpw.lastcwnd = tp->snd_cwnd;
a42e9d6c
SH
108 }
109
110 jprobe_return();
111 return 0;
112}
113
85795d64 114static struct jprobe tcp_probe = {
3a872d89 115 .kp = {
85795d64 116 .symbol_name = "tcp_rcv_established",
3a872d89 117 },
85795d64 118 .entry = JPROBE_ENTRY(jtcp_rcv_established),
a42e9d6c
SH
119};
120
121
122static int tcpprobe_open(struct inode * inode, struct file * file)
123{
124 kfifo_reset(tcpw.fifo);
85795d64 125 tcpw.start = ktime_get();
a42e9d6c
SH
126 return 0;
127}
128
129static ssize_t tcpprobe_read(struct file *file, char __user *buf,
130 size_t len, loff_t *ppos)
131{
118075b3 132 int error = 0, cnt = 0;
a42e9d6c
SH
133 unsigned char *tbuf;
134
135 if (!buf || len < 0)
136 return -EINVAL;
137
138 if (len == 0)
139 return 0;
140
141 tbuf = vmalloc(len);
142 if (!tbuf)
143 return -ENOMEM;
144
145 error = wait_event_interruptible(tcpw.wait,
146 __kfifo_len(tcpw.fifo) != 0);
147 if (error)
18b6fe64 148 goto out_free;
a42e9d6c
SH
149
150 cnt = kfifo_get(tcpw.fifo, tbuf, len);
151 error = copy_to_user(buf, tbuf, cnt);
152
18b6fe64 153out_free:
a42e9d6c
SH
154 vfree(tbuf);
155
156 return error ? error : cnt;
157}
158
9a32144e 159static const struct file_operations tcpprobe_fops = {
a42e9d6c
SH
160 .owner = THIS_MODULE,
161 .open = tcpprobe_open,
162 .read = tcpprobe_read,
163};
164
165static __init int tcpprobe_init(void)
166{
167 int ret = -ENOMEM;
168
169 init_waitqueue_head(&tcpw.wait);
170 spin_lock_init(&tcpw.lock);
171 tcpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &tcpw.lock);
ac16ca64
AM
172 if (IS_ERR(tcpw.fifo))
173 return PTR_ERR(tcpw.fifo);
a42e9d6c
SH
174
175 if (!proc_net_fops_create(procname, S_IRUSR, &tcpprobe_fops))
176 goto err0;
177
85795d64 178 ret = register_jprobe(&tcp_probe);
a42e9d6c
SH
179 if (ret)
180 goto err1;
181
182 pr_info("TCP watch registered (port=%d)\n", port);
183 return 0;
184 err1:
185 proc_net_remove(procname);
186 err0:
187 kfifo_free(tcpw.fifo);
188 return ret;
189}
190module_init(tcpprobe_init);
191
192static __exit void tcpprobe_exit(void)
193{
194 kfifo_free(tcpw.fifo);
195 proc_net_remove(procname);
85795d64 196 unregister_jprobe(&tcp_probe);
a42e9d6c
SH
197
198}
199module_exit(tcpprobe_exit);