All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch iproute2/net-next] devlink: fix JSON output of mon command
@ 2020-04-02  9:56 Jiri Pirko
  2020-04-06 18:43 ` Jacob Keller
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jiri Pirko @ 2020-04-02  9:56 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, stephen, davem, kuba, mlxsw

From: Jiri Pirko <jiri@mellanox.com>

The current JSON output of mon command is broken. Fix it and make sure
that the output is a valid JSON. Also, handle SIGINT gracefully to allow
to end the JSON properly.

Example:
$ devlink mon -j -p
{
    "mon": [ {
            "command": "new",
            "dev": {
                "netdevsim/netdevsim10": {}
            }
        },{
            "command": "new",
            "port": {
                "netdevsim/netdevsim10/0": {
                    "type": "notset",
                    "flavour": "physical",
                    "port": 1
                }
            }
        },{
            "command": "new",
            "port": {
                "netdevsim/netdevsim10/0": {
                    "type": "eth",
                    "netdev": "eth0",
                    "flavour": "physical",
                    "port": 1
                }
            }
        },{
            "command": "new",
            "port": {
                "netdevsim/netdevsim10/0": {
                    "type": "notset",
                    "flavour": "physical",
                    "port": 1
                }
            }
        },{
            "command": "del",
            "port": {
                "netdevsim/netdevsim10/0": {
                    "type": "notset",
                    "flavour": "physical",
                    "port": 1
                }
            }
        },{
            "command": "del",
            "dev": {
                "netdevsim/netdevsim10": {}
            }
        } ]
}

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
---
 devlink/devlink.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index b8c1170be0e2..6434e68593ea 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -149,6 +149,30 @@ static int _mnlg_socket_recv_run(struct mnlg_socket *nlg,
 	return 0;
 }
 
+static void dummy_signal_handler(int signum)
+{
+}
+
+static int _mnlg_socket_recv_run_intr(struct mnlg_socket *nlg,
+				      mnl_cb_t data_cb, void *data)
+{
+	struct sigaction act, oact;
+	int err;
+
+	act.sa_handler = dummy_signal_handler;
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = SA_NODEFER;
+
+	sigaction(SIGINT, &act, &oact);
+	err = mnlg_socket_recv_run(nlg, data_cb, data);
+	sigaction(SIGINT, &oact, NULL);
+	if (err < 0 && errno != EINTR) {
+		pr_err("devlink answers: %s\n", strerror(errno));
+		return -errno;
+	}
+	return 0;
+}
+
 static int _mnlg_socket_send(struct mnlg_socket *nlg,
 			     const struct nlmsghdr *nlh)
 {
@@ -4190,7 +4214,21 @@ static const char *cmd_obj(uint8_t cmd)
 
 static void pr_out_mon_header(uint8_t cmd)
 {
-	pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd));
+	if (!is_json_context()) {
+		pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd));
+	} else {
+		open_json_object(NULL);
+		print_string(PRINT_JSON, "command", NULL, cmd_name(cmd));
+		open_json_object(cmd_obj(cmd));
+	}
+}
+
+static void pr_out_mon_footer(void)
+{
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
+	}
 }
 
 static bool cmd_filter_check(struct dl *dl, uint8_t cmd)
@@ -4259,6 +4297,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_handle(dl, tb);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_PORT_GET: /* fall through */
 	case DEVLINK_CMD_PORT_SET: /* fall through */
@@ -4270,6 +4309,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_port(dl, tb);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_PARAM_GET: /* fall through */
 	case DEVLINK_CMD_PARAM_SET: /* fall through */
@@ -4281,6 +4321,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_param(dl, tb, false);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_REGION_GET: /* fall through */
 	case DEVLINK_CMD_REGION_SET: /* fall through */
@@ -4292,6 +4333,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_region(dl, tb);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_FLASH_UPDATE: /* fall through */
 	case DEVLINK_CMD_FLASH_UPDATE_END: /* fall through */
@@ -4301,6 +4343,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_flash_update(dl, tb);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_HEALTH_REPORTER_RECOVER:
 		mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
@@ -4309,6 +4352,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_health(dl, tb);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_TRAP_GET: /* fall through */
 	case DEVLINK_CMD_TRAP_SET: /* fall through */
@@ -4325,6 +4369,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_trap(dl, tb, false);
+		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_TRAP_GROUP_GET: /* fall through */
 	case DEVLINK_CMD_TRAP_GROUP_SET: /* fall through */
@@ -4337,6 +4382,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		pr_out_trap_group(dl, tb, false);
+		pr_out_mon_footer();
 		break;
 	}
 	return MNL_CB_OK;
@@ -4362,7 +4408,11 @@ static int cmd_mon_show(struct dl *dl)
 	err = _mnlg_socket_group_add(dl->nlg, DEVLINK_GENL_MCGRP_CONFIG_NAME);
 	if (err)
 		return err;
-	err = _mnlg_socket_recv_run(dl->nlg, cmd_mon_show_cb, dl);
+	open_json_object(NULL);
+	open_json_array(PRINT_JSON, "mon");
+	err = _mnlg_socket_recv_run_intr(dl->nlg, cmd_mon_show_cb, dl);
+	close_json_array(PRINT_JSON, NULL);
+	close_json_object();
 	if (err)
 		return err;
 	return 0;
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-02  9:56 [patch iproute2/net-next] devlink: fix JSON output of mon command Jiri Pirko
@ 2020-04-06 18:43 ` Jacob Keller
  2020-04-06 20:28   ` Petr Machata
  2020-04-07  9:09 ` Jiri Pirko
  2020-04-09 14:43 ` David Ahern
  2 siblings, 1 reply; 7+ messages in thread
From: Jacob Keller @ 2020-04-06 18:43 UTC (permalink / raw)
  To: Jiri Pirko, netdev; +Cc: dsahern, stephen, davem, kuba, mlxsw



On 4/2/2020 2:56 AM, Jiri Pirko wrote:
> From: Jiri Pirko <jiri@mellanox.com>
> 
> The current JSON output of mon command is broken. Fix it and make sure
> that the output is a valid JSON. Also, handle SIGINT gracefully to allow
> to end the JSON properly.
> 

I wonder if there is an easy way we could get "make check" or something
to add a test to help verify this is valid JSON?

Thanks,
Jake

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-06 18:43 ` Jacob Keller
@ 2020-04-06 20:28   ` Petr Machata
  2020-04-06 22:48     ` Stephen Hemminger
  0 siblings, 1 reply; 7+ messages in thread
