Subject: Re: [PATCH v2 bpf-next 2/3] libbpf: add bpf_object__open_{file,mem} w/ extensible opts

From: Toke Høiland-Jørgensen <[email-masked]>
Date: Fri, 04 Oct 2019 08:38:09 +0200

Andrii Nakryiko <[email-masked]> writes:

> Add new set of bpf_object__open APIs using new approach to optional
> parameters extensibility allowing simpler ABI compatibility approach.
>
> This patch demonstrates an approach to implementing libbpf APIs that
> makes it easy to extend existing APIs with extra optional parameters in
> such a way, that ABI compatibility is preserved without having to do
> symbol versioning and generating lots of boilerplate code to handle it.
> To facilitate succinct code for working with options, add OPTS_VALID,
> OPTS_HAS, and OPTS_GET macros that hide all the NULL, size, and zero
> checks.
>
> Additionally, newly added libbpf APIs are encouraged to follow similar
> pattern of having all mandatory parameters as formal function parameters
> and always have optional (NULL-able) xxx_opts struct, which should
> always have real struct size as a first field and the rest would be
> optional parameters added over time, which tune the behavior of existing
> API, if specified by user.
>
> Signed-off-by: Andrii Nakryiko <[email-masked]>
> ---
>  tools/lib/bpf/libbpf.c          | 51 ++++++++++++++++++++++++++++-----
>  tools/lib/bpf/libbpf.h          | 36 +++++++++++++++++++++--
>  tools/lib/bpf/libbpf.map        |  3 ++
>  tools/lib/bpf/libbpf_internal.h | 32 +++++++++++++++++++++
>  4 files changed, 112 insertions(+), 10 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 056769ce4fd0..503fba903e99 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -3620,16 +3620,33 @@ struct bpf_object *bpf_object__open(const char *path)
>  	return bpf_object__open_xattr(&attr);
>  }
>  
> -struct bpf_object *bpf_object__open_buffer(void *obj_buf,
> -					   size_t obj_buf_sz,
> -					   const char *name)
> +struct bpf_object *
> +bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)
> +{
> +	if (!OPTS_VALID(opts, bpf_object_open_opts))
> +		return ERR_PTR(-EINVAL);
> +	if (!path)
> +		return ERR_PTR(-EINVAL);
> +
> +	pr_debug("loading %s\n", path);
> +
> +	return __bpf_object__open(path, NULL, 0, 0);
> +}

This is not doing anything with opts...

[...]

> +struct bpf_object_open_opts {
> +	/* size of this struct, for forward/backward compatiblity */
> +	size_t sz;
> +	/* object name override, if provided:
> +	 * - for object open from file, this will override setting object
> +	 *   name from file path's base name;

... but this says it should be, no?

-Toke