Skip to content

Commit 9f824eb

Browse files
author
lec-bit
committed
adapt 6.6
Signed-off-by: lec-bit <[email protected]>
1 parent 44b2525 commit 9f824eb

File tree

20 files changed

+336
-117
lines changed

20 files changed

+336
-117
lines changed

bpf/include/common.h

Lines changed: 54 additions & 7 deletions
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>
@@ -16,8 +17,60 @@
1617

1718
#include "errno.h"
1819

20+
struct bpf_mem_ptr {
21+
void *ptr;
22+
__u32 size;
23+
};
24+
1925
#if ENHANCED_KERNEL
26+
#if KERNEL_KFUNC
27+
extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym;
28+
extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym;
29+
extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym;
30+
extern int bpf_setsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, const char *optval, int optval__sz) __ksym;
31+
extern int bpf_getsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, char *optval, int optval__sz) __ksym;
32+
33+
#define bpf_km_header_strncmp bpf_km_header_strncmp_func
34+
35+
int bpf_km_header_strnstr(void *ctx, const char *key, int key__sz, const char *subptr, int subptr__sz)
36+
{
37+
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
38+
return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr);
39+
}
40+
41+
int bpf_parse_header_msg(struct bpf_sock_addr *ctx)
42+
{
43+
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
44+
return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr));
45+
}
46+
47+
// Due to the limitation of bpf verifier, optval and optval__sz are required to correspond.
48+
// The strnlen function cannot be used here, so the string is redefined.
49+
int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz)
50+
{
51+
const char kmesh_module_name[] = "kmesh_defer";
52+
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name))
53+
return -1;
54+
55+
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
56+
return bpf_setsockopt_func(
57+
&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_name, sizeof(kmesh_module_name));
58+
}
59+
60+
int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz)
61+
{
62+
if (level != IPPROTO_TCP) {
63+
return -1;
64+
}
65+
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
66+
return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz);
67+
}
68+
69+
#else
2070
#include <bpf_helper_defs_ext.h>
71+
#define bpf_km_setsockopt bpf_setsockopt
72+
#define bpf_km_getsockopt bpf_getsockopt
73+
#endif
2174
#endif
2275

2376
#define bpf_unused __attribute__((__unused__))
@@ -113,14 +166,8 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4])
113166
(dst)[3] = (src)[3]; \
114167
} while (0)
115168

116-
#if OE_23_03
117-
#define bpf__strncmp bpf_strncmp
118-
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port)
119-
#else
120169
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16)
121-
#endif
122-
123-
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
170+
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
124171

125172
#define MAX_BUF_LEN 100
126173
#define MAX_IP4_LEN 16

bpf/kmesh/ads/cgroup_sock.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
#if KMESH_ENABLE_HTTP
2020

2121
static const char kmesh_module_name[] = "kmesh_defer";
22-
static char kmesh_module_name_get[KMESH_MODULE_NAME_LEN] = "";
2322
static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
2423
{
2524
int ret;
26-
25+
char kmesh_module_name_get[KMESH_MODULE_NAME_LEN] = "";
2726
Listener__Listener *listener = NULL;
2827

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

4443
#if ENHANCED_KERNEL
45-
ret = bpf_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name_get, KMESH_MODULE_NAME_LEN);
44+
ret = bpf_km_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_name_get, KMESH_MODULE_NAME_LEN);
4645
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));
46+
ret = bpf_km_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_name, sizeof(kmesh_module_name));
4847
if (ret)
4948
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret %d\n", ret);
5049
return 0;

bpf/kmesh/ads/include/ctx/sock_ops.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ typedef struct bpf_sock_ops ctx_buff_t;
2222
name.ipv4 = (ctx)->remote_ip4; \
2323
name.port = (ctx)->remote_port
2424

25-
#if OE_23_03
26-
#define SET_CTX_ADDRESS(ctx, address) \
27-
(ctx)->remote_ip4 = (address)->ipv4; \
28-
(ctx)->remote_port = (address)->port
29-
30-
#define MARK_REJECTED(ctx) \
31-
BPF_LOG(DEBUG, KMESH, "mark reject\n"); \
32-
(ctx)->remote_ip4 = 0; \
33-
(ctx)->remote_port = 0
34-
#else
3525
#define SET_CTX_ADDRESS(ctx, address) \
3626
(ctx)->replylong[2] = (address)->ipv4; \
3727
(ctx)->replylong[3] = (address)->port
@@ -40,6 +30,5 @@ typedef struct bpf_sock_ops ctx_buff_t;
4030
BPF_LOG(DEBUG, KMESH, "mark reject\n"); \
4131
(ctx)->replylong[2] = 0; \
4232
(ctx)->replylong[3] = 0
43-
#endif
4433

