]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * net/sched/sch_fifo.c The simplest FIFO queue. | |
4 | * | |
1da177e4 LT |
5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
6 | */ | |
7 | ||
1da177e4 | 8 | #include <linux/module.h> |
5a0e3ad6 | 9 | #include <linux/slab.h> |
1da177e4 LT |
10 | #include <linux/types.h> |
11 | #include <linux/kernel.h> | |
1da177e4 | 12 | #include <linux/errno.h> |
1da177e4 | 13 | #include <linux/skbuff.h> |
1da177e4 LT |
14 | #include <net/pkt_sched.h> |
15 | ||
16 | /* 1 band FIFO pseudo-"scheduler" */ | |
17 | ||
520ac30f ED |
18 | static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
19 | struct sk_buff **to_free) | |
1da177e4 | 20 | { |
d276055c | 21 | if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit)) |
aaae3013 | 22 | return qdisc_enqueue_tail(skb, sch); |
1da177e4 | 23 | |
520ac30f | 24 | return qdisc_drop(skb, sch, to_free); |
1da177e4 LT |
25 | } |
26 | ||
520ac30f ED |
27 | static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
28 | struct sk_buff **to_free) | |
1da177e4 | 29 | { |
97d0678f | 30 | if (likely(sch->q.qlen < sch->limit)) |
aaae3013 | 31 | return qdisc_enqueue_tail(skb, sch); |
1da177e4 | 32 | |
520ac30f | 33 | return qdisc_drop(skb, sch, to_free); |
1da177e4 LT |
34 | } |
35 | ||
520ac30f ED |
36 | static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
37 | struct sk_buff **to_free) | |
57dbb2d8 | 38 | { |
6c0d54f1 ED |
39 | unsigned int prev_backlog; |
40 | ||
97d0678f | 41 | if (likely(sch->q.qlen < sch->limit)) |
57dbb2d8 HPP |
42 | return qdisc_enqueue_tail(skb, sch); |
43 | ||
6c0d54f1 | 44 | prev_backlog = sch->qstats.backlog; |
57dbb2d8 | 45 | /* queue full, remove one skb to fulfill the limit */ |
520ac30f | 46 | __qdisc_queue_drop_head(sch, &sch->q, to_free); |
25331d6c | 47 | qdisc_qstats_drop(sch); |
57dbb2d8 HPP |
48 | qdisc_enqueue_tail(skb, sch); |
49 | ||
6c0d54f1 | 50 | qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog); |
57dbb2d8 HPP |
51 | return NET_XMIT_CN; |
52 | } | |
53 | ||
e63d7dfd AA |
54 | static int fifo_init(struct Qdisc *sch, struct nlattr *opt, |
55 | struct netlink_ext_ack *extack) | |
1da177e4 | 56 | { |
23624935 ED |
57 | bool bypass; |
58 | bool is_bfifo = sch->ops == &bfifo_qdisc_ops; | |
1da177e4 LT |
59 | |
60 | if (opt == NULL) { | |
348e3435 | 61 | u32 limit = qdisc_dev(sch)->tx_queue_len; |
1da177e4 | 62 | |
23624935 | 63 | if (is_bfifo) |
6473990c | 64 | limit *= psched_mtu(qdisc_dev(sch)); |
6fc8e84f | 65 | |
d276055c | 66 | sch->limit = limit; |
1da177e4 | 67 | } else { |
1e90474c | 68 | struct tc_fifo_qopt *ctl = nla_data(opt); |
6fc8e84f | 69 | |
1e90474c | 70 | if (nla_len(opt) < sizeof(*ctl)) |
1da177e4 | 71 | return -EINVAL; |
6fc8e84f | 72 | |
d276055c | 73 | sch->limit = ctl->limit; |
1da177e4 | 74 | } |
6fc8e84f | 75 | |
23624935 | 76 | if (is_bfifo) |
d276055c | 77 | bypass = sch->limit >= psched_mtu(qdisc_dev(sch)); |
23624935 | 78 | else |
d276055c | 79 | bypass = sch->limit >= 1; |
23624935 ED |
80 | |
81 | if (bypass) | |
82 | sch->flags |= TCQ_F_CAN_BYPASS; | |
83 | else | |
84 | sch->flags &= ~TCQ_F_CAN_BYPASS; | |
1da177e4 LT |
85 | return 0; |
86 | } | |
87 | ||
88 | static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb) | |
89 | { | |
d276055c | 90 | struct tc_fifo_qopt opt = { .limit = sch->limit }; |
1da177e4 | 91 | |
1b34ec43 DM |
92 | if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) |
93 | goto nla_put_failure; | |
1da177e4 LT |
94 | return skb->len; |
95 | ||
1e90474c | 96 | nla_put_failure: |
1da177e4 LT |
97 | return -1; |
98 | } | |
99 | ||
20fea08b | 100 | struct Qdisc_ops pfifo_qdisc_ops __read_mostly = { |
1da177e4 | 101 | .id = "pfifo", |
d276055c | 102 | .priv_size = 0, |
1da177e4 | 103 | .enqueue = pfifo_enqueue, |
aaae3013 | 104 | .dequeue = qdisc_dequeue_head, |
48a8f519 | 105 | .peek = qdisc_peek_head, |
1da177e4 | 106 | .init = fifo_init, |
aaae3013 | 107 | .reset = qdisc_reset_queue, |
2030721c | 108 | .change = fifo_init, |
1da177e4 LT |
109 | .dump = fifo_dump, |
110 | .owner = THIS_MODULE, | |
111 | }; | |
62e3ba1b | 112 | EXPORT_SYMBOL(pfifo_qdisc_ops); |
1da177e4 | 113 | |
20fea08b | 114 | struct Qdisc_ops bfifo_qdisc_ops __read_mostly = { |
1da177e4 | 115 | .id = "bfifo", |
d276055c | 116 | .priv_size = 0, |
1da177e4 | 117 | .enqueue = bfifo_enqueue, |
aaae3013 | 118 | .dequeue = qdisc_dequeue_head, |
48a8f519 | 119 | .peek = qdisc_peek_head, |
1da177e4 | 120 | .init = fifo_init, |
aaae3013 | 121 | .reset = qdisc_reset_queue, |
2030721c | 122 | .change = fifo_init, |
1da177e4 LT |
123 | .dump = fifo_dump, |
124 | .owner = THIS_MODULE, | |
125 | }; | |
1da177e4 | 126 | EXPORT_SYMBOL(bfifo_qdisc_ops); |
fb0305ce | 127 | |
57dbb2d8 HPP |
128 | struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = { |
129 | .id = "pfifo_head_drop", | |
d276055c | 130 | .priv_size = 0, |
57dbb2d8 HPP |
131 | .enqueue = pfifo_tail_enqueue, |
132 | .dequeue = qdisc_dequeue_head, | |
133 | .peek = qdisc_peek_head, | |
57dbb2d8 HPP |
134 | .init = fifo_init, |
135 | .reset = qdisc_reset_queue, | |
2030721c | 136 | .change = fifo_init, |
57dbb2d8 HPP |
137 | .dump = fifo_dump, |
138 | .owner = THIS_MODULE, | |
139 | }; | |
140 | ||
fb0305ce PM |
141 | /* Pass size change message down to embedded FIFO */ |
142 | int fifo_set_limit(struct Qdisc *q, unsigned int limit) | |
143 | { | |
144 | struct nlattr *nla; | |
145 | int ret = -ENOMEM; | |
146 | ||
147 | /* Hack to avoid sending change message to non-FIFO */ | |
148 | if (strncmp(q->ops->id + 1, "fifo", 4) != 0) | |
149 | return 0; | |
150 | ||
151 | nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL); | |
152 | if (nla) { | |
153 | nla->nla_type = RTM_NEWQDISC; | |
154 | nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt)); | |
155 | ((struct tc_fifo_qopt *)nla_data(nla))->limit = limit; | |
156 | ||
2030721c | 157 | ret = q->ops->change(q, nla, NULL); |
fb0305ce PM |
158 | kfree(nla); |
159 | } | |
160 | return ret; | |
161 | } | |
162 | EXPORT_SYMBOL(fifo_set_limit); | |
163 | ||
164 | struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops, | |
a38a9882 AA |
165 | unsigned int limit, |
166 | struct netlink_ext_ack *extack) | |
fb0305ce PM |
167 | { |
168 | struct Qdisc *q; | |
169 | int err = -ENOMEM; | |
170 | ||
a38a9882 AA |
171 | q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1), |
172 | extack); | |
fb0305ce PM |
173 | if (q) { |
174 | err = fifo_set_limit(q, limit); | |
175 | if (err < 0) { | |
86bd446b | 176 | qdisc_put(q); |
fb0305ce PM |
177 | q = NULL; |
178 | } | |
179 | } | |
180 | ||
181 | return q ? : ERR_PTR(err); | |
182 | } | |
183 | EXPORT_SYMBOL(fifo_create_dflt); |