tcp_probe.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/socket.h>
  24. #include <linux/tcp.h>
  25. #include <linux/slab.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/module.h>
  28. #include <linux/ktime.h>
  29. #include <linux/time.h>
  30. #include <net/net_namespace.h>
  31. #include <net/tcp.h>
  32. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  33. MODULE_DESCRIPTION("TCP cwnd snooper");
  34. MODULE_LICENSE("GPL");
  35. MODULE_VERSION("1.1");
  36. static int port __read_mostly;
  37. MODULE_PARM_DESC(port, "Port to match (0=all)");
  38. module_param(port, int, 0);
  39. static unsigned int bufsize __read_mostly = 4096;
  40. MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)");
  41. module_param(bufsize, uint, 0);
  42. static unsigned int fwmark __read_mostly;
  43. MODULE_PARM_DESC(fwmark, "skb mark to match (0=no mark)");
  44. module_param(fwmark, uint, 0);
  45. static int full __read_mostly;
  46. MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
  47. module_param(full, int, 0);
  48. static const char procname[] = "tcpprobe";
  49. struct tcp_log {
  50. ktime_t tstamp;
  51. union {
  52. struct sockaddr raw;
  53. struct sockaddr_in v4;
  54. struct sockaddr_in6 v6;
  55. } src, dst;
  56. u16 length;
  57. u32 snd_nxt;
  58. u32 snd_una;
  59. u32 snd_wnd;
  60. u32 rcv_wnd;
  61. u32 snd_cwnd;
  62. u32 ssthresh;
  63. u32 srtt;
  64. };
  65. static struct {
  66. spinlock_t lock;
  67. wait_queue_head_t wait;
  68. ktime_t start;
  69. u32 lastcwnd;
  70. unsigned long head, tail;
  71. struct tcp_log *log;
  72. } tcp_probe;
  73. static inline int tcp_probe_used(void)
  74. {
  75. return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1);
  76. }
  77. static inline int tcp_probe_avail(void)
  78. {
  79. return bufsize - tcp_probe_used() - 1;
  80. }
  81. #define tcp_probe_copy_fl_to_si4(inet, si4, mem) \
  82. do { \
  83. si4.sin_family = AF_INET; \
  84. si4.sin_port = inet->inet_##mem##port; \
  85. si4.sin_addr.s_addr = inet->inet_##mem##addr; \
  86. } while (0) \
  87. /*
  88. * Hook inserted to be called before each receive packet.
  89. * Note: arguments must match tcp_rcv_established()!
  90. */
  91. static void jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  92. const struct tcphdr *th, unsigned int len)
  93. {
  94. const struct tcp_sock *tp = tcp_sk(sk);
  95. const struct inet_sock *inet = inet_sk(sk);
  96. /* Only update if port or skb mark matches */
  97. if (((port == 0 && fwmark == 0) ||
  98. ntohs(inet->inet_dport) == port ||
  99. ntohs(inet->inet_sport) == port ||
  100. (fwmark > 0 && skb->mark == fwmark)) &&
  101. (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  102. spin_lock(&tcp_probe.lock);
  103. /* If log fills, just silently drop */
  104. if (tcp_probe_avail() > 1) {
  105. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  106. p->tstamp = ktime_get();
  107. switch (sk->sk_family) {
  108. case AF_INET:
  109. tcp_probe_copy_fl_to_si4(inet, p->src.v4, s);
  110. tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d);
  111. break;
  112. case AF_INET6:
  113. memset(&p->src.v6, 0, sizeof(p->src.v6));
  114. memset(&p->dst.v6, 0, sizeof(p->dst.v6));
  115. #if IS_ENABLED(CONFIG_IPV6)
  116. p->src.v6.sin6_family = AF_INET6;
  117. p->src.v6.sin6_port = inet->inet_sport;
  118. p->src.v6.sin6_addr = inet6_sk(sk)->saddr;
  119. p->dst.v6.sin6_family = AF_INET6;
  120. p->dst.v6.sin6_port = inet->inet_dport;
  121. p->dst.v6.sin6_addr = sk->sk_v6_daddr;
  122. #endif
  123. break;
  124. default:
  125. BUG();
  126. }
  127. p->length = skb->len;
  128. p->snd_nxt = tp->snd_nxt;
  129. p->snd_una = tp->snd_una;
  130. p->snd_cwnd = tp->snd_cwnd;
  131. p->snd_wnd = tp->snd_wnd;
  132. p->rcv_wnd = tp->rcv_wnd;
  133. p->ssthresh = tcp_current_ssthresh(sk);
  134. p->srtt = tp->srtt_us >> 3;
  135. tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1);
  136. }
  137. tcp_probe.lastcwnd = tp->snd_cwnd;
  138. spin_unlock(&tcp_probe.lock);
  139. wake_up(&tcp_probe.wait);
  140. }
  141. jprobe_return();
  142. }
  143. static struct jprobe tcp_jprobe = {
  144. .kp = {
  145. .symbol_name = "tcp_rcv_established",
  146. },
  147. .entry = jtcp_rcv_established,
  148. };
  149. static int tcpprobe_open(struct inode *inode, struct file *file)
  150. {
  151. /* Reset (empty) log */
  152. spin_lock_bh(&tcp_probe.lock);
  153. tcp_probe.head = tcp_probe.tail = 0;
  154. tcp_probe.start = ktime_get();
  155. spin_unlock_bh(&tcp_probe.lock);
  156. return 0;
  157. }
  158. static int tcpprobe_sprint(char *tbuf, int n)
  159. {
  160. const struct tcp_log *p
  161. = tcp_probe.log + tcp_probe.tail;
  162. struct timespec tv
  163. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  164. return scnprintf(tbuf, n,
  165. "%lu.%09lu %pISpc %pISpc %d %#x %#x %u %u %u %u %u\n",
  166. (unsigned long)tv.tv_sec,
  167. (unsigned long)tv.tv_nsec,
  168. &p->src, &p->dst, p->length, p->snd_nxt, p->snd_una,
  169. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, p->rcv_wnd);
  170. }
  171. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  172. size_t len, loff_t *ppos)
  173. {
  174. int error = 0;
  175. size_t cnt = 0;
  176. if (!buf)
  177. return -EINVAL;
  178. while (cnt < len) {
  179. char tbuf[256];
  180. int width;
  181. /* Wait for data in buffer */
  182. error = wait_event_interruptible(tcp_probe.wait,
  183. tcp_probe_used() > 0);
  184. if (error)
  185. break;
  186. spin_lock_bh(&tcp_probe.lock);
  187. if (tcp_probe.head == tcp_probe.tail) {
  188. /* multiple readers race? */
  189. spin_unlock_bh(&tcp_probe.lock);
  190. continue;
  191. }
  192. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  193. if (cnt + width < len)
  194. tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1);
  195. spin_unlock_bh(&tcp_probe.lock);
  196. /* if record greater than space available
  197. return partial buffer (so far) */
  198. if (cnt + width >= len)
  199. break;
  200. if (copy_to_user(buf + cnt, tbuf, width))
  201. return -EFAULT;
  202. cnt += width;
  203. }
  204. return cnt == 0 ? error : cnt;
  205. }
  206. static const struct file_operations tcpprobe_fops = {
  207. .owner = THIS_MODULE,
  208. .open = tcpprobe_open,
  209. .read = tcpprobe_read,
  210. .llseek = noop_llseek,
  211. };
  212. static __init int tcpprobe_init(void)
  213. {
  214. int ret = -ENOMEM;
  215. /* Warning: if the function signature of tcp_rcv_established,
  216. * has been changed, you also have to change the signature of
  217. * jtcp_rcv_established, otherwise you end up right here!
  218. */
  219. BUILD_BUG_ON(__same_type(tcp_rcv_established,
  220. jtcp_rcv_established) == 0);
  221. init_waitqueue_head(&tcp_probe.wait);
  222. spin_lock_init(&tcp_probe.lock);
  223. if (bufsize == 0)
  224. return -EINVAL;
  225. bufsize = roundup_pow_of_two(bufsize);
  226. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  227. if (!tcp_probe.log)
  228. goto err0;
  229. if (!proc_create(procname, S_IRUSR, init_net.proc_net, &tcpprobe_fops))
  230. goto err0;
  231. ret = register_jprobe(&tcp_jprobe);
  232. if (ret)
  233. goto err1;
  234. pr_info("probe registered (port=%d/fwmark=%u) bufsize=%u\n",
  235. port, fwmark, bufsize);
  236. return 0;
  237. err1:
  238. remove_proc_entry(procname, init_net.proc_net);
  239. err0:
  240. kfree(tcp_probe.log);
  241. return ret;
  242. }
  243. module_init(tcpprobe_init);
  244. static __exit void tcpprobe_exit(void)
  245. {
  246. remove_proc_entry(procname, init_net.proc_net);
  247. unregister_jprobe(&tcp_jprobe);
  248. kfree(tcp_probe.log);
  249. }
  250. module_exit(tcpprobe_exit);