Commit 5f44e4c8 authored by Wang Nan's avatar Wang Nan Committed by Arnaldo Carvalho de Melo

tools lib bpf: New API to adjust type of a BPF program

Add 4 new APIs to adjust and query the type of a BPF program.
Load program according to type set by caller. Default is set to
BPF_PROG_TYPE_KPROBE.
Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1468406646-21642-2-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 1a4bf285
...@@ -158,6 +158,7 @@ struct bpf_program { ...@@ -158,6 +158,7 @@ struct bpf_program {
char *section_name; char *section_name;
struct bpf_insn *insns; struct bpf_insn *insns;
size_t insns_cnt; size_t insns_cnt;
enum bpf_prog_type type;
struct { struct {
int insn_idx; int insn_idx;
...@@ -299,6 +300,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx, ...@@ -299,6 +300,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,
prog->idx = idx; prog->idx = idx;
prog->instances.fds = NULL; prog->instances.fds = NULL;
prog->instances.nr = -1; prog->instances.nr = -1;
prog->type = BPF_PROG_TYPE_KPROBE;
return 0; return 0;
errout: errout:
...@@ -894,8 +896,8 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) ...@@ -894,8 +896,8 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
} }
static int static int
load_program(struct bpf_insn *insns, int insns_cnt, load_program(enum bpf_prog_type type, struct bpf_insn *insns,
char *license, u32 kern_version, int *pfd) int insns_cnt, char *license, u32 kern_version, int *pfd)
{ {
int ret; int ret;
char *log_buf; char *log_buf;
...@@ -907,9 +909,8 @@ load_program(struct bpf_insn *insns, int insns_cnt, ...@@ -907,9 +909,8 @@ load_program(struct bpf_insn *insns, int insns_cnt,
if (!log_buf) if (!log_buf)
pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns, ret = bpf_load_program(type, insns, insns_cnt, license,
insns_cnt, license, kern_version, kern_version, log_buf, BPF_LOG_BUF_SIZE);
log_buf, BPF_LOG_BUF_SIZE);
if (ret >= 0) { if (ret >= 0) {
*pfd = ret; *pfd = ret;
...@@ -968,7 +969,7 @@ bpf_program__load(struct bpf_program *prog, ...@@ -968,7 +969,7 @@ bpf_program__load(struct bpf_program *prog,
pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
prog->section_name, prog->instances.nr); prog->section_name, prog->instances.nr);
} }
err = load_program(prog->insns, prog->insns_cnt, err = load_program(prog->type, prog->insns, prog->insns_cnt,
license, kern_version, &fd); license, kern_version, &fd);
if (!err) if (!err)
prog->instances.fds[0] = fd; prog->instances.fds[0] = fd;
...@@ -997,7 +998,7 @@ bpf_program__load(struct bpf_program *prog, ...@@ -997,7 +998,7 @@ bpf_program__load(struct bpf_program *prog,
continue; continue;
} }
err = load_program(result.new_insn_ptr, err = load_program(prog->type, result.new_insn_ptr,
result.new_insn_cnt, result.new_insn_cnt,
license, kern_version, &fd); license, kern_version, &fd);
...@@ -1316,6 +1317,44 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) ...@@ -1316,6 +1317,44 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
return fd; return fd;
} }
static void bpf_program__set_type(struct bpf_program *prog,
enum bpf_prog_type type)
{
prog->type = type;
}
int bpf_program__set_tracepoint(struct bpf_program *prog)
{
if (!prog)
return -EINVAL;
bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
return 0;
}
int bpf_program__set_kprobe(struct bpf_program *prog)
{
if (!prog)
return -EINVAL;
bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
return 0;
}
static bool bpf_program__is_type(struct bpf_program *prog,
enum bpf_prog_type type)
{
return prog ? (prog->type == type) : false;
}
bool bpf_program__is_tracepoint(struct bpf_program *prog)
{
return bpf_program__is_type(prog, BPF_PROG_TYPE_TRACEPOINT);
}
bool bpf_program__is_kprobe(struct bpf_program *prog)
{
return bpf_program__is_type(prog, BPF_PROG_TYPE_KPROBE);
}
int bpf_map__fd(struct bpf_map *map) int bpf_map__fd(struct bpf_map *map)
{ {
return map ? map->fd : -EINVAL; return map ? map->fd : -EINVAL;
......
...@@ -164,6 +164,15 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, ...@@ -164,6 +164,15 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
int bpf_program__nth_fd(struct bpf_program *prog, int n); int bpf_program__nth_fd(struct bpf_program *prog, int n);
/*
* Adjust type of bpf program. Default is kprobe.
*/
int bpf_program__set_tracepoint(struct bpf_program *prog);
int bpf_program__set_kprobe(struct bpf_program *prog);
bool bpf_program__is_tracepoint(struct bpf_program *prog);
bool bpf_program__is_kprobe(struct bpf_program *prog);
/* /*
* We don't need __attribute__((packed)) now since it is * We don't need __attribute__((packed)) now since it is
* unnecessary for 'bpf_map_def' because they are all aligned. * unnecessary for 'bpf_map_def' because they are all aligned.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment