tcp_probe.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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->dport) == port || ntohs(inet->sport) == port)
  83. && (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  84. spin_lock(&tcp_probe.lock);
  85. /* If log fills, just silently drop */
  86. if (tcp_probe_avail() > 1) {
  87. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  88. p->tstamp = ktime_get();
  89. p->saddr = inet->saddr;
  90. p->sport = inet->sport;
  91. p->daddr = inet->daddr;
  92. p->dport = inet->dport;
  93. p->length = skb->len;
  94. p->snd_nxt = tp->snd_nxt;
  95. p->snd_una = tp->snd_una;
  96. p->snd_cwnd = tp->snd_cwnd;
  97. p->snd_wnd = tp->snd_wnd;
  98. p->ssthresh = tcp_current_ssthresh(sk);
  99. p->srtt = tp->srtt >> 3;
  100. tcp_probe.head = (tcp_probe.head + 1) % bufsize;
  101. }
  102. tcp_probe.lastcwnd = tp->snd_cwnd;
  103. spin_unlock(&tcp_probe.lock);
  104. wake_up(&tcp_probe.wait);
  105. }
  106. jprobe_return();
  107. return 0;
  108. }
  109. static struct jprobe tcp_jprobe = {
  110. .kp = {
  111. .symbol_name = "tcp_rcv_established",
  112. },
  113. .entry = jtcp_rcv_established,
  114. };
  115. static int tcpprobe_open(struct inode * inode, struct file * file)
  116. {
  117. /* Reset (empty) log */
  118. spin_lock_bh(&tcp_probe.lock);
  119. tcp_probe.head = tcp_probe.tail = 0;
  120. tcp_probe.start = ktime_get();
  121. spin_unlock_bh(&tcp_probe.lock);
  122. return 0;
  123. }
  124. static int tcpprobe_sprint(char *tbuf, int n)
  125. {
  126. const struct tcp_log *p
  127. = tcp_probe.log + tcp_probe.tail % bufsize;
  128. struct timespec tv
  129. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  130. return snprintf(tbuf, n,
  131. "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
  132. (unsigned long) tv.tv_sec,
  133. (unsigned long) tv.tv_nsec,
  134. &p->saddr, ntohs(p->sport),
  135. &p->daddr, ntohs(p->dport),
  136. p->length, p->snd_nxt, p->snd_una,
  137. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
  138. }
  139. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  140. size_t len, loff_t *ppos)
  141. {
  142. int error = 0;
  143. size_t cnt = 0;
  144. if (!buf)
  145. return -EINVAL;
  146. while (cnt < len) {
  147. char tbuf[128];
  148. int width;
  149. /* Wait for data in buffer */
  150. error = wait_event_interruptible(tcp_probe.wait,
  151. tcp_probe_used() > 0);
  152. if (error)
  153. break;
  154. spin_lock_bh(&tcp_probe.lock);
  155. if (tcp_probe.head == tcp_probe.tail) {
  156. /* multiple readers race? */
  157. spin_unlock_bh(&tcp_probe.lock);
  158. continue;
  159. }
  160. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  161. if (cnt + width < len)
  162. tcp_probe.tail = (tcp_probe.tail + 1) % bufsize;
  163. spin_unlock_bh(&tcp_probe.lock);
  164. /* if record greater than space available
  165. return partial buffer (so far) */
  166. if (cnt + width >= len)
  167. break;
  168. if (copy_to_user(buf + cnt, tbuf, width))
  169. return -EFAULT;
  170. cnt += width;
  171. }
  172. return cnt == 0 ? error : cnt;
  173. }
  174. static const struct file_operations tcpprobe_fops = {
  175. .owner = THIS_MODULE,
  176. .open = tcpprobe_open,
  177. .read = tcpprobe_read,
  178. };
  179. static __init int tcpprobe_init(void)
  180. {
  181. int ret = -ENOMEM;
  182. init_waitqueue_head(&tcp_probe.wait);
  183. spin_lock_init(&tcp_probe.lock);
  184. if (bufsize < 0)
  185. return -EINVAL;
  186. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  187. if (!tcp_probe.log)
  188. goto err0;
  189. if (!proc_net_fops_create(&init_net, procname, S_IRUSR, &tcpprobe_fops))
  190. goto err0;
  191. ret = register_jprobe(&tcp_jprobe);
  192. if (ret)
  193. goto err1;
  194. pr_info("TCP probe registered (port=%d)\n", port);
  195. return 0;
  196. err1:
  197. proc_net_remove(&init_net, procname);
  198. err0:
  199. kfree(tcp_probe.log);
  200. return ret;
  201. }
  202. module_init(tcpprobe_init);
  203. static __exit void tcpprobe_exit(void)
  204. {
  205. proc_net_remove(&init_net, procname);
  206. unregister_jprobe(&tcp_jprobe);
  207. kfree(tcp_probe.log);
  208. }
  209. module_exit(tcpprobe_exit);