gen_estimator.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_sync *bstats;
  41. spinlock_t *stats_lock;
  42. bool running;
  43. struct gnet_stats_basic_sync __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_sync *b)
  57. {
  58. gnet_stats_basic_sync_init(b);
  59. if (e->stats_lock)
  60. spin_lock(e->stats_lock);
  61. gnet_stats_add_basic(b, e->cpu_bstats, e->bstats, e->running);
  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_sync b;
  69. u64 b_bytes, b_packets;
  70. u64 rate, brate;
  71. est_fetch_counters(est, &b);
  72. b_bytes = u64_stats_read(&b.bytes);
  73. b_packets = u64_stats_read(&b.packets);
  74. brate = (b_bytes - est->last_bytes) << (10 - est->intvl_log);
  75. brate = (brate >> est->ewma_log) - (est->avbps >> est->ewma_log);
  76. rate = (b_packets - est->last_packets) << (10 - est->intvl_log);
  77. rate = (rate >> est->ewma_log) - (est->avpps >> est->ewma_log);
  78. write_seqcount_begin(&est->seq);
  79. est->avbps += brate;
  80. est->avpps += rate;
  81. write_seqcount_end(&est->seq);
  82. est->last_bytes = b_bytes;
  83. est->last_packets = b_packets;
  84. est->next_jiffies += ((HZ/4) << est->intvl_log);
  85. if (unlikely(time_after_eq(jiffies, est->next_jiffies))) {
  86. /* Ouch... timer was delayed. */
  87. est->next_jiffies = jiffies + 1;
  88. }
  89. mod_timer(&est->timer, est->next_jiffies);
  90. }
  91. /**
  92. * gen_new_estimator - create a new rate estimator
  93. * @bstats: basic statistics
  94. * @cpu_bstats: bstats per cpu
  95. * @rate_est: rate estimator statistics
  96. * @lock: lock for statistics and control path
  97. * @running: true if @bstats represents a running qdisc, thus @bstats'
  98. * internal values might change during basic reads. Only used
  99. * if @bstats_cpu is NULL
  100. * @opt: rate estimator configuration TLV
  101. *
  102. * Creates a new rate estimator with &bstats as source and &rate_est
  103. * as destination. A new timer with the interval specified in the
  104. * configuration TLV is created. Upon each interval, the latest statistics
  105. * will be read from &bstats and the estimated rate will be stored in
  106. * &rate_est with the statistics lock grabbed during this period.
  107. *
  108. * Returns 0 on success or a negative error code.
  109. *
  110. */
  111. int gen_new_estimator(struct gnet_stats_basic_sync *bstats,
  112. struct gnet_stats_basic_sync __percpu *cpu_bstats,
  113. struct net_rate_estimator __rcu **rate_est,
  114. spinlock_t *lock,
  115. bool running,
  116. struct nlattr *opt)
  117. {
  118. struct gnet_estimator *parm = nla_data(opt);
  119. struct net_rate_estimator *old, *est;
  120. struct gnet_stats_basic_sync b;
  121. int intvl_log;
  122. if (nla_len(opt) < sizeof(*parm))
  123. return -EINVAL;
  124. /* allowed timer periods are :
  125. * -2 : 250ms, -1 : 500ms, 0 : 1 sec
  126. * 1 : 2 sec, 2 : 4 sec, 3 : 8 sec
  127. */
  128. if (parm->interval < -2 || parm->interval > 3)
  129. return -EINVAL;
  130. if (parm->ewma_log == 0 || parm->ewma_log >= 31)
  131. return -EINVAL;
  132. est = kzalloc(sizeof(*est), GFP_KERNEL);
  133. if (!est)
  134. return -ENOBUFS;
  135. seqcount_init(&est->seq);
  136. intvl_log = parm->interval + 2;
  137. est->bstats = bstats;
  138. est->stats_lock = lock;
  139. est->running = running;
  140. est->ewma_log = parm->ewma_log;
  141. est->intvl_log = intvl_log;
  142. est->cpu_bstats = cpu_bstats;
  143. if (lock)
  144. local_bh_disable();
  145. est_fetch_counters(est, &b);
  146. if (lock)
  147. local_bh_enable();
  148. est->last_bytes = u64_stats_read(&b.bytes);
  149. est->last_packets = u64_stats_read(&b.packets);
  150. if (lock)
  151. spin_lock_bh(lock);
  152. old = rcu_dereference_protected(*rate_est, 1);
  153. if (old) {
  154. del_timer_sync(&old->timer);
  155. est->avbps = old->avbps;
  156. est->avpps = old->avpps;
  157. }
  158. est->next_jiffies = jiffies + ((HZ/4) << intvl_log);
  159. timer_setup(&est->timer, est_timer, 0);
  160. mod_timer(&est->timer, est->next_jiffies);
  161. rcu_assign_pointer(*rate_est, est);
  162. if (lock)
  163. spin_unlock_bh(lock);
  164. if (old)
  165. kfree_rcu(old, rcu);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(gen_new_estimator);
  169. /**
  170. * gen_kill_estimator - remove a rate estimator
  171. * @rate_est: rate estimator
  172. *
  173. * Removes the rate estimator.
  174. *
  175. */
  176. void gen_kill_estimator(struct net_rate_estimator __rcu **rate_est)
  177. {
  178. struct net_rate_estimator *est;
  179. est = unrcu_pointer(xchg(rate_est, NULL));
  180. if (est) {
  181. timer_shutdown_sync(&est->timer);
  182. kfree_rcu(est, rcu);
  183. }
  184. }
  185. EXPORT_SYMBOL(gen_kill_estimator);
  186. /**
  187. * gen_replace_estimator - replace rate estimator configuration
  188. * @bstats: basic statistics
  189. * @cpu_bstats: bstats per cpu
  190. * @rate_est: rate estimator statistics
  191. * @lock: lock for statistics and control path
  192. * @running: true if @bstats represents a running qdisc, thus @bstats'
  193. * internal values might change during basic reads. Only used
  194. * if @cpu_bstats is NULL
  195. * @opt: rate estimator configuration TLV
  196. *
  197. * Replaces the configuration of a rate estimator by calling
  198. * gen_kill_estimator() and gen_new_estimator().
  199. *
  200. * Returns 0 on success or a negative error code.
  201. */
  202. int gen_replace_estimator(struct gnet_stats_basic_sync *bstats,
  203. struct gnet_stats_basic_sync __percpu *cpu_bstats,
  204. struct net_rate_estimator __rcu **rate_est,
  205. spinlock_t *lock,
  206. bool running, struct nlattr *opt)
  207. {
  208. return gen_new_estimator(bstats, cpu_bstats, rate_est,
  209. lock, running, opt);
  210. }
  211. EXPORT_SYMBOL(gen_replace_estimator);
  212. /**
  213. * gen_estimator_active - test if estimator is currently in use
  214. * @rate_est: rate estimator
  215. *
  216. * Returns true if estimator is active, and false if not.
  217. */
  218. bool gen_estimator_active(struct net_rate_estimator __rcu **rate_est)
  219. {
  220. return !!rcu_access_pointer(*rate_est);
  221. }
  222. EXPORT_SYMBOL(gen_estimator_active);
  223. bool gen_estimator_read(struct net_rate_estimator __rcu **rate_est,
  224. struct gnet_stats_rate_est64 *sample)
  225. {
  226. struct net_rate_estimator *est;
  227. unsigned seq;
  228. rcu_read_lock();
  229. est = rcu_dereference(*rate_est);
  230. if (!est) {
  231. rcu_read_unlock();
  232. return false;
  233. }
  234. do {
  235. seq = read_seqcount_begin(&est->seq);
  236. sample->bps = est->avbps >> 8;
  237. sample->pps = est->avpps >> 8;
  238. } while (read_seqcount_retry(&est->seq, seq));
  239. rcu_read_unlock();
  240. return true;
  241. }
  242. EXPORT_SYMBOL(gen_estimator_read);