From patchwork Tue Nov 1 14:38:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13692 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3011343wru; Tue, 1 Nov 2022 07:39:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7igHoDjAGeTgsuuWtTec0Z6+wer8faKHDq0T839NQvhOivds9a06ZcoY/PB6ZeipwbGT5p X-Received: by 2002:a17:902:ab1c:b0:186:b59e:b9f7 with SMTP id ik28-20020a170902ab1c00b00186b59eb9f7mr140659plb.99.1667313589101; Tue, 01 Nov 2022 07:39:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667313589; cv=none; d=google.com; s=arc-20160816; b=YDkIj5Q08cwJirpHHKRYHXp8Iw7AkE3dJjmrIck6bdna3GRH02mnBZRqPnr3vyjgp8 wgq9L0XBmCA1a07AHrT+9iz3Or0/9xBCNmRdbW+gKNlBpsfamE4DOyDrbHlDx7exEzei URsfuWzeoGNlgIzC3cSQa4vQZ10BBX/aMAfjNs5lhyvLvWMlljIRPrjgrKnrb/c5wPeV 11zvIa7SnJ/ZZBmx9yHfrcsZbMQXC6lj3hvOZGMvzV3+uWmxkSaGMk6Fy1vw0a0S1YIj xn4d9x4LvyA2t7UuAAS6i5rDdUsj6pKNGDGDY0rt98aF9EwXwK1e2KM8BmcAhRizN0Fo M3YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=da786fu286IBLolXw3azxaElPjGdDF4d3maYxKnyops=; b=ELycA8UcQdwFsGA0Ba4rt3BwZDR9SMS0MHRsZJ4teNmB2sIJBWfoJc+DeDMCUQD2Mr W4jc/KCHL2kyO42syQgiC8ZB6qd93KbqwfT+kkTIenggIxwxhpEVAVxxGQDtuIxrVgln MDoeXiggYUwSTZMK+LmkEEhFiMh1WDFYghprVCP5srWs92dDdFieGFoQ9BLOm6u9tUDP Y7VoqQ40jWMOYzbbNUVJ/5mi3YJJphkxhPLLT/LMYnPATH+2c79UucTYj/yB7GlwH56E javRMul4X2JywdgMqp2Q91otcYdyTDNpK7DuYVrOgW9CKqjkDUte/Fvz/DLlvy1+ZosO 10tw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s13-20020a056a00194d00b0056c07a694dasi14277048pfk.158.2022.11.01.07.39.35; Tue, 01 Nov 2022 07:39:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229752AbiKAOjD (ORCPT + 99 others); Tue, 1 Nov 2022 10:39:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229717AbiKAOjA (ORCPT ); Tue, 1 Nov 2022 10:39:00 -0400 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 762B03895; Tue, 1 Nov 2022 07:38:56 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=chentao.kernel@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0VTjllWc_1667313514; Received: from VM20210331-5.tbsite.net(mailfrom:chentao.kernel@linux.alibaba.com fp:SMTPD_---0VTjllWc_1667313514) by smtp.aliyun-inc.com; Tue, 01 Nov 2022 22:38:52 +0800 From: Tao Chen To: Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Chen Subject: [PATCH] bpftool: Support use full prog name in prog subcommand Date: Tue, 1 Nov 2022 22:38:32 +0800 Message-Id: X-Mailer: git-send-email 2.2.1 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748305013677227919?= X-GMAIL-MSGID: =?utf-8?q?1748305013677227919?= Now that the commit: ("bpftool: Adding support for BTF program names") supported show the full prog name, we can also use the full prog name more than 16 (BPF_OBJ_NAME_LEN) chars in prog subcommand, such as "bpftool prog show name PROG_NAME". Signed-off-by: Tao Chen --- tools/bpf/bpftool/common.c | 48 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 067e9ea..5efdedc 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -720,6 +720,40 @@ print_all_levels(__maybe_unused enum libbpf_print_level level, return vfprintf(stderr, format, args); } +static bool is_invalid_prog(char *nametag, struct bpf_prog_info *info, + struct bpf_func_info *finfo, bool tag) +{ + const struct btf *prog_btf; + const struct btf_type *func_type; + const char *name; + + if (tag) + return memcmp(nametag, info->tag, BPF_TAG_SIZE); + + if (strlen(nametag) < BPF_OBJ_NAME_LEN) + return strncmp(nametag, info->name, BPF_OBJ_NAME_LEN); + + prog_btf = btf__load_from_kernel_by_id(info->btf_id); + if (!prog_btf) { + p_err("get prog btf failed, btf_id:%u\n", info->btf_id); + return true; + } + + func_type = btf__type_by_id(prog_btf, finfo->type_id); + if (!func_type || !btf_is_func(func_type)) { + p_err("func type invalid, type_id:%u\n", finfo->type_id); + return true; + } + + name = btf__name_by_offset(prog_btf, func_type->name_off); + if (!name) { + p_err("func name invalid, name_off:%u\n", func_type->name_off); + return true; + } + + return strncmp(nametag, name, strlen(name)); +} + static int prog_fd_by_nametag(void *nametag, int **fds, bool tag) { unsigned int id = 0; @@ -729,6 +763,7 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag) while (true) { struct bpf_prog_info info = {}; + struct bpf_func_info finfo = {}; __u32 len = sizeof(info); err = bpf_prog_get_next_id(id, &id); @@ -747,15 +782,17 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag) goto err_close_fds; } + info.nr_func_info = 1; + info.func_info_rec_size = sizeof(finfo); + info.func_info = ptr_to_u64(&finfo); + err = bpf_obj_get_info_by_fd(fd, &info, &len); if (err) { p_err("can't get prog info (%u): %s", id, strerror(errno)); goto err_close_fd; } - - if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) || - (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) { + if (is_invalid_prog(nametag, &info, &finfo, tag)) { close(fd); continue; } @@ -818,12 +855,7 @@ int prog_parse_fds(int *argc, char ***argv, int **fds) char *name; NEXT_ARGP(); - name = **argv; - if (strlen(name) > BPF_OBJ_NAME_LEN - 1) { - p_err("can't parse name"); - return -1; - } NEXT_ARGP(); return prog_fd_by_nametag(name, fds, false);