tcp_probe.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/socket.h>
  23. #include <linux/tcp.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/module.h>
  26. #include <linux/ktime.h>
  27. #include <linux/time.h>
  28. #include <net/net_namespace.h>
  29. #include <net/tcp.h>
  30. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  31. MODULE_DESCRIPTION("TCP cwnd snooper");
  32. MODULE_LICENSE("GPL");
  33. MODULE_VERSION("1.1");
  34. static int port __read_mostly = 0;
  35. MODULE_PARM_DESC(port, "Port to match (0=all)");
  36. module_param(port, int, 0);
  37. static int bufsize __read_mostly = 4096;
  38. MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)");
  39. module_param(bufsize, int, 0);
  40. static int full __read_mostly;
  41. MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
  42. module_param(full, int, 0);
  43. static const char procname[] = "tcpprobe";
  44. struct tcp_log {
  45. ktime_t tstamp;
  46. __be32 saddr, daddr;
  47. __be16 sport, dport;
  48. u16 length;
  49. u32 snd_nxt;
  50. u32 snd_una;
  51. u32 snd_wnd;
  52. u32 snd_cwnd;
  53. u32 ssthresh;
  54. u32 srtt;
  55. };
  56. static struct {
  57. spinlock_t lock;
  58. wait_queue_head_t wait;
  59. ktime_t start;
  60. u32 lastcwnd;
  61. unsigned long head, tail;
  62. struct tcp_log *log;
  63. } tcp_probe;
  64. static inline int tcp_probe_used(void)
  65. {
  66. return (tcp_probe.head - tcp_probe.tail) % bufsize;
  67. }
  68. static inline int tcp_probe_avail(void)
  69. {
  70. return bufsize - tcp_probe_used();
  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->inet_dport) == port ||
  83. ntohs(inet->inet_sport) == port) &&
  84. (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  85. spin_lock(&tcp_probe.lock);
  86. /* If log fills, just silently drop */
  87. if (tcp_probe_avail() > 1) {
  88. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  89. p->tstamp = ktime_get();
  90. p->saddr = inet->inet_saddr;
  91. p->sport = inet->inet_sport;
  92. p->daddr = inet->inet_daddr;
  93. p->dport = inet->inet_dport;
  94. p->length = skb->len;
  95. p->snd_nxt = tp->snd_nxt;
  96. p->snd_una = tp->snd_una;
  97. p->snd_cwnd = tp->snd_cwnd;
  98. p->snd_wnd = tp->snd_wnd;
  99. p->ssthresh = tcp_current_ssthresh(sk);
  100. p->srtt = tp->srtt >> 3;
  101. tcp_probe.head = (tcp_probe.head + 1) % bufsize;
  102. }
  103. tcp_probe.lastcwnd = tp->snd_cwnd;
  104. spin_unlock(&tcp_probe.lock);
  105. wake_up(&tcp_probe.wait);
  106. }
  107. jprobe_return();
  108. return 0;
  109. }
  110. static struct jprobe tcp_jprobe = {
  111. .kp = {
  112. .symbol_name = "tcp_rcv_established",
  113. },
  114. .entry = jtcp_rcv_established,
  115. };
  116. static int tcpprobe_open(struct inode * inode, struct file * file)
  117. {
  118. /* Reset (empty) log */
  119. spin_lock_bh(&tcp_probe.lock);
  120. tcp_probe.head = tcp_probe.tail = 0;
  121. tcp_probe.start = ktime_get();
  122. spin_unlock_bh(&tcp_probe.lock);
  123. return 0;
  124. }
  125. static int tcpprobe_sprint(char *tbuf, int n)
  126. {
  127. const struct tcp_log *p
  128. = tcp_probe.log + tcp_probe.tail % bufsize;
  129. struct timespec tv
  130. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  131. return snprintf(tbuf, n,
  132. "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
  133. (unsigned long) tv.tv_sec,
  134. (unsigned long) tv.tv_nsec,
  135. &p->saddr, ntohs(p->sport),
  136. &p->daddr, ntohs(p->dport),
  137. p->length, p->snd_nxt, p->snd_una,
  138. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
  139. }
  140. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  141. size_t len, loff_t *ppos)
  142. {
  143. int error = 0;
  144. size_t cnt = 0;
  145. if (!buf)
  146. return -EINVAL;
  147. while (cnt < len) {
  148. char tbuf[128];
  149. int width;
  150. /* Wait for data in buffer */
  151. error = wait_event_interruptible(tcp_probe.wait,
  152. tcp_probe_used() > 0);
  153. if (error)
  154. break;
  155. spin_lock_bh(&tcp_probe.lock);
  156. if (tcp_probe.head == tcp_probe.tail) {
  157. /* multiple readers race? */
  158. spin_unlock_bh(&tcp_probe.lock);
  159. continue;
  160. }
  161. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  162. if (cnt + width < len)
  163. tcp_probe.tail = (tcp_probe.tail + 1) % bufsize;
  164. spin_unlock_bh(&tcp_probe.lock);
  165. /* if record greater than space available
  166. return partial buffer (so far) */
  167. if (cnt + width >= len)
  168. break;
  169. if (copy_to_user(buf + cnt, tbuf, width))
  170. return -EFAULT;
  171. cnt += width;
  172. }
  173. return cnt == 0 ? error : cnt;
  174. }
  175. static const struct file_operations tcpprobe_fops = {
  176. .owner = THIS_MODULE,
  177. .open = tcpprobe_open,
  178. .read = tcpprobe_read,
  179. };
  180. static __init int tcpprobe_init(void)
  181. {
  182. int ret = -ENOMEM;
  183. init_waitqueue_head(&tcp_probe.wait);
  184. spin_lock_init(&tcp_probe.lock);
  185. if (bufsize < 0)
  186. return -EINVAL;
  187. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  188. if (!tcp_probe.log)
  189. goto err0;
  190. if (!proc_net_fops_create(&init_net, procname, S_IRUSR, &tcpprobe_fops))
  191. goto err0;
  192. ret = register_jprobe(&tcp_jprobe);
  193. if (ret)
  194. goto err1;
  195. pr_info("TCP probe registered (port=%d)\n", port);
  196. return 0;
  197. err1:
  198. proc_net_remove(&init_net, procname);
  199. err0:
  200. kfree(tcp_probe.log);
  201. return ret;
  202. }
  203. module_init(tcpprobe_init);
  204. static __exit void tcpprobe_exit(void)
  205. {
  206. proc_net_remove(&init_net, procname);
  207. unregister_jprobe(&tcp_jprobe);
  208. kfree(tcp_probe.log);
  209. }
  210. module_exit(tcpprobe_exit);