add PKT_TAG_VAL_SESS_RAWPKT and PKT_TAG_VAL_SESS_PSEUDOPKT; add session debugger module

This commit is contained in:
luwenpeng
2024-11-27 16:53:15 +08:00
parent 4e6f89dfe6
commit 39948a23ad
14 changed files with 239 additions and 224 deletions

View File

@@ -73,3 +73,5 @@
cli_request_timeout = 3 # second cli_request_timeout = 3 # second
pktdump_task_max_num = 3 pktdump_task_max_num = 3
[session_debugger]
enable = 0 # range: [0, 1]

View File

@@ -19,18 +19,22 @@ extern "C"
#define PKT_TAG_VAL_SESS_NEW 1 << 0 #define PKT_TAG_VAL_SESS_NEW 1 << 0
#define PKT_TAG_VAL_SESS_FREE 1 << 1 #define PKT_TAG_VAL_SESS_FREE 1 << 1
#define PKT_TAG_VAL_SESS_FLAG 1 << 2 #define PKT_TAG_VAL_SESS_RAWPKT 1 << 2
#define PKT_TAG_VAL_SESS_TCP_STREAM 1 << 3 #define PKT_TAG_VAL_SESS_PSEUDOPKT 1 << 3
#define PKT_TAG_VAL_SESS_CTRL_MSG 1 << 4 #define PKT_TAG_VAL_SESS_FLAG 1 << 4
#define PKT_TAG_VAL_SESS_TCP_STREAM 1 << 5
#define PKT_TAG_VAL_SESS_ALL (PKT_TAG_VAL_SESS_NEW | PKT_TAG_VAL_SESS_FREE | PKT_TAG_VAL_SESS_RAWPKT | PKT_TAG_VAL_SESS_PSEUDOPKT | PKT_TAG_VAL_SESS_FLAG | PKT_TAG_VAL_SESS_TCP_STREAM)
#define PKT_TAG_VAL_IPPROTO_TCP 1 << 5 #define PKT_TAG_VAL_IPPROTO_TCP 1 << 10
#define PKT_TAG_VAL_IPPROTO_UDP 1 << 6 #define PKT_TAG_VAL_IPPROTO_UDP 1 << 11
#define PKT_TAG_VAL_IPPROTO_ICMP 1 << 7 #define PKT_TAG_VAL_IPPROTO_ICMP 1 << 12
#define PKT_TAG_VAL_IPPROTO_ALL (PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP | PKT_TAG_VAL_IPPROTO_ICMP)
#define PKT_TAG_VAL_L7PROTO_SSL 1 << 8 #define PKT_TAG_VAL_L7PROTO_SSL 1 << 21
#define PKT_TAG_VAL_L7PROTO_DNS 1 << 9 #define PKT_TAG_VAL_L7PROTO_DNS 1 << 22
#define PKT_TAG_VAL_L7PROTO_HTTP 1 << 10 #define PKT_TAG_VAL_L7PROTO_HTTP 1 << 23
#define PKT_TAG_VAL_L7PROTO_QUIC 1 << 11 #define PKT_TAG_VAL_L7PROTO_QUIC 1 << 24
#define PKT_TAG_VAL_L7PROTO_ALL (PKT_TAG_VAL_L7PROTO_SSL | PKT_TAG_VAL_L7PROTO_DNS | PKT_TAG_VAL_L7PROTO_HTTP | PKT_TAG_VAL_L7PROTO_QUIC)
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -146,14 +146,19 @@ struct session *session_manager_lookup_session_by_packet(struct session_manager
struct session *session_manager_lookup_session_by_id(struct session_manager *sess_mgr, uint16_t thread_id, uint64_t sess_id); struct session *session_manager_lookup_session_by_id(struct session_manager *sess_mgr, uint16_t thread_id, uint64_t sess_id);
void session_manager_discard_session(struct session_manager *sess_mgr, uint16_t thread_id, struct session *sess); void session_manager_discard_session(struct session_manager *sess_mgr, uint16_t thread_id, struct session *sess);
void session_manager_on_packet_forward(struct packet *pkt, struct module *mod);
void session_manager_on_packet_output(struct packet *pkt, struct module *mod);
struct module *session_manager_on_init(struct module_manager *mod_mgr); struct module *session_manager_on_init(struct module_manager *mod_mgr);
void session_manager_on_exit(struct module_manager *mod_mgr, struct module *mod); void session_manager_on_exit(struct module_manager *mod_mgr, struct module *mod);
struct module *session_manager_on_thread_init(struct module_manager *mod_mgr, int thread_id, struct module *mod); struct module *session_manager_on_thread_init(struct module_manager *mod_mgr, int thread_id, struct module *mod);
void session_manager_on_thread_exit(struct module_manager *mod_mgr, int thread_id, struct module *mod); void session_manager_on_thread_exit(struct module_manager *mod_mgr, int thread_id, struct module *mod);
void session_manager_on_packet_forward(struct packet *pkt, struct module *mod);
void session_manager_on_packet_output(struct packet *pkt, struct module *mod);
#define SESSION_DEBUGGER_MODULE_NAME "session_debugger_module"
struct module *session_debugger_on_init(struct module_manager *mod_mgr);
void session_debugger_on_exit(struct module_manager *mod_mgr, struct module *mod);
void session_debugger_on_packet_forward(struct packet *pkt, struct module *mod);
struct module *session_monitor_on_init(struct module_manager *mod_mgr); struct module *session_monitor_on_init(struct module_manager *mod_mgr);
void session_monitor_on_exit(struct module_manager *mod_mgr, struct module *mod); void session_monitor_on_exit(struct module_manager *mod_mgr, struct module *mod);

View File

@@ -255,6 +255,32 @@ struct packet *packet_manager_egress(struct packet_manager *pkt_mgr, uint16_t th
} }
} }
static void packet_tag_set_ip_proto(struct packet *pkt)
{
uint64_t pkt_tag_key_bits = 0;
packet_tag_get(pkt, &pkt_tag_key_bits, NULL);
if (pkt_tag_key_bits & PKT_TAG_KEY_IPPROTO)
{
return;
}
switch (packet_get_ip_proto(pkt))
{
case IPPROTO_TCP:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP);
break;
case IPPROTO_UDP:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_UDP);
break;
case IPPROTO_ICMP: /* fall through */
case IPPROTO_ICMPV6:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_ICMP);
break;
default:
break;
}
}
void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id) void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id)
{ {
uint64_t pkt_tag_key_bits = 0; uint64_t pkt_tag_key_bits = 0;
@@ -272,22 +298,7 @@ void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id)
while ((pkt = TAILQ_FIRST(&pkt_mgr_rte->queue[pkt_mgr_rte->curr_stage]))) while ((pkt = TAILQ_FIRST(&pkt_mgr_rte->queue[pkt_mgr_rte->curr_stage])))
{ {
packet_set_stage(pkt, pkt_mgr_rte->curr_stage); packet_set_stage(pkt, pkt_mgr_rte->curr_stage);
switch (packet_get_ip_proto(pkt)) packet_tag_set_ip_proto(pkt); // schedule packet may not set ip proto tag, so we need to set it here
{
case IPPROTO_TCP:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP);
break;
case IPPROTO_UDP:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_UDP);
break;
case IPPROTO_ICMP: /* fall through */
case IPPROTO_ICMPV6:
packet_tag_set(pkt, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_ICMP);
break;
default:
break;
}
packet_set_claim(pkt, false); packet_set_claim(pkt, false);
pkt_mgr_rte->claim_cb = NULL; pkt_mgr_rte->claim_cb = NULL;
pkt_mgr_rte->claim_arg = NULL; pkt_mgr_rte->claim_arg = NULL;

View File

@@ -976,11 +976,15 @@ void packet_tag_set(struct packet *pkt, uint64_t key_bits, uint64_t val_bits)
void packet_tag_get(const struct packet *pkt, uint64_t *key_bits, uint64_t *val_bits) void packet_tag_get(const struct packet *pkt, uint64_t *key_bits, uint64_t *val_bits)
{ {
*key_bits = 0; if (key_bits)
*val_bits = 0; {
*key_bits = pkt->tag_key_bits;
}
*key_bits = pkt->tag_key_bits; if (val_bits)
*val_bits = pkt->tag_val_bits; {
*val_bits = pkt->tag_val_bits;
}
} }
void packet_set_user_data(struct packet *pkt, void *data) void packet_set_user_data(struct packet *pkt, void *data)

View File

@@ -6,6 +6,7 @@ add_library(session_manager
session_dabloom.c session_dabloom.c
session_transition.c session_transition.c
session_monitor.c session_monitor.c
session_debugger.c
session_manager.c session_manager.c
session_manager_cfg.c session_manager_cfg.c
session_manager_rte.c session_manager_rte.c

View File

@@ -160,7 +160,7 @@ static void session_debugger_exdata_free(struct session_debugger_exdata *exdata)
} }
} }
static void session_debugger_exdata_free_callback(int idx, void *ex_ptr, void *arg) static void session_debugger_exdata_free_cb(int idx, void *ex_ptr, void *arg)
{ {
__attribute__((unused)) struct session_debugger *dbg = (struct session_debugger *)arg; __attribute__((unused)) struct session_debugger *dbg = (struct session_debugger *)arg;
assert(idx == dbg->sess_exdata_idx); assert(idx == dbg->sess_exdata_idx);
@@ -168,9 +168,14 @@ static void session_debugger_exdata_free_callback(int idx, void *ex_ptr, void *a
session_debugger_exdata_free((struct session_debugger_exdata *)ex_ptr); session_debugger_exdata_free((struct session_debugger_exdata *)ex_ptr);
} }
static void on_session_closed(struct session *sess, void *arg) static void session_debugger_on_session_new(struct session_debugger *dbg, struct session *sess)
{
struct session_debugger_exdata *exdata = session_debugger_exdata_new(dbg, sess);
session_set_exdata(sess, dbg->sess_exdata_idx, exdata);
}
static void session_debugger_on_session_free(struct session_debugger *dbg, struct session *sess)
{ {
struct session_debugger *dbg = (struct session_debugger *)arg;
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx); struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
char buff[PATH_MAX] = {0}; char buff[PATH_MAX] = {0};
@@ -201,85 +206,12 @@ static void on_session_closed(struct session *sess, void *arg)
session_debugger_log(exdata->dbg->fd, "session %lu %s statistics:\n%s", session_get_id(exdata->sess), session_get_readable_addr(exdata->sess), buff); session_debugger_log(exdata->dbg->fd, "session %lu %s statistics:\n%s", session_get_id(exdata->sess), session_get_readable_addr(exdata->sess), buff);
} }
static void on_session_message(struct session *sess, enum session_state state, struct packet *pkt, void *arg) static void session_debugger_on_tcp_payload(struct session_debugger *dbg, struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len)
{ {
struct session_debugger *dbg = (struct session_debugger *)arg;
if (state == SESSION_STATE_CLOSED)
{
on_session_closed(sess, dbg);
assert(pkt == NULL);
return;
}
int is_pseudo = (packet_get_type(pkt) == PACKET_TYPE_PSEUDO);
char buff[PATH_MAX];
enum flow_type flow = session_get_flow_type(sess); enum flow_type flow = session_get_flow_type(sess);
assert(flow == FLOW_TYPE_C2S || flow == FLOW_TYPE_S2C);
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx); struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
if (exdata == NULL)
{
exdata = session_debugger_exdata_new(dbg, sess);
session_set_exdata(sess, dbg->sess_exdata_idx, exdata);
}
if (flow == FLOW_TYPE_C2S)
{
if (is_pseudo)
{
exdata->c2s_rx_ctrl_pkts++;
exdata->c2s_rx_ctrl_bytes += packet_get_raw_len(pkt);
}
else
{
exdata->c2s_rx_data_pkts++;
exdata->c2s_rx_data_bytes += packet_get_raw_len(pkt);
}
}
else
{
if (is_pseudo)
{
exdata->s2c_rx_ctrl_pkts++;
exdata->s2c_rx_ctrl_bytes += packet_get_raw_len(pkt);
}
else
{
exdata->s2c_rx_data_pkts++;
exdata->s2c_rx_data_bytes += packet_get_raw_len(pkt);
}
}
memset(buff, 0, sizeof(buff));
session_to_str(sess, 1, buff, sizeof(buff) - 1);
session_debugger_log(dbg->fd, "on %s %s packet: %s", session_type_to_str(session_get_type(sess)), (is_pseudo ? "pseudo" : "data"), buff);
memset(buff, 0, sizeof(buff));
packet_dump_str(pkt, buff, sizeof(buff) - 1);
session_debugger_log(dbg->fd, "rx %s %s packet\n%s", session_type_to_str(session_get_type(sess)), (is_pseudo ? "pseudo" : "data"), buff);
pthread_spin_lock(&dbg->lock);
packet_dump_hex(pkt, dbg->fd);
pthread_spin_unlock(&dbg->lock);
}
static void on_tcp_payload_message(struct session *sess, enum session_state state, const char *tcp_payload, uint32_t tcp_payload_len, void *arg)
{
struct session_debugger *dbg = (struct session_debugger *)arg;
if (state == SESSION_STATE_CLOSED)
{
assert(tcp_payload == NULL);
assert(tcp_payload_len == 0);
return;
}
char buff[PATH_MAX]; char buff[PATH_MAX];
enum flow_type flow = session_get_flow_type(sess);
assert(flow == FLOW_TYPE_C2S || flow == FLOW_TYPE_S2C);
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
assert(exdata);
memset(buff, 0, sizeof(buff)); memset(buff, 0, sizeof(buff));
session_to_str(sess, 1, buff, sizeof(buff) - 1); session_to_str(sess, 1, buff, sizeof(buff) - 1);
session_debugger_log(dbg->fd, "on TCP stream: %s", buff); session_debugger_log(dbg->fd, "on TCP stream: %s", buff);
@@ -306,22 +238,12 @@ static void on_tcp_payload_message(struct session *sess, enum session_state stat
pthread_spin_unlock(&dbg->lock); pthread_spin_unlock(&dbg->lock);
} }
static void on_udp_payload_message(struct session *sess, enum session_state state, struct packet *pkt, void *arg) static void session_debugger_on_udp_payload(struct session_debugger *dbg, struct session *sess, struct packet *pkt)
{ {
struct session_debugger *dbg = (struct session_debugger *)arg; enum flow_type flow = session_get_flow_type(sess);
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
if (state == SESSION_STATE_CLOSED)
{
assert(pkt == NULL);
return;
}
char buff[PATH_MAX]; char buff[PATH_MAX];
enum flow_type flow = session_get_flow_type(sess);
assert(flow == FLOW_TYPE_C2S || flow == FLOW_TYPE_S2C);
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
assert(exdata);
const char *udp_payload = packet_get_payload_data(pkt); const char *udp_payload = packet_get_payload_data(pkt);
uint32_t udp_payload_len = packet_get_payload_len(pkt); uint32_t udp_payload_len = packet_get_payload_len(pkt);
if (udp_payload_len == 0) if (udp_payload_len == 0)
@@ -353,6 +275,100 @@ static void on_udp_payload_message(struct session *sess, enum session_state stat
pthread_spin_unlock(&dbg->lock); pthread_spin_unlock(&dbg->lock);
} }
void session_debugger_on_packet_forward(struct packet *pkt, struct module *mod)
{
struct session_debugger *dbg = module_get_ctx(mod);
assert(dbg);
struct session_manager *sess_mgr = dbg->sess_mgr;
struct session *sess = packet_exdata_to_session(sess_mgr, pkt);
if (sess == NULL)
{
assert(0);
return;
}
uint64_t pkt_tag_key = 0;
uint64_t pkt_tag_val = 0;
packet_tag_get(pkt, &pkt_tag_key, &pkt_tag_val);
if (!(pkt_tag_key & PKT_TAG_KEY_SESS))
{
assert(0);
return;
}
if (pkt_tag_val & PKT_TAG_VAL_SESS_FREE)
{
session_debugger_on_session_free(dbg, sess);
return;
}
if (pkt_tag_val & PKT_TAG_VAL_SESS_NEW)
{
session_debugger_on_session_new(dbg, sess);
}
struct session_debugger_exdata *exdata = (struct session_debugger_exdata *)session_get_exdata(sess, dbg->sess_exdata_idx);
enum flow_type flow = session_get_flow_type(sess);
assert(flow == FLOW_TYPE_C2S || flow == FLOW_TYPE_S2C);
int is_pseudo = (packet_get_type(pkt) == PACKET_TYPE_PSEUDO);
if (flow == FLOW_TYPE_C2S)
{
if (is_pseudo)
{
exdata->c2s_rx_ctrl_pkts++;
exdata->c2s_rx_ctrl_bytes += packet_get_raw_len(pkt);
}
else
{
exdata->c2s_rx_data_pkts++;
exdata->c2s_rx_data_bytes += packet_get_raw_len(pkt);
}
}
else
{
if (is_pseudo)
{
exdata->s2c_rx_ctrl_pkts++;
exdata->s2c_rx_ctrl_bytes += packet_get_raw_len(pkt);
}
else
{
exdata->s2c_rx_data_pkts++;
exdata->s2c_rx_data_bytes += packet_get_raw_len(pkt);
}
}
char buff[PATH_MAX];
memset(buff, 0, sizeof(buff));
session_to_str(sess, 1, buff, sizeof(buff) - 1);
session_debugger_log(dbg->fd, "on %s %s packet: %s", session_type_to_str(session_get_type(sess)), (is_pseudo ? "pseudo" : "data"), buff);
memset(buff, 0, sizeof(buff));
packet_dump_str(pkt, buff, sizeof(buff) - 1);
session_debugger_log(dbg->fd, "rx %s %s packet\n%s", session_type_to_str(session_get_type(sess)), (is_pseudo ? "pseudo" : "data"), buff);
pthread_spin_lock(&dbg->lock);
packet_dump_hex(pkt, dbg->fd);
pthread_spin_unlock(&dbg->lock);
if (session_get_type(sess) == SESSION_TYPE_TCP)
{
struct tcp_segment *seg = packet_exdata_to_tcp_segment(sess_mgr, pkt);
while (seg)
{
session_debugger_on_tcp_payload(dbg, sess, seg->data, seg->len);
seg = seg->next;
}
}
else
{
session_debugger_on_udp_payload(dbg, sess, pkt);
}
}
static void session_debugger_free(struct session_debugger *dbg) static void session_debugger_free(struct session_debugger *dbg)
{ {
if (dbg) if (dbg)
@@ -386,39 +402,13 @@ static struct session_debugger *session_debugger_new(struct session_manager *ses
goto error_out; goto error_out;
} }
dbg->sess_exdata_idx = session_manager_new_session_exdata_index(dbg->sess_mgr, "session_debugger_exdata", session_debugger_exdata_free_callback, dbg); dbg->sess_exdata_idx = session_manager_new_session_exdata_index(dbg->sess_mgr, "session_debugger_exdata", session_debugger_exdata_free_cb, dbg);
if (dbg->sess_exdata_idx == -1) if (dbg->sess_exdata_idx == -1)
{ {
session_debugger_log(STDERR_FILENO, "new session exdata index failed\n"); session_debugger_log(STDERR_FILENO, "new session exdata index failed\n");
goto error_out; goto error_out;
} }
if (session_manager_subscribe_tcp(sess_mgr, on_session_message, dbg) == -1)
{
session_debugger_log(STDERR_FILENO, "subscribe tcp failed\n");
goto error_out;
}
if (session_manager_subscribe_udp(sess_mgr, on_session_message, dbg) == -1)
{
session_debugger_log(STDERR_FILENO, "subscribe udp failed\n");
goto error_out;
}
if (session_manager_subscribe_control_packet(sess_mgr, on_session_message, dbg) == -1)
{
session_debugger_log(STDERR_FILENO, "subscribe control packet failed\n");
goto error_out;
}
if (session_manager_subscribe_tcp_stream(sess_mgr, on_tcp_payload_message, dbg) == -1)
{
session_debugger_log(STDERR_FILENO, "subscribe tcp stream failed\n");
goto error_out;
}
if (session_manager_subscribe_udp(sess_mgr, on_udp_payload_message, dbg) == -1)
{
session_debugger_log(STDERR_FILENO, "subscribe udp failed\n");
goto error_out;
}
return dbg; return dbg;
error_out: error_out:
@@ -432,12 +422,22 @@ error_out:
struct module *session_debugger_on_init(struct module_manager *mod_mgr) struct module *session_debugger_on_init(struct module_manager *mod_mgr)
{ {
assert(mod_mgr); struct session_manager *sess_mgr = module_to_session_manager(module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME));
struct module *sess_mgr_mod = module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME);
struct session_manager *sess_mgr = module_to_session_manager(sess_mgr_mod);
assert(sess_mgr); assert(sess_mgr);
struct logger *logger = module_manager_get_logger(mod_mgr); struct logger *logger = module_manager_get_logger(mod_mgr);
assert(logger); assert(logger);
const char *toml = module_manager_get_toml_path(mod_mgr);
assert(toml);
uint64_t enable = 0;
if (load_toml_integer_config(toml, "session_debugger.enable", &enable, 0, 1) != 0)
{
return NULL;
}
if (enable == 0)
{
return NULL;
}
struct session_debugger *dbg = session_debugger_new(sess_mgr, logger); struct session_debugger *dbg = session_debugger_new(sess_mgr, logger);
if (dbg == NULL) if (dbg == NULL)
@@ -445,13 +445,12 @@ struct module *session_debugger_on_init(struct module_manager *mod_mgr)
return NULL; return NULL;
} }
struct module *dbg_mod = module_new("session_debugger", NULL); struct module *dbg_mod = module_new(SESSION_DEBUGGER_MODULE_NAME, dbg);
if (dbg_mod == NULL) if (dbg_mod == NULL)
{ {
session_debugger_free(dbg); session_debugger_free(dbg);
return NULL; return NULL;
} }
module_set_ctx(dbg_mod, dbg);
STELLAR_LOG_FATAL(dbg->logger, "session debugger", "session_debugger init") STELLAR_LOG_FATAL(dbg->logger, "session debugger", "session_debugger init")

