1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/module.h>
36#include <linux/types.h>
37#include <linux/kernel.h>
38#include <linux/errno.h>
39#include <linux/netdevice.h>
40#include <linux/skbuff.h>
41#include <net/pkt_sched.h>
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56struct plug_sched_data {
57
58
59
60
61 bool unplug_indefinite;
62
63 bool throttled;
64
65
66 u32 limit;
67
68
69
70
71 u32 pkts_current_epoch;
72
73
74
75
76
77
78 u32 pkts_last_epoch;
79
80
81
82
83
84 u32 pkts_to_release;
85};
86
87static int plug_enqueue(struct sk_buff *skb, struct Qdisc *sch,
88 struct sk_buff **to_free)
89{
90 struct plug_sched_data *q = qdisc_priv(sch);
91
92 if (likely(sch->qstats.backlog + skb->len <= q->limit)) {
93 if (!q->unplug_indefinite)
94 q->pkts_current_epoch++;
95 return qdisc_enqueue_tail(skb, sch);
96 }
97
98 return qdisc_drop(skb, sch, to_free);
99}
100
101static struct sk_buff *plug_dequeue(struct Qdisc *sch)
102{
103 struct plug_sched_data *q = qdisc_priv(sch);
104
105 if (q->throttled)
106 return NULL;
107
108 if (!q->unplug_indefinite) {
109 if (!q->pkts_to_release) {
110
111
112
113 q->throttled = true;
114 return NULL;
115 }
116 q->pkts_to_release--;
117 }
118
119 return qdisc_dequeue_head(sch);
120}
121
122static int plug_init(struct Qdisc *sch, struct nlattr *opt,
123 struct netlink_ext_ack *extack)
124{
125 struct plug_sched_data *q = qdisc_priv(sch);
126
127 q->pkts_current_epoch = 0;
128 q->pkts_last_epoch = 0;
129 q->pkts_to_release = 0;
130 q->unplug_indefinite = false;
131
132 if (opt == NULL) {
133 q->limit = qdisc_dev(sch)->tx_queue_len
134 * psched_mtu(qdisc_dev(sch));
135 } else {
136 struct tc_plug_qopt *ctl = nla_data(opt);
137
138 if (nla_len(opt) < sizeof(*ctl))
139 return -EINVAL;
140
141 q->limit = ctl->limit;
142 }
143
144 q->throttled = true;
145 return 0;
146}
147
148
149
150
151
152
153
154
155
156
157
158static int plug_change(struct Qdisc *sch, struct nlattr *opt,
159 struct netlink_ext_ack *extack)
160{
161 struct plug_sched_data *q = qdisc_priv(sch);
162 struct tc_plug_qopt *msg;
163
164 if (opt == NULL)
165 return -EINVAL;
166
167 msg = nla_data(opt);
168 if (nla_len(opt) < sizeof(*msg))
169 return -EINVAL;
170
171 switch (msg->action) {
172 case TCQ_PLUG_BUFFER:
173
174 q->pkts_last_epoch = q->pkts_current_epoch;
175 q->pkts_current_epoch = 0;
176 if (q->unplug_indefinite)
177 q->throttled = true;
178 q->unplug_indefinite = false;
179 break;
180 case TCQ_PLUG_RELEASE_ONE:
181
182
183
184 q->pkts_to_release += q->pkts_last_epoch;
185 q->pkts_last_epoch = 0;
186 q->throttled = false;
187 netif_schedule_queue(sch->dev_queue);
188 break;
189 case TCQ_PLUG_RELEASE_INDEFINITE:
190 q->unplug_indefinite = true;
191 q->pkts_to_release = 0;
192 q->pkts_last_epoch = 0;
193 q->pkts_current_epoch = 0;
194 q->throttled = false;
195 netif_schedule_queue(sch->dev_queue);
196 break;
197 case TCQ_PLUG_LIMIT:
198
199 q->limit = msg->limit;
200 break;
201 default:
202 return -EINVAL;
203 }
204
205 return 0;
206}
207
208static struct Qdisc_ops plug_qdisc_ops __read_mostly = {
209 .id = "plug",
210 .priv_size = sizeof(struct plug_sched_data),
211 .enqueue = plug_enqueue,
212 .dequeue = plug_dequeue,
213 .peek = qdisc_peek_head,
214 .init = plug_init,
215 .change = plug_change,
216 .reset = qdisc_reset_queue,
217 .owner = THIS_MODULE,
218};
219
220static int __init plug_module_init(void)
221{
222 return register_qdisc(&plug_qdisc_ops);
223}
224
225static void __exit plug_module_exit(void)
226{
227 unregister_qdisc(&plug_qdisc_ops);
228}
229module_init(plug_module_init)
230module_exit(plug_module_exit)
231MODULE_LICENSE("GPL");
232