2024-01-09 18:03:24 +08:00
|
|
|
#include <errno.h>
|
2024-06-25 10:32:51 +08:00
|
|
|
#include <stdio.h>
|
2024-05-09 14:57:12 +08:00
|
|
|
#include <assert.h>
|
2024-01-09 18:03:24 +08:00
|
|
|
#include <unistd.h>
|
2024-06-25 10:32:51 +08:00
|
|
|
#include <string.h>
|
2024-06-27 15:07:54 +08:00
|
|
|
#include <stdlib.h>
|
2024-08-19 17:28:45 +08:00
|
|
|
#include <limits.h>
|
2024-06-25 10:32:51 +08:00
|
|
|
#include <pthread.h>
|
2024-01-30 18:07:08 +08:00
|
|
|
#include <sys/prctl.h>
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-08-16 10:19:36 +08:00
|
|
|
#include "utils.h"
|
2024-08-16 11:02:19 +08:00
|
|
|
#include "snowflake.h"
|
2024-08-23 18:44:17 +08:00
|
|
|
#include "packet_io.h"
|
|
|
|
|
#include "log_private.h"
|
2024-06-25 14:08:33 +08:00
|
|
|
#include "stellar_stat.h"
|
2024-06-27 15:07:54 +08:00
|
|
|
#include "stellar_core.h"
|
2024-06-25 19:12:21 +08:00
|
|
|
#include "stellar_config.h"
|
2024-08-23 18:44:17 +08:00
|
|
|
#include "packet_private.h"
|
2024-05-28 10:26:29 +08:00
|
|
|
#include "plugin_manager.h"
|
2024-08-23 18:44:17 +08:00
|
|
|
#include "session_private.h"
|
2024-06-27 15:07:54 +08:00
|
|
|
#include "session_manager.h"
|
2024-01-30 18:07:08 +08:00
|
|
|
|
2024-08-23 18:44:17 +08:00
|
|
|
#define CORE_LOG_FATAL(format, ...) STELLAR_LOG_FATAL(__thread_local_logger, "core", format, ##__VA_ARGS__)
|
|
|
|
|
#define CORE_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "core", format, ##__VA_ARGS__)
|
|
|
|
|
#define CORE_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "core", format, ##__VA_ARGS__)
|
2024-06-25 10:32:51 +08:00
|
|
|
|
2024-08-16 10:43:00 +08:00
|
|
|
#ifdef STELLAR_GIT_VERSION
|
|
|
|
|
static __attribute__((__used__)) const char *version = STELLAR_GIT_VERSION;
|
|
|
|
|
#else
|
|
|
|
|
static __attribute__((__used__)) const char *version = "Unknown";
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-08-16 17:19:39 +08:00
|
|
|
static const char logo_str[] =
|
|
|
|
|
" _ _ _\n"
|
|
|
|
|
" ___ | |_ ___ | | | | __ _ _ __\n"
|
|
|
|
|
" / __| | __| / _ \\ | | | | / _` | | '__|\n"
|
|
|
|
|
" \\__ \\ | |_ | __/ | | | | | (_| | | |\n"
|
|
|
|
|
" |___/ \\__| \\___| |_| |_| \\__,_| |_|\n";
|
|
|
|
|
|
2024-08-15 19:03:48 +08:00
|
|
|
struct schedule_data
|
|
|
|
|
{
|
|
|
|
|
uint64_t last_free_expired_session_timestamp;
|
|
|
|
|
uint64_t last_free_expired_ip_frag_timestamp;
|
|
|
|
|
uint64_t last_merge_thread_stat_timestamp;
|
|
|
|
|
|
|
|
|
|
uint64_t free_expired_session_interval;
|
|
|
|
|
uint64_t free_expired_session_batch;
|
|
|
|
|
|
|
|
|
|
uint64_t free_expired_ip_frag_interval;
|
|
|
|
|
uint64_t free_expired_ip_frag_batch;
|
|
|
|
|
|
|
|
|
|
uint64_t merge_stat_interval;
|
|
|
|
|
uint64_t packet_io_yield_interval;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-28 10:26:29 +08:00
|
|
|
struct stellar_thread
|
|
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
|
|
|
|
uint16_t idx;
|
|
|
|
|
uint64_t is_runing;
|
2024-08-15 19:03:48 +08:00
|
|
|
struct schedule_data sched_data;
|
2024-08-23 15:21:07 +08:00
|
|
|
struct snowflake *snowflake;
|
2024-05-28 10:26:29 +08:00
|
|
|
struct ip_reassembly *ip_mgr;
|
|
|
|
|
struct session_manager *sess_mgr;
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar *st;
|
2024-05-28 10:26:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stellar_runtime
|
|
|
|
|
{
|
|
|
|
|
uint64_t need_exit;
|
|
|
|
|
uint64_t stat_last_output_ts;
|
2024-08-23 18:44:17 +08:00
|
|
|
struct logger *logger;
|
2024-05-28 10:26:29 +08:00
|
|
|
struct stellar_stat *stat;
|
|
|
|
|
struct packet_io *packet_io;
|
|
|
|
|
struct plugin_manager_schema *plug_mgr;
|
|
|
|
|
struct stellar_thread threads[MAX_THREAD_NUM];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stellar
|
|
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
char stellar_cfg_file[PATH_MAX];
|
|
|
|
|
char plugin_cfg_file[PATH_MAX];
|
|
|
|
|
char log_cfg_file[PATH_MAX];
|
|
|
|
|
|
2024-05-28 10:26:29 +08:00
|
|
|
struct stellar_runtime runtime;
|
|
|
|
|
struct stellar_config config;
|
|
|
|
|
};
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-08-23 15:21:07 +08:00
|
|
|
static thread_local uint16_t __current_thread_idx = UINT16_MAX;
|
|
|
|
|
static thread_local struct snowflake *__current_thread_snowflake = NULL;
|
|
|
|
|
|
|
|
|
|
uint64_t stellar_generate_session_id(uint64_t now_sec);
|
2024-05-28 10:26:29 +08:00
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Stellar Thread Main Loop
|
|
|
|
|
******************************************************************************/
|
2024-01-30 18:07:08 +08:00
|
|
|
|
2024-04-18 14:20:28 +08:00
|
|
|
static void update_session_stat(struct session *sess, struct packet *pkt)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
2024-04-18 14:20:28 +08:00
|
|
|
if (sess)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
2024-05-15 16:29:33 +08:00
|
|
|
enum flow_direction dir = session_get_current_flow_direction(sess);
|
2024-05-16 17:05:45 +08:00
|
|
|
assert(dir != FLOW_DIRECTION_NONE);
|
2024-04-18 14:20:28 +08:00
|
|
|
int is_ctrl = packet_is_ctrl(pkt);
|
2024-06-14 19:24:27 +08:00
|
|
|
uint16_t len = packet_get_raw_len(pkt);
|
2024-05-09 14:57:12 +08:00
|
|
|
switch (packet_get_action(pkt))
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
2024-05-09 14:57:12 +08:00
|
|
|
case PACKET_ACTION_DROP:
|
2024-05-09 16:32:45 +08:00
|
|
|
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1);
|
2024-06-14 19:24:27 +08:00
|
|
|
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), len);
|
2024-05-09 14:57:12 +08:00
|
|
|
break;
|
|
|
|
|
case PACKET_ACTION_FORWARD:
|
2024-05-09 16:32:45 +08:00
|
|
|
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1);
|
2024-06-14 19:24:27 +08:00
|
|
|
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), len);
|
|
|
|
|
break;
|
2024-05-09 14:57:12 +08:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session_set_current_packet(sess, NULL);
|
2024-05-16 17:05:45 +08:00
|
|
|
session_set_current_flow_direction(sess, FLOW_DIRECTION_NONE);
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
2024-04-09 15:07:53 +08:00
|
|
|
|
2024-04-18 14:20:28 +08:00
|
|
|
static inline void free_evicted_sessions(struct session_manager *sess_mgr, uint64_t max_free)
|
|
|
|
|
{
|
|
|
|
|
void *plugin_ctx = NULL;
|
|
|
|
|
struct session *sess = NULL;
|
|
|
|
|
for (uint64_t i = 0; i < max_free; i++)
|
2024-04-09 15:07:53 +08:00
|
|
|
{
|
2024-04-18 14:20:28 +08:00
|
|
|
sess = session_manager_get_evicted_session(sess_mgr);
|
|
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
plugin_ctx = session_get_user_data(sess);
|
2024-08-06 20:37:59 +08:00
|
|
|
plugin_manager_on_session_closing(sess);
|
2024-05-20 11:12:24 +08:00
|
|
|
plugin_manager_session_runtime_free((struct plugin_manager_runtime *)plugin_ctx);
|
2024-04-18 14:20:28 +08:00
|
|
|
session_manager_free_session(sess_mgr, sess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-04-09 15:07:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 17:13:32 +08:00
|
|
|
static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now_ms)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
2024-04-18 14:20:28 +08:00
|
|
|
void *plugin_ctx = NULL;
|
|
|
|
|
struct session *sess = NULL;
|
|
|
|
|
for (uint64_t i = 0; i < max_free; i++)
|
|
|
|
|
{
|
2024-07-16 17:13:32 +08:00
|
|
|
sess = session_manager_get_expired_session(sess_mgr, now_ms);
|
2024-04-18 14:20:28 +08:00
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
plugin_ctx = session_get_user_data(sess);
|
2024-08-06 20:37:59 +08:00
|
|
|
plugin_manager_on_session_closing(sess);
|
2024-05-20 11:12:24 +08:00
|
|
|
plugin_manager_session_runtime_free((struct plugin_manager_runtime *)plugin_ctx);
|
2024-04-18 14:20:28 +08:00
|
|
|
session_manager_free_session(sess_mgr, sess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 17:13:32 +08:00
|
|
|
static inline void merge_thread_stat(struct stellar_thread *thread)
|
2024-04-16 14:12:41 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar *st = thread->st;
|
|
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
2024-04-17 11:44:31 +08:00
|
|
|
struct thread_stat thr_stat = {
|
2024-06-25 14:08:33 +08:00
|
|
|
.packet_io = packet_io_stat(runtime->packet_io, thread->idx),
|
|
|
|
|
.ip_reassembly = ip_reassembly_stat(thread->ip_mgr),
|
|
|
|
|
.session_mgr = session_manager_stat(thread->sess_mgr),
|
2024-04-17 11:44:31 +08:00
|
|
|
};
|
2024-04-18 14:20:28 +08:00
|
|
|
stellar_stat_merge(runtime->stat, &thr_stat, thread->idx);
|
2024-04-16 14:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
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;
|
2024-07-16 17:13:32 +08:00
|
|
|
uint64_t now_ms = 0;
|
2024-04-18 14:20:28 +08:00
|
|
|
char thd_name[16] = {0};
|
|
|
|
|
void *plugin_ctx = NULL;
|
|
|
|
|
struct packet *pkt = NULL;
|
2024-04-30 16:19:28 +08:00
|
|
|
struct packet *defraged_pkt = NULL;
|
2024-04-11 16:30:21 +08:00
|
|
|
struct packet packets[RX_BURST_MAX];
|
2024-04-18 14:20:28 +08:00
|
|
|
struct session *sess = NULL;
|
|
|
|
|
struct stellar_thread *thread = (struct stellar_thread *)arg;
|
|
|
|
|
struct ip_reassembly *ip_reass = thread->ip_mgr;
|
|
|
|
|
struct session_manager *sess_mgr = thread->sess_mgr;
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar *st = thread->st;
|
|
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
2024-08-15 19:03:48 +08:00
|
|
|
struct schedule_data *sched_data = &thread->sched_data;
|
2024-05-28 10:26:29 +08:00
|
|
|
struct packet_io *packet_io = runtime->packet_io;
|
|
|
|
|
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
|
2024-04-22 20:01:15 +08:00
|
|
|
uint16_t thr_idx = thread->idx;
|
2024-08-23 15:21:07 +08:00
|
|
|
|
|
|
|
|
__current_thread_idx = thr_idx;
|
2024-08-23 18:44:17 +08:00
|
|
|
__thread_local_logger = runtime->logger;
|
2024-08-23 15:21:07 +08:00
|
|
|
__current_thread_snowflake = thread->snowflake;
|
2024-04-18 14:20:28 +08:00
|
|
|
|
2024-08-06 15:32:24 +08:00
|
|
|
memset(packets, 0, sizeof(packets));
|
|
|
|
|
|
2024-08-14 10:57:08 +08:00
|
|
|
for (int i = 0; i < RX_BURST_MAX; i++)
|
2024-08-06 20:37:59 +08:00
|
|
|
{
|
2024-08-14 10:57:08 +08:00
|
|
|
packet_set_user_data(&packets[i], (void *)plug_mgr);
|
2024-08-06 20:37:59 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-18 14:20:28 +08:00
|
|
|
snprintf(thd_name, sizeof(thd_name), "stellar:%d", thr_idx);
|
|
|
|
|
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
|
2024-04-17 11:44:31 +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
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to init marsio thread");
|
2024-01-30 18:07:08 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-18 14:20:28 +08:00
|
|
|
ATOMIC_SET(&thread->is_runing, 1);
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("worker thread %d runing", thr_idx);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
while (ATOMIC_READ(&runtime->need_exit) == 0)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-07-16 17:13:32 +08:00
|
|
|
/*
|
|
|
|
|
* We use the system's real time instead of monotonic time for the following reasons:
|
|
|
|
|
* -> Session creation/closure times require real time (e.g., for logging session activities).
|
|
|
|
|
* -> Session ID generation relies on real time (e.g., for reverse calculating session creation time from the session ID).
|
|
|
|
|
*
|
|
|
|
|
* Note: Modifying the system time will affect the timing wheel, impacting session expiration, IP reassembly expiration, and TCP reassembly expiration.
|
|
|
|
|
* Suggestion: After modifying the system time, restart the service to ensure consistent timing.
|
|
|
|
|
*/
|
2024-08-16 10:19:36 +08:00
|
|
|
now_ms = clock_get_real_time_ms();
|
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-04-30 16:19:28 +08:00
|
|
|
defraged_pkt = NULL;
|
2024-03-08 18:10:38 +08:00
|
|
|
pkt = &packets[i];
|
|
|
|
|
|
2024-08-06 20:37:59 +08:00
|
|
|
plugin_manager_on_packet_ingress(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-07-16 17:13:32 +08:00
|
|
|
defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now_ms);
|
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-08-14 10:57:08 +08:00
|
|
|
plugin_manager_on_packet_ingress(plug_mgr, defraged_pkt);
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 10:44:57 +08:00
|
|
|
sess = session_manager_lookup_session_by_packet(sess_mgr, pkt);
|
2024-03-08 18:10:38 +08:00
|
|
|
if (sess == NULL)
|
2024-01-30 18:07:08 +08:00
|
|
|
{
|
2024-07-16 17:13:32 +08:00
|
|
|
sess = session_manager_new_session(sess_mgr, pkt, now_ms);
|
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-08-19 17:28:45 +08:00
|
|
|
plugin_ctx = plugin_manager_session_runtime_new(plug_mgr, sess);
|
2024-04-11 16:30:21 +08:00
|
|
|
session_set_user_data(sess, plugin_ctx);
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-16 17:13:32 +08:00
|
|
|
if (session_manager_update_session(sess_mgr, sess, pkt, now_ms) == -1)
|
2024-04-10 11:40:26 +08:00
|
|
|
{
|
|
|
|
|
goto fast_path;
|
|
|
|
|
}
|
2024-01-30 18:07:08 +08:00
|
|
|
}
|
2024-05-09 14:57:12 +08:00
|
|
|
if (packet_get_session_id(pkt) == 0)
|
|
|
|
|
{
|
|
|
|
|
packet_set_session_id(pkt, session_get_id(sess));
|
|
|
|
|
}
|
2024-05-17 16:55:46 +08:00
|
|
|
plugin_manager_on_session_ingress(sess, pkt);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-04-09 15:07:53 +08:00
|
|
|
fast_path:
|
2024-05-17 16:55:46 +08:00
|
|
|
plugin_manager_on_session_egress(sess, pkt);
|
2024-08-14 10:57:08 +08:00
|
|
|
if (pkt == defraged_pkt)
|
|
|
|
|
{
|
|
|
|
|
plugin_manager_on_packet_egress(plug_mgr, defraged_pkt);
|
|
|
|
|
plugin_manager_on_packet_egress(plug_mgr, &packets[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
plugin_manager_on_packet_egress(plug_mgr, pkt);
|
|
|
|
|
}
|
2024-08-06 20:37:59 +08:00
|
|
|
|
2024-05-20 17:02:16 +08:00
|
|
|
if (sess && session_get_current_state(sess) == SESSION_STATE_DISCARD)
|
|
|
|
|
{
|
|
|
|
|
packet_set_action(pkt, PACKET_ACTION_DROP);
|
|
|
|
|
}
|
2024-04-18 14:20:28 +08:00
|
|
|
update_session_stat(sess, pkt);
|
2024-04-24 11:40:00 +08:00
|
|
|
if (packet_get_action(pkt) == PACKET_ACTION_DROP)
|
2024-04-18 14:20:28 +08:00
|
|
|
{
|
2024-04-30 16:19:28 +08:00
|
|
|
if (pkt == defraged_pkt)
|
|
|
|
|
{
|
|
|
|
|
packet_io_drop(packet_io, thr_idx, &packets[i], 1);
|
|
|
|
|
packet_free(defraged_pkt);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_drop(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
2024-04-24 11:40:00 +08:00
|
|
|
}
|
|
|
|
|
else // PACKET_ACTION_FORWARD
|
|
|
|
|
{
|
2024-04-30 16:19:28 +08:00
|
|
|
if (pkt == defraged_pkt)
|
|
|
|
|
{
|
2024-07-02 10:09:54 +08:00
|
|
|
// TODO
|
|
|
|
|
// copy meta from defraged_pkt to packets[i]
|
2024-04-30 16:19:28 +08:00
|
|
|
packet_io_egress(packet_io, thr_idx, &packets[i], 1);
|
|
|
|
|
packet_free(defraged_pkt);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_egress(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
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:
|
2024-04-18 14:20:28 +08:00
|
|
|
// nr_recv packet atmost trigger nr_recv session evicted
|
|
|
|
|
free_evicted_sessions(sess_mgr, nr_recv);
|
2024-08-19 17:28:45 +08:00
|
|
|
plugin_manager_on_polling(plug_mgr);
|
2024-04-18 14:20:28 +08:00
|
|
|
|
2024-08-15 19:03:48 +08:00
|
|
|
// 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)
|
2024-03-09 19:28:14 +08:00
|
|
|
{
|
2024-08-15 19:03:48 +08:00
|
|
|
free_expired_sessions(sess_mgr, sched_data->free_expired_session_batch, now_ms);
|
|
|
|
|
sched_data->last_free_expired_session_timestamp = now_ms;
|
2024-03-08 18:10:38 +08:00
|
|
|
}
|
2024-02-28 16:30:03 +08:00
|
|
|
|
2024-08-15 19:03:48 +08:00
|
|
|
// per merge_stat_interval merge thread stat
|
|
|
|
|
if (now_ms - sched_data->last_merge_thread_stat_timestamp > sched_data->merge_stat_interval)
|
|
|
|
|
{
|
|
|
|
|
merge_thread_stat(thread);
|
|
|
|
|
sched_data->last_merge_thread_stat_timestamp = now_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// per free_expired_ip_frag_interval MAX free_expired_ip_frag_batch ip fragments are released
|
|
|
|
|
if (now_ms - sched_data->last_free_expired_ip_frag_timestamp > sched_data->free_expired_ip_frag_interval)
|
|
|
|
|
{
|
|
|
|
|
ip_reassembly_expire(ip_reass, sched_data->free_expired_ip_frag_batch, now_ms);
|
|
|
|
|
sched_data->last_free_expired_ip_frag_timestamp = now_ms;
|
|
|
|
|
}
|
2024-04-18 14:20:28 +08:00
|
|
|
|
|
|
|
|
if (nr_recv == 0)
|
|
|
|
|
{
|
2024-08-15 19:03:48 +08:00
|
|
|
packet_io_yield(packet_io, thr_idx, sched_data->packet_io_yield_interval);
|
2024-04-18 14:20:28 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-18 14:20:28 +08:00
|
|
|
ATOMIC_SET(&thread->is_runing, 0);
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("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-05-28 10:26:29 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Stellar Main Function
|
|
|
|
|
******************************************************************************/
|
2024-04-25 16:48:50 +08:00
|
|
|
|
2024-05-28 10:26:29 +08:00
|
|
|
static int all_session_have_freed(struct stellar_runtime *runtime, struct stellar_config *config)
|
2024-04-25 16:48:50 +08:00
|
|
|
{
|
2024-06-25 19:12:21 +08:00
|
|
|
for (int i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = runtime->threads[i].sess_mgr;
|
|
|
|
|
struct session_manager_stat *sess_stat = session_manager_stat(sess_mgr);
|
|
|
|
|
|
2024-08-16 18:41:02 +08:00
|
|
|
if (ATOMIC_READ(&sess_stat->tcp_sess_used) != 0)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 18:41:02 +08:00
|
|
|
if (ATOMIC_READ(&sess_stat->udp_sess_used) != 0)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
static int stellar_thread_init(struct stellar *st)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
|
|
|
|
struct stellar_config *config = &st->config;
|
|
|
|
|
|
2024-08-16 10:19:36 +08:00
|
|
|
uint64_t now_ms = clock_get_real_time_ms();
|
2024-06-25 19:12:21 +08:00
|
|
|
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-25 16:48:50 +08:00
|
|
|
struct stellar_thread *thread = &runtime->threads[i];
|
2024-04-18 14:20:28 +08:00
|
|
|
thread->idx = i;
|
|
|
|
|
thread->is_runing = 0;
|
2024-08-15 19:03:48 +08:00
|
|
|
|
|
|
|
|
thread->sched_data.last_free_expired_session_timestamp = now_ms;
|
|
|
|
|
thread->sched_data.last_free_expired_ip_frag_timestamp = now_ms;
|
|
|
|
|
thread->sched_data.last_merge_thread_stat_timestamp = now_ms;
|
|
|
|
|
|
|
|
|
|
thread->sched_data.free_expired_session_interval = config->sched_opts.free_expired_session_interval;
|
|
|
|
|
thread->sched_data.free_expired_session_batch = config->sched_opts.free_expired_session_batch;
|
|
|
|
|
|
|
|
|
|
thread->sched_data.free_expired_ip_frag_interval = config->sched_opts.free_expired_ip_frag_interval;
|
|
|
|
|
thread->sched_data.free_expired_ip_frag_batch = config->sched_opts.free_expired_ip_frag_batch;
|
|
|
|
|
|
|
|
|
|
thread->sched_data.merge_stat_interval = config->sched_opts.merge_stat_interval;
|
|
|
|
|
thread->sched_data.packet_io_yield_interval = config->sched_opts.packet_io_yield_interval;
|
|
|
|
|
|
2024-08-23 15:21:07 +08:00
|
|
|
thread->snowflake = snowflake_new(i, config->snowflake_opts.snowflake_base, config->snowflake_opts.snowflake_offset);
|
|
|
|
|
if (thread->snowflake == NULL)
|
|
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create snowflake id generator");
|
2024-08-23 15:21:07 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-07-16 17:13:32 +08:00
|
|
|
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms);
|
2024-04-18 14:20:28 +08:00
|
|
|
if (thread->sess_mgr == NULL)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create session manager");
|
2024-01-29 14:15:33 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-08-23 15:21:07 +08:00
|
|
|
session_manager_set_session_id_generator(thread->sess_mgr, stellar_generate_session_id);
|
2024-06-25 19:12:21 +08:00
|
|
|
thread->ip_mgr = ip_reassembly_new(&config->ip_reass_opts);
|
2024-04-18 14:20:28 +08:00
|
|
|
if (thread->ip_mgr == NULL)
|
2024-02-28 16:30:03 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create ip reassemble manager");
|
2024-02-28 16:30:03 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-08-19 17:28:45 +08:00
|
|
|
thread->st = st;
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
static void stellar_thread_clean(struct stellar *st)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
|
|
|
|
struct stellar_config *config = &st->config;
|
|
|
|
|
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("cleaning worker thread context ...");
|
2024-06-25 19:12:21 +08:00
|
|
|
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-25 16:48:50 +08:00
|
|
|
struct stellar_thread *thread = &runtime->threads[i];
|
2024-04-18 14:20:28 +08:00
|
|
|
if (ATOMIC_READ(&thread->is_runing) == 0)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-18 14:20:28 +08:00
|
|
|
ip_reassembly_free(thread->ip_mgr);
|
2024-08-23 15:21:07 +08:00
|
|
|
session_manager_free(thread->sess_mgr);
|
|
|
|
|
snowflake_free(thread->snowflake);
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("worker thread context cleaned");
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
static int stellar_thread_run(struct stellar *st)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
|
|
|
|
struct stellar_config *config = &st->config;
|
|
|
|
|
|
2024-06-25 19:12:21 +08:00
|
|
|
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-25 16:48:50 +08:00
|
|
|
struct stellar_thread *thread = &runtime->threads[i];
|
2024-04-18 14:20:28 +08:00
|
|
|
if (pthread_create(&thread->tid, NULL, work_thread, (void *)thread) < 0)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
|
2024-01-29 14:15:33 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
static void stellar_thread_join(struct stellar *st)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
|
|
|
|
struct stellar_config *config = &st->config;
|
|
|
|
|
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("waiting worker thread stop ...");
|
2024-06-25 19:12:21 +08:00
|
|
|
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-04-25 16:48:50 +08:00
|
|
|
struct stellar_thread *thread = &runtime->threads[i];
|
2024-04-18 14:20:28 +08:00
|
|
|
while (ATOMIC_READ(&thread->is_runing) == 1)
|
2024-01-29 14:15:33 +08:00
|
|
|
{
|
2024-08-15 19:03:48 +08:00
|
|
|
usleep(1000); // 1ms
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("all worker thread stoped");
|
2024-01-29 14:15:33 +08:00
|
|
|
}
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg_file, const char *log_cfg_file)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-19 17:28:45 +08:00
|
|
|
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));
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
struct stellar_runtime *runtime = &st->runtime;
|
|
|
|
|
struct stellar_config *config = &st->config;
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-23 18:44:17 +08:00
|
|
|
runtime->logger = log_new(st->log_cfg_file);
|
|
|
|
|
if (runtime->logger == NULL)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
printf("unable to create logger");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-08-23 18:44:17 +08:00
|
|
|
__thread_local_logger = runtime->logger;
|
|
|
|
|
CORE_LOG_FATAL("stellar start (version: %s)\n %s", version, logo_str);
|
|
|
|
|
CORE_LOG_FATAL("stellar config file : %s", st->stellar_cfg_file);
|
|
|
|
|
CORE_LOG_FATAL("plugin config file : %s", st->plugin_cfg_file);
|
|
|
|
|
CORE_LOG_FATAL("log config file : %s", st->log_cfg_file);
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
if (stellar_config_load(config, st->stellar_cfg_file) != 0)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to load config file");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-06-25 19:12:21 +08:00
|
|
|
stellar_config_print(config);
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-06-25 19:12:21 +08:00
|
|
|
runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_threads);
|
2024-05-28 10:26:29 +08:00
|
|
|
if (runtime->stat == NULL)
|
|
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create stellar stat");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-08-19 17:28:45 +08:00
|
|
|
runtime->plug_mgr = plugin_manager_init(st, st->plugin_cfg_file);
|
2024-05-28 10:26:29 +08:00
|
|
|
if (runtime->plug_mgr == NULL)
|
|
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create plugin manager");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-25 19:12:21 +08:00
|
|
|
runtime->packet_io = packet_io_new(&config->pkt_io_opts);
|
2024-05-28 10:26:29 +08:00
|
|
|
if (runtime->packet_io == NULL)
|
|
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create packet io");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
if (stellar_thread_init(st) != 0)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to init thread context");
|
2024-05-28 10:26:29 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
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)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_ERROR("unable to create worker thread");
|
2024-08-19 17:28:45 +08:00
|
|
|
return;
|
2024-05-28 10:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-16 10:19:36 +08:00
|
|
|
runtime->stat_last_output_ts = clock_get_real_time_ms();
|
2024-08-19 17:28:45 +08:00
|
|
|
while (!ATOMIC_READ(&runtime->need_exit))
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-16 10:19:36 +08:00
|
|
|
if (clock_get_real_time_ms() - runtime->stat_last_output_ts > config->sched_opts.output_stat_interval)
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-16 10:19:36 +08:00
|
|
|
runtime->stat_last_output_ts = clock_get_real_time_ms();
|
2024-05-28 10:26:29 +08:00
|
|
|
stellar_stat_output(runtime->stat);
|
|
|
|
|
}
|
|
|
|
|
usleep(1000); // 1ms
|
|
|
|
|
|
2024-05-29 18:12:46 +08:00
|
|
|
// only available in dump file mode, automatically exits when all sessions have been released
|
2024-08-21 14:33:03 +08:00
|
|
|
if (packet_io_isbreak(runtime->packet_io) && all_session_have_freed(runtime, config))
|
2024-05-28 10:26:29 +08:00
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("all sessions have been released, notify threads to exit");
|
2024-08-21 15:54:35 +08:00
|
|
|
stellar_stat_output(runtime->stat); // flush stat
|
2024-08-19 17:28:45 +08:00
|
|
|
ATOMIC_SET(&runtime->need_exit, 1);
|
2024-05-28 10:26:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-19 17:28:45 +08:00
|
|
|
}
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
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);
|
2024-08-23 18:44:17 +08:00
|
|
|
CORE_LOG_FATAL("stellar exit\n");
|
|
|
|
|
log_free(runtime->logger);
|
2024-08-23 17:19:05 +08:00
|
|
|
|
|
|
|
|
free(st);
|
|
|
|
|
st = NULL;
|
2024-08-19 17:28:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-28 10:26:29 +08:00
|
|
|
|
2024-08-19 17:28:45 +08:00
|
|
|
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)
|
|
|
|
|
{
|
2024-08-23 18:44:17 +08:00
|
|
|
log_level_reload(st->runtime.logger);
|
2024-08-19 17:28:45 +08:00
|
|
|
}
|
2024-05-28 10:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* Stellar Utility Function
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
struct packet_io *stellar_get_packet_io(const struct stellar *st)
|
|
|
|
|
{
|
|
|
|
|
return st->runtime.packet_io;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *st)
|
|
|
|
|
{
|
|
|
|
|
return st->runtime.plug_mgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr)
|
|
|
|
|
{
|
|
|
|
|
st->runtime.plug_mgr = plug_mgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session_manager *stellar_get_session_manager(const struct stellar *st)
|
|
|
|
|
{
|
|
|
|
|
uint16_t idx = stellar_get_current_thread_index();
|
|
|
|
|
return st->runtime.threads[idx].sess_mgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t stellar_get_current_thread_index()
|
|
|
|
|
{
|
2024-08-23 15:21:07 +08:00
|
|
|
if (__current_thread_idx == UINT16_MAX)
|
|
|
|
|
{
|
|
|
|
|
printf("get current thread index before set\n");
|
|
|
|
|
abort();
|
|
|
|
|
return UINT16_MAX;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return __current_thread_idx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t stellar_generate_session_id(uint64_t now_sec)
|
|
|
|
|
{
|
|
|
|
|
if (__current_thread_snowflake == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("get current thread snowflake before set\n");
|
|
|
|
|
abort();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return snowflake_generate(__current_thread_snowflake, now_sec);
|
|
|
|
|
}
|
2024-05-28 10:26:29 +08:00
|
|
|
}
|
2024-06-27 15:07:54 +08:00
|
|
|
|
|
|
|
|
// only send user crafted packet, can't send packet which come from network
|
2024-08-16 14:15:03 +08:00
|
|
|
void stellar_send_build_packet(struct stellar *st, struct packet *pkt)
|
2024-06-27 15:07:54 +08:00
|
|
|
{
|
|
|
|
|
uint16_t thr_idx = stellar_get_current_thread_index();
|
|
|
|
|
struct packet_io *packet_io = stellar_get_packet_io(st);
|
|
|
|
|
struct session_manager *sess_mgr = stellar_get_session_manager(st);
|
2024-07-16 17:13:32 +08:00
|
|
|
session_manager_record_duplicated_packet(sess_mgr, pkt);
|
2024-06-27 15:07:54 +08:00
|
|
|
|
|
|
|
|
if (packet_get_origin_ctx(pkt))
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
abort();
|
|
|
|
|
packet_io_egress(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
packet_io_inject(packet_io, thr_idx, pkt, 1);
|
|
|
|
|
}
|
2024-08-06 20:37:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int stellar_get_worker_thread_num(struct stellar *st)
|
|
|
|
|
{
|
|
|
|
|
return st->config.pkt_io_opts.nr_threads;
|
2024-08-23 18:44:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct logger *stellar_get_logger(struct stellar *st)
|
|
|
|
|
{
|
|
|
|
|
if (st)
|
|
|
|
|
{
|
|
|
|
|
return st->runtime.logger;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-06-27 15:07:54 +08:00
|
|
|
}
|