2 * lib/route/class.c Queueing Classes
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 class Classes
18 #include <netlink-local.h>
19 #include <netlink-tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/route/tc.h>
22 #include <netlink/route/class.h>
23 #include <netlink/route/class-modules.h>
24 #include <netlink/route/qdisc.h>
25 #include <netlink/route/classifier.h>
26 #include <netlink/utils.h>
29 static struct nl_cache_ops rtnl_class_ops;
32 static struct rtnl_class_ops *class_ops_list;
34 static struct rtnl_class_ops *class_lookup_ops(const char *kind)
36 struct rtnl_class_ops *ops;
38 for (ops = class_ops_list; ops; ops = ops->co_next)
39 if (!strcmp(kind, ops->co_kind))
45 static inline struct rtnl_class_ops *class_ops(struct rtnl_class *class)
48 class->c_ops = class_lookup_ops(class->c_kind);
54 * @name Class Module API
59 * Register a class module
60 * @arg ops class module operations
62 int rtnl_class_register(struct rtnl_class_ops *ops)
64 struct rtnl_class_ops *o, **op;
69 for (op = &class_ops_list; (o = *op) != NULL; op = &o->co_next)
70 if (!strcasecmp(ops->co_kind, o->co_kind))
71 return nl_errno(EEXIST);
80 * Unregister a class module
81 * @arg ops class module operations
83 int rtnl_class_unregister(struct rtnl_class_ops *ops)
85 struct rtnl_class_ops *o, **op;
87 for (op = &class_ops_list; (o = *op) != NULL; op = &o->co_next)
88 if (!strcasecmp(ops->co_kind, o->co_kind))
92 return nl_errno(ENOENT);
101 static int class_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
105 struct nl_parser_param *pp = arg;
106 struct rtnl_class *class;
107 struct rtnl_class_ops *ops;
109 class = rtnl_class_alloc();
111 err = nl_errno(ENOMEM);
114 class->ce_msgtype = n->nlmsg_type;
116 err = tca_msg_parser(n, (struct rtnl_tca *) class);
120 ops = class_ops(class);
121 if (ops && ops->co_msg_parser) {
122 err = ops->co_msg_parser(class);
127 err = pp->pp_cb((struct nl_object *) class, pp);
134 rtnl_class_put(class);
139 static int class_request_update(struct nl_cache *cache,
140 struct nl_handle *handle)
142 struct tcmsg tchdr = {
143 .tcm_family = AF_UNSPEC,
144 .tcm_ifindex = cache->c_iarg1,
147 return nl_send_simple(handle, RTM_GETTCLASS, NLM_F_DUMP, &tchdr,
151 static void class_free_data(struct nl_object *obj)
153 struct rtnl_class *class = (struct rtnl_class *) obj;
154 struct rtnl_class_ops *ops;
156 tca_free_data((struct rtnl_tca *) class);
158 ops = class_ops(class);
159 if (ops && ops->co_free_data)
160 ops->co_free_data(class);
163 static int class_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
165 struct rtnl_class *class = (struct rtnl_class *) obj;
166 struct rtnl_class_ops *ops;
168 int line = tca_dump_brief((struct rtnl_tca *) class, "class", p, 0);
170 ops = class_ops(class);
171 if (ops && ops->co_dump[NL_DUMP_BRIEF])
172 line = ops->co_dump[NL_DUMP_BRIEF](class, p, line);
178 static int class_dump_full(struct nl_object *obj, struct nl_dump_params *p)
180 struct rtnl_class *class = (struct rtnl_class *) obj;
181 struct rtnl_class_ops *ops;
184 line = class_dump_brief(obj, p);
185 line = tca_dump_full((struct rtnl_tca *) class, p, line);
189 dp_dump(p, "child-qdisc %s ",
190 rtnl_tc_handle2str(class->c_info, buf, sizeof(buf)));
193 ops = class_ops(class);
194 if (ops && ops->co_dump[NL_DUMP_FULL])
195 line = ops->co_dump[NL_DUMP_FULL](class, p, line);
196 else if (!class->c_info)
197 dp_dump(p, "noop (no leaf qdisc)");
204 static int class_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
206 struct rtnl_class *class = (struct rtnl_class *) obj;
207 struct rtnl_class_ops *ops;
210 line = class_dump_full(obj, p);
211 line = tca_dump_stats((struct rtnl_tca *) class, p, line);
214 ops = class_ops(class);
215 if (ops && ops->co_dump[NL_DUMP_STATS])
216 line = ops->co_dump[NL_DUMP_STATS](class, p, line);
221 static int class_filter(struct nl_object *obj, struct nl_object *filter)
223 return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
227 * @name Class Addition/Modification
231 static struct nl_msg *class_build(struct rtnl_class *class, int type, int flags)
233 struct rtnl_class_ops *ops;
237 msg = tca_build_msg((struct rtnl_tca *) class, type, flags);
241 ops = class_ops(class);
242 if (ops && ops->co_get_opts) {
245 opts = ops->co_get_opts(class);
247 err = nla_put_nested(msg, TCA_OPTIONS, opts);
261 * Build a netlink message to add a new class
262 * @arg class class to add
263 * @arg flags additional netlink message flags
265 * Builds a new netlink message requesting an addition of a class.
266 * The netlink message header isn't fully equipped with all relevant
267 * fields and must be sent out via nl_send_auto_complete() or
268 * supplemented as needed.
270 * Common message flags
271 * - NLM_F_REPLACE - replace possibly existing classes
273 * @return New netlink message
275 struct nl_msg *rtnl_class_build_add_request(struct rtnl_class *class, int flags)
277 return class_build(class, RTM_NEWTCLASS, NLM_F_CREATE | flags);
282 * @arg handle netlink handle
283 * @arg class class to delete
284 * @arg flags additional netlink message flags
286 * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
287 * sends the request to the kernel and waits for the next ACK to be
288 * received and thus blocks until the request has been processed.
290 * Common message flags
291 * - NLM_F_REPLACE - replace possibly existing classes
293 * @return 0 on success or a negative error code
295 int rtnl_class_add(struct nl_handle *handle, struct rtnl_class *class,
301 msg = rtnl_class_build_add_request(class, flags);
303 return nl_errno(ENOMEM);
305 err = nl_send_auto_complete(handle, msg);
310 return nl_wait_for_ack(handle);
321 * Allocate a new class object
322 * @return New class object
324 struct rtnl_class *rtnl_class_alloc(void)
326 return (struct rtnl_class *) nl_object_alloc_from_ops(&rtnl_class_ops);
330 * Give back reference on rclass object.
331 * @arg class Class object to be given back.
333 * Decrements the reference counter and frees the object if the
334 * last reference has been released.
336 void rtnl_class_put(struct rtnl_class *class)
338 nl_object_put((struct nl_object *) class);
343 * @arg class Class object to be freed.
345 * @note Always use rtnl_class_put() unless you're absolutely sure
346 * that no other user may have a reference on this object.
348 void rtnl_class_free(struct rtnl_class *class)
350 nl_object_free((struct nl_object *) class);
354 * Build a class cache including all classes attached to the specified interface
355 * @arg handle netlink handle
356 * @arg ifindex interface index of the link the classes are
359 * Allocates a new cache, initializes it properly and updates it to
360 * include all classes attached to the specified interface.
362 * @note The caller is responsible for destroying and freeing the
363 * cache after using it.
364 * @return The cache or NULL if an error has occured.
366 struct nl_cache * rtnl_class_alloc_cache(struct nl_handle *handle, int ifindex)
368 struct nl_cache * cache;
370 cache = nl_cache_alloc_from_ops(&rtnl_class_ops);
374 cache->c_iarg1 = ifindex;
376 if (nl_cache_update(handle, cache) < 0) {
377 nl_cache_free(cache);
387 * @name Leaf Qdisc Access
392 * Determine if the class has a leaf qdisc attached
393 * @arg class class to check
395 int rtnl_class_has_leaf_qdisc(struct rtnl_class *class)
397 return !!class->c_info;
401 * Lookup the leaf qdisc of a class
402 * @arg class the parent class
403 * @arg cache a qdisc cache including at laest all qdiscs of the
404 * interface the specified class is attached to
405 * @return The qdisc from the cache or NULL if the class has no leaf qdisc
407 struct rtnl_qdisc *rtnl_class_leaf_qdisc(struct rtnl_class *class,
408 struct nl_cache *cache)
410 struct rtnl_qdisc *leaf;
412 if (!rtnl_class_has_leaf_qdisc(class))
415 leaf = rtnl_qdisc_get_by_parent(cache, class->c_ifindex,
417 if (!leaf || leaf->q_handle != class->c_info)
431 * Call a callback for each child class of a class
432 * @arg class the parent class
433 * @arg cache a class cache including all classes of the interface
434 * the specified class is attached to
435 * @arg cb callback function
436 * @arg arg argument to be passed to callback function
438 void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
439 void (*cb)(struct nl_object *, void *), void *arg)
441 struct rtnl_class *filter;
443 filter = rtnl_class_alloc();
447 rtnl_class_set_parent(filter, class->c_handle);
448 rtnl_class_set_ifindex(filter, class->c_ifindex);
449 rtnl_class_set_kind(filter, class->c_kind);
451 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
452 rtnl_class_put(filter);
456 * Call a callback for each classifier attached to the class
457 * @arg class the parent class
458 * @arg cache a filter cache including at least all the filters
459 * attached to the specified class
460 * @arg cb callback function
461 * @arg arg argument to be passed to callback function
463 void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
464 void (*cb)(struct nl_object *, void *), void *arg)
466 struct rtnl_cls *filter;
468 filter = rtnl_cls_alloc();
472 rtnl_cls_set_ifindex(filter, class->c_ifindex);
473 rtnl_cls_set_parent(filter, class->c_parent);
475 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
476 rtnl_cls_put(filter);
482 * @name Attribute Modifications
487 * Set the interface index of a class to the specified value
488 * @arg class class to be changed
489 * @arg ifindex new interface index
491 void rtnl_class_set_ifindex(struct rtnl_class *class, int ifindex)
493 tca_set_ifindex((struct rtnl_tca *) class, ifindex);
497 * Get the interface index of a class
498 * @arg class class handle
499 * @return Interface index or RTNL_LINK_NOT_FOUND if not set
501 int rtnl_class_get_ifindex(struct rtnl_class *class)
503 return tca_get_ifindex((struct rtnl_tca *) class);
508 * Set the handle of a class to the specified value
509 * @arg class class to be changed
510 * @arg handle new handle
512 void rtnl_class_set_handle(struct rtnl_class *class, uint32_t handle)
514 tca_set_handle((struct rtnl_tca *) class, handle);
518 * Get the handle of a class
519 * @arg class class handle
520 * @return Handle or 0 if not set
522 uint32_t rtnl_class_get_handle(struct rtnl_class *class)
524 return tca_get_handle((struct rtnl_tca *) class);
528 * Set the parent handle of a class to the specified value
529 * @arg class class to be changed
530 * @arg parent new parent handle
532 void rtnl_class_set_parent(struct rtnl_class *class, uint32_t parent)
534 tca_set_parent((struct rtnl_tca *) class, parent);
538 * Get the parent handle of a class
539 * @arg class class handle
540 * @return Parent handle or 0 if not set
542 uint32_t rtnl_class_get_parent(struct rtnl_class *class)
544 return tca_get_parent((struct rtnl_tca *) class);
548 * Set the kind of a class to the specified value
549 * @arg class class to be changed
550 * @arg name new kind name
552 void rtnl_class_set_kind(struct rtnl_class *class, const char *name)
554 tca_set_kind((struct rtnl_tca *) class, name);
558 * Get the kind of a class
559 * @arg class class handle
560 * @return Kind or NULL if not set
562 char *rtnl_class_get_kind(struct rtnl_class *class)
564 return tca_get_kind((struct rtnl_tca *) class);
568 * Get the statistic specified by the id
569 * @arg class class handle
570 * @arg id statistic id
571 * @return The current counter of the specified statistic
573 uint64_t rtnl_class_get_stat(struct rtnl_class *class,
574 enum rtnl_tc_stats_id id)
576 return tca_get_stat((struct rtnl_tca *) class, id);
581 static struct nl_cache_ops rtnl_class_ops = {
582 .co_name = "route/class",
583 .co_size = sizeof(struct rtnl_class),
584 .co_hdrsize = sizeof(struct tcmsg),
586 { RTM_NEWTCLASS, "new" },
587 { RTM_DELTCLASS, "del" },
588 { RTM_GETTCLASS, "get" },
591 .co_protocol = NETLINK_ROUTE,
592 .co_request_update = &class_request_update,
593 .co_msg_parser = &class_msg_parser,
594 .co_free_data = &class_free_data,
595 .co_dump[NL_DUMP_BRIEF] = &class_dump_brief,
596 .co_dump[NL_DUMP_FULL] = &class_dump_full,
597 .co_dump[NL_DUMP_STATS] = &class_dump_stats,
598 .co_filter = &class_filter,
601 static void __init class_init(void)
603 nl_cache_mngt_register(&rtnl_class_ops);
606 static void __exit class_exit(void)
608 nl_cache_mngt_unregister(&rtnl_class_ops);