netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: mkubecek@suse.cz
Cc: netdev@vger.kernel.org, Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH ethtool-next 2/5] pause: add --json support
Date: Tue, 15 Sep 2020 16:52:56 -0700	[thread overview]
Message-ID: <20200915235259.457050-3-kuba@kernel.org> (raw)
In-Reply-To: <20200915235259.457050-1-kuba@kernel.org>

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
        }
    } ]

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 netlink/netlink.h | 12 ++++++++++--
 netlink/pause.c   | 45 +++++++++++++++++++++++++++++++++++----------
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/netlink/netlink.h b/netlink/netlink.h
index dd4a02bcc916..4916d25ed5c0 100644
--- a/netlink/netlink.h
+++ b/netlink/netlink.h
@@ -106,9 +106,17 @@ static inline const char *u8_to_bool(const struct nlattr *attr)
 		return "n/a";
 }
 
-static inline void show_bool(const struct nlattr *attr, const char *label)
+static inline void show_bool(const char *key, const char *fmt,
+			     const struct nlattr *attr)
 {
-	printf("%s%s\n", label, u8_to_bool(attr));
+	if (is_json_context()) {
+		if (attr) {
+			print_bool(PRINT_JSON, key, NULL,
+				   mnl_attr_get_u8(attr));
+		}
+	} else {
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(attr));
+	}
 }
 
 /* misc */
diff --git a/netlink/pause.c b/netlink/pause.c
index 7b6b3a1d2c10..30ecdccb15eb 100644
--- a/netlink/pause.c
+++ b/netlink/pause.c
@@ -72,8 +72,16 @@ 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");
+
+	if (is_json_context()) {
+		open_json_object("negotiated");
+		print_bool(PRINT_JSON, "rx", NULL, rx_status);
+		print_bool(PRINT_JSON, "tx", NULL, tx_status);
+		close_json_object();
+	} else {
+		printf("RX negotiated: %s\nTX negotiated: %s\n",
+		       rx_status ? "on" : "off", tx_status ? "on" : "off");
+	}
 
 	return MNL_CB_OK;
 }
@@ -121,21 +129,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 +177,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 */
-- 
2.26.2


  parent reply	other threads:[~2020-09-15 23:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-15 23:52 [PATCH ethtool-next 0/5] pause frame stats Jakub Kicinski
2020-09-15 23:52 ` [PATCH ethtool-next 1/5] update UAPI header copies Jakub Kicinski
2020-09-15 23:52 ` Jakub Kicinski [this message]
2020-09-24  0:10   ` [PATCH ethtool-next 2/5] pause: add --json support Jacob Keller
2020-09-24 15:36     ` Jakub Kicinski
2020-09-24 20:41       ` Jacob Keller
2020-09-15 23:52 ` [PATCH ethtool-next 3/5] separate FLAGS out in -h Jakub Kicinski
2020-09-24  0:12   ` Jacob Keller
2020-09-15 23:52 ` [PATCH ethtool-next 4/5] add support for stats in subcommands Jakub Kicinski
2020-09-15 23:52 ` [PATCH ethtool-next 5/5] pause: add support for dumping statistics Jakub Kicinski
2020-09-23 22:45   ` Michal Kubecek
2020-09-23 23:36     ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200915235259.457050-3-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=mkubecek@suse.cz \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).