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

@@ -22,7 +22,6 @@ struct session_manager
struct session_table *udp_sess_table;
struct session_timer *sess_timer;
struct session_queue *sess_evicted_queue;
struct session_queue *sess_toclosed_queue;
struct dupkt_filter *tcp_dupkt_filter;
struct eviction_filter *udp_eviction_filter;
@@ -87,7 +86,6 @@ static inline void session_manager_update_udp_to_closing(struct session_manager
static inline void session_manager_update_tcp_to_opening(struct session_manager *mgr, struct session *sess, int opening_by_syn);
static inline void session_manager_update_tcp_to_active(struct session_manager *mgr, struct session *sess);
static inline void session_manager_update_tcp_to_closing(struct session_manager *mgr, struct session *sess, int enable_time_wait);
static inline void session_manager_update_session_to_closed(struct session_manager *mgr, struct session *sess);
static inline void session_manager_handle_tcp_on_opening(struct session_manager *mgr, struct session *sess, enum tcp_state tcp_old_state, enum tcp_state tcp_curr_state);
static inline void session_manager_handle_tcp_on_active(struct session_manager *mgr, struct session *sess, enum tcp_state tcp_old_state, enum tcp_state tcp_curr_state);
@@ -95,11 +93,10 @@ static inline void session_manager_handle_tcp_on_closing(struct session_manager
static inline struct session *session_manager_new_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key);
static inline struct session *session_manager_new_udp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key);
static inline struct session *session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key);
static inline struct session *session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key);
static inline int session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt);
static inline int session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt);
static inline void session_manager_free_session(struct session_manager *mgr, struct session *sess);
static inline void session_manager_recycle_session(struct session_manager *mgr);
void session_manager_free_session(struct session_manager *mgr, struct session *sess);
static inline void session_manager_evicte_session(struct session_manager *mgr, struct session *sess);
/******************************************************************************
@@ -646,7 +643,6 @@ static inline void session_manager_update_udp_to_closing(struct session_manager
{
session_manager_update_session_state(mgr, sess, SESSION_STATE_CLOSING);
session_timer_del_session(mgr->sess_timer, sess);
session_queue_push(mgr->sess_toclosed_queue, sess);
eviction_filter_add(mgr->udp_eviction_filter, session_get0_1st_pkt(sess));
}
@@ -682,12 +678,6 @@ static inline void session_manager_update_tcp_to_closing(struct session_manager
}
}
static inline void session_manager_update_session_to_closed(struct session_manager *mgr, struct session *sess)
{
session_manager_update_session_state(mgr, sess, SESSION_STATE_CLOSED);
session_timer_del_session(mgr->sess_timer, sess);
}
// opening -> opening
// opening -> active
// opening -> closing
@@ -907,21 +897,29 @@ static inline struct session *session_manager_new_udp_session(struct session_man
return sess;
}
static inline struct session *session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key)
static inline int session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt)
{
struct tuple6 key;
memset(&key, 0, sizeof(struct tuple6));
if (packet_get_innermost_tuple6(pkt, &key) == -1)
{
mgr->npkts_miss_l4_proto++;
return -1;
}
const struct layer_record *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
if (tcp_layer == NULL)
{
mgr->npkts_miss_l4_proto++;
return NULL;
return -1;
}
enum session_dir curr_dir = judge_direction_by_session(sess, key);
enum session_dir curr_dir = judge_direction_by_session(sess, &key);
if (session_manager_update_tcp_filter(mgr, sess, pkt, curr_dir))
{
mgr->npkts_hit_tcp_dupkt++;
session_set_dup_traffic_flag(sess, DUP_TRAFFIC_YES);
return NULL;
return -1;
}
enum session_state sess_state = session_get_state(sess);
@@ -948,12 +946,19 @@ static inline struct session *session_manager_update_tcp_session(struct session_
break;
}
return sess;
return 0;
}
static inline struct session *session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key)
static inline int session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt)
{
enum session_dir curr_dir = judge_direction_by_session(sess, key);
struct tuple6 key;
memset(&key, 0, sizeof(struct tuple6));
if (packet_get_innermost_tuple6(pkt, &key) == -1)
{
return -1;
}
enum session_dir curr_dir = judge_direction_by_session(sess, &key);
session_manager_update_session_packet(mgr, sess, pkt, curr_dir);
session_update_udp_state(sess, NULL, curr_dir);
enum session_state sess_state = session_get_state(sess);
@@ -965,7 +970,7 @@ static inline struct session *session_manager_update_udp_session(struct session_
break;
case SESSION_STATE_ACTIVE:
session_manager_update_udp_to_active(mgr, sess);
return sess;
break;
case SESSION_STATE_CLOSING:
assert(0);
break;
@@ -974,42 +979,7 @@ static inline struct session *session_manager_update_udp_session(struct session_
break;
}
return sess;
}
static inline void session_manager_free_session(struct session_manager *mgr, struct session *sess)
{
if (sess)
{
SESSION_LOG_DEBUG("%s, session %lu closing -> closed", session_closing_reason_to_str(session_get_closing_reason(sess)), session_get_id(sess));
session_manager_update_session_to_closed(mgr, sess);
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
session_table_del_session(mgr->tcp_sess_table, session_get0_key(sess));
}
if (session_get_type(sess) == SESSION_TYPE_UDP)
{
session_table_del_session(mgr->udp_sess_table, session_get0_key(sess));
}
session_set0_cur_pkt(sess, NULL);
session_set_cur_dir(sess, SESSION_DIR_NONE);
session_free(sess);
session_pool_free(mgr->sess_pool, sess);
sess = NULL;
}
}
static inline void session_manager_recycle_session(struct session_manager *mgr)
{
while (1)
{
struct session *sess = session_queue_pop(mgr->sess_toclosed_queue);
if (sess == NULL)
{
break;
}
session_manager_free_session(mgr, sess);
}
return 0;
}
static inline void session_manager_evicte_session(struct session_manager *mgr, struct session *sess)
@@ -1073,12 +1043,6 @@ struct session_manager *session_manager_new(struct session_manager_options *opts
goto error;
}
mgr->sess_toclosed_queue = session_queue_new();
if (mgr->sess_toclosed_queue == NULL)
{
goto error;
}
mgr->tcp_dupkt_filter = dupkt_filter_new(mgr->opts.tcp_dupkt_filter_enable, mgr->opts.tcp_dupkt_filter_capacity, mgr->opts.tcp_dupkt_filter_error_rate, mgr->opts.tcp_dupkt_filter_timeout);
if (mgr->tcp_dupkt_filter == NULL)
{
@@ -1104,14 +1068,9 @@ void session_manager_free(struct session_manager *mgr)
if (mgr)
{
// move all evicted session to closed queue
while (mgr->sess_evicted_queue && session_manager_get_evicted_session(mgr))
while (mgr->sess_evicted_queue && (sess = session_manager_get_evicted_session(mgr)))
{
}
// free all closed queue
if (mgr->sess_toclosed_queue)
{
session_manager_recycle_session(mgr);
session_manager_free_session(mgr, sess);
}
// free all udp session which is not in closed state
@@ -1128,7 +1087,6 @@ void session_manager_free(struct session_manager *mgr)
eviction_filter_free(mgr->udp_eviction_filter);
dupkt_filter_free(mgr->tcp_dupkt_filter);
session_queue_free(mgr->sess_toclosed_queue);
session_queue_free(mgr->sess_evicted_queue);
session_timer_free(mgr->sess_timer);
session_table_free(mgr->udp_sess_table);
@@ -1140,7 +1098,7 @@ void session_manager_free(struct session_manager *mgr)
}
// only use the packet six-tuple to find the session, not update it
struct session *session_manager_lookup_sesssion(struct session_manager *mgr, const struct packet *pkt)
struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt)
{
struct tuple6 key;
memset(&key, 0, sizeof(struct tuple6));
@@ -1164,20 +1122,15 @@ struct session *session_manager_lookup_sesssion(struct session_manager *mgr, con
}
/*
* Return NULL in the following cases:
* 1.not a TCP or UDP packet
* 2.TCP packet miss session but no syn packet seen
* 3.TCP duplicate packet
* 4.TCP discards packets
* 5.UDP evict packet
* pakcet will not update the session and needs to be fast forwarded
* return NULL following case:
* 1.Not TCP or UDP
* 2.UDP eviction packet
* 3.UDP overloading and config to bypass new session
* 4.TCP no SYN flag
* 5.UDP overloading and config to bypass new session
*/
struct session *session_manager_update_session(struct session_manager *mgr, const struct packet *pkt)
struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt)
{
assert(session_manager_get_evicted_session(mgr) == NULL);
session_manager_recycle_session(mgr);
struct tuple6 key;
memset(&key, 0, sizeof(struct tuple6));
if (packet_get_innermost_tuple6(pkt, &key))
@@ -1186,30 +1139,13 @@ struct session *session_manager_update_session(struct session_manager *mgr, cons
return NULL;
}
struct session *sess = NULL;
if (key.ip_proto == IPPROTO_UDP)
{
sess = session_table_find_session(mgr->udp_sess_table, &key);
if (sess)
{
return session_manager_update_udp_session(mgr, sess, pkt, &key);
}
else
{
return session_manager_new_udp_session(mgr, pkt, &key);
}
return session_manager_new_udp_session(mgr, pkt, &key);
}
else if (key.ip_proto == IPPROTO_TCP)
{
sess = session_table_find_session(mgr->tcp_sess_table, &key);
if (sess)
{
return session_manager_update_tcp_session(mgr, sess, pkt, &key);
}
else
{
return session_manager_new_tcp_session(mgr, pkt, &key);
}
return session_manager_new_tcp_session(mgr, pkt, &key);
}
else
{
@@ -1217,40 +1153,72 @@ struct session *session_manager_update_session(struct session_manager *mgr, cons
}
}
void session_manager_free_session(struct session_manager *mgr, struct session *sess)
{
if (sess)
{
SESSION_LOG_DEBUG("%s, session %lu closing -> closed", session_closing_reason_to_str(session_get_closing_reason(sess)), session_get_id(sess));
session_manager_update_session_state(mgr, sess, SESSION_STATE_CLOSED);
session_timer_del_session(mgr->sess_timer, sess);
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
session_table_del_session(mgr->tcp_sess_table, session_get0_key(sess));
}
if (session_get_type(sess) == SESSION_TYPE_UDP)
{
session_table_del_session(mgr->udp_sess_table, session_get0_key(sess));
}
session_set0_cur_pkt(sess, NULL);
session_set_cur_dir(sess, SESSION_DIR_NONE);
session_free(sess);
session_pool_free(mgr->sess_pool, sess);
sess = NULL;
}
}
/*
* return NULL following case:
* 1.Not TCP or UDP
* 2.TCP duplicate packet
*/
int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt)
{
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
return session_manager_update_tcp_session(mgr, sess, pkt);
}
else if (session_get_type(sess) == SESSION_TYPE_UDP)
{
return session_manager_update_udp_session(mgr, sess, pkt);
}
else
{
return -1;
}
}
// return session need free by session_manager_free_session()
struct session *session_manager_get_expired_session(struct session_manager *mgr)
{
session_manager_recycle_session(mgr);
struct session *sess = session_timer_expire_session(mgr->sess_timer, timestamp_get_sec());
if (sess)
{
session_run_expirecb(sess);
if (session_get_state(sess) == SESSION_STATE_CLOSED)
if (session_get_state(sess) == SESSION_STATE_CLOSING)
{
return NULL;
}
if (session_get_type(sess) == SESSION_TYPE_UDP)
{
session_queue_push(mgr->sess_toclosed_queue, sess);
return sess;
}
}
return sess;
return NULL;
}
// return session need free by session_manager_free_session()
struct session *session_manager_get_evicted_session(struct session_manager *mgr)
{
session_manager_recycle_session(mgr);
struct session *sess = session_queue_pop(mgr->sess_evicted_queue);
if (sess)
{
session_queue_push(mgr->sess_toclosed_queue, sess);
}
return sess;
return session_queue_pop(mgr->sess_evicted_queue);
}
uint64_t session_manager_get_expire_interval(struct session_manager *mgr)