12#include "nl-default.h"
14#include <netlink/netlink.h>
15#include <netlink/utils.h>
16#include <netlink/route/link.h>
17#include <netlink/route/qdisc.h>
18#include <netlink/route/class.h>
19#include <netlink/route/classifier.h>
23static struct nl_cache_ops rtnl_qdisc_ops;
24static struct nl_object_ops qdisc_obj_ops;
26static int qdisc_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
27 struct nlmsghdr *n,
struct nl_parser_param *pp)
32 if (!(qdisc = rtnl_qdisc_alloc()))
35 if ((err = rtnl_tc_msg_parse(n,
TC_CAST(qdisc))) < 0)
38 err = pp->pp_cb(OBJ_CAST(qdisc), pp);
40 rtnl_qdisc_put(qdisc);
45static int qdisc_request_update(
struct nl_cache *c,
struct nl_sock *sk)
47 struct tcmsg tchdr = {
48 .tcm_family = AF_UNSPEC,
49 .tcm_ifindex = c->c_iarg1,
67 tc->tc_type = RTNL_TC_TYPE_QDISC;
84static int build_qdisc_msg(
struct rtnl_qdisc *qdisc,
int type,
int flags,
85 struct nl_msg **result)
87 if (!(qdisc->ce_mask & TCA_ATTR_IFINDEX)) {
88 APPBUG(
"ifindex must be specified");
89 return -NLE_MISSING_ATTR;
92 return rtnl_tc_msg_build(
TC_CAST(qdisc), type, flags, result);
110 struct nl_msg **result)
112 if (!(qdisc->ce_mask & (TCA_ATTR_HANDLE | TCA_ATTR_PARENT))) {
113 APPBUG(
"handle or parent must be specified");
114 return -NLE_MISSING_ATTR;
117 return build_qdisc_msg(qdisc, RTM_NEWQDISC, flags, result);
185 struct nl_msg **result)
187 if (flags & (NLM_F_CREATE | NLM_F_EXCL)) {
188 APPBUG(
"NLM_F_CREATE and NLM_F_EXCL may not be used here, "
189 "use rtnl_qdisc_add()");
193 if (!(qdisc->ce_mask & TCA_ATTR_IFINDEX)) {
194 APPBUG(
"ifindex must be specified");
195 return -NLE_MISSING_ATTR;
198 if (!(qdisc->ce_mask & (TCA_ATTR_HANDLE | TCA_ATTR_PARENT))) {
199 APPBUG(
"handle or parent must be specified");
200 return -NLE_MISSING_ATTR;
205 if (qdisc->ce_mask & TCA_ATTR_HANDLE)
208 if (qdisc->ce_mask & TCA_ATTR_PARENT)
211 return build_qdisc_msg(
new, RTM_NEWQDISC, flags, result);
269 struct nl_msg **result)
273 uint32_t required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
275 if ((qdisc->ce_mask & required) != required) {
276 APPBUG(
"ifindex and parent must be specified");
277 return -NLE_MISSING_ATTR;
283 memset(&tchdr, 0,
sizeof(tchdr));
285 tchdr.tcm_family = AF_UNSPEC;
286 tchdr.tcm_ifindex = qdisc->q_ifindex;
287 tchdr.tcm_parent = qdisc->q_parent;
289 if (qdisc->ce_mask & TCA_ATTR_HANDLE)
290 tchdr.tcm_handle = qdisc->q_handle;
292 if (
nlmsg_append(msg, &tchdr,
sizeof(tchdr), NLMSG_ALIGNTO) < 0)
293 goto nla_put_failure;
295 if (qdisc->ce_mask & TCA_ATTR_KIND)
383 int ifindex, uint32_t parent)
387 if (cache->c_ops != &rtnl_qdisc_ops)
390 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
391 if (q->q_parent == parent &&
392 q->q_ifindex == ((
unsigned)ifindex)) {
416 int ifindex,
char *kind)
420 if (cache->c_ops != &rtnl_qdisc_ops)
423 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
424 if ((q->q_ifindex == ((
unsigned)ifindex)) &&
425 (!strcmp(q->q_kind, kind))) {
453 if (cache->c_ops != &rtnl_qdisc_ops)
456 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
457 if (q->q_handle == handle &&
458 q->q_ifindex == ((
unsigned)ifindex)) {
481 void (*cb)(
struct nl_object *,
void *),
void *arg)
485 filter = rtnl_class_alloc();
495 rtnl_class_put(filter);
505 void (*cb)(
struct nl_object *,
void *),
void *arg)
507 struct rtnl_cls *filter;
509 if (!(filter = rtnl_cls_alloc()))
516 rtnl_cls_put(filter);
528 struct nl_msg **result)
553 nl_dump(p,
"refcnt %u", qdisc->q_info);
557 .tt_type = RTNL_TC_TYPE_QDISC,
558 .tt_dump_prefix =
"qdisc",
564static struct nl_cache_ops rtnl_qdisc_ops = {
565 .co_name =
"route/qdisc",
566 .co_hdrsize =
sizeof(
struct tcmsg),
568 { RTM_NEWQDISC, NL_ACT_NEW,
"new" },
569 { RTM_DELQDISC, NL_ACT_DEL,
"del" },
570 { RTM_GETQDISC, NL_ACT_GET,
"get" },
571 END_OF_MSGTYPES_LIST,
573 .co_protocol = NETLINK_ROUTE,
574 .co_groups = tc_groups,
575 .co_request_update = qdisc_request_update,
576 .co_msg_parser = qdisc_msg_parser,
577 .co_obj_ops = &qdisc_obj_ops,
580static struct nl_object_ops qdisc_obj_ops = {
581 .oo_name =
"route/qdisc",
583 .oo_free_data = rtnl_tc_free_data,
584 .oo_clone = rtnl_tc_clone,
590 .oo_compare = rtnl_tc_compare,
591 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
594static void _nl_init qdisc_init(
void)
596 rtnl_tc_type_register(&qdisc_ops);
600static void _nl_exit qdisc_exit(
void)
603 rtnl_tc_type_unregister(&qdisc_ops);
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
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).
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int rtnl_qdisc_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Allocate a cache and fill it with all configured qdiscs.
int rtnl_qdisc_delete(struct nl_sock *sk, struct rtnl_qdisc *qdisc)
Delete qdisc.
int rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new, struct nl_msg **result)
Build a netlink message requesting the update of a qdisc.
int rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc, struct nl_msg **result)
Build netlink message requesting the deletion of a qdisc.
struct rtnl_qdisc * rtnl_qdisc_get_by_kind(struct nl_cache *cache, int ifindex, char *kind)
Search qdisc by kind.
int rtnl_qdisc_change(struct nl_sock *sk, struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new)
Change attributes of a qdisc.
struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache, int ifindex, uint32_t parent)
Search qdisc by interface index and parent.
void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback for each filter attached to the qdisc (deprecated)
struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache, int ifindex, uint32_t handle)
Search qdisc by interface index and handle.
int rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc, int flags, struct nl_msg **result)
Build a netlink message requesting the addition of a qdisc.
int rtnl_qdisc_update(struct nl_sock *sk, struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new, int flags)
Update qdisc.
int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc, int flags)
Add qdisc.
void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback for each child class of a qdisc (deprecated)
int rtnl_qdisc_build_update_request(struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new, int flags, struct nl_msg **result)
Build netlink message requesting the update of a qdisc.
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
Set interface index of traffic control object.
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t id)
Set identifier of traffic control object.
void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
Set the parent identifier of a traffic control object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.