gen_estimator.c 7.1 KB

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