Skip to content

Commit 9ef7d96

Browse files
author
lec-bit
committed
adapt 6.6
Signed-off-by: lec-bit <[email protected]>
1 parent 2a8ca91 commit 9ef7d96

18 files changed

+281
-125
lines changed

bpf/include/common.h

Lines changed: 55 additions & 8 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__))
@@ -121,14 +174,8 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4])
121174
(dst)[3] = (src)[3]; \
122175
} while (0)
123176

124-
#if OE_23_03
125-
#define bpf__strncmp bpf_strncmp
126-
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port)
127-
#else
128177
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16)
129-
#endif
130-
131-
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
178+
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
132179

133180
#define MAX_BUF_LEN 100
134181
#define MAX_IP4_LEN 16
@@ -244,7 +291,7 @@ static inline int convert_v6(char *data, __u32 *ip6)
244291
__u16 ip_1 = (ip >> 0) & 0xFFFF;
245292
__u16 ip_2 = (ip >> 16) & 0xFFFF;
246293
for (int j = 0; j < 2; j++) {
247-
__u16 ip_1 = (ip)&0xFFFF;
294+
__u16 ip_1 = (ip) & 0xFFFF;
248295
__u8 h_1 = (ip_1 >> 0) & 0xFF;
249296
__u8 h_2 = (ip_1 >> 8) & 0xFF;
250297
*data++ = hex_digits[(h_1 >> 4) & 0xF];

bpf/include/inner_map_defs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
typedef enum { MAP_TYPE_64, MAP_TYPE_192, MAP_TYPE_296, MAP_TYPE_1600, MAP_TYPE_MAX } map_in_map_type;
1111

1212
#define MAP_GET_TYPE(idx) (__u8)((__u32)(idx) >> 24)
13-
#define MAP_GET_INDEX(idx) (__u32)((__u32)(idx)&0xFFFFFF)
14-
#define MAP_GEN_OUTER_KEY(map_type, pos) ((__u32)((((__u8)(map_type)&0xFF) << 24) + ((__u32)(pos)&0xFFFFFF)))
13+
#define MAP_GET_INDEX(idx) (__u32)((__u32)(idx) & 0xFFFFFF)
14+
#define MAP_GEN_OUTER_KEY(map_type, pos) ((__u32)((((__u8)(map_type) & 0xFF) << 24) + ((__u32)(pos) & 0xFFFFFF)))
1515

1616
#define MAP_VAL_SIZE_64 64
1717
#define MAP_VAL_SIZE_192 192

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: 1 addition & 6 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)
@@ -84,7 +79,7 @@ enum kmesh_l7_msg_type { MSG_UNKNOW = 0, MSG_REQUEST, MSG_MID_REPONSE, MSG_FINAL
8479
enum kmesh_strncmp_type { STRNCMP_FAILED = 0, STRNCMP_PREFIX, STRNCMP_EXACT };
8580

8681
#define KMESH_PROTO_TYPE_WIDTH (8)
87-
#define GET_RET_PROTO_TYPE(n) ((n)&0xff)
82+
#define GET_RET_PROTO_TYPE(n) ((n) & 0xff)
8883
#define GET_RET_MSG_TYPE(n) (((n) >> KMESH_PROTO_TYPE_WIDTH) & 0xff)
8984

9085
#define CHECK_MODULE_NAME_NULL(ret) ((ret) == -EINVAL)

build/docker/builder.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#
66

77
# base image
8-
FROM openeuler/openeuler:23.09
8+
FROM openeuler/openeuler:24.03
99

1010
# Setup Go
11-
COPY --from=golang:1.23.2 /usr/local/go/ /usr/local/go/
11+
COPY --from=golang:latest /usr/local/go/ /usr/local/go/
1212
RUN mkdir -p /go
1313
ENV GOROOT /usr/local/go
1414
ENV GOPATH /go

build/docker/dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Usage:
22
# docker run -itd --privileged=true -v /etc/cni/net.d:/etc/cni/net.d -v /opt/cni/bin:/opt/cni/bin -v /mnt:/mnt -v /sys/fs/bpf:/sys/fs/bpf -v /lib/modules:/lib/modules --name kmesh kmesh:latest
33
#
4-
FROM openeuler/openeuler:23.09
4+
FROM openeuler/openeuler:24.03
55

66
WORKDIR /kmesh
77

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

kernel/ko_src/kmesh/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
obj-m := kmesh.o
1111
kmesh-objs = kmesh_main.o defer_connect.o \
1212
kmesh_parse_protocol_data.o \
13-
kmesh_parse_http_1_1.o
13+
kmesh_parse_http_1_1.o kmesh_func.o
1414

1515
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
1616
PWD := $(shell pwd)

kernel/ko_src/kmesh/defer_connect.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
22
/* Copyright Authors of Kmesh */
33

4-
#include "../../../config/kmesh_marcos_def.h"
54
#include <linux/init.h>
65
#include <linux/module.h>
76
#include <linux/kmod.h>
@@ -21,9 +20,24 @@
2120
#include "defer_connect.h"
2221

2322
static struct proto *kmesh_defer_proto = NULL;
24-
#define KMESH_DELAY_ERROR -1000
2523

26-
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, t_ctx) \
24+
#ifdef KERNEL_KFUNC
25+
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \
26+
({ \
27+
int __ret = -1; \
28+
if (t_ctx == NULL) { \
29+
__ret = -EINVAL; \
30+
} else { \
31+
__ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, uaddrlen, CGROUP_INET4_CONNECT, t_ctx, NULL); \
32+
} \
33+
__ret; \
34+
})
35+
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))
39+
#else
40+
#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \
2741
({ \
2842
int __ret = -1; \
2943
if (t_ctx == NULL) { \
@@ -34,6 +48,11 @@ static struct proto *kmesh_defer_proto = NULL;
3448
__ret; \
3549
})
3650

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)
54+
#endif
55+
3756
static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
3857
{
3958
struct bpf_mem_ptr tmpMem = {0};
@@ -43,6 +62,7 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
4362
const struct iovec *iov;
4463
struct bpf_sock_addr_kern sock_addr;
4564
struct sockaddr_in uaddr;
65+
int uaddrlen = sizeof(struct sockaddr_in);
4666
void __user *ubase;
4767
int err;
4868
u32 dport, daddr;
@@ -54,7 +74,11 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
5474
ubase = iov->iov_base;
5575
kbuf_size = iov->iov_len;
5676
} else if (iter_is_iovec(&msg->msg_iter)) {
77+
#ifdef KERNEL_KFUNC
78+
iov = msg->msg_iter.__iov;
79+
#else
5780
iov = msg->msg_iter.iov;
81+
#endif
5882
ubase = iov->iov_base;
5983
kbuf_size = iov->iov_len;
6084
#if ITER_TYPE_IS_UBUF
@@ -79,31 +103,11 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
79103
tmpMem.size = kbuf_size;
80104
tmpMem.ptr = kbuf;
81105

82-
#if OE_23_03
83-
tcp_call_bpf_3arg(
84-
sk,
85-
BPF_SOCK_OPS_TCP_DEFER_CONNECT_CB,
86-
((u64)(&tmpMem) & U32_MAX),
87-
(((u64)(&tmpMem) >> 32) & U32_MAX),
88-
kbuf_size);
89-
daddr = sk->sk_daddr;
90-
dport = sk->sk_dport;
91-
92-
// daddr == 0 && dport == 0 are special flags meaning the circuit breaker is open
93-
// Should reject connection here
94-
if (daddr == 0 && dport == 0) {
95-
tcp_set_state(sk, TCP_CLOSE);
96-
sk->sk_route_caps = 0;
97-
inet_sk(sk)->inet_dport = 0;
98-
err = -1;
99-
goto out;
100-
}
101-
#else
102106
uaddr.sin_family = AF_INET;
103107
uaddr.sin_addr.s_addr = daddr;
104108
uaddr.sin_port = dport;
105-
err = BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, (struct sockaddr *)&uaddr, &tmpMem);
106-
#endif
109+
err = BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, (struct sockaddr *)&uaddr, &uaddrlen, &tmpMem);
110+
107111
connect:
108112
err = sk->sk_prot->connect(sk, (struct sockaddr *)&uaddr, sizeof(struct sockaddr_in));
109113
if (unlikely(err)) {
@@ -113,7 +117,7 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size)
113117
inet_sk(sk)->inet_dport = 0;
114118
goto out;
115119
}
116-
inet_sk(sk)->defer_connect = 0;
120+
SET_FDEFER_CONNECT_OFF(sk);
117121

