2 * lib/route/qdisc.c Queueing Disciplines
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup qdisc Queueing Disciplines
17 * In general, qdiscs are identified by the major part of a traffic control
18 * handle (the upper 16 bits). A few special values exist though:
19 * - \c TC_H_ROOT: root qdisc (directly attached to the device)
20 * - \c TC_H_INGRESS: ingress qdisc (directly attached to the device)
21 * - \c TC_H_UNSPEC: unspecified qdisc (no reference)
23 * @par 1) Adding a Qdisc
25 * // Allocate a new empty qdisc to be filled out
26 * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
28 * // ... specify the kind of the Qdisc
29 * rtnl_qdisc_set_kind(qdisc, "pfifo");
31 * // Specify the device the qdisc should be attached to
32 * rtnl_qdisc_set_ifindex(qdisc, ifindex);
34 * // ... specify the parent qdisc
35 * rtnl_qdisc_set_parent(qdisc, TC_H_ROOT);
37 * // Specifying the handle is not required but makes reidentifying easier
38 * // and may help to avoid adding a qdisc twice.
39 * rtnl_qdisc_set_handle(qdisc, 0x000A0000);
41 * // Now on to specify the qdisc specific options, see the relevant qdisc
42 * // modules for documentation, in this example we set the upper limit of
43 * // the packet fifo qdisc to 64
44 * rtnl_qdisc_fifo_set_limit(qdisc, 64);
46 * rtnl_qdisc_add(handle, qdisc, NLM_R_REPLACE);
48 * // Free up the memory
49 * rtnl_qdisc_put(qdisc);
52 * @par 2) Deleting a Qdisc
54 * // Allocate a new empty qdisc to be filled out with the parameters
55 * // specifying the qdisc to be deleted. Alternatively a fully equiped
56 * // Qdisc object from a cache can be used.
57 * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
59 * // The interface index of the device the qdisc is on and the parent handle
60 * // are the least required fields to be filled out.
61 * // Note: Specify TC_H_ROOT or TC_H_INGRESS as parent handle to delete the
62 * // root respectively root ingress qdisc.
63 * rtnl_qdisc_set_ifindex(qdisc, ifindex);
64 * rtnl_qdisc_set_parent(qdisc, parent_handle);
66 * // If required for identification, the handle can be specified as well.
67 * rtnl_qdisc_set_handle(qdisc, qdisc_handle);
69 * // Not required but maybe helpful as sanity check, the kind of the qdisc
70 * // can be specified to avoid mistakes.
71 * rtnl_qdisc_set_kind(qdisc, "pfifo");
73 * // Finally delete the qdisc with rtnl_qdisc_delete(), alternatively
74 * // rtnl_qdisc_build_delete_request() can be invoked to generate an
75 * // appropritate netlink message to send out.
76 * rtnl_qdisc_delete(handle, qdisc);
78 * // Free up the memory
79 * rtnl_qdisc_put(qdisc);
85 #include <netlink-local.h>
86 #include <netlink-tc.h>
87 #include <netlink/netlink.h>
88 #include <netlink/utils.h>
89 #include <netlink/route/link.h>
90 #include <netlink/route/tc.h>
91 #include <netlink/route/qdisc.h>
92 #include <netlink/route/class.h>
93 #include <netlink/route/classifier.h>
94 #include <netlink/route/qdisc-modules.h>
97 static struct nl_cache_ops rtnl_qdisc_ops;
100 static struct rtnl_qdisc_ops *qdisc_ops_list;
102 static struct rtnl_qdisc_ops *qdisc_lookup_ops(const char *kind)
104 struct rtnl_qdisc_ops *ops;
106 for (ops = qdisc_ops_list; ops; ops = ops->qo_next)
107 if (!strcmp(kind, ops->qo_kind))
113 static inline struct rtnl_qdisc_ops *qdisc_ops(struct rtnl_qdisc *qdisc)
116 qdisc->q_ops = qdisc_lookup_ops(qdisc->q_kind);
122 * @name QDisc Module API
127 * Register a qdisc module
128 * @arg ops qdisc module operations
130 int rtnl_qdisc_register(struct rtnl_qdisc_ops *ops)
132 struct rtnl_qdisc_ops *o, **op;
134 if (!ops->qo_kind[0])
137 for (op = &qdisc_ops_list; (o = *op) != NULL; op = &o->qo_next)
138 if (!strcasecmp(ops->qo_kind, o->qo_kind))
139 return nl_errno(EEXIST);
148 * Unregister a qdisc module
149 * @arg ops qdisc module operations
151 int rtnl_qdisc_unregister(struct rtnl_qdisc_ops *ops)
153 struct rtnl_qdisc_ops *o, **op;
155 for (op = &qdisc_ops_list; (o = *op) != NULL; op = &o->qo_next)
156 if (!strcasecmp(ops->qo_kind, o->qo_kind))
160 return nl_errno(ENOENT);
169 static int qdisc_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
173 struct nl_parser_param *pp = arg;
174 struct rtnl_qdisc *qdisc;
175 struct rtnl_qdisc_ops *ops;
177 qdisc = rtnl_qdisc_alloc();
179 err = nl_errno(ENOMEM);
183 qdisc->ce_msgtype = n->nlmsg_type;
185 err = tca_msg_parser(n, (struct rtnl_tca *) qdisc);
189 ops = qdisc_ops(qdisc);
190 if (ops && ops->qo_msg_parser) {
191 err = ops->qo_msg_parser(qdisc);
196 err = pp->pp_cb((struct nl_object *) qdisc, pp);
203 rtnl_qdisc_put(qdisc);
208 static int qdisc_request_update(struct nl_cache *c, struct nl_handle *h)
210 struct tcmsg tchdr = {
211 .tcm_family = AF_UNSPEC,
212 .tcm_ifindex = c->c_iarg1,
215 return nl_send_simple(h, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
219 static void qdisc_free_data(struct nl_object *obj)
221 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
222 struct rtnl_qdisc_ops *ops;
224 tca_free_data((struct rtnl_tca *) qdisc);
226 ops = qdisc_ops(qdisc);
227 if (ops && ops->qo_free_data)
228 ops->qo_free_data(qdisc);
231 static int qdisc_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
233 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
234 struct rtnl_qdisc_ops *ops;
236 int line = tca_dump_brief((struct rtnl_tca *) qdisc, "qdisc", p, 0);
238 ops = qdisc_ops(qdisc);
239 if (ops && ops->qo_dump[NL_DUMP_BRIEF])
240 line = ops->qo_dump[NL_DUMP_BRIEF](qdisc, p, line);
247 static int qdisc_dump_full(struct nl_object *arg, struct nl_dump_params *p)
249 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
250 struct rtnl_qdisc_ops *ops;
252 int line = qdisc_dump_brief(arg, p);
254 line = tca_dump_full((struct rtnl_tca *) qdisc, p, line);
255 dp_dump(p, "refcnt %u ", qdisc->q_info);
257 ops = qdisc_ops(qdisc);
258 if (ops && ops->qo_dump[NL_DUMP_FULL])
259 line = ops->qo_dump[NL_DUMP_FULL](qdisc, p, line);
265 static int qdisc_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
267 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
268 struct rtnl_qdisc_ops *ops;
270 int line = qdisc_dump_full(arg, p);
271 line = tca_dump_stats((struct rtnl_tca *) qdisc, p, line );
274 ops = qdisc_ops(qdisc);
275 if (ops && ops->qo_dump[NL_DUMP_STATS])
276 line = ops->qo_dump[NL_DUMP_STATS](qdisc, p, line);
281 static int qdisc_filter(struct nl_object *obj, struct nl_object *filter)
283 return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
287 * @name QDisc Addition
291 static struct nl_msg *qdisc_build(struct rtnl_qdisc *qdisc, int type, int flags)
293 struct rtnl_qdisc_ops *ops;
297 msg = tca_build_msg((struct rtnl_tca *) qdisc, type, flags);
301 ops = qdisc_ops(qdisc);
302 if (ops && ops->qo_get_opts) {
305 opts = ops->qo_get_opts(qdisc);
307 err = nla_put_nested(msg, TCA_OPTIONS, opts);
322 * Build a netlink message to add a new qdisc
323 * @arg qdisc qdisc to add
324 * @arg flags additional netlink message flags
326 * Builds a new netlink message requesting an addition of a qdisc.
327 * The netlink message header isn't fully equipped with all relevant
328 * fields and must be sent out via nl_send_auto_complete() or
329 * supplemented as needed.
331 * Common message flags used:
332 * - NLM_F_REPLACE - replace a potential existing qdisc
334 * @return New netlink message
336 struct nl_msg *rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc,
341 msg = qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags);
350 * @arg handle netlink handle
351 * @arg qdisc qdisc to delete
352 * @arg flags additional netlink message flags
354 * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
355 * sends the request to the kernel and waits for the ACK to be
356 * received and thus blocks until the request has been processed.
358 * Common message flags used:
359 * - NLM_F_REPLACE - replace a potential existing qdisc
361 * @return 0 on success or a negative error code
363 int rtnl_qdisc_add(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
369 msg = rtnl_qdisc_build_add_request(qdisc, flags);
371 return nl_errno(ENOMEM);
373 err = nl_send_auto_complete(handle, msg);
378 return nl_wait_for_ack(handle);
384 * @name QDisc Modification
389 * Build a netlink message to change attributes of a existing qdisc
390 * @arg qdisc qdisc to change
391 * @arg new new qdisc attributes
393 * Builds a new netlink message requesting an change of qdisc
394 * attributes. The netlink message header isn't fully equipped
395 * with all relevant fields and must be sent out via
396 * nl_send_auto_complete() or supplemented as needed.
398 * @return New netlink message
400 struct nl_msg *rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
401 struct rtnl_qdisc *new)
403 return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE);
407 * Change attributes of a qdisc
408 * @arg handle netlink handle
409 * @arg qdisc qdisc to change
410 * @arg new new qdisc attributes
412 * Builds a netlink message by calling rtnl_qdisc_build_change_request(),
413 * sends the request to the kernel and waits for the ACK to be
414 * received and thus blocks until the request has been processed.
416 * @return 0 on success or a negative error code
418 int rtnl_qdisc_change(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
419 struct rtnl_qdisc *new)
424 msg = rtnl_qdisc_build_change_request(qdisc, new);
426 return nl_errno(ENOMEM);
428 err = nl_send_auto_complete(handle, msg);
433 return nl_wait_for_ack(handle);
439 * @name QDisc Deletion
444 * Build a netlink request message to delete a qdisc
445 * @arg qdisc qdisc to delete
447 * Builds a new netlink message requesting a deletion of a qdisc.
448 * The netlink message header isn't fully equipped with all relevant
449 * fields and must thus be sent out via nl_send_auto_complete()
450 * or supplemented as needed.
452 * @return New netlink message
454 struct nl_msg *rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc)
458 int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
460 if ((qdisc->q_mask & required) != required)
463 msg = nlmsg_build_simple(RTM_DELQDISC, 0);
467 tchdr.tcm_family = AF_UNSPEC,
468 tchdr.tcm_handle = qdisc->q_handle,
469 tchdr.tcm_parent = qdisc->q_parent,
470 tchdr.tcm_ifindex = qdisc->q_ifindex,
471 nlmsg_append(msg, &tchdr, sizeof(tchdr), 1);
478 * @arg handle netlink handle
479 * @arg qdisc qdisc to delete
481 * Builds a netlink message by calling rtnl_qdisc_build_delete_request(),
482 * sends the request to the kernel and waits for the ACK to be
483 * received and thus blocks until the request has been processed.
485 * @return 0 on success or a negative error code
487 int rtnl_qdisc_delete(struct nl_handle *handle, struct rtnl_qdisc *qdisc)
492 msg = rtnl_qdisc_build_delete_request(qdisc);
494 return nl_errno(ENOMEM);
496 err = nl_send_auto_complete(handle, msg);
501 return nl_wait_for_ack(handle);
512 * Allocate a new qdisc object
513 * @return New qdisc object
515 struct rtnl_qdisc *rtnl_qdisc_alloc(void)
517 return (struct rtnl_qdisc *) nl_object_alloc_from_ops(&rtnl_qdisc_ops);
521 * Give back reference on rqdisc object.
522 * @arg qdisc Qdisc object to be given back.
524 * Decrements the reference counter and frees the object if the
525 * last reference has been released.
527 void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
529 nl_object_put((struct nl_object *) qdisc);
533 * @arg qdisc Qdisc object to be freed.
535 * @note Always use rtnl_qdisc_put() unless you're absolutely sure
536 * that no other user may have a reference on this object.
538 void rtnl_qdisc_free(struct rtnl_qdisc *qdisc)
540 nl_object_free((struct nl_object *) qdisc);
546 * @name Qdisc Cache Management
551 * Build a qdisc cache including all qdiscs currently configured in
553 * @arg handle netlink handle
555 * Allocates a new cache, initializes it properly and updates it to
556 * include all qdiscs currently configured in the kernel.
558 * @note The caller is responsible for destroying and freeing the
559 * cache after using it.
560 * @return The cache or NULL if an error has occured.
562 struct nl_cache * rtnl_qdisc_alloc_cache(struct nl_handle *handle)
564 struct nl_cache * cache;
566 cache = nl_cache_alloc_from_ops(&rtnl_qdisc_ops);
570 if (nl_cache_update(handle, cache) < 0) {
571 nl_cache_free(cache);
579 * Look up qdisc by its parent in the provided cache
580 * @arg cache qdisc cache
581 * @arg ifindex interface the qdisc is attached to
582 * @arg parent parent handle
583 * @return pointer to qdisc inside the cache or NULL if no match was found.
585 struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache,
586 int ifindex, uint32_t parent)
588 struct rtnl_qdisc *q;
590 if (cache->c_ops != &rtnl_qdisc_ops)
593 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
594 if (q->q_parent == parent && q->q_ifindex == ifindex) {
595 nl_object_get((struct nl_object *) q);
604 * Look up qdisc by its handle in the provided cache
605 * @arg cache qdisc cache
606 * @arg ifindex interface the qdisc is attached to
607 * @arg handle qdisc handle
608 * @return pointer to qdisc inside the cache or NULL if no match was found.
610 struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache,
611 int ifindex, uint32_t handle)
613 struct rtnl_qdisc *q;
615 if (cache->c_ops != &rtnl_qdisc_ops)
618 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
619 if (q->q_handle == handle && q->q_ifindex == ifindex) {
620 nl_object_get((struct nl_object *) q);
631 * @name Qdisc Specific Options
636 * Return qdisc specific options for use in TCA_OPTIONS
637 * @arg qdisc qdisc carrying the optiosn
639 * @return new headerless netlink message carrying the options as payload
641 struct nl_msg *rtnl_qdisc_get_opts(struct rtnl_qdisc *qdisc)
643 struct rtnl_qdisc_ops *ops;
645 ops = qdisc_ops(qdisc);
646 if (ops && ops->qo_get_opts)
647 return ops->qo_get_opts(qdisc);
660 * Call a callback for each child class of a qdisc
661 * @arg qdisc the parent qdisc
662 * @arg cache a class cache including all classes of the interface
663 * the specified qdisc is attached to
664 * @arg cb callback function
665 * @arg arg argument to be passed to callback function
667 void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
668 void (*cb)(struct nl_object *, void *), void *arg)
670 struct rtnl_class *filter;
672 filter = rtnl_class_alloc();
676 rtnl_class_set_parent(filter, qdisc->q_handle);
677 rtnl_class_set_ifindex(filter, qdisc->q_ifindex);
678 rtnl_class_set_kind(filter, qdisc->q_kind);
680 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
682 rtnl_class_put(filter);
686 * Call a callback for each filter attached to the qdisc
687 * @arg qdisc the parent qdisc
688 * @arg cache a filter cache including at least all the filters
689 * attached to the specified qdisc
690 * @arg cb callback function
691 * @arg arg argument to be passed to callback function
693 void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
694 void (*cb)(struct nl_object *, void *), void *arg)
696 struct rtnl_cls *filter;
698 filter = rtnl_cls_alloc();
702 rtnl_cls_set_ifindex(filter, qdisc->q_ifindex);
703 rtnl_cls_set_parent(filter, qdisc->q_parent);
705 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
706 rtnl_cls_put(filter);
712 * @name Attribute Modification
717 * Set the interface index of a qdisc to the specified value
718 * @arg qdisc qdisc to be changed
719 * @arg ifindex new interface index
721 void rtnl_qdisc_set_ifindex(struct rtnl_qdisc *qdisc, int ifindex)
723 tca_set_ifindex((struct rtnl_tca *) qdisc, ifindex);
727 * Get the interface index of a qdisc
728 * @arg qdisc qdisc handle
729 * @return Interface index or RTNL_LINK_NOT_FOUND if not set
731 int rtnl_qdisc_get_ifindex(struct rtnl_qdisc *qdisc)
733 return tca_get_ifindex((struct rtnl_tca *) qdisc);
737 * Set the handle of a qdisc to the specified value
738 * @arg qdisc qdisc to be changed
739 * @arg handle new handle
741 void rtnl_qdisc_set_handle(struct rtnl_qdisc *qdisc, uint32_t handle)
743 tca_set_handle((struct rtnl_tca *) qdisc, handle);
747 * Get the handle of a qdisc
748 * @arg qdisc qdisc handle
749 * @return Handle or 0 if not set
751 uint32_t rtnl_qdisc_get_handle(struct rtnl_qdisc *qdisc)
753 return tca_get_handle((struct rtnl_tca *) qdisc);
757 * Set the parent handle of a qdisc to the specified value
758 * @arg qdisc qdisc to be changed
759 * @arg parent new parent handle
761 void rtnl_qdisc_set_parent(struct rtnl_qdisc *qdisc, uint32_t parent)
763 tca_set_parent((struct rtnl_tca *) qdisc, parent);
767 * Get the parent handle of a qdisc
768 * @arg qdisc qdisc handle
769 * @return Parent handle or 0 if not set
771 uint32_t rtnl_qdisc_get_parent(struct rtnl_qdisc *qdisc)
773 return tca_get_parent((struct rtnl_tca *) qdisc);
777 * Set the kind of a qdisc to the specified value
778 * @arg qdisc qdisc to be changed
779 * @arg name new kind name
781 void rtnl_qdisc_set_kind(struct rtnl_qdisc *qdisc, const char *name)
783 tca_set_kind((struct rtnl_tca *) qdisc, name);
784 qdisc->q_ops = qdisc_lookup_ops(name);
788 * Get the kind of a qdisc
789 * @arg qdisc qdisc handle
790 * @return Kind or NULL if not set
792 char *rtnl_qdisc_get_kind(struct rtnl_qdisc *qdisc)
794 return tca_get_kind((struct rtnl_tca *) qdisc);
798 * Get the statistic specified by the id
799 * @arg qdisc qdisc handle
800 * @arg id statistic id
801 * @return The current counter of the specified statistic
803 uint64_t rtnl_qdisc_get_stat(struct rtnl_qdisc *qdisc,
804 enum rtnl_tc_stats_id id)
806 return tca_get_stat((struct rtnl_tca *) qdisc, id);
811 static struct nl_cache_ops rtnl_qdisc_ops = {
812 .co_name = "route/qdisc",
813 .co_size = sizeof(struct rtnl_qdisc),
814 .co_hdrsize = sizeof(struct tcmsg),
816 { RTM_NEWQDISC, "new" },
817 { RTM_DELQDISC, "delete" },
818 { RTM_GETQDISC, "get" },
821 .co_protocol = NETLINK_ROUTE,
822 .co_request_update = qdisc_request_update,
823 .co_msg_parser = qdisc_msg_parser,
824 .co_free_data = qdisc_free_data,
825 .co_dump[NL_DUMP_BRIEF] = qdisc_dump_brief,
826 .co_dump[NL_DUMP_FULL] = qdisc_dump_full,
827 .co_dump[NL_DUMP_STATS] = qdisc_dump_stats,
828 .co_filter = qdisc_filter,
831 static void __init qdisc_init(void)
833 nl_cache_mngt_register(&rtnl_qdisc_ops);
836 static void __exit qdisc_exit(void)
838 nl_cache_mngt_unregister(&rtnl_qdisc_ops);