Skip to content

Commit 3f19956

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Convert xdp_monitor_kern.o to XDP samples helper
We already moved all the functionality it provided in XDP samples helper userspace and kernel BPF object, so just delete the unneeded code. We also add generation of BPF skeleton and compilation using clang -target bpf for files ending with .bpf.c suffix (to denote that they use vmlinux.h). Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-14-memxor@gmail.com
1 parent 384b6b3 commit 3f19956

3 files changed

Lines changed: 49 additions & 258 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ always-y += xdp_redirect_kern.o
164164
always-y += xdp_redirect_map_kern.o
165165
always-y += xdp_redirect_map_multi_kern.o
166166
always-y += xdp_redirect_cpu_kern.o
167-
always-y += xdp_monitor_kern.o
168167
always-y += xdp_rxq_info_kern.o
169168
always-y += xdp2skb_meta_kern.o
170169
always-y += syscall_tp_kern.o
@@ -338,6 +337,47 @@ endif
338337

339338
clean-files += vmlinux.h
340339

340+
# Get Clang's default includes on this system, as opposed to those seen by
341+
# '-target bpf'. This fixes "missing" files on some architectures/distros,
342+
# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
343+
#
344+
# Use '-idirafter': Don't interfere with include mechanics except where the
345+
# build would have failed anyways.
346+
define get_sys_includes
347+
$(shell $(1) -v -E - </dev/null 2>&1 \
348+
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
349+
$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
350+
endef
351+
352+
CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
353+
354+
$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
355+
356+
$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
357+
@echo " CLANG-BPF " $@
358+
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
359+
-Wno-compare-distinct-pointer-types -I$(srctree)/include \
360+
-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
361+
-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
362+
-c $(filter %.bpf.c,$^) -o $@
363+
364+
LINKED_SKELS := xdp_monitor.skel.h
365+
clean-files += $(LINKED_SKELS)
366+
367+
xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
368+
369+
LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
370+
371+
BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
372+
BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
373+
BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
374+
375+
$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
376+
@echo " BPF GEN-OBJ " $(@:.skel.h=)
377+
$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
378+
@echo " BPF GEN-SKEL" $(@:.skel.h=)
379+
$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
380+
341381
# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
342382
# But, there is no easy way to fix it, so just exclude it since it is
343383
# useless for BPF samples.

‎samples/bpf/xdp_monitor.bpf.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright(c) 2017-2018 Jesper Dangaard Brouer, Red Hat Inc.
3+
*
4+
* XDP monitor tool, based on tracepoints
5+
*/
6+
#include "xdp_sample.bpf.h"
7+
8+
char _license[] SEC("license") = "GPL";

‎samples/bpf/xdp_monitor_kern.c‎

Lines changed: 0 additions & 257 deletions
This file was deleted.

0 commit comments

Comments
 (0)