Skip to content

Commit 9ce313a

Browse files
author
lec-bit
committed
env com
1 parent 4c23178 commit 9ce313a

File tree

13 files changed

+66
-61
lines changed

13 files changed

+66
-61
lines changed

bpf/include/bpf_kfunc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct bpf_mem_ptr {
1919
__u32 size;
2020
};
2121

22+
#define KMESH_MODULE_ULP_NAME "kmesh_defer"
23+
2224
extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym;
2325
extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym;
2426
extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym;
@@ -43,13 +45,13 @@ static int bpf_parse_header_msg(struct bpf_sock_addr *ctx)
4345
// The strnlen function cannot be used here, so the string is redefined.
4446
static int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz)
4547
{
46-
const char kmesh_module_name[] = "kmesh_defer";
47-
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name))
48+
const char kmesh_module_ulp_name[] = KMESH_MODULE_ULP_NAME;
49+
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_ulp_name))
4850
return -1;
4951

5052
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
5153
return bpf_setsockopt_func(
52-
&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_name, sizeof(kmesh_module_name));
54+
&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_ulp_name, sizeof(kmesh_module_ulp_name));
5355
}
5456

5557
static int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz)

bpf/kmesh/ads/cgroup_sock.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
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 inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
2323
{
2424
int ret;
25-
char kmesh_module_name_get[KMESH_MODULE_NAME_LEN] = "";
25+
char kmesh_module_ulp_name_get[KMESH_MODULE_NAME_LEN] = "";
2626
Listener__Listener *listener = NULL;
2727

2828
if (ctx->protocol != IPPROTO_TCP)
@@ -41,9 +41,10 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
4141
BPF_LOG(DEBUG, KMESH, "bpf find listener addr=[%s:%u]\n", ip2str(&ip, 1), bpf_ntohs(ctx->user_port));
4242

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

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: 1 addition & 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.

hack/gen_bpf_specs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ func main() {
167167
importPath := filepath.ToSlash(filepath.Join(modulePrefix, "bpf", "kmesh", "bpf2go", real))
168168
pi := pkgInfo{Alias: alias, ImportPath: importPath, OutputDir: real, Entries: list}
169169
pkgsDefault = append(pkgsDefault, pi)
170-
pkgsEnhanced = append(pkgsEnhanced, pi)
170+
171+
if !strings.HasPrefix(real, "dualengine") {
172+
pkgsEnhanced = append(pkgsEnhanced, pi)
173+
}
171174
}
172175
}
173176
}

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 kmesh_func.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: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
#include "defer_connect.h"
2121

22+
#define KMESH_MODULE_ULP_NAME "kmesh_defer"
23+
2224
static struct proto *kmesh_defer_proto = NULL;
2325

24-
#ifdef KERNEL_KFUNC
26+
#if KERNEL_KFUNC
2527
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \
2628
({ \
2729
int __ret = -1; \
@@ -33,9 +35,9 @@ static struct proto *kmesh_defer_proto = NULL;
3335
__ret; \
3436
})
3537

36-
#define SET_FDEFER_CONNECT_ON(sk) (inet_set_bit(DEFER_CONNECT, sk))
37-
#define SET_FDEFER_CONNECT_OFF(sk) (inet_clear_bit(DEFER_CONNECT, sk))
38-
#define IS_DEFER_CONNECT(sk) (inet_test_bit(DEFER_CONNECT, sk))
38+
#define SET_DEFER_CONNECT_ON(sk) (inet_set_bit(DEFER_CONNECT, sk))
39+
#define SET_DEFER_CONNECT_OFF(sk) (inet_clear_bit(DEFER_CONNECT, sk))
40+
#define IS_DEFER_CONNECT(sk) (inet_test_bit(DEFER_CONNECT, sk))
3941
#else
4042
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \
4143
({ \
@@ -48,9 +50,9 @@ static struct proto *kmesh_defer_proto = NULL;
4850
__ret; \
4951
})
5052

51-
#define SET_FDEFER_CONNECT_ON(sk) (inet_sk(sk)->defer_connect = 1)
52-
#define SET_FDEFER_CONNECT_OFF(sk) (inet_sk(sk)->defer_connect = 0)
53-
#define IS_DEFER_CONNECT(sk) (inet_sk(sk)->defer_connect == 1)
53+
#define SET_DEFER_CONNECT_ON(sk) (inet_sk(sk)->defer_connect = 1)
54+
#define SET_DEFER_CONNECT_OFF(sk) (inet_sk(sk)->defer_connect = 0)
55+
#define IS_DEFER_CONNECT(sk) (inet_sk(sk)->defer_connect == 1)
5456
#endif
5557

5658
static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
@@ -74,18 +76,19 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
7476
ubase = iov->iov_base;
7577
kbuf_size = iov->iov_len;
7678
} else if (iter_is_iovec(&msg->msg_iter)) {
77-
#ifdef KERNEL_KFUNC
79+
#if KERNEL_VERISON6
7880
iov = msg->msg_iter.__iov;
79-
#else
80-
iov = msg->msg_iter.iov;
81-
#endif
8281
ubase = iov->iov_base;
8382
kbuf_size = iov->iov_len;
84-
#if ITER_TYPE_IS_UBUF
8583
} else if (iter_is_ubuf(&msg->msg_iter)) {
8684
ubase = msg->msg_iter.ubuf;
8785
kbuf_size = msg->msg_iter.count;
86+
#else
87+
iov = msg->msg_iter.iov;
88+
ubase = iov->iov_base;
89+
kbuf_size = iov->iov_len;
8890
#endif
91+
8992
} else
9093
goto connect;
9194