4534
#endif //__BPF_CTX_SOCK_OPS_H

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)

bpf/kmesh/bpf2go/kernelnative/enhanced/kmeshsockops_bpfeb.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/kmesh/bpf2go/kernelnative/enhanced/kmeshsockopscompat_bpfel.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ if [ -z "$1" -o "$1" == "-b" -o "$1" == "--build" ]; then
4848
exit
4949
fi
5050

51+
if [ "$1" == "-d" -o "$1" == "--docker" ]; then
52+
prepare
53+
make kmesh-bpf
54+
make all-binary
55+
install
56+
exit
57+
fi
58+
5159
if [ "$1" == "-i" -o "$1" == "--install" ]; then
5260
make install
5361
install

config/kmesh_marcos_def.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,6 @@
2424
*/
2525
#define MDA_GID_UID_FILTER 1
2626

27-
/*
28-
* openEuler-23.03 is an innovative version of openEuler, in the early time, we
29-
* developed kmesh based on openEuler-23.03, and the implementation of kmesh
30-
* was related to the openEuler-23.03 kernel. Now, the general implementation
31-
* of kmesh differs from the previous openEuler-23.03 version, so we need to
32-
* use this macro to distinguish these differences.
33-
* The main differences between the general implementation of kmesh and the
34-
* openEuler-23.03 version are as follows:
35-
* 1. Use replylong parameter instead of directly modifying the remote IP and Port;
36-
* 2. Use bpf__strncmp instead of bpf_strncmp for string comparison;
37-
* 3. Fix Port shift bug on openEuler-23.03.In the kernel network protocol
38-
* stack, the port is stored in u16, but in the bpf network module, the port
39-
* is stored in u32. Therefore, after the endian conversion, the 16-bit port
40-
* needs to be obtained from the 32-bit data structure.
41-
* You need to find the position of the valid 16 bits. Generally, after the
42-
* port is extended from 16 bits to 32 bits, the port is in the upper 16
43-
* bits after the endian conversion. Therefore, you need to offset the port
44-
* before using the u16 RX port. In some specific kernels, the port stored
45-
* in sockops is in the lower 16 bits and does not need to be offset.
46-
*/
47-
#define OE_23_03 0
48-
4927
/*
5028
* in kernel 6.x version, add the new iter type ITER_UBUF, and we need add code
5129
* for the corresponding scenarios.
@@ -68,3 +46,8 @@
6846
* is enabled accordingly.
6947
* */
7048
#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

hack/utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function get_arch() {
3030
function build_kmesh() {
3131
local container_id=$1
3232
docker exec $container_id git config --global --add safe.directory /kmesh
33-
docker exec -e VERSION=$VERSION $container_id sh /kmesh/build.sh
33+
docker exec -e VERSION=$VERSION $container_id sh /kmesh/build.sh -d
3434
docker exec -e VERSION=$VERSION $container_id sh /kmesh/build.sh -i
3535
docker exec $container_id sh -c "$(declare -f copy_to_host); copy_to_host"
3636
}

kernel/ko_src/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ CURRENT_PATH := $(shell pwd)
22
DIRS := $(shell find $(CURRENT_PATH) -maxdepth 1 -type d)
33
BASE_DIRS := $(basename $(patsubst $(CURRENT_PATH)/%, %, $(DIRS)))
44
BASE_DIRS := $(filter-out $(CURRENT_PATH), $(BASE_DIRS))
5+
CONFIG_FILE := ../../config/kmesh_marcos_def.h
6+
ENHANCED_KERNEL := $(shell grep -q "#define ENHANCED_KERNEL 1" $(CONFIG_FILE) && echo yes || echo no)
57

6-
ifeq ($(ENHANCED_KERNEL), enhanced)
8+
ifeq ($(ENHANCED_KERNEL), yes)
79
all:
810
@for dir in ${BASE_DIRS}; do \
911
make -C $(CURRENT_PATH)/$$dir; \

0 commit comments

Comments
 (0)