Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7181134ybi; Wed, 5 Jun 2019 12:40:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqxJZDYpxbpqbF7Jzh4S4ihpSxBbosNJd5CyAs5hEw/Ap8CiVJ/xroLJrdHfORNZsmfoLe7D X-Received: by 2002:a17:90a:3aed:: with SMTP id b100mr2994429pjc.63.1559763637724; Wed, 05 Jun 2019 12:40:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559763637; cv=none; d=google.com; s=arc-20160816; b=oeAi+omyrnEk4sp0W5PBP9DMX0oDwRDxTHgHubb6ahpUbSgO0OMXnmSvI7aH/53/yU x1EQqkVbo6SQ0AC6udvWRVLXt+00ZMyVGqTIwM+TU/Z0BNlVt8kcIx65+KyB3pH+73H6 fsTQqsKcGAsm3mT66N/SR8CbEvAxg5ZRbHF8DxkcDm3gDltnVg7rfagG/QFMi0/M1KdR alqMUKK/a3d6QkFvyQ0qH/MdVqeU63+WmRU2mM2jBoMMHlFuCWCDU/MabxiffLXUTj5q HicNKHH3jmTS9DWeRPLUHlc2Sokn6nD70t5nZvf52W61J4UInC1e7BDiik5R0SHrRAOa o/Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=O2csJP2e8mT2YZIAsKNMmzTnJtLz0nsfolBkD/FQ0NM=; b=beDFM2aENIEQjTjLLkWYqLuM+hBrGYCnfAQ9cL6xPX3qNrox9SOdAPYFlJLR4zsm3I ECrpd2s87XHuSnu2JaRT4xiFq7PfRGZR8zrn+CfwGCC9aKJIE5cZmmAU+HzZ/HfppEnf RRaV0+X7lZs5yXtvbs8AZ5l6U/dmRfOdddIJwA3FDvGgljuo5VheNVavspIc54yWJKNA yL4ZerlsmYymLXROtVpsflFZ0R9ynVC3OKy7yp2o5ONfAWEakn41ukWEvrT7zd/nPVwF Bmvr5ORuoCu+EKLNVSVLZIHIKB+n9SJVX3RqIxRPeb0LXJfjaTs6aWxLNyxKkiT9Jgmz 4gvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iNXoPmjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 9si29287769pjd.48.2019.06.05.12.40.21; Wed, 05 Jun 2019 12:40:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iNXoPmjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726747AbfFEThH (ORCPT + 99 others); Wed, 5 Jun 2019 15:37:07 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:33451 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726305AbfFEThG (ORCPT ); Wed, 5 Jun 2019 15:37:06 -0400 Received: by mail-qt1-f196.google.com with SMTP id 14so19454047qtf.0; Wed, 05 Jun 2019 12:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=O2csJP2e8mT2YZIAsKNMmzTnJtLz0nsfolBkD/FQ0NM=; b=iNXoPmjllI2/CbhIrRsJHG8jG71wQBxSAfnaOyDnT60rEsaUMvW4f2k91xQ4XWIdwC Pm8i3V7NI4ynV+7ThzEN/bQG6dRXwBu8HWxm4KgCHvXdxm2JGDjVv10rGuOQNSMKLvld T5bm8j+sYOoBio994RCOeI2lrTUBnLVTA/wmqp0sO25fY+FAE30GFF+tSVNlsoZlH2/2 qhjojyuQjL7qtOJ16BA4aDRB+T7IFnaK4IBsaTjwTLshCovTfnLTCXJlv7hTpQnd0WLt Q+Z8pMLnPOm5kGo/12ntVIQ0QSuDior4Mb1OvxNZW3NQhFN1aVkiwTdqXlhbcocqs5pU yFQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=O2csJP2e8mT2YZIAsKNMmzTnJtLz0nsfolBkD/FQ0NM=; b=BEa43mN8TbuOG5zncDHEI+tO2x1UDUo/Ny7bmcZHIpxlc8pxhku5vBWVHLEk/N+L8t fSBGXx3Q+QRah6UWay6qU831sB44Bb8unrLgkJ0vUIdLz2qlOiRw4MgHoilc2jj9H/xd uEOKMd0HGmxl2FnettwMohwavUnygcQ9/fIW9N8Haw6AF+mJHsnKFuDHfwqq2DQzhiCh aWmXu44v4SxBcF6sUgJ/hmJ99/jhGHD/mMAkj6Nnk8D/ds/sfWPsWOHD/gcZ2JwCV9JU e1LgOekDPvJ6+IbzzSYJfYn3TbieKZLknQ+6k1E04lSDPSXO6Q2k6B0r75dIolB/HFhz jOmA== X-Gm-Message-State: APjAAAV4pJztrcLX2eAITXt3LfrwByxrJKk2Jxvaq1Vzi70K1svZDMJ0 EOkxVb3XVYHrWs7NAUz2Andl5KQQPxDeWrK0qW0= X-Received: by 2002:a0c:95af:: with SMTP id s44mr7864192qvs.162.1559763425613; Wed, 05 Jun 2019 12:37:05 -0700 (PDT) MIME-Version: 1.0 References: <20190605191707.24429-1-krzesimir@kinvolk.io> In-Reply-To: <20190605191707.24429-1-krzesimir@kinvolk.io> From: Andrii Nakryiko Date: Wed, 5 Jun 2019 12:36:54 -0700 Message-ID: Subject: Re: [BPF v1] tools: bpftool: Fix JSON output when lookup fails To: Krzesimir Nowak Cc: bpf , Alban Crequy , =?UTF-8?Q?Iago_L=C3=B3pez_Galeiras?= , Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jakub Kicinski , Stanislav Fomichev , Prashant Bhole , Okash Khawaja , David Calavera , Networking , open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 5, 2019 at 12:18 PM Krzesimir Nowak wrote: > > In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros > into functions") one case of error reporting was special cased, so it > could report a lookup error for a specific key when dumping the map > element. What the code forgot to do is to wrap the key and value keys > into a JSON object, so an example output of pretty JSON dump of a > sockhash map (which does not support looking up its values) is: > > [ > "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" > ], > "value": { > "error": "Operation not supported" > }, > "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" > ], > "value": { > "error": "Operation not supported" > } > ] > > Note the key-value pairs inside the toplevel array. They should be > wrapped inside a JSON object, otherwise it is an invalid JSON. This > commit fixes this, so the output now is: > > [{ > "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" > ], > "value": { > "error": "Operation not supported" > } > },{ > "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" > ], > "value": { > "error": "Operation not supported" > } > } > ] > > Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions") > Cc: Quentin Monnet > Signed-off-by: Krzesimir Nowak > --- Acked-by: Andrii Nakryiko > tools/bpf/bpftool/map.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c > index 3ec82904ccec..5da5a7311f13 100644 > --- a/tools/bpf/bpftool/map.c > +++ b/tools/bpf/bpftool/map.c > @@ -716,12 +716,14 @@ static int dump_map_elem(int fd, void *key, void *value, > return 0; > > if (json_output) { > + jsonw_start_object(json_wtr); > jsonw_name(json_wtr, "key"); > print_hex_data_json(key, map_info->key_size); > jsonw_name(json_wtr, "value"); > jsonw_start_object(json_wtr); > jsonw_string_field(json_wtr, "error", strerror(lookup_errno)); > jsonw_end_object(json_wtr); > + jsonw_end_object(json_wtr); > } else { > const char *msg = NULL; > > -- > 2.20.1 >