Refactor TCP reassembly, the session knows where the TCP segment comes from: raw packet or tcp segment queue

This commit is contained in:
luwenpeng
2024-04-02 16:21:39 +08:00
parent a509f0ce3b
commit e8e60cee6d
25 changed files with 678 additions and 1419 deletions

View File

@@ -6,62 +6,35 @@ extern "C"
{
#endif
#include "log.h"
#include <stdint.h>
#define TCP_REASSEMBLE_DEBUG(format, ...) LOG_DEBUG("tcp_reassembly", format, ##__VA_ARGS__)
#define TCP_REASSEMBLE_ERROR(format, ...) LOG_ERROR("tcp_reassembly", format, ##__VA_ARGS__)
#include "log.h"
/*
* If retransmission or overlap occurs, the old data packet may have been consumed by the upper-layer plug-in,
* so the old data packet takes priority and the new data packet will be bypassed.
*/
#define TCP_REASSEMBLY_LOG_DEBUG(format, ...) LOG_DEBUG("tcp_reassembly", format, ##__VA_ARGS__)
#define TCP_REASSEMBLY_LOG_ERROR(format, ...) LOG_ERROR("tcp_reassembly", format, ##__VA_ARGS__)
struct tcp_reassembly_options
struct tcp_segment
{
uint8_t enable;
uint32_t max_timeout; // range: [1, 60000]
uint32_t max_segments; // 0: unlimited
uint32_t max_bytes; // 0: unlimited
uint32_t len;
const void *data;
};
struct tcp_reassembly_stat
{
uint64_t curr_segments;
uint64_t curr_bytes;
struct tcp_segment *tcp_segment_new(uint32_t seq, const void *data, uint32_t len);
void tcp_segment_free(struct tcp_segment *seg);
uint64_t insert_segments;
uint64_t insert_bytes;
struct tcp_reassembly *tcp_reassembly_new(uint64_t max_timeout, uint64_t max_seg_num);
void tcp_reassembly_free(struct tcp_reassembly *assembler);
uint64_t remove_segments;
uint64_t remove_bytes;
// return: 1: success (seg overlap)
// return: 0: success
// return: -1: failed (no space)
int tcp_reassembly_push(struct tcp_reassembly *assembler, struct tcp_segment *seg, uint64_t now);
struct tcp_segment *tcp_reassembly_pop(struct tcp_reassembly *assembler);
struct tcp_segment *tcp_reassembly_expire(struct tcp_reassembly *assembler, uint64_t now);
uint64_t consume_segments;
uint64_t consume_bytes;
uint64_t retrans_bypass_segments;
uint64_t retrans_bypass_bytes;
uint64_t overload_bypass_segments;
uint64_t overload_bypass_bytes;
uint64_t timeout_discard_segments;
uint64_t timeout_discard_bytes;
};
struct tcp_reassembly *tcp_reassembly_new(struct tcp_reassembly_options *opts);
void tcp_reassembly_free(struct tcp_reassembly *assy);
void tcp_reassembly_init(struct tcp_reassembly *assy, uint32_t syn_seq);
void tcp_reassembly_expire(struct tcp_reassembly *assy, uint64_t now);
void tcp_reassembly_insert(struct tcp_reassembly *assy, uint32_t seq, const char *payload, uint32_t len, uint64_t now);
const char *tcp_reassembly_peek(struct tcp_reassembly *assy, uint32_t *len);
void tcp_reassembly_consume(struct tcp_reassembly *assy, uint32_t len);
struct tcp_reassembly_stat *tcp_reassembly_get_stat(struct tcp_reassembly *assy);
void tcp_reassembly_print_stat(struct tcp_reassembly *assy);
void tcp_reassembly_inc_recv_next(struct tcp_reassembly *assembler, uint32_t offset);
void tcp_reassembly_set_recv_next(struct tcp_reassembly *assembler, uint32_t seq);
uint32_t tcp_reassembly_get_recv_next(struct tcp_reassembly *assembler);
#ifdef __cpluscplus
}