Skip to content

Commit 4d7ddf0

Browse files
author
lec-bit
committed
opt
Signed-off-by: lec-bit <glfhzmy@126.com>
1 parent 5ef0990 commit 4d7ddf0

File tree

10 files changed

+92
-60
lines changed

10 files changed

+92
-60
lines changed

bpf/include/common.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define _COMMON_H_
66

77
#include "../../config/kmesh_marcos_def.h"
8+
#include <linux/in.h>
89
#include <stddef.h>
910
#include <stdbool.h>
1011
#include <stdint.h>
@@ -17,7 +18,17 @@
1718
#include "errno.h"
1819

1920
#if ENHANCED_KERNEL
20-
#include <bpf_helper_defs_ext.h>
21+
#if KERNEL_KFUNC
22+
#include "bpf_kfunc.h"
23+
#else
24+
struct bpf_mem_ptr {
25+
void *ptr;
26+
__u32 size;
27+
};
28+
#include "bpf_helper_defs_ext.h"
29+
#define bpf_km_setsockopt bpf_setsockopt
30+
#define bpf_km_getsockopt bpf_getsockopt
31+
#endif
2132
#endif
2233

2334
#define bpf_unused __attribute__((__unused__))

bpf/kmesh/ads/cgroup_sock.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
#if ENHANCED_KERNEL
1616
#include "route_config.h"
17+
static const char kmesh_module_ulp_name[] = KMESH_MODULE_ULP_NAME;
1718
#endif
1819
#if KMESH_ENABLE_IPV4
1920
#if KMESH_ENABLE_HTTP
2021

21-
static const char kmesh_module_name[] = "kmesh_defer";
2222
static char kmesh_module_name_get[KMESH_MODULE_NAME_LEN] = "";
2323
static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
2424
{
@@ -42,9 +42,10 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
4242
BPF_LOG(DEBUG, KMESH, "bpf find listener addr=[%s:%u]\n", ip2str(&ip, 1), bpf_ntohs(ctx->user_port));
4343

4444
#if ENHANCED_KERNEL
45-
ret = bpf_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name_get, KMESH_MODULE_NAME_LEN);
46-
if (CHECK_MODULE_NAME_NULL(ret) || bpf__strncmp(kmesh_module_name_get, KMESH_MODULE_NAME_LEN, kmesh_module_name)) {
47-
ret = bpf_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name, sizeof(kmesh_module_name));
45+
ret = bpf_km_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_ulp_name_get, KMESH_MODULE_NAME_LEN);
46+
if (CHECK_MODULE_NAME_NULL(ret)
47+
|| bpf__strncmp(kmesh_module_ulp_name_get, KMESH_MODULE_NAME_LEN, kmesh_module_ulp_name)) {
48+
ret = bpf_km_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_ulp_name, sizeof(kmesh_module_ulp_name));
4849
if (ret)
4950
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret %d\n", ret);
5051
return 0;

bpf/kmesh/ads/include/kmesh_common.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
val; \
3232
})
3333

