Skip to contentSkip to conversation

improve the logic of fallback from netlink to ioctl

PATCH In review Michal Kubecek 2 participants 4 messages Apr 28, 2020 – May 13, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover improve the logic of fallback from netlink to ioctl

At the moment, ethtool falls back to ioctl implementation whenever either in response to request type not implemented in kernel or netlink interface is unavailable or netlink request fails with EOPNOTSUPP error code. This is not perfect as EOPNOTSUPP can have different meanings and we only want to fall back if it is caused by kernel lacking netlink implementation of the request. In other cases, we would needlessly repeat the same failure trying both netlink and ioctl.

These two patches improve the logic to avoid such duplicate failures and improve handling of cases where fallback to ioctl is impossible for other reasons (e.g. wildcard device name or no ioctl handler).

Michal Kubecek (2):
refactor interface between ioctl and netlink code
netlink: use genetlink ops information to decide about fallback

 ethtool.c          |  51 +++---------
 netlink/extapi.h   |  14 ++--
 netlink/monitor.c  |  15 +++-
 netlink/netlink.c  | 193 ++++++++++++++++++++++++++++++++++++++-------
 netlink/netlink.h  |   6 ++
 netlink/parser.c   |   7 ++
 netlink/settings.c |   7 ++
 7 files changed, 220 insertions(+), 73 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 refactor interface between ioctl and netlink code

Move netlink related code from main() to separate handlers in netlink code. Improve the logic of fallback to ioctl and improve error messages when fallback is not possible (e.g. wildcard device name or name longer than IFNAMSIZ). Also handle the (theoretical for now) case when ioctl handler is not available.

---
 ethtool.c         | 51 +++++++++++-----------------------------
 netlink/extapi.h  | 14 +++++++----
 netlink/monitor.c | 15 +++++++++---
 netlink/netlink.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++-
 netlink/netlink.h |  1 +
 5 files changed, 93 insertions(+), 47 deletions(-)
ethtool.c +13 -38
index 1b4e08b6e60f..14c0bdc776f6 100644
@@ -5157,7 +5157,7 @@ struct option {
 	const char	*opts;
 	bool		no_dev;
 	int		(*func)(struct cmd_context *);
-	int		(*nlfunc)(struct cmd_context *);
+	nl_func_t	nlfunc;
 	const char	*help;
 	const char	*xhelp;
 };
@@ -5731,6 +5731,11 @@ static int ioctl_init(struct cmd_context *ctx, bool no_dev)
 		ctx->fd = -1;
 		return 0;
 	}
+	if (strlen(ctx->devname) >= IFNAMSIZ) {
+		fprintf(stderr, "Device name longer than %u characters\n",
+			IFNAMSIZ - 1);
+		exit_bad_args();
+	}
 
 	/* Setup our control structures. */
 	memset(&ctx->ifr, 0, sizeof(ctx->ifr));
@@ -5750,9 +5755,9 @@ static int ioctl_init(struct cmd_context *ctx, bool no_dev)
 
 int main(int argc, char **argp)
 {
-	int (*nlfunc)(struct cmd_context *) = NULL;
 	int (*func)(struct cmd_context *);
 	struct cmd_context ctx = {};
+	nl_func_t nlfunc = NULL;
 	bool no_dev;
 	int ret;
 	int k;
@@ -5775,19 +5780,12 @@ int main(int argc, char **argp)
 		argp += 2;
 		argc -= 2;
 	}
-#ifdef ETHTOOL_ENABLE_NETLINK
 	if (*argp && !strcmp(*argp, "--monitor")) {
-		if (netlink_init(&ctx)) {
-			fprintf(stderr,
-				"Option --monitor is only available with netlink.\n");
-			return 1;
-		} else {
-			ctx.argp = ++argp;
-			ctx.argc = --argc;
-			return nl_monitor(&ctx);
-		}
+		ctx.argp = ++argp;
+		ctx.argc = --argc;
+		ret = nl_monitor(&ctx);
+		return ret ? 1 : 0;
 	}
