18 #include <netlink-private/netlink.h>
19 #include <netlink-private/tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/rtnl.h>
23 #include <netlink/route/link.h>
24 #include <netlink/route/tc.h>
25 #include <netlink-private/route/tc-api.h>
29 static struct nl_list_head tc_ops_list[__RTNL_TC_TYPE_MAX];
30 static struct rtnl_tc_type_ops *tc_type_ops[__RTNL_TC_TYPE_MAX];
32 static struct nla_policy tc_policy[TCA_MAX+1] = {
34 .maxlen = TCKINDSIZ },
35 [TCA_STATS] = { .minlen =
sizeof(
struct tc_stats) },
39 int tca_parse(
struct nlattr **tb,
int maxattr,
struct rtnl_tc *g,
43 if (g->ce_mask & TCA_ATTR_OPTS)
45 (
struct nlattr *) g->tc_opts->d_data,
46 g->tc_opts->d_size, policy);
50 memset(tb, 0,
sizeof(
struct nlattr *) * (maxattr + 1));
55 static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
56 [TCA_STATS_BASIC] = { .
minlen =
sizeof(
struct gnet_stats_basic) },
57 [TCA_STATS_RATE_EST] = { .minlen =
sizeof(
struct gnet_stats_rate_est) },
58 [TCA_STATS_QUEUE] = { .minlen =
sizeof(
struct gnet_stats_queue) },
61 int rtnl_tc_msg_parse(
struct nlmsghdr *n,
struct rtnl_tc *tc)
63 struct nl_cache *link_cache;
64 struct rtnl_tc_ops *ops;
65 struct nlattr *tb[TCA_MAX + 1];
70 tc->ce_msgtype = n->nlmsg_type;
72 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCA_MAX, tc_policy);
76 if (tb[TCA_KIND] == NULL)
77 return -NLE_MISSING_ATTR;
83 tc->tc_family = tm->tcm_family;
84 tc->tc_ifindex = tm->tcm_ifindex;
85 tc->tc_handle = tm->tcm_handle;
86 tc->tc_parent = tm->tcm_parent;
87 tc->tc_info = tm->tcm_info;
89 tc->ce_mask |= (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE|
90 TCA_ATTR_PARENT | TCA_ATTR_INFO);
92 if (tb[TCA_OPTIONS]) {
96 tc->ce_mask |= TCA_ATTR_OPTS;
100 struct nlattr *tbs[TCA_STATS_MAX + 1];
107 if (tbs[TCA_STATS_BASIC]) {
108 struct gnet_stats_basic *bs;
110 bs =
nla_data(tbs[TCA_STATS_BASIC]);
115 if (tbs[TCA_STATS_RATE_EST]) {
116 struct gnet_stats_rate_est *re;
118 re =
nla_data(tbs[TCA_STATS_RATE_EST]);
123 if (tbs[TCA_STATS_QUEUE]) {
124 struct gnet_stats_queue *q;
134 tc->ce_mask |= TCA_ATTR_STATS;
136 if (tbs[TCA_STATS_APP]) {
138 if (tc->tc_xstats == NULL)
140 tc->ce_mask |= TCA_ATTR_XSTATS;
145 struct tc_stats *st =
nla_data(tb[TCA_STATS]);
156 tc->ce_mask |= TCA_ATTR_STATS;
160 if (tb[TCA_XSTATS]) {
162 if (tc->tc_xstats == NULL)
164 tc->ce_mask |= TCA_ATTR_XSTATS;
168 ops = rtnl_tc_get_ops(tc);
169 if (ops && ops->to_msg_parser) {
175 err = ops->to_msg_parser(tc, data);
180 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
194 int rtnl_tc_msg_build(
struct rtnl_tc *tc,
int type,
int flags,
195 struct nl_msg **result)
198 struct rtnl_tc_ops *ops;
199 struct tcmsg tchdr = {
200 .tcm_family = AF_UNSPEC,
201 .tcm_ifindex = tc->tc_ifindex,
202 .tcm_handle = tc->tc_handle,
203 .tcm_parent = tc->tc_parent,
205 int err = -NLE_MSGSIZE;
211 if (
nlmsg_append(msg, &tchdr,
sizeof(tchdr), NLMSG_ALIGNTO) < 0)
212 goto nla_put_failure;
214 if (tc->ce_mask & TCA_ATTR_KIND)
217 ops = rtnl_tc_get_ops(tc);
218 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
222 if (ops->to_msg_fill) {
224 goto nla_put_failure;
226 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
227 goto nla_put_failure;
230 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
231 goto nla_put_failure;
242 void tca_set_kind(
struct rtnl_tc *t,
const char *kind)
244 strncpy(t->tc_kind, kind,
sizeof(t->tc_kind) - 1);
245 t->ce_mask |= TCA_ATTR_KIND;
271 tc->ce_mask &= ~TCA_ATTR_LINK;
273 tc->tc_ifindex = ifindex;
274 tc->ce_mask |= TCA_ATTR_IFINDEX;
283 return tc->tc_ifindex;
309 tc->tc_ifindex = link->l_index;
310 tc->ce_mask |= TCA_ATTR_LINK | TCA_ATTR_IFINDEX;
355 tc->ce_mask |= TCA_ATTR_MTU;
369 if (tc->ce_mask & TCA_ATTR_MTU)
371 else if (tc->ce_mask & TCA_ATTR_LINK)
372 return tc->tc_link->l_mtu;
390 tc->ce_mask |= TCA_ATTR_MPU;
418 tc->tc_overhead = overhead;
419 tc->ce_mask |= TCA_ATTR_OVERHEAD;
430 return tc->tc_overhead;
446 tc->tc_linktype = type;
447 tc->ce_mask |= TCA_ATTR_LINKTYPE;
461 if (tc->ce_mask & TCA_ATTR_LINKTYPE)
462 return tc->tc_linktype;
463 else if (tc->ce_mask & TCA_ATTR_LINK)
464 return tc->tc_link->l_arptype;
477 tc->ce_mask |= TCA_ATTR_HANDLE;
486 return tc->tc_handle;
497 tc->tc_parent = parent;
498 tc->ce_mask |= TCA_ATTR_PARENT;
507 return tc->tc_parent;
519 if (tc->ce_mask & TCA_ATTR_KIND)
522 strncpy(tc->tc_kind, kind,
sizeof(tc->tc_kind) - 1);
523 tc->ce_mask |= TCA_ATTR_KIND;
539 if (tc->ce_mask & TCA_ATTR_KIND)
554 if ((
unsigned int)
id > RTNL_TC_STATS_MAX)
557 return tc->tc_stats[id];
567 static const struct trans_tbl tc_stats[] = {
579 char *rtnl_tc_stat2str(
enum rtnl_tc_stat st,
char *buf,
size_t len)
581 return __type2str(st, buf, len, tc_stats, ARRAY_SIZE(tc_stats));
584 int rtnl_tc_str2stat(
const char *name)
586 return __str2type(name, tc_stats, ARRAY_SIZE(tc_stats));
607 tx_time_secs = (double) bufsize / (
double) rate;
609 return tx_time_secs * 1000000.;
630 bufsize = (double) txtime * (
double) rate;
632 return bufsize / 1000000.;
644 for (i = 0; i < 32; i++)
645 if ((1 << i) == cell_size)
674 static unsigned int align_to_atm(
unsigned int size)
677 cells = size / ATM_CELL_PAYLOAD;
678 if ((size % ATM_CELL_PAYLOAD) > 0)
681 linksize = cells * ATM_CELL_SIZE;
685 static unsigned int adjust_size(
unsigned int size,
unsigned int mpu,
693 return align_to_atm(size);
719 uint8_t cell_log = spec->rs_cell_log;
720 unsigned int size, i;
728 if (cell_log == UINT8_MAX) {
735 while ((mtu >> cell_log) >= RTNL_TC_RTABLE_SIZE)
739 for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
740 size = adjust_size((i + 1) << cell_log, spec->rs_mpu, linktype);
744 spec->rs_cell_align = -1;
745 spec->rs_cell_log = cell_log;
756 void rtnl_tc_free_data(
struct nl_object *obj)
758 struct rtnl_tc *tc =
TC_CAST(obj);
759 struct rtnl_tc_ops *ops;
765 if (tc->tc_subdata) {
766 ops = rtnl_tc_get_ops(tc);
767 if (ops && ops->to_free_data)
768 ops->to_free_data(tc,
nl_data_get(tc->tc_subdata));
774 int rtnl_tc_clone(
struct nl_object *dstobj,
struct nl_object *srcobj)
776 struct rtnl_tc *dst =
TC_CAST(dstobj);
777 struct rtnl_tc *src =
TC_CAST(srcobj);
778 struct rtnl_tc_ops *ops;
782 dst->tc_link = src->tc_link;
786 dst->tc_xstats = NULL;
787 dst->tc_subdata = NULL;
788 dst->ce_mask &= ~(TCA_ATTR_OPTS |
795 dst->ce_mask |= TCA_ATTR_OPTS;
798 if (src->tc_xstats) {
802 dst->ce_mask |= TCA_ATTR_XSTATS;
805 if (src->tc_subdata) {
811 ops = rtnl_tc_get_ops(src);
812 if (ops && ops->to_clone) {
820 return ops->to_clone(a, b);
826 static int tc_dump(
struct rtnl_tc *tc,
enum nl_dump_type type,
829 struct rtnl_tc_type_ops *type_ops;
830 struct rtnl_tc_ops *ops;
833 type_ops = tc_type_ops[tc->tc_type];
834 if (type_ops && type_ops->tt_dump[type])
835 type_ops->tt_dump[type](tc, p);
837 ops = rtnl_tc_get_ops(tc);
838 if (ops && ops->to_dump[type]) {
839 ops->to_dump[type](tc, data, p);
846 void rtnl_tc_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
848 struct rtnl_tc_type_ops *type_ops;
849 struct rtnl_tc *tc =
TC_CAST(obj);
850 struct nl_cache *link_cache;
855 type_ops = tc_type_ops[tc->tc_type];
856 if (type_ops && type_ops->tt_dump_prefix)
857 nl_dump(p,
"%s ", type_ops->tt_dump_prefix);
859 nl_dump(p,
"%s ", tc->tc_kind);
866 nl_dump(p,
"dev %u ", tc->tc_ifindex);
878 nl_cache_put(link_cache);
881 void rtnl_tc_dump_details(
struct nl_object *obj,
struct nl_dump_params *p)
883 struct rtnl_tc *tc =
TC_CAST(obj);
885 rtnl_tc_dump_line(OBJ_CAST(tc), p);
887 nl_dump_line(p,
" ");
889 if (tc->ce_mask & TCA_ATTR_MTU)
890 nl_dump(p,
" mtu %u", tc->tc_mtu);
892 if (tc->ce_mask & TCA_ATTR_MPU)
893 nl_dump(p,
" mpu %u", tc->tc_mpu);
895 if (tc->ce_mask & TCA_ATTR_OVERHEAD)
896 nl_dump(p,
" overhead %u", tc->tc_overhead);
903 void rtnl_tc_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
905 struct rtnl_tc *tc =
TC_CAST(obj);
909 rtnl_tc_dump_details(OBJ_CAST(tc), p);
912 " stats: %-14s %-10s %-10s %-10s %-10s %-10s\n",
913 "bytes",
"packets",
"drops",
"overlimits",
"qlen",
"backlog");
918 " %10.2f %3s %10u %-10u %-10u %-10u %-10u\n",
929 " %10.2f %3s/s %10u/s\n",
935 int rtnl_tc_compare(
struct nl_object *aobj,
struct nl_object *bobj,
936 uint32_t attrs,
int flags)
938 struct rtnl_tc *a =
TC_CAST(aobj);
939 struct rtnl_tc *b =
TC_CAST(bobj);
942 #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
944 diff |= TC_DIFF(HANDLE, a->tc_handle != b->tc_handle);
945 diff |= TC_DIFF(PARENT, a->tc_parent != b->tc_parent);
946 diff |= TC_DIFF(IFINDEX, a->tc_ifindex != b->tc_ifindex);
947 diff |= TC_DIFF(KIND, strcmp(a->tc_kind, b->tc_kind));
960 struct rtnl_tc_ops *rtnl_tc_lookup_ops(
enum rtnl_tc_type type,
const char *kind)
962 struct rtnl_tc_ops *ops;
964 nl_list_for_each_entry(ops, &tc_ops_list[type], to_list)
965 if (!strcmp(kind, ops->to_kind))
971 struct rtnl_tc_ops *rtnl_tc_get_ops(struct rtnl_tc *tc)
974 tc->tc_ops = rtnl_tc_lookup_ops(tc->tc_type, tc->tc_kind);
996 for (i = 0; i < __RTNL_TC_TYPE_MAX; i++)
997 nl_init_list_head(&tc_ops_list[i]);
1002 if (!ops->to_kind || ops->to_type > RTNL_TC_TYPE_MAX)
1005 if (rtnl_tc_lookup_ops(ops->to_type, ops->to_kind))
1008 nl_list_add_tail(&ops->to_list, &tc_ops_list[ops->to_type]);
1019 nl_list_del(&ops->to_list);
1032 return tc->tc_subdata ?
nl_data_get(tc->tc_subdata) : NULL;
1046 if (!tc->tc_subdata) {
1050 if (!rtnl_tc_get_ops(tc))
1054 if (!(size = tc->tc_ops->to_size))
1083 if (tc->tc_ops != ops) {
1086 snprintf(buf,
sizeof(buf),
1087 "tc object %p used in %s context but is of type %s",
1088 tc, ops->to_kind, tc->tc_ops->to_kind);
1092 *err = -NLE_OPNOTSUPP;
1105 struct nl_af_group tc_groups[] = {
1106 { AF_UNSPEC, RTNLGRP_TC },
1107 { END_OF_GROUP_LIST },
1111 void rtnl_tc_type_register(
struct rtnl_tc_type_ops *ops)
1113 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1116 tc_type_ops[ops->tt_type] = ops;
1119 void rtnl_tc_type_unregister(
struct rtnl_tc_type_ops *ops)
1121 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1124 tc_type_ops[ops->tt_type] = NULL;
Dump object briefly on one line.
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
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.
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
int rtnl_tc_calc_txtime(int bufsize, int rate)
Calculate time required to transmit buffer at a specific rate.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
uint32_t rtnl_tc_get_linktype(struct rtnl_tc *tc)
Return the linktype of a traffic control object.
void * nl_data_get(const struct nl_data *data)
Get data buffer of abstract data object.
uint32_t rtnl_tc_get_overhead(struct rtnl_tc *tc)
Return per packet overhead of a traffic control object.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Lookup link in cache by interface index.
Current bits/s (rate estimator)
int rtnl_tc_calc_cell_log(int cell_size)
Calculate the binary logarithm for a specific cell size.
Attribute validation policy.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type)
Set the linktype of a traffic control object.
Current packet/s (rate estimator)
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
Set the parent identifier of a traffic control object.
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
NUL terminated character string.
Dump all attributes but no statistics.
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.
void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
Set link of traffic control object.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
nl_dump_type
Enumeration of dumping variations (dp_type)
Total number of overlimits.
int rtnl_tc_calc_bufsize(int txtime, int rate)
Calculate buffer size able to transmit in a specific time and rate.
rtnl_tc_stat
Traffic control statistical identifier.
uint32_t rtnl_tc_get_mtu(struct rtnl_tc *tc)
Return the MTU of traffic control object.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
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.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t id)
Set identifier of traffic control object.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Create attribute index based on a stream of attributes.
struct nl_data * nl_data_clone(const struct nl_data *src)
Clone an abstract data object.
uint16_t minlen
Minimal length of payload required.
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu)
Set the Minimum Packet Unit (MPU) of a traffic control object.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
struct nl_data * nl_data_alloc(const void *buf, size_t size)
Allocate a new abstract data object.
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
uint16_t type
Type of attribute or NLA_UNSPEC.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu)
Set the Maximum Transmission Unit (MTU) of traffic control object.
uint32_t nl_us2ticks(uint32_t us)
Convert micro seconds to ticks.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
int rtnl_tc_build_rate_table(struct rtnl_tc *tc, struct rtnl_ratespec *spec, uint32_t *dst)
Compute a transmission time lookup table.
char * rtnl_tc_get_kind(struct rtnl_tc *tc)
Return kind of traffic control object.
Total number of requeues.
void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead)
Set per packet overhead 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.
struct rtnl_link * rtnl_tc_get_link(struct rtnl_tc *tc)
Get link of traffic control object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
void * rtnl_tc_data_peek(struct rtnl_tc *tc)
Returns the private data of the traffic control object.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Total number of packets dropped.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
void nl_data_free(struct nl_data *data)
Free an abstract data object.