libbpf: add bpf object kern_version attribute setter
PATCH 1/1 libbpf: add bpf object kern_version attribute setter
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().
--- src/libbpf.c | 15 +++++++++++++++ src/libbpf.h | 1 + 2 files changed, 16 insertions(+)
src/libbpf.c +15 -0
@@ -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) {
src/libbpf.h +1 -0
@@ -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);
Signature · 1 line
2.27.0
Quoted 22 lines · Rafael David Tinoco
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_versionattribute during runtime: some old kernels might still check forkern_versionattribute duringbpf_prog_load().Signed-off-by Rafael David Tinoco <[email protected]>--- src/libbpf.c | 15 +++++++++++++++ src/libbpf.h | 1 + 2 files changed, 16 insertions(+)src/libbpf.c +0
-0@@ -8278,6 +8278,21 @@ int bpf_object__btf_fd(const struct bpf_object *obj)return obj->btf ?btf__fd (obj->btf) :-1 ; }
src/libbpf.c int bpf_object__btf_fd(const struct bpf_object *obj) · line 8289
Quoted 10 lines
src/libbpf.c +9
-0+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!
Quoted 24 lines
src/libbpf.c +6
-0+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) {src/libbpf.h +1
-1@@ -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
Quoted 26 lines · Andrii Nakryiko
Quoted 23 lines · Rafael David Tinoco
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_versionattribute during runtime: some old kernels might still check forkern_versionattribute duringbpf_prog_load().Signed-off-by Rafael David Tinoco <[email protected]>--- src/libbpf.c | 15 +++++++++++++++ src/libbpf.h | 1 + 2 files changed, 16 insertions(+)src/libbpf.c +0
-0@@ -8278,6 +8278,21 @@ int bpf_object__btf_fd(const struct bpf_object *obj)return obj->btf ?btf__fd (obj->btf) :-1 ; }
Quoted 15 lines
src/libbpf.c int bpf_object__btf_fd(const struct bpf_object *obj) · line 8289
Quoted 9 lines
src/libbpf.c +9
-0+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")expectsintandbpf_object__kversion()returns int, I think it's appropriate forbpf_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.
Quoted 24 lines
Quoted 24 lines
src/libbpf.c +6
-0+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) {src/libbpf.h +1
-1@@ -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
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
src/libbpf.c +7
-0+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 ;Quoted 8 lines
Quoted 3 lines
(quoted diff) +2
-0+ } + if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) !=3)
given
SEC("version")expectsintandbpf_object__kversion()returns int, I think it's appropriate forbpf_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!
Quoted 3 lines
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.