Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BPF example with ktrace and CO-RE #24

Merged
merged 18 commits into from Jun 24, 2021

Conversation

netoptimizer
Copy link
Member

"Learning by doing":

  • Learning what CO-RE can do via writing a simple kprobe tracing program use some of these features.

I had to change the Makefile system to handle SEC("kprobe") and macro BPF_KPROBE as compiling with -target bpf result in PT_REGS_PARM1 not being defined. As include <bpf/bpf_tracing.h> cannot determine the ARCH from compiler defines.
To solve this detect the ARCH our makefile system in file lib/common.mk (via uname -m) and defines the __TARGET_ARCH_$(ARCH) in BPF_CFLAGS.

This PR also contains some vmlinux.h header variants for a number of ARCHs.
Choose to splitup vmlinux_*.h headers and only include what was needed for this example.
We will see how much maintenance this cause, and if this is the case we can consider adding the full vmlinux.h later.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Using bpf_core_type_id_kernel to find kernels BTF_ID for SKB.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Use this --debug to better understand what libbpf does of
CO-RE relocation tricks and lookup of BTF-IDs.

Example output:

 [...]
 libbpf: loading kernel BTF '/sys/kernel/btf/vmlinux': 0
 libbpf: map 'ktrace01.data': created successfully, fd=4
 libbpf: sec 'kprobe/udp_send_skb': found 4 CO-RE relocations
 libbpf: prog 'udp_send_skb': relo #0: kind <byte_off> (0), spec is [2] struct pt_regs.di (0:14 @ offset 112)
 libbpf: CO-RE relocating [0] struct pt_regs: found target candidate [176] struct pt_regs in [vmlinux]
 libbpf: prog 'udp_send_skb': relo #0: matching candidate #0 [176] struct pt_regs.di (0:14 @ offset 112)
 libbpf: prog 'udp_send_skb': relo #0: patched insn #0 (LDX/ST/STX) off 112 -> 112
 libbpf: prog 'udp_send_skb': relo xdp-project#1: kind <byte_off> (0), spec is [7] struct sk_buff___local.hash (0:1 @ offset 4)
 libbpf: CO-RE relocating [0] struct sk_buff___local: found target candidate [2965] struct sk_buff in [vmlinux]
 [...]

Notice how the BTF-ID of 'struct sk_buff' is resolved to be 2965.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
@netoptimizer netoptimizer self-assigned this Jun 24, 2021
@netoptimizer
Copy link
Member Author

Going to merge this as I need to reference some of the commits in emails.

@netoptimizer netoptimizer merged commit 4a84222 into xdp-project:master Jun 24, 2021
netoptimizer added a commit that referenced this pull request Aug 25, 2021
Fixes pull request #24

I had forgot to git add headers/vmlinux/vmlinux_arch.h in PR#24
which caused compile failures for ktrace-CO-RE.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant