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