All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2 0/2] iproute JSON fixes
@ 2019-03-18 17:19 Matteo Croce
  2019-03-18 17:19 ` [PATCH iproute2 1/2] ip route: print route type in JSON output Matteo Croce
  2019-03-18 17:19 ` [PATCH iproute2 2/2] ip route: get: print JSON output when -j is given Matteo Croce
  0 siblings, 2 replies; 4+ messages in thread
From: Matteo Croce @ 2019-03-18 17:19 UTC (permalink / raw)
  To: netdev; +Cc: Phil Sutter, Andrea Claudi, Stephen Hemminger, David Ahern

ip route can produce invalid JSON under certain circumstances,
here there are two fixes for this.

Matteo Croce (2):
  ip route: print route type in JSON output
  ip route: get: print JSON output when -j is given

 ip/iproute.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

-- 
2.20.1


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

* [PATCH iproute2 1/2] ip route: print route type in JSON output
  2019-03-18 17:19 [PATCH iproute2 0/2] iproute JSON fixes Matteo Croce
@ 2019-03-18 17:19 ` Matteo Croce
  2019-03-19 22:24   ` Stephen Hemminger
  2019-03-18 17:19 ` [PATCH iproute2 2/2] ip route: get: print JSON output when -j is given Matteo Croce
  1 sibling, 1 reply; 4+ messages in thread
From: Matteo Croce @ 2019-03-18 17:19 UTC (permalink / raw)
  To: netdev; +Cc: Phil Sutter, Andrea Claudi, Stephen Hemminger, David Ahern

ip route generates an invalid JSON if the route type has to be printed,
eg. when detailed mode is active, or the type is different that unicast:

    $ ip -d -j -p route show
    [ {"unicast",
            "dst": "192.168.122.0/24",
            "dev": "virbr0",
            "protocol": "kernel",
            "scope": "link",
            "prefsrc": "192.168.122.1",
            "flags": [ "linkdown" ]
        } ]

    $ ip -j -p route show
    [ {"unreachable",
            "dst": "192.168.23.0/24",
            "flags": [ ]
        },{"prohibit",
            "dst": "192.168.24.0/24",
            "flags": [ ]
        },{"blackhole",
            "dst": "192.168.25.0/24",
            "flags": [ ]
        } ]

Fix it by printing the route type as the "type" attribute:

    $ ip -d -j -p route show
    [ {
            "type": "unicast",
            "dst": "default",
            "gateway": "192.168.85.1",
            "dev": "wlp3s0",
            "protocol": "dhcp",
            "scope": "global",
            "metric": 600,
            "flags": [ ]
        },{
            "type": "unreachable",
            "dst": "192.168.23.0/24",
            "protocol": "boot",
            "scope": "global",
            "flags": [ ]
        },{
            "type": "prohibit",
            "dst": "192.168.24.0/24",
            "protocol": "boot",
            "scope": "global",
            "flags": [ ]
        },{
            "type": "blackhole",
            "dst": "192.168.25.0/24",
            "protocol": "boot",
            "scope": "global",
            "flags": [ ]
        } ]

Fixes: 663c3cb23103 ("iproute: implement JSON and color output")
Acked-by: Phil Sutter <phil@nwl.cc>
Reviewed-and-tested-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Matteo Croce <mcroce@redhat.com>
---
 ip/iproute.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index cc02a3e1..e091927b 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -766,7 +766,7 @@ int print_route(struct nlmsghdr *n, void *arg)
 
 	if ((r->rtm_type != RTN_UNICAST || show_details > 0) &&
 	    (!filter.typemask || (filter.typemask & (1 << r->rtm_type))))
-		print_string(PRINT_ANY, NULL, "%s ",
+		print_string(PRINT_ANY, "type", "%s ",
 			     rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
 
 	color = COLOR_NONE;
-- 
2.20.1


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

* [PATCH iproute2 2/2] ip route: get: print JSON output when -j is given
  2019-03-18 17:19 [PATCH iproute2 0/2] iproute JSON fixes Matteo Croce
  2019-03-18 17:19 ` [PATCH iproute2 1/2] ip route: print route type in JSON output Matteo Croce
