tcp_probe.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/slab.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/module.h>
  27. #include <linux/ktime.h>
  28. #include <linux/time.h>
  29. #include <net/net_namespace.h>
  30. #include <net/tcp.h>
  31. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  32. MODULE_DESCRIPTION("TCP cwnd snooper");
  33. MODULE_LICENSE("GPL");
  34. MODULE_VERSION("1.1");
  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 unsigned int bufsize __read_mostly = 4096;
  39. MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)");
  40. module_param(bufsize, uint, 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 tcp_log {
  46. ktime_t tstamp;
  47. __be32 saddr, daddr;
  48. __be16 sport, dport;
  49. u16 length;
  50. u32 snd_nxt;
  51. u32 snd_una;
  52. u32 snd_wnd;
  53. u32 snd_cwnd;
  54. u32 ssthresh;
  55. u32 srtt;
  56. };
  57. static struct {
  58. spinlock_t lock;
  59. wait_queue_head_t wait;
  60. ktime_t start;
  61. u32 lastcwnd;
  62. unsigned long head, tail;
  63. struct tcp_log *log;
  64. } tcp_probe;
  65. static inline int tcp_probe_used(void)
  66. {
  67. return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1);
  68. }
  69. static inline int tcp_probe_avail(void)
  70. {
  71. return bufsize - tcp_probe_used() - 1;
  72. }
  73. /*
  74. * Hook inserted to be called before each receive packet.
  75. * Note: arguments must match tcp_rcv_established()!
  76. */
  77. static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  78. struct tcphdr *th, unsigned len)
  79. {
  80. const struct tcp_sock *tp = tcp_sk(sk);
  81. const struct inet_sock *inet = inet_sk(sk);
  82. /* Only update if port matches */
  83. if ((port == 0 || ntohs(inet->inet_dport) == port ||
  84. ntohs(inet->inet_sport) == port) &&
  85. (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  86. spin_lock(&tcp_probe.lock);
  87. /* If log fills, just silently drop */
  88. if (tcp_probe_avail() > 1) {
  89. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  90. p->tstamp = ktime_get();
  91. p->saddr = inet->inet_saddr;
  92. p->sport = inet->inet_sport;
  93. p->daddr = inet->inet_daddr;
  94. p->dport = inet->inet_dport;
  95. p->length = skb->len;
  96. p->snd_nxt = tp->snd_nxt;
  97. p->snd_una = tp->snd_una;
  98. p->snd_cwnd = tp->snd_cwnd;
  99. p->snd_wnd = tp->snd_wnd;
  100. p->ssthresh = tcp_current_ssthresh(sk);
  101. p->srtt = tp->srtt >> 3;
  102. tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1);
  103. }
  104. tcp_probe.lastcwnd = tp->snd_cwnd;
  105. spin_unlock(&tcp_probe.lock);
  106. wake_up(&tcp_probe.wait);
  107. }
  108. jprobe_return();
  109. return 0;
  110. }
  111. static struct jprobe tcp_jprobe = {
  112. .kp = {
  113. .symbol_name = "tcp_rcv_established",
  114. },
  115. .entry = jtcp_rcv_established,
  116. };
  117. static int tcpprobe_open(struct inode * inode, struct file * file)
  118. {
  119. /* Reset (empty) log */
  120. spin_lock_bh(&tcp_probe.lock);
  121. tcp_probe.head = tcp_probe.tail = 0;
  122. tcp_probe.start = ktime_get();
  123. spin_unlock_bh(&tcp_probe.lock);
  124. return 0;
  125. }
  126. static int tcpprobe_sprint(char *tbuf, int n)
  127. {
  128. const struct tcp_log *p
  129. = tcp_probe.log + tcp_probe.tail;
  130. struct timespec tv
  131. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  132. return scnprintf(tbuf, n,
  133. "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
  134. (unsigned long) tv.tv_sec,
  135. (unsigned long) tv.tv_nsec,
  136. &p->saddr, ntohs(p->sport),
  137. &p->daddr, ntohs(p->dport),
  138. p->length, p->snd_nxt, p->snd_una,
  139. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
  140. }
  141. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  142. size_t len, loff_t *ppos)
  143. {
  144. int error = 0;
  145. size_t cnt = 0;
  146. if (!buf)
  147. return -EINVAL;
  148. while (cnt < len) {
  149. char tbuf[164];
  150. int width;
  151. /* Wait for data in buffer */
  152. error = wait_event_interruptible(tcp_probe.wait,
  153. tcp_probe_used() > 0);
  154. if (error)
  155. break;
  156. spin_lock_bh(&tcp_probe.lock);
  157. if (tcp_probe.head == tcp_probe.tail) {
  158. /* multiple readers race? */
  159. spin_unlock_bh(&tcp_probe.lock);
  160. continue;
  161. }
  162. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  163. if (cnt + width < len)
  164. tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1);
  165. spin_unlock_bh(&tcp_probe.lock);
  166. /* if record greater than space available
  167. return partial buffer (so far) */
  168. if (cnt + width >= len)
  169. break;
  170. if (copy_to_user(buf + cnt, tbuf, width))
  171. return -EFAULT;
  172. cnt += width;
  173. }
  174. return cnt == 0 ? error : cnt;
  175. }
  176. static const struct file_operations tcpprobe_fops = {
  177. .owner = THIS_MODULE,
  178. .open = tcpprobe_open,
  179. .read = tcpprobe_read,
  180. .llseek = noop_llseek,
  181. };
  182. static __init int tcpprobe_init(void)
  183. {
  184. int ret = -ENOMEM;
  185. init_waitqueue_head(&tcp_probe.wait);
  186. spin_lock_init(&tcp_probe.lock);
  187. if (bufsize == 0)
  188. return -EINVAL;
  189. bufsize = roundup_pow_of_two(bufsize);
  190. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  191. if (!tcp_probe.log)
  192. goto err0;
  193. if (!proc_net_fops_create(&init_net, procname, S_IRUSR, &tcpprobe_fops))
  194. goto err0;
  195. ret = register_jprobe(&tcp_jprobe);
  196. if (ret)
  197. goto err1;
  198. pr_info("TCP probe registered (port=%d) bufsize=%u\n", port, bufsize);
  199. return 0;
  200. err1:
  201. proc_net_remove(&init_net, procname);
  202. err0:
  203. kfree(tcp_probe.log);
  204. return ret;
  205. }
  206. module_init(tcpprobe_init);
  207. static __exit void tcpprobe_exit(void)
  208. {
  209. proc_net_remove(&init_net, procname);
  210. unregister_jprobe(&tcp_jprobe);
  211. kfree(tcp_probe.log);
  212. }
  213. module_exit(tcpprobe_exit);