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