File netlink-add-netlink-handler-for-gcoalesce-c.patch of Package ethtool

From: Michal Kubecek <mkubecek@suse.cz>
Date: Fri, 8 Dec 2017 23:54:51 +0100
Subject: netlink: add netlink handler for gcoalesce (-c)
Patch-mainline: Not yet, work in progress
References: none

Implement "ethtool -c <dev>" subcommand using netlink interface command
ETHNL_CMD_GET_PARAMS with ETH_PARAMS_IM_COALESCE mask.

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
---
 Makefile.am       |   2 +-
 ethtool.c         |   3 +-
 netlink/extapi.h  |   1 +
 netlink/monitor.c |  10 +++++
 netlink/params.c  | 101 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 2 deletions(-)
 create mode 100644 netlink/params.c

diff --git a/Makefile.am b/Makefile.am
index 9e4be2589064..5be2c23f4a3e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ ethtool_SOURCES += \
 		  netlink/netlink.c netlink/netlink.h netlink/extapi.h \
 		  netlink/strset.c netlink/strset.h netlink/monitor.c \
 		  netlink/parser.c netlink/parser.h \
-		  netlink/info.c netlink/settings.c \
+		  netlink/info.c netlink/settings.c netlink/params.c \
 		  uapi/linux/ethtool_netlink.h \
 		  uapi/linux/netlink.h uapi/linux/genetlink.h
 ethtool_CFLAGS += @MNL_CFLAGS@
diff --git a/ethtool.c b/ethtool.c
index 8d6e48f2cfe5..1b2d6fb36a1c 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -4843,6 +4843,7 @@ static int show_usage(struct cmd_context *ctx);
 #define nl_sset		NULL
 #define nl_gfeatures	NULL
 #define nl_sfeatures	NULL
