sch_fifo.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 fifo_init(struct Qdisc *sch, struct nlattr *opt)
  37. {
  38. struct fifo_sched_data *q = qdisc_priv(sch);
  39. if (opt == NULL) {
  40. u32 limit = qdisc_dev(sch)->tx_queue_len ? : 1;
  41. if (sch->ops == &bfifo_qdisc_ops)
  42. limit *= psched_mtu(qdisc_dev(sch));
  43. q->limit = limit;
  44. } else {
  45. struct tc_fifo_qopt *ctl = nla_data(opt);
  46. if (nla_len(opt) < sizeof(*ctl))
  47. return -EINVAL;
  48. q->limit = ctl->limit;
  49. }
  50. return 0;
  51. }
  52. static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  53. {
  54. struct fifo_sched_data *q = qdisc_priv(sch);
  55. struct tc_fifo_qopt opt = { .limit = q->limit };
  56. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  57. return skb->len;
  58. nla_put_failure:
  59. return -1;
  60. }
  61. struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
  62. .id = "pfifo",
  63. .priv_size = sizeof(struct fifo_sched_data),
  64. .enqueue = pfifo_enqueue,
  65. .dequeue = qdisc_dequeue_head,
  66. .peek = qdisc_peek_head,
  67. .drop = qdisc_queue_drop,
  68. .init = fifo_init,
  69. .reset = qdisc_reset_queue,
  70. .change = fifo_init,
  71. .dump = fifo_dump,
  72. .owner = THIS_MODULE,
  73. };
  74. EXPORT_SYMBOL(pfifo_qdisc_ops);
  75. struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
  76. .id = "bfifo",
  77. .priv_size = sizeof(struct fifo_sched_data),
  78. .enqueue = bfifo_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(bfifo_qdisc_ops);
  89. /* Pass size change message down to embedded FIFO */
  90. int fifo_set_limit(struct Qdisc *q, unsigned int limit)
  91. {
  92. struct nlattr *nla;
  93. int ret = -ENOMEM;
  94. /* Hack to avoid sending change message to non-FIFO */
  95. if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
  96. return 0;
  97. nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
  98. if (nla) {
  99. nla->nla_type = RTM_NEWQDISC;
  100. nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
  101. ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
  102. ret = q->ops->change(q, nla);
  103. kfree(nla);
  104. }
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(fifo_set_limit);
  108. struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  109. unsigned int limit)
  110. {
  111. struct Qdisc *q;
  112. int err = -ENOMEM;
  113. q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  114. ops, TC_H_MAKE(sch->handle, 1));
  115. if (q) {
  116. err = fifo_set_limit(q, limit);
  117. if (err < 0) {
  118. qdisc_destroy(q);
  119. q = NULL;
  120. }
  121. }
  122. return q ? : ERR_PTR(err);
  123. }
  124. EXPORT_SYMBOL(fifo_create_dflt);