tcp_probe.c 7.4 KB

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