-#endif
 
 	/* First argument must be either a valid option or a device
 	 * name to get settings for (which we don't expect to begin
@@ -5812,40 +5810,17 @@ int main(int argc, char **argp)
 	no_dev = false;
 
 opt_found:
-	if (nlfunc) {
-		if (netlink_init(&ctx))
-			nlfunc = NULL;		/* fallback to ioctl() */
-	}
-
 	if (!no_dev) {
 		ctx.devname = *argp++;
 		argc--;
 
-		/* netlink supports altnames, we will have to recheck against
-		 * IFNAMSIZ later in case of fallback to ioctl
-		 */
-		if (!ctx.devname || strlen(ctx.devname) >= ALTIFNAMSIZ) {
-			netlink_done(&ctx);
+		if (!ctx.devname)
 			exit_bad_args();
-		}
 	}
-
 	ctx.argc = argc;
 	ctx.argp = argp;
+	netlink_run_handler(&ctx, nlfunc, !func);
 
-	if (nlfunc) {
-		ret = nlfunc(&ctx);
-		netlink_done(&ctx);
-		if ((ret != -EOPNOTSUPP) || !func)
-			return (ret >= 0) ? ret : 1;
-	}
-
-	if (ctx.devname && strlen(ctx.devname) >= IFNAMSIZ) {
-		fprintf(stderr,
-			"ethtool: device names longer than %u characters are only allowed with netlink\n",
-			IFNAMSIZ - 1);
-		exit_bad_args();
-	}
 	ret = ioctl_init(&ctx, no_dev);
 	if (ret)
 		return ret;
netlink/extapi.h +9 -5
index d5b3cd92d4ab..9484b4c5ae7e 100644
@@ -10,10 +10,12 @@
 struct cmd_context;
 struct nl_context;
 
+typedef int (*nl_func_t)(struct cmd_context *);
+
 #ifdef ETHTOOL_ENABLE_NETLINK
 
-int netlink_init(struct cmd_context *ctx);
-void netlink_done(struct cmd_context *ctx);
+void netlink_run_handler(struct cmd_context *ctx, nl_func_t nlfunc,
+			 bool no_fallback);
 
 int nl_gset(struct cmd_context *ctx);
 int nl_sset(struct cmd_context *ctx);
@@ -24,13 +26,15 @@ void nl_monitor_usage(void);
 
 #else /* ETHTOOL_ENABLE_NETLINK */
 
-static inline int netlink_init(struct cmd_context *ctx maybe_unused)
+static inline void netlink_run_handler(struct cmd_context *ctx,
+				       nl_func_t nlfunc, bool no_fallback)
 {
-	return -EOPNOTSUPP;
 }
 
-static inline void netlink_done(struct cmd_context *ctx maybe_unused)
+static inline int nl_monitor(struct cmd_context *ctx)
 {
+	fprintf(stderr, "Netlink not supported by ethtool, option --monitor unsupported.\n");
+	return -EOPNOTSUPP;
 }
 
 static inline void nl_monitor_usage(void)
