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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/sch_fifo.c The simplest FIFO 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. #include <linux/config.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/system.h>
  14. #include <asm/bitops.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/in.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/inet.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/notifier.h>
  30. #include <net/ip.h>
  31. #include <net/route.h>
  32. #include <linux/skbuff.h>
  33. #include <net/sock.h>
  34. #include <net/pkt_sched.h>
  35. /* 1 band FIFO pseudo-"scheduler" */
  36. struct fifo_sched_data
  37. {
  38. unsigned limit;
  39. };
  40. static int
  41. bfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  42. {
  43. struct fifo_sched_data *q = (struct fifo_sched_data *)sch->data;
  44. if (sch->stats.backlog <= q->limit) {
  45. __skb_queue_tail(&sch->q, skb);
  46. sch->stats.backlog += skb->len;
  47. sch->stats.bytes += skb->len;
  48. sch->stats.packets++;
  49. return 0;
  50. }
  51. sch->stats.drops++;
  52. #ifdef CONFIG_NET_CLS_POLICE
  53. if (sch->reshape_fail==NULL || sch->reshape_fail(skb, sch))
  54. #endif
  55. kfree_skb(skb);
  56. return NET_XMIT_DROP;
  57. }
  58. static int
  59. bfifo_requeue(struct sk_buff *skb, struct Qdisc* sch)
  60. {
  61. __skb_queue_head(&sch->q, skb);
  62. sch->stats.backlog += skb->len;
  63. return 0;
  64. }
  65. static struct sk_buff *
  66. bfifo_dequeue(struct Qdisc* sch)
  67. {
  68. struct sk_buff *skb;
  69. skb = __skb_dequeue(&sch->q);
  70. if (skb)
  71. sch->stats.backlog -= skb->len;
  72. return skb;
  73. }
  74. static int
  75. fifo_drop(struct Qdisc* sch)
  76. {
  77. struct sk_buff *skb;
  78. skb = __skb_dequeue_tail(&sch->q);
  79. if (skb) {
  80. sch->stats.backlog -= skb->len;
  81. kfree_skb(skb);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static void
  87. fifo_reset(struct Qdisc* sch)
  88. {
  89. skb_queue_purge(&sch->q);
  90. sch->stats.backlog = 0;
  91. }
  92. static int
  93. pfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  94. {
  95. struct fifo_sched_data *q = (struct fifo_sched_data *)sch->data;
  96. if (sch->q.qlen <= q->limit) {
  97. __skb_queue_tail(&sch->q, skb);
  98. sch->stats.bytes += skb->len;
  99. sch->stats.packets++;
  100. return 0;
  101. }
  102. sch->stats.drops++;
  103. #ifdef CONFIG_NET_CLS_POLICE
  104. if (sch->reshape_fail==NULL || sch->reshape_fail(skb, sch))
  105. #endif
  106. kfree_skb(skb);
  107. return NET_XMIT_DROP;
  108. }
  109. static int
  110. pfifo_requeue(struct sk_buff *skb, struct Qdisc* sch)
  111. {
  112. __skb_queue_head(&sch->q, skb);
  113. return 0;
  114. }
  115. static struct sk_buff *
  116. pfifo_dequeue(struct Qdisc* sch)
  117. {
  118. return __skb_dequeue(&sch->q);
  119. }
  120. static int fifo_init(struct Qdisc *sch, struct rtattr *opt)
  121. {
  122. struct fifo_sched_data *q = (void*)sch->data;
  123. if (opt == NULL) {
  124. if (sch->ops == &bfifo_qdisc_ops)
  125. q->limit = sch->dev->tx_queue_len*sch->dev->mtu;
  126. else
  127. q->limit = sch->dev->tx_queue_len;
  128. } else {
  129. struct tc_fifo_qopt *ctl = RTA_DATA(opt);
  130. if (opt->rta_len < RTA_LENGTH(sizeof(*ctl)))
  131. return -EINVAL;
  132. q->limit = ctl->limit;
  133. }
  134. return 0;
  135. }
  136. static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  137. {
  138. struct fifo_sched_data *q = (void*)sch->data;
  139. unsigned char  *b = skb->tail;
  140. struct tc_fifo_qopt opt;
  141. opt.limit = q->limit;
  142. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  143. return skb->len;
  144. rtattr_failure:
  145. skb_trim(skb, b - skb->data);
  146. return -1;
  147. }
  148. struct Qdisc_ops pfifo_qdisc_ops =
  149. {
  150. NULL,
  151. NULL,
  152. "pfifo",
  153. sizeof(struct fifo_sched_data),
  154. pfifo_enqueue,
  155. pfifo_dequeue,
  156. pfifo_requeue,
  157. fifo_drop,
  158. fifo_init,
  159. fifo_reset,
  160. NULL,
  161. fifo_init,
  162. fifo_dump,
  163. };
  164. struct Qdisc_ops bfifo_qdisc_ops =
  165. {
  166. NULL,
  167. NULL,
  168. "bfifo",
  169. sizeof(struct fifo_sched_data),
  170. bfifo_enqueue,
  171. bfifo_dequeue,
  172. bfifo_requeue,
  173. fifo_drop,
  174. fifo_init,
  175. fifo_reset,
  176. NULL,
  177. fifo_init,
  178. fifo_dump,
  179. };