Skip to content

Commit 166750b

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Support libbpf-provided extern variables
Add support for extern variables, provided to BPF program by libbpf. Currently the following extern variables are supported: - LINUX_KERNEL_VERSION; version of a kernel in which BPF program is executing, follows KERNEL_VERSION() macro convention, can be 4- and 8-byte long; - CONFIG_xxx values; a set of values of actual kernel config. Tristate, boolean, strings, and integer values are supported. Set of possible values is determined by declared type of extern variable. Supported types of variables are: - Tristate values. Are represented as `enum libbpf_tristate`. Accepted values are **strictly** 'y', 'n', or 'm', which are represented as TRI_YES, TRI_NO, or TRI_MODULE, respectively. - Boolean values. Are represented as bool (_Bool) types. Accepted values are 'y' and 'n' only, turning into true/false values, respectively. - Single-character values. Can be used both as a substritute for bool/tristate, or as a small-range integer: - 'y'/'n'/'m' are represented as is, as characters 'y', 'n', or 'm'; - integers in a range [-128, 127] or [0, 255] (depending on signedness of char in target architecture) are recognized and represented with respective values of char type. - Strings. String values are declared as fixed-length char arrays. String of up to that length will be accepted and put in first N bytes of char array, with the rest of bytes zeroed out. If config string value is longer than space alloted, it will be truncated and warning message emitted. Char array is always zero terminated. String literals in config have to be enclosed in double quotes, just like C-style string literals. - Integers. 8-, 16-, 32-, and 64-bit integers are supported, both signed and unsigned variants. Libbpf enforces parsed config value to be in the supported range of corresponding integer type. Integers values in config can be: - decimal integers, with optional + and - signs; - hexadecimal integers, prefixed with 0x or 0X; - octal integers, starting with 0. Config file itself is searched in /boot/config-$(uname -r) location with fallback to /proc/config.gz, unless config path is specified explicitly through bpf_object_open_opts' kernel_config_path option. Both gzipped and plain text formats are supported. Libbpf adds explicit dependency on zlib because of this, but this shouldn't be a problem, given libelf already depends on zlib. All detected extern variables, are put into a separate .extern internal map. It, similarly to .rodata map, is marked as read-only from BPF program side, as well as is frozen on load. This allows BPF verifier to track extern values as constants and perform enhanced branch prediction and dead code elimination. This can be relied upon for doing kernel version/feature detection and using potentially unsupported field relocations or BPF helpers in a CO-RE-based BPF program, while still having a single version of BPF program running on old and new kernels. Selftests are validating this explicitly for unexisting BPF helper. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20191214014710.3449601-3-andriin@fb.com
1 parent ac9d138 commit 166750b

8 files changed

Lines changed: 729 additions & 66 deletions

File tree

