[PATCH] route/act: add gact tc action

Sushma Sitaram sushma.sitaram at intel.com
Thu Nov 10 14:47:53 PST 2016


Signed-off-by: Sushma Sitaram <sushma.sitaram at intel.com>
---
 include/Makefile.am                          |    2 
 include/linux-private/linux/tc_act/tc_gact.h |   33 +++++
 include/netlink-private/types.h              |    6 +
 include/netlink/route/act/gact.h             |   31 ++++
 lib/Makefile.am                              |    1 
 lib/route/act/gact.c                         |  183 ++++++++++++++++++++++++++
 libnl-route-3.sym                            |    2 
 7 files changed, 258 insertions(+)
 create mode 100644 include/linux-private/linux/tc_act/tc_gact.h
 create mode 100644 include/netlink/route/act/gact.h
 create mode 100644 lib/route/act/gact.c

diff --git a/include/Makefile.am b/include/Makefile.am
index fafb90a..3e6efc1 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -34,6 +34,7 @@ nobase_libnlinclude_HEADERS = \
 	netlink/route/action.h \
 	netlink/route/act/mirred.h \
 	netlink/route/act/skbedit.h \
+	netlink/route/act/gact.h \
 	netlink/route/cls/ematch/cmp.h \
 	netlink/route/cls/ematch/meta.h \
 	netlink/route/cls/ematch/nbyte.h \
@@ -156,6 +157,7 @@ noinst_HEADERS = \
 	linux-private/linux/socket.h \
 	linux-private/linux/tc_act/tc_mirred.h \
 	linux-private/linux/tc_act/tc_skbedit.h \
+	linux-private/linux/tc_act/tc_gact.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_gact.h b/include/linux-private/linux/tc_act/tc_gact.h
new file mode 100644
index 0000000..9986a56
--- /dev/null
+++ b/include/linux-private/linux/tc_act/tc_gact.h
@@ -0,0 +1,33 @@
+#ifndef __LINUX_TC_GACT_H
+#define __LINUX_TC_GACT_H
+
+#include <linux/types.h>
+#include <linux/pkt_cls.h>
+
+#define TCA_ACT_GACT 5
+struct tc_gact {
+	tc_gen;
+
+};
+
+struct tc_gact_p {
+#define PGACT_NONE              0
+#define PGACT_NETRAND           1
+#define PGACT_DETERM            2
+#define MAX_RAND                (PGACT_DETERM + 1 )
+	__u16                 ptype;
+	__u16                 pval;
+	int                   paction;
+};
+
+enum {
+	TCA_GACT_UNSPEC,
+	TCA_GACT_TM,
+	TCA_GACT_PARMS,
+	TCA_GACT_PROB,
+	TCA_GACT_PAD,
+	__TCA_GACT_MAX
+};
+#define TCA_GACT_MAX (__TCA_GACT_MAX - 1)
+
+#endif
diff --git a/include/netlink-private/types.h b/include/netlink-private/types.h
index 23b9aba..ca34cc0 100644
--- a/include/netlink-private/types.h
+++ b/include/netlink-private/types.h
@@ -24,6 +24,7 @@
 #include <netlink-private/route/tc-api.h>
 #include <linux/tc_act/tc_mirred.h>
 #include <linux/tc_act/tc_skbedit.h>
+#include <linux/tc_act/tc_gact.h>
 
 #define NL_SOCK_PASSCRED	(1<<1)
 #define NL_OWN_PORT		(1<<2)
@@ -533,6 +534,11 @@ struct rtnl_skbedit
 	uint16_t	  s_queue_mapping;
 };
 
