* [PATCH iproute2(-next) 1/1] ip: fix ip route show json output for multipath nexthops
@ 2019-09-24 22:32 Julien Fortin
2019-09-26 14:58 ` David Ahern
0 siblings, 1 reply; 2+ messages in thread
From: Julien Fortin @ 2019-09-24 22:32 UTC (permalink / raw)
To: netdev; +Cc: roopa, Julien Fortin
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 | 30 +++++++++++++++++++++---------
1 file changed, 21 insertions(+), 9 deletions(-)
diff --git a/ip/iproute.c b/ip/iproute.c
index a4533851..5d5f1551 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -649,23 +649,27 @@ 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;
+ open_json_object(NULL);
+
if (!is_json_context()) {
if ((r->rtm_flags & RTM_F_CLONED) &&
r->rtm_type == RTN_MULTICAST) {
if (first) {
- fprintf(fp, "Oifs: ");
+ print_string(PRINT_FP, NULL, "Oifs: ", NULL);
first = 0;
} else {
- fprintf(fp, " ");
+ print_string(PRINT_FP, NULL, " ", NULL);
}
} else
- fprintf(fp, "%s\tnexthop ", _SL_);
+ print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
}
if (nh->rtnh_len > sizeof(*nh)) {
@@ -689,22 +693,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)
--
2.23.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH iproute2(-next) 1/1] ip: fix ip route show json output for multipath nexthops
2019-09-24 22:32 [PATCH iproute2(-next) 1/1] ip: fix ip route show json output for multipath nexthops Julien Fortin
@ 2019-09-26 14:58 ` David Ahern
0 siblings, 0 replies; 2+ messages in thread
From: David Ahern @ 2019-09-26 14:58 UTC (permalink / raw)
To: Julien Fortin, netdev; +Cc: roopa
On 9/24/19 4:32 PM, Julien Fortin wrote:
> diff --git a/ip/iproute.c b/ip/iproute.c
> index a4533851..5d5f1551 100644
> --- a/ip/iproute.c
> +++ b/ip/iproute.c
> @@ -649,23 +649,27 @@ 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;
>
> + open_json_object(NULL);
> +
> if (!is_json_context()) {
With the fprintf removed in favor of print_string(PRINT_FP), you should
be able to remove this is_json_context() check and remove a level of
indentation for this section.
> if ((r->rtm_flags & RTM_F_CLONED) &&
> r->rtm_type == RTN_MULTICAST) {
> if (first) {
> - fprintf(fp, "Oifs: ");
> + print_string(PRINT_FP, NULL, "Oifs: ", NULL);
> first = 0;
> } else {
> - fprintf(fp, " ");
> + print_string(PRINT_FP, NULL, " ", NULL);
> }
> } else
> - fprintf(fp, "%s\tnexthop ", _SL_);
> + print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
> }
>
> if (nh->rtnh_len > sizeof(*nh)) {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-09-26 14:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-24 22:32 [PATCH iproute2(-next) 1/1] ip: fix ip route show json output for multipath nexthops Julien Fortin
2019-09-26 14:58 ` David Ahern
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox