* [PATCH] libbpf: add bpf object kern_version attribute setter
@ 2021-03-20 4:16 Rafael David Tinoco
2021-03-20 17:23 ` Andrii Nakryiko
0 siblings, 1 reply; 4+ messages in thread
From: Rafael David Tinoco @ 2021-03-20 4:16 UTC (permalink / raw)
To: bpf, rafaeldtinoco; +Cc: andrii.nakryiko, daniel
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@ubuntu.com>
---
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)
+ 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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] libbpf: add bpf object kern_version attribute setter
2021-03-20 4:16 [PATCH] libbpf: add bpf object kern_version attribute setter Rafael David Tinoco
@ 2021-03-20 17:23 ` Andrii Nakryiko
2021-03-20 17:25 ` Andrii Nakryiko
0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-20 17:23 UTC (permalink / raw)
To: Rafael David Tinoco; +Cc: bpf, Daniel Borkmann
On Fri, Mar 19, 2021 at 9:16 PM Rafael David Tinoco
<rafaeldtinoco@ubuntu.com> 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@ubuntu.com>
> ---
> 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
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] libbpf: add bpf object kern_version attribute setter
2021-03-20 17:23 ` Andrii Nakryiko
@ 2021-03-20 17:25 ` Andrii Nakryiko
2021-03-20 19:07 ` Rafael David Tinoco
0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-20 17:25 UTC (permalink / raw)
To: Rafael David Tinoco; +Cc: bpf, Daniel Borkmann
On Sat, Mar 20, 2021 at 10:23 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Mar 19, 2021 at 9:16 PM Rafael David Tinoco
> <rafaeldtinoco@ubuntu.com> 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@ubuntu.com>
> > ---
> > 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!
>
Oh, and please use [PATCH bpf-next] subject prefix to specify that
this is destined to the bpf-next tree. You'll also add v2 in between
PATCH and bpf-next for the next version, of course.
> > + 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
> >
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] libbpf: add bpf object kern_version attribute setter
2021-03-20 17:25 ` Andrii Nakryiko
@ 2021-03-20 19:07 ` Rafael David Tinoco
0 siblings, 0 replies; 4+ messages in thread
From: Rafael David Tinoco @ 2021-03-20 19:07 UTC (permalink / raw)
To: Andrii Nakryiko; +Cc: bpf, Daniel Borkmann
>>> +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!
>
> Oh, and please use [PATCH bpf-next] subject prefix to specify that
> this is destined to the bpf-next tree. You'll also add v2 in between
> PATCH and bpf-next for the next version, of course.
will do, and sorry for the initial burden, getting used to libbpf devel.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-03-20 19:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-20 4:16 [PATCH] libbpf: add bpf object kern_version attribute setter Rafael David Tinoco
2021-03-20 17:23 ` Andrii Nakryiko
2021-03-20 17:25 ` Andrii Nakryiko
2021-03-20 19:07 ` Rafael David Tinoco
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).