This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/session/session.cpp

530 lines
17 KiB
C++
Raw Normal View History

2023-12-11 16:35:26 +08:00
#include <assert.h>
#include "session_priv.h"
2024-04-01 17:13:26 +08:00
#include "tcp_utils.h"
#include "tcp_reassembly.h"
2023-12-11 16:35:26 +08:00
#define EX_KEY_MAX_LEN 64
struct ex_schema
{
char key[EX_KEY_MAX_LEN];
session_ex_free_cb *free_cb;
void *args;
};
struct ex_manager
{
struct ex_schema schemas[EX_DATA_MAX_COUNT];
uint8_t count;
};
static struct ex_manager g_ex_manager = {0};
/******************************************************************************
2024-04-01 17:13:26 +08:00
* session set/get
2023-12-11 16:35:26 +08:00
******************************************************************************/
void session_init(struct session *sess)
{
memset(sess, 0, sizeof(struct session));
}
void session_set_id(struct session *sess, uint64_t id)
{
sess->id = id;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_id(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->id;
}
2024-04-01 17:13:26 +08:00
void session_set_tuple(struct session *sess, const struct tuple6 *tuple)
2023-12-11 16:35:26 +08:00
{
2023-12-15 16:34:53 +08:00
memcpy(&sess->tuple, tuple, sizeof(struct tuple6));
2023-12-11 16:35:26 +08:00
}
2024-04-01 17:13:26 +08:00
const struct tuple6 *session_get_tuple(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
2023-12-15 16:34:53 +08:00
return &sess->tuple;
2023-12-11 16:35:26 +08:00
}
void session_set_tuple_direction(struct session *sess, enum flow_direction dir)
2023-12-19 10:47:26 +08:00
{
sess->tuple_dir = dir;
}
enum flow_direction session_get_tuple_direction(const struct session *sess)
2023-12-19 10:47:26 +08:00
{
return sess->tuple_dir;
}
const char *session_get_tuple_str(const struct session *sess)
{
return sess->tuple_str;
}
void session_set_direction(struct session *sess, enum session_direction dir)
2024-04-01 17:13:26 +08:00
{
sess->sess_dir = dir;
2024-04-01 17:13:26 +08:00
}
enum session_direction session_get_direction(const struct session *sess)
2024-04-01 17:13:26 +08:00
{
return sess->sess_dir;
}
void session_set_flow_direction(struct session *sess, enum flow_direction dir)
{
sess->flow_dir = dir;
}
enum flow_direction session_get_flow_direction(const struct session *sess)
{
return sess->flow_dir;
2024-04-01 17:13:26 +08:00
}
2023-12-11 16:35:26 +08:00
void session_set_state(struct session *sess, enum session_state state)
{
sess->state = state;
}
2023-12-19 10:47:26 +08:00
enum session_state session_get_state(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->state;
}
void session_set_type(struct session *sess, enum session_type type)
{
sess->type = type;
}
2023-12-19 10:47:26 +08:00
enum session_type session_get_type(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->type;
}
2024-04-01 17:13:26 +08:00
void session_set_dup_traffic(struct session *sess)
{
2024-04-01 17:13:26 +08:00
sess->dup = 1;
}
2024-04-01 17:13:26 +08:00
int session_has_dup_traffic(const struct session *sess)
{
2024-04-01 17:13:26 +08:00
return sess->dup;
}
2024-03-08 13:33:49 +08:00
void session_set_closing_reason(struct session *sess, enum closing_reason reason)
{
2024-04-01 17:13:26 +08:00
sess->reason = reason;
}
2024-03-08 13:33:49 +08:00
enum closing_reason session_get_closing_reason(const struct session *sess)
{
2024-04-01 17:13:26 +08:00
return sess->reason;
2023-12-11 16:35:26 +08:00
}
void session_inc_stat(struct session *sess, enum flow_direction dir, enum session_stat stat, uint64_t val)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
sess->stats[dir][stat] += val;
2023-12-11 16:35:26 +08:00
}
uint64_t session_get_stat(const struct session *sess, enum flow_direction dir, enum session_stat stat)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
return sess->stats[dir][stat];
2023-12-11 16:35:26 +08:00
}
void session_set_timestamp(struct session *sess, enum session_timestamp type, uint64_t value)
2023-12-11 16:35:26 +08:00
{
sess->timestamps[type] = value;
2023-12-11 16:35:26 +08:00
}
uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type)
2023-12-11 16:35:26 +08:00
{
return sess->timestamps[type];
2023-12-11 16:35:26 +08:00
}
void session_clear_sid_list(struct session *sess, enum flow_direction dir)
2024-05-08 18:24:26 +08:00
{
memset(&sess->sids[dir], 0, sizeof(struct sid_list));
}
void session_set_sid_list(struct session *sess, enum flow_direction dir, const struct sid_list *list)
2024-05-08 18:24:26 +08:00
{
sess->sids[dir] = *list;
}
void session_get_sid_list(const struct session *sess, enum flow_direction dir, struct sid_list *list)
2024-05-08 18:24:26 +08:00
{
*list = sess->sids[dir];
}
void session_clear_route_ctx(struct session *sess, enum flow_direction dir)
2024-05-08 18:24:26 +08:00
{
memset(&sess->route_ctx[dir], 0, sizeof(struct route_ctx));
}
void session_set_route_ctx(struct session *sess, enum flow_direction dir, const struct route_ctx *ctx)
2024-05-08 18:24:26 +08:00
{
sess->route_ctx[dir] = *ctx;
}
void session_get_route_ctx(const struct session *sess, enum flow_direction dir, struct route_ctx *ctx)
2024-05-08 18:24:26 +08:00
{
*ctx = sess->route_ctx[dir];
}
void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
sess->first_pkt[dir] = packet_dup(pkt);
2023-12-11 16:35:26 +08:00
}
const struct packet *session_get_first_packet(const struct session *sess, enum flow_direction dir)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
return sess->first_pkt[dir];
2023-12-11 16:35:26 +08:00
}
2024-04-09 15:07:53 +08:00
void session_set_current_packet(struct session *sess, const struct packet *pkt)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
sess->curr_pkt = pkt;
2023-12-11 16:35:26 +08:00
}
2024-04-09 15:07:53 +08:00
const struct packet *session_get_current_packet(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
2024-04-09 15:07:53 +08:00
return sess->curr_pkt;
2023-12-11 16:35:26 +08:00
}
2024-03-08 18:10:38 +08:00
void session_set_user_data(struct session *sess, void *user_data)
{
sess->user_data = user_data;
}
void *session_get_user_data(const struct session *sess)
{
return sess->user_data;
}
struct tcp_segment *session_get_tcp_segment(struct session *sess)
{
enum flow_direction dir = session_get_flow_direction(sess);
2024-04-09 15:07:53 +08:00
struct tcp_half *half = &sess->tcp_halfs[dir];
2024-04-09 15:07:53 +08:00
if (half->in_order.data != NULL && half->in_order.len > 0)
{
2024-04-09 15:07:53 +08:00
return &half->in_order;
}
else
{
2024-04-03 18:59:46 +08:00
struct tcp_segment *seg = tcp_reassembly_pop(half->assembler);
if (seg)
{
2024-04-09 15:07:53 +08:00
session_inc_stat(sess, dir, STAT_TCP_SEGS_REORDERED, 1);
session_inc_stat(sess, dir, STAT_TCP_PLDS_REORDERED, seg->len);
2024-04-09 10:36:39 +08:00
sess->mgr_stat->nr_tcp_seg_reorded++;
2024-04-03 18:59:46 +08:00
}
return seg;
}
}
2024-04-03 18:59:46 +08:00
void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg)
{
2024-04-03 18:59:46 +08:00
if (seg == NULL)
{
2024-04-03 18:59:46 +08:00
return;
}
enum flow_direction dir = session_get_flow_direction(sess);
2024-04-09 15:07:53 +08:00
struct tcp_half *half = &sess->tcp_halfs[dir];
2024-01-11 16:46:33 +08:00
2024-04-09 15:07:53 +08:00
if (seg == &half->in_order)
2024-04-01 17:13:26 +08:00
{
2024-04-09 15:07:53 +08:00
half->in_order.data = NULL;
half->in_order.len = 0;
2024-04-03 18:59:46 +08:00
return;
2024-04-01 17:13:26 +08:00
}
2024-04-03 18:59:46 +08:00
else
2024-04-01 17:13:26 +08:00
{
2024-04-09 15:07:53 +08:00
session_inc_stat(sess, dir, STAT_TCP_SEGS_RELEASED, 1);
session_inc_stat(sess, dir, STAT_TCP_PLDS_RELEASED, seg->len);
2024-04-09 10:36:39 +08:00
sess->mgr_stat->nr_tcp_seg_released++;
2024-04-09 15:07:53 +08:00
2024-04-03 18:59:46 +08:00
tcp_segment_free(seg);
2024-04-01 17:13:26 +08:00
}
2023-12-15 11:20:05 +08:00
}
2023-12-11 16:35:26 +08:00
/******************************************************************************
* session ex data
******************************************************************************/
/*
* the exdata prodoced by user, and comsumed by same user.
* so, the exdata is not shared by different user.
* otherwise, the exdata need dup by refer count, and free by refer count.
*
* if key exist, not allow update, return original index.
*/
uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args)
{
if (g_ex_manager.count >= EX_DATA_MAX_COUNT)
{
abort();
return EX_DATA_MAX_COUNT;
}
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
if (strcmp(g_ex_manager.schemas[i].key, key) == 0)
{
return i;
}
}
uint8_t idx = g_ex_manager.count;
g_ex_manager.count++;
struct ex_schema *schema = &g_ex_manager.schemas[idx];
strncpy(schema->key, key, EX_KEY_MAX_LEN);
schema->free_cb = free_cb;
schema->args = args;
return idx;
}
/*
* Support update ex_data.
*
* if key exist: run free_cb free old value, then set new value.
* if not run free_cb, old value will be memory leak.
* if not allow update, new value will be memory leak.
* if key not exist: set new value.
*/
void session_set_ex_data(struct session *sess, uint8_t idx, void *val)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
session_free_ex_data(sess, idx);
sess->ex_data[idx] = val;
}
2023-12-19 10:47:26 +08:00
void *session_get0_ex_data(const struct session *sess, uint8_t idx)
2023-12-11 16:35:26 +08:00
{
if (idx >= g_ex_manager.count)
{
assert(0);
return NULL;
}
return sess->ex_data[idx];
}
/*
* after set ex_data, the owner of ex_data is session, so user should not free it directly.
* if user want to free ex_data, should use session_free_ex_data.
*/
void session_free_ex_data(struct session *sess, uint8_t idx)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
struct ex_schema *schema = &g_ex_manager.schemas[idx];
if (schema->free_cb != NULL && sess->ex_data[idx] != NULL)
{
2024-01-09 18:03:24 +08:00
printf("free ex_data, idx: %d, key: %s, val: %p\n", idx, schema->key, sess->ex_data[idx]);
2023-12-11 16:35:26 +08:00
schema->free_cb(sess, idx, sess->ex_data[idx], schema->args);
}
sess->ex_data[idx] = NULL;
}
2023-12-12 18:41:53 +08:00
2024-03-26 15:09:03 +08:00
void session_free_all_ex_data(struct session *sess)
{
if (sess)
{
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
session_free_ex_data(sess, i);
}
}
}
2024-04-03 18:59:46 +08:00
/******************************************************************************
* to string
******************************************************************************/
const char *closing_reason_to_str(enum closing_reason reason)
{
switch (reason)
{
case CLOSING_BY_TIMEOUT:
return "closing by timeout";
case CLOSING_BY_EVICTED:
return "closing by evicted";
case CLOSING_BY_CLIENT_FIN:
return "closing by client FIN";
case CLOSING_BY_CLIENT_RST:
return "closing by client RST";
case CLOSING_BY_SERVER_FIN:
return "closing by server FIN";
case CLOSING_BY_SERVER_RST:
return "closing by server RST";
default:
return "unknown";
}
}
const char *session_state_to_str(enum session_state state)
{
switch (state)
{
case SESSION_STATE_INIT:
return "init";
case SESSION_STATE_OPENING:
return "opening";
case SESSION_STATE_ACTIVE:
return "active";
case SESSION_STATE_CLOSING:
return "closing";
case SESSION_STATE_DISCARD:
return "discard";
case SESSION_STATE_CLOSED:
return "closed";
default:
return "unknown";
}
}
const char *session_type_to_str(enum session_type type)
{
switch (type)
{
case SESSION_TYPE_TCP:
return "TCP";
case SESSION_TYPE_UDP:
return "UDP";
default:
return "unknown";
}
}
const char *flow_direction_to_str(enum flow_direction dir)
2024-04-03 18:59:46 +08:00
{
switch (dir)
{
case FLOW_DIRECTION_C2S:
2024-04-03 18:59:46 +08:00
return "C2S";
case FLOW_DIRECTION_S2C:
2024-04-03 18:59:46 +08:00
return "S2C";
default:
return "unknown";
}
}
static void tcp_flags_to_str(uint8_t flags, char *buffer, size_t len)
2024-04-03 18:59:46 +08:00
{
int used = 0;
2024-04-09 15:07:53 +08:00
if (flags & TH_SYN)
2024-04-03 18:59:46 +08:00
{
2024-04-09 15:07:53 +08:00
used += snprintf(buffer + used, len - used, "SYN ");
2024-04-03 18:59:46 +08:00
}
2024-04-09 15:07:53 +08:00
if (flags & TH_ACK)
2024-04-03 18:59:46 +08:00
{
2024-04-09 15:07:53 +08:00
used += snprintf(buffer + used, len - used, "ACK ");
2024-04-03 18:59:46 +08:00
}
2024-04-09 15:07:53 +08:00
if (flags & TH_FIN)
2024-04-03 18:59:46 +08:00
{
2024-04-09 15:07:53 +08:00
used += snprintf(buffer + used, len - used, "FIN ");
2024-04-03 18:59:46 +08:00
}
2024-04-09 15:07:53 +08:00
if (flags & TH_RST)
2024-04-03 18:59:46 +08:00
{
2024-04-09 15:07:53 +08:00
used += snprintf(buffer + used, len - used, "RST ");
2024-04-03 18:59:46 +08:00
}
}
2024-04-11 19:44:02 +08:00
int session_to_json(struct session *sess, char *buff, int size)
{
memset(buff, 0, size);
char flags[64] = {0};
int used = 0;
2024-04-09 15:07:53 +08:00
2024-04-11 19:44:02 +08:00
used += snprintf(buff + used, size - used, "{");
used += snprintf(buff + used, size - used, "\"id\":%" PRIu64 ",", session_get_id(sess));
used += snprintf(buff + used, size - used, "\"start_timestamp\":%" PRIu64 ",", session_get_timestamp(sess, SESSION_TIMESTAMP_START));
used += snprintf(buff + used, size - used, "\"last_timestamp\":%" PRIu64 ",", session_get_timestamp(sess, SESSION_TIMESTAMP_LAST));
used += snprintf(buff + used, size - used, "\"tuple\":\"%s\",", session_get_tuple_str(sess));
used += snprintf(buff + used, size - used, "\"type\":\"%s\",", session_type_to_str(session_get_type(sess)));
used += snprintf(buff + used, size - used, "\"state\":\"%s\",", session_state_to_str(session_get_state(sess)));
used += snprintf(buff + used, size - used, "\"closing_reason\":\"%s\",", closing_reason_to_str(session_get_closing_reason(sess)));
used += snprintf(buff + used, size - used, "\"dup_traffic\":%d,", session_has_dup_traffic(sess));
used += snprintf(buff + used, size - used, "\"current_packet\":\"%p\",", session_get_current_packet(sess));
const char *str[] = {"c2s", "s2c"};
enum flow_direction dir[] = {FLOW_DIRECTION_C2S, FLOW_DIRECTION_S2C};
2024-04-11 19:44:02 +08:00
for (int i = 0; i < 2; i++)
2024-04-03 18:59:46 +08:00
{
2024-04-11 19:44:02 +08:00
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
used += snprintf(buff + used, size - used, "\"%s_tcp_last_seq\":%u,", str[i], sess->tcp_halfs[dir[i]].seq);
used += snprintf(buff + used, size - used, "\"%s_tcp_last_ack\":%u,", str[i], sess->tcp_halfs[dir[i]].ack);
memset(flags, 0, sizeof(flags));
tcp_flags_to_str(sess->tcp_halfs[dir[i]].flags, flags, sizeof(flags));
used += snprintf(buff + used, size - used, "\"%s_tcp_flags\":\"%s\",", str[i], flags);
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_RX));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_RX));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_expired\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_EXPIRED));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_expired\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_EXPIRED));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_overlap\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_OVERLAP));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_overlap\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_OVERLAP));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_nospace\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_NOSPACE));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_nospace\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_NOSPACE));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_inorder\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_INORDER));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_inorder\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_INORDER));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_reordered\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_REORDERED));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_reordered\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_REORDERED));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_buffered\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_BUFFERED));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_buffered\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_BUFFERED));
used += snprintf(buff + used, size - used, "\"%s_tcp_segs_released\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_SEGS_RELEASED));
used += snprintf(buff + used, size - used, "\"%s_tcp_plds_released\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_TCP_PLDS_RELEASED));
}
used += snprintf(buff + used, size - used, "\"%s_1st_pkt\":\"%p\",", str[i], session_get_first_packet(sess, dir[i]));
2024-04-11 19:44:02 +08:00
used += snprintf(buff + used, size - used, "\"%s_raw_pkts_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_PKTS_RX));
used += snprintf(buff + used, size - used, "\"%s_raw_bytes_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_BYTES_RX));
used += snprintf(buff + used, size - used, "\"%s_raw_pkts_tx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_PKTS_TX));
used += snprintf(buff + used, size - used, "\"%s_raw_bytes_tx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_BYTES_TX));
used += snprintf(buff + used, size - used, "\"%s_raw_pkts_drop\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_PKTS_DROP));
used += snprintf(buff + used, size - used, "\"%s_raw_bytes_drop\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_RAW_BYTES_DROP));
used += snprintf(buff + used, size - used, "\"%s_dup_pkts_bypass\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_DUP_PKTS_BYPASS));
used += snprintf(buff + used, size - used, "\"%s_dup_bytes_bypass\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_DUP_BYTES_BYPASS));
used += snprintf(buff + used, size - used, "\"%s_ctrl_pkts_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CTRL_PKTS_RX));
used += snprintf(buff + used, size - used, "\"%s_ctrl_bytes_rx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CTRL_BYTES_RX));
used += snprintf(buff + used, size - used, "\"%s_ctrl_pkts_tx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CTRL_PKTS_TX));
used += snprintf(buff + used, size - used, "\"%s_ctrl_bytes_tx\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CTRL_BYTES_TX));
used += snprintf(buff + used, size - used, "\"%s_ctrl_pkts_drop\":%" PRIu64 ",", str[i], session_get_stat(sess, dir[i], STAT_CTRL_PKTS_DROP));
used += snprintf(buff + used, size - used, "\"%s_ctrl_bytes_drop\":%" PRIu64 "", str[i], session_get_stat(sess, dir[i], STAT_CTRL_BYTES_DROP));
if (i == 0)
{
used += snprintf(buff + used, size - used, ",");
}
2024-04-03 18:59:46 +08:00
}
2024-04-11 19:44:02 +08:00
used += snprintf(buff + used, size - used, "}");
return used;
2024-04-03 18:59:46 +08:00
}
2024-04-11 19:44:02 +08:00
void session_print(struct session *sess)
{
char buff[4096];
session_to_json(sess, buff, sizeof(buff));
printf("%s\n", buff);
}