[PATCH bpf-next v2] bpftool: implement perf attach command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch introduces a new bpftool command: perf attach,
which used to attaching/pinning tracepoints programs.

  bpftool perf attach PROG TP_NAME FILE

It will attach bpf program PROG to tracepoint TP_NAME and
pin tracepoint program as FILE, FILE must be located in
bpffs mount.

For example,
  $ bpftool prog load mtd-mchp23k256.o /sys/fs/bpf/test_prog

  $ bpftool prog
  510: raw_tracepoint_writable  name mtd_mchp23k256  tag 2e13281b1f781bf3  gpl
        loaded_at 2022-08-24T02:50:06+0000  uid 0
        xlated 960B  not jited  memlock 4096B  map_ids 439,437,440
        btf_id 740
  $ bpftool perf attach id 510 spi_transfer_writeable /sys/fs/bpf/test_perf

  $ bpftool link show
  74: raw_tracepoint  prog 510
        tp 'spi_transfer_writeable'

The implementation a BPF based backend for mchp23k256 mtd mockup
device.

Signed-off-by: Wei Yongjun <weiyongjun1@xxxxxxxxxx>
---
v1 -> v2: switch to extend perf command instead add new one
v1: https://patchwork.kernel.org/project/netdevbpf/patch/20220816151725.153343-1-weiyongjun1@xxxxxxxxxx/
---
 .../bpftool/Documentation/bpftool-perf.rst    | 11 ++-
 tools/bpf/bpftool/perf.c                      | 67 ++++++++++++++++++-
 2 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-perf.rst b/tools/bpf/bpftool/Documentation/bpftool-perf.rst
index 5fea633a82f1..085c8dcfb9aa 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-perf.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-perf.rst
@@ -19,12 +19,13 @@ SYNOPSIS
 	*OPTIONS* := { |COMMON_OPTIONS| }
 
 	*COMMANDS* :=
-	{ **show** | **list** | **help** }
+	{ **show** | **list** | **help** | **attach** }
 
 PERF COMMANDS
 =============
 
 |	**bpftool** **perf** { **show** | **list** }
+|	**bpftool** **perf** **attach** *PROG* *TP_NAME* *FILE*
 |	**bpftool** **perf help**
 
 DESCRIPTION
@@ -39,6 +40,14 @@ DESCRIPTION
 		  or a kernel virtual address.
 		  The attachment point for u[ret]probe is the file name and the file offset.
 
+	**bpftool perf attach PROG TP_NAME FILE**
+		  Attach bpf program *PROG* to tracepoint *TP_NAME* and pin
+		  program *PROG* as *FILE*.
+
+		  Note: *FILE* must be located in *bpffs* mount. It must not
+		  contain a dot character ('.'), which is reserved for future
+		  extensions of *bpffs*.
+
 	**bpftool perf help**
 		  Print short help message.
 
diff --git a/tools/bpf/bpftool/perf.c b/tools/bpf/bpftool/perf.c
index 226ec2c39052..9149ba960784 100644
--- a/tools/bpf/bpftool/perf.c
+++ b/tools/bpf/bpftool/perf.c
@@ -233,8 +233,9 @@ static int do_show(int argc, char **argv)
 static int do_help(int argc, char **argv)
 {
 	fprintf(stderr,
-		"Usage: %1$s %2$s { show | list }\n"
+		"Usage: %1$s %2$s { show | list | attach }\n"
 		"       %1$s %2$s help }\n"
+		"       %1$s %2$s attach PROG TP_NAME FILE\n"
 		"\n"
 		"       " HELP_SPEC_OPTIONS " }\n"
 		"",
@@ -243,10 +244,74 @@ static int do_help(int argc, char **argv)
 	return 0;
 }
 
+static enum bpf_prog_type get_prog_type(int progfd)
+{
+	struct bpf_prog_info info = {};
+	__u32 len = sizeof(info);
+
+	if (bpf_obj_get_info_by_fd(progfd, &info, &len))
+		return BPF_PROG_TYPE_UNSPEC;
+
+	return info.type;
+}
+
+static int do_attach(int argc, char **argv)
+{
+	enum bpf_prog_type prog_type;
+	char *tp_name, *path;
+	int err, progfd, pfd;
+
+	if (!REQ_ARGS(4))
+		return -EINVAL;
+
+	progfd = prog_parse_fd(&argc, &argv);
+	if (progfd < 0)
+		return progfd;
+
+	if (!REQ_ARGS(2)) {
+		err = -EINVAL;
+		goto out_close;
+	}
+
+	tp_name = GET_ARG();
+	path = GET_ARG();
+
+	prog_type = get_prog_type(progfd);
+	switch (prog_type) {
+	case BPF_PROG_TYPE_RAW_TRACEPOINT:
+	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
+		pfd = bpf_raw_tracepoint_open(tp_name, progfd);
+		if (pfd < 0) {
+			printf("failed to attach to raw tracepoint '%s'\n",
+			       tp_name);
+			err = pfd;
+			goto out_close;
+		}
+		break;
+	default:
+		printf("invalid program type %s\n",
+		       libbpf_bpf_prog_type_str(prog_type));
+		err = -EINVAL;
+		goto out_close;
+	}
+
+	err = do_pin_fd(pfd, path);
+	if (err) {
+		close(pfd);
+		goto out_close;
+	}
+
+	return 0;
+
+out_close:
+	return err;
+}
+
 static const struct cmd cmds[] = {
 	{ "show",	do_show },
 	{ "list",	do_show },
 	{ "help",	do_help },
+	{ "attach",	do_attach },
 	{ 0 }
 };
 
-- 
2.34.1




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux