gen_estimator.c 6.9 KB

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