gen_estimator.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <linux/bitops.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/in.h>
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/init.h>
  32. #include <linux/rbtree.h>
  33. #include <linux/slab.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. * avbps is scaled by 2^5, avpps is scaled by 2^10.
  58. * both values are reported as 32 bit unsigned values. bps can
  59. overflow for fast links : max speed being 34360Mbit/sec
  60. * Minimal interval is HZ/4=250msec (it is the greatest common divisor
  61. for HZ=100 and HZ=1024 8)), maximal interval
  62. is (HZ*2^EST_MAX_INTERVAL)/4 = 8sec. Shorter intervals
  63. are too expensive, longer ones can be implemented
  64. at user level painlessly.
  65. */
  66. #define EST_MAX_INTERVAL 5
  67. struct gen_estimator
  68. {
  69. struct list_head list;
  70. struct gnet_stats_basic_packed *bstats;
  71. struct gnet_stats_rate_est *rate_est;
  72. spinlock_t *stats_lock;
  73. int ewma_log;
  74. u64 last_bytes;
  75. u64 avbps;
  76. u32 last_packets;
  77. u32 avpps;
  78. struct rcu_head e_rcu;
  79. struct rb_node node;
  80. };
  81. struct gen_estimator_head
  82. {
  83. struct timer_list timer;
  84. struct list_head list;
  85. };
  86. static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
  87. /* Protects against NULL dereference */
  88. static DEFINE_RWLOCK(est_lock);
  89. /* Protects against soft lockup during large deletion */
  90. static struct rb_root est_root = RB_ROOT;
  91. static DEFINE_SPINLOCK(est_tree_lock);
  92. static void est_timer(unsigned long arg)
  93. {
  94. int idx = (int)arg;
  95. struct gen_estimator *e;
  96. rcu_read_lock();
  97. list_for_each_entry_rcu(e, &elist[idx].list, list) {
  98. u64 nbytes;
  99. u64 brate;
  100. u32 npackets;
  101. u32 rate;
  102. spin_lock(e->stats_lock);
  103. read_lock(&est_lock);
  104. if (e->bstats == NULL)
  105. goto skip;
  106. nbytes = e->bstats->bytes;
  107. npackets = e->bstats->packets;
  108. brate = (nbytes - e->last_bytes)<<(7 - idx);
  109. e->last_bytes = nbytes;
  110. e->avbps += (brate >> e->ewma_log) - (e->avbps >> e->ewma_log);
  111. e->rate_est->bps = (e->avbps+0xF)>>5;
  112. rate = (npackets - e->last_packets)<<(12 - idx);
  113. e->last_packets = npackets;
  114. e->avpps += (rate >> e->ewma_log) - (e->avpps >> e->ewma_log);
  115. e->rate_est->pps = (e->avpps+0x1FF)>>10;
  116. skip:
  117. read_unlock(&est_lock);
  118. spin_unlock(e->stats_lock);
  119. }
  120. if (!list_empty(&elist[idx].list))
  121. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  122. rcu_read_unlock();
  123. }
  124. static void gen_add_node(struct gen_estimator *est)
  125. {
  126. struct rb_node **p = &est_root.rb_node, *parent = NULL;
  127. while (*p) {
  128. struct gen_estimator *e;
  129. parent = *p;
  130. e = rb_entry(parent, struct gen_estimator, node);
  131. if (est->bstats > e->bstats)
  132. p = &parent->rb_right;
  133. else
  134. p = &parent->rb_left;
  135. }
  136. rb_link_node(&est->node, parent, p);
  137. rb_insert_color(&est->node, &est_root);
  138. }
  139. static
  140. struct gen_estimator *gen_find_node(const struct gnet_stats_basic_packed *bstats,
  141. const struct gnet_stats_rate_est *rate_est)
  142. {
  143. struct rb_node *p = est_root.rb_node;
  144. while (p) {
  145. struct gen_estimator *e;
  146. e = rb_entry(p, struct gen_estimator, node);
  147. if (bstats > e->bstats)
  148. p = p->rb_right;
  149. else if (bstats < e->bstats || rate_est != e->rate_est)
  150. p = p->rb_left;
  151. else
  152. return e;
  153. }
  154. return NULL;
  155. }
  156. /**
  157. * gen_new_estimator - create a new rate estimator
  158. * @bstats: basic statistics
  159. * @rate_est: rate estimator statistics
  160. * @stats_lock: statistics lock
  161. * @opt: rate estimator configuration TLV
  162. *
  163. * Creates a new rate estimator with &bstats as source and &rate_est
  164. * as destination. A new timer with the interval specified in the
  165. * configuration TLV is created. Upon each interval, the latest statistics
  166. * will be read from &bstats and the estimated rate will be stored in
  167. * &rate_est with the statistics lock grabed during this period.
  168. *
  169. * Returns 0 on success or a negative error code.
  170. *
  171. */
  172. int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
  173. struct gnet_stats_rate_est *rate_est,
  174. spinlock_t *stats_lock,
  175. struct nlattr *opt)
  176. {
  177. struct gen_estimator *est;
  178. struct gnet_estimator *parm = nla_data(opt);
  179. int idx;
  180. if (nla_len(opt) < sizeof(*parm))
  181. return -EINVAL;
  182. if (parm->interval < -2 || parm->interval > 3)
  183. return -EINVAL;
  184. est = kzalloc(sizeof(*est), GFP_KERNEL);
  185. if (est == NULL)
  186. return -ENOBUFS;
  187. idx = parm->interval + 2;
  188. est->bstats = bstats;
  189. est->rate_est = rate_est;
  190. est->stats_lock = stats_lock;
  191. est->ewma_log = parm->ewma_log;
  192. est->last_bytes = bstats->bytes;
  193. est->avbps = rate_est->bps<<5;
  194. est->last_packets = bstats->packets;
  195. est->avpps = rate_est->pps<<10;
  196. spin_lock_bh(&est_tree_lock);
  197. if (!elist[idx].timer.function) {
  198. INIT_LIST_HEAD(&elist[idx].list);
  199. setup_timer(&elist[idx].timer, est_timer, idx);
  200. }
  201. if (list_empty(&elist[idx].list))
  202. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  203. list_add_rcu(&est->list, &elist[idx].list);
  204. gen_add_node(est);
  205. spin_unlock_bh(&est_tree_lock);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(gen_new_estimator);
  209. /**
  210. * gen_kill_estimator - remove a rate estimator
  211. * @bstats: basic statistics
  212. * @rate_est: rate estimator statistics
  213. *
  214. * Removes the rate estimator specified by &bstats and &rate_est.
  215. *
  216. * Note : Caller should respect an RCU grace period before freeing stats_lock
  217. */
  218. void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
  219. struct gnet_stats_rate_est *rate_est)
  220. {
  221. struct gen_estimator *e;
  222. spin_lock_bh(&est_tree_lock);
  223. while ((e = gen_find_node(bstats, rate_est))) {
  224. rb_erase(&e->node, &est_root);
  225. write_lock(&est_lock);
  226. e->bstats = NULL;
  227. write_unlock(&est_lock);
  228. list_del_rcu(&e->list);
  229. kfree_rcu(e, e_rcu);
  230. }
  231. spin_unlock_bh(&est_tree_lock);
  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_packed *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_packed *bstats,
  262. const struct gnet_stats_rate_est *rate_est)
  263. {
  264. bool res;
  265. ASSERT_RTNL();
  266. spin_lock_bh(&est_tree_lock);
  267. res = gen_find_node(bstats, rate_est) != NULL;
  268. spin_unlock_bh(&est_tree_lock);
  269. return res;
  270. }
  271. EXPORT_SYMBOL(gen_estimator_active);