From patchwork Thu Aug 15 15:00:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1147648 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="ukoAf53B"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 468V4g4qrzz9sPK for ; Fri, 16 Aug 2019 01:00:35 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733293AbfHOPAe (ORCPT ); Thu, 15 Aug 2019 11:00:34 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34710 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733284AbfHOPAd (ORCPT ); Thu, 15 Aug 2019 11:00:33 -0400 Received: by mail-wr1-f65.google.com with SMTP id s18so1727412wrn.1 for ; Thu, 15 Aug 2019 08:00:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SHpZ4QmRLvu5QStiFloxnaEe5T1qWZxwzosx1ZcjyYQ=; b=ukoAf53Bsy/bOcsJFxcxJcpKiJcYp8IjD4tGnpHtoyKbikXDc1NDu7Xe0OOOL2jNwv S8hNlQMCAq4e4PoCYUKpgkQ6XYS0sGGMqcrfnG43cAagfG704+30EVb/MDWfFU2ZWP6B /3CpEyQxc279pqVZp1tFuF7M7ckS4OD1psI6aQAmsCCh5YUvdRY+GdX5C54FUi5yzMnu 3KzkfKVDC+LvgB+FNdO55gkzbSjuxXkkpWKKpI4/LvGDuRunQCxxPTW/LiRGUZSzcvNb i9hRcmNoOd8wfVvKhMGmn0wHYojfPB++B7R6ZOmUXbxD6Nt7p8vwGZvO8Ps72w0IpQrb gvkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SHpZ4QmRLvu5QStiFloxnaEe5T1qWZxwzosx1ZcjyYQ=; b=K/ZXXRj13zhRSqtX07KWyrHaGxo//ymDsHT3usehx0RAu0zIYuQRh67YPU3lq0zznP AQmDnB0myFHzohXHSrZVJm56IWFlWb6+onKrXcJ3XYFgaCOxWPvQPV9gq8/lL6OXzkhL OsCmSgwg40lJ2Z9s5m3BZkQZOJQYXme98jTlmyn6bIF8uJp6xQF4msDmf/4JTcPhrwC8 yYg/TwWETKKEKUevi3ujfGLVe+1m+9j037+9zkE7N/DGrmaHntjzWpxFQXwkZRb7/jmw jHzcwnQzgnHllkWAnXcNjSXcCX6Fi/K/myeFrvIxrb0+HksUq4BBFS9ZFsdGOEpqZBfj GMVA== X-Gm-Message-State: APjAAAWuaNroHz7wE+J3qLgyjKOBMZJFD2fWPLtpNg0ziCMdhjTJKYjh BVLhTM8/wcf/msBAjTbkiH97Rg== X-Google-Smtp-Source: APXvYqxwWiUAz+0j8qKwd6syVgnRK/tsO398dvNffHeLQE33E6k5hiddUP3gLIUPM/g/BuOqPcM4tA== X-Received: by 2002:a5d:6a49:: with SMTP id t9mr5993705wrw.134.1565881231378; Thu, 15 Aug 2019 08:00:31 -0700 (PDT) Received: from cbtest32.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id a23sm2794857wma.24.2019.08.15.08.00.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 08:00:30 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Quentin Monnet Subject: [PATCH bpf-next 1/5] bpf: add new BPF_BTF_GET_NEXT_ID syscall command Date: Thu, 15 Aug 2019 16:00:15 +0100 Message-Id: <20190815150019.8523-2-quentin.monnet@netronome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190815150019.8523-1-quentin.monnet@netronome.com> References: <20190815150019.8523-1-quentin.monnet@netronome.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add a new command for the bpf() system call: BPF_BTF_GET_NEXT_ID is used to cycle through all BTF objects loaded on the system. The motivation is to be able to inspect (list) all BTF objects presents on the system. Signed-off-by: Quentin Monnet Reviewed-by: Jakub Kicinski --- include/linux/bpf.h | 3 +++ include/uapi/linux/bpf.h | 1 + kernel/bpf/btf.c | 4 ++-- kernel/bpf/syscall.c | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f9a506147c8a..279ea762c34e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -24,6 +24,9 @@ struct seq_file; struct btf; struct btf_type; +extern struct idr btf_idr; +extern spinlock_t btf_idr_lock; + /* map is generic key/value storage optionally accesible by eBPF programs */ struct bpf_map_ops { /* funcs callable from userspace (via syscall) */ diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4393bd4b2419..874bc5eefee1 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -106,6 +106,7 @@ enum bpf_cmd { BPF_TASK_FD_QUERY, BPF_MAP_LOOKUP_AND_DELETE_ELEM, BPF_MAP_FREEZE, + BPF_BTF_GET_NEXT_ID, }; enum bpf_map_type { diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 5fcc7a17eb5a..e716a64b2f7f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -195,8 +195,8 @@ i < btf_type_vlen(struct_type); \ i++, member++) -static DEFINE_IDR(btf_idr); -static DEFINE_SPINLOCK(btf_idr_lock); +DEFINE_IDR(btf_idr); +DEFINE_SPINLOCK(btf_idr_lock); struct btf { void *data; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 5d141f16f6fa..407b7f840874 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2874,6 +2874,10 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz err = bpf_obj_get_next_id(&attr, uattr, &map_idr, &map_idr_lock); break; + case BPF_BTF_GET_NEXT_ID: + err = bpf_obj_get_next_id(&attr, uattr, + &btf_idr, &btf_idr_lock); + break; case BPF_PROG_GET_FD_BY_ID: err = bpf_prog_get_fd_by_id(&attr); break; From patchwork Thu Aug 15 15:00:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1147649 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="z4TOHubQ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 468V4h0dTXz9sPM for ; Fri, 16 Aug 2019 01:00:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733296AbfHOPAf (ORCPT ); Thu, 15 Aug 2019 11:00:35 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:37724 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733212AbfHOPAe (ORCPT ); Thu, 15 Aug 2019 11:00:34 -0400 Received: by mail-wm1-f68.google.com with SMTP id z23so1514738wmf.2 for ; Thu, 15 Aug 2019 08:00:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=WPjOAnZ6DM44rpKDf5ErzyVWiSn5JFBQCXEWf67zCeY=; b=z4TOHubQDXl0F5kyDbvSwCXz9icNhreThbItk9nKoAQY5wQqrxBQDgRo8xatFyyEjK 2u3E8Mj5eNJBVxRuL6+e/fa6Vx6OWC2jj3Ca3vmG/D5Qxnk21+t2vCaEloSW7wzcZklT mMy+0llYblKvE6jTAFBrcPD19E107q3Ltop7YHzSmwERZuYZrgdFC71As8hHrbvuy1AP cr69m1Y7ao2sBOPwbCSnyDwrA9UV0J8bVaSo5wcvcPDt8usYlSU1olbBMoXg1symRdLD 6OC+mb4UHVrOMKRbyiw7R0SBsUUUUcuONBU1ngcMEhfvZUptdK52m1orwAOwPNvKfQiE 3pQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=WPjOAnZ6DM44rpKDf5ErzyVWiSn5JFBQCXEWf67zCeY=; b=gAPRgL1Lu9AehG3tbP2v9ZTcBtZx4YwkWX2/SvWpODI93Tv73rL6xfgPbnZtOfBgff 18jjifOMicw/WxtHdeYNvW9VuXON82qhdVzS21YlVMV63Hv1kUhWv7D2EJh0ey092IS/ mkap5G40mJquBUwuZ8j6dCyXHsOCDWSQt0T/QQiXGEHN+hPg9HcpXA0eiylq2CV+lLVS Rv6rJOfd8yoUZl1AHp2/eUf9fJ+4Ps3KuLnhydytwIma4KrXY9O4LqQB+qnP1IvV9/IG MCEThQ5h2+MqLjrklqnCAfiSii5dyMkVpZwi0KKfDodGq0yLjshxTGyRCbSiPTywXK6h wDqg== X-Gm-Message-State: APjAAAVBFEydsckVTN0bVyDHU6x81zXYUeYe014MQ4NLL0Gv/41omKRm Cne0Fvz4K7UI6KVbK6KVXywPaQ== X-Google-Smtp-Source: APXvYqzoUpka7CXCUJQAbcSDGE/qzVr65hzIknalCFmbkpMRxk0FW9NHqHkCMLDHn/JhrxHa2l/agQ== X-Received: by 2002:a1c:b189:: with SMTP id a131mr3298787wmf.7.1565881232364; Thu, 15 Aug 2019 08:00:32 -0700 (PDT) Received: from cbtest32.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id a23sm2794857wma.24.2019.08.15.08.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 08:00:31 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Quentin Monnet Subject: [PATCH bpf-next 2/5] tools: bpf: synchronise BPF UAPI header with tools Date: Thu, 15 Aug 2019 16:00:16 +0100 Message-Id: <20190815150019.8523-3-quentin.monnet@netronome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190815150019.8523-1-quentin.monnet@netronome.com> References: <20190815150019.8523-1-quentin.monnet@netronome.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Synchronise the bpf.h header under tools, to report the addition of the new BPF_BTF_GET_NEXT_ID syscall command for bpf(). Signed-off-by: Quentin Monnet --- tools/include/uapi/linux/bpf.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4393bd4b2419..874bc5eefee1 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -106,6 +106,7 @@ enum bpf_cmd { BPF_TASK_FD_QUERY, BPF_MAP_LOOKUP_AND_DELETE_ELEM, BPF_MAP_FREEZE, + BPF_BTF_GET_NEXT_ID, }; enum bpf_map_type { From patchwork Thu Aug 15 15:00:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1147650 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="KLl3BXkV"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 468V4h54CWz9sNm for ; Fri, 16 Aug 2019 01:00:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733300AbfHOPAg (ORCPT ); Thu, 15 Aug 2019 11:00:36 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:46604 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733284AbfHOPAf (ORCPT ); Thu, 15 Aug 2019 11:00:35 -0400 Received: by mail-wr1-f65.google.com with SMTP id z1so2451540wru.13 for ; Thu, 15 Aug 2019 08:00:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AMLCV3YzxdATq/4eGHOKLDLXG9C3cKBdYE1r3RyBJ+0=; b=KLl3BXkVBJpfnKeUU3H6k867vHag42Ex9+v9AEx81+dRf4yk4pCDSMXI+bLv93GQ81 YtCbU1BWam10D8THgpjZXR/LgYcx1GTIWT1c/N3668Iw6UXjrIDmpyEG1gEtlX+1yOAK L4fHF2IcmUvRqEJ5P1Q+fyDcBe76wvE44/+62dqAsC2QP+nAHAK7wqKwGh6R1G3B9KLz zg9m9do2LUWKjsGRXIBl1Xchoz8CqW9XOcw7tdO6JI1FrNEq6c7zuGtArtmsYVm3fGxS XicoR/oDCXCSbYusITcT+8dk7Qcl872DXn7X/YyYS8CrT3obFSl0uvDvDXnV5BWipkx+ SqGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AMLCV3YzxdATq/4eGHOKLDLXG9C3cKBdYE1r3RyBJ+0=; b=VOkHO5UIW14itxIdj88EF8TBihj6enhE/y7CByzZQh+oVzSRQE6/ehdJ+mVbkE+N2N BUHxe5ZRIfP7eUGZ7SIdSYbC13CQbNYvY9uSqt8yDQDFgRYo7ziqciftTbPgpS2JoqQO U7IuPazMXi9Wdf+m/oO5wmxdnI4t4rGZz+Cgqg+COmizsSCAky5xbo0O8/mGS8xnBrKj Al2u/oSXZpJvUH8eOYP4OnHkBA8Qcg42EVEDkZbrMbqzv7h6rWMTwH2B2syOI7w0cO6/ yg8XG6tdzueERKiiJQo6Av/k7iJ7kl4yZ28lQ1+JHEaEcH573OIveS2QI7/8wWYS8S6R 3Hig== X-Gm-Message-State: APjAAAW2vBp6e4ILWh6HwGQ5GCY3/McK49Es/YVkconjAu4vB1XBhzWL rs4YOP9A6RtNG5zZ3KPBvzb8Xg== X-Google-Smtp-Source: APXvYqzSPMZMQ36wQTI/IEQUqZqLLwIv9BaYuThd3wcWPBg+IUtwvRQObrrPHz97lsiTdetQriR2Lw== X-Received: by 2002:a5d:4a45:: with SMTP id v5mr5727220wrs.108.1565881233814; Thu, 15 Aug 2019 08:00:33 -0700 (PDT) Received: from cbtest32.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id a23sm2794857wma.24.2019.08.15.08.00.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 08:00:33 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Quentin Monnet Subject: [PATCH bpf-next 3/5] libbpf: refactor bpf_*_get_next_id() functions Date: Thu, 15 Aug 2019 16:00:17 +0100 Message-Id: <20190815150019.8523-4-quentin.monnet@netronome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190815150019.8523-1-quentin.monnet@netronome.com> References: <20190815150019.8523-1-quentin.monnet@netronome.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org In preparation for the introduction of a similar function for retrieving the id of the next BTF object, consolidate the code from bpf_prog_get_next_id() and bpf_map_get_next_id() in libbpf. Signed-off-by: Quentin Monnet Reviewed-by: Jakub Kicinski --- tools/lib/bpf/bpf.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index c7d7993c44bb..1439e99c9be5 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -568,7 +568,7 @@ int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr) return ret; } -int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) +static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) { union bpf_attr attr; int err; @@ -576,26 +576,21 @@ int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) memset(&attr, 0, sizeof(attr)); attr.start_id = start_id; - err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr)); + err = sys_bpf(cmd, &attr, sizeof(attr)); if (!err) *next_id = attr.next_id; return err; } -int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) +int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) { - union bpf_attr attr; - int err; - - memset(&attr, 0, sizeof(attr)); - attr.start_id = start_id; - - err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr)); - if (!err) - *next_id = attr.next_id; + return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID); +} - return err; +int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID); } int bpf_prog_get_fd_by_id(__u32 id) From patchwork Thu Aug 15 15:00:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1147652 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="zENmOImN"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 468V4n415Vz9sN1 for ; Fri, 16 Aug 2019 01:00:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387418AbfHOPAj (ORCPT ); Thu, 15 Aug 2019 11:00:39 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:36984 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733212AbfHOPAh (ORCPT ); Thu, 15 Aug 2019 11:00:37 -0400 Received: by mail-wr1-f67.google.com with SMTP id z11so2496371wrt.4 for ; Thu, 15 Aug 2019 08:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zMkkZCc7A2SkaJGYw2ZUa/LWnENmyPIwRLlvJbRLQ28=; b=zENmOImNLUw07wZgcZYaXEpDwbRtqdLrLC+vfmR21VxDh3W7U9lfBvTNT/TRbchEkb 4LXFb6x//KwtZC2FZJzyVGkrtzJEYjb+R2HLvjkXu9FrLslIIFQ8ZpyfLcGoadE/ZzJy O7O3EqjntXI/vNw9b59sPYRuWdo4YqPyTW2NyhIBqlpVabN/XIPp9h+VpG/CVe/rLkp3 1dhDbQqk9afSFsRAo+kPS6Tl6M/UwFPmB04wnmasym+LSULvxb6QBWVfO/fSsXS4OKKk Iu8kHvC4zob1cwLokEEroWLIyyDyjOEF3G4VGMkuoAbiLAwKpDRghqc7k7bWCIHpjQ6C BSJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zMkkZCc7A2SkaJGYw2ZUa/LWnENmyPIwRLlvJbRLQ28=; b=c51E1r+SywLzZIbrBoIRG9i1YzxouWyzJW+X7BmKDX1jxy0cDar6oP8cl1Wcqrhkae gV6NE7BNFOxAc3/Gpvsf8ZisHjKtp5t4Y4ADl2RxGDmiVBcgrezr12TmE7PBDZiCQhry LoJ993NP1UlUB8/kyk00U+DsuqLoT6O9fQZFbLaumDUE/RVriOA6CHOtLD+6SXCyJlBD PYR9Nw/hTmR+UbdAueYTeFTkjMIj47MKZrM8PYCJ3hAx6+94dFwfjDTlk1ppmOQ1OFne 1kkJX5VKuPcSdCvyU8fzYwoFPrTj/CuWhy5l6Iw4EPVt4nXmeLuIrwy3uzoHoLsEu18x 6GsA== X-Gm-Message-State: APjAAAWZ4N+OjM7QwnI2GaxsCuzoF/ZVXGko4lQ0mPL2yEksc3bh/0JT eVSpK2M4pFxnpl1F4e19c2tlww== X-Google-Smtp-Source: APXvYqwm3duem7KqODE8INGIWxm9P/+JbdFX4+gcHW+viGD9pnm31kPLdAvUMqQ2hJfz6OQYpvg1CQ== X-Received: by 2002:adf:f2c1:: with SMTP id d1mr6063075wrp.157.1565881235174; Thu, 15 Aug 2019 08:00:35 -0700 (PDT) Received: from cbtest32.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id a23sm2794857wma.24.2019.08.15.08.00.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 08:00:34 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Quentin Monnet Subject: [PATCH bpf-next 4/5] libbpf: add bpf_btf_get_next_id() to cycle through BTF objects Date: Thu, 15 Aug 2019 16:00:18 +0100 Message-Id: <20190815150019.8523-5-quentin.monnet@netronome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190815150019.8523-1-quentin.monnet@netronome.com> References: <20190815150019.8523-1-quentin.monnet@netronome.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add an API function taking a BTF object id and providing the id of the next BTF object in the kernel. This can be used to list all BTF objects loaded on the system. Signed-off-by: Quentin Monnet Reviewed-by: Jakub Kicinski --- tools/lib/bpf/Makefile | 2 +- tools/lib/bpf/bpf.c | 5 +++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/libbpf.map | 5 +++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index 9312066a1ae3..f6c0295c6a5e 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile @@ -3,7 +3,7 @@ BPF_VERSION = 0 BPF_PATCHLEVEL = 0 -BPF_EXTRAVERSION = 4 +BPF_EXTRAVERSION = 5 MAKEFLAGS += --no-print-directory diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 1439e99c9be5..cbb933532981 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -593,6 +593,11 @@ int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID); } +int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID); +} + int bpf_prog_get_fd_by_id(__u32 id) { union bpf_attr attr; diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index ff42ca043dc8..0db01334740f 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -156,6 +156,7 @@ LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 *retval, __u32 *duration); LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); +LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index f9d316e873d8..664ce8e7a60e 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -184,3 +184,8 @@ LIBBPF_0.0.4 { perf_buffer__new_raw; perf_buffer__poll; } LIBBPF_0.0.3; + +LIBBPF_0.0.5 { + global: + bpf_btf_get_next_id; +} LIBBPF_0.0.4; From patchwork Thu Aug 15 15:00:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1147653 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netronome.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=netronome-com.20150623.gappssmtp.com header.i=@netronome-com.20150623.gappssmtp.com header.b="AP8QcTXT"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 468V4n5K1Xz9sNm for ; Fri, 16 Aug 2019 01:00:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733212AbfHOPAk (ORCPT ); Thu, 15 Aug 2019 11:00:40 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:35450 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733311AbfHOPAj (ORCPT ); Thu, 15 Aug 2019 11:00:39 -0400 Received: by mail-wr1-f65.google.com with SMTP id k2so2500846wrq.2 for ; Thu, 15 Aug 2019 08:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=F3c3B8ToqzZZt83DOovnHXflBxS6NmcDdVW+ZOpTlGo=; b=AP8QcTXTuMMWWd5w7alGjDTF02YHMU03TRl28e6+vM04T/ZC5fDED5nGsZ7RWNy6ZW I/PPMD89dB5h+XdeW7nwtPJolxJ/8zOQh/cXog4eLLWf9Pg+NdNceZ9O/EocXjZUPesM jLzKjAimLOFNIWsqtK4TWBMyK1p9Xa5zc2+KB65+o+HIK5CSFSKj7Tv0WTg/Idbvsv30 eGsr3SrGbsRd6ayxy5+YH7t+DBQwVZ4GVy69NY/i02k51aN2v0Myq/u/3kGr34MaXipC vfNu95qi9s4nDTsbfdyskDVNe36xzi2W44yWJApV1pqj5gBTjWQ8Cpa5RNRyHhJC3P5t +Xpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=F3c3B8ToqzZZt83DOovnHXflBxS6NmcDdVW+ZOpTlGo=; b=QknlyWJ6OARrbFE8Kbao8gJ+F8bTn3JKSoMxCMDJsNCn6muHAKj0Zt3PVzAUP5hWgx 9WA/zq4ymhxWkqvwUtNSGqlk9mwgHVCfgvskwR2I1H2fHcB7ywNlrNuMJ6NS1VZG09c4 7a2d20tML9bGxSVS0A4zG7M//j7VJ2A6zVtkDfc0OkESVZ+EJlkru1mkl6kpXPzTcEmm 3OqgbKL1fC9hm3Cb8j5FRZE4PchmG5iJB5LEdESDz9bQ3+H3VlL5HT38HREG91L9CgcV djwW9zDm6lwh/L2V9iUdz4WRaQkGlG7ibB9GroX5/zOuwHrkSR3GXGAAJrNyag+Q/fwF GNbQ== X-Gm-Message-State: APjAAAUHtO0Xa2F5EPxMt00m64kj9VjwAxLwzjSaKvJULjJrKLcKrTI/ aw3c1t+m2m3VvvttKHQbrDzVeA== X-Google-Smtp-Source: APXvYqwK+qit5oq5RSA9DFoMN+WyG063cszNlVo/RL7ttpalDYNzqRYa7Bi9UwnmrytQljp8H/PeJA== X-Received: by 2002:a05:6000:1085:: with SMTP id y5mr5871079wrw.285.1565881237312; Thu, 15 Aug 2019 08:00:37 -0700 (PDT) Received: from cbtest32.netronome.com ([217.38.71.146]) by smtp.gmail.com with ESMTPSA id a23sm2794857wma.24.2019.08.15.08.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 08:00:35 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, oss-drivers@netronome.com, Quentin Monnet Subject: [PATCH bpf-next 5/5] tools: bpftool: implement "bpftool btf show|list" Date: Thu, 15 Aug 2019 16:00:19 +0100 Message-Id: <20190815150019.8523-6-quentin.monnet@netronome.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190815150019.8523-1-quentin.monnet@netronome.com> References: <20190815150019.8523-1-quentin.monnet@netronome.com> Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add a "btf list" (alias: "btf show") subcommand to bpftool in order to dump all BTF objects loaded on a system. When running the command, hash tables are built in bpftool to retrieve all the associations between BTF objects and BPF maps and programs. This allows for printing all such associations when listing the BTF objects. The command is added at the top of the subcommands for "bpftool btf", so that typing only "bpftool btf" also comes down to listing the programs. We could not have this with the previous command ("dump"), which required a BTF object id, so it should not break any previous behaviour. This also makes the "btf" command behaviour consistent with "prog" or "map". Bash completion is updated to use "bpftool btf" instead of "bpftool prog" to list the BTF ids, as it looks more consistent. Example output (plain): # bpftool btf show 9: size 2989B prog_ids 21 map_ids 15 17: size 2847B prog_ids 36 map_ids 30,29,28 26: size 2847B Signed-off-by: Quentin Monnet Reviewed-by: Jakub Kicinski --- .../bpf/bpftool/Documentation/bpftool-btf.rst | 7 + tools/bpf/bpftool/bash-completion/bpftool | 20 +- tools/bpf/bpftool/btf.c | 342 +++++++++++++++++- 3 files changed, 363 insertions(+), 6 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst index 6694a0fc8f99..39615f8e145b 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst @@ -19,6 +19,7 @@ SYNOPSIS BTF COMMANDS ============= +| **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*] | **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] | **bpftool** **btf help** | @@ -29,6 +30,12 @@ BTF COMMANDS DESCRIPTION =========== + **bpftool btf { show | list }** [**id** *BTF_ID*] + Show information about loaded BTF objects. If a BTF ID is + specified, show information only about given BTF object, + otherwise list all BTF objects currently loaded on the + system. + **bpftool btf dump** *BTF_SRC* Dump BTF entries from a given *BTF_SRC*. diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index df16c5415444..2c0081121b2b 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -73,8 +73,8 @@ _bpftool_get_prog_tags() _bpftool_get_btf_ids() { - COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \ - command sed -n 's/.*"btf_id": \(.*\),\?$/\1/p' )" -- "$cur" ) ) + COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \ + command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) ) } _bpftool_get_obj_map_names() @@ -674,7 +674,7 @@ _bpftool() map) _bpftool_get_map_ids ;; - dump) + $command) _bpftool_get_btf_ids ;; esac @@ -702,9 +702,21 @@ _bpftool() ;; esac ;; + show|list) + case $prev in + $command) + COMPREPLY+=( $( compgen -W "id" -- "$cur" ) ) + ;; + id) + _bpftool_get_btf_ids + ;; + esac + return 0 + ;; *) [[ $prev == $object ]] && \ - COMPREPLY=( $( compgen -W 'dump help' -- "$cur" ) ) + COMPREPLY=( $( compgen -W 'dump help show list' \ + -- "$cur" ) ) ;; esac ;; diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 1b8ec91899e6..839b76af689c 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "btf.h" #include "json_writer.h" @@ -35,6 +36,16 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { [BTF_KIND_DATASEC] = "DATASEC", }; +struct btf_attach_table { + DECLARE_HASHTABLE(table, 16); +}; + +struct btf_attach_point { + __u32 obj_id; + __u32 btf_id; + struct hlist_node hash; +}; + static const char *btf_int_enc_str(__u8 encoding) { switch (encoding) { @@ -522,6 +533,330 @@ static int do_dump(int argc, char **argv) return err; } +static int btf_parse_fd(int *argc, char ***argv) +{ + unsigned int id; + char *endptr; + int fd; + + if (!is_prefix(*argv[0], "id")) { + p_err("expected 'id', got: '%s'?", **argv); + return -1; + } + NEXT_ARGP(); + + id = strtoul(**argv, &endptr, 0); + if (*endptr) { + p_err("can't parse %s as ID", **argv); + return -1; + } + NEXT_ARGP(); + + fd = bpf_btf_get_fd_by_id(id); + if (fd < 0) + p_err("can't get BTF object by id (%u): %s", + id, strerror(errno)); + + return fd; +} + +static void delete_btf_table(struct btf_attach_table *tab) +{ + struct btf_attach_point *obj; + struct hlist_node *tmp; + + unsigned int bkt; + + hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { + hash_del(&obj->hash); + free(obj); + } +} + +static int +build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type, + void *info, __u32 *len) +{ + static const char * const names[] = { + [BPF_OBJ_UNKNOWN] = "unknown", + [BPF_OBJ_PROG] = "prog", + [BPF_OBJ_MAP] = "map", + }; + struct btf_attach_point *obj_node; + __u32 btf_id, id = 0; + int err; + int fd; + + while (true) { + switch (type) { + case BPF_OBJ_PROG: + err = bpf_prog_get_next_id(id, &id); + break; + case BPF_OBJ_MAP: + err = bpf_map_get_next_id(id, &id); + break; + default: + err = -1; + p_err("unexpected object type: %d", type); + goto err_free; + } + if (err) { + if (errno == ENOENT) { + err = 0; + break; + } + p_err("can't get next %s: %s%s", names[type], + strerror(errno), + errno == EINVAL ? " -- kernel too old?" : ""); + goto err_free; + } + + switch (type) { + case BPF_OBJ_PROG: + fd = bpf_prog_get_fd_by_id(id); + break; + case BPF_OBJ_MAP: + fd = bpf_map_get_fd_by_id(id); + break; + default: + err = -1; + p_err("unexpected object type: %d", type); + goto err_free; + } + if (fd < 0) { + if (errno == ENOENT) + continue; + p_err("can't get %s by id (%u): %s", names[type], id, + strerror(errno)); + err = -1; + goto err_free; + } + + memset(info, 0, *len); + err = bpf_obj_get_info_by_fd(fd, info, len); + close(fd); + if (err) { + p_err("can't get %s info: %s", names[type], + strerror(errno)); + goto err_free; + } + + switch (type) { + case BPF_OBJ_PROG: + btf_id = ((struct bpf_prog_info *)info)->btf_id; + break; + case BPF_OBJ_MAP: + btf_id = ((struct bpf_map_info *)info)->btf_id; + break; + default: + err = -1; + p_err("unexpected object type: %d", type); + goto err_free; + } + if (!btf_id) + continue; + + obj_node = calloc(1, sizeof(*obj_node)); + if (!obj_node) { + p_err("failed to allocate memory: %s", strerror(errno)); + goto err_free; + } + + obj_node->obj_id = id; + obj_node->btf_id = btf_id; + hash_add(tab->table, &obj_node->hash, obj_node->btf_id); + } + + return 0; + +err_free: + delete_btf_table(tab); + return err; +} + +static int +build_btf_tables(struct btf_attach_table *btf_prog_table, + struct btf_attach_table *btf_map_table) +{ + struct bpf_prog_info prog_info; + __u32 prog_len = sizeof(prog_info); + struct bpf_map_info map_info; + __u32 map_len = sizeof(map_info); + int err = 0; + + err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info, + &prog_len); + if (err) + return err; + + err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info, + &map_len); + if (err) { + delete_btf_table(btf_prog_table); + return err; + } + + return 0; +} + +static void +show_btf_plain(struct bpf_btf_info *info, int fd, + struct btf_attach_table *btf_prog_table, + struct btf_attach_table *btf_map_table) +{ + struct btf_attach_point *obj; + int n; + + printf("%u: ", info->id); + printf("size %uB", info->btf_size); + + n = 0; + hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) { + if (obj->btf_id == info->id) + printf("%s%u", n++ == 0 ? " prog_ids " : ",", + obj->obj_id); + } + + n = 0; + hash_for_each_possible(btf_map_table->table, obj, hash, info->id) { + if (obj->btf_id == info->id) + printf("%s%u", n++ == 0 ? " map_ids " : ",", + obj->obj_id); + } + + printf("\n"); +} + +static void +show_btf_json(struct bpf_btf_info *info, int fd, + struct btf_attach_table *btf_prog_table, + struct btf_attach_table *btf_map_table) +{ + struct btf_attach_point *obj; + + jsonw_start_object(json_wtr); /* btf object */ + jsonw_uint_field(json_wtr, "id", info->id); + jsonw_uint_field(json_wtr, "size", info->btf_size); + + jsonw_name(json_wtr, "prog_ids"); + jsonw_start_array(json_wtr); /* prog_ids */ + hash_for_each_possible(btf_prog_table->table, obj, hash, + info->id) { + if (obj->btf_id == info->id) + jsonw_uint(json_wtr, obj->obj_id); + } + jsonw_end_array(json_wtr); /* prog_ids */ + + jsonw_name(json_wtr, "map_ids"); + jsonw_start_array(json_wtr); /* map_ids */ + hash_for_each_possible(btf_map_table->table, obj, hash, + info->id) { + if (obj->btf_id == info->id) + jsonw_uint(json_wtr, obj->obj_id); + } + jsonw_end_array(json_wtr); /* map_ids */ + jsonw_end_object(json_wtr); /* btf object */ +} + +static int +show_btf(int fd, struct btf_attach_table *btf_prog_table, + struct btf_attach_table *btf_map_table) +{ + struct bpf_btf_info info = {}; + __u32 len = sizeof(info); + int err; + + err = bpf_obj_get_info_by_fd(fd, &info, &len); + if (err) { + p_err("can't get BTF object info: %s", strerror(errno)); + return -1; + } + + if (json_output) + show_btf_json(&info, fd, btf_prog_table, btf_map_table); + else + show_btf_plain(&info, fd, btf_prog_table, btf_map_table); + + return 0; +} + +static int do_show(int argc, char **argv) +{ + struct btf_attach_table btf_prog_table; + struct btf_attach_table btf_map_table; + int err, fd = -1; + __u32 id = 0; + + if (argc == 2) { + fd = btf_parse_fd(&argc, &argv); + if (fd < 0) + return -1; + } + + if (argc) { + if (fd >= 0) + close(fd); + return BAD_ARG(); + } + + hash_init(btf_prog_table.table); + hash_init(btf_map_table.table); + err = build_btf_tables(&btf_prog_table, &btf_map_table); + if (err) { + if (fd >= 0) + close(fd); + return err; + } + + if (fd >= 0) { + err = show_btf(fd, &btf_prog_table, &btf_map_table); + close(fd); + goto exit_free; + } + + if (json_output) + jsonw_start_array(json_wtr); /* root array */ + + while (true) { + err = bpf_btf_get_next_id(id, &id); + if (err) { + if (errno == ENOENT) { + err = 0; + break; + } + p_err("can't get next BTF object: %s%s", + strerror(errno), + errno == EINVAL ? " -- kernel too old?" : ""); + err = -1; + break; + } + + fd = bpf_btf_get_fd_by_id(id); + if (fd < 0) { + if (errno == ENOENT) + continue; + p_err("can't get BTF object by id (%u): %s", + id, strerror(errno)); + err = -1; + break; + } + + err = show_btf(fd, &btf_prog_table, &btf_map_table); + close(fd); + if (err) + break; + } + + if (json_output) + jsonw_end_array(json_wtr); /* root array */ + +exit_free: + delete_btf_table(&btf_prog_table); + delete_btf_table(&btf_map_table); + + return err; +} + static int do_help(int argc, char **argv) { if (json_output) { @@ -530,7 +865,8 @@ static int do_help(int argc, char **argv) } fprintf(stderr, - "Usage: %s btf dump BTF_SRC [format FORMAT]\n" + "Usage: %s btf { show | list } [id BTF_ID]\n" + " %s btf dump BTF_SRC [format FORMAT]\n" " %s btf help\n" "\n" " BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n" @@ -539,12 +875,14 @@ static int do_help(int argc, char **argv) " " HELP_SPEC_PROGRAM "\n" " " HELP_SPEC_OPTIONS "\n" "", - bin_name, bin_name); + bin_name, bin_name, bin_name); return 0; } static const struct cmd cmds[] = { + { "show", do_show }, + { "list", do_show }, { "help", do_help }, { "dump", do_dump }, { 0 }