Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: fix bpf ins over 100w #1059

Merged
merged 2 commits into from
Nov 20, 2024
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
64 changes: 35 additions & 29 deletions bpf/kmesh/workload/include/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,34 @@ static inline int waypoint_manager(struct kmesh_context *kmesh_ctx, struct ip_ad
return 0;
}

static inline int
update_dst_addr_with_service_port(struct kmesh_context *kmesh_ctx, backend_value *backend_v, service_value *service_v)
{
int i;
ctx_buff_t *ctx = (ctx_buff_t *)kmesh_ctx->ctx;

#pragma unroll
for (i = 0; i < MAX_PORT_COUNT; i++) {
if (ctx->user_port == service_v->service_port[i]) {
if (ctx->user_family == AF_INET)
kmesh_ctx->dnat_ip.ip4 = backend_v->addr.ip4;
else
bpf_memcpy(kmesh_ctx->dnat_ip.ip6, backend_v->addr.ip6, IPV6_ADDR_LEN);

kmesh_ctx->dnat_port = service_v->target_port[i];
kmesh_ctx->via_waypoint = false;
Copy link
Member

Choose a reason for hiding this comment

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

nit: should we always set false before entering the loop

return 0;
}
}
return -ENOENT;
}

static inline int
backend_manager(struct kmesh_context *kmesh_ctx, backend_value *backend_v, __u32 service_id, service_value *service_v)
{
int ret;
int ret = -ENOENT;
Copy link
Member

Choose a reason for hiding this comment

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

nit: set error in the error branch

ctx_buff_t *ctx = (ctx_buff_t *)kmesh_ctx->ctx;
__u32 user_port = ctx->user_port;
__u32 i, user_port = ctx->user_port;

if (backend_v->waypoint_port != 0) {
BPF_LOG(
Expand All @@ -68,36 +90,20 @@ backend_manager(struct kmesh_context *kmesh_ctx, backend_value *backend_v, __u32
}

#pragma unroll
for (__u32 i = 0; i < MAX_SERVICE_COUNT; i++) {
if (i >= backend_v->service_count) {
BPF_LOG(WARN, BACKEND, "exceed the max backend service count:%d", backend_v->service_count);
for (i = 0; i < MAX_SERVICE_COUNT; i++) {
if (i >= backend_v->service_count)
break;

if (service_id != backend_v->service[i])
continue;

ret = update_dst_addr_with_service_port(kmesh_ctx, backend_v, service_v);
if (ret == 0)
break;
}
if (service_id == backend_v->service[i]) {
BPF_LOG(DEBUG, BACKEND, "access the backend by service:%u\n", service_id);
#pragma unroll
for (__u32 j = 0; j < MAX_PORT_COUNT; j++) {
if (user_port == service_v->service_port[j]) {
if (ctx->user_family == AF_INET)
kmesh_ctx->dnat_ip.ip4 = backend_v->addr.ip4;
else
bpf_memcpy(kmesh_ctx->dnat_ip.ip6, backend_v->addr.ip6, IPV6_ADDR_LEN);
kmesh_ctx->dnat_port = service_v->target_port[j];
kmesh_ctx->via_waypoint = false;
BPF_LOG(
DEBUG,
BACKEND,
"get the backend addr=[%s:%u]\n",
ip2str((__u32 *)&kmesh_ctx->dnat_ip, ctx->family == AF_INET),
bpf_ntohs(service_v->target_port[j]));
return 0;
}
}
}
}

BPF_LOG(ERR, BACKEND, "failed to get the backend\n");
return -ENOENT;
BPF_LOG(WARN, BACKEND, "backend_manager svc_id:%u i:%u ret:%d.\n", service_id, i, ret);
return ret;
}

#endif
84 changes: 44 additions & 40 deletions bpf/kmesh/workload/include/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,57 @@ static inline int lb_random_handle(struct kmesh_context *kmesh_ctx, __u32 servic
return 0;
}

static inline int lb_locality_strict_handle(struct kmesh_context *kmesh_ctx, __u32 service_id, service_value *service_v)
{
int ret = -ENOENT;
endpoint_key endpoint_k = {0};
endpoint_value *endpoint_v = NULL;
endpoint_k.service_id = service_id;

if (service_v->prio_endpoint_count[0]) {
endpoint_k.backend_index = bpf_get_prandom_u32() % service_v->prio_endpoint_count[0] + 1;
endpoint_v = map_lookup_endpoint(&endpoint_k);
if (endpoint_v)
ret = endpoint_manager(kmesh_ctx, endpoint_v, service_id, service_v);
}

if (ret) {
kmesh_ctx->dnat_ip = (struct ip_addr){0};
kmesh_ctx->dnat_port = 0;
BPF_LOG(ERR, SERVICE, "locality loadbalance match nothing in STRICT mode, ret:%d\n", ret);
}
return ret;
}

static inline int
lb_locality_failover_handle(struct kmesh_context *kmesh_ctx, __u32 service_id, service_value *service_v, bool is_strict)
lb_locality_failover_handle(struct kmesh_context *kmesh_ctx, __u32 service_id, service_value *service_v)
{
int ret = 0;
uint32_t rand_k = 0;
int i, ret = -ENOENT;
endpoint_key endpoint_k = {0};
endpoint_value *endpoint_v = NULL;
endpoint_k.service_id = service_id;
struct ip_addr zero_addr = {0};
__u32 zero_port = 0;

// #pragma unroll
for (int match_prio = 0; match_prio < PRIO_COUNT; match_prio++) {
endpoint_k.prio = match_prio; // 0->6
// if we have endpoints in this prio
if (service_v->prio_endpoint_count[match_prio] > 0) {
rand_k = bpf_get_prandom_u32() % service_v->prio_endpoint_count[match_prio] + 1;
endpoint_k.backend_index = rand_k;
endpoint_v = map_lookup_endpoint(&endpoint_k);
if (!endpoint_v) {
BPF_LOG(
ERR, SERVICE, "find endpoint [%u/%u/%u] failed", service_id, match_prio, endpoint_k.backend_index);
return -ENOENT;
}
ret = endpoint_manager(kmesh_ctx, endpoint_v, service_id, service_v);
if (ret != 0) {
if (ret != -ENOENT)
BPF_LOG(ERR, SERVICE, "endpoint_manager failed, ret:%d\n", ret);
return ret;
}
BPF_LOG(
DEBUG,
SERVICE,
"locality lb matched backend_uid %d with priority %d\n",
endpoint_v->backend_uid,
match_prio);
return 0; // find the backend successfully
}
if (is_strict) { // only match max priority in strict mode
kmesh_ctx->dnat_ip = zero_addr;
kmesh_ctx->dnat_port = zero_port;
BPF_LOG(DEBUG, SERVICE, "locality loadbalance match nothing in STRICT mode\n");
return -ENOENT;
for (i = 0; i < PRIO_COUNT; i++) {
if (service_v->prio_endpoint_count[i] == 0)
continue;

endpoint_k.prio = i;
endpoint_k.backend_index = bpf_get_prandom_u32() % service_v->prio_endpoint_count[i] + 1;
endpoint_v = map_lookup_endpoint(&endpoint_k);
if (!endpoint_v) {
ret = -ENOENT;
break;
}

ret = endpoint_manager(kmesh_ctx, endpoint_v, service_id, service_v);
if (ret != -ENOENT)
break;
}
// no backend matched
return -ENOENT;

if (ret)
BPF_LOG(ERR, SERVICE, "lb_locality_failover_hdl [%u %u %u]\n", service_id, i, endpoint_k.backend_index);
Copy link
Member

Choose a reason for hiding this comment

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

lb_locality_failover_hdl typo

return ret;
}

static inline int service_manager(struct kmesh_context *kmesh_ctx, __u32 service_id, service_value *service_v)
Expand Down Expand Up @@ -118,10 +122,10 @@ static inline int service_manager(struct kmesh_context *kmesh_ctx, __u32 service
ret = lb_random_handle(kmesh_ctx, service_id, service_v);
break;
case LB_POLICY_STRICT:
ret = lb_locality_failover_handle(kmesh_ctx, service_id, service_v, true);
ret = lb_locality_strict_handle(kmesh_ctx, service_id, service_v);
break;
case LB_POLICY_FAILOVER:
ret = lb_locality_failover_handle(kmesh_ctx, service_id, service_v, false);
ret = lb_locality_failover_handle(kmesh_ctx, service_id, service_v);
break;
default:
BPF_LOG(ERR, SERVICE, "unsupported load balance type:%u\n", service_v->lb_policy);
Expand Down
Loading