netlink/monitor.c +12 -3
index 5fce6b64c08c..e20db5fc79d4 100644
@@ -167,16 +167,25 @@ static int parse_monitor(struct cmd_context *ctx)
 
 int nl_monitor(struct cmd_context *ctx)
 {
-	struct nl_context *nlctx = ctx->nlctx;
-	struct nl_socket *nlsk = nlctx->ethnl_socket;
-	uint32_t grpid = nlctx->ethnl_mongrp;
+	struct nl_context *nlctx;
+	struct nl_socket *nlsk;
+	uint32_t grpid;
 	bool is_dev;
 	int ret;
 
+	ret = netlink_init(ctx);
+	if (ret < 0) {
+		fprintf(stderr, "Netlink interface initialization failed, option --monitor not supported.\n");
+		return ret;
+	}
+	nlctx = ctx->nlctx;
+	nlsk = nlctx->ethnl_socket;
+	grpid = nlctx->ethnl_mongrp;
 	if (!grpid) {
 		fprintf(stderr, "multicast group 'monitor' not found\n");
 		return -EOPNOTSUPP;
 	}
+
 	if (parse_monitor(ctx) < 0)
 		return 1;
 	is_dev = ctx->devname && strcmp(ctx->devname, WILDCARD_DEVNAME);
netlink/netlink.c +58 -1

68 lines not on this page · raw message

netlink/netlink.h +1 -0
index db078d28fabb..41102d270eaf 100644
@@ -49,6 +49,7 @@ struct attr_tb_info {
 int nomsg_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 int attr_cb(const struct nlattr *attr, void *data);
 
+int netlink_init(struct cmd_context *ctx);
 const char *get_dev_name(const struct nlattr *nest);
 int get_dev_info(const struct nlattr *nest, int *ifindex, char *ifname);
 
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/2 netlink: use genetlink ops information to decide about fallback

In reply to Michal Kubecek

Currently ethtool falls back to ioctl when netlink socket initialization fails or if netlink request fails with -EOPNOTSUPP (and we do not know that request cannot possibly work via ioctl, e.g. because of wildcard device name or device name longer than IFNAMSIZ). This logic has one problem: we cannot distinguish if -EOPNOTSUPP error code means that subcommand as such is not implemented in kernel or that it failed for some other reason (e.g. specific combination of parameters is not supported by driver). If the latter is the case, fallback to ioctl is pointless as the same request would fail via ioctl as well. In some cases we can even get a duplicate error message.

Fortunately, genetlink provides information about supported family commands in the form of CTRL_ATTR_OPS nested attribute. Parse this information when available and use it to only fall back to ioctl only if kernel support for netlink request as such is missing so that there is a chance that ioctl request will work. In such case, do not send netlink request at all.

---
 netlink/netlink.c  | 138 ++++++++++++++++++++++++++++++++++++---------
 netlink/netlink.h  |   5 ++
 netlink/parser.c   |   7 +++
 netlink/settings.c |   7 +++
 4 files changed, 129 insertions(+), 28 deletions(-)
netlink/netlink.c +110 -28
index 59dbab2dee00..ccd88448197d 100644
@@ -92,16 +92,88 @@ int get_dev_info(const struct nlattr *nest, int *ifindex, char *ifname)
 	return 0;
 }
 
+/**
+ * netlink_cmd_check() - check support for netlink command
+ * @ctx:            ethtool command context
+ * @cmd:            netlink command id
+ * @devname:        device name from user
+ * @allow_wildcard: wildcard dumps supported
+ *
+ * Check if command @cmd is known to be unsupported based on ops information
+ * from genetlink family id request. Set nlctx->ioctl_fallback if ethtool
+ * should fall back to ioctl, i.e. when we do not know in advance that
+ * netlink request is supported. Set nlctx->wildcard_unsupported if "*" was
+ * used as device name but the request does not support wildcards (on either
+ * side).
+ *
+ * Return: true if we know the netlink request is not supported and should
+ * fail (and possibly fall back) without actually sending it to kernel.
+ */
+bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd,
+		       bool allow_wildcard)
+{
+	bool is_dump = !strcmp(ctx->devname, WILDCARD_DEVNAME);
+	uint32_t cap = is_dump ? GENL_CMD_CAP_DUMP : GENL_CMD_CAP_DO;
+	struct nl_context *nlctx = ctx->nlctx;
+
+	if (is_dump && !allow_wildcard) {
+		nlctx->wildcard_unsupported = true;
+		return true;
+	}
+	if (!nlctx->ops_flags) {
+		nlctx->ioctl_fallback = true;
+		return false;
+	}
+	if (cmd > ETHTOOL_MSG_USER_MAX || !nlctx->ops_flags[cmd]) {
+		nlctx->ioctl_fallback = true;
+		return true;
+	}
+
+	if (is_dump && !(nlctx->ops_flags[cmd] & GENL_CMD_CAP_DUMP))
+		nlctx->wildcard_unsupported = true;
+
+	return !(nlctx->ops_flags[cmd] & cap);
+}
+
 /* initialization */
 
