feature: stellar dev API support stellar_new()/stellar_run()/stellar_free()/stellar_loopbreak()
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/prctl.h>
|
||||
|
||||
@@ -61,7 +61,7 @@ struct stellar_thread
|
||||
struct schedule_data sched_data;
|
||||
struct ip_reassembly *ip_mgr;
|
||||
struct session_manager *sess_mgr;
|
||||
struct stellar_runtime *runtime;
|
||||
struct stellar *st;
|
||||
};
|
||||
|
||||
struct stellar_runtime
|
||||
@@ -76,15 +76,15 @@ struct stellar_runtime
|
||||
|
||||
struct stellar
|
||||
{
|
||||
char stellar_cfg_file[PATH_MAX];
|
||||
char plugin_cfg_file[PATH_MAX];
|
||||
char log_cfg_file[PATH_MAX];
|
||||
|
||||
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 = "./plugin/spec.toml";
|
||||
static thread_local uint16_t __thread_id = 0; // TODO
|
||||
|
||||
/******************************************************************************
|
||||
* Stellar Thread Main Loop
|
||||
@@ -162,7 +162,8 @@ static inline void free_expired_sessions(struct session_manager *sess_mgr, uint6
|
||||
|
||||
static inline void merge_thread_stat(struct stellar_thread *thread)
|
||||
{
|
||||
struct stellar_runtime *runtime = thread->runtime;
|
||||
struct stellar *st = thread->st;
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct thread_stat thr_stat = {
|
||||
.packet_io = packet_io_stat(runtime->packet_io, thread->idx),
|
||||
.ip_reassembly = ip_reassembly_stat(thread->ip_mgr),
|
||||
@@ -184,7 +185,8 @@ static void *work_thread(void *arg)
|
||||
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 stellar *st = thread->st;
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct schedule_data *sched_data = &thread->sched_data;
|
||||
struct packet_io *packet_io = runtime->packet_io;
|
||||
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
|
||||
@@ -210,7 +212,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(&need_exit) == 0)
|
||||
while (ATOMIC_READ(&runtime->need_exit) == 0)
|
||||
{
|
||||
/*
|
||||
* We use the system's real time instead of monotonic time for the following reasons:
|
||||
@@ -256,7 +258,7 @@ static void *work_thread(void *arg)
|
||||
{
|
||||
goto fast_path;
|
||||
}
|
||||
plugin_ctx = plugin_manager_session_runtime_new(runtime->plug_mgr, sess);
|
||||
plugin_ctx = plugin_manager_session_runtime_new(plug_mgr, sess);
|
||||
session_set_user_data(sess, plugin_ctx);
|
||||
}
|
||||
else
|
||||
@@ -320,7 +322,7 @@ static void *work_thread(void *arg)
|
||||
idle_tasks:
|
||||
// nr_recv packet atmost trigger nr_recv session evicted
|
||||
free_evicted_sessions(sess_mgr, nr_recv);
|
||||
plugin_manager_on_polling(runtime->plug_mgr);
|
||||
plugin_manager_on_polling(plug_mgr);
|
||||
|
||||
// per free_expired_session_interval MAX free_expired_session_batch sessions are released
|
||||
if (now_ms - sched_data->last_free_expired_session_timestamp > sched_data->free_expired_session_interval)
|
||||
@@ -359,33 +361,6 @@ static void *work_thread(void *arg)
|
||||
* Stellar Main Function
|
||||
******************************************************************************/
|
||||
|
||||
static void signal_handler(int signo)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static int all_session_have_freed(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
{
|
||||
for (int i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
@@ -407,8 +382,11 @@ static int all_session_have_freed(struct stellar_runtime *runtime, struct stella
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static int stellar_thread_init(struct stellar *st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
uint64_t now_ms = clock_get_real_time_ms();
|
||||
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
{
|
||||
@@ -441,14 +419,17 @@ static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_c
|
||||
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
|
||||
return -1;
|
||||
}
|
||||
thread->runtime = runtime;
|
||||
thread->st = st;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static void stellar_thread_clean(struct stellar *st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
{
|
||||
struct stellar_thread *thread = &runtime->threads[i];
|
||||
@@ -460,8 +441,11 @@ static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar
|
||||
}
|
||||
}
|
||||
|
||||
static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static int stellar_thread_run(struct stellar *st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
{
|
||||
struct stellar_thread *thread = &runtime->threads[i];
|
||||
@@ -475,8 +459,11 @@ static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_co
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config)
|
||||
static void stellar_thread_join(struct stellar *st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
STELLAR_LOG_STATE("wait worker thread exit ...");
|
||||
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
{
|
||||
@@ -488,28 +475,48 @@ static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_
|
||||
}
|
||||
}
|
||||
|
||||
int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unused)))
|
||||
struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg_file, const char *log_cfg_file)
|
||||
{
|
||||
static struct stellar st = {};
|
||||
struct stellar_runtime *runtime = &st.runtime;
|
||||
struct stellar_config *config = &st.config;
|
||||
if (stellar_cfg_file == NULL)
|
||||
{
|
||||
printf("stellar config file is null\n");
|
||||
return NULL;
|
||||
}
|
||||
if (plugin_cfg_file == NULL)
|
||||
{
|
||||
printf("plugin config file is null\n");
|
||||
return NULL;
|
||||
}
|
||||
if (log_cfg_file == NULL)
|
||||
{
|
||||
printf("log config file is null\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGQUIT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGHUP, signal_handler);
|
||||
struct stellar *st = (struct stellar *)calloc(1, sizeof(struct stellar));
|
||||
if (st == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (log_init(log_config_file) != 0)
|
||||
memcpy(st->stellar_cfg_file, stellar_cfg_file, strlen(stellar_cfg_file));
|
||||
memcpy(st->plugin_cfg_file, plugin_cfg_file, strlen(plugin_cfg_file));
|
||||
memcpy(st->log_cfg_file, log_cfg_file, strlen(log_cfg_file));
|
||||
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
if (log_init(st->log_cfg_file) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init log");
|
||||
goto error_out;
|
||||
}
|
||||
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", 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);
|
||||
STELLAR_LOG_STATE("stellar config file : %s", st->stellar_cfg_file);
|
||||
STELLAR_LOG_STATE("plugin config file : %s", st->plugin_cfg_file);
|
||||
STELLAR_LOG_STATE("log config file : %s", st->log_cfg_file);
|
||||
|
||||
if (stellar_config_load(config, main_config_file) != 0)
|
||||
if (stellar_config_load(config, st->stellar_cfg_file) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to load config file");
|
||||
goto error_out;
|
||||
@@ -528,7 +535,7 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu
|
||||
STELLAR_LOG_ERROR("unable to create stellar stat");
|
||||
goto error_out;
|
||||
}
|
||||
runtime->plug_mgr = plugin_manager_init(&st, plugin_config_file);
|
||||
runtime->plug_mgr = plugin_manager_init(st, st->plugin_cfg_file);
|
||||
if (runtime->plug_mgr == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create plugin manager");
|
||||
@@ -542,20 +549,42 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_init(runtime, config) != 0)
|
||||
if (stellar_thread_init(st) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init thread context");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_run(runtime, config) != 0)
|
||||
return st;
|
||||
|
||||
error_out:
|
||||
if (st == NULL)
|
||||
{
|
||||
stellar_free(st);
|
||||
st = NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void stellar_run(struct stellar *st)
|
||||
{
|
||||
if (st == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
struct stellar_config *config = &st->config;
|
||||
|
||||
if (stellar_thread_run(st) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create worker thread");
|
||||
goto error_out;
|
||||
return;
|
||||
}
|
||||
|
||||
runtime->stat_last_output_ts = clock_get_real_time_ms();
|
||||
while (!ATOMIC_READ(&need_exit))
|
||||
while (!ATOMIC_READ(&runtime->need_exit))
|
||||
{
|
||||
if (clock_get_real_time_ms() - runtime->stat_last_output_ts > config->sched_opts.output_stat_interval)
|
||||
{
|
||||
@@ -569,20 +598,42 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu
|
||||
{
|
||||
stellar_stat_output(runtime->stat); // flush stat
|
||||
STELLAR_LOG_STATE("all sessions have been released, notify threads to exit");
|
||||
ATOMIC_SET(&need_exit, 1);
|
||||
ATOMIC_SET(&runtime->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();
|
||||
void stellar_free(struct stellar *st)
|
||||
{
|
||||
if (st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
|
||||
return 0;
|
||||
stellar_thread_join(st);
|
||||
stellar_thread_clean(st);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void stellar_loopbreak(struct stellar *st)
|
||||
{
|
||||
if (st)
|
||||
{
|
||||
struct stellar_runtime *runtime = &st->runtime;
|
||||
ATOMIC_SET(&runtime->need_exit, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void stellar_reload_log_level(struct stellar *st)
|
||||
{
|
||||
if (st)
|
||||
{
|
||||
log_reload_level(st->log_cfg_file);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user