Blame SOURCES/0032-netlink-use-policy-dumping-to-check-if-stats-flag-is.patch

268297
From 5966f5dae0dd173fdc7fe34af5f400e36fe782c7 Mon Sep 17 00:00:00 2001
268297
From: Jakub Kicinski <kuba@kernel.org>
268297
Date: Sun, 18 Oct 2020 14:31:50 -0700
268297
Subject: [PATCH 32/37] netlink: use policy dumping to check if stats flag is
268297
 supported
268297
268297
Older kernels don't support statistics, to avoid retries
268297
make use of netlink policy dumps to figure out which
268297
flags kernel actually supports.
268297
268297
v3:
268297
 - s/ctx/policy_ctx/
268297
 - save the flags in nl_context to be able to reuse them,
268297
   and not have to return errors and values from the policy
268297
   get function
268297
268297
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
268297
Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
268297
(cherry picked from commit 5c90128a47d7c96cc4dd2c4ad26a0fed1ab60940)
268297
---
268297
 netlink/msgbuff.h |   6 ++
268297
 netlink/netlink.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++
268297
 netlink/netlink.h |   4 ++
268297
 3 files changed, 164 insertions(+)
268297
268297
diff --git a/netlink/msgbuff.h b/netlink/msgbuff.h
268297
index 24b99c5a28d7..7d6731fc24a3 100644
268297
--- a/netlink/msgbuff.h
268297
+++ b/netlink/msgbuff.h
268297
@@ -81,6 +81,12 @@ static inline bool ethnla_put_u32(struct nl_msg_buff *msgbuff, uint16_t type,
268297
 	return ethnla_put(msgbuff, type, sizeof(uint32_t), &data);
268297
 }
268297
 
268297
+static inline bool ethnla_put_u16(struct nl_msg_buff *msgbuff, uint16_t type,
268297
+				  uint16_t data)
268297
+{
268297
+	return ethnla_put(msgbuff, type, sizeof(uint16_t), &data);
268297
+}
268297
+
268297
 static inline bool ethnla_put_u8(struct nl_msg_buff *msgbuff, uint16_t type,
268297
 				 uint8_t data)
268297
 {
268297
diff --git a/netlink/netlink.c b/netlink/netlink.c
268297
index 5677274c2fce..ffe06339f099 100644
268297
--- a/netlink/netlink.c
268297
+++ b/netlink/netlink.c
268297
@@ -135,6 +135,160 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd,
268297
 	return !(nlctx->ops_info[cmd].op_flags & cap);
268297
 }
268297
 