-struct fam_info {
-	const char	*fam_name;
-	const char	*grp_name;
-	uint16_t	fam_id;
-	uint32_t	grp_id;
-};
+static int genl_read_ops(struct nl_context *nlctx,
+			 const struct nlattr *ops_attr)
+{
+	struct nlattr *op_attr;
+	uint32_t *ops_flags;
+	int ret;
+
+	ops_flags = calloc(__ETHTOOL_MSG_USER_CNT, sizeof(ops_flags[0]));
+	if (!ops_flags)
+		return -ENOMEM;
+
+	mnl_attr_for_each_nested(op_attr, ops_attr) {
+		const struct nlattr *tb[CTRL_ATTR_OP_MAX + 1] = {};
+		DECLARE_ATTR_TB_INFO(tb);
+		uint32_t op_id;
+
+		ret = mnl_attr_parse_nested(op_attr, attr_cb, &tb_info);
+		if (ret < 0)
+			goto err;
+
+		if (!tb[CTRL_ATTR_OP_ID] || !tb[CTRL_ATTR_OP_FLAGS])
+			continue;
+		op_id = mnl_attr_get_u32(tb[CTRL_ATTR_OP_ID]);
+		if (op_id >= __ETHTOOL_MSG_USER_CNT)
+			continue;
+
+		ops_flags[op_id] = mnl_attr_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
+	}
+
+	nlctx->ops_flags = ops_flags;
+	return 0;
+err:
+	free(ops_flags);
+	return ret;
+}
 
-static void find_mc_group(struct nlattr *nest, struct fam_info *info)
+static void find_mc_group(struct nl_context *nlctx, struct nlattr *nest)
 {
 	const struct nlattr *grp_tb[CTRL_ATTR_MCAST_GRP_MAX + 1] = {};
 	DECLARE_ATTR_TB_INFO(grp_tb);
@@ -116,9 +188,9 @@ static void find_mc_group(struct nlattr *nest, struct fam_info *info)
 		    !grp_tb[CTRL_ATTR_MCAST_GRP_ID])
 			continue;
 		if (strcmp(mnl_attr_get_str(grp_tb[CTRL_ATTR_MCAST_GRP_NAME]),
-			   info->grp_name))
+			   ETHTOOL_MCGRP_MONITOR_NAME))
 			continue;
-		info->grp_id =
+		nlctx->ethnl_mongrp =
 			mnl_attr_get_u32(grp_tb[CTRL_ATTR_MCAST_GRP_ID]);
 		return;
 	}
@@ -126,16 +198,21 @@ static void find_mc_group(struct nlattr *nest, struct fam_info *info)
 
 static int family_info_cb(const struct nlmsghdr *nlhdr, void *data)
 {
-	struct fam_info *info = data;
+	struct nl_context *nlctx = data;
 	struct nlattr *attr;
+	int ret;
 
 	mnl_attr_for_each(attr, nlhdr, GENL_HDRLEN) {
 		switch (mnl_attr_get_type(attr)) {
 		case CTRL_ATTR_FAMILY_ID:
-			info->fam_id = mnl_attr_get_u16(attr);
+			nlctx->ethnl_fam = mnl_attr_get_u16(attr);
 			break;
+		case CTRL_ATTR_OPS:
+			ret = genl_read_ops(nlctx, attr);
+			if (ret < 0)
+				return MNL_CB_ERROR;
 		case CTRL_ATTR_MCAST_GROUPS:
-			find_mc_group(attr, info);
+			find_mc_group(nlctx, attr);
 			break;
 		}
 	}
@@ -144,41 +221,37 @@ static int family_info_cb(const struct nlmsghdr *nlhdr, void *data)
 }
 
 #ifdef TEST_ETHTOOL
-static int get_genl_family(struct nl_socket *nlsk, struct fam_info *info)
+static int get_genl_family(struct nl_context *nlctx, struct nl_socket *nlsk)
 {
 	return 0;
 }
 #else
