2 * tcpprobe - Observe the TCP flow with kprobes.
4 * The idea for this came from Werner Almesberger's umlsim
5 * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
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.
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.
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.
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>
29 #include <linux/ktime.h>
30 #include <linux/time.h>
31 #include <linux/vmalloc.h>
35 MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
36 MODULE_DESCRIPTION("TCP cwnd snooper");
37 MODULE_LICENSE("GPL");
39 static int port __read_mostly
= 0;
40 MODULE_PARM_DESC(port
, "Port to match (0=all)");
41 module_param(port
, int, 0);
43 static int bufsize __read_mostly
= 64*1024;
44 MODULE_PARM_DESC(bufsize
, "Log buffer size (default 64k)");
45 module_param(bufsize
, int, 0);
47 static int full __read_mostly
;
48 MODULE_PARM_DESC(full
, "Full log (1=every ack packet received, 0=only cwnd changes)");
49 module_param(full
, int, 0);
51 static const char procname
[] = "tcpprobe";
56 wait_queue_head_t wait
;
62 * Print to log with timestamps.
63 * FIXME: causes an extra copy
65 static void printl(const char *fmt
, ...)
73 /* want monotonic time since start of tcp_probe */
74 tv
= ktime_to_timespec(ktime_sub(ktime_get(), tcpw
.start
));
76 len
= sprintf(tbuf
, "%lu.%09lu ",
77 (unsigned long) tv
.tv_sec
, (unsigned long) tv
.tv_nsec
);
78 len
+= vscnprintf(tbuf
+len
, sizeof(tbuf
)-len
, fmt
, args
);
81 kfifo_put(tcpw
.fifo
, tbuf
, len
);
83 } __attribute__ ((format (printf
, 1, 2)));
87 * Hook inserted to be called before each receive packet.
88 * Note: arguments must match tcp_rcv_established()!
90 static int jtcp_rcv_established(struct sock
*sk
, struct sk_buff
*skb
,
91 struct tcphdr
*th
, unsigned len
)
93 const struct tcp_sock
*tp
= tcp_sk(sk
);
94 const struct inet_sock
*inet
= inet_sk(sk
);
96 /* Only update if port matches */
97 if ((port
== 0 || ntohs(inet
->dport
) == port
|| ntohs(inet
->sport
) == port
)
98 && (full
|| tp
->snd_cwnd
!= tcpw
.lastcwnd
)) {
99 printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u %u\n",
100 NIPQUAD(inet
->saddr
), ntohs(inet
->sport
),
101 NIPQUAD(inet
->daddr
), ntohs(inet
->dport
),
102 skb
->len
, tp
->snd_nxt
, tp
->snd_una
,
103 tp
->snd_cwnd
, tcp_current_ssthresh(sk
),
104 tp
->snd_wnd
, tp
->srtt
>> 3);
105 tcpw
.lastcwnd
= tp
->snd_cwnd
;
112 static struct jprobe tcp_probe
= {
114 .symbol_name
= "tcp_rcv_established",
116 .entry
= JPROBE_ENTRY(jtcp_rcv_established
),
120 static int tcpprobe_open(struct inode
* inode
, struct file
* file
)
122 kfifo_reset(tcpw
.fifo
);
123 tcpw
.start
= ktime_get();
127 static ssize_t
tcpprobe_read(struct file
*file
, char __user
*buf
,
128 size_t len
, loff_t
*ppos
)
130 int error
= 0, cnt
= 0;
143 error
= wait_event_interruptible(tcpw
.wait
,
144 __kfifo_len(tcpw
.fifo
) != 0);
148 cnt
= kfifo_get(tcpw
.fifo
, tbuf
, len
);
149 error
= copy_to_user(buf
, tbuf
, cnt
);
154 return error
? error
: cnt
;
157 static const struct file_operations tcpprobe_fops
= {
158 .owner
= THIS_MODULE
,
159 .open
= tcpprobe_open
,
160 .read
= tcpprobe_read
,
163 static __init
int tcpprobe_init(void)
167 init_waitqueue_head(&tcpw
.wait
);
168 spin_lock_init(&tcpw
.lock
);
169 tcpw
.fifo
= kfifo_alloc(bufsize
, GFP_KERNEL
, &tcpw
.lock
);
170 if (IS_ERR(tcpw
.fifo
))
171 return PTR_ERR(tcpw
.fifo
);
173 if (!proc_net_fops_create(procname
, S_IRUSR
, &tcpprobe_fops
))
176 ret
= register_jprobe(&tcp_probe
);
180 pr_info("TCP watch registered (port=%d)\n", port
);
183 proc_net_remove(procname
);
185 kfifo_free(tcpw
.fifo
);
188 module_init(tcpprobe_init
);
190 static __exit
void tcpprobe_exit(void)
192 kfifo_free(tcpw
.fifo
);
193 proc_net_remove(procname
);
194 unregister_jprobe(&tcp_probe
);
197 module_exit(tcpprobe_exit
);