Skip to content

Commit

Permalink
flower-New-API-for-setting-ip_proto
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Zetterlund authored and Johan Zetterlund committed Jan 12, 2024
1 parent 87b880a commit 062546e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/netlink-private/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ struct rtnl_flower
in_addr_t cf_ipv4_dst_mask;
uint8_t cf_ip_dscp;
uint8_t cf_ip_dscp_mask;
uint8_t cf_ip_proto;
};

struct rtnl_cgroup
Expand Down
3 changes: 3 additions & 0 deletions include/netlink/route/cls/flower.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ extern int rtnl_flower_set_ipv4_dst(struct rtnl_cls *, in_addr_t, in_addr_t);
extern int rtnl_flower_get_ipv4_dst(struct rtnl_cls *, in_addr_t *,
in_addr_t *);

extern int rtnl_flower_set_ip_proto(struct rtnl_cls *, uint8_t);
extern int rtnl_flower_get_ip_proto(struct rtnl_cls *, uint8_t *);

extern int rtnl_flower_set_flags(struct rtnl_cls *, int);

extern int rtnl_flower_append_action(struct rtnl_cls *, struct rtnl_act *);
Expand Down
53 changes: 53 additions & 0 deletions lib/route/cls/flower.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define FLOWER_ATTR_IPV4_SRC_MASK (1 << 13)
#define FLOWER_ATTR_IPV4_DST (1 << 14)
#define FLOWER_ATTR_IPV4_DST_MASK (1 << 15)
#define FLOWER_ATTR_IP_PROTO (1 << 16)
/** @endcond */

#define FLOWER_DSCP_MAX 0xe0
Expand All @@ -54,6 +55,7 @@ static struct nla_policy flower_policy[TCA_FLOWER_MAX + 1] = {
[TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
[TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
[TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
[TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
};

static int flower_msg_parser(struct rtnl_tc *tc, void *data)
Expand Down Expand Up @@ -151,6 +153,11 @@ static int flower_msg_parser(struct rtnl_tc *tc, void *data)
f->cf_mask |= FLOWER_ATTR_IPV4_DST_MASK;
}

if (tb[TCA_FLOWER_KEY_IP_PROTO]) {
f->cf_ip_proto = nla_get_u8(tb[TCA_FLOWER_KEY_IP_PROTO]);
f->cf_mask |= FLOWER_ATTR_IP_PROTO;
}

return 0;
}

Expand Down Expand Up @@ -215,6 +222,9 @@ static int flower_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
NLA_PUT_U32(msg, TCA_FLOWER_KEY_IPV4_DST_MASK,
f->cf_ipv4_dst_mask);

if (f->cf_mask & FLOWER_ATTR_IP_PROTO)
NLA_PUT_U8(msg, TCA_FLOWER_KEY_IP_PROTO, f->cf_ip_proto);

return 0;

nla_put_failure:
Expand Down Expand Up @@ -330,6 +340,9 @@ static void flower_dump_details(struct rtnl_tc *tc, void *data,
inet_ntop(AF_INET, &f->cf_ipv4_dst_mask, mask_str, sizeof(mask_str));
nl_dump(p, "IPv4 dst %s mask %s\n", addr_str, mask_str);
}

if (f->cf_mask & FLOWER_ATTR_IP_PROTO)
nl_dump(p, " protocol %u", f->cf_ip_proto);
}

/**
Expand Down Expand Up @@ -778,6 +791,46 @@ int rtnl_flower_get_ipv4_dst(struct rtnl_cls *cls, in_addr_t *out_addr,
return 0;
}

/**
* Set ip protocol for flower classifier
* @arg cls Flower classifier.
* @arg ip_proto ip protocol (tcp, udp, icmp ...)
* @return 0 on success or a negative error code.
*/
int rtnl_flower_set_ip_proto(struct rtnl_cls *cls, uint8_t ip_proto)
{
struct rtnl_flower *f;

if (!(f = rtnl_tc_data(TC_CAST(cls))))
return -NLE_NOMEM;

f->cf_ip_proto = ip_proto;
f->cf_mask |= FLOWER_ATTR_IP_PROTO;

return 0;
}

/**
* Get ip protocol for flower classifier
* @arg cls Flower classifier.
* @arg proto ip protocol
* @return 0 on success or a negative error code.
*/
int rtnl_flower_get_ip_proto(struct rtnl_cls *cls, uint8_t *ip_proto)
{
struct rtnl_flower *f;

if (!(f = rtnl_tc_data_peek(TC_CAST(cls))))
return -NLE_INVAL;

if (!(f->cf_mask & FLOWER_ATTR_IP_PROTO))
return -NLE_MISSING_ATTR;

*ip_proto = f->cf_ip_proto;

return 0;
}

/**
* Append action for flower classifier
* @arg cls Flower classifier.
Expand Down
2 changes: 2 additions & 0 deletions libnl-route-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,8 @@ global:
rtnl_link_bridge_set_boolopt;
rtnl_tc_set_ingress_block;
rtnl_tc_set_egress_block;
rtnl_flower_set_ip_proto;
rtnl_flower_get_ip_proto;
rtnl_nat_get_action;
rtnl_nat_get_flags;
rtnl_nat_get_mask;
Expand Down

0 comments on commit 062546e

Please sign in to comment.