30
30
#if KMESH_ENABLE_IPV4
31
31
#if KMESH_ENABLE_HTTP
32
32
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
+
33
41
static const char kmesh_module_name [] = "kmesh_defer" ;
34
42
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 )
36
54
{
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 );
47
66
}
48
67
49
68
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)
52
71
53
72
Listener__Listener * listener = NULL ;
54
73
55
- if (!check_sock_enable_kmesh ( ))
74
+ if (!check_kmesh_enabled ( ctx ))
56
75
return 0 ;
57
76
58
77
DECLARE_VAR_ADDRESS (ctx , address );
@@ -83,9 +102,35 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
83
102
return 0 ;
84
103
}
85
104
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
+
86
121
SEC ("cgroup/connect4" )
87
122
int cgroup_connect4_prog (struct bpf_sock_addr * ctx )
88
123
{
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
+ }
89
134
int ret = sock4_traffic_control (ctx );
90
135
return CGROUP_SOCK_OK ;
91
136
}
0 commit comments