-
Notifications
You must be signed in to change notification settings - Fork 69
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
Support ads bpf map lookup all #827
Changes from 3 commits
4247987
e50a459
2a341ae
a531634
e3c37f8
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 |
---|---|---|
|
@@ -19,9 +19,15 @@ | |
#include "deserialization_to_bpf_map.h" | ||
#include "../../config/kmesh_marcos_def.h" | ||
|
||
#define LOG_ERR(fmt, args...) printf(fmt, ##args) | ||
#define LOG_WARN(fmt, args...) printf(fmt, ##args) | ||
#define LOG_INFO(fmt, args...) printf(fmt, ##args) | ||
#define PRINTF(fmt, args...) \ | ||
do { \ | ||
printf(fmt, ##args); \ | ||
fflush(stdout); \ | ||
} while (0) | ||
|
||
#define LOG_ERR(fmt, args...) PRINTF(fmt, ##args) | ||
#define LOG_WARN(fmt, args...) PRINTF(fmt, ##args) | ||
#define LOG_INFO(fmt, args...) PRINTF(fmt, ##args) | ||
|
||
struct op_context { | ||
void *key; | ||
|
@@ -37,10 +43,10 @@ struct op_context { | |
const ProtobufCMessageDescriptor *desc; | ||
}; | ||
|
||
#define init_op_context(context, key, val, desc, o_fd, fd, o_info, i_info, m_info) \ | ||
#define init_op_context(context, k, v, desc, o_fd, fd, o_info, i_info, m_info) \ | ||
do { \ | ||
(context).key = (key); \ | ||
(context).value = (val); \ | ||
(context).key = (k); \ | ||
(context).value = (v); \ | ||
(context).desc = (desc); \ | ||
(context).outter_fd = (o_fd); \ | ||
(context).map_fd = (fd); \ | ||
|
@@ -771,6 +777,56 @@ static int repeat_field_query(struct op_context *ctx, const ProtobufCFieldDescri | |
return ret; | ||
} | ||
|
||
void deserial_free_elem_list(struct element_list_node *head) | ||
{ | ||
while (head != NULL) { | ||
struct element_list_node *n = head; | ||
deserial_free_elem(n->elem); | ||
head = n->next; | ||
free(n); | ||
} | ||
} | ||
|
||
static void *create_struct_list(struct op_context *ctx, int *err) | ||
{ | ||
void *prev_key = NULL; | ||
void *value; | ||
struct element_list_node *elem_list_head = NULL; | ||
struct element_list_node *curr_elem_list_node = NULL; | ||
|
||
*err = 0; | ||
ctx->key = calloc(1, ctx->curr_info->key_size); | ||
while (!bpf_map_get_next_key(ctx->curr_fd, prev_key, ctx->key)) { | ||
prev_key = ctx->key; | ||
|
||
value = create_struct(ctx, err); | ||
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. When |
||
if (*err != 0) { | ||
LOG_ERR("create_struct failed, err = %d\n", err); | ||
deserial_free_elem_list(elem_list_head); | ||
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. When an error occurs, resources must be reclaimed and returned in a unified manner. The recommended format is as follows: while (!bpf_map_get_next_key(ctx->curr_fd, prev_key, ctx->key)) {
value = create_struct(ctx, err);
if (*err != 0)
break;
......
struct element_list_node *new_node = (struct element_list_node *)calloc(1, sizeof(struct element_list_node));
if (!new_node) {
*err = -1;
break;
}
.......
}
if (*err) {
deserial_free_elem_list(elem_list_head);
return NULL;
}
return elem_list_head; |
||
value = NULL; | ||
} | ||
|
||
if (value == NULL) { | ||
continue; | ||
} | ||
|
||
struct element_list_node *new_node = (struct element_list_node *)calloc(1, sizeof(struct element_list_node)); | ||
if (!new_node) { | ||
deserial_free_elem_list(elem_list_head); | ||
return NULL; | ||
} | ||
new_node->elem = value; | ||
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. These are basic operations for updating list. It is recommended that you encapsulate them into independent functions or macros or call existing libraries. 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. thanks, will modify it. |
||
new_node->next = NULL; | ||
if (curr_elem_list_node == NULL) { | ||
curr_elem_list_node = elem_list_head = new_node; | ||
} else { | ||
curr_elem_list_node->next = new_node; | ||
curr_elem_list_node = new_node; | ||
} | ||
} | ||
return elem_list_head; | ||
} | ||
|
||
static void *create_struct(struct op_context *ctx, int *err) | ||
{ | ||
void *value; | ||
|
@@ -821,6 +877,61 @@ static void *create_struct(struct op_context *ctx, int *err) | |
return value; | ||
} | ||
|
||
struct element_list_node *deserial_lookup_all_elems(const void *msg_desciptor) | ||
{ | ||
int ret, err; | ||
struct element_list_node *value_list_head = NULL; | ||
const char *map_name = NULL; | ||
struct op_context context = {.inner_map_object = NULL}; | ||
const ProtobufCMessageDescriptor *desc; | ||
struct bpf_map_info outter_info = {0}, inner_info = {0}, info = {0}; | ||
int map_fd, outter_fd = 0, inner_fd = 0; | ||
unsigned int id, outter_id = 0, inner_id = 0; | ||
|
||
if (msg_desciptor == NULL) | ||
return NULL; | ||
|
||
desc = (ProtobufCMessageDescriptor *)msg_desciptor; | ||
if (desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) | ||
return NULL; | ||
|
||
map_name = desc->short_name; | ||
ret = get_map_ids(map_name, &id, &outter_id, &inner_id); | ||
if (ret) | ||
return NULL; | ||
|
||
ret = get_map_fd_info(id, &map_fd, &info); | ||
if (ret < 0) { | ||
LOG_ERR("invalid MAP_ID: %d\n", id); | ||
return NULL; | ||
} | ||
|
||
ret = get_map_fd_info(inner_id, &inner_fd, &inner_info); | ||
ret |= get_map_fd_info(outter_id, &outter_fd, &outter_info); | ||
if (ret < 0 || map_info_check(&outter_info, &inner_info)) | ||
goto end; | ||
|
||
init_op_context(context, NULL, NULL, desc, outter_fd, map_fd, &outter_info, &inner_info, &info); | ||
|
||
value_list_head = create_struct_list(&context, &err); | ||
if (err != 0) { | ||
LOG_ERR("create_struct_list failed, err = %d", err); | ||
deserial_free_elem_list(value_list_head); | ||
value_list_head = NULL; | ||
} | ||
|
||
end: | ||
if (context.key != NULL) | ||
free(context.key); | ||
if (map_fd > 0) | ||
close(map_fd); | ||
if (outter_fd > 0) | ||
close(outter_fd); | ||
if (inner_fd > 0) | ||
close(inner_fd); | ||
return value_list_head; | ||
} | ||
|
||
void *deserial_lookup_elem(void *key, const void *msg_desciptor) | ||
{ | ||
int ret, err; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -441,3 +441,7 @@ func (sc *BpfKmesh) Detach() error { | |
} | ||
return nil | ||
} | ||
|
||
func AdsL7Enabled() bool { | ||
return true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,3 +111,7 @@ | |
} | ||
return nil | ||
} | ||
|
||
func AdsL7Enabled() bool { | ||
return false | ||
} |
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.
why do we need this
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.
without fflush we cannot see the log.
the c printf uses a buffer.
I met this when i am debugging.