gen_estimator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. seqcount_t *running;
  74. int ewma_log;
  75. u32 last_packets;
  76. unsigned long avpps;
  77. u64 last_bytes;
  78. u64 avbps;
  79. struct rcu_head e_rcu;
  80. struct rb_node node;
  81. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  82. struct rcu_head head;
  83. };
  84. struct gen_estimator_head
  85. {
  86. struct timer_list timer;
  87. struct list_head list;
  88. };
  89. static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
  90. /* Protects against NULL dereference */
  91. static DEFINE_RWLOCK(est_lock);
  92. /* Protects against soft lockup during large deletion */
  93. static struct rb_root est_root = RB_ROOT;
  94. static DEFINE_SPINLOCK(est_tree_lock);
  95. static void est_timer(unsigned long arg)
  96. {
  97. int idx = (int)arg;
  98. struct gen_estimator *e;
  99. rcu_read_lock();
  100. list_for_each_entry_rcu(e, &elist[idx].list, list) {
  101. struct gnet_stats_basic_packed b = {0};
  102. unsigned long rate;
  103. u64 brate;
  104. if (e->stats_lock)
  105. spin_lock(e->stats_lock);
  106. read_lock(&est_lock);
  107. if (e->bstats == NULL)
  108. goto skip;
  109. __gnet_stats_copy_basic(e->running, &b, e->cpu_bstats, e->bstats);
  110. brate = (b.bytes - e->last_bytes)<<(7 - idx);
  111. e->last_bytes = b.bytes;
  112. e->avbps += (brate >> e->ewma_log) - (e->avbps >> e->ewma_log);
  113. WRITE_ONCE(e->rate_est->bps, (e->avbps + 0xF) >> 5);
  114. rate = b.packets - e->last_packets;
  115. rate <<= (7 - idx);
  116. e->last_packets = b.packets;
  117. e->avpps += (rate >> e->ewma_log) - (e->avpps >> e->ewma_log);
  118. WRITE_ONCE(e->rate_est->pps, (e->avpps + 0xF) >> 5);
  119. skip:
  120. read_unlock(&est_lock);
  121. if (e->stats_lock)
  122. spin_unlock(e->stats_lock);
  123. }
  124. if (!list_empty(&elist[idx].list))
  125. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  126. rcu_read_unlock();
  127. }
  128. static void gen_add_node(struct gen_estimator *est)
  129. {
  130. struct rb_node **p = &est_root.rb_node, *parent = NULL;
  131. while (*p) {
  132. struct gen_estimator *e;
  133. parent = *p;
  134. e = rb_entry(parent, struct gen_estimator, node);
  135. if (est->bstats > e->bstats)
  136. p = &parent->rb_right;
  137. else
  138. p = &parent->rb_left;
  139. }
  140. rb_link_node(&est->node, parent, p);
  141. rb_insert_color(&est->node, &est_root);
  142. }
  143. static
  144. struct gen_estimator *gen_find_node(const struct gnet_stats_basic_packed *bstats,
  145. const struct gnet_stats_rate_est64 *rate_est)
  146. {
  147. struct rb_node *p = est_root.rb_node;
  148. while (p) {
  149. struct gen_estimator *e;
  150. e = rb_entry(p, struct gen_estimator, node);
  151. if (bstats > e->bstats)
  152. p = p->rb_right;
  153. else if (bstats < e->bstats || rate_est != e->rate_est)
  154. p = p->rb_left;
  155. else
  156. return e;
  157. }
  158. return NULL;
  159. }
  160. /**
  161. * gen_new_estimator - create a new rate estimator
  162. * @bstats: basic statistics
  163. * @cpu_bstats: bstats per cpu
  164. * @rate_est: rate estimator statistics
  165. * @stats_lock: statistics lock
  166. * @running: qdisc running seqcount
  167. * @opt: rate estimator configuration TLV
  168. *
  169. * Creates a new rate estimator with &bstats as source and &rate_est
  170. * as destination. A new timer with the interval specified in the
  171. * configuration TLV is created. Upon each interval, the latest statistics
  172. * will be read from &bstats and the estimated rate will be stored in
  173. * &rate_est with the statistics lock grabbed during this period.
  174. *
  175. * Returns 0 on success or a negative error code.
  176. *
  177. */
  178. int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
  179. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  180. struct gnet_stats_rate_est64 *rate_est,
  181. spinlock_t *stats_lock,
  182. seqcount_t *running,
  183. struct nlattr *opt)
  184. {
  185. struct gen_estimator *est;
  186. struct gnet_estimator *parm = nla_data(opt);
  187. struct gnet_stats_basic_packed b = {0};
  188. int idx;
  189. if (nla_len(opt) < sizeof(*parm))
  190. return -EINVAL;
  191. if (parm->interval < -2 || parm->interval > 3)
  192. return -EINVAL;
  193. est = kzalloc(sizeof(*est), GFP_KERNEL);
  194. if (est == NULL)
  195. return -ENOBUFS;
  196. __gnet_stats_copy_basic(running, &b, cpu_bstats, bstats);
  197. idx = parm->interval + 2;
  198. est->bstats = bstats;
  199. est->rate_est = rate_est;
  200. est->stats_lock = stats_lock;
  201. est->running = running;
  202. est->ewma_log = parm->ewma_log;
  203. est->last_bytes = b.bytes;
  204. est->avbps = rate_est->bps<<5;
  205. est->last_packets = b.packets;
  206. est->avpps = rate_est->pps<<10;
  207. est->cpu_bstats = cpu_bstats;
  208. spin_lock_bh(&est_tree_lock);
  209. if (!elist[idx].timer.function) {
  210. INIT_LIST_HEAD(&elist[idx].list);
  211. setup_timer(&elist[idx].timer, est_timer, idx);
  212. }
  213. if (list_empty(&elist[idx].list))
  214. mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
  215. list_add_rcu(&est->list, &elist[idx].list);
  216. gen_add_node(est);
  217. spin_unlock_bh(&est_tree_lock);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(gen_new_estimator);
  221. /**
  222. * gen_kill_estimator - remove a rate estimator
  223. * @bstats: basic statistics
  224. * @rate_est: rate estimator statistics
  225. *
  226. * Removes the rate estimator specified by &bstats and &rate_est.
  227. *
  228. * Note : Caller should respect an RCU grace period before freeing stats_lock
  229. */
  230. void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
  231. struct gnet_stats_rate_est64 *rate_est)
  232. {
  233. struct gen_estimator *e;
  234. spin_lock_bh(&est_tree_lock);
  235. while ((e = gen_find_node(bstats, rate_est))) {
  236. rb_erase(&e->node, &est_root);
  237. write_lock(&est_lock);
  238. e->bstats = NULL;
  239. write_unlock(&est_lock);
  240. list_del_rcu(&e->list);
  241. kfree_rcu(e, e_rcu);
  242. }
  243. spin_unlock_bh(&est_tree_lock);
  244. }
  245. EXPORT_SYMBOL(gen_kill_estimator);
  246. /**
  247. * gen_replace_estimator - replace rate estimator configuration
  248. * @bstats: basic statistics
  249. * @cpu_bstats: bstats per cpu
  250. * @rate_est: rate estimator statistics
  251. * @stats_lock: statistics lock
  252. * @running: qdisc running seqcount (might be NULL)
  253. * @opt: rate estimator configuration TLV
  254. *
  255. * Replaces the configuration of a rate estimator by calling
  256. * gen_kill_estimator() and gen_new_estimator().
  257. *
  258. * Returns 0 on success or a negative error code.
  259. */
  260. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  261. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  262. struct gnet_stats_rate_est64 *rate_est,
  263. spinlock_t *stats_lock,
  264. seqcount_t *running, struct nlattr *opt)
  265. {
  266. gen_kill_estimator(bstats, rate_est);
  267. return gen_new_estimator(bstats, cpu_bstats, rate_est, stats_lock, running, opt);
  268. }
  269. EXPORT_SYMBOL(gen_replace_estimator);
  270. /**
  271. * gen_estimator_active - test if estimator is currently in use
  272. * @bstats: basic statistics
  273. * @rate_est: rate estimator statistics
  274. *
  275. * Returns true if estimator is active, and false if not.
  276. */
  277. bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
  278. const struct gnet_stats_rate_est64 *rate_est)
  279. {
  280. bool res;
  281. ASSERT_RTNL();
  282. spin_lock_bh(&est_tree_lock);
  283. res = gen_find_node(bstats, rate_est) != NULL;
  284. spin_unlock_bh(&est_tree_lock);
  285. return res;
  286. }
  287. EXPORT_SYMBOL(gen_estimator_active);