diff mbox series

[iproute2(-next),v2,1/1] ip: fix ip route show json output for multipath nexthops

Message ID 20190926152934.9121-1-julien@cumulusnetworks.com
State Accepted
Delegated to: David Ahern
Headers show
Series [iproute2(-next),v2,1/1] ip: fix ip route show json output for multipath nexthops | expand

Commit Message

Julien Fortin Sept. 26, 2019, 3:29 p.m. UTC
From: Julien Fortin <julien@cumulusnetworks.com>

print_rta_multipath doesn't support JSON output:

{
    "dst":"27.0.0.13",
    "protocol":"bgp",
    "metric":20,
    "flags":[],
    "gateway":"169.254.0.1"dev uplink-1 weight 1 ,
    "flags":["onlink"],
    "gateway":"169.254.0.1"dev uplink-2 weight 1 ,
    "flags":["onlink"]
},

since RTA_MULTIPATH has nested objects we should print them
in a json array.

With the path we have the following output:

{
    "flags": [],
    "dst": "36.0.0.13",
    "protocol": "bgp",
    "metric": 20,
    "nexthops": [
        {
            "weight": 1,
            "flags": [
                "onlink"
            ],
            "gateway": "169.254.0.1",
            "dev": "uplink-1"
        },
        {
            "weight": 1,
            "flags": [
                "onlink"
            ],
            "gateway": "169.254.0.1",
            "dev": "uplink-2"
        }
    ]
}

Fixes: 663c3cb23103f4 ("iproute: implement JSON and color output")

Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
---
 ip/iproute.c | 46 ++++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

Comments

Stephen Hemminger Sept. 26, 2019, 4:07 p.m. UTC | #1
On Thu, 26 Sep 2019 17:29:34 +0200
Julien Fortin <julien@cumulusnetworks.com> wrote:

> +			print_string(PRINT_ANY, "dev",
> +				     "%s", ll_index_to_name(nh->rtnh_ifindex))

you might want to use interface color for this?
Julien Fortin Sept. 26, 2019, 6:16 p.m. UTC | #2
On Thu, Sep 26, 2019 at 6:07 PM Stephen Hemminger
<stephen@networkplumber.org> wrote:
>
> On Thu, 26 Sep 2019 17:29:34 +0200
> Julien Fortin <julien@cumulusnetworks.com> wrote:
>
> > +                     print_string(PRINT_ANY, "dev",
> > +                                  "%s", ll_index_to_name(nh->rtnh_ifindex))
>
> you might want to use interface color for this?

Since this is not part of the existing code, I guess this can be added
in a later patch.
David Ahern Oct. 1, 2019, 3:14 p.m. UTC | #3
On 9/26/19 9:29 AM, Julien Fortin wrote:
> From: Julien Fortin <julien@cumulusnetworks.com>
> 
> print_rta_multipath doesn't support JSON output:
> 
> {
>     "dst":"27.0.0.13",
>     "protocol":"bgp",
>     "metric":20,
>     "flags":[],
>     "gateway":"169.254.0.1"dev uplink-1 weight 1 ,
>     "flags":["onlink"],
>     "gateway":"169.254.0.1"dev uplink-2 weight 1 ,
>     "flags":["onlink"]
> },
> 
> since RTA_MULTIPATH has nested objects we should print them
> in a json array.
> 
> With the path we have the following output:
> 
> {
>     "flags": [],
>     "dst": "36.0.0.13",
>     "protocol": "bgp",
>     "metric": 20,
>     "nexthops": [
>         {
>             "weight": 1,
>             "flags": [
>                 "onlink"
>             ],
>             "gateway": "169.254.0.1",
>             "dev": "uplink-1"
>         },
>         {
>             "weight": 1,
>             "flags": [
>                 "onlink"
>             ],
>             "gateway": "169.254.0.1",
>             "dev": "uplink-2"
>         }
>     ]
> }
> 
> Fixes: 663c3cb23103f4 ("iproute: implement JSON and color output")
> 
> Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
> ---
>  ip/iproute.c | 46 ++++++++++++++++++++++++++++------------------
>  1 file changed, 28 insertions(+), 18 deletions(-)
> 

applied to iproute2-next. Thanks

Stephen: I see only 1 place (mdb) that prints devices with color, so
that can be done across all of the commands by a follow up.
Julien Fortin Oct. 1, 2019, 3:16 p.m. UTC | #4
Thanks David!

