15#include <netlink-private/netlink.h>
16#include <netlink-private/tc.h>
17#include <netlink/netlink.h>
18#include <netlink/attr.h>
19#include <netlink/utils.h>
20#include <netlink-private/route/tc-api.h>
21#include <netlink/route/classifier.h>
22#include <netlink/route/cls/u32.h>
23#include <netlink/route/action.h>
25#include "netlink-private/utils.h"
28#define U32_ATTR_DIVISOR 0x001
29#define U32_ATTR_HASH 0x002
30#define U32_ATTR_CLASSID 0x004
31#define U32_ATTR_LINK 0x008
32#define U32_ATTR_PCNT 0x010
33#define U32_ATTR_SELECTOR 0x020
34#define U32_ATTR_ACTION 0x040
35#define U32_ATTR_POLICE 0x080
36#define U32_ATTR_INDEV 0x100
37#define U32_ATTR_MARK 0x200
40static inline struct tc_u32_sel *u32_selector(
struct rtnl_u32 *u)
42 return (
struct tc_u32_sel *) u->cu_selector->d_data;
45static inline struct tc_u32_sel *u32_selector_alloc(
struct rtnl_u32 *u)
48 u->cu_selector =
nl_data_alloc(NULL,
sizeof(
struct tc_u32_sel));
50 return u32_selector(u);
53static inline struct tc_u32_mark *u32_mark_alloc(
struct rtnl_u32 *u)
58 return (
struct tc_u32_mark *) u->cu_mark->d_data;
61static struct nla_policy u32_policy[TCA_U32_MAX+1] = {
63 [TCA_U32_HASH] = { .type =
NLA_U32 },
64 [TCA_U32_CLASSID] = { .type =
NLA_U32 },
65 [TCA_U32_LINK] = { .type =
NLA_U32 },
68 [TCA_U32_SEL] = { .minlen =
sizeof(
struct tc_u32_sel) },
69 [TCA_U32_PCNT] = { .minlen =
sizeof(
struct tc_u32_pcnt) },
70 [TCA_U32_MARK] = { .minlen =
sizeof(
struct tc_u32_mark) }
73static int u32_msg_parser(
struct rtnl_tc *tc,
void *data)
75 struct rtnl_u32 *u = data;
76 struct nlattr *tb[TCA_U32_MAX + 1];
79 err = tca_parse(tb, TCA_U32_MAX, tc, u32_policy);
83 if (tb[TCA_U32_DIVISOR]) {
85 u->cu_mask |= U32_ATTR_DIVISOR;
88 if (tb[TCA_U32_SEL]) {
92 u->cu_mask |= U32_ATTR_SELECTOR;
95 if (tb[TCA_U32_MARK]) {
99 u->cu_mask |= U32_ATTR_MARK;
102 if (tb[TCA_U32_HASH]) {
104 u->cu_mask |= U32_ATTR_HASH;
107 if (tb[TCA_U32_CLASSID]) {
109 u->cu_mask |= U32_ATTR_CLASSID;
112 if (tb[TCA_U32_LINK]) {
114 u->cu_mask |= U32_ATTR_LINK;
117 if (tb[TCA_U32_ACT]) {
118 u->cu_mask |= U32_ATTR_ACTION;
119 err = rtnl_act_parse(&u->cu_act, tb[TCA_U32_ACT]);
124 if (tb[TCA_U32_POLICE]) {
128 u->cu_mask |= U32_ATTR_POLICE;
131 if (tb[TCA_U32_PCNT]) {
132 struct tc_u32_sel *sel;
135 if (!tb[TCA_U32_SEL]) {
136 err = -NLE_MISSING_ATTR;
140 sel = u->cu_selector->d_data;
141 pcnt_size =
sizeof(
struct tc_u32_pcnt) +
142 (sel->nkeys *
sizeof(uint64_t));
143 if (
nla_len(tb[TCA_U32_PCNT]) < pcnt_size) {
151 u->cu_mask |= U32_ATTR_PCNT;
154 if (tb[TCA_U32_INDEV]) {
155 nla_strlcpy(u->cu_indev, tb[TCA_U32_INDEV], IFNAMSIZ);
156 u->cu_mask |= U32_ATTR_INDEV;
167static void u32_free_data(
struct rtnl_tc *tc,
void *data)
169 struct rtnl_u32 *u = data;
172 rtnl_act_put_all(&u->cu_act);
179static int u32_clone(
void *_dst,
void *_src)
181 struct rtnl_u32 *dst = _dst, *src = _src;
182 _nl_auto_nl_data
struct nl_data *selector = NULL;
183 _nl_auto_nl_data
struct nl_data *mark = NULL;
184 _nl_auto_nl_data
struct nl_data *police = NULL;
185 _nl_auto_nl_data
struct nl_data *pcnt = NULL;
186 _nl_auto_nl_data
struct nl_data *opts = NULL;
187 _nl_auto_nl_data
struct nl_data *xstats = NULL;
188 _nl_auto_nl_data
struct nl_data *subdata = NULL;
189 _nl_auto_rtnl_act
struct rtnl_act *act = NULL;
192 dst->cu_selector = NULL;
195 dst->cu_police = NULL;
197 if (src->cu_selector) {
208 if (!(act = rtnl_act_alloc()))
211 if (src->cu_act->c_opts) {
216 if (src->cu_act->c_xstats) {
221 if (src->cu_act->c_subdata) {
227 if (src->cu_police) {
240 dst->cu_selector = _nl_steal_pointer(&selector);
243 dst->cu_mark = _nl_steal_pointer(&mark);
246 dst->cu_police = _nl_steal_pointer(&police);
249 dst->cu_pcnt = _nl_steal_pointer(&pcnt);
252 dst->cu_act = _nl_steal_pointer(&act);
255 nl_init_list_head(&dst->cu_act->ce_list);
258 dst->cu_act->c_opts = _nl_steal_pointer(&opts);
261 dst->cu_act->c_xstats = _nl_steal_pointer(&xstats);
264 dst->cu_act->c_subdata = _nl_steal_pointer(&subdata);
266 if (dst->cu_act->c_link) {
270 dst->cu_act->a_next = NULL;
276static void u32_dump_line(
struct rtnl_tc *tc,
void *data,
279 struct rtnl_u32 *u = data;
285 if (u->cu_mask & U32_ATTR_DIVISOR)
286 nl_dump(p,
" divisor %u", u->cu_divisor);
287 else if (u->cu_mask & U32_ATTR_CLASSID)
292static void print_selector(
struct nl_dump_params *p,
struct tc_u32_sel *sel,
296 struct tc_u32_key *key;
298 if (sel->hmask || sel->hoff) {
303 nl_dump(p,
" hash at %u & 0x%x", sel->hoff, sel->hmask);
306 if (sel->flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
307 nl_dump(p,
" offset at %u", sel->off);
309 if (sel->flags & TC_U32_VAROFFSET)
310 nl_dump(p,
" variable (at %u & 0x%x) >> %u",
311 sel->offoff, ntohs(sel->offmask), sel->offshift);
315 int flags = sel->flags;
318#define PRINT_FLAG(f) if (flags & TC_U32_##f) { \
319 flags &= ~TC_U32_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
321 PRINT_FLAG(TERMINAL);
323 PRINT_FLAG(VAROFFSET);
331 for (i = 0; i < sel->nkeys; i++) {
335 nl_dump_line(p,
" match key at %s%u ",
336 key->offmask ?
"nexthdr+" :
"", key->off);
339 nl_dump(p,
"[0x%u] ", key->offmask);
341 nl_dump(p,
"& 0x%08x == 0x%08x", ntohl(key->mask), ntohl(key->val));
344 (u->cu_mask & U32_ATTR_PCNT)) {
345 struct tc_u32_pcnt *pcnt = u->cu_pcnt->d_data;
348 (
long long unsigned)pcnt->kcnts[i]);
353static void u32_dump_details(
struct rtnl_tc *tc,
void *data,
356 struct rtnl_u32 *u = data;
357 struct tc_u32_sel *s = NULL;
358 struct tc_u32_mark *m;
363 if (!(u->cu_mask & U32_ATTR_SELECTOR)) {
366 s = u->cu_selector->d_data;
367 nl_dump(p,
"nkeys %u", s->nkeys);
370 if (!(u->cu_mask & U32_ATTR_MARK)) {
373 m = u->cu_mark->d_data;
374 nl_dump(p,
" mark 0x%u 0x%u", m->val, m->mask);
377 if (u->cu_mask & U32_ATTR_HASH)
378 nl_dump(p,
" ht key 0x%x hash 0x%u",
379 TC_U32_USERHTID(u->cu_hash), TC_U32_HASH(u->cu_hash));
381 if (u->cu_mask & U32_ATTR_LINK)
382 nl_dump(p,
" link %u", u->cu_link);
384 if (u->cu_mask & U32_ATTR_INDEV)
385 nl_dump(p,
" indev %s", u->cu_indev);
387 if (u->cu_mask & U32_ATTR_SELECTOR)
388 print_selector(p, s, u);
393static void u32_dump_stats(
struct rtnl_tc *tc,
void *data,
396 struct rtnl_u32 *u = data;
401 if (u->cu_mask & U32_ATTR_PCNT) {
402 struct tc_u32_pcnt *pc = u->cu_pcnt->d_data;
405 nl_dump_line(p,
" hit %8llu count %8llu\n",
406 (
long long unsigned)pc->rhit,
407 (
long long unsigned)pc->rcnt);
411static int u32_msg_fill(
struct rtnl_tc *tc,
void *data,
struct nl_msg *msg)
413 struct rtnl_u32 *u = data;
418 if (u->cu_mask & U32_ATTR_DIVISOR)
421 if (u->cu_mask & U32_ATTR_HASH)
424 if (u->cu_mask & U32_ATTR_CLASSID)
427 if (u->cu_mask & U32_ATTR_LINK)
430 if (u->cu_mask & U32_ATTR_SELECTOR)
433 if (u->cu_mask & U32_ATTR_MARK)
436 if (u->cu_mask & U32_ATTR_ACTION) {
439 err = rtnl_act_fill(msg, TCA_U32_ACT, u->cu_act);
444 if (u->cu_mask & U32_ATTR_POLICE)
447 if (u->cu_mask & U32_ATTR_INDEV)
461void rtnl_u32_set_handle(
struct rtnl_cls *cls,
int htid,
int hash,
464 uint32_t handle = (htid << 20) | (hash << 12) | nodeid;
469int rtnl_u32_set_classid(
struct rtnl_cls *cls, uint32_t classid)
476 u->cu_classid = classid;
477 u->cu_mask |= U32_ATTR_CLASSID;
482int rtnl_u32_get_classid(
struct rtnl_cls *cls, uint32_t *classid)
489 if (!(u->cu_mask & U32_ATTR_CLASSID))
492 *classid = u->cu_classid;
496int rtnl_u32_set_divisor(
struct rtnl_cls *cls, uint32_t divisor)
503 u->cu_divisor = divisor;
504 u->cu_mask |= U32_ATTR_DIVISOR;
508int rtnl_u32_set_link(
struct rtnl_cls *cls, uint32_t link)
516 u->cu_mask |= U32_ATTR_LINK;
520int rtnl_u32_set_hashtable(
struct rtnl_cls *cls, uint32_t ht)
528 u->cu_mask |= U32_ATTR_HASH;
532int rtnl_u32_set_hashmask(
struct rtnl_cls *cls, uint32_t hashmask, uint32_t offset)
535 struct tc_u32_sel *sel;
537 hashmask = htonl(hashmask);
542 sel = u32_selector_alloc(u);
546 sel->hmask = hashmask;
551int rtnl_u32_set_selector(
struct rtnl_cls *cls,
int offoff, uint32_t offmask,
char offshift, uint16_t off,
char flags)
554 struct tc_u32_sel *sel;
556 offmask = ntohs(offmask);
561 sel = u32_selector_alloc(u);
565 sel->offoff = offoff;
566 sel->offmask = offmask;
567 sel->offshift = offshift;
568 sel->flags |= TC_U32_VAROFFSET;
574int rtnl_u32_set_cls_terminal(
struct rtnl_cls *cls)
577 struct tc_u32_sel *sel;
582 sel = u32_selector_alloc(u);
586 sel->flags |= TC_U32_TERMINAL;
590int rtnl_u32_add_action(
struct rtnl_cls *cls,
struct rtnl_act *act)
601 u->cu_mask |= U32_ATTR_ACTION;
602 if ((err = rtnl_act_append(&u->cu_act, act)))
610struct rtnl_act* rtnl_u32_get_action(
struct rtnl_cls *cls)
617 if (!(u->cu_mask & U32_ATTR_ACTION))
623int rtnl_u32_del_action(
struct rtnl_cls *cls,
struct rtnl_act *act)
634 if (!(u->cu_mask & U32_ATTR_ACTION))
637 ret = rtnl_act_remove(&u->cu_act, act);
642 u->cu_mask &= ~U32_ATTR_ACTION;
653int rtnl_u32_set_flags(
struct rtnl_cls *cls,
int flags)
655 struct tc_u32_sel *sel;
661 sel = u32_selector_alloc(u);
666 u->cu_mask |= U32_ATTR_SELECTOR;
686 int off,
int offmask)
688 struct tc_u32_sel *sel;
695 sel = u32_selector_alloc(u);
699 if (sel->nkeys == UCHAR_MAX)
702 err =
nl_data_append(u->cu_selector, NULL,
sizeof(
struct tc_u32_key));
707 sel = u32_selector(u);
709 sel->keys[sel->nkeys].mask = mask;
710 sel->keys[sel->nkeys].val = val & mask;
711 sel->keys[sel->nkeys].off = off;
712 sel->keys[sel->nkeys].offmask = offmask;
714 u->cu_mask |= U32_ATTR_SELECTOR;
719int rtnl_u32_add_mark(
struct rtnl_cls *cls, uint32_t val, uint32_t mask)
721 struct tc_u32_mark *mark;
727 mark = u32_mark_alloc(u);
734 u->cu_mask |= U32_ATTR_MARK;
739int rtnl_u32_del_mark(
struct rtnl_cls *cls)
749 if (!(u->cu_mask & U32_ATTR_MARK))
754 u->cu_mask &= ~U32_ATTR_MARK;
771 uint32_t *val, uint32_t *mask,
int *off,
int *offmask)
773 struct tc_u32_sel *sel;
779 if (!(u->cu_mask & U32_ATTR_SELECTOR))
782 sel = u32_selector(u);
783 if (index >= sel->nkeys)
786 *mask = sel->keys[index].mask;
787 *val = sel->keys[index].val;
788 *off = sel->keys[index].off;
789 *offmask = sel->keys[index].offmask;
794int rtnl_u32_add_key_uint8(
struct rtnl_cls *cls, uint8_t val, uint8_t mask,
795 int off,
int offmask)
797 int shift = 24 - 8 * (off & 3);
800 htonl((uint32_t)mask << shift),
814 int off,
int offmask)
816 int shift = ((off & 3) == 0 ? 16 : 0);
821 htonl((uint32_t)mask << shift),
835 int off,
int offmask)
841int rtnl_u32_add_key_in_addr(
struct rtnl_cls *cls,
const struct in_addr *addr,
842 uint8_t bitmask,
int off,
int offmask)
844 uint32_t mask = 0xFFFFFFFF << (32 - bitmask);
848int rtnl_u32_add_key_in6_addr(
struct rtnl_cls *cls,
const struct in6_addr *addr,
849 uint8_t bitmask,
int off,
int offmask)
853 for (i = 1; i <= 4; i++) {
854 if (32 * i - bitmask <= 0) {
856 0xFFFFFFFF, off+4*(i-1), offmask)) < 0)
859 else if (32 * i - bitmask < 32) {
860 uint32_t mask = 0xFFFFFFFF << (32 * i - bitmask);
862 htonl(mask), off+4*(i-1), offmask)) < 0)
873static struct rtnl_tc_ops u32_ops = {
875 .to_type = RTNL_TC_TYPE_CLS,
876 .to_size =
sizeof(
struct rtnl_u32),
877 .to_msg_parser = u32_msg_parser,
878 .to_free_data = u32_free_data,
879 .to_clone = u32_clone,
880 .to_msg_fill = u32_msg_fill,
888static void __init u32_init(
void)
893static void __exit u32_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_DATA(msg, attrtype, data)
Add abstract data attribute to netlink message.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
int nla_len(const struct nlattr *nla)
Return length of the payload .
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
@ NLA_STRING
NUL terminated character string.
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
int rtnl_u32_get_key(struct rtnl_cls *cls, uint8_t index, uint32_t *val, uint32_t *mask, int *off, int *offmask)
Get the 32-bit key from the selector.
int rtnl_u32_add_key(struct rtnl_cls *cls, uint32_t val, uint32_t mask, int off, int offmask)
Append new 32-bit key to the selector.
int rtnl_u32_add_key_uint32(struct rtnl_cls *cls, uint32_t val, uint32_t mask, int off, int offmask)
Append new selector key to match a 32-bit number.
int rtnl_u32_add_key_uint16(struct rtnl_cls *cls, uint16_t val, uint16_t mask, int off, int offmask)
Append new selector key to match a 16-bit number.
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.
int nl_data_append(struct nl_data *data, const void *buf, size_t size)
Append data to 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_object_get(struct nl_object *obj)
Acquire a reference on a object.
void * rtnl_tc_data_peek(struct rtnl_tc *tc)
Returns the private data of the 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_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 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.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.