sch_fifo.c 4.2 KB

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