Skip to content

Commit 5f44e4c

Browse files
WangNan0acmel
authored andcommitted
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 <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Zefan Li <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 1a4bf28 commit 5f44e4c

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ struct bpf_program {
158158
char *section_name;
159159
struct bpf_insn *insns;
160160
size_t insns_cnt;
161+
enum bpf_prog_type type;
161162

162163
struct {
163164
int insn_idx;
@@ -299,6 +300,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx,
299300
prog->idx = idx;
300301
prog->instances.fds = NULL;
301302
prog->instances.nr = -1;
303+
prog->type = BPF_PROG_TYPE_KPROBE;
302304

303305
return 0;
304306
errout:
@@ -894,8 +896,8 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
894896
}
895897

896898
static int
897-
load_program(struct bpf_insn *insns, int insns_cnt,
898-
char *license, u32 kern_version, int *pfd)
899+
load_program(enum bpf_prog_type type, struct bpf_insn *insns,
900+
int insns_cnt, char *license, u32 kern_version, int *pfd)
899901
{
900902
int ret;
901903
char *log_buf;
@@ -907,9 +909,8 @@ load_program(struct bpf_insn *insns, int insns_cnt,
907909
if (!log_buf)
908910
pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
909911

910-
ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
911-
insns_cnt, license, kern_version,
912-
log_buf, BPF_LOG_BUF_SIZE);
912+
ret = bpf_load_program(type, insns, insns_cnt, license,
913+
kern_version, log_buf, BPF_LOG_BUF_SIZE);
913914

914915
if (ret >= 0) {
915916
*pfd = ret;
@@ -968,7 +969,7 @@ bpf_program__load(struct bpf_program *prog,
968969
pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
969970
prog->section_name, prog->instances.nr);
970971
}
971-
err = load_program(prog->insns, prog->insns_cnt,
972+
err = load_program(prog->type, prog->insns, prog->insns_cnt,
972973
license, kern_version, &fd);
973974
if (!err)
974975
prog->instances.fds[0] = fd;
@@ -997,7 +998,7 @@ bpf_program__load(struct bpf_program *prog,
997998
continue;
998999
}
9991000

1000-
err = load_program(result.new_insn_ptr,
1001+
err = load_program(prog->type, result.new_insn_ptr,
10011002
result.new_insn_cnt,
10021003
license, kern_version, &fd);
10031004

@@ -1316,6 +1317,44 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
13161317
return fd;
13171318
}
13181319

1320+
static void bpf_program__set_type(struct bpf_program *prog,
1321+
enum bpf_prog_type type)
1322+
{
1323+
prog->type = type;
1324+
}
1325+
1326+
int 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+
1334+
int 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+
1342+
static 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+
1348+
bool bpf_program__is_tracepoint(struct bpf_program *prog)
1349+
{
1350+
return bpf_program__is_type(prog, BPF_PROG_TYPE_TRACEPOINT);
1351+
}
1352+
1353+
bool bpf_program__is_kprobe(struct bpf_program *prog)
1354+
{
1355+
return bpf_program__is_type(prog, BPF_PROG_TYPE_KPROBE);
1356+
}
1357+
13191358
int bpf_map__fd(struct bpf_map *map)
13201359
{
13211360
return map ? map->fd : -EINVAL;

tools/lib/bpf/libbpf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
164164

165165
int bpf_program__nth_fd(struct bpf_program *prog, int n);
166166

167+
/*
168+
* Adjust type of bpf program. Default is kprobe.
169+
*/
170+
int bpf_program__set_tracepoint(struct bpf_program *prog);
171+
int bpf_program__set_kprobe(struct bpf_program *prog);
172+
173+
bool bpf_program__is_tracepoint(struct bpf_program *prog);
174+
bool bpf_program__is_kprobe(struct bpf_program *prog);
175+
167176
/*
168177
* We don't need __attribute__((packed)) now since it is
169178
* unnecessary for 'bpf_map_def' because they are all aligned.

0 commit comments

Comments
 (0)