Skip to content

Commit 1378205

Browse files
author
lec-bit
committed
new kernel bugfix
Signed-off-by: lec-bit <glfhzmy@126.com>
1 parent f839eff commit 1378205

File tree

7 files changed

+108
-97
lines changed

7 files changed

+108
-97
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ ifeq ($(TAG),)
8282
$(error "TAG cannot be empty")
8383
endif
8484

85-
TMP_FILES := bpf/kmesh/bpf2go/bpf2go.go \
86-
config/kmesh_marcos_def.h \
85+
TMP_FILES := config/kmesh_marcos_def.h \
8786
mk/api-v2-c.pc \
8887
mk/bpf.pc \
8988
bpf/include/bpf_helper_defs_ext.h \

bpf/include/bpf_common.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,86 @@ static inline void *get_ptr_val_from_map(void *map, __u8 map_type, const void *p
223223
val_tmp; \
224224
})
225225

226+
static inline void record_kmesh_managed_ip(__u32 family, __u32 ip4, __u32 *ip6)
227+
{
228+
int err;
229+
__u32 value = 0;
230+
struct manager_key key = {0};
231+
if (family == AF_INET)
232+
key.addr.ip4 = ip4;
233+
if (family == AF_INET6 && ip6)
234+
IP6_COPY(key.addr.ip6, ip6);
235+
236+
err = bpf_map_update_elem(&map_of_manager, &key, &value, BPF_ANY);
237+
if (err)
238+
BPF_LOG(ERR, KMESH, "record ip failed!, err is %d\n", err);
239+
}
240+
241+
static inline void remove_kmesh_managed_ip(__u32 family, __u32 ip4, __u32 *ip6)
242+
{
243+
struct manager_key key = {0};
244+
if (family == AF_INET)
245+
key.addr.ip4 = ip4;
246+
if (family == AF_INET6 && ip6)
247+
IP6_COPY(key.addr.ip6, ip6);
248+
249+
int err = bpf_map_delete_elem(&map_of_manager, &key);
250+
if (err && err != -ENOENT)
251+
BPF_LOG(ERR, KMESH, "remove ip failed!, err is %d\n", err);
252+
}
253+
254+
static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u16 port)
255+
{
256+
__u16 remote_port = GET_SKOPS_REMOTE_PORT(skops);
257+
if (bpf_ntohs(remote_port) != port)
258+
return false;
259+
260+
if (skops->family == AF_INET)
261+
return (bpf_ntohl(skops->remote_ip4) == ip);
262+
263+
return (
264+
skops->remote_ip6[0] == 0 && skops->remote_ip6[1] == 0 && skops->remote_ip6[2] == 0
265+
&& bpf_ntohl(skops->remote_ip6[3]) == ip);
266+
}
267+
268+
static inline bool skops_conn_from_cni_sim_add(struct bpf_sock_ops *skops)
269+
{
270+
// cni sim connect CONTROL_CMD_IP:929(0x3a1)
271+
// 0x3a1 is the specific port handled by the cni to enable Kmesh
272+
return conn_from_sim(skops, CONTROL_CMD_IP, ENABLE_KMESH_PORT);
273+
}
274+
275+
static inline bool skops_conn_from_cni_sim_delete(struct bpf_sock_ops *skops)
276+
{
277+
// cni sim connect CONTROL_CMD_IP:930(0x3a2)
278+
// 0x3a2 is the specific port handled by the cni to disable Kmesh
279+
return conn_from_sim(skops, CONTROL_CMD_IP, DISABLE_KMESH_PORT);
280+
}
281+
282+
static inline void skops_handle_kmesh_managed_process(struct bpf_sock_ops *skops)
283+
{
284+
if (skops_conn_from_cni_sim_add(skops))
285+
record_kmesh_managed_ip(skops->family, skops->local_ip4, skops->local_ip6);
286+
if (skops_conn_from_cni_sim_delete(skops))
287+
remove_kmesh_managed_ip(skops->family, skops->local_ip4, skops->local_ip6);
288+
}
289+
290+
static inline bool is_managed_by_kmesh(struct bpf_sock_ops *skops)
291+
{
292+
struct manager_key key = {0};
293+
if (skops->family == AF_INET)
294+
key.addr.ip4 = skops->local_ip4;
295+
if (skops->family == AF_INET6) {
296+
if (is_ipv4_mapped_addr(skops->local_ip6))
297+
key.addr.ip4 = skops->local_ip6[3];
298+
else
299+
IP6_COPY(key.addr.ip6, skops->local_ip6);
300+
}
301+
302+
int *value = bpf_map_lookup_elem(&map_of_manager, &key);
303+
if (!value)
304+
return false;
305+
return (*value == 0);
306+
}
307+
226308
#endif

