Skip to content
Merged
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
1,206 changes: 364 additions & 842 deletions bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c

Large diffs are not rendered by default.

21 changes: 3 additions & 18 deletions bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

#include <stdbool.h>

/* equal MAP_SIZE_OF_OUTTER_MAP */
#define MAX_OUTTER_MAP_ENTRIES (1 << 20)
#define OUTTER_MAP_USAGE_HIGH_PERCENT (0.7)
#define OUTTER_MAP_USAGE_LOW_PERCENT (0.3)
#define TASK_SIZE (512)

// 32,768
#define OUTTER_MAP_SCALEUP_STEP (1 << 15)
// 8,192
#define OUTTER_MAP_SCALEIN_STEP (1 << 13)

#define ELASTIC_SLOTS_NUM \
((OUTTER_MAP_SCALEUP_STEP > OUTTER_MAP_SCALEIN_STEP) ? OUTTER_MAP_SCALEUP_STEP : OUTTER_MAP_SCALEIN_STEP)

struct element_list_node {
void *elem;
struct element_list_node *next;
Expand All @@ -32,8 +18,7 @@ void deserial_free_elem(void *value);
void deserial_free_elem_list(struct element_list_node *head);
int deserial_delete_elem(void *key, const void *msg_desciptor);

int deserial_init(bool restore);
int deserial_uninit(bool persist);
int inner_map_mng_persist();
int deserial_init();
void deserial_uninit();

#endif /* __DESERIALIZATION_TO_BPF_MAP_H__ */
#endif /* __DESERIALIZATION_TO_BPF_MAP_H__ */
100 changes: 68 additions & 32 deletions bpf/include/bpf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,36 @@ struct {
} map_of_sock_storage SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(__u32));
__uint(value_size, MAP_VAL_SIZE_64);
__uint(max_entries, MAP_MAX_ENTRIES);
__uint(map_flags, BPF_F_NO_PREALLOC);
} kmesh_map64 SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(__u32));
__uint(value_size, MAP_VAL_SIZE_192);
__uint(max_entries, MAP_MAX_ENTRIES);
__uint(map_flags, BPF_F_NO_PREALLOC);
} kmesh_map192 SEC(".maps");

struct {
Copy link
Member

Choose a reason for hiding this comment

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

add a blank line

__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
__uint(max_entries, MAP_SIZE_OF_OUTTER_MAP);
__uint(map_flags, 0);
} outer_map SEC(".maps");
__uint(value_size, MAP_VAL_SIZE_296);
__uint(max_entries, MAP_MAX_ENTRIES);
__uint(map_flags, BPF_F_NO_PREALLOC);
} kmesh_map296 SEC(".maps");

struct {
Copy link
Member

Choose a reason for hiding this comment

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

add a blank line before

__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(__u32));
__uint(value_size, BPF_INNER_MAP_DATA_LEN);
__uint(max_entries, 1);
__uint(map_flags, 0);
} inner_map SEC(".maps");
__uint(value_size, MAP_VAL_SIZE_1600);
__uint(max_entries, MAP_MAX_ENTRIES);
__uint(map_flags, BPF_F_NO_PREALLOC);
} kmesh_map1600 SEC(".maps");

/*
* From v5.4, bpf_get_netns_cookie can be called for bpf cgroup hooks, from v5.15, it can be called for bpf sockops
Expand Down Expand Up @@ -160,31 +176,51 @@ static inline bool handle_kmesh_manage_process(struct kmesh_context *kmesh_ctx)
return false;
}

static inline void *kmesh_get_ptr_val(const void *ptr)
static inline void kmesh_parse_outer_key(__u32 outer_key, __u8 *type, __u32 *inner_idx)
{
/*
map_in_map -- outer_map:
key value
idx1 inner_map_fd1 // point to inner map1
idx2 inner_map_fd2 // point to inner map2

structA.ptr_member1 = idx1; // store idx in outer_map
*/
void *inner_map_instance = NULL;
__u32 inner_idx = 0;
__u32 idx = (__u32)(uintptr_t)ptr;

if (!ptr) {
return NULL;
}
*type = MAP_GET_TYPE(outer_key);
*inner_idx = MAP_GET_INDEX(outer_key);
return;
}

