File netlink-add-netlink-handler-for-nway_rst-r.patch of Package ethtool

From: Michal Kubecek <mkubecek@suse.cz>
Date: Fri, 31 Aug 2018 14:29:46 +0200
Subject: netlink: add netlink handler for nway_rst (-r)
Patch-mainline: Not yet, work in progress
References: none

Implement "ethtool -r <dev>" subcommand using netlink interface command
ETHNL_CMD_ACT_NWAY_RST.

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

diff --git a/Makefile.am b/Makefile.am
index 5be2c23f4a3e..4c8783e592d6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -25,6 +25,7 @@ ethtool_SOURCES += \
 		  netlink/strset.c netlink/strset.h netlink/monitor.c \
 		  netlink/parser.c netlink/parser.h \
 		  netlink/info.c netlink/settings.c netlink/params.c \
+		  netlink/actions.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 364834f1f69c..f77c8375a25c 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -4855,6 +4855,7 @@ static int show_usage(struct cmd_context *ctx);
 #define nl_schannels	NULL
 #define nl_seee		NULL
 #define nl_sfec		NULL
+#define nl_nway_rst	NULL
 #endif
 
 static const struct option {
@@ -4941,7 +4942,7 @@ static const struct option {
 	  "		[ offset N ]\n"
 	  "		[ length N ]\n"
 	  "		[ value N ]\n" },
-	{ "-r|--negotiate", 1, do_nway_rst, NULL,
+	{ "-r|--negotiate", 1, do_nway_rst, nl_nway_rst,
 	  "Restart N-WAY negotiation" },
 	{ "-p|--identify", 1, do_phys_id, NULL,
 	  "Show visible port identification (e.g. blinking)",
diff --git a/netlink/actions.c b/netlink/actions.c
new file mode 100644
index 000000000000..d1bbcd163549
--- /dev/null
+++ b/netlink/actions.c
@@ -0,0 +1,54 @@
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "../internal.h"
+#include "../common.h"
+#include "netlink.h"
+#include "parser.h"
+
+/* ACT_NWAY_RST */
+
+int nwayrst_reply_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHA_NWAYRST_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_NWAYRST_DEV]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+
+	printf("Autonegotiation restarted on device %s\n", nlctx->devname);
+
+	return MNL_CB_OK;
+}
+
+int nl_nway_rst(struct cmd_context *ctx)
+{
+	struct nl_context *nlctx = ctx->nlctx;
+	int ret;
+
+	nlctx->cmd = "-r";
+	nlctx->argp = ctx->argp;
+	nlctx->argc = ctx->argc;
+	ret = msg_init(nlctx, ETHNL_CMD_ACT_NWAY_RST,
+		       NLM_F_REQUEST | NLM_F_ACK);
+	if (ret < 0)
+		return -EFAULT;
+	if (ethnla_put_dev(nlctx, ETHA_NWAYRST_DEV, ctx->devname))
+		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 restart autonegotiation\n");
+
+	return ret;
+}
diff --git a/netlink/extapi.h b/netlink/extapi.h
index 206f20029cff..bb6ae9205642 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -32,6 +32,7 @@ int nl_spause(struct cmd_context *ctx);
 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_monitor(struct cmd_context *ctx);
 
 void monitor_usage();
diff --git a/netlink/monitor.c b/netlink/monitor.c
index 234f254cff12..2ae374cdaeb3 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -69,6 +69,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);
+int nwayrst_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 
 static struct {
 	uint8_t		cmd;
@@ -90,6 +91,10 @@ static struct {
 		.cmd	= ETHNL_CMD_SET_PARAMS,
 		.cb	= params_reply_cb,
 	},
+	{
+		.cmd	= ETHNL_CMD_ACT_NWAY_RST,
+		.cb	= nwayrst_reply_cb,
+	},
 };
 
 static int monitor_any_cb(const struct nlmsghdr *nlhdr, void *data)
@@ -168,6 +173,10 @@ static struct monitor_option monitor_opts[] = {
 		.cmd		= ETHNL_CMD_SET_PARAMS,
 		.info_mask	= ETH_PARAMS_IM_FEC,
 	},
+	{
+		.pattern	= "-r|--negotiate",
+		.cmd		= ETHNL_CMD_ACT_NWAY_RST,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
-- 
2.19.0