File netlink-add-netlink-handler-for-scoalesce-C.patch of Package ethtool

From: Michal Kubecek <mkubecek@suse.cz>
Date: Mon, 11 Dec 2017 07:27:00 +0100
Subject: netlink: add netlink handler for scoalesce (-C)
Patch-mainline: Not yet, work in progress
References: none

Implement "ethtool -C <dev>" subcommand using netlink interface command
ETHNL_CMD_SET_PARAMS.

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
---
 ethtool.c        |   3 +-
 netlink/extapi.h |   1 +
 netlink/params.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 183 insertions(+), 1 deletion(-)

diff --git a/ethtool.c b/ethtool.c
index b3ac546b08f6..d5979d58341c 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -4849,6 +4849,7 @@ static int show_usage(struct cmd_context *ctx);
 #define nl_gchannels	NULL
 #define nl_geee		NULL
 #define nl_gfec		NULL
+#define nl_scoalesce	NULL
 #endif
 
 static const struct option {
@@ -4881,7 +4882,7 @@ static const struct option {
 	  "		[ tx on|off ]\n" },
 	{ "-c|--show-coalesce", 1, do_gcoalesce, nl_gcoalesce,
 	  "Show coalesce options" },
-	{ "-C|--coalesce", 1, do_scoalesce, NULL,
+	{ "-C|--coalesce", 1, do_scoalesce, nl_scoalesce,
 	  "Set coalesce options",
 	  "		[adaptive-rx on|off]\n"
 	  "		[adaptive-tx on|off]\n"
diff --git a/netlink/extapi.h b/netlink/extapi.h
index 81f8b1ec572a..fd3e0ca062f4 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -26,6 +26,7 @@ int nl_gpause(struct cmd_context *ctx);
 int nl_gchannels(struct cmd_context *ctx);
 int nl_geee(struct cmd_context *ctx);
 int nl_gfec(struct cmd_context *ctx);
+int nl_scoalesce(struct cmd_context *ctx);
 int nl_monitor(struct cmd_context *ctx);
 
 void monitor_usage();
diff --git a/netlink/params.c b/netlink/params.c
index c187ca0b37c5..a5fe473676e4 100644
--- a/netlink/params.c
+++ b/netlink/params.c
@@ -328,3 +328,183 @@ int nl_gfec(struct cmd_context *ctx)
 {
 	return params_request(ctx, ETH_PARAMS_IM_FEC);
 }
+
+/* SET_PARAMS */
+
+static int nl_set_param(struct cmd_context *ctx, const char *opt,
+			const struct param_parser *params, uint16_t nest_type)
+{
+	struct nl_context *nlctx = ctx->nlctx;
+	struct nlattr *nest;
+	int ret;
+
+	nlctx->cmd = opt;
+	nlctx->argp = ctx->argp;
+	nlctx->argc = ctx->argc;
+	ret = msg_init(nlctx, ETHNL_CMD_SET_PARAMS, NLM_F_REQUEST | NLM_F_ACK);
+	if (ret < 0)
+		return 2;
+	if (ethnla_put_dev(nlctx, ETHA_PARAMS_DEV, ctx->devname))
+		return -EMSGSIZE;
+
+	nest = ethnla_nest_start(nlctx, nest_type);
+	if (!nest) {
+		fprintf(stderr, "ethtool(%s): failed to allocate message\n",
+			opt);
+		return 76;
+	}
+
+	ret = nl_parser(nlctx, params, NULL);
+	if (ret < 0)
+		return 2;
+	mnl_attr_nest_end(nlctx->nlhdr, nest);
+
+	ret = ethnl_sendmsg(nlctx);
+	if (ret < 0)
+		return 75;
+	ret = ethnl_process_reply(nlctx, nomsg_reply_cb);
+	if (ret == 0)
+		return 0;
+	return nlctx->exit_code ?: 81;
+}
+
+static const struct param_parser scoalesce_params[] = {
+	{
+		.arg		= "adaptive-rx",
+		.type		= ETHA_COALESCE_RX_USE_ADAPTIVE,
+		.handler	= nl_parse_u8bool,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "adaptive-tx",
+		.type		= ETHA_COALESCE_TX_USE_ADAPTIVE,
+		.handler	= nl_parse_u8bool,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "sample-interval",
+		.type		= ETHA_COALESCE_RATE_SAMPLE_INTERVAL,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "stats-block-usecs",
+		.type		= ETHA_COALESCE_STATS_BLOCK_USECS,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "pkt-rate-low",
+		.type		= ETHA_COALESCE_PKT_RATE_LOW,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "pkt-rate-high",
+		.type		= ETHA_COALESCE_PKT_RATE_HIGH,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-usecs",
+		.type		= ETHA_COALESCE_RX_USECS,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-frames",
+		.type		= ETHA_COALESCE_RX_MAXFRM,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-usecs-irq",
+		.type		= ETHA_COALESCE_RX_USECS_IRQ,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-frames-irq",
+		.type		= ETHA_COALESCE_RX_MAXFRM_IRQ,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-usecs",
+		.type		= ETHA_COALESCE_TX_USECS,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-frames",
+		.type		= ETHA_COALESCE_TX_MAXFRM,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-usecs-irq",
+		.type		= ETHA_COALESCE_TX_USECS_IRQ,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-frames-irq",
+		.type		= ETHA_COALESCE_TX_MAXFRM_IRQ,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-usecs-low",
+		.type		= ETHA_COALESCE_RX_USECS_LOW,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-frames-low",
+		.type		= ETHA_COALESCE_RX_MAXFRM_LOW,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-usecs-low",
+		.type		= ETHA_COALESCE_TX_USECS_LOW,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-frames-low",
+		.type		= ETHA_COALESCE_TX_MAXFRM_LOW,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-usecs-high",
+		.type		= ETHA_COALESCE_RX_USECS_HIGH,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "rx-frames-high",
+		.type		= ETHA_COALESCE_RX_MAXFRM_HIGH,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-usecs-high",
+		.type		= ETHA_COALESCE_TX_USECS_HIGH,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{
+		.arg		= "tx-frames-high",
+		.type		= ETHA_COALESCE_TX_MAXFRM_HIGH,
+		.handler	= nl_parse_direct_u32,
+		.min_argc	= 1,
+	},
+	{}
+};
+
+int nl_scoalesce(struct cmd_context *ctx)
+{
+	return nl_set_param(ctx, "-C", scoalesce_params, ETHA_PARAMS_COALESCE);
+}
-- 
2.19.0