linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpftool: Support use full prog name in prog subcommand
@ 2022-11-01 14:38 Tao Chen
  2022-11-01 14:38 ` Tao Chen
  2022-11-01 16:02 ` Daniel Borkmann
  0 siblings, 2 replies; 4+ messages in thread
From: Tao Chen @ 2022-11-01 14:38 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev
  Cc: bpf, linux-kernel, Tao Chen

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 | 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);
-- 
2.2.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] bpftool: Support use full prog name in prog subcommand
  2022-11-01 14:38 [PATCH] bpftool: Support use full prog name in prog subcommand Tao Chen
@ 2022-11-01 14:38 ` Tao Chen
  2022-11-01 16:02 ` Daniel Borkmann
  1 sibling, 0 replies; 4+ messages in thread
From: Tao Chen @ 2022-11-01 14:38 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev
  Cc: bpf, linux-kernel, Tao Chen

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 | 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);
-- 
2.2.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpftool: Support use full prog name in prog subcommand
  2022-11-01 14:38 [PATCH] bpftool: Support use full prog name in prog subcommand Tao Chen
  2022-11-01 14:38 ` Tao Chen
@ 2022-11-01 16:02 ` Daniel Borkmann
  2022-11-02  2:16   ` Tao Chen
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Borkmann @ 2022-11-01 16:02 UTC (permalink / raw)
  To: Tao Chen, Quentin Monnet, Alexei Starovoitov, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev
  Cc: bpf, linux-kernel

On 11/1/22 3:38 PM, Tao Chen wrote:
> 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>

Please rebase against bpf-next, this does not apply cleanly.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpftool: Support use full prog name in prog subcommand
  2022-11-01 16:02 ` Daniel Borkmann
@ 2022-11-02  2:16   ` Tao Chen
  0 siblings, 0 replies; 4+ messages in thread
From: Tao Chen @ 2022-11-02  2:16 UTC (permalink / raw)
  To: Daniel Borkmann, Quentin Monnet, Alexei Starovoitov,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev
  Cc: bpf, linux-kernel

在 2022/11/2 上午12:02, Daniel Borkmann 写道:
> On 11/1/22 3:38 PM, Tao Chen wrote:
>> 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>
> 
> Please rebase against bpf-next, this does not apply cleanly.
Ok, i will resend it, thank you!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-02  2:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-01 14:38 [PATCH] bpftool: Support use full prog name in prog subcommand Tao Chen
2022-11-01 14:38 ` Tao Chen
2022-11-01 16:02 ` Daniel Borkmann
2022-11-02  2:16   ` Tao Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).