diff --git a/include/stellar/session.h b/include/stellar/session.h index d62086d..3a74029 100644 --- a/include/stellar/session.h +++ b/include/stellar/session.h @@ -113,7 +113,7 @@ enum session_stat MAX_STAT, }; -// realtime in seconds +// realtime in milliseconds enum session_timestamp { SESSION_TIMESTAMP_START, diff --git a/src/id_generator/id_generator.cpp b/src/id_generator/id_generator.cpp index 0237a64..ec6e245 100644 --- a/src/id_generator/id_generator.cpp +++ b/src/id_generator/id_generator.cpp @@ -3,7 +3,6 @@ #include "log.h" #include "macro.h" -#include "times.h" #include "stellar_core.h" #include "id_generator.h" @@ -18,7 +17,7 @@ struct id_generator uint64_t thread_volatile[MAX_THREAD_NUM]; }; -struct id_generator global_id_generator; +struct id_generator global_id_generator = {0}; // return 0: success // return -1: failed @@ -62,7 +61,7 @@ int id_generator_init(const struct id_generator_options *opts) * | 1bit | 12bit device_id | 8bit thread_id | 28bit timestamp in sec | 15bit sequence per thread | * +------+------------------+----------------+------------------------+---------------------------+ */ -uint64_t id_generator_alloc() +uint64_t id_generator_alloc(uint64_t now_sec) { #define MAX_ID_PER_THREAD (32768) #define MAX_ID_BASE_TIME (268435456L) @@ -71,7 +70,7 @@ uint64_t id_generator_alloc() uint64_t global_id = 0; uint64_t id_per_thread = (global_id_generator.thread_volatile[thr_idx]++) % MAX_ID_PER_THREAD; - uint64_t id_base_time = stellar_get_real_time_sec() % MAX_ID_BASE_TIME; + uint64_t id_base_time = now_sec % MAX_ID_BASE_TIME; global_id = (global_id_generator.device_id << 51) | (thr_idx << 43) | (id_base_time << 15) | diff --git a/src/id_generator/id_generator.h b/src/id_generator/id_generator.h index 2627860..b3a2d60 100644 --- a/src/id_generator/id_generator.h +++ b/src/id_generator/id_generator.h @@ -16,7 +16,7 @@ struct id_generator_options // return 0: success // return -1: failed int id_generator_init(const struct id_generator_options *opts); -uint64_t id_generator_alloc(); +uint64_t id_generator_alloc(uint64_t now_sec); #ifdef __cplusplus } diff --git a/src/session/session_manager.cpp b/src/session/session_manager.cpp index 8a62712..8694b84 100644 --- a/src/session/session_manager.cpp +++ b/src/session/session_manager.cpp @@ -4,7 +4,6 @@ #include "log.h" #include "macro.h" -#include "times.h" #include "tcp_utils.h" #include "udp_utils.h" #include "packet_layer.h" @@ -36,6 +35,12 @@ struct session_manager struct session_manager_stat stat; struct session_manager_options opts; + + /* + * only used for session_set_discard() or session_manager record_duplicated_packet(), + * because the function is called by pluin and has no time input. + */ + uint64_t now_ms; }; #define EVICTE_SESSION_BURST (RX_BURST_MAX) @@ -286,7 +291,7 @@ static int tcp_init(struct session_manager *mgr, struct session *sess) return 0; } -static void tcp_update(struct session_manager *mgr, struct session *sess, enum flow_direction dir, const struct raw_layer *tcp_layer, uint64_t now) +static void tcp_update(struct session_manager *mgr, struct session *sess, enum flow_direction dir, const struct raw_layer *tcp_layer) { struct tcp_segment *seg; struct tcphdr *hdr = (struct tcphdr *)tcp_layer->hdr_ptr; @@ -329,7 +334,7 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f tcp_reassembly_set_recv_next(half->assembler, len ? half->seq : half->seq + 1); } - seg = tcp_reassembly_expire(half->assembler, now); + seg = tcp_reassembly_expire(half->assembler, mgr->now_ms); if (seg) { session_inc_stat(sess, dir, STAT_TCP_SEGMENTS_EXPIRED, 1); @@ -371,7 +376,7 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f } else if ((seg = tcp_segment_new(half->seq, tcp_layer->pld_ptr, len))) { - switch (tcp_reassembly_push(half->assembler, seg, now)) + switch (tcp_reassembly_push(half->assembler, seg, mgr->now_ms)) { case -2: session_inc_stat(sess, dir, STAT_TCP_SEGMENTS_RETRANSMIT, 1); @@ -452,7 +457,7 @@ static enum flow_direction identify_direction_by_history(const struct session *s ******************************************************************************/ // on new session -static int tcp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key, uint64_t now) +static int tcp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key) { if (key->ip_proto == IPPROTO_TCP && mgr->stat.curr_nr_tcp_sess_used >= mgr->opts.max_tcp_session_num) { @@ -461,7 +466,8 @@ static int tcp_overload_bypass(struct session_manager *mgr, const struct tuple6 } return 0; } -static int udp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key, uint64_t now) + +static int udp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key) { if (key->ip_proto == IPPROTO_UDP && mgr->stat.curr_nr_udp_sess_used >= mgr->opts.max_udp_session_num) { @@ -470,9 +476,10 @@ static int udp_overload_bypass(struct session_manager *mgr, const struct tuple6 } return 0; } -static int evicted_session_bypass(struct session_manager *mgr, const struct tuple6 *key, uint64_t now) + +static int evicted_session_bypass(struct session_manager *mgr, const struct tuple6 *key) { - if (mgr->opts.evicted_session_filter_enable && evicted_session_filter_lookup(mgr->evicte_sess_filter, key, now)) + if (mgr->opts.evicted_session_filter_enable && evicted_session_filter_lookup(mgr->evicte_sess_filter, key, mgr->now_ms)) { mgr->stat.nr_udp_pkts_evctd_bypass++; return 1; @@ -480,8 +487,9 @@ static int evicted_session_bypass(struct session_manager *mgr, const struct tupl return 0; } + // on update session -static int duplicated_packet_bypass(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static int duplicated_packet_bypass(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key) { if (mgr->opts.duplicated_packet_filter_enable == 0) { @@ -491,7 +499,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session enum flow_direction dir = identify_direction_by_history(sess, key); if (session_get_stat(sess, dir, STAT_RAW_PACKETS_RECEIVED) < 3 || session_has_duplicate_traffic(sess)) { - if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, now)) + if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, mgr->now_ms)) { session_inc_stat(sess, dir, STAT_DUPLICATE_PACKETS_BYPASS, 1); session_inc_stat(sess, dir, STAT_DUPLICATE_BYTES_BYPASS, packet_get_raw_len(pkt)); @@ -515,7 +523,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session } else { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, now); + duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); return 0; } } @@ -527,12 +535,11 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session * Session Manager ******************************************************************************/ -static void session_update(struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum flow_direction dir) +static void session_update(struct session_manager *mgr, struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum flow_direction dir) { - uint64_t real_sec = stellar_get_real_time_sec(); if (session_get_current_state(sess) == SESSION_STATE_INIT) { - session_set_id(sess, id_generator_alloc()); + session_set_id(sess, id_generator_alloc(mgr->now_ms / 1000)); session_set_tuple6(sess, key); session_set_tuple_direction(sess, dir); @@ -562,7 +569,7 @@ static void session_update(struct session *sess, enum session_state next_state, } tuple6_to_str(key, sess->tuple_str, sizeof(sess->tuple_str)); - session_set_timestamp(sess, SESSION_TIMESTAMP_START, real_sec); + session_set_timestamp(sess, SESSION_TIMESTAMP_START, mgr->now_ms); switch (key->ip_proto) { case IPPROTO_TCP: @@ -589,11 +596,11 @@ static void session_update(struct session *sess, enum session_state next_state, session_set_current_packet(sess, pkt); session_set_current_flow_direction(sess, dir); - session_set_timestamp(sess, SESSION_TIMESTAMP_LAST, real_sec); + session_set_timestamp(sess, SESSION_TIMESTAMP_LAST, mgr->now_ms); session_set_current_state(sess, next_state); } -static void session_manager_evicte_session(struct session_manager *mgr, struct session *sess, uint64_t now, int reason) +static void session_manager_evicte_session(struct session_manager *mgr, struct session *sess, int reason) { if (sess == NULL) { @@ -632,7 +639,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s session_table_del(mgr->udp_sess_table, sess); if (mgr->opts.evicted_session_filter_enable) { - evicted_session_filter_add(mgr->evicte_sess_filter, session_get_tuple6(sess), now); + evicted_session_filter_add(mgr->evicte_sess_filter, session_get_tuple6(sess), mgr->now_ms); } SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); mgr->stat.nr_udp_sess_evicted++; @@ -643,7 +650,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s } } -static struct session *session_manager_lookup_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static struct session *session_manager_lookup_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key) { struct session *sess = session_table_find_tuple6(mgr->tcp_sess_table, key); if (sess == NULL) @@ -665,7 +672,7 @@ static struct session *session_manager_lookup_tcp_session(struct session_manager ((half->history & TH_FIN) || (half->history & TH_RST))) // recv SYN after FIN or RST { // TCP port reuse, evict old session - session_manager_evicte_session(mgr, sess, now, PORT_REUSE_EVICT); + session_manager_evicte_session(mgr, sess, PORT_REUSE_EVICT); return NULL; } else @@ -675,7 +682,7 @@ static struct session *session_manager_lookup_tcp_session(struct session_manager } } -static struct session *session_manager_new_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static struct session *session_manager_new_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key) { const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(pkt, LAYER_PROTO_TCP); const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr; @@ -690,7 +697,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m if (mgr->opts.tcp_overload_evict_old_sess && mgr->stat.curr_nr_tcp_sess_used >= mgr->opts.max_tcp_session_num - EVICTE_SESSION_BURST) { struct session *evic_sess = session_table_find_lru(mgr->tcp_sess_table); - session_manager_evicte_session(mgr, evic_sess, now, LRU_EVICT); + session_manager_evicte_session(mgr, evic_sess, LRU_EVICT); } enum flow_direction dir = (flags & TH_ACK) ? FLOW_DIRECTION_S2C : FLOW_DIRECTION_C2S; @@ -705,7 +712,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m sess->mgr_stat = &mgr->stat; enum session_state next_state = session_transition_run(SESSION_STATE_INIT, TCP_SYN); - session_update(sess, next_state, pkt, key, dir); + session_update(mgr, sess, next_state, pkt, key, dir); session_transition_log(sess, SESSION_STATE_INIT, next_state, TCP_SYN); if (tcp_init(mgr, sess) == -1) @@ -714,15 +721,15 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m session_pool_push(mgr->sess_pool, sess); return NULL; } - tcp_update(mgr, sess, dir, tcp_layer, now); + tcp_update(mgr, sess, dir, tcp_layer); uint64_t timeout = (flags & TH_ACK) ? mgr->opts.tcp_handshake_timeout : mgr->opts.tcp_init_timeout; - session_timer_update(mgr->sess_timer, sess, now + timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + timeout); session_table_add(mgr->tcp_sess_table, sess); if (mgr->opts.duplicated_packet_filter_enable) { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, now); + duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } SESS_MGR_STAT_INC(&mgr->stat, next_state, tcp); @@ -732,13 +739,13 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m return sess; } -static struct session *session_manager_new_udp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static struct session *session_manager_new_udp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key) { // udp table full evict old session if (mgr->opts.udp_overload_evict_old_sess && mgr->stat.curr_nr_udp_sess_used >= mgr->opts.max_udp_session_num - EVICTE_SESSION_BURST) { struct session *evic_sess = session_table_find_lru(mgr->udp_sess_table); - session_manager_evicte_session(mgr, evic_sess, now, LRU_EVICT); + session_manager_evicte_session(mgr, evic_sess, LRU_EVICT); } struct session *sess = session_pool_pop(mgr->sess_pool); @@ -753,10 +760,10 @@ static struct session *session_manager_new_udp_session(struct session_manager *m enum flow_direction dir = identify_direction_by_port(ntohs(key->src_port), ntohs(key->dst_port)); enum session_state next_state = session_transition_run(SESSION_STATE_INIT, UDP_DATA); - session_update(sess, next_state, pkt, key, dir); + session_update(mgr, sess, next_state, pkt, key, dir); session_transition_log(sess, SESSION_STATE_INIT, next_state, UDP_DATA); - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_data_timeout); session_table_add(mgr->udp_sess_table, sess); SESS_MGR_STAT_INC(&mgr->stat, next_state, udp); @@ -766,7 +773,7 @@ static struct session *session_manager_new_udp_session(struct session_manager *m return sess; } -static int session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static int session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key) { const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(pkt, LAYER_PROTO_TCP); const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr; @@ -783,11 +790,11 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc enum session_state next_state = session_transition_run(curr_state, inputs); // update session - session_update(sess, next_state, pkt, key, dir); + session_update(mgr, sess, next_state, pkt, key, dir); session_transition_log(sess, curr_state, next_state, inputs); // update tcp - tcp_update(mgr, sess, dir, tcp_layer, now); + tcp_update(mgr, sess, dir, tcp_layer); // set closing reason if (next_state == SESSION_STATE_CLOSING && !session_get_closing_reason(sess)) @@ -844,28 +851,28 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc assert(0); break; } - session_timer_update(mgr->sess_timer, sess, now + timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + timeout); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, tcp); return 0; } -static int session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key, uint64_t now) +static int session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key) { enum flow_direction dir = identify_direction_by_history(sess, key); enum session_state curr_state = session_get_current_state(sess); enum session_state next_state = session_transition_run(curr_state, UDP_DATA); - session_update(sess, next_state, pkt, key, dir); + session_update(mgr, sess, next_state, pkt, key, dir); session_transition_log(sess, curr_state, next_state, UDP_DATA); if (session_get_current_state(sess) == SESSION_STATE_DISCARD) { - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_discard_timeout); } else { - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_data_timeout); } SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); @@ -877,7 +884,7 @@ static int session_manager_update_udp_session(struct session_manager *mgr, struc * Public API ******************************************************************************/ -struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now) +struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now_ms) { if (check_options(opts) == -1) { @@ -894,7 +901,7 @@ struct session_manager *session_manager_new(struct session_manager_options *opts mgr->sess_pool = session_pool_new(mgr->opts.max_tcp_session_num + mgr->opts.max_udp_session_num); mgr->tcp_sess_table = session_table_new(); mgr->udp_sess_table = session_table_new(); - mgr->sess_timer = session_timer_new(now); + mgr->sess_timer = session_timer_new(now_ms); if (mgr->sess_pool == NULL || mgr->tcp_sess_table == NULL || mgr->udp_sess_table == NULL || mgr->sess_timer == NULL) { goto error; @@ -903,7 +910,7 @@ struct session_manager *session_manager_new(struct session_manager_options *opts { mgr->evicte_sess_filter = evicted_session_filter_new(mgr->opts.evicted_session_filter_capacity, mgr->opts.evicted_session_filter_timeout, - mgr->opts.evicted_session_filter_error_rate, now); + mgr->opts.evicted_session_filter_error_rate, now_ms); if (mgr->evicte_sess_filter == NULL) { goto error; @@ -913,7 +920,7 @@ struct session_manager *session_manager_new(struct session_manager_options *opts { mgr->dup_pkt_filter = duplicated_packet_filter_new(mgr->opts.duplicated_packet_filter_capacity, mgr->opts.duplicated_packet_filter_timeout, - mgr->opts.duplicated_packet_filter_error_rate, now); + mgr->opts.duplicated_packet_filter_error_rate, now_ms); if (mgr->dup_pkt_filter == NULL) { goto error; @@ -922,6 +929,7 @@ struct session_manager *session_manager_new(struct session_manager_options *opts INIT_LIST_HEAD(&mgr->evicte_queue); session_transition_init(); + mgr->now_ms = now_ms; return mgr; @@ -969,16 +977,18 @@ void session_manager_free(struct session_manager *mgr) } } -void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt, uint64_t now) +void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt) { if (mgr->opts.duplicated_packet_filter_enable) { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, now); + duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } } -struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now) +struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now_ms) { + mgr->now_ms = now_ms; + struct tuple6 key; if (packet_get_innermost_tuple6(pkt, &key)) { @@ -987,21 +997,21 @@ struct session *session_manager_new_session(struct session_manager *mgr, const s switch (key.ip_proto) { case IPPROTO_TCP: - if (tcp_overload_bypass(mgr, &key, now)) + if (tcp_overload_bypass(mgr, &key)) { return NULL; } - return session_manager_new_tcp_session(mgr, pkt, &key, now); + return session_manager_new_tcp_session(mgr, pkt, &key); case IPPROTO_UDP: - if (udp_overload_bypass(mgr, &key, now)) + if (udp_overload_bypass(mgr, &key)) { return NULL; } - if (evicted_session_bypass(mgr, &key, now)) + if (evicted_session_bypass(mgr, &key)) { return NULL; } - return session_manager_new_udp_session(mgr, pkt, &key, now); + return session_manager_new_udp_session(mgr, pkt, &key); default: return NULL; } @@ -1054,8 +1064,10 @@ void session_manager_free_session(struct session_manager *mgr, struct session *s } } -struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now) +struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now_ms) { + mgr->now_ms = now_ms; + struct tuple6 key; if (packet_get_innermost_tuple6(pkt, &key)) { @@ -1066,37 +1078,41 @@ struct session *session_manager_lookup_session(struct session_manager *mgr, cons case IPPROTO_UDP: return session_table_find_tuple6(mgr->udp_sess_table, &key); case IPPROTO_TCP: - return session_manager_lookup_tcp_session(mgr, pkt, &key, now); + return session_manager_lookup_tcp_session(mgr, pkt, &key); default: return NULL; } } -int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, uint64_t now) +int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, uint64_t now_ms) { + mgr->now_ms = now_ms; + struct tuple6 key; if (packet_get_innermost_tuple6(pkt, &key)) { return -1; } - if (duplicated_packet_bypass(mgr, sess, pkt, &key, now)) + if (duplicated_packet_bypass(mgr, sess, pkt, &key)) { return -1; } switch (session_get_type(sess)) { case SESSION_TYPE_TCP: - return session_manager_update_tcp_session(mgr, sess, pkt, &key, now); + return session_manager_update_tcp_session(mgr, sess, pkt, &key); case SESSION_TYPE_UDP: - return session_manager_update_udp_session(mgr, sess, pkt, &key, now); + return session_manager_update_udp_session(mgr, sess, pkt, &key); default: return -1; } } -struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now) +struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now_ms) { - struct session *sess = session_timer_expire(mgr->sess_timer, now); + mgr->now_ms = now_ms; + + struct session *sess = session_timer_expire(mgr->sess_timer, now_ms); if (sess) { enum session_state curr_state = session_get_current_state(sess); @@ -1132,10 +1148,10 @@ struct session *session_manager_get_expired_session(struct session_manager *mgr, switch (session_get_type(sess)) { case SESSION_TYPE_TCP: - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.tcp_data_timeout); + session_timer_update(mgr->sess_timer, sess, now_ms + mgr->opts.tcp_data_timeout); break; case SESSION_TYPE_UDP: - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, now_ms + mgr->opts.udp_data_timeout); break; default: assert(0); @@ -1174,7 +1190,6 @@ struct session_manager_stat *session_manager_stat(struct session_manager *mgr) void session_set_discard(struct session *sess) { - uint64_t now = stellar_get_monotonic_time_msec(); struct session_manager *mgr = sess->mgr; enum session_type type = session_get_type(sess); enum session_state curr_state = session_get_current_state(sess); @@ -1185,11 +1200,11 @@ void session_set_discard(struct session *sess) switch (type) { case SESSION_TYPE_TCP: - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.tcp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.tcp_discard_timeout); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, tcp); break; case SESSION_TYPE_UDP: - session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_discard_timeout); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); break; default: diff --git a/src/session/session_manager.h b/src/session/session_manager.h index f562a80..46754ab 100644 --- a/src/session/session_manager.h +++ b/src/session/session_manager.h @@ -90,18 +90,18 @@ struct session_manager_stat }; struct session_manager; -struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now); +struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now_ms); void session_manager_free(struct session_manager *mgr); -void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt, uint64_t now); +void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt); -struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now); +struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now_ms); void session_manager_free_session(struct session_manager *mgr, struct session *sess); -struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now); -int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, uint64_t now); +struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now_ms); +int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, uint64_t now_ms); // return session need free by session_manager_free_session() -struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now); +struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now_ms); struct session *session_manager_get_evicted_session(struct session_manager *mgr); // return 0: have already timeout session diff --git a/src/session/test/gtest_case_tcp_fast_open.cpp b/src/session/test/gtest_case_tcp_fast_open.cpp index 26329a2..3cd09a2 100644 --- a/src/session/test/gtest_case_tcp_fast_open.cpp +++ b/src/session/test/gtest_case_tcp_fast_open.cpp @@ -1,9 +1,9 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "tcp_reassembly.h" @@ -307,7 +307,6 @@ TEST(CASE, TCP_FAST_OPEN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -421,6 +420,11 @@ TEST(CASE, TCP_FAST_OPEN) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_tcp_active_to_closing.cpp b/src/session/test/gtest_state_tcp_active_to_closing.cpp index aa0dc8d..bbf78ef 100644 --- a/src/session/test/gtest_state_tcp_active_to_closing.cpp +++ b/src/session/test/gtest_state_tcp_active_to_closing.cpp @@ -2,11 +2,11 @@ #include #include "tuple.h" -#include "times.h" #include "tcp_utils.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -92,7 +92,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -185,7 +184,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -273,7 +271,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -359,7 +356,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -409,7 +405,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -490,7 +485,6 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -560,6 +554,11 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_tcp_init_to_opening.cpp b/src/session/test/gtest_state_tcp_init_to_opening.cpp index a8130e3..844c0c2 100644 --- a/src/session/test/gtest_state_tcp_init_to_opening.cpp +++ b/src/session/test/gtest_state_tcp_init_to_opening.cpp @@ -2,11 +2,11 @@ #include #include "tuple.h" -#include "times.h" #include "ip4_utils.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -63,7 +63,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -142,7 +141,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -221,7 +219,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -312,7 +309,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -417,7 +413,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -515,7 +510,6 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -612,7 +606,6 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -703,7 +696,6 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -783,6 +775,11 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp b/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp index 5562e5d..4be8039 100644 --- a/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp +++ b/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp @@ -2,10 +2,10 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -58,7 +58,6 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -445,6 +444,11 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_tcp_opening_to_active.cpp b/src/session/test/gtest_state_tcp_opening_to_active.cpp index ede25e5..ef59682 100644 --- a/src/session/test/gtest_state_tcp_opening_to_active.cpp +++ b/src/session/test/gtest_state_tcp_opening_to_active.cpp @@ -2,10 +2,10 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -62,7 +62,6 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -153,7 +152,6 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -233,6 +231,11 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_tcp_opening_to_closing.cpp b/src/session/test/gtest_state_tcp_opening_to_closing.cpp index 2e820dc..f3d4236 100644 --- a/src/session/test/gtest_state_tcp_opening_to_closing.cpp +++ b/src/session/test/gtest_state_tcp_opening_to_closing.cpp @@ -2,11 +2,11 @@ #include #include "tuple.h" -#include "times.h" #include "tcp_utils.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -63,7 +63,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -165,7 +164,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -262,7 +260,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -358,7 +355,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -417,7 +413,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -508,7 +503,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -611,7 +605,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -701,7 +694,6 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -780,6 +772,11 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp b/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp index 146605a..2a063d3 100644 --- a/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp +++ b/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp @@ -2,10 +2,10 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -58,7 +58,6 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -157,6 +156,11 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp b/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp index f7b5cf0..e155fbb 100644 --- a/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp +++ b/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp @@ -2,10 +2,10 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" +#include "id_generator.h" #include "session_utils.h" #include "session_manager.h" #include "test_packets.h" @@ -63,7 +63,6 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_C2S) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -143,7 +142,6 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -213,6 +211,11 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C) int main(int argc, char **argv) { + struct id_generator_options opt = { + .snowflake_worker_id_base = 1, + .snowflake_worker_id_offset = 2, + }; + id_generator_init(&opt); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/src/session/test/gtest_timeout_tcp_data.cpp b/src/session/test/gtest_timeout_tcp_data.cpp index ae88c81..7d62aac 100644 --- a/src/session/test/gtest_timeout_tcp_data.cpp +++ b/src/session/test/gtest_timeout_tcp_data.cpp @@ -1,7 +1,6 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" @@ -55,7 +54,6 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA) struct session *sess = NULL; struct session_manager *mgr = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_tcp_handshake.cpp b/src/session/test/gtest_timeout_tcp_handshake.cpp index 2cc0c20..b20bdba 100644 --- a/src/session/test/gtest_timeout_tcp_handshake.cpp +++ b/src/session/test/gtest_timeout_tcp_handshake.cpp @@ -1,7 +1,6 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" @@ -55,7 +54,6 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE) struct session *sess = NULL; struct session_manager *mgr = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_tcp_init.cpp b/src/session/test/gtest_timeout_tcp_init.cpp index 2bf3dc8..5e02667 100644 --- a/src/session/test/gtest_timeout_tcp_init.cpp +++ b/src/session/test/gtest_timeout_tcp_init.cpp @@ -1,7 +1,6 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" @@ -55,7 +54,6 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT) struct session *sess = NULL; struct session_manager *mgr = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_udp_data.cpp b/src/session/test/gtest_timeout_udp_data.cpp index 5cad862..5a94a02 100644 --- a/src/session/test/gtest_timeout_udp_data.cpp +++ b/src/session/test/gtest_timeout_udp_data.cpp @@ -1,7 +1,6 @@ #include #include "tuple.h" -#include "times.h" #include "packet_def.h" #include "packet_parse.h" #include "packet_layer.h" @@ -55,7 +54,6 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1) struct session *sess = NULL; struct session_manager *mgr = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -92,7 +90,6 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2) struct session *sess = NULL; struct session_manager *mgr = NULL; - stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/stellar/stellar_core.cpp b/src/stellar/stellar_core.cpp index c58b41a..4ddf37d 100644 --- a/src/stellar/stellar_core.cpp +++ b/src/stellar/stellar_core.cpp @@ -111,13 +111,13 @@ static inline void free_evicted_sessions(struct session_manager *sess_mgr, uint6 } } -static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now) +static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now_ms) { void *plugin_ctx = NULL; struct session *sess = NULL; for (uint64_t i = 0; i < max_free; i++) { - sess = session_manager_get_expired_session(sess_mgr, now); + sess = session_manager_get_expired_session(sess_mgr, now_ms); if (sess) { plugin_ctx = session_get_user_data(sess); @@ -131,7 +131,7 @@ static inline void free_expired_sessions(struct session_manager *sess_mgr, uint6 } } -static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now) +static inline void merge_thread_stat(struct stellar_thread *thread) { struct stellar_runtime *runtime = thread->runtime; struct thread_stat thr_stat = { @@ -145,7 +145,7 @@ static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now static void *work_thread(void *arg) { int nr_recv; - uint64_t now = 0; + uint64_t now_ms = 0; char thd_name[16] = {0}; void *plugin_ctx = NULL; struct packet *pkt = NULL; @@ -176,7 +176,15 @@ static void *work_thread(void *arg) while (ATOMIC_READ(&need_exit) == 0) { - now = stellar_get_monotonic_time_msec(); + /* + * 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. + */ + now_ms = stellar_get_real_time_msec(); memset(packets, 0, sizeof(packets)); nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX); if (nr_recv == 0) @@ -193,7 +201,7 @@ static void *work_thread(void *arg) plugin_manager_on_packet(plug_mgr, pkt); if (packet_is_fragment(pkt)) { - defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now); + defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now_ms); if (defraged_pkt == NULL) { goto fast_path; @@ -205,10 +213,10 @@ static void *work_thread(void *arg) } } - sess = session_manager_lookup_session(sess_mgr, pkt, now); + sess = session_manager_lookup_session(sess_mgr, pkt, now_ms); if (sess == NULL) { - sess = session_manager_new_session(sess_mgr, pkt, now); + sess = session_manager_new_session(sess_mgr, pkt, now_ms); if (sess == NULL) { goto fast_path; @@ -218,7 +226,7 @@ static void *work_thread(void *arg) } else { - if (session_manager_update_session(sess_mgr, sess, pkt, now) == -1) + if (session_manager_update_session(sess_mgr, sess, pkt, now_ms) == -1) { goto fast_path; } @@ -269,14 +277,14 @@ static void *work_thread(void *arg) free_evicted_sessions(sess_mgr, nr_recv); // per 5 ms, atmost free 8 expired session - if (now - thread->timing_wheel_last_update_ts > 5) + if (now_ms - thread->timing_wheel_last_update_ts > 5) { - free_expired_sessions(sess_mgr, 8, now); - thread->timing_wheel_last_update_ts = now; + free_expired_sessions(sess_mgr, 8, now_ms); + thread->timing_wheel_last_update_ts = now_ms; } - merge_thread_stat(thread, now); - ip_reassembly_expire(ip_reass, now); + merge_thread_stat(thread); + ip_reassembly_expire(ip_reass, now_ms); plugin_manager_on_polling(runtime->plug_mgr); if (nr_recv == 0) @@ -345,14 +353,14 @@ static int all_session_have_freed(struct stellar_runtime *runtime, struct stella static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config) { - uint64_t now = stellar_get_monotonic_time_msec(); + uint64_t now_ms = stellar_get_real_time_msec(); for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; thread->idx = i; thread->is_runing = 0; - thread->timing_wheel_last_update_ts = now; - thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now); + thread->timing_wheel_last_update_ts = now_ms; + thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms); if (thread->sess_mgr == NULL) { STELLAR_LOG_ERROR("unable to create session manager"); @@ -418,8 +426,6 @@ int stellar_run(int argc, char **argv) struct stellar_runtime *runtime = &st.runtime; struct stellar_config *config = &st.config; - stellar_update_time_cache(); - signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); signal(SIGTERM, signal_handler); @@ -480,13 +486,12 @@ int stellar_run(int argc, char **argv) goto error_out; } - runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); + runtime->stat_last_output_ts = stellar_get_real_time_msec(); while (!ATOMIC_READ(&need_exit)) { - stellar_update_time_cache(); - if (stellar_get_monotonic_time_msec() - runtime->stat_last_output_ts > 2000) + if (stellar_get_real_time_msec() - runtime->stat_last_output_ts > 2000) { - runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); + runtime->stat_last_output_ts = stellar_get_real_time_msec(); stellar_stat_output(runtime->stat); } usleep(1000); // 1ms @@ -545,11 +550,10 @@ uint16_t stellar_get_current_thread_index() // only send user crafted packet, can't send packet which come from network void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt) { - uint64_t time_ms = stellar_get_monotonic_time_msec(); 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); - session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms); + session_manager_record_duplicated_packet(sess_mgr, pkt); if (packet_get_origin_ctx(pkt)) { diff --git a/src/times/test/gtest_times.cpp b/src/times/test/gtest_times.cpp index dc413a9..9ec7988 100644 --- a/src/times/test/gtest_times.cpp +++ b/src/times/test/gtest_times.cpp @@ -9,12 +9,11 @@ TEST(TIMES, TEST) uint64_t curr_sec = 0; uint64_t curr_msec = 0; - stellar_update_time_cache(); last_sec = stellar_get_monotonic_time_sec(); last_msec = stellar_get_monotonic_time_msec(); usleep(1000); // 1ms - stellar_update_time_cache(); + curr_sec = stellar_get_monotonic_time_sec(); curr_msec = stellar_get_monotonic_time_msec(); printf("After usleep(1000)\n"); @@ -24,7 +23,7 @@ TEST(TIMES, TEST) EXPECT_TRUE(curr_msec - last_msec >= 1); usleep(1000 * 1000); // 1s - stellar_update_time_cache(); + last_sec = curr_sec; last_msec = curr_msec; curr_sec = stellar_get_monotonic_time_sec(); diff --git a/src/times/times.cpp b/src/times/times.cpp index 1a1854c..2289e11 100644 --- a/src/times/times.cpp +++ b/src/times/times.cpp @@ -1,6 +1,5 @@ #include -#include "macro.h" #include "times.h" // 1 s = 1000 ms @@ -17,69 +16,49 @@ * }; */ -struct -{ - struct timespec real_time_spec; - uint64_t real_time_msec; - uint64_t real_time_sec; +/* + * CLOCK_MONOTONIC + * + * On Linux, that point corresponds to the number of sec‐ + * onds that the system has been running since it was booted. + * + * The CLOCK_MONOTONIC clock is not affected by discontinuous + * jumps in the system time (e.g., if the system administrator + * manually changes the clock), but is affected by the incremen‐ + * tal adjustments performed by adjtime(3) and NTP. + */ - struct timespec monotonic_time_spec; - uint64_t monotonic_time_msec; - uint64_t monotonic_time_sec; -} global_time; - -void stellar_update_time_cache() -{ - uint64_t time_msec; - uint64_t time_sec; - /* - * CLOCK_MONOTONIC - * - * On Linux, that point corresponds to the number of sec‐ - * onds that the system has been running since it was booted. - * - * The CLOCK_MONOTONIC clock is not affected by discontinuous - * jumps in the system time (e.g., if the system administrator - * manually changes the clock), but is affected by the incremen‐ - * tal adjustments performed by adjtime(3) and NTP. - */ - clock_gettime(CLOCK_MONOTONIC, &global_time.monotonic_time_spec); - - time_msec = global_time.monotonic_time_spec.tv_sec * 1000 + - global_time.monotonic_time_spec.tv_nsec / 1000000; - time_sec = global_time.monotonic_time_spec.tv_sec + - global_time.monotonic_time_spec.tv_nsec / 1000000000; - - ATOMIC_SET(&global_time.monotonic_time_msec, time_msec); - ATOMIC_SET(&global_time.monotonic_time_sec, time_sec); - - clock_gettime(CLOCK_REALTIME, &global_time.real_time_spec); - - time_msec = global_time.real_time_spec.tv_sec * 1000 + - global_time.real_time_spec.tv_nsec / 1000000; - time_sec = global_time.real_time_spec.tv_sec + - global_time.real_time_spec.tv_nsec / 1000000000; - - ATOMIC_SET(&global_time.real_time_msec, time_msec); - ATOMIC_SET(&global_time.real_time_sec, time_sec); -} +#define TIMESPEC_TO_MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000) +#define TIMESPEC_TO_SEC(ts) ((ts).tv_sec + (ts).tv_nsec / 1000000000) uint64_t stellar_get_monotonic_time_sec() { - return ATOMIC_READ(&global_time.monotonic_time_sec); + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + return TIMESPEC_TO_SEC(now); } uint64_t stellar_get_monotonic_time_msec() { - return ATOMIC_READ(&global_time.monotonic_time_msec); + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + return TIMESPEC_TO_MSEC(now); } uint64_t stellar_get_real_time_sec() { - return ATOMIC_READ(&global_time.real_time_sec); + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + + return TIMESPEC_TO_SEC(now); } uint64_t stellar_get_real_time_msec() { - return ATOMIC_READ(&global_time.real_time_msec); + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + + return TIMESPEC_TO_MSEC(now); } diff --git a/src/times/times.h b/src/times/times.h index c426a17..b4d6cf1 100644 --- a/src/times/times.h +++ b/src/times/times.h @@ -7,8 +7,6 @@ extern "C" #include -void stellar_update_time_cache(); - uint64_t stellar_get_monotonic_time_sec(); uint64_t stellar_get_monotonic_time_msec();