/* get inner_map_instance by idx */
inner_map_instance = kmesh_map_lookup_elem(&outer_map, &idx);
if (!inner_map_instance) {
static inline void *get_ptr_val_from_map(void *map, __u8 map_type, const void *ptr)
{
__u8 type;
__u32 inner_idx;
__u32 outer_key = (__u32)(uintptr_t)ptr;

kmesh_parse_outer_key(outer_key, &type, &inner_idx);
if (type != map_type) {
BPF_LOG(ERR, KMESH, "get_ptr_val: invalid map type(%u %u)\n", type, map_type);
return NULL;
}

/* get inner_map_instance value */
return kmesh_map_lookup_elem(inner_map_instance, &inner_idx);
return kmesh_map_lookup_elem(map, &inner_idx);
}
#endif

#define KMESH_GET_PTR_VAL(ptr, type) \
Copy link
Contributor

Choose a reason for hiding this comment

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

Defined as whether a function can reduce the number of instructions?

({ \
void *val_tmp = NULL; \
if (sizeof(type) == sizeof(void *)) { \
if (__builtin_types_compatible_p(type, char *)) \
val_tmp = get_ptr_val_from_map(&kmesh_map192, MAP_TYPE_192, ptr); \
else if (__builtin_types_compatible_p(type, void *)) \
val_tmp = get_ptr_val_from_map(&kmesh_map1600, MAP_TYPE_1600, ptr); \
else if (__builtin_types_compatible_p(type, void **)) \
val_tmp = get_ptr_val_from_map(&kmesh_map1600, MAP_TYPE_1600, ptr); \
else \
val_tmp = get_ptr_val_from_map(&kmesh_map64, MAP_TYPE_64, ptr); \
} else if (sizeof(type) <= MAP_VAL_SIZE_64) \
val_tmp = get_ptr_val_from_map(&kmesh_map64, MAP_TYPE_64, ptr); \
else if (sizeof(type) <= MAP_VAL_SIZE_192) \
val_tmp = get_ptr_val_from_map(&kmesh_map192, MAP_TYPE_192, ptr); \
else if (sizeof(type) <= MAP_VAL_SIZE_296) \
val_tmp = get_ptr_val_from_map(&kmesh_map296, MAP_TYPE_296, ptr); \
else if (sizeof(type) <= MAP_VAL_SIZE_1600) \
val_tmp = get_ptr_val_from_map(&kmesh_map1600, MAP_TYPE_1600, ptr); \
else \
val_tmp = NULL; \
val_tmp; \
})

#endif
30 changes: 28 additions & 2 deletions bpf/include/inner_map_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@
#ifndef __INNER_MAP_H__
#define __INNER_MAP_H__

#define BPF_INNER_MAP_DATA_LEN 1300
// outer key:
// map_type(1 byte) + inner_index(3 bytes)

#endif // __INNER_MAP_H__
typedef enum { MAP_TYPE_64, MAP_TYPE_192, MAP_TYPE_296, MAP_TYPE_1600, MAP_TYPE_MAX } map_in_map_type;

#define MAP_GET_TYPE(idx) (__u8)((__u32)(idx) >> 24)
#define MAP_GET_INDEX(idx) (__u32)((__u32)(idx)&0xFFFFFF)
#define MAP_GEN_OUTER_KEY(map_type, pos) ((__u32)((((__u8)(map_type)&0xFF) << 24) + ((__u32)(pos)&0xFFFFFF)))

#define MAP_VAL_SIZE_64 64
#define MAP_VAL_SIZE_192 192
#define MAP_VAL_SIZE_296 296
#define MAP_VAL_SIZE_1600 1600
#define MAP_MAX_ENTRIES 1000000

#define MAP_VAL_STR_SIZE MAP_VAL_SIZE_192
#define MAP_VAL_REPEAT_SIZE MAP_VAL_SIZE_1600

#define SET_BIT(bitmap, n) ((bitmap)[(n) / 8] |= (1U << ((n) % 8)))

#define CLEAR_BIT(bitmap, n) ((bitmap)[(n) / 8] &= ~(1U << ((n) % 8)))

#define IS_SET(bitmap, n) (((bitmap)[(n) / 8] & (1U << ((n) % 8))) != 0)

#define IS_CLEAR(bitmap, n) (((bitmap)[(n) / 8] & (1U << ((n) % 8))) == 0)

#define FLIP_BIT(bitmap, n) ((bitmap)[(n) / 8] ^= (1U << ((n) % 8)))

#endif // __INNER_MAP_H__
2 changes: 1 addition & 1 deletion bpf/kmesh/ads/include/circuit_breaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static inline int on_cluster_sock_bind(ctx_buff_t *ctx, const Cluster__Cluster *

if (stats != NULL) {
Cluster__CircuitBreakers *cbs = NULL;
cbs = kmesh_get_ptr_val(cluster->circuit_breakers);
cbs = KMESH_GET_PTR_VAL(cluster->circuit_breakers, Cluster__CircuitBreakers);
if (cbs != NULL && stats->active_connections >= cbs->max_connections) {
BPF_LOG(
DEBUG,
Expand Down
24 changes: 13 additions & 11 deletions bpf/kmesh/ads/include/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ cluster_add_endpoints(const Endpoint__LocalityLbEndpoints *lb_ep, struct cluster
__u32 i;
void *ep_ptrs = NULL;

ep_ptrs = kmesh_get_ptr_val(lb_ep->lb_endpoints);
ep_ptrs = KMESH_GET_PTR_VAL(lb_ep->lb_endpoints, void *);
if (!ep_ptrs)
return -1;

Expand All @@ -115,7 +115,7 @@ static inline __u32 cluster_get_endpoints_num(const Endpoint__ClusterLoadAssignm
void *ptrs = NULL;
Endpoint__LocalityLbEndpoints *lb_ep = NULL;

ptrs = kmesh_get_ptr_val(cla->endpoints);
ptrs = KMESH_GET_PTR_VAL(cla->endpoints, void *);
if (!ptrs)
return 0;

Expand All @@ -125,7 +125,8 @@ static inline __u32 cluster_get_endpoints_num(const Endpoint__ClusterLoadAssignm
break;
}

lb_ep = (Endpoint__LocalityLbEndpoints *)kmesh_get_ptr_val((void *)*((__u64 *)ptrs + i));
lb_ep = (Endpoint__LocalityLbEndpoints *)KMESH_GET_PTR_VAL(
(void *)*((__u64 *)ptrs + i), Endpoint__LocalityLbEndpoints);
if (!lb_ep)
continue;

Expand All @@ -152,7 +153,7 @@ static inline int cluster_init_endpoints(const char *cluster_name, const Endpoin
}
cluster_eps->ep_num = 0;

ptrs = kmesh_get_ptr_val(cla->endpoints);
ptrs = KMESH_GET_PTR_VAL(cla->endpoints, void *);
if (!ptrs) {
BPF_LOG(ERR, CLUSTER, "failed to get cla endpoints ptrs\n");
return -1;
Expand All @@ -163,7 +164,8 @@ static inline int cluster_init_endpoints(const char *cluster_name, const Endpoin
if (i >= cla->n_endpoints)
break;

ep = (Endpoint__LocalityLbEndpoints *)kmesh_get_ptr_val((void *)*((__u64 *)ptrs + i));
ep = (Endpoint__LocalityLbEndpoints *)KMESH_GET_PTR_VAL(
(void *)*((__u64 *)ptrs + i), Endpoint__LocalityLbEndpoints);
if (!ep)
continue;

Expand All @@ -186,7 +188,7 @@ cluster_check_endpoints(const struct cluster_endpoints *eps, const Endpoint__Clu
if (!eps || eps->ep_num != lb_num)
return 0;

ptrs = kmesh_get_ptr_val(cla->endpoints);
ptrs = KMESH_GET_PTR_VAL(cla->endpoints, void *);
if (!ptrs)
return 0;

Expand All @@ -207,7 +209,7 @@ static inline struct cluster_endpoints *cluster_refresh_endpoints(const Cluster_
struct cluster_endpoints *eps = NULL;
Endpoint__ClusterLoadAssignment *cla = NULL;

cla = kmesh_get_ptr_val(cluster->load_assignment);
cla = KMESH_GET_PTR_VAL(cluster->load_assignment, Endpoint__ClusterLoadAssignment);
if (!cla) {
BPF_LOG(ERR, CLUSTER, "get load_assignment failed\n");
return NULL;
Expand Down Expand Up @@ -264,13 +266,13 @@ static inline Core__SocketAddress *cluster_get_ep_sock_addr(const void *ep_ident
Endpoint__Endpoint *ep = NULL;
Core__SocketAddress *sock_addr = NULL;

ep = kmesh_get_ptr_val(ep_identity);
ep = KMESH_GET_PTR_VAL(ep_identity, Endpoint__Endpoint);
if (!ep) {
BPF_LOG(ERR, CLUSTER, "cluster get ep failed\n");
return NULL;
}

sock_addr = kmesh_get_ptr_val(ep->address);
sock_addr = KMESH_GET_PTR_VAL(ep->address, Core__SocketAddress);
if (!sock_addr) {
BPF_LOG(ERR, CLUSTER, "ep get sock addr failed\n");
return NULL;
Expand All @@ -285,7 +287,7 @@ static inline int cluster_handle_loadbalance(Cluster__Cluster *cluster, address_
Core__SocketAddress *sock_addr = NULL;
struct cluster_endpoints *eps = NULL;

name = kmesh_get_ptr_val(cluster->name);
name = KMESH_GET_PTR_VAL(cluster->name, char *);
if (!name) {
BPF_LOG(ERR, CLUSTER, "failed to get cluster\n");
return -EAGAIN;
Expand Down Expand Up @@ -350,4 +352,4 @@ int cluster_manager(ctx_buff_t *ctx)
return KMESH_TAIL_CALL_RET(ret);
}

#endif
#endif
16 changes: 8 additions & 8 deletions bpf/kmesh/ads/include/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static inline int filter_chain_filter_match(
}

/* filter match */
ptrs = kmesh_get_ptr_val(filter_chain->filters);
ptrs = KMESH_GET_PTR_VAL(filter_chain->filters, void *);
if (!ptrs) {
BPF_LOG(ERR, FILTER, "failed to get filter ptrs\n");
return -1;
Expand All @@ -63,7 +63,7 @@ static inline int filter_chain_filter_match(
break;
}

filter = (Listener__Filter *)kmesh_get_ptr_val((void *)*((__u64 *)ptrs + i));
filter = (Listener__Filter *)KMESH_GET_PTR_VAL((void *)*((__u64 *)ptrs + i), Listener__Filter);
if (!filter) {
continue;
}
Expand All @@ -86,7 +86,7 @@ static inline int handle_http_connection_manager(
ctx_key_t ctx_key = {0};
ctx_val_t ctx_val = {0};

route_name = kmesh_get_ptr_val((http_conn->route_config_name));
route_name = KMESH_GET_PTR_VAL((http_conn->route_config_name), char *);
if (!route_name) {
BPF_LOG(ERR, FILTER, "failed to get http conn route name\n");
return -1;
Expand Down Expand Up @@ -117,7 +117,7 @@ int filter_manager(ctx_buff_t *ctx)
return KMESH_TAIL_CALL_RET(-1);
}

filter = (Listener__Filter *)kmesh_get_ptr_val((void *)ctx_val->val);
filter = (Listener__Filter *)KMESH_GET_PTR_VAL((void *)ctx_val->val, Listener__Filter);
if (!filter) {
BPF_LOG(ERR, FILTER, "failed to get filter\n");
return KMESH_TAIL_CALL_RET(-1);
Expand All @@ -127,7 +127,7 @@ int filter_manager(ctx_buff_t *ctx)
switch (filter->config_type_case) {
#ifndef CGROUP_SOCK_MANAGE
case LISTENER__FILTER__CONFIG_TYPE_HTTP_CONNECTION_MANAGER:
http_conn = kmesh_get_ptr_val(filter->http_connection_manager);
http_conn = KMESH_GET_PTR_VAL(filter->http_connection_manager, Filter__HttpConnectionManager);
ret = bpf_parse_header_msg(ctx_val->msg);
if (GET_RET_PROTO_TYPE(ret) != PROTO_HTTP_1_1) {
BPF_LOG(DEBUG, FILTER, "http filter manager,only support http1.1 this version");
Expand All @@ -143,7 +143,7 @@ int filter_manager(ctx_buff_t *ctx)
break;
#endif
case LISTENER__FILTER__CONFIG_TYPE_TCP_PROXY:
tcp_proxy = kmesh_get_ptr_val(filter->tcp_proxy);
tcp_proxy = KMESH_GET_PTR_VAL(filter->tcp_proxy, Filter__TcpProxy);
if (!tcp_proxy) {
BPF_LOG(ERR, FILTER, "get tcp_prxoy failed\n");
ret = -1;
Expand Down Expand Up @@ -178,7 +178,7 @@ int filter_chain_manager(ctx_buff_t *ctx)
}
kmesh_tail_delete_ctx(&ctx_key);

filter_chain = (Listener__FilterChain *)kmesh_get_ptr_val((void *)ctx_val_ptr->val);
filter_chain = (Listener__FilterChain *)KMESH_GET_PTR_VAL((void *)ctx_val_ptr->val, Listener__FilterChain);
if (filter_chain == NULL) {
return KMESH_TAIL_CALL_RET(-1);
}
Expand Down Expand Up @@ -207,4 +207,4 @@ int filter_chain_manager(ctx_buff_t *ctx)
return KMESH_TAIL_CALL_RET(ret);
}

#endif
#endif
Loading
Loading