12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c3059be1SShriram Rajagopalan /*
3c3059be1SShriram Rajagopalan * sch_plug.c Queue traffic until an explicit release command
4c3059be1SShriram Rajagopalan *
5c3059be1SShriram Rajagopalan * There are two ways to use this qdisc:
6c3059be1SShriram Rajagopalan * 1. A simple "instantaneous" plug/unplug operation, by issuing an alternating
7c3059be1SShriram Rajagopalan * sequence of TCQ_PLUG_BUFFER & TCQ_PLUG_RELEASE_INDEFINITE commands.
8c3059be1SShriram Rajagopalan *
9c3059be1SShriram Rajagopalan * 2. For network output buffering (a.k.a output commit) functionality.
10c3059be1SShriram Rajagopalan * Output commit property is commonly used by applications using checkpoint
11c3059be1SShriram Rajagopalan * based fault-tolerance to ensure that the checkpoint from which a system
12c3059be1SShriram Rajagopalan * is being restored is consistent w.r.t outside world.
13c3059be1SShriram Rajagopalan *
14c3059be1SShriram Rajagopalan * Consider for e.g. Remus - a Virtual Machine checkpointing system,
15c3059be1SShriram Rajagopalan * wherein a VM is checkpointed, say every 50ms. The checkpoint is replicated
16c3059be1SShriram Rajagopalan * asynchronously to the backup host, while the VM continues executing the
17c3059be1SShriram Rajagopalan * next epoch speculatively.
18c3059be1SShriram Rajagopalan *
19c3059be1SShriram Rajagopalan * The following is a typical sequence of output buffer operations:
20c3059be1SShriram Rajagopalan * 1.At epoch i, start_buffer(i)
21c3059be1SShriram Rajagopalan * 2. At end of epoch i (i.e. after 50ms):
22c3059be1SShriram Rajagopalan * 2.1 Stop VM and take checkpoint(i).
23c3059be1SShriram Rajagopalan * 2.2 start_buffer(i+1) and Resume VM
24c3059be1SShriram Rajagopalan * 3. While speculatively executing epoch(i+1), asynchronously replicate
25c3059be1SShriram Rajagopalan * checkpoint(i) to backup host.
26c3059be1SShriram Rajagopalan * 4. When checkpoint_ack(i) is received from backup, release_buffer(i)
27c3059be1SShriram Rajagopalan * Thus, this Qdisc would receive the following sequence of commands:
28c3059be1SShriram Rajagopalan * TCQ_PLUG_BUFFER (epoch i)
29c3059be1SShriram Rajagopalan * .. TCQ_PLUG_BUFFER (epoch i+1)
30c3059be1SShriram Rajagopalan * ....TCQ_PLUG_RELEASE_ONE (epoch i)
31c3059be1SShriram Rajagopalan * ......TCQ_PLUG_BUFFER (epoch i+2)
32c3059be1SShriram Rajagopalan * ........
33c3059be1SShriram Rajagopalan */
34c3059be1SShriram Rajagopalan
35c3059be1SShriram Rajagopalan #include <linux/module.h>
36c3059be1SShriram Rajagopalan #include <linux/types.h>
37c3059be1SShriram Rajagopalan #include <linux/kernel.h>
38c3059be1SShriram Rajagopalan #include <linux/errno.h>
39c3059be1SShriram Rajagopalan #include <linux/netdevice.h>
40c3059be1SShriram Rajagopalan #include <linux/skbuff.h>
41c3059be1SShriram Rajagopalan #include <net/pkt_sched.h>
42c3059be1SShriram Rajagopalan
43c3059be1SShriram Rajagopalan /*
44c3059be1SShriram Rajagopalan * State of the queue, when used for network output buffering:
45c3059be1SShriram Rajagopalan *
46c3059be1SShriram Rajagopalan * plug(i+1) plug(i) head
47c3059be1SShriram Rajagopalan * ------------------+--------------------+---------------->
48c3059be1SShriram Rajagopalan * | |
49c3059be1SShriram Rajagopalan * | |
50c3059be1SShriram Rajagopalan * pkts_current_epoch| pkts_last_epoch |pkts_to_release
51c3059be1SShriram Rajagopalan * ----------------->|<--------+--------->|+--------------->
52c3059be1SShriram Rajagopalan * v v
53c3059be1SShriram Rajagopalan *
54c3059be1SShriram Rajagopalan */
55c3059be1SShriram Rajagopalan
56c3059be1SShriram Rajagopalan struct plug_sched_data {
57c3059be1SShriram Rajagopalan /* If true, the dequeue function releases all packets
58c3059be1SShriram Rajagopalan * from head to end of the queue. The queue turns into
59c3059be1SShriram Rajagopalan * a pass-through queue for newly arriving packets.
60c3059be1SShriram Rajagopalan */
61c3059be1SShriram Rajagopalan bool unplug_indefinite;
62c3059be1SShriram Rajagopalan
638fe6a79fSEric Dumazet bool throttled;
648fe6a79fSEric Dumazet
65c3059be1SShriram Rajagopalan /* Queue Limit in bytes */
66c3059be1SShriram Rajagopalan u32 limit;
67c3059be1SShriram Rajagopalan
68c3059be1SShriram Rajagopalan /* Number of packets (output) from the current speculatively
69c3059be1SShriram Rajagopalan * executing epoch.
70c3059be1SShriram Rajagopalan */
71c3059be1SShriram Rajagopalan u32 pkts_current_epoch;
72c3059be1SShriram Rajagopalan
73c3059be1SShriram Rajagopalan /* Number of packets corresponding to the recently finished
74c3059be1SShriram Rajagopalan * epoch. These will be released when we receive a
75c3059be1SShriram Rajagopalan * TCQ_PLUG_RELEASE_ONE command. This command is typically
76c3059be1SShriram Rajagopalan * issued after committing a checkpoint at the target.
77c3059be1SShriram Rajagopalan */
78c3059be1SShriram Rajagopalan u32 pkts_last_epoch;
79c3059be1SShriram Rajagopalan
80c3059be1SShriram Rajagopalan /*
81c3059be1SShriram Rajagopalan * Number of packets from the head of the queue, that can
82c3059be1SShriram Rajagopalan * be released (committed checkpoint).
83c3059be1SShriram Rajagopalan */
84c3059be1SShriram Rajagopalan u32 pkts_to_release;
85c3059be1SShriram Rajagopalan };
86c3059be1SShriram Rajagopalan
plug_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)87520ac30fSEric Dumazet static int plug_enqueue(struct sk_buff *skb, struct Qdisc *sch,
88520ac30fSEric Dumazet struct sk_buff **to_free)
89c3059be1SShriram Rajagopalan {
90c3059be1SShriram Rajagopalan struct plug_sched_data *q = qdisc_priv(sch);
91c3059be1SShriram Rajagopalan
92c3059be1SShriram Rajagopalan if (likely(sch->qstats.backlog + skb->len <= q->limit)) {
93c3059be1SShriram Rajagopalan if (!q->unplug_indefinite)
94c3059be1SShriram Rajagopalan q->pkts_current_epoch++;
95c3059be1SShriram Rajagopalan return qdisc_enqueue_tail(skb, sch);
96c3059be1SShriram Rajagopalan }
97c3059be1SShriram Rajagopalan
98520ac30fSEric Dumazet return qdisc_drop(skb, sch, to_free);
99c3059be1SShriram Rajagopalan }
100c3059be1SShriram Rajagopalan
plug_dequeue(struct Qdisc * sch)101c3059be1SShriram Rajagopalan static struct sk_buff *plug_dequeue(struct Qdisc *sch)
102c3059be1SShriram Rajagopalan {
103c3059be1SShriram Rajagopalan struct plug_sched_data *q = qdisc_priv(sch);
104c3059be1SShriram Rajagopalan
1058fe6a79fSEric Dumazet if (q->throttled)
106c3059be1SShriram Rajagopalan return NULL;
107c3059be1SShriram Rajagopalan
108c3059be1SShriram Rajagopalan if (!q->unplug_indefinite) {
109c3059be1SShriram Rajagopalan if (!q->pkts_to_release) {
110c3059be1SShriram Rajagopalan /* No more packets to dequeue. Block the queue
111c3059be1SShriram Rajagopalan * and wait for the next release command.
112c3059be1SShriram Rajagopalan */
1138fe6a79fSEric Dumazet q->throttled = true;
114c3059be1SShriram Rajagopalan return NULL;
115c3059be1SShriram Rajagopalan }
116c3059be1SShriram Rajagopalan q->pkts_to_release--;
117c3059be1SShriram Rajagopalan }
118c3059be1SShriram Rajagopalan
119c3059be1SShriram Rajagopalan return qdisc_dequeue_head(sch);
120c3059be1SShriram Rajagopalan }
121c3059be1SShriram Rajagopalan
plug_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)122e63d7dfdSAlexander Aring static int plug_init(struct Qdisc *sch, struct nlattr *opt,
123e63d7dfdSAlexander Aring struct netlink_ext_ack *extack)
124c3059be1SShriram Rajagopalan {
125c3059be1SShriram Rajagopalan struct plug_sched_data *q = qdisc_priv(sch);
126c3059be1SShriram Rajagopalan
127c3059be1SShriram Rajagopalan q->pkts_current_epoch = 0;
128c3059be1SShriram Rajagopalan q->pkts_last_epoch = 0;
129c3059be1SShriram Rajagopalan q->pkts_to_release = 0;
130c3059be1SShriram Rajagopalan q->unplug_indefinite = false;
131c3059be1SShriram Rajagopalan
132c3059be1SShriram Rajagopalan if (opt == NULL) {
133348e3435SPhil Sutter q->limit = qdisc_dev(sch)->tx_queue_len
134348e3435SPhil Sutter * psched_mtu(qdisc_dev(sch));
135c3059be1SShriram Rajagopalan } else {
136c3059be1SShriram Rajagopalan struct tc_plug_qopt *ctl = nla_data(opt);
137c3059be1SShriram Rajagopalan
138c3059be1SShriram Rajagopalan if (nla_len(opt) < sizeof(*ctl))
139c3059be1SShriram Rajagopalan return -EINVAL;
140c3059be1SShriram Rajagopalan
141c3059be1SShriram Rajagopalan q->limit = ctl->limit;
142c3059be1SShriram Rajagopalan }
143c3059be1SShriram Rajagopalan
1448fe6a79fSEric Dumazet q->throttled = true;
145c3059be1SShriram Rajagopalan return 0;
146c3059be1SShriram Rajagopalan }
147c3059be1SShriram Rajagopalan
148c3059be1SShriram Rajagopalan /* Receives 4 types of messages:
149c3059be1SShriram Rajagopalan * TCQ_PLUG_BUFFER: Inset a plug into the queue and
150c3059be1SShriram Rajagopalan * buffer any incoming packets
151c3059be1SShriram Rajagopalan * TCQ_PLUG_RELEASE_ONE: Dequeue packets from queue head
152c3059be1SShriram Rajagopalan * to beginning of the next plug.
153c3059be1SShriram Rajagopalan * TCQ_PLUG_RELEASE_INDEFINITE: Dequeue all packets from queue.
154c3059be1SShriram Rajagopalan * Stop buffering packets until the next TCQ_PLUG_BUFFER
155c3059be1SShriram Rajagopalan * command is received (just act as a pass-thru queue).
156c3059be1SShriram Rajagopalan * TCQ_PLUG_LIMIT: Increase/decrease queue size
157c3059be1SShriram Rajagopalan */
plug_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)1582030721cSAlexander Aring static int plug_change(struct Qdisc *sch, struct nlattr *opt,
1592030721cSAlexander Aring struct netlink_ext_ack *extack)
160c3059be1SShriram Rajagopalan {
161c3059be1SShriram Rajagopalan struct plug_sched_data *q = qdisc_priv(sch);
162c3059be1SShriram Rajagopalan struct tc_plug_qopt *msg;
163c3059be1SShriram Rajagopalan
164c3059be1SShriram Rajagopalan msg = nla_data(opt);
165c3059be1SShriram Rajagopalan if (nla_len(opt) < sizeof(*msg))
166c3059be1SShriram Rajagopalan return -EINVAL;
167c3059be1SShriram Rajagopalan
168c3059be1SShriram Rajagopalan switch (msg->action) {
169c3059be1SShriram Rajagopalan case TCQ_PLUG_BUFFER:
170c3059be1SShriram Rajagopalan /* Save size of the current buffer */
171c3059be1SShriram Rajagopalan q->pkts_last_epoch = q->pkts_current_epoch;
172c3059be1SShriram Rajagopalan q->pkts_current_epoch = 0;
173c3059be1SShriram Rajagopalan if (q->unplug_indefinite)
1748fe6a79fSEric Dumazet q->throttled = true;
175c3059be1SShriram Rajagopalan q->unplug_indefinite = false;
176c3059be1SShriram Rajagopalan break;
177c3059be1SShriram Rajagopalan case TCQ_PLUG_RELEASE_ONE:
178c3059be1SShriram Rajagopalan /* Add packets from the last complete buffer to the
179c3059be1SShriram Rajagopalan * packets to be released set.
180c3059be1SShriram Rajagopalan */
181c3059be1SShriram Rajagopalan q->pkts_to_release += q->pkts_last_epoch;
182c3059be1SShriram Rajagopalan q->pkts_last_epoch = 0;
1838fe6a79fSEric Dumazet q->throttled = false;
184c3059be1SShriram Rajagopalan netif_schedule_queue(sch->dev_queue);
185c3059be1SShriram Rajagopalan break;
186c3059be1SShriram Rajagopalan case TCQ_PLUG_RELEASE_INDEFINITE:
187c3059be1SShriram Rajagopalan q->unplug_indefinite = true;
188c3059be1SShriram Rajagopalan q->pkts_to_release = 0;
189c3059be1SShriram Rajagopalan q->pkts_last_epoch = 0;
190c3059be1SShriram Rajagopalan q->pkts_current_epoch = 0;
1918fe6a79fSEric Dumazet q->throttled = false;
192c3059be1SShriram Rajagopalan netif_schedule_queue(sch->dev_queue);
193c3059be1SShriram Rajagopalan break;
194c3059be1SShriram Rajagopalan case TCQ_PLUG_LIMIT:
195c3059be1SShriram Rajagopalan /* Limit is supplied in bytes */
196c3059be1SShriram Rajagopalan q->limit = msg->limit;
197c3059be1SShriram Rajagopalan break;
198c3059be1SShriram Rajagopalan default:
199c3059be1SShriram Rajagopalan return -EINVAL;
200c3059be1SShriram Rajagopalan }
201c3059be1SShriram Rajagopalan
202c3059be1SShriram Rajagopalan return 0;
203c3059be1SShriram Rajagopalan }
204c3059be1SShriram Rajagopalan
2052132cf64SEric Dumazet static struct Qdisc_ops plug_qdisc_ops __read_mostly = {
206c3059be1SShriram Rajagopalan .id = "plug",
207c3059be1SShriram Rajagopalan .priv_size = sizeof(struct plug_sched_data),
208c3059be1SShriram Rajagopalan .enqueue = plug_enqueue,
209c3059be1SShriram Rajagopalan .dequeue = plug_dequeue,
210*8fc134feSvalis .peek = qdisc_peek_dequeued,
211c3059be1SShriram Rajagopalan .init = plug_init,
212c3059be1SShriram Rajagopalan .change = plug_change,
213fe6bea7fSWANG Cong .reset = qdisc_reset_queue,
214c3059be1SShriram Rajagopalan .owner = THIS_MODULE,
215c3059be1SShriram Rajagopalan };
216c3059be1SShriram Rajagopalan
plug_module_init(void)217c3059be1SShriram Rajagopalan static int __init plug_module_init(void)
218c3059be1SShriram Rajagopalan {
219c3059be1SShriram Rajagopalan return register_qdisc(&plug_qdisc_ops);
220c3059be1SShriram Rajagopalan }
221c3059be1SShriram Rajagopalan
plug_module_exit(void)222c3059be1SShriram Rajagopalan static void __exit plug_module_exit(void)
223c3059be1SShriram Rajagopalan {
224c3059be1SShriram Rajagopalan unregister_qdisc(&plug_qdisc_ops);
225c3059be1SShriram Rajagopalan }
226c3059be1SShriram Rajagopalan module_init(plug_module_init)
227c3059be1SShriram Rajagopalan module_exit(plug_module_exit)
228c3059be1SShriram Rajagopalan MODULE_LICENSE("GPL");
229