naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame SOURCES/0011-ip-fix-ip-route-show-json-output-for-multipath-nexth.patch

359b1d
From 65d5e933e5162c3464857ee233a2f20e778ee1b6 Mon Sep 17 00:00:00 2001
359b1d
From: Andrea Claudi <aclaudi@redhat.com>
359b1d
Date: Thu, 30 Apr 2020 12:35:47 +0200
359b1d
Subject: [PATCH] ip: fix ip route show json output for multipath nexthops
359b1d
359b1d
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1738633
359b1d
Upstream Status: iproute2.git commit 4ecefff3cf250
359b1d
359b1d
commit 4ecefff3cf250c1e4499dff14c80ed38bec6d7de
359b1d
Author: Julien Fortin <julien@cumulusnetworks.com>
359b1d
Date:   Thu Sep 26 17:29:34 2019 +0200
359b1d
359b1d
    ip: fix ip route show json output for multipath nexthops
359b1d
359b1d
    print_rta_multipath doesn't support JSON output:
359b1d
359b1d
    {
359b1d
        "dst":"27.0.0.13",
359b1d
        "protocol":"bgp",
359b1d
        "metric":20,
359b1d
        "flags":[],
359b1d
        "gateway":"169.254.0.1"dev uplink-1 weight 1 ,
359b1d
        "flags":["onlink"],
359b1d
        "gateway":"169.254.0.1"dev uplink-2 weight 1 ,
359b1d
        "flags":["onlink"]
359b1d
    },
359b1d
359b1d
    since RTA_MULTIPATH has nested objects we should print them
359b1d
    in a json array.
359b1d
359b1d
    With the path we have the following output:
359b1d
359b1d
    {
359b1d
        "flags": [],
359b1d
        "dst": "36.0.0.13",
359b1d
        "protocol": "bgp",
359b1d
        "metric": 20,
359b1d
        "nexthops": [
359b1d
            {
359b1d
                "weight": 1,
359b1d
                "flags": [
359b1d
                    "onlink"
359b1d
                ],
359b1d
                "gateway": "169.254.0.1",
359b1d
                "dev": "uplink-1"
359b1d
            },
359b1d
            {
359b1d
                "weight": 1,
359b1d
                "flags": [
359b1d
                    "onlink"
359b1d
                ],
359b1d
                "gateway": "169.254.0.1",
359b1d
                "dev": "uplink-2"
359b1d
            }
359b1d
        ]
359b1d
    }
359b1d
359b1d
    Fixes: 663c3cb23103f4 ("iproute: implement JSON and color output")
359b1d
359b1d
    Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
359b1d
    Signed-off-by: David Ahern <dsahern@gmail.com>
359b1d
---
359b1d
 ip/iproute.c | 46 ++++++++++++++++++++++++++++------------------
359b1d
 1 file changed, 28 insertions(+), 18 deletions(-)
359b1d
359b1d
diff --git a/ip/iproute.c b/ip/iproute.c
359b1d
index a453385113cb9..32bb52df250c2 100644
359b1d
--- a/ip/iproute.c
359b1d
+++ b/ip/iproute.c
359b1d
@@ -649,24 +649,26 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
359b1d
 	int len = RTA_PAYLOAD(rta);
359b1d
 	int first = 1;
359b1d
 
359b1d
+	open_json_array(PRINT_JSON, "nexthops");
359b1d
+
359b1d
 	while (len >= sizeof(*nh)) {
359b1d
 		struct rtattr *tb[RTA_MAX + 1];
359b1d
 
359b1d
 		if (nh->rtnh_len > len)
359b1d
 			break;
359b1d
 
359b1d
-		if (!is_json_context()) {
359b1d
-			if ((r->rtm_flags & RTM_F_CLONED) &&
359b1d
-			    r->rtm_type == RTN_MULTICAST) {
359b1d
-				if (first) {
359b1d
-					fprintf(fp, "Oifs: ");
359b1d
-					first = 0;
359b1d
-				} else {
359b1d
-					fprintf(fp, " ");
359b1d
-				}
359b1d
-			} else
359b1d
-				fprintf(fp, "%s\tnexthop ", _SL_);
359b1d
-		}
359b1d
+		open_json_object(NULL);
359b1d
+
359b1d
+		if ((r->rtm_flags & RTM_F_CLONED) &&
359b1d
+		    r->rtm_type == RTN_MULTICAST) {
359b1d
+			if (first) {
359b1d
+				print_string(PRINT_FP, NULL, "Oifs: ", NULL);
359b1d
+				first = 0;
359b1d
+			} else {
359b1d
+				print_string(PRINT_FP, NULL, " ", NULL);
359b1d
+			}
359b1d
+		} else
359b1d
+			print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
359b1d
 
359b1d
 		if (nh->rtnh_len > sizeof(*nh)) {
359b1d
 			parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
359b1d
@@ -689,22 +691,30 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
359b1d
 
359b1d
 		if ((r->rtm_flags & RTM_F_CLONED) &&
359b1d
 		    r->rtm_type == RTN_MULTICAST) {
359b1d
-			fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex));
359b1d
+			print_string(PRINT_ANY, "dev",
359b1d
+				     "%s", ll_index_to_name(nh->rtnh_ifindex));
359b1d
+
359b1d
 			if (nh->rtnh_hops != 1)
359b1d
-				fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
359b1d
-			fprintf(fp, " ");
359b1d
+				print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
359b1d
+
359b1d
+			print_string(PRINT_FP, NULL, " ", NULL);
359b1d
 		} else {
359b1d
-			fprintf(fp, "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
359b1d
+			print_string(PRINT_ANY, "dev",
359b1d
+				     "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
359b1d
+
359b1d
 			if (r->rtm_family != AF_MPLS)
359b1d
-				fprintf(fp, "weight %d ",
359b1d
-					nh->rtnh_hops+1);
359b1d
+				print_int(PRINT_ANY, "weight",
359b1d
+					  "weight %d ", nh->rtnh_hops + 1);
359b1d
 		}
359b1d
 
359b1d
 		print_rt_flags(fp, nh->rtnh_flags);
359b1d
 
359b1d
 		len -= NLMSG_ALIGN(nh->rtnh_len);
359b1d
 		nh = RTNH_NEXT(nh);
359b1d
+
359b1d
+		close_json_object();
359b1d
 	}
359b1d
+	close_json_array(PRINT_JSON, NULL);
359b1d
 }
359b1d
 
359b1d
 int print_route(struct nlmsghdr *n, void *arg)
359b1d
-- 
359b1d
2.25.4
359b1d