libnl 3.9.0
class.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup tc
8 * @defgroup class Traffic Classes
9 * @{
10 */
11
12#include "nl-default.h"
13
14#include <netlink/netlink.h>
15#include <netlink/route/class.h>
16#include <netlink/route/qdisc.h>
17#include <netlink/route/classifier.h>
18#include <netlink/utils.h>
19
20#include "nl-route.h"
21#include "tc-api.h"
22
23struct rtnl_class {
24 NL_TC_GENERIC(c);
25};
26
27static struct nl_cache_ops rtnl_class_ops;
28static struct nl_object_ops class_obj_ops;
29
30static void class_dump_details(struct rtnl_tc *tc, struct nl_dump_params *p)
31{
32 struct rtnl_class *class = (struct rtnl_class *) tc;
33 char buf[32];
34
35 if (class->c_info)
36 nl_dump(p, "child-qdisc %s ",
37 rtnl_tc_handle2str(class->c_info, buf, sizeof(buf)));
38}
39
40
41static int class_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
42 struct nlmsghdr *nlh, struct nl_parser_param *pp)
43{
44 struct rtnl_class *class;
45 int err;
46
47 if (!(class = rtnl_class_alloc()))
48 return -NLE_NOMEM;
49
50 if ((err = rtnl_tc_msg_parse(nlh, TC_CAST(class))) < 0)
51 goto errout;
52
53 err = pp->pp_cb(OBJ_CAST(class), pp);
54errout:
55 rtnl_class_put(class);
56
57 return err;
58}
59
60static int class_request_update(struct nl_cache *cache, struct nl_sock *sk)
61{
62 struct tcmsg tchdr = {
63 .tcm_family = AF_UNSPEC,
64 .tcm_ifindex = cache->c_iarg1,
65 };
66
67 return nl_send_simple(sk, RTM_GETTCLASS, NLM_F_DUMP, &tchdr,
68 sizeof(tchdr));
69}
70
71/**
72 * @name Allocation/Freeing
73 * @{
74 */
75
76struct rtnl_class *rtnl_class_alloc(void)
77{
78 struct rtnl_tc *tc;
79
80 tc = TC_CAST(nl_object_alloc(&class_obj_ops));
81 if (tc)
82 tc->tc_type = RTNL_TC_TYPE_CLASS;
83
84 return (struct rtnl_class *) tc;
85}
86
87void rtnl_class_put(struct rtnl_class *class)
88{
89 nl_object_put((struct nl_object *) class);
90}
91
92/** @} */
93
94
95/**
96 * @name Addition/Modification/Deletion
97 * @{
98 */
99
100static int class_build(struct rtnl_class *class, int type, int flags,
101 struct nl_msg **result)
102{
103 uint32_t needed = TCA_ATTR_PARENT | TCA_ATTR_HANDLE;
104
105 if ((class->ce_mask & needed) == needed &&
106 TC_H_MAJ(class->c_parent) && TC_H_MAJ(class->c_handle) &&
107 TC_H_MAJ(class->c_parent) != TC_H_MAJ(class->c_handle)) {
108 APPBUG("TC_H_MAJ(parent) must match TC_H_MAJ(handle)");
109 return -NLE_INVAL;
110 }
111
112 return rtnl_tc_msg_build(TC_CAST(class), type, flags, result);
113}
114
115/**
116 * Build a netlink message requesting the addition of a traffic class
117 * @arg class Traffic class to add
118 * @arg flags Additional netlink message flags
119 * @arg result Pointer to store resulting netlink message
120 *
121 * The behaviour of this function is identical to rtnl_class_add() with
122 * the exception that it will not send the message but return it int the
123 * provided return pointer instead.
124 *
125 * @see rtnl_class_add()
126 *
127 * @return 0 on success or a negative error code.
128 */
129int rtnl_class_build_add_request(struct rtnl_class *class, int flags,
130 struct nl_msg **result)
131{
132 return class_build(class, RTM_NEWTCLASS, flags, result);
133}
134
135/**
136 * Add/Update traffic class
137 * @arg sk Netlink socket
138 * @arg class Traffic class to add
139 * @arg flags Additional netlink message flags
140 *
141 * Builds a \c RTM_NEWTCLASS netlink message requesting the addition
142 * of a new traffic class and sends the message to the kernel. The
143 * configuration of the traffic class is derived from the attributes
144 * of the specified traffic class.
145 *
146 * The following flags may be specified:
147 * - \c NLM_F_CREATE: Create traffic class if it does not exist,
148 * otherwise -NLE_OBJ_NOTFOUND is returned.
149 * - \c NLM_F_EXCL: Return -NLE_EXISTS if a traffic class with
150 * matching handle exists already.
151 *
152 * Existing traffic classes with matching handles will be updated,
153 * unless the flag \c NLM_F_EXCL is specified. If no matching traffic
154 * class exists, it will be created if the flag \c NLM_F_CREATE is set,
155 * otherwise the error -NLE_OBJ_NOTFOUND is returned.
156 *
157 * If the parent qdisc does not support classes, the error
158 * \c NLE_OPNOTSUPP is returned.
159 *
160 * After sending, the function will wait for the ACK or an eventual
161 * error message to be received and will therefore block until the
162 * operation has been completed.
163 *
164 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
165 * this function to return immediately after sending. In this case,
166 * it is the responsibility of the caller to handle any error
167 * messages returned.
168 *
169 * @return 0 on success or a negative error code.
170 */
171int rtnl_class_add(struct nl_sock *sk, struct rtnl_class *class, int flags)
172{
173 struct nl_msg *msg;
174 int err;
175
176 if ((err = rtnl_class_build_add_request(class, flags, &msg)) < 0)
177 return err;
178
179 return nl_send_sync(sk, msg);
180}
181
182/**
183 * Build netlink message requesting the deletion of a traffic class
184 * @arg class Traffic class to delete
185 * @arg result Pointer to store resulting netlink message
186 *
187 * The behaviour of this function is identical to rtnl_class_delete() with
188 * the exception that it will not send the message but return it in the
189 * provided return pointer instead.
190 *
191 * @see rtnl_class_delete()
192 *
193 * @return 0 on success or a negative error code.
194 */
195int rtnl_class_build_delete_request(struct rtnl_class *class, struct nl_msg **result)
196{
197 struct nl_msg *msg;
198 struct tcmsg tchdr;
199 uint32_t required = TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE;
200
201 if ((class->ce_mask & required) != required) {
202 APPBUG("ifindex and handle must be specified");
203 return -NLE_MISSING_ATTR;
204 }
205
206 if (!(msg = nlmsg_alloc_simple(RTM_DELTCLASS, 0)))
207 return -NLE_NOMEM;
208
209 memset(&tchdr, 0, sizeof(tchdr));
210 tchdr.tcm_family = AF_UNSPEC;
211 tchdr.tcm_ifindex = class->c_ifindex;
212 tchdr.tcm_handle = class->c_handle;
213
214 if (class->ce_mask & TCA_ATTR_PARENT)
215 tchdr.tcm_parent = class->c_parent;
216
217 if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
218 nlmsg_free(msg);
219 return -NLE_MSGSIZE;
220 }
221
222 *result = msg;
223 return 0;
224}
225
226/**
227 * Delete traffic class
228 * @arg sk Netlink socket
229 * @arg class Traffic class to delete
230 *
231 * Builds a \c RTM_DELTCLASS netlink message requesting the deletion
232 * of a traffic class and sends the message to the kernel.
233 *
234 * The message is constructed out of the following attributes:
235 * - \c ifindex and \c handle (required)
236 * - \c parent (optional, must match if provided)
237 *
238 * All other class attributes including all class type specific
239 * attributes are ignored.
240 *
241 * After sending, the function will wait for the ACK or an eventual
242 * error message to be received and will therefore block until the
243 * operation has been completed.
244 *
245 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
246 * this function to return immediately after sending. In this case,
247 * it is the responsibility of the caller to handle any error
248 * messages returned.
249 *
250 * @return 0 on success or a negative error code.
251 */
252int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
253{
254 struct nl_msg *msg;
255 int err;
256
257 if ((err = rtnl_class_build_delete_request(class, &msg)) < 0)
258 return err;
259
260 return nl_send_sync(sk, msg);
261}
262
263/** @} */
264
265/**
266 * @name Leaf Qdisc
267 * @{
268 */
269
270/**
271 * Lookup the leaf qdisc of a traffic class
272 * @arg class the parent traffic class
273 * @arg cache a qdisc cache allocated using rtnl_qdisc_alloc_cache()
274 *
275 * @return Matching Qdisc or NULL if the traffic class has no leaf qdisc
276 */
278 struct nl_cache *cache)
279{
280 struct rtnl_qdisc *leaf;
281
282 if (!class->c_info)
283 return NULL;
284
285 leaf = rtnl_qdisc_get_by_parent(cache, class->c_ifindex,
286 class->c_handle);
287 if (!leaf || leaf->q_handle != class->c_info)
288 return NULL;
289
290 return leaf;
291}
292
293/** @} */
294
295/**
296 * @name Cache Related Functions
297 * @{
298 */
299
300/**
301 * Allocate a cache and fill it with all configured traffic classes
302 * @arg sk Netlink socket
303 * @arg ifindex Interface index of the network device
304 * @arg result Pointer to store the created cache
305 *
306 * Allocates a new traffic class cache and fills it with a list of all
307 * configured traffic classes on a specific network device. Release the
308 * cache with nl_cache_free().
309 *
310 * @return 0 on success or a negative error code.
311 */
312int rtnl_class_alloc_cache(struct nl_sock *sk, int ifindex,
313 struct nl_cache **result)
314{
315 struct nl_cache * cache;
316 int err;
317
318 if (!ifindex) {
319 APPBUG("ifindex must be specified");
320 return -NLE_INVAL;
321 }
322
323 if (!(cache = nl_cache_alloc(&rtnl_class_ops)))
324 return -NLE_NOMEM;
325
326 cache->c_iarg1 = ifindex;
327
328 if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
329 nl_cache_free(cache);
330 return err;
331 }
332
333 *result = cache;
334 return 0;
335}
336
337/**
338 * Search traffic class by interface index and handle
339 * @arg cache Traffic class cache
340 * @arg ifindex Interface index
341 * @arg handle ID of traffic class
342 *
343 * Searches a traffic class cache previously allocated with
344 * rtnl_class_alloc_cache() and searches for a traffi class matching
345 * the interface index and handle.
346 *
347 * The reference counter is incremented before returning the traffic
348 * class, therefore the reference must be given back with rtnl_class_put()
349 * after usage.
350 *
351 * @return Traffic class or NULL if no match was found.
352 */
353struct rtnl_class *rtnl_class_get(struct nl_cache *cache, int ifindex,
354 uint32_t handle)
355{
356 struct rtnl_class *class;
357
358 if (cache->c_ops != &rtnl_class_ops)
359 return NULL;
360
361 nl_list_for_each_entry(class, &cache->c_items, ce_list) {
362 if (class->c_handle == handle && class->c_ifindex == ifindex) {
363 nl_object_get((struct nl_object *) class);
364 return class;
365 }
366 }
367 return NULL;
368}
369
370/**
371 * Search class by interface index and parent
372 * @arg cache Traffic class cache
373 * @arg ifindex Interface index
374 * @arg parent Handle of parent qdisc
375 *
376 * Searches a class cache previously allocated with rtnl_class_alloc_cache()
377 * and searches for a class matching the interface index and parent qdisc.
378 *
379 * The reference counter is incremented before returning the class, therefore
380 * the reference must be given back with rtnl_class_put() after usage.
381 *
382 * @return pointer to class inside the cache or NULL if no match was found.
383 */
384struct rtnl_class *rtnl_class_get_by_parent(struct nl_cache *cache, int ifindex,
385 uint32_t parent)
386{
387 struct rtnl_class *class;
388
389 if (cache->c_ops != &rtnl_class_ops)
390 return NULL;
391
392 nl_list_for_each_entry(class, &cache->c_items, ce_list) {
393 if (class->c_parent == parent && class->c_ifindex == ifindex) {
394 nl_object_get((struct nl_object *) class);
395 return class;
396 }
397 }
398
399 return NULL;
400}
401
402/** @} */
403
404/**
405 * @name Deprecated Functions
406 * @{
407 */
408
409/**
410 * Call a callback for each child of a class
411 *
412 * @deprecated Use of this function is deprecated, it does not allow
413 * to handle the out of memory situation that can occur.
414 */
415void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
416 void (*cb)(struct nl_object *, void *), void *arg)
417{
418 struct rtnl_class *filter;
419
420 filter = rtnl_class_alloc();
421 if (!filter)
422 return;
423
424 rtnl_tc_set_parent(TC_CAST(filter), class->c_handle);
425 rtnl_tc_set_ifindex(TC_CAST(filter), class->c_ifindex);
426 rtnl_tc_set_kind(TC_CAST(filter), class->c_kind);
427
428 nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
429 rtnl_class_put(filter);
430}
431
432/**
433 * Call a callback for each classifier attached to the class
434 *
435 * @deprecated Use of this function is deprecated, it does not allow
436 * to handle the out of memory situation that can occur.
437 */
438void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
439 void (*cb)(struct nl_object *, void *), void *arg)
440{
441 struct rtnl_cls *filter;
442
443 filter = rtnl_cls_alloc();
444 if (!filter)
445 return;
446
447 rtnl_tc_set_ifindex((struct rtnl_tc *) filter, class->c_ifindex);
448 rtnl_tc_set_parent((struct rtnl_tc *) filter, class->c_parent);
449
450 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
451 rtnl_cls_put(filter);
452}
453
454/** @} */
455
456static struct rtnl_tc_type_ops class_ops = {
457 .tt_type = RTNL_TC_TYPE_CLASS,
458 .tt_dump_prefix = "class",
459 .tt_dump = {
460 [NL_DUMP_DETAILS] = class_dump_details,
461 },
462};
463
464static struct nl_object_ops class_obj_ops = {
465 .oo_name = "route/class",
466 .oo_size = sizeof(struct rtnl_class),
467 .oo_free_data = rtnl_tc_free_data,
468 .oo_clone = rtnl_tc_clone,
469 .oo_dump = {
470 [NL_DUMP_LINE] = rtnl_tc_dump_line,
471 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
472 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
473 },
474 .oo_compare = rtnl_tc_compare,
475 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
476};
477
478static struct nl_cache_ops rtnl_class_ops = {
479 .co_name = "route/class",
480 .co_hdrsize = sizeof(struct tcmsg),
481 .co_msgtypes = {
482 { RTM_NEWTCLASS, NL_ACT_NEW, "new" },
483 { RTM_DELTCLASS, NL_ACT_DEL, "del" },
484 { RTM_GETTCLASS, NL_ACT_GET, "get" },
485 END_OF_MSGTYPES_LIST,
486 },
487 .co_protocol = NETLINK_ROUTE,
488 .co_groups = tc_groups,
489 .co_request_update = &class_request_update,
490 .co_msg_parser = &class_msg_parser,
491 .co_obj_ops = &class_obj_ops,
492};
493
494static void _nl_init class_init(void)
495{
496 rtnl_tc_type_register(&class_ops);
497 nl_cache_mngt_register(&rtnl_class_ops);
498}
499
500static void _nl_exit class_exit(void)
501{
502 nl_cache_mngt_unregister(&rtnl_class_ops);
503 rtnl_tc_type_unregister(&class_ops);
504}
505
506/** @} */
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition cache_mngt.c:287
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition cache_mngt.c:252
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition cache.c:1041
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition cache.c:409
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition cache.c:1300
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
Definition cache.c:184
struct rtnl_qdisc * rtnl_class_leaf_qdisc(struct rtnl_class *class, struct nl_cache *cache)
Lookup the leaf qdisc of a traffic class.
Definition class.c:277
int rtnl_class_add(struct nl_sock *sk, struct rtnl_class *class, int flags)
Add/Update traffic class.
Definition class.c:171
int rtnl_class_build_delete_request(struct rtnl_class *class, struct nl_msg **result)
Build netlink message requesting the deletion of a traffic class.
Definition class.c:195
struct rtnl_class * rtnl_class_get_by_parent(struct nl_cache *cache, int ifindex, uint32_t parent)
Search class by interface index and parent.
Definition class.c:384
int rtnl_class_build_add_request(struct rtnl_class *class, int flags, struct nl_msg **result)
Build a netlink message requesting the addition of a traffic class.
Definition class.c:129
struct rtnl_class * rtnl_class_get(struct nl_cache *cache, int ifindex, uint32_t handle)
Search traffic class by interface index and handle.
Definition class.c:353
void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback for each classifier attached to the class.
Definition class.c:438
int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
Delete traffic class.
Definition class.c:252
int rtnl_class_alloc_cache(struct nl_sock *sk, int ifindex, struct nl_cache **result)
Allocate a cache and fill it with all configured traffic classes.
Definition class.c:312
void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback for each child of a class.
Definition class.c:415
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
Definition classid.c:109
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:349
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:566
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:450
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache, int ifindex, uint32_t parent)
Search qdisc by interface index and parent.
Definition qdisc.c:382
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
Definition nl.c:547
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition nl.c:579
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition tc.c:528
void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
Set interface index of traffic control object.
Definition tc.c:277
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition tc.h:50
void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
Set the parent identifier of a traffic control object.
Definition tc.c:506
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1017
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
Dumping parameters.
Definition types.h:32