-static int get_genl_family(struct nl_socket *nlsk, struct fam_info *info)
+static int get_genl_family(struct nl_context *nlctx, struct nl_socket *nlsk)
 {
 	struct nl_msg_buff *msgbuff = &nlsk->msgbuff;
 	int ret;
 
-	nlsk->nlctx->suppress_nlerr = 2;
+	nlctx->suppress_nlerr = 2;
 	ret = __msg_init(msgbuff, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
 			 NLM_F_REQUEST | NLM_F_ACK, 1);
 	if (ret < 0)
 		goto out;
 	ret = -EMSGSIZE;
-	if (ethnla_put_strz(msgbuff, CTRL_ATTR_FAMILY_NAME, info->fam_name))
+	if (ethnla_put_strz(msgbuff, CTRL_ATTR_FAMILY_NAME, ETHTOOL_GENL_NAME))
 		goto out;
 
 	nlsock_sendmsg(nlsk, NULL);
-	nlsock_process_reply(nlsk, family_info_cb, info);
-	ret = info->fam_id ? 0 : -EADDRNOTAVAIL;
+	nlsock_process_reply(nlsk, family_info_cb, nlctx);
+	ret = nlctx->ethnl_fam ? 0 : -EADDRNOTAVAIL;
 
 out:
-	nlsk->nlctx->suppress_nlerr = 0;
+	nlctx->suppress_nlerr = 0;
 	return ret;
 }
 #endif
 
 int netlink_init(struct cmd_context *ctx)
 {
-	struct fam_info info = {
-		.fam_name	= ETHTOOL_GENL_NAME,
-		.grp_name	= ETHTOOL_MCGRP_MONITOR_NAME,
-	};
 	struct nl_context *nlctx;
 	int ret;
 
@@ -189,11 +262,9 @@ int netlink_init(struct cmd_context *ctx)
 	ret = nlsock_init(nlctx, &nlctx->ethnl_socket, NETLINK_GENERIC);
 	if (ret < 0)
 		goto out_free;
-	ret = get_genl_family(nlctx->ethnl_socket, &info);
+	ret = get_genl_family(nlctx, nlctx->ethnl_socket);
 	if (ret < 0)
 		goto out_nlsk;
-	nlctx->ethnl_fam = info.fam_id;
-	nlctx->ethnl_mongrp = info.grp_id;
 
 	ctx->nlctx = nlctx;
 	return 0;
@@ -201,6 +272,7 @@ int netlink_init(struct cmd_context *ctx)
 out_nlsk:
 	nlsock_done(nlctx->ethnl_socket);
 out_free:
+	free(nlctx->ops_flags);
 	free(nlctx);
 	return ret;
 }
@@ -210,6 +282,7 @@ static void netlink_done(struct cmd_context *ctx)
 	if (!ctx->nlctx)
 		return;
 
+	free(ctx->nlctx->ops_flags);
 	free(ctx->nlctx);
 	ctx->nlctx = NULL;
 	cleanup_all_strings();
