1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
4 */
5
6 /**
7 * @ingroup tc
8 * @defgroup qdisc Queueing Disciplines
9 * @{
10 */
11
12 #include <netlink-private/netlink.h>
13 #include <netlink-private/tc.h>
14 #include <netlink/netlink.h>
15 #include <netlink/utils.h>
16 #include <netlink/route/link.h>
17 #include <netlink-private/route/tc-api.h>
18 #include <netlink/route/qdisc.h>
19 #include <netlink/route/class.h>
20 #include <netlink/route/classifier.h>
21
22 static struct nl_cache_ops rtnl_qdisc_ops;
23 static struct nl_object_ops qdisc_obj_ops;
24
qdisc_msg_parser(struct nl_cache_ops * ops,struct sockaddr_nl * who,struct nlmsghdr * n,struct nl_parser_param * pp)25 static int qdisc_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
26 struct nlmsghdr *n, struct nl_parser_param *pp)
27 {
28 struct rtnl_qdisc *qdisc;
29 int err;
30
31 if (!(qdisc = rtnl_qdisc_alloc()))
32 return -NLE_NOMEM;
33
34 if ((err = rtnl_tc_msg_parse(n, TC_CAST(qdisc))) < 0)
35 goto errout;
36
37 err = pp->pp_cb(OBJ_CAST(qdisc), pp);
38 errout:
39 rtnl_qdisc_put(qdisc);
40
41 return err;
42 }
43
qdisc_request_update(struct nl_cache * c,struct nl_sock * sk)44 static int qdisc_request_update(struct nl_cache *c, struct nl_sock *sk)
45 {
46 struct tcmsg tchdr = {
47 .tcm_family = AF_UNSPEC,
48 .tcm_ifindex = c->c_iarg1,
49 };
50
51 return nl_send_simple(sk, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
52 sizeof(tchdr));
53 }
54
55 /**
56 * @name Allocation/Freeing
57 * @{
58 */
59
rtnl_qdisc_alloc(void)60 struct rtnl_qdisc *rtnl_qdisc_alloc(void)
61 {
62 struct rtnl_tc *tc;
63
64 tc = TC_CAST(nl_object_alloc(&qdisc_obj_ops));
65 if (tc)
66 tc->tc_type = RTNL_TC_TYPE_QDISC;
67
68 return (struct rtnl_qdisc *) tc;
69 }
70
rtnl_qdisc_put(struct rtnl_qdisc * qdisc)71 void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
72 {
73 nl_object_put((struct nl_object *) qdisc);
74 }
75
76 /** @} */
77
78 /**
79 * @name Addition / Modification / Deletion
80 * @{
81 */
82
build_qdisc_msg(struct rtnl_qdisc * qdisc,int type,int flags,struct nl_msg ** result)83 static int build_qdisc_msg(struct rtnl_qdisc *qdisc, int type, int flags,
84 struct nl_msg **result)
85 {
86 if (!(qdisc->ce_mask & TCA_ATTR_IFINDEX)) {
87 APPBUG("ifindex must be specified");
88 return -NLE_MISSING_ATTR;
89 }
90
91 return rtnl_tc_msg_build(TC_CAST(qdisc), type, flags, result);
92 }
93
94 /**
95 * Build a netlink message requesting the addition of a qdisc
96 * @arg qdisc Qdisc to add
97 * @arg flags Additional netlink message flags
98 * @arg result Pointer to store resulting netlink message
99 *
100 * The behaviour of this function is identical to rtnl_qdisc_add() with
101 * the exception that it will not send the message but return it int the
102 * provided return pointer instead.
103 *
104 * @see rtnl_qdisc_add()
105 *
106 * @return 0 on success or a negative error code.
107 */
rtnl_qdisc_build_add_request(struct rtnl_qdisc * qdisc,int flags,struct nl_msg ** result)108 int rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc, int flags,
109 struct nl_msg **result)
110 {
111 if (!(qdisc->ce_mask & (TCA_ATTR_HANDLE | TCA_ATTR_PARENT))) {
112 APPBUG("handle or parent must be specified");
113 return -NLE_MISSING_ATTR;
114 }
115
116 return build_qdisc_msg(qdisc, RTM_NEWQDISC, flags, result);
117 }
118
119 /**
120 * Add qdisc
121 * @arg sk Netlink socket
122 * @arg qdisc Qdisc to add
123 * @arg flags Additional netlink message flags
124 *
125 * Builds a \c RTM_NEWQDISC netlink message requesting the addition
126 * of a new qdisc and sends the message to the kernel. The configuration
127 * of the qdisc is derived from the attributes of the specified qdisc.
128 *
129 * The following flags may be specified:
130 * - \c NLM_F_CREATE: Create qdisc if it does not exist, otherwise
131 * -NLE_OBJ_NOTFOUND is returned.
132 * - \c NLM_F_REPLACE: If another qdisc is already attached to the
133 * parent, replace it even if the handles mismatch.
134 * - \c NLM_F_EXCL: Return -NLE_EXISTS if a qdisc with matching
135 * handle exists already.
136 *
137 * Existing qdiscs with matching handles will be updated, unless the
138 * flag \c NLM_F_EXCL is specified. If their handles do not match, the
139 * error -NLE_EXISTS is returned unless the flag \c NLM_F_REPLACE is
140 * specified in which case the existing qdisc is replaced with the new
141 * one. If no matching qdisc exists, it will be created if the flag
142 * \c NLM_F_CREATE is set, otherwise the error -NLE_OBJ_NOTFOUND is
143 * returned.
144 *
145 * After sending, the function will wait for the ACK or an eventual
146 * error message to be received and will therefore block until the
147 * operation has been completed.
148 *
149 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
150 * this function to return immediately after sending. In this case,
151 * it is the responsibility of the caller to handle any error
152 * messages returned.
153 *
154 * @return 0 on success or a negative error code.
155 */
rtnl_qdisc_add(struct nl_sock * sk,struct rtnl_qdisc * qdisc,int flags)156 int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc, int flags)
157 {
158 struct nl_msg *msg;
159 int err;
160
161 if ((err = rtnl_qdisc_build_add_request(qdisc, flags, &msg)) < 0)
162 return err;
163
164 return nl_send_sync(sk, msg);
165 }
166
167 /**
168 * Build netlink message requesting the update of a qdisc
169 * @arg qdisc Qdisc to update
170 * @arg new Qdisc with updated attributes
171 * @arg flags Additional netlink message flags
172 * @arg result Pointer to store resulting netlink message
173 *
174 * The behaviour of this function is identical to rtnl_qdisc_update() with
175 * the exception that it will not send the message but return it in the
176 * provided return pointer instead.
177 *
178 * @see rtnl_qdisc_update()
179 *
180 * @return 0 on success or a negative error code.
181 */
rtnl_qdisc_build_update_request(struct rtnl_qdisc * qdisc,struct rtnl_qdisc * new,int flags,struct nl_msg ** result)182 int rtnl_qdisc_build_update_request(struct rtnl_qdisc *qdisc,
183 struct rtnl_qdisc *new, int flags,
184 struct nl_msg **result)
185 {
186 if (flags & (NLM_F_CREATE | NLM_F_EXCL)) {
187 APPBUG("NLM_F_CREATE and NLM_F_EXCL may not be used here, "
188 "use rtnl_qdisc_add()");
189 return -NLE_INVAL;
190 }
191
192 if (!(qdisc->ce_mask & TCA_ATTR_IFINDEX)) {
193 APPBUG("ifindex must be specified");
194 return -NLE_MISSING_ATTR;
195 }
196
197 if (!(qdisc->ce_mask & (TCA_ATTR_HANDLE | TCA_ATTR_PARENT))) {
198 APPBUG("handle or parent must be specified");
199 return -NLE_MISSING_ATTR;
200 }
201
202 rtnl_tc_set_ifindex(TC_CAST(new), qdisc->q_ifindex);
203
204 if (qdisc->ce_mask & TCA_ATTR_HANDLE)
205 rtnl_tc_set_handle(TC_CAST(new), qdisc->q_handle);
206
207 if (qdisc->ce_mask & TCA_ATTR_PARENT)
208 rtnl_tc_set_parent(TC_CAST(new), qdisc->q_parent);
209
210 return build_qdisc_msg(new, RTM_NEWQDISC, flags, result);
211 }
212
213 /**
214 * Update qdisc
215 * @arg sk Netlink socket
216 * @arg qdisc Qdisc to update
217 * @arg new Qdisc with updated attributes
218 * @arg flags Additional netlink message flags
219 *
220 * Builds a \c RTM_NEWQDISC netlink message requesting the update
221 * of an existing qdisc and sends the message to the kernel.
222 *
223 * This function is a varation of rtnl_qdisc_add() to update qdiscs
224 * if the qdisc to be updated is available as qdisc object. The
225 * behaviour is identical to the one of rtnl_qdisc_add except that
226 * before constructing the message, it copies the \c ifindex,
227 * \c handle, and \c parent from the original \p qdisc to the \p new
228 * qdisc.
229 *
230 * After sending, the function will wait for the ACK or an eventual
231 * error message to be received and will therefore block until the
232 * operation has been completed.
233 *
234 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
235 * this function to return immediately after sending. In this case,
236 * it is the responsibility of the caller to handle any error
237 * messages returned.
238 *
239 * @return 0 on success or a negative error code.
240 */
rtnl_qdisc_update(struct nl_sock * sk,struct rtnl_qdisc * qdisc,struct rtnl_qdisc * new,int flags)241 int rtnl_qdisc_update(struct nl_sock *sk, struct rtnl_qdisc *qdisc,
242 struct rtnl_qdisc *new, int flags)
243 {
244 struct nl_msg *msg;
245 int err;
246
247 err = rtnl_qdisc_build_update_request(qdisc, new, flags, &msg);
248 if (err < 0)
249 return err;
250
251 return nl_send_sync(sk, msg);
252 }
253
254 /**
255 * Build netlink message requesting the deletion of a qdisc
256 * @arg qdisc Qdisc to delete
257 * @arg result Pointer to store resulting netlink message
258 *
259 * The behaviour of this function is identical to rtnl_qdisc_delete() with
260 * the exception that it will not send the message but return it in the
261 * provided return pointer instead.
262 *
263 * @see rtnl_qdisc_delete()
264 *
265 * @return 0 on success or a negative error code.
266 */
rtnl_qdisc_build_delete_request(struct rtnl_qdisc * qdisc,struct nl_msg ** result)267 int rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc,
268 struct nl_msg **result)
269 {
270 struct nl_msg *msg;
271 struct tcmsg tchdr;
272 uint32_t required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
273
274 if ((qdisc->ce_mask & required) != required) {
275 APPBUG("ifindex and parent must be specified");
276 return -NLE_MISSING_ATTR;
277 }
278
279 if (!(msg = nlmsg_alloc_simple(RTM_DELQDISC, 0)))
280 return -NLE_NOMEM;
281
282 memset(&tchdr, 0, sizeof(tchdr));
283
284 tchdr.tcm_family = AF_UNSPEC;
285 tchdr.tcm_ifindex = qdisc->q_ifindex;
286 tchdr.tcm_parent = qdisc->q_parent;
287
288 if (qdisc->ce_mask & TCA_ATTR_HANDLE)
289 tchdr.tcm_handle = qdisc->q_handle;
290
291 if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0)
292 goto nla_put_failure;
293
294 if (qdisc->ce_mask & TCA_ATTR_KIND)
295 NLA_PUT_STRING(msg, TCA_KIND, qdisc->q_kind);
296
297 *result = msg;
298 return 0;
299
300 nla_put_failure:
301 nlmsg_free(msg);
302 return -NLE_MSGSIZE;
303 }
304
305 /**
306 * Delete qdisc
307 * @arg sk Netlink socket
308 * @arg qdisc Qdisc to add
309 *
310 * Builds a \c RTM_NEWQDISC netlink message requesting the deletion
311 * of a qdisc and sends the message to the kernel.
312 *
313 * The message is constructed out of the following attributes:
314 * - \c ifindex and \c parent
315 * - \c handle (optional, must match if provided)
316 * - \c kind (optional, must match if provided)
317 *
318 * All other qdisc attributes including all qdisc type specific
319 * attributes are ignored.
320 *
321 * After sending, the function will wait for the ACK or an eventual
322 * error message to be received and will therefore block until the
323 * operation has been completed.
324 *
325 * @note It is not possible to delete default qdiscs.
326 *
327 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
328 * this function to return immediately after sending. In this case,
329 * it is the responsibility of the caller to handle any error
330 * messages returned.
331 *
332 * @return 0 on success or a negative error code.
333 */
rtnl_qdisc_delete(struct nl_sock * sk,struct rtnl_qdisc * qdisc)334 int rtnl_qdisc_delete(struct nl_sock *sk, struct rtnl_qdisc *qdisc)
335 {
336 struct nl_msg *msg;
337 int err;
338
339 if ((err = rtnl_qdisc_build_delete_request(qdisc, &msg)) < 0)
340 return err;
341
342 return nl_send_sync(sk, msg);
343 }
344
345 /** @} */
346
347 /**
348 * @name Cache Related Functions
349 * @{
350 */
351
352 /**
353 * Allocate a cache and fill it with all configured qdiscs
354 * @arg sk Netlink socket
355 * @arg result Pointer to store the created cache
356 *
357 * Allocates a new qdisc cache and fills it with a list of all configured
358 * qdiscs on all network devices. Release the cache with nl_cache_free().
359 *
360 * @return 0 on success or a negative error code.
361 */
rtnl_qdisc_alloc_cache(struct nl_sock * sk,struct nl_cache ** result)362 int rtnl_qdisc_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
363 {
364 return nl_cache_alloc_and_fill(&rtnl_qdisc_ops, sk, result);
365 }
366
367 /**
368 * Search qdisc by interface index and parent
369 * @arg cache Qdisc cache
370 * @arg ifindex Interface index
371 * @arg parent Handle of parent qdisc
372 *
373 * Searches a qdisc cache previously allocated with rtnl_qdisc_alloc_cache()
374 * and searches for a qdisc matching the interface index and parent qdisc.
375 *
376 * The reference counter is incremented before returning the qdisc, therefore
377 * the reference must be given back with rtnl_qdisc_put() after usage.
378 *
379 * @return pointer to qdisc inside the cache or NULL if no match was found.
380 */
rtnl_qdisc_get_by_parent(struct nl_cache * cache,int ifindex,uint32_t parent)381 struct rtnl_qdisc *rtnl_qdisc_get_by_parent(struct nl_cache *cache,
382 int ifindex, uint32_t parent)
383 {
384 struct rtnl_qdisc *q;
385
386 if (cache->c_ops != &rtnl_qdisc_ops)
387 return NULL;
388
389 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
390 if (q->q_parent == parent && q->q_ifindex == ifindex) {
391 nl_object_get((struct nl_object *) q);
392 return q;
393 }
394 }
395
396 return NULL;
397 }
398
399 /**
400 * Search qdisc by kind
401 * @arg cache Qdisc cache
402 * @arg ifindex Interface index
403 * @arg kind Qdisc kind (tbf, htb, cbq, etc)
404 *
405 * Searches a qdisc cache previously allocated with rtnl_qdisc_alloc_cache()
406 * and searches for a qdisc matching the interface index and kind.
407 *
408 * The reference counter is incremented before returning the qdisc, therefore
409 * the reference must be given back with rtnl_qdisc_put() after usage.
410 *
411 * @return pointer to qdisc inside the cache or NULL if no match was found.
412 */
rtnl_qdisc_get_by_kind(struct nl_cache * cache,int ifindex,char * kind)413 struct rtnl_qdisc *rtnl_qdisc_get_by_kind(struct nl_cache *cache,
414 int ifindex, char *kind)
415 {
416 struct rtnl_qdisc *q;
417
418 if (cache->c_ops != &rtnl_qdisc_ops)
419 return NULL;
420
421 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
422 if ((q->q_ifindex == ifindex) && (!strcmp(q->q_kind, kind))) {
423 nl_object_get((struct nl_object *) q);
424 return q;
425 }
426 }
427
428 return NULL;
429 }
430
431 /**
432 * Search qdisc by interface index and handle
433 * @arg cache Qdisc cache
434 * @arg ifindex Interface index
435 * @arg handle Handle
436 *
437 * Searches a qdisc cache previously allocated with rtnl_qdisc_alloc_cache()
438 * and searches for a qdisc matching the interface index and handle.
439 *
440 * The reference counter is incremented before returning the qdisc, therefore
441 * the reference must be given back with rtnl_qdisc_put() after usage.
442 *
443 * @return Qdisc or NULL if no match was found.
444 */
rtnl_qdisc_get(struct nl_cache * cache,int ifindex,uint32_t handle)445 struct rtnl_qdisc *rtnl_qdisc_get(struct nl_cache *cache, int ifindex,
446 uint32_t handle)
447 {
448 struct rtnl_qdisc *q;
449
450 if (cache->c_ops != &rtnl_qdisc_ops)
451 return NULL;
452
453 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
454 if (q->q_handle == handle && q->q_ifindex == ifindex) {
455 nl_object_get((struct nl_object *) q);
456 return q;
457 }
458 }
459
460 return NULL;
461 }
462
463 /** @} */
464
465 /**
466 * @name Deprecated Functions
467 * @{
468 */
469
470 /**
471 * Call a callback for each child class of a qdisc (deprecated)
472 *
473 * @deprecated Use of this function is deprecated, it does not allow
474 * to handle the out of memory situation that can occur.
475 */
rtnl_qdisc_foreach_child(struct rtnl_qdisc * qdisc,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)476 void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
477 void (*cb)(struct nl_object *, void *), void *arg)
478 {
479 struct rtnl_class *filter;
480
481 filter = rtnl_class_alloc();
482 if (!filter)
483 return;
484
485 rtnl_tc_set_parent(TC_CAST(filter), qdisc->q_handle);
486 rtnl_tc_set_ifindex(TC_CAST(filter), qdisc->q_ifindex);
487 rtnl_tc_set_kind(TC_CAST(filter), qdisc->q_kind);
488
489 nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
490
491 rtnl_class_put(filter);
492 }
493
494 /**
495 * Call a callback for each filter attached to the qdisc (deprecated)
496 *
497 * @deprecated Use of this function is deprecated, it does not allow
498 * to handle the out of memory situation that can occur.
499 */
rtnl_qdisc_foreach_cls(struct rtnl_qdisc * qdisc,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)500 void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
501 void (*cb)(struct nl_object *, void *), void *arg)
502 {
503 struct rtnl_cls *filter;
504
505 if (!(filter = rtnl_cls_alloc()))
506 return;
507
508 rtnl_tc_set_ifindex(TC_CAST(filter), qdisc->q_ifindex);
509 rtnl_tc_set_parent(TC_CAST(filter), qdisc->q_parent);
510
511 nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
512 rtnl_cls_put(filter);
513 }
514
515 /**
516 * Build a netlink message requesting the update of a qdisc
517 *
518 * @deprecated Use of this function is deprecated in favour of
519 * rtnl_qdisc_build_update_request() due to the missing
520 * possibility of specifying additional flags.
521 */
rtnl_qdisc_build_change_request(struct rtnl_qdisc * qdisc,struct rtnl_qdisc * new,struct nl_msg ** result)522 int rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
523 struct rtnl_qdisc *new,
524 struct nl_msg **result)
525 {
526 return rtnl_qdisc_build_update_request(qdisc, new, NLM_F_REPLACE,
527 result);
528 }
529
530 /**
531 * Change attributes of a qdisc
532 *
533 * @deprecated Use of this function is deprecated in favour of
534 * rtnl_qdisc_update() due to the missing possibility of
535 * specifying additional flags.
536 */
rtnl_qdisc_change(struct nl_sock * sk,struct rtnl_qdisc * qdisc,struct rtnl_qdisc * new)537 int rtnl_qdisc_change(struct nl_sock *sk, struct rtnl_qdisc *qdisc,
538 struct rtnl_qdisc *new)
539 {
540 return rtnl_qdisc_update(sk, qdisc, new, NLM_F_REPLACE);
541 }
542
543 /** @} */
544
qdisc_dump_details(struct rtnl_tc * tc,struct nl_dump_params * p)545 static void qdisc_dump_details(struct rtnl_tc *tc, struct nl_dump_params *p)
546 {
547 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) tc;
548
549 nl_dump(p, "refcnt %u", qdisc->q_info);
550 }
551
552 static struct rtnl_tc_type_ops qdisc_ops = {
553 .tt_type = RTNL_TC_TYPE_QDISC,
554 .tt_dump_prefix = "qdisc",
555 .tt_dump = {
556 [NL_DUMP_DETAILS] = qdisc_dump_details,
557 },
558 };
559
560 static struct nl_cache_ops rtnl_qdisc_ops = {
561 .co_name = "route/qdisc",
562 .co_hdrsize = sizeof(struct tcmsg),
563 .co_msgtypes = {
564 { RTM_NEWQDISC, NL_ACT_NEW, "new" },
565 { RTM_DELQDISC, NL_ACT_DEL, "del" },
566 { RTM_GETQDISC, NL_ACT_GET, "get" },
567 END_OF_MSGTYPES_LIST,
568 },
569 .co_protocol = NETLINK_ROUTE,
570 .co_groups = tc_groups,
571 .co_request_update = qdisc_request_update,
572 .co_msg_parser = qdisc_msg_parser,
573 .co_obj_ops = &qdisc_obj_ops,
574 };
575
576 static struct nl_object_ops qdisc_obj_ops = {
577 .oo_name = "route/qdisc",
578 .oo_size = sizeof(struct rtnl_qdisc),
579 .oo_free_data = rtnl_tc_free_data,
580 .oo_clone = rtnl_tc_clone,
581 .oo_dump = {
582 [NL_DUMP_LINE] = rtnl_tc_dump_line,
583 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
584 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
585 },
586 .oo_compare = rtnl_tc_compare,
587 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
588 };
589
qdisc_init(void)590 static void __init qdisc_init(void)
591 {
592 rtnl_tc_type_register(&qdisc_ops);
593 nl_cache_mngt_register(&rtnl_qdisc_ops);
594 }
595
qdisc_exit(void)596 static void __exit qdisc_exit(void)
597 {
598 nl_cache_mngt_unregister(&rtnl_qdisc_ops);
599 rtnl_tc_type_unregister(&qdisc_ops);
600 }
601
602 /** @} */
603