Skip to content

Commit

Permalink
Merge pull request #343 from bfforever/main
Browse files Browse the repository at this point in the history
Split the ebpf code directory of ads and workload.
  • Loading branch information
kmesh-bot authored May 20, 2024
2 parents 29309db + 9670c19 commit 22434f3
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 104 deletions.
65 changes: 65 additions & 0 deletions bpf/include/bpf_common.h
Original file line number Diff line number Diff line change
@@ -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));
}
48 changes: 1 addition & 47 deletions bpf/kmesh/cgroup_sock.c → bpf/kmesh/ads/cgroup_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions bpf/kmesh/bpf2go/bpf2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 1 addition & 38 deletions bpf/kmesh/workload/cgroup_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 0 additions & 1 deletion bpf/kmesh/workload/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions bpf/kmesh/workload/include/workload.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions bpf/kmesh/workload/sockops_tuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "workload.h"
#include "config.h"
#include "encoder.h"
#include "bpf_common.h"

#define FORMAT_IP_LENGTH (16)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions kmesh_compile_env_pre.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bpf/bpf_kmesh_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 22434f3

Please sign in to comment.