From: sidchintamaneni <sidchintamaneni@xxxxxx> Introduces bpf_prog_termination API in libbpf to trigger termination signal from userspace. Adds do_terminate functionality to bpftool to use cmd line interface. cmd - bpftool prog terminate id [] cpu [] Will split this commit to two (bpftool/ libbpf) while sending the patches Signed-off-by: Raj <rjsu26@xxxxxxxxx> Signed-off-by: Siddharth <sidchintamaneni@xxxxxxxxx> --- include/uapi/linux/bpf.h | 5 +++++ tools/bpf/bpftool/prog.c | 40 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ tools/lib/bpf/bpf.c | 15 +++++++++++++ tools/lib/bpf/bpf.h | 10 +++++++++ tools/lib/bpf/libbpf.map | 1 + 6 files changed, 76 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 71d5ac83cf5d..9b9061b9b8e1 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -961,6 +961,7 @@ enum bpf_cmd { BPF_LINK_DETACH, BPF_PROG_BIND_MAP, BPF_TOKEN_CREATE, + BPF_PROG_TERMINATE, __MAX_BPF_CMD, }; @@ -1842,6 +1843,10 @@ union bpf_attr { __u32 bpffs_fd; } token_create; + struct { /* struct used by BPF_PROG_TERMINATE command */ + __u32 prog_id; + __u32 term_cpu_id; + } prog_terminate; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index f010295350be..77bf3fa10d46 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -1968,6 +1968,44 @@ static int do_loadall(int argc, char **argv) return load_with_options(argc, argv, false); } +static int do_terminate(int argc, char **argv) +{ + int prog_id, cpu_id; + + if (!REQ_ARGS(4)) + return BAD_ARG(); + + if (!is_prefix(*argv, "id")) { + p_err("expected 'id', got: %s", *argv); + return -1; + } + NEXT_ARG(); + + prog_id = atoi(argv[0]); + if (prog_id <= 0) { + p_err("Invalid prog_id: %d\n", prog_id); + return -1; + } + NEXT_ARG(); + + if (!is_prefix(*argv, "cpu")) { + p_err("expected 'cpu', got: %s", *argv); + return -1; + } + NEXT_ARG(); + + cpu_id = atoi(argv[0]); + if (cpu_id < 0) { + p_err("Invalid cpu_id: %d\n", cpu_id); + return -1; + } + + bpf_prog_terminate(prog_id, cpu_id); + + return 0; + +} + #ifdef BPFTOOL_WITHOUT_SKELETONS static int do_profile(int argc, char **argv) @@ -2466,6 +2504,7 @@ static int do_help(int argc, char **argv) fprintf(stderr, "Usage: %1$s %2$s { show | list } [PROG]\n" + " %1$s %2$s terminate PROG CPU\n" " %1$s %2$s dump xlated PROG [{ file FILE | [opcodes] [linum] [visual] }]\n" " %1$s %2$s dump jited PROG [{ file FILE | [opcodes] [linum] }]\n" " %1$s %2$s pin PROG FILE\n" @@ -2525,6 +2564,7 @@ static const struct cmd cmds[] = { { "tracelog", do_tracelog }, { "run", do_run }, { "profile", do_profile }, + { "terminate", do_terminate }, { 0 } }; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 71d5ac83cf5d..9b9061b9b8e1 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -961,6 +961,7 @@ enum bpf_cmd { BPF_LINK_DETACH, BPF_PROG_BIND_MAP, BPF_TOKEN_CREATE, + BPF_PROG_TERMINATE, __MAX_BPF_CMD, }; @@ -1842,6 +1843,10 @@ union bpf_attr { __u32 bpffs_fd; } token_create; + struct { /* struct used by BPF_PROG_TERMINATE command */ + __u32 prog_id; + __u32 term_cpu_id; + } prog_terminate; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index a9c3e33d0f8a..0b9dc9b16e02 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -1331,3 +1331,18 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz); return libbpf_err_errno(fd); } + +int bpf_prog_terminate(int prog_id, int cpu_id) +{ + int fd; + union bpf_attr attr; + const size_t attr_sz = offsetofend(union bpf_attr, prog_terminate); + + memset(&attr, 0, sizeof(attr)); + attr.prog_terminate.prog_id = prog_id; + attr.prog_terminate.term_cpu_id = cpu_id; + + fd = sys_bpf(BPF_PROG_TERMINATE, &attr, attr_sz); + + return libbpf_err_errno(fd); +} diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 777627d33d25..6d09d17467b7 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -704,6 +704,16 @@ struct bpf_token_create_opts { LIBBPF_API int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts); + +/** + * @brief **bpf_prog_terminate()** when provided with prog id and cpu id + * of the running prog, it terminated the running BPF program. + * + * @param BPF program file descriptor + * @cpu_id cpu id of the running program + */ +LIBBPF_API int bpf_prog_terminate(int prog_id, int cpu_id); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 1205f9a4fe04..80793f215464 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -443,4 +443,5 @@ LIBBPF_1.6.0 { bpf_program__line_info_cnt; btf__add_decl_attr; btf__add_type_attr; + bpf_prog_terminate; } LIBBPF_1.5.0; -- 2.43.0