File netlink-add-netlink-handler-for-phys_id-p.patch of Package ethtool

From: Michal Kubecek <mkubecek@suse.cz>
Date: Sun, 2 Sep 2018 16:43:40 +0200
Subject: netlink: add netlink handler for phys_id (-p)
Patch-mainline: Not yet, work in progress
References: none

Implement "ethtool -p <dev>" using netlink interface command
ETHNL_CMD_ACT_PHYS_ID.

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

diff --git a/ethtool.c b/ethtool.c
index f77c8375a25c..a9c408545dab 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -4856,6 +4856,7 @@ static int show_usage(struct cmd_context *ctx);
 #define nl_seee		NULL
 #define nl_sfec		NULL
 #define nl_nway_rst	NULL
+#define nl_phys_id	NULL
 #endif
 
 static const struct option {
@@ -4944,7 +4945,7 @@ static const struct option {
 	  "		[ value N ]\n" },
 	{ "-r|--negotiate", 1, do_nway_rst, nl_nway_rst,
 	  "Restart N-WAY negotiation" },
-	{ "-p|--identify", 1, do_phys_id, NULL,
+	{ "-p|--identify", 1, do_phys_id, nl_phys_id,
 	  "Show visible port identification (e.g. blinking)",
 	  "               [ TIME-IN-SECONDS ]\n" },
 	{ "-t|--test", 1, do_test, NULL,
diff --git a/netlink/actions.c b/netlink/actions.c
index d1bbcd163549..160352ea8df3 100644
--- a/netlink/actions.c
+++ b/netlink/actions.c
@@ -52,3 +52,81 @@ int nl_nway_rst(struct cmd_context *ctx)
 
 	return ret;
 }
+
+/* ACT_PHYS_ID */
+
+int physid_reply_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHA_PHYSID_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	struct nl_context *nlctx = data;
+	uint32_t timeout = 0;
+	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_PHYSID_DEV]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+	if(tb[ETHA_PHYSID_LENGTH])
+		timeout = mnl_attr_get_u32(tb[ETHA_PHYSID_LENGTH]);
+
+	if (timeout) {
+		printf("NIC identification for %s started", nlctx->devname);
+		if (timeout < UINT32_MAX)
+			printf(", timeout %u seconds", timeout);
+		putchar('\n');
+	} else {
+		printf("NIC identification for %s finshed\n", nlctx->devname);
+	}
+
+	return MNL_CB_OK;
+}
+
+int nl_phys_id(struct cmd_context *ctx)
+{
+	struct nl_context *nlctx = ctx->nlctx;
+	bool have_length = false;
+	uint32_t length;
+	int ret;
+
+	nlctx->cmd = "-p";
+	nlctx->argp = ctx->argp;
+	nlctx->argc = ctx->argc;
+	if (nlctx->argc > 1) {
+		fprintf(stderr,
+			"only one argument expected for -p / --identify\n");
+		return 2;
+	}
+	if (nlctx->argc > 0) {
+		ret = parse_u32(nlctx->argp[0], &length);
+		if (ret < 0) {
+			fprintf(stderr,
+				"invalid argument '%s' for -p / --identify\n",
+				nlctx->argp[0]);
+			return 2;
+		}
+		have_length = true;
+		nlctx->argp++;
+		nlctx->argc--;
+	}
+
+	ret = msg_init(nlctx, ETHNL_CMD_ACT_PHYS_ID,
+		       NLM_F_REQUEST | NLM_F_ACK);
+	if (ret < 0)
+		return -EFAULT;
+	if (ethnla_put_dev(nlctx, ETHA_PHYSID_DEV, ctx->devname))
+		return -EMSGSIZE;
+	if (have_length && ethnla_put_u32(nlctx, ETHA_PHYSID_LENGTH, length))
+		return -EMSGSIZE;
+
+	ret = ethnl_sendmsg(nlctx);
+	if (ret < 0)
+		return -EFAULT;
+	ret = ethnl_process_reply(nlctx, nomsg_reply_cb);
+	if (ret < 0)
+		fprintf(stderr, "Cannot identify NIC\n");
+
+	return ret;
+}
diff --git a/netlink/extapi.h b/netlink/extapi.h
index bb6ae9205642..87ac57955bab 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -33,6 +33,7 @@ int nl_schannels(struct cmd_context *ctx);
 int nl_seee(struct cmd_context *ctx);
 int nl_sfec(struct cmd_context *ctx);
 int nl_nway_rst(struct cmd_context *ctx);
+int nl_phys_id(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 2ae374cdaeb3..281aa1f5da16 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -70,6 +70,7 @@ 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);
 int nwayrst_reply_cb(const struct nlmsghdr *nlhdr, void *data);
+int physid_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 
 static struct {
 	uint8_t		cmd;
@@ -95,6 +96,10 @@ static struct {
 		.cmd	= ETHNL_CMD_ACT_NWAY_RST,
 		.cb	= nwayrst_reply_cb,
 	},
+	{
+		.cmd	= ETHNL_CMD_ACT_PHYS_ID,
+		.cb	= physid_reply_cb,
+	},
 };
 
 static int monitor_any_cb(const struct nlmsghdr *nlhdr, void *data)
@@ -177,6 +182,10 @@ static struct monitor_option monitor_opts[] = {
 		.pattern	= "-r|--negotiate",
 		.cmd		= ETHNL_CMD_ACT_NWAY_RST,
 	},
+	{
+		.pattern	= "-p|--identify",
+		.cmd		= ETHNL_CMD_ACT_PHYS_ID,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
-- 
2.19.0