Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libbpf: Add libbpf_set_strict_mode() API to turn on libbpf 1.0 behaviors

Add libbpf_set_strict_mode() API that allows application to simulate libbpf
1.0 breaking changes before libbpf 1.0 is released. This will help users
migrate gradually and with confidence.

For now only ALL or NONE options are available, subsequent patches will add
more flags. This patch is preliminary for selftests/bpf changes.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-2-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
5981881d a720a2a0

+71
+1
tools/lib/bpf/Makefile
··· 229 229 $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ 230 230 $(call do_install,btf.h,$(prefix)/include/bpf,644); \ 231 231 $(call do_install,libbpf_common.h,$(prefix)/include/bpf,644); \ 232 + $(call do_install,libbpf_legacy.h,$(prefix)/include/bpf,644); \ 232 233 $(call do_install,xsk.h,$(prefix)/include/bpf,644); \ 233 234 $(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \ 234 235 $(call do_install,$(BPF_HELPER_DEFS),$(prefix)/include/bpf,644); \
+17
tools/lib/bpf/libbpf.c
··· 151 151 return (__u64) (unsigned long) ptr; 152 152 } 153 153 154 + /* this goes away in libbpf 1.0 */ 155 + enum libbpf_strict_mode libbpf_mode = LIBBPF_STRICT_NONE; 156 + 157 + int libbpf_set_strict_mode(enum libbpf_strict_mode mode) 158 + { 159 + /* __LIBBPF_STRICT_LAST is the last power-of-2 value used + 1, so to 160 + * get all possible values we compensate last +1, and then (2*x - 1) 161 + * to get the bit mask 162 + */ 163 + if (mode != LIBBPF_STRICT_ALL 164 + && (mode & ~((__LIBBPF_STRICT_LAST - 1) * 2 - 1))) 165 + return errno = EINVAL, -EINVAL; 166 + 167 + libbpf_mode = mode; 168 + return 0; 169 + } 170 + 154 171 enum kern_feature_id { 155 172 /* v4.14: kernel support for program & map names. */ 156 173 FEAT_PROG_NAME,
+1
tools/lib/bpf/libbpf.h
··· 18 18 #include <linux/bpf.h> 19 19 20 20 #include "libbpf_common.h" 21 + #include "libbpf_legacy.h" 21 22 22 23 #ifdef __cplusplus 23 24 extern "C" {
+5
tools/lib/bpf/libbpf.map
··· 370 370 bpf_tc_hook_destroy; 371 371 bpf_tc_query; 372 372 } LIBBPF_0.3.0; 373 + 374 + LIBBPF_0.5.0 { 375 + global: 376 + libbpf_set_strict_mode; 377 + } LIBBPF_0.4.0;
+47
tools/lib/bpf/libbpf_legacy.h
··· 1 + /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 + 3 + /* 4 + * Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0]) 5 + * 6 + * [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY 7 + * 8 + * Copyright (C) 2021 Facebook 9 + */ 10 + #ifndef __LIBBPF_LEGACY_BPF_H 11 + #define __LIBBPF_LEGACY_BPF_H 12 + 13 + #include <linux/bpf.h> 14 + #include <stdbool.h> 15 + #include <stddef.h> 16 + #include <stdint.h> 17 + #include "libbpf_common.h" 18 + 19 + #ifdef __cplusplus 20 + extern "C" { 21 + #endif 22 + 23 + enum libbpf_strict_mode { 24 + /* Turn on all supported strict features of libbpf to simulate libbpf 25 + * v1.0 behavior. 26 + * This will be the default behavior in libbpf v1.0. 27 + */ 28 + LIBBPF_STRICT_ALL = 0xffffffff, 29 + 30 + /* 31 + * Disable any libbpf 1.0 behaviors. This is the default before libbpf 32 + * v1.0. It won't be supported anymore in v1.0, please update your 33 + * code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0. 34 + */ 35 + LIBBPF_STRICT_NONE = 0x00, 36 + 37 + __LIBBPF_STRICT_LAST, 38 + }; 39 + 40 + LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode); 41 + 42 + 43 + #ifdef __cplusplus 44 + } /* extern "C" */ 45 + #endif 46 + 47 + #endif /* __LIBBPF_LEGACY_BPF_H */