+#define nl_gcoalesce	NULL
 #endif
 
 static const struct option {
@@ -4873,7 +4874,7 @@ static const struct option {
 	  "		[ autoneg on|off ]\n"
 	  "		[ rx on|off ]\n"
 	  "		[ tx on|off ]\n" },
-	{ "-c|--show-coalesce", 1, do_gcoalesce, NULL,
+	{ "-c|--show-coalesce", 1, do_gcoalesce, nl_gcoalesce,
 	  "Show coalesce options" },
 	{ "-C|--coalesce", 1, do_scoalesce, NULL,
 	  "Set coalesce options",
diff --git a/netlink/extapi.h b/netlink/extapi.h
index ed526cf60142..bc0ca8f59686 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -20,6 +20,7 @@ int nl_gset(struct cmd_context *ctx);
 int nl_sset(struct cmd_context *ctx);
 int nl_gfeatures(struct cmd_context *ctx);
 int nl_sfeatures(struct cmd_context *ctx);
+int nl_gcoalesce(struct cmd_context *ctx);
 int nl_monitor(struct cmd_context *ctx);
 
 void monitor_usage();
diff --git a/netlink/monitor.c b/netlink/monitor.c
index 38d669ef58a6..af6c1bbd4981 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -68,6 +68,7 @@ static int monitor_event_cb(const struct nlmsghdr *nlhdr, void *data)
 
 int info_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 int settings_reply_cb(const struct nlmsghdr *nlhdr, void *data);
+int params_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 
 static struct {
 	uint8_t		cmd;
@@ -85,6 +86,10 @@ static struct {
 		.cmd	= ETHNL_CMD_SET_SETTINGS,
 		.cb	= settings_reply_cb,
 	},
+	{
+		.cmd	= ETHNL_CMD_SET_PARAMS,
+		.cb	= params_reply_cb,
+	},
 };
 
 static int monitor_any_cb(const struct nlmsghdr *nlhdr, void *data)
@@ -133,6 +138,11 @@ static struct monitor_option monitor_opts[] = {
 		.cmd		= ETHNL_CMD_SET_SETTINGS,
 		.info_mask	= ETH_SETTINGS_IM_FEATURES,
 	},
+	{
+		.pattern	= "-c|--show-coalesce|-C|--coalesce",
+		.cmd		= ETHNL_CMD_SET_PARAMS,
+		.info_mask	= ETH_PARAMS_IM_COALESCE,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
diff --git a/netlink/params.c b/netlink/params.c
new file mode 100644
index 000000000000..30aee5758c42
--- /dev/null
+++ b/netlink/params.c
@@ -0,0 +1,101 @@
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "../internal.h"
+#include "../common.h"
+#include "netlink.h"
+#include "strset.h"
+#include "parser.h"
+
+/* GET_PARAMS */
+
+static int show_coalesce(struct nl_context *nlctx, const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHA_COALESCE_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	int ret;
+
+	if (!nest)
+		return -EOPNOTSUPP;
+	ret = mnl_attr_parse_nested(nest, attr_cb, &tb_info);
+	if (ret < 0)
+		return ret;
+
+	printf("Coalesce parameters for %s:\n", nlctx->devname);
+	printf("Adaptive RX: %s  TX: %s\n",
+	       u8_to_bool(tb[ETHA_COALESCE_RX_USE_ADAPTIVE]),
+	       u8_to_bool(tb[ETHA_COALESCE_TX_USE_ADAPTIVE]));
+	show_u32(tb[ETHA_COALESCE_STATS_BLOCK_USECS], "stats-block-usecs: ");
+	show_u32(tb[ETHA_COALESCE_RATE_SAMPLE_INTERVAL], "sample-interval: ");
+	show_u32(tb[ETHA_COALESCE_PKT_RATE_LOW], "pkt-rate-low: ");
+	show_u32(tb[ETHA_COALESCE_PKT_RATE_HIGH], "pkt-rate-high: ");
+	putchar('\n');
+	show_u32(tb[ETHA_COALESCE_RX_USECS], "rx-usecs: ");
+	show_u32(tb[ETHA_COALESCE_RX_MAXFRM], "rx-frames: ");
+	show_u32(tb[ETHA_COALESCE_RX_USECS_IRQ], "rx-usecs-irq: ");
+	show_u32(tb[ETHA_COALESCE_RX_MAXFRM_IRQ], "rx-frames-irq: ");
+	putchar('\n');
+	show_u32(tb[ETHA_COALESCE_TX_USECS], "tx-usecs: ");
+	show_u32(tb[ETHA_COALESCE_TX_MAXFRM], "tx-frames: ");
+	show_u32(tb[ETHA_COALESCE_TX_USECS_IRQ], "tx-usecs-irq: ");
+	show_u32(tb[ETHA_COALESCE_TX_MAXFRM_IRQ], "tx-frames-irq: ");
+	putchar('\n');
+	show_u32(tb[ETHA_COALESCE_RX_USECS_LOW], "rx-usecs-low: ");
+	show_u32(tb[ETHA_COALESCE_RX_MAXFRM_LOW], "rx-frame-low: ");
+	show_u32(tb[ETHA_COALESCE_TX_USECS_LOW], "tx-usecs-low: ");
+	show_u32(tb[ETHA_COALESCE_TX_MAXFRM_LOW], "tx-frame-low: ");
+	putchar('\n');
+	show_u32(tb[ETHA_COALESCE_RX_USECS_HIGH], "rx-usecs-high: ");
+	show_u32(tb[ETHA_COALESCE_RX_MAXFRM_HIGH], "rx-frame-high: ");
+	show_u32(tb[ETHA_COALESCE_TX_USECS_HIGH], "tx-usecs-high: ");
+	show_u32(tb[ETHA_COALESCE_TX_MAXFRM_HIGH], "tx-frame-high: ");
+	putchar('\n');
+
+	return 0;
+}
+
+int params_reply_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHA_PARAMS_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	struct nl_context *nlctx = data;
+	int ret;
+
+	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
+	if (ret < 0)
+		return ret;
+	nlctx->devname = get_dev_name(tb[ETHA_PARAMS_DEV]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+
+	if (mask_ok(nlctx, ETH_PARAMS_IM_COALESCE)) {
+		ret = show_coalesce(nlctx, tb[ETHA_PARAMS_COALESCE]);
+		if ((ret < 0) && show_only(nlctx, ETH_PARAMS_IM_COALESCE)) {
+			nlctx->exit_code = 82;
+			errno = -ret;
+			perror("Cannot get device coalesce settings");
+			return MNL_CB_ERROR;
+		}
+	}
+
+	return MNL_CB_OK;
+}
+
+static int params_request(struct cmd_context *ctx, uint32_t info_mask)
+{
+	int ret;
+
+	ret = ethnl_prep_get_request(ctx, ETHNL_CMD_GET_PARAMS,
+				     ETHA_PARAMS_DEV);
+	if (ret < 0)
+		return ret;
+	if (ethnla_put_u32(ctx->nlctx, ETHA_PARAMS_INFOMASK, info_mask))
+		return -EMSGSIZE;
+	return ethnl_send_get_request(ctx->nlctx, params_reply_cb);
+}
+
+int nl_gcoalesce(struct cmd_context *ctx)
+{
+	return params_request(ctx, ETH_PARAMS_IM_COALESCE);
+}
-- 
2.19.0