PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/sc7731_kernel/net/ipv4/tcp_probe.c

https://gitlab.com/envieidoc/sprd_project
C | 260 lines | 184 code | 46 blank | 30 comment | 22 complexity | c64857e50e9685ec9da0ffd7e6889fa9 MD5 | raw file
  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. __be32 saddr, daddr;
  49. __be16 sport, dport;
  50. u16 length;
  51. u32 snd_nxt;
  52. u32 snd_una;
  53. u32 snd_wnd;
  54. u32 snd_cwnd;
  55. u32 ssthresh;
  56. u32 srtt;
  57. };
  58. static struct {
  59. spinlock_t lock;
  60. wait_queue_head_t wait;
  61. ktime_t start;
  62. u32 lastcwnd;
  63. unsigned long head, tail;
  64. struct tcp_log *log;
  65. } tcp_probe;
  66. static inline int tcp_probe_used(void)
  67. {
  68. return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1);
  69. }
  70. static inline int tcp_probe_avail(void)
  71. {
  72. return bufsize - tcp_probe_used() - 1;
  73. }
  74. /*
  75. * Hook inserted to be called before each receive packet.
  76. * Note: arguments must match tcp_rcv_established()!
  77. */
  78. static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  79. struct tcphdr *th, unsigned int len)
  80. {
  81. const struct tcp_sock *tp = tcp_sk(sk);
  82. const struct inet_sock *inet = inet_sk(sk);
  83. /* Only update if port matches */
  84. if ((port == 0 || ntohs(inet->inet_dport) == port ||
  85. ntohs(inet->inet_sport) == port) &&
  86. (full || tp->snd_cwnd != tcp_probe.lastcwnd)) {
  87. spin_lock(&tcp_probe.lock);
  88. /* If log fills, just silently drop */
  89. if (tcp_probe_avail() > 1) {
  90. struct tcp_log *p = tcp_probe.log + tcp_probe.head;
  91. p->tstamp = ktime_get();
  92. p->saddr = inet->inet_saddr;
  93. p->sport = inet->inet_sport;
  94. p->daddr = inet->inet_daddr;
  95. p->dport = inet->inet_dport;
  96. p->length = skb->len;
  97. p->snd_nxt = tp->snd_nxt;
  98. p->snd_una = tp->snd_una;
  99. p->snd_cwnd = tp->snd_cwnd;
  100. p->snd_wnd = tp->snd_wnd;
  101. p->ssthresh = tcp_current_ssthresh(sk);
  102. p->srtt = tp->srtt >> 3;
  103. tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1);
  104. }
  105. tcp_probe.lastcwnd = tp->snd_cwnd;
  106. spin_unlock(&tcp_probe.lock);
  107. wake_up(&tcp_probe.wait);
  108. }
  109. jprobe_return();
  110. return 0;
  111. }
  112. static struct jprobe tcp_jprobe = {
  113. .kp = {
  114. .symbol_name = "tcp_rcv_established",
  115. },
  116. .entry = jtcp_rcv_established,
  117. };
  118. static int tcpprobe_open(struct inode *inode, struct file *file)
  119. {
  120. /* Reset (empty) log */
  121. spin_lock_bh(&tcp_probe.lock);
  122. tcp_probe.head = tcp_probe.tail = 0;
  123. tcp_probe.start = ktime_get();
  124. spin_unlock_bh(&tcp_probe.lock);
  125. return 0;
  126. }
  127. static int tcpprobe_sprint(char *tbuf, int n)
  128. {
  129. const struct tcp_log *p
  130. = tcp_probe.log + tcp_probe.tail;
  131. struct timespec tv
  132. = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
  133. return scnprintf(tbuf, n,
  134. "%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
  135. (unsigned long) tv.tv_sec,
  136. (unsigned long) tv.tv_nsec,
  137. &p->saddr, ntohs(p->sport),
  138. &p->daddr, ntohs(p->dport),
  139. p->length, p->snd_nxt, p->snd_una,
  140. p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
  141. }
  142. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  143. size_t len, loff_t *ppos)
  144. {
  145. int error = 0;
  146. size_t cnt = 0;
  147. if (!buf)
  148. return -EINVAL;
  149. while (cnt < len) {
  150. char tbuf[164];
  151. int width;
  152. /* Wait for data in buffer */
  153. error = wait_event_interruptible(tcp_probe.wait,
  154. tcp_probe_used() > 0);
  155. if (error)
  156. break;
  157. spin_lock_bh(&tcp_probe.lock);
  158. if (tcp_probe.head == tcp_probe.tail) {
  159. /* multiple readers race? */
  160. spin_unlock_bh(&tcp_probe.lock);
  161. continue;
  162. }
  163. width = tcpprobe_sprint(tbuf, sizeof(tbuf));
  164. if (cnt + width < len)
  165. tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1);
  166. spin_unlock_bh(&tcp_probe.lock);
  167. /* if record greater than space available
  168. return partial buffer (so far) */
  169. if (cnt + width >= len)
  170. break;
  171. if (copy_to_user(buf + cnt, tbuf, width))
  172. return -EFAULT;
  173. cnt += width;
  174. }
  175. return cnt == 0 ? error : cnt;
  176. }
  177. static const struct file_operations tcpprobe_fops = {
  178. .owner = THIS_MODULE,
  179. .open = tcpprobe_open,
  180. .read = tcpprobe_read,
  181. .llseek = noop_llseek,
  182. };
  183. static __init int tcpprobe_init(void)
  184. {
  185. int ret = -ENOMEM;
  186. init_waitqueue_head(&tcp_probe.wait);
  187. spin_lock_init(&tcp_probe.lock);
  188. if (bufsize == 0)
  189. return -EINVAL;
  190. bufsize = roundup_pow_of_two(bufsize);
  191. tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL);
  192. if (!tcp_probe.log)
  193. goto err0;
  194. if (!proc_create(procname, S_IRUSR, init_net.proc_net, &tcpprobe_fops))
  195. goto err0;
  196. ret = register_jprobe(&tcp_jprobe);
  197. if (ret)
  198. goto err1;
  199. pr_info("probe registered (port=%d) bufsize=%u\n", port, bufsize);
  200. return 0;
  201. err1:
  202. remove_proc_entry(procname, init_net.proc_net);
  203. err0:
  204. kfree(tcp_probe.log);
  205. return ret;
  206. }
  207. module_init(tcpprobe_init);
  208. static __exit void tcpprobe_exit(void)
  209. {
  210. remove_proc_entry(procname, init_net.proc_net);
  211. unregister_jprobe(&tcp_jprobe);
  212. kfree(tcp_probe.log);
  213. }
  214. module_exit(tcpprobe_exit);