diff --git a/bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c b/bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c index f218266b7160f9ea57591073f1756feea9f9e3d6..533300f512fb566e8ce7a60f2bd05ebfe7590dba 100644 --- a/bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c +++ b/bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c @@ -16,8 +16,6 @@ #include "deserialization_to_bpf_map.h" -#define MAX_ERRNO 4095 - #define LOG_ERR(fmt, args...) printf(fmt, ## args) #define LOG_WARN(fmt, args...) printf(fmt, ## args) #define LOG_INFO(fmt, args...) printf(fmt, ## args) @@ -30,17 +28,17 @@ struct outter_map_alloc_control { }; struct op_context { - void *key; - void *value; + void* key; + void* value; int outter_fd; int map_fd; int curr_fd; - struct bpf_map_info *outter_info; - struct bpf_map_info *inner_info; - struct bpf_map_info *info; - struct bpf_map_info *curr_info; - char *inner_map_object; - const ProtobufCMessageDescriptor *desc; + struct bpf_map_info* outter_info; + struct bpf_map_info* inner_info; + struct bpf_map_info* info; + struct bpf_map_info* curr_info; + char* inner_map_object; + const ProtobufCMessageDescriptor* desc; }; #define init_op_context(context, key, val, desc, o_fd, fd, o_info, \ @@ -58,17 +56,20 @@ do { \ (context).curr_fd = (fd); \ } while (0) -static int update_bpf_map(struct op_context *ctx); -static void* create_struct(struct op_context *ctx, int *err); -static int del_bpf_map(struct op_context *ctx, int is_inner); -static int free_outter_map_entry(struct op_context *ctx, void *outter_key); +static int update_bpf_map(struct op_context* ctx); +static void* create_struct(struct op_context* ctx, int* err); +static int del_bpf_map(struct op_context* ctx, int is_inner); +static int free_outter_map_entry(struct op_context* ctx, void* outter_key); -static int normalize_key(struct op_context *ctx, void *key, const char *map_name) +static int normalize_key(struct op_context* ctx, void* key, const char* map_name) { ctx->key = calloc(1, ctx->curr_info->key_size); if (!ctx->key) return -errno; + if (!map_name) + return -errno; + // TODO if (!strncmp(map_name, "Listener", strlen(map_name))) memcpy_s(ctx->key, ctx->curr_info->key_size, key, ctx->curr_info->key_size); @@ -78,8 +79,8 @@ static int normalize_key(struct op_context *ctx, void *key, const char *map_name return 0; } -static inline int selected_oneof_field(void *value, - const ProtobufCFieldDescriptor *field) +static inline int selected_oneof_field(void* value, + const ProtobufCFieldDescriptor* field) { uint32_t n = *(uint32_t*)((char*)value + field->quantifier_offset); @@ -89,8 +90,8 @@ static inline int selected_oneof_field(void *value, return 1; } -static inline int valid_field_value(void *value, - const ProtobufCFieldDescriptor *field) +static inline int valid_field_value(void* value, + const ProtobufCFieldDescriptor* field) { uint32_t val = *(uint32_t*)((char*)value + field->offset); @@ -136,7 +137,7 @@ static inline size_t sizeof_elt_in_repeated_array(ProtobufCType type) return sizeof(protobuf_c_boolean); case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_MESSAGE: - return sizeof(void *); + return sizeof(void*); case PROTOBUF_C_TYPE_BYTES: return sizeof(ProtobufCBinaryData); default: @@ -146,7 +147,7 @@ static inline size_t sizeof_elt_in_repeated_array(ProtobufCType type) return 0; } -static inline int valid_outter_key(struct op_context *ctx, unsigned int outter_key) +static inline int valid_outter_key(struct op_context* ctx, unsigned int outter_key) { if (outter_key >= ctx->outter_info->max_entries || !outter_key) return 0; @@ -154,7 +155,7 @@ static inline int valid_outter_key(struct op_context *ctx, unsigned int outter_k return 1; } -static void free_elem(void *ptr) +static void free_elem(void* ptr) { if ((uintptr_t)ptr < MAX_OUTTER_MAP_ENTRIES) return; @@ -162,9 +163,10 @@ static void free_elem(void *ptr) free(ptr); } -static void free_keys(struct op_context *ctx, void *keys, int n) +static void free_keys(struct op_context* ctx, void* keys, int n) { - int i, key; + int i; + unsigned int key; for (i = 0; i < n; i++) { key = *((uintptr_t*)keys + i); @@ -175,10 +177,10 @@ static void free_keys(struct op_context *ctx, void *keys, int n) } } -static int get_map_ids(const char *name, int *id, int *outter_id, int *inner_id) +static int get_map_ids(const char* name, unsigned int* id, unsigned int* outter_id, unsigned int* inner_id) { - char *ptr = NULL; - char *map_id, *map_in_map_id; + char* ptr = NULL; + char* map_id, * map_in_map_id; map_id = (name == NULL) ? getenv("MAP_ID") : getenv(name); if (!map_id) { @@ -188,12 +190,12 @@ static int get_map_ids(const char *name, int *id, int *outter_id, int *inner_id) errno = 0; - *id = strtol(map_id, &ptr, 10); + *id = (unsigned int)strtol(map_id, &ptr, 10); if (!ptr[0]) { map_in_map_id = getenv("OUTTER_MAP_ID"); if (!map_in_map_id) return 0; - *outter_id = strtol(map_in_map_id, &ptr, 10); + *outter_id = (unsigned int)strtol(map_in_map_id, &ptr, 10); } if (!ptr[0]) { @@ -202,12 +204,12 @@ static int get_map_ids(const char *name, int *id, int *outter_id, int *inner_id) LOG_ERR("INNER_MAP_ID is not set\n"); return -EINVAL; } - *inner_id = strtol(map_in_map_id, &ptr, 10); + *inner_id = (unsigned int)strtol(map_in_map_id, &ptr, 10); } return -errno; } -static int get_map_fd_info(int id, int *map_fd, struct bpf_map_info *info) +static int get_map_fd_info(unsigned int id, int* map_fd, struct bpf_map_info* info) { int ret; __u32 map_info_len; @@ -221,14 +223,14 @@ static int get_map_fd_info(int id, int *map_fd, struct bpf_map_info *info) return ret; } -static int alloc_and_set_inner_map(struct op_context *ctx, int key) +static int alloc_and_set_inner_map(struct op_context* ctx, int key) { int fd, ret; - struct bpf_map_info *inner_info = ctx->inner_info; + struct bpf_map_info* inner_info = ctx->inner_info; - fd = bpf_create_map_name(inner_info->type, NULL, inner_info->key_size, - inner_info->value_size, inner_info->max_entries, - inner_info->map_flags); + fd = bpf_create_map_name(inner_info->type, NULL, ()inner_info->key_size, + inner_info->value_size, inner_info->max_entries, + inner_info->map_flags); if (fd < 0) return fd; @@ -238,8 +240,8 @@ static int alloc_and_set_inner_map(struct op_context *ctx, int key) return ret; } -static int find_free_outter_map_entry(struct op_context *ctx, - struct outter_map_alloc_control *a_ctl) +static int find_free_outter_map_entry(struct op_context* ctx, + struct outter_map_alloc_control* a_ctl) { int index; unsigned int i; @@ -255,15 +257,15 @@ static int find_free_outter_map_entry(struct op_context *ctx, return -ENOENT; } -static int free_outter_map_entry(struct op_context *ctx, void *outter_key) +static int free_outter_map_entry(struct op_context* ctx, void* outter_key) { int ret; int key = 0; unsigned int i = *(unsigned int*)outter_key; int inner_map_fd; __u32 inner_map_id; - struct outter_map_alloc_control *a_ctl; - void *inner_map_object; + struct outter_map_alloc_control* a_ctl; + void* inner_map_object; ret = bpf_map_delete_elem(ctx->outter_fd, outter_key); if (ret) @@ -290,7 +292,7 @@ static int free_outter_map_entry(struct op_context *ctx, void *outter_key) return ret; } - a_ctl = (struct outter_map_alloc_control *)inner_map_object; + a_ctl = (struct outter_map_alloc_control*)inner_map_object; a_ctl->used--; a_ctl->free_map[i / 32] |= (1U << (i % 32)); bpf_map_update_elem(inner_map_fd, &key, a_ctl, BPF_ANY); @@ -300,14 +302,14 @@ static int free_outter_map_entry(struct op_context *ctx, void *outter_key) return 0; } -static int alloc_outter_map_entry(struct op_context *ctx) +static int alloc_outter_map_entry(struct op_context* ctx) { int ret; unsigned int first = 0; int key = 0, ret_key; int inner_map_fd; __u32 inner_map_id; - struct outter_map_alloc_control *a_ctl; + struct outter_map_alloc_control* a_ctl; retry: ret = bpf_map_lookup_elem(ctx->outter_fd, &key, &inner_map_id); @@ -332,8 +334,8 @@ retry: return ret; } - a_ctl = (struct outter_map_alloc_control *)ctx->inner_map_object; - if (a_ctl->used >= ctx->outter_info->max_entries) { + a_ctl = (struct outter_map_alloc_control*)ctx->inner_map_object; + if (a_ctl->used >= (int)ctx->outter_info->max_entries) { LOG_ERR("outter map entries has consumed out\n"); close(inner_map_fd); return -ENOENT; @@ -341,7 +343,7 @@ retry: if (first) { memset_s(a_ctl->free_map, sizeof(a_ctl->free_map), 0xff, sizeof(a_ctl->free_map)); - a_ctl->free_map[0] &= ~(1U << 0); + a_ctl->free_map[0] &= ~1U; } ret_key = find_free_outter_map_entry(ctx, a_ctl); @@ -360,7 +362,7 @@ retry: return ret_key; } -static int outter_key_to_inner_fd(struct op_context *ctx, int key) +static int outter_key_to_inner_fd(struct op_context* ctx, unsigned long key) { int ret; __u32 inner_map_id; @@ -372,16 +374,16 @@ static int outter_key_to_inner_fd(struct op_context *ctx, int key) return bpf_map_get_fd_by_id(inner_map_id); } -static int copy_sfield_to_map(struct op_context *ctx, int o_index, - const ProtobufCFieldDescriptor *field) +static int copy_sfield_to_map(struct op_context* ctx, int o_index, + const ProtobufCFieldDescriptor* field) { int ret; int key = 0; int inner_fd; - char **value = (char**)((char*)ctx->value + field->offset); - char *save_value = *value; + char** value = (char**)((char*)ctx->value + field->offset); + char* save_value = *value; - *(uintptr_t *)value = o_index; + *(uintptr_t*)value = (uintptr_t)o_index; ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY); if (ret) { free_outter_map_entry(ctx, &o_index); @@ -398,18 +400,18 @@ static int copy_sfield_to_map(struct op_context *ctx, int o_index, return ret; } -static int copy_msg_field_to_map(struct op_context *ctx, int o_index, - const ProtobufCFieldDescriptor *field) +static int copy_msg_field_to_map(struct op_context* ctx, int o_index, + const ProtobufCFieldDescriptor* field) { int ret; int key = 0; int inner_fd; - void **value = (void**)((char*)ctx->value + field->offset); - void *msg = *value; + void** value = (void**)((char*)ctx->value + field->offset); + void* msg = *value; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; + const ProtobufCMessageDescriptor* desc; - *(uintptr_t*)value = o_index; + *(uintptr_t*)value = (uintptr_t)o_index; ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY); if (ret) { free_outter_map_entry(ctx, &o_index); @@ -427,7 +429,7 @@ static int copy_msg_field_to_map(struct op_context *ctx, int o_index, new_ctx.value = msg; new_ctx.curr_info = ctx->inner_info; - desc = ((ProtobufCMessage *)new_ctx.value)->descriptor; + desc = ((ProtobufCMessage*)new_ctx.value)->descriptor; if (!desc || desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) { close(inner_fd); return -EINVAL; @@ -440,8 +442,8 @@ static int copy_msg_field_to_map(struct op_context *ctx, int o_index, return ret; } -static int field_handle(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int field_handle(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int key = 0; @@ -464,13 +466,13 @@ static int field_handle(struct op_context *ctx, return 0; } -static int copy_indirect_data_to_map(struct op_context *ctx, int outter_key, - void *value, ProtobufCType type) +static int copy_indirect_data_to_map(struct op_context* ctx, int outter_key, + void* value, ProtobufCType type) { int ret = 0; int inner_fd, key = 0; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; + const ProtobufCMessageDescriptor* desc; inner_fd = outter_key_to_inner_fd(ctx, outter_key); if (inner_fd < 0) @@ -484,7 +486,7 @@ static int copy_indirect_data_to_map(struct op_context *ctx, int outter_key, new_ctx.value = value; new_ctx.curr_info = ctx->inner_info; - desc = ((ProtobufCMessage *)value)->descriptor; + desc = ((ProtobufCMessage*)value)->descriptor; if (!desc || desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) { close(inner_fd); return -EINVAL; @@ -496,7 +498,7 @@ static int copy_indirect_data_to_map(struct op_context *ctx, int outter_key, case PROTOBUF_C_TYPE_STRING: strcpy_s(ctx->inner_map_object, ctx->inner_info->value_size, (char*)value); ret = bpf_map_update_elem(inner_fd, &key, - ctx->inner_map_object, BPF_ANY); + ctx->inner_map_object, BPF_ANY); break; default: break; @@ -517,22 +519,22 @@ static bool indirect_data_type(ProtobufCType type) } } -static int repeat_field_handle(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int repeat_field_handle(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int ret, ret1; - unsigned int i; + size_t i; int outter_key, inner_fd, key = 0; - void *n = ((char*)ctx->value) + field->quantifier_offset; - void ***value = (void***)((char*)ctx->value + field->offset); - void **origin_value = *value; - char *inner_map_object; + void* n = ((char*)ctx->value) + field->quantifier_offset; + void*** value = (void***)((char*)ctx->value + field->offset); + void** origin_value = *value; + char* inner_map_object; outter_key = alloc_outter_map_entry(ctx); if (outter_key < 0) return outter_key; - *(uintptr_t*)value = outter_key; + *(uintptr_t*)value = (size_t)outter_key; ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY); if (ret) { free_outter_map_entry(ctx, &outter_key); @@ -557,7 +559,7 @@ static int repeat_field_handle(struct op_context *ctx, if (outter_key < 0) goto end; - *((uintptr_t*)inner_map_object + i) = outter_key; + *((uintptr_t*)inner_map_object + i) = (size_t)outter_key; ret = copy_indirect_data_to_map(ctx, outter_key, origin_value[i], field->type); if (ret) @@ -584,12 +586,12 @@ end: return ret; } -static int update_bpf_map(struct op_context *ctx) +static int update_bpf_map(struct op_context* ctx) { int ret; unsigned int i; - void *temp_val; - const ProtobufCMessageDescriptor *desc = ctx->desc; + void* temp_val; + const ProtobufCMessageDescriptor* desc = ctx->desc; if (desc->sizeof_message > ctx->curr_info->value_size) { LOG_ERR("map entry size is too small\n"); @@ -604,7 +606,7 @@ static int update_bpf_map(struct op_context *ctx) ctx->value = temp_val; for (i = 0; i < desc->n_fields; i++) { - const ProtobufCFieldDescriptor *field = desc->fields + i; + const ProtobufCFieldDescriptor* field = desc->fields + i; if (!selected_oneof_field(ctx->value, field) || !valid_field_value(ctx->value, field)) @@ -631,8 +633,8 @@ static int update_bpf_map(struct op_context *ctx) return ret; } -static int map_info_check(struct bpf_map_info *outter_info, - struct bpf_map_info *inner_info) +static int map_info_check(struct bpf_map_info* outter_info, + struct bpf_map_info* inner_info) { if (outter_info->type != BPF_MAP_TYPE_ARRAY_OF_MAPS) { LOG_ERR("outter map type must be BPF_MAP_TYPE_ARRAY_OF_MAPS\n"); @@ -642,12 +644,12 @@ static int map_info_check(struct bpf_map_info *outter_info, if (outter_info->max_entries < 2 || outter_info->max_entries > MAX_OUTTER_MAP_ENTRIES) { LOG_ERR("outter map max_entries must be in[2,%d]\n", - MAX_OUTTER_MAP_ENTRIES); + MAX_OUTTER_MAP_ENTRIES); return -EINVAL; } if (inner_info->value_size < sizeof(struct outter_map_alloc_control)) { - LOG_ERR("inner map value_size must be large than %ld(bytes)\n", + LOG_ERR("inner map value_size must be large than %u(bytes)\n", sizeof(struct outter_map_alloc_control)); return -EINVAL; } @@ -655,20 +657,20 @@ static int map_info_check(struct bpf_map_info *outter_info, return 0; } -int deserial_update_elem(void *key, void *value) +int deserial_update_elem(void* key, void* value) { int ret; - const char *map_name = NULL; - struct op_context context = {.inner_map_object = NULL}; - const ProtobufCMessageDescriptor *desc; + const char* map_name = NULL; + struct op_context context = { .inner_map_object = NULL }; + const ProtobufCMessageDescriptor* desc; struct bpf_map_info outter_info, inner_info, info; int map_fd, outter_fd = 0, inner_fd = 0; - int id, outter_id = 0, inner_id = 0; + unsigned int id, outter_id = 0, inner_id = 0; if (!key || !value) return -EINVAL; - desc = ((ProtobufCMessage *)value)->descriptor; + desc = ((ProtobufCMessage*)value)->descriptor; if (desc && desc->magic == PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) map_name = desc->short_name; @@ -695,7 +697,7 @@ int deserial_update_elem(void *key, void *value) deserial_delete_elem(key, desc); init_op_context(context, key, value, desc, outter_fd, map_fd, - &outter_info, &inner_info, &info); + &outter_info, &inner_info, &info); context.inner_map_object = calloc(1, context.inner_info->value_size); if (context.inner_map_object == NULL) { @@ -723,13 +725,13 @@ end: return ret; } -static int query_string_field(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int query_string_field(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int key = 0, ret; int inner_fd; - void *string; - void *outter_key = (void*)((char*)ctx->value + field->offset); + void* string; + void* outter_key = (void*)((char*)ctx->value + field->offset); inner_fd = outter_key_to_inner_fd(ctx, *(int*)outter_key); if (inner_fd < 0) @@ -741,23 +743,23 @@ static int query_string_field(struct op_context *ctx, return -ENOMEM; } - (*(uintptr_t *)outter_key) = (uintptr_t)string; + (*(uintptr_t*)outter_key) = (uintptr_t)string; ret = bpf_map_lookup_elem(inner_fd, &key, string); close(inner_fd); return ret; } -static int query_message_field(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int query_message_field(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int ret; int key = 0; int inner_fd; - void *message; + void* message; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; - uintptr_t *outter_key = (uintptr_t *)((char*)ctx->value + field->offset); + const ProtobufCMessageDescriptor* desc; + uintptr_t* outter_key = (uintptr_t*)((char*)ctx->value + field->offset); inner_fd = outter_key_to_inner_fd(ctx, *outter_key); if (inner_fd < 0) @@ -775,15 +777,15 @@ static int query_message_field(struct op_context *ctx, } new_ctx.desc = desc; - + message = create_struct(&new_ctx, &ret); *outter_key = (uintptr_t)message; close(inner_fd); - return ret; + return ret; } -static int field_query(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int field_query(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { switch (field->type) { case PROTOBUF_C_TYPE_MESSAGE: @@ -797,14 +799,14 @@ static int field_query(struct op_context *ctx, return 0; } -static void* create_indirect_struct(struct op_context *ctx, int outter_key, - const ProtobufCFieldDescriptor *field, - int *err) +static void* create_indirect_struct(struct op_context* ctx, unsigned long outter_key, + const ProtobufCFieldDescriptor* field, + int* err) { int inner_fd, key = 0; - void *value; + void* value; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; + const ProtobufCMessageDescriptor* desc; inner_fd = outter_key_to_inner_fd(ctx, outter_key); if (inner_fd < 0) { @@ -851,17 +853,17 @@ static void* create_indirect_struct(struct op_context *ctx, int outter_key, *err = 0; return value; } - -static int repeat_field_query(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) + +static int repeat_field_query(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int ret; int key = 0; int inner_fd; - void *array; - unsigned int i; - void *n = ((char*)ctx->value) + field->quantifier_offset; - uintptr_t *outter_key = (uintptr_t*)((char*)ctx->value + field->offset); + void* array; + size_t i; + void* n = ((char*)ctx->value) + field->quantifier_offset; + uintptr_t* outter_key = (uintptr_t*)((char*)ctx->value + field->offset); inner_fd = outter_key_to_inner_fd(ctx, *outter_key); if (inner_fd < 0) @@ -886,7 +888,7 @@ static int repeat_field_query(struct op_context *ctx, for (i = 0; i < *(size_t*)n; i++) { outter_key = (uintptr_t*)array + i; *outter_key = (uintptr_t)create_indirect_struct(ctx, - *outter_key, field, &ret); + *outter_key, field, &ret); if (ret) break; } @@ -899,12 +901,12 @@ static int repeat_field_query(struct op_context *ctx, return ret; } -static void* create_struct(struct op_context *ctx, int *err) +static void* create_struct(struct op_context* ctx, int* err) { - void *value; + void* value; int ret; unsigned int i; - const ProtobufCMessageDescriptor *desc = ctx->desc; + const ProtobufCMessageDescriptor* desc = ctx->desc; *err = 0; @@ -925,7 +927,7 @@ static void* create_struct(struct op_context *ctx, int *err) ctx->value = value; for (i = 0; i < desc->n_fields; i++) { - const ProtobufCFieldDescriptor *field = desc->fields + i; + const ProtobufCFieldDescriptor* field = desc->fields + i; if (!selected_oneof_field(ctx->value, field) || !valid_field_value(ctx->value, field)) @@ -950,16 +952,16 @@ static void* create_struct(struct op_context *ctx, int *err) return value; } -void* deserial_lookup_elem(void *key, const void *msg_desciptor) +void* deserial_lookup_elem(void* key, const void* msg_desciptor) { int ret, err; - void *value = NULL; - const char *map_name = NULL; - struct op_context context = {.inner_map_object = NULL}; - const ProtobufCMessageDescriptor *desc; + void* value = NULL; + const char* map_name = NULL; + struct op_context context = { .inner_map_object = NULL }; + const ProtobufCMessageDescriptor* desc; struct bpf_map_info outter_info, inner_info, info; int map_fd, outter_fd = 0, inner_fd = 0; - int id, outter_id = 0, inner_id = 0; + unsigned int id, outter_id = 0, inner_id = 0; if (msg_desciptor == NULL || key == NULL) return NULL; @@ -985,7 +987,7 @@ void* deserial_lookup_elem(void *key, const void *msg_desciptor) goto end; init_op_context(context, key, NULL, desc, outter_fd, map_fd, - &outter_info, &inner_info, &info); + &outter_info, &inner_info, &info); normalize_key(&context, key, map_name); value = create_struct(&context, &err); @@ -1006,65 +1008,65 @@ end: return value; } -static int indirect_field_del(struct op_context *ctx, unsigned int outter_key, - const ProtobufCFieldDescriptor *field) +static int indirect_field_del(struct op_context* ctx, unsigned int outter_key, + const ProtobufCFieldDescriptor* field) { - char *inner_map_object = NULL; + char* inner_map_object = NULL; int inner_fd, key = 0; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; - + const ProtobufCMessageDescriptor* desc; + if (!valid_outter_key(ctx, outter_key)) return -EINVAL; - - inner_fd = outter_key_to_inner_fd(ctx, outter_key); + + inner_fd = outter_key_to_inner_fd(ctx, (unsigned long)outter_key); if (inner_fd < 0) return inner_fd; free_outter_map_entry(ctx, &outter_key); switch (field->type) { - case PROTOBUF_C_TYPE_MESSAGE: - desc = (ProtobufCMessageDescriptor*)field->descriptor; - if (!desc || desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) { - close(inner_fd); - return -EINVAL; - } - - inner_map_object = malloc(ctx->inner_info->value_size); - if (!inner_map_object) { - close(inner_fd); - return -ENOMEM; - } - - memcpy_s(&new_ctx, sizeof(new_ctx), ctx, sizeof(*ctx)); - new_ctx.curr_fd = inner_fd; - new_ctx.key = (void*)&key; - new_ctx.curr_info = ctx->inner_info; - new_ctx.value = inner_map_object; - new_ctx.desc = desc; - - (void)del_bpf_map(&new_ctx, 1); - free(inner_map_object); - break; - - default: - break; + case PROTOBUF_C_TYPE_MESSAGE: + desc = (ProtobufCMessageDescriptor*)field->descriptor; + if (!desc || desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) { + close(inner_fd); + return -EINVAL; + } + + inner_map_object = malloc(ctx->inner_info->value_size); + if (!inner_map_object) { + close(inner_fd); + return -ENOMEM; + } + + memcpy_s(&new_ctx, sizeof(new_ctx), ctx, sizeof(*ctx)); + new_ctx.curr_fd = inner_fd; + new_ctx.key = (void*)&key; + new_ctx.curr_info = ctx->inner_info; + new_ctx.value = inner_map_object; + new_ctx.desc = desc; + + (void)del_bpf_map(&new_ctx, 1); + free(inner_map_object); + break; + + default: + break; } close(inner_fd); return 0; } -static int repeat_field_del(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int repeat_field_del(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int ret; unsigned int i; int inner_fd, key = 0; - void *inner_map_object = NULL; - void *n; - uintptr_t *outter_key; + void* inner_map_object = NULL; + void* n; + uintptr_t* outter_key; ret = bpf_map_lookup_elem(ctx->curr_fd, ctx->key, ctx->value); if (ret < 0) { @@ -1115,14 +1117,14 @@ end: return ret; } -static int msg_field_del(struct op_context *ctx, int inner_fd, - const ProtobufCFieldDescriptor *field) +static int msg_field_del(struct op_context* ctx, int inner_fd, + const ProtobufCFieldDescriptor* field) { int key = 0; int ret; - char *inner_map_object = NULL; + char* inner_map_object = NULL; struct op_context new_ctx; - const ProtobufCMessageDescriptor *desc; + const ProtobufCMessageDescriptor* desc; desc = (ProtobufCMessageDescriptor*)field->descriptor; if (!desc || desc->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) @@ -1144,12 +1146,12 @@ static int msg_field_del(struct op_context *ctx, int inner_fd, return ret; } -static int field_del(struct op_context *ctx, - const ProtobufCFieldDescriptor *field) +static int field_del(struct op_context* ctx, + const ProtobufCFieldDescriptor* field) { int ret; int inner_fd; - uintptr_t *outter_key; + uintptr_t* outter_key; switch (field->type) { case PROTOBUF_C_TYPE_MESSAGE: @@ -1183,19 +1185,19 @@ static int field_del(struct op_context *ctx, return 0; } -static int del_bpf_map(struct op_context *ctx, int is_inner) +static int del_bpf_map(struct op_context* ctx, int is_inner) { int ret; unsigned int i; - const ProtobufCMessageDescriptor *desc = ctx->desc; + const ProtobufCMessageDescriptor* desc = ctx->desc; ret = bpf_map_lookup_elem(ctx->curr_fd, ctx->key, ctx->value); if (ret < 0) LOG_WARN("failed to find map(%d) elem:%d.", ctx->curr_fd, ret); - return ret; + return ret; for (i = 0; i < desc->n_fields; i++) { - const ProtobufCFieldDescriptor *field = desc->fields + i; + const ProtobufCFieldDescriptor* field = desc->fields + i; if (!selected_oneof_field(ctx->value, field) || !valid_field_value(ctx->value, field)) @@ -1220,21 +1222,21 @@ end: bpf_map_delete_elem(ctx->curr_fd, ctx->key); } -int deserial_delete_elem(void *key, const void *msg_desciptor) +int deserial_delete_elem(void* key, const void* msg_desciptor) { int ret; - const char *map_name = NULL; - struct op_context context = {.inner_map_object = NULL}; - const ProtobufCMessageDescriptor *desc; + const char* map_name = NULL; + struct op_context context = { .inner_map_object = NULL }; + const ProtobufCMessageDescriptor* desc; struct bpf_map_info outter_info, inner_info, info; int map_fd, outter_fd = 0, inner_fd = 0; - int id, outter_id = 0, inner_id = 0; - char *inner_map_object = NULL; + unsigned int id, outter_id = 0, inner_id = 0; + char* inner_map_object = NULL; if (!key || !msg_desciptor) return -EINVAL; - desc = (ProtobufCMessageDescriptor *)msg_desciptor; + desc = (ProtobufCMessageDescriptor*)msg_desciptor; if (desc->magic == PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) map_name = desc->short_name; @@ -1259,7 +1261,7 @@ int deserial_delete_elem(void *key, const void *msg_desciptor) goto end; init_op_context(context, key, NULL, desc, outter_fd, map_fd, - &outter_info, &inner_info, &info); + &outter_info, &inner_info, &info); context.inner_map_object = calloc(1, context.inner_info->value_size); context.value = calloc(1, context.curr_info->value_size); @@ -1290,17 +1292,17 @@ end: } -static void repeat_field_free(void *value, - const ProtobufCFieldDescriptor *field) +static void repeat_field_free(void* value, + const ProtobufCFieldDescriptor* field) { unsigned int i; - void *n = ((char*)value) + field->quantifier_offset; - uintptr_t *ptr_array = *(uintptr_t**)((char*)value + field->offset); + void* n = ((char*)value) + field->quantifier_offset; + uintptr_t* ptr_array = *(uintptr_t**)((char*)value + field->offset); switch (field->type) { case PROTOBUF_C_TYPE_MESSAGE: case PROTOBUF_C_TYPE_STRING: - for (i = 0; i < *(size_t*)n; i++) { + for (i = 0; i < *(unsigned int*)n; i++) { if (field->type == PROTOBUF_C_TYPE_STRING) free_elem((void*)ptr_array[i]); else @@ -1315,10 +1317,10 @@ static void repeat_field_free(void *value, return; } -static void field_free(void *value, - const ProtobufCFieldDescriptor *field) +static void field_free(void* value, + const ProtobufCFieldDescriptor* field) { - uintptr_t *tobe_free = (uintptr_t *)((char*)value + field->offset); + uintptr_t* tobe_free = (uintptr_t*)((char*)value + field->offset); switch (field->type) { case PROTOBUF_C_TYPE_MESSAGE: @@ -1334,16 +1336,16 @@ static void field_free(void *value, return; } -void deserial_free_elem(void *value) +void deserial_free_elem(void* value) { unsigned int i; - const char *map_name = NULL; - const ProtobufCMessageDescriptor *desc; + const char* map_name = NULL; + const ProtobufCMessageDescriptor* desc; if (!value || (uintptr_t)value < MAX_OUTTER_MAP_ENTRIES) return; - desc = ((ProtobufCMessage *)value)->descriptor; + desc = ((ProtobufCMessage*)value)->descriptor; if (desc && desc->magic == PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) map_name = desc->short_name; @@ -1354,7 +1356,7 @@ void deserial_free_elem(void *value) } for (i = 0; i < desc->n_fields; i++) { - const ProtobufCFieldDescriptor *field = desc->fields + i; + const ProtobufCFieldDescriptor* field = desc->fields + i; if (!selected_oneof_field(value, field) || !valid_field_value(value, field))