feature: stellar dev API support stellar_new()/stellar_run()/stellar_free()/stellar_loopbreak()

This commit is contained in:
luwenpeng
2024-08-19 17:28:45 +08:00
parent 28f50b922b
commit 520eb085b8
7 changed files with 197 additions and 83 deletions

View File

@@ -53,7 +53,12 @@ int stellar_get_worker_thread_num(struct stellar *st);
uint16_t stellar_get_current_thread_index(); uint16_t stellar_get_current_thread_index();
// only send user crafted packet, can't send packet which come from network // only send user crafted packet, can't send packet which come from network
void stellar_send_build_packet(struct stellar *st, struct packet *pkt); void stellar_send_build_packet(struct stellar *st, struct packet *pkt);
int stellar_run(int argc, char **argv);
struct stellar;
struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg_file, const char *log_cfg_file);
void stellar_run(struct stellar *st);
void stellar_free(struct stellar *st);
void stellar_loopbreak(struct stellar *st);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -1,6 +1,58 @@
#include "stellar/stellar.h" #include <stdio.h>
#include <stddef.h>
#include <signal.h>
int main(int argc, char **argv) #include "stellar/stellar.h"
#include "stellar_core.h"
struct stellar *st = NULL;
static void signal_handler(int signo)
{ {
return stellar_run(argc, argv); if (signo == SIGINT)
{
printf("SIGINT received, notify threads to exit");
stellar_loopbreak(st);
}
if (signo == SIGQUIT)
{
printf("SIGQUIT received, notify threads to exit");
stellar_loopbreak(st);
}
if (signo == SIGTERM)
{
printf("SIGTERM received, notify threads to exit");
stellar_loopbreak(st);
}
if (signo == SIGHUP)
{
printf("SIGHUP received, reload log level !!!");
stellar_reload_log_level(st);
}
}
int main(int argc __attribute__((__unused__)), char **argv __attribute__((__unused__)))
{
const char *stellar_cfg_file = "./conf/stellar.toml";
const char *plugin_cfg_file = "./plugin/spec.toml";
const char *log_cfg_file = "./conf/log.toml";
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
st = stellar_new(stellar_cfg_file, plugin_cfg_file, log_cfg_file);
if (st == NULL)
{
return 0;
}
stellar_run(st);
stellar_free(st);
return 0;
} }

View File

