gen_estimator.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 and avpps are scaled by 2^5.
  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_est64 *rate_est;
  72. spinlock_t *stats_lock;
  73. int ewma_log;
  74. u32 last_packets;
  75. unsigned long avpps;
  76. u64 last_bytes;
  77. u64 avbps;
  78. struct rcu_head e_rcu;
  79. struct rb_node node;
  80. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  81. struct rcu_head head;
  82. };
  83. struct gen_estimator_head
  84. {
  85. struct timer_list timer;
  86. struct list_head list;
  87. };
  88. static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
  89. /* Protects against NULL dereference */
  90. static DEFINE_RWLOCK(est_lock);
  91. /* Protects against soft lockup during large deletion */
  92. static struct rb_root est_root = RB_ROOT;
  93. static DEFINE_SPINLOCK(est_tree_lock);
  94. static void est_timer(unsigned long arg)
  95. {
  96. int idx = (int)arg;
  97. struct gen_estimator *e;
  98. rcu_read_lock();
  99. list_for_each_entry_rcu(e, &elist[idx].list, list) {
  100. struct gnet_stats_basic_packed b = {0};
  101. unsigned long rate;
  102. u64 brate;
  103. spin_lock(e->stats_lock);
  104. read_lock(&est_lock);
  105. if (e->bstats == NULL)
  106. goto skip;
  107. __gnet_stats_copy_basic(&b, e->cpu_bstats, e->bstats);
  108. brate = (b.bytes - e->last_bytes)<<(7 - idx);
  109. e->last_bytes = b.bytes;
  110. e->avbps += (brate >> e->ewma_log) - (e->avbps >> e->ewma_log);
  111. e->rate_est->bps = (e->avbps+0xF)>>5;
  112. rate = b.packets - e->last_packets;
  113. rate <<= (7 - idx);
  114. e->last_packets = b.packets;
  115. e->avpps += (rate >> e->ewma_log) - (e->avpps >> e->ewma_log);
  116. e->rate_est->pps = (e->avpps + 0xF) >> 5;
  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_est64 *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 grabbed 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_basic_cpu __percpu *cpu_bstats,
  175. struct gnet_stats_rate_est64 *rate_est,
  176. spinlock_t *stats_lock,
  177. struct nlattr *opt)
  178. {
  179. struct gen_estimator *est;
  180. struct gnet_estimator *parm = nla_data(opt);
  181. struct gnet_stats_basic_packed b = {0};
  182. int idx;
  183. if (nla_len(opt) < sizeof(*parm))
  184. return -EINVAL;
  185. if (parm->interval < -2 || parm->interval > 3)
  186. return -EINVAL;
  187. est = kzalloc(sizeof(*est), GFP_KERNEL);
  188. if (est == NULL)
  189. return -ENOBUFS;
  190. __gnet_stats_copy_basic(&b, cpu_bstats, bstats);
  191. idx = parm->interval + 2;
  192. est->bstats = bstats;
  193. est->rate_est = rate_est;
  194. est->stats_lock = stats_lock;
  195. est->ewma_log = parm->ewma_log;
  196. est->last_bytes = b.bytes;
  197. est->avbps = rate_est->bps<<5;
  198. est->last_packets = b.packets;
  199. est->avpps = rate_est->pps<<10;
  200. est->cpu_bstats = cpu_bstats;
  201. spin_lock_bh(&est_tree_lock);
  202. if (!elist[idx].timer.function) {
  203. INIT_LIST_HEAD(&elist[idx].list);
  204. setup_timer(&elist[idx].timer, est_timer, idx);
  205. }
  206. if (list_empty(&elist[idx].list))
  207. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  208. list_add_rcu(&est->list, &elist[idx].list);
  209. gen_add_node(est);
  210. spin_unlock_bh(&est_tree_lock);
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(gen_new_estimator);
  214. /**
  215. * gen_kill_estimator - remove a rate estimator
  216. * @bstats: basic statistics
  217. * @rate_est: rate estimator statistics
  218. *
  219. * Removes the rate estimator specified by &bstats and &rate_est.
  220. *
  221. * Note : Caller should respect an RCU grace period before freeing stats_lock
  222. */
  223. void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
  224. struct gnet_stats_rate_est64 *rate_est)
  225. {
  226. struct gen_estimator *e;
  227. spin_lock_bh(&est_tree_lock);
  228. while ((e = gen_find_node(bstats, rate_est))) {
  229. rb_erase(&e->node, &est_root);
  230. write_lock(&est_lock);
  231. e->bstats = NULL;
  232. write_unlock(&est_lock);
  233. list_del_rcu(&e->list);
  234. kfree_rcu(e, e_rcu);
  235. }
  236. spin_unlock_bh(&est_tree_lock);
  237. }
  238. EXPORT_SYMBOL(gen_kill_estimator);
  239. /**
  240. * gen_replace_estimator - replace rate estimator configuration
  241. * @bstats: basic statistics
  242. * @rate_est: rate estimator statistics
  243. * @stats_lock: statistics lock
  244. * @opt: rate estimator configuration TLV
  245. *
  246. * Replaces the configuration of a rate estimator by calling
  247. * gen_kill_estimator() and gen_new_estimator().
  248. *
  249. * Returns 0 on success or a negative error code.
  250. */
  251. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  252. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  253. struct gnet_stats_rate_est64 *rate_est,
  254. spinlock_t *stats_lock, struct nlattr *opt)
  255. {
  256. gen_kill_estimator(bstats, rate_est);
  257. return gen_new_estimator(bstats, cpu_bstats, rate_est, stats_lock, opt);
  258. }
  259. EXPORT_SYMBOL(gen_replace_estimator);
  260. /**
  261. * gen_estimator_active - test if estimator is currently in use
  262. * @bstats: basic statistics
  263. * @rate_est: rate estimator statistics
  264. *
  265. * Returns true if estimator is active, and false if not.
  266. */
  267. bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
  268. const struct gnet_stats_rate_est64 *rate_est)
  269. {
  270. bool res;
  271. ASSERT_RTNL();
  272. spin_lock_bh(&est_tree_lock);
  273. res = gen_find_node(bstats, rate_est) != NULL;
  274. spin_unlock_bh(&est_tree_lock);
  275. return res;
  276. }
  277. EXPORT_SYMBOL(gen_estimator_active);