On Sun, May 17, 2020 at 12:57:23PM -0700, Andrii Nakryiko wrote:
>
> +static enum bpf_ref_type get_release_ref_type(enum bpf_func_id func_id)
> +{
> + switch (func_id) {
> + case BPF_FUNC_sk_release:
> + return BPF_REF_SOCKET;
> + case BPF_FUNC_ringbuf_submit:
> + case BPF_FUNC_ringbuf_discard:
> + return BPF_REF_RINGBUF;
> + default:
> + return BPF_REF_INVALID;
> + }
> +}
> +
> static bool may_be_acquire_function(enum bpf_func_id func_id)
> {
> return func_id == BPF_FUNC_sk_lookup_tcp ||
> @@ -464,6 +477,28 @@ static bool is_acquire_function(enum bpf_func_id func_id,
> return false;
> }
>
> +static enum bpf_ref_type get_acquire_ref_type(enum bpf_func_id func_id,
> + const struct bpf_map *map)
> +{
> + enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC;
> +
> + switch (func_id) {
> + case BPF_FUNC_sk_lookup_tcp:
> + case BPF_FUNC_sk_lookup_udp:
> + case BPF_FUNC_skc_lookup_tcp:
> + return BPF_REF_SOCKET;
> + case BPF_FUNC_map_lookup_elem:
> + if (map_type == BPF_MAP_TYPE_SOCKMAP ||
> + map_type == BPF_MAP_TYPE_SOCKHASH)
> + return BPF_REF_SOCKET;
> + return BPF_REF_INVALID;
> + case BPF_FUNC_ringbuf_reserve:
> + return BPF_REF_RINGBUF;
> + default:
> + return BPF_REF_INVALID;
> + }
> +}
Two switch() stmts to convert helpers to REF is kinda hacky.
I think get_release_ref_type() could have got the ref's type as btf_id from its
arguments which would have made it truly generic and 'enum bpf_ref_type' would
be unnecessary, but sk_lookup_* helpers return u64 and don't have btf_id of
return value. I think we better fix that. Then btf_id would be that ref type.