Skip to content

Commit

Permalink
route:tc: allow to set chain index for tc objects
Browse files Browse the repository at this point in the history
This is useful when one wants to chain filters.

Signed-off-by: Volodymyr Bendiuga <[email protected]>
  • Loading branch information
voldymyr authored and thom311 committed Oct 10, 2018
1 parent 1bffe3c commit c9b2817
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/netlink-private/tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ extern "C" {
#define TCA_ATTR_MPU 0x0800
#define TCA_ATTR_OVERHEAD 0x1000
#define TCA_ATTR_LINKTYPE 0x2000
#define TCA_ATTR_MAX TCA_ATTR_LINKTYPE
#define TCA_ATTR_CHAIN 0x4000
#define TCA_ATTR_MAX TCA_ATTR_CHAIN

extern int tca_parse(struct nlattr **, int, struct rtnl_tc *,
const struct nla_policy *);
Expand Down
3 changes: 2 additions & 1 deletion include/netlink-private/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ struct rtnl_tstats
struct nl_data * pre ##_subdata; \
struct rtnl_link * pre ##_link; \
struct rtnl_tc_ops * pre ##_ops; \
enum rtnl_tc_type pre ##_type
enum rtnl_tc_type pre ##_type; \
uint32_t pre ##_chain

struct rtnl_tc
{
Expand Down
2 changes: 2 additions & 0 deletions include/netlink/route/tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ extern char * rtnl_tc_handle2str(uint32_t, char *, size_t);
extern int rtnl_tc_str2handle(const char *, uint32_t *);
extern int rtnl_classid_generate(const char *, uint32_t *,
uint32_t);
extern void rtnl_tc_set_chain(struct rtnl_tc *, uint32_t);
extern uint32_t rtnl_tc_get_chain(struct rtnl_tc *);

#ifdef __cplusplus
}
Expand Down
33 changes: 33 additions & 0 deletions lib/route/tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static struct rtnl_tc_type_ops *tc_type_ops[__RTNL_TC_TYPE_MAX];
static struct nla_policy tc_policy[TCA_MAX+1] = {
[TCA_KIND] = { .type = NLA_STRING,
.maxlen = TCKINDSIZ },
[TCA_CHAIN] = { .type = NLA_U32 },
[TCA_STATS] = { .minlen = sizeof(struct tc_stats) },
[TCA_STATS2] = { .type = NLA_NESTED },
};
Expand Down Expand Up @@ -79,6 +80,9 @@ int rtnl_tc_msg_parse(struct nlmsghdr *n, struct rtnl_tc *tc)
nla_strlcpy(kind, tb[TCA_KIND], sizeof(kind));
rtnl_tc_set_kind(tc, kind);

if (tb[TCA_CHAIN])
rtnl_tc_set_chain(tc, nla_get_u32(tb[TCA_CHAIN]));

tm = nlmsg_data(n);
tc->tc_family = tm->tcm_family;
tc->tc_ifindex = tm->tcm_ifindex;
Expand Down Expand Up @@ -216,6 +220,9 @@ int rtnl_tc_msg_build(struct rtnl_tc *tc, int type, int flags,
if (tc->ce_mask & TCA_ATTR_KIND)
NLA_PUT_STRING(msg, TCA_KIND, tc->tc_kind);

if (tc->ce_mask & TCA_ATTR_CHAIN)
NLA_PUT_U32(msg, TCA_CHAIN, tc->tc_chain);

ops = rtnl_tc_get_ops(tc);
if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
struct nlattr *opts;
Expand Down Expand Up @@ -560,6 +567,32 @@ uint64_t rtnl_tc_get_stat(struct rtnl_tc *tc, enum rtnl_tc_stat id)
return tc->tc_stats[id];
}

/**
* Set the chain index of a traffic control object
* @arg tc traffic control object
* @arg chain chain index of traffic control object
*
*/
void rtnl_tc_set_chain(struct rtnl_tc *tc, uint32_t chain)
{
tc->tc_chain = chain;
tc->ce_mask |= TCA_ATTR_CHAIN;
}

/**
* Return chain index of traffic control object
* @arg tc traffic control object
*
* @return chain index of traffic control object or 0 if not set.
*/
uint32_t rtnl_tc_get_chain(struct rtnl_tc *tc)
{
if (tc->ce_mask & TCA_ATTR_CHAIN)
return tc->tc_chain;
else
return 0;
}

/** @} */

/**
Expand Down
2 changes: 2 additions & 0 deletions libnl-route-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1125,4 +1125,6 @@ global:
rtnl_rule_set_protocol;
rtnl_rule_set_sport;
rtnl_rule_set_sport_range;
rtnl_tc_get_chain;
rtnl_tc_set_chain;
} libnl_3_4;

0 comments on commit c9b2817

Please sign in to comment.