diff options
author | Andrii Nakryiko <andriin@fb.com> | 2019-06-17 18:48:58 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2019-06-18 20:21:02 -0400 |
commit | a324aae32fa9bfdd03e89078e20ebcbd7737fda5 (patch) | |
tree | 7f92fea075055be977132438fb6670b5bfab6f04 /tools/lib/bpf/libbpf.c | |
parent | 4d18f6de6ac1d85dc0bc85481fb7cabde09e59ae (diff) |
libbpf: constify getter APIs
Add const qualifiers to bpf_object/bpf_program/bpf_map arguments for
getter APIs. There is no need for them to not be const pointers.
Verified that
make -C tools/lib/bpf
make -C tools/testing/selftests/bpf
make -C tools/perf
all build without warnings.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r-- | tools/lib/bpf/libbpf.c | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 585e3a2f1eb4..8ce3beba8551 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -1663,7 +1663,8 @@ bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) | |||
1663 | } | 1663 | } |
1664 | 1664 | ||
1665 | struct bpf_program * | 1665 | struct bpf_program * |
1666 | bpf_object__find_program_by_title(struct bpf_object *obj, const char *title) | 1666 | bpf_object__find_program_by_title(const struct bpf_object *obj, |
1667 | const char *title) | ||
1667 | { | 1668 | { |
1668 | struct bpf_program *pos; | 1669 | struct bpf_program *pos; |
1669 | 1670 | ||
@@ -2589,8 +2590,8 @@ out: | |||
2589 | return err; | 2590 | return err; |
2590 | } | 2591 | } |
2591 | 2592 | ||
2592 | static bool bpf_program__is_function_storage(struct bpf_program *prog, | 2593 | static bool bpf_program__is_function_storage(const struct bpf_program *prog, |
2593 | struct bpf_object *obj) | 2594 | const struct bpf_object *obj) |
2594 | { | 2595 | { |
2595 | return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls; | 2596 | return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls; |
2596 | } | 2597 | } |
@@ -3296,17 +3297,17 @@ bpf_object__next(struct bpf_object *prev) | |||
3296 | return next; | 3297 | return next; |
3297 | } | 3298 | } |
3298 | 3299 | ||
3299 | const char *bpf_object__name(struct bpf_object *obj) | 3300 | const char *bpf_object__name(const struct bpf_object *obj) |
3300 | { | 3301 | { |
3301 | return obj ? obj->path : ERR_PTR(-EINVAL); | 3302 | return obj ? obj->path : ERR_PTR(-EINVAL); |
3302 | } | 3303 | } |
3303 | 3304 | ||
3304 | unsigned int bpf_object__kversion(struct bpf_object *obj) | 3305 | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
3305 | { | 3306 | { |
3306 | return obj ? obj->kern_version : 0; | 3307 | return obj ? obj->kern_version : 0; |
3307 | } | 3308 | } |
3308 | 3309 | ||
3309 | struct btf *bpf_object__btf(struct bpf_object *obj) | 3310 | struct btf *bpf_object__btf(const struct bpf_object *obj) |
3310 | { | 3311 | { |
3311 | return obj ? obj->btf : NULL; | 3312 | return obj ? obj->btf : NULL; |
3312 | } | 3313 | } |
@@ -3327,13 +3328,14 @@ int bpf_object__set_priv(struct bpf_object *obj, void *priv, | |||
3327 | return 0; | 3328 | return 0; |
3328 | } | 3329 | } |
3329 | 3330 | ||
3330 | void *bpf_object__priv(struct bpf_object *obj) | 3331 | void *bpf_object__priv(const struct bpf_object *obj) |
3331 | { | 3332 | { |
3332 | return obj ? obj->priv : ERR_PTR(-EINVAL); | 3333 | return obj ? obj->priv : ERR_PTR(-EINVAL); |
3333 | } | 3334 | } |
3334 | 3335 | ||
3335 | static struct bpf_program * | 3336 | static struct bpf_program * |
3336 | __bpf_program__iter(struct bpf_program *p, struct bpf_object *obj, bool forward) | 3337 | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
3338 | bool forward) | ||
3337 | { | 3339 | { |
3338 | size_t nr_programs = obj->nr_programs; | 3340 | size_t nr_programs = obj->nr_programs; |
3339 | ssize_t idx; | 3341 | ssize_t idx; |
@@ -3358,7 +3360,7 @@ __bpf_program__iter(struct bpf_program *p, struct bpf_object *obj, bool forward) | |||
3358 | } | 3360 | } |
3359 | 3361 | ||
3360 | struct bpf_program * | 3362 | struct bpf_program * |
3361 | bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) | 3363 | bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj) |
3362 | { | 3364 | { |
3363 | struct bpf_program *prog = prev; | 3365 | struct bpf_program *prog = prev; |
3364 | 3366 | ||
@@ -3370,7 +3372,7 @@ bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) | |||
3370 | } | 3372 | } |
3371 | 3373 | ||
3372 | struct bpf_program * | 3374 | struct bpf_program * |
3373 | bpf_program__prev(struct bpf_program *next, struct bpf_object *obj) | 3375 | bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj) |
3374 | { | 3376 | { |
3375 | struct bpf_program *prog = next; | 3377 | struct bpf_program *prog = next; |
3376 | 3378 | ||
@@ -3392,7 +3394,7 @@ int bpf_program__set_priv(struct bpf_program *prog, void *priv, | |||
3392 | return 0; | 3394 | return 0; |
3393 | } | 3395 | } |
3394 | 3396 | ||
3395 | void *bpf_program__priv(struct bpf_program *prog) | 3397 | void *bpf_program__priv(const struct bpf_program *prog) |
3396 | { | 3398 | { |
3397 | return prog ? prog->priv : ERR_PTR(-EINVAL); | 3399 | return prog ? prog->priv : ERR_PTR(-EINVAL); |
3398 | } | 3400 | } |
@@ -3402,7 +3404,7 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) | |||
3402 | prog->prog_ifindex = ifindex; | 3404 | prog->prog_ifindex = ifindex; |
3403 | } | 3405 | } |
3404 | 3406 | ||
3405 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) | 3407 | const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy) |
3406 | { | 3408 | { |
3407 | const char *title; | 3409 | const char *title; |
3408 | 3410 | ||
@@ -3418,7 +3420,7 @@ const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) | |||
3418 | return title; | 3420 | return title; |
3419 | } | 3421 | } |
3420 | 3422 | ||
3421 | int bpf_program__fd(struct bpf_program *prog) | 3423 | int bpf_program__fd(const struct bpf_program *prog) |
3422 | { | 3424 | { |
3423 | return bpf_program__nth_fd(prog, 0); | 3425 | return bpf_program__nth_fd(prog, 0); |
3424 | } | 3426 | } |
@@ -3451,7 +3453,7 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, | |||
3451 | return 0; | 3453 | return 0; |
3452 | } | 3454 | } |
3453 | 3455 | ||
3454 | int bpf_program__nth_fd(struct bpf_program *prog, int n) | 3456 | int bpf_program__nth_fd(const struct bpf_program *prog, int n) |
3455 | { | 3457 | { |
3456 | int fd; | 3458 | int fd; |
3457 | 3459 | ||
@@ -3479,25 +3481,25 @@ void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) | |||
3479 | prog->type = type; | 3481 | prog->type = type; |
3480 | } | 3482 | } |
3481 | 3483 | ||
3482 | static bool bpf_program__is_type(struct bpf_program *prog, | 3484 | static bool bpf_program__is_type(const struct bpf_program *prog, |
3483 | enum bpf_prog_type type) | 3485 | enum bpf_prog_type type) |
3484 | { | 3486 | { |
3485 | return prog ? (prog->type == type) : false; | 3487 | return prog ? (prog->type == type) : false; |
3486 | } | 3488 | } |
3487 | 3489 | ||
3488 | #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ | 3490 | #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ |
3489 | int bpf_program__set_##NAME(struct bpf_program *prog) \ | 3491 | int bpf_program__set_##NAME(struct bpf_program *prog) \ |
3490 | { \ | 3492 | { \ |
3491 | if (!prog) \ | 3493 | if (!prog) \ |
3492 | return -EINVAL; \ | 3494 | return -EINVAL; \ |
3493 | bpf_program__set_type(prog, TYPE); \ | 3495 | bpf_program__set_type(prog, TYPE); \ |
3494 | return 0; \ | 3496 | return 0; \ |
3495 | } \ | 3497 | } \ |
3496 | \ | 3498 | \ |
3497 | bool bpf_program__is_##NAME(struct bpf_program *prog) \ | 3499 | bool bpf_program__is_##NAME(const struct bpf_program *prog) \ |
3498 | { \ | 3500 | { \ |
3499 | return bpf_program__is_type(prog, TYPE); \ | 3501 | return bpf_program__is_type(prog, TYPE); \ |
3500 | } \ | 3502 | } \ |
3501 | 3503 | ||
3502 | BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); | 3504 | BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); |
3503 | BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); | 3505 | BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); |
@@ -3692,17 +3694,17 @@ bpf_program__identify_section(struct bpf_program *prog, | |||
3692 | expected_attach_type); | 3694 | expected_attach_type); |
3693 | } | 3695 | } |
3694 | 3696 | ||
3695 | int bpf_map__fd(struct bpf_map *map) | 3697 | int bpf_map__fd(const struct bpf_map *map) |
3696 | { | 3698 | { |
3697 | return map ? map->fd : -EINVAL; | 3699 | return map ? map->fd : -EINVAL; |
3698 | } | 3700 | } |
3699 | 3701 | ||
3700 | const struct bpf_map_def *bpf_map__def(struct bpf_map *map) | 3702 | const struct bpf_map_def *bpf_map__def(const struct bpf_map *map) |
3701 | { | 3703 | { |
3702 | return map ? &map->def : ERR_PTR(-EINVAL); | 3704 | return map ? &map->def : ERR_PTR(-EINVAL); |
3703 | } | 3705 | } |
3704 | 3706 | ||
3705 | const char *bpf_map__name(struct bpf_map *map) | 3707 | const char *bpf_map__name(const struct bpf_map *map) |
3706 | { | 3708 | { |
3707 | return map ? map->name : NULL; | 3709 | return map ? map->name : NULL; |
3708 | } | 3710 | } |
@@ -3733,17 +3735,17 @@ int bpf_map__set_priv(struct bpf_map *map, void *priv, | |||
3733 | return 0; | 3735 | return 0; |
3734 | } | 3736 | } |
3735 | 3737 | ||
3736 | void *bpf_map__priv(struct bpf_map *map) | 3738 | void *bpf_map__priv(const struct bpf_map *map) |
3737 | { | 3739 | { |
3738 | return map ? map->priv : ERR_PTR(-EINVAL); | 3740 | return map ? map->priv : ERR_PTR(-EINVAL); |
3739 | } | 3741 | } |
3740 | 3742 | ||
3741 | bool bpf_map__is_offload_neutral(struct bpf_map *map) | 3743 | bool bpf_map__is_offload_neutral(const struct bpf_map *map) |
3742 | { | 3744 | { |
3743 | return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; | 3745 | return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; |
3744 | } | 3746 | } |
3745 | 3747 | ||
3746 | bool bpf_map__is_internal(struct bpf_map *map) | 3748 | bool bpf_map__is_internal(const struct bpf_map *map) |
3747 | { | 3749 | { |
3748 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; | 3750 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
3749 | } | 3751 | } |
@@ -3768,7 +3770,7 @@ int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) | |||
3768 | } | 3770 | } |
3769 | 3771 | ||
3770 | static struct bpf_map * | 3772 | static struct bpf_map * |
3771 | __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i) | 3773 | __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) |
3772 | { | 3774 | { |
3773 | ssize_t idx; | 3775 | ssize_t idx; |
3774 | struct bpf_map *s, *e; | 3776 | struct bpf_map *s, *e; |
@@ -3792,7 +3794,7 @@ __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i) | |||
3792 | } | 3794 | } |
3793 | 3795 | ||
3794 | struct bpf_map * | 3796 | struct bpf_map * |
3795 | bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) | 3797 | bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj) |
3796 | { | 3798 | { |
3797 | if (prev == NULL) | 3799 | if (prev == NULL) |
3798 | return obj->maps; | 3800 | return obj->maps; |
@@ -3801,7 +3803,7 @@ bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) | |||
3801 | } | 3803 | } |
3802 | 3804 | ||
3803 | struct bpf_map * | 3805 | struct bpf_map * |
3804 | bpf_map__prev(struct bpf_map *next, struct bpf_object *obj) | 3806 | bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj) |
3805 | { | 3807 | { |
3806 | if (next == NULL) { | 3808 | if (next == NULL) { |
3807 | if (!obj->nr_maps) | 3809 | if (!obj->nr_maps) |
@@ -3813,7 +3815,7 @@ bpf_map__prev(struct bpf_map *next, struct bpf_object *obj) | |||
3813 | } | 3815 | } |
3814 | 3816 | ||
3815 | struct bpf_map * | 3817 | struct bpf_map * |
3816 | bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) | 3818 | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name) |
3817 | { | 3819 | { |
3818 | struct bpf_map *pos; | 3820 | struct bpf_map *pos; |
3819 | 3821 | ||
@@ -3825,7 +3827,7 @@ bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) | |||
3825 | } | 3827 | } |
3826 | 3828 | ||
3827 | int | 3829 | int |
3828 | bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name) | 3830 | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) |
3829 | { | 3831 | { |
3830 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); | 3832 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
3831 | } | 3833 | } |