sch_fifo.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/skbuff.h>
  16. #include <net/pkt_sched.h>
  17. /* 1 band FIFO pseudo-"scheduler" */
  18. struct fifo_sched_data
  19. {
  20. u32 limit;
  21. };
  22. static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  23. {
  24. struct fifo_sched_data *q = qdisc_priv(sch);
  25. if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= q->limit))
  26. return qdisc_enqueue_tail(skb, sch);
  27. return qdisc_reshape_fail(skb, sch);
  28. }
  29. static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  30. {
  31. struct fifo_sched_data *q = qdisc_priv(sch);
  32. if (likely(skb_queue_len(&sch->q) < q->limit))
  33. return qdisc_enqueue_tail(skb, sch);
  34. return qdisc_reshape_fail(skb, sch);
  35. }
  36. static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  37. {
  38. struct sk_buff *skb_head;
  39. struct fifo_sched_data *q = qdisc_priv(sch);
  40. if (likely(skb_queue_len(&sch->q) < q->limit))
  41. return qdisc_enqueue_tail(skb, sch);
  42. /* queue full, remove one skb to fulfill the limit */
  43. skb_head = qdisc_dequeue_head(sch);
  44. sch->bstats.bytes -= qdisc_pkt_len(skb_head);
  45. sch->bstats.packets--;
  46. sch->qstats.drops++;
  47. kfree_skb(skb_head);
  48. qdisc_enqueue_tail(skb, sch);
  49. return NET_XMIT_CN;
  50. }
  51. static int fifo_init(struct Qdisc *sch, struct nlattr *opt)
  52. {
  53. struct fifo_sched_data *q = qdisc_priv(sch);
  54. if (opt == NULL) {
  55. u32 limit = qdisc_dev(sch)->tx_queue_len ? : 1;
  56. if (sch->ops == &bfifo_qdisc_ops)
  57. limit *= psched_mtu(qdisc_dev(sch));
  58. q->limit = limit;
  59. } else {
  60. struct tc_fifo_qopt *ctl = nla_data(opt);
  61. if (nla_len(opt) < sizeof(*ctl))
  62. return -EINVAL;
  63. q->limit = ctl->limit;
  64. }
  65. return 0;
  66. }
  67. static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  68. {
  69. struct fifo_sched_data *q = qdisc_priv(sch);
  70. struct tc_fifo_qopt opt = { .limit = q->limit };
  71. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  72. return skb->len;
  73. nla_put_failure:
  74. return -1;
  75. }
  76. struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
  77. .id = "pfifo",
  78. .priv_size = sizeof(struct fifo_sched_data),
  79. .enqueue = pfifo_enqueue,
  80. .dequeue = qdisc_dequeue_head,
  81. .peek = qdisc_peek_head,
  82. .drop = qdisc_queue_drop,
  83. .init = fifo_init,
  84. .reset = qdisc_reset_queue,
  85. .change = fifo_init,
  86. .dump = fifo_dump,
  87. .owner = THIS_MODULE,
  88. };
  89. EXPORT_SYMBOL(pfifo_qdisc_ops);
  90. struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
  91. .id = "bfifo",
  92. .priv_size = sizeof(struct fifo_sched_data),
  93. .enqueue = bfifo_enqueue,
  94. .dequeue = qdisc_dequeue_head,
  95. .peek = qdisc_peek_head,
  96. .drop = qdisc_queue_drop,
  97. .init = fifo_init,
  98. .reset = qdisc_reset_queue,
  99. .change = fifo_init,
  100. .dump = fifo_dump,
  101. .owner = THIS_MODULE,
  102. };
  103. EXPORT_SYMBOL(bfifo_qdisc_ops);
  104. struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
  105. .id = "pfifo_head_drop",
  106. .priv_size = sizeof(struct fifo_sched_data),
  107. .enqueue = pfifo_tail_enqueue,
  108. .dequeue = qdisc_dequeue_head,
  109. .peek = qdisc_peek_head,
  110. .drop = qdisc_queue_drop_head,
  111. .init = fifo_init,
  112. .reset = qdisc_reset_queue,
  113. .change = fifo_init,
  114. .dump = fifo_dump,
  115. .owner = THIS_MODULE,
  116. };
  117. /* Pass size change message down to embedded FIFO */
  118. int fifo_set_limit(struct Qdisc *q, unsigned int limit)
  119. {
  120. struct nlattr *nla;
  121. int ret = -ENOMEM;
  122. /* Hack to avoid sending change message to non-FIFO */
  123. if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
  124. return 0;
  125. nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
  126. if (nla) {
  127. nla->nla_type = RTM_NEWQDISC;
  128. nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
  129. ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
  130. ret = q->ops->change(q, nla);
  131. kfree(nla);
  132. }
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(fifo_set_limit);
  136. struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  137. unsigned int limit)
  138. {
  139. struct Qdisc *q;
  140. int err = -ENOMEM;
  141. q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  142. ops, TC_H_MAKE(sch->handle, 1));
  143. if (q) {
  144. err = fifo_set_limit(q, limit);
  145. if (err < 0) {
  146. qdisc_destroy(q);
  147. q = NULL;
  148. }
  149. }
  150. return q ? : ERR_PTR(err);
  151. }
  152. EXPORT_SYMBOL(fifo_create_dflt);