2 * lib/route/qdisc_obj.c Queueing Discipline Object
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_obj Queueing Discipline Object
18 #include <netlink-local.h>
19 #include <netlink-tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/link.h>
23 #include <netlink/route/tc.h>
24 #include <netlink/route/qdisc.h>
25 #include <netlink/route/class.h>
26 #include <netlink/route/classifier.h>
27 #include <netlink/route/qdisc-modules.h>
29 static void qdisc_free_data(struct nl_object *obj)
31 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
32 struct rtnl_qdisc_ops *qops;
34 tca_free_data((struct rtnl_tca *) qdisc);
36 qops = rtnl_qdisc_lookup_ops(qdisc);
37 if (qops && qops->qo_free_data)
38 qops->qo_free_data(qdisc);
41 static int qdisc_clone(struct nl_object *_dst, struct nl_object *_src)
43 struct rtnl_qdisc *dst = (struct rtnl_qdisc *) _dst;
44 struct rtnl_qdisc *src = (struct rtnl_qdisc *) _src;
45 struct rtnl_qdisc_ops *qops;
48 err = tca_clone((struct rtnl_tca *) dst, (struct rtnl_tca *) src);
52 qops = rtnl_qdisc_lookup_ops(src);
53 if (qops && qops->qo_clone)
54 err = qops->qo_clone(dst, src);
59 static void qdisc_dump_line(struct nl_object *obj, struct nl_dump_params *p)
61 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
62 struct rtnl_qdisc_ops *qops;
64 tca_dump_line((struct rtnl_tca *) qdisc, "qdisc", p);
66 qops = rtnl_qdisc_lookup_ops(qdisc);
67 if (qops && qops->qo_dump[NL_DUMP_LINE])
68 qops->qo_dump[NL_DUMP_LINE](qdisc, p);
73 static void qdisc_dump_details(struct nl_object *arg, struct nl_dump_params *p)
75 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
76 struct rtnl_qdisc_ops *qops;
78 qdisc_dump_line(arg, p);
80 tca_dump_details((struct rtnl_tca *) qdisc, p);
81 nl_dump(p, "refcnt %u ", qdisc->q_info);
83 qops = rtnl_qdisc_lookup_ops(qdisc);
84 if (qops && qops->qo_dump[NL_DUMP_DETAILS])
85 qops->qo_dump[NL_DUMP_DETAILS](qdisc, p);
90 static void qdisc_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
92 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
93 struct rtnl_qdisc_ops *qops;
95 qdisc_dump_details(arg, p);
96 tca_dump_stats((struct rtnl_tca *) qdisc, p);
99 qops = rtnl_qdisc_lookup_ops(qdisc);
100 if (qops && qops->qo_dump[NL_DUMP_STATS])
101 qops->qo_dump[NL_DUMP_STATS](qdisc, p);
105 * @name Allocation/Freeing
109 struct rtnl_qdisc *rtnl_qdisc_alloc(void)
111 return (struct rtnl_qdisc *) nl_object_alloc(&qdisc_obj_ops);
114 void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
116 nl_object_put((struct nl_object *) qdisc);
127 * Call a callback for each child class of a qdisc
128 * @arg qdisc the parent qdisc
129 * @arg cache a class cache including all classes of the interface
130 * the specified qdisc is attached to
131 * @arg cb callback function
132 * @arg arg argument to be passed to callback function
134 void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
135 void (*cb)(struct nl_object *, void *), void *arg)
137 struct rtnl_class *filter;
139 filter = rtnl_class_alloc();
143 rtnl_class_set_parent(filter, qdisc->q_handle);
144 rtnl_class_set_ifindex(filter, qdisc->q_ifindex);
145 rtnl_class_set_kind(filter, qdisc->q_kind);
147 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
149 rtnl_class_put(filter);
153 * Call a callback for each filter attached to the qdisc
154 * @arg qdisc the parent qdisc
155 * @arg cache a filter cache including at least all the filters
156 * attached to the specified qdisc
157 * @arg cb callback function
158 * @arg arg argument to be passed to callback function
160 void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
161 void (*cb)(struct nl_object *, void *), void *arg)
163 struct rtnl_cls *filter;
165 filter = rtnl_cls_alloc();
169 rtnl_cls_set_ifindex(filter, qdisc->q_ifindex);
170 rtnl_cls_set_parent(filter, qdisc->q_parent);
172 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
173 rtnl_cls_put(filter);
183 void rtnl_qdisc_set_ifindex(struct rtnl_qdisc *qdisc, int ifindex)
185 tca_set_ifindex((struct rtnl_tca *) qdisc, ifindex);
188 int rtnl_qdisc_get_ifindex(struct rtnl_qdisc *qdisc)
190 return tca_get_ifindex((struct rtnl_tca *) qdisc);
193 void rtnl_qdisc_set_handle(struct rtnl_qdisc *qdisc, uint32_t handle)
195 tca_set_handle((struct rtnl_tca *) qdisc, handle);
198 uint32_t rtnl_qdisc_get_handle(struct rtnl_qdisc *qdisc)
200 return tca_get_handle((struct rtnl_tca *) qdisc);
203 void rtnl_qdisc_set_parent(struct rtnl_qdisc *qdisc, uint32_t parent)
205 tca_set_parent((struct rtnl_tca *) qdisc, parent);
208 uint32_t rtnl_qdisc_get_parent(struct rtnl_qdisc *qdisc)
210 return tca_get_parent((struct rtnl_tca *) qdisc);
213 void rtnl_qdisc_set_kind(struct rtnl_qdisc *qdisc, const char *name)
215 tca_set_kind((struct rtnl_tca *) qdisc, name);
216 qdisc->q_ops = __rtnl_qdisc_lookup_ops(name);
219 char *rtnl_qdisc_get_kind(struct rtnl_qdisc *qdisc)
221 return tca_get_kind((struct rtnl_tca *) qdisc);
224 uint64_t rtnl_qdisc_get_stat(struct rtnl_qdisc *qdisc,
225 enum rtnl_tc_stats_id id)
227 return tca_get_stat((struct rtnl_tca *) qdisc, id);
233 * @name Qdisc Specific Options
238 * Return qdisc specific options for use in TCA_OPTIONS
239 * @arg qdisc qdisc carrying the optiosn
241 * @return new headerless netlink message carrying the options as payload
243 struct nl_msg *rtnl_qdisc_get_opts(struct rtnl_qdisc *qdisc)
245 struct rtnl_qdisc_ops *ops;
247 ops = rtnl_qdisc_lookup_ops(qdisc);
248 if (ops && ops->qo_get_opts)
249 return ops->qo_get_opts(qdisc);
256 struct nl_object_ops qdisc_obj_ops = {
257 .oo_name = "route/qdisc",
258 .oo_size = sizeof(struct rtnl_qdisc),
259 .oo_free_data = qdisc_free_data,
260 .oo_clone = qdisc_clone,
262 [NL_DUMP_LINE] = qdisc_dump_line,
263 [NL_DUMP_DETAILS] = qdisc_dump_details,
264 [NL_DUMP_STATS] = qdisc_dump_stats,
266 .oo_compare = tca_compare,
267 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),