[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240603114442.4099003-2-f.pfitzner@pengutronix.de>
Date: Mon, 3 Jun 2024 13:44:44 +0200
From: Fabian Pfitzner <f.pfitzner@...gutronix.de>
To: mkubecek@...e.cz,
netdev@...r.kernel.org
Cc: Fabian Pfitzner <f.pfitzner@...gutronix.de>
Subject: [PATCH ethtool 2/2]: add json support for --show-eee command
Implement the option to output json for the --show-eee sub command.
Signed-off-by: Fabian Pfitzner <f.pfitzner@...gutronix.de>
---
ethtool.c | 1 +
netlink/eee.c | 35 ++++++++++++++++++++++-------------
2 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/ethtool.c b/ethtool.c
index 73c26e2..f08c198 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -6041,6 +6041,7 @@ static const struct option args[] = {
},
{
.opts = "--show-eee",
+ .json = true,
.func = do_geee,
.nlfunc = nl_geee,
.help = "Show EEE settings",
diff --git a/netlink/eee.c b/netlink/eee.c
index 6c53756..51b9d10 100644
--- a/netlink/eee.c
+++ b/netlink/eee.c
@@ -14,6 +14,7 @@
#include "netlink.h"
#include "bitset.h"
#include "parser.h"
+#include "../json_writer.h"
/* EEE_GET */
@@ -21,13 +22,13 @@ int eee_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
const struct nlattr *tb[ETHTOOL_A_EEE_MAX + 1] = {};
DECLARE_ATTR_TB_INFO(tb);
- bool enabled, active, tx_lpi_enabled;
+ bool enabled, active, tx_lpi_enabled, status_support;
struct nl_context *nlctx = data;
bool silent;
int err_ret;
int ret;
- silent = nlctx->is_dump || nlctx->is_monitor;
+ silent = nlctx->is_dump || nlctx->is_monitor || is_json_context();
err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
if (ret < 0)
@@ -46,25 +47,26 @@ int eee_reply_cb(const struct nlmsghdr *nlhdr, void *data)
active = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_ACTIVE]);
enabled = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_ENABLED]);
tx_lpi_enabled = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_TX_LPI_ENABLED]);
+ status_support = bitset_is_empty(tb[ETHTOOL_A_EEE_MODES_OURS], true, &ret);
if (silent)
putchar('\n');
- printf("EEE settings for %s:\n", nlctx->devname);
- printf("\tEEE status: ");
- if (bitset_is_empty(tb[ETHTOOL_A_EEE_MODES_OURS], true, &ret)) {
- printf("not supported\n");
+ print_string(PRINT_ANY, "ifname", "EEE settings for %s:\n", nlctx->devname);
+ print_string(PRINT_FP, NULL, "\tEEE status: ", NULL);
+ if (status_support) {
+ print_string(PRINT_ANY, "status", "%s\n", "not supported");
return MNL_CB_OK;
}
if (!enabled)
- printf("disabled\n");
+ print_string(PRINT_ANY, "status", "%s\n", "disabled");
else
- printf("enabled - %s\n", active ? "active" : "inactive");
- printf("\tTx LPI: ");
+ print_string(PRINT_ANY, "status", "enabled - %s\n", active ? "active" : "inactive");
+ print_string(PRINT_FP, NULL, "\tTx LPI: ", NULL);
if (tx_lpi_enabled)
- printf("%u (us)\n",
+ print_uint(PRINT_ANY, "tx-lpi", "%u (us)\n",
mnl_attr_get_u32(tb[ETHTOOL_A_EEE_TX_LPI_TIMER]));
else
- printf("disabled\n");
+ print_string(PRINT_FP, NULL, "%s\n", "disabled");
ret = dump_link_modes(nlctx, tb[ETHTOOL_A_EEE_MODES_OURS], true,
LM_CLASS_REAL,
@@ -102,11 +104,18 @@ int nl_geee(struct cmd_context *ctx)
return 1;
}
+ new_json_obj(ctx->json);
+ open_json_object(NULL);
+
ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_EEE_GET,
ETHTOOL_A_EEE_HEADER, 0);
if (ret < 0)
- return ret;
- return nlsock_send_get_request(nlsk, eee_reply_cb);
+ goto out;
+ ret = nlsock_send_get_request(nlsk, eee_reply_cb);
+out:
+ close_json_object();
+ delete_json_obj();
+ return ret;
}
/* EEE_SET */
--
2.39.2
Powered by blists - more mailing lists