From ef419a95cb4e12165c4c7787c952f8b85be3e043 Mon Sep 17 00:00:00 2001 From: wuchangye Date: Tue, 19 Nov 2024 22:14:08 +0800 Subject: [PATCH 1/2] optimize: fix bpf ins over 100w Signed-off-by: wuchangye --- bpf/kmesh/workload/include/service.h | 84 +++++++++++++++------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/bpf/kmesh/workload/include/service.h b/bpf/kmesh/workload/include/service.h index 139684fe4..b3ec88a7f 100644 --- a/bpf/kmesh/workload/include/service.h +++ b/bpf/kmesh/workload/include/service.h @@ -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); + return ret; } static inline int service_manager(struct kmesh_context *kmesh_ctx, __u32 service_id, service_value *service_v) @@ -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); From dcacbd9fec022d542205c838e98802c164f35273 Mon Sep 17 00:00:00 2001 From: wuchangye Date: Wed, 20 Nov 2024 13:43:09 +0800 Subject: [PATCH 2/2] optimize backend_manager Signed-off-by: wuchangye --- bpf/kmesh/workload/include/backend.h | 64 +++++++++++++++------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/bpf/kmesh/workload/include/backend.h b/bpf/kmesh/workload/include/backend.h index 760b9dc55..fa6834db5 100644 --- a/bpf/kmesh/workload/include/backend.h +++ b/bpf/kmesh/workload/include/backend.h @@ -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; + 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; 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( @@ -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