tcp_probe.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. {
  58. va_list args;
  59. int len;
  60. struct timespec tv;
  61. char tbuf[256];
  62. va_start(args, fmt);
  63. /* want monotonic time since start of tcp_probe */
  64. tv = ktime_to_timespec(ktime_sub(ktime_get(), tcpw.start));
  65. len = sprintf(tbuf, "%lu.%09lu ",
  66. (unsigned long) tv.tv_sec, (unsigned long) tv.tv_nsec);
  67. len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
  68. va_end(args);
  69. kfifo_put(tcpw.fifo, tbuf, len);
  70. wake_up(&tcpw.wait);
  71. }
  72. /*
  73. * Hook inserted to be called before each receive packet.
  74. * Note: arguments must match tcp_rcv_established()!
  75. */
  76. static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  77. struct tcphdr *th, unsigned len)
  78. {
  79. const struct tcp_sock *tp = tcp_sk(sk);
  80. const struct inet_sock *inet = inet_sk(sk);
  81. /* Only update if port matches */
  82. if ((port == 0 || ntohs(inet->dport) == port || ntohs(inet->sport) == port)
  83. && (full || tp->snd_cwnd != tcpw.lastcwnd)) {
  84. printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u\n",
  85. NIPQUAD(inet->saddr), ntohs(inet->sport),
  86. NIPQUAD(inet->daddr), ntohs(inet->dport),
  87. skb->len, tp->snd_nxt, tp->snd_una,
  88. tp->snd_cwnd, tcp_current_ssthresh(sk),
  89. tp->snd_wnd, tp->srtt >> 3);
  90. tcpw.lastcwnd = tp->snd_cwnd;
  91. }
  92. jprobe_return();
  93. return 0;
  94. }
  95. static struct jprobe tcp_probe = {
  96. .kp = {
  97. .symbol_name = "tcp_rcv_established",
  98. },
  99. .entry = JPROBE_ENTRY(jtcp_rcv_established),
  100. };
  101. static int tcpprobe_open(struct inode * inode, struct file * file)
  102. {
  103. kfifo_reset(tcpw.fifo);
  104. tcpw.start = ktime_get();
  105. return 0;
  106. }
  107. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  108. size_t len, loff_t *ppos)
  109. {
  110. int error = 0, cnt = 0;
  111. unsigned char *tbuf;
  112. if (!buf || len < 0)
  113. return -EINVAL;
  114. if (len == 0)
  115. return 0;
  116. tbuf = vmalloc(len);
  117. if (!tbuf)
  118. return -ENOMEM;
  119. error = wait_event_interruptible(tcpw.wait,
  120. __kfifo_len(tcpw.fifo) != 0);
  121. if (error)
  122. goto out_free;
  123. cnt = kfifo_get(tcpw.fifo, tbuf, len);
  124. error = copy_to_user(buf, tbuf, cnt);
  125. out_free:
  126. vfree(tbuf);
  127. return error ? error : cnt;
  128. }
  129. static const struct file_operations tcpprobe_fops = {
  130. .owner = THIS_MODULE,
  131. .open = tcpprobe_open,
  132. .read = tcpprobe_read,
  133. };
  134. static __init int tcpprobe_init(void)
  135. {
  136. int ret = -ENOMEM;
  137. init_waitqueue_head(&tcpw.wait);
  138. spin_lock_init(&tcpw.lock);
  139. tcpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &tcpw.lock);
  140. if (IS_ERR(tcpw.fifo))
  141. return PTR_ERR(tcpw.fifo);
  142. if (!proc_net_fops_create(procname, S_IRUSR, &tcpprobe_fops))
  143. goto err0;
  144. ret = register_jprobe(&tcp_probe);
  145. if (ret)
  146. goto err1;
  147. pr_info("TCP watch registered (port=%d)\n", port);
  148. return 0;
  149. err1:
  150. proc_net_remove(procname);
  151. err0:
  152. kfifo_free(tcpw.fifo);
  153. return ret;
  154. }
  155. module_init(tcpprobe_init);
  156. static __exit void tcpprobe_exit(void)
  157. {
  158. kfifo_free(tcpw.fifo);
  159. proc_net_remove(procname);
  160. unregister_jprobe(&tcp_probe);
  161. }
  162. module_exit(tcpprobe_exit);