Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git
4 changes: 2 additions & 2 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install dependencies
run: make deps
Expand Down
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Reproducible build/test environment for ebpf-kill-example.
#
# Build: docker build -t ebpf-kill-example .
# Test: docker run --rm --privileged ebpf-kill-example
#
# The eBPF program is loaded into the kernel and attached to a tracepoint at
# test time, so running the tests requires --privileged (or CAP_BPF +
# CAP_PERFMON + CAP_SYS_ADMIN) and a kernel that exposes the kill tracepoint.
FROM ubuntu:24.04

# Toolchain: clang/llvm emit the BPF bytecode, libelf + zlib are libbpf's
# link-time dependencies, and build-essential/make build libbpf and the loader.
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
clang \
llvm \
libelf-dev \
zlib1g-dev \
make \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . .

# Build the vendored libbpf submodule and the example.
RUN make

# The entrypoint mounts tracefs so the tracepoint is reachable at test time.
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["make", "test"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

deps:
sudo apt update
sudo apt install -y build-essential git make gcc clang llvm libelf-dev gcc-multilib
sudo apt install -y build-essential git make gcc clang llvm libelf-dev zlib1g-dev gcc-multilib
git submodule update --init

libbpf:
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ To run this example, the following software is required.
- **Linux kernel v4.19+**
- **LLVM 10+**
- libelf-dev (Installed via *make deps*)
- zlib1g-dev (Installed via *make deps*)
- gcc-multilib (Installed via *make deps*)

Alternatively, you can build and run everything with [Docker](#docker), which
needs only Docker installed — no local toolchain. This is the easiest way to
try the example on non-Linux hosts (e.g. macOS) or on architectures where
*gcc-multilib* is unavailable, such as arm64.

## Installation

To install ebpf-kill-example, first clone this repository.
Expand Down Expand Up @@ -57,6 +63,29 @@ nhed@nhed-1:~/Development/ebpf-kill-example$ make test
[ OK ] -- eBPF program ran as expected.
```

## Docker

A `Dockerfile` is provided for a reproducible build and test environment that
does not depend on your host setup. This is handy on non-Linux hosts or when
you would rather not install the toolchain locally.

Build the image:

```
docker build -t ebpf-kill-example .
```

Build and run the test. The eBPF program is loaded into the kernel and attached
to a tracepoint, so `--privileged` is required (the container mounts tracefs so
the tracepoint is reachable):

```
docker run --rm --privileged ebpf-kill-example
```

On Docker Desktop (macOS/Windows) the container runs against the Docker VM's
Linux kernel, so a Linux host is not required.

## Example
![Example](/img/example.png?raw=true)

Expand Down
10 changes: 10 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# The eBPF loader attaches to a kernel tracepoint, which is exposed through
# tracefs. Containers do not mount tracefs by default, so mount it here (this
# requires --privileged). Failure is ignored so non-test uses of the image
# still work without extra privileges.
if [ ! -e /sys/kernel/tracing/events ]; then
mount -t tracefs nodev /sys/kernel/tracing 2>/dev/null || true
fi

exec "$@"
2 changes: 1 addition & 1 deletion libbpf
Submodule libbpf updated 98 files
+1 −1 .gitattributes
+0 −31 .github/actions/build-selftests/action.yml
+0 −59 .github/actions/build-selftests/build_selftests.sh
+0 −38 .github/actions/build-selftests/helpers.sh
+0 −3 .github/actions/build-selftests/prepare_selftests-4.9.0.sh
+0 −3 .github/actions/build-selftests/prepare_selftests-5.5.0.sh
+93,689 −87,619 .github/actions/build-selftests/vmlinux.h
+0 −99 .github/actions/vmtest/action.yml
+26 −25 .github/workflows/build.yml
+1 −1 .github/workflows/cifuzz.yml
+2 −2 .github/workflows/codeql.yml
+7 −7 .github/workflows/coverity.yml
+1 −1 .github/workflows/lint.yml
+18 −23 .github/workflows/ondemand.yml
+0 −20 .github/workflows/pahole.yml
+13 −26 .github/workflows/test.yml
+104 −0 .github/workflows/vmtest.yml
+27 −0 .mailmap
+6 −2 .readthedocs.yaml
+1 −1 BPF-CHECKPOINT-COMMIT
+1 −1 CHECKPOINT-COMMIT
+1 −1 README.md
+14 −0 ci/build-in-docker.sh
+0 −70 ci/diffs/0001-s390-define-RUNTIME_DISCARD_EXIT-to-fix-link-error-w.patch
+0 −46 ci/diffs/0001-selftests-bpf-Select-CONFIG_FUNCTION_ERROR_INJECTION.patch
+0 −83 ci/diffs/0001-veth-take-into-account-peer-device-for-NETDEV_XDP_AC.patch
+72 −0 ci/diffs/20260312-selftests-bpf-Bump-path-and-command-buffer-sizes-in-.patch
+0 −8 ci/vmtest/configs/ALLOWLIST-4.9.0
+0 −53 ci/vmtest/configs/ALLOWLIST-5.5.0
+17 −0 ci/vmtest/configs/DENYLIST
+0 −118 ci/vmtest/configs/DENYLIST-5.5.0
+0 −1 ci/vmtest/configs/DENYLIST-latest
+0 −3 ci/vmtest/configs/DENYLIST-latest.s390x
+37 −0 ci/vmtest/configs/run-vmtest.env
+0 −38 ci/vmtest/helpers.sh
+0 −87 ci/vmtest/run_selftests.sh
+1 −0 docs/conf.py
+8 −0 docs/libbpf_overview.rst
+54 −4 docs/program_types.rst
+2 −1 docs/sphinx/requirements.txt
+58 −0 fuzz/readme.md
+16 −0 include/linux/filter.h
+16 −0 include/linux/kernel.h
+3 −0 include/linux/types.h
+638 −93 include/uapi/linux/bpf.h
+3 −3 include/uapi/linux/bpf_common.h
+5 −4 include/uapi/linux/btf.h
+108 −30 include/uapi/linux/fcntl.h
+729 −3 include/uapi/linux/if_link.h
+74 −1 include/uapi/linux/if_xdp.h
+182 −4 include/uapi/linux/netdev.h
+144 −13 include/uapi/linux/netlink.h
+3 −3 include/uapi/linux/openat2.h
+403 −323 include/uapi/linux/perf_event.h
+250 −61 include/uapi/linux/pkt_cls.h
+240 −121 include/uapi/linux/pkt_sched.h
+85 −0 include/uapi/linux/stddef.h
+35 −30 scripts/build-fuzzers.sh
+37 −0 scripts/mailmap-update.sh
+30 −12 scripts/sync-kernel.sh
+15 −9 src/Makefile
+280 −53 src/bpf.c
+259 −34 src/bpf.h
+91 −8 src/bpf_core_read.h
+3 −0 src/bpf_gen_internal.h
+276 −241 src/bpf_helper_defs.h
+60 −11 src/bpf_helpers.h
+55 −49 src/bpf_tracing.h
+1,503 −432 src/btf.c
+95 −2 src/btf.h
+103 −12 src/btf_dump.c
+177 −0 src/btf_iter.c
+519 −0 src/btf_relocate.c
+558 −0 src/elf.c
+698 −0 src/features.c
+191 −61 src/gen_loader.c
+10 −20 src/hashmap.h
+3,147 −1,337 src/libbpf.c
+410 −38 src/libbpf.h
+66 −2 src/libbpf.map
+19 −0 src/libbpf_common.h
+0 −75 src/libbpf_errno.c
+197 −13 src/libbpf_internal.h
+2 −2 src/libbpf_legacy.h
+28 −6 src/libbpf_probes.c
+256 −0 src/libbpf_utils.c
+1 −1 src/libbpf_version.h
+330 −119 src/linker.c
+26 −5 src/netlink.c
+7 −8 src/nlattr.c
+22 −7 src/relo_core.c
+132 −35 src/ringbuf.c
+75 −6 src/skel_internal.h
+0 −21 src/str_error.c
+0 −6 src/str_error.h
+89 −17 src/usdt.bpf.h
+206 −74 src/usdt.c
+1 −1 src/zip.c
13 changes: 9 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ LDFLAGS ?= -L$(LIBBPF_DIR)

LIBS = -lbpf -lelf

# clang's `bpf` target does not search the host's multiarch include directory,
# so kernel/libc headers such as <asm/types.h> are not found. Point it there
# explicitly. Using $(uname -m) keeps this portable across architectures
# (e.g. x86_64-linux-gnu, aarch64-linux-gnu).
ARCH_INCLUDES = -I/usr/include/$(shell uname -m)-linux-gnu

all: $(TARGET) kern.o

.PHONY: clean

clean:
rm -f $(TARGET)
rm -f kern.o
rm -f kern.ll

$(TARGET): %: user.c Makefile
gcc $(CFLAGS) $(LDFLAGS) -o $(TARGET) user.c -Wl,-rpath=$(LIBBPF_DIR) $(LIBS)

kern.o: kern.c
clang -S \
clang \
-target bpf \
-D __BPF_TRACING__ \
$(CFLAGS) \
$(ARCH_INCLUDES) \
-Wall \
-Werror \
-O2 -emit-llvm -c -g kern.c
llc -march=bpf -filetype=obj -o kern.o kern.ll
-O2 -g -c kern.c -o kern.o
2 changes: 1 addition & 1 deletion src/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

int main(int argc, char **argv) {
char path[PATH_MAX];
sprintf(path, "%s/kern.o", dirname(argv[0]));
snprintf(path, sizeof(path), "%s/kern.o", dirname(argv[0]));

struct bpf_object *obj;
struct bpf_link *link = NULL;
Expand Down
5 changes: 4 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ set -e
[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"

echo "-- Loading eBPF program."
sudo ./src/ebpf-kill-example > /tmp/ebpf-kill.log &
# Already running as root (line 14 re-execs via sudo when needed), so invoke
# the loader directly. This keeps the test working in environments without
# sudo installed, such as containers.
./src/ebpf-kill-example > /tmp/ebpf-kill.log &

sleep 5

Expand Down
Loading