summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/stellar/stellar.h7
-rw-r--r--src/core/main.cpp56
-rw-r--r--src/core/stellar_core.cpp195
-rw-r--r--src/core/stellar_core.h2
-rw-r--r--src/core/version.map3
-rw-r--r--src/log/log.cpp6
-rw-r--r--test/packet_inject/packet_inject_test.h9
7 files changed, 196 insertions, 82 deletions
diff --git a/include/stellar/stellar.h b/include/stellar/stellar.h
index 6007da4..ed204dc 100644
--- a/include/stellar/stellar.h
+++ b/include/stellar/stellar.h
@@ -53,7 +53,12 @@ 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
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
}
diff --git a/src/core/main.cpp b/src/core/main.cpp
index 69a7405..e4ab67b 100644
--- a/src/core/main.cpp
+++ b/src/core/main.cpp
@@ -1,6 +1,58 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <signal.h>
+
#include "stellar/stellar.h"
+#include "stellar_core.h"
+
+struct stellar *st = NULL;
+static void signal_handler(int signo)
+{
+ 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, char **argv)
+int main(int argc __attribute__((__unused__)), char **argv __attribute__((__unused__)))
{
- return stellar_run(argc, argv);
+ 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;
} \ No newline at end of file
diff --git a/src/core/stellar_core.cpp b/src/core/stellar_core.cpp
index 27592df..1252106 100644
--- a/src/core/stellar_core.cpp
+++ b/src/core/stellar_core.cpp
@@ -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;
+ }
+
+ struct stellar *st = (struct stellar *)calloc(1, sizeof(struct stellar));
+ if (st == NULL)
+ {
+ return NULL;
+ }
+
+ 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));
- signal(SIGINT, signal_handler);
- signal(SIGQUIT, signal_handler);
- signal(SIGTERM, signal_handler);
- signal(SIGHUP, signal_handler);
+ struct stellar_runtime *runtime = &st->runtime;
+ struct stellar_config *config = &st->config;
- if (log_init(log_config_file) != 0)
+ 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;
+
+ 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();
+ }
+}
- 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);
+ }
}
/******************************************************************************
diff --git a/src/core/stellar_core.h b/src/core/stellar_core.h
index 3d9b5fb..d4bee86 100644
--- a/src/core/stellar_core.h
+++ b/src/core/stellar_core.h
@@ -13,6 +13,8 @@ struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *s
// TODO fix plugin manager, delete this function
void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr);
+void stellar_reload_log_level(struct stellar *st);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/core/version.map b/src/core/version.map
index ef5181c..f7a0850 100644
--- a/src/core/version.map
+++ b/src/core/version.map
@@ -57,7 +57,10 @@ global:
stellar_get_current_thread_index;
stellar_get_worker_thread_num;
stellar_send_build_packet;
+ stellar_new;
stellar_run;
+ stellar_free;
+ stellar_loopbreak;
local: *;
};
diff --git a/src/log/log.cpp b/src/log/log.cpp
index f890ba2..7ed4ab2 100644
--- a/src/log/log.cpp
+++ b/src/log/log.cpp
@@ -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);
} while (nwrite == -1 && errno == EINTR);
-
- // log level is LOG_STATE, also print to stderr
- if (level == LOG_STATE)
- {
- fprintf(stderr, "%s", buf);
- }
}
} \ No newline at end of file
diff --git a/test/packet_inject/packet_inject_test.h b/test/packet_inject/packet_inject_test.h
index 1c327eb..dd7dd57 100644
--- a/test/packet_inject/packet_inject_test.h
+++ b/test/packet_inject/packet_inject_test.h
@@ -176,7 +176,14 @@ static inline void packet_inject_test(struct packet_inject_case *test)
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", "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
for (int i = 0; i < MAX_COMPARISON; i++)