Skip to content

Commit 1696b2b

Browse files
committed
update 43 for xdp test
1 parent cad85ca commit 1696b2b

File tree

10 files changed

+534
-4
lines changed

10 files changed

+534
-4
lines changed

src/43-kfuncs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ Assuming you have a user-space application or a tool to load and attach the eBPF
393393
**Sample Output:**
394394

395395
```bash
396-
# sudo ./load_kfunc
396+
# sudo ./kfunc
397397
BPF program loaded and attached successfully. Press Ctrl-C to exit.
398398
```
399399

src/43-kfuncs/README.zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ int handle_kprobe(struct pt_regs *ctx)
393393
**示例输出:**
394394

395395
```bash
396-
# sudo ./load_kfunc
396+
# sudo ./kfunc
397397
BPF 程序已加载并成功附加。按 Ctrl-C 退出。
398398
```
399399

src/46-xdp-test/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.output
2+
/bootstrap
3+
xdp-pktgen

src/46-xdp-test/Makefile

+141
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-pktgen # minimal minimal_legacy uprobe kprobe fentry usdt sockfilter tc ksyscall
28+
29+
CARGO ?= $(shell which cargo)
30+
ifeq ($(strip $(CARGO)),)
31+
BZS_APPS :=
32+
else
33+
BZS_APPS := # profile
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/46-xdp-test/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# xdp-pktgen: xdp based packet generator
2+
3+
This is a simple xdp based packet generator.
4+
5+
## **How to use**
6+
7+
clone the repo, you can update the git submodule with following commands:
8+
9+
```sh
10+
git submodule update --init --recursive
11+
```
12+
13+
### **3. Install dependencies**
14+
15+
For dependencies, it varies from distribution to distribution. You can refer to shell.nix and dockerfile for installation.
16+
17+
On Ubuntu, you may run `make install` or
18+
19+
```sh
20+
sudo apt-get install -y --no-install-recommends \
21+
libelf1 libelf-dev zlib1g-dev \
22+
make clang llvm
23+
```
24+
25+
to install dependencies.
26+
27+
### **4. Build the project**
28+
29+
To build the project, run the following command:
30+
31+
```sh
32+
make build
33+
```
34+
35+
This will compile your code and create the necessary binaries. You can you the `Github Code space` or `Github Action` to build the project as well.
36+
37+
### ***Run the Project***
38+
39+
You can run the binary with:
40+
41+
```console
42+
sudo ./xdp-pktgen
43+
```
44+

src/46-xdp-test/README.zh.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# xdp-pktgen: xdp based packet generator
2+
3+
This is a simple xdp based packet generator.
4+
5+
## **How to use**
6+
7+
clone the repo, you can update the git submodule with following commands:
8+
9+
```sh
10+
git submodule update --init --recursive
11+
```
12+
13+
### **3. Install dependencies**
14+
15+
For dependencies, it varies from distribution to distribution. You can refer to shell.nix and dockerfile for installation.
16+
17+
On Ubuntu, you may run `make install` or
18+
19+
```sh
20+
sudo apt-get install -y --no-install-recommends \
21+
libelf1 libelf-dev zlib1g-dev \
22+
make clang llvm
23+
```
24+
25+
to install dependencies.
26+
27+
### **4. Build the project**
28+
29+
To build the project, run the following command:
30+
31+
```sh
32+
make build
33+
```
34+
35+
This will compile your code and create the necessary binaries. You can you the `Github Code space` or `Github Action` to build the project as well.
36+
37+
### ***Run the Project***
38+
39+
You can run the binary with:
40+
41+
```console
42+
sudo ./xdp-pktgen
43+
```
44+