bpf/kmesh/ads/sockops.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ int sockops_prog(struct bpf_sock_ops *skops)
1919
return BPF_OK;
2020

2121
switch (skops->op) {
22+
case BPF_SOCK_OPS_TCP_CONNECT_CB:
23+
skops_handle_kmesh_managed_process(skops);
24+
break;
2225
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
26+
if (!is_managed_by_kmesh(skops))
27+
break;
28+
2329
if (bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG) != 0) {
2430
BPF_LOG(ERR, SOCKOPS, "set sockops cb failed!\n");
2531
} else {

bpf/kmesh/workload/sockops.c

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@ struct {
2929
__uint(map_flags, 0);
3030
} map_of_kmesh_socket SEC(".maps");
3131

32-
static inline bool is_managed_by_kmesh(struct bpf_sock_ops *skops)
33-
{
34-
struct manager_key key = {0};
35-
if (skops->family == AF_INET)
36-
key.addr.ip4 = skops->local_ip4;
37-
if (skops->family == AF_INET6) {
38-
if (is_ipv4_mapped_addr(skops->local_ip6))
39-
key.addr.ip4 = skops->local_ip6[3];
40-
else
41-
IP6_COPY(key.addr.ip6, skops->local_ip6);
42-
}
43-
44-
int *value = bpf_map_lookup_elem(&map_of_manager, &key);
45-
if (!value)
46-
return false;
47-
return (*value == 0);
48-
}
49-
5032
static inline bool skip_specific_probe(struct bpf_sock_ops *skops)
5133
{
5234
struct kmesh_config *data = {0};
@@ -182,70 +164,6 @@ static inline void enable_encoding_metadata(struct bpf_sock_ops *skops)
182164
BPF_LOG(ERR, SOCKOPS, "enable encoding metadata failed!, err is %d", err);
183165
}
184166

185-
static inline void record_kmesh_managed_ip(__u32 family, __u32 ip4, __u32 *ip6)
186-
{
187-
int err;
188-
__u32 value = 0;
189-
struct manager_key key = {0};
190-
if (family == AF_INET)
191-
key.addr.ip4 = ip4;
192-
if (family == AF_INET6 && ip6)
193-
IP6_COPY(key.addr.ip6, ip6);
194-
195-
err = bpf_map_update_elem(&map_of_manager, &key, &value, BPF_ANY);
196-
if (err)
197-
BPF_LOG(ERR, KMESH, "record ip failed!, err is %d\n", err);
198-
}
199-
200-
static inline void remove_kmesh_managed_ip(__u32 family, __u32 ip4, __u32 *ip6)
201-
{
202-
struct manager_key key = {0};
203-
if (family == AF_INET)
204-
key.addr.ip4 = ip4;
205-
if (family == AF_INET6 && ip6)
206-
IP6_COPY(key.addr.ip6, ip6);
207-
208-
int err = bpf_map_delete_elem(&map_of_manager, &key);
209-
if (err && err != -ENOENT)
210-
BPF_LOG(ERR, KMESH, "remove ip failed!, err is %d\n", err);
211-
}
212-
213-
static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u16 port)
214-
{
215-
__u16 remote_port = GET_SKOPS_REMOTE_PORT(skops);
216-
if (bpf_ntohs(remote_port) != port)
217-
return false;
218-
219-
if (skops->family == AF_INET)
220-
return (bpf_ntohl(skops->remote_ip4) == ip);
221-
222-
return (
223-
skops->remote_ip6[0] == 0 && skops->remote_ip6[1] == 0 && skops->remote_ip6[2] == 0
224-
&& bpf_ntohl(skops->remote_ip6[3]) == ip);
225-
}
226-
227-
static inline bool skops_conn_from_cni_sim_add(struct bpf_sock_ops *skops)
228-
{
229-
// cni sim connect CONTROL_CMD_IP:929(0x3a1)
230-
// 0x3a1 is the specific port handled by the cni to enable Kmesh
231-
return conn_from_sim(skops, CONTROL_CMD_IP, ENABLE_KMESH_PORT);
232-
}
233-
234-
static inline bool skops_conn_from_cni_sim_delete(struct bpf_sock_ops *skops)
235-
{
236-
// cni sim connect CONTROL_CMD_IP:930(0x3a2)
237-
// 0x3a2 is the specific port handled by the cni to disable Kmesh
238-
return conn_from_sim(skops, CONTROL_CMD_IP, DISABLE_KMESH_PORT);
239-
}
240-
241-
static inline void skops_handle_kmesh_managed_process(struct bpf_sock_ops *skops)
242-
{
243-
if (skops_conn_from_cni_sim_add(skops))
244-
record_kmesh_managed_ip(skops->family, skops->local_ip4, skops->local_ip6);
245-
if (skops_conn_from_cni_sim_delete(skops))
246-
remove_kmesh_managed_ip(skops->family, skops->local_ip4, skops->local_ip6);
247-
}
248-
249167
SEC("sockops")
250168
int sockops_prog(struct bpf_sock_ops *skops)
251169
{

kmesh_compile_env_pre.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ function kmesh_set_env(){
109109
export EXTRA_CFLAGS="-O0 -g"
110110
}
111111

112-
# adjust the range of BPF code compilation based on the kernel is enhanced
113-
function bpf_compile_range_adjust() {
114-
if [ "$ENHANCED_KERNEL" == "enhanced" ]; then
115-
sed -i '/ads\/sockops\.c/s/\(.*\)generate/\/\/go:generate/' bpf/kmesh/bpf2go/bpf2go.go
116-
else
117-
sed -i '/ads\/sockops\.c/s/\(.*\)generate/\/\/not go:generate/' bpf/kmesh/bpf2go/bpf2go.go
118-
fi
119-
}
120-
121112
function set_enhanced_kernel_env() {
122113
# we use /usr/include/linux/bpf.h to determine the runtime environment’s
123114
# support for kmesh. Considering the case of online image compilation, a
@@ -149,5 +140,4 @@ function prepare() {
149140
kmesh_set_env
150141
bash kmesh_macros_env.sh
151142
bash kmesh_bpf_env.sh
152-
bpf_compile_range_adjust
153143
}

pkg/bpf/ads/loader.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ var log = logger.NewLoggerScope("bpf_ads")
3939

4040
type BpfAds struct {
4141
SockConn BpfSockConn
42+
SockOps BpfSockOps
4243
Tc *general.BpfTCGeneral
4344
}
4445

4546
func NewBpfAds(cfg *options.BpfConfig) (*BpfAds, error) {
4647
sc := &BpfAds{}
48+
49+
if err := sc.SockOps.NewBpf(cfg); err != nil {
50+
return nil, err
51+
}
52+
4753
if err := sc.SockConn.NewBpf(cfg); err != nil {
4854
return nil, err
4955
}
@@ -105,6 +111,10 @@ func (sc *BpfAds) Load() error {
105111
return err
106112
}
107113

114+
if err := sc.SockOps.Load(); err != nil {
115+
return err
116+
}
117+
108118
if err := sc.Tc.LoadTC(); err != nil {
109119
return err
110120
}
@@ -142,6 +152,10 @@ func (sc *BpfAds) ApiEnvCfg() error {
142152
}
143153

144154
func (sc *BpfAds) Attach() error {
155+
if err := sc.SockOps.Attach(); err != nil {
156+
return err
157+
}
158+
145159
if err := sc.SockConn.Attach(); err != nil {
146160
return err
147161
}
@@ -150,9 +164,14 @@ func (sc *BpfAds) Attach() error {
150164
}
151165

152166
func (sc *BpfAds) Detach() error {
167+
if err := sc.SockOps.Detach(); err != nil {
168+
return err
169+
}
170+
153171
if err := sc.SockConn.Detach(); err != nil {
154172
return err
155173
}
174+
156175
if err := sc.Tc.Close(); err != nil {
157176
return err
158177
}

pkg/bpf/ads/sock_ops.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build enhanced
2-
// +build enhanced
3-
41
/*
52
* Copyright The Kmesh Authors.
63
*

0 commit comments

Comments
 (0)