Blame SOURCES/0029-pause-add-json-support.patch

6975f2
From 7bb75228e6849f3371cd434cf36d29f406db1ccc Mon Sep 17 00:00:00 2001
6975f2
From: Jakub Kicinski <kuba@kernel.org>
6975f2
Date: Sun, 18 Oct 2020 14:31:46 -0700
6975f2
Subject: [PATCH 29/37] pause: add --json support
6975f2
6975f2
No change in normal text output:
6975f2
6975f2
 # ./ethtool  -a eth0
6975f2
Pause parameters for eth0:
6975f2
Autonegotiate:	on
6975f2
RX:		on
6975f2
TX:		on
6975f2
RX negotiated: on
6975f2
TX negotiated: on
6975f2
6975f2
JSON:
6975f2
6975f2
 # ./ethtool --json -a eth0
6975f2
[ {
6975f2
        "ifname": "eth0",
6975f2
        "autonegotiate": true,
6975f2
        "rx": true,
6975f2
        "tx": true,
6975f2
        "negotiated": {
6975f2
            "rx": true,
6975f2
            "tx": true
6975f2
        }
6975f2
    } ]
6975f2
6975f2
v2:
6975f2
 - restructure show_bool() so we can use its logic for show_bool_val()
6975f2
6975f2
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
6975f2
Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
6975f2
(cherry picked from commit 7e5c1ddbe67d0eefe9004e9a69f2ea5378e3bc5e)
6975f2
---
6975f2
 netlink/coalesce.c |  6 +++---
6975f2
 netlink/netlink.h  | 21 ++++++++++++++++-----
6975f2
 netlink/pause.c    | 44 ++++++++++++++++++++++++++++++++------------
6975f2
 3 files changed, 51 insertions(+), 20 deletions(-)
6975f2
6975f2
diff --git a/netlink/coalesce.c b/netlink/coalesce.c
6975f2
index 0223f8e3484e..75922a91c2e7 100644
6975f2
--- a/netlink/coalesce.c
6975f2
+++ b/netlink/coalesce.c
6975f2
@@ -36,9 +36,9 @@ int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data)
6975f2
 	if (silent)
6975f2
 		putchar('\n');
6975f2
 	printf("Coalesce parameters for %s:\n", nlctx->devname);
6975f2
-	printf("Adaptive RX: %s  TX: %s\n",
6975f2
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]),
6975f2
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]));
6975f2
+	show_bool("rx", "Adaptive RX: %s  ",
6975f2
+		  tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]);
6975f2
+	show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]);
6975f2
 	show_u32(tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS],
6975f2
 		 "stats-block-usecs: ");
6975f2
 	show_u32(tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL],
6975f2
diff --git a/netlink/netlink.h b/netlink/netlink.h
6975f2
index 61a072db8ed9..e79143016bd5 100644
6975f2
--- a/netlink/netlink.h
6975f2
+++ b/netlink/netlink.h
6975f2
@@ -102,17 +102,28 @@ static inline void show_u32(const struct nlattr *attr, const char *label)
6975f2
 		printf("%sn/a\n", label);
6975f2
 }
6975f2
 
6975f2
-static inline const char *u8_to_bool(const struct nlattr *attr)
6975f2
+static inline const char *u8_to_bool(const uint8_t *val)
6975f2
 {
6975f2
-	if (attr)
6975f2
-		return mnl_attr_get_u8(attr) ? "on" : "off";
6975f2
+	if (val)
6975f2
+		return *val ? "on" : "off";
6975f2
 	else
6975f2
 		return "n/a";
6975f2
 }
6975f2
 
6975f2
-static inline void show_bool(const struct nlattr *attr, const char *label)
6975f2
+static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
6975f2
+{
6975f2
+	if (is_json_context()) {
6975f2
+		if (val)
6975f2
+			print_bool(PRINT_JSON, key, NULL, val);
6975f2
+	} else {
6975f2
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
6975f2
+	}
6975f2
+}
6975f2
+
6975f2
+static inline void show_bool(const char *key, const char *fmt,
6975f2
+			     const struct nlattr *attr)
6975f2
 {
6975f2
-	printf("%s%s\n", label, u8_to_bool(attr));
6975f2
+	show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL);
6975f2
 }
6975f2
 
6975f2
 /* misc */
6975f2
diff --git a/netlink/pause.c b/netlink/pause.c
6975f2
index 048320b123d2..c54488d71fce 100644
6975f2
--- a/netlink/pause.c
6975f2
+++ b/netlink/pause.c
6975f2
@@ -40,8 +40,8 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
6975f2
 	struct pause_autoneg_status ours = {};
6975f2
 	struct pause_autoneg_status peer = {};
6975f2
 	struct nl_context *nlctx = data;
6975f2
-	bool rx_status = false;
6975f2
-	bool tx_status = false;
6975f2
+	uint8_t rx_status = false;
6975f2
+	uint8_t tx_status = false;
6975f2
 	bool silent;
6975f2
 	int err_ret;
6975f2
 	int ret;
6975f2
@@ -72,8 +72,11 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
6975f2
 		else if (peer.pause)
6975f2
 			tx_status = true;
6975f2
 	}
6975f2
-	printf("RX negotiated: %s\nTX negotiated: %s\n",
6975f2
-	       rx_status ? "on" : "off", tx_status ? "on" : "off");
6975f2
+
6975f2
+	open_json_object("negotiated");
6975f2
+	show_bool_val("rx", "RX negotiated: %s\n", &rx_status);
6975f2
+	show_bool_val("tx", "TX negotiated: %s\n", &tx_status);
6975f2
+	close_json_object();
6975f2
 
6975f2
 	return MNL_CB_OK;
6975f2
 }
6975f2
@@ -121,21 +124,34 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
6975f2
 		return err_ret;
6975f2
 
6975f2
 	if (silent)
6975f2
-		putchar('\n');
6975f2
-	printf("Pause parameters for %s:\n", nlctx->devname);
6975f2
-	show_bool(tb[ETHTOOL_A_PAUSE_AUTONEG], "Autonegotiate:\t");
6975f2
-	show_bool(tb[ETHTOOL_A_PAUSE_RX], "RX:\t\t");
6975f2
-	show_bool(tb[ETHTOOL_A_PAUSE_TX], "TX:\t\t");
6975f2
+		print_nl();
6975f2
+
6975f2
+	open_json_object(NULL);
6975f2
+
6975f2
+	print_string(PRINT_ANY, "ifname", "Pause parameters for %s:\n",
6975f2
+		     nlctx->devname);
6975f2
+
6975f2
+	show_bool("autonegotiate", "Autonegotiate:\t%s\n",
6975f2
+		  tb[ETHTOOL_A_PAUSE_AUTONEG]);
6975f2
+	show_bool("rx", "RX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_RX]);
6975f2
+	show_bool("tx", "TX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_TX]);
6975f2
+
6975f2
 	if (!nlctx->is_monitor && tb[ETHTOOL_A_PAUSE_AUTONEG] &&
6975f2
 	    mnl_attr_get_u8(tb[ETHTOOL_A_PAUSE_AUTONEG])) {
6975f2
 		ret = show_pause_autoneg_status(nlctx);
6975f2
 		if (ret < 0)
6975f2
-			return err_ret;
6975f2
+			goto err_close_dev;
6975f2
 	}
6975f2
 	if (!silent)
6975f2
-		putchar('\n');
6975f2
+		print_nl();
6975f2
+
6975f2
+	close_json_object();
6975f2
 
6975f2
 	return MNL_CB_OK;
6975f2
+
6975f2
+err_close_dev:
6975f2
+	close_json_object();
6975f2
+	return err_ret;
6975f2
 }
6975f2
 
6975f2
 int nl_gpause(struct cmd_context *ctx)
6975f2
@@ -156,7 +172,11 @@ int nl_gpause(struct cmd_context *ctx)
6975f2
 				      ETHTOOL_A_PAUSE_HEADER, 0);
6975f2
 	if (ret < 0)
6975f2
 		return ret;
6975f2
-	return nlsock_send_get_request(nlsk, pause_reply_cb);
6975f2
+
6975f2
+	new_json_obj(ctx->json);
6975f2
+	ret = nlsock_send_get_request(nlsk, pause_reply_cb);
6975f2
+	delete_json_obj();
6975f2
+	return ret;
6975f2
 }
6975f2
 
6975f2
 /* PAUSE_SET */
6975f2
-- 
6975f2
2.26.2
6975f2