tcp_probe.c 5.9 KB

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