sch_fifo.c 4.4 KB

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