xref: /openbmc/linux/net/sched/sch_fifo.c (revision 6473990c7f0565fca2007f8662395d122e30f0d8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sched/sch_fifo.c	The simplest FIFO queue.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
51da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
61da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
71da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/skbuff.h>
171da177e4SLinus Torvalds #include <net/pkt_sched.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /* 1 band FIFO pseudo-"scheduler" */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds struct fifo_sched_data
221da177e4SLinus Torvalds {
236fc8e84fSThomas Graf 	u32 limit;
241da177e4SLinus Torvalds };
251da177e4SLinus Torvalds 
266fc8e84fSThomas Graf static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds 	struct fifo_sched_data *q = qdisc_priv(sch);
291da177e4SLinus Torvalds 
300abf77e5SJussi Kivilinna 	if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= q->limit))
31aaae3013SThomas Graf 		return qdisc_enqueue_tail(skb, sch);
321da177e4SLinus Torvalds 
33aaae3013SThomas Graf 	return qdisc_reshape_fail(skb, sch);
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds 
366fc8e84fSThomas Graf static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc* sch)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct fifo_sched_data *q = qdisc_priv(sch);
391da177e4SLinus Torvalds 
40aaae3013SThomas Graf 	if (likely(skb_queue_len(&sch->q) < q->limit))
41aaae3013SThomas Graf 		return qdisc_enqueue_tail(skb, sch);
421da177e4SLinus Torvalds 
43aaae3013SThomas Graf 	return qdisc_reshape_fail(skb, sch);
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461e90474cSPatrick McHardy static int fifo_init(struct Qdisc *sch, struct nlattr *opt)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds 	struct fifo_sched_data *q = qdisc_priv(sch);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	if (opt == NULL) {
515ce2d488SDavid S. Miller 		u32 limit = qdisc_dev(sch)->tx_queue_len ? : 1;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 		if (sch->ops == &bfifo_qdisc_ops)
54*6473990cSPatrick McHardy 			limit *= psched_mtu(qdisc_dev(sch));
556fc8e84fSThomas Graf 
561da177e4SLinus Torvalds 		q->limit = limit;
571da177e4SLinus Torvalds 	} else {
581e90474cSPatrick McHardy 		struct tc_fifo_qopt *ctl = nla_data(opt);
596fc8e84fSThomas Graf 
601e90474cSPatrick McHardy 		if (nla_len(opt) < sizeof(*ctl))
611da177e4SLinus Torvalds 			return -EINVAL;
626fc8e84fSThomas Graf 
631da177e4SLinus Torvalds 		q->limit = ctl->limit;
641da177e4SLinus Torvalds 	}
656fc8e84fSThomas Graf 
661da177e4SLinus Torvalds 	return 0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	struct fifo_sched_data *q = qdisc_priv(sch);
726fc8e84fSThomas Graf 	struct tc_fifo_qopt opt = { .limit = q->limit };
731da177e4SLinus Torvalds 
741e90474cSPatrick McHardy 	NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
751da177e4SLinus Torvalds 	return skb->len;
761da177e4SLinus Torvalds 
771e90474cSPatrick McHardy nla_put_failure:
781da177e4SLinus Torvalds 	return -1;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
8120fea08bSEric Dumazet struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
821da177e4SLinus Torvalds 	.id		=	"pfifo",
831da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct fifo_sched_data),
841da177e4SLinus Torvalds 	.enqueue	=	pfifo_enqueue,
85aaae3013SThomas Graf 	.dequeue	=	qdisc_dequeue_head,
8648a8f519SPatrick McHardy 	.peek		=	qdisc_peek_head,
87aaae3013SThomas Graf 	.drop		=	qdisc_queue_drop,
881da177e4SLinus Torvalds 	.init		=	fifo_init,
89aaae3013SThomas Graf 	.reset		=	qdisc_reset_queue,
901da177e4SLinus Torvalds 	.change		=	fifo_init,
911da177e4SLinus Torvalds 	.dump		=	fifo_dump,
921da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
931da177e4SLinus Torvalds };
9462e3ba1bSPatrick McHardy EXPORT_SYMBOL(pfifo_qdisc_ops);
951da177e4SLinus Torvalds 
9620fea08bSEric Dumazet struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
971da177e4SLinus Torvalds 	.id		=	"bfifo",
981da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct fifo_sched_data),
991da177e4SLinus Torvalds 	.enqueue	=	bfifo_enqueue,
100aaae3013SThomas Graf 	.dequeue	=	qdisc_dequeue_head,
10148a8f519SPatrick McHardy 	.peek		=	qdisc_peek_head,
102aaae3013SThomas Graf 	.drop		=	qdisc_queue_drop,
1031da177e4SLinus Torvalds 	.init		=	fifo_init,
104aaae3013SThomas Graf 	.reset		=	qdisc_reset_queue,
1051da177e4SLinus Torvalds 	.change		=	fifo_init,
1061da177e4SLinus Torvalds 	.dump		=	fifo_dump,
1071da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds EXPORT_SYMBOL(bfifo_qdisc_ops);
110fb0305ceSPatrick McHardy 
111fb0305ceSPatrick McHardy /* Pass size change message down to embedded FIFO */
112fb0305ceSPatrick McHardy int fifo_set_limit(struct Qdisc *q, unsigned int limit)
113fb0305ceSPatrick McHardy {
114fb0305ceSPatrick McHardy 	struct nlattr *nla;
115fb0305ceSPatrick McHardy 	int ret = -ENOMEM;
116fb0305ceSPatrick McHardy 
117fb0305ceSPatrick McHardy 	/* Hack to avoid sending change message to non-FIFO */
118fb0305ceSPatrick McHardy 	if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
119fb0305ceSPatrick McHardy 		return 0;
120fb0305ceSPatrick McHardy 
121fb0305ceSPatrick McHardy 	nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
122fb0305ceSPatrick McHardy 	if (nla) {
123fb0305ceSPatrick McHardy 		nla->nla_type = RTM_NEWQDISC;
124fb0305ceSPatrick McHardy 		nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
125fb0305ceSPatrick McHardy 		((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
126fb0305ceSPatrick McHardy 
127fb0305ceSPatrick McHardy 		ret = q->ops->change(q, nla);
128fb0305ceSPatrick McHardy 		kfree(nla);
129fb0305ceSPatrick McHardy 	}
130fb0305ceSPatrick McHardy 	return ret;
131fb0305ceSPatrick McHardy }
132fb0305ceSPatrick McHardy EXPORT_SYMBOL(fifo_set_limit);
133fb0305ceSPatrick McHardy 
134fb0305ceSPatrick McHardy struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
135fb0305ceSPatrick McHardy 			       unsigned int limit)
136fb0305ceSPatrick McHardy {
137fb0305ceSPatrick McHardy 	struct Qdisc *q;
138fb0305ceSPatrick McHardy 	int err = -ENOMEM;
139fb0305ceSPatrick McHardy 
1405ce2d488SDavid S. Miller 	q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
141bb949fbdSDavid S. Miller 			      ops, TC_H_MAKE(sch->handle, 1));
142fb0305ceSPatrick McHardy 	if (q) {
143fb0305ceSPatrick McHardy 		err = fifo_set_limit(q, limit);
144fb0305ceSPatrick McHardy 		if (err < 0) {
145fb0305ceSPatrick McHardy 			qdisc_destroy(q);
146fb0305ceSPatrick McHardy 			q = NULL;
147fb0305ceSPatrick McHardy 		}
148fb0305ceSPatrick McHardy 	}
149fb0305ceSPatrick McHardy 
150fb0305ceSPatrick McHardy 	return q ? : ERR_PTR(err);
151fb0305ceSPatrick McHardy }
152fb0305ceSPatrick McHardy EXPORT_SYMBOL(fifo_create_dflt);
153