sch_fifo.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/sch_fifo.c The simplest FIFO queue.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/skbuff.h>
  13. #include <net/pkt_sched.h>
  14. #include <net/pkt_cls.h>
  15. /* 1 band FIFO pseudo-"scheduler" */
  16. static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  17. struct sk_buff **to_free)
  18. {
  19. if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit))
  20. return qdisc_enqueue_tail(skb, sch);
  21. return qdisc_drop(skb, sch, to_free);
  22. }
  23. static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  24. struct sk_buff **to_free)
  25. {
  26. if (likely(sch->q.qlen < sch->limit))
  27. return qdisc_enqueue_tail(skb, sch);
  28. return qdisc_drop(skb, sch, to_free);
  29. }
  30. static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  31. struct sk_buff **to_free)
  32. {
  33. unsigned int prev_backlog;
  34. if (likely(sch->q.qlen < sch->limit))
  35. return qdisc_enqueue_tail(skb, sch);
  36. prev_backlog = sch->qstats.backlog;
  37. /* queue full, remove one skb to fulfill the limit */
  38. __qdisc_queue_drop_head(sch, &sch->q, to_free);
  39. qdisc_qstats_drop(sch);
  40. qdisc_enqueue_tail(skb, sch);
  41. qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
  42. return NET_XMIT_CN;
  43. }
  44. static void fifo_offload_init(struct Qdisc *sch)
  45. {
  46. struct net_device *dev = qdisc_dev(sch);
  47. struct tc_fifo_qopt_offload qopt;
  48. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  49. return;
  50. qopt.command = TC_FIFO_REPLACE;
  51. qopt.handle = sch->handle;
  52. qopt.parent = sch->parent;
  53. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_FIFO, &qopt);
  54. }
  55. static void fifo_offload_destroy(struct Qdisc *sch)
  56. {
  57. struct net_device *dev = qdisc_dev(sch);
  58. struct tc_fifo_qopt_offload qopt;
  59. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  60. return;
  61. qopt.command = TC_FIFO_DESTROY;
  62. qopt.handle = sch->handle;
  63. qopt.parent = sch->parent;
  64. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_FIFO, &qopt);
  65. }
  66. static int fifo_offload_dump(struct Qdisc *sch)
  67. {
  68. struct tc_fifo_qopt_offload qopt;
  69. qopt.command = TC_FIFO_STATS;
  70. qopt.handle = sch->handle;
  71. qopt.parent = sch->parent;
  72. qopt.stats.bstats = &sch->bstats;
  73. qopt.stats.qstats = &sch->qstats;
  74. return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_FIFO, &qopt);
  75. }
  76. static int __fifo_init(struct Qdisc *sch, struct nlattr *opt,
  77. struct netlink_ext_ack *extack)
  78. {
  79. bool bypass;
  80. bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
  81. if (opt == NULL) {
  82. u32 limit = qdisc_dev(sch)->tx_queue_len;
  83. if (is_bfifo)
  84. limit *= psched_mtu(qdisc_dev(sch));
  85. sch->limit = limit;
  86. } else {
  87. struct tc_fifo_qopt *ctl = nla_data(opt);
  88. if (nla_len(opt) < sizeof(*ctl))
  89. return -EINVAL;
  90. sch->limit = ctl->limit;
  91. }
  92. if (is_bfifo)
  93. bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
  94. else
  95. bypass = sch->limit >= 1;
  96. if (bypass)
  97. sch->flags |= TCQ_F_CAN_BYPASS;
  98. else
  99. sch->flags &= ~TCQ_F_CAN_BYPASS;
  100. return 0;
  101. }
  102. static int fifo_init(struct Qdisc *sch, struct nlattr *opt,
  103. struct netlink_ext_ack *extack)
  104. {
  105. int err;
  106. err = __fifo_init(sch, opt, extack);
  107. if (err)
  108. return err;
  109. fifo_offload_init(sch);
  110. return 0;
  111. }
  112. static int fifo_hd_init(struct Qdisc *sch, struct nlattr *opt,
  113. struct netlink_ext_ack *extack)
  114. {
  115. return __fifo_init(sch, opt, extack);
  116. }
  117. static void fifo_destroy(struct Qdisc *sch)
  118. {
  119. fifo_offload_destroy(sch);
  120. }
  121. static int __fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  122. {
  123. struct tc_fifo_qopt opt = { .limit = sch->limit };
  124. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  125. goto nla_put_failure;
  126. return skb->len;
  127. nla_put_failure:
  128. return -1;
  129. }
  130. static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  131. {
  132. int err;
  133. err = fifo_offload_dump(sch);
  134. if (err)
  135. return err;
  136. return __fifo_dump(sch, skb);
  137. }
  138. static int fifo_hd_dump(struct Qdisc *sch, struct sk_buff *skb)
  139. {
  140. return __fifo_dump(sch, skb);
  141. }
  142. struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
  143. .id = "pfifo",
  144. .priv_size = 0,
  145. .enqueue = pfifo_enqueue,
  146. .dequeue = qdisc_dequeue_head,
  147. .peek = qdisc_peek_head,
  148. .init = fifo_init,
  149. .destroy = fifo_destroy,
  150. .reset = qdisc_reset_queue,
  151. .change = fifo_init,
  152. .dump = fifo_dump,
  153. .owner = THIS_MODULE,
  154. };
  155. EXPORT_SYMBOL(pfifo_qdisc_ops);
  156. struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
  157. .id = "bfifo",
  158. .priv_size = 0,
  159. .enqueue = bfifo_enqueue,
  160. .dequeue = qdisc_dequeue_head,
  161. .peek = qdisc_peek_head,
  162. .init = fifo_init,
  163. .destroy = fifo_destroy,
  164. .reset = qdisc_reset_queue,
  165. .change = fifo_init,
  166. .dump = fifo_dump,
  167. .owner = THIS_MODULE,
  168. };
  169. EXPORT_SYMBOL(bfifo_qdisc_ops);
  170. struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
  171. .id = "pfifo_head_drop",
  172. .priv_size = 0,
  173. .enqueue = pfifo_tail_enqueue,
  174. .dequeue = qdisc_dequeue_head,
  175. .peek = qdisc_peek_head,
  176. .init = fifo_hd_init,
  177. .reset = qdisc_reset_queue,
  178. .change = fifo_hd_init,
  179. .dump = fifo_hd_dump,
  180. .owner = THIS_MODULE,
  181. };
  182. /* Pass size change message down to embedded FIFO */
  183. int fifo_set_limit(struct Qdisc *q, unsigned int limit)
  184. {
  185. struct nlattr *nla;
  186. int ret = -ENOMEM;
  187. /* Hack to avoid sending change message to non-FIFO */
  188. if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
  189. return 0;
  190. if (!q->ops->change)
  191. return 0;
  192. nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
  193. if (nla) {
  194. nla->nla_type = RTM_NEWQDISC;
  195. nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
  196. ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
  197. ret = q->ops->change(q, nla, NULL);
  198. kfree(nla);
  199. }
  200. return ret;
  201. }
  202. EXPORT_SYMBOL(fifo_set_limit);
  203. struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  204. unsigned int limit,
  205. struct netlink_ext_ack *extack)
  206. {
  207. struct Qdisc *q;
  208. int err = -ENOMEM;
  209. q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1),
  210. extack);
  211. if (q) {
  212. err = fifo_set_limit(q, limit);
  213. if (err < 0) {
  214. qdisc_put(q);
  215. q = NULL;
  216. }
  217. }
  218. return q ? : ERR_PTR(err);
  219. }
  220. EXPORT_SYMBOL(fifo_create_dflt);