From: Tao Chen <chentao.kernel@linux.alibaba.com>
To: Quentin Monnet <quentin@isovalent.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
Tao Chen <chentao.kernel@linux.alibaba.com>
Subject: [RESEND PATCH] bpftool: Support use full prog name in prog subcommand
Date: Wed, 2 Nov 2022 10:35:49 +0800 [thread overview]
Message-ID: <2851b8859666a02878bc897d6c2fb51c80cadce8.1667356049.git.chentao.kernel@linux.alibaba.com> (raw)
Now that the commit: <b662000aff84> ("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 <chentao.kernel@linux.alibaba.com>
---
tools/bpf/bpftool/common.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 40 insertions(+), 5 deletions(-)
diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index 8727765..5d61f26 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_name(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)
{
char prog_name[MAX_PROG_FULL_NAME];
@@ -730,6 +764,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);
@@ -748,6 +783,10 @@ 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",
@@ -755,7 +794,7 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
goto err_close_fd;
}
- if (tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) {
+ if (is_invalid_name(nametag, &info, &finfo, tag)) {
close(fd);
continue;
}
@@ -829,10 +868,6 @@ int prog_parse_fds(int *argc, char ***argv, int **fds)
NEXT_ARGP();
name = **argv;
- if (strlen(name) > MAX_PROG_FULL_NAME - 1) {
- p_err("can't parse name");
- return -1;
- }
NEXT_ARGP();
return prog_fd_by_nametag(name, fds, false);
--
2.2.1
next reply other threads:[~2022-11-02 2:36 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-02 2:35 Tao Chen [this message]
2022-11-02 11:56 ` Quentin Monnet
2022-11-02 12:55 ` Tao Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2851b8859666a02878bc897d6c2fb51c80cadce8.1667356049.git.chentao.kernel@linux.alibaba.com \
--to=chentao.kernel@linux.alibaba.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=quentin@isovalent.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®