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