Skip to content

Commit ef488e7

Browse files
author
lec-bit
committed
adapt 6.6
Signed-off-by: lec-bit <glfhzmy@126.com>
1 parent 2a8ca91 commit ef488e7

File tree

17 files changed

+283
-115
lines changed

17 files changed

+283
-115
lines changed

bpf/include/common.h

Lines changed: 58 additions & 6 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,64 @@
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+
struct bpf_mem_ptr msg_tmp = {
37+
.ptr = ctx,
38+
.size = sizeof(struct bpf_sock_addr)
39+
};
40+
return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr);
41+
}
42+
43+
int bpf_parse_header_msg(struct bpf_sock_addr *ctx) {
44+
struct bpf_mem_ptr msg_tmp = {
45+
.ptr = ctx,
46+
.size = sizeof(struct bpf_sock_addr)
47+
};
48+
return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr));
49+
}
50+
51+
int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz) {
52+
if (level != IPPROTO_TCP && optval__sz != sizeof(optval))
53+
return -1;
54+
55+
struct bpf_mem_ptr msg_tmp = {
56+
.ptr = ctx,
57+
.size = sizeof(struct bpf_sock_addr)
58+
};
59+
return bpf_setsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz);
60+
}
61+
62+
int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz) {
63+
if (level != IPPROTO_TCP) {
64+
return -1;
65+
}
66+
struct bpf_mem_ptr msg_tmp = {
67+
.ptr = ctx,
68+
.size = sizeof(struct bpf_sock_addr)
69+
};
70+
return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz);
71+
}
72+
73+
#else
2074
#include <bpf_helper_defs_ext.h>
75+
#define bpf_km_setsockopt bpf_setsockopt
76+
#define bpf_km_getsockopt bpf_getsockopt
77+
#endif
2178
#endif
2279

2380
#define bpf_unused __attribute__((__unused__))
@@ -121,13 +178,7 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4])
121178
(dst)[3] = (src)[3]; \
122179
} while (0)
123180

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
128181
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16)
129-
#endif
130-
131182
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
132183

133184
#define MAX_BUF_LEN 100
@@ -282,3 +333,4 @@ static inline char *ip2str(__u32 *ip_ptr, bool v4)
282333
}
283334

284335
#endif // _COMMON_H_
336+

bpf/kmesh/ads/cgroup_sock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ 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);
45+
ret = bpf_km_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name_get, KMESH_MODULE_NAME_LEN);
4646
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));
47+
ret = bpf_km_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_name, sizeof(kmesh_module_name));
4848
if (ret)
4949
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret %d\n", ret);
5050
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)

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: 7 additions & 23 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.
@@ -57,7 +35,7 @@
5735
* It’s necessary to determine whether the current environment has an
5836
* enhanced kernel in order to enable Kmesh’s capabilities.
5937
*/
60-
#define ENHANCED_KERNEL 0
38+
#define ENHANCED_KERNEL 1
6139

6240
/*
6341
* Different versions of libbpf can be installed in different environments,
@@ -68,3 +46,9 @@
6846
* is enabled accordingly.
6947
* */
7048
#define LIBBPF_HIGHER_0_6_0_VERSION 0
49+
50+
51+
/*
52+
* Determine whether the current kernel version supports the use of kfunc.
53+
*/
54+
#define KERNEL_KFUNC 1

kernel/ko_src/kmesh/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
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)
1717

18-
ccflags-y += -Wno-discarded-qualifiers
18+
ccflags-y += -Wno-discarded-qualifiers -DKERNEL_KFUNC
1919

2020
all:
2121
$(MAKE) -C $(KERNELDIR) M=$(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)