gen_estimator.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. *
  11. * Changes:
  12. * Jamal Hadi Salim - moved it to net/core and reshulfed
  13. * names to make it usable in general net subsystem.
  14. */
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/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 <net/sock.h>
  34. #include <net/gen_stats.h>
  35. /*
  36. This code is NOT intended to be used for statistics collection,
  37. its purpose is to provide a base for statistical multiplexing
  38. for controlled load service.
  39. If you need only statistics, run a user level daemon which
  40. periodically reads byte counters.
  41. Unfortunately, rate estimation is not a very easy task.
  42. F.e. I did not find a simple way to estimate the current peak rate
  43. and even failed to formulate the problem 8)8)
  44. So I preferred not to built an estimator into the scheduler,
  45. but run this task separately.
  46. Ideally, it should be kernel thread(s), but for now it runs
  47. from timers, which puts apparent top bounds on the number of rated
  48. flows, has minimal overhead on small, but is enough
  49. to handle controlled load service, sets of aggregates.
  50. We measure rate over A=(1<<interval) seconds and evaluate EWMA:
  51. avrate = avrate*(1-W) + rate*W
  52. where W is chosen as negative power of 2: W = 2^(-ewma_log)
  53. The resulting time constant is:
  54. T = A/(-ln(1-W))
  55. NOTES.
  56. * The stored value for avbps is scaled by 2^5, so that maximal
  57. rate is ~1Gbit, avpps is scaled by 2^10.
  58. * Minimal interval is HZ/4=250msec (it is the greatest common divisor
  59. for HZ=100 and HZ=1024 8)), maximal interval
  60. is (HZ*2^EST_MAX_INTERVAL)/4 = 8sec. Shorter intervals
  61. are too expensive, longer ones can be implemented
  62. at user level painlessly.
  63. */
  64. #define EST_MAX_INTERVAL 5
  65. struct gen_estimator
  66. {
  67. struct list_head list;
  68. struct gnet_stats_basic *bstats;
  69. struct gnet_stats_rate_est *rate_est;
  70. spinlock_t *stats_lock;
  71. int ewma_log;
  72. u64 last_bytes;
  73. u32 last_packets;
  74. u32 avpps;
  75. u32 avbps;
  76. struct rcu_head e_rcu;
  77. };
  78. struct gen_estimator_head
  79. {
  80. struct timer_list timer;
  81. struct list_head list;
  82. };
  83. static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
  84. /* Protects against NULL dereference */
  85. static DEFINE_RWLOCK(est_lock);
  86. static void est_timer(unsigned long arg)
  87. {
  88. int idx = (int)arg;
  89. struct gen_estimator *e;
  90. rcu_read_lock();
  91. list_for_each_entry_rcu(e, &elist[idx].list, list) {
  92. u64 nbytes;
  93. u32 npackets;
  94. u32 rate;
  95. spin_lock(e->stats_lock);
  96. read_lock(&est_lock);
  97. if (e->bstats == NULL)
  98. goto skip;
  99. nbytes = e->bstats->bytes;
  100. npackets = e->bstats->packets;
  101. rate = (nbytes - e->last_bytes)<<(7 - idx);
  102. e->last_bytes = nbytes;
  103. e->avbps += ((long)rate - (long)e->avbps) >> e->ewma_log;
  104. e->rate_est->bps = (e->avbps+0xF)>>5;
  105. rate = (npackets - e->last_packets)<<(12 - idx);
  106. e->last_packets = npackets;
  107. e->avpps += ((long)rate - (long)e->avpps) >> e->ewma_log;
  108. e->rate_est->pps = (e->avpps+0x1FF)>>10;
  109. skip:
  110. read_unlock(&est_lock);
  111. spin_unlock(e->stats_lock);
  112. }
  113. if (!list_empty(&elist[idx].list))
  114. mod_timer(&elist[idx].timer, jiffies + ((HZ<<idx)/4));
  115. rcu_read_unlock();
  116. }
  117. /**
  118. * gen_new_estimator - create a new rate estimator
  119. * @bstats: basic statistics
  120. * @rate_est: rate estimator statistics
  121. * @stats_lock: statistics lock
  122. * @opt: rate estimator configuration TLV
  123. *
  124. * Creates a new rate estimator with &bstats as source and &rate_est
  125. * as destination. A new timer with the interval specified in the
  126. * configuration TLV is created. Upon each interval, the latest statistics
  127. * will be read from &bstats and the estimated rate will be stored in
  128. * &rate_est with the statistics lock grabed during this period.
  129. *
  130. * Returns 0 on success or a negative error code.
  131. *
  132. * NOTE: Called under rtnl_mutex
  133. */
  134. int gen_new_estimator(struct gnet_stats_basic *bstats,
  135. struct gnet_stats_rate_est *rate_est,
  136. spinlock_t *stats_lock,
  137. struct rtattr *opt)
  138. {
  139. struct gen_estimator *est;
  140. struct gnet_estimator *parm = RTA_DATA(opt);
  141. int idx;
  142. if (RTA_PAYLOAD(opt) < sizeof(*parm))
  143. return -EINVAL;
  144. if (parm->interval < -2 || parm->interval > 3)
  145. return -EINVAL;
  146. est = kzalloc(sizeof(*est), GFP_KERNEL);
  147. if (est == NULL)
  148. return -ENOBUFS;
  149. idx = parm->interval + 2;
  150. est->bstats = bstats;
  151. est->rate_est = rate_est;
  152. est->stats_lock = stats_lock;
  153. est->ewma_log = parm->ewma_log;
  154. est->last_bytes = bstats->bytes;
  155. est->avbps = rate_est->bps<<5;
  156. est->last_packets = bstats->packets;
  157. est->avpps = rate_est->pps<<10;
  158. if (!elist[idx].timer.function) {
  159. INIT_LIST_HEAD(&elist[idx].list);
  160. setup_timer(&elist[idx].timer, est_timer, idx);
  161. }
  162. if (list_empty(&elist[idx].list))
  163. mod_timer(&elist[idx].timer, jiffies + ((HZ<<idx)/4));
  164. list_add_rcu(&est->list, &elist[idx].list);
  165. return 0;
  166. }
  167. static void __gen_kill_estimator(struct rcu_head *head)
  168. {
  169. struct gen_estimator *e = container_of(head,
  170. struct gen_estimator, e_rcu);
  171. kfree(e);
  172. }
  173. /**
  174. * gen_kill_estimator - remove a rate estimator
  175. * @bstats: basic statistics
  176. * @rate_est: rate estimator statistics
  177. *
  178. * Removes the rate estimator specified by &bstats and &rate_est
  179. * and deletes the timer.
  180. *
  181. * NOTE: Called under rtnl_mutex
  182. */
  183. void gen_kill_estimator(struct gnet_stats_basic *bstats,
  184. struct gnet_stats_rate_est *rate_est)
  185. {
  186. int idx;
  187. struct gen_estimator *e, *n;
  188. for (idx=0; idx <= EST_MAX_INTERVAL; idx++) {
  189. /* Skip non initialized indexes */
  190. if (!elist[idx].timer.function)
  191. continue;
  192. list_for_each_entry_safe(e, n, &elist[idx].list, list) {
  193. if (e->rate_est != rate_est || e->bstats != bstats)
  194. continue;
  195. write_lock_bh(&est_lock);
  196. e->bstats = NULL;
  197. write_unlock_bh(&est_lock);
  198. list_del_rcu(&e->list);
  199. call_rcu(&e->e_rcu, __gen_kill_estimator);
  200. }
  201. }
  202. }
  203. /**
  204. * gen_replace_estimator - replace rate estimator configruation
  205. * @bstats: basic statistics
  206. * @rate_est: rate estimator statistics
  207. * @stats_lock: statistics lock
  208. * @opt: rate estimator configuration TLV
  209. *
  210. * Replaces the configuration of a rate estimator by calling
  211. * gen_kill_estimator() and gen_new_estimator().
  212. *
  213. * Returns 0 on success or a negative error code.
  214. */
  215. int
  216. gen_replace_estimator(struct gnet_stats_basic *bstats,
  217. struct gnet_stats_rate_est *rate_est, spinlock_t *stats_lock,
  218. struct rtattr *opt)
  219. {
  220. gen_kill_estimator(bstats, rate_est);
  221. return gen_new_estimator(bstats, rate_est, stats_lock, opt);
  222. }
  223. EXPORT_SYMBOL(gen_kill_estimator);
  224. EXPORT_SYMBOL(gen_new_estimator);
  225. EXPORT_SYMBOL(gen_replace_estimator);