sch_fifo.c 4.1 KB

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