tcp_probe.c 7.6 KB

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