gen_estimator.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <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/rbtree.h>
  34. #include <net/sock.h>
  35. #include <net/gen_stats.h>
  36. /*
  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. Unfortunately, rate estimation is not a very easy task.
  43. F.e. I did not find a simple way to estimate the current peak rate
  44. and even failed to formulate the problem 8)8)
  45. So I preferred not to built an estimator into the scheduler,
  46. but run this task separately.
  47. Ideally, it should be kernel thread(s), but for now it runs
  48. from timers, which puts apparent top bounds on the number of rated
  49. flows, has minimal overhead on small, but is enough
  50. to handle controlled load service, sets of aggregates.
  51. We measure rate over A=(1<<interval) seconds and evaluate EWMA:
  52. avrate = avrate*(1-W) + rate*W
  53. where W is chosen as negative power of 2: W = 2^(-ewma_log)
  54. The resulting time constant is:
  55. T = A/(-ln(1-W))
  56. NOTES.
  57. * The stored value for avbps is scaled by 2^5, so that maximal
  58. rate is ~1Gbit, avpps is scaled by 2^10.
  59. * Minimal interval is HZ/4=250msec (it is the greatest common divisor
  60. for HZ=100 and HZ=1024 8)), maximal interval
  61. is (HZ*2^EST_MAX_INTERVAL)/4 = 8sec. Shorter intervals
  62. are too expensive, longer ones can be implemented
  63. at user level painlessly.
  64. */
  65. #define EST_MAX_INTERVAL 5
  66. struct gen_estimator
  67. {
  68. struct list_head list;
  69. struct gnet_stats_basic *bstats;
  70. struct gnet_stats_rate_est *rate_est;
  71. spinlock_t *stats_lock;
  72. int ewma_log;
  73. u64 last_bytes;
  74. u32 last_packets;
  75. u32 avpps;
  76. u32 avbps;
  77. struct rcu_head e_rcu;
  78. struct rb_node node;
  79. };
  80. struct gen_estimator_head
  81. {
  82. struct timer_list timer;
  83. struct list_head list;
  84. };
  85. static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
  86. /* Protects against NULL dereference */
  87. static DEFINE_RWLOCK(est_lock);
  88. /* Protects against soft lockup during large deletion */
  89. static struct rb_root est_root = RB_ROOT;
  90. static void est_timer(unsigned long arg)
  91. {
  92. int idx = (int)arg;
  93. struct gen_estimator *e;
  94. rcu_read_lock();
  95. list_for_each_entry_rcu(e, &elist[idx].list, list) {
  96. u64 nbytes;
  97. u32 npackets;
  98. u32 rate;
  99. spin_lock(e->stats_lock);
  100. read_lock(&est_lock);
  101. if (e->bstats == NULL)
  102. goto skip;
  103. nbytes = e->bstats->bytes;
  104. npackets = e->bstats->packets;
  105. rate = (nbytes - e->last_bytes)<<(7 - idx);
  106. e->last_bytes = nbytes;
  107. e->avbps += ((long)rate - (long)e->avbps) >> e->ewma_log;
  108. e->rate_est->bps = (e->avbps+0xF)>>5;
  109. rate = (npackets - e->last_packets)<<(12 - idx);
  110. e->last_packets = npackets;
  111. e->avpps += ((long)rate - (long)e->avpps) >> e->ewma_log;
  112. e->rate_est->pps = (e->avpps+0x1FF)>>10;
  113. skip:
  114. read_unlock(&est_lock);
  115. spin_unlock(e->stats_lock);
  116. }
  117. if (!list_empty(&elist[idx].list))
  118. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  119. rcu_read_unlock();
  120. }
  121. static void gen_add_node(struct gen_estimator *est)
  122. {
  123. struct rb_node **p = &est_root.rb_node, *parent = NULL;
  124. while (*p) {
  125. struct gen_estimator *e;
  126. parent = *p;
  127. e = rb_entry(parent, struct gen_estimator, node);
  128. if (est->bstats > e->bstats)
  129. p = &parent->rb_right;
  130. else
  131. p = &parent->rb_left;
  132. }
  133. rb_link_node(&est->node, parent, p);
  134. rb_insert_color(&est->node, &est_root);
  135. }
  136. static
  137. struct gen_estimator *gen_find_node(const struct gnet_stats_basic *bstats,
  138. const struct gnet_stats_rate_est *rate_est)
  139. {
  140. struct rb_node *p = est_root.rb_node;
  141. while (p) {
  142. struct gen_estimator *e;
  143. e = rb_entry(p, struct gen_estimator, node);
  144. if (bstats > e->bstats)
  145. p = p->rb_right;
  146. else if (bstats < e->bstats || rate_est != e->rate_est)
  147. p = p->rb_left;
  148. else
  149. return e;
  150. }
  151. return NULL;
  152. }
  153. /**
  154. * gen_new_estimator - create a new rate estimator
  155. * @bstats: basic statistics
  156. * @rate_est: rate estimator statistics
  157. * @stats_lock: statistics lock
  158. * @opt: rate estimator configuration TLV
  159. *
  160. * Creates a new rate estimator with &bstats as source and &rate_est
  161. * as destination. A new timer with the interval specified in the
  162. * configuration TLV is created. Upon each interval, the latest statistics
  163. * will be read from &bstats and the estimated rate will be stored in
  164. * &rate_est with the statistics lock grabed during this period.
  165. *
  166. * Returns 0 on success or a negative error code.
  167. *
  168. * NOTE: Called under rtnl_mutex
  169. */
  170. int gen_new_estimator(struct gnet_stats_basic *bstats,
  171. struct gnet_stats_rate_est *rate_est,
  172. spinlock_t *stats_lock,
  173. struct nlattr *opt)
  174. {
  175. struct gen_estimator *est;
  176. struct gnet_estimator *parm = nla_data(opt);
  177. int idx;
  178. if (nla_len(opt) < sizeof(*parm))
  179. return -EINVAL;
  180. if (parm->interval < -2 || parm->interval > 3)
  181. return -EINVAL;
  182. est = kzalloc(sizeof(*est), GFP_KERNEL);
  183. if (est == NULL)
  184. return -ENOBUFS;
  185. idx = parm->interval + 2;
  186. est->bstats = bstats;
  187. est->rate_est = rate_est;
  188. est->stats_lock = stats_lock;
  189. est->ewma_log = parm->ewma_log;
  190. est->last_bytes = bstats->bytes;
  191. est->avbps = rate_est->bps<<5;
  192. est->last_packets = bstats->packets;
  193. est->avpps = rate_est->pps<<10;
  194. if (!elist[idx].timer.function) {
  195. INIT_LIST_HEAD(&elist[idx].list);
  196. setup_timer(&elist[idx].timer, est_timer, idx);
  197. }
  198. if (list_empty(&elist[idx].list))
  199. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  200. list_add_rcu(&est->list, &elist[idx].list);
  201. gen_add_node(est);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(gen_new_estimator);
  205. static void __gen_kill_estimator(struct rcu_head *head)
  206. {
  207. struct gen_estimator *e = container_of(head,
  208. struct gen_estimator, e_rcu);
  209. kfree(e);
  210. }
  211. /**
  212. * gen_kill_estimator - remove a rate estimator
  213. * @bstats: basic statistics
  214. * @rate_est: rate estimator statistics
  215. *
  216. * Removes the rate estimator specified by &bstats and &rate_est.
  217. *
  218. * NOTE: Called under rtnl_mutex
  219. */
  220. void gen_kill_estimator(struct gnet_stats_basic *bstats,
  221. struct gnet_stats_rate_est *rate_est)
  222. {
  223. struct gen_estimator *e;
  224. while ((e = gen_find_node(bstats, rate_est))) {
  225. rb_erase(&e->node, &est_root);
  226. write_lock_bh(&est_lock);
  227. e->bstats = NULL;
  228. write_unlock_bh(&est_lock);
  229. list_del_rcu(&e->list);
  230. call_rcu(&e->e_rcu, __gen_kill_estimator);
  231. }
  232. }
  233. EXPORT_SYMBOL(gen_kill_estimator);
  234. /**
  235. * gen_replace_estimator - replace rate estimator configuration
  236. * @bstats: basic statistics
  237. * @rate_est: rate estimator statistics
  238. * @stats_lock: statistics lock
  239. * @opt: rate estimator configuration TLV
  240. *
  241. * Replaces the configuration of a rate estimator by calling
  242. * gen_kill_estimator() and gen_new_estimator().
  243. *
  244. * Returns 0 on success or a negative error code.
  245. */
  246. int gen_replace_estimator(struct gnet_stats_basic *bstats,
  247. struct gnet_stats_rate_est *rate_est,
  248. spinlock_t *stats_lock, struct nlattr *opt)
  249. {
  250. gen_kill_estimator(bstats, rate_est);
  251. return gen_new_estimator(bstats, rate_est, stats_lock, opt);
  252. }
  253. EXPORT_SYMBOL(gen_replace_estimator);
  254. /**
  255. * gen_estimator_active - test if estimator is currently in use
  256. * @bstats: basic statistics
  257. * @rate_est: rate estimator statistics
  258. *
  259. * Returns true if estimator is active, and false if not.
  260. */
  261. bool gen_estimator_active(const struct gnet_stats_basic *bstats,
  262. const struct gnet_stats_rate_est *rate_est)
  263. {
  264. ASSERT_RTNL();
  265. return gen_find_node(bstats, rate_est) != NULL;
  266. }
  267. EXPORT_SYMBOL(gen_estimator_active);