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