gen_estimator.c 7.2 KB

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