tcp_probe.c 4.8 KB

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