@@ -117,7 +120,7 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
117120
inet_sk(sk)->inet_dport = 0;
118121
goto out;
119122
}
120-
SET_FDEFER_CONNECT_OFF(sk);
123+
SET_DEFER_CONNECT_OFF(sk);
121124

122125
if ((((__u32)1 << sk->sk_state) & ~(__u32)(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && !tcp_passive_fastopen(sk)) {
123126
sk_stream_wait_connect(sk, &timeo);
@@ -169,7 +172,7 @@ static int defer_tcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_l
169172
*/
170173
if (IS_DEFER_CONNECT(sk))
171174
return tcp_v4_connect(sk, uaddr, addr_len);
172-
SET_FDEFER_CONNECT_ON(sk);
175+
SET_DEFER_CONNECT_ON(sk);
173176
sk->sk_dport = ((struct sockaddr_in *)uaddr)->sin_port;
174177
sk_daddr_set(sk, ((struct sockaddr_in *)uaddr)->sin_addr.s_addr);
175178
sk->sk_socket->state = SS_CONNECTING;
@@ -192,7 +195,7 @@ static int kmesh_defer_init(struct sock *sk)
192195
}
193196

194197
static struct tcp_ulp_ops kmesh_defer_ulp_ops __read_mostly = {
195-
.name = "kmesh_defer",
198+
.name = KMESH_MODULE_ULP_NAME,
196199
.owner = THIS_MODULE,
197200
.init = kmesh_defer_init,
198201
};

kernel/ko_src/kmesh/kmesh_func.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
#include <linux/string.h>
1111
#include "kmesh_func.h"
1212

13-
#ifdef KERNEL_KFUNC
13+
#define HOST_KEY_LEN 5
14+
15+
#if KERNEL_KFUNC
1416
__diag_push();
1517
__diag_ignore_all("-Wmissing-prototypes", "Global functions as their definitions will be in BTF");
1618

1719
__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
1820
{
1921
struct bpf_sock_addr_kern *sa_kern = ctx;
20-
int subptr__sz = 5;
22+
int subptr__sz = HOST_KEY_LEN;
2123
return bpf_km_header_strnstr_impl(ctx, key, key__sz, subptr, subptr__sz);
2224
}
2325

kmesh_compile_env_pre.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,17 @@ function set_enhanced_kernel_env() {
143143
if [ ! -f "$KERNEL_HEADER_LINUX_BPF" ]; then
144144
export KERNEL_HEADER_LINUX_BPF=/usr/include/linux/bpf.h
145145
fi
146-
146+
147147
KERNEL_MAJOR=$(uname -r | awk -F '.' '{print $1}')
148148

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"
149153
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF ||
150-
[ "$(check_config "CONFIG_DEBUG_INFO_BTF_MODULES")" == "y" ] &&
151-
[ "$(check_config "CONFIG_DEBUG_INFO_BTF")" == "y" ] &&
152-
[ "$KERNEL_MAJOR" -ge 6 ]; then
154+
[ "$(check_config "CONFIG_DEBUG_INFO_BTF_MODULES")" == "y" ] &&
155+
[ "$(check_config "CONFIG_DEBUG_INFO_BTF")" == "y" ] &&
156+
[ "$KERNEL_MAJOR" -ge 6 ]; then
153157
export ENHANCED_KERNEL="enhanced"
154158
else
155159
export ENHANCED_KERNEL="normal"

kmesh_macros_env.sh

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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+
5+
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1)
56

67
# MDA_LOOPBACK_ADDR
78
if grep -q "FN(get_netns_cookie)" $KERNEL_HEADER_LINUX_BPF; then
@@ -24,20 +25,6 @@ else
2425
set_config MDA_GID_UID_FILTER 0
2526
fi
2627

27-
# ITER_TYPE_IS_UBUF
28-
if [ "$VERSION" -ge 6 ]; then
29-
set_config ITER_TYPE_IS_UBUF 1
30-
else
31-
set_config ITER_TYPE_IS_UBUF 0
32-
fi
33-
34-
# ENHANCED_KERNEL
35-
if grep -q "FN(parse_header_msg)" $KERNEL_HEADER_LINUX_BPF; then
36-
set_config ENHANCED_KERNEL 1
37-
else
38-
set_config ENHANCED_KERNEL 0
39-
fi
40-
4128
# Determine libbpf version
4229
if command -v apt >/dev/null; then
4330
LIBBPF_VERSION=$(ls /usr/lib/x86_64-linux-gnu | grep -P 'libbpf\.so\.\d+\.\d+\.\d+$' | sed -n -e 's/^.*libbpf.so.\(.*\)$/\1/p')
@@ -50,4 +37,3 @@ if [[ $LIBBPF_VERSION < "0.6.0" ]]; then
5037
else
5138
set_config LIBBPF_HIGHER_0_6_0_VERSION 1
5239
fi
53-

0 commit comments

Comments
 (0)