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-01-31 14:45:50 +08:00
|
|
|
#include "stellar.h"
|
2024-01-29 14:15:33 +08:00
|
|
|
#include "config.h"
|
2024-01-09 18:03:24 +08:00
|
|
|
#include "packet.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-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
|
|
|
|
|
|
|
|
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
|
|
|
|
|
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
struct thread_context
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
|
|
|
|
uint16_t index;
|
|
|
|
|
uint64_t need_exit;
|
|
|
|
|
uint64_t is_runing;
|
|
|
|
|
struct session_manager *sess_mgr;
|
2024-03-08 13:55:17 +08:00
|
|
|
struct ip_reassembly *ip_mgr;
|
2024-01-09 18:03:24 +08:00
|
|
|
};
|
|
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
struct stellar_context
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
uint64_t need_exit;
|
2024-01-29 14:15:33 +08:00
|
|
|
struct config config;
|
2024-01-10 10:19:47 +08:00
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
struct packet_io *packet_io;
|
2024-01-30 18:07:08 +08:00
|
|
|
struct thread_context threads_ctx[MAX_THREAD_NUM];
|
2024-01-29 14:15:33 +08:00
|
|
|
};
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
struct stellar_context stellar_context;
|
2024-02-28 16:30:03 +08:00
|
|
|
struct stellar_context *stellar_ctx = &stellar_context;
|
|
|
|
|
|
|
|
|
|
// config
|
2024-03-08 14:51:21 +08:00
|
|
|
struct device_options *dev_opts = &stellar_context.config.dev_opts;
|
|
|
|
|
struct packet_io_options *pkt_io_opts = &stellar_context.config.pkt_io_opts;
|
|
|
|
|
struct ip_reassembly_options *ip_reass_opts = &stellar_context.config.ip_reass_opts;
|
|
|
|
|
struct session_manager_options *sess_mgr_opts = &stellar_context.config.sess_mgr_opts;
|
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";
|
|
|
|
|
|
2024-03-26 15:09:03 +08:00
|
|
|
static void hex_dump(const char *payload, uint32_t len)
|
|
|
|
|
{
|
|
|
|
|
printf("Payload Length: %d\n", len);
|
|
|
|
|
for (uint32_t i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i > 0 && i % 16 == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("%02x ", (uint8_t)payload[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
void *plugin_manager_new_ctx()
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-03-08 18:10:38 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-01-30 18:07:08 +08:00
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
void plugin_manager_free_ctx(void *ctx)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
void plugin_manager_dispatch(void *plugin_mgr, struct session *sess, const struct packet *pkt)
|
2024-01-17 11:47:55 +08:00
|
|
|
{
|
2024-01-30 18:07:08 +08:00
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 15:09:03 +08:00
|
|
|
uint32_t len = 0;
|
|
|
|
|
const char *payload = NULL;
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
printf("=> plugin dispatch session: %p\n", sess);
|
2024-01-30 18:07:08 +08:00
|
|
|
session_dump(sess);
|
2024-03-26 15:09:03 +08:00
|
|
|
|
|
|
|
|
if (session_get_type(sess) == SESSION_TYPE_TCP)
|
|
|
|
|
{
|
|
|
|
|
payload = session_peek_tcp_payload(sess, &len);
|
|
|
|
|
if (payload && len > 0)
|
|
|
|
|
{
|
|
|
|
|
hex_dump(payload, len);
|
|
|
|
|
}
|
|
|
|
|
session_consume_tcp_payload(sess, len);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 18:10:38 +08:00
|
|
|
printf("<= plugin dispatch session\n");
|
2024-01-17 11:47:55 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* util
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-01-09 18:03:24 +08:00
|
|
|
static void signal_handler(int signo)
|
|
|
|
|
{
|
|
|
|
|
if (signo == SIGINT)
|
|
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("SIGINT received, exit !!!");
|
2024-02-28 16:30:03 +08:00
|
|
|
ATOMIC_SET(&stellar_ctx->need_exit, 1);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGQUIT)
|
|
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("SIGQUIT received, exit !!!");
|
2024-02-28 16:30:03 +08:00
|
|
|
ATOMIC_SET(&stellar_ctx->need_exit, 1);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGTERM)
|
|
|
|
|
{
|
2024-01-31 14:45:50 +08:00
|
|
|
STELLAR_LOG_STATE("SIGTERM received, exit !!!");
|
2024-02-28 16:30:03 +08:00
|
|
|
ATOMIC_SET(&stellar_ctx->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-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-01-30 18:07:08 +08:00
|
|
|
static void *main_loop(void *arg)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
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;
|
|
|
|
|
struct packet *pkt;
|
|
|
|
|
struct packet packets[RX_BURST_MAX];
|
2024-01-30 18:07:08 +08:00
|
|
|
struct thread_context *threads_ctx = (struct thread_context *)arg;
|
2024-02-28 16:30:03 +08:00
|
|
|
struct packet_io *packet_io = stellar_ctx->packet_io;
|
|
|
|
|
struct session_manager *sess_mgr = threads_ctx->sess_mgr;
|
2024-03-09 19:28:14 +08:00
|
|
|
struct ip_reassembly *ip_reass = threads_ctx->ip_mgr;
|
2024-03-08 18:10:38 +08:00
|
|
|
void *plug_mgr = NULL;
|
|
|
|
|
void *plug_mgr_ctx = NULL;
|
|
|
|
|
|
|
|
|
|
int nr_recv;
|
2024-03-09 19:28:14 +08:00
|
|
|
int need_drop_pkt = 1; // TODO
|
2024-03-14 10:56:09 +08:00
|
|
|
uint64_t now = 0;
|
2024-03-09 19:28:14 +08:00
|
|
|
uint16_t thr_idx = threads_ctx->index;
|
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-01-30 18:07:08 +08:00
|
|
|
ATOMIC_SET(&threads_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-01-30 18:07:08 +08:00
|
|
|
while (ATOMIC_READ(&threads_ctx->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-03-08 18:10:38 +08:00
|
|
|
pkt = &packets[i];
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// call plugin_manager_dispatch_raw_pkt();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-09 19:28:14 +08:00
|
|
|
// call plugin_manager_dispatch_defrag_pkt();
|
|
|
|
|
pkt = defraged_pkt;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
// TODO filter protocol before session lookup
|
|
|
|
|
|
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-03-09 19:28:14 +08:00
|
|
|
continue;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
|
|
|
|
plug_mgr_ctx = plugin_manager_new_ctx();
|
|
|
|
|
session_set_user_data(sess, plug_mgr_ctx);
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-14 10:56:09 +08:00
|
|
|
session_manager_update_session(sess_mgr, sess, pkt, now);
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
2024-03-08 18:10:38 +08:00
|
|
|
plugin_manager_dispatch(plug_mgr, sess, pkt);
|
2024-03-09 19:28:14 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
if (unlikely(need_drop_pkt))
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < nr_recv; i++)
|
|
|
|
|
{
|
|
|
|
|
if (packet_get_action(&packets[i]) == PACKET_ACTION_DROP)
|
|
|
|
|
{
|
|
|
|
|
packet_io_drop(packet_io, thr_idx, &packets[i], 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_egress(packet_io, thr_idx, &packets[i], 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_egress(packet_io, thr_idx, packets, nr_recv);
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
plug_mgr_ctx = session_get_user_data(evicted_sess);
|
|
|
|
|
plugin_manager_free_ctx(plug_mgr_ctx);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
plug_mgr_ctx = session_get_user_data(expired_sess);
|
|
|
|
|
plugin_manager_free_ctx(plug_mgr_ctx);
|
|
|
|
|
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-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-01-30 18:07:08 +08:00
|
|
|
ATOMIC_SET(&threads_ctx->is_runing, 0);
|
2024-03-09 19:28:14 +08:00
|
|
|
STELLAR_LOG_STATE("worker thread %d stop", thr_idx);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-12-08 11:16:23 +08:00
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
static int thread_context_init(struct stellar_context *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-01-30 18:07:08 +08:00
|
|
|
struct thread_context *threads_ctx = &ctx->threads_ctx[i];
|
|
|
|
|
threads_ctx->index = i;
|
|
|
|
|
threads_ctx->need_exit = 0;
|
|
|
|
|
threads_ctx->is_runing = 0;
|
2024-01-29 14:15:33 +08:00
|
|
|
|
2024-03-14 10:56:09 +08:00
|
|
|
threads_ctx->sess_mgr = session_manager_new(sess_mgr_opts, now);
|
2024-01-30 18:07:08 +08:00
|
|
|
if (threads_ctx->sess_mgr == NULL)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create session manager");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
threads_ctx->ip_mgr = ip_reassembly_new(ip_reass_opts);
|
2024-02-28 16:30:03 +08:00
|
|
|
if (threads_ctx->ip_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 16:30:03 +08:00
|
|
|
static void thread_context_free(struct stellar_context *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-01-30 18:07:08 +08:00
|
|
|
struct thread_context *threads_ctx = &ctx->threads_ctx[i];
|
|
|
|
|
if (ATOMIC_READ(&threads_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-03-08 14:20:36 +08:00
|
|
|
session_manager_free(threads_ctx->sess_mgr);
|
|
|
|
|
ip_reassembly_free(threads_ctx->ip_mgr);
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
static int thread_new(struct thread_context threads_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-01-30 18:07:08 +08:00
|
|
|
struct thread_context *ctx = &threads_ctx[i];
|
|
|
|
|
if (pthread_create(&ctx->tid, NULL, main_loop, (void *)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-03-08 14:20:36 +08:00
|
|
|
static void thread_free(struct thread_context threads_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-01-30 18:07:08 +08:00
|
|
|
struct thread_context *ctx = &threads_ctx[i];
|
|
|
|
|
ATOMIC_SET(&ctx->need_exit, 1);
|
|
|
|
|
while (ATOMIC_READ(&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-02-28 16:30:03 +08:00
|
|
|
memset(stellar_ctx, 0, sizeof(struct stellar_context));
|
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-01-29 14:15:33 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 18:07:08 +08:00
|
|
|
STELLAR_LOG_STATE("Start Stellar (version: %s)\n %s", __stellar_version, logo_str);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
if (parse_config_file(stellar_config_file, &stellar_ctx->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-01-30 18:07:08 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
print_config_options(&stellar_ctx->config);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
if (id_generator_init(dev_opts->device_base, dev_opts->device_offset) != 0)
|
2024-01-31 14:45:50 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to init id generator");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-31 14:45:50 +08:00
|
|
|
// TODO load plugin
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-03-08 14:51:21 +08:00
|
|
|
uint8_t nr_threads = pkt_io_opts->nr_threads;
|
|
|
|
|
stellar_ctx->packet_io = packet_io_new(pkt_io_opts);
|
2024-02-28 16:30:03 +08:00
|
|
|
if (stellar_ctx->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-02-28 16:30:03 +08:00
|
|
|
if (thread_context_init(stellar_ctx, 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-03-08 14:20:36 +08:00
|
|
|
if (thread_new(stellar_ctx->threads_ctx, 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-02-28 16:30:03 +08:00
|
|
|
while (!ATOMIC_READ(&stellar_ctx->need_exit))
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
timestamp_update();
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-03-08 14:20:36 +08:00
|
|
|
thread_free(stellar_ctx->threads_ctx, nr_threads);
|
2024-02-28 16:30:03 +08:00
|
|
|
thread_context_free(stellar_ctx, nr_threads);
|
2024-03-08 14:20:36 +08:00
|
|
|
packet_io_free(stellar_ctx->packet_io);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
// TODO free plugin
|
|
|
|
|
|
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;
|
|
|
|
|
}
|