All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2] ip-route: fix json formatting for metrics
@ 2019-07-08  9:36 Andrea Claudi
  2019-07-08 15:14 ` Andrea Claudi
  2019-07-10  0:29 ` Stephen Hemminger
  0 siblings, 2 replies; 3+ messages in thread
From: Andrea Claudi @ 2019-07-08  9:36 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

Setting metrics for routes currently lead to non-parsable
json output. For example:

$ ip link add type dummy
$ ip route add 192.168.2.0 dev dummy0 metric 100 mtu 1000 rto_min 3
$ ip -j route | jq
parse error: ':' not as part of an object at line 1, column 319

Fixing this opening a json object in the metrics array and using
print_string() instead of fprintf().

This is the output for the above commands applying this patch:

$ ip -j route | jq
[
  {
    "dst": "192.168.2.0",
    "dev": "dummy0",
    "scope": "link",
    "metric": 100,
    "flags": [],
    "metrics": [
      {
        "mtu": 1000,
        "rto_min": 3
      }
    ]
  }
]

Fixes: 663c3cb23103f ("iproute: implement JSON and color output")
Fixes: 968272e791710 ("iproute: refactor metrics print")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 ip/iproute.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index 1669e0138259e..2f9b612b0b506 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -578,6 +578,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
 	int i;
 
 	open_json_array(PRINT_JSON, "metrics");
+	open_json_object(NULL);
 
 	parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
 
@@ -611,7 +612,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
 			print_rtax_features(fp, val);
 			break;
 		default:
-			fprintf(fp, "%u ", val);
+			print_uint(PRINT_ANY, mx_names[i], "%u ", val);
 			break;
 
 		case RTAX_RTT:
@@ -639,6 +640,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
 		}
 	}
 
+	close_json_object();
 	close_json_array(PRINT_JSON, NULL);
 }
 
-- 
2.20.1


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

* Re: [PATCH iproute2] ip-route: fix json formatting for metrics
  2019-07-08  9:36 [PATCH iproute2] ip-route: fix json formatting for metrics Andrea Claudi
@ 2019-07-08 15:14 ` Andrea Claudi
  2019-07-10  0:29 ` Stephen Hemminger
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Claudi @ 2019-07-08 15:14 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, David Ahern

On Mon, Jul 8, 2019 at 11:38 AM Andrea Claudi <aclaudi@redhat.com> wrote:
>
> Setting metrics for routes currently lead to non-parsable
> json output. For example:
>
> $ ip link add type dummy
> $ ip route add 192.168.2.0 dev dummy0 metric 100 mtu 1000 rto_min 3
> $ ip -j route | jq
> parse error: ':' not as part of an object at line 1, column 319
>
> Fixing this opening a json object in the metrics array and using
> print_string() instead of fprintf().
>
> This is the output for the above commands applying this patch:
>
> $ ip -j route | jq
> [
>   {
>     "dst": "192.168.2.0",
>     "dev": "dummy0",
>     "scope": "link",
>     "metric": 100,
>     "flags": [],
>     "metrics": [
>       {
>         "mtu": 1000,
>         "rto_min": 3
>       }
>     ]
>   }
> ]
>
> Fixes: 663c3cb23103f ("iproute: implement JSON and color output")
> Fixes: 968272e791710 ("iproute: refactor metrics print")
> Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> ---
>  ip/iproute.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/ip/iproute.c b/ip/iproute.c
> index 1669e0138259e..2f9b612b0b506 100644
> --- a/ip/iproute.c
> +++ b/ip/iproute.c
> @@ -578,6 +578,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
>         int i;
>
>         open_json_array(PRINT_JSON, "metrics");
> +       open_json_object(NULL);
>
>         parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
>
> @@ -611,7 +612,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
>                         print_rtax_features(fp, val);
>                         break;
>                 default:
> -                       fprintf(fp, "%u ", val);
> +                       print_uint(PRINT_ANY, mx_names[i], "%u ", val);
>                         break;
>
>                 case RTAX_RTT:
> @@ -639,6 +640,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
>                 }
>         }
>
> +       close_json_object();
>         close_json_array(PRINT_JSON, NULL);
>  }
>
> --
> 2.20.1
>

Sorry, I forgot to add:
Reported-by: Frank Hofmann <fhofmann@cloudflare.com>

Regards,
Andrea

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

* Re: [PATCH iproute2] ip-route: fix json formatting for metrics
  2019-07-08  9:36 [PATCH iproute2] ip-route: fix json formatting for metrics Andrea Claudi
  2019-07-08 15:14 ` Andrea Claudi
@ 2019-07-10  0:29 ` Stephen Hemminger
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2019-07-10  0:29 UTC (permalink / raw)
  To: Andrea Claudi; +Cc: netdev, dsahern

On Mon,  8 Jul 2019 11:36:42 +0200
Andrea Claudi <aclaudi@redhat.com> wrote:

> Setting metrics for routes currently lead to non-parsable
> json output. For example:
> 
> $ ip link add type dummy
> $ ip route add 192.168.2.0 dev dummy0 metric 100 mtu 1000 rto_min 3
> $ ip -j route | jq
> parse error: ':' not as part of an object at line 1, column 319
> 
> Fixing this opening a json object in the metrics array and using
> print_string() instead of fprintf().
> 
> This is the output for the above commands applying this patch:
> 
> $ ip -j route | jq
> [
>   {
>     "dst": "192.168.2.0",
>     "dev": "dummy0",
>     "scope": "link",
>     "metric": 100,
>     "flags": [],
>     "metrics": [
>       {
>         "mtu": 1000,
>         "rto_min": 3
>       }
>     ]
>   }
> ]
> 
> Fixes: 663c3cb23103f ("iproute: implement JSON and color output")
> Fixes: 968272e791710 ("iproute: refactor metrics print")
> Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> ---

Applied, thanks

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

end of thread, other threads:[~2019-07-10  0:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-08  9:36 [PATCH iproute2] ip-route: fix json formatting for metrics Andrea Claudi
2019-07-08 15:14 ` Andrea Claudi
2019-07-10  0:29 ` Stephen Hemminger

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.