Optimize integration testing
- Add injection package plug-in - Add libstellar_dynamic.so to facilitate unit testing of upper-level plug-ins
This commit is contained in:
@@ -2,17 +2,53 @@
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/prctl.h>
|
||||
|
||||
#include "stat.h"
|
||||
#include "logo.h"
|
||||
#include "times.h"
|
||||
#include "config.h"
|
||||
#include "id_generator.h"
|
||||
#include "stellar_priv.h"
|
||||
#include "plugin_manager.h"
|
||||
|
||||
struct stellar_runtime __runtime = {0};
|
||||
struct stellar_runtime *runtime = &__runtime;
|
||||
struct stellar_thread
|
||||
{
|
||||
pthread_t tid;
|
||||
uint16_t idx;
|
||||
uint64_t is_runing;
|
||||
uint64_t timing_wheel_last_update_ts;
|
||||
struct ip_reassembly *ip_mgr;
|
||||
struct session_manager *sess_mgr;
|
||||
struct stellar_runtime *runtime;
|
||||
};
|
||||
|
||||
struct stellar_config __config = {0};
|
||||
struct stellar_config *config = &__config;
|
||||
struct stellar_runtime
|
||||
{
|
||||
uint64_t need_exit;
|
||||
uint64_t stat_last_output_ts;
|
||||
struct stellar_stat *stat;
|
||||
struct packet_io *packet_io;
|
||||
struct plugin_manager_schema *plug_mgr;
|
||||
struct stellar_thread threads[MAX_THREAD_NUM];
|
||||
};
|
||||
|
||||
struct stellar
|
||||
{
|
||||
struct stellar_runtime runtime;
|
||||
struct stellar_config config;
|
||||
};
|
||||
|
||||
static uint64_t need_exit = 0;
|
||||
static thread_local uint16_t __thread_id = 0;
|
||||
static const char *log_config_file = "./conf/log.toml";
|
||||
static const char *main_config_file = "./conf/stellar.toml";
|
||||
static const char *plugin_config_file = "./stellar_plugin/spec.toml";
|
||||
|
||||
/******************************************************************************
|
||||
* Stellar Thread Main Loop
|
||||
******************************************************************************/
|
||||
|
||||
static void update_session_stat(struct session *sess, struct packet *pkt)
|
||||
{
|
||||
@@ -83,6 +119,7 @@ static inline void free_expired_sessions(struct session_manager *sess_mgr, uint6
|
||||
|
||||
static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now)
|
||||
{
|
||||
struct stellar_runtime *runtime = thread->runtime;
|
||||
struct thread_stat thr_stat = {
|
||||
packet_io_stat(runtime->packet_io, thread->idx),
|
||||
ip_reassembly_stat(thread->ip_mgr),
|
||||
@@ -101,13 +138,15 @@ static void *work_thread(void *arg)
|
||||
struct packet *defraged_pkt = NULL;
|
||||
struct packet packets[RX_BURST_MAX];
|
||||
struct session *sess = NULL;
|
||||
struct packet_io *packet_io = runtime->packet_io;
|
||||
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
|
||||
struct stellar_thread *thread = (struct stellar_thread *)arg;
|
||||
struct ip_reassembly *ip_reass = thread->ip_mgr;
|
||||
struct session_manager *sess_mgr = thread->sess_mgr;
|
||||
struct stellar_runtime *runtime = thread->runtime;
|
||||
struct packet_io *packet_io = runtime->packet_io;
|
||||
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
|
||||
|
||||
uint16_t thr_idx = thread->idx;
|
||||
stellar_set_current_thread_index(thr_idx);
|
||||
__thread_id = thr_idx;
|
||||
|
||||
snprintf(thd_name, sizeof(thd_name), "stellar:%d", thr_idx);
|
||||
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
|
||||
@@ -121,7 +160,7 @@ static void *work_thread(void *arg)
|
||||
ATOMIC_SET(&thread->is_runing, 1);
|
||||
STELLAR_LOG_STATE("worker thread %d runing", thr_idx);
|
||||
|
||||
while (ATOMIC_READ(&runtime->need_exit) == 0)
|
||||
while (ATOMIC_READ(&need_exit) == 0)
|
||||
{
|
||||
now = stellar_get_monotonic_time_msec();
|
||||
memset(packets, 0, sizeof(packets));
|
||||
@@ -236,19 +275,70 @@ static void *work_thread(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static thread_local uint16_t __thread_id = 0;
|
||||
/******************************************************************************
|
||||
* Stellar Main Function
|
||||
******************************************************************************/
|
||||
|
||||
uint16_t stellar_get_current_thread_index()
|
||||
static void signal_handler(int signo)
|
||||
{
|
||||
return __thread_id;
|
||||
if (signo == SIGINT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGQUIT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGTERM)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGHUP)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
|
||||
log_reload_level(log_config_file);
|
||||
}
|
||||
}
|
||||
|
||||
void stellar_set_current_thread_index(uint16_t idx)
|
||||
static int all_session_have_freed(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
__thread_id = idx;
|
||||
for (int i = 0; i < config->io_opts.nr_threads; i++)
|
||||
{
|
||||
struct session_manager *sess_mgr = runtime->threads[i].sess_mgr;
|
||||
struct session_manager_stat *sess_stat = session_manager_stat(sess_mgr);
|
||||
|
||||
if (ATOMIC_READ(&sess_stat->curr_nr_tcp_sess_used) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ATOMIC_READ(&sess_stat->curr_nr_udp_sess_used) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static int all_stat_have_output(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
static int count = 0;
|
||||
if (runtime->stat_last_output_ts == stellar_get_monotonic_time_msec())
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
return count == 2;
|
||||
}
|
||||
|
||||
static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
uint64_t now = stellar_get_monotonic_time_msec();
|
||||
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
|
||||
@@ -269,12 +359,13 @@ int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *
|
||||
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
|
||||
return -1;
|
||||
}
|
||||
thread->runtime = runtime;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
|
||||
{
|
||||
@@ -288,7 +379,7 @@ void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config
|
||||
}
|
||||
}
|
||||
|
||||
int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
|
||||
{
|
||||
@@ -303,7 +394,7 @@ int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *c
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
|
||||
{
|
||||
@@ -315,3 +406,133 @@ void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int stellar_main(int argc, char **argv)
|
||||
{
|
||||
static struct stellar st = {0};
|
||||
static struct stellar_runtime *runtime = &st.runtime;
|
||||
static struct stellar_config *config = &st.config;
|
||||
|
||||
stellar_update_time_cache();
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGQUIT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGHUP, signal_handler);
|
||||
|
||||
if (log_init(log_config_file) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init log");
|
||||
goto error_out;
|
||||
}
|
||||
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
|
||||
STELLAR_LOG_STATE("log config file : %s", log_config_file);
|
||||
STELLAR_LOG_STATE("main config file : %s", main_config_file);
|
||||
STELLAR_LOG_STATE("plugin config file : %s", plugin_config_file);
|
||||
|
||||
if (stellar_load_config(main_config_file, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to load config file");
|
||||
goto error_out;
|
||||
}
|
||||
stellar_print_config(config);
|
||||
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
|
||||
|
||||
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init id generator");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->stat = stellar_stat_new(config->io_opts.nr_threads);
|
||||
if (runtime->stat == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create stellar stat");
|
||||
goto error_out;
|
||||
}
|
||||
runtime->plug_mgr = plugin_manager_init(&st, plugin_config_file);
|
||||
if (runtime->plug_mgr == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create plugin manager");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->packet_io = packet_io_new(&config->io_opts);
|
||||
if (runtime->packet_io == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create packet io");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_init(runtime, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init thread context");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_run(runtime, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create worker thread");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->stat_last_output_ts = stellar_get_monotonic_time_msec();
|
||||
while (!ATOMIC_READ(&need_exit))
|
||||
{
|
||||
stellar_update_time_cache();
|
||||
if (stellar_get_monotonic_time_msec() - runtime->stat_last_output_ts > 2000)
|
||||
{
|
||||
runtime->stat_last_output_ts = stellar_get_monotonic_time_msec();
|
||||
stellar_stat_output(runtime->stat);
|
||||
}
|
||||
usleep(1000); // 1ms
|
||||
|
||||
// Only available in dump file mode, automatically exits when all sessions have been released
|
||||
if (packet_io_wait_exit(runtime->packet_io) && all_session_have_freed(runtime, config) && all_stat_have_output(runtime, config))
|
||||
{
|
||||
STELLAR_LOG_STATE("all sessions have been released and all stat have been output, notify threads to exit !!!");
|
||||
ATOMIC_SET(&need_exit, 1);
|
||||
}
|
||||
}
|
||||
|
||||
error_out:
|
||||
stellar_thread_join(runtime, config);
|
||||
stellar_thread_clean(runtime, config);
|
||||
packet_io_free(runtime->packet_io);
|
||||
plugin_manager_exit(runtime->plug_mgr);
|
||||
stellar_stat_free(runtime->stat);
|
||||
STELLAR_LOG_STATE("stellar exit !!!\n");
|
||||
log_free();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* 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;
|
||||
}
|
||||
|
||||
void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr)
|
||||
{
|
||||
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()
|
||||
{
|
||||
return __thread_id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user