• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lib/route/class.c            Traffic Classes
3  *
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
7  *	of the License.
8  *
9  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup tc
14  * @defgroup class Traffic Classes
15  * @{
16  */
17 
18 #include <netlink-private/netlink.h>
19 #include <netlink-private/tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink-private/route/tc-api.h>
22 #include <netlink/route/class.h>
23 #include <netlink/route/qdisc.h>
24 #include <netlink/route/classifier.h>
25 #include <netlink/utils.h>
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 && class->c_ifindex == ifindex) {
363 			nl_object_get((struct nl_object *) class);
364 			return class;
365 		}
366 	}
367 	return NULL;
368 }
369 
370 /** @} */
371 
372 /**
373  * @name Deprecated Functions
374  * @{
375  */
376 
377 /**
378  * Call a callback for each child of a class
379  *
380  * @deprecated Use of this function is deprecated, it does not allow
381  *             to handle the out of memory situation that can occur.
382  */
rtnl_class_foreach_child(struct rtnl_class * class,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)383 void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
384 			      void (*cb)(struct nl_object *, void *), void *arg)
385 {
386 	struct rtnl_class *filter;
387 
388 	filter = rtnl_class_alloc();
389 	if (!filter)
390 		return;
391 
392 	rtnl_tc_set_parent(TC_CAST(filter), class->c_handle);
393 	rtnl_tc_set_ifindex(TC_CAST(filter), class->c_ifindex);
394 	rtnl_tc_set_kind(TC_CAST(filter), class->c_kind);
395 
396 	nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
397 	rtnl_class_put(filter);
398 }
399 
400 /**
401  * Call a callback for each classifier attached to the class
402  *
403  * @deprecated Use of this function is deprecated, it does not allow
404  *             to handle the out of memory situation that can occur.
405  */
rtnl_class_foreach_cls(struct rtnl_class * class,struct nl_cache * cache,void (* cb)(struct nl_object *,void *),void * arg)406 void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
407 			    void (*cb)(struct nl_object *, void *), void *arg)
408 {
409 	struct rtnl_cls *filter;
410 
411 	filter = rtnl_cls_alloc();
412 	if (!filter)
413 		return;
414 
415 	rtnl_tc_set_ifindex((struct rtnl_tc *) filter, class->c_ifindex);
416 	rtnl_tc_set_parent((struct rtnl_tc *) filter, class->c_parent);
417 
418 	nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
419 	rtnl_cls_put(filter);
420 }
421 
422 /** @} */
423 
424 static struct rtnl_tc_type_ops class_ops = {
425 	.tt_type		= RTNL_TC_TYPE_CLASS,
426 	.tt_dump_prefix		= "class",
427 	.tt_dump = {
428 	    [NL_DUMP_DETAILS]	= class_dump_details,
429 	},
430 };
431 
432 static struct nl_object_ops class_obj_ops = {
433 	.oo_name		= "route/class",
434 	.oo_size		= sizeof(struct rtnl_class),
435 	.oo_free_data         	= rtnl_tc_free_data,
436 	.oo_clone		= rtnl_tc_clone,
437 	.oo_dump = {
438 	    [NL_DUMP_LINE]	= rtnl_tc_dump_line,
439 	    [NL_DUMP_DETAILS]	= rtnl_tc_dump_details,
440 	    [NL_DUMP_STATS]	= rtnl_tc_dump_stats,
441 	},
442 	.oo_compare		= rtnl_tc_compare,
443 	.oo_id_attrs		= (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
444 };
445 
446 static struct nl_cache_ops rtnl_class_ops = {
447 	.co_name		= "route/class",
448 	.co_hdrsize		= sizeof(struct tcmsg),
449 	.co_msgtypes		= {
450 					{ RTM_NEWTCLASS, NL_ACT_NEW, "new" },
451 					{ RTM_DELTCLASS, NL_ACT_DEL, "del" },
452 					{ RTM_GETTCLASS, NL_ACT_GET, "get" },
453 					END_OF_MSGTYPES_LIST,
454 				  },
455 	.co_protocol		= NETLINK_ROUTE,
456 	.co_groups		= tc_groups,
457 	.co_request_update	= &class_request_update,
458 	.co_msg_parser		= &class_msg_parser,
459 	.co_obj_ops		= &class_obj_ops,
460 };
461 
class_init(void)462 static void __init class_init(void)
463 {
464 	rtnl_tc_type_register(&class_ops);
465 	nl_cache_mngt_register(&rtnl_class_ops);
466 }
467 
class_exit(void)468 static void __exit class_exit(void)
469 {
470 	nl_cache_mngt_unregister(&rtnl_class_ops);
471 	rtnl_tc_type_unregister(&class_ops);
472 }
473 
474 /** @} */
475