[DTrace-devel] [PATCH v2] Add dt_bpf_map_lookup() function to retrieve values from BPF maps

Eugene Loh eugene.loh at oracle.com
Wed Sep 16 13:16:43 PDT 2020


Reviewed-by: Eugene Loh <eugene.loh at oracle.com>

On 09/14/2020 12:50 PM, Kris Van Hees wrote:
> Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
> ---
>   libdtrace/dt_bpf.c | 47 ++++++++++++++++++++++++++++++----------------
>   libdtrace/dt_bpf.h |  1 +
>   2 files changed, 32 insertions(+), 16 deletions(-)
>
> diff --git a/libdtrace/dt_bpf.c b/libdtrace/dt_bpf.c
> index d6d05dc8..853e3521 100644
> --- a/libdtrace/dt_bpf.c
> +++ b/libdtrace/dt_bpf.c
> @@ -51,6 +51,37 @@ dt_bpf_error(dtrace_hdl_t *dtp, const char *fmt, ...)
>   	return dt_set_errno(dtp, EDT_BPF);
>   }
>   
> +/*
> + * Load the value for the given key in the map referenced by the given fd.
> + */
> +int dt_bpf_map_lookup(int fd, const void *key, void *val)
> +{
> +	union bpf_attr attr;
> +
> +	memset(&attr, 0, sizeof(attr));
> +	attr.map_fd = fd;
> +	attr.key = (uint64_t)(unsigned long)key;
> +	attr.value = (uint64_t)(unsigned long)val;
> +
> +	return bpf(BPF_MAP_LOOKUP_ELEM, &attr);
> +}
> +
> +/*
> + * Store the (key, value) pair in the map referenced by the given fd.
> + */
> +int dt_bpf_map_update(int fd, const void *key, const void *val)
> +{
> +	union bpf_attr attr;
> +
> +	memset(&attr, 0, sizeof(attr));
> +	attr.map_fd = fd;
> +	attr.key = (uint64_t)(unsigned long)key;
> +	attr.value = (uint64_t)(unsigned long)val;
> +	attr.flags = 0;
> +
> +	return bpf(BPF_MAP_UPDATE_ELEM, &attr);
> +}
> +
>   static int
>   create_gmap(dtrace_hdl_t *dtp, const char *name, enum bpf_map_type type,
>   	    int ksz, int vsz, int size)
> @@ -185,22 +216,6 @@ dt_bpf_gmap_create(dtrace_hdl_t *dtp)
>   	return 0;
>   }
>   
> -/*
> - * Store the (key, value) pair in the map referenced by the given fd.
> - */
> -int dt_bpf_map_update(int fd, const void *key, const void *val)
> -{
> -	union bpf_attr attr;
> -
> -	memset(&attr, 0, sizeof(attr));
> -	attr.map_fd = fd;
> -	attr.key = (uint64_t)(unsigned long)key;
> -	attr.value = (uint64_t)(unsigned long)val;
> -	attr.flags = 0;
> -
> -	return bpf(BPF_MAP_UPDATE_ELEM, &attr);
> -}
> -
>   /*
>    * Perform relocation processing on a program.
>    */
> diff --git a/libdtrace/dt_bpf.h b/libdtrace/dt_bpf.h
> index c4a4a99c..e87d4653 100644
> --- a/libdtrace/dt_bpf.h
> +++ b/libdtrace/dt_bpf.h
> @@ -23,6 +23,7 @@ extern int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
>   extern int bpf(enum bpf_cmd cmd, union bpf_attr *attr);
>   
>   extern int dt_bpf_gmap_create(dtrace_hdl_t *);
> +extern int dt_bpf_map_lookup(int fd, const void *key, void *val);
>   extern int dt_bpf_map_update(int fd, const void *key, const void *val);
>   extern int dt_bpf_load_progs(dtrace_hdl_t *, uint_t);
>   




More information about the DTrace-devel mailing list