From: Petr Machata @ 2020-04-06 20:28 UTC (permalink / raw)
  To: Jacob Keller; +Cc: Jiri Pirko, netdev, dsahern, stephen, davem, kuba, mlxsw


Jacob Keller <jacob.e.keller@intel.com> writes:

> On 4/2/2020 2:56 AM, Jiri Pirko wrote:
>> From: Jiri Pirko <jiri@mellanox.com>
>>
>> The current JSON output of mon command is broken. Fix it and make sure
>> that the output is a valid JSON. Also, handle SIGINT gracefully to allow
>> to end the JSON properly.
>>
>
> I wonder if there is an easy way we could get "make check" or something
> to add a test to help verify this is valid JSON?

Simply piping to jq is an easy way to figure out if it's at least valid
JSON. In principle it would be possible to write more detailed checks as
TDC (tc-testing) selftests in the kernel.

Something like this:

    {
        "id": "a520",
        "name": "JSON",
        "category": [
            "qdisc",
            "fifo"
        ],
        "setup": [
            "$IP link add dev $DUMMY type dummy || /bin/true",
	    "$TC qdisc add dev $DUMMY handle 1: root bfifo"
        ],
        "cmdUnderTest": "/bin/true",
        "expExitCode": "0",
        "verifyCmd": "$TC -j qdisc show dev $DUMMY | jq '.[].kind'",
        "matchPattern": "bfifo",
        "matchCount": "1",
        "teardown": [
            "$TC qdisc del dev $DUMMY handle 1: root bfifo",
            "$IP link del dev $DUMMY type dummy"
        ]
    }

Kinda verbose for this level of detail though.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-06 20:28   ` Petr Machata
@ 2020-04-06 22:48     ` Stephen Hemminger
  0 siblings, 0 replies; 7+ messages in thread
From: Stephen Hemminger @ 2020-04-06 22:48 UTC (permalink / raw)
  To: Petr Machata
  Cc: Jacob Keller, Jiri Pirko, netdev, dsahern, davem, kuba, mlxsw

On Mon, 06 Apr 2020 22:28:31 +0200
Petr Machata <petrm@mellanox.com> wrote:

