Skip to content

Commit ba6314a

Browse files
committed
add basic of xdp lb and xdp firewall
1 parent 2f3ed55 commit ba6314a

File tree

7 files changed

+425
-0
lines changed

7 files changed

+425
-0
lines changed

src/41-xdp-loadbalancer/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.output
2+
uprobe
3+
merge-btf
4+
*.btf
5+
xdp_lb

src/41-xdp-loadbalancer/Makefile

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2+
OUTPUT := .output
3+
CLANG ?= clang
4+
LIBBPF_SRC := $(abspath ../third_party/libbpf/src)
5+
BPFTOOL_SRC := $(abspath ../third_party/bpftool/src)
6+
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
7+
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
8+
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
9+
LIBBLAZESYM_SRC := $(abspath ../third_party/blazesym/)
10+
LIBBLAZESYM_OBJ := $(abspath $(OUTPUT)/libblazesym.a)
11+
LIBBLAZESYM_HEADER := $(abspath $(OUTPUT)/blazesym.h)
12+
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
13+
| sed 's/arm.*/arm/' \
14+
| sed 's/aarch64/arm64/' \
15+
| sed 's/ppc64le/powerpc/' \
16+
| sed 's/mips.*/mips/' \
17+
| sed 's/riscv64/riscv/' \
18+
| sed 's/loongarch64/loongarch/')
19+
VMLINUX := ../third_party/vmlinux/$(ARCH)/vmlinux.h
20+
# Use our own libbpf API headers and Linux UAPI headers distributed with
21+
# libbpf to avoid dependency on system-wide headers, which could be missing or
22+
# outdated
23+
INCLUDES := -I$(OUTPUT) -I../third_party/libbpf/include/uapi -I$(dir $(VMLINUX))
24+
CFLAGS := -g -Wall
25+
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
26+
27+
APPS = xdp_lb
28+
29+
CARGO ?= $(shell which cargo)
30+
ifeq ($(strip $(CARGO)),)
31+
BZS_APPS :=
32+
else
33+
BZS_APPS :=
34+
APPS += $(BZS_APPS)
35+
# Required by libblazesym
36+
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
37+
endif
38+
39+
# Get Clang's default includes on this system. We'll explicitly add these dirs
40+
# to the includes list when compiling with `-target bpf` because otherwise some
41+
# architecture-specific dirs will be "missing" on some architectures/distros -
42+
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
43+
# sys/cdefs.h etc. might be missing.
44+
#
45+
# Use '-idirafter': Don't interfere with include mechanics except where the
46+
# build would have failed anyways.
47+
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
48+
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
49+
50+
ifeq ($(V),1)
51+
Q =
52+
msg =
53+
else
54+
Q = @
55+
msg = @printf ' %-8s %s%s\n' \
56+
"$(1)" \
57+
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
58+
"$(if $(3), $(3))";
59+
MAKEFLAGS += --no-print-directory
60+
endif
61+
62+
define allow-override
63+
$(if $(or $(findstring environment,$(origin $(1))),\
64+
$(findstring command line,$(origin $(1)))),,\
65+
$(eval $(1) = $(2)))
66+
endef
67+
68+
$(call allow-override,CC,$(CROSS_COMPILE)cc)
69+
$(call allow-override,LD,$(CROSS_COMPILE)ld)
70+
71+
.PHONY: all
72+
all: $(APPS)
73+
74+
.PHONY: clean
75+
clean:
76+
$(call msg,CLEAN)
77+
$(Q)rm -rf $(OUTPUT) $(APPS)
78+
79+
$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
80+
$(call msg,MKDIR,$@)
81+
$(Q)mkdir -p $@
82+
83+
# Build libbpf
84+
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
85+
$(call msg,LIB,$@)
86+
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
87+
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
88+
INCLUDEDIR= LIBDIR= UAPIDIR= \
89+
install
90+
91+
# Build bpftool
92+
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
93+
$(call msg,BPFTOOL,$@)
94+
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap
95+
96+
97+
$(LIBBLAZESYM_SRC)/target/release/libblazesym.a::
98+
$(Q)cd $(LIBBLAZESYM_SRC) && $(CARGO) build --features=cheader,dont-generate-test-files --release
99+
100+
$(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
101+
$(call msg,LIB, $@)
102+
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym.a $@
103+
104+
$(LIBBLAZESYM_HEADER): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
105+
$(call msg,LIB,$@)
106+
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/blazesym.h $@
107+
108+
# Build BPF code
109+
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
110+
$(call msg,BPF,$@)
111+
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
112+
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
113+
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
114+
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
115+
116+
# Generate BPF skeletons
117+
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
118+
$(call msg,GEN-SKEL,$@)
119+
$(Q)$(BPFTOOL) gen skeleton $< > $@
120+
121+
# Build user-space code
122+
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h
123+
124+
$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
125+
$(call msg,CC,$@)
126+
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
127+
128+
$(patsubst %,$(OUTPUT)/%.o,$(BZS_APPS)): $(LIBBLAZESYM_HEADER)
129+
130+
$(BZS_APPS): $(LIBBLAZESYM_OBJ)
131+
132+
# Build application binary
133+
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
134+
$(call msg,BINARY,$@)
135+
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@
136+
137+
# delete failed targets
138+
.DELETE_ON_ERROR:
139+
140+
# keep intermediate (.skel.h, .bpf.o, etc) targets
141+
.SECONDARY:

src/41-xdp-loadbalancer/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// xdp_lb.bpf.c
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <linux/if_ether.h>
5+
#include <linux/ip.h>
6+
#include <linux/in.h>
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_ARRAY);
10+
__uint(max_entries, 2); // Two backends
11+
__type(key, __u32);
12+
__type(value, __u32); // Backend IPs
13+
} backends SEC(".maps");
14+
15+
SEC("xdp")
16+
int xdp_load_balancer(struct xdp_md *ctx) {
17+
void *data_end = (void *)(long)ctx->data_end;
18+
void *data = (void *)(long)ctx->data;
19+
20+
struct ethhdr *eth = data;
21+
if ((void *)(eth + 1) > data_end)
22+
return XDP_PASS;
23+
24+
if (eth->h_proto != __constant_htons(ETH_P_IP))
25+
return XDP_PASS;
26+
27+
struct iphdr *iph = (struct iphdr *)(eth + 1);
28+
if ((void *)(iph + 1) > data_end)
29+
return XDP_PASS;
30+
31+
if (iph->protocol != IPPROTO_TCP && iph->protocol != IPPROTO_UDP)
32+
return XDP_PASS;
33+
34+
__u32 key = 0;
35+
static __u32 cnt = 0;
36+
__u32 *backend_ip = bpf_map_lookup_elem(&backends, &key);
37+
38+
if (!backend_ip)
39+
return XDP_PASS;
40+
41+
cnt = (cnt + 1) % 2; // Round-robin
42+
key = cnt;
43+
backend_ip = bpf_map_lookup_elem(&backends, &key);
44+
45+
if (backend_ip) {
46+
iph->daddr = *backend_ip; // Redirect to the backend IP
47+
iph->check = 0; // Needs recomputation in real cases
48+
}
49+
50+
return XDP_TX; // Transmit modified packet back
51+
}
52+
53+
char _license[] SEC("license") = "GPL";

