libnl: /data/src/libnl/lib/route/qdisc/sfq.c Source File

libnl 3.12.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 "nl-default.h"
21
22#include <netlink/netlink.h>
23#include <netlink/utils.h>
24#include <netlink/route/qdisc.h>
25#include <netlink/route/qdisc/sfq.h>
26
27#include "tc-api.h"
28
29/** @cond SKIP */
30struct rtnl_sfq {
31 uint32_t qs_quantum;
32 uint32_t qs_perturb;
33 uint32_t qs_limit;
34 uint32_t qs_divisor;
35 uint32_t qs_flows;
36 uint32_t qs_mask;
37};
38
39#define SCH_SFQ_ATTR_QUANTUM 0x01
40#define SCH_SFQ_ATTR_PERTURB 0x02
41#define SCH_SFQ_ATTR_LIMIT 0x04
42#define SCH_SFQ_ATTR_DIVISOR 0x08
43#define SCH_SFQ_ATTR_FLOWS 0x10
44/** @endcond */
45
46static int sfq_msg_parser(struct rtnl_tc *tc, void *data)
47{
48 struct rtnl_sfq *sfq = data;
49 struct tc_sfq_qopt *opts;
50
51 if (!(tc->ce_mask & TCA_ATTR_OPTS))
52 return 0;
53
54 if (tc->tc_opts->d_size < sizeof(*opts))
55 return -NLE_INVAL;
56
57 opts = (struct tc_sfq_qopt *) tc->tc_opts->d_data;
58
59 sfq->qs_quantum = opts->quantum;
60 sfq->qs_perturb = opts->perturb_period;
61 sfq->qs_limit = opts->limit;
62 sfq->qs_divisor = opts->divisor;
63 sfq->qs_flows = opts->flows;
64
65 sfq->qs_mask = (SCH_SFQ_ATTR_QUANTUM | SCH_SFQ_ATTR_PERTURB |
66 SCH_SFQ_ATTR_LIMIT | SCH_SFQ_ATTR_DIVISOR |
67 SCH_SFQ_ATTR_FLOWS);
68
69 return 0;
70}
71
72static void sfq_dump_line(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, " quantum %u perturb %us", sfq->qs_quantum,
79 sfq->qs_perturb);
80}
81
82static void sfq_dump_details(struct rtnl_tc *tc, void *data,
83 struct nl_dump_params *p)
84{
85 struct rtnl_sfq *sfq = data;
86
87 if (sfq)
88 nl_dump(p, "limit %u divisor %u",
89 sfq->qs_limit, sfq->qs_divisor);
90}
91
92static int sfq_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
93{
94 struct rtnl_sfq *sfq = data;
95 struct tc_sfq_qopt opts = {0};
96
97 if (!sfq)
98 BUG();
99
100 opts.quantum = sfq->qs_quantum;
101 opts.perturb_period = sfq->qs_perturb;
102 opts.limit = sfq->qs_limit;
103
104 return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
105}
106
107/**
108 * @name Attribute Access
109 * @{
110 */
111
112/**
113 * Set quantum of SFQ qdisc.
114 * @arg qdisc SFQ qdisc to be modified.
115 * @arg quantum New quantum in bytes.
116 * @return 0 on success or a negative error code.
117 */
118 void rtnl_sfq_set_quantum(struct rtnl_qdisc *qdisc, int quantum)
119{
120 struct rtnl_sfq *sfq;
121
122 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
123 BUG();
124
125 sfq->qs_quantum = quantum;
126 sfq->qs_mask |= SCH_SFQ_ATTR_QUANTUM;
127}
128
129/**
130 * Get quantum of SFQ qdisc.
131 * @arg qdisc SFQ qdisc.
132 * @return Quantum in bytes or a negative error code.
133 */
134 int rtnl_sfq_get_quantum(struct rtnl_qdisc *qdisc)
135{
136 struct rtnl_sfq *sfq;
137
138 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
139 BUG();
140
141 if (sfq->qs_mask & SCH_SFQ_ATTR_QUANTUM)
142 return sfq->qs_quantum;
143 else
144 return -NLE_NOATTR;
145}
146
147/**
148 * Set limit of SFQ qdisc.
149 * @arg qdisc SFQ qdisc to be modified.
150 * @arg limit New limit in number of packets.
151 * @return 0 on success or a negative error code.
152 */
153 void rtnl_sfq_set_limit(struct rtnl_qdisc *qdisc, int limit)
154{
155 struct rtnl_sfq *sfq;
156
157 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
158 BUG();
159
160 sfq->qs_limit = limit;
161 sfq->qs_mask |= SCH_SFQ_ATTR_LIMIT;
162}
163
164/**
165 * Get limit of SFQ qdisc.
166 * @arg qdisc SFQ qdisc.
167 * @return Limit or a negative error code.
168 */
169 int rtnl_sfq_get_limit(struct rtnl_qdisc *qdisc)
170{
171 struct rtnl_sfq *sfq;
172
173 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
174 BUG();
175
176 if (sfq->qs_mask & SCH_SFQ_ATTR_LIMIT)
177 return sfq->qs_limit;
178 else
179 return -NLE_NOATTR;
180}
181
182/**
183 * Set perturbation interval of SFQ qdisc.
184 * @arg qdisc SFQ qdisc to be modified.
185 * @arg perturb New perturbation interval in seconds.
186 * @note A value of 0 disables perturbation altogether.
187 * @return 0 on success or a negative error code.
188 */
189 void rtnl_sfq_set_perturb(struct rtnl_qdisc *qdisc, int perturb)
190{
191 struct rtnl_sfq *sfq;
192
193 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
194 BUG();
195
196 sfq->qs_perturb = perturb;
197 sfq->qs_mask |= SCH_SFQ_ATTR_PERTURB;
198}
199
200/**
201 * Get perturbation interval of SFQ qdisc.
202 * @arg qdisc SFQ qdisc.
203 * @return Perturbation interval in seconds or a negative error code.
204 */
205 int rtnl_sfq_get_perturb(struct rtnl_qdisc *qdisc)
206{
207 struct rtnl_sfq *sfq;
208
209 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
210 BUG();
211
212 if (sfq->qs_mask & SCH_SFQ_ATTR_PERTURB)
213 return sfq->qs_perturb;
214 else
215 return -NLE_NOATTR;
216}
217
218/**
219 * Get divisor of SFQ qdisc.
220 * @arg qdisc SFQ qdisc.
221 * @return Divisor in number of entries or a negative error code.
222 */
223 int rtnl_sfq_get_divisor(struct rtnl_qdisc *qdisc)
224{
225 struct rtnl_sfq *sfq;
226
227 if (!(sfq = rtnl_tc_data(TC_CAST(qdisc))))
228 BUG();
229
230 if (sfq->qs_mask & SCH_SFQ_ATTR_DIVISOR)
231 return sfq->qs_divisor;
232 else
233 return -NLE_NOATTR;
234}
235
236/** @} */
237
238static struct rtnl_tc_ops sfq_ops = {
239 .to_kind = "sfq",
240 .to_type = RTNL_TC_TYPE_QDISC,
241 .to_size = sizeof(struct rtnl_sfq),
242 .to_msg_parser = sfq_msg_parser,
243 .to_dump = {
244 [NL_DUMP_LINE] = sfq_dump_line,
245 [NL_DUMP_DETAILS] = sfq_dump_details,
246 },
247 .to_msg_fill = sfq_msg_fill,
248};
249
250static void _nl_init sfq_init(void)
251{
252 rtnl_tc_register(&sfq_ops);
253}
254
255static void _nl_exit sfq_exit(void)
256{
257 rtnl_tc_unregister(&sfq_ops);
258}
259
260/** @} */
nlmsg_append
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:456
rtnl_sfq_get_quantum
int rtnl_sfq_get_quantum(struct rtnl_qdisc *qdisc)
Get quantum of SFQ qdisc.
Definition sfq.c:134
rtnl_sfq_set_perturb
void rtnl_sfq_set_perturb(struct rtnl_qdisc *qdisc, int perturb)
Set perturbation interval of SFQ qdisc.
Definition sfq.c:189
rtnl_sfq_get_divisor
int rtnl_sfq_get_divisor(struct rtnl_qdisc *qdisc)
Get divisor of SFQ qdisc.
Definition sfq.c:223
rtnl_sfq_get_perturb
int rtnl_sfq_get_perturb(struct rtnl_qdisc *qdisc)
Get perturbation interval of SFQ qdisc.
Definition sfq.c:205
rtnl_sfq_set_quantum
void rtnl_sfq_set_quantum(struct rtnl_qdisc *qdisc, int quantum)
Set quantum of SFQ qdisc.
Definition sfq.c:118
rtnl_sfq_get_limit
int rtnl_sfq_get_limit(struct rtnl_qdisc *qdisc)
Get limit of SFQ qdisc.
Definition sfq.c:169
rtnl_sfq_set_limit
void rtnl_sfq_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of SFQ qdisc.
Definition sfq.c:153
TC_CAST
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition tc.h:50
rtnl_tc_data
void * rtnl_tc_data(struct rtnl_tc *)
Return pointer to private data of traffic control object.
Definition tc.c:1079
rtnl_tc_register
int rtnl_tc_register(struct rtnl_tc_ops *)
Register a traffic control module.
Definition tc.c:1018
rtnl_tc_unregister
void rtnl_tc_unregister(struct rtnl_tc_ops *)
Unregister a traffic control module.
Definition tc.c:1052
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
rtnl_qdisc
Definition nl-route.h:119
rtnl_tc
Definition nl-route.h:115