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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/sch_tbf.c Token Bucket Filter queue.
  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.  */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/system.h>
  16. #include <asm/bitops.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/in.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/inet.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/notifier.h>
  32. #include <net/ip.h>
  33. #include <net/route.h>
  34. #include <linux/skbuff.h>
  35. #include <net/sock.h>
  36. #include <net/pkt_sched.h>
  37. /* Simple Token Bucket Filter.
  38. =======================================
  39. SOURCE.
  40. -------
  41. None.
  42. Description.
  43. ------------
  44. A data flow obeys TBF with rate R and depth B, if for any
  45. time interval t_i...t_f the number of transmitted bits
  46. does not exceed B + R*(t_f-t_i).
  47. Packetized version of this definition:
  48. The sequence of packets of sizes s_i served at moments t_i
  49. obeys TBF, if for any i<=k:
  50. s_i+....+s_k <= B + R*(t_k - t_i)
  51. Algorithm.
  52. ----------
  53. Let N(t_i) be B/R initially and N(t) grow continuously with time as:
  54. N(t+delta) = min{B/R, N(t) + delta}
  55. If the first packet in queue has length S, it may be
  56. transmitted only at the time t_* when S/R <= N(t_*),
  57. and in this case N(t) jumps:
  58. N(t_* + 0) = N(t_* - 0) - S/R.
  59. Actually, QoS requires two TBF to be applied to a data stream.
  60. One of them controls steady state burst size, another
  61. one with rate P (peak rate) and depth M (equal to link MTU)
  62. limits bursts at a smaller time scale.
  63. It is easy to see that P>R, and B>M. If P is infinity, this double
  64. TBF is equivalent to a single one.
  65. When TBF works in reshaping mode, latency is estimated as:
  66. lat = max ((L-B)/R, (L-M)/P)
  67. NOTES.
  68. ------
  69. If TBF throttles, it starts a watchdog timer, which will wake it up
  70. when it is ready to transmit.
  71. Note that the minimal timer resolution is 1/HZ.
  72. If no new packets arrive during this period,
  73. or if the device is not awaken by EOI for some previous packet,
  74. TBF can stop its activity for 1/HZ.
  75. This means, that with depth B, the maximal rate is
  76. R_crit = B*HZ
  77. F.e. for 10Mbit ethernet and HZ=100 the minimal allowed B is ~10Kbytes.
  78. Note that the peak rate TBF is much more tough: with MTU 1500
  79. P_crit = 150Kbytes/sec. So, if you need greater peak
  80. rates, use alpha with HZ=1000 :-)
  81. */
  82. struct tbf_sched_data
  83. {
  84. /* Parameters */
  85. u32 limit; /* Maximal length of backlog: bytes */
  86. u32 buffer; /* Token bucket depth/rate: MUST BE >= MTU/B */
  87. u32 mtu;
  88. u32 max_size;
  89. struct qdisc_rate_table *R_tab;
  90. struct qdisc_rate_table *P_tab;
  91. /* Variables */
  92. long tokens; /* Current number of B tokens */
  93. long ptokens; /* Current number of P tokens */
  94. psched_time_t t_c; /* Time check-point */
  95. struct timer_list wd_timer; /* Watchdog timer */
  96. };
  97. #define L2T(q,L)   ((q)->R_tab->data[(L)>>(q)->R_tab->rate.cell_log])
  98. #define L2T_P(q,L) ((q)->P_tab->data[(L)>>(q)->P_tab->rate.cell_log])
  99. static int
  100. tbf_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  101. {
  102. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  103. if (skb->len > q->max_size)
  104. goto drop;
  105. __skb_queue_tail(&sch->q, skb);
  106. if ((sch->stats.backlog += skb->len) <= q->limit) {
  107. sch->stats.bytes += skb->len;
  108. sch->stats.packets++;
  109. return 0;
  110. }
  111. /* Drop action: undo the things that we just did,
  112.  * i.e. make tail drop
  113.  */
  114. __skb_unlink(skb, &sch->q);
  115. sch->stats.backlog -= skb->len;
  116. drop:
  117. sch->stats.drops++;
  118. #ifdef CONFIG_NET_CLS_POLICE
  119. if (sch->reshape_fail==NULL || sch->reshape_fail(skb, sch))
  120. #endif
  121. kfree_skb(skb);
  122. return NET_XMIT_DROP;
  123. }
  124. static int
  125. tbf_requeue(struct sk_buff *skb, struct Qdisc* sch)
  126. {
  127. __skb_queue_head(&sch->q, skb);
  128. sch->stats.backlog += skb->len;
  129. return 0;
  130. }
  131. static int
  132. tbf_drop(struct Qdisc* sch)
  133. {
  134. struct sk_buff *skb;
  135. skb = __skb_dequeue_tail(&sch->q);
  136. if (skb) {
  137. sch->stats.backlog -= skb->len;
  138. sch->stats.drops++;
  139. kfree_skb(skb);
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. static void tbf_watchdog(unsigned long arg)
  145. {
  146. struct Qdisc *sch = (struct Qdisc*)arg;
  147. sch->flags &= ~TCQ_F_THROTTLED;
  148. netif_schedule(sch->dev);
  149. }
  150. static struct sk_buff *
  151. tbf_dequeue(struct Qdisc* sch)
  152. {
  153. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  154. struct sk_buff *skb;
  155. skb = __skb_dequeue(&sch->q);
  156. if (skb) {
  157. psched_time_t now;
  158. long toks;
  159. long ptoks = 0;
  160. PSCHED_GET_TIME(now);
  161. toks = PSCHED_TDIFF_SAFE(now, q->t_c, q->buffer, 0);
  162. if (q->P_tab) {
  163. ptoks = toks + q->ptokens;
  164. if (ptoks > (long)q->mtu)
  165. ptoks = q->mtu;
  166. ptoks -= L2T_P(q, skb->len);
  167. }
  168. toks += q->tokens;
  169. if (toks > (long)q->buffer)
  170. toks = q->buffer;
  171. toks -= L2T(q, skb->len);
  172. if ((toks|ptoks) >= 0) {
  173. q->t_c = now;
  174. q->tokens = toks;
  175. q->ptokens = ptoks;
  176. sch->stats.backlog -= skb->len;
  177. sch->flags &= ~TCQ_F_THROTTLED;
  178. return skb;
  179. }
  180. if (!netif_queue_stopped(sch->dev)) {
  181. long delay = PSCHED_US2JIFFIE(max_t(long, -toks, -ptoks));
  182. if (delay == 0)
  183. delay = 1;
  184. mod_timer(&q->wd_timer, jiffies+delay);
  185. }
  186. /* Maybe we have a shorter packet in the queue,
  187.    which can be sent now. It sounds cool,
  188.    but, however, this is wrong in principle.
  189.    We MUST NOT reorder packets under these circumstances.
  190.    Really, if we split the flow into independent
  191.    subflows, it would be a very good solution.
  192.    This is the main idea of all FQ algorithms
  193.    (cf. CSZ, HPFQ, HFSC)
  194.  */
  195. __skb_queue_head(&sch->q, skb);
  196. sch->flags |= TCQ_F_THROTTLED;
  197. sch->stats.overlimits++;
  198. }
  199. return NULL;
  200. }
  201. static void
  202. tbf_reset(struct Qdisc* sch)
  203. {
  204. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  205. skb_queue_purge(&sch->q);
  206. sch->stats.backlog = 0;
  207. PSCHED_GET_TIME(q->t_c);
  208. q->tokens = q->buffer;
  209. q->ptokens = q->mtu;
  210. sch->flags &= ~TCQ_F_THROTTLED;
  211. del_timer(&q->wd_timer);
  212. }
  213. static int tbf_change(struct Qdisc* sch, struct rtattr *opt)
  214. {
  215. int err = -EINVAL;
  216. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  217. struct rtattr *tb[TCA_TBF_PTAB];
  218. struct tc_tbf_qopt *qopt;
  219. struct qdisc_rate_table *rtab = NULL;
  220. struct qdisc_rate_table *ptab = NULL;
  221. int max_size,n;
  222. if (rtattr_parse(tb, TCA_TBF_PTAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) ||
  223.     tb[TCA_TBF_PARMS-1] == NULL ||
  224.     RTA_PAYLOAD(tb[TCA_TBF_PARMS-1]) < sizeof(*qopt))
  225. goto done;
  226. qopt = RTA_DATA(tb[TCA_TBF_PARMS-1]);
  227. rtab = qdisc_get_rtab(&qopt->rate, tb[TCA_TBF_RTAB-1]);
  228. if (rtab == NULL)
  229. goto done;
  230. if (qopt->peakrate.rate) {
  231. if (qopt->peakrate.rate > qopt->rate.rate)
  232. ptab = qdisc_get_rtab(&qopt->peakrate, tb[TCA_TBF_PTAB-1]);
  233. if (ptab == NULL)
  234. goto done;
  235. }
  236. for (n = 0; n < 256; n++)
  237. if (rtab->data[n] > qopt->buffer) break;
  238. max_size = (n << qopt->rate.cell_log)-1;
  239. if (ptab) {
  240. int size;
  241. for (n = 0; n < 256; n++)
  242. if (ptab->data[n] > qopt->mtu) break;
  243. size = (n << qopt->peakrate.cell_log)-1;
  244. if (size < max_size) max_size = size;
  245. }
  246. if (max_size < 0)
  247. goto done;
  248. sch_tree_lock(sch);
  249. q->limit = qopt->limit;
  250. q->mtu = qopt->mtu;
  251. q->max_size = max_size;
  252. q->buffer = qopt->buffer;
  253. q->tokens = q->buffer;
  254. q->ptokens = q->mtu;
  255. rtab = xchg(&q->R_tab, rtab);
  256. ptab = xchg(&q->P_tab, ptab);
  257. sch_tree_unlock(sch);
  258. err = 0;
  259. done:
  260. if (rtab)
  261. qdisc_put_rtab(rtab);
  262. if (ptab)
  263. qdisc_put_rtab(ptab);
  264. return err;
  265. }
  266. static int tbf_init(struct Qdisc* sch, struct rtattr *opt)
  267. {
  268. int err;
  269. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  270. if (opt == NULL)
  271. return -EINVAL;
  272. MOD_INC_USE_COUNT;
  273. PSCHED_GET_TIME(q->t_c);
  274. init_timer(&q->wd_timer);
  275. q->wd_timer.function = tbf_watchdog;
  276. q->wd_timer.data = (unsigned long)sch;
  277. if ((err = tbf_change(sch, opt)) != 0) {
  278. MOD_DEC_USE_COUNT;
  279. }
  280. return err;
  281. }
  282. static void tbf_destroy(struct Qdisc *sch)
  283. {
  284. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  285. del_timer(&q->wd_timer);
  286. if (q->P_tab)
  287. qdisc_put_rtab(q->P_tab);
  288. if (q->R_tab)
  289. qdisc_put_rtab(q->R_tab);
  290. MOD_DEC_USE_COUNT;
  291. }
  292. static int tbf_dump(struct Qdisc *sch, struct sk_buff *skb)
  293. {
  294. struct tbf_sched_data *q = (struct tbf_sched_data *)sch->data;
  295. unsigned char  *b = skb->tail;
  296. struct rtattr *rta;
  297. struct tc_tbf_qopt opt;
  298. rta = (struct rtattr*)b;
  299. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  300. opt.limit = q->limit;
  301. opt.rate = q->R_tab->rate;
  302. if (q->P_tab)
  303. opt.peakrate = q->P_tab->rate;
  304. else
  305. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  306. opt.mtu = q->mtu;
  307. opt.buffer = q->buffer;
  308. RTA_PUT(skb, TCA_TBF_PARMS, sizeof(opt), &opt);
  309. rta->rta_len = skb->tail - b;
  310. return skb->len;
  311. rtattr_failure:
  312. skb_trim(skb, b - skb->data);
  313. return -1;
  314. }
  315. struct Qdisc_ops tbf_qdisc_ops =
  316. {
  317. NULL,
  318. NULL,
  319. "tbf",
  320. sizeof(struct tbf_sched_data),
  321. tbf_enqueue,
  322. tbf_dequeue,
  323. tbf_requeue,
  324. tbf_drop,
  325. tbf_init,
  326. tbf_reset,
  327. tbf_destroy,
  328. tbf_change,
  329. tbf_dump,
  330. };
  331. #ifdef MODULE
  332. int init_module(void)
  333. {
  334. return register_qdisc(&tbf_qdisc_ops);
  335. }
  336. void cleanup_module(void) 
  337. {
  338. unregister_qdisc(&tbf_qdisc_ops);
  339. }
  340. #endif
  341. MODULE_LICENSE("GPL");