-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: Add BPF program and map iterators as built-in BPF programs.
The program and map iterators work similar to seq_file-s. Once the program is pinned in bpffs it can be read with "cat" tool to print human readable output. In this case about BPF programs and maps. For example: $ cat /sys/fs/bpf/progs.debug id name attached 5 dump_bpf_map bpf_iter_bpf_map 6 dump_bpf_prog bpf_iter_bpf_prog $ cat /sys/fs/bpf/maps.debug id name max_entries 3 iterator.rodata 1 To avoid kernel build dependency on clang 10 separate bpf skeleton generation into manual "make" step and instead check-in generated .skel.h into git. Unlike 'bpftool prog show' in-kernel BTF name is used (when available) to print full name of BPF program instead of 16-byte truncated name. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200819042759.51280-3-alexei.starovoitov@gmail.com
- Loading branch information
Alexei Starovoitov
authored and
Daniel Borkmann
committed
Aug 20, 2020
1 parent
005142b
commit f0fdfef
Showing
5 changed files
with
587 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-License-Identifier: GPL-2.0-only | ||
/.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# SPDX-License-Identifier: GPL-2.0 | ||
OUTPUT := .output | ||
CLANG ?= clang | ||
LLC ?= llc | ||
LLVM_STRIP ?= llvm-strip | ||
DEFAULT_BPFTOOL := $(OUTPUT)/sbin/bpftool | ||
BPFTOOL ?= $(DEFAULT_BPFTOOL) | ||
LIBBPF_SRC := $(abspath ../../../../tools/lib/bpf) | ||
BPFOBJ := $(OUTPUT)/libbpf.a | ||
BPF_INCLUDE := $(OUTPUT) | ||
INCLUDES := -I$(OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../../../tools/lib) \ | ||
-I$(abspath ../../../../tools/include/uapi) | ||
CFLAGS := -g -Wall | ||
|
||
abs_out := $(abspath $(OUTPUT)) | ||
ifeq ($(V),1) | ||
Q = | ||
msg = | ||
else | ||
Q = @ | ||
msg = @printf ' %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))"; | ||
MAKEFLAGS += --no-print-directory | ||
submake_extras := feature_display=0 | ||
endif | ||
|
||
.DELETE_ON_ERROR: | ||
|
||
.PHONY: all clean | ||
|
||
all: iterators.skel.h | ||
|
||
clean: | ||
$(call msg,CLEAN) | ||
$(Q)rm -rf $(OUTPUT) iterators | ||
|
||
iterators.skel.h: $(OUTPUT)/iterators.bpf.o | $(BPFTOOL) | ||
$(call msg,GEN-SKEL,$@) | ||
$(Q)$(BPFTOOL) gen skeleton $< > $@ | ||
|
||
|
||
$(OUTPUT)/iterators.bpf.o: iterators.bpf.c $(BPFOBJ) | $(OUTPUT) | ||
$(call msg,BPF,$@) | ||
$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES) \ | ||
-c $(filter %.c,$^) -o $@ && \ | ||
$(LLVM_STRIP) -g $@ | ||
|
||
$(OUTPUT): | ||
$(call msg,MKDIR,$@) | ||
$(Q)mkdir -p $(OUTPUT) | ||
|
||
$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT) | ||
$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) \ | ||
OUTPUT=$(abspath $(dir $@))/ $(abspath $@) | ||
|
||
$(DEFAULT_BPFTOOL): | ||
$(Q)$(MAKE) $(submake_extras) -C ../../../../tools/bpf/bpftool \ | ||
prefix= OUTPUT=$(abs_out)/ DESTDIR=$(abs_out) install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
WARNING: | ||
If you change "iterators.bpf.c" do "make -j" in this directory to rebuild "iterators.skel.h". | ||
Make sure to have clang 10 installed. | ||
See Documentation/bpf/bpf_devel_QA.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_core_read.h> | ||
|
||
#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record) | ||
struct seq_file; | ||
struct bpf_iter_meta { | ||
struct seq_file *seq; | ||
__u64 session_id; | ||
__u64 seq_num; | ||
}; | ||
|
||
struct bpf_map { | ||
__u32 id; | ||
char name[16]; | ||
__u32 max_entries; | ||
}; | ||
|
||
struct bpf_iter__bpf_map { | ||
struct bpf_iter_meta *meta; | ||
struct bpf_map *map; | ||
}; | ||
|
||
struct btf_type { | ||
__u32 name_off; | ||
}; | ||
|
||
struct btf_header { | ||
__u32 str_len; | ||
}; | ||
|
||
struct btf { | ||
const char *strings; | ||
struct btf_type **types; | ||
struct btf_header hdr; | ||
}; | ||
|
||
struct bpf_prog_aux { | ||
__u32 id; | ||
char name[16]; | ||
const char *attach_func_name; | ||
struct bpf_prog *linked_prog; | ||
struct bpf_func_info *func_info; | ||
struct btf *btf; | ||
}; | ||
|
||
struct bpf_prog { | ||
struct bpf_prog_aux *aux; | ||
}; | ||
|
||
struct bpf_iter__bpf_prog { | ||
struct bpf_iter_meta *meta; | ||
struct bpf_prog *prog; | ||
}; | ||
#pragma clang attribute pop | ||
|
||
static const char *get_name(struct btf *btf, long btf_id, const char *fallback) | ||
{ | ||
struct btf_type **types, *t; | ||
unsigned int name_off; | ||
const char *str; | ||
|
||
if (!btf) | ||
return fallback; | ||
str = btf->strings; | ||
types = btf->types; | ||
bpf_probe_read_kernel(&t, sizeof(t), types + btf_id); | ||
name_off = BPF_CORE_READ(t, name_off); | ||
if (name_off >= btf->hdr.str_len) | ||
return fallback; | ||
return str + name_off; | ||
} | ||
|
||
SEC("iter/bpf_map") | ||
int dump_bpf_map(struct bpf_iter__bpf_map *ctx) | ||
{ | ||
struct seq_file *seq = ctx->meta->seq; | ||
__u64 seq_num = ctx->meta->seq_num; | ||
struct bpf_map *map = ctx->map; | ||
|
||
if (!map) | ||
return 0; | ||
|
||
if (seq_num == 0) | ||
BPF_SEQ_PRINTF(seq, " id name max_entries\n"); | ||
|
||
BPF_SEQ_PRINTF(seq, "%4u %-16s%6d\n", map->id, map->name, map->max_entries); | ||
return 0; | ||
} | ||
|
||
SEC("iter/bpf_prog") | ||
int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx) | ||
{ | ||
struct seq_file *seq = ctx->meta->seq; | ||
__u64 seq_num = ctx->meta->seq_num; | ||
struct bpf_prog *prog = ctx->prog; | ||
struct bpf_prog_aux *aux; | ||
|
||
if (!prog) | ||
return 0; | ||
|
||
aux = prog->aux; | ||
if (seq_num == 0) | ||
BPF_SEQ_PRINTF(seq, " id name attached\n"); | ||
|
||
BPF_SEQ_PRINTF(seq, "%4u %-16s %s %s\n", aux->id, | ||
get_name(aux->btf, aux->func_info[0].type_id, aux->name), | ||
aux->attach_func_name, aux->linked_prog->aux->name); | ||
return 0; | ||
} | ||
char LICENSE[] SEC("license") = "GPL"; |
Oops, something went wrong.