tcp_probe.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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)
  93. {
  94. unsigned int len = skb->len;
  95. const struct tcp_sock *tp = tcp_sk(sk);
  96. const struct inet_sock *inet = inet_sk(sk);
  97. /* Only update if port or skb mark matches */
  98. if (((port == 0 && fwmark == 0) ||
  99. ntohs(inet->inet_dport) == port ||
  100. ntohs(inet->inet_sport) == port ||
  101. (fwmark > 0 && skb->mark == fwmark)) &&
  102. (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  103. spin_lock(&tcp_probe.lock);
  104. /* If log fills, just silently drop */
  105. if (tcp_probe_avail() > 1) {
  106. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  107. p->tstamp = ktime_get();
  108. switch (sk->sk_family) {
  109. case AF_INET:
  110. tcp_probe_copy_fl_to_si4(inet, p->src.v4, s);
  111. tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d);
  112. break;
  113. case AF_INET6:
  114. memset(&p->src.v6, 0, sizeof(p->src.v6));
  115. memset(&p->dst.v6, 0, sizeof(p->dst.v6));
  116. #if IS_ENABLED(CONFIG_IPV6)
  117. p->src.v6.sin6_family = AF_INET6;
  118. p->src.v6.sin6_port = inet->inet_sport;
  119. p->src.v6.sin6_addr = inet6_sk(sk)->saddr;
  120. p->dst.v6.sin6_family = AF_INET6;
  121. p->dst.v6.sin6_port = inet->inet_dport;
  122. p->dst.v6.sin6_addr = sk->sk_v6_daddr;
  123. #endif
  124. break;
  125. default:
  126. BUG();
  127. }
  128. p->length = len;
  129. p->snd_nxt = tp->snd_nxt;
  130. p->snd_una = tp->snd_una;
  131. p->snd_cwnd = tp->snd_cwnd;
  132. p->snd_wnd = tp->snd_wnd;
  133. p->rcv_wnd = tp->rcv_wnd;
  134. p->ssthresh = tcp_current_ssthresh(sk);
  135. p->srtt = tp->srtt_us >> 3;
  136. tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1);
  137. }
  138. tcp_probe.lastcwnd = tp->snd_cwnd;
  139. spin_unlock(&tcp_probe.lock);
  140. wake_up(&tcp_probe.wait);
  141. }
  142. jprobe_return();
  143. }
  144. static struct jprobe tcp_jprobe = {
  145. .kp = {
  146. .symbol_name = "tcp_rcv_established",
  147. },
  148. .entry = jtcp_rcv_established,
  149. };
  150. static int tcpprobe_open(struct inode *inode, struct file *file)
  151. {
  152. /* Reset (empty) log */
  153. spin_lock_bh(&tcp_probe.lock);
  154. tcp_probe.head = tcp_probe.tail = 0;
  155. tcp_probe.start = ktime_get();
  156. spin_unlock_bh(&tcp_probe.lock);
  157. return 0;
  158. }
  159. static int tcpprobe_sprint(char *tbuf, int n)
  160. {
  161. const struct tcp_log *p
  162. = tcp_probe.log + tcp_probe.tail;
  163. struct timespec64 ts
  164. = ktime_to_timespec64(ktime_sub(p->tstamp, tcp_probe.start));
  165. return scnprintf(tbuf, n,
  166. "%lu.%09lu %pISpc %pISpc %d %#x %#x %u %u %u %u %u\n",
  167. (unsigned long)ts.tv_sec,
  168. (unsigned long)ts.tv_nsec,
  169. &p->src, &p->dst, p->length, p->snd_nxt, p->snd_una,
  170. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, p->rcv_wnd);
  171. }
  172. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  173. size_t len, loff_t *ppos)
  174. {
  175. int error = 0;
  176. size_t cnt = 0;
  177. if (!buf)
  178. return -EINVAL;
  179. while (cnt < len) {
  180. char tbuf[256];
  181. int width;
  182. /* Wait for data in buffer */
  183. error = wait_event_interruptible(tcp_probe.wait,
  184. tcp_probe_used() > 0);
  185. if (error)
  186. break;
  187. spin_lock_bh(&tcp_probe.lock);
  188. if (tcp_probe.head == tcp_probe.tail) {
  189. /* multiple readers race? */
  190. spin_unlock_bh(&tcp_probe.lock);
  191. continue;
  192. }
  193. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  194. if (cnt + width < len)
  195. tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1);
  196. spin_unlock_bh(&tcp_probe.lock);
  197. /* if record greater than space available
  198. return partial buffer (so far) */
  199. if (cnt + width >= len)
  200. break;
  201. if (copy_to_user(buf + cnt, tbuf, width))
  202. return -EFAULT;
  203. cnt += width;
  204. }
  205. return cnt == 0 ? error : cnt;
  206. }
  207. static const struct file_operations tcpprobe_fops = {
  208. .owner = THIS_MODULE,
  209. .open = tcpprobe_open,
  210. .read = tcpprobe_read,
  211. .llseek = noop_llseek,
  212. };
  213. static __init int tcpprobe_init(void)
  214. {
  215. int ret = -ENOMEM;
  216. /* Warning: if the function signature of tcp_rcv_established,
  217. * has been changed, you also have to change the signature of
  218. * jtcp_rcv_established, otherwise you end up right here!
  219. */
  220. BUILD_BUG_ON(__same_type(tcp_rcv_established,
  221. jtcp_rcv_established) == 0);
  222. init_waitqueue_head(&tcp_probe.wait);
  223. spin_lock_init(&tcp_probe.lock);
  224. if (bufsize == 0)
  225. return -EINVAL;
  226. bufsize = roundup_pow_of_two(bufsize);
  227. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  228. if (!tcp_probe.log)
  229. goto err0;
  230. if (!proc_create(procname, S_IRUSR, init_net.proc_net, &tcpprobe_fops))
  231. goto err0;
  232. ret = register_jprobe(&tcp_jprobe);
  233. if (ret)
  234. goto err1;
  235. pr_info("probe registered (port=%d/fwmark=%u) bufsize=%u\n",
  236. port, fwmark, bufsize);
  237. return 0;
  238. err1:
  239. remove_proc_entry(procname, init_net.proc_net);
  240. err0:
  241. kfree(tcp_probe.log);
  242. return ret;
  243. }
  244. module_init(tcpprobe_init);
  245. static __exit void tcpprobe_exit(void)
  246. {
  247. remove_proc_entry(procname, init_net.proc_net);
  248. unregister_jprobe(&tcp_jprobe);
  249. kfree(tcp_probe.log);
  250. }
  251. module_exit(tcpprobe_exit);