268297
+struct ethtool_op_policy_query_ctx {
268297
+	struct nl_context *nlctx;
268297
+	unsigned int op;
268297
+	unsigned int op_hdr_attr;
268297
+
268297
+	bool op_policy_found;
268297
+	bool hdr_policy_found;
268297
+	unsigned int op_policy_idx;
268297
+	unsigned int hdr_policy_idx;
268297
+	uint64_t flag_mask;
268297
+};
268297
+
268297
+static int family_policy_find_op(struct ethtool_op_policy_query_ctx *policy_ctx,
268297
+				 const struct nlattr *op_policy)
268297
+{
268297
+	const struct nlattr *attr;
268297
+	unsigned int type;
268297
+	int ret;
268297
+
268297
+	type = policy_ctx->nlctx->is_dump ?
268297
+		CTRL_ATTR_POLICY_DUMP : CTRL_ATTR_POLICY_DO;
268297
+
268297
+	mnl_attr_for_each_nested(attr, op_policy) {
268297
+		const struct nlattr *tb[CTRL_ATTR_POLICY_DUMP_MAX + 1] = {};
268297
+		DECLARE_ATTR_TB_INFO(tb);
268297
+
268297
+		if (mnl_attr_get_type(attr) != policy_ctx->op)
268297
+			continue;
268297
+
268297
+		ret = mnl_attr_parse_nested(attr, attr_cb, &tb_info);
268297
+		if (ret < 0)
268297
+			return ret;
268297
+
268297
+		if (!tb[type])
268297
+			continue;
268297
+
268297
+		policy_ctx->op_policy_found = true;
268297
+		policy_ctx->op_policy_idx = mnl_attr_get_u32(tb[type]);
268297
+		break;
268297
+	}
268297
+
268297
+	return 0;
268297
+}
268297
+
268297
+static int family_policy_cb(const struct nlmsghdr *nlhdr, void *data)
268297
+{
268297
+	const struct nlattr *tba[NL_POLICY_TYPE_ATTR_MAX + 1] = {};
268297
+	DECLARE_ATTR_TB_INFO(tba);
268297
+	const struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
268297
+	DECLARE_ATTR_TB_INFO(tb);
268297
+	struct ethtool_op_policy_query_ctx *policy_ctx = data;
268297
+	const struct nlattr *policy_attr, *attr_attr, *attr;
268297
+	unsigned int attr_idx, policy_idx;
268297
+	int ret;
268297
+
268297
+	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
268297
+	if (ret < 0)
268297
+		return MNL_CB_ERROR;
268297
+
268297
+	if (!policy_ctx->op_policy_found) {
268297
+		if (!tb[CTRL_ATTR_OP_POLICY]) {
268297
+			fprintf(stderr, "Error: op policy map not present\n");
268297
+			return MNL_CB_ERROR;
268297
+		}
268297
+		ret = family_policy_find_op(policy_ctx, tb[CTRL_ATTR_OP_POLICY]);
268297
+		return ret < 0 ? MNL_CB_ERROR : MNL_CB_OK;
268297
+	}
268297
+
268297
+	if (!tb[CTRL_ATTR_POLICY])
268297
+		return MNL_CB_OK;
268297
+
268297
+	policy_attr = mnl_attr_get_payload(tb[CTRL_ATTR_POLICY]);
268297
+	policy_idx = mnl_attr_get_type(policy_attr);
268297
+	attr_attr = mnl_attr_get_payload(policy_attr);
268297
+	attr_idx = mnl_attr_get_type(attr_attr);
268297
+
268297
+	ret = mnl_attr_parse_nested(attr_attr, attr_cb, &tba_info);
268297
+	if (ret < 0)
268297
+		return MNL_CB_ERROR;
268297
+
268297
+	if (policy_idx == policy_ctx->op_policy_idx &&
268297
+	    attr_idx == policy_ctx->op_hdr_attr) {
268297
+		attr = tba[NL_POLICY_TYPE_ATTR_POLICY_IDX];
268297
+		if (!attr) {
268297
+			fprintf(stderr,	"Error: no policy index in what was expected to be ethtool header attribute\n");
268297
+			return MNL_CB_ERROR;
268297
+		}
268297
+		policy_ctx->hdr_policy_found = true;
268297
+		policy_ctx->hdr_policy_idx = mnl_attr_get_u32(attr);
268297
+	}
268297
+
268297
+	if (policy_ctx->hdr_policy_found &&
268297
+	    policy_ctx->hdr_policy_idx == policy_idx &&
268297
+	    attr_idx == ETHTOOL_A_HEADER_FLAGS) {
268297
+		attr = tba[NL_POLICY_TYPE_ATTR_MASK];
268297
+		if (!attr) {
268297
+			fprintf(stderr,	"Error: validation mask not reported for ethtool header flags\n");
268297
+			return MNL_CB_ERROR;
268297
+		}
268297
+
268297
+		policy_ctx->flag_mask = mnl_attr_get_u64(attr);
268297
+	}
268297
+
268297
+	return MNL_CB_OK;
268297
+}
268297
+
268297
+static int read_flags_policy(struct nl_context *nlctx, struct nl_socket *nlsk,
268297
+			     unsigned int nlcmd, unsigned int hdrattr)
268297
+{
268297
+	struct ethtool_op_policy_query_ctx policy_ctx;
268297
+	struct nl_msg_buff *msgbuff = &nlsk->msgbuff;
268297
+	int ret;
268297
+
268297
+	if (nlctx->ops_info[nlcmd].hdr_policy_loaded)
268297
+		return 0;
268297
+
268297
+	memset(&policy_ctx, 0, sizeof(policy_ctx));
268297
+	policy_ctx.nlctx = nlctx;
268297
+	policy_ctx.op = nlcmd;
268297
+	policy_ctx.op_hdr_attr = hdrattr;
268297
+
268297
+	ret = __msg_init(msgbuff, GENL_ID_CTRL, CTRL_CMD_GETPOLICY,
268297
+			 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP, 1);
268297
+	if (ret < 0)
268297
+		return ret;
268297
+	ret = -EMSGSIZE;
268297
+	if (ethnla_put_u16(msgbuff, CTRL_ATTR_FAMILY_ID, nlctx->ethnl_fam))
268297
+		return ret;
268297
+	if (ethnla_put_u32(msgbuff, CTRL_ATTR_OP, nlcmd))
268297
+		return ret;
268297
+
268297
+	nlsock_sendmsg(nlsk, NULL);
268297
+	nlsock_process_reply(nlsk, family_policy_cb, &policy_ctx);
268297
+
268297
+	nlctx->ops_info[nlcmd].hdr_policy_loaded = 1;
268297
+	nlctx->ops_info[nlcmd].hdr_flags = policy_ctx.flag_mask;
268297
+	return 0;
268297
+}
268297
+
268297
+u32 get_stats_flag(struct nl_context *nlctx, unsigned int nlcmd,
268297
+		   unsigned int hdrattr)
268297
+{
268297
+	if (!nlctx->ctx->show_stats)
268297
+		return 0;
268297
+	if (nlcmd > ETHTOOL_MSG_USER_MAX ||
268297
+	    !(nlctx->ops_info[nlcmd].op_flags & GENL_CMD_CAP_HASPOL))
268297
+		return 0;
268297
+
268297
+	if (read_flags_policy(nlctx, nlctx->ethnl_socket, nlcmd, hdrattr) < 0)
268297
+		return 0;
268297
+
268297
+	return nlctx->ops_info[nlcmd].hdr_flags & ETHTOOL_FLAG_STATS;
268297
+}
268297
+
268297
 /* initialization */
268297
 
268297
 static int genl_read_ops(struct nl_context *nlctx,
268297
diff --git a/netlink/netlink.h b/netlink/netlink.h
268297
index e79143016bd5..c02558540218 100644
268297
--- a/netlink/netlink.h
268297
+++ b/netlink/netlink.h
268297
@@ -27,6 +27,8 @@ enum link_mode_class {
268297
 
268297
 struct nl_op_info {
268297
 	uint32_t		op_flags;
268297
+	uint32_t		hdr_flags;
268297
+	uint8_t			hdr_policy_loaded:1;
268297
 };
268297
 
268297
 struct nl_context {
268297
@@ -70,6 +72,8 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd,
268297
 		       bool allow_wildcard);
268297
 const char *get_dev_name(const struct nlattr *nest);
268297
 int get_dev_info(const struct nlattr *nest, int *ifindex, char *ifname);
268297
+u32 get_stats_flag(struct nl_context *nlctx, unsigned int nlcmd,
268297
+		   unsigned int hdrattr);
268297
 
268297
 int linkmodes_reply_cb(const struct nlmsghdr *nlhdr, void *data);
268297
 int linkinfo_reply_cb(const struct nlmsghdr *nlhdr, void *data);
268297
-- 
268297
2.26.2
268297