gen_estimator.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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->intvl_log);
  72. brate = (brate >> est->ewma_log) - (est->avbps >> est->ewma_log);
  73. rate = (b.packets - est->last_packets) << (10 - est->intvl_log);
  74. rate = (rate >> est->ewma_log) - (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. if (parm->ewma_log == 0 || parm->ewma_log >= 31)
  126. return -EINVAL;
  127. est = kzalloc(sizeof(*est), GFP_KERNEL);
  128. if (!est)
  129. return -ENOBUFS;
  130. seqcount_init(&est->seq);
  131. intvl_log = parm->interval + 2;
  132. est->bstats = bstats;
  133. est->stats_lock = lock;
  134. est->running = running;
  135. est->ewma_log = parm->ewma_log;
  136. est->intvl_log = intvl_log;
  137. est->cpu_bstats = cpu_bstats;
  138. if (lock)
  139. local_bh_disable();
  140. est_fetch_counters(est, &b);
  141. if (lock)
  142. local_bh_enable();
  143. est->last_bytes = b.bytes;
  144. est->last_packets = b.packets;
  145. if (lock)
  146. spin_lock_bh(lock);
  147. old = rcu_dereference_protected(*rate_est, 1);
  148. if (old) {
  149. del_timer_sync(&old->timer);
  150. est->avbps = old->avbps;
  151. est->avpps = old->avpps;
  152. }
  153. est->next_jiffies = jiffies + ((HZ/4) << intvl_log);
  154. timer_setup(&est->timer, est_timer, 0);
  155. mod_timer(&est->timer, est->next_jiffies);
  156. rcu_assign_pointer(*rate_est, est);
  157. if (lock)
  158. spin_unlock_bh(lock);
  159. if (old)
  160. kfree_rcu(old, rcu);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL(gen_new_estimator);
  164. /**
  165. * gen_kill_estimator - remove a rate estimator
  166. * @rate_est: rate estimator
  167. *
  168. * Removes the rate estimator.
  169. *
  170. */
  171. void gen_kill_estimator(struct net_rate_estimator __rcu **rate_est)
  172. {
  173. struct net_rate_estimator *est;
  174. est = xchg((__force struct net_rate_estimator **)rate_est, NULL);
  175. if (est) {
  176. del_timer_sync(&est->timer);
  177. kfree_rcu(est, rcu);
  178. }
  179. }
  180. EXPORT_SYMBOL(gen_kill_estimator);
  181. /**
  182. * gen_replace_estimator - replace rate estimator configuration
  183. * @bstats: basic statistics
  184. * @cpu_bstats: bstats per cpu
  185. * @rate_est: rate estimator statistics
  186. * @lock: lock for statistics and control path
  187. * @running: qdisc running seqcount (might be NULL)
  188. * @opt: rate estimator configuration TLV
  189. *
  190. * Replaces the configuration of a rate estimator by calling
  191. * gen_kill_estimator() and gen_new_estimator().
  192. *
  193. * Returns 0 on success or a negative error code.
  194. */
  195. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  196. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  197. struct net_rate_estimator __rcu **rate_est,
  198. spinlock_t *lock,
  199. seqcount_t *running, struct nlattr *opt)
  200. {
  201. return gen_new_estimator(bstats, cpu_bstats, rate_est,
  202. lock, running, opt);
  203. }
  204. EXPORT_SYMBOL(gen_replace_estimator);
  205. /**
  206. * gen_estimator_active - test if estimator is currently in use
  207. * @rate_est: rate estimator
  208. *
  209. * Returns true if estimator is active, and false if not.
  210. */
  211. bool gen_estimator_active(struct net_rate_estimator __rcu **rate_est)
  212. {
  213. return !!rcu_access_pointer(*rate_est);
  214. }
  215. EXPORT_SYMBOL(gen_estimator_active);
  216. bool gen_estimator_read(struct net_rate_estimator __rcu **rate_est,
  217. struct gnet_stats_rate_est64 *sample)
  218. {
  219. struct net_rate_estimator *est;
  220. unsigned seq;
  221. rcu_read_lock();
  222. est = rcu_dereference(*rate_est);
  223. if (!est) {
  224. rcu_read_unlock();
  225. return false;
  226. }
  227. do {
  228. seq = read_seqcount_begin(&est->seq);
  229. sample->bps = est->avbps >> 8;
  230. sample->pps = est->avpps >> 8;
  231. } while (read_seqcount_retry(&est->seq, seq));
  232. rcu_read_unlock();
  233. return true;
  234. }
  235. EXPORT_SYMBOL(gen_estimator_read);