aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2016-07-13 06:44:01 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-07-13 22:09:02 -0400
commit5f44e4c810bf3ace5a97a84554d4eeccbb563ca5 (patch)
tree84e8aa1aa6450e3a7ecb08633a4798a31d3faaea /tools
parent1a4bf28573c82b4cbeb4e8d3326b24a93ed64c8e (diff)
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: Wang 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.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/lib/bpf/libbpf.c53
-rw-r--r--tools/lib/bpf/libbpf.h9
2 files changed, 55 insertions, 7 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3dcda9e215b0..4751936831d7 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -158,6 +158,7 @@ struct bpf_program {
158 char *section_name; 158 char *section_name;
159 struct bpf_insn *insns; 159 struct bpf_insn *insns;
160 size_t insns_cnt; 160 size_t insns_cnt;
161 enum bpf_prog_type type;
161 162
162 struct { 163 struct {
163 int insn_idx; 164 int insn_idx;
@@ -299,6 +300,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,
299 prog->idx = idx; 300 prog->idx = idx;
300 prog->instances.fds = NULL; 301 prog->instances.fds = NULL;
301 prog->instances.nr = -1; 302 prog->instances.nr = -1;
303 prog->type = BPF_PROG_TYPE_KPROBE;
302 304
303 return 0; 305 return 0;
304errout: 306errout:
@@ -894,8 +896,8 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
894} 896}
895 897
896static int 898static int
897load_program(struct bpf_insn *insns, int insns_cnt, 899load_program(enum bpf_prog_type type, struct bpf_insn *insns,
898 char *license, u32 kern_version, int *pfd) 900 int insns_cnt, char *license, u32 kern_version, int *pfd)
899{ 901{
900 int ret; 902 int ret;
901 char *log_buf; 903 char *log_buf;
@@ -907,9 +909,8 @@ load_program(struct bpf_insn *insns, int insns_cnt,
907 if (!log_buf) 909 if (!log_buf)
908 pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); 910 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
909 911
910 ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns, 912 ret = bpf_load_program(type, insns, insns_cnt, license,
911 insns_cnt, license, kern_version, 913 kern_version, log_buf, BPF_LOG_BUF_SIZE);
912 log_buf, BPF_LOG_BUF_SIZE);
913 914
914 if (ret >= 0) { 915 if (ret >= 0) {
915 *pfd = ret; 916 *pfd = ret;
@@ -968,7 +969,7 @@ bpf_program__load(struct bpf_program *prog,
968 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", 969 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
969 prog->section_name, prog->instances.nr); 970 prog->section_name, prog->instances.nr);
970 } 971 }
971 err = load_program(prog->insns, prog->insns_cnt, 972 err = load_program(prog->type, prog->insns, prog->insns_cnt,
972 license, kern_version, &fd); 973 license, kern_version, &fd);
973 if (!err) 974 if (!err)
974 prog->instances.fds[0] = fd; 975 prog->instances.fds[0] = fd;
@@ -997,7 +998,7 @@ bpf_program__load(struct bpf_program *prog,
997 continue; 998 continue;
998 } 999 }
999 1000
1000 err = load_program(result.new_insn_ptr, 1001 err = load_program(prog->type, result.new_insn_ptr,
1001 result.new_insn_cnt, 1002 result.new_insn_cnt,
1002 license, kern_version, &fd); 1003 license, kern_version, &fd);
1003 1004
@@ -1316,6 +1317,44 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
1316 return fd; 1317 return fd;
1317} 1318}
1318 1319
1320static void bpf_program__set_type(struct bpf_program *prog,
1321 enum bpf_prog_type type)
1322{
1323 prog->type = type;
1324}
1325
1326int bpf_program__set_tracepoint(struct bpf_program *prog)
1327{
1328 if (!prog)
1329 return -EINVAL;
1330 bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
1331 return 0;
1332}
1333
1334int bpf_program__set_kprobe(struct bpf_program *prog)
1335{
1336 if (!prog)
1337 return -EINVAL;
1338 bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
1339 return 0;
1340}
1341
1342static bool bpf_program__is_type(struct bpf_program *prog,
1343 enum bpf_prog_type type)
1344{
1345 return prog ? (prog->type == type) : false;
1346}
1347
1348bool bpf_program__is_tracepoint(struct bpf_program *prog)
1349{
1350 return bpf_program__is_type(prog, BPF_PROG_TYPE_TRACEPOINT);
1351}
1352
1353bool bpf_program__is_kprobe(struct bpf_program *prog)
1354{
1355 return bpf_program__is_type(prog, BPF_PROG_TYPE_KPROBE);
1356}
1357
1319int bpf_map__fd(struct bpf_map *map) 1358int bpf_map__fd(struct bpf_map *map)
1320{ 1359{
1321 return map ? map->fd : -EINVAL; 1360 return map ? map->fd : -EINVAL;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index f392c5e04cc1..eb2a4c45f6b6 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -165,6 +165,15 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
165int bpf_program__nth_fd(struct bpf_program *prog, int n); 165int bpf_program__nth_fd(struct bpf_program *prog, int n);
166 166
167/* 167/*
168 * Adjust type of bpf program. Default is kprobe.
169 */
170int bpf_program__set_tracepoint(struct bpf_program *prog);
171int bpf_program__set_kprobe(struct bpf_program *prog);
172
173bool bpf_program__is_tracepoint(struct bpf_program *prog);
174bool bpf_program__is_kprobe(struct bpf_program *prog);
175
176/*
168 * We don't need __attribute__((packed)) now since it is 177 * We don't need __attribute__((packed)) now since it is
169 * unnecessary for 'bpf_map_def' because they are all aligned. 178 * unnecessary for 'bpf_map_def' because they are all aligned.
170 * In addition, using it will trigger -Wpacked warning message, 179 * In addition, using it will trigger -Wpacked warning message,