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