session manager: support session timeouts & tcp dupkt filter & udp eviction filter

This commit is contained in:
luwenpeng
2024-01-17 11:47:55 +08:00
parent 1d4736ac88
commit 4fbafab4e3
20 changed files with 972 additions and 607 deletions

View File

@@ -10,8 +10,6 @@
#include "packet.h"
#include "timestamp.h"
#include "session_manager.h"
#include "dupkt_filter.h"
#include "eviction_filter.h"
#ifndef STELLAR_LOG_ERROR
#define STELLAR_LOG_ERROR(format, ...) \
@@ -32,8 +30,6 @@ struct thread_ctx
uint64_t need_exit;
uint64_t is_runing;
struct session_manager *sess_mgr;
struct dupkt_filter *dupkt_filter;
struct eviction_filter *eviction_filter;
};
struct stellar_ctx
@@ -41,48 +37,16 @@ struct stellar_ctx
uint64_t need_exit;
uint16_t max_worker_num;
// session manager
uint64_t sess_mgr_max_session_num;
uint64_t sess_mgr_timeout_ms_toclsoing;
uint64_t sess_mgr_timeout_ms_toclosed;
struct session_manager_config sess_mgr_cfg;
// duplicated packet filter
uint8_t dupkt_filter_enable;
unsigned int dupkt_filter_capacity;
double dupkt_filter_error_rate;
int dupkt_filter_timeout_s;
// eviction filter
uint8_t eviction_filter_enable;
unsigned int eviction_filter_capacity;
double eviction_filter_error_rate;
int eviction_filter_timeout_s;
// thread
struct thread_ctx thread_ctx[128];
} g_stellar_ctx = {
.need_exit = 0,
.max_worker_num = 1,
// session manager
.sess_mgr_max_session_num = 1000000,
.sess_mgr_timeout_ms_toclsoing = 1000,
.sess_mgr_timeout_ms_toclosed = 10000,
// duplicated packet filter
.dupkt_filter_enable = 1,
.dupkt_filter_capacity = 1000000,
.dupkt_filter_error_rate = 0.0001,
.dupkt_filter_timeout_s = 10,
// eviction filter
.eviction_filter_enable = 1,
.eviction_filter_capacity = 1000000,
.eviction_filter_error_rate = 0.0001,
.eviction_filter_timeout_s = 10,
// TODO session manager config
};
// TODO
static int recv_packet(const char **data)
{
static unsigned char packet_data[] = {
@@ -96,6 +60,10 @@ static int recv_packet(const char **data)
return sizeof(packet_data);
}
static void send_packet(const char *data, uint16_t len)
{
}
static void signal_handler(int signo)
{
if (signo == SIGINT)
@@ -119,11 +87,6 @@ static void signal_handler(int signo)
static void __session_dispatch(struct session *sess)
{
if (sess == NULL)
{
return;
}
printf("\n");
printf("=> session dispatch: %p\n", sess);
session_dump(sess);
@@ -141,17 +104,9 @@ static void thread_ctx_init(struct stellar_ctx *ctx)
thd_ctx->index = i;
thd_ctx->need_exit = 0;
thd_ctx->is_runing = 0;
// session manager
thd_ctx->sess_mgr = session_manager_create(ctx->sess_mgr_max_session_num);
thd_ctx->sess_mgr = session_manager_create(&ctx->sess_mgr_cfg);
assert(thd_ctx->sess_mgr != NULL);
session_manager_set_timeout_toclosing(thd_ctx->sess_mgr, ctx->sess_mgr_timeout_ms_toclsoing);
session_manager_set_timeout_toclosed(thd_ctx->sess_mgr, ctx->sess_mgr_timeout_ms_toclosed);
// duplicated packet filter
thd_ctx->dupkt_filter = dupkt_filter_create(ctx->dupkt_filter_enable, ctx->dupkt_filter_capacity, ctx->dupkt_filter_error_rate, ctx->dupkt_filter_timeout_s);
assert(thd_ctx->dupkt_filter != NULL);
// eviction filter
thd_ctx->eviction_filter = eviction_filter_create(ctx->eviction_filter_enable, ctx->eviction_filter_capacity, ctx->eviction_filter_error_rate, ctx->eviction_filter_timeout_s);
assert(thd_ctx->eviction_filter != NULL);
}
}
@@ -175,7 +130,6 @@ static void *thread_cycle(void *arg)
struct session *sess = NULL;
struct thread_ctx *thd_ctx = (struct thread_ctx *)arg;
struct session_manager *sess_mgr = thd_ctx->sess_mgr;
struct dupkt_filter *dupkt_filter = thd_ctx->dupkt_filter;
char thread_name[16];
ATOMIC_SET(&thd_ctx->is_runing, 1);
@@ -185,60 +139,38 @@ static void *thread_cycle(void *arg)
while (ATOMIC_READ(&thd_ctx->need_exit) == 0)
{
// TODO recv packet
len = recv_packet(&data);
if (data == NULL)
{
goto poll_wait;
}
// parse packet
packet_parse(&pkt, data, len);
// duplicated packet filter
if (dupkt_filter_lookup(dupkt_filter, &pkt) == 0)
{
STELLAR_LOG_DEBUG("duplicated packet, forward it");
goto fast_forward;
}
// eviction filter
if (eviction_filter_lookup(thd_ctx->eviction_filter, &pkt) == 0)
{
STELLAR_LOG_DEBUG("eviction packet, forward it");
goto fast_forward;
}
// update session
sess = session_manager_update(sess_mgr, &pkt);
sess = session_manager_update_session(sess_mgr, &pkt);
if (sess == NULL)
{
goto fast_forward;
}
// TODO session synchronization
__session_dispatch(sess);
dupkt_filter_add(dupkt_filter, &pkt);
sess = session_manager_get_evicted_session(sess_mgr);
if (sess)
{
__session_dispatch(sess);
}
fast_forward:
// TODO send packet
send_packet(data, len);
// dispatch expire session
sess = session_manager_expire(sess_mgr);
if (sess && session_get_type(sess) == SESSION_TYPE_UDP && session_get_state(sess) == SESSION_STATE_CLOSING)
sess = session_manager_get_expired_session(sess_mgr);
if (sess)
{
const struct packet *sess_1st_pkt = session_get0_1st_pkt(sess);
eviction_filter_add(thd_ctx->eviction_filter, sess_1st_pkt);
__session_dispatch(sess);
}
__session_dispatch(sess);
continue;
sess = session_manager_evicte(sess_mgr);
if (sess && session_get_type(sess) == SESSION_TYPE_UDP)
{
const struct packet *sess_1st_pkt = session_get0_1st_pkt(sess);
eviction_filter_add(thd_ctx->eviction_filter, sess_1st_pkt);
}
__session_dispatch(sess);
// TODO get next timeout
sleep(1);
poll_wait:
sleep(session_manager_get_expire_interval(sess_mgr));
}
ATOMIC_SET(&thd_ctx->is_runing, 0);
@@ -255,18 +187,14 @@ int main(int argc, char **argv)
// TODO init plugin
// register signal handler
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
// update timestamp
timestamp_update();
// init thread context
thread_ctx_init(&g_stellar_ctx);
// create worker thread
for (uint16_t i = 0; i < g_stellar_ctx.max_worker_num; i++)
{
struct thread_ctx *thd_ctx = &g_stellar_ctx.thread_ctx[i];
@@ -277,14 +205,12 @@ int main(int argc, char **argv)
}
}
// master thread update timestamp
while (!g_stellar_ctx.need_exit)
{
timestamp_update();
sleep(1);
}
// wait worker thread exit
for (uint16_t i = 0; i < g_stellar_ctx.max_worker_num; i++)
{
struct thread_ctx *thd_ctx = &g_stellar_ctx.thread_ctx[i];