View File

@@ -81,6 +81,7 @@ static void notify_sess_closed_by_pseudo_pkt(struct session_manager *sess_mgr, i
packet_tag_set(pseudo, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_FREE); packet_tag_set(pseudo, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_FREE);
packet_set_type(pseudo, PACKET_TYPE_PSEUDO); packet_set_type(pseudo, PACKET_TYPE_PSEUDO);
packet_set_action(pseudo, PACKET_ACTION_DROP); packet_set_action(pseudo, PACKET_ACTION_DROP);
packet_set_exdata(pseudo, sess_mgr->pkt_ex_to_get_sess, sess);
packet_set_exdata(pseudo, sess_mgr->pkt_ex_to_free_sess, sess); packet_set_exdata(pseudo, sess_mgr->pkt_ex_to_free_sess, sess);
packet_manager_schedule_packet(pkt_mgr, thread_id, pseudo, PACKET_STAGE_FORWARD); 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); SESSION_MANAGER_LOG_INFO("notify session %lu %s closed by pseudo packet: %p", session_get_id(sess), session_get_readable_addr(sess), pseudo);
@@ -145,29 +146,13 @@ void session_manager_on_packet_forward(struct packet *pkt, struct module *mod)
* Note: Modifying the system time will affect the timing wheel, impacting session expiration, and TCP reassembly expiration. * Note: Modifying the system time will affect the timing wheel, impacting session expiration, and TCP reassembly expiration.
* Suggestion: After modifying the system time, restart the service to ensure consistent timing. * Suggestion: After modifying the system time, restart the service to ensure consistent timing.
*/ */
uint64_t now_ms = clock_get_real_time_ms(); uint64_t now_ms = 0;
struct tuple6 key; struct tuple6 key;
struct session *sess = session_manager_rte_lookup_session_by_packet(sess_mgr_rte, pkt); struct session *sess = NULL;
if (sess == NULL) if (packet_get_type(pkt) == PACKET_TYPE_PSEUDO)
{ {
if (packet_get_type(pkt) == PACKET_TYPE_RAW) sess = packet_exdata_to_session(sess_mgr, pkt);
{ if (sess || (sess = session_manager_rte_lookup_session_by_packet(sess_mgr_rte, pkt)))
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));
packet_tag_set(pkt, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_NEW);
}
}
else
{
// TODO new session by pseudo packet
}
}
else
{
if (packet_get_type(pkt) == PACKET_TYPE_PSEUDO)
{ {
session_set_current_packet(sess, pkt); session_set_current_packet(sess, pkt);
packet_get_innermost_tuple6(pkt, &key); packet_get_innermost_tuple6(pkt, &key);
@@ -183,16 +168,34 @@ void session_manager_on_packet_forward(struct packet *pkt, struct module *mod)
sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_PACKETS_RECEIVED]++; sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_PACKETS_RECEIVED]++;
sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_BYTES_RECEIVED] += packet_get_raw_len(pkt); sess->stats[FLOW_TYPE_S2C][STAT_PSEUDO_BYTES_RECEIVED] += packet_get_raw_len(pkt);
} }
packet_tag_set(pkt, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_CTRL_MSG); packet_tag_set(pkt, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_PSEUDOPKT);
packet_set_exdata(pkt, sess_mgr->pkt_ex_to_get_sess, sess);
} }
else else
{ {
session_manager_rte_update_session(sess_mgr_rte, sess, pkt, now_ms); // TODO new session by pseudo packet
packet_tag_set(pkt, PKT_TAG_KEY_SESS, 0); }
}
else
{
now_ms = clock_get_real_time_ms();
sess = session_manager_rte_lookup_session_by_packet(sess_mgr_rte, pkt);
if (sess || (sess = session_manager_rte_new_session(sess_mgr_rte, pkt, now_ms)))
{
if (session_get_user_data(sess) == NULL)
{
session_set_user_data(sess, exdata_runtime_new(sess_mgr->ex_sche));
packet_tag_set(pkt, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_NEW);
}
session_manager_rte_update_session(sess_mgr_rte, sess, pkt, now_ms);
packet_tag_set(pkt, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_RAWPKT);
packet_set_exdata(pkt, sess_mgr->pkt_ex_to_get_sess, sess);
}
else
{
// miss sess
} }
} }
packet_set_exdata(pkt, sess_mgr->pkt_ex_to_get_sess, sess);
if (sess && session_get_type(sess) == SESSION_TYPE_TCP) if (sess && session_get_type(sess) == SESSION_TYPE_TCP)
{ {

View File

@@ -132,13 +132,13 @@ static void stellar_thread_join(struct stellar *st)
#include "stellar/lpi_plus.h" #include "stellar/lpi_plus.h"
struct module_hooks mod_hooks[] = { struct module_hooks mod_hooks[] = {
{monitor_on_init, monitor_on_exit, NULL, NULL}, {monitor_on_init, monitor_on_exit, NULL, NULL},
{packet_manager_on_init, packet_manager_on_exit, packet_manager_on_thread_init, packet_manager_on_thread_exit}, {packet_manager_on_init, packet_manager_on_exit, packet_manager_on_thread_init, packet_manager_on_thread_exit},
{session_manager_on_init, session_manager_on_exit, session_manager_on_thread_init, session_manager_on_thread_exit}, {session_manager_on_init, session_manager_on_exit, session_manager_on_thread_init, session_manager_on_thread_exit},
{session_monitor_on_init, session_monitor_on_exit, NULL, NULL}, {session_debugger_on_init, session_debugger_on_exit, NULL, NULL},
{lpi_plus_init, lpi_plus_exit, NULL, NULL}, {session_monitor_on_init, session_monitor_on_exit, NULL, NULL},
}; {lpi_plus_init, lpi_plus_exit, NULL, NULL},
};
struct packet_node_spec struct packet_node_spec
{ {
@@ -150,36 +150,38 @@ struct packet_node_spec
on_packet_callback *cb; on_packet_callback *cb;
}; };
struct packet_node_spec packet_nodes[] = { struct packet_node_spec packet_nodes[] = {
{SESSION_MANAGER_MODULE_NAME, "session_manager",PACKET_STAGE_FORWARD, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, session_manager_on_packet_forward}, // PACKET_STAGE_FORWARD
{SESSION_MANAGER_MODULE_NAME, "session_manager",PACKET_STAGE_OUTPUT, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, session_manager_on_packet_output}, {SESSION_MANAGER_MODULE_NAME, "session_manager", PACKET_STAGE_FORWARD, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, session_manager_on_packet_forward},
{LPI_PLUS_MODULE_NAME, "lpi_plus",PACKET_STAGE_FORWARD, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, lpi_plus_on_packet}, {SESSION_DEBUGGER_MODULE_NAME, "session_debugger", PACKET_STAGE_FORWARD, PKT_TAG_KEY_SESS, PKT_TAG_VAL_SESS_ALL, session_debugger_on_packet_forward},
{LPI_PLUS_MODULE_NAME, "lpi_plus", PACKET_STAGE_FORWARD, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, lpi_plus_on_packet},
// PACKET_STAGE_OUTPUT
{SESSION_MANAGER_MODULE_NAME, "session_manager", PACKET_STAGE_OUTPUT, PKT_TAG_KEY_IPPROTO, PKT_TAG_VAL_IPPROTO_TCP | PKT_TAG_VAL_IPPROTO_UDP, session_manager_on_packet_output},
}; };
static int register_packet_node_for_module(struct module_manager *mod_mgr, struct packet_node_spec *specs, size_t n_specs) static int register_packet_node_for_module(struct module_manager *mod_mgr, struct packet_node_spec *specs, size_t n_specs)
{ {
struct module *pkt_mgr_mod = module_manager_get_module(mod_mgr, PACKET_MANAGER_MODULE_NAME); 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); struct packet_manager *pkt_mgr = module_to_packet_manager(pkt_mgr_mod);
struct module *mod = NULL; struct module *mod = NULL;
for(size_t i=0; i < n_specs; i++) for (size_t i = 0; i < n_specs; i++)
{ {
mod= module_manager_get_module(mod_mgr, specs[i].module_name); mod = module_manager_get_module(mod_mgr, specs[i].module_name);
if(mod == NULL) if (mod == NULL)
{ {
CORE_LOG_FATAL("%s unable to get module %s", __FUNCTION__, specs[i].module_name); CORE_LOG_FATAL("%s unable to get module %s", __FUNCTION__, specs[i].module_name);
continue; continue;
} }
if(packet_manager_register_node(pkt_mgr, if (packet_manager_register_node(pkt_mgr,
specs[i].node_name, specs[i].node_name,
specs[i].stage, specs[i].stage,
specs[i].interested_tag_key_bits, specs[i].interested_tag_key_bits,
specs[i].interested_tag_val_bits, specs[i].interested_tag_val_bits,
specs[i].cb, specs[i].cb,
mod)<0) mod) < 0)
{ {
CORE_LOG_FATAL("%s failed to register node:%s for module:%s in stage:%d", __FUNCTION__, specs[i].node_name, specs[i].module_name, specs[i].stage); CORE_LOG_FATAL("%s failed to register node:%s for module:%s in stage:%d", __FUNCTION__, specs[i].node_name, specs[i].module_name, specs[i].stage);
} }
@@ -221,7 +223,7 @@ struct stellar *stellar_new(const char *toml_file)
goto error_out; goto error_out;
} }
st->mod_mgr = module_manager_new(mod_hooks, count_of(mod_hooks),st->thread_num, toml_file, st->logger); st->mod_mgr = module_manager_new(mod_hooks, count_of(mod_hooks), st->thread_num, toml_file, st->logger);
if (st->mod_mgr == NULL) if (st->mod_mgr == NULL)
{ {
CORE_LOG_ERROR("unable to create packet manager"); CORE_LOG_ERROR("unable to create packet manager");

View File

@@ -61,6 +61,9 @@ global:
packet_exdata_to_session; packet_exdata_to_session;
packet_exdata_to_tcp_segment; packet_exdata_to_tcp_segment;
session_debugger_on_init;
session_debugger_on_exit;
session_monitor_on_init; session_monitor_on_init;
session_monitor_on_exit; session_monitor_on_exit;

View File

@@ -1,7 +1,7 @@
#add_subdirectory(packet_inject) #add_subdirectory(packet_inject)
add_subdirectory(packet_tool) add_subdirectory(packet_tool)
#add_subdirectory(session_debugger)
add_subdirectory(lpi_plus) add_subdirectory(lpi_plus)
#add_subdirectory(lpi_plus)
#add_subdirectory(decoders/http) #add_subdirectory(decoders/http)
#add_subdirectory(decoders/socks) #add_subdirectory(decoders/socks)
#add_subdirectory(decoders/stratum) #add_subdirectory(decoders/stratum)

View File

@@ -1,8 +0,0 @@
# build libsession_debugger.so
add_library(session_debugger SHARED session_debugger.c)
target_link_libraries(session_debugger stellar_lib session_manager packet_manager)
target_include_directories(session_debugger PUBLIC ${CMAKE_SOURCE_DIR}/include/)
target_include_directories(session_debugger PUBLIC ${CMAKE_SOURCE_DIR}/infra)
set_target_properties(session_debugger PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map")
file(COPY spec.toml DESTINATION ./)

View File

@@ -1,4 +0,0 @@
[[module]]
path = "./module/libsession_debugger.so"
init = "session_debugger_on_init"
exit = "session_debugger_on_exit"

View File

@@ -1,7 +0,0 @@
LIBSESSION_DEBUGGER {
global:
session_debugger_on_init;
session_debugger_on_exit;
local: *;
};