libnl 3.7.0
sfq.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup qdisc
8 * @defgroup qdisc_sfq Stochastic Fairness Queueing (SFQ)
9 * @brief
10 *
11 * @par Parameter Description
12 * - \b Quantum: Number of bytes to send out per slot and round.
13 * - \b Perturbation: Timer period between changing the hash function.
14 * - \b Limit: Upper limit of queue in number of packets before SFQ starts
15 * dropping packets.
16 * - \b Divisor: Hash table divisor, i.e. size of hash table.
17 * @{
18 */
19
20#include <netlink-private/netlink.h>
21#include <netlink-private/tc.h>
22#include <netlink/netlink.h>
23#include <netlink/utils.h>
24#include <netlink-private/route/tc-api.h>
25#include <netlink/route/qdisc.h>
26#include <netlink/route/qdisc/sfq.h>
27
28/** @cond SKIP */
29#define SCH_SFQ_ATTR_QUANTUM 0x01
30#define SCH_SFQ_ATTR_PERTURB 0x02
31#define SCH_SFQ_ATTR_LIMIT 0x04
32#define SCH_SFQ_ATTR_DIVISOR 0x08
33#define SCH_SFQ_ATTR_FLOWS 0x10
34/** @endcond */
35
36static int sfq_msg_parser(struct rtnl_tc *tc, void *data)
37{
38 struct rtnl_sfq *sfq = data;
39 struct tc_sfq_qopt *opts;
40
41 if (!(tc->ce_mask & TCA_ATTR_OPTS))
42 return 0;
43
44 if (tc->tc_opts->d_size < sizeof(*opts))
45 return -NLE_INVAL;
46
47 opts = (struct tc_sfq_qopt *) tc->tc_opts->d_data;
48
49 sfq->qs_quantum = opts->quantum;
50 sfq->qs_perturb = opts->perturb_period;
51 sfq->qs_limit = opts->limit;
52 sfq->qs_divisor = opts->divisor;
53 sfq->qs_flows = opts->flows;
54
55 sfq->qs_mask = (SCH_SFQ_ATTR_QUANTUM | SCH_SFQ_ATTR_PERTURB |
56 SCH_SFQ_ATTR_LIMIT | SCH_SFQ_ATTR_DIVISOR |
57 SCH_SFQ_ATTR_FLOWS);
58
59 return 0;
60}
61
62static void sfq_dump_line(struct rtnl_tc *tc, void *data,
63 struct nl_dump_params *p)
64{
65 struct rtnl_sfq *sfq = data;
66
67 if (sfq)
68 nl_dump(p, " quantum %u perturb %us", sfq->qs_quantum,
69 sfq->qs_perturb);
70}
71
72static void sfq_dump_details(struct rtnl_tc *tc, void *data,
73 struct nl_dump_params *p)
74{
75 struct rtnl_sfq *sfq = data;
76
77 if (sfq)
78 nl_dump(p, "limit %u divisor %u",
79 sfq->qs_limit, sfq->qs_divisor);
80}
81
82static int sfq_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
83{
84 struct rtnl_sfq *sfq = data;
85 struct tc_sfq_qopt opts = {0};
86
87 if (!sfq)
88 BUG();
89
90 opts.quantum = sfq->qs_quantum;
91 opts.perturb_period = sfq->qs_perturb;
92 opts.limit = sfq->qs_limit;
93
94 return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
95}
96
97/**
98 * @name Attribute Access
99 * @{
100 */
101
102/**
103 * Set quantum of SFQ qdisc.
104 * @arg qdisc SFQ qdisc to be modified.
105 * @arg quantum New quantum in bytes.
106 * @return 0 on success or a negative error code.
107 */
108void rtnl_sfq_set_quantum(struct rtnl_qdisc *qdisc, int quantum)
109{
110 struct rtnl_sfq *sfq;
111
112 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
113 BUG();
114
115 sfq->qs_quantum = quantum;
116 sfq->qs_mask |= SCH_SFQ_ATTR_QUANTUM;
117}
118
119/**
120 * Get quantum of SFQ qdisc.
121 * @arg qdisc SFQ qdisc.
122 * @return Quantum in bytes or a negative error code.
123 */
124int rtnl_sfq_get_quantum(struct rtnl_qdisc *qdisc)
125{
126 struct rtnl_sfq *sfq;
127
128 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
129 BUG();
130
131 if (sfq->qs_mask & SCH_SFQ_ATTR_QUANTUM)
132 return sfq->qs_quantum;
133 else
134 return -NLE_NOATTR;
135}
136
137/**
138 * Set limit of SFQ qdisc.
139 * @arg qdisc SFQ qdisc to be modified.
140 * @arg limit New limit in number of packets.
141 * @return 0 on success or a negative error code.
142 */
143void rtnl_sfq_set_limit(struct rtnl_qdisc *qdisc, int limit)
144{
145 struct rtnl_sfq *sfq;
146
147 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
148 BUG();
149
150 sfq->qs_limit = limit;
151 sfq->qs_mask |= SCH_SFQ_ATTR_LIMIT;
152}
153
154/**
155 * Get limit of SFQ qdisc.
156 * @arg qdisc SFQ qdisc.
157 * @return Limit or a negative error code.
158 */
159int rtnl_sfq_get_limit(struct rtnl_qdisc *qdisc)
160{
161 struct rtnl_sfq *sfq;
162
163 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
164 BUG();
165
166 if (sfq->qs_mask & SCH_SFQ_ATTR_LIMIT)
167 return sfq->qs_limit;
168 else
169 return -NLE_NOATTR;
170}
171
172/**
173 * Set perturbation interval of SFQ qdisc.
174 * @arg qdisc SFQ qdisc to be modified.
175 * @arg perturb New perturbation interval in seconds.
176 * @note A value of 0 disables perturbation altogether.
177 * @return 0 on success or a negative error code.
178 */
179void rtnl_sfq_set_perturb(struct rtnl_qdisc *qdisc, int perturb)
180{
181 struct rtnl_sfq *sfq;
182
183 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
184 BUG();
185
186 sfq->qs_perturb = perturb;
187 sfq->qs_mask |= SCH_SFQ_ATTR_PERTURB;
188}
189
190/**
191 * Get perturbation interval of SFQ qdisc.
192 * @arg qdisc SFQ qdisc.
193 * @return Perturbation interval in seconds or a negative error code.
194 */
195int rtnl_sfq_get_perturb(struct rtnl_qdisc *qdisc)
196{
197 struct rtnl_sfq *sfq;
198
199 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
200 BUG();
201
202 if (sfq->qs_mask & SCH_SFQ_ATTR_PERTURB)
203 return sfq->qs_perturb;
204 else
205 return -NLE_NOATTR;
206}
207
208/**
209 * Get divisor of SFQ qdisc.
210 * @arg qdisc SFQ qdisc.
211 * @return Divisor in number of entries or a negative error code.
212 */
213int rtnl_sfq_get_divisor(struct rtnl_qdisc *qdisc)
214{
215 struct rtnl_sfq *sfq;
216
217 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
218 BUG();
219
220 if (sfq->qs_mask & SCH_SFQ_ATTR_DIVISOR)
221 return sfq->qs_divisor;
222 else
223 return -NLE_NOATTR;
224}
225
226/** @} */
227
228static struct rtnl_tc_ops sfq_ops = {
229 .to_kind = "sfq",
230 .to_type = RTNL_TC_TYPE_QDISC,
231 .to_size = sizeof(struct rtnl_sfq),
232 .to_msg_parser = sfq_msg_parser,
233 .to_dump = {
234 [NL_DUMP_LINE] = sfq_dump_line,
235 [NL_DUMP_DETAILS] = sfq_dump_details,
236 },
237 .to_msg_fill = sfq_msg_fill,
238};
239
240static void __init sfq_init(void)
241{
242 rtnl_tc_register(&sfq_ops);
243}
244
245static void __exit sfq_exit(void)
246{
247 rtnl_tc_unregister(&sfq_ops);
248}
249
250/** @} */
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:442
int rtnl_sfq_get_quantum(struct rtnl_qdisc *qdisc)
Get quantum of SFQ qdisc.
Definition: sfq.c:124
void rtnl_sfq_set_perturb(struct rtnl_qdisc *qdisc, int perturb)
Set perturbation interval of SFQ qdisc.
Definition: sfq.c:179
int rtnl_sfq_get_divisor(struct rtnl_qdisc *qdisc)
Get divisor of SFQ qdisc.
Definition: sfq.c:213
int rtnl_sfq_get_perturb(struct rtnl_qdisc *qdisc)
Get perturbation interval of SFQ qdisc.
Definition: sfq.c:195
void rtnl_sfq_set_quantum(struct rtnl_qdisc *qdisc, int quantum)
Set quantum of SFQ qdisc.
Definition: sfq.c:108
int rtnl_sfq_get_limit(struct rtnl_qdisc *qdisc)
Get limit of SFQ qdisc.
Definition: sfq.c:159
void rtnl_sfq_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of SFQ qdisc.
Definition: sfq.c:143
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1076
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1015
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1049
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28