update stellar thread main loop

This commit is contained in:
luwenpeng
2024-03-08 18:10:38 +08:00
parent 734f6a5135
commit ee35a26a9d
14 changed files with 406 additions and 340 deletions

View File

@@ -56,35 +56,27 @@ struct session_manager_options *sess_mgr_opts = &stellar_context.config.sess_mgr
static const char *log_config_file = "./conf/log.toml";
static const char *stellar_config_file = "./conf/stellar.toml";
/******************************************************************************
* example
******************************************************************************/
static void __packet_plugin(const struct packet *pkt)
// TODO
void *plugin_manager_new_ctx()
{
if (pkt == NULL)
{
return;
}
printf("=> packet dispatch: %p\n", pkt);
printf("<= packet dispatch\n");
return NULL;
}
static void __session_plugin(struct session *sess)
void plugin_manager_free_ctx(void *ctx)
{
return;
}
void plugin_manager_dispatch(void *plugin_mgr, struct session *sess, const struct packet *pkt)
{
if (sess == NULL)
{
return;
}
printf("=> session dispatch: %p\n", sess);
printf("=> plugin dispatch session: %p\n", sess);
session_dump(sess);
printf("<= session dispatch\n");
// after session dispatch, we should reset session current packet and direction
session_set0_cur_pkt(sess, NULL);
session_set_cur_dir(sess, SESSION_DIR_NONE);
printf("<= plugin dispatch session\n");
}
/******************************************************************************
@@ -131,13 +123,21 @@ static inline void thread_set_name(const char *thd_symbol, uint16_t thd_idx)
static void *main_loop(void *arg)
{
struct packet *pkt = NULL;
struct session *sess;
struct session *evicted_sess;
struct session *expired_sess;
struct packet *pkt;
struct packet packets[RX_BURST_MAX];
struct thread_context *threads_ctx = (struct thread_context *)arg;
uint16_t thd_idx = threads_ctx->index;
struct packet_io *packet_io = stellar_ctx->packet_io;
struct session_manager *sess_mgr = threads_ctx->sess_mgr;
struct ip_reassembly *ip_mgr = threads_ctx->ip_mgr;
void *plug_mgr = NULL;
void *plug_mgr_ctx = NULL;
int nr_recv;
uint16_t thd_idx = threads_ctx->index;
uint64_t now_msec = 0;
if (packet_io_init(packet_io, thd_idx) != 0)
{
@@ -147,53 +147,94 @@ static void *main_loop(void *arg)
ATOMIC_SET(&threads_ctx->is_runing, 1);
thread_set_name("stellar", thd_idx);
STELLAR_LOG_DEBUG("worker thread %d runing", thd_idx);
STELLAR_LOG_STATE("worker thread %d runing", thd_idx);
while (ATOMIC_READ(&threads_ctx->need_exit) == 0)
{
// recv packet
if (packet_io_recv(packet_io, thd_idx, &pkt) != 0)
now_msec = timestamp_get_msec();
nr_recv = packet_io_ingress(packet_io, thd_idx, packets, RX_BURST_MAX);
if (nr_recv == 0)
{
goto poll_wait;
goto idle_tasks;
}
// call packet plugin
__packet_plugin(pkt);
// ip fragment reassemble
if (pkt->frag_layer)
for (int i = 0; i < nr_recv; i++)
{
struct packet *temp = ip_reassembly_packet(ip_mgr, pkt);
// forward the original fragment packet
packet_io_send(packet_io, thd_idx, pkt);
if (temp == NULL)
pkt = &packets[i];
// TODO
// call packet plugin
// ip fragment reassemble
if (pkt->frag_layer)
{
goto poll_wait;
struct packet *temp = ip_reassembly_packet(ip_mgr, pkt);
packet_io_egress(packet_io, thd_idx, pkt, 1); // forward the original fragment packet
if (temp == NULL)
{
continue;
}
else
{
pkt = temp;
}
}
sess = session_manager_lookup_session(sess_mgr, pkt);
if (sess == NULL)
{
sess = session_manager_new_session(sess_mgr, pkt);
if (sess == NULL)
{
// 1.Not TCP or UDP
// 2.UDP evict packet
// 3.UDP overloading and config to bypass new session
// 4.TCP no SYN flag
// 5.UDP overloading and config to bypass new session
goto fast_forward;
}
plug_mgr_ctx = plugin_manager_new_ctx();
session_set_user_data(sess, plug_mgr_ctx);
}
else
{
pkt = temp;
if (session_manager_update_session(sess_mgr, sess, pkt) == -1)
{
// TCP duplicate packet
goto fast_forward;
}
}
plugin_manager_dispatch(plug_mgr, sess, pkt);
fast_forward:
packet_io_egress(packet_io, thd_idx, pkt, 1);
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);
}
}
// update session
sess = session_manager_update_session(sess_mgr, pkt);
__session_plugin(sess);
idle_tasks:
expired_sess = session_manager_get_expired_session(sess_mgr);
if (expired_sess)
{
plug_mgr_ctx = session_get_user_data(expired_sess);
plugin_manager_free_ctx(plug_mgr_ctx);
session_manager_free_session(sess_mgr, expired_sess);
}
// get evicted session
sess = session_manager_get_evicted_session(sess_mgr);
__session_plugin(sess);
packet_io_send(packet_io, thd_idx, pkt);
poll_wait:
// get expired session
sess = session_manager_get_expired_session(sess_mgr);
__session_plugin(sess);
// TODO
// plugin_manager_cron();
// poll_non_packet_events();
// packet_io_yield();
}
ATOMIC_SET(&threads_ctx->is_runing, 0);
STELLAR_LOG_DEBUG("worker thread %d stop", thd_idx);
STELLAR_LOG_STATE("worker thread %d stop", thd_idx);
return NULL;
}