src/46-xdp-test/test_udp_pkt.h

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#ifndef TEST_UDP_PKT_H
2+
#define TEST_UDP_PKT_H
3+
4+
#include <net/if.h>
5+
#include <linux/if_ether.h>
6+
#include <linux/if_packet.h>
7+
#include <linux/if_link.h>
8+
#include <linux/ipv6.h>
9+
#include <linux/ip.h>
10+
#include <linux/in6.h>
11+
#include <linux/in.h>
12+
#include <linux/udp.h>
13+
#include <arpa/inet.h> // For inet_addr()
14+
#include <string.h> // For memcpy, memset
15+
#include <bpf/bpf_endian.h>
16+
#include <linux/netdev.h>
17+
#include <assert.h>
18+
19+
#define PORT 9876
20+
#define SERVER_IP "127.0.0.1"
21+
22+
// Define packet structures first
23+
struct test_udp_packet {
24+
struct ethhdr eth;
25+
struct ipv6hdr iph;
26+
struct udphdr udp;
27+
__u8 payload[64 - sizeof(struct udphdr) - sizeof(struct ethhdr) - sizeof(struct ipv6hdr)];
28+
} __attribute__((packed));
29+
30+
struct test_udp_packet_v4 {
31+
struct ethhdr eth;
32+
struct iphdr iph;
33+
struct udphdr udp;
34+
uint8_t payload[64 - sizeof(struct udphdr) - sizeof(struct ethhdr) - sizeof(struct iphdr)];
35+
} __attribute__((packed));
36+
37+
// Helper function declarations
38+
static uint16_t ip_checksum(void *vdata, size_t length);
39+
static __be16 __calc_udp_cksum(const struct test_udp_packet *pkt);
40+
41+
// Helper function implementations
42+
static uint16_t ip_checksum(void *vdata, size_t length) {
43+
char *data = vdata;
44+
uint64_t acc = 0xffff;
45+
46+
for (size_t i = 0; i + 1 < length; i += 2) {
47+
uint16_t word;
48+
memcpy(&word, data + i, 2);
49+
acc += ntohs(word);
50+
if (acc > 0xffff) {
51+
acc -= 0xffff;
52+
}
53+
}
54+
55+
if (length & 1) {
56+
uint16_t word = 0;
57+
memcpy(&word, data + length - 1, 1);
58+
acc += ntohs(word);
59+
if (acc > 0xffff) {
60+
acc -= 0xffff;
61+
}
62+
}
63+
64+
return htons(~acc);
65+
}
66+
67+
static __be16 __calc_udp_cksum(const struct test_udp_packet *pkt) {
68+
__u32 chksum = pkt->iph.nexthdr + bpf_ntohs(pkt->iph.payload_len);
69+
int i;
70+
71+
for (i = 0; i < 8; i++) {
72+
chksum += bpf_ntohs(pkt->iph.saddr.s6_addr16[i]);
73+
chksum += bpf_ntohs(pkt->iph.daddr.s6_addr16[i]);
74+
}
75+
chksum += bpf_ntohs(pkt->udp.source);
76+
chksum += bpf_ntohs(pkt->udp.dest);
77+
chksum += bpf_ntohs(pkt->udp.len);
78+
79+
while (chksum >> 16)
80+
chksum = (chksum & 0xFFFF) + (chksum >> 16);
81+
return bpf_htons(~chksum);
82+
}
83+
84+
static struct test_udp_packet_v4 create_test_udp_packet_v4(void) {
85+
struct test_udp_packet_v4 pkt = {0};
86+
87+
// Ethernet header
88+
pkt.eth.h_proto = htons(ETH_P_IP);
89+
memcpy(pkt.eth.h_dest, (const unsigned char[]){0xb8, 0x3f, 0xd2, 0x2a, 0xe5, 0x11}, sizeof(pkt.eth.h_dest));
90+
memcpy(pkt.eth.h_source, (const unsigned char[]){0xb8, 0x3f, 0xd2, 0x2a, 0xe7, 0x69}, sizeof(pkt.eth.h_source));
91+
92+
// IPv4 header
93+
pkt.iph.version = 4;
94+
pkt.iph.ihl = 5;
95+
pkt.iph.tot_len = htons(sizeof(struct test_udp_packet_v4) - sizeof(struct ethhdr));
96+
pkt.iph.ttl = 64; // default TTL
97+
pkt.iph.protocol = IPPROTO_UDP;
98+
pkt.iph.saddr = inet_addr(SERVER_IP);
99+
pkt.iph.daddr = inet_addr(SERVER_IP);
100+
pkt.iph.check = ip_checksum(&pkt.iph, sizeof(struct iphdr));
101+
102+
// UDP header
103+
pkt.udp.source = htons(12345);
104+
pkt.udp.dest = htons(PORT);
105+
pkt.udp.len = htons(sizeof(struct udphdr) + sizeof(pkt.payload));
106+
pkt.udp.check = 0; // Optional for IPv4
107+
108+
// Payload
109+
memset(pkt.payload, 0x42, sizeof(pkt.payload));
110+
111+
return pkt;
112+
}
113+
114+
#endif // TEST_UDP_PKT_H

src/46-xdp-test/xdp-pktgen.bpf.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: MIT */
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_endian.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
SEC("xdp")
9+
int xdp_redirect_notouch(struct xdp_md *ctx)
10+
{
11+
return XDP_TX;
12+
}

0 commit comments

Comments
 (0)