[PATCH 1/2] act: add skbedit action
Cong Wang
xiyou.wangcong at gmail.com
Wed Jan 28 15:19:18 PST 2015
Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>
---
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 ++++++++++++++++++++++++
6 files changed, 386 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 <alexander.h.duyck at intel.com>
+ */
+
+#ifndef __LINUX_TC_SKBEDIT_H
+#define __LINUX_TC_SKBEDIT_H
+
+#include <linux/pkt_cls.h>
+
+#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 <netlink/netfilter/ct.h>
#include <netlink-private/route/tc-api.h>
#include <linux/tc_act/tc_mirred.h>
+#include <linux/tc_act/tc_skbedit.h>
#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 <xiyou.wangcong at gmail.com>
+ */
+
+#ifndef NETLINK_SKBEDIT_H_
+#define NETLINK_SKBEDIT_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/action.h>
+#include <linux/tc_act/tc_skbedit.h>
+
+#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 f7e5a93..62c1e2c 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -70,6 +70,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..f3d011b
--- /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 <xiyou.wangcong at gmail.com>
+ */
+
+/**
+ * @ingroup act
+ * @defgroup act_skbedit SKB Editing
+ *
+ * @{
+ */
+
+#include <netlink-private/netlink.h>
+#include <netlink-private/tc.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink-private/route/tc-api.h>
+#include <netlink/route/act/skbedit.h>
+
+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);
+}
+
+/** @} */
--
1.8.3.1
More information about the libnl
mailing list