@ 2019-03-18 17:19 ` Matteo Croce
  1 sibling, 0 replies; 4+ messages in thread
From: Matteo Croce @ 2019-03-18 17:19 UTC (permalink / raw)
  To: netdev; +Cc: Phil Sutter, Andrea Claudi, Stephen Hemminger, David Ahern

The ip -j option to print output as JSON is ignored when using 'route get':

    $ ip -j route get 127.0.0.1
    local 127.0.0.1 dev lo src 127.0.0.1 uid 1000
        cache <local>

Enable JSON output in iproute_get(), and don't let print_cache_flags() close
the JSON output, as it's not always the last called JSON function.

Tested on different route types:

    $ ip -j -p route get 127.0.0.1
    [ {
            "type": "local",
            "dst": "127.0.0.1",
            "dev": "lo",
            "prefsrc": "127.0.0.1",
            "flags": [ ],
            "uid": 1000,
            "cache": [ "local" ]
        } ]

    $ ip -d -j -p route get 192.0.2.1
    [ {
            "type": "unicast",
            "dst": "192.0.2.1",
            "gateway": "192.168.85.1",
            "dev": "wlp3s0",
            "table": "main",
            "prefsrc": "192.168.85.2",
            "flags": [ ],
            "uid": 1000,
            "cache": [ ]
        } ]

Fixes: 663c3cb23103 ("iproute: implement JSON and color output")
Acked-by: Phil Sutter <phil@nwl.cc>
Reviewed-and-tested-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Matteo Croce <mcroce@redhat.com>
---
 ip/iproute.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index e091927b..2b3dcc5d 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -450,10 +450,8 @@ static void print_cache_flags(FILE *fp, __u32 flags)
 	if (flags)
 		print_hex(PRINT_ANY, "flags", "%x>", flags);
 
-	if (jw) {
+	if (jw)
 		jsonw_end_array(jw);
-		jsonw_destroy(&jw);
-	}
 }
 
 static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
@@ -2079,6 +2077,8 @@ static int iproute_get(int argc, char **argv)
 	if (rtnl_talk(&rth, &req.n, &answer) < 0)
 		return -2;
 
+	new_json_obj(json);
+
 	if (connected && !from_ok) {
 		struct rtmsg *r = NLMSG_DATA(answer);
 		int len = answer->nlmsg_len;
@@ -2123,6 +2123,7 @@ static int iproute_get(int argc, char **argv)
 		req.n.nlmsg_flags = NLM_F_REQUEST;
 		req.n.nlmsg_type = RTM_GETROUTE;
 
+		delete_json_obj();
 		free(answer);
 		if (rtnl_talk(&rth, &req.n, &answer) < 0)
 			return -2;
@@ -2134,6 +2135,7 @@ static int iproute_get(int argc, char **argv)
 		return -1;
 	}
 
+	delete_json_obj();
 	free(answer);
 	return 0;
 }
-- 
2.20.1


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

* Re: [PATCH iproute2 1/2] ip route: print route type in JSON output
  2019-03-18 17:19 ` [PATCH iproute2 1/2] ip route: print route type in JSON output Matteo Croce
@ 2019-03-19 22:24   ` Stephen Hemminger
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Hemminger @ 2019-03-19 22:24 UTC (permalink / raw)
  To: Matteo Croce; +Cc: netdev, Phil Sutter, Andrea Claudi, David Ahern

On Mon, 18 Mar 2019 18:19:29 +0100
Matteo Croce <mcroce@redhat.com> wrote:

> ip route generates an invalid JSON if the route type has to be printed,
> eg. when detailed mode is active, or the type is different that unicast:
> 
>     $ ip -d -j -p route show
>     [ {"unicast",
>             "dst": "192.168.122.0/24",
>             "dev": "virbr0",
>             "protocol": "kernel",
>             "scope": "link",
>             "prefsrc": "192.168.122.1",
>             "flags": [ "linkdown" ]
>         } ]
> 
>     $ ip -j -p route show
>     [ {"unreachable",
>             "dst": "192.168.23.0/24",
>             "flags": [ ]
>         },{"prohibit",
>             "dst": "192.168.24.0/24",
>             "flags": [ ]
>         },{"blackhole",
>             "dst": "192.168.25.0/24",
>             "flags": [ ]
>         } ]
> 
> Fix it by printing the route type as the "type" attribute:
> 
>     $ ip -d -j -p route show
>     [ {
>             "type": "unicast",
>             "dst": "default",
>             "gateway": "192.168.85.1",
>             "dev": "wlp3s0",
>             "protocol": "dhcp",
>             "scope": "global",
>             "metric": 600,
>             "flags": [ ]
>         },{
>             "type": "unreachable",
>             "dst": "192.168.23.0/24",
>             "protocol": "boot",
>             "scope": "global",
>             "flags": [ ]
>         },{
>             "type": "prohibit",
>             "dst": "192.168.24.0/24",
>             "protocol": "boot",
>             "scope": "global",
>             "flags": [ ]
>         },{
>             "type": "blackhole",
>             "dst": "192.168.25.0/24",
>             "protocol": "boot",
>             "scope": "global",
>             "flags": [ ]
>         } ]
> 
> Fixes: 663c3cb23103 ("iproute: implement JSON and color output")
> Acked-by: Phil Sutter <phil@nwl.cc>
> Reviewed-and-tested-by: Andrea Claudi <aclaudi@redhat.com>
> Signed-off-by: Matteo Croce <mcroce@redhat.com>

Applied both of these just before tagging 5.0


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

end of thread, other threads:[~2019-03-19 22:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-18 17:19 [PATCH iproute2 0/2] iproute JSON fixes Matteo Croce
2019-03-18 17:19 ` [PATCH iproute2 1/2] ip route: print route type in JSON output Matteo Croce
2019-03-19 22:24   ` Stephen Hemminger
2019-03-18 17:19 ` [PATCH iproute2 2/2] ip route: get: print JSON output when -j is given Matteo Croce

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.