Skip to content

Commit

Permalink
build: add minimum vmlinux.h and uapi headers
Browse files Browse the repository at this point in the history
- Add caped vmlinux.h and uapi headers that are needed to compile
  bpf selftests.
- uapi headers also provide exclusive definitions that are not available
  in the libbpf internal headers.
- Add a new makefile target to generate uapi headers.
- Remove bpftool dependency from Makefile.
  • Loading branch information
geyslan committed Feb 9, 2023
1 parent 15ae9ff commit 84a6686
Show file tree
Hide file tree
Showing 38 changed files with 152 additions and 304 deletions.
39 changes: 12 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ CLANG = clang
GO = go
VAGRANT = vagrant
CLANG_FMT = clang-format
GIT = $(shell which git || /bin/false)

HOSTOS = $(shell uname)
ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')

BTFFILE = /sys/kernel/btf/vmlinux
BPFTOOL = $(shell which bpftool || /bin/false)
GIT = $(shell which git || /bin/false)
VMLINUXH = $(OUTPUT)/vmlinux.h

# libbpf

Expand All @@ -46,6 +43,16 @@ CGO_LDFLAGS_DYN = "-lelf -lz -lbpf"
all: libbpfgo-static
test: libbpfgo-static-test

# libbpf uapi

.PHONY: libbpf-uapi

libbpf-uapi: $(LIBBPF_SRC)
# UAPI headers can be installed by a different package so they're not installed
# in by (libbpf) install rule.
UAPIDIR=$(LIBBPF_DESTDIR) \
$(MAKE) -C $(LIBBPF_SRC) install_uapi_headers

# libbpfgo test object

libbpfgo-test-bpf-static: libbpfgo-static # needed for serialization
Expand Down Expand Up @@ -73,7 +80,7 @@ libbpfgo-dynamic-test: libbpfgo-test-bpf-dynamic

# libbpf: static

libbpfgo-static: $(VMLINUXH) | $(LIBBPF_OBJ)
libbpfgo-static: libbpf-uapi $(LIBBPF_OBJ)
CC=$(CLANG) \
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
Expand All @@ -92,28 +99,6 @@ libbpfgo-static-test: libbpfgo-test-bpf-static
-v -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \
.

# vmlinux header file

.PHONY: vmlinuxh
vmlinuxh: $(VMLINUXH)

$(VMLINUXH): $(OUTPUT)
@if [ ! -f $(BTFFILE) ]; then \
echo "ERROR: kernel does not seem to support BTF"; \
exit 1; \
fi
@if [ ! $(BPFTOOL) ]; then \
echo "ERROR: could not find bpftool"; \
exit 1; \
fi;

@echo "INFO: generating $(VMLINUXH) from $(BTFFILE)"; \
if ! $(BPFTOOL) btf dump file $(BTFFILE) format c > $(VMLINUXH); then \
echo "ERROR: could not create $(VMLINUXH)"; \
rm -f "$(VMLINUXH)"; \
exit 1; \
fi;

# static libbpf generation for the git submodule

.PHONY: libbpf-static
Expand Down
1 change: 1 addition & 0 deletions libbpfgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <linux/bpf.h> // uapi

int libbpf_print_fn(enum libbpf_print_level level, // libbpf print level
const char *format, // format used for the msg
Expand Down
4 changes: 3 additions & 1 deletion samples/fentry/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
6 changes: 1 addition & 5 deletions selftest/build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ all: $(TEST).bpf.o
outputdir:
$(MAKE) -C $(BASEDIR) outputdir

vmlinuxh: outputdir
$(MAKE) -C $(BASEDIR) vmlinuxh

## test bpf dependency

$(TEST).bpf.o: $(TEST).bpf.c
$(MAKE) -C $(BASEDIR) vmlinuxh
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

## clean

Expand Down
4 changes: 3 additions & 1 deletion selftest/build/libbpfgo_test.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include <vmlinux.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
Expand Down
8 changes: 6 additions & 2 deletions selftest/cgroup-legacy/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//+build ignore
#include <vmlinux.h>

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>

#include <netinet/ip_icmp.h>
#include <netinet/ip.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
4 changes: 3 additions & 1 deletion selftest/cgroup/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
8 changes: 2 additions & 6 deletions selftest/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')
CFLAGS = -g -O2 -Wall -fpie
LDFLAGS =

CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))"
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)"
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)"
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"'

