sch_fifo.c 4.5 KB

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