Update session timeouts

This commit is contained in:
luwenpeng
2024-03-29 16:32:16 +08:00
parent 814a0d739f
commit 6e422ecb8d
34 changed files with 875 additions and 593 deletions

View File

@@ -18,7 +18,10 @@ struct segment
struct tcp_reassembly
{
struct tcp_reassembly_options opts;
uint8_t enable;
uint32_t max_timeout;
uint32_t max_segments;
uint32_t max_bytes;
struct tcp_reassembly_stat stat;
struct rb_root_cached tree_root;
@@ -26,6 +29,10 @@ struct tcp_reassembly
uint64_t exp_seq;
};
/******************************************************************************
* Private API
******************************************************************************/
/*
* The next routines deal with comparing 32 bit unsigned ints
* and worry about wraparound (automatic with unsigned arithmetic).
@@ -36,16 +43,46 @@ static inline bool before(uint32_t seq1, uint32_t seq2)
return (int32_t)(seq1 - seq2) < 0;
}
static int check_options(const struct tcp_reassembly_options *opts)
{
if (opts == NULL)
{
TCP_REASSEMBLE_ERROR("invalid options");
return -1;
}
if (opts->enable)
{
if (opts->max_timeout < 1 || opts->max_timeout > 60000)
{
TCP_REASSEMBLE_ERROR("invalid max_timeout: %u, supported range: [1, 60000]", opts->max_timeout);
return -1;
}
}
return 0;
}
/******************************************************************************
* Public API
******************************************************************************/
struct tcp_reassembly *tcp_reassembly_new(struct tcp_reassembly_options *opts)
{
struct tcp_reassembly *assy = NULL;
if (check_options(opts) == -1)
{
return NULL;
}
assy = (struct tcp_reassembly *)calloc(1, sizeof(struct tcp_reassembly));
struct tcp_reassembly *assy = (struct tcp_reassembly *)calloc(1, sizeof(struct tcp_reassembly));
if (assy == NULL)
{
return NULL;
}
memcpy(&assy->opts, opts, sizeof(struct tcp_reassembly_options));
assy->enable = opts->enable;
assy->max_timeout = opts->max_timeout;
assy->max_segments = opts->max_segments;
assy->max_bytes = opts->max_bytes;
assy->tree_root = RB_ROOT_CACHED;
INIT_LIST_HEAD(&assy->list_root);
@@ -74,7 +111,7 @@ void tcp_reassembly_free(struct tcp_reassembly *assy)
void tcp_reassembly_init(struct tcp_reassembly *assy, uint32_t syn_seq)
{
if (!assy->opts.enable)
if (!assy->enable)
{
return;
}
@@ -85,7 +122,7 @@ void tcp_reassembly_init(struct tcp_reassembly *assy, uint32_t syn_seq)
void tcp_reassembly_expire(struct tcp_reassembly *assy, uint64_t now)
{
if (!assy->opts.enable)
if (!assy->enable)
{
return;
}
@@ -95,7 +132,7 @@ void tcp_reassembly_expire(struct tcp_reassembly *assy, uint64_t now)
while (!list_empty(&assy->list_root))
{
seg = list_first_entry(&assy->list_root, struct segment, list_node);
if (seg->time + assy->opts.max_timeout > now)
if (seg->time + assy->max_timeout > now)
{
break;
}
@@ -119,7 +156,7 @@ void tcp_reassembly_expire(struct tcp_reassembly *assy, uint64_t now)
void tcp_reassembly_insert(struct tcp_reassembly *assy, uint32_t offset, const char *payload, uint32_t len, uint64_t now)
{
if (!assy->opts.enable)
if (!assy->enable || len == 0)
{
return;
}
@@ -130,23 +167,23 @@ void tcp_reassembly_insert(struct tcp_reassembly *assy, uint32_t offset, const c
assy->stat.insert_segments++;
assy->stat.insert_bytes += len;
if (assy->opts.max_segments > 0 && assy->stat.curr_segments >= assy->opts.max_segments)
if (assy->max_segments > 0 && assy->stat.curr_segments >= assy->max_segments)
{
assy->stat.overload_bypass_segments++;
assy->stat.overload_bypass_bytes += len;
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max packets %u", assy, low, high, assy->opts.max_segments);
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max packets %u", assy, low, high, assy->max_segments);
return;
}
if (assy->opts.max_bytes > 0 && assy->stat.curr_bytes >= assy->opts.max_bytes)
if (assy->max_bytes > 0 && assy->stat.curr_bytes >= assy->max_bytes)
{
assy->stat.overload_bypass_segments++;
assy->stat.overload_bypass_bytes += len;
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max bytes %u", assy, low, high, assy->opts.max_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max bytes %u", assy, low, high, assy->max_bytes);
return;
}
if (len == 0 || before(offset + len, assy->exp_seq))
if (before(offset + len, assy->exp_seq))
{
assy->stat.retrans_bypass_segments++;
assy->stat.retrans_bypass_bytes += len;
@@ -183,7 +220,7 @@ const char *tcp_reassembly_peek(struct tcp_reassembly *assy, uint32_t *len)
{
*len = 0;
if (!assy->opts.enable)
if (!assy->enable)
{
return NULL;
}
@@ -226,7 +263,7 @@ const char *tcp_reassembly_peek(struct tcp_reassembly *assy, uint32_t *len)
void tcp_reassembly_consume(struct tcp_reassembly *assy, uint32_t len)
{
if (!assy->opts.enable || len == 0)
if (!assy->enable || len == 0)
{
return;
}
@@ -293,7 +330,7 @@ void tcp_reassembly_consume(struct tcp_reassembly *assy, uint32_t len)
struct tcp_reassembly_stat *tcp_reassembly_get_stat(struct tcp_reassembly *assy)
{
if (!assy->opts.enable)
if (!assy->enable)
{
return NULL;
}
@@ -303,7 +340,7 @@ struct tcp_reassembly_stat *tcp_reassembly_get_stat(struct tcp_reassembly *assy)
void tcp_reassembly_print_stat(struct tcp_reassembly *assy)
{
if (!assy->opts.enable)
if (!assy->enable)
{
return;
}