xref: /aosp_15_r20/external/libnl/lib/route/qdisc/plug.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2012 Shriram Rajagopalan <[email protected]>
4  */
5 
6 /**
7  * @ingroup qdisc
8  * @defgroup qdisc_plug Plug/Unplug Traffic (PLUG)
9  * @brief
10  *
11  * Queue traffic until an explicit release command.
12  *
13  * There are two ways to use this qdisc:
14  * 1. A simple "instantaneous" plug/unplug operation, by issuing an alternating
15  *    sequence of TCQ_PLUG_BUFFER & TCQ_PLUG_RELEASE_INDEFINITE commands.
16  *
17  * 2. For network output buffering (a.k.a output commit) functionality.
18  *    Output commit property is commonly used by applications using checkpoint
19  *    based fault-tolerance to ensure that the checkpoint from which a system
20  *    is being restored is consistent w.r.t outside world.
21  *
22  *    Consider for e.g. Remus - a Virtual Machine checkpointing system,
23  *    wherein a VM is checkpointed, say every 50ms. The checkpoint is replicated
24  *    asynchronously to the backup host, while the VM continues executing the
25  *    next epoch speculatively.
26  *
27  *    The following is a typical sequence of output buffer operations:
28  *       1.At epoch i, start_buffer(i)
29  *       2. At end of epoch i (i.e. after 50ms):
30  *          2.1 Stop VM and take checkpoint(i).
31  *          2.2 start_buffer(i+1) and Resume VM
32  *       3. While speculatively executing epoch(i+1), asynchronously replicate
33  *          checkpoint(i) to backup host.
34  *       4. When checkpoint_ack(i) is received from backup, release_buffer(i)
35  *    Thus, this Qdisc would receive the following sequence of commands:
36  *       TCQ_PLUG_BUFFER (epoch i)
37  *       .. TCQ_PLUG_BUFFER (epoch i+1)
38  *       ....TCQ_PLUG_RELEASE_ONE (epoch i)
39  *       ......TCQ_PLUG_BUFFER (epoch i+2)
40  *       ........
41  *
42  *
43  * State of the queue, when used for network output buffering:
44  *
45  *                 plug(i+1)            plug(i)          head
46  * ------------------+--------------------+---------------->
47  *                   |                    |
48  *                   |                    |
49  * pkts_current_epoch| pkts_last_epoch    |pkts_to_release
50  * ----------------->|<--------+--------->|+--------------->
51  *                   v                    v
52  *
53  *
54  * @{
55  */
56 
57 #include "nl-default.h"
58 
59 #include <netlink/netlink.h>
60 #include <netlink/utils.h>
61 #include <netlink/route/qdisc/plug.h>
62 
63 #include "tc-api.h"
64 
65 struct rtnl_plug {
66 	int action;
67 	uint32_t limit;
68 };
69 
plug_msg_fill(struct rtnl_tc * tc,void * data,struct nl_msg * msg)70 static int plug_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
71 {
72 	struct rtnl_plug *plug = data;
73 	struct tc_plug_qopt opts;
74 
75 	if (!plug)
76 		return -NLE_INVAL;
77 
78 	opts.action = plug->action;
79 	opts.limit  = plug->limit;
80 
81 	return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
82 }
83 
84 /**
85  * @name Attribute Modification
86  * @{
87  */
88 
89 /**
90  * Insert a plug into the qdisc and buffer any incoming
91  * network traffic.
92  * @arg qdisc		PLUG qdisc to be modified.
93  */
rtnl_qdisc_plug_buffer(struct rtnl_qdisc * qdisc)94 int rtnl_qdisc_plug_buffer(struct rtnl_qdisc *qdisc)
95 {
96 	struct rtnl_plug *plug;
97 
98 	if (!(plug = rtnl_tc_data(TC_CAST(qdisc))))
99 		return -NLE_NOMEM;
100 
101 	plug->action = TCQ_PLUG_BUFFER;
102 	return 0;
103 }
104 
105 /**
106  * Unplug the qdisc, releasing packets from queue head
107  * to the last complete buffer, while new traffic
108  * continues to be buffered.
109  * @arg qdisc		PLUG qdisc to be modified.
110  */
rtnl_qdisc_plug_release_one(struct rtnl_qdisc * qdisc)111 int rtnl_qdisc_plug_release_one(struct rtnl_qdisc *qdisc)
112 {
113 	struct rtnl_plug *plug;
114 
115 	if (!(plug = rtnl_tc_data(TC_CAST(qdisc))))
116 		return -NLE_NOMEM;
117 
118 	plug->action = TCQ_PLUG_RELEASE_ONE;
119 	return 0;
120 }
121 
122 /**
123  * Indefinitely unplug the qdisc, releasing all packets.
124  * Network traffic will not be buffered until the next
125  * buffer command is issued.
126  * @arg qdisc		PLUG qdisc to be modified.
127  */
rtnl_qdisc_plug_release_indefinite(struct rtnl_qdisc * qdisc)128 int rtnl_qdisc_plug_release_indefinite(struct rtnl_qdisc *qdisc)
129 {
130 	struct rtnl_plug *plug;
131 
132 	if (!(plug = rtnl_tc_data(TC_CAST(qdisc))))
133 		return -NLE_NOMEM;
134 
135 	plug->action = TCQ_PLUG_RELEASE_INDEFINITE;
136 	return 0;
137 }
138 
139 /**
140  * Set limit of PLUG qdisc.
141  * @arg qdisc		PLUG qdisc to be modified.
142  * @arg limit		New limit.
143  * @return 0 on success or a negative error code.
144  */
rtnl_qdisc_plug_set_limit(struct rtnl_qdisc * qdisc,int limit)145 int rtnl_qdisc_plug_set_limit(struct rtnl_qdisc *qdisc, int limit)
146 {
147 	struct rtnl_plug *plug;
148 
149 	if (!(plug = rtnl_tc_data(TC_CAST(qdisc))))
150 		return -NLE_NOMEM;
151 
152 	plug->action = TCQ_PLUG_LIMIT;
153 	plug->limit  = limit;
154 
155 	return 0;
156 }
157 
158 /** @} */
159 
160 static struct rtnl_tc_ops plug_ops = {
161 	.to_kind		= "plug",
162 	.to_type		= RTNL_TC_TYPE_QDISC,
163 	.to_size		= sizeof(struct rtnl_plug),
164 	.to_msg_fill		= plug_msg_fill,
165 };
166 
plug_init(void)167 static void _nl_init plug_init(void)
168 {
169 	rtnl_tc_register(&plug_ops);
170 }
171 
plug_exit(void)172 static void _nl_exit plug_exit(void)
173 {
174 	rtnl_tc_unregister(&plug_ops);
175 }
176 
177 /** @} */
178