/net/core/gen_estimator.c

http://github.com/mirrors/linux · C · 267 lines · 166 code · 34 blank · 67 comment · 16 complexity · 47b33b59cf41440a165c41ed8b7ec51f MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/gen_estimator.c Simple rate estimator.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. * Eric Dumazet <edumazet@google.com>
  7. *
  8. * Changes:
  9. * Jamal Hadi Salim - moved it to net/core and reshulfed
  10. * names to make it usable in general net subsystem.
  11. */
  12. #include <linux/uaccess.h>
  13. #include <linux/bitops.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/in.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/seqlock.h>
  31. #include <net/sock.h>
  32. #include <net/gen_stats.h>
  33. /* This code is NOT intended to be used for statistics collection,
  34. * its purpose is to provide a base for statistical multiplexing
  35. * for controlled load service.
  36. * If you need only statistics, run a user level daemon which
  37. * periodically reads byte counters.
  38. */
  39. struct net_rate_estimator {
  40. struct gnet_stats_basic_packed *bstats;
  41. spinlock_t *stats_lock;
  42. seqcount_t *running;
  43. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  44. u8 ewma_log;
  45. u8 intvl_log; /* period : (250ms << intvl_log) */
  46. seqcount_t seq;
  47. u64 last_packets;
  48. u64 last_bytes;
  49. u64 avpps;
  50. u64 avbps;
  51. unsigned long next_jiffies;
  52. struct timer_list timer;
  53. struct rcu_head rcu;
  54. };
  55. static void est_fetch_counters(struct net_rate_estimator *e,
  56. struct gnet_stats_basic_packed *b)
  57. {
  58. memset(b, 0, sizeof(*b));
  59. if (e->stats_lock)
  60. spin_lock(e->stats_lock);
  61. __gnet_stats_copy_basic(e->running, b, e->cpu_bstats, e->bstats);
  62. if (e->stats_lock)
  63. spin_unlock(e->stats_lock);
  64. }
  65. static void est_timer(struct timer_list *t)
  66. {
  67. struct net_rate_estimator *est = from_timer(est, t, timer);
  68. struct gnet_stats_basic_packed b;
  69. u64 rate, brate;
  70. est_fetch_counters(est, &b);
  71. brate = (b.bytes - est->last_bytes) << (10 - est->ewma_log - est->intvl_log);
  72. brate -= (est->avbps >> est->ewma_log);
  73. rate = (b.packets - est->last_packets) << (10 - est->ewma_log - est->intvl_log);
  74. rate -= (est->avpps >> est->ewma_log);
  75. write_seqcount_begin(&est->seq);
  76. est->avbps += brate;
  77. est->avpps += rate;
  78. write_seqcount_end(&est->seq);
  79. est->last_bytes = b.bytes;
  80. est->last_packets = b.packets;
  81. est->next_jiffies += ((HZ/4) << est->intvl_log);
  82. if (unlikely(time_after_eq(jiffies, est->next_jiffies))) {
  83. /* Ouch... timer was delayed. */
  84. est->next_jiffies = jiffies + 1;
  85. }
  86. mod_timer(&est->timer, est->next_jiffies);
  87. }
  88. /**
  89. * gen_new_estimator - create a new rate estimator
  90. * @bstats: basic statistics
  91. * @cpu_bstats: bstats per cpu
  92. * @rate_est: rate estimator statistics
  93. * @lock: lock for statistics and control path
  94. * @running: qdisc running seqcount
  95. * @opt: rate estimator configuration TLV
  96. *
  97. * Creates a new rate estimator with &bstats as source and &rate_est
  98. * as destination. A new timer with the interval specified in the
  99. * configuration TLV is created. Upon each interval, the latest statistics
  100. * will be read from &bstats and the estimated rate will be stored in
  101. * &rate_est with the statistics lock grabbed during this period.
  102. *
  103. * Returns 0 on success or a negative error code.
  104. *
  105. */
  106. int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
  107. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  108. struct net_rate_estimator __rcu **rate_est,
  109. spinlock_t *lock,
  110. seqcount_t *running,
  111. struct nlattr *opt)
  112. {
  113. struct gnet_estimator *parm = nla_data(opt);
  114. struct net_rate_estimator *old, *est;
  115. struct gnet_stats_basic_packed b;
  116. int intvl_log;
  117. if (nla_len(opt) < sizeof(*parm))
  118. return -EINVAL;
  119. /* allowed timer periods are :
  120. * -2 : 250ms, -1 : 500ms, 0 : 1 sec
  121. * 1 : 2 sec, 2 : 4 sec, 3 : 8 sec
  122. */
  123. if (parm->interval < -2 || parm->interval > 3)
  124. return -EINVAL;
  125. est = kzalloc(sizeof(*est), GFP_KERNEL);
  126. if (!est)
  127. return -ENOBUFS;
  128. seqcount_init(&est->seq);
  129. intvl_log = parm->interval + 2;
  130. est->bstats = bstats;
  131. est->stats_lock = lock;
  132. est->running = running;
  133. est->ewma_log = parm->ewma_log;
  134. est->intvl_log = intvl_log;
  135. est->cpu_bstats = cpu_bstats;
  136. if (lock)
  137. local_bh_disable();
  138. est_fetch_counters(est, &b);
  139. if (lock)
  140. local_bh_enable();
  141. est->last_bytes = b.bytes;
  142. est->last_packets = b.packets;
  143. if (lock)
  144. spin_lock_bh(lock);
  145. old = rcu_dereference_protected(*rate_est, 1);
  146. if (old) {
  147. del_timer_sync(&old->timer);
  148. est->avbps = old->avbps;
  149. est->avpps = old->avpps;
  150. }
  151. est->next_jiffies = jiffies + ((HZ/4) << intvl_log);
  152. timer_setup(&est->timer, est_timer, 0);
  153. mod_timer(&est->timer, est->next_jiffies);
  154. rcu_assign_pointer(*rate_est, est);
  155. if (lock)
  156. spin_unlock_bh(lock);
  157. if (old)
  158. kfree_rcu(old, rcu);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL(gen_new_estimator);
  162. /**
  163. * gen_kill_estimator - remove a rate estimator
  164. * @rate_est: rate estimator
  165. *
  166. * Removes the rate estimator.
  167. *
  168. */
  169. void gen_kill_estimator(struct net_rate_estimator __rcu **rate_est)
  170. {
  171. struct net_rate_estimator *est;
  172. est = xchg((__force struct net_rate_estimator **)rate_est, NULL);
  173. if (est) {
  174. del_timer_sync(&est->timer);
  175. kfree_rcu(est, rcu);
  176. }
  177. }
  178. EXPORT_SYMBOL(gen_kill_estimator);
  179. /**
  180. * gen_replace_estimator - replace rate estimator configuration
  181. * @bstats: basic statistics
  182. * @cpu_bstats: bstats per cpu
  183. * @rate_est: rate estimator statistics
  184. * @lock: lock for statistics and control path
  185. * @running: qdisc running seqcount (might be NULL)
  186. * @opt: rate estimator configuration TLV
  187. *
  188. * Replaces the configuration of a rate estimator by calling
  189. * gen_kill_estimator() and gen_new_estimator().
  190. *
  191. * Returns 0 on success or a negative error code.
  192. */
  193. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  194. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  195. struct net_rate_estimator __rcu **rate_est,
  196. spinlock_t *lock,
  197. seqcount_t *running, struct nlattr *opt)
  198. {
  199. return gen_new_estimator(bstats, cpu_bstats, rate_est,
  200. lock, running, opt);
  201. }
  202. EXPORT_SYMBOL(gen_replace_estimator);
  203. /**
  204. * gen_estimator_active - test if estimator is currently in use
  205. * @rate_est: rate estimator
  206. *
  207. * Returns true if estimator is active, and false if not.
  208. */
  209. bool gen_estimator_active(struct net_rate_estimator __rcu **rate_est)
  210. {
  211. return !!rcu_access_pointer(*rate_est);
  212. }
  213. EXPORT_SYMBOL(gen_estimator_active);
  214. bool gen_estimator_read(struct net_rate_estimator __rcu **rate_est,
  215. struct gnet_stats_rate_est64 *sample)
  216. {
  217. struct net_rate_estimator *est;
  218. unsigned seq;
  219. rcu_read_lock();
  220. est = rcu_dereference(*rate_est);
  221. if (!est) {
  222. rcu_read_unlock();
  223. return false;
  224. }
  225. do {
  226. seq = read_seqcount_begin(&est->seq);
  227. sample->bps = est->avbps >> 8;
  228. sample->pps = est->avpps >> 8;
  229. } while (read_seqcount_retry(&est->seq, seq));
  230. rcu_read_unlock();
  231. return true;
  232. }
  233. EXPORT_SYMBOL(gen_estimator_read);