Skip to content

Commit d708c51

Browse files
committed
Fix unexpected bpf_sk_storage_get error
Signed-off-by: Zhonghu Xu <[email protected]>
1 parent b572de1 commit d708c51

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

bpf/include/bpf_common.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,21 @@ static inline void remove_kmesh_managed_ip(__u32 family, __u32 ip4, __u32 *ip6)
260260
BPF_LOG(ERR, KMESH, "remove ip failed, err is %d\n", err);
261261
}
262262

263-
static inline bool sock_conn_from_sim(struct bpf_sock *sk)
263+
static inline bool sock_conn_from_sim(struct __sk_buff *skb)
264264
{
265265
__u16 enable_port = ENABLE_KMESH_PORT;
266266
__u16 disable_port = DISABLE_KMESH_PORT;
267-
__u16 dst_port = sk->dst_port;
267+
__u16 dst_port = (__u16)(skb->remote_port >> 16);
268268
if (bpf_ntohs(dst_port) != enable_port && bpf_ntohs(dst_port) != disable_port)
269269
return false;
270270

271-
if (sk->family == AF_INET)
272-
return bpf_ntohl(sk->dst_ip4) == CONTROL_CMD_IP;
273-
271+
if (skb->protocol == AF_INET)
272+
return bpf_ntohl(skb->remote_ip4) == CONTROL_CMD_IP;
273+
// If directly read skb->remote_ip6. bpf prog load failed
274+
__u32 remote_ip6[4] = {0};
275+
bpf_skb_load_bytes(skb, offsetof(struct __sk_buff, remote_ip6), &remote_ip6, sizeof(remote_ip6));
274276
return (
275-
sk->dst_ip6[0] == 0 && sk->dst_ip6[1] == 0 && sk->dst_ip6[2] == 0
276-
&& bpf_ntohl(sk->dst_ip6[3]) == CONTROL_CMD_IP);
277+
remote_ip6[0] == 0 && remote_ip6[1] == 0 && remote_ip6[2] == 0 && bpf_ntohl(remote_ip6[3]) == CONTROL_CMD_IP);
277278
}
278279

279280
static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u16 port)

bpf/kmesh/probes/probe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ static inline void observe_on_data(struct bpf_sock *sk)
9595
if (!tcp_sock)
9696
return;
9797

98-
storage = bpf_sk_storage_get(&map_of_sock_storage, sk, 0, 0);
98+
// Use BPF_LOCAL_STORAGE_GET_F_CREATE in case a connection being established before kmesh start.
99+
storage = bpf_sk_storage_get(&map_of_sock_storage, sk, 0, BPF_LOCAL_STORAGE_GET_F_CREATE);
99100
if (!storage) {
100101
BPF_LOG(ERR, PROBE, "on data: bpf_sk_storage_get failed dst %u \n", bpf_ntohs(sk->dst_port));
101102
return;

bpf/kmesh/workload/cgroup_skb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int cgroup_skb_ingress_prog(struct __sk_buff *skb)
2424
if (!sk)
2525
return SK_PASS;
2626

27-
if (sock_conn_from_sim(sk)) {
27+
if (sock_conn_from_sim(skb)) {
2828
return SK_PASS;
2929
}
3030

@@ -48,7 +48,7 @@ int cgroup_skb_egress_prog(struct __sk_buff *skb)
4848
if (!sk)
4949
return SK_PASS;
5050

51-
if (sock_conn_from_sim(sk)) {
51+
if (sock_conn_from_sim(skb)) {
5252
return SK_PASS;
5353
}
5454

0 commit comments

Comments
 (0)