From 6d5b9ab8ae731050ab2242360429b8a192c27a4d Mon Sep 17 00:00:00 2001 From: Cong Wang Date: Wed, 28 Jan 2015 15:19:18 -0800 Subject: route/act: add skbedit action [thaller@redhat.com: changed whitespace and libnl-route.sym] Signed-off-by: Cong Wang Signed-off-by: Thomas Haller --- include/Makefile.am | 2 + include/linux-private/linux/tc_act/tc_skbedit.h | 46 ++++ include/netlink-private/types.h | 10 + include/netlink/route/act/skbedit.h | 37 +++ lib/Makefile.am | 1 + lib/route/act/skbedit.c | 290 ++++++++++++++++++++++++ libnl-route-3.sym | 8 + 7 files changed, 394 insertions(+) create mode 100644 include/linux-private/linux/tc_act/tc_skbedit.h create mode 100644 include/netlink/route/act/skbedit.h create mode 100644 lib/route/act/skbedit.c diff --git a/include/Makefile.am b/include/Makefile.am index 6592995..bf86ed1 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -33,6 +33,7 @@ nobase_libnlinclude_HEADERS = \ netlink/object.h \ netlink/route/action.h \ netlink/route/act/mirred.h \ + netlink/route/act/skbedit.h \ netlink/route/cls/ematch/cmp.h \ netlink/route/cls/ematch/meta.h \ netlink/route/cls/ematch/nbyte.h \ @@ -148,6 +149,7 @@ noinst_HEADERS = \ linux-private/linux/pkt_cls.h \ linux-private/linux/sock_diag.h \ linux-private/linux/tc_act/tc_mirred.h \ + linux-private/linux/tc_act/tc_skbedit.h \ linux-private/linux/pkt_sched.h \ linux-private/linux/rtnetlink.h \ linux-private/linux/snmp.h \ diff --git a/include/linux-private/linux/tc_act/tc_skbedit.h b/include/linux-private/linux/tc_act/tc_skbedit.h new file mode 100644 index 0000000..7a2e910 --- /dev/null +++ b/include/linux-private/linux/tc_act/tc_skbedit.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2008, Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + * + * Author: Alexander Duyck + */ + +#ifndef __LINUX_TC_SKBEDIT_H +#define __LINUX_TC_SKBEDIT_H + +#include + +#define TCA_ACT_SKBEDIT 11 + +#define SKBEDIT_F_PRIORITY 0x1 +#define SKBEDIT_F_QUEUE_MAPPING 0x2 +#define SKBEDIT_F_MARK 0x4 + +struct tc_skbedit { + tc_gen; +}; + +enum { + TCA_SKBEDIT_UNSPEC, + TCA_SKBEDIT_TM, + TCA_SKBEDIT_PARMS, + TCA_SKBEDIT_PRIORITY, + TCA_SKBEDIT_QUEUE_MAPPING, + TCA_SKBEDIT_MARK, + __TCA_SKBEDIT_MAX +}; +#define TCA_SKBEDIT_MAX (__TCA_SKBEDIT_MAX - 1) + +#endif diff --git a/include/netlink-private/types.h b/include/netlink-private/types.h index b4dc8ac..6d5cc7f 100644 --- a/include/netlink-private/types.h +++ b/include/netlink-private/types.h @@ -22,6 +22,7 @@ #include #include #include +#include #define NL_SOCK_BUFSIZE_SET (1<<0) #define NL_SOCK_PASSCRED (1<<1) @@ -520,6 +521,15 @@ struct rtnl_mirred struct tc_mirred m_parm; }; +struct rtnl_skbedit +{ + struct tc_skbedit s_parm; + uint32_t s_flags; + uint32_t s_mark; + uint32_t s_prio; + uint16_t s_queue_mapping; +}; + struct rtnl_u32 { uint32_t cu_divisor; diff --git a/include/netlink/route/act/skbedit.h b/include/netlink/route/act/skbedit.h new file mode 100644 index 0000000..69829e8 --- /dev/null +++ b/include/netlink/route/act/skbedit.h @@ -0,0 +1,37 @@ +/* + * netlink/route/act/skbedit.h skbedit action + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2015 Cong Wang + */ + +#ifndef NETLINK_SKBEDIT_H_ +#define NETLINK_SKBEDIT_H_ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern int rtnl_skbedit_set_action(struct rtnl_act *act, int action); +extern int rtnl_skbedit_get_action(struct rtnl_act *act); +extern int rtnl_skbedit_set_queue_mapping(struct rtnl_act *act, uint16_t index); +extern int rtnl_skbedit_get_queue_mapping(struct rtnl_act *act, uint16_t *index); +extern int rtnl_skbedit_set_mark(struct rtnl_act *act, uint32_t mark); +extern int rtnl_skbedit_get_mark(struct rtnl_act *act, uint32_t *mark); +extern int rtnl_skbedit_set_priority(struct rtnl_act *act, uint32_t prio); +extern int rtnl_skbedit_get_priority(struct rtnl_act *act, uint32_t *prio); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/Makefile.am b/lib/Makefile.am index f193c29..12a793a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -94,6 +94,7 @@ libnl_route_3_la_SOURCES = \ route/cls/cgroup.c \ \ route/act/mirred.c \ + route/act/skbedit.c \ \ route/cls/ematch.c \ route/cls/ematch/container.c route/cls/ematch/cmp.c \ diff --git a/lib/route/act/skbedit.c b/lib/route/act/skbedit.c new file mode 100644 index 0000000..d85265e --- /dev/null +++ b/lib/route/act/skbedit.c @@ -0,0 +1,290 @@ +/* + * lib/route/act/skbedit.c skbedit action + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2015 Cong Wang + */ + +/** + * @ingroup act + * @defgroup act_skbedit SKB Editing + * + * @{ + */ + +#include +#include +#include +#include +#include +#include +#include + +static struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { + [TCA_SKBEDIT_PARMS] = { .minlen = sizeof(struct tc_skbedit) }, + [TCA_SKBEDIT_PRIORITY] = { .type = NLA_U32 }, + [TCA_SKBEDIT_QUEUE_MAPPING] = { .type = NLA_U16 }, + [TCA_SKBEDIT_MARK] = { .type = NLA_U32 }, +}; + +static int skbedit_msg_parser(struct rtnl_tc *tc, void *data) +{ + struct rtnl_skbedit *u = data; + struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; + int err; + + err = tca_parse(tb, TCA_SKBEDIT_MAX, tc, skbedit_policy); + if (err < 0) + return err; + + if (!tb[TCA_SKBEDIT_PARMS]) + return -NLE_MISSING_ATTR; + + u->s_flags = 0; + if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { + u->s_flags |= SKBEDIT_F_PRIORITY; + u->s_prio = nla_get_u32(tb[TCA_SKBEDIT_PRIORITY]); + } + + if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { + u->s_flags |= SKBEDIT_F_QUEUE_MAPPING; + u->s_queue_mapping = nla_get_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING]); + } + + if (tb[TCA_SKBEDIT_MARK] != NULL) { + u->s_flags |= SKBEDIT_F_MARK; + u->s_mark = nla_get_u32(tb[TCA_SKBEDIT_MARK]); + } + + return 0; +} + +static void skbedit_free_data(struct rtnl_tc *tc, void *data) +{ +} + +static int skbedit_clone(void *_dst, void *_src) +{ + struct rtnl_skbedit *dst = _dst, *src = _src; + + memcpy(dst, src, sizeof(*src)); + return 0; +} + +static void skbedit_dump_line(struct rtnl_tc *tc, void *data, + struct nl_dump_params *p) +{ + struct rtnl_skbedit *u = data; + + if (!u) + return; + + if (u->s_flags & SKBEDIT_F_PRIORITY) + nl_dump(p, " priority %u", u->s_prio); + + if (u->s_flags & SKBEDIT_F_MARK) + nl_dump(p, " mark %u", u->s_mark); + + if (u->s_flags & SKBEDIT_F_QUEUE_MAPPING) + nl_dump(p, " queue_mapping %u", u->s_queue_mapping); + + switch(u->s_parm.action){ + case TC_ACT_UNSPEC: + nl_dump(p, " unspecified"); + break; + case TC_ACT_PIPE: + nl_dump(p, " pipe"); + break; + case TC_ACT_STOLEN: + nl_dump(p, " stolen"); + break; + case TC_ACT_SHOT: + nl_dump(p, " shot"); + break; + case TC_ACT_QUEUED: + nl_dump(p, " queued"); + break; + case TC_ACT_REPEAT: + nl_dump(p, " repeat"); + break; + } +} + +static void skbedit_dump_details(struct rtnl_tc *tc, void *data, + struct nl_dump_params *p) +{ +} + +static void skbedit_dump_stats(struct rtnl_tc *tc, void *data, + struct nl_dump_params *p) +{ + struct rtnl_skbedit *u = data; + + if (!u) + return; + /* TODO */ +} + + +static int skbedit_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg) +{ + struct rtnl_skbedit *u = data; + + if (!u) + return 0; + + NLA_PUT(msg, TCA_SKBEDIT_PARMS, sizeof(u->s_parm), &u->s_parm); + + if (u->s_flags & SKBEDIT_F_MARK) + NLA_PUT_U32(msg, TCA_SKBEDIT_MARK, u->s_mark); + + if (u->s_flags & SKBEDIT_F_PRIORITY) + NLA_PUT_U32(msg, TCA_SKBEDIT_PRIORITY, u->s_prio); + + if (u->s_flags & SKBEDIT_F_QUEUE_MAPPING) + NLA_PUT_U32(msg, TCA_SKBEDIT_QUEUE_MAPPING, u->s_queue_mapping); + + return 0; + +nla_put_failure: + return -NLE_NOMEM; +} + +/** + * @name Attribute Modifications + * @{ + */ + +int rtnl_skbedit_set_action(struct rtnl_act *act, int action) +{ + struct rtnl_skbedit *u; + + if (!(u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)))) + return -NLE_NOMEM; + + if (action > TC_ACT_REPEAT || action < TC_ACT_UNSPEC) + return -NLE_INVAL; + + u->s_parm.action = action; + return 0; +} + +int rtnl_skbedit_get_action(struct rtnl_act *act) +{ + struct rtnl_skbedit *u; + + if (!(u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)))) + return -NLE_NOMEM; + return u->s_parm.action; +} + +int rtnl_skbedit_set_queue_mapping(struct rtnl_act *act, uint16_t index) +{ + struct rtnl_skbedit *u; + + if (!(u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)))) + return -NLE_NOMEM; + + u->s_queue_mapping = index; + u->s_flags |= SKBEDIT_F_QUEUE_MAPPING; + return 0; +} + +int rtnl_skbedit_get_queue_mapping(struct rtnl_act *act, uint16_t *index) +{ + struct rtnl_skbedit *u; + + u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)); + if (!u) + return -NLE_NOMEM; + if (!(u->s_flags & SKBEDIT_F_QUEUE_MAPPING)) + return -NLE_NOATTR; + + *index = u->s_queue_mapping; + return 0; +} + +int rtnl_skbedit_set_mark(struct rtnl_act *act, uint32_t mark) +{ + struct rtnl_skbedit *u; + + if (!(u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)))) + return -NLE_NOMEM; + + u->s_mark = mark; + u->s_flags |= SKBEDIT_F_MARK; + return 0; +} + +int rtnl_skbedit_get_mark(struct rtnl_act *act, uint32_t *mark) +{ + struct rtnl_skbedit *u; + + u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)); + if (!u) + return -NLE_NOMEM; + if (!(u->s_flags & SKBEDIT_F_MARK)) + return -NLE_NOATTR; + + *mark = u->s_mark; + return 0; +} + +int rtnl_skbedit_set_priority(struct rtnl_act *act, uint32_t prio) +{ + struct rtnl_skbedit *u; + + if (!(u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)))) + return -NLE_NOMEM; + + u->s_prio = prio; + u->s_flags |= SKBEDIT_F_PRIORITY; + return 0; +} + +int rtnl_skbedit_get_priority(struct rtnl_act *act, uint32_t *prio) +{ + struct rtnl_skbedit *u; + + u = (struct rtnl_skbedit *) rtnl_tc_data(TC_CAST(act)); + if (!u) + return -NLE_NOMEM; + if (!(u->s_flags & SKBEDIT_F_PRIORITY)) + return -NLE_NOATTR; + + *prio = u->s_prio; + return 0; +} + +/** @} */ + +static struct rtnl_tc_ops skbedit_ops = { + .to_kind = "skbedit", + .to_type = RTNL_TC_TYPE_ACT, + .to_size = sizeof(struct rtnl_skbedit), + .to_msg_parser = skbedit_msg_parser, + .to_free_data = skbedit_free_data, + .to_clone = skbedit_clone, + .to_msg_fill = skbedit_msg_fill, + .to_dump = { + [NL_DUMP_LINE] = skbedit_dump_line, + [NL_DUMP_DETAILS] = skbedit_dump_details, + [NL_DUMP_STATS] = skbedit_dump_stats, + }, +}; + +static void __init skbedit_init(void) +{ + rtnl_tc_register(&skbedit_ops); +} + +static void __exit skbedit_exit(void) +{ + rtnl_tc_unregister(&skbedit_ops); +} + +/** @} */ diff --git a/libnl-route-3.sym b/libnl-route-3.sym index 7ce2e38..a0ca051 100644 --- a/libnl-route-3.sym +++ b/libnl-route-3.sym @@ -842,6 +842,14 @@ global: rtnl_link_inet6_str2addrgenmode; rtnl_qdisc_hfsc_get_defcls; rtnl_qdisc_hfsc_set_defcls; + rtnl_skbedit_get_action; + rtnl_skbedit_get_mark; + rtnl_skbedit_get_priority; + rtnl_skbedit_get_queue_mapping; + rtnl_skbedit_set_action; + rtnl_skbedit_set_mark; + rtnl_skbedit_set_priority; + rtnl_skbedit_set_queue_mapping; rtnl_u32_add_mark; rtnl_u32_del_mark; } libnl_3; -- cgit v0.12 From a23d16a7235e0c06c30848efcb626227dfc1f41a Mon Sep 17 00:00:00 2001 From: Cong Wang Date: Wed, 28 Jan 2015 15:19:19 -0800 Subject: route/act: add a test case for skbedit action Signed-off-by: Cong Wang Signed-off-by: Thomas Haller --- tests/test-u32-filter-with-actions.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/test-u32-filter-with-actions.c b/tests/test-u32-filter-with-actions.c index 55f913a..70bab4d 100644 --- a/tests/test-u32-filter-with-actions.c +++ b/tests/test-u32-filter-with-actions.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -101,7 +102,7 @@ static uint32_t get_u32_parse_handle(const char *cHandle) static int u32_add_filter_on_ht_with_hashmask(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio, uint32_t keyval, uint32_t keymask, int keyoff, int keyoffmask, - uint32_t htid, uint32_t htlink, uint32_t hmask, uint32_t hoffset, struct rtnl_act *act) + uint32_t htid, uint32_t htlink, uint32_t hmask, uint32_t hoffset, struct rtnl_act *act, struct rtnl_act *act2) { struct rtnl_cls *cls; int err; @@ -136,6 +137,8 @@ int u32_add_filter_on_ht_with_hashmask(struct nl_sock *sock, struct rtnl_link *r rtnl_u32_add_action(cls, act); + rtnl_u32_add_action(cls, act2); + if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) { printf("Can not add classifier: %s\n", nl_geterror(err)); @@ -236,7 +239,7 @@ int main(void) char chashlink[16]=""; int err; struct nl_cache *link_cache; - struct rtnl_act *act; + struct rtnl_act *act, *act2; if (!(sock = nl_socket_alloc())) { printf("Unable to allocate netlink socket\n"); @@ -354,7 +357,7 @@ int main(void) u32_add_filter_on_ht_with_hashmask(sock, link, 1, 0x0, 0x0, direction, 0, - 0, htlink, 0xff000000, direction, NULL); + 0, htlink, 0xff000000, direction, NULL, NULL); /* * For each first byte that we need to match we will create a new hash table @@ -373,16 +376,24 @@ int main(void) * previous steps. * */ - act = rtnl_act_alloc(); if (!act) { printf("rtnl_act_alloc() returns %p\n", act); return -1; - } - rtnl_tc_set_kind(TC_CAST(act), "mirred"); - rtnl_mirred_set_action(act, TCA_EGRESS_REDIR); - rtnl_mirred_set_policy(act, TC_ACT_STOLEN); - rtnl_mirred_set_ifindex(act, rtnl_link_name2i(link_cache, "eth1")); + } + rtnl_tc_set_kind(TC_CAST(act), "skbedit"); + rtnl_skbedit_set_queue_mapping(act, 4); + rtnl_skbedit_set_action(act, TC_ACT_PIPE); + + act2 = rtnl_act_alloc(); + if (!act2) { + printf("rtnl_act_alloc() returns %p\n", act2); + return -1; + } + rtnl_tc_set_kind(TC_CAST(act2), "mirred"); + rtnl_mirred_set_action(act2, TCA_EGRESS_REDIR); + rtnl_mirred_set_policy(act2, TC_ACT_STOLEN); + rtnl_mirred_set_ifindex(act2, rtnl_link_name2i(link_cache, "eth1")); // /8 check // 10.0.0.0/8 @@ -392,7 +403,7 @@ int main(void) u32_add_filter_on_ht_with_hashmask(sock, link, 1, 0x0a000000, 0xff000000, direction, 0, - htid, htlink, 0x00ff0000, direction, act); + htid, htlink, 0x00ff0000, direction, act, act2); rtnl_act_put(act); nl_socket_free(sock); -- cgit v0.12