1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2013 Thomas Graf <[email protected]>
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
23 struct rtnl_class {
24 NL_TC_GENERIC(c);
25 };
26
27 static struct nl_cache_ops rtnl_class_ops;
28 static struct nl_object_ops class_obj_ops;
29
class_dump_details(struct rtnl_tc * tc,struct nl_dump_params * p)30 static 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
class_msg_parser(struct nl_cache_ops * ops,struct sockaddr_nl * who,struct nlmsghdr * nlh,struct nl_parser_param * pp)41 static 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);
54 errout:
55 rtnl_class_put(class);
56
57 return err;
58 }
59
class_request_update(struct nl_cache * cache,struct nl_sock * sk)60 static 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
rtnl_class_alloc(void)76 struct 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
rtnl_class_put(struct rtnl_class * class)87 void 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
class_build(struct rtnl_class * class,int type,int flags,struct nl_msg ** result)100 static 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 */
rtnl_class_build_add_request(struct rtnl_class * class,int flags,struct nl_msg ** result)129 int 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 */
rtnl_class_add(struct nl_sock * sk,struct rtnl_class * class,int flags)171 int 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 */
rtnl_class_build_delete_request(struct rtnl_class * class,struct nl_msg ** result)195 int 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 */
rtnl_class_delete(struct nl_sock * sk,struct rtnl_class * class)252 int 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 */
rtnl_class_leaf_qdisc(struct rtnl_class * class,struct nl_cache * cache)277 struct rtnl_qdisc *rtnl_class_leaf_qdisc(struct rtnl_class *class,
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 */
rtnl_class_alloc_cache(struct nl_sock * sk,int ifindex,struct nl_cache ** result)312 int 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 */
rtnl_class_get(struct nl_cache * cache,int ifindex,uint32_t handle)353 struct 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 &&
363 class->c_ifindex == ((unsigned)ifindex)) {
364 nl_object_get((struct nl_object *) class);
365 return class;
366 }
367 }
368 return NULL;
369 }
370
371 /**
372 * Search class by interface index and parent
373 * @arg cache Traffic class cache
374 * @arg ifindex Interface index
375 * @arg parent Handle of parent qdisc
376 *
377 * Searches a class cache previously allocated with rtnl_class_alloc_cache()
378 * and searches for a class matching the interface index and parent qdisc.
379 *
380 * The reference counter is incremented before returning the class, therefore
381 * the reference must be given back with rtnl_class_put() after usage.
382 *
383 * @return pointer to class inside the cache or NULL if no match was found.
384 */
rtnl_class_get_by_parent(struct nl_cache * cache,int ifindex,uint32_t parent)385 struct rtnl_class *rtnl_class_get_by_parent(struct nl_cache *cache, int ifindex,
386 uint32_t parent)
387 {
388 struct rtnl_class *class;
389
390 if (cache->c_ops != &rtnl_class_ops)
391 return NULL;
392
393 nl_list_for_each_entry(class, &cache->c_items, ce_list) {
394 if (class->c_parent == parent &&
395 class->c_ifindex == ((unsigned)ifindex)) {
396 nl_object_get((struct nl_object *) class);
397 return class;
398 }
399 }
400
401 return NULL;
402 }
403
404 /** @} */
405
406 /**
407 * @name Deprecated Functions
408 * @{
409 */
410
411 /**
412 * Call a callback for each child of a class
413 *
414 * @deprecated Use of this function is deprecated, it does not allow
415 * to handle the out of memory situation that can occur.
416 */
rtnl_class_foreach_child(struct rtnl_class * class,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)417 void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
418 void (*cb)(struct nl_object *, void *), void *arg)
419 {
420 struct rtnl_class *filter;
421
422 filter = rtnl_class_alloc();
423 if (!filter)
424 return;
425
426 rtnl_tc_set_parent(TC_CAST(filter), class->c_handle);
427 rtnl_tc_set_ifindex(TC_CAST(filter), class->c_ifindex);
428 rtnl_tc_set_kind(TC_CAST(filter), class->c_kind);
429
430 nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
431 rtnl_class_put(filter);
432 }
433
434 /**
435 * Call a callback for each classifier attached to the class
436 *
437 * @deprecated Use of this function is deprecated, it does not allow
438 * to handle the out of memory situation that can occur.
439 */
rtnl_class_foreach_cls(struct rtnl_class * class,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)440 void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
441 void (*cb)(struct nl_object *, void *), void *arg)
442 {
443 struct rtnl_cls *filter;
444
445 filter = rtnl_cls_alloc();
446 if (!filter)
447 return;
448
449 rtnl_tc_set_ifindex((struct rtnl_tc *) filter, class->c_ifindex);
450 rtnl_tc_set_parent((struct rtnl_tc *) filter, class->c_parent);
451
452 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
453 rtnl_cls_put(filter);
454 }
455
456 /** @} */
457
458 static struct rtnl_tc_type_ops class_ops = {
459 .tt_type = RTNL_TC_TYPE_CLASS,
460 .tt_dump_prefix = "class",
461 .tt_dump = {
462 [NL_DUMP_DETAILS] = class_dump_details,
463 },
464 };
465
466 static struct nl_object_ops class_obj_ops = {
467 .oo_name = "route/class",
468 .oo_size = sizeof(struct rtnl_class),
469 .oo_free_data = rtnl_tc_free_data,
470 .oo_clone = rtnl_tc_clone,
471 .oo_dump = {
472 [NL_DUMP_LINE] = rtnl_tc_dump_line,
473 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
474 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
475 },
476 .oo_compare = rtnl_tc_compare,
477 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
478 };
479
480 static struct nl_cache_ops rtnl_class_ops = {
481 .co_name = "route/class",
482 .co_hdrsize = sizeof(struct tcmsg),
483 .co_msgtypes = {
484 { RTM_NEWTCLASS, NL_ACT_NEW, "new" },
485 { RTM_DELTCLASS, NL_ACT_DEL, "del" },
486 { RTM_GETTCLASS, NL_ACT_GET, "get" },
487 END_OF_MSGTYPES_LIST,
488 },
489 .co_protocol = NETLINK_ROUTE,
490 .co_groups = tc_groups,
491 .co_request_update = &class_request_update,
492 .co_msg_parser = &class_msg_parser,
493 .co_obj_ops = &class_obj_ops,
494 };
495
class_init(void)496 static void _nl_init class_init(void)
497 {
498 rtnl_tc_type_register(&class_ops);
499 nl_cache_mngt_register(&rtnl_class_ops);
500 }
501
class_exit(void)502 static void _nl_exit class_exit(void)
503 {
504 nl_cache_mngt_unregister(&rtnl_class_ops);
505 rtnl_tc_type_unregister(&class_ops);
506 }
507
508 /** @} */
509