2023-12-08 11:16:23 +08:00
|
|
|
#include <stdio.h>
|
2024-01-09 18:03:24 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <signal.h>
|
2024-01-30 18:07:08 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/prctl.h>
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
#include "logo.h"
|
2024-04-16 14:12:41 +08:00
|
|
|
#include "stat.h"
|
2024-01-31 14:45:50 +08:00
|
|
|
#include "stellar.h"
|
2024-01-29 14:15:33 +08:00
|
|
|
#include "config.h"
|
2024-04-10 11:40:26 +08:00
|
|
|
#include "packet_private.h"
|
2024-02-28 16:30:03 +08:00
|
|
|
#include "packet_io.h"
|
2024-01-09 18:03:24 +08:00
|
|
|
#include "timestamp.h"
|
2024-01-31 14:45:50 +08:00
|
|
|
#include "id_generator.h"
|
2024-03-08 13:55:17 +08:00
|
|
|
#include "ip_reassembly.h"
|
2024-02-28 16:30:03 +08:00
|
|
|
#include "session_manager.h"
|
2024-04-11 16:30:21 +08:00
|
|
|
#include "plugin_manager.h"
|
2024-01-30 18:07:08 +08:00
|
|
|
|
|
|
|
|
#define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__)
|
|
|
|
|
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
|
2024-01-26 14:41:40 +08:00
|
|
|
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
2024-04-11 16:30:21 +08:00
|
|
|
uint16_t idx;
|
2024-01-09 18:03:24 +08:00
|
|
|
uint64_t is_runing;
|
2024-03-08 13:55:17 +08:00
|
|
|
struct ip_reassembly *ip_mgr;
|
2024-04-11 16:30:21 +08:00
|
|
|
struct session_manager *sess_mgr;
|
2024-01-09 18:03:24 +08:00
|
|
|
};
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
struct stellar_runtime
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
uint64_t need_exit;
|
2024-04-16 14:12:41 +08:00
|
|
|
struct stellar_stat *stat;
|
2024-02-28 16:30:03 +08:00
|
|
|
struct packet_io *packet_io;
|
2024-04-11 16:30:21 +08:00
|
|
|
struct plugin_manager *plug_mgr;
|
|
|
|
|
struct thread_ctx threads[MAX_THREAD_NUM];
|
2024-01-29 14:15:33 +08:00
|
|
|
};
|
2024-04-11 16:30:21 +08:00
|
|
|
struct stellar_runtime __runtime;
|
|
|
|
|
struct stellar_runtime *runtime = &__runtime;
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
struct stellar_config __config;
|
|
|
|
|
struct stellar_config *config = &__config;
|
2024-01-30 18:07:08 +08:00
|
|
|
|
|
|
|
|
static const char *log_config_file = "./conf/log.toml";
|
|
|
|
|
static const char *stellar_config_file = "./conf/stellar.toml";
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* util
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-01-09 18:03:24 +08:00
|
|
|
static void signal_handler(int signo)
|
|
|
|
|
{
|
|
|
|
|
if (signo == SIGINT)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
|
2024-04-11 16:30:21 +08:00
|
|
|
ATOMIC_SET(&runtime->need_exit, 1);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGQUIT)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
|
2024-04-11 16:30:21 +08:00
|
|
|
ATOMIC_SET(&runtime->need_exit, 1);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGTERM)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
|
2024-04-11 16:30:21 +08:00
|
|
|
ATOMIC_SET(&runtime->need_exit, 1);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
if (signo == SIGHUP)
|
2024-01-26 14:41:40 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
|
2024-01-30 18:07:08 +08:00
|
|
|
log_reload_level(log_config_file);
|
2024-01-26 14:41:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 15:07:53 +08:00
|
|
|
static void execute_packet_action(struct packet_io *packet_io, struct session *sess, struct packet *pkt, uint16_t thr_idx)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
int is_ctrl = packet_is_ctrl(pkt);
|
|
|
|
|
int need_drop = packet_need_drop(pkt);
|
2024-04-09 15:07:53 +08:00
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
if (sess != NULL)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
|
|
|
|
enum session_stat stat_pkt;
|
|
|
|
|
enum session_stat stat_byte;
|
2024-04-11 19:44:02 +08:00
|
|
|
if (need_drop)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_DROP : STAT_RAW_PKTS_DROP;
|
|
|
|
|
stat_byte = is_ctrl ? STAT_CTRL_BYTES_DROP : STAT_RAW_BYTES_DROP;
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_TX : STAT_RAW_PKTS_TX;
|
|
|
|
|
stat_byte = is_ctrl ? STAT_CTRL_BYTES_TX : STAT_RAW_BYTES_TX;
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 11:40:26 +08:00
|
|
|
session_inc_stat(sess, session_get_current_direction(sess), stat_pkt, 1);
|
|
|
|
|
session_inc_stat(sess, session_get_current_direction(sess), stat_byte, packet_get_len(pkt));
|
2024-04-09 15:07:53 +08:00
|
|
|
session_set_current_packet(sess, NULL);
|
2024-04-10 11:40:26 +08:00
|
|
|
session_set_current_direction(sess, SESSION_DIRECTION_NONE);
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
if (need_drop)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
|
|
|
|
packet_io_drop(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_egress(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* thread
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline void thread_set_name(const char *thd_symbol, uint16_t thd_idx)
|
|
|
|
|
{
|
|
|
|
|
char thd_name[16];
|
|
|
|
|
snprintf(thd_name, sizeof(thd_name), "%s:%d", thd_symbol, thd_idx);
|
|
|
|
|
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 14:12:41 +08:00
|
|
|
static inline void stellar_thread_cron(struct thread_ctx *thr_ctx)
|
|
|
|
|
{
|
|
|
|
|
thread_local uint64_t last = 0;
|
|
|
|
|
if (timestamp_get_msec() - last > 2000)
|
|
|
|
|
{
|
|
|
|
|
struct thread_stat thr_stat = {
|
|
|
|
|
ip_reassembly_get_stat(thr_ctx->ip_mgr),
|
|
|
|
|
session_manager_get_stat(thr_ctx->sess_mgr),
|
|
|
|
|
};
|
|
|
|
|
stellar_peek_thr_stat(runtime->stat, &thr_stat, thr_ctx->idx);
|
|
|
|
|
last = timestamp_get_msec();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
static void *work_thread(void *arg)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-04-16 14:12:41 +08:00
|
|
|
int nr_recv;
|
|
|
|
|
uint64_t now = 0;
|
|
|
|
|
uint16_t thr_idx = 0;
|
2024-04-11 16:30:21 +08:00
|
|
|
void *plugin_ctx;
|
|
|
|
|
struct packet *pkt;
|
|
|
|
|
struct packet packets[RX_BURST_MAX];
|
2024-01-30 18:07:08 +08:00
|
|
|
struct session *sess;
|
2024-03-08 18:10:38 +08:00
|
|
|
struct session *evicted_sess;
|
|
|
|
|
struct session *expired_sess;
|
2024-04-16 14:12:41 +08:00
|
|
|
struct packet_io *packet_io = runtime->packet_io;
|
|
|
|
|
struct plugin_manager *plug_mgr = runtime->plug_mgr;
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx *thr_ctx = (struct thread_ctx *)arg;
|
|
|
|
|
struct ip_reassembly *ip_reass = thr_ctx->ip_mgr;
|
|
|
|
|
struct session_manager *sess_mgr = thr_ctx->sess_mgr;
|
2024-04-16 14:12:41 +08:00
|
|
|
thr_idx = thr_ctx->idx;
|
2024-01-30 18:07:08 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
if (packet_io_init(packet_io, thr_idx) != 0)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to init marsio thread");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
ATOMIC_SET(&thr_ctx->is_runing, 1);
|
2024-03-09 19:28:14 +08:00
|
|
|
thread_set_name("stellar", thr_idx);
|
|
|
|
|
STELLAR_LOG_STATE("worker thread %d runing", thr_idx);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
while (ATOMIC_READ(&runtime->need_exit) == 0)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-03-14 10:56:09 +08:00
|
|
|
now = timestamp_get_msec(); // TODO
|
2024-03-09 19:28:14 +08:00
|
|
|
nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX);
|
2024-03-08 18:10:38 +08:00
|
|
|
if (nr_recv == 0)
|
2024-01-10 10:19:47 +08:00
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
goto idle_tasks;
|
2024-01-10 10:19:47 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
for (int i = 0; i < nr_recv; i++)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
sess = NULL;
|
2024-03-08 18:10:38 +08:00
|
|
|
pkt = &packets[i];
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_manager_dispatch_packet(plug_mgr, pkt);
|
2024-03-09 19:28:14 +08:00
|
|
|
if (packet_is_fragment(pkt))
|
2024-03-08 18:10:38 +08:00
|
|
|
{
|
2024-03-14 10:56:09 +08:00
|
|
|
struct packet *defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now);
|
2024-03-09 19:28:14 +08:00
|
|
|
if (defraged_pkt == NULL)
|
2024-03-08 18:10:38 +08:00
|
|
|
{
|
2024-04-09 15:07:53 +08:00
|
|
|
goto fast_path;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
pkt = defraged_pkt;
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_manager_dispatch_packet(plug_mgr, pkt);
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess = session_manager_lookup_session(sess_mgr, pkt);
|
|
|
|
|
if (sess == NULL)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
2024-03-14 10:56:09 +08:00
|
|
|
sess = session_manager_new_session(sess_mgr, pkt, now);
|
2024-03-08 18:10:38 +08:00
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
2024-04-09 15:07:53 +08:00
|
|
|
goto fast_path;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_ctx = plugin_manager_new_ctx(sess);
|
|
|
|
|
session_set_user_data(sess, plugin_ctx);
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-10 11:40:26 +08:00
|
|
|
if (session_manager_update_session(sess_mgr, sess, pkt, now) == -1)
|
|
|
|
|
{
|
|
|
|
|
goto fast_path;
|
|
|
|
|
}
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_manager_dispatch_session(plug_mgr, sess, pkt);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-09 15:07:53 +08:00
|
|
|
fast_path:
|
|
|
|
|
execute_packet_action(packet_io, sess, pkt, thr_idx);
|
2024-03-09 19:28:14 +08:00
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
idle_tasks:
|
|
|
|
|
// nr_recv packet atmost trigger nr_recv session evict
|
|
|
|
|
for (int i = 0; i < nr_recv; i++)
|
|
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
evicted_sess = session_manager_get_evicted_session(sess_mgr);
|
|
|
|
|
if (evicted_sess)
|
|
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_ctx = session_get_user_data(evicted_sess);
|
|
|
|
|
plugin_manager_free_ctx(plugin_ctx);
|
2024-03-08 18:10:38 +08:00
|
|
|
session_manager_free_session(sess_mgr, evicted_sess);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-14 10:56:09 +08:00
|
|
|
while ((expired_sess = session_manager_get_expired_session(sess_mgr, now)))
|
2024-03-08 18:10:38 +08:00
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
plugin_ctx = session_get_user_data(expired_sess);
|
|
|
|
|
plugin_manager_free_ctx(plugin_ctx);
|
2024-03-08 18:10:38 +08:00
|
|
|
session_manager_free_session(sess_mgr, expired_sess);
|
|
|
|
|
}
|
2024-03-21 19:27:41 +08:00
|
|
|
ip_reassembly_expire(ip_reass, now);
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-04-16 14:12:41 +08:00
|
|
|
stellar_thread_cron(thr_ctx);
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
// TODO
|
|
|
|
|
// plugin_manager_cron();
|
|
|
|
|
// poll_non_packet_events();
|
|
|
|
|
// packet_io_yield();
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
ATOMIC_SET(&thr_ctx->is_runing, 0);
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("worker thread %d exit !!!", thr_idx);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-12-08 11:16:23 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
static int stellar_thread_init(struct stellar_runtime *ctx, uint8_t nr_threads)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-03-14 10:56:09 +08:00
|
|
|
uint64_t now = timestamp_get_msec();
|
2024-02-28 16:30:03 +08:00
|
|
|
for (uint8_t i = 0; i < nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx *thr_ctx = &ctx->threads[i];
|
|
|
|
|
thr_ctx->idx = i;
|
|
|
|
|
thr_ctx->is_runing = 0;
|
|
|
|
|
thr_ctx->sess_mgr = session_manager_new(&config->sess_mgr_opts, now);
|
|
|
|
|
if (thr_ctx->sess_mgr == NULL)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create session manager");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-04-11 16:30:21 +08:00
|
|
|
thr_ctx->ip_mgr = ip_reassembly_new(&config->ip_opts);
|
|
|
|
|
if (thr_ctx->ip_mgr == NULL)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
static void stellar_thread_clean(struct stellar_runtime *ctx, uint8_t nr_threads)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-02-28 16:30:03 +08:00
|
|
|
for (uint8_t i = 0; i < nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx *thr_ctx = &ctx->threads[i];
|
|
|
|
|
if (ATOMIC_READ(&thr_ctx->is_runing) == 0)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("wait worker thread %d free context", i);
|
2024-04-11 16:30:21 +08:00
|
|
|
session_manager_free(thr_ctx->sess_mgr);
|
|
|
|
|
ip_reassembly_free(thr_ctx->ip_mgr);
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
static int stellar_thread_run(struct stellar_runtime *ctx, uint8_t nr_threads)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
for (uint8_t i = 0; i < nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx *thr_ctx = &ctx->threads[i];
|
|
|
|
|
if (pthread_create(&thr_ctx->tid, NULL, work_thread, (void *)thr_ctx) < 0)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
for (uint8_t i = 0; i < nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-11 16:30:21 +08:00
|
|
|
struct thread_ctx *thr_ctx = &ctx->threads[i];
|
|
|
|
|
while (ATOMIC_READ(&thr_ctx->is_runing) == 1)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("wait worker thread %d stop", i);
|
2024-01-29 14:15:33 +08:00
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* main
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2024-04-11 19:44:02 +08:00
|
|
|
uint8_t nr_threads;
|
2024-04-16 14:12:41 +08:00
|
|
|
uint64_t last_stat = 0;
|
|
|
|
|
struct io_stat *io_stat;
|
2024-04-11 16:30:21 +08:00
|
|
|
memset(runtime, 0, sizeof(struct stellar_runtime));
|
2024-04-11 19:44:02 +08:00
|
|
|
memset(config, 0, sizeof(struct stellar_config));
|
2024-01-30 18:07:08 +08:00
|
|
|
timestamp_update();
|
|
|
|
|
|
2024-01-31 14:45:50 +08:00
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
|
signal(SIGHUP, signal_handler);
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
if (log_init(log_config_file) != 0)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_ERROR("unable to init log");
|
2024-04-11 19:44:02 +08:00
|
|
|
goto error_out;
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
if (stellar_config_load(stellar_config_file, config) != 0)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_ERROR("unable to load config file");
|
2024-04-11 19:44:02 +08:00
|
|
|
goto error_out;
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
2024-04-11 16:30:21 +08:00
|
|
|
stellar_config_print(config);
|
2024-04-16 14:12:41 +08:00
|
|
|
nr_threads = config->io_opts.nr_threads;
|
2024-01-30 18:07:08 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
|
2024-01-31 14:45:50 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to init id generator");
|
2024-04-11 19:44:02 +08:00
|
|
|
goto error_out;
|
2024-01-31 14:45:50 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-16 14:12:41 +08:00
|
|
|
runtime->stat = stellar_stat_new(nr_threads);
|
|
|
|
|
if (runtime->stat == NULL)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create stellar stat");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
runtime->plug_mgr = plugin_manager_new();
|
|
|
|
|
if (runtime->plug_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create plugin manager");
|
2024-04-11 19:44:02 +08:00
|
|
|
goto error_out;
|
2024-04-11 16:30:21 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
runtime->packet_io = packet_io_new(&config->io_opts);
|
|
|
|
|
if (runtime->packet_io == NULL)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create packet io");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
if (stellar_thread_init(runtime, nr_threads) != 0)
|
2024-01-23 14:30:46 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to init thread context");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-11 16:30:21 +08:00
|
|
|
if (stellar_thread_run(runtime, nr_threads) != 0)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-01-29 14:15:33 +08:00
|
|
|
STELLAR_LOG_ERROR("unable to create worker thread");
|
|
|
|
|
goto error_out;
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 14:12:41 +08:00
|
|
|
io_stat = packet_io_get_stat(runtime->packet_io);
|
|
|
|
|
last_stat = timestamp_get_msec();
|
2024-04-11 16:30:21 +08:00
|
|
|
while (!ATOMIC_READ(&runtime->need_exit))
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
timestamp_update();
|
2024-04-16 14:12:41 +08:00
|
|
|
if (timestamp_get_msec() - last_stat > 2000)
|
|
|
|
|
{
|
|
|
|
|
stellar_peek_io_stat(runtime->stat, io_stat);
|
|
|
|
|
stellar_stat_output(runtime->stat);
|
|
|
|
|
last_stat = timestamp_get_msec();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
usleep(5 * 1000);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-04-11 16:30:21 +08:00
|
|
|
stellar_thread_join(runtime, nr_threads);
|
|
|
|
|
stellar_thread_clean(runtime, nr_threads);
|
|
|
|
|
packet_io_free(runtime->packet_io);
|
|
|
|
|
plugin_manager_free(runtime->plug_mgr);
|
2024-04-16 14:12:41 +08:00
|
|
|
stellar_stat_free(runtime->stat);
|
2024-04-11 19:44:02 +08:00
|
|
|
STELLAR_LOG_STATE("stellar exit !!!\n");
|
2024-01-30 18:07:08 +08:00
|
|
|
log_free();
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2023-12-08 11:16:23 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|