@@ -3,8 +3,8 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <pthread.h> #include <pthread.h>
#include <sys/prctl.h> #include <sys/prctl.h>
@@ -61,7 +61,7 @@ struct stellar_thread
struct schedule_data sched_data; struct schedule_data sched_data;
struct ip_reassembly *ip_mgr; struct ip_reassembly *ip_mgr;
struct session_manager *sess_mgr; struct session_manager *sess_mgr;
struct stellar_runtime *runtime; struct stellar *st;
}; };
struct stellar_runtime struct stellar_runtime
@@ -76,15 +76,15 @@ struct stellar_runtime
struct stellar 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_runtime runtime;
struct stellar_config config; struct stellar_config config;
}; };
static uint64_t need_exit = 0; static thread_local uint16_t __thread_id = 0; // TODO
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";
/****************************************************************************** /******************************************************************************
* Stellar Thread Main Loop * 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) 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 = { struct thread_stat thr_stat = {
.packet_io = packet_io_stat(runtime->packet_io, thread->idx), .packet_io = packet_io_stat(runtime->packet_io, thread->idx),
.ip_reassembly = ip_reassembly_stat(thread->ip_mgr), .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 stellar_thread *thread = (struct stellar_thread *)arg;
struct ip_reassembly *ip_reass = thread->ip_mgr; struct ip_reassembly *ip_reass = thread->ip_mgr;
struct session_manager *sess_mgr = thread->sess_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 schedule_data *sched_data = &thread->sched_data;
struct packet_io *packet_io = runtime->packet_io; struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr; 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); ATOMIC_SET(&thread->is_runing, 1);
STELLAR_LOG_STATE("worker thread %d runing", thr_idx); 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: * 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; 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); session_set_user_data(sess, plugin_ctx);
} }
else else
@@ -320,7 +322,7 @@ static void *work_thread(void *arg)
idle_tasks: idle_tasks:
// nr_recv packet atmost trigger nr_recv session evicted // nr_recv packet atmost trigger nr_recv session evicted
free_evicted_sessions(sess_mgr, nr_recv); 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 // 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) 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 * 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) 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++) 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; 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(); uint64_t now_ms = clock_get_real_time_ms();
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) 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"); STELLAR_LOG_ERROR("unable to create ip reassemble manager");
return -1; return -1;
} }
thread->runtime = runtime; thread->st = st;
} }
return 0; 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++) for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
{ {
struct stellar_thread *thread = &runtime->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++) for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
{ {
struct stellar_thread *thread = &runtime->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; 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 ..."); STELLAR_LOG_STATE("wait worker thread exit ...");
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) 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 = {}; if (stellar_cfg_file == NULL)
struct stellar_runtime *runtime = &st.runtime; {
struct stellar_config *config = &st.config; 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); struct stellar *st = (struct stellar *)calloc(1, sizeof(struct stellar));
signal(SIGQUIT, signal_handler); if (st == NULL)
signal(SIGTERM, signal_handler); {
signal(SIGHUP, signal_handler); 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"); STELLAR_LOG_ERROR("unable to init log");
goto error_out; goto error_out;
} }
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", version, logo_str); 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("stellar config file : %s", st->stellar_cfg_file);
STELLAR_LOG_STATE("main config file : %s", main_config_file); STELLAR_LOG_STATE("plugin config file : %s", st->plugin_cfg_file);
STELLAR_LOG_STATE("plugin config file : %s", plugin_config_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"); STELLAR_LOG_ERROR("unable to load config file");
goto error_out; 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"); STELLAR_LOG_ERROR("unable to create stellar stat");
goto error_out; 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) if (runtime->plug_mgr == NULL)
{ {
STELLAR_LOG_ERROR("unable to create plugin manager"); 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; goto error_out;
} }
if (stellar_thread_init(runtime, config) != 0) if (stellar_thread_init(st) != 0)
{ {
STELLAR_LOG_ERROR("unable to init thread context"); STELLAR_LOG_ERROR("unable to init thread context");
goto error_out; 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"); STELLAR_LOG_ERROR("unable to create worker thread");
goto error_out; return;
} }
runtime->stat_last_output_ts = clock_get_real_time_ms(); 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) 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_stat_output(runtime->stat); // flush stat
STELLAR_LOG_STATE("all sessions have been released, notify threads to exit"); 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: void stellar_free(struct stellar *st)
stellar_thread_join(runtime, config); {
stellar_thread_clean(runtime, config); if (st)
{
struct stellar_runtime *runtime = &st->runtime;
stellar_thread_join(st);
stellar_thread_clean(st);
packet_io_free(runtime->packet_io); packet_io_free(runtime->packet_io);
plugin_manager_exit(runtime->plug_mgr); plugin_manager_exit(runtime->plug_mgr);
stellar_stat_free(runtime->stat); stellar_stat_free(runtime->stat);
STELLAR_LOG_STATE("stellar exit\n"); STELLAR_LOG_STATE("stellar exit\n");
log_free(); log_free();
}
}
return 0; 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);
}
} }
/****************************************************************************** /******************************************************************************

View File

@@ -13,6 +13,8 @@ struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *s
// TODO fix plugin manager, delete this function // TODO fix plugin manager, delete this function
void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr); void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr);
void stellar_reload_log_level(struct stellar *st);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -57,7 +57,10 @@ global:
stellar_get_current_thread_index; stellar_get_current_thread_index;
stellar_get_worker_thread_num; stellar_get_worker_thread_num;
stellar_send_build_packet; stellar_send_build_packet;
stellar_new;
stellar_run; stellar_run;
stellar_free;
stellar_loopbreak;
local: *; local: *;
}; };

View File

@@ -321,11 +321,5 @@ void log_print(enum log_level level, const char *module, const char *fmt, ...)
{ {
nwrite = write(g_log_ctx->log_fd, buf, p - buf); nwrite = write(g_log_ctx->log_fd, buf, p - buf);
} while (nwrite == -1 && errno == EINTR); } while (nwrite == -1 && errno == EINTR);
// log level is LOG_STATE, also print to stderr
if (level == LOG_STATE)
{
fprintf(stderr, "%s", buf);
}
} }
} }

View File

@@ -176,7 +176,14 @@ static inline void packet_inject_test(struct packet_inject_case *test)
snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir); snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir);
EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0); EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0);
EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0); EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0);
stellar_run(0, NULL);
const char *stellar_cfg_file = "./conf/stellar.toml";
const char *plugin_cfg_file = "./plugin/spec.toml";
const char *log_cfg_file = "./conf/log.toml";
struct stellar *st = stellar_new(stellar_cfg_file, plugin_cfg_file, log_cfg_file);
EXPECT_TRUE(st != NULL);
stellar_run(st);
stellar_free(st);
// compare // compare
for (int i = 0; i < MAX_COMPARISON; i++) for (int i = 0; i < MAX_COMPARISON; i++)