|
|
|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
#include "utils_internal.h"
|
|
|
|
|
#include "session_internal.h"
|
|
|
|
|
#include "session_manager.h"
|
|
|
|
|
#include "session_manager_log.h"
|
|
|
|
|
#include "session_manager_cfg.h"
|
|
|
|
|
#include "session_manager_rte.h"
|
|
|
|
|
@@ -12,89 +13,36 @@
|
|
|
|
|
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
|
|
|
|
|
struct session_manager_sche
|
|
|
|
|
{
|
|
|
|
|
int pkt_ex_id;
|
|
|
|
|
int sess_msg_id_tcp;
|
|
|
|
|
int sess_msg_id_udp;
|
|
|
|
|
int sess_msg_id_ctrl;
|
|
|
|
|
int sess_msg_id_stream;
|
|
|
|
|
|
|
|
|
|
struct mq_schema *mq_sche;
|
|
|
|
|
struct exdata_schema *ex_sche;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct session_manager
|
|
|
|
|
{
|
|
|
|
|
int pkt_ex_to_get_sess;
|
|
|
|
|
int pkt_ex_to_free_sess;
|
|
|
|
|
struct exdata_schema *ex_sche;
|
|
|
|
|
|
|
|
|
|
int stat_idx[SESS_MGR_STAT_MAX];
|
|
|
|
|
struct fieldstat_easy *fs;
|
|
|
|
|
struct session_manager_cfg *cfg;
|
|
|
|
|
struct session_manager_sche *sche;
|
|
|
|
|
struct session_manager_rte *rte[MAX_THREAD_NUM];
|
|
|
|
|
struct mq_runtime *mq[MAX_THREAD_NUM];
|
|
|
|
|
struct module_manager *mod_mgr;
|
|
|
|
|
struct packet_manager *pkt_mgr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
__thread int __thread_pkt_ex_to_get_sess = 0;
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* session manager sche
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void clean_closed_session(struct session_manager *sess_mgr, uint16_t thread_id, uint64_t now_ms)
|
|
|
|
|
static void free_session(int idx, void *ex_ptr, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = NULL;
|
|
|
|
|
struct session *cleaned[CLEAN_SESSION_BURST] = {NULL};
|
|
|
|
|
struct mq_runtime *mq_rte = sess_mgr->mq[thread_id];
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = sess_mgr->rte[thread_id];
|
|
|
|
|
struct session *sess = (struct session *)ex_ptr;
|
|
|
|
|
struct session_manager *sess_mgr = (struct session_manager *)arg;
|
|
|
|
|
assert(idx == sess_mgr->pkt_ex_to_free_sess);
|
|
|
|
|
|
|
|
|
|
uint64_t used = session_manager_rte_clean_session(sess_mgr_rte, now_ms, cleaned, CLEAN_SESSION_BURST);
|
|
|
|
|
for (uint64_t i = 0; i < used; i++)
|
|
|
|
|
if (sess && session_get_current_state(sess) == SESSION_STATE_CLOSED)
|
|
|
|
|
{
|
|
|
|
|
sess = cleaned[i];
|
|
|
|
|
assert(session_get_current_state(sess) == SESSION_STATE_CLOSED);
|
|
|
|
|
session_set_current_packet(sess, NULL);
|
|
|
|
|
session_set_flow_type(sess, FLOW_TYPE_NONE);
|
|
|
|
|
|
|
|
|
|
if (session_get_type(sess) == SESSION_TYPE_TCP)
|
|
|
|
|
{
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_stream, &sess->empty_seg);
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_ctrl, sess);
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_tcp, sess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_ctrl, sess);
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_udp, sess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_sess_msg_dispatch(int sess_msg_id, void *msg, on_msg_cb_func *msg_cb, void *msg_cb_args, void *dispatch_args)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = (struct session *)msg;
|
|
|
|
|
struct packet *pkt = (struct packet *)session_get_current_packet(sess);
|
|
|
|
|
enum session_state state = session_get_current_state(sess);
|
|
|
|
|
|
|
|
|
|
((on_session_message_callback *)(void *)msg_cb)(sess, state, pkt, msg_cb_args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_tcp_payload_msg_dispatch(int sess_msg_id, void *msg, on_msg_cb_func *msg_cb, void *msg_cb_args, void *dispatch_args)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_segment *seg = (struct tcp_segment *)msg;
|
|
|
|
|
struct session *sess = (struct session *)seg->user_data;
|
|
|
|
|
enum session_state state = session_get_current_state(sess);
|
|
|
|
|
|
|
|
|
|
((on_tcp_payload_callback *)(void *)msg_cb)(sess, state, seg->data, seg->len, msg_cb_args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_sess_msg_free(void *msg, void *args)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = (struct session *)msg;
|
|
|
|
|
|
|
|
|
|
if (session_get_current_state(sess) == SESSION_STATE_CLOSED)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = (struct session_manager *)args;
|
|
|
|
|
int thread_id = module_manager_get_thread_id(sess_mgr->mod_mgr);
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = sess_mgr->rte[thread_id];
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = session_manager_get_rte(sess_mgr, thread_id);
|
|
|
|
|
|
|
|
|
|
char buffer[4096] = {0};
|
|
|
|
|
session_to_str(sess, 0, buffer, sizeof(buffer));
|
|
|
|
|
@@ -106,20 +54,34 @@ static void on_sess_msg_free(void *msg, void *args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_tcp_payload_msg_free(void *msg, void *args)
|
|
|
|
|
static void notify_sess_closed_by_pseudo_pkt(struct session_manager *sess_mgr, int thread_id, struct session *sess)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_segment *seg = (struct tcp_segment *)msg;
|
|
|
|
|
struct session *sess = (struct session *)seg->user_data;
|
|
|
|
|
struct packet *pseudo = NULL;
|
|
|
|
|
struct packet_manager *pkt_mgr = sess_mgr->pkt_mgr;
|
|
|
|
|
assert(session_get_current_state(sess) == SESSION_STATE_CLOSED);
|
|
|
|
|
|
|
|
|
|
session_free_tcp_segment(sess, seg);
|
|
|
|
|
if (session_get_first_packet(sess, FLOW_TYPE_C2S))
|
|
|
|
|
{
|
|
|
|
|
pseudo = packet_manager_dup_packet(pkt_mgr, session_get_first_packet(sess, FLOW_TYPE_C2S));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pseudo = packet_manager_dup_packet(pkt_mgr, session_get_first_packet(sess, FLOW_TYPE_S2C));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(pseudo);
|
|
|
|
|
packet_set_type(pseudo, PACKET_TYPE_PSEUDO);
|
|
|
|
|
packet_set_action(pseudo, PACKET_ACTION_DROP);
|
|
|
|
|
packet_set_exdata(pseudo, sess_mgr->pkt_ex_to_free_sess, sess);
|
|
|
|
|
packet_manager_schedule_packet(pkt_mgr, thread_id, pseudo, PACKET_STAGE_FORWARD);
|
|
|
|
|
SESSION_MANAGER_LOG_INFO("notify session %lu %s closed by pseudo packet: %p", session_get_id(sess), session_get_readable_addr(sess), pseudo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_packet_forward(struct packet *pkt, enum packet_stage stage, void *args)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = (struct session_manager *)args;
|
|
|
|
|
int thread_id = module_manager_get_thread_id(sess_mgr->mod_mgr);
|
|
|
|
|
struct mq_runtime *mq_rte = sess_mgr->mq[thread_id];
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = sess_mgr->rte[thread_id];
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = session_manager_get_rte(sess_mgr, thread_id);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We use the system's real time instead of monotonic time for the following reasons:
|
|
|
|
|
@@ -132,86 +94,62 @@ static void on_packet_forward(struct packet *pkt, enum packet_stage stage, void
|
|
|
|
|
uint64_t now_ms = clock_get_real_time_ms();
|
|
|
|
|
|
|
|
|
|
struct tuple6 key;
|
|
|
|
|
struct tcp_segment *seg = NULL;
|
|
|
|
|
struct session *sess = session_manager_rte_lookup_session_by_packet(sess_mgr_rte, pkt);
|
|
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (packet_get_type(pkt) == PACKET_TYPE_PSEUDO)
|
|
|
|
|
if (packet_get_type(pkt) == PACKET_TYPE_RAW)
|
|
|
|
|
{
|
|
|
|
|
goto fast_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess = session_manager_rte_new_session(sess_mgr_rte, pkt, now_ms);
|
|
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto fast_path;
|
|
|
|
|
sess = session_manager_rte_new_session(sess_mgr_rte, pkt, now_ms);
|
|
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
session_set_user_data(sess, exdata_runtime_new(sess_mgr->ex_sche));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
session_set_user_data(sess, exdata_runtime_new(sess_mgr->sche->ex_sche));
|
|
|
|
|
goto slow_path;
|
|
|
|
|
// TODO new session by pseudo packet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (packet_get_type(pkt) == PACKET_TYPE_PSEUDO)
|
|
|
|
|
{
|
|
|
|
|
goto ctrl_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (session_manager_rte_update_session(sess_mgr_rte, sess, pkt, now_ms) == -1)
|
|
|
|
|
{
|
|
|
|
|
goto fast_path;
|
|
|
|
|
session_set_current_packet(sess, pkt);
|
|
|
|
|
packet_get_innermost_tuple6(pkt, &key);
|
|
|
|
|
if (tuple6_cmp(session_get_tuple6(sess), &key) == 0)
|
|
|
|
|
{
|
|
|
|
|
session_set_flow_type(sess, FLOW_TYPE_C2S);
|
|
|
|
|
sess->stats[FLOW_TYPE_C2S][STAT_PSEUDO_PACKETS_RECEIVED]++;
|
|
|
|
|
sess->stats[FLOW_TYPE_C2S][STAT_PSEUDO_BYTES_RECEIVED] += packet_get_raw_len(pkt);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
session_set_flow_type(sess, FLOW_TYPE_S2C);
|
|
|
|
|
sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_PACKETS_RECEIVED]++;
|
|
|
|
|
sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_BYTES_RECEIVED] += packet_get_raw_len(pkt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
goto slow_path;
|
|
|
|
|
session_manager_rte_update_session(sess_mgr_rte, sess, pkt, now_ms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctrl_path:
|
|
|
|
|
session_set_current_packet(sess, pkt);
|
|
|
|
|
packet_get_innermost_tuple6(pkt, &key);
|
|
|
|
|
if (tuple6_cmp(session_get_tuple6(sess), &key) == 0)
|
|
|
|
|
{
|
|
|
|
|
session_set_flow_type(sess, FLOW_TYPE_C2S);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
session_set_flow_type(sess, FLOW_TYPE_S2C);
|
|
|
|
|
}
|
|
|
|
|
packet_set_exdata(pkt, sess_mgr->sche->pkt_ex_id, sess);
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_ctrl, sess);
|
|
|
|
|
return;
|
|
|
|
|
packet_set_exdata(pkt, sess_mgr->pkt_ex_to_get_sess, sess);
|
|
|
|
|
|
|
|
|
|
slow_path:
|
|
|
|
|
if (session_get_type(sess) == SESSION_TYPE_TCP)
|
|
|
|
|
while ((sess = session_manager_rte_get_evicted_session(sess_mgr_rte)))
|
|
|
|
|
{
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_tcp, sess);
|
|
|
|
|
while ((seg = session_get_tcp_segment(sess)))
|
|
|
|
|
{
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_stream, seg);
|
|
|
|
|
}
|
|
|
|
|
notify_sess_closed_by_pseudo_pkt(sess_mgr, thread_id, sess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mq_runtime_publish_message(mq_rte, sess_mgr->sche->sess_msg_id_udp, sess);
|
|
|
|
|
}
|
|
|
|
|
packet_set_exdata(pkt, sess_mgr->sche->pkt_ex_id, sess);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fast_path:
|
|
|
|
|
packet_set_exdata(pkt, sess_mgr->sche->pkt_ex_id, NULL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_packet_output(struct packet *pkt, enum packet_stage stage, void *args)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = (struct session_manager *)args;
|
|
|
|
|
int thread_id = module_manager_get_thread_id(sess_mgr->mod_mgr);
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = sess_mgr->rte[thread_id];
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = session_manager_get_rte(sess_mgr, thread_id);
|
|
|
|
|
|
|
|
|
|
struct session *sess = (struct session *)packet_get_exdata(pkt, sess_mgr->sche->pkt_ex_id);
|
|
|
|
|
struct session *sess = (struct session *)packet_get_exdata(pkt, sess_mgr->pkt_ex_to_get_sess);
|
|
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
struct tuple6 key;
|
|
|
|
|
@@ -255,18 +193,35 @@ static void on_packet_output(struct packet *pkt, enum packet_stage stage, void *
|
|
|
|
|
|
|
|
|
|
static void on_polling(struct module_manager *mod_mgr, void *args)
|
|
|
|
|
{
|
|
|
|
|
struct session *sess = NULL;
|
|
|
|
|
uint64_t now_ms = clock_get_real_time_ms();
|
|
|
|
|
struct session_manager *sess_mgr = (struct session_manager *)args;
|
|
|
|
|
int thread_id = module_manager_get_thread_id(mod_mgr);
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = sess_mgr->rte[thread_id];
|
|
|
|
|
struct session_manager_stat *sess_mgr_stat = session_manager_rte_get_stat(sess_mgr_rte);
|
|
|
|
|
struct session_manager_rte *sess_mgr_rte = session_manager_get_rte(sess_mgr, thread_id);
|
|
|
|
|
|
|
|
|
|
clean_closed_session(sess_mgr, thread_id, now_ms);
|
|
|
|
|
static __thread uint64_t last_clean_expired_sess_ts = 0;
|
|
|
|
|
if (now_ms - last_clean_expired_sess_ts > sess_mgr->cfg->expire_period_ms)
|
|
|
|
|
{
|
|
|
|
|
for (uint64_t i = 0; i < sess_mgr->cfg->expire_batch_max; i++)
|
|
|
|
|
{
|
|
|
|
|
sess = session_manager_rte_get_expired_session(sess_mgr_rte, now_ms);
|
|
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
notify_sess_closed_by_pseudo_pkt(sess_mgr, thread_id, sess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
last_clean_expired_sess_ts = now_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __thread uint64_t last_sync_stat_ms = 0;
|
|
|
|
|
static __thread struct session_manager_stat last_stat = {0};
|
|
|
|
|
if (now_ms - last_sync_stat_ms >= SYNC_STAT_INTERVAL_MS)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager_stat *sess_mgr_stat = session_manager_rte_get_stat(sess_mgr_rte);
|
|
|
|
|
for (int i = 0; i < SESS_MGR_STAT_MAX; i++)
|
|
|
|
|
{
|
|
|
|
|
uint64_t val = session_manager_stat_get(sess_mgr_stat, i) - session_manager_stat_get(&last_stat, i);
|
|
|
|
|
@@ -277,100 +232,6 @@ static void on_polling(struct module_manager *mod_mgr, void *args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_manager_sche_free(struct session_manager_sche *sess_mgr_schema)
|
|
|
|
|
{
|
|
|
|
|
if (sess_mgr_schema)
|
|
|
|
|
{
|
|
|
|
|
if (sess_mgr_schema->mq_sche)
|
|
|
|
|
{
|
|
|
|
|
mq_schema_destroy_topic(sess_mgr_schema->mq_sche, sess_mgr_schema->sess_msg_id_tcp);
|
|
|
|
|
mq_schema_destroy_topic(sess_mgr_schema->mq_sche, sess_mgr_schema->sess_msg_id_udp);
|
|
|
|
|
mq_schema_destroy_topic(sess_mgr_schema->mq_sche, sess_mgr_schema->sess_msg_id_ctrl);
|
|
|
|
|
mq_schema_destroy_topic(sess_mgr_schema->mq_sche, sess_mgr_schema->sess_msg_id_stream);
|
|
|
|
|
}
|
|
|
|
|
exdata_schema_free(sess_mgr_schema->ex_sche);
|
|
|
|
|
|
|
|
|
|
free(sess_mgr_schema);
|
|
|
|
|
sess_mgr_schema = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct session_manager_sche *session_manager_sche_new(struct packet_manager *pkt_mgr, struct mq_schema *mq_sche, void *sess_mgr)
|
|
|
|
|
{
|
|
|
|
|
if (packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, on_packet_forward, sess_mgr))
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to subscribe PACKET_STAGE_FORWARD");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, on_packet_output, sess_mgr))
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to subscribe PACKET_STAGE_OUTPUT");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session_manager_sche *sess_mgr_schema = calloc(1, sizeof(struct session_manager_sche));
|
|
|
|
|
if (sess_mgr_schema == NULL)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to allocate memory for session_manager_sche");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr_schema->ex_sche = exdata_schema_new();
|
|
|
|
|
if (sess_mgr_schema->ex_sche == NULL)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create exdata_schema");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr_schema->mq_sche = mq_sche;
|
|
|
|
|
sess_mgr_schema->pkt_ex_id = packet_manager_new_packet_exdata_index(pkt_mgr, "session_manager", NULL, NULL);
|
|
|
|
|
if (sess_mgr_schema->pkt_ex_id == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create packet exdata index");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Publish session closed messages to multiple topics.
|
|
|
|
|
* Each topic has its own session message free callback.
|
|
|
|
|
* To prevent the same session from being freeed multiple times,
|
|
|
|
|
* only TCP/UDP topics register session message free callbacks,
|
|
|
|
|
* and other topics do not register session message callbacks;
|
|
|
|
|
*
|
|
|
|
|
* Restriction: MQ ensures that the session message free order is consistent with the publishing order
|
|
|
|
|
*/
|
|
|
|
|
sess_mgr_schema->sess_msg_id_tcp = mq_schema_create_topic(sess_mgr_schema->mq_sche, "SESSION_MESSAGE_TCP", &on_sess_msg_dispatch, NULL, &on_sess_msg_free, sess_mgr);
|
|
|
|
|
if (sess_mgr_schema->sess_msg_id_tcp == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create topic SESSION_MESSAGE_FREE");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
sess_mgr_schema->sess_msg_id_udp = mq_schema_create_topic(sess_mgr_schema->mq_sche, "SESSION_MESSAGE_UDP", &on_sess_msg_dispatch, NULL, &on_sess_msg_free, sess_mgr);
|
|
|
|
|
if (sess_mgr_schema->sess_msg_id_udp == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create topic SESSION_MESSAGE_UDP");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
sess_mgr_schema->sess_msg_id_ctrl = mq_schema_create_topic(sess_mgr_schema->mq_sche, "SESSION_MESSAGE_CTRL_PKT", &on_sess_msg_dispatch, NULL, NULL, NULL);
|
|
|
|
|
if (sess_mgr_schema->sess_msg_id_ctrl == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create topic SESSION_MESSAGE_CTRL_PKT");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
sess_mgr_schema->sess_msg_id_stream = mq_schema_create_topic(sess_mgr_schema->mq_sche, "SESSION_MESSAGE_TCP_STREAM", &on_tcp_payload_msg_dispatch, NULL, &on_tcp_payload_msg_free, sess_mgr);
|
|
|
|
|
if (sess_mgr_schema->sess_msg_id_stream == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create topic SESSION_MESSAGE_TCP_STREAM");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sess_mgr_schema;
|
|
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
session_manager_sche_free(sess_mgr_schema);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* session manager
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
@@ -379,9 +240,9 @@ void session_manager_free(struct session_manager *sess_mgr)
|
|
|
|
|
{
|
|
|
|
|
if (sess_mgr)
|
|
|
|
|
{
|
|
|
|
|
if (sess_mgr->sche)
|
|
|
|
|
if (sess_mgr->ex_sche)
|
|
|
|
|
{
|
|
|
|
|
session_manager_sche_free(sess_mgr->sche);
|
|
|
|
|
exdata_schema_free(sess_mgr->ex_sche);
|
|
|
|
|
}
|
|
|
|
|
if (sess_mgr->fs)
|
|
|
|
|
{
|
|
|
|
|
@@ -395,7 +256,7 @@ void session_manager_free(struct session_manager *sess_mgr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct session_manager *session_manager_new(struct packet_manager *pkt_mgr, struct mq_schema *mq_schema, const char *toml_file)
|
|
|
|
|
static struct session_manager *session_manager_new(struct packet_manager *pkt_mgr, const char *toml_file)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = calloc(1, sizeof(struct session_manager));
|
|
|
|
|
if (sess_mgr == NULL)
|
|
|
|
|
@@ -422,18 +283,45 @@ static struct session_manager *session_manager_new(struct packet_manager *pkt_mg
|
|
|
|
|
{
|
|
|
|
|
sess_mgr->stat_idx[i] = fieldstat_easy_register_counter(sess_mgr->fs, sess_mgr_stat_str[i]);
|
|
|
|
|
}
|
|
|
|
|
if (fieldstat_easy_enable_auto_output(sess_mgr->fs, "session_manager.fs4", 2) != 0)
|
|
|
|
|
if (fieldstat_easy_enable_auto_output(sess_mgr->fs, "metrics/session_manager.json", 2) != 0)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to enable auto output");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr->sche = session_manager_sche_new(pkt_mgr, mq_schema, sess_mgr);
|
|
|
|
|
if (sess_mgr->sche == NULL)
|
|
|
|
|
if (packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, on_packet_forward, sess_mgr))
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to subscribe PACKET_STAGE_FORWARD");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
if (packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, on_packet_output, sess_mgr))
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to subscribe PACKET_STAGE_OUTPUT");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr->ex_sche = exdata_schema_new();
|
|
|
|
|
if (sess_mgr->ex_sche == NULL)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create exdata_schema");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr->pkt_ex_to_get_sess = packet_manager_new_packet_exdata_index(pkt_mgr, "pkt_ex_key_for_get_sess", NULL, NULL);
|
|
|
|
|
if (sess_mgr->pkt_ex_to_get_sess == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create packet exdata index");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr->pkt_ex_to_free_sess = packet_manager_new_packet_exdata_index(pkt_mgr, "pkt_ex_key_for_free_sess", free_session, sess_mgr);
|
|
|
|
|
if (sess_mgr->pkt_ex_to_free_sess == -1)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to create packet exdata index");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess_mgr->pkt_mgr = pkt_mgr;
|
|
|
|
|
return sess_mgr;
|
|
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
@@ -446,44 +334,15 @@ int session_manager_new_session_exdata_index(struct session_manager *sess_mgr, c
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(name);
|
|
|
|
|
assert(func);
|
|
|
|
|
return exdata_schema_new_index(sess_mgr->sche->ex_sche, name, func, arg);
|
|
|
|
|
return exdata_schema_new_index(sess_mgr->ex_sche, name, func, arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int session_manager_subscribe_tcp(struct session_manager *sess_mgr, on_session_message_callback *cb, void *args)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(cb);
|
|
|
|
|
return mq_schema_subscribe(sess_mgr->sche->mq_sche, sess_mgr->sche->sess_msg_id_tcp, (on_msg_cb_func *)(void *)cb, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int session_manager_subscribe_udp(struct session_manager *sess_mgr, on_session_message_callback *cb, void *args)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(cb);
|
|
|
|
|
return mq_schema_subscribe(sess_mgr->sche->mq_sche, sess_mgr->sche->sess_msg_id_udp, (on_msg_cb_func *)(void *)cb, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int session_manager_subscribe_control_packet(struct session_manager *sess_mgr, on_session_message_callback *cb, void *args)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(cb);
|
|
|
|
|
return mq_schema_subscribe(sess_mgr->sche->mq_sche, sess_mgr->sche->sess_msg_id_ctrl, (on_msg_cb_func *)(void *)cb, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int session_manager_subscribe_tcp_stream(struct session_manager *sess_mgr, on_tcp_payload_callback *cb, void *args)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(cb);
|
|
|
|
|
return mq_schema_subscribe(sess_mgr->sche->mq_sche, sess_mgr->sche->sess_msg_id_stream, (on_msg_cb_func *)(void *)cb, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int session_manager_init(struct session_manager *sess_mgr, uint16_t thread_id, struct mq_runtime *mq_rte)
|
|
|
|
|
int session_manager_init(struct session_manager *sess_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
uint64_t now_ms = clock_get_real_time_ms();
|
|
|
|
|
|
|
|
|
|
sess_mgr->cfg->session_id_seed = sess_mgr->cfg->instance_id << 8 | thread_id;
|
|
|
|
|
sess_mgr->mq[thread_id] = mq_rte;
|
|
|
|
|
sess_mgr->rte[thread_id] = session_manager_rte_new(sess_mgr->cfg, now_ms);
|
|
|
|
|
if (sess_mgr->rte[thread_id] == NULL)
|
|
|
|
|
{
|
|
|
|
|
@@ -500,22 +359,26 @@ void session_manager_clean(struct session_manager *sess_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
|
|
|
|
|
struct mq_runtime *mq_rte = sess_mgr->mq[thread_id];
|
|
|
|
|
if (sess_mgr->rte[thread_id] == NULL)
|
|
|
|
|
struct session_manager_rte *rte = session_manager_get_rte(sess_mgr, thread_id);
|
|
|
|
|
|
|
|
|
|
if (rte == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session_manager_stat *stat = session_manager_rte_get_stat(sess_mgr->rte[thread_id]);
|
|
|
|
|
struct session *sess = NULL;
|
|
|
|
|
struct session_manager_stat *stat = session_manager_rte_get_stat(rte);
|
|
|
|
|
|
|
|
|
|
while (stat->tcp_sess_used || stat->udp_sess_used)
|
|
|
|
|
{
|
|
|
|
|
clean_closed_session(sess_mgr, thread_id, UINT64_MAX);
|
|
|
|
|
// here we need to dispatch the message to ensure that the session is cleaned up
|
|
|
|
|
mq_runtime_dispatch(mq_rte);
|
|
|
|
|
while ((sess = session_manager_rte_get_expired_session(rte, UINT64_MAX)))
|
|
|
|
|
{
|
|
|
|
|
exdata_runtime_free((struct exdata_runtime *)session_get_user_data(sess));
|
|
|
|
|
session_manager_rte_free_session(rte, sess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session_manager_rte_free(sess_mgr->rte[thread_id]);
|
|
|
|
|
sess_mgr->rte[thread_id] = NULL;
|
|
|
|
|
session_manager_rte_free(rte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
@@ -535,12 +398,10 @@ struct module *session_manager_on_init(struct module_manager *mod_mgr)
|
|
|
|
|
struct module *pkt_mgr_mod = module_manager_get_module(mod_mgr, PACKET_MANAGER_MODULE_NAME);
|
|
|
|
|
struct packet_manager *pkt_mgr = module_to_packet_manager(pkt_mgr_mod);
|
|
|
|
|
assert(pkt_mgr);
|
|
|
|
|
struct mq_schema *mq_sche = module_manager_get_mq_schema(mod_mgr);
|
|
|
|
|
assert(mq_sche);
|
|
|
|
|
const char *toml_file = module_manager_get_toml_path(mod_mgr);
|
|
|
|
|
assert(toml_file);
|
|
|
|
|
|
|
|
|
|
struct session_manager *sess_mgr = session_manager_new(pkt_mgr, mq_sche, toml_file);
|
|
|
|
|
struct session_manager *sess_mgr = session_manager_new(pkt_mgr, toml_file);
|
|
|
|
|
if (sess_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
@@ -575,10 +436,9 @@ struct module *session_manager_on_thread_init(struct module_manager *mod_mgr, in
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = module_get_ctx(mod);
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
struct mq_runtime *mq_rte = module_manager_get_mq_runtime(mod_mgr);
|
|
|
|
|
assert(mq_rte);
|
|
|
|
|
|
|
|
|
|
if (session_manager_init(sess_mgr, thread_id, mq_rte) != 0)
|
|
|
|
|
__thread_pkt_ex_to_get_sess = sess_mgr->pkt_ex_to_get_sess;
|
|
|
|
|
if (session_manager_init(sess_mgr, thread_id) != 0)
|
|
|
|
|
{
|
|
|
|
|
SESSION_MANAGER_LOG_ERROR("failed to int session_manager_init");
|
|
|
|
|
return NULL;
|
|
|
|
|
@@ -592,10 +452,11 @@ struct module *session_manager_on_thread_init(struct module_manager *mod_mgr, in
|
|
|
|
|
void session_manager_on_thread_exit(struct module_manager *mod_mgr, int thread_id, struct module *mod)
|
|
|
|
|
{
|
|
|
|
|
struct session_manager *sess_mgr = module_get_ctx(mod);
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
assert(thread_id < (int)sess_mgr->cfg->thread_num);
|
|
|
|
|
|
|
|
|
|
session_manager_clean(sess_mgr, thread_id);
|
|
|
|
|
if (sess_mgr)
|
|
|
|
|
{
|
|
|
|
|
assert(thread_id < (int)sess_mgr->cfg->thread_num);
|
|
|
|
|
session_manager_clean(sess_mgr, thread_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session_manager_rte *session_manager_get_rte(struct session_manager *sess_mgr, uint16_t thread_id)
|
|
|
|
|
@@ -609,4 +470,9 @@ struct session_manager_cfg *session_manager_get_cfg(struct session_manager *sess
|
|
|
|
|
{
|
|
|
|
|
assert(sess_mgr);
|
|
|
|
|
return sess_mgr->cfg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session *packet_exdata_to_session(struct packet *pkt)
|
|
|
|
|
{
|
|
|
|
|
return (struct session *)packet_get_exdata(pkt, __thread_pkt_ex_to_get_sess);
|
|
|
|
|
}
|