Skip to content

Commit 7615209

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Add runtime APIs to query libbpf version
Libbpf provided LIBBPF_MAJOR_VERSION and LIBBPF_MINOR_VERSION macros to check libbpf version at compilation time. This doesn't cover all the needs, though, because version of libbpf that application is compiled against doesn't necessarily match the version of libbpf at runtime, especially if libbpf is used as a shared library. Add libbpf_major_version() and libbpf_minor_version() returning major and minor versions, respectively, as integers. Also add a convenience libbpf_version_string() for various tooling using libbpf to print out libbpf version in a human-readable form. Currently it will return "v0.6", but in the future it can contains some extra information, so the format itself is not part of a stable API and shouldn't be relied upon. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Link: https://lore.kernel.org/bpf/20211118174054.2699477-1-andrii@kernel.org
1 parent 29ad850 commit 7615209

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
168168
return 0;
169169
}
170170

171+
__u32 libbpf_major_version(void)
172+
{
173+
return LIBBPF_MAJOR_VERSION;
174+
}
175+
176+
__u32 libbpf_minor_version(void)
177+
{
178+
return LIBBPF_MINOR_VERSION;
179+
}
180+
181+
const char *libbpf_version_string(void)
182+
{
183+
#define __S(X) #X
184+
#define _S(X) __S(X)
185+
return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION);
186+
#undef _S
187+
#undef __S
188+
}
189+
171190
enum kern_feature_id {
172191
/* v4.14: kernel support for program & map names. */
173192
FEAT_PROG_NAME,

tools/lib/bpf/libbpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
extern "C" {
2525
#endif
2626

27+
LIBBPF_API __u32 libbpf_major_version(void);
28+
LIBBPF_API __u32 libbpf_minor_version(void);
29+
LIBBPF_API const char *libbpf_version_string(void);
30+
2731
enum libbpf_errno {
2832
__LIBBPF_ERRNO__START = 4000,
2933

tools/lib/bpf/libbpf.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ LIBBPF_0.6.0 {
410410
btf__type_cnt;
411411
btf_dump__new;
412412
btf_dump__new_deprecated;
413+
libbpf_major_version;
414+
libbpf_minor_version;
415+
libbpf_version_string;
413416
perf_buffer__new;
414417
perf_buffer__new_deprecated;
415418
perf_buffer__new_raw;

0 commit comments

Comments
 (0)