‎include/uapi/linux/btf.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ struct btf_param {
142142

143143
enum {
144144
BTF_VAR_STATIC = 0,
145-
BTF_VAR_GLOBAL_ALLOCATED,
145+
BTF_VAR_GLOBAL_ALLOCATED = 1,
146+
BTF_VAR_GLOBAL_EXTERN = 2,
146147
};
147148

148149
/* BTF_KIND_VAR is followed by a single "struct btf_var" to describe

‎tools/include/uapi/linux/btf.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ struct btf_header {
2222
};
2323

2424
/* Max # of type identifier */
25-
#define BTF_MAX_TYPE 0x0000ffff
25+
#define BTF_MAX_TYPE 0x000fffff
2626
/* Max offset into the string section */
27-
#define BTF_MAX_NAME_OFFSET 0x0000ffff
27+
#define BTF_MAX_NAME_OFFSET 0x00ffffff
2828
/* Max # of struct/union/enum members or func args */
2929
#define BTF_MAX_VLEN 0xffff
3030

@@ -142,7 +142,8 @@ struct btf_param {
142142

143143
enum {
144144
BTF_VAR_STATIC = 0,
145-
BTF_VAR_GLOBAL_ALLOCATED,
145+
BTF_VAR_GLOBAL_ALLOCATED = 1,
146+
BTF_VAR_GLOBAL_EXTERN = 2,
146147
};
147148

148149
/* BTF_KIND_VAR is followed by a single "struct btf_var" to describe

‎tools/lib/bpf/Makefile‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ ifndef VERBOSE
5656
endif
5757

5858
FEATURE_USER = .libbpf
59-
FEATURE_TESTS = libelf libelf-mmap bpf reallocarray
60-
FEATURE_DISPLAY = libelf bpf
59+
FEATURE_TESTS = libelf libelf-mmap zlib bpf reallocarray
60+
FEATURE_DISPLAY = libelf zlib bpf
6161

6262
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
6363
FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
@@ -160,7 +160,7 @@ all: fixdep
160160

161161
all_cmd: $(CMD_TARGETS) check
162162

163-
$(BPF_IN_SHARED): force elfdep bpfdep bpf_helper_defs.h
163+
$(BPF_IN_SHARED): force elfdep zdep bpfdep bpf_helper_defs.h
164164
@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
165165
(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
166166
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
@@ -178,7 +178,7 @@ $(BPF_IN_SHARED): force elfdep bpfdep bpf_helper_defs.h
178178
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'" >&2 )) || true
179179
$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
180180

181-
$(BPF_IN_STATIC): force elfdep bpfdep bpf_helper_defs.h
181+
$(BPF_IN_STATIC): force elfdep zdep bpfdep bpf_helper_defs.h
182182
$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
183183

184184
bpf_helper_defs.h: $(srctree)/tools/include/uapi/linux/bpf.h
@@ -190,7 +190,7 @@ $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
190190
$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED)
191191
$(QUIET_LINK)$(CC) $(LDFLAGS) \
192192
--shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
193-
-Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -o $@
193+
-Wl,--version-script=$(VERSION_SCRIPT) $^ -lelf -lz -o $@
194194
@ln -sf $(@F) $(OUTPUT)libbpf.so
195195
@ln -sf $(@F) $(OUTPUT)libbpf.so.$(LIBBPF_MAJOR_VERSION)
196196

@@ -279,12 +279,15 @@ clean:
279279

280280

281281

282-
PHONY += force elfdep bpfdep cscope tags
282+
PHONY += force elfdep zdep bpfdep cscope tags
283283
force:
284284

285285
elfdep:
286286
@if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit 1 ; fi
287287

288+
zdep:
289+
@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
290+
288291
bpfdep:
289292
@if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit 1 ; fi
290293

‎tools/lib/bpf/bpf_helpers.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#ifndef __always_inline
2626
#define __always_inline __attribute__((always_inline))
2727
#endif
28+
#ifndef __weak
29+
#define __weak __attribute__((weak))
30+
#endif
2831

2932
/*
3033
* Helper structure used by eBPF C program
@@ -44,4 +47,10 @@ enum libbpf_pin_type {
4447
LIBBPF_PIN_BY_NAME,
4548
};
4649

50+
enum libbpf_tristate {
51+
TRI_NO = 0,
52+
TRI_YES = 1,
53+
TRI_MODULE = 2,
54+
};
55+
4756
#endif

‎tools/lib/bpf/btf.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
578578
return -ENOENT;
579579
}
580580

581+
/* .extern datasec size and var offsets were set correctly during
582+
* extern collection step, so just skip straight to sorting variables
583+
*/
584+
if (t->size)
585+
goto sort_vars;
586+
581587
ret = bpf_object__section_size(obj, name, &size);
582588
if (ret || !size || (t->size && t->size != size)) {
583589
pr_debug("Invalid size for section %s: %u bytes\n", name, size);
@@ -614,7 +620,8 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
614620
vsi->offset = off;
615621
}
616622

617-
qsort(t + 1, vars, sizeof(*vsi), compare_vsi_off);
623+
sort_vars:
624+
qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
618625
return 0;
619626
}
620627

0 commit comments

Comments
 (0)