Skip to content

Commit 0340457

Browse files
authored
Merge pull request #252 from bitcoffeeiux/deveop_ads_new
Kmesh: adapt ads ebpf program for cni
2 parents 728e70f + 141e154 commit 0340457

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

bpf/kmesh/cgroup_sock.c

+57-12
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,39 @@
3030
#if KMESH_ENABLE_IPV4
3131
#if KMESH_ENABLE_HTTP
3232

33+
struct {
34+
__uint(type, BPF_MAP_TYPE_HASH);
35+
__type(key, __u64);
36+
__type(value, __u32);
37+
__uint(max_entries, MAP_SIZE_OF_MANAGER);
38+
__uint(map_flags, 0);
39+
} map_of_manager SEC(".maps");
40+
3341
static const char kmesh_module_name[] = "kmesh_defer";
3442

35-
static inline bool check_sock_enable_kmesh()
43+
static inline void record_netns_cookie(struct bpf_sock_addr *ctx)
44+
{
45+
int err;
46+
int value = 0;
47+
__u64 cookie = bpf_get_netns_cookie(ctx);
48+
err = bpf_map_update_elem(&map_of_manager, &cookie, &value, BPF_NOEXIST);
49+
if (err)
50+
BPF_LOG(ERR, KMESH, "record netcookie failed!, err is %d\n", err);
51+
}
52+
53+
static inline void remove_netns_cookie(struct bpf_sock_addr *ctx)
3654
{
37-
/* currently, namespace that use Kmesh are marked by using the
38-
* specified number in net_cls.classid of cgroupv1.
39-
* When the container is started, the CNI adds the corresponding
40-
* tag to the classid file of the container. eBPF obtains the tag
41-
* to determine whether to manage the container in Kmesh.
42-
*/
43-
__u64 classid = bpf_get_cgroup_classid(NULL);
44-
if (classid != KMESH_CLASSID_MARK)
45-
return false;
46-
return true;
55+
int err;
56+
__u64 cookie = bpf_get_netns_cookie(ctx);
57+
err = bpf_map_delete_elem(&map_of_manager, &cookie);
58+
if (err && err != -ENOENT)
59+
BPF_LOG(ERR, KMESH, "remove netcookie failed!, err is %d\n", err);
60+
}
61+
62+
static inline bool check_kmesh_enabled(struct bpf_sock_addr *ctx)
63+
{
64+
__u64 cookie = bpf_get_netns_cookie(ctx);
65+
return bpf_map_lookup_elem(&map_of_manager, &cookie);
4766
}
4867

4968
static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
@@ -52,7 +71,7 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
5271

5372
Listener__Listener *listener = NULL;
5473

55-
if (!check_sock_enable_kmesh())
74+
if (!check_kmesh_enabled(ctx))
5675
return 0;
5776

5877
DECLARE_VAR_ADDRESS(ctx, address);
@@ -83,9 +102,35 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
83102
return 0;
84103
}
85104

105+
static inline bool conn_from_cni_sim_add(struct bpf_sock_addr *ctx)
106+
{
107+
// cni sim connect 0.0.0.0:929(0x3a1)
108+
// 0x3a1 is the specific port handled by the cni for enable Kmesh
109+
return ((bpf_ntohl(ctx->user_ip4) == 1) &&
110+
(bpf_ntohl(ctx->user_port) == 0x3a10000));
111+
}
112+
113+
static inline bool conn_from_cni_sim_delete(struct bpf_sock_addr *ctx)
114+
{
115+
// cni sim connect 0.0.0.1:930(0x3a2)
116+
// 0x3a2 is the specific port handled by the cni for disable Kmesh
117+
return ((bpf_ntohl(ctx->user_ip4) == 1) &&
118+
(bpf_ntohl(ctx->user_port) == 0x3a20000));
119+
}
120+
86121
SEC("cgroup/connect4")
87122
int cgroup_connect4_prog(struct bpf_sock_addr *ctx)
88123
{
124+
if (conn_from_cni_sim_add(ctx)) {
125+
record_netns_cookie(ctx);
126+
// return failed, cni sim connect 0.0.0.1:929(0x3a1)
127+
// A normal program will not connect to this IP address
128+
return CGROUP_SOCK_OK;
129+
}
130+
if (conn_from_cni_sim_delete(ctx)) {
131+
remove_netns_cookie(ctx);
132+
return CGROUP_SOCK_OK;
133+
}
89134
int ret = sock4_traffic_control(ctx);
90135
return CGROUP_SOCK_OK;
91136
}

bpf/kmesh/include/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#define MAP_SIZE_OF_PER_ROUTE 8
4646
#define MAP_SIZE_OF_PER_CLUSTER 32
4747
#define MAP_SIZE_OF_PER_ENDPOINT 64
48+
#define MAP_SIZE_OF_MANAGER 8192
4849

4950
#define MAP_SIZE_OF_MAX 8192
5051

0 commit comments

Comments
 (0)