+struct rtnl_gact
+{
+	struct tc_gact g_parm;
+};
+
 struct rtnl_u32
 {
 	uint32_t		cu_divisor;
diff --git a/include/netlink/route/act/gact.h b/include/netlink/route/act/gact.h
new file mode 100644
index 0000000..9538711
--- /dev/null
+++ b/include/netlink/route/act/gact.h
@@ -0,0 +1,31 @@
+/*
+ * netlink/route/act/gact.h	gact 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) 2016 Sushma Sitaram <sushma.sitaram at intel.com>
+ */
+
+#ifndef NETLINK_GACT_H_
+#define NETLINK_GACT_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/action.h>
+#include <linux/tc_act/tc_gact.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int rtnl_gact_set_action(struct rtnl_act *act, int action);
+extern int rtnl_gact_get_action(struct rtnl_act *act);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 21d28ca..c266dd9 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -99,6 +99,7 @@ libnl_route_3_la_SOURCES = \
 	\
 	route/act/mirred.c \
 	route/act/skbedit.c \
+	route/act/gact.c \
 	\
 	route/cls/ematch.c \
 	route/cls/ematch/container.c route/cls/ematch/cmp.c \
diff --git a/lib/route/act/gact.c b/lib/route/act/gact.c
new file mode 100644
index 0000000..e37ef9f
--- /dev/null
+++ b/lib/route/act/gact.c
@@ -0,0 +1,183 @@
+/*
+ * lib/route/act/gact.c		gact 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) 2016 Sushma Sitaram <sushma.sitaram at intel.com>
+ */
+
+/**
+ * @ingroup act
+ * @defgroup act_gact GACT 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/gact.h>
+
+static struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
+	[TCA_GACT_PARMS]             = { .minlen = sizeof(struct tc_gact) },
+};
+
+static int gact_msg_parser(struct rtnl_tc *tc, void *data)
+{
+	struct rtnl_gact *u = data;
+	struct nlattr *tb[TCA_GACT_MAX + 1];
+	int err;
+
+	err = tca_parse(tb, TCA_GACT_MAX, tc, gact_policy);
+	if (err < 0)
+		return err;
+
+	if (!tb[TCA_GACT_PARMS])
+		return -NLE_MISSING_ATTR;
+
+	nla_memcpy(&u->g_parm, tb[TCA_GACT_PARMS], sizeof(u->g_parm));
+
+	return 0;
+}
+
+static void gact_free_data(struct rtnl_tc *tc, void *data)
+{
+}
+
+static int gact_clone(void *_dst, void *_src)
+{
+	struct rtnl_gact *dst = _dst, *src = _src;
+
+	memcpy(&dst->g_parm, &src->g_parm, sizeof(src->g_parm));
+	return 0;
+}
+
+static void gact_dump_line(struct rtnl_tc *tc, void *data,
+			  struct nl_dump_params *p)
+{
+	struct rtnl_gact *u = data;
+
+	if (!u)
+		return;
+
+	switch(u->g_parm.action){
+	case TC_ACT_UNSPEC:
+		nl_dump(p, " continue");
+		break;
+	case TC_ACT_SHOT:
+		nl_dump(p, " drop");
+		break;
+	case TC_ACT_RECLASSIFY:
+		nl_dump(p, " reclassify");
+		break;
+	case TC_ACT_OK:
+		nl_dump(p, " pass");
+		break;
+	}
+
+}
+
+static void gact_dump_details(struct rtnl_tc *tc, void *data,
+			     struct nl_dump_params *p)
+{
+}
+
+static void gact_dump_stats(struct rtnl_tc *tc, void *data,
+			   struct nl_dump_params *p)
+{
+	struct rtnl_gact *u = data;
+
+	if (!u)
+		return;
+	/* TODO */
+}
+
+
+static int gact_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
+{
+	struct rtnl_gact *u = data;
+
+	if (!u)
+		return 0;
+
+	NLA_PUT(msg, TCA_GACT_PARMS, sizeof(u->g_parm), &u->g_parm);
+
+	return 0;
+
+nla_put_failure:
+	return -NLE_NOMEM;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+int rtnl_gact_set_action(struct rtnl_act *act, int action)
+{
+	struct rtnl_gact *u;
+
+	if (!(u = (struct rtnl_gact *) rtnl_tc_data(TC_CAST(act))))
+		return -NLE_NOMEM;
+
+	if (action > TC_ACT_SHOT || action < TC_ACT_UNSPEC)
+		return -NLE_INVAL;
+
+	switch (action) {
+	case TC_ACT_UNSPEC:
+	case TC_ACT_SHOT:
+		u->g_parm.action = action;
+		break;
+	case TC_ACT_OK:
+	case TC_ACT_RECLASSIFY:
+	default:
+		return NLE_OPNOTSUPP;
+	}
+
+	return 0;
+}
+
+int rtnl_gact_get_action(struct rtnl_act *act)
+{
+	struct rtnl_gact *u;
+
+	if (!(u = (struct rtnl_gact *) rtnl_tc_data(TC_CAST(act))))
+		return -NLE_NOMEM;
+	return u->g_parm.action;
+}
+
+
+/** @} */
+
+static struct rtnl_tc_ops gact_ops = {
+	.to_kind		= "gact",
+	.to_type		= RTNL_TC_TYPE_ACT,
+	.to_size		= sizeof(struct rtnl_gact),
+	.to_msg_parser		= gact_msg_parser,
+	.to_free_data		= gact_free_data,
+	.to_clone		= gact_clone,
+	.to_msg_fill		= gact_msg_fill,
+	.to_dump = {
+	    [NL_DUMP_LINE]	= gact_dump_line,
+	    [NL_DUMP_DETAILS]	= gact_dump_details,
+	    [NL_DUMP_STATS]	= gact_dump_stats,
+	},
+};
+
+static void __init gact_init(void)
+{
+	rtnl_tc_register(&gact_ops);
+}
+
+static void __exit gact_exit(void)
+{
+	rtnl_tc_unregister(&gact_ops);
+}
+
+/** @} */
diff --git a/libnl-route-3.sym b/libnl-route-3.sym
index bfab145..4a47714 100644
--- a/libnl-route-3.sym
+++ b/libnl-route-3.sym
@@ -984,5 +984,7 @@ global:
 	rtnl_link_vxlan_set_udp_csum;
 	rtnl_link_vxlan_set_udp_zero_csum6_rx;
 	rtnl_link_vxlan_set_udp_zero_csum6_tx;
+	rtnl_gact_set_action;
+	rtnl_gact_get_action;
 } libnl_3_2_28;
 




More information about the libnl mailing list