estimator.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/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. #include <asm/uaccess.h>
  12. #include <asm/system.h>
  13. #include <asm/bitops.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/socket.h>
  20. #include <linux/sockios.h>
  21. #include <linux/in.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/init.h>
  28. #include <linux/proc_fs.h>
  29. #include <net/sock.h>
  30. #include <net/pkt_sched.h>
  31. /*
  32.    This code is NOT intended to be used for statistics collection,
  33.    its purpose is to provide a base for statistical multiplexing
  34.    for controlled load service.
  35.    If you need only statistics, run a user level daemon which
  36.    periodically reads byte counters.
  37.    Unfortunately, rate estimation is not a very easy task.
  38.    F.e. I did not find a simple way to estimate the current peak rate
  39.    and even failed to formulate the problem 8)8)
  40.    So I preferred not to built an estimator into the scheduler,
  41.    but run this task separately.
  42.    Ideally, it should be kernel thread(s), but for now it runs
  43.    from timers, which puts apparent top bounds on the number of rated
  44.    flows, has minimal overhead on small, but is enough
  45.    to handle controlled load service, sets of aggregates.
  46.    We measure rate over A=(1<<interval) seconds and evaluate EWMA:
  47.    avrate = avrate*(1-W) + rate*W
  48.    where W is chosen as negative power of 2: W = 2^(-ewma_log)
  49.    The resulting time constant is:
  50.    T = A/(-ln(1-W))
  51.    NOTES.
  52.    * The stored value for avbps is scaled by 2^5, so that maximal
  53.      rate is ~1Gbit, avpps is scaled by 2^10.
  54.    * Minimal interval is HZ/4=250msec (it is the greatest common divisor
  55.      for HZ=100 and HZ=1024 8)), maximal interval
  56.      is (HZ/4)*2^EST_MAX_INTERVAL = 8sec. Shorter intervals
  57.      are too expensive, longer ones can be implemented
  58.      at user level painlessly.
  59.  */
  60. #if (HZ%4) != 0
  61. #error Bad HZ value.
  62. #endif
  63. #define EST_MAX_INTERVAL 5
  64. struct qdisc_estimator
  65. {
  66. struct qdisc_estimator *next;
  67. struct tc_stats *stats;
  68. unsigned interval;
  69. int ewma_log;
  70. u64 last_bytes;
  71. u32 last_packets;
  72. u32 avpps;
  73. u32 avbps;
  74. };
  75. struct qdisc_estimator_head
  76. {
  77. struct timer_list timer;
  78. struct qdisc_estimator *list;
  79. };
  80. static struct qdisc_estimator_head elist[EST_MAX_INTERVAL+1];
  81. /* Estimator array lock */
  82. static rwlock_t est_lock = RW_LOCK_UNLOCKED;
  83. static void est_timer(unsigned long arg)
  84. {
  85. int idx = (int)arg;
  86. struct qdisc_estimator *e;
  87. read_lock(&est_lock);
  88. for (e = elist[idx].list; e; e = e->next) {
  89. struct tc_stats *st = e->stats;
  90. u64 nbytes;
  91. u32 npackets;
  92. u32 rate;
  93. spin_lock(st->lock);
  94. nbytes = st->bytes;
  95. npackets = st->packets;
  96. rate = (nbytes - e->last_bytes)<<(7 - idx);
  97. e->last_bytes = nbytes;
  98. e->avbps += ((long)rate - (long)e->avbps) >> e->ewma_log;
  99. st->bps = (e->avbps+0xF)>>5;
  100. rate = (npackets - e->last_packets)<<(12 - idx);
  101. e->last_packets = npackets;
  102. e->avpps += ((long)rate - (long)e->avpps) >> e->ewma_log;
  103. e->stats->pps = (e->avpps+0x1FF)>>10;
  104. spin_unlock(st->lock);
  105. }
  106. mod_timer(&elist[idx].timer, jiffies + ((HZ/4)<<idx));
  107. read_unlock(&est_lock);
  108. }
  109. int qdisc_new_estimator(struct tc_stats *stats, struct rtattr *opt)
  110. {
  111. struct qdisc_estimator *est;
  112. struct tc_estimator *parm = RTA_DATA(opt);
  113. if (RTA_PAYLOAD(opt) < sizeof(*parm))
  114. return -EINVAL;
  115. if (parm->interval < -2 || parm->interval > 3)
  116. return -EINVAL;
  117. est = kmalloc(sizeof(*est), GFP_KERNEL);
  118. if (est == NULL)
  119. return -ENOBUFS;
  120. memset(est, 0, sizeof(*est));
  121. est->interval = parm->interval + 2;
  122. est->stats = stats;
  123. est->ewma_log = parm->ewma_log;
  124. est->last_bytes = stats->bytes;
  125. est->avbps = stats->bps<<5;
  126. est->last_packets = stats->packets;
  127. est->avpps = stats->pps<<10;
  128. est->next = elist[est->interval].list;
  129. if (est->next == NULL) {
  130. init_timer(&elist[est->interval].timer);
  131. elist[est->interval].timer.data = est->interval;
  132. elist[est->interval].timer.expires = jiffies + ((HZ/4)<<est->interval);
  133. elist[est->interval].timer.function = est_timer;
  134. add_timer(&elist[est->interval].timer);
  135. }
  136. write_lock_bh(&est_lock);
  137. elist[est->interval].list = est;
  138. write_unlock_bh(&est_lock);
  139. return 0;
  140. }
  141. void qdisc_kill_estimator(struct tc_stats *stats)
  142. {
  143. int idx;
  144. struct qdisc_estimator *est, **pest;
  145. for (idx=0; idx <= EST_MAX_INTERVAL; idx++) {
  146. int killed = 0;
  147. pest = &elist[idx].list;
  148. while ((est=*pest) != NULL) {
  149. if (est->stats != stats) {
  150. pest = &est->next;
  151. continue;
  152. }
  153. write_lock_bh(&est_lock);
  154. *pest = est->next;
  155. write_unlock_bh(&est_lock);
  156. kfree(est);
  157. killed++;
  158. }
  159. if (killed && elist[idx].list == NULL)
  160. del_timer(&elist[idx].timer);
  161. }
  162. }