tcp_probe.c 5.9 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/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->ssthresh = tcp_current_ssthresh(sk);
  98. p->srtt = tp->srtt >> 3;
  99. tcp_probe.head = (tcp_probe.head + 1) % bufsize;
  100. }
  101. tcp_probe.lastcwnd = tp->snd_cwnd;
  102. spin_unlock(&tcp_probe.lock);
  103. wake_up(&tcp_probe.wait);
  104. }
  105. jprobe_return();
  106. return 0;
  107. }
  108. static struct jprobe tcp_jprobe = {
  109. .kp = {
  110. .symbol_name = "tcp_rcv_established",
  111. },
  112. .entry = JPROBE_ENTRY(jtcp_rcv_established),
  113. };
  114. static int tcpprobe_open(struct inode * inode, struct file * file)
  115. {
  116. /* Reset (empty) log */
  117. spin_lock_bh(&tcp_probe.lock);
  118. tcp_probe.head = tcp_probe.tail = 0;
  119. tcp_probe.start = ktime_get();
  120. spin_unlock_bh(&tcp_probe.lock);
  121. return 0;
  122. }
  123. static int tcpprobe_sprint(char *tbuf, int n)
  124. {
  125. const struct tcp_log *p
  126. = tcp_probe.log + tcp_probe.tail % bufsize;
  127. struct timespec tv
  128. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  129. return snprintf(tbuf, n,
  130. "%lu.%09lu %d.%d.%d.%d:%u %d.%d.%d.%d:%u"
  131. " %d %#x %#x %u %u %u %u\n",
  132. (unsigned long) tv.tv_sec,
  133. (unsigned long) tv.tv_nsec,
  134. NIPQUAD(p->saddr), ntohs(p->sport),
  135. NIPQUAD(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, cnt = 0;
  143. if (!buf || len < 0)
  144. return -EINVAL;
  145. while (cnt < len) {
  146. char tbuf[128];
  147. int width;
  148. /* Wait for data in buffer */
  149. error = wait_event_interruptible(tcp_probe.wait,
  150. tcp_probe_used() > 0);
  151. if (error)
  152. break;
  153. spin_lock_bh(&tcp_probe.lock);
  154. if (tcp_probe.head == tcp_probe.tail) {
  155. /* multiple readers race? */
  156. spin_unlock_bh(&tcp_probe.lock);
  157. continue;
  158. }
  159. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  160. if (width < len)
  161. tcp_probe.tail = (tcp_probe.tail + 1) % bufsize;
  162. spin_unlock_bh(&tcp_probe.lock);
  163. /* if record greater than space available
  164. return partial buffer (so far) */
  165. if (width >= len)
  166. break;
  167. error = copy_to_user(buf + cnt, tbuf, width);
  168. if (error)
  169. break;
  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(sizeof(struct tcp_log), bufsize, GFP_KERNEL);
  187. if (!tcp_probe.log)
  188. goto err0;
  189. if (!proc_net_fops_create(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(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(procname);
  206. unregister_jprobe(&tcp_jprobe);
  207. kfree(tcp_probe.log);
  208. }
  209. module_exit(tcpprobe_exit);