src/41-xdp-loadbalancer/xdp_lb.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// xdp_lb.c
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <bpf/libbpf.h>
5+
#include <bpf/bpf.h>
6+
#include <net/if.h>
7+
#include <linux/if_link.h>
8+
#include "xdp_lb.skel.h" // This header is auto-generated by bpftool
9+
10+
#define IFACE "eth0" // Replace with your network interface
11+
12+
static int set_up_backends(struct xdp_lb_bpf *skel) {
13+
__u32 backend1 = htonl(0xC0A80102); // 192.168.1.2
14+
__u32 backend2 = htonl(0xC0A80103); // 192.168.1.3
15+
__u32 key = 0;
16+
17+
if (bpf_map_update_elem(bpf_map__fd(skel->maps.backends), &key, &backend1, BPF_ANY) < 0) {
18+
fprintf(stderr, "Failed to update backend 1\n");
19+
return -1;
20+
}
21+
22+
key = 1;
23+
if (bpf_map_update_elem(bpf_map__fd(skel->maps.backends), &key, &backend2, BPF_ANY) < 0) {
24+
fprintf(stderr, "Failed to update backend 2\n");
25+
return -1;
26+
}
27+
28+
return 0;
29+
}
30+
31+
int main() {
32+
struct xdp_lb_bpf *skel;
33+
int err, ifindex;
34+
35+
// Load and verify the eBPF skeleton
36+
skel = xdp_lb_bpf__open();
37+
if (!skel) {
38+
fprintf(stderr, "Failed to open and load skeleton\n");
39+
return 1;
40+
}
41+
42+
// Load eBPF program
43+
err = xdp_lb_bpf__load(skel);
44+
if (err) {
45+
fprintf(stderr, "Failed to load BPF program: %d\n", err);
46+
return 1;
47+
}
48+
49+
// Set up the backend IP addresses
50+
if (set_up_backends(skel) < 0) {
51+
fprintf(stderr, "Failed to set up backend IP addresses\n");
52+
return 1;
53+
}
54+
55+
// Get interface index
56+
ifindex = if_nametoindex(IFACE);
57+
if (ifindex == 0) {
58+
perror("if_nametoindex");
59+
return 1;
60+
}
61+
62+
// Attach the XDP program
63+
err = bpf_xdp_attach(ifindex, bpf_program__fd(skel->progs.xdp_load_balancer), XDP_FLAGS_SKB_MODE, NULL);
64+
if (err) {
65+
fprintf(stderr, "Failed to attach XDP program: %d\n", err);
66+
return 1;
67+
}
68+
69+
printf("XDP Load Balancer is running on interface %s...\n", IFACE);
70+
sleep(60); // Keep running for 60 seconds
71+
72+
// Detach the XDP program before exiting
73+
err = bpf_xdp_detach(ifindex, XDP_FLAGS_SKB_MODE, NULL);
74+
if (err) {
75+
fprintf(stderr, "Failed to detach XDP program: %d\n", err);
76+
return 1;
77+
}
78+
79+
// Clean up
80+
xdp_lb_bpf__destroy(skel);
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)