-
Notifications
You must be signed in to change notification settings - Fork 88
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
new kernel adapt #1198
base: main
Are you sure you want to change the base?
new kernel adapt #1198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,14 @@ | |
#include "cluster.h" | ||
#include "bpf_common.h" | ||
|
||
#if ENHANCED_KERNEL | ||
#include "route_config.h" | ||
#endif | ||
#if KMESH_ENABLE_IPV4 | ||
#if KMESH_ENABLE_HTTP | ||
|
||
static const char kmesh_module_name[] = "kmesh_defer"; | ||
|
||
static char kmesh_module_name_get[16]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write a macro to define |
||
static inline int sock4_traffic_control(struct bpf_sock_addr *ctx) | ||
{ | ||
int ret; | ||
|
@@ -39,18 +42,19 @@ 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 | ||
// todo build when kernel support http parse and route | ||
// defer conn | ||
ret = bpf_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name, sizeof(kmesh_module_name)); | ||
if (ret) | ||
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret:%d\n", ret); | ||
#else // KMESH_ENABLE_HTTP | ||
ret = listener_manager(ctx, listener, NULL); | ||
ret = bpf_getsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name_get, 16); | ||
BPF_LOG(DEBUG, KMESH, "kmesh_module_name_get:%s ret:%d\n", kmesh_module_name_get, ret); | ||
if (ret != 0 || bpf__strncmp(kmesh_module_name_get, 16, kmesh_module_name)) { | ||
ret = bpf_setsockopt(ctx, IPPROTO_TCP, TCP_ULP, (void *)kmesh_module_name, sizeof(kmesh_module_name)); | ||
if (ret) | ||
BPF_LOG(ERR, KMESH, "bpf set sockopt failed! ret %d\n", ret); | ||
return 0; | ||
} | ||
#endif | ||
ret = listener_manager(ctx, listener, ctx); | ||
if (ret != 0) { | ||
BPF_LOG(ERR, KMESH, "listener_manager failed, ret %d\n", ret); | ||
return ret; | ||
} | ||
#endif // KMESH_ENABLE_HTTP | ||
|
||
return 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,24 +28,15 @@ static inline Route__RouteConfiguration *map_lookup_route_config(const char *rou | |
return kmesh_map_lookup_elem(&map_of_router_config, route_name); | ||
} | ||
|
||
static inline int | ||
virtual_host_match_check(Route__VirtualHost *virt_host, address_t *addr, ctx_buff_t *ctx, struct bpf_mem_ptr *host) | ||
static inline int virtual_host_match_check( | ||
Route__VirtualHost *virt_host, address_t *addr, ctx_buff_t *ctx, char *host_key, int host_key_len) | ||
{ | ||
int i; | ||
void *domains = NULL; | ||
void *domain = NULL; | ||
void *ptr; | ||
__u32 ptr_length; | ||
|
||
if (!host) | ||
return 0; | ||
|
||
ptr = _(host->ptr); | ||
if (!ptr) | ||
return 0; | ||
|
||
ptr_length = _(host->size); | ||
|
||
if (!virt_host->domains) | ||
return 0; | ||
|
||
|
@@ -65,14 +56,8 @@ virtual_host_match_check(Route__VirtualHost *virt_host, address_t *addr, ctx_buf | |
if (((char *)domain)[0] == '*' && ((char *)domain)[1] == '\0') | ||
return 1; | ||
|
||
if (bpf_strnstr(ptr, domain, ptr_length) != NULL) { | ||
BPF_LOG( | ||
DEBUG, | ||
ROUTER_CONFIG, | ||
"match virtual_host, name=\"%s\"\n", | ||
(char *)KMESH_GET_PTR_VAL(virt_host->name, char *)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The number of instructions occupied by BPF_LOG is very large, and printing logs within multiple nested loops can cause the total instruction count of the ebpf program to exceed 1,000,000 insn. |
||
if (bpf_km_strnstr(ctx, host_key, host_key_len, domain, BPF_DATA_MAX_LEN) != 0) | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
|
@@ -95,7 +80,7 @@ virtual_host_match(Route__RouteConfiguration *route_config, address_t *addr, ctx | |
Route__VirtualHost *virt_host = NULL; | ||
Route__VirtualHost *virt_host_allow_any = NULL; | ||
char host_key[5] = {'H', 'o', 's', 't', '\0'}; | ||
struct bpf_mem_ptr *host; | ||
int host_key_len = 5; | ||
|
||
if (route_config->n_virtual_hosts <= 0 || route_config->n_virtual_hosts > KMESH_PER_VIRT_HOST_NUM) { | ||
BPF_LOG(WARN, ROUTER_CONFIG, "invalid virt hosts num=%d\n", route_config->n_virtual_hosts); | ||
|
@@ -108,12 +93,6 @@ virtual_host_match(Route__RouteConfiguration *route_config, address_t *addr, ctx | |
return NULL; | ||
} | ||
|
||
host = bpf_get_msg_header_element(host_key); | ||
if (!host) { | ||
BPF_LOG(ERR, ROUTER_CONFIG, "failed to get URI in msg\n"); | ||
return NULL; | ||
} | ||
|
||
for (i = 0; i < KMESH_PER_VIRT_HOST_NUM; i++) { | ||
if (i >= route_config->n_virtual_hosts) { | ||
break; | ||
|
@@ -128,27 +107,34 @@ virtual_host_match(Route__RouteConfiguration *route_config, address_t *addr, ctx | |
continue; | ||
} | ||
|
||
if (virtual_host_match_check(virt_host, addr, ctx, host)) | ||
if (virtual_host_match_check(virt_host, addr, ctx, host_key, host_key_len)) { | ||
BPF_LOG( | ||
DEBUG, | ||
ROUTER_CONFIG, | ||
"match virtual_host, name=\"%s\"\n", | ||
(char *)KMESH_GET_PTR_VAL(virt_host->name, char *)); | ||
return virt_host; | ||
} | ||
} | ||
// allow_any as the default virt_host | ||
if (virt_host_allow_any && virtual_host_match_check(virt_host_allow_any, addr, ctx, host)) | ||
if (virt_host_allow_any && virtual_host_match_check(virt_host_allow_any, addr, ctx, host_key, host_key_len)) | ||
return virt_host_allow_any; | ||
return NULL; | ||
} | ||
|
||
static inline bool check_header_value_match(char *target, struct bpf_mem_ptr *head, bool exact) | ||
static inline bool check_header_value_match(struct bpf_sock_addr *ctx, char *header_name, char *target, bool exact) | ||
{ | ||
BPF_LOG(DEBUG, ROUTER_CONFIG, "header match, is exact:%d value:%s\n", exact, target); | ||
long target_length = bpf_strnlen(target, BPF_DATA_MAX_LEN); | ||
if (!exact) | ||
return (bpf__strncmp(target, target_length, _(head->ptr)) == 0); | ||
if (target_length != _(head->size)) | ||
return false; | ||
return (bpf__strncmp(target, target_length, _(head->ptr)) == 0); | ||
int ret = 0; | ||
ret = bpf_km_strncmp(ctx, header_name, BPF_DATA_MAX_LEN, target, BPF_DATA_MAX_LEN); | ||
if (ret == STRNCMP_EXACT) { | ||
return true; | ||
} else if (ret == STRNCMP_PREFIX && !exact) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static inline bool check_headers_match(Route__RouteMatch *match) | ||
static inline bool check_headers_match(struct bpf_sock_addr *ctx, Route__RouteMatch *match) | ||
{ | ||
int i; | ||
void *ptrs = NULL; | ||
|
@@ -182,19 +168,15 @@ static inline bool check_headers_match(Route__RouteMatch *match) | |
BPF_LOG(ERR, ROUTER_CONFIG, "failed to get match headers in route match\n"); | ||
return false; | ||
} | ||
msg_header = (struct bpf_mem_ptr *)bpf_get_msg_header_element(header_name); | ||
if (!msg_header) { | ||
BPF_LOG(DEBUG, ROUTER_CONFIG, "failed to get header value form msg\n"); | ||
return false; | ||
} | ||
BPF_LOG(DEBUG, ROUTER_CONFIG, "header match check, name:%s\n", header_name); | ||
|
||
switch (header_match->header_match_specifier_case) { | ||
case ROUTE__HEADER_MATCHER__HEADER_MATCH_SPECIFIER_EXACT_MATCH: { | ||
config_header_value = KMESH_GET_PTR_VAL(header_match->exact_match, char *); | ||
if (config_header_value == NULL) { | ||
BPF_LOG(ERR, ROUTER_CONFIG, "failed to get config_header_value\n"); | ||
return false; | ||
} | ||
if (!check_header_value_match(config_header_value, msg_header, true)) { | ||
if (!check_header_value_match(ctx, header_name, config_header_value, true)) { | ||
return false; | ||
} | ||
break; | ||
|
@@ -203,8 +185,9 @@ static inline bool check_headers_match(Route__RouteMatch *match) | |
config_header_value = KMESH_GET_PTR_VAL(header_match->prefix_match, char *); | ||
if (config_header_value == NULL) { | ||
BPF_LOG(ERR, ROUTER_CONFIG, "prefix:failed to get config_header_value\n"); | ||
return false; | ||
} | ||
if (!check_header_value_match(config_header_value, msg_header, false)) { | ||
if (!check_header_value_match(ctx, header_name, config_header_value, false)) { | ||
return false; | ||
} | ||
break; | ||
|
@@ -223,10 +206,8 @@ virtual_host_route_match_check(Route__Route *route, address_t *addr, ctx_buff_t | |
Route__RouteMatch *match; | ||
char *prefix; | ||
void *ptr; | ||
|
||
ptr = _(msg->ptr); | ||
if (!ptr) | ||
return 0; | ||
char all_header[4] = {'A', 'l', 'l', '\0'}; | ||
int all_header_len = 4; | ||
|
||
if (!route->match) | ||
return 0; | ||
|
@@ -239,10 +220,11 @@ virtual_host_route_match_check(Route__Route *route, address_t *addr, ctx_buff_t | |
if (!prefix) | ||
return 0; | ||
|
||
if (bpf_strnstr(ptr, prefix, BPF_DATA_MAX_LEN) == NULL) | ||
if (bpf_km_strnstr(ctx, all_header, all_header_len, prefix, BPF_DATA_MAX_LEN) == 0) { | ||
return 0; | ||
} | ||
|
||
if (!check_headers_match(match)) | ||
if (!check_headers_match(ctx, match)) | ||
return 0; | ||
|
||
BPF_LOG(DEBUG, ROUTER_CONFIG, "match route, name=\"%s\"\n", (char *)KMESH_GET_PTR_VAL(route->name, char *)); | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment needs to be supplemented and the usage needs to be defined.
The naming needs to be adjusted.