Skip to content

Commit 1d2122a

Browse files
authored
Merge pull request #1350 from hzxuzhonghu/cgroup_skb
Fix cgroup_skb/* get sk_storage failed
2 parents 8a07412 + 44253ce commit 1d2122a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

bpf/include/bpf_common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +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 __sk_buff *skb)
264+
{
265+
__u16 dst_port = (__u16)(skb->remote_port >> 16);
266+
if (bpf_ntohs(dst_port) != ENABLE_KMESH_PORT && bpf_ntohs(dst_port) != DISABLE_KMESH_PORT)
267+
return false;
268+
269+
if (skb->protocol == AF_INET)
270+
return bpf_ntohl(skb->remote_ip4) == CONTROL_CMD_IP;
271+
// If directly read skb->remote_ip6. bpf prog load would fail with permission denied.
272+
__u32 remote_ip6[4] = {0};
273+
bpf_skb_load_bytes(skb, offsetof(struct __sk_buff, remote_ip6), &remote_ip6, sizeof(remote_ip6));
274+
return (
275+
remote_ip6[0] == 0 && remote_ip6[1] == 0 && remote_ip6[2] == 0 && bpf_ntohl(remote_ip6[3]) == CONTROL_CMD_IP);
276+
}
277+
263278
static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u16 port)
264279
{
265280
__u16 remote_port = GET_SKOPS_REMOTE_PORT(skops);

bpf/kmesh/probes/probe.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ static inline void observe_on_data(struct bpf_sock *sk)
9090
struct sock_storage_data *storage = NULL;
9191
if (!sk)
9292
return;
93+
9394
tcp_sock = bpf_tcp_sock(sk);
9495
if (!tcp_sock)
9596
return;
9697

97-
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);
98100
if (!storage) {
99-
BPF_LOG(ERR, PROBE, "on data: bpf_sk_storage_get failed\n");
101+
BPF_LOG(ERR, PROBE, "on data: bpf_sk_storage_get failed dst %u \n", bpf_ntohs(sk->dst_port));
100102
return;
101103
}
102104
__u64 now = bpf_ktime_get_ns();

bpf/kmesh/workload/cgroup_skb.c

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

27+
if (sock_conn_from_sim(skb)) {
28+
return SK_PASS;
29+
}
30+
2731
if (!is_managed_by_kmesh_skb(skb))
2832
return SK_PASS;
33+
2934
observe_on_data(sk);
3035
return SK_PASS;
3136
}
@@ -43,8 +48,13 @@ int cgroup_skb_egress_prog(struct __sk_buff *skb)
4348
if (!sk)
4449
return SK_PASS;
4550

51+
if (sock_conn_from_sim(skb)) {
52+
return SK_PASS;
53+
}
54+
4655
if (!is_managed_by_kmesh_skb(skb))
4756
return SK_PASS;
57+
4858
observe_on_data(sk);
4959
return SK_PASS;
5060
}

0 commit comments

Comments
 (0)