118122
if ((((__u32)1 << sk->sk_state) & ~(__u32)(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && !tcp_passive_fastopen(sk)) {
119123
sk_stream_wait_connect(sk, &timeo);
@@ -128,7 +132,7 @@ static int defer_connect_and_sendmsg(struct sock *sk, struct msghdr *msg, size_t
128132
struct socket *sock;
129133
int err = 0;
130134

131-
if (unlikely(inet_sk(sk)->defer_connect == 1)) {
135+
if (unlikely(IS_DEFER_CONNECT(sk))) {
132136
lock_sock(sk);
133137

134138
err = defer_connect(sk, msg, size);
@@ -163,9 +167,9 @@ static int defer_tcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_l
163167
* of defer_connect should be 1 and the normal connect function
164168
* needs to be used.
165169
*/
166-
if (inet_sk(sk)->defer_connect)
170+
if (IS_DEFER_CONNECT(sk))
167171
return tcp_v4_connect(sk, uaddr, addr_len);
168-
inet_sk(sk)->defer_connect = 1;
172+
SET_FDEFER_CONNECT_ON(sk);
169173
sk->sk_dport = ((struct sockaddr_in *)uaddr)->sin_port;
170174
sk_daddr_set(sk, ((struct sockaddr_in *)uaddr)->sin_addr.s_addr);
171175
sk->sk_socket->state = SS_CONNECTING;

0 commit comments

Comments
 (0)