File netlink-add-netlink-handler-for-gdrv-i.patch of Package ethtool

From: Michal Kubecek <mkubecek@suse.cz>
Date: Sun, 19 Nov 2017 21:10:58 +0100
Subject: netlink: add netlink handler for gdrv (-i)
Patch-mainline: Not yet, work in progress
References: none

Implement "-i" subcommand using netlink interface command
ETHNL_CMD_GET_INFO.

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

diff --git a/Makefile.am b/Makefile.am
index 188364c0c837..4006e7da5074 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,6 +23,7 @@ if ETHTOOL_ENABLE_NETLINK
 ethtool_SOURCES += \
 		  netlink/netlink.c netlink/netlink.h netlink/extapi.h \
 		  netlink/strset.c netlink/strset.h netlink/monitor.c \
+		  netlink/info.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 6943834f4142..4ed41d08057a 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -5060,6 +5060,7 @@ static int show_usage(struct cmd_context *ctx);
 /* Just define all netlink handlers as null when building without netlink
  * support so that we do not get unresolved symbols in args array below
  */
+#define nl_gdrv		NULL
 #endif
 
 static const struct option {
@@ -5129,7 +5130,7 @@ static const struct option {
 	{ "-K|--features|--offload", 1, do_sfeatures, NULL,
 	  "Set protocol offload and other features",
 	  "		FEATURE on|off ...\n" },
-	{ "-i|--driver", 1, do_gdrv, NULL,
+	{ "-i|--driver", 1, do_gdrv, nl_gdrv,
 	  "Show driver information" },
 	{ "-d|--register-dump", 1, do_gregs, NULL,
 	  "Do a register dump",
diff --git a/netlink/extapi.h b/netlink/extapi.h
index 827e3732888a..546090a02a0d 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -13,6 +13,7 @@ struct nl_context;
 int netlink_init(struct cmd_context *ctx);
 int netlink_done(struct cmd_context *ctx);
 
+int nl_gdrv(struct cmd_context *ctx);
 int nl_monitor(struct cmd_context *ctx);
 
 void monitor_usage();
diff --git a/netlink/info.c b/netlink/info.c
new file mode 100644
index 000000000000..8fa0c3e36088
--- /dev/null
+++ b/netlink/info.c
@@ -0,0 +1,70 @@
+#include <errno.h>
+
+#include "../internal.h"
+#include "netlink.h"
+
+static int show_drvinfo(struct nl_context *nlctx, const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHA_DRVINFO_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;
+
+	if (nlctx->is_dump || nlctx->is_monitor)
+		printf("\nDriver info for %s:\n", nlctx->devname);
+	show_string(tb, ETHA_DRVINFO_DRIVER, "driver");
+	show_string(tb, ETHA_DRVINFO_FWVERSION, "firmware-version");
+	show_string(tb, ETHA_DRVINFO_EROM_VER, "expansion-rom-version");
+	show_string(tb, ETHA_DRVINFO_BUSINFO, "bus-info");
+
+	return MNL_CB_OK;
+}
+
+int info_reply_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHA_INFO_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_INFO_DEV]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+
+	if (mask_ok(nlctx, ETH_INFO_IM_DRVINFO)) {
+		ret = show_drvinfo(nlctx, tb[ETHA_INFO_DRVINFO]);
+		if ((ret < 0) && show_only(nlctx, ETH_INFO_IM_DRVINFO)) {
+			nlctx->exit_code = 1;
+			errno = -ret;
+			perror("Cannot get device driver info");
+			return MNL_CB_ERROR;
+		}
+	}
+
+	return MNL_CB_OK;
+}
+
+static int info_request(struct cmd_context *ctx, uint32_t info_mask)
+{
+	int ret;
+
+	ret = ethnl_prep_get_request(ctx, ETHNL_CMD_GET_INFO, ETHA_INFO_DEV);
+	if (ret < 0)
+		return ret;
+	if (ethnla_put_u32(ctx->nlctx, ETHA_INFO_INFOMASK, info_mask))
+		return -EMSGSIZE;
+	return ethnl_send_get_request(ctx->nlctx, info_reply_cb);
+}
+
+int nl_gdrv(struct cmd_context *ctx)
+{
+	return info_request(ctx, ETH_INFO_IM_DRVINFO);
+}
diff --git a/netlink/monitor.c b/netlink/monitor.c
index 51f64e6ad839..c6817b4883ff 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -66,6 +66,8 @@ static int monitor_event_cb(const struct nlmsghdr *nlhdr, void *data)
 	return MNL_CB_OK;
 }
 
+int info_reply_cb(const struct nlmsghdr *nlhdr, void *data);
+
 static struct {
 	uint8_t		cmd;
 	mnl_cb_t	cb;
@@ -74,6 +76,10 @@ static struct {
 		.cmd	= ETHNL_CMD_EVENT,
 		.cb	= monitor_event_cb,
 	},
+	{
+		.cmd	= ETHNL_CMD_SET_INFO,
+		.cb	= info_reply_cb,
+	},
 };
 
 static int monitor_any_cb(const struct nlmsghdr *nlhdr, void *data)
@@ -103,6 +109,10 @@ static struct monitor_option monitor_opts[] = {
 		.pattern	= "|--all",
 		.cmd		= 0,
 	},
+	{
+		.pattern	= "-i|--driver",
+		.cmd		= ETHNL_CMD_SET_INFO,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
-- 
2.19.0