From 9670c193910d8d431525368438e1286b1f66e146 Mon Sep 17 00:00:00 2001 From: kangmingfa <1640528278@qq.com> Date: Mon, 20 May 2024 14:41:05 +0800 Subject: [PATCH] Split the ebpf code directory of ads and workload. Signed-off-by: kangmingfa <1640528278@qq.com> --- bpf/include/bpf_common.h | 65 +++++++++++++++++++++ bpf/kmesh/{ => ads}/cgroup_sock.c | 48 +-------------- bpf/kmesh/{ => ads}/include/cluster.h | 0 bpf/kmesh/{ => ads}/include/config.h | 1 - bpf/kmesh/{ => ads}/include/ctx/sock_addr.h | 0 bpf/kmesh/{ => ads}/include/ctx/sock_ops.h | 0 bpf/kmesh/{ => ads}/include/filter.h | 0 bpf/kmesh/{ => ads}/include/kmesh_common.h | 0 bpf/kmesh/{ => ads}/include/listener.h | 0 bpf/kmesh/{ => ads}/include/route_config.h | 0 bpf/kmesh/{ => ads}/include/tail_call.h | 0 bpf/kmesh/{ => ads}/include/tcp_proxy.h | 0 bpf/kmesh/{ => ads}/sockops.c | 0 bpf/kmesh/{ => ads}/tracepoint.c | 0 bpf/kmesh/bpf2go/bpf2go.go | 6 +- bpf/kmesh/workload/cgroup_sock.c | 39 +------------ bpf/kmesh/workload/include/config.h | 1 - bpf/kmesh/workload/include/workload.h | 7 --- bpf/kmesh/workload/sockops_tuple.c | 9 +-- kmesh_compile_env_pre.sh | 4 +- pkg/bpf/bpf_kmesh_common.go | 2 +- 21 files changed, 78 insertions(+), 104 deletions(-) create mode 100644 bpf/include/bpf_common.h rename bpf/kmesh/{ => ads}/cgroup_sock.c (63%) rename bpf/kmesh/{ => ads}/include/cluster.h (100%) rename bpf/kmesh/{ => ads}/include/config.h (98%) rename bpf/kmesh/{ => ads}/include/ctx/sock_addr.h (100%) rename bpf/kmesh/{ => ads}/include/ctx/sock_ops.h (100%) rename bpf/kmesh/{ => ads}/include/filter.h (100%) rename bpf/kmesh/{ => ads}/include/kmesh_common.h (100%) rename bpf/kmesh/{ => ads}/include/listener.h (100%) rename bpf/kmesh/{ => ads}/include/route_config.h (100%) rename bpf/kmesh/{ => ads}/include/tail_call.h (100%) rename bpf/kmesh/{ => ads}/include/tcp_proxy.h (100%) rename bpf/kmesh/{ => ads}/sockops.c (100%) rename bpf/kmesh/{ => ads}/tracepoint.c (100%) diff --git a/bpf/include/bpf_common.h b/bpf/include/bpf_common.h new file mode 100644 index 000000000..86ab8f0a6 --- /dev/null +++ b/bpf/include/bpf_common.h @@ -0,0 +1,65 @@ +/* + * Copyright 2024 The Kmesh Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define map_of_manager kmesh_manage +#define MAP_SIZE_OF_MANAGER 8192 + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u64); + __type(value, __u32); + __uint(max_entries, MAP_SIZE_OF_MANAGER); + __uint(map_flags, 0); +} map_of_manager SEC(".maps"); + +static inline void record_netns_cookie(struct bpf_sock_addr *ctx) +{ + int err; + int value = 0; + __u64 cookie = bpf_get_netns_cookie(ctx); + err = bpf_map_update_elem(&map_of_manager, &cookie, &value, BPF_NOEXIST); + if (err) + BPF_LOG(ERR, KMESH, "record netcookie failed!, err is %d\n", err); +} + +static inline void remove_netns_cookie(struct bpf_sock_addr *ctx) +{ + int err; + __u64 cookie = bpf_get_netns_cookie(ctx); + err = bpf_map_delete_elem(&map_of_manager, &cookie); + if (err && err != -ENOENT) + BPF_LOG(ERR, KMESH, "remove netcookie failed!, err is %d\n", err); +} + +static inline bool check_kmesh_enabled(struct bpf_sock_addr *ctx) +{ + __u64 cookie = bpf_get_netns_cookie(ctx); + return bpf_map_lookup_elem(&map_of_manager, &cookie); +} + +static inline bool conn_from_cni_sim_add(struct bpf_sock_addr *ctx) +{ + // cni sim connect 0.0.0.0:929(0x3a1) + // 0x3a1 is the specific port handled by the cni for enable Kmesh + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a10000)); +} + +static inline bool conn_from_cni_sim_delete(struct bpf_sock_addr *ctx) +{ + // cni sim connect 0.0.0.1:930(0x3a2) + // 0x3a2 is the specific port handled by the cni for disable Kmesh + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a20000)); +} \ No newline at end of file diff --git a/bpf/kmesh/cgroup_sock.c b/bpf/kmesh/ads/cgroup_sock.c similarity index 63% rename from bpf/kmesh/cgroup_sock.c rename to bpf/kmesh/ads/cgroup_sock.c index 732e49674..8bd2da13c 100644 --- a/bpf/kmesh/cgroup_sock.c +++ b/bpf/kmesh/ads/cgroup_sock.c @@ -26,45 +26,13 @@ #include "listener/listener.pb-c.h" #include "filter.h" #include "cluster.h" +#include "bpf_common.h" #if KMESH_ENABLE_IPV4 #if KMESH_ENABLE_HTTP -struct { - __uint(type, BPF_MAP_TYPE_HASH); - __type(key, __u64); - __type(value, __u32); - __uint(max_entries, MAP_SIZE_OF_MANAGER); - __uint(map_flags, 0); -} map_of_manager SEC(".maps"); - static const char kmesh_module_name[] = "kmesh_defer"; -static inline void record_netns_cookie(struct bpf_sock_addr *ctx) -{ - int err; - int value = 0; - __u64 cookie = bpf_get_netns_cookie(ctx); - err = bpf_map_update_elem(&map_of_manager, &cookie, &value, BPF_NOEXIST); - if (err) - BPF_LOG(ERR, KMESH, "record netcookie failed!, err is %d\n", err); -} - -static inline void remove_netns_cookie(struct bpf_sock_addr *ctx) -{ - int err; - __u64 cookie = bpf_get_netns_cookie(ctx); - err = bpf_map_delete_elem(&map_of_manager, &cookie); - if (err && err != -ENOENT) - BPF_LOG(ERR, KMESH, "remove netcookie failed!, err is %d\n", err); -} - -static inline bool check_kmesh_enabled(struct bpf_sock_addr *ctx) -{ - __u64 cookie = bpf_get_netns_cookie(ctx); - return bpf_map_lookup_elem(&map_of_manager, &cookie); -} - static inline int sock4_traffic_control(struct bpf_sock_addr *ctx) { int ret; @@ -102,20 +70,6 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx) return 0; } -static inline bool conn_from_cni_sim_add(struct bpf_sock_addr *ctx) -{ - // cni sim connect 0.0.0.0:929(0x3a1) - // 0x3a1 is the specific port handled by the cni for enable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a10000)); -} - -static inline bool conn_from_cni_sim_delete(struct bpf_sock_addr *ctx) -{ - // cni sim connect 0.0.0.1:930(0x3a2) - // 0x3a2 is the specific port handled by the cni for disable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a20000)); -} - SEC("cgroup/connect4") int cgroup_connect4_prog(struct bpf_sock_addr *ctx) { diff --git a/bpf/kmesh/include/cluster.h b/bpf/kmesh/ads/include/cluster.h similarity index 100% rename from bpf/kmesh/include/cluster.h rename to bpf/kmesh/ads/include/cluster.h diff --git a/bpf/kmesh/include/config.h b/bpf/kmesh/ads/include/config.h similarity index 98% rename from bpf/kmesh/include/config.h rename to bpf/kmesh/ads/include/config.h index fafd65b86..38bf0acbb 100644 --- a/bpf/kmesh/include/config.h +++ b/bpf/kmesh/ads/include/config.h @@ -44,7 +44,6 @@ #define MAP_SIZE_OF_PER_ROUTE 8 #define MAP_SIZE_OF_PER_CLUSTER 32 #define MAP_SIZE_OF_PER_ENDPOINT 64 -#define MAP_SIZE_OF_MANAGER 8192 #define MAP_SIZE_OF_MAX 8192 #define MAP_SIZE_OF_OUTTER_MAP 8192 diff --git a/bpf/kmesh/include/ctx/sock_addr.h b/bpf/kmesh/ads/include/ctx/sock_addr.h similarity index 100% rename from bpf/kmesh/include/ctx/sock_addr.h rename to bpf/kmesh/ads/include/ctx/sock_addr.h diff --git a/bpf/kmesh/include/ctx/sock_ops.h b/bpf/kmesh/ads/include/ctx/sock_ops.h similarity index 100% rename from bpf/kmesh/include/ctx/sock_ops.h rename to bpf/kmesh/ads/include/ctx/sock_ops.h diff --git a/bpf/kmesh/include/filter.h b/bpf/kmesh/ads/include/filter.h similarity index 100% rename from bpf/kmesh/include/filter.h rename to bpf/kmesh/ads/include/filter.h diff --git a/bpf/kmesh/include/kmesh_common.h b/bpf/kmesh/ads/include/kmesh_common.h similarity index 100% rename from bpf/kmesh/include/kmesh_common.h rename to bpf/kmesh/ads/include/kmesh_common.h diff --git a/bpf/kmesh/include/listener.h b/bpf/kmesh/ads/include/listener.h similarity index 100% rename from bpf/kmesh/include/listener.h rename to bpf/kmesh/ads/include/listener.h diff --git a/bpf/kmesh/include/route_config.h b/bpf/kmesh/ads/include/route_config.h similarity index 100% rename from bpf/kmesh/include/route_config.h rename to bpf/kmesh/ads/include/route_config.h diff --git a/bpf/kmesh/include/tail_call.h b/bpf/kmesh/ads/include/tail_call.h similarity index 100% rename from bpf/kmesh/include/tail_call.h rename to bpf/kmesh/ads/include/tail_call.h diff --git a/bpf/kmesh/include/tcp_proxy.h b/bpf/kmesh/ads/include/tcp_proxy.h similarity index 100% rename from bpf/kmesh/include/tcp_proxy.h rename to bpf/kmesh/ads/include/tcp_proxy.h diff --git a/bpf/kmesh/sockops.c b/bpf/kmesh/ads/sockops.c similarity index 100% rename from bpf/kmesh/sockops.c rename to bpf/kmesh/ads/sockops.c diff --git a/bpf/kmesh/tracepoint.c b/bpf/kmesh/ads/tracepoint.c similarity index 100% rename from bpf/kmesh/tracepoint.c rename to bpf/kmesh/ads/tracepoint.c diff --git a/bpf/kmesh/bpf2go/bpf2go.go b/bpf/kmesh/bpf2go/bpf2go.go index 1a27513a4..a71028777 100644 --- a/bpf/kmesh/bpf2go/bpf2go.go +++ b/bpf/kmesh/bpf2go/bpf2go.go @@ -21,10 +21,10 @@ package bpf2go // go run github.com/cilium/ebpf/cmd/bpf2go --help -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshCgroupSock ../cgroup_sock.c -- -I../include -I../../include -I../../../api/v2-c -DCGROUP_SOCK_MANAGE +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshCgroupSock ../ads/cgroup_sock.c -- -I../ads/include -I../../include -I../../../api/v2-c -DCGROUP_SOCK_MANAGE //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshCgroupSockWorkload ../workload/cgroup_sock.c -- -I../workload/include -I../../include -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshSockops ../sockops.c -- -I../include -I../../include -I../../../api/v2-c -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshTracePoint ../tracepoint.c -- -I../include -I../../include +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshSockops ../ads/sockops.c -- -I../ads/include -I../../include -I../../../api/v2-c +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshTracePoint ../ads/tracepoint.c -- -I../ads/include -I../../include //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshSockopsWorkload ../workload/sockops_tuple.c -- -I../workload/include -I../../include //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshXDPAuth ../workload/xdp.c -- -I../workload/include -I../../include //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang --cflags $EXTRA_CFLAGS --cflags $EXTRA_CDEFINE KmeshSendmsg ../workload/sendmsg.c -- -I../workload/include -I../../include diff --git a/bpf/kmesh/workload/cgroup_sock.c b/bpf/kmesh/workload/cgroup_sock.c index 6ed31f0bb..63ebc3af6 100644 --- a/bpf/kmesh/workload/cgroup_sock.c +++ b/bpf/kmesh/workload/cgroup_sock.c @@ -23,30 +23,7 @@ #include "bpf_log.h" #include "ctx/sock_addr.h" #include "frontend.h" - -static inline void record_netns_cookie(struct bpf_sock_addr *ctx) -{ - int err; - int value = 0; - __u64 cookie = bpf_get_netns_cookie(ctx); - err = bpf_map_update_elem(&map_of_manager, &cookie, &value, BPF_NOEXIST); - if (err) - BPF_LOG(ERR, KMESH, "record netcookie failed!, err is %d\n", err); -} - -static inline void remove_netns_cookie(struct bpf_sock_addr *ctx) -{ - __u64 cookie = bpf_get_netns_cookie(ctx); - int err = bpf_map_delete_elem(&map_of_manager, &cookie); - if (err && err != -ENOENT) - BPF_LOG(ERR, KMESH, "remove netcookie failed!, err is %d\n", err); -} - -static inline bool check_kmesh_enabled(struct bpf_sock_addr *ctx) -{ - __u64 cookie = bpf_get_netns_cookie(ctx); - return bpf_map_lookup_elem(&map_of_manager, &cookie); -} +#include "bpf_common.h" static inline int sock4_traffic_control(struct bpf_sock_addr *ctx) { @@ -75,20 +52,6 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx) return 0; } -static inline bool conn_from_cni_sim_add(struct bpf_sock_addr *ctx) -{ - // cni sim connect 0.0.0.0:929(0x3a1) - // 0x3a1 is the specific port handled by the cni for enable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a10000)); -} - -static inline bool conn_from_cni_sim_delete(struct bpf_sock_addr *ctx) -{ - // cni sim connect 0.0.0.1:930(0x3a2) - // 0x3a2 is the specific port handled by the cni for disable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == 0x3a20000)); -} - SEC("cgroup/connect4") int cgroup_connect4_prog(struct bpf_sock_addr *ctx) { diff --git a/bpf/kmesh/workload/include/config.h b/bpf/kmesh/workload/include/config.h index 2db21278f..05c1016fd 100644 --- a/bpf/kmesh/workload/include/config.h +++ b/bpf/kmesh/workload/include/config.h @@ -26,7 +26,6 @@ #define MAP_SIZE_OF_ENDPOINT 1000 #define MAP_SIZE_OF_BACKEND 500 #define MAP_SIZE_OF_AUTH 8192 -#define MAP_SIZE_OF_MANAGER 8192 #define MAP_SIZE_OF_DSTINFO 8192 // map name diff --git a/bpf/kmesh/workload/include/workload.h b/bpf/kmesh/workload/include/workload.h index 4579e48ef..e17a38ca5 100644 --- a/bpf/kmesh/workload/include/workload.h +++ b/bpf/kmesh/workload/include/workload.h @@ -112,11 +112,4 @@ struct { __uint(max_entries, RINGBUF_SIZE); } map_of_tuple SEC(".maps"); -struct { - __uint(type, BPF_MAP_TYPE_HASH); - __type(key, __u64); - __type(value, __u32); - __uint(max_entries, MAP_SIZE_OF_MANAGER); - __uint(map_flags, 0); -} map_of_manager SEC(".maps"); #endif diff --git a/bpf/kmesh/workload/sockops_tuple.c b/bpf/kmesh/workload/sockops_tuple.c index 90b66d822..2ca5a827a 100644 --- a/bpf/kmesh/workload/sockops_tuple.c +++ b/bpf/kmesh/workload/sockops_tuple.c @@ -22,6 +22,7 @@ #include "workload.h" #include "config.h" #include "encoder.h" +#include "bpf_common.h" #define FORMAT_IP_LENGTH (16) @@ -148,7 +149,7 @@ static inline void remove_ip(__u32 ip) BPF_LOG(ERR, KMESH, "record netcookie failed!, err is %d\n", err); } -static inline bool conn_from_cni_sim_add(struct bpf_sock_ops *skops) +static inline bool skops_conn_from_cni_sim_add(struct bpf_sock_ops *skops) { // cni sim connect 0.0.0.1:929(0x3a1) // 0x3a1 is the specific port handled by the cni for enable Kmesh @@ -159,7 +160,7 @@ static inline bool conn_from_cni_sim_add(struct bpf_sock_ops *skops) #endif } -static inline bool conn_from_cni_sim_delete(struct bpf_sock_ops *skops) +static inline bool skops_conn_from_cni_sim_delete(struct bpf_sock_ops *skops) { // cni sim connect 0.0.0.1:930(0x3a2) // 0x3a2 is the specific port handled by the cni for disable Kmesh @@ -182,9 +183,9 @@ int record_tuple(struct bpf_sock_ops *skops) return 0; switch (skops->op) { case BPF_SOCK_OPS_TCP_CONNECT_CB: - if (conn_from_cni_sim_add(skops)) + if (skops_conn_from_cni_sim_add(skops)) record_ip(skops->local_ip4); - if (conn_from_cni_sim_delete(skops)) + if (skops_conn_from_cni_sim_delete(skops)) remove_ip(skops->local_ip4); break; case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: diff --git a/kmesh_compile_env_pre.sh b/kmesh_compile_env_pre.sh index 7d596b05c..142df0422 100644 --- a/kmesh_compile_env_pre.sh +++ b/kmesh_compile_env_pre.sh @@ -37,7 +37,7 @@ function fix_libbpf_bug() { function adapt_low_version_kernel() { # adapt less insn in kernel 4.19, only 4096, so modify KMESH_PER_ENDPOINT_NUM into 15 if [ "$(uname -r | cut -d '.' -f 1)" -le 4 ]; then - sed -i 's/\(KMESH_PER_ENDPOINT_NUM\).*/\1 15/g' bpf/kmesh/include/config.h + sed -i 's/\(KMESH_PER_ENDPOINT_NUM\).*/\1 15/g' bpf/kmesh/ads/include/config.h fi } @@ -48,7 +48,7 @@ function adapt_low_version_kernel() { # the current compilation environment during compilation. function adapt_include_env { if grep -q "struct bpf_mem_ptr {" /usr/include/linux/bpf.h; then - sed -i '/bpf_mem_ptr/{N;N;N;N;d;}' bpf/kmesh/include/kmesh_common.h + sed -i '/bpf_mem_ptr/{N;N;N;N;d;}' bpf/kmesh/ads/include/kmesh_common.h fi } diff --git a/pkg/bpf/bpf_kmesh_common.go b/pkg/bpf/bpf_kmesh_common.go index d3a8fec46..be28fa902 100644 --- a/pkg/bpf/bpf_kmesh_common.go +++ b/pkg/bpf/bpf_kmesh_common.go @@ -20,7 +20,7 @@ package bpf // #cgo pkg-config: bpf api-v2-c -// #include "kmesh/include/kmesh_common.h" +// #include "kmesh/ads/include/kmesh_common.h" import "C" import ( "os"