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