Expand All @@ -41,17 +41,13 @@ libbpfgo-static:
libbpfgo-dynamic:
$(MAKE) -C $(BASEDIR) libbpfgo-dynamic

vmlinuxh:
$(MAKE) -C $(BASEDIR) vmlinuxh

outputdir:
$(MAKE) -C $(BASEDIR) outputdir

## test bpf dependency

$(MAIN).bpf.o: $(MAIN).bpf.c
$(MAKE) -C $(BASEDIR) vmlinuxh
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

## test

Expand Down
32 changes: 32 additions & 0 deletions selftest/common/vmlinux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __VMLINUX_H__
#define __VMLINUX_H__

typedef __u8 u8;

typedef __s16 s16;

typedef __u16 u16;

typedef __s32 s32;

typedef __u32 u32;

typedef __s64 s64;

typedef __u64 u64;

struct trace_entry {
short unsigned int type;
unsigned char flags;
unsigned char preempt_count;
int pid;
};

struct trace_event_raw_sys_enter {
struct trace_entry ent;
long int id;
long unsigned int args[6];
char __data[0];
};

#endif /* __VMLINUX_H__ */
2 changes: 1 addition & 1 deletion selftest/create-map/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("kprobe/sys_execve")
Expand Down
2 changes: 1 addition & 1 deletion selftest/error-handling/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("kprobe/sys_mmap")
Expand Down
4 changes: 3 additions & 1 deletion selftest/global-variable/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
4 changes: 3 additions & 1 deletion selftest/iterators/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
4 changes: 3 additions & 1 deletion selftest/map-batch/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

#include "vmlinux.h"

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
Expand Down
4 changes: 3 additions & 1 deletion selftest/map-pin-info/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

#include "vmlinux.h"

struct value {
int x;
char y;
Expand Down
6 changes: 4 additions & 2 deletions selftest/map-update/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

#include "vmlinux.h"

struct value {
int x;
char y;
Expand Down Expand Up @@ -30,7 +32,7 @@ int kprobe__sys_mmap(struct pt_regs *ctx)
}
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, v1, sizeof(struct value));

int64_t secondKey = 42069420;
s64 secondKey = 42069420;
struct value *v2 = bpf_map_lookup_elem(&tester, &secondKey);
if (!v2) {
return 1;
Expand Down
16 changes: 5 additions & 11 deletions selftest/multiple-objects/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')
CFLAGS = -g -O2 -Wall -fpie
LDFLAGS =

CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT))"
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)"
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)"
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"'

CGO_CFLAGS_DYN = "-I. -I/usr/include/"
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf"
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf"

MAIN = main
FIRST = first
Expand All @@ -44,25 +44,19 @@ libbpfgo-static:
libbpfgo-dynamic:
$(MAKE) -C $(BASEDIR) libbpfgo-dynamic

vmlinuxh:
$(MAKE) -C $(BASEDIR) vmlinuxh

outputdir:
$(MAKE) -C $(BASEDIR) outputdir

## test bpf dependency

$(FIRST).bpf.o: $(FIRST).bpf.c
$(MAKE) -C $(BASEDIR) vmlinuxh
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

$(SECOND).bpf.o: $(SECOND).bpf.c
$(MAKE) -C $(BASEDIR) vmlinuxh
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

$(MAP).bpf.o: $(MAP).bpf.c
$(MAKE) -C $(BASEDIR) vmlinuxh
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

## test

Expand Down
2 changes: 1 addition & 1 deletion selftest/multiple-objects/first.bpf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
Expand Down
5 changes: 2 additions & 3 deletions selftest/multiple-objects/map.bpf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//+build ignore

#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "map.bpf.h"
#include "map.bpf.h"
4 changes: 2 additions & 2 deletions selftest/multiple-objects/map.bpf.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//+build ignore

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "vmlinux.h"

struct {
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(type, BPF_MAP_TYPE_RINGBUF);
Expand Down
2 changes: 1 addition & 1 deletion selftest/multiple-objects/second.bpf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
Expand Down
4 changes: 3 additions & 1 deletion selftest/netns/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
4 changes: 3 additions & 1 deletion selftest/object-iterator/main.bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//+build ignore
#include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#include "vmlinux.h"

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
Expand Down
Loading

0 comments on commit 84a6686

Please sign in to comment.