> Jacob Keller <jacob.e.keller@intel.com> writes:
> 
> > On 4/2/2020 2:56 AM, Jiri Pirko wrote:  
> >> From: Jiri Pirko <jiri@mellanox.com>
> >>
> >> The current JSON output of mon command is broken. Fix it and make sure
> >> that the output is a valid JSON. Also, handle SIGINT gracefully to allow
> >> to end the JSON properly.
> >>  
> >
> > I wonder if there is an easy way we could get "make check" or something
> > to add a test to help verify this is valid JSON?  
> 
> Simply piping to jq is an easy way to figure out if it's at least valid
> JSON. In principle it would be possible to write more detailed checks as
> TDC (tc-testing) selftests in the kernel.
> 
> Something like this:
> 
>     {
>         "id": "a520",
>         "name": "JSON",
>         "category": [
>             "qdisc",
>             "fifo"
>         ],
>         "setup": [
>             "$IP link add dev $DUMMY type dummy || /bin/true",
> 	    "$TC qdisc add dev $DUMMY handle 1: root bfifo"
>         ],
>         "cmdUnderTest": "/bin/true",
>         "expExitCode": "0",
>         "verifyCmd": "$TC -j qdisc show dev $DUMMY | jq '.[].kind'",
>         "matchPattern": "bfifo",
>         "matchCount": "1",
>         "teardown": [
>             "$TC qdisc del dev $DUMMY handle 1: root bfifo",
>             "$IP link del dev $DUMMY type dummy"
>         ]
>     }
> 
> Kinda verbose for this level of detail though.

I just use python -m json.tool which is easy (always available)
and picky

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-02  9:56 [patch iproute2/net-next] devlink: fix JSON output of mon command Jiri Pirko
  2020-04-06 18:43 ` Jacob Keller
@ 2020-04-07  9:09 ` Jiri Pirko
  2020-04-09 14:43 ` David Ahern
  2 siblings, 0 replies; 7+ messages in thread
From: Jiri Pirko @ 2020-04-07  9:09 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, stephen, davem, kuba, mlxsw

Stephen, this is also non net-next material. Please consider taking
directly to master.

Well make sure to send the patches with correct target next time.

Thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-02  9:56 [patch iproute2/net-next] devlink: fix JSON output of mon command Jiri Pirko
  2020-04-06 18:43 ` Jacob Keller
  2020-04-07  9:09 ` Jiri Pirko
@ 2020-04-09 14:43 ` David Ahern
  2020-04-09 18:28   ` Jiri Pirko
  2 siblings, 1 reply; 7+ messages in thread
From: David Ahern @ 2020-04-09 14:43 UTC (permalink / raw)
  To: Jiri Pirko, netdev; +Cc: stephen, davem, kuba, mlxsw

On 4/2/20 3:56 AM, Jiri Pirko wrote:
> From: Jiri Pirko <jiri@mellanox.com>
> 
> The current JSON output of mon command is broken. Fix it and make sure
> that the output is a valid JSON. Also, handle SIGINT gracefully to allow
> to end the JSON properly.
> 
...
> 
> Signed-off-by: Jiri Pirko <jiri@mellanox.com>
> ---
>  devlink/devlink.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 52 insertions(+), 2 deletions(-)
> 

does not apply cleanly to -next


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [patch iproute2/net-next] devlink: fix JSON output of mon command
  2020-04-09 14:43 ` David Ahern
@ 2020-04-09 18:28   ` Jiri Pirko
  0 siblings, 0 replies; 7+ messages in thread
From: Jiri Pirko @ 2020-04-09 18:28 UTC (permalink / raw)
  To: David Ahern; +Cc: netdev, stephen, davem, kuba, mlxsw

Thu, Apr 09, 2020 at 04:43:42PM CEST, dsahern@gmail.com wrote:
>On 4/2/20 3:56 AM, Jiri Pirko wrote:
>> From: Jiri Pirko <jiri@mellanox.com>
>> 
>> The current JSON output of mon command is broken. Fix it and make sure
>> that the output is a valid JSON. Also, handle SIGINT gracefully to allow
>> to end the JSON properly.
>> 
>...
>> 
>> Signed-off-by: Jiri Pirko <jiri@mellanox.com>
>> ---
>>  devlink/devlink.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--
>>  1 file changed, 52 insertions(+), 2 deletions(-)
>> 
>
>does not apply cleanly to -next

Odd, rebasing and reposting.

>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-09 18:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-02  9:56 [patch iproute2/net-next] devlink: fix JSON output of mon command Jiri Pirko
2020-04-06 18:43 ` Jacob Keller
2020-04-06 20:28   ` Petr Machata
2020-04-06 22:48     ` Stephen Hemminger
2020-04-07  9:09 ` Jiri Pirko
2020-04-09 14:43 ` David Ahern
2020-04-09 18:28   ` Jiri Pirko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.