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