1919#include "deserialization_to_bpf_map.h"
2020#include "../../config/kmesh_marcos_def.h"
2121
22- #define LOG_ERR (fmt , args ...) printf(fmt, ##args)
23- #define LOG_WARN (fmt , args ...) printf(fmt, ##args)
24- #define LOG_INFO (fmt , args ...) printf(fmt, ##args)
22+ #define PRINTF (fmt , args ...) \
23+ do { \
24+ printf(fmt, ##args); \
25+ fflush(stdout); \
26+ } while (0)
27+
28+ #define LOG_ERR (fmt , args ...) PRINTF(fmt, ##args)
29+ #define LOG_WARN (fmt , args ...) PRINTF(fmt, ##args)
30+ #define LOG_INFO (fmt , args ...) PRINTF(fmt, ##args)
2531
2632struct op_context {
2733 void * key ;
@@ -37,10 +43,10 @@ struct op_context {
3743 const ProtobufCMessageDescriptor * desc ;
3844};
3945
40- #define init_op_context (context , key , val , desc , o_fd , fd , o_info , i_info , m_info ) \
46+ #define init_op_context (context , k , v , desc , o_fd , fd , o_info , i_info , m_info ) \
4147 do { \
42- (context).key = (key); \
43- (context).value = (val); \
48+ (context).key = (k); \
49+ (context).value = (v); \
4450 (context).desc = (desc); \
4551 (context).outter_fd = (o_fd); \
4652 (context).map_fd = (fd); \
@@ -51,6 +57,16 @@ struct op_context {
5157 (context).curr_fd = (fd); \
5258 } while (0)
5359
60+ #define append_new_node (elem_list_head , curr_elem_list_node , new_node ) \
61+ do { \
62+ if (curr_elem_list_node == NULL) { \
63+ curr_elem_list_node = elem_list_head = new_node; \
64+ } else { \
65+ curr_elem_list_node->next = new_node; \
66+ curr_elem_list_node = new_node; \
67+ } \
68+ } while (0)
69+
5470#define TASK_SIZE (100)
5571struct inner_map_stat {
5672 int map_fd ;
@@ -771,6 +787,55 @@ static int repeat_field_query(struct op_context *ctx, const ProtobufCFieldDescri
771787 return ret ;
772788}
773789
790+ void deserial_free_elem_list (struct element_list_node * head )
791+ {
792+ while (head != NULL ) {
793+ struct element_list_node * n = head ;
794+ deserial_free_elem (n -> elem );
795+ head = n -> next ;
796+ free (n );
797+ }
798+ }
799+
800+ static void * create_struct_list (struct op_context * ctx , int * err )
801+ {
802+ void * prev_key = NULL ;
803+ void * value ;
804+ struct element_list_node * elem_list_head = NULL ;
805+ struct element_list_node * curr_elem_list_node = NULL ;
806+
807+ * err = 0 ;
808+ ctx -> key = calloc (1 , ctx -> curr_info -> key_size );
809+ while (!bpf_map_get_next_key (ctx -> curr_fd , prev_key , ctx -> key )) {
810+ prev_key = ctx -> key ;
811+
812+ value = create_struct (ctx , err );
813+ if (* err ) {
814+ LOG_ERR ("create_struct failed, err = %d\n" , err );
815+ break ;
816+ }
817+
818+ if (value == NULL ) {
819+ continue ;
820+ }
821+
822+ struct element_list_node * new_node = (struct element_list_node * )calloc (1 , sizeof (struct element_list_node ));
823+ if (!new_node ) {
824+ * err = -1 ;
825+ break ;
826+ }
827+
828+ new_node -> elem = value ;
829+ new_node -> next = NULL ;
830+ append_new_node (elem_list_head , curr_elem_list_node , new_node );
831+ }
832+ if (* err ) {
833+ deserial_free_elem_list (elem_list_head );
834+ return NULL ;
835+ }
836+ return elem_list_head ;
837+ }
838+
774839static void * create_struct (struct op_context * ctx , int * err )
775840{
776841 void * value ;
@@ -814,13 +879,71 @@ static void *create_struct(struct op_context *ctx, int *err)
814879 if (ret ) {
815880 LOG_INFO ("field[%d] query fail\n" , i );
816881 * err = 1 ;
817- return value ;
882+ break ;
818883 }
819884 }
820885
886+ if (* err ) {
887+ deserial_free_elem (value );
888+ return NULL ;
889+ }
890+
821891 return value ;
822892}
823893
894+ struct element_list_node * deserial_lookup_all_elems (const void * msg_desciptor )
895+ {
896+ int ret , err ;
897+ struct element_list_node * value_list_head = NULL ;
898+ const char * map_name = NULL ;
899+ struct op_context context = {.inner_map_object = NULL };
900+ const ProtobufCMessageDescriptor * desc ;
901+ struct bpf_map_info outter_info = {0 }, inner_info = {0 }, info = {0 };
902+ int map_fd , outter_fd = 0 , inner_fd = 0 ;
903+ unsigned int id , outter_id = 0 , inner_id = 0 ;
904+
905+ if (msg_desciptor == NULL )
906+ return NULL ;
907+
908+ desc = (ProtobufCMessageDescriptor * )msg_desciptor ;
909+ if (desc -> magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC )
910+ return NULL ;
911+
912+ map_name = desc -> short_name ;
913+ ret = get_map_ids (map_name , & id , & outter_id , & inner_id );
914+ if (ret )
915+ return NULL ;
916+
917+ ret = get_map_fd_info (id , & map_fd , & info );
918+ if (ret < 0 ) {
919+ LOG_ERR ("invalid MAP_ID: %d\n" , id );
920+ return NULL ;
921+ }
922+
923+ ret = get_map_fd_info (inner_id , & inner_fd , & inner_info );
924+ ret |= get_map_fd_info (outter_id , & outter_fd , & outter_info );
925+ if (ret < 0 || map_info_check (& outter_info , & inner_info ))
926+ goto end ;
927+
928+ init_op_context (context , NULL , NULL , desc , outter_fd , map_fd , & outter_info , & inner_info , & info );
929+
930+ value_list_head = create_struct_list (& context , & err );
931+ if (err != 0 ) {
932+ LOG_ERR ("create_struct_list failed, err = %d" , err );
933+ }
934+
935+ end :
936+ if (context .key != NULL )
937+ free (context .key );
938+ if (map_fd > 0 )
939+ close (map_fd );
940+ if (outter_fd > 0 )
941+ close (outter_fd );
942+ if (inner_fd > 0 )
943+ close (inner_fd );
944+ return value_list_head ;
945+ }
946+
824947void * deserial_lookup_elem (void * key , const void * msg_desciptor )
825948{
826949 int ret , err ;
@@ -860,8 +983,7 @@ void *deserial_lookup_elem(void *key, const void *msg_desciptor)
860983 normalize_key (& context , key , map_name );
861984 value = create_struct (& context , & err );
862985 if (err != 0 ) {
863- deserial_free_elem (value );
864- value = NULL ;
986+ LOG_ERR ("create_struct failed, err = %d\n" , err );
865987 }
866988
867989end :
0 commit comments