34-
struct bpf_mem_ptr {
35-
void *ptr;
36-
__u32 size;
37-
};
38-
3934
static inline int bpf__strncmp(const char *dst, int n, const char *src)
4035
{
4136
if (dst == NULL || src == NULL)

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ if [ "$1" == "-h" -o "$1" == "--help" ]; then
4343
fi
4444

4545
if [ -z "$1" -o "$1" == "-b" -o "$1" == "--build" ]; then
46+
bash kmesh_macros_env_kernel.sh
4647
prepare
4748
make
4849
exit

config/kmesh_marcos_def.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* in kernel 6.x version, add the new iter type ITER_UBUF, and we need add code
2929
* for the corresponding scenarios.
3030
*/
31-
#define ITER_TYPE_IS_UBUF 0
31+
#define KERNEL_VERISON6 1
3232

3333
/*
3434
* Kmesh’s Layer 7 acceleration proxy capability relies on kernel enhancements.
@@ -46,3 +46,8 @@
4646
* is enabled accordingly.
4747
* */
4848
#define LIBBPF_HIGHER_0_6_0_VERSION 0
49+
50+
/*
51+
* Determine whether the current kernel version supports the use of kfunc.
52+
*/
53+
#define KERNEL_KFUNC 0

kernel/ko_src/kmesh/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ kmesh-objs = kmesh_main.o defer_connect.o \
1313
kmesh_parse_http_1_1.o
1414

1515
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
16+
VMLINUX_PATH := /sys/kernel/btf/vmlinux
1617
PWD := $(shell pwd)
1718

1819
ccflags-y += -Wno-discarded-qualifiers
1920

2021
all:
21-
$(MAKE) -C $(KERNELDIR) M=$(PWD)
22+
$(MAKE) -C $(KERNELDIR) M=$(PWD) KBUILD_VMLINUX_LIBS=$(VMLINUX_PATH) modules
2223

2324
install:
2425
install -dp -m 0500 /lib/modules/kmesh

kernel/ko_src/kmesh/defer_connect.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
#include "defer_connect.h"
2222

23+
#define KMESH_MODULE_ULP_NAME "kmesh_defer"
24+
2325
static struct proto *kmesh_defer_proto = NULL;
24-
#define KMESH_DELAY_ERROR -1000
2526

2627
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, t_ctx) \
2728
({ \
@@ -54,13 +55,17 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
5455
ubase = iov->iov_base;
5556
kbuf_size = iov->iov_len;
5657
} else if (iter_is_iovec(&msg->msg_iter)) {
57-
iov = msg->msg_iter.iov;
58+
#if KERNEL_VERISON6
59+
iov = msg->msg_iter.__iov;
5860
ubase = iov->iov_base;
5961
kbuf_size = iov->iov_len;
60-
#if ITER_TYPE_IS_UBUF
6162
} else if (iter_is_ubuf(&msg->msg_iter)) {
6263
ubase = msg->msg_iter.ubuf;
6364
kbuf_size = msg->msg_iter.count;
65+
#else
66+
iov = msg->msg_iter.iov;
67+
ubase = iov->iov_base;
68+
kbuf_size = iov->iov_len;
6469
#endif
6570
} else
6671
goto connect;

kmesh_compile_env_pre.sh

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function dependency_pkg_install() {
1515
if command -v apt >/dev/null; then
1616
echo "Checking for required packages on a Debian-based system..."
1717

18-
packages=(git make clang libbpf-dev llvm linux-tools-generic protobuf-compiler libprotobuf-dev libprotobuf-c-dev protobuf-c-compiler cmake pkg-config gcc-multilib)
18+
packages=(git make clang libbpf-dev llvm linux-tools-generic protobuf-compiler libprotobuf-dev libprotobuf-c-dev protobuf-c-compiler cmake pkg-config gcc-multilib pahole linux-headers)
1919

2020
update_needed=false
2121
for pkg in "${packages[@]}"; do
@@ -47,7 +47,7 @@ function dependency_pkg_install() {
4747
echo "Checking for required packages on a Red Hat-based system..."
4848

4949
# List of required packages
50-
packages=(git make clang llvm libboundscheck protobuf protobuf-c protobuf-c-devel bpftool libbpf libbpf-devel cmake pkg-config glibc-devel libstdc++-devel)
50+
packages=(git make clang llvm libboundscheck protobuf protobuf-c protobuf-c-devel bpftool libbpf libbpf-devel cmake pkg-config glibc-devel libstdc++-devel dwarves)
5151

5252
# Install each missing package
5353
for pkg in "${packages[@]}"; do
@@ -104,6 +104,32 @@ function kmesh_set_env() {
104104
export EXTRA_CFLAGS="-O0 -g"
105105
}
106106

107+
function set_config() {
108+
sed -i -r -e "s/($1)([ \t]*)([0-9]+)/\1\2$2/" config/kmesh_marcos_def.h
109+
}
110+
111+
detect_config() {
112+
local kernel_version=$(uname -r)
113+
114+
if [ -f "/proc/config.gz" ]; then
115+
zcat /proc/config.gz 2>/dev/null
116+
return $?
117+
fi
118+
119+
if [ -f "/boot/config-$kernel_version" ]; then
120+
cat "/boot/config-$kernel_version" 2>/dev/null
121+
return $?
122+
fi
123+
}
124+
125+
CONFIG_CONTENT=$(detect_config)
126+
127+
check_config() {
128+
local config_name=$1
129+
value=$(echo "$CONFIG_CONTENT" | grep -w "$config_name" | cut -d= -f2)
130+
echo "$value"
131+
}
132+
107133
function set_enhanced_kernel_env() {
108134
# we use /usr/include/linux/bpf.h to determine the runtime environment’s
109135
# support for kmesh. Considering the case of online image compilation, a
@@ -118,8 +144,17 @@ function set_enhanced_kernel_env() {
118144
export KERNEL_HEADER_LINUX_BPF=/usr/include/linux/bpf.h
119145
fi
120146

121-
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF; then
122-
export ENHANCED_KERNEL="enhanced"
147+
KERNEL_MAJOR=$(uname -r | awk -F '.' '{print $1}')
148+
149+
zcat /proc/config.gz | grep BTF
150+
cat "/boot/config-$(uname -r)" | grep BTF
151+
check_config "CONFIG_DEBUG_INFO_BTF_MODULES"
152+
check_config "CONFIG_DEBUG_INFO_BTF"
153+
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF ||
154+
[ "$(check_config "CONFIG_DEBUG_INFO_BTF_MODULES")" == "y" ] &&
155+
[ "$(check_config "CONFIG_DEBUG_INFO_BTF")" == "y" ] &&
156+
[ "$KERNEL_MAJOR" -ge 6 ]; then
157+
export ENHANCED_KERNEL="normal"
123158
else
124159
export ENHANCED_KERNEL="normal"
125160
fi

kmesh_macros_env.sh

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#!/bin/bash
22

3-
VERSION=$(uname -r | cut -d '.' -f 1)
4-
KERNEL_VERSION=$(uname -r | cut -d '-' -f 1)
5-
6-
function set_config() {
7-
sed -i -r -e "s/($1)([ \t]*)([0-9]+)/\1\2$2/" config/kmesh_marcos_def.h
8-
}
3+
source ./kmesh_compile_env_pre.sh
4+
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1)
95

106
# MDA_LOOPBACK_ADDR
117
if grep -q "FN(get_netns_cookie)" $KERNEL_HEADER_LINUX_BPF; then
@@ -28,27 +24,6 @@ else
2824
set_config MDA_GID_UID_FILTER 0
2925
fi
3026

31-
# OE_23_03
32-
if (uname -r | grep oe2303); then
33-
set_config OE_23_03 1
34-
else
35-
set_config OE_23_03 0
36-
fi
37-
38-
# ITER_TYPE_IS_UBUF
39-
if [ "$VERSION" -ge 6 ]; then
40-
set_config ITER_TYPE_IS_UBUF 1
41-
else
42-
set_config ITER_TYPE_IS_UBUF 0
43-
fi
44-
45-
# ENHANCED_KERNEL
46-
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF; then
47-
set_config ENHANCED_KERNEL 1
48-
else
49-
set_config ENHANCED_KERNEL 0
50-
fi
51-
5227
# Determine libbpf version
5328
if command -v apt >/dev/null; then
5429
LIBBPF_VERSION=$(ls /usr/lib/x86_64-linux-gnu | grep -P 'libbpf\.so\.\d+\.\d+\.\d+$' | sed -n -e 's/^.*libbpf.so.\(.*\)$/\1/p')

kmesh_macros_env_kernel.sh

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#!/bin/bash
22

3-
VERSION=$(uname -r | cut -d '.' -f 1)
4-
KERNEL_VERSION=$(uname -r | cut -d '-' -f 1)
3+
source ./kmesh_compile_env_pre.sh
4+
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1)
55
KERNEL_HEADER_LINUX_BPF=/usr/include/linux/bpf.h
66

7-
function set_config() {
8-
sed -i -r -e "s/($1)([ \t]*)([0-9]+)/\1\2$2/" config/kmesh_marcos_def.h
9-
}
10-
117
# MDA_LOOPBACK_ADDR
128
if grep -q "FN(get_netns_cookie)" $KERNEL_HEADER_LINUX_BPF; then
139
set_config MDA_LOOPBACK_ADDR 1
@@ -29,24 +25,31 @@ else
2925
set_config MDA_GID_UID_FILTER 0
3026
fi
3127

32-
# ITER_TYPE_IS_UBUF
33-
if [ "$VERSION" -ge 6 ]; then
34-
set_config ITER_TYPE_IS_UBUF 1
28+
# KERNEL_VERISON6
29+
if [ "$KERNEL_VERSION" -ge 6 ]; then
30+
set_config KERNEL_VERISON6 1
3531
else
36-
set_config ITER_TYPE_IS_UBUF 0
32+
set_config KERNEL_VERISON6 0
3733
fi
3834

3935
# ENHANCED_KERNEL
4036
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF; then
41-
set_config ENHANCED_KERNEL 1
37+
set_config ENHANCED_KERNEL 0
4238
else
4339
set_config ENHANCED_KERNEL 0
4440
fi
4541

42+
zcat /proc/config.gz | grep BTF
43+
cat "/boot/config-$(uname -r)" | grep BTF
4644
# KERNEL_KFUNC
47-
if [ "$VERSION" -ge 6 ]; then
48-
set_config ENHANCED_KERNEL 1
49-
set_config KERNEL_KFUNC 1
45+
if [ "$(check_config "CONFIG_DEBUG_INFO_BTF_MODULES")" == "y" ] &&
46+
[ "$(check_config "CONFIG_DEBUG_INFO_BTF")" == "y" ] &&
47+
[ "$KERNEL_VERSION" -ge 6 ]; then
48+
set_config ENHANCED_KERNEL 0
49+
set_config KERNEL_KFUNC 0
50+
cp /sys/kernel/btf/vmlinux /lib/modules/$(uname -r)/build
5051
else
5152
set_config KERNEL_KFUNC 0
5253
fi
54+
55+
cat config/kmesh_marcos_def.h

0 commit comments

Comments
 (0)