From patchwork Sat Apr 20 02:35:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13636908 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F3F36FB0; Sat, 20 Apr 2024 02:35:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580557; cv=none; b=C6zXWYr/FED7CKGbXCO+BpPDP+VzB5H4MHYfGTVtVncVyXzjY93ovmozRoFEleTVMtNLklr+102NttTRuiOPci/6v9V2uMsP7oEEcIdNkc2K1+tiVJm3OkyPgSCYSttgWugRmQaFeuuWIST8m6JqsDUAM1xtRVZB6AcrdSS6kzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580557; c=relaxed/simple; bh=9s0PpQWr+QekpYHM80WK83YbLRvMu+xsycegi6JRwoU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hlB9mJbtP0MhgZfaoIbhh2e1Au7cIb8QoQ7qvsn1v0yvPMEyoFtCPDkQkv3KDPkuoZcJ6z3klRyIvSFbTnb+ZtUGqhNq71S0N8Gcmtp+V5E8F+S5vYGgWuoITPRr/f9ns/oJHKTOT0vdGefFDMY8p8TiVTxsQv0liaYeDVpZ0uM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fKcDwhyU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fKcDwhyU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E504C32782; Sat, 20 Apr 2024 02:35:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713580557; bh=9s0PpQWr+QekpYHM80WK83YbLRvMu+xsycegi6JRwoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fKcDwhyUtlxyE2rnnvzoQcaDVaXqUWzTl97q1c2w5WX2M7MfJubVJawK/52xtyBSt 3pGkhgA8cYX15LriihvFCyqQE1Psqi8nsBzh9cYvWb1ZmdVdZGF914N0acEwLAxnUc OjRYz+g1tvc3QU+idpW+wb0LpzwpMfARXaVpjU/f0Y4fk3MzpOdzL2WdIKQh/Z9N/5 flw2bA24Kwv2wk+7yX7sSbsJaYIWYHvBpW8vD6idkVkRTkiTjCFU57iHl7t6p41+0l GRryejlcvFh6wmjci7CmymRn/t2sz8cWV5ScN3fEBjMLXJRaH5ZI0oKMV+oBhJf5QX uhwaEDnHWaMZg== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, sdf@google.com, amritha.nambiar@intel.com, linux-kselftest@vger.kernel.org, dsahern@gmail.com, Jakub Kicinski Subject: [PATCH net-next 1/4] netdev: support dumping a single netdev in qstats Date: Fri, 19 Apr 2024 19:35:39 -0700 Message-ID: <20240420023543.3300306-2-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240420023543.3300306-1-kuba@kernel.org> References: <20240420023543.3300306-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Having to filter the right ifindex in the tests is a bit tedious. Add support for dumping qstats for a single ifindex. Signed-off-by: Jakub Kicinski Reviewed-by: David Ahern Reviewed-by: Eric Dumazet --- Documentation/netlink/specs/netdev.yaml | 1 + net/core/netdev-genl-gen.c | 1 + net/core/netdev-genl.c | 52 ++++++++++++++++++------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Documentation/netlink/specs/netdev.yaml b/Documentation/netlink/specs/netdev.yaml index 76352dbd2be4..679c4130707c 100644 --- a/Documentation/netlink/specs/netdev.yaml +++ b/Documentation/netlink/specs/netdev.yaml @@ -486,6 +486,7 @@ name: netdev dump: request: attributes: + - ifindex - scope reply: attributes: diff --git a/net/core/netdev-genl-gen.c b/net/core/netdev-genl-gen.c index 8d8ace9ef87f..8350a0afa9ec 100644 --- a/net/core/netdev-genl-gen.c +++ b/net/core/netdev-genl-gen.c @@ -70,6 +70,7 @@ static const struct nla_policy netdev_napi_get_dump_nl_policy[NETDEV_A_NAPI_IFIN /* NETDEV_CMD_QSTATS_GET - dump */ static const struct nla_policy netdev_qstats_get_nl_policy[NETDEV_A_QSTATS_SCOPE + 1] = { + [NETDEV_A_QSTATS_IFINDEX] = NLA_POLICY_MIN(NLA_U32, 1), [NETDEV_A_QSTATS_SCOPE] = NLA_POLICY_MASK(NLA_UINT, 0x1), }; diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c index 7004b3399c2b..dd6510f2c652 100644 --- a/net/core/netdev-genl.c +++ b/net/core/netdev-genl.c @@ -639,6 +639,24 @@ netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp, return -EMSGSIZE; } +static int +netdev_nl_qstats_get_dump_one(struct net_device *netdev, unsigned int scope, + struct sk_buff *skb, const struct genl_info *info, + struct netdev_nl_dump_ctx *ctx) +{ + if (!netdev->stat_ops) + return 0; + + switch (scope) { + case 0: + return netdev_nl_stats_by_netdev(netdev, skb, info); + case NETDEV_QSTATS_SCOPE_QUEUE: + return netdev_nl_stats_by_queue(netdev, skb, info, ctx); + } + + return -EINVAL; /* Should not happen, per netlink policy */ +} + int netdev_nl_qstats_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) { @@ -646,6 +664,7 @@ int netdev_nl_qstats_get_dumpit(struct sk_buff *skb, const struct genl_info *info = genl_info_dump(cb); struct net *net = sock_net(skb->sk); struct net_device *netdev; + unsigned int ifindex; unsigned int scope; int err = 0; @@ -653,21 +672,28 @@ int netdev_nl_qstats_get_dumpit(struct sk_buff *skb, if (info->attrs[NETDEV_A_QSTATS_SCOPE]) scope = nla_get_uint(info->attrs[NETDEV_A_QSTATS_SCOPE]); - rtnl_lock(); - for_each_netdev_dump(net, netdev, ctx->ifindex) { - if (!netdev->stat_ops) - continue; + ifindex = 0; + if (info->attrs[NETDEV_A_QSTATS_IFINDEX]) + ifindex = nla_get_u32(info->attrs[NETDEV_A_QSTATS_IFINDEX]); - switch (scope) { - case 0: - err = netdev_nl_stats_by_netdev(netdev, skb, info); - break; - case NETDEV_QSTATS_SCOPE_QUEUE: - err = netdev_nl_stats_by_queue(netdev, skb, info, ctx); - break; + rtnl_lock(); + if (ifindex) { + netdev = __dev_get_by_index(net, ifindex); + if (netdev && netdev->stat_ops) { + err = netdev_nl_qstats_get_dump_one(netdev, scope, skb, + info, ctx); + } else { + NL_SET_BAD_ATTR(info->extack, + info->attrs[NETDEV_A_QSTATS_IFINDEX]); + err = netdev ? -EOPNOTSUPP : -ENODEV; + } + } else { + for_each_netdev_dump(net, netdev, ctx->ifindex) { + err = netdev_nl_qstats_get_dump_one(netdev, scope, skb, + info, ctx); + if (err < 0) + break; } - if (err < 0) - break; } rtnl_unlock(); From patchwork Sat Apr 20 02:35:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13636909 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21AD3846B; Sat, 20 Apr 2024 02:35:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580558; cv=none; b=FhUCcYaKkRi7KrP7Vt+egqe149tiPgOkzYkK2HKmuh2bMgY415fqgtthQL7WArLP6rHTiS+Qun/lRxTTF7Zi1Y/69+RNygukNFuhv3J3hUfYDwJAYiWU60hYw06WqTN869s+nv6KcCGRsFqVirX6Id8Ff56qEznqMm7lPpt2esg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580558; c=relaxed/simple; bh=spQJtF/HsP8yoN5+KzKsqhBojxsYdBcRuNU80ChfsUc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f4yPTbEnG7E6khgjgKJ4xMrE0WGgupqnamnlMLBp+KjDFQaUACOoYWmVAvNut4K1NOYaqc2zW9HsDBqKVEt3AfU8dAesmKTaTp5kV72phuvdx8hYBJvd7IkzXpOAk2dQO+NWKAJYkm6NP85reQZQ6IMKSyKv32jh2IKkCs7NjtE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fO85k0jY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fO85k0jY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F575C2BD10; Sat, 20 Apr 2024 02:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713580557; bh=spQJtF/HsP8yoN5+KzKsqhBojxsYdBcRuNU80ChfsUc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fO85k0jYNwJnRkR9WX02vYYauE+qjt0DhLo0OzWM0xkwF8zVh7z2uUittp4LBKUZQ I9wuM5Ue8gJT8Fad2LPRIYrcdcuOhO2XyQ64R9wl43hZQhrvPY94ebI95P+Rn2AgHN PnybFxwpLKMp3zi8uPndZLz+EgNVNgL3oARnEdXbGTQXriZLArfKsNffq1ts50jPa/ XcZTNoaQRqBempKfJp61LiTXRHUeEattCo4+2ykR3pkwud/cfrorZx5cS6SDgokhbM kdRis06G2P0/h77TAlutqHblWGKcG2dO1dgr5cDDVzFFa16taQA2dxK7MAWBVOywxJ KfrwF4GiZT2AQ== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, sdf@google.com, amritha.nambiar@intel.com, linux-kselftest@vger.kernel.org, dsahern@gmail.com, Jakub Kicinski Subject: [PATCH net-next 2/4] netlink: move extack writing helpers Date: Fri, 19 Apr 2024 19:35:40 -0700 Message-ID: <20240420023543.3300306-3-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240420023543.3300306-1-kuba@kernel.org> References: <20240420023543.3300306-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Next change will need them in netlink_dump_done(), pure move. Signed-off-by: Jakub Kicinski Reviewed-by: David Ahern --- net/netlink/af_netlink.c | 126 +++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index dc8c3c01d51b..c5bb09597831 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2165,6 +2165,69 @@ __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int fla } EXPORT_SYMBOL(__nlmsg_put); +static size_t +netlink_ack_tlv_len(struct netlink_sock *nlk, int err, + const struct netlink_ext_ack *extack) +{ + size_t tlvlen; + + if (!extack || !test_bit(NETLINK_F_EXT_ACK, &nlk->flags)) + return 0; + + tlvlen = 0; + if (extack->_msg) + tlvlen += nla_total_size(strlen(extack->_msg) + 1); + if (extack->cookie_len) + tlvlen += nla_total_size(extack->cookie_len); + + /* Following attributes are only reported as error (not warning) */ + if (!err) + return tlvlen; + + if (extack->bad_attr) + tlvlen += nla_total_size(sizeof(u32)); + if (extack->policy) + tlvlen += netlink_policy_dump_attr_size_estimate(extack->policy); + if (extack->miss_type) + tlvlen += nla_total_size(sizeof(u32)); + if (extack->miss_nest) + tlvlen += nla_total_size(sizeof(u32)); + + return tlvlen; +} + +static void +netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, + struct nlmsghdr *nlh, int err, + const struct netlink_ext_ack *extack) +{ + if (extack->_msg) + WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg)); + if (extack->cookie_len) + WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, + extack->cookie_len, extack->cookie)); + + if (!err) + return; + + if (extack->bad_attr && + !WARN_ON((u8 *)extack->bad_attr < in_skb->data || + (u8 *)extack->bad_attr >= in_skb->data + in_skb->len)) + WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, + (u8 *)extack->bad_attr - (u8 *)nlh)); + if (extack->policy) + netlink_policy_dump_write_attr(skb, extack->policy, + NLMSGERR_ATTR_POLICY); + if (extack->miss_type) + WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_TYPE, + extack->miss_type)); + if (extack->miss_nest && + !WARN_ON((u8 *)extack->miss_nest < in_skb->data || + (u8 *)extack->miss_nest > in_skb->data + in_skb->len)) + WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_NEST, + (u8 *)extack->miss_nest - (u8 *)nlh)); +} + /* * It looks a bit ugly. * It would be better to create kernel thread. @@ -2406,69 +2469,6 @@ int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, } EXPORT_SYMBOL(__netlink_dump_start); -static size_t -netlink_ack_tlv_len(struct netlink_sock *nlk, int err, - const struct netlink_ext_ack *extack) -{ - size_t tlvlen; - - if (!extack || !test_bit(NETLINK_F_EXT_ACK, &nlk->flags)) - return 0; - - tlvlen = 0; - if (extack->_msg) - tlvlen += nla_total_size(strlen(extack->_msg) + 1); - if (extack->cookie_len) - tlvlen += nla_total_size(extack->cookie_len); - - /* Following attributes are only reported as error (not warning) */ - if (!err) - return tlvlen; - - if (extack->bad_attr) - tlvlen += nla_total_size(sizeof(u32)); - if (extack->policy) - tlvlen += netlink_policy_dump_attr_size_estimate(extack->policy); - if (extack->miss_type) - tlvlen += nla_total_size(sizeof(u32)); - if (extack->miss_nest) - tlvlen += nla_total_size(sizeof(u32)); - - return tlvlen; -} - -static void -netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, - struct nlmsghdr *nlh, int err, - const struct netlink_ext_ack *extack) -{ - if (extack->_msg) - WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg)); - if (extack->cookie_len) - WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, - extack->cookie_len, extack->cookie)); - - if (!err) - return; - - if (extack->bad_attr && - !WARN_ON((u8 *)extack->bad_attr < in_skb->data || - (u8 *)extack->bad_attr >= in_skb->data + in_skb->len)) - WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, - (u8 *)extack->bad_attr - (u8 *)nlh)); - if (extack->policy) - netlink_policy_dump_write_attr(skb, extack->policy, - NLMSGERR_ATTR_POLICY); - if (extack->miss_type) - WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_TYPE, - extack->miss_type)); - if (extack->miss_nest && - !WARN_ON((u8 *)extack->miss_nest < in_skb->data || - (u8 *)extack->miss_nest > in_skb->data + in_skb->len)) - WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_NEST, - (u8 *)extack->miss_nest - (u8 *)nlh)); -} - void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err, const struct netlink_ext_ack *extack) { From patchwork Sat Apr 20 02:35:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13636910 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADABAAD51; Sat, 20 Apr 2024 02:35:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580558; cv=none; b=pHJy3VedT5E7l6WUEI9e7pv2ok0xGkb1LjRq0LjzEP4AYpiYck02dtT2gGXSr3tGdn0WKhcPGjY+jDwHimzec0FqieWzldYHmFimnv7zI7Q2jY8TdfTT0wOb5hHuSHETcrwV2qEkyPRDmew0DIpq339oW43mzTuzbdGpfGgKFd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580558; c=relaxed/simple; bh=momb5cFYBoc1k0B9mFw2kRYFezIu6vVQ4eyYyoV9SDw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PqmjN21iF5hF4VcnIdnKG2V8zy4g3bGpfFJq1Ir9kbSPIHX3t110/7jy4ZM5j2lKLnF8WAEYMFfgzmhp4SRkr5bBLDdYm3nwRPvRkE8nFutOZjAzCpXnk17pUQLw1CoWRCJ9Dnf/jaiZCideL5mXK/W9r4w2oYGcPWEwZ5M/0Yc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FaCW9NOP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FaCW9NOP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE524C4AF0C; Sat, 20 Apr 2024 02:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713580558; bh=momb5cFYBoc1k0B9mFw2kRYFezIu6vVQ4eyYyoV9SDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FaCW9NOP+GRCbqd88Iv1zrEUApoIrjyhhuCg1cNelLZag9mKLEMp9PxL0x8Yi0d9D CqzjFj1mfVXzncTnCr4sDBd/u2h7hK+r5greGjIABnIYYTsBXg2AVAa3PdwoXknXvz z5+UGwHzM6sfwYGrtqodwVdwPIdLaQ5uTQYgGbgfKxZg4VcBsWwQR+8JguC1DaiCF9 8xIMm67afqGiUQnTxp9e6AQBGmI6VoRnbqSD8BDxInCg7gIcziVMHu7OUOewoZDOpT SLtO905KqOPuXh0U4RYyAYrnlW155NU4kq5Ok9Nmmca3ZrA7x7aHI3T5I5BtMwwVaN xsnkIOtM9bq2w== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, sdf@google.com, amritha.nambiar@intel.com, linux-kselftest@vger.kernel.org, dsahern@gmail.com, Jakub Kicinski Subject: [PATCH net-next 3/4] netlink: support all extack types in dumps Date: Fri, 19 Apr 2024 19:35:41 -0700 Message-ID: <20240420023543.3300306-4-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240420023543.3300306-1-kuba@kernel.org> References: <20240420023543.3300306-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Note that when this commit message refers to netlink dump it only means the actual dumping part, the parsing / dump start is handled by the same code as "doit". Commit 4a19edb60d02 ("netlink: Pass extack to dump handlers") added support for returning extack messages from dump handlers, but left out other extack info, e.g. bad attribute. This used to be fine because until YNL we had little practical use for the machine readable attributes, and only messages were used in practice. YNL flips the preference 180 degrees, it's now much more useful to point to a bad attr with NL_SET_BAD_ATTR() than type an English message saying "attribute XYZ is $reason-why-bad". Support all of extack. The fact that extack only gets added if it fits remains unaddressed. Signed-off-by: Jakub Kicinski Reviewed-by: David Ahern --- net/netlink/af_netlink.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index c5bb09597831..fa9c090cf629 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2198,7 +2198,7 @@ netlink_ack_tlv_len(struct netlink_sock *nlk, int err, static void netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, - struct nlmsghdr *nlh, int err, + const struct nlmsghdr *nlh, int err, const struct netlink_ext_ack *extack) { if (extack->_msg) @@ -2214,7 +2214,7 @@ netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, !WARN_ON((u8 *)extack->bad_attr < in_skb->data || (u8 *)extack->bad_attr >= in_skb->data + in_skb->len)) WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, - (u8 *)extack->bad_attr - (u8 *)nlh)); + (u8 *)extack->bad_attr - (const u8 *)nlh)); if (extack->policy) netlink_policy_dump_write_attr(skb, extack->policy, NLMSGERR_ATTR_POLICY); @@ -2225,7 +2225,7 @@ netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, !WARN_ON((u8 *)extack->miss_nest < in_skb->data || (u8 *)extack->miss_nest > in_skb->data + in_skb->len)) WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_NEST, - (u8 *)extack->miss_nest - (u8 *)nlh)); + (u8 *)extack->miss_nest - (const u8 *)nlh)); } /* @@ -2238,6 +2238,7 @@ static int netlink_dump_done(struct netlink_sock *nlk, struct sk_buff *skb, struct netlink_ext_ack *extack) { struct nlmsghdr *nlh; + size_t extack_len; nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(nlk->dump_done_errno), NLM_F_MULTI | cb->answer_flags); @@ -2247,10 +2248,14 @@ static int netlink_dump_done(struct netlink_sock *nlk, struct sk_buff *skb, nl_dump_check_consistent(cb, nlh); memcpy(nlmsg_data(nlh), &nlk->dump_done_errno, sizeof(nlk->dump_done_errno)); - if (extack->_msg && test_bit(NETLINK_F_EXT_ACK, &nlk->flags)) { + extack_len = netlink_ack_tlv_len(nlk, nlk->dump_done_errno, extack); + if (extack_len) { nlh->nlmsg_flags |= NLM_F_ACK_TLVS; - if (!nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg)) + if (skb_tailroom(skb) >= extack_len) { + netlink_ack_tlv_fill(cb->skb, skb, cb->nlh, + nlk->dump_done_errno, extack); nlmsg_end(skb, nlh); + } } return 0; From patchwork Sat Apr 20 02:35:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13636911 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CC60D52A; Sat, 20 Apr 2024 02:35:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580559; cv=none; b=T/tr5n/c+iVL5HD7pQsZ1SJQdH0/6TgKmHR8/Oikft2SqcODXiF4l+MHZvJE9PjY+Sq775IxE3X9+zmsuE3a/gjEsJhyHjgRS82MP+3hzsfWalrBnyuGzrBEjroUGVA/7VFbmF/RAa3MtbmaGrTWyCExv1u8yaXNMVLhm19yaog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713580559; c=relaxed/simple; bh=/zBdyd1Mme/sptQ9EdUM8tqm9YYlq4wrqQG/3XLLSTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FIHnH0oJsCKCSZRQGUz818fy3qTV65sbMlKib/OYfRdJTLkPchdLayli2onVkh9BHkYgUcjFUJkSJji28iIDLJmsLOEf2jtV++S+CfEFXXhI+C9Z1qYsewUATT7qOsbio1B9sAf4fpX9vy+WvROAA8XvlGUmn/OaCvE+MiOor2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VVKlXjLy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VVKlXjLy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71CCBC4AF13; Sat, 20 Apr 2024 02:35:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713580558; bh=/zBdyd1Mme/sptQ9EdUM8tqm9YYlq4wrqQG/3XLLSTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VVKlXjLysBxetKKCeViYq+D7ULAM6S9HHnuHei7l5rvo6dbW7J1/VAy+VH9LuSC0R wBZ9lKW/xLJMBSiwBRr5FrtBwJ8jwc3ioGkX80xUSDMQsYP9KpmHFmhAfVEsgats+3 cCP/zms7oNLHufTLRQeJZ4cWNvkFomiMVVw3fYXb+/mjUqhXBcfCd96Ku8F0k4wM78 t51JCykEoIg6CXb2jO0Cu9PEn3q8PzNXEtdmbi65bQrJJm74YHIm+N8l8fAoiQf1LT F2z4LiIFAILUgXnVqbnTWYsaidZoJqWt/62YtZNrIGayakCSNSI795mzI78LAo7cj5 drS2D2A19ou2g== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, sdf@google.com, amritha.nambiar@intel.com, linux-kselftest@vger.kernel.org, dsahern@gmail.com, Jakub Kicinski Subject: [PATCH net-next 4/4] selftests: drv-net: test dumping qstats per device Date: Fri, 19 Apr 2024 19:35:42 -0700 Message-ID: <20240420023543.3300306-5-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240420023543.3300306-1-kuba@kernel.org> References: <20240420023543.3300306-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a test for dumping qstats device by device. ksft framework grows a ksft_raises() helper, to be used under with, which should be familiar to unittest users. Signed-off-by: Jakub Kicinski --- tools/testing/selftests/drivers/net/stats.py | 62 +++++++++++++++++++- tools/testing/selftests/net/lib/py/ksft.py | 18 ++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/drivers/net/stats.py b/tools/testing/selftests/drivers/net/stats.py index 947df3eb681f..7a7b16b180e2 100755 --- a/tools/testing/selftests/drivers/net/stats.py +++ b/tools/testing/selftests/drivers/net/stats.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0 -from lib.py import ksft_run, ksft_exit -from lib.py import ksft_in, ksft_true, KsftSkipEx, KsftXfailEx +from lib.py import ksft_run, ksft_exit, ksft_pr +from lib.py import ksft_ge, ksft_eq, ksft_in, ksft_true, ksft_raises, KsftSkipEx, KsftXfailEx from lib.py import EthtoolFamily, NetdevFamily, RtnlFamily, NlError from lib.py import NetDrvEnv @@ -77,9 +77,65 @@ rtnl = RtnlFamily() raise Exception("Qstats are lower, fetched later") +def qstat_by_ifindex(cfg) -> None: + global netfam + global rtnl + + # Construct a map ifindex -> [dump, by-index, dump] + ifindexes = {} + stats = netfam.qstats_get({}, dump=True) + for entry in stats: + ifindexes[entry['ifindex']] = [entry, None, None] + + for ifindex in ifindexes.keys(): + entry = netfam.qstats_get({"ifindex": ifindex}, dump=True) + ksft_eq(len(entry), 1) + ifindexes[entry[0]['ifindex']][1] = entry[0] + + stats = netfam.qstats_get({}, dump=True) + for entry in stats: + ifindexes[entry['ifindex']][2] = entry + + if len(ifindexes) == 0: + raise KsftSkipEx("No ifindex supports qstats") + + # Now make sure the stats match/make sense + for ifindex, triple in ifindexes.items(): + all_keys = triple[0].keys() | triple[1].keys() | triple[2].keys() + + for key in all_keys: + ksft_ge(triple[1][key], triple[0][key], comment="bad key: " + key) + ksft_ge(triple[2][key], triple[1][key], comment="bad key: " + key) + + # Test invalid dumps + # 0 is invalid + with ksft_raises(NlError) as cm: + netfam.qstats_get({"ifindex": 0}, dump=True) + ksft_eq(cm.exception.nl_msg.error, -34) + ksft_eq(cm.exception.nl_msg.extack['bad-attr'], '.ifindex') + + # loopback has no stats + with ksft_raises(NlError) as cm: + netfam.qstats_get({"ifindex": 1}, dump=True) + ksft_eq(cm.exception.nl_msg.error, -95) + ksft_eq(cm.exception.nl_msg.extack['bad-attr'], '.ifindex') + + # Try to get stats for lowest unused ifindex but not 0 + devs = rtnl.getlink({}, dump=True) + all_ifindexes = set([dev["ifi-index"] for dev in devs]) + lowest = 2 + while lowest in all_ifindexes: + lowest += 1 + + with ksft_raises(NlError) as cm: + netfam.qstats_get({"ifindex": lowest}, dump=True) + ksft_eq(cm.exception.nl_msg.error, -19) + ksft_eq(cm.exception.nl_msg.extack['bad-attr'], '.ifindex') + + def main() -> None: with NetDrvEnv(__file__) as cfg: - ksft_run([check_pause, check_fec, pkt_byte_sum], + ksft_run([check_pause, check_fec, pkt_byte_sum, qstat_by_ifindex], args=(cfg, )) ksft_exit() diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index 25f2572fa540..e7f79f6185b0 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -53,6 +53,24 @@ KSFT_RESULT_ALL = True _fail("Check failed", a, "<", b, comment) +class ksft_raises: + def __init__(self, expected_type): + self.exception = None + self.expected_type = expected_type + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is None: + _fail(f"Expected exception {str(self.expected_type.__name__)}, none raised") + elif self.expected_type != exc_type: + _fail(f"Expected exception {str(self.expected_type.__name__)}, raised {str(exc_type.__name__)}") + self.exception = exc_val + # Suppress the exception if its the expected one + return self.expected_type == exc_type + + def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""): end = time.monotonic() + deadline while True: