diff --git a/include/stellar/stellar.h b/include/stellar/stellar.h index 152beb5..7929f25 100644 --- a/include/stellar/stellar.h +++ b/include/stellar/stellar.h @@ -60,7 +60,7 @@ void stellar_emit_datapath_telemetry(struct packet *pkt, const char * module, c int stellar_get_worker_thread_num(struct stellar *st); uint16_t stellar_get_current_thread_index(); -// only send user crafted packet, can't send packet which come from network +// only send user build packet, can't send packet which come from network void stellar_send_build_packet(struct stellar *st, struct packet *pkt); struct stellar; diff --git a/infra/CMakeLists.txt b/infra/CMakeLists.txt index 353a05f..5e4b228 100644 --- a/infra/CMakeLists.txt +++ b/infra/CMakeLists.txt @@ -1,4 +1,4 @@ -set(INFRA tuple packet_parser packet_io ip_reassembly tcp_reassembly session_manager plugin_manager core) +set(INFRA tuple packet_parser packet_io ip_reassembly tcp_reassembly session_manager plugin_manager) set(DEPS bitmap dablooms interval_tree logger nmx_pool rbtree timeout toml) set(DECODERS http lpi) set(WHOLE_ARCHIVE ${DEPS} ${INFRA} ${DECODERS}) @@ -9,13 +9,13 @@ endforeach() file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/stellar_lib.c "") -add_library(stellar_lib SHARED stellar_lib.c) +add_library(stellar_lib SHARED stellar_lib.c stellar_core.c stellar_stat.c) set_target_properties(stellar_lib PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") target_link_libraries(stellar_lib PRIVATE pthread -Wl,--whole-archive ${WHOLE_ARCHIVE} -Wl,--no-whole-archive) target_link_options(stellar_lib PRIVATE -rdynamic) set_target_properties(stellar_lib PROPERTIES OUTPUT_NAME "stellar") -add_executable(stellar main.c) +add_executable(stellar main.c stellar_core.c stellar_stat.c) set_target_properties(stellar PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") target_link_libraries(stellar PRIVATE pthread -Wl,--whole-archive ${WHOLE_ARCHIVE} -Wl,--no-whole-archive) target_link_options(stellar PRIVATE -rdynamic) diff --git a/infra/core/CMakeLists.txt b/infra/core/CMakeLists.txt deleted file mode 100644 index 89b5b0f..0000000 --- a/infra/core/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ - -add_library(core stellar_stat.c stellar_core.c) -target_link_libraries(core PUBLIC packet_io ip_reassembly plugin_manager) diff --git a/infra/packet_parser/CMakeLists.txt b/infra/packet_parser/CMakeLists.txt index a44fab4..f9e4995 100644 --- a/infra/packet_parser/CMakeLists.txt +++ b/infra/packet_parser/CMakeLists.txt @@ -9,7 +9,7 @@ target_include_directories(packet_parser PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(packet_parser PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash) target_include_directories(packet_parser PUBLIC ${CMAKE_SOURCE_DIR}/deps/logger) target_include_directories(packet_parser PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_include_directories(packet_parser PUBLIC ${CMAKE_SOURCE_DIR}/infra/core) +target_include_directories(packet_parser PUBLIC ${CMAKE_SOURCE_DIR}/infra) target_link_libraries(packet_parser tuple logger dablooms) add_subdirectory(test) \ No newline at end of file diff --git a/infra/packet_parser/packet_builder.c b/infra/packet_parser/packet_builder.c index 64e77d8..fc95a81 100644 --- a/infra/packet_parser/packet_builder.c +++ b/infra/packet_parser/packet_builder.c @@ -79,7 +79,7 @@ static void update_gtp1_hdr(struct gtp1_hdr *gtp, int trim_len) gtp1_hdr_set_msg_len(gtp, msg_len - trim_len); if (gtp1_hdr_get_seq_flag(gtp) && gtp1_hdr_get_seq(gtp)) { - PACKET_CRAFT_LOG_ERROR("crafted packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number"); + PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number"); } } @@ -89,7 +89,7 @@ static void update_gtp2_hdr(struct gtp2_hdr *gtp, int trim_len) gtp2_hdr_set_msg_len(gtp, msg_len - trim_len); if (gtp2_hdr_get_seq(gtp)) { - PACKET_CRAFT_LOG_ERROR("crafted packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number"); + PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number"); } } diff --git a/infra/plugin_manager/CMakeLists.txt b/infra/plugin_manager/CMakeLists.txt index 742a069..4c0fd90 100644 --- a/infra/plugin_manager/CMakeLists.txt +++ b/infra/plugin_manager/CMakeLists.txt @@ -1,7 +1,7 @@ add_library(plugin_manager plugin_manager.c) target_include_directories(plugin_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/include/) -target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/core) +target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/) target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/packet_parser) target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/session_manager) target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/tuple) diff --git a/infra/session_manager/CMakeLists.txt b/infra/session_manager/CMakeLists.txt index 74cc479..2df7c74 100644 --- a/infra/session_manager/CMakeLists.txt +++ b/infra/session_manager/CMakeLists.txt @@ -8,7 +8,7 @@ add_library(session_manager session_transition.c ) target_include_directories(session_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/core) +target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/infra/) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/include) target_link_libraries(session_manager timeout packet_parser tcp_reassembly) diff --git a/infra/core/stellar_core.c b/infra/stellar_core.c similarity index 92% rename from infra/core/stellar_core.c rename to infra/stellar_core.c index 197225a..246c495 100644 --- a/infra/core/stellar_core.c +++ b/infra/stellar_core.c @@ -319,6 +319,7 @@ static int stellar_thread_init(struct stellar *st) struct stellar_thread *thread = &runtime->threads[i]; thread->idx = i; thread->is_runing = 0; + thread->st = st; config->sess_mgr_cfg->session_id_seed = config->instance_id << 8 | i; thread->sess_mgr = session_manager_new(config->sess_mgr_cfg, now_ms); @@ -334,30 +335,11 @@ static int stellar_thread_init(struct stellar *st) CORE_LOG_ERROR("unable to create ip reassemble manager"); return -1; } - thread->st = st; } return 0; } -static void stellar_thread_clean(struct stellar *st) -{ - struct stellar_runtime *runtime = &st->runtime; - struct stellar_config *config = &st->config; - - CORE_LOG_FATAL("cleaning worker thread context ..."); - for (uint16_t i = 0; i < config->pkt_io_cfg->nr_worker_thread; i++) - { - struct stellar_thread *thread = &runtime->threads[i]; - if (ATOMIC_READ(&thread->is_runing) == 0) - { - ip_reassembly_free(thread->ip_reass); - session_manager_free(thread->sess_mgr); - } - } - CORE_LOG_FATAL("worker thread context cleaned"); -} - static int stellar_thread_run(struct stellar *st) { struct stellar_runtime *runtime = &st->runtime; @@ -381,16 +363,16 @@ static void stellar_thread_join(struct stellar *st) struct stellar_runtime *runtime = &st->runtime; struct stellar_config *config = &st->config; - CORE_LOG_FATAL("waiting worker thread stop ..."); + CORE_LOG_FATAL("waiting worker thread exit"); for (uint16_t i = 0; i < config->pkt_io_cfg->nr_worker_thread; i++) { struct stellar_thread *thread = &runtime->threads[i]; - while (ATOMIC_READ(&thread->is_runing) == 1) - { - usleep(1000); // 1ms - } + pthread_join(thread->tid, NULL); + + ip_reassembly_free(thread->ip_reass); + session_manager_free(thread->sess_mgr); } - CORE_LOG_FATAL("all worker thread stoped"); + CORE_LOG_FATAL("all worker thread exited"); } struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg_file, const char *log_cfg_file) @@ -429,9 +411,7 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg } __thread_local_logger = runtime->logger; CORE_LOG_FATAL("stellar start (version: %s)\n %s", version, logo_str); - CORE_LOG_FATAL("stellar config file : %s", stellar_cfg_file); - CORE_LOG_FATAL("plugin config file : %s", plugin_cfg_file); - CORE_LOG_FATAL("log config file : %s", log_cfg_file); + CORE_LOG_FATAL("stellar config file: %s, plugin config file: %s, log config file: %s", stellar_cfg_file, plugin_cfg_file, log_cfg_file); if (load_and_validate_toml_integer_config(stellar_cfg_file, "instance.id", (uint64_t *)&config->instance_id, 0, 4095) != 0) { @@ -463,11 +443,10 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg CORE_LOG_ERROR("unable to create stellar stat config"); goto error_out; } - - session_manager_config_print(config->sess_mgr_cfg); - ip_reassembly_config_print(config->ip_reass_cfg); - packet_io_config_print(config->pkt_io_cfg); stellar_stat_config_print(config->stat_cfg); + packet_io_config_print(config->pkt_io_cfg); + ip_reassembly_config_print(config->ip_reass_cfg); + session_manager_config_print(config->sess_mgr_cfg); runtime->stat = stellar_stat_new(config->stat_cfg, clock_get_real_time_ms()); if (runtime->stat == NULL) @@ -544,7 +523,6 @@ void stellar_run(struct stellar *st) } stellar_thread_join(st); - stellar_stat_output(runtime->stat, UINT64_MAX); } void stellar_free(struct stellar *st) @@ -554,7 +532,6 @@ void stellar_free(struct stellar *st) struct stellar_runtime *runtime = &st->runtime; struct stellar_config *config = &st->config; - stellar_thread_clean(st); packet_io_free(runtime->packet_io); plugin_manager_exit(runtime->plug_mgr); stellar_stat_free(runtime->stat); @@ -593,11 +570,6 @@ void stellar_reload_log_level(struct stellar *st) * Stellar Utility Function ******************************************************************************/ -struct packet_io *stellar_get_packet_io(const struct stellar *st) -{ - return st->runtime.packet_io; -} - struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *st) { return st->runtime.plug_mgr; @@ -608,32 +580,12 @@ void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema st->runtime.plug_mgr = plug_mgr; } -struct session_manager *stellar_get_session_manager(const struct stellar *st) -{ - uint16_t idx = stellar_get_current_thread_index(); - return st->runtime.threads[idx].sess_mgr; -} - -uint16_t stellar_get_current_thread_index() -{ - if (__current_thread_idx == UINT16_MAX) - { - printf("get current thread index before set\n"); - abort(); - return UINT16_MAX; - } - else - { - return __current_thread_idx; - } -} - -// only send user crafted packet, can't send packet which come from network +// only send user build packet, can't send packet which come from network void stellar_send_build_packet(struct stellar *st, struct packet *pkt) { uint16_t thr_idx = stellar_get_current_thread_index(); - struct packet_io *packet_io = stellar_get_packet_io(st); - struct session_manager *sess_mgr = stellar_get_session_manager(st); + struct packet_io *packet_io = st->runtime.packet_io; + struct session_manager *sess_mgr = st->runtime.threads[thr_idx].sess_mgr; session_manager_record_duplicated_packet(sess_mgr, pkt); if (packet_get_origin_ctx(pkt)) @@ -653,6 +605,20 @@ int stellar_get_worker_thread_num(struct stellar *st) return st->config.pkt_io_cfg->nr_worker_thread; } +uint16_t stellar_get_current_thread_index() +{ + if (__current_thread_idx == UINT16_MAX) + { + printf("get current thread index before set\n"); + abort(); + return UINT16_MAX; + } + else + { + return __current_thread_idx; + } +} + struct logger *stellar_get_logger(struct stellar *st) { if (st) diff --git a/infra/core/stellar_core.h b/infra/stellar_core.h similarity index 69% rename from infra/core/stellar_core.h rename to infra/stellar_core.h index 1c92167..c460d78 100644 --- a/infra/core/stellar_core.h +++ b/infra/stellar_core.h @@ -7,14 +7,10 @@ extern "C" #include "stellar/stellar.h" -struct packet_io *stellar_get_packet_io(const struct stellar *st); -struct session_manager *stellar_get_session_manager(const struct stellar *st); -struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *st); // TODO fix plugin manager, delete this function +struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *st); void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr); - - #ifdef __cplusplus } #endif diff --git a/infra/core/stellar_stat.c b/infra/stellar_stat.c similarity index 100% rename from infra/core/stellar_stat.c rename to infra/stellar_stat.c diff --git a/infra/core/stellar_stat.h b/infra/stellar_stat.h similarity index 100% rename from infra/core/stellar_stat.h rename to infra/stellar_stat.h diff --git a/infra/core/utils.h b/infra/utils.h similarity index 100% rename from infra/core/utils.h rename to infra/utils.h diff --git a/test/debug_plugin/CMakeLists.txt b/test/debug_plugin/CMakeLists.txt index bfcc4ca..38be076 100644 --- a/test/debug_plugin/CMakeLists.txt +++ b/test/debug_plugin/CMakeLists.txt @@ -2,7 +2,7 @@ add_library(debug_plugin SHARED debug_plugin.c) target_link_libraries(debug_plugin stellar_lib session_manager packet_parser) target_include_directories(debug_plugin PUBLIC ${CMAKE_SOURCE_DIR}/include/) -target_include_directories(debug_plugin PUBLIC ${CMAKE_SOURCE_DIR}/infra/core/utils) +target_include_directories(debug_plugin PUBLIC ${CMAKE_SOURCE_DIR}/infra) set_target_properties(debug_plugin PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") file(COPY ./conf/ DESTINATION ./conf/) \ No newline at end of file