File netlink-add-netlink-handler-for-gring-g.patch of Package ethtool

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

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

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

diff --git a/ethtool.c b/ethtool.c
index 1b2d6fb36a1c..342f5554d442 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -4844,6 +4844,7 @@ static int show_usage(struct cmd_context *ctx);
 #define nl_gfeatures	NULL
 #define nl_sfeatures	NULL
 #define nl_gcoalesce	NULL
+#define nl_gring	NULL
 #endif
 
 static const struct option {
@@ -4900,7 +4901,7 @@ static const struct option {
 	  "		[tx-usecs-high N]\n"
 	  "		[tx-frames-high N]\n"
 	  "		[sample-interval N]\n" },
-	{ "-g|--show-ring", 1, do_gring, NULL,
+	{ "-g|--show-ring", 1, do_gring, nl_gring,
 	  "Query RX/TX ring parameters" },
 	{ "-G|--set-ring", 1, do_sring, NULL,
 	  "Set RX/TX ring parameters",
diff --git a/netlink/extapi.h b/netlink/extapi.h
index bc0ca8f59686..9c84a80d223c 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -21,6 +21,7 @@ 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_gring(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 af6c1bbd4981..0f65d21c9377 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -143,6 +143,11 @@ static struct monitor_option monitor_opts[] = {
 		.cmd		= ETHNL_CMD_SET_PARAMS,
 		.info_mask	= ETH_PARAMS_IM_COALESCE,
 	},
+	{
+		.pattern	= "-g|--show-ring|-G|--set-ring",
+		.cmd		= ETHNL_CMD_SET_PARAMS,
+		.info_mask	= ETH_PARAMS_IM_RING,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
diff --git a/netlink/params.c b/netlink/params.c
index 30aee5758c42..6782d98a6956 100644
--- a/netlink/params.c
+++ b/netlink/params.c
@@ -55,6 +55,34 @@ static int show_coalesce(struct nl_context *nlctx, const struct nlattr *nest)
 	return 0;
 }
 
+static int show_ring(struct nl_context *nlctx, const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHA_RING_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("Ring parameters for %s:\n", nlctx->devname);
+	printf("Pre-set maximums:\n");
+	show_u32(tb[ETHA_RING_RX_MAX_PENDING], "RX:\t\t");
+	show_u32(tb[ETHA_RING_RX_MINI_MAX_PENDING], "RX Mini:\t");
+	show_u32(tb[ETHA_RING_RX_JUMBO_MAX_PENDING], "RX Jumbo:\t");
+	show_u32(tb[ETHA_RING_TX_MAX_PENDING], "TX:\t\t");
+	printf("Current hardware settings:\n");
+	show_u32(tb[ETHA_RING_RX_PENDING], "RX:\t\t");
+	show_u32(tb[ETHA_RING_RX_MINI_PENDING], "RX Mini:\t");
+	show_u32(tb[ETHA_RING_RX_JUMBO_PENDING], "RX Jumbo:\t");
+	show_u32(tb[ETHA_RING_TX_PENDING], "TX:\t\t");
+	putchar('\n');
+
+	return 0;
+}
+
 int params_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 {
 	const struct nlattr *tb[ETHA_PARAMS_MAX + 1] = {};
@@ -78,6 +106,15 @@ int params_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 			return MNL_CB_ERROR;
 		}
 	}
+	if (mask_ok(nlctx, ETH_PARAMS_IM_RING)) {
+		ret = show_ring(nlctx, tb[ETHA_PARAMS_RING]);
+		if ((ret < 0) && show_only(nlctx, ETH_PARAMS_IM_RING)) {
+			nlctx->exit_code = 76;
+			errno = -ret;
+			perror("Cannot get device ring settings");
+			return MNL_CB_ERROR;
+		}
+	}
 
 	return MNL_CB_OK;
 }
@@ -99,3 +136,8 @@ int nl_gcoalesce(struct cmd_context *ctx)
 {
 	return params_request(ctx, ETH_PARAMS_IM_COALESCE);
 }
+
+int nl_gring(struct cmd_context *ctx)
+{
+	return params_request(ctx, ETH_PARAMS_IM_RING);
+}
-- 
2.19.0