[RFC PATCH bpf-next v1 2/3] libbpf: Add support for BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE

Fuyu Zhao posted 3 patches 2 weeks, 1 day ago
[RFC PATCH bpf-next v1 2/3] libbpf: Add support for BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE
Posted by Fuyu Zhao 2 weeks, 1 day ago
Extend libbpf to support the new BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE
program type, making it available to user space applications through
the standard libbpf API.

Signed-off-by: Fuyu Zhao <zhaofuyu@vivo.com>
---
 tools/include/uapi/linux/bpf.h |  2 ++
 tools/lib/bpf/bpf.c            |  1 +
 tools/lib/bpf/bpf.h            |  3 ++-
 tools/lib/bpf/libbpf.c         | 27 ++++++++++++++++++++++++++-
 tools/lib/bpf/libbpf.h         |  3 ++-
 5 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 233de8677382..7438836b3e4b 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1071,6 +1071,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_SK_LOOKUP,
 	BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
 	BPF_PROG_TYPE_NETFILTER,
+	BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE,
 	__MAX_BPF_PROG_TYPE
 };
 
@@ -1707,6 +1708,7 @@ union bpf_attr {
 		__u32		prog_fd;
 		__u32		:32;
 		__aligned_u64	cookie;
+		__aligned_u64   probe_name;
 	} raw_tracepoint;
 
 	struct { /* anonymous struct for BPF_BTF_LOAD */
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index ab40dbf9f020..95b73f94ce72 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1235,6 +1235,7 @@ int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts)
 	attr.raw_tracepoint.prog_fd = prog_fd;
 	attr.raw_tracepoint.name = ptr_to_u64(OPTS_GET(opts, tp_name, NULL));
 	attr.raw_tracepoint.cookie = OPTS_GET(opts, cookie, 0);
+	attr.raw_tracepoint.probe_name = ptr_to_u64(OPTS_GET(opts, probe_name, NULL));
 
 	fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
 	return libbpf_err_errno(fd);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 7252150e7ad3..0ebedbd99fe5 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -630,9 +630,10 @@ struct bpf_raw_tp_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
 	const char *tp_name;
 	__u64 cookie;
+	const char *probe_name;
 	size_t :0;
 };
-#define bpf_raw_tp_opts__last_field cookie
+#define bpf_raw_tp_opts__last_field probe_name
 
 LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts);
 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fe4fc5438678..ce67c917ba59 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9557,6 +9557,8 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("raw_tp+",		RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("raw_tracepoint.w+",	RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("raw_tp.w+",		RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
+	SEC_DEF("raw_tracepoint.o+",	RAW_TRACEPOINT_OVERRIDE, 0, SEC_NONE, attach_raw_tp),
+	SEC_DEF("raw_tp.o+",		RAW_TRACEPOINT_OVERRIDE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("tp_btf+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fentry+",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fmod_ret+",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
@@ -12684,6 +12686,7 @@ bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
 
 	raw_opts.tp_name = tp_name;
 	raw_opts.cookie = OPTS_GET(opts, cookie, 0);
+	raw_opts.probe_name = OPTS_GET(opts, probe_name, NULL);
 	pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts);
 	if (pfd < 0) {
 		pfd = -errno;
@@ -12704,14 +12707,18 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *pr
 
 static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
 {
+	LIBBPF_OPTS(bpf_raw_tracepoint_opts, raw_opts);
 	static const char *const prefixes[] = {
 		"raw_tp",
 		"raw_tracepoint",
 		"raw_tp.w",
 		"raw_tracepoint.w",
+		"raw_tp.o",
+		"raw_tracepoint.o",
 	};
 	size_t i;
 	const char *tp_name = NULL;
+	char *dup = NULL, *sep = NULL;
 
 	*link = NULL;
 
@@ -12739,7 +12746,25 @@ static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf
 		return -EINVAL;
 	}
 
-	*link = bpf_program__attach_raw_tracepoint(prog, tp_name);
+	if (prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE) {
+		dup = strdup(tp_name);
+		if (!dup)
+			return -ENOMEM;
+
+		sep = strchr(dup, ':');
+		if (!sep) {
+			free(dup);
+			return -EINVAL;
+		}
+		*sep = '\0';
+
+		tp_name = dup;
+		raw_opts.probe_name = sep + 1,
+		*link = bpf_program__attach_raw_tracepoint_opts(prog, tp_name, &raw_opts);
+		free(dup);
+	} else {
+		*link = bpf_program__attach_raw_tracepoint(prog, tp_name);
+	}
 	return libbpf_get_error(*link);
 }
 
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 2e91148d9b44..f4e9cb819b75 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -820,9 +820,10 @@ bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
 struct bpf_raw_tracepoint_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
 	__u64 cookie;
+	const char *probe_name;
 	size_t :0;
 };
-#define bpf_raw_tracepoint_opts__last_field cookie
+#define bpf_raw_tracepoint_opts__last_field probe_name
 
 LIBBPF_API struct bpf_link *
 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
-- 
2.43.0