Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/bpf: sync kernel header bpf.h and add bpf_task_fd_query in libbpf

Sync kernel header bpf.h to tools/include/uapi/linux/bpf.h and
implement bpf_task_fd_query() in libbpf. The test programs
in samples/bpf and tools/testing/selftests/bpf, and later bpftool
will use this libbpf function to query kernel.

Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Yonghong Song and committed by
Alexei Starovoitov
30687ad9 41bdc4b4

+52
+26
tools/include/uapi/linux/bpf.h
··· 97 97 BPF_RAW_TRACEPOINT_OPEN, 98 98 BPF_BTF_LOAD, 99 99 BPF_BTF_GET_FD_BY_ID, 100 + BPF_TASK_FD_QUERY, 100 101 }; 101 102 102 103 enum bpf_map_type { ··· 381 380 __u32 btf_log_size; 382 381 __u32 btf_log_level; 383 382 }; 383 + 384 + struct { 385 + __u32 pid; /* input: pid */ 386 + __u32 fd; /* input: fd */ 387 + __u32 flags; /* input: flags */ 388 + __u32 buf_len; /* input/output: buf len */ 389 + __aligned_u64 buf; /* input/output: 390 + * tp_name for tracepoint 391 + * symbol for kprobe 392 + * filename for uprobe 393 + */ 394 + __u32 prog_id; /* output: prod_id */ 395 + __u32 fd_type; /* output: BPF_FD_TYPE_* */ 396 + __u64 probe_offset; /* output: probe_offset */ 397 + __u64 probe_addr; /* output: probe_addr */ 398 + } task_fd_query; 384 399 } __attribute__((aligned(8))); 385 400 386 401 /* The description below is an attempt at providing documentation to eBPF ··· 2572 2555 __be16 h_vlan_TCI; 2573 2556 __u8 smac[6]; /* ETH_ALEN */ 2574 2557 __u8 dmac[6]; /* ETH_ALEN */ 2558 + }; 2559 + 2560 + enum bpf_task_fd_type { 2561 + BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ 2562 + BPF_FD_TYPE_TRACEPOINT, /* tp name */ 2563 + BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ 2564 + BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ 2565 + BPF_FD_TYPE_UPROBE, /* filename + offset */ 2566 + BPF_FD_TYPE_URETPROBE, /* filename + offset */ 2575 2567 }; 2576 2568 2577 2569 #endif /* _UAPI__LINUX_BPF_H__ */
+23
tools/lib/bpf/bpf.c
··· 643 643 644 644 return fd; 645 645 } 646 + 647 + int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, 648 + __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, 649 + __u64 *probe_addr) 650 + { 651 + union bpf_attr attr = {}; 652 + int err; 653 + 654 + attr.task_fd_query.pid = pid; 655 + attr.task_fd_query.fd = fd; 656 + attr.task_fd_query.flags = flags; 657 + attr.task_fd_query.buf = ptr_to_u64(buf); 658 + attr.task_fd_query.buf_len = *buf_len; 659 + 660 + err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr)); 661 + *buf_len = attr.task_fd_query.buf_len; 662 + *prog_id = attr.task_fd_query.prog_id; 663 + *fd_type = attr.task_fd_query.fd_type; 664 + *probe_offset = attr.task_fd_query.probe_offset; 665 + *probe_addr = attr.task_fd_query.probe_addr; 666 + 667 + return err; 668 + }
+3
tools/lib/bpf/bpf.h
··· 107 107 int bpf_raw_tracepoint_open(const char *name, int prog_fd); 108 108 int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, 109 109 bool do_log); 110 + int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, 111 + __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, 112 + __u64 *probe_addr); 110 113 #endif