From patchwork Fri Aug 13 17:19:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 496942 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=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, 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 BE48DC432BE for ; Fri, 13 Aug 2021 17:19:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9E2C360FC3 for ; Fri, 13 Aug 2021 17:19:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231182AbhHMRUN (ORCPT ); Fri, 13 Aug 2021 13:20:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:36982 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229909AbhHMRUM (ORCPT ); Fri, 13 Aug 2021 13:20:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D798760F51; Fri, 13 Aug 2021 17:19:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628875185; bh=Th7mLcEdgD9MZb91A/vTDuBPjRrjvhIZqBlMQc2naVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IxLfDI3kHCArf+F17HsythtMwhXtIhD5PXZn4BGoWcMLVm8chNs9MEd/TliZthI6q lVDmYpRh+RfRRNCgDxLTwXchg/+LtX3JCkZQ8K8eBhlRtJr/LpacJjA3jt7wUtYsmL hrOPAq0yKszrQ+Q7HD/E4+uxO1q9ex2ikpoHcz9Vy4n83dSugvIfJExolatY8XSV3j yhek3uUYM/gSPUmfbiOG9hwZ/bQS9GlWte8syR/oOhrGwJpXYq0jlGk4IlzQ7xavde xeOzq+gBMtQsvJ/Ei7IYH+KI1BV6pblaG39qDo7jcahJ5TKw7ZOq0KuumPQ5l9C/mZ RxqZ2CiTZkdYg== From: Jakub Kicinski To: mkubecek@suse.cz, andrew@lunn.ch Cc: netdev@vger.kernel.org, dcavalca@fb.com, filbranden@fb.com, michel@fb.com, Jakub Kicinski Subject: [PATCH ethtool 1/3] ethtool: remove questionable goto Date: Fri, 13 Aug 2021 10:19:36 -0700 Message-Id: <20210813171938.1127891-2-kuba@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210813171938.1127891-1-kuba@kernel.org> References: <20210813171938.1127891-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org goto opt_found can be trivially replaced by an else branch. Signed-off-by: Jakub Kicinski --- ethtool.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ethtool.c b/ethtool.c index 33a0a492cb15..8cf1b13e4176 100644 --- a/ethtool.c +++ b/ethtool.c @@ -6352,15 +6352,14 @@ int main(int argc, char **argp) nlfunc = args[k].nlfunc; nlchk = args[k].nlchk; no_dev = args[k].no_dev; - goto opt_found; + } else { + if ((*argp)[0] == '-') + exit_bad_args(); + nlfunc = nl_gset; + func = do_gset; + no_dev = false; } - if ((*argp)[0] == '-') - exit_bad_args(); - nlfunc = nl_gset; - func = do_gset; - no_dev = false; -opt_found: if (!no_dev) { ctx.devname = *argp++; argc--; From patchwork Fri Aug 13 17:19:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 496941 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=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, 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 0043CC4338F for ; Fri, 13 Aug 2021 17:19:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D65DA60EE0 for ; Fri, 13 Aug 2021 17:19:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231496AbhHMRUQ (ORCPT ); Fri, 13 Aug 2021 13:20:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:37006 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230163AbhHMRUN (ORCPT ); Fri, 13 Aug 2021 13:20:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B11D6610F7; Fri, 13 Aug 2021 17:19:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628875186; bh=NiOVLorQ0fo+GJMVv9D5vtnTb7PM9CBJlmYrV2SQj50=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k23tHUDEFFtuJjhuY/sUN8HpjYtVHtZU0kscOdG0XNzn26Nm+IAQojF2MAalcMJgV F6bsxgbxniUnyaNeyXa2mROEyJpdgi9Ll6UJTWoGLpoudCvcnl+4umhzEeUMkGFILT DtbCEfnpkBrZRNMQbNkopw5hmLjlOkkOY0pXp+a4y49rslw1dCHV3QI+QpyC7II2Rm Ty7BwLObCjdWom+GFZeu87el99LaB+LFP7t31MSPWh16+l3ccQEuDaWMy9DKGticWl yK/nCY9Lj1xFR7Pl0m5mReKhKpCx76OBlmJUP9ofR1OyLgqdoI7RF5ovRk140QyJiC CjK0fHnN3J5Kg== From: Jakub Kicinski To: mkubecek@suse.cz, andrew@lunn.ch Cc: netdev@vger.kernel.org, dcavalca@fb.com, filbranden@fb.com, michel@fb.com, Jakub Kicinski Subject: [PATCH ethtool 3/3] ethtool: return error if command does not support --json Date: Fri, 13 Aug 2021 10:19:38 -0700 Message-Id: <20210813171938.1127891-4-kuba@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210813171938.1127891-1-kuba@kernel.org> References: <20210813171938.1127891-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The --json switch is currently best effort, which is similar to how other networking utilities treat it. Change to returning an error if selected command does not support --json. ethtool is more complex than other utilities because the JSON support depends on both user space and kernel version. Older kernel make ethtool use the IOCTL and none of the IOCTL handlers support JSON. The current behavior is counter-productive when trying to query statistics - user has to check (1) if stats (-I) are supported, (2) if json is supported (--json) and then (3) if underlying device populates the statistic. Making --json fail if not supported allows (1) and (2) to both be taken care of with simple check of ethtool's exit code. Link: https://pagure.io/centos-sig-hyperscale/package-bugs/issue/6 Signed-off-by: Jakub Kicinski --- ethtool.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ethtool.c b/ethtool.c index 9e02fe4f09a5..bd6242ed383e 100644 --- a/ethtool.c +++ b/ethtool.c @@ -5619,6 +5619,7 @@ static int show_usage(struct cmd_context *ctx); struct option { const char *opts; bool no_dev; + bool json; int (*func)(struct cmd_context *); nl_chk_t nlchk; nl_func_t nlfunc; @@ -5655,6 +5656,7 @@ static const struct option args[] = { }, { .opts = "-a|--show-pause", + .json = true, .func = do_gpause, .nlfunc = nl_gpause, .help = "Show pause options" @@ -5779,6 +5781,7 @@ static const struct option args[] = { }, { .opts = "-S|--statistics", + .json = true, .func = do_gnicstats, .nlchk = nl_gstats_chk, .nlfunc = nl_gstats, @@ -5990,6 +5993,7 @@ static const struct option args[] = { }, { .opts = "--show-fec", + .json = true, .func = do_gfec, .nlfunc = nl_gfec, .help = "Show FEC settings", @@ -6010,11 +6014,13 @@ static const struct option args[] = { }, { .opts = "--cable-test", + .json = true, .nlfunc = nl_cable_test, .help = "Perform a cable test", }, { .opts = "--cable-test-tdr", + .json = true, .nlfunc = nl_cable_test_tdr, .help = "Print cable test time domain reflectrometery data", .xhelp = " [ first N ]\n" @@ -6361,10 +6367,15 @@ int main(int argc, char **argp) if (!ctx.devname) exit_bad_args(); } + if (ctx.json && !args[k].json) + exit_bad_args(); ctx.argc = argc; ctx.argp = argp; netlink_run_handler(&ctx, args[k].nlchk, args[k].nlfunc, !args[k].func); + if (ctx.json) /* no IOCTL command supports JSON output */ + exit_bad_args(); + ret = ioctl_init(&ctx, args[k].no_dev); if (ret) return ret;