[PATCH 2/5] add mirred tc action
Cong Wang
xiyou.wangcong at gmail.com
Fri Nov 8 13:47:50 EST 2013
Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>
---
include/linux/pkt_act.h | 32 ++++++
include/netlink/route/act/mirred.h | 31 ++++++
include/netlink/route/action.h | 1 +
lib/Makefile.am | 2 +
lib/route/act/mirred.c | 214 +++++++++++++++++++++++++++++++++++++
5 files changed, 280 insertions(+)
create mode 100644 include/linux/pkt_act.h
create mode 100644 include/netlink/route/act/mirred.h
create mode 100644 lib/route/act/mirred.c
diff --git a/include/linux/pkt_act.h b/include/linux/pkt_act.h
new file mode 100644
index 0000000..ba8c860
--- /dev/null
+++ b/include/linux/pkt_act.h
@@ -0,0 +1,32 @@
+#ifndef __LINUX_PKT_ACT_H
+#define __LINUX_PKT_ACT_H
+
+#include <linux/types.h>
+#include <linux/pkt_cls.h>
+
+#define TCA_ACT_MIRRED 8
+#define TCA_EGRESS_REDIR 1 /* packet redirect to EGRESS*/
+#define TCA_EGRESS_MIRROR 2 /* mirror packet to EGRESS */
+#define TCA_INGRESS_REDIR 3 /* packet redirect to INGRESS*/
+#define TCA_INGRESS_MIRROR 4 /* mirror packet to INGRESS */
+
+struct tc_mirred {
+ tc_gen;
+ int eaction; /* one of IN/EGRESS_MIRROR/REDIR */
+ __u32 ifindex; /* ifindex of egress port */
+};
+
+enum {
+ TCA_MIRRED_UNSPEC,
+ TCA_MIRRED_TM,
+ TCA_MIRRED_PARMS,
+ __TCA_MIRRED_MAX
+};
+#define TCA_MIRRED_MAX (__TCA_MIRRED_MAX - 1)
+
+struct rtnl_mirred
+{
+ struct tc_mirred m_parm;
+};
+
+#endif
diff --git a/include/netlink/route/act/mirred.h b/include/netlink/route/act/mirred.h
new file mode 100644
index 0000000..20a777c
--- /dev/null
+++ b/include/netlink/route/act/mirred.h
@@ -0,0 +1,31 @@
+/*
+ * netlink/route/cls/mirred.h mirred 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) 2013 Cong Wang <xiyou.wangcong at gmail.com>
+ */
+
+#ifndef NETLINK_MIRRED_H_
+#define NETLINK_MIRRED_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/action.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int rtnl_mirred_set_action(struct rtnl_act *, int);
+extern int rtnl_mirred_set_index(struct rtnl_act *, uint32_t);
+extern int rtnl_mirred_set_policy(struct rtnl_act *, int);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/netlink/route/action.h b/include/netlink/route/action.h
index be52767..8e265ea 100644
--- a/include/netlink/route/action.h
+++ b/include/netlink/route/action.h
@@ -16,6 +16,7 @@
#include <netlink/cache.h>
#include <netlink/route/tc.h>
#include <netlink/utils.h>
+#include <linux/pkt_act.h>
#ifdef __cplusplus
extern "C" {
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 949d3ac..5b1a002 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -68,6 +68,8 @@ libnl_route_3_la_SOURCES = \
route/cls/fw.c route/cls/police.c route/cls/u32.c route/cls/basic.c \
route/cls/cgroup.c \
\
+ route/act/mirred.c \
+ \
route/cls/ematch.c \
route/cls/ematch/container.c route/cls/ematch/cmp.c \
route/cls/ematch/nbyte.c route/cls/ematch/text.c \
diff --git a/lib/route/act/mirred.c b/lib/route/act/mirred.c
new file mode 100644
index 0000000..43b223f
--- /dev/null
+++ b/lib/route/act/mirred.c
@@ -0,0 +1,214 @@
+/*
+ * lib/route/cls/mirred.c mirred 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) 2013 Cong Wang <xiyou.wangcong at gmail.com>
+ */
+
+/**
+ * @ingroup act
+ * @defgroup act_mirred Mirror and Redirect
+ *
+ * @{
+ */
+
+#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/mirred.h>
+
+static struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
+ [TCA_MIRRED_PARMS] = { .minlen = sizeof(struct tc_mirred) },
+};
+
+static int mirred_msg_parser(struct rtnl_tc *tc, void *data)
+{
+ struct rtnl_mirred *u = data;
+ struct nlattr *tb[TCA_MIRRED_MAX + 1];
+ int err;
+
+ err = tca_parse(tb, TCA_MIRRED_MAX, tc, mirred_policy);
+ if (err < 0)
+ return err;
+
+ if (!tb[TCA_MIRRED_PARMS])
+ return -NLE_MISSING_ATTR;
+
+ nla_memcpy(&u->m_parm, tb[TCA_MIRRED_PARMS], sizeof(u->m_parm));
+ return 0;
+}
+
+static void mirred_free_data(struct rtnl_tc *tc, void *data)
+{
+}
+
+static int mirred_clone(void *_dst, void *_src)
+{
+ struct rtnl_mirred *dst = _dst, *src = _src;
+
+ memcpy(&dst->m_parm, &src->m_parm, sizeof(src->m_parm));
+ return 0;
+}
+
+static void mirred_dump_line(struct rtnl_tc *tc, void *data,
+ struct nl_dump_params *p)
+{
+ struct rtnl_mirred *u = data;
+ if (!u)
+ return;
+
+ nl_dump(p, " index %u", u->m_parm.ifindex);
+
+ if (u->m_parm.eaction == TCA_EGRESS_MIRROR)
+ nl_dump(p, " egress mirror");
+ else if (u->m_parm.eaction == TCA_EGRESS_REDIR)
+ nl_dump(p, " egress redirect");
+
+ switch(u->m_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 mirred_dump_details(struct rtnl_tc *tc, void *data,
+ struct nl_dump_params *p)
+{
+}
+
+static void mirred_dump_stats(struct rtnl_tc *tc, void *data,
+ struct nl_dump_params *p)
+{
+ struct rtnl_mirred *u = data;
+
+ if (!u)
+ return;
+ /* TODO */
+}
+
+
+static int mirred_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
+{
+ struct rtnl_mirred *u = data;
+
+ if (!u)
+ return 0;
+
+ NLA_PUT(msg, TCA_MIRRED_PARMS, sizeof(u->m_parm), &u->m_parm);
+ return 0;
+
+nla_put_failure:
+ return -NLE_NOMEM;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+int rtnl_mirred_set_action(struct rtnl_act *act, int action)
+{
+ struct rtnl_mirred *u;
+
+ if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
+ return -NLE_NOMEM;
+
+ if (action > TCA_INGRESS_MIRROR || action < TCA_EGRESS_REDIR)
+ return -NLE_INVAL;
+
+ switch (action) {
+ case TCA_EGRESS_MIRROR:
+ case TCA_EGRESS_REDIR:
+ u->m_parm.eaction = action;
+ break;
+ case TCA_INGRESS_REDIR:
+ case TCA_INGRESS_MIRROR:
+ default:
+ return NLE_OPNOTSUPP;
+ }
+ return 0;
+}
+
+int rtnl_mirred_set_index(struct rtnl_act *act, uint32_t link)
+{
+ struct rtnl_mirred *u;
+
+ if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
+ return -NLE_NOMEM;
+
+ u->m_parm.ifindex = link;
+ return 0;
+}
+
+int rtnl_mirred_set_policy(struct rtnl_act *act, int policy)
+{
+ struct rtnl_mirred *u;
+
+ if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
+ return -NLE_NOMEM;
+
+ if (policy > TC_POLICE_PIPE || policy < TC_POLICE_OK)
+ return -NLE_INVAL;
+
+ switch (u->m_parm.eaction) {
+ case TCA_EGRESS_MIRROR:
+ case TCA_EGRESS_REDIR:
+ u->m_parm.action = policy;
+ break;
+ case TCA_INGRESS_REDIR:
+ case TCA_INGRESS_MIRROR:
+ default:
+ return NLE_OPNOTSUPP;
+ }
+ return 0;
+}
+/** @} */
+
+static struct rtnl_tc_ops mirred_ops = {
+ .to_kind = "mirred",
+ .to_type = RTNL_TC_TYPE_ACT,
+ .to_size = sizeof(struct rtnl_mirred),
+ .to_msg_parser = mirred_msg_parser,
+ .to_free_data = mirred_free_data,
+ .to_clone = mirred_clone,
+ .to_msg_fill = mirred_msg_fill,
+ .to_dump = {
+ [NL_DUMP_LINE] = mirred_dump_line,
+ [NL_DUMP_DETAILS] = mirred_dump_details,
+ [NL_DUMP_STATS] = mirred_dump_stats,
+ },
+};
+
+static void __init mirred_init(void)
+{
+ rtnl_tc_register(&mirred_ops);
+}
+
+static void __exit mirred_exit(void)
+{
+ rtnl_tc_unregister(&mirred_ops);
+}
+
+/** @} */
--
1.7.11.7
More information about the libnl
mailing list