libnl  3.6.0
fq_codel.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
4  */
5 
6 /**
7  * @ingroup qdisc
8  * @defgroup qdisc_fq_codel Fair Queue CoDel
9  * @brief
10  *
11  * @{
12  */
13 
14 #include <netlink-private/netlink.h>
15 #include <netlink-private/tc.h>
16 #include <netlink/netlink.h>
17 #include <netlink-private/route/tc-api.h>
18 #include <netlink/route/qdisc.h>
19 #include <netlink/route/qdisc/fq_codel.h>
20 #include <netlink/utils.h>
21 
22 /** @cond SKIP */
23 #define SCH_FQ_CODEL_ATTR_TARGET 0x1
24 #define SCH_FQ_CODEL_ATTR_LIMIT 0x2
25 #define SCH_FQ_CODEL_ATTR_INTERVAL 0x4
26 #define SCH_FQ_CODEL_ATTR_FLOWS 0x8
27 #define SCH_FQ_CODEL_ATTR_QUANTUM 0x10
28 #define SCH_FQ_CODEL_ATTR_ECN 0x20
29 /** @endcond */
30 
31 static struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
32  [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
33  [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
34  [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
35  [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
36  [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
37  [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
38 };
39 
40 static int fq_codel_msg_parser(struct rtnl_tc *tc, void *data)
41 {
42  struct rtnl_fq_codel *fq_codel = data;
43  struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
44  int err;
45 
46  err = tca_parse(tb, TCA_FQ_CODEL_MAX, tc, fq_codel_policy);
47  if (err < 0)
48  return err;
49 
50  if (tb[TCA_FQ_CODEL_TARGET]) {
51  fq_codel->fq_target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
52  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_TARGET;
53  }
54 
55  if (tb[TCA_FQ_CODEL_INTERVAL]) {
56  fq_codel->fq_interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
57  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_INTERVAL;
58  }
59 
60  if (tb[TCA_FQ_CODEL_LIMIT]) {
61  fq_codel->fq_limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
62  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_LIMIT;
63  }
64 
65  if (tb[TCA_FQ_CODEL_QUANTUM]) {
66  fq_codel->fq_quantum = nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]);
67  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_QUANTUM;
68  }
69 
70  if (tb[TCA_FQ_CODEL_FLOWS]) {
71  fq_codel->fq_flows = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
72  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_FLOWS;
73  }
74 
75  if (tb[TCA_FQ_CODEL_ECN]) {
76  fq_codel->fq_ecn = nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
77  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_ECN;
78  }
79 
80  return 0;
81 }
82 
83 static void fq_codel_dump_line(struct rtnl_tc *tc, void *data,
84  struct nl_dump_params *p)
85 {
86  struct rtnl_fq_codel *fq_codel = data;
87 
88  if (!fq_codel)
89  return;
90 
91  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_LIMIT)
92  nl_dump(p, " limit %u packets", fq_codel->fq_limit);
93  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_TARGET)
94  nl_dump(p, " target %u", fq_codel->fq_target);
95  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_INTERVAL)
96  nl_dump(p, " interval %u", fq_codel->fq_interval);
97  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_ECN)
98  nl_dump(p, " ecn %u", fq_codel->fq_ecn);
99  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_FLOWS)
100  nl_dump(p, " flows %u", fq_codel->fq_flows);
101  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_QUANTUM)
102  nl_dump(p, " quantum %u", fq_codel->fq_quantum);
103 }
104 
105 static int fq_codel_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
106 {
107  struct rtnl_fq_codel *fq_codel = data;
108 
109  if (!fq_codel)
110  return -NLE_INVAL;
111 
112  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_LIMIT)
113  NLA_PUT_U32(msg, TCA_FQ_CODEL_LIMIT, fq_codel->fq_limit);
114  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_INTERVAL)
115  NLA_PUT_U32(msg, TCA_FQ_CODEL_INTERVAL, fq_codel->fq_interval);
116  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_TARGET)
117  NLA_PUT_U32(msg, TCA_FQ_CODEL_TARGET, fq_codel->fq_target);
118  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_QUANTUM)
119  NLA_PUT_U32(msg, TCA_FQ_CODEL_QUANTUM, fq_codel->fq_quantum);
120  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_FLOWS)
121  NLA_PUT_U32(msg, TCA_FQ_CODEL_FLOWS, fq_codel->fq_flows);
122  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_ECN)
123  NLA_PUT_U32(msg, TCA_FQ_CODEL_ECN, fq_codel->fq_ecn);
124  return 0;
125 
126 nla_put_failure:
127  return -NLE_MSGSIZE;
128 
129 }
130 
131 /**
132  * @name Attribute Modification
133  * @{
134  */
135 
136 /**
137  * Set limit of fq_codel qdisc.
138  * @arg qdisc fq_codel qdisc to be modified.
139  * @arg limit New limit.
140  * @return 0 on success or a negative error code.
141  */
142 int rtnl_qdisc_fq_codel_set_limit(struct rtnl_qdisc *qdisc, int limit)
143 {
144  struct rtnl_fq_codel *fq_codel;
145 
146  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
147  return -NLE_NOMEM;
148 
149  fq_codel->fq_limit = limit;
150  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_LIMIT;
151 
152  return 0;
153 }
154 
155 /**
156  * Get limit of a fq_codel qdisc.
157  * @arg qdisc fq_codel qdisc.
158  * @return Numeric limit or a negative error code.
159  */
160 int rtnl_qdisc_fq_codel_get_limit(struct rtnl_qdisc *qdisc)
161 {
162  struct rtnl_fq_codel *fq_codel;
163 
164  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
165  return -NLE_NOMEM;
166 
167  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_LIMIT)
168  return fq_codel->fq_limit;
169  else
170  return -NLE_NOATTR;
171 }
172 
173 /**
174  * Set target of fq_codel qdisc.
175  * @arg qdisc fq_codel qdisc to be modified.
176  * @arg target New target.
177  * @return 0 on success or a negative error code.
178  */
179 int rtnl_qdisc_fq_codel_set_target(struct rtnl_qdisc *qdisc, uint32_t target)
180 {
181  struct rtnl_fq_codel *fq_codel;
182 
183  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
184  return -NLE_NOMEM;
185 
186  fq_codel->fq_target = target;
187  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_TARGET;
188 
189  return 0;
190 }
191 
192 /**
193  * Get target of a fq_codel qdisc.
194  * @arg qdisc fq_codel qdisc.
195  * @return Numeric target or zero.
196  */
197 uint32_t rtnl_qdisc_fq_codel_get_target(struct rtnl_qdisc *qdisc)
198 {
199  struct rtnl_fq_codel *fq_codel;
200 
201  if ((fq_codel = rtnl_tc_data(TC_CAST(qdisc))) &&
202  fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_TARGET)
203  return fq_codel->fq_target;
204  else
205  return 0;
206 }
207 
208 /**
209  * Set interval of fq_codel qdisc.
210  * @arg qdisc fq_codel qdisc to be modified.
211  * @arg interval New interval.
212  * @return 0 on success or a negative error code.
213  */
214 int rtnl_qdisc_fq_codel_set_interval(struct rtnl_qdisc *qdisc, uint32_t interval)
215 {
216  struct rtnl_fq_codel *fq_codel;
217 
218  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
219  return -NLE_NOMEM;
220 
221  fq_codel->fq_interval = interval;
222  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_INTERVAL;
223 
224  return 0;
225 }
226 
227 /**
228  * Get target of a fq_codel qdisc.
229  * @arg qdisc fq_codel qdisc.
230  * @return Numeric interval or zero.
231  */
232 uint32_t rtnl_qdisc_fq_codel_get_interval(struct rtnl_qdisc *qdisc)
233 {
234  struct rtnl_fq_codel *fq_codel;
235 
236  if ((fq_codel = rtnl_tc_data(TC_CAST(qdisc))) &&
237  fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_INTERVAL)
238  return fq_codel->fq_interval;
239  else
240  return 0;
241 }
242 
243 /**
244  * Set quantum of fq_codel qdisc.
245  * @arg qdisc fq_codel qdisc to be modified.
246  * @arg quantum New quantum.
247  * @return 0 on success or a negative error code.
248  */
249 int rtnl_qdisc_fq_codel_set_quantum(struct rtnl_qdisc *qdisc, uint32_t quantum)
250 {
251  struct rtnl_fq_codel *fq_codel;
252 
253  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
254  return -NLE_NOMEM;
255 
256  fq_codel->fq_quantum = quantum;
257  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_QUANTUM;
258 
259  return 0;
260 }
261 
262 /**
263  * Get quantum of a fq_codel qdisc.
264  * @arg qdisc fq_codel qdisc.
265  * @return Numeric quantum or zero.
266  */
267 uint32_t rtnl_qdisc_fq_codel_get_quantum(struct rtnl_qdisc *qdisc)
268 {
269  struct rtnl_fq_codel *fq_codel;
270 
271  if ((fq_codel = rtnl_tc_data(TC_CAST(qdisc))) &&
272  (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_QUANTUM))
273  return fq_codel->fq_quantum;
274  else
275  return 0;
276 }
277 
278 /**
279  * Set flows of fq_codel qdisc.
280  * @arg qdisc fq_codel qdisc to be modified.
281  * @arg flows New flows value.
282  * @return 0 on success or a negative error code.
283  */
284 int rtnl_qdisc_fq_codel_set_flows(struct rtnl_qdisc *qdisc, int flows)
285 {
286  struct rtnl_fq_codel *fq_codel;
287 
288  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
289  return -NLE_NOMEM;
290 
291  fq_codel->fq_flows = flows;
292  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_FLOWS;
293 
294  return 0;
295 }
296 
297 /**
298  * Get flows of a fq_codel qdisc.
299  * @arg qdisc fq_codel qdisc.
300  * @return Numeric flows or a negative error code.
301  */
302 int rtnl_qdisc_fq_codel_get_flows(struct rtnl_qdisc *qdisc)
303 {
304  struct rtnl_fq_codel *fq_codel;
305 
306  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
307  return -NLE_NOMEM;
308 
309  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_FLOWS)
310  return fq_codel->fq_flows;
311  else
312  return -NLE_NOATTR;
313 }
314 /**
315  * Set ecn of fq_codel qdisc.
316  * @arg qdisc fq_codel qdisc to be modified.
317  * @arg ecn New ecn value.
318  * @return 0 on success or a negative error code.
319  */
320 int rtnl_qdisc_fq_codel_set_ecn(struct rtnl_qdisc *qdisc, int ecn)
321 {
322  struct rtnl_fq_codel *fq_codel;
323 
324  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
325  return -NLE_NOMEM;
326 
327  fq_codel->fq_ecn = ecn;
328  fq_codel->fq_mask |= SCH_FQ_CODEL_ATTR_ECN;
329 
330  return 0;
331 }
332 
333 /**
334  * Get ecn of a fq_codel qdisc.
335  * @arg qdisc fq_codel qdisc.
336  * @return Numeric ecn or a negative error code.
337  */
338 int rtnl_qdisc_fq_codel_get_ecn(struct rtnl_qdisc *qdisc)
339 {
340  struct rtnl_fq_codel *fq_codel;
341 
342  if (!(fq_codel = rtnl_tc_data(TC_CAST(qdisc))))
343  return -NLE_NOMEM;
344 
345  if (fq_codel->fq_mask & SCH_FQ_CODEL_ATTR_ECN)
346  return fq_codel->fq_ecn;
347  else
348  return -NLE_NOATTR;
349 }
350 /** @} */
351 
352 static struct rtnl_tc_ops fq_codel_ops = {
353  .to_kind = "fq_codel",
354  .to_type = RTNL_TC_TYPE_QDISC,
355  .to_size = sizeof(struct rtnl_fq_codel),
356  .to_msg_parser = fq_codel_msg_parser,
357  .to_dump[NL_DUMP_LINE] = fq_codel_dump_line,
358  .to_msg_fill = fq_codel_msg_fill,
359 };
360 
361 static void __init fq_codel_init(void)
362 {
363  rtnl_tc_register(&fq_codel_ops);
364 }
365 
366 static void __exit fq_codel_exit(void)
367 {
368  rtnl_tc_unregister(&fq_codel_ops);
369 }
370 
371 /** @} */
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:699
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:230
@ NLA_U32
32 bit integer
Definition: attr.h:37
int rtnl_qdisc_fq_codel_get_ecn(struct rtnl_qdisc *qdisc)
Get ecn of a fq_codel qdisc.
Definition: fq_codel.c:338
int rtnl_qdisc_fq_codel_get_flows(struct rtnl_qdisc *qdisc)
Get flows of a fq_codel qdisc.
Definition: fq_codel.c:302
int rtnl_qdisc_fq_codel_set_target(struct rtnl_qdisc *qdisc, uint32_t target)
Set target of fq_codel qdisc.
Definition: fq_codel.c:179
uint32_t rtnl_qdisc_fq_codel_get_target(struct rtnl_qdisc *qdisc)
Get target of a fq_codel qdisc.
Definition: fq_codel.c:197
int rtnl_qdisc_fq_codel_set_interval(struct rtnl_qdisc *qdisc, uint32_t interval)
Set interval of fq_codel qdisc.
Definition: fq_codel.c:214
int rtnl_qdisc_fq_codel_set_flows(struct rtnl_qdisc *qdisc, int flows)
Set flows of fq_codel qdisc.
Definition: fq_codel.c:284
int rtnl_qdisc_fq_codel_set_quantum(struct rtnl_qdisc *qdisc, uint32_t quantum)
Set quantum of fq_codel qdisc.
Definition: fq_codel.c:249
uint32_t rtnl_qdisc_fq_codel_get_quantum(struct rtnl_qdisc *qdisc)
Get quantum of a fq_codel qdisc.
Definition: fq_codel.c:267
int rtnl_qdisc_fq_codel_set_ecn(struct rtnl_qdisc *qdisc, int ecn)
Set ecn of fq_codel qdisc.
Definition: fq_codel.c:320
int rtnl_qdisc_fq_codel_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of fq_codel qdisc.
Definition: fq_codel.c:142
uint32_t rtnl_qdisc_fq_codel_get_interval(struct rtnl_qdisc *qdisc)
Get target of a fq_codel qdisc.
Definition: fq_codel.c:232
int rtnl_qdisc_fq_codel_get_limit(struct rtnl_qdisc *qdisc)
Get limit of a fq_codel qdisc.
Definition: fq_codel.c:160
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1079
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1018
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1052
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
Dumping parameters.
Definition: types.h:28
Attribute validation policy.
Definition: attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:65