From patchwork Sun Oct 18 21:31:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843425 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B197C433DF for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE4FE22267 for ; Sun, 18 Oct 2020 21:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056724; bh=TanqdjBUf0rjKInxfQwL21fLYLSWFj3b+In8ojzGMdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=kIGEdZor/mqPE12e3vaun/0e9tsF6f0fxBvkfavF9/xauvMK8TG3I3O4TBkj8wAX6 wW2NhltoJiCaJ8Z4bu67AOxv0+dZ4E2NqXD0feTn1ml2ZMVAHnwRCLbC3O8g6H3ojo uy3ZAI8no6daXz1a+xJoDNFkhsgXyh8LJaHfuZhI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728099AbgJRVcA (ORCPT ); Sun, 18 Oct 2020 17:32:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:46994 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728036AbgJRVb7 (ORCPT ); Sun, 18 Oct 2020 17:31:59 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1F79C22280; Sun, 18 Oct 2020 21:31:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056719; bh=TanqdjBUf0rjKInxfQwL21fLYLSWFj3b+In8ojzGMdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y5x9DMyoBPU8yk/sBSaneUzMwROZ32jPfWf0QXFvwYnE/H8rxMjdWazQSUqdww7Hz Lyyuf8MR3V+GCeSh3mNCteGlJDS5dtSh8Zqo/BhxYdz4zwOqPkJ+V7d4Vjk+qIcSAV FfdoEUL8EYGyCzNuEq53waIIp/azBOojQpSTlvxY= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 1/7] update UAPI header copies Date: Sun, 18 Oct 2020 14:31:45 -0700 Message-Id: <20201018213151.3450437-2-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Update to kernel commit 9453b2d4694c. Signed-off-by: Jakub Kicinski --- uapi/linux/genetlink.h | 11 +++++++++++ uapi/linux/netlink.h | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/uapi/linux/genetlink.h b/uapi/linux/genetlink.h index 7c6c390c48ee..9fa720ee87ae 100644 --- a/uapi/linux/genetlink.h +++ b/uapi/linux/genetlink.h @@ -64,6 +64,8 @@ enum { CTRL_ATTR_OPS, CTRL_ATTR_MCAST_GROUPS, CTRL_ATTR_POLICY, + CTRL_ATTR_OP_POLICY, + CTRL_ATTR_OP, __CTRL_ATTR_MAX, }; @@ -85,6 +87,15 @@ enum { __CTRL_ATTR_MCAST_GRP_MAX, }; +enum { + CTRL_ATTR_POLICY_UNSPEC, + CTRL_ATTR_POLICY_DO, + CTRL_ATTR_POLICY_DUMP, + + __CTRL_ATTR_POLICY_DUMP_MAX, + CTRL_ATTR_POLICY_DUMP_MAX = __CTRL_ATTR_POLICY_DUMP_MAX - 1 +}; + #define CTRL_ATTR_MCAST_GRP_MAX (__CTRL_ATTR_MCAST_GRP_MAX - 1) diff --git a/uapi/linux/netlink.h b/uapi/linux/netlink.h index 695c88e3c29d..dfef006be9f9 100644 --- a/uapi/linux/netlink.h +++ b/uapi/linux/netlink.h @@ -129,6 +129,7 @@ struct nlmsgerr { * @NLMSGERR_ATTR_COOKIE: arbitrary subsystem specific cookie to * be used - in the success case - to identify a created * object or operation or similar (binary) + * @NLMSGERR_ATTR_POLICY: policy for a rejected attribute * @__NLMSGERR_ATTR_MAX: number of attributes * @NLMSGERR_ATTR_MAX: highest attribute number */ @@ -137,6 +138,7 @@ enum nlmsgerr_attrs { NLMSGERR_ATTR_MSG, NLMSGERR_ATTR_OFFS, NLMSGERR_ATTR_COOKIE, + NLMSGERR_ATTR_POLICY, __NLMSGERR_ATTR_MAX, NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1 @@ -327,6 +329,7 @@ enum netlink_attribute_type { * the index, if limited inside the nesting (U32) * @NL_POLICY_TYPE_ATTR_BITFIELD32_MASK: valid mask for the * bitfield32 type (U32) + * @NL_POLICY_TYPE_ATTR_MASK: mask of valid bits for unsigned integers (U64) * @NL_POLICY_TYPE_ATTR_PAD: pad attribute for 64-bit alignment */ enum netlink_policy_type_attr { @@ -342,6 +345,7 @@ enum netlink_policy_type_attr { NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE, NL_POLICY_TYPE_ATTR_BITFIELD32_MASK, NL_POLICY_TYPE_ATTR_PAD, + NL_POLICY_TYPE_ATTR_MASK, /* keep last */ __NL_POLICY_TYPE_ATTR_MAX, From patchwork Sun Oct 18 21:31:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843429 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C0BFC43457 for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EB13922268 for ; Sun, 18 Oct 2020 21:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056725; bh=zoZ+0kjLHvCLFkFzCWm5daKUx3r60Kvsci5LHnZYE58=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=g1+K+gE73kru7sVSacbLu1rbXmm0j0nNCmnstN/kpVfhiYnldKAdz2oks0jQ+o5HH EIwQOuiSwuvKsnunPx03gmmjfZ8JzoGphGLl5qoUNKmfoRJ4l/wrGbF5v3ldMvZdSx O14Nyz/31/DRFeuqOwwwieOBbeXyujvlxdEb0PlA= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728168AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:47004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726458AbgJRVcA (ORCPT ); Sun, 18 Oct 2020 17:32:00 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 73E82222C2; Sun, 18 Oct 2020 21:31:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056719; bh=zoZ+0kjLHvCLFkFzCWm5daKUx3r60Kvsci5LHnZYE58=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JzAuxjjcHUK9zo16ONeCJ6KaudU87Rf8fcla9ojVn0d7BCOU/8zDndgWcYHqEaaEB m00hCG1nMOklr+EVRGUq4Vz9bDW5nMEmBbaiseZxxGUzwpoOYaagMh3AtCMB/ws4dO 4+WdGs9+3IZzpx5ovIr81SNftWnJbtyK9/HiyPNo= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 2/7] pause: add --json support Date: Sun, 18 Oct 2020 14:31:46 -0700 Message-Id: <20201018213151.3450437-3-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz No change in normal text output: # ./ethtool -a eth0 Pause parameters for eth0: Autonegotiate: on RX: on TX: on RX negotiated: on TX negotiated: on JSON: # ./ethtool --json -a eth0 [ { "ifname": "eth0", "autonegotiate": true, "rx": true, "tx": true, "negotiated": { "rx": true, "tx": true } } ] v2: - restructure show_bool() so we can use its logic for show_bool_val() Signed-off-by: Jakub Kicinski --- netlink/coalesce.c | 6 +++--- netlink/netlink.h | 21 ++++++++++++++++----- netlink/pause.c | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/netlink/coalesce.c b/netlink/coalesce.c index 65f75cf9a8dd..07a92d04b7a1 100644 --- a/netlink/coalesce.c +++ b/netlink/coalesce.c @@ -36,9 +36,9 @@ int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data) if (silent) putchar('\n'); printf("Coalesce parameters for %s:\n", nlctx->devname); - printf("Adaptive RX: %s TX: %s\n", - u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]), - u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX])); + show_bool("rx", "Adaptive RX: %s ", + tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]); + show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]); show_u32(tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS], "stats-block-usecs: "); show_u32(tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL], diff --git a/netlink/netlink.h b/netlink/netlink.h index dd4a02bcc916..1012e8e32cd8 100644 --- a/netlink/netlink.h +++ b/netlink/netlink.h @@ -98,17 +98,28 @@ static inline void show_u32(const struct nlattr *attr, const char *label) printf("%sn/a\n", label); } -static inline const char *u8_to_bool(const struct nlattr *attr) +static inline const char *u8_to_bool(const uint8_t *val) { - if (attr) - return mnl_attr_get_u8(attr) ? "on" : "off"; + if (val) + return *val ? "on" : "off"; else return "n/a"; } -static inline void show_bool(const struct nlattr *attr, const char *label) +static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val) +{ + if (is_json_context()) { + if (val) + print_bool(PRINT_JSON, key, NULL, val); + } else { + print_string(PRINT_FP, NULL, fmt, u8_to_bool(val)); + } +} + +static inline void show_bool(const char *key, const char *fmt, + const struct nlattr *attr) { - printf("%s%s\n", label, u8_to_bool(attr)); + show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL); } /* misc */ diff --git a/netlink/pause.c b/netlink/pause.c index 7b6b3a1d2c10..c960c82cba5f 100644 --- a/netlink/pause.c +++ b/netlink/pause.c @@ -40,8 +40,8 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data) struct pause_autoneg_status ours = {}; struct pause_autoneg_status peer = {}; struct nl_context *nlctx = data; - bool rx_status = false; - bool tx_status = false; + uint8_t rx_status = false; + uint8_t tx_status = false; bool silent; int err_ret; int ret; @@ -72,8 +72,11 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data) else if (peer.pause) tx_status = true; } - printf("RX negotiated: %s\nTX negotiated: %s\n", - rx_status ? "on" : "off", tx_status ? "on" : "off"); + + open_json_object("negotiated"); + show_bool_val("rx", "RX negotiated: %s\n", &rx_status); + show_bool_val("tx", "TX negotiated: %s\n", &tx_status); + close_json_object(); return MNL_CB_OK; } @@ -121,21 +124,34 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data) return err_ret; if (silent) - putchar('\n'); - printf("Pause parameters for %s:\n", nlctx->devname); - show_bool(tb[ETHTOOL_A_PAUSE_AUTONEG], "Autonegotiate:\t"); - show_bool(tb[ETHTOOL_A_PAUSE_RX], "RX:\t\t"); - show_bool(tb[ETHTOOL_A_PAUSE_TX], "TX:\t\t"); + print_nl(); + + open_json_object(NULL); + + print_string(PRINT_ANY, "ifname", "Pause parameters for %s:\n", + nlctx->devname); + + show_bool("autonegotiate", "Autonegotiate:\t%s\n", + tb[ETHTOOL_A_PAUSE_AUTONEG]); + show_bool("rx", "RX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_RX]); + show_bool("tx", "TX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_TX]); + if (!nlctx->is_monitor && tb[ETHTOOL_A_PAUSE_AUTONEG] && mnl_attr_get_u8(tb[ETHTOOL_A_PAUSE_AUTONEG])) { ret = show_pause_autoneg_status(nlctx); if (ret < 0) - return err_ret; + goto err_close_dev; } if (!silent) - putchar('\n'); + print_nl(); + + close_json_object(); return MNL_CB_OK; + +err_close_dev: + close_json_object(); + return err_ret; } int nl_gpause(struct cmd_context *ctx) @@ -156,7 +172,11 @@ int nl_gpause(struct cmd_context *ctx) ETHTOOL_A_PAUSE_HEADER, 0); if (ret < 0) return ret; - return nlsock_send_get_request(nlsk, pause_reply_cb); + + new_json_obj(ctx->json); + ret = nlsock_send_get_request(nlsk, pause_reply_cb); + delete_json_obj(); + return ret; } /* PAUSE_SET */ From patchwork Sun Oct 18 21:31:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843439 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E457C433E7 for ; Sun, 18 Oct 2020 21:32:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D3ECB22280 for ; Sun, 18 Oct 2020 21:32:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056733; bh=7jZD/GtUD3QHyHulr4ysB0ecL1g3/Rs1Jq5fu1SCE4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=JcWukCRrN5sxkvrk331VM/bB7hRA6CxWjkmNVsT4ap+H5TlnQlKELdZ811xZ4CIZU IeKwND5mEe7Gp3176zRw9cX2jpinnSKBnoO4IWKSjWB5PWOMluReOV9ApV7T16MTn9 mzP1MJFJ6tH/yJNtwVvv6fKpttfpacM0uj9tR578= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726458AbgJRVcM (ORCPT ); Sun, 18 Oct 2020 17:32:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:47014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728067AbgJRVcA (ORCPT ); Sun, 18 Oct 2020 17:32:00 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C6F4C222C4; Sun, 18 Oct 2020 21:31:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056720; bh=7jZD/GtUD3QHyHulr4ysB0ecL1g3/Rs1Jq5fu1SCE4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cbRy3H6/e/LVXz81X8LzkgdSK7zzsQVM6gdTT27O817hwJ+Z3iY8+t352KV/3/e45 SzctHeRtI4sPoRuiS4de40P6VDWT6/gDw/8xwRXkLWVP8Ozmq7uBcC+Z3XOBR97hD6 YAXya7+eSxQosOTRV7I2JFED1pIYlR6FlPRT5n3c= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 3/7] separate FLAGS out in -h Date: Sun, 18 Oct 2020 14:31:47 -0700 Message-Id: <20201018213151.3450437-4-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Help output is quite crowded already with every command being prefixed by --debug and --json options, and we're about to add a third one. Add an indirection. Signed-off-by: Jakub Kicinski --- ethtool.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ethtool.c b/ethtool.c index 2a7de97c51bb..403616bb7fa0 100644 --- a/ethtool.c +++ b/ethtool.c @@ -6015,10 +6015,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused) fprintf(stdout, PACKAGE " version " VERSION "\n"); fprintf(stdout, "Usage:\n" - " ethtool [ --debug MASK ][ --json ] DEVNAME\t" + " ethtool [ FLAGS ] DEVNAME\t" "Display standard information about device\n"); for (i = 0; args[i].opts; i++) { - fputs(" ethtool [ --debug MASK ][ --json ] ", stdout); + fputs(" ethtool [ FLAGS ] ", stdout); fprintf(stdout, "%s %s\t%s\n", args[i].opts, args[i].no_dev ? "\t" : "DEVNAME", @@ -6027,7 +6027,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused) fputs(args[i].xhelp, stdout); } nl_monitor_usage(); - fprintf(stdout, "Not all options support JSON output\n"); + fprintf(stdout, "\n"); + fprintf(stdout, "FLAGS:\n"); + fprintf(stdout, " --debug MASK turn on debugging messages\n"); + fprintf(stdout, " --json enable JSON output format (not supported by all commands)\n"); return 0; } From patchwork Sun Oct 18 21:31:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843435 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0ABEC433DF for ; Sun, 18 Oct 2020 21:32:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 850EA22268 for ; Sun, 18 Oct 2020 21:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056728; bh=yXlYCoOLipCk/06sWYrK1oOxN3qsJsTcRc2Eo16HeT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=UouIjyFQlkk4a1NVJUG1aM0lcmIkREvZ+43foIsOIg21XVvoYhZraS/gZ9WzTKxBG Ga+h+yLezI2FUdusnWHjuMapMpgnG+EhDZ9mv/gknvVDm61Crglm1XIo1F/0csJvy4 B70E+wduc8G4BRrNKE1zP+RN5/3PaUHG7+7+I4Zw= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728144AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:47032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728084AbgJRVcA (ORCPT ); Sun, 18 Oct 2020 17:32:00 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2597C222D5; Sun, 18 Oct 2020 21:32:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056720; bh=yXlYCoOLipCk/06sWYrK1oOxN3qsJsTcRc2Eo16HeT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uA6bDyDtyt7vhiGuc1ieUGS5ELO7zYNYvdwOtdtX2nwKkC7/lQgeQfRAsc7yuXQuV +gmX5nhTsa3Gsiho99sfwLhM0yjAOExrDksqxzib2IwHDBwU0FFl0NberFiBWSqs5+ 8Ha0Fx2z/ABgEr5VGtg9CrG+FtXX6U+/0wEk5uH4= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 4/7] add support for stats in subcommands Date: Sun, 18 Oct 2020 14:31:48 -0700 Message-Id: <20201018213151.3450437-5-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Add new parameters (-I | --include-statistics) which will control requesting statistic dumps from the kernel. Signed-off-by: Jakub Kicinski --- ethtool.8.in | 7 +++++++ ethtool.c | 8 ++++++++ internal.h | 1 + 3 files changed, 16 insertions(+) diff --git a/ethtool.8.in b/ethtool.8.in index 9beb1e5791eb..429c75f3f682 100644 --- a/ethtool.8.in +++ b/ethtool.8.in @@ -140,6 +140,9 @@ ethtool \- query or control network driver and hardware settings .B ethtool [--json] .I args .HP +.B ethtool [-I | --include-statistics] +.I args +.HP .B ethtool \-\-monitor [ .I command @@ -499,6 +502,10 @@ Output results in JavaScript Object Notation (JSON). Only a subset of options support this. Those which do not will continue to output plain text in the presence of this option. .TP +.B \-I \-\-include\-statistics +Include command-related statistics in the output. This option allows +displaying relevant device statistics for selected get commands. +.TP .B \-a \-\-show\-pause Queries the specified Ethernet device for pause parameter information. .TP diff --git a/ethtool.c b/ethtool.c index 403616bb7fa0..1d9067e774af 100644 --- a/ethtool.c +++ b/ethtool.c @@ -6031,6 +6031,7 @@ static int show_usage(struct cmd_context *ctx __maybe_unused) fprintf(stdout, "FLAGS:\n"); fprintf(stdout, " --debug MASK turn on debugging messages\n"); fprintf(stdout, " --json enable JSON output format (not supported by all commands)\n"); + fprintf(stdout, " -I|--include-statistics request device statistics related to the command (not supported by all commands)\n"); return 0; } @@ -6291,6 +6292,13 @@ int main(int argc, char **argp) argc -= 1; continue; } + if (*argp && (!strcmp(*argp, "--include-statistics") || + !strcmp(*argp, "-I"))) { + ctx.show_stats = true; + argp += 1; + argc -= 1; + continue; + } break; } if (*argp && !strcmp(*argp, "--monitor")) { diff --git a/internal.h b/internal.h index 935ebac3ca2e..27da8eac57bb 100644 --- a/internal.h +++ b/internal.h @@ -225,6 +225,7 @@ struct cmd_context { char **argp; /* arguments to the sub-command */ unsigned long debug; /* debugging mask */ bool json; /* Output JSON, if supported */ + bool show_stats; /* include command-specific stats */ #ifdef ETHTOOL_ENABLE_NETLINK struct nl_context *nlctx; /* netlink context (opaque) */ #endif From patchwork Sun Oct 18 21:31:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843433 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C04EC433E7 for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1883522280 for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056725; bh=4n0rObmDSbi7LahmXq9n2QapHkJN41lZHJPAcMtVJB4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=NyrCuIvIZ9Grdo1zJz/c9jW5C2nxvw2RGXTIrYIZpr/mVoEmcrX31LbrmWdVjLfXi RjcCfwnrEAAU18OsMHDyOq3xqhACl9VB8/s+dR0WRYP8jgy81QjcLmpuvyPLE+i26e jBNe7kKKwy2xSrijdo2QZXoalbQbzi52cCVRumkY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728189AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:47014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728036AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 79B37222E7; Sun, 18 Oct 2020 21:32:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056720; bh=4n0rObmDSbi7LahmXq9n2QapHkJN41lZHJPAcMtVJB4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bRVKlLcq1vNSQgruyYq/eVW2dFj15FvXHRYsMOxwZWh21Ti2zSqaVzEoU1tZLMgv2 2DxjgpnzKIjFkLv7hXf/c00xnOhxQczZlVwHm405VEiZQmStLfuuqTSZ+nmqIB93qy +tUX7O8kJtNEwioz4qSa+OtXADWNxqRaEbLtMfHY= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 5/7] netlink: prepare for more per-op info Date: Sun, 18 Oct 2020 14:31:49 -0700 Message-Id: <20201018213151.3450437-6-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz We stored an array of op flags, to check if operations are supported. Make that array a structure rather than plain uint32_t in preparation for storing more state. v3: new patch Signed-off-by: Jakub Kicinski --- netlink/netlink.c | 25 +++++++++++++------------ netlink/netlink.h | 6 +++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/netlink/netlink.c b/netlink/netlink.c index e42d57076a4b..86dc1efdf5ce 100644 --- a/netlink/netlink.c +++ b/netlink/netlink.c @@ -120,19 +120,19 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd, nlctx->wildcard_unsupported = true; return true; } - if (!nlctx->ops_flags) { + if (!nlctx->ops_info) { nlctx->ioctl_fallback = true; return false; } - if (cmd > ETHTOOL_MSG_USER_MAX || !nlctx->ops_flags[cmd]) { + if (cmd > ETHTOOL_MSG_USER_MAX || !nlctx->ops_info[cmd].op_flags) { nlctx->ioctl_fallback = true; return true; } - if (is_dump && !(nlctx->ops_flags[cmd] & GENL_CMD_CAP_DUMP)) + if (is_dump && !(nlctx->ops_info[cmd].op_flags & GENL_CMD_CAP_DUMP)) nlctx->wildcard_unsupported = true; - return !(nlctx->ops_flags[cmd] & cap); + return !(nlctx->ops_info[cmd].op_flags & cap); } /* initialization */ @@ -140,12 +140,12 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd, static int genl_read_ops(struct nl_context *nlctx, const struct nlattr *ops_attr) { + struct nl_op_info *ops_info; struct nlattr *op_attr; - uint32_t *ops_flags; int ret; - ops_flags = calloc(__ETHTOOL_MSG_USER_CNT, sizeof(ops_flags[0])); - if (!ops_flags) + ops_info = calloc(__ETHTOOL_MSG_USER_CNT, sizeof(ops_info[0])); + if (!ops_info) return -ENOMEM; mnl_attr_for_each_nested(op_attr, ops_attr) { @@ -163,13 +163,14 @@ static int genl_read_ops(struct nl_context *nlctx, if (op_id >= __ETHTOOL_MSG_USER_CNT) continue; - ops_flags[op_id] = mnl_attr_get_u32(tb[CTRL_ATTR_OP_FLAGS]); + ops_info[op_id].op_flags = + mnl_attr_get_u32(tb[CTRL_ATTR_OP_FLAGS]); } - nlctx->ops_flags = ops_flags; + nlctx->ops_info = ops_info; return 0; err: - free(ops_flags); + free(ops_info); return ret; } @@ -273,7 +274,7 @@ int netlink_init(struct cmd_context *ctx) out_nlsk: nlsock_done(nlctx->ethnl_socket); out_free: - free(nlctx->ops_flags); + free(nlctx->ops_info); free(nlctx); return ret; } @@ -283,7 +284,7 @@ static void netlink_done(struct cmd_context *ctx) if (!ctx->nlctx) return; - free(ctx->nlctx->ops_flags); + free(ctx->nlctx->ops_info); free(ctx->nlctx); ctx->nlctx = NULL; cleanup_all_strings(); diff --git a/netlink/netlink.h b/netlink/netlink.h index 1012e8e32cd8..e79143016bd5 100644 --- a/netlink/netlink.h +++ b/netlink/netlink.h @@ -25,6 +25,10 @@ enum link_mode_class { LM_CLASS_FEC, }; +struct nl_op_info { + uint32_t op_flags; +}; + struct nl_context { struct cmd_context *ctx; void *cmd_private; @@ -34,7 +38,7 @@ struct nl_context { unsigned int suppress_nlerr; uint16_t ethnl_fam; uint32_t ethnl_mongrp; - uint32_t *ops_flags; + struct nl_op_info *ops_info; struct nl_socket *ethnl_socket; struct nl_socket *ethnl2_socket; struct nl_socket *rtnl_socket; From patchwork Sun Oct 18 21:31:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843437 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66A22C43467 for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3AA2422267 for ; Sun, 18 Oct 2020 21:32:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056725; bh=5ifuAkzKnv6A4Vme/DxuArxYZ/Ig0wqbuCQ7R7EhgHI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=jxOXi1BhxFCcV0HVAX39XFFDRQjKAMn+bj6ylUcg9FEH2Y+Lh8QCdWxl+NMB3K0aQ QptIw/vhKX+hQ0UuSMnhuK5GkFh4H4f/WRKNfoSKHz4Ay1qdm3CUZ+0Jx0hxk8YVwS hhQDtTDj49GT76g1QqoW9JVc3h1rcw1LzFr85M0o= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728206AbgJRVcE (ORCPT ); Sun, 18 Oct 2020 17:32:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:47004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728084AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CFF53222EA; Sun, 18 Oct 2020 21:32:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056721; bh=5ifuAkzKnv6A4Vme/DxuArxYZ/Ig0wqbuCQ7R7EhgHI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d0Bmi6YhcTJ5ZFTWrsaIjIiGDV6Sw0DFs5whldeQr6Z7+B6iZD0FRkIjA/0VPIDt2 XbofJXSA/qOxGNzOeDNcUyodDBczzgogFUuDYnnH1UM4yOa7+eX13RIXNkmEdRhQ6r MRcjWh2KIDYms75IFrgCF1xqdHnFAxxI4XKdmNHs= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 6/7] netlink: use policy dumping to check if stats flag is supported Date: Sun, 18 Oct 2020 14:31:50 -0700 Message-Id: <20201018213151.3450437-7-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Older kernels don't support statistics, to avoid retries make use of netlink policy dumps to figure out which flags kernel actually supports. v3: - s/ctx/policy_ctx/ - save the flags in nl_context to be able to reuse them, and not have to return errors and values from the policy get function Signed-off-by: Jakub Kicinski --- netlink/msgbuff.h | 6 ++ netlink/netlink.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++ netlink/netlink.h | 4 ++ 3 files changed, 164 insertions(+) diff --git a/netlink/msgbuff.h b/netlink/msgbuff.h index 24b99c5a28d7..7d6731fc24a3 100644 --- a/netlink/msgbuff.h +++ b/netlink/msgbuff.h @@ -81,6 +81,12 @@ static inline bool ethnla_put_u32(struct nl_msg_buff *msgbuff, uint16_t type, return ethnla_put(msgbuff, type, sizeof(uint32_t), &data); } +static inline bool ethnla_put_u16(struct nl_msg_buff *msgbuff, uint16_t type, + uint16_t data) +{ + return ethnla_put(msgbuff, type, sizeof(uint16_t), &data); +} + static inline bool ethnla_put_u8(struct nl_msg_buff *msgbuff, uint16_t type, uint8_t data) { diff --git a/netlink/netlink.c b/netlink/netlink.c index 86dc1efdf5ce..f655f6ea25b7 100644 --- a/netlink/netlink.c +++ b/netlink/netlink.c @@ -135,6 +135,160 @@ bool netlink_cmd_check(struct cmd_context *ctx, unsigned int cmd, return !(nlctx->ops_info[cmd].op_flags & cap); } +struct ethtool_op_policy_query_ctx { + struct nl_context *nlctx; + unsigned int op; + unsigned int op_hdr_attr; + + bool op_policy_found; + bool hdr_policy_found; + unsigned int op_policy_idx; + unsigned int hdr_policy_idx; + uint64_t flag_mask; +}; + +static int family_policy_find_op(struct ethtool_op_policy_query_ctx *policy_ctx, + const struct nlattr *op_policy) +{ + const struct nlattr *attr; + unsigned int type; + int ret; + + type = policy_ctx->nlctx->is_dump ? + CTRL_ATTR_POLICY_DUMP : CTRL_ATTR_POLICY_DO; + + mnl_attr_for_each_nested(attr, op_policy) { + const struct nlattr *tb[CTRL_ATTR_POLICY_DUMP_MAX + 1] = {}; + DECLARE_ATTR_TB_INFO(tb); + + if (mnl_attr_get_type(attr) != policy_ctx->op) + continue; + + ret = mnl_attr_parse_nested(attr, attr_cb, &tb_info); + if (ret < 0) + return ret; + + if (!tb[type]) + continue; + + policy_ctx->op_policy_found = true; + policy_ctx->op_policy_idx = mnl_attr_get_u32(tb[type]); + break; + } + + return 0; +} + +static int family_policy_cb(const struct nlmsghdr *nlhdr, void *data) +{ + const struct nlattr *tba[NL_POLICY_TYPE_ATTR_MAX + 1] = {}; + DECLARE_ATTR_TB_INFO(tba); + const struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; + DECLARE_ATTR_TB_INFO(tb); + struct ethtool_op_policy_query_ctx *policy_ctx = data; + const struct nlattr *policy_attr, *attr_attr, *attr; + unsigned int attr_idx, policy_idx; + int ret; + + ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info); + if (ret < 0) + return MNL_CB_ERROR; + + if (!policy_ctx->op_policy_found) { + if (!tb[CTRL_ATTR_OP_POLICY]) { + fprintf(stderr, "Error: op policy map not present\n"); + return MNL_CB_ERROR; + } + ret = family_policy_find_op(policy_ctx, tb[CTRL_ATTR_OP_POLICY]); + return ret < 0 ? MNL_CB_ERROR : MNL_CB_OK; + } + + if (!tb[CTRL_ATTR_POLICY]) + return MNL_CB_OK; + + policy_attr = mnl_attr_get_payload(tb[CTRL_ATTR_POLICY]); + policy_idx = mnl_attr_get_type(policy_attr); + attr_attr = mnl_attr_get_payload(policy_attr); + attr_idx = mnl_attr_get_type(attr_attr); + + ret = mnl_attr_parse_nested(attr_attr, attr_cb, &tba_info); + if (ret < 0) + return MNL_CB_ERROR; + + if (policy_idx == policy_ctx->op_policy_idx && + attr_idx == policy_ctx->op_hdr_attr) { + attr = tba[NL_POLICY_TYPE_ATTR_POLICY_IDX]; + if (!attr) { + fprintf(stderr, "Error: no policy index in what was expected to be ethtool header attribute\n"); + return MNL_CB_ERROR; + } + policy_ctx->hdr_policy_found = true; + policy_ctx->hdr_policy_idx = mnl_attr_get_u32(attr); + } + + if (policy_ctx->hdr_policy_found && + policy_ctx->hdr_policy_idx == policy_idx && + attr_idx == ETHTOOL_A_HEADER_FLAGS) { + attr = tba[NL_POLICY_TYPE_ATTR_MASK]; + if (!attr) { + fprintf(stderr, "Error: validation mask not reported for ethtool header flags\n"); + return MNL_CB_ERROR; + } + + policy_ctx->flag_mask = mnl_attr_get_u64(attr); + } + + return MNL_CB_OK; +} + +static int read_flags_policy(struct nl_context *nlctx, struct nl_socket *nlsk, + unsigned int nlcmd, unsigned int hdrattr) +{ + struct ethtool_op_policy_query_ctx policy_ctx; + struct nl_msg_buff *msgbuff = &nlsk->msgbuff; + int ret; + + if (nlctx->ops_info[nlcmd].hdr_policy_loaded) + return 0; + + memset(&policy_ctx, 0, sizeof(policy_ctx)); + policy_ctx.nlctx = nlctx; + policy_ctx.op = nlcmd; + policy_ctx.op_hdr_attr = hdrattr; + + ret = __msg_init(msgbuff, GENL_ID_CTRL, CTRL_CMD_GETPOLICY, + NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP, 1); + if (ret < 0) + return ret; + ret = -EMSGSIZE; + if (ethnla_put_u16(msgbuff, CTRL_ATTR_FAMILY_ID, nlctx->ethnl_fam)) + return ret; + if (ethnla_put_u32(msgbuff, CTRL_ATTR_OP, nlcmd)) + return ret; + + nlsock_sendmsg(nlsk, NULL); + nlsock_process_reply(nlsk, family_policy_cb, &policy_ctx); + + nlctx->ops_info[nlcmd].hdr_policy_loaded = 1; + nlctx->ops_info[nlcmd].hdr_flags = policy_ctx.flag_mask; + return 0; +} + +u32 get_stats_flag(struct nl_context *nlctx, unsigned int nlcmd, + unsigned int hdrattr) +{ + if (!nlctx->ctx->show_stats) + return 0; + if (nlcmd > ETHTOOL_MSG_USER_MAX || + !(nlctx->ops_info[nlcmd].op_flags & GENL_CMD_CAP_HASPOL)) + return 0; + + if (read_flags_policy(nlctx, nlctx->ethnl_socket, nlcmd, hdrattr) < 0) + return 0; + + return nlctx->ops_info[nlcmd].hdr_flags & ETHTOOL_FLAG_STATS; +} + /* initialization */ static int genl_read_ops(struct nl_context *nlctx, diff --git a/netlink/netlink.h b/netlink/netlink.h index e79143016bd5..c02558540218 100644 --- a/netlink/netlink.h +++ b/netlink/netlink.h @@ -27,6 +27,8 @@ enum link_mode_class { struct nl_op_info { uint32_t op_flags; + uint32_t hdr_flags; + uint8_t hdr_policy_loaded:1; }; struct nl_context { @@ -70,6 +72,8 @@ 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); +u32 get_stats_flag(struct nl_context *nlctx, unsigned int nlcmd, + unsigned int hdrattr); int linkmodes_reply_cb(const struct nlmsghdr *nlhdr, void *data); int linkinfo_reply_cb(const struct nlmsghdr *nlhdr, void *data); From patchwork Sun Oct 18 21:31:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 11843427 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8D14C43457 for ; Sun, 18 Oct 2020 21:32:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B1087222BA for ; Sun, 18 Oct 2020 21:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056728; bh=kOc0SU+wBfjrVQZ8eee9WVB+ukBN0CzbCnEbg/duRf0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=u2zLxVHF3ipcpANDfw7gC2lS0Hp7/neF9E+LlqMUIodWg/612U7S9WauO/sqaBa99 NkBMq/7MGx8JK1WiBpDR+Y3ePL39MVr0JOXH26wdVRNlSlM9C5t0fl9Ipd8Lqimlg3 U75bQl9YWsL0SK8PGmAnuTENkCORbg11wTzAGDwE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728221AbgJRVcE (ORCPT ); Sun, 18 Oct 2020 17:32:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:47032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726458AbgJRVcB (ORCPT ); Sun, 18 Oct 2020 17:32:01 -0400 Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2D89822268; Sun, 18 Oct 2020 21:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603056721; bh=kOc0SU+wBfjrVQZ8eee9WVB+ukBN0CzbCnEbg/duRf0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xsLgKjbNpZ5/HESlGf+5BxUyFY+x+2gdlNrJNhHBB3xyIGsUCJ9gAmhPh8ap45Ztc R1oyCsnlRRZuXdsnXApYvZC+IN+3rK8FC+HF3UZ7xWOwBwJRbh1Tx8lHkZC4ge2txQ pSJ7g+1E2rohWZy+PDO0j/eKyOvLvezmZrdZtyBQ= From: Jakub Kicinski To: mkubecek@suse.cz Cc: netdev@vger.kernel.org, kernel-team@fb.com, idosch@idosch.org, Jakub Kicinski Subject: [PATCH ethtool v3 7/7] pause: add support for dumping statistics Date: Sun, 18 Oct 2020 14:31:51 -0700 Message-Id: <20201018213151.3450437-8-kuba@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201018213151.3450437-1-kuba@kernel.org> References: <20201018213151.3450437-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Add support for requesting pause frame stats from the kernel. # ./ethtool -I -a eth0 Pause parameters for eth0: Autonegotiate: on RX: on TX: on Statistics: tx_pause_frames: 1 rx_pause_frames: 1 # ./ethtool -I --json -a eth0 [ { "ifname": "eth0", "autonegotiate": true, "rx": true, "tx": true, "statistics": { "tx_pause_frames": 1, "rx_pause_frames": 1 } } ] v2: - correct print format for u64 Signed-off-by: Jakub Kicinski --- netlink/pause.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/netlink/pause.c b/netlink/pause.c index c960c82cba5f..9bc9a301821f 100644 --- a/netlink/pause.c +++ b/netlink/pause.c @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -105,6 +106,62 @@ static int show_pause_autoneg_status(struct nl_context *nlctx) return ret; } +static int show_pause_stats(const struct nlattr *nest) +{ + const struct nlattr *tb[ETHTOOL_A_PAUSE_STAT_MAX + 1] = {}; + DECLARE_ATTR_TB_INFO(tb); + static const struct { + unsigned int attr; + char *name; + } stats[] = { + { ETHTOOL_A_PAUSE_STAT_TX_FRAMES, "tx_pause_frames" }, + { ETHTOOL_A_PAUSE_STAT_RX_FRAMES, "rx_pause_frames" }, + }; + bool header = false; + unsigned int i; + size_t n; + int ret; + + ret = mnl_attr_parse_nested(nest, attr_cb, &tb_info); + if (ret < 0) + return ret; + + open_json_object("statistics"); + for (i = 0; i < ARRAY_SIZE(stats); i++) { + char fmt[32]; + + if (!tb[stats[i].attr]) + continue; + + if (!header && !is_json_context()) { + printf("Statistics:\n"); + header = true; + } + + if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) { + fprintf(stderr, "malformed netlink message (statistic)\n"); + goto err_close_stats; + } + + n = snprintf(fmt, sizeof(fmt), " %s: %%" PRIu64 "\n", + stats[i].name); + if (n >= sizeof(fmt)) { + fprintf(stderr, "internal error - malformed label\n"); + goto err_close_stats; + } + + print_u64(PRINT_ANY, stats[i].name, fmt, + mnl_attr_get_u64(tb[stats[i].attr])); + } + close_json_object(); + + return 0; + +err_close_stats: + close_json_object(); + return -1; +} + int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data) { const struct nlattr *tb[ETHTOOL_A_PAUSE_MAX + 1] = {}; @@ -142,6 +199,11 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data) if (ret < 0) goto err_close_dev; } + if (tb[ETHTOOL_A_PAUSE_STATS]) { + ret = show_pause_stats(tb[ETHTOOL_A_PAUSE_STATS]); + if (ret < 0) + goto err_close_dev; + } if (!silent) print_nl(); @@ -158,6 +220,7 @@ int nl_gpause(struct cmd_context *ctx) { struct nl_context *nlctx = ctx->nlctx; struct nl_socket *nlsk = nlctx->ethnl_socket; + u32 flags; int ret; if (netlink_cmd_check(ctx, ETHTOOL_MSG_PAUSE_GET, true)) @@ -168,8 +231,10 @@ int nl_gpause(struct cmd_context *ctx) return 1; } + flags = get_stats_flag(nlctx, ETHTOOL_MSG_PAUSE_GET, + ETHTOOL_A_PAUSE_HEADER); ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PAUSE_GET, - ETHTOOL_A_PAUSE_HEADER, 0); + ETHTOOL_A_PAUSE_HEADER, flags); if (ret < 0) return ret;