aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorAndrii Nakryiko <andriin@fb.com>2019-06-17 18:48:58 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2019-06-18 20:21:02 -0400
commita324aae32fa9bfdd03e89078e20ebcbd7737fda5 (patch)
tree7f92fea075055be977132438fb6670b5bfab6f04 /tools/lib/bpf/libbpf.c
parent4d18f6de6ac1d85dc0bc85481fb7cabde09e59ae (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.c80
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
1665struct bpf_program * 1665struct bpf_program *
1666bpf_object__find_program_by_title(struct bpf_object *obj, const char *title) 1666bpf_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
2592static bool bpf_program__is_function_storage(struct bpf_program *prog, 2593static 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
3299const char *bpf_object__name(struct bpf_object *obj) 3300const 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
3304unsigned int bpf_object__kversion(struct bpf_object *obj) 3305unsigned 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
3309struct btf *bpf_object__btf(struct bpf_object *obj) 3310struct 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
3330void *bpf_object__priv(struct bpf_object *obj) 3331void *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
3335static struct bpf_program * 3336static 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
3360struct bpf_program * 3362struct bpf_program *
3361bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) 3363bpf_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
3372struct bpf_program * 3374struct bpf_program *
3373bpf_program__prev(struct bpf_program *next, struct bpf_object *obj) 3375bpf_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
3395void *bpf_program__priv(struct bpf_program *prog) 3397void *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
3405const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) 3407const 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
3421int bpf_program__fd(struct bpf_program *prog) 3423int 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
3454int bpf_program__nth_fd(struct bpf_program *prog, int n) 3456int 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
3482static bool bpf_program__is_type(struct bpf_program *prog, 3484static 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) \
3489int bpf_program__set_##NAME(struct bpf_program *prog) \ 3491int 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 \
3497bool bpf_program__is_##NAME(struct bpf_program *prog) \ 3499bool 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
3502BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); 3504BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
3503BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); 3505BPF_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
3695int bpf_map__fd(struct bpf_map *map) 3697int 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
3700const struct bpf_map_def *bpf_map__def(struct bpf_map *map) 3702const 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
3705const char *bpf_map__name(struct bpf_map *map) 3707const 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
3736void *bpf_map__priv(struct bpf_map *map) 3738void *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
3741bool bpf_map__is_offload_neutral(struct bpf_map *map) 3743bool 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
3746bool bpf_map__is_internal(struct bpf_map *map) 3748bool 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
3770static struct bpf_map * 3772static 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
3794struct bpf_map * 3796struct bpf_map *
3795bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) 3797bpf_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
3803struct bpf_map * 3805struct bpf_map *
3804bpf_map__prev(struct bpf_map *next, struct bpf_object *obj) 3806bpf_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
3815struct bpf_map * 3817struct bpf_map *
3816bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) 3818bpf_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
3827int 3829int
3828bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name) 3830bpf_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}