2024-04-22 20:01:15 +08:00
|
|
|
#ifndef _SESSION_PRIV_H
|
|
|
|
|
#define _SESSION_PRIV_H
|
2024-04-10 11:40:26 +08:00
|
|
|
|
2024-04-21 11:30:41 +08:00
|
|
|
#ifdef __cplusplus
|
2024-04-10 11:40:26 +08:00
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "list.h"
|
2024-04-22 20:01:15 +08:00
|
|
|
#include "packet_priv.h"
|
2024-04-10 11:40:26 +08:00
|
|
|
#include "timeout.h"
|
|
|
|
|
#include "uthash.h"
|
2024-04-22 20:01:15 +08:00
|
|
|
#include "stellar/tuple.h"
|
|
|
|
|
#include "stellar/session.h"
|
2024-04-10 11:40:26 +08:00
|
|
|
#include "tcp_reassembly.h"
|
|
|
|
|
#include "session_manager.h"
|
|
|
|
|
|
2024-04-17 17:53:42 +08:00
|
|
|
#define EX_DATA_MAX_COUNT 4
|
2024-04-10 11:40:26 +08:00
|
|
|
|
|
|
|
|
// tuple6 str format: "src_addr:src_port -> dst_addr:dst_port, proto: ip_proto, domain: domain"
|
|
|
|
|
// tuple6 max len: 46 + 1 + 5 + 4 + 46 + 1 + 5 + 9 + 1 + 10 + 20 = 107
|
|
|
|
|
#define TUPLE6_STR_SIZE 108
|
|
|
|
|
|
|
|
|
|
struct tcp_half
|
|
|
|
|
{
|
|
|
|
|
struct tcp_reassembly *assembler;
|
|
|
|
|
struct tcp_segment in_order;
|
|
|
|
|
|
|
|
|
|
uint32_t seq;
|
|
|
|
|
uint32_t ack;
|
2024-04-25 15:39:02 +08:00
|
|
|
uint16_t len;
|
2024-04-10 11:40:26 +08:00
|
|
|
uint8_t flags;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-17 17:53:42 +08:00
|
|
|
/*
|
|
|
|
|
* sizeof(struct session) = 1024 bytes
|
|
|
|
|
* max thread number = 128
|
|
|
|
|
* per thread max tcp session number = 50000
|
|
|
|
|
* per thread max udp session number = 50000
|
|
|
|
|
*
|
|
|
|
|
* session memory usage = 128 * (50000 + 50000) * 1024 = 13107200000 bytes = 12.2 GB
|
|
|
|
|
*/
|
2024-04-24 11:39:15 +08:00
|
|
|
struct session
|
2024-04-10 11:40:26 +08:00
|
|
|
{
|
2024-04-17 17:53:42 +08:00
|
|
|
uint64_t id; // 8 bytes
|
|
|
|
|
uint64_t stats[MAX_DIRECTION][MAX_STAT]; // 480 bytes
|
|
|
|
|
uint64_t timestamps[MAX_TIMESTAMP]; // 16 bytes
|
|
|
|
|
struct tcp_half tcp_halfs[MAX_DIRECTION]; // 80 bytes
|
|
|
|
|
struct timeout timeout; // 72 bytes -- used for timer
|
|
|
|
|
struct list_head lru; // 16 bytes -- used for lru queue
|
|
|
|
|
struct list_head free; // 16 bytes -- used for free queue
|
|
|
|
|
struct list_head evicte; // 16 bytes -- used for evicte queue
|
2024-04-24 11:39:15 +08:00
|
|
|
UT_hash_handle hh1; // 56 bytes -- used for hash table (tuple6)
|
|
|
|
|
UT_hash_handle hh2; // 56 bytes -- used for hash table (tuple4)
|
|
|
|
|
UT_hash_handle hh3; // 56 bytes -- used for hash table (session id)
|
2024-04-17 17:53:42 +08:00
|
|
|
struct tuple6 tuple; // 56 bytes
|
|
|
|
|
char tuple_str[TUPLE6_STR_SIZE]; // 108 bytes
|
|
|
|
|
const struct packet *first_pkt[MAX_DIRECTION]; // 16 bytes
|
|
|
|
|
const struct packet *curr_pkt; // 8 bytes
|
|
|
|
|
void *ex_data[EX_DATA_MAX_COUNT]; // 32 bytes
|
|
|
|
|
void *user_data; // 8 bytes
|
|
|
|
|
int is_symmetric; // 4 bytes
|
|
|
|
|
int dup; // 4 bytes
|
|
|
|
|
enum session_direction tuple_dir; // 4 bytes
|
|
|
|
|
enum session_direction cur_dir; // 4 bytes
|
|
|
|
|
enum session_type type; // 4 bytes
|
|
|
|
|
enum session_state state; // 4 bytes
|
|
|
|
|
enum closing_reason reason; // 4 bytes
|
|
|
|
|
struct session_manager_stat *mgr_stat; // 8 bytes
|
2024-04-10 11:40:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void session_init(struct session *sess);
|
|
|
|
|
void session_set_id(struct session *sess, uint64_t id);
|
|
|
|
|
void session_set_tuple(struct session *sess, const struct tuple6 *key);
|
|
|
|
|
const struct tuple6 *session_get_tuple(const struct session *sess);
|
|
|
|
|
void session_set_tuple_direction(struct session *sess, enum session_direction dir);
|
|
|
|
|
enum session_direction session_get_tuple_direction(const struct session *sess);
|
|
|
|
|
void session_set_current_direction(struct session *sess, enum session_direction dir);
|
|
|
|
|
void session_set_state(struct session *sess, enum session_state state);
|
|
|
|
|
void session_set_type(struct session *sess, enum session_type type);
|
|
|
|
|
void session_set_dup_traffic(struct session *sess);
|
|
|
|
|
void session_set_closing_reason(struct session *sess, enum closing_reason reason);
|
|
|
|
|
void session_inc_stat(struct session *sess, enum session_direction dir, enum session_stat stat, uint64_t val);
|
|
|
|
|
void session_set_timestamp(struct session *sess, enum session_timestamp type, uint64_t value);
|
|
|
|
|
void session_set_1st_packet(struct session *sess, enum session_direction dir, const struct packet *pkt);
|
|
|
|
|
void session_set_current_packet(struct session *sess, const struct packet *pkt);
|
|
|
|
|
const struct packet *session_get_current_packet(const struct session *sess);
|
|
|
|
|
void session_set_user_data(struct session *sess, void *user_data);
|
|
|
|
|
void *session_get_user_data(const struct session *sess);
|
|
|
|
|
struct tcp_segment *session_get_tcp_segment(struct session *sess);
|
|
|
|
|
void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg);
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* session ex data
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
typedef void session_ex_free_cb(struct session *sess, uint8_t idx, void *ex_ptr, void *arg);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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);
|
|
|
|
|
/*
|
|
|
|
|
* 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);
|
|
|
|
|
void *session_get0_ex_data(const struct session *sess, uint8_t 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);
|
|
|
|
|
void session_free_all_ex_data(struct session *sess);
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* debug
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-04-11 19:44:02 +08:00
|
|
|
void session_print(struct session *sess);
|
|
|
|
|
int session_to_json(struct session *sess, char *buff, int size);
|
2024-04-10 11:40:26 +08:00
|
|
|
|
2024-04-21 11:30:41 +08:00
|
|
|
#ifdef __cplusplus
|
2024-04-10 11:40:26 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|