On Fri, Mar 19, 2021 at 9:16 PM Rafael David Tinoco <rafaeldtinoco@xxxxxxxxxx> wrote: > > Unfortunately some distros don't have their kernel version defined > accurately in <linux/version.h> due to different long term support > reasons. > > It is important to have a way to override the bpf kern_version > attribute during runtime: some old kernels might still check for > kern_version attribute during bpf_prog_load(). > > Signed-off-by: Rafael David Tinoco <rafaeldtinoco@xxxxxxxxxx> > --- > src/libbpf.c | 15 +++++++++++++++ > src/libbpf.h | 1 + > 2 files changed, 16 insertions(+) > > diff --git a/src/libbpf.c b/src/libbpf.c > index 0c4a386..7b52cd6 100644 > --- a/src/libbpf.c > +++ b/src/libbpf.c > @@ -8278,6 +8278,21 @@ int bpf_object__btf_fd(const struct bpf_object *obj) > return obj->btf ? btf__fd(obj->btf) : -1; > } > > +int bpf_object__set_kversion(struct bpf_object *obj, char *kern_version) > +{ > + __u32 major, minor, patch; > + > + if (!kern_version) { > + obj->kern_version = 0; > + return 0; > + } > + if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) != 3) given SEC("version") expects `int` and bpf_object__kversion() returns int, I think it's appropriate for bpf_object__set_kversion() to accept just opaque int as well. Please also check that obj is not loaded and return error if it is. Thanks! > + return -1; > + obj->kern_version = KERNEL_VERSION(major, minor, patch); > + > + return 0; > +} > + > int bpf_object__set_priv(struct bpf_object *obj, void *priv, > bpf_object_clear_priv_t clear_priv) > { > diff --git a/src/libbpf.h b/src/libbpf.h > index 3c35eb4..3e14ae7 100644 > --- a/src/libbpf.h > +++ b/src/libbpf.h > @@ -143,6 +143,7 @@ LIBBPF_API int bpf_object__unload(struct bpf_object *obj); > > LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); > LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); > +LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, char *kern_version); > > struct btf; > LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj); > -- > 2.27.0 >