On Tue, Oct 1, 2019 at 5:14 PM David Ahern <dsahern@gmail.com> wrote:
>
> On 9/26/19 9:29 AM, Julien Fortin wrote:
> > From: Julien Fortin <julien@cumulusnetworks.com>
> >
> > print_rta_multipath doesn't support JSON output:
> >
> > {
> >     "dst":"27.0.0.13",
> >     "protocol":"bgp",
> >     "metric":20,
> >     "flags":[],
> >     "gateway":"169.254.0.1"dev uplink-1 weight 1 ,
> >     "flags":["onlink"],
> >     "gateway":"169.254.0.1"dev uplink-2 weight 1 ,
> >     "flags":["onlink"]
> > },
> >
> > since RTA_MULTIPATH has nested objects we should print them
> > in a json array.
> >
> > With the path we have the following output:
> >
> > {
> >     "flags": [],
> >     "dst": "36.0.0.13",
> >     "protocol": "bgp",
> >     "metric": 20,
> >     "nexthops": [
> >         {
> >             "weight": 1,
> >             "flags": [
> >                 "onlink"
> >             ],
> >             "gateway": "169.254.0.1",
> >             "dev": "uplink-1"
> >         },
> >         {
> >             "weight": 1,
> >             "flags": [
> >                 "onlink"
> >             ],
> >             "gateway": "169.254.0.1",
> >             "dev": "uplink-2"
> >         }
> >     ]
> > }
> >
> > Fixes: 663c3cb23103f4 ("iproute: implement JSON and color output")
> >
> > Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
> > ---
> >  ip/iproute.c | 46 ++++++++++++++++++++++++++++------------------
> >  1 file changed, 28 insertions(+), 18 deletions(-)
> >
>
> applied to iproute2-next. Thanks
>
> Stephen: I see only 1 place (mdb) that prints devices with color, so
> that can be done across all of the commands by a follow up.
>
diff mbox series

Patch

diff --git a/ip/iproute.c b/ip/iproute.c
index a4533851..32bb52df 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -649,24 +649,26 @@  static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
 	int len = RTA_PAYLOAD(rta);
 	int first = 1;
 
+	open_json_array(PRINT_JSON, "nexthops");
+
 	while (len >= sizeof(*nh)) {
 		struct rtattr *tb[RTA_MAX + 1];
 
 		if (nh->rtnh_len > len)
 			break;
 
-		if (!is_json_context()) {
-			if ((r->rtm_flags & RTM_F_CLONED) &&
-			    r->rtm_type == RTN_MULTICAST) {
-				if (first) {
-					fprintf(fp, "Oifs: ");
-					first = 0;
-				} else {
-					fprintf(fp, " ");
-				}
-			} else
-				fprintf(fp, "%s\tnexthop ", _SL_);
-		}
+		open_json_object(NULL);
+
+		if ((r->rtm_flags & RTM_F_CLONED) &&
+		    r->rtm_type == RTN_MULTICAST) {
+			if (first) {
+				print_string(PRINT_FP, NULL, "Oifs: ", NULL);
+				first = 0;
+			} else {
+				print_string(PRINT_FP, NULL, " ", NULL);
+			}
+		} else
+			print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
 
 		if (nh->rtnh_len > sizeof(*nh)) {
 			parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
@@ -689,22 +691,30 @@  static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
 
 		if ((r->rtm_flags & RTM_F_CLONED) &&
 		    r->rtm_type == RTN_MULTICAST) {
-			fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex));
+			print_string(PRINT_ANY, "dev",
+				     "%s", ll_index_to_name(nh->rtnh_ifindex));
+
 			if (nh->rtnh_hops != 1)
-				fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
-			fprintf(fp, " ");
+				print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
+
+			print_string(PRINT_FP, NULL, " ", NULL);
 		} else {
-			fprintf(fp, "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
+			print_string(PRINT_ANY, "dev",
+				     "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
+
 			if (r->rtm_family != AF_MPLS)
-				fprintf(fp, "weight %d ",
-					nh->rtnh_hops+1);
+				print_int(PRINT_ANY, "weight",
+					  "weight %d ", nh->rtnh_hops + 1);
 		}
 
 		print_rt_flags(fp, nh->rtnh_flags);
 
 		len -= NLMSG_ALIGN(nh->rtnh_len);
 		nh = RTNH_NEXT(nh);
+
+		close_json_object();
 	}
+	close_json_array(PRINT_JSON, NULL);
 }
 
 int print_route(struct nlmsghdr *n, void *arg)