gen_estimator.c 7.4 KB

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