Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions bpf/include/bpf_kfunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright Authors of Kmesh */

#include "../../config/kmesh_marcos_def.h"
#include <linux/in.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <linux/bpf.h>
#include <sys/socket.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include "map_config.h"

#include "errno.h"

struct bpf_mem_ptr {
void *ptr;
__u32 size;
};

#define KMESH_MODULE_ULP_NAME "kmesh_defer"

extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym;
extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym;
extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym;
extern int bpf_setsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, const char *optval, int optval__sz) __ksym;
extern int bpf_getsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, char *optval, int optval__sz) __ksym;

#define bpf_km_header_strncmp bpf_km_header_strncmp_func

static int bpf_km_header_strnstr(void *ctx, const char *key, int key__sz, const char *subptr, int subptr__sz)
{
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr);
}

static int bpf_parse_header_msg(struct bpf_sock_addr *ctx)
{
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr));
}

// Due to the limitation of bpf verifier, optval and optval__sz are required to correspond.
// The strnlen function cannot be used here, so the string is redefined.
static int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz)
{
const char kmesh_module_ulp_name[] = KMESH_MODULE_ULP_NAME;
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_ulp_name))
return -1;

struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_setsockopt_func(
&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_ulp_name, sizeof(kmesh_module_ulp_name));
}

static int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz)
{
if (level != IPPROTO_TCP) {
return -1;
}
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz);
}
21 changes: 13 additions & 8 deletions bpf/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define _COMMON_H_

#include "../../config/kmesh_marcos_def.h"
#include <linux/in.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
Expand All @@ -17,7 +18,17 @@
#include "errno.h"

#if ENHANCED_KERNEL
#include <bpf_helper_defs_ext.h>
#if KERNEL_KFUNC
#include "bpf_kfunc.h"
#else
struct bpf_mem_ptr {
void *ptr;
__u32 size;
};
#include "bpf_helper_defs_ext.h"
#define bpf_km_setsockopt bpf_setsockopt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls define macros in bpf_helper_defs_ext.h

#define bpf_km_getsockopt bpf_getsockopt
#endif
#endif

#define bpf_unused __attribute__((__unused__))
Expand Down Expand Up @@ -113,14 +124,8 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4])
(dst)[3] = (src)[3]; \
} while (0)

#if OE_23_03
#define bpf__strncmp bpf_strncmp
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port)
#else
#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16)
#endif

#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)
#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port)

#define MAX_BUF_LEN 100
#define MAX_IP4_LEN 16
Expand Down
12 changes: 6 additions & 6 deletions bpf/kmesh/ads/cgroup_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

#if ENHANCED_KERNEL
#include "route_config.h"
static const char kmesh_module_ulp_name[] = KMESH_MODULE_ULP_NAME;
#endif
#if KMESH_ENABLE_IPV4
#if KMESH_ENABLE_HTTP

static const char kmesh_module_name[] = "kmesh_defer";
static char kmesh_module_name_get[KMESH_MODULE_NAME_LEN] = "";
static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
{
int ret;

char kmesh_module_ulp_name_get[KMESH_MODULE_NAME_LEN] = "";
Listener__Listener *listener = NULL;

if (ctx->protocol != IPPROTO_TCP)
Expand All @@ -42,9 +41,10 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
BPF_LOG(DEBUG, KMESH, "bpf find listener addr=[%s:%u]\n", ip2str(&ip, 1), bpf_ntohs(ctx->user_port));

#if ENHANCED_KERNEL
ret = bpf_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name_get, KMESH_MODULE_NAME_LEN);
if (CHECK_MODULE_NAME_NULL(ret) || bpf__strncmp(kmesh_module_name_get, KMESH_MODULE_NAME_LEN, kmesh_module_name)) {
ret = bpf_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name, sizeof(kmesh_module_name));
ret = bpf_km_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_ulp_name_get, KMESH_MODULE_NAME_LEN);
if (CHECK_MODULE_NAME_NULL(ret)
|| bpf__strncmp(kmesh_module_ulp_name_get, KMESH_MODULE_NAME_LEN, kmesh_module_ulp_name)) {
ret = bpf_km_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, kmesh_module_ulp_name, sizeof(kmesh_module_ulp_name));
if (ret)
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret %d\n", ret);
return 0;
Expand Down
11 changes: 0 additions & 11 deletions bpf/kmesh/ads/include/ctx/sock_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ typedef struct bpf_sock_ops ctx_buff_t;
name.ipv4 = (ctx)->remote_ip4; \
name.port = (ctx)->remote_port

#if OE_23_03
#define SET_CTX_ADDRESS(ctx, address) \
(ctx)->remote_ip4 = (address)->ipv4; \
(ctx)->remote_port = (address)->port

#define MARK_REJECTED(ctx) \
BPF_LOG(DEBUG, KMESH, "mark reject\n"); \
(ctx)->remote_ip4 = 0; \
(ctx)->remote_port = 0
#else
#define SET_CTX_ADDRESS(ctx, address) \
(ctx)->replylong[2] = (address)->ipv4; \
(ctx)->replylong[3] = (address)->port
Expand All @@ -40,6 +30,5 @@ typedef struct bpf_sock_ops ctx_buff_t;
BPF_LOG(DEBUG, KMESH, "mark reject\n"); \
(ctx)->replylong[2] = 0; \
(ctx)->replylong[3] = 0
#endif

#endif //__BPF_CTX_SOCK_OPS_H
5 changes: 0 additions & 5 deletions bpf/kmesh/ads/include/kmesh_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
val; \
})

struct bpf_mem_ptr {
void *ptr;
__u32 size;
};

static inline int bpf__strncmp(const char *dst, int n, const char *src)
{
if (dst == NULL || src == NULL)
Expand Down
Loading
Loading