From patchwork Wed Apr 15 19:27:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 221143 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C97EC2BA19 for ; Wed, 15 Apr 2020 19:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3276C20732 for ; Wed, 15 Apr 2020 19:30:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="Qh7bl8eg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436659AbgDOT3a (ORCPT ); Wed, 15 Apr 2020 15:29:30 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:32640 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2411864AbgDOT23 (ORCPT ); Wed, 15 Apr 2020 15:28:29 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03FJSNVB024724 for ; Wed, 15 Apr 2020 12:28:27 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=h6xOUe1xExobaAv34ghOQ+fX0xY5qRBwmBhSlt1y7+k=; b=Qh7bl8egwa4/is6XEP5fK1rldPNjt4kVsEfJmBQRVfT5V4/eFGk3P1arLfSD7MqZRBfd 9b66b4M1byZJtFUwZNoVbgePk555GQolQ4Z9WWdWX0EfwhOjWXUe9KhTv1BUOryjNrHi 1a01UpneHSbm+tlZzOf15kT5c/1CTl5oh7Q= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 30dn7gqkrj-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 15 Apr 2020 12:28:27 -0700 Received: from intmgw001.03.ash8.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Wed, 15 Apr 2020 12:28:06 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id D4E853700AF5; Wed, 15 Apr 2020 12:27:56 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: Andrii Nakryiko , , Martin KaFai Lau , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [RFC PATCH bpf-next v2 14/17] tools/bpftool: add bpf dumper support Date: Wed, 15 Apr 2020 12:27:56 -0700 Message-ID: <20200415192756.4083931-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200415192740.4082659-1-yhs@fb.com> References: <20200415192740.4082659-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-15_07:2020-04-14,2020-04-15 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 bulkscore=0 phishscore=0 adultscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 lowpriorityscore=0 suspectscore=0 clxscore=1015 malwarescore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004150144 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Implemented "bpftool dumper" command. Two subcommands now: bpftool dumper pin bpftool dumper show {target|dumper} The "pin" subcommand will create a dumper with under the dump target (specified in .o). The "show" subcommand will show bpf prog ctx type name , which will be useful on how to write the bpf program, and the "dumper" subcommand will further show the corresponding prog_id for each dumper. For example, with some of later selftest dumpers are pinned in the kernel, we can do inspection like below: $ bpftool dumper show target target prog_ctx_type task bpfdump__task task/file bpfdump__task_file bpf_map bpfdump__bpf_map ipv6_route bpfdump__ipv6_route netlink bpfdump__netlink $ bpftool dumper show dumper dumper prog_id prog_ctx_type task/my1 8 bpfdump__task task/file/my1 12 bpfdump__task_file bpf_map/my1 4 bpfdump__bpf_map ipv6_route/my2 16 bpfdump__ipv6_route netlink/my2 24 bpfdump__ipv6_route netlink/my3 20 bpfdump__netlink Signed-off-by: Yonghong Song --- tools/bpf/bpftool/dumper.c | 135 +++++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/main.c | 3 +- tools/bpf/bpftool/main.h | 1 + 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tools/bpf/bpftool/dumper.c diff --git a/tools/bpf/bpftool/dumper.c b/tools/bpf/bpftool/dumper.c new file mode 100644 index 000000000000..46ca9d1d9a67 --- /dev/null +++ b/tools/bpf/bpftool/dumper.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +// Copyright (C) 2020 Facebook +// Author: Yonghong Song + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "main.h" + +static int do_pin(int argc, char **argv) +{ + struct bpf_program *prog; + struct bpf_object *obj; + const char *objfile, *dname; + int err; + + if (!REQ_ARGS(2)) { + usage(); + return -1; + } + + objfile = GET_ARG(); + dname = GET_ARG(); + + obj = bpf_object__open(objfile); + if (IS_ERR_OR_NULL(obj)) + return -1; + + err = bpf_object__load(obj); + if (err < 0) + return -1; + + prog = bpf_program__next(NULL, obj); + return bpf_dump__pin(prog, dname); +} + +static bool for_targets; +static const char *bpfdump_root = "/sys/kernel/bpfdump"; + +static int check_file(const char *fpath, const struct stat *sb, + int typeflag, struct FTW *ftwbuf) +{ + char prog_ctx_cname_buf[64]; + struct bpf_dump_info info; + unsigned info_len; + const char *name; + int ret, fd; + + if ((for_targets && typeflag == FTW_F) || + (!for_targets && typeflag == FTW_D)) + return 0; + + if (for_targets && strcmp(fpath, bpfdump_root) == 0) + return 0; + + fd = open(fpath, O_RDONLY); + if (fd < 0) + return fd; + + info_len = sizeof(info); + memset(&info, 0, info_len); + info.prog_ctx_type_name = ptr_to_u64(prog_ctx_cname_buf); + info.type_name_buf_len = sizeof(prog_ctx_cname_buf); + ret = bpf_obj_get_info_by_fd(fd, &info, &info_len); + if (ret < 0) + goto done; + + name = fpath + strlen(bpfdump_root) + 1; + if (for_targets) + fprintf(stdout, "%-24s%-24s\n", name, prog_ctx_cname_buf); + else + fprintf(stdout, "%-24s%-10d%-24s\n", name, info.prog_id, + prog_ctx_cname_buf); + +done: + close(fd); + return ret; +} + +static int do_show(int argc, char **argv) +{ + int flags = FTW_PHYS; + int nopenfd = 16; + const char *spec; + + if (!REQ_ARGS(1)) { + usage(); + return -1; + } + + spec = GET_ARG(); + if (strcmp(spec, "target") == 0) { + for_targets = true; + fprintf(stdout, "target prog_ctx_type\n"); + } else if (strcmp(spec, "dumper") == 0) { + fprintf(stdout, "dumper prog_id prog_ctx_type\n"); + for_targets = false; + } else { + return -1; + } + + if (nftw(bpfdump_root, check_file, nopenfd, flags) == -1) + return -1; + + return 0; +} + +static int do_help(int argc, char **argv) +{ + return 0; +} + +static const struct cmd cmds[] = { + { "help", do_help }, + { "show", do_show }, + { "pin", do_pin }, + { 0 } +}; + +int do_dumper(int argc, char **argv) +{ + return cmd_select(cmds, argc, argv, do_help); +} diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 466c269eabdd..8489aba6543d 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -58,7 +58,7 @@ static int do_help(int argc, char **argv) " %s batch file FILE\n" " %s version\n" "\n" - " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n" + " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops | dumper}\n" " " HELP_SPEC_OPTIONS "\n" "", bin_name, bin_name, bin_name); @@ -222,6 +222,7 @@ static const struct cmd cmds[] = { { "btf", do_btf }, { "gen", do_gen }, { "struct_ops", do_struct_ops }, + { "dumper", do_dumper }, { "version", do_version }, { 0 } }; diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 86f14ce26fd7..2c59f319bbe9 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -162,6 +162,7 @@ int do_feature(int argc, char **argv); int do_btf(int argc, char **argv); int do_gen(int argc, char **argv); int do_struct_ops(int argc, char **argv); +int do_dumper(int argc, char **arg); int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what); int prog_parse_fd(int *argc, char ***argv);