12#include "nl-default.h"
14#include <linux/if_arp.h>
15#include <linux/gen_stats.h>
19#include <netlink/netlink.h>
20#include <netlink/utils.h>
21#include <netlink/route/rtnl.h>
22#include <netlink/route/link.h>
23#include <netlink/route/tc.h>
29static struct nl_list_head tc_ops_list[__RTNL_TC_TYPE_MAX];
34 .maxlen = TCKINDSIZ },
36 [
TCA_STATS] = { .minlen =
sizeof(
struct tc_stats) },
40int tca_parse(
struct nlattr **tb,
int maxattr,
struct rtnl_tc *g,
44 if (g->ce_mask & TCA_ATTR_OPTS)
46 (
struct nlattr *) g->tc_opts->d_data,
47 g->tc_opts->d_size, policy);
51 memset(tb, 0,
sizeof(
struct nlattr *) * (maxattr + 1));
58 [TCA_STATS_RATE_EST] = { .minlen =
sizeof(
struct gnet_stats_rate_est) },
59 [TCA_STATS_QUEUE] = { .minlen =
sizeof(
struct gnet_stats_queue) },
62int rtnl_tc_msg_parse(
struct nlmsghdr *n,
struct rtnl_tc *tc)
64 struct nl_cache *link_cache;
65 struct rtnl_tc_ops *ops;
66 struct nlattr *tb[TCA_MAX + 1];
71 tc->ce_msgtype = n->nlmsg_type;
73 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCA_MAX, tc_policy);
77 if (tb[TCA_KIND] == NULL)
78 return -NLE_MISSING_ATTR;
87 tc->tc_family = tm->tcm_family;
88 tc->tc_ifindex = tm->tcm_ifindex;
89 tc->tc_handle = tm->tcm_handle;
90 tc->tc_parent = tm->tcm_parent;
91 tc->tc_info = tm->tcm_info;
93 tc->ce_mask |= (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE|
94 TCA_ATTR_PARENT | TCA_ATTR_INFO);
96 if (tb[TCA_OPTIONS]) {
100 tc->ce_mask |= TCA_ATTR_OPTS;
103 if (tb[TCA_STATS2]) {
104 struct nlattr *tbs[TCA_STATS_MAX + 1];
111 if (tbs[TCA_STATS_BASIC]) {
112 struct gnet_stats_basic bs;
114 memcpy(&bs,
nla_data(tbs[TCA_STATS_BASIC]),
sizeof(bs));
119 if (tbs[TCA_STATS_RATE_EST]) {
120 struct gnet_stats_rate_est *re;
122 re =
nla_data(tbs[TCA_STATS_RATE_EST]);
127 if (tbs[TCA_STATS_QUEUE]) {
128 struct gnet_stats_queue *q;
138 tc->ce_mask |= TCA_ATTR_STATS;
140 if (tbs[TCA_STATS_APP]) {
142 if (tc->tc_xstats == NULL)
144 tc->ce_mask |= TCA_ATTR_XSTATS;
151 memcpy(&st,
nla_data(tb[TCA_STATS]),
sizeof(st));
161 tc->ce_mask |= TCA_ATTR_STATS;
165 if (tb[TCA_XSTATS]) {
167 if (tc->tc_xstats == NULL)
169 tc->ce_mask |= TCA_ATTR_XSTATS;
173 ops = rtnl_tc_get_ops(tc);
174 if (ops && ops->to_msg_parser) {
180 err = ops->to_msg_parser(tc, data);
185 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
199int rtnl_tc_msg_build(
struct rtnl_tc *tc,
int type,
int flags,
200 struct nl_msg **result)
203 struct rtnl_tc_ops *ops;
204 struct tcmsg tchdr = {
205 .tcm_family = AF_UNSPEC,
206 .tcm_ifindex = tc->tc_ifindex,
207 .tcm_handle = tc->tc_handle,
208 .tcm_parent = tc->tc_parent,
216 if (
nlmsg_append(msg, &tchdr,
sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
221 if (tc->ce_mask & TCA_ATTR_KIND)
224 if (tc->ce_mask & TCA_ATTR_CHAIN)
227 ops = rtnl_tc_get_ops(tc);
228 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
232 if (ops->to_msg_fill) {
238 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
241 if (strcmp(
"cgroup", tc->tc_kind))
245 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
282 tc->ce_mask &= ~TCA_ATTR_LINK;
284 tc->tc_ifindex = ifindex;
285 tc->ce_mask |= TCA_ATTR_IFINDEX;
294 return tc->tc_ifindex;
320 tc->tc_ifindex = link->l_index;
321 tc->ce_mask |= TCA_ATTR_LINK | TCA_ATTR_IFINDEX;
366 tc->ce_mask |= TCA_ATTR_MTU;
380 if (tc->ce_mask & TCA_ATTR_MTU)
382 else if (tc->ce_mask & TCA_ATTR_LINK)
383 return tc->tc_link->l_mtu;
401 tc->ce_mask |= TCA_ATTR_MPU;
429 tc->tc_overhead = overhead;
430 tc->ce_mask |= TCA_ATTR_OVERHEAD;
441 return tc->tc_overhead;
457 tc->tc_linktype = type;
458 tc->ce_mask |= TCA_ATTR_LINKTYPE;
472 if (tc->ce_mask & TCA_ATTR_LINKTYPE)
473 return tc->tc_linktype;
474 else if (tc->ce_mask & TCA_ATTR_LINK)
475 return tc->tc_link->l_arptype;
488 tc->ce_mask |= TCA_ATTR_HANDLE;
497 return tc->tc_handle;
508 tc->tc_parent = parent;
509 tc->ce_mask |= TCA_ATTR_PARENT;
518 return tc->tc_parent;
530 if (tc->ce_mask & TCA_ATTR_KIND)
534 || strlen (kind) >=
sizeof (tc->tc_kind))
537 _nl_strncpy_assert(tc->tc_kind, kind,
sizeof(tc->tc_kind));
539 tc->ce_mask |= TCA_ATTR_KIND;
555 if (tc->ce_mask & TCA_ATTR_KIND)
570 if ((
unsigned int)
id > RTNL_TC_STATS_MAX)
573 return tc->tc_stats[id];
584 tc->tc_chain = chain;
585 tc->ce_mask |= TCA_ATTR_CHAIN;
598 if (!(tc->ce_mask & TCA_ATTR_CHAIN))
599 return -NLE_MISSING_ATTR;
600 *out_value = tc->tc_chain;
611static const struct trans_tbl tc_stats[] = {
623char *rtnl_tc_stat2str(
enum rtnl_tc_stat st,
char *buf,
size_t len)
625 return __type2str(st, buf, len, tc_stats, ARRAY_SIZE(tc_stats));
628int rtnl_tc_str2stat(
const char *name)
630 return __str2type(name, tc_stats, ARRAY_SIZE(tc_stats));
649 return ((
double) bufsize / (
double) rate) * 1000000.0;
668 return ((
double) txtime * (
double) rate) / 1000000.0;
680 for (i = 0; i < 32; i++)
681 if ((((uint32_t)1u) << i) == ((uint32_t)cell_size))
710static unsigned int align_to_atm(
unsigned int size)
713 cells = size / ATM_CELL_PAYLOAD;
714 if ((size % ATM_CELL_PAYLOAD) > 0)
717 linksize = cells * ATM_CELL_SIZE;
721static unsigned int adjust_size(
unsigned int size,
unsigned int mpu,
729 return align_to_atm(size);
755 uint8_t cell_log = spec->rs_cell_log;
756 unsigned int size, i;
764 if (cell_log == UINT8_MAX) {
771 while ((mtu >> cell_log) >= RTNL_TC_RTABLE_SIZE)
775 for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
776 size = adjust_size((i + 1) << cell_log, spec->rs_mpu, linktype);
777 dst[i] =
nl_us2ticks(rtnl_tc_calc_txtime64(size, spec->rs_rate64));
780 spec->rs_cell_align = -1;
781 spec->rs_cell_log = cell_log;
792void rtnl_tc_free_data(
struct nl_object *obj)
795 struct rtnl_tc_ops *ops;
801 if (tc->tc_subdata) {
802 ops = rtnl_tc_get_ops(tc);
803 if (ops && ops->to_free_data)
804 ops->to_free_data(tc,
nl_data_get(tc->tc_subdata));
810int rtnl_tc_clone(
struct nl_object *dstobj,
struct nl_object *srcobj)
814 struct rtnl_tc_ops *ops;
817 dst->tc_xstats = NULL;
818 dst->tc_subdata = NULL;
824 dst->tc_link = src->tc_link;
827 dst->ce_mask &= ~(TCA_ATTR_OPTS |
834 dst->ce_mask |= TCA_ATTR_OPTS;
837 if (src->tc_xstats) {
841 dst->ce_mask |= TCA_ATTR_XSTATS;
844 if (src->tc_subdata) {
857 ops = rtnl_tc_get_ops(src);
858 if (ops && ops->to_clone) {
870 struct rtnl_tc_ops *ops;
873 type_ops = tc_type_ops[tc->tc_type];
874 if (type_ops && type_ops->
tt_dump[type])
875 type_ops->
tt_dump[type](tc, p);
877 ops = rtnl_tc_get_ops(tc);
878 if (ops && ops->to_dump[type]) {
879 ops->to_dump[type](tc, data, p);
886void rtnl_tc_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
890 struct nl_cache *link_cache;
895 type_ops = tc_type_ops[tc->tc_type];
896 if (type_ops && type_ops->tt_dump_prefix)
897 nl_dump(p,
"%s ", type_ops->tt_dump_prefix);
899 nl_dump(p,
"%s ", tc->tc_kind);
906 nl_dump(p,
"dev %u ", tc->tc_ifindex);
918 nl_cache_put(link_cache);
921void rtnl_tc_dump_details(
struct nl_object *obj,
struct nl_dump_params *p)
925 rtnl_tc_dump_line(OBJ_CAST(tc), p);
927 nl_dump_line(p,
" ");
929 if (tc->ce_mask & TCA_ATTR_MTU)
930 nl_dump(p,
" mtu %u", tc->tc_mtu);
932 if (tc->ce_mask & TCA_ATTR_MPU)
933 nl_dump(p,
" mpu %u", tc->tc_mpu);
935 if (tc->ce_mask & TCA_ATTR_OVERHEAD)
936 nl_dump(p,
" overhead %u", tc->tc_overhead);
943void rtnl_tc_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
949 rtnl_tc_dump_details(OBJ_CAST(tc), p);
952 " stats: %-14s %-10s %-10s %-10s %-10s %-10s\n",
953 "bytes",
"packets",
"drops",
"overlimits",
"qlen",
"backlog");
959 " %10.2f %3s %10llu %-10llu %-10llu %-10llu %-10llu\n",
968 nl_dump_line(p,
" %10.2f %3s/s %10llu/s\n", res, unit,
972uint64_t rtnl_tc_compare(
struct nl_object *aobj,
struct nl_object *bobj,
973 uint64_t attrs,
int flags)
979#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
980 diff |= _DIFF(TCA_ATTR_HANDLE, a->tc_handle != b->tc_handle);
981 diff |= _DIFF(TCA_ATTR_PARENT, a->tc_parent != b->tc_parent);
982 diff |= _DIFF(TCA_ATTR_IFINDEX, a->tc_ifindex != b->tc_ifindex);
983 diff |= _DIFF(TCA_ATTR_KIND, strcmp(a->tc_kind, b->tc_kind));
995struct rtnl_tc_ops *rtnl_tc_lookup_ops(
enum rtnl_tc_type type,
const char *kind)
997 struct rtnl_tc_ops *ops;
999 nl_list_for_each_entry(ops, &tc_ops_list[type], to_list)
1000 if (!strcmp(kind, ops->to_kind))
1006struct rtnl_tc_ops *rtnl_tc_get_ops(
struct rtnl_tc *tc)
1009 tc->tc_ops = rtnl_tc_lookup_ops(tc->tc_type, tc->tc_kind);
1020 static int init = 0;
1031 for (i = 0; i < __RTNL_TC_TYPE_MAX; i++)
1032 nl_init_list_head(&tc_ops_list[i]);
1037 if (!ops->to_kind || ops->to_type > RTNL_TC_TYPE_MAX)
1040 if (rtnl_tc_lookup_ops(ops->to_type, ops->to_kind))
1043 nl_list_add_tail(&ops->to_list, &tc_ops_list[ops->to_type]);
1054 nl_list_del(&ops->to_list);
1067 return tc->tc_subdata ?
nl_data_get(tc->tc_subdata) : NULL;
1081 if (!tc->tc_subdata) {
1085 if (!rtnl_tc_get_ops(tc))
1089 if (!(size = tc->tc_ops->to_size))
1118 if (tc->tc_ops != ops) {
1121 snprintf(buf,
sizeof(buf),
1122 "tc object %p used in %s context but is of type %s",
1123 tc, ops->to_kind, tc->tc_ops->to_kind);
1127 *err = -NLE_OPNOTSUPP;
1140struct nl_af_group tc_groups[] = {
1141 { AF_UNSPEC, RTNLGRP_TC },
1142 { END_OF_GROUP_LIST },
1148 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1151 tc_type_ops[ops->tt_type] = ops;
1156 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1159 tc_type_ops[ops->tt_type] = NULL;
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nla_nest_end_keep_empty(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes without stripping off empty attributes.
@ NLA_STRING
NUL terminated character string.
@ NLA_NESTED
Nested attributes.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
struct nl_data * nl_data_clone(const struct nl_data *src)
Clone an abstract data object.
void nl_data_free(struct nl_data *data)
Free an abstract data object.
struct nl_data * nl_data_alloc(const void *buf, size_t size)
Allocate a new abstract data object.
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
void * nl_data_get(const struct nl_data *data)
Get data buffer of abstract data object.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Lookup link in cache by interface index.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a 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_get(struct nl_object *obj)
Acquire a reference on a object.
void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu)
Set the Minimum Packet Unit (MPU) of a traffic control object.
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type)
Set the linktype of a traffic control object.
void * rtnl_tc_data_check(struct rtnl_tc *tc, struct rtnl_tc_ops *ops, int *err)
Check traffic control object type and return private data section.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
void * rtnl_tc_data_peek(struct rtnl_tc *tc)
Returns the private data of the traffic control object.
int rtnl_tc_calc_cell_log(int cell_size)
Calculate the binary logarithm for a specific cell size.
int rtnl_tc_get_chain(struct rtnl_tc *tc, uint32_t *out_value)
Return chain index of traffic control object.
char * rtnl_tc_get_kind(struct rtnl_tc *tc)
Return kind of traffic control object.
void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
Set interface index of traffic control object.
int rtnl_tc_build_rate_table(struct rtnl_tc *tc, struct rtnl_ratespec *spec, uint32_t *dst)
Compute a transmission time lookup table.
void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead)
Set per packet overhead of a traffic control object.
int rtnl_tc_calc_bufsize(int txtime, int rate)
Calculate buffer size able to transmit in a specific time and rate.
uint32_t rtnl_tc_get_mtu(struct rtnl_tc *tc)
Return the MTU of traffic control object.
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
Set link of traffic control object.
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
uint32_t rtnl_tc_get_overhead(struct rtnl_tc *tc)
Return per packet overhead of a traffic control 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 * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu)
Set the Maximum Transmission Unit (MTU) of traffic control object.
void rtnl_tc_set_chain(struct rtnl_tc *tc, uint32_t chain)
Set the chain index of a traffic control object.
uint64_t rtnl_tc_get_stat(struct rtnl_tc *tc, enum rtnl_tc_stat id)
Return value of a statistical counter of a traffic control object.
rtnl_tc_stat
Traffic control statistical identifier.
struct rtnl_link * rtnl_tc_get_link(struct rtnl_tc *tc)
Get link of traffic control object.
uint32_t rtnl_tc_get_linktype(struct rtnl_tc *tc)
Return the linktype of a traffic control object.
uint32_t rtnl_tc_get_mpu(struct rtnl_tc *tc)
Return the Minimum Packet Unit (MPU) of a traffic control object.
int rtnl_tc_calc_txtime(int bufsize, int rate)
Calculate time required to transmit buffer at a specific rate.
@ RTNL_TC_PACKETS
Number of packets seen.
@ RTNL_TC_BYTES
Total bytes seen.
@ RTNL_TC_QLEN
Current queue length.
@ RTNL_TC_OVERLIMITS
Total number of overlimits.
@ RTNL_TC_DROPS
Total number of packets dropped.
@ RTNL_TC_RATE_PPS
Current packet/s (rate estimator)
@ RTNL_TC_REQUEUES
Total number of requeues.
@ RTNL_TC_BACKLOG
Current backlog length.
@ RTNL_TC_RATE_BPS
Current bits/s (rate estimator)
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
nl_dump_type
Enumeration of dumping variations (dp_type)
uint32_t nl_us2ticks(uint32_t us)
Convert micro seconds to ticks.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
uint16_t type
Type of attribute or NLA_UNSPEC.
void(* tt_dump[NL_DUMP_MAX+1])(struct rtnl_tc *, struct nl_dump_params *)
Dump callbacks.