@@ -228,6 +301,7 @@ void netlink_run_handler(struct cmd_context *ctx, nl_func_t nlfunc,
 			 bool no_fallback)
 {
 	bool wildcard = ctx->devname && !strcmp(ctx->devname, WILDCARD_DEVNAME);
+	struct nl_context *nlctx;
 	const char *reason;
 	int ret;
 
@@ -245,12 +319,20 @@ void netlink_run_handler(struct cmd_context *ctx, nl_func_t nlfunc,
 		reason = "netlink interface initialization failed";
 		goto no_support;
 	}
+	nlctx = ctx->nlctx;
 
 	ret = nlfunc(ctx);
 	netlink_done(ctx);
-	if (ret != -EOPNOTSUPP || no_fallback)
+	if (no_fallback || ret != -EOPNOTSUPP || !nlctx->ioctl_fallback) {
+		if (nlctx->wildcard_unsupported)
+			fprintf(stderr, "%s\n",
+				"subcommand does not support wildcard dump");
 		exit(ret >= 0 ? ret : 1);
-	reason = "kernel netlink support for subcommand missing";
+	}
+	if (nlctx->wildcard_unsupported)
+		reason = "subcommand does not support wildcard dump";
+	else
+		reason = "kernel netlink support for subcommand missing";
 
 no_support:
 	if (no_fallback) {
netlink/netlink.h +5 -0
index 41102d270eaf..4419be0f751a 100644
@@ -25,6 +25,7 @@ struct nl_context {
 	unsigned int		suppress_nlerr;
 	uint16_t		ethnl_fam;
 	uint32_t		ethnl_mongrp;
+	uint32_t		*ops_flags;
 	struct nl_socket	*ethnl_socket;
 	struct nl_socket	*ethnl2_socket;
 	struct nl_socket	*rtnl_socket;
@@ -36,6 +37,8 @@ struct nl_context {
 	const char		*param;
 	char			**argp;
 	int			argc;
+	bool			ioctl_fallback;
+	bool			wildcard_unsupported;
 };
 
 struct attr_tb_info {
@@ -50,6 +53,8 @@ int nomsg_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 int attr_cb(const struct nlattr *attr, void *data);
 
 int netlink_init(struct cmd_context *ctx);
+bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd,
+		       bool allow_wildcard);
 const char *get_dev_name(const struct nlattr *nest);
 int get_dev_info(const struct nlattr *nest, int *ifindex, char *ifname);
 
netlink/parser.c +7 -0
index 40eb4a5c0b26..fff23f2425e9 100644
@@ -1023,6 +1023,13 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
 			goto out_free;
 	}
 
+	if (group_style == PARSER_GROUP_MSG) {
+		ret = -EOPNOTSUPP;
+		for (buff = buffs; buff; buff = buff->next)
+			if (msgbuff_len(&buff->msgbuff) > buff->orig_len &&
+			    netlink_cmd_check(nlctx->ctx, buff->id, false))
+				goto out_free;
+	}
 	for (buff = buffs; buff; buff = buff->next) {
 		struct nl_msg_buff *msgbuff = &buff->msgbuff;
 
netlink/settings.c +7 -0
index c8a911d718b9..25aadb988cd1 100644
@@ -726,6 +726,13 @@ int nl_gset(struct cmd_context *ctx)
 	struct nl_socket *nlsk = nlctx->ethnl_socket;
 	int ret;
 
+	if (netlink_cmd_check(ctx, ETHTOOL_MSG_LINKMODES_GET, true) ||
+	    netlink_cmd_check(ctx, ETHTOOL_MSG_LINKINFO_GET, true) ||
+	    netlink_cmd_check(ctx, ETHTOOL_MSG_WOL_GET, true) ||
+	    netlink_cmd_check(ctx, ETHTOOL_MSG_DEBUG_GET, true) ||
+	    netlink_cmd_check(ctx, ETHTOOL_MSG_LINKSTATE_GET, true))
+		return -EOPNOTSUPP;
+
 	nlctx->suppress_nlerr = 1;
 
 	ret = gset_request(nlsk, ETHTOOL_MSG_LINKMODES_GET,
Signature · 1 line
2.26.2
#3 lore raw
In reply to Michal Kubecek
Quoted 16 lines · Michal Kubecek

At the moment, ethtool falls back to ioctl implementation whenever either in response to request type not implemented in kernel or netlink interface is unavailable or netlink request fails with EOPNOTSUPP error code. This is not perfect as EOPNOTSUPP can have different meanings and we only want to fall back if it is caused by kernel lacking netlink implementation of the request. In other cases, we would needlessly repeat the same failure trying both netlink and ioctl.

These two patches improve the logic to avoid such duplicate failures and improve handling of cases where fallback to ioctl is impossible for other reasons (e.g. wildcard device name or no ioctl handler).

Michal Kubecek (2):
refactor interface between ioctl and netlink code
netlink: use genetlink ops information to decide about fallback

Quoted 8 lines
 ethtool.c          |  51 +++---------
 netlink/extapi.h   |  14 ++--
 netlink/monitor.c  |  15 +++-
 netlink/netlink.c  | 193 ++++++++++++++++++++++++++++++++++++++-------
 netlink/netlink.h  |   6 ++
 netlink/parser.c   |   7 ++
 netlink/settings.c |   7 ++
 7 files changed, 220 insertions(+), 73 deletions(-)

Thanks (belatedly) -- queued for next release!

John

Signature · 2 lines
John W. Linville		Someday the world will need a hero, and you
[email protected]			might be all we have.  Be ready.