gen_estimator.c 8.5 KB

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