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/tcp_reassembly/tcp_reassembly.cpp

319 lines
10 KiB
C++
Raw Normal View History

2024-03-21 19:27:41 +08:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
2024-03-27 17:11:38 +08:00
#include "list.h"
2024-03-21 19:27:41 +08:00
#include "tcp_reassembly.h"
2024-03-27 17:11:38 +08:00
#include "interval_tree.h"
2024-03-21 19:27:41 +08:00
struct segment
{
2024-03-27 17:11:38 +08:00
struct interval_tree_node tree_node;
struct list_head list_node;
2024-03-21 19:27:41 +08:00
uint64_t time;
2024-03-27 17:11:38 +08:00
uint64_t id;
2024-03-21 19:27:41 +08:00
char *payload; // Flexible array member
};
2024-03-25 17:30:48 +08:00
struct tcp_reassembly
{
struct tcp_reassembly_options opts;
struct tcp_reassembly_stat stat;
2024-03-27 17:11:38 +08:00
struct rb_root_cached tree_root;
struct list_head list_root;
2024-03-25 17:30:48 +08:00
uint64_t exp_seq;
};
/*
* The next routines deal with comparing 32 bit unsigned ints
* and worry about wraparound (automatic with unsigned arithmetic).
*/
static inline bool before(uint32_t seq1, uint32_t seq2)
{
return (int32_t)(seq1 - seq2) < 0;
}
struct tcp_reassembly *tcp_reassembly_new(struct tcp_reassembly_options *opts)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
struct tcp_reassembly *assy = NULL;
assy = (struct tcp_reassembly *)calloc(1, sizeof(struct tcp_reassembly));
if (assy == NULL)
2024-03-21 19:27:41 +08:00
{
return NULL;
}
2024-03-25 17:30:48 +08:00
memcpy(&assy->opts, opts, sizeof(struct tcp_reassembly_options));
2024-03-21 19:27:41 +08:00
2024-03-27 17:11:38 +08:00
assy->tree_root = RB_ROOT_CACHED;
INIT_LIST_HEAD(&assy->list_root);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
return assy;
2024-03-21 19:27:41 +08:00
}
2024-03-25 17:30:48 +08:00
void tcp_reassembly_free(struct tcp_reassembly *assy)
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
struct segment *seg = NULL;
struct interval_tree_node *tree_node = NULL;
2024-03-25 17:30:48 +08:00
if (assy)
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
while ((tree_node = interval_tree_iter_first(&assy->tree_root, 0, UINT64_MAX)))
2024-03-25 17:30:48 +08:00
{
2024-03-27 17:11:38 +08:00
seg = container_of(tree_node, struct segment, tree_node);
interval_tree_remove(&seg->tree_node, &assy->tree_root);
list_del(&seg->list_node);
free(seg);
seg = NULL;
2024-03-25 17:30:48 +08:00
}
free(assy);
2024-03-27 17:11:38 +08:00
assy = NULL;
2024-03-21 19:27:41 +08:00
}
}
2024-03-25 17:30:48 +08:00
void tcp_reassembly_init(struct tcp_reassembly *assy, uint32_t syn_seq)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (!assy->opts.enable)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
return;
2024-03-21 19:27:41 +08:00
}
2024-03-25 17:30:48 +08:00
assy->exp_seq = syn_seq + 1;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p init expect seq %lu", assy, assy->exp_seq);
2024-03-21 19:27:41 +08:00
}
2024-03-25 17:30:48 +08:00
void tcp_reassembly_expire(struct tcp_reassembly *assy, uint64_t now)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (!assy->opts.enable)
2024-03-21 19:27:41 +08:00
{
return;
}
2024-03-27 17:11:38 +08:00
uint64_t len;
2024-03-25 17:30:48 +08:00
struct segment *seg = NULL;
2024-03-27 17:11:38 +08:00
while (!list_empty(&assy->list_root))
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
seg = list_first_entry(&assy->list_root, struct segment, list_node);
2024-03-25 17:30:48 +08:00
if (seg->time + assy->opts.max_timeout > now)
2024-03-21 19:27:41 +08:00
{
break;
}
2024-03-27 17:11:38 +08:00
len = seg->tree_node.last - seg->tree_node.start + 1;
2024-03-25 17:30:48 +08:00
assy->stat.timeout_discard_segments++;
2024-03-27 17:11:38 +08:00
assy->stat.timeout_discard_bytes += len;
2024-03-25 17:30:48 +08:00
2024-03-27 17:11:38 +08:00
assy->stat.curr_segments--;
assy->stat.curr_bytes -= len;
TCP_REASSEMBLE_DEBUG("reassembler %p expire segment %p [%lu, %lu] (time: %lu, now: %lu)", assy, seg, seg->tree_node.start, seg->tree_node.last, seg->time, now);
interval_tree_remove(&seg->tree_node, &assy->tree_root);
list_del(&seg->list_node);
free(seg);
seg = NULL;
2024-03-21 19:27:41 +08:00
}
}
2024-03-25 17:30:48 +08:00
void tcp_reassembly_insert(struct tcp_reassembly *assy, uint32_t offset, const char *payload, uint32_t len, uint64_t now)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (!assy->opts.enable)
2024-03-21 19:27:41 +08:00
{
return;
}
2024-03-25 17:30:48 +08:00
uint64_t low = (uint64_t)offset;
uint64_t high = (uint64_t)offset + (uint64_t)len - 1; // from uint32_t to uint64_t, so no overflow
assy->stat.insert_segments++;
assy->stat.insert_bytes += len;
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
if (assy->opts.max_segments > 0 && assy->stat.curr_segments >= assy->opts.max_segments)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
assy->stat.overload_bypass_segments++;
assy->stat.overload_bypass_bytes += len;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max packets %u", assy, low, high, assy->opts.max_segments);
2024-03-21 19:27:41 +08:00
return;
}
2024-03-25 17:30:48 +08:00
if (assy->opts.max_bytes > 0 && assy->stat.curr_bytes >= assy->opts.max_bytes)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
assy->stat.overload_bypass_segments++;
assy->stat.overload_bypass_bytes += len;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, reach max bytes %u", assy, low, high, assy->opts.max_bytes);
2024-03-21 19:27:41 +08:00
return;
}
2024-03-25 17:30:48 +08:00
if (len == 0 || before(offset + len, assy->exp_seq))
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
assy->stat.retrans_bypass_segments++;
assy->stat.retrans_bypass_bytes += len;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, less the expect seq %lu", assy, low, high, assy->exp_seq);
2024-03-21 19:27:41 +08:00
return;
}
2024-03-27 17:11:38 +08:00
struct segment *seg = (struct segment *)calloc(1, sizeof(struct segment) + len);
2024-03-21 19:27:41 +08:00
if (seg == NULL)
{
2024-03-27 17:11:38 +08:00
assy->stat.overload_bypass_segments++;
assy->stat.overload_bypass_bytes += len;
TCP_REASSEMBLE_DEBUG("reassembler %p insert [%lu, %lu] failed, calloc segment failed", assy, low, high);
2024-03-21 19:27:41 +08:00
return;
}
2024-03-27 17:11:38 +08:00
seg->tree_node.start = low;
seg->tree_node.last = high;
2024-03-21 19:27:41 +08:00
seg->time = now;
2024-03-27 17:11:38 +08:00
seg->id = assy->stat.insert_segments;
2024-03-21 19:27:41 +08:00
seg->payload = (char *)seg + sizeof(struct segment);
memcpy(seg->payload, payload, len);
2024-03-27 17:11:38 +08:00
list_add_tail(&seg->list_node, &assy->list_root);
interval_tree_insert(&seg->tree_node, &assy->tree_root);
TCP_REASSEMBLE_DEBUG("reassembler %p insert segment %p [%lu, %lu]", assy, seg, low, high);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
assy->stat.curr_segments++;
2024-03-27 17:11:38 +08:00
assy->stat.curr_bytes += len;
2024-03-21 19:27:41 +08:00
}
2024-03-25 17:30:48 +08:00
const char *tcp_reassembly_peek(struct tcp_reassembly *assy, uint32_t *len)
2024-03-21 19:27:41 +08:00
{
*len = 0;
2024-03-25 17:30:48 +08:00
if (!assy->opts.enable)
2024-03-21 19:27:41 +08:00
{
return NULL;
}
2024-03-27 17:11:38 +08:00
uint64_t id = UINT64_MAX;
2024-03-25 17:30:48 +08:00
struct segment *seg = NULL;
2024-03-27 17:11:38 +08:00
struct interval_tree_node *tree_node = NULL;
struct interval_tree_node *oldest_node = NULL;
tree_node = interval_tree_iter_first(&assy->tree_root, assy->exp_seq, assy->exp_seq);
while (tree_node)
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
seg = container_of(tree_node, struct segment, tree_node);
if (seg->id < id)
2024-03-25 17:30:48 +08:00
{
2024-03-27 17:11:38 +08:00
id = seg->id;
oldest_node = tree_node;
2024-03-25 17:30:48 +08:00
}
2024-03-27 17:11:38 +08:00
tree_node = interval_tree_iter_next(tree_node, assy->exp_seq, assy->exp_seq);
2024-03-25 17:30:48 +08:00
}
2024-03-27 17:11:38 +08:00
if (oldest_node == NULL)
2024-03-25 17:30:48 +08:00
{
return NULL;
}
2024-03-27 17:11:38 +08:00
uint64_t payload_len = oldest_node->last - oldest_node->start + 1;
seg = container_of(oldest_node, struct segment, tree_node);
if (oldest_node->start < assy->exp_seq)
2024-03-25 17:30:48 +08:00
{
2024-03-27 17:11:38 +08:00
uint64_t overlap = assy->exp_seq - oldest_node->start;
*len = (uint16_t)(payload_len - overlap);
TCP_REASSEMBLE_DEBUG("reassembler %p peek [%lu, +∞], found segment %p [%lu, %lu] (left overlap: %lu)", assy, assy->exp_seq, seg, oldest_node->start, oldest_node->last, overlap);
2024-03-25 17:30:48 +08:00
return seg->payload + overlap;
2024-03-21 19:27:41 +08:00
}
2024-03-27 17:11:38 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p peek [%lu, +∞], found segment %p [%lu, %lu]", assy, assy->exp_seq, seg, oldest_node->start, oldest_node->last);
2024-03-21 19:27:41 +08:00
2024-03-27 17:11:38 +08:00
*len = (uint16_t)payload_len;
2024-03-21 19:27:41 +08:00
return seg->payload;
}
2024-03-25 17:30:48 +08:00
void tcp_reassembly_consume(struct tcp_reassembly *assy, uint32_t len)
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
if (!assy->opts.enable || len == 0)
2024-03-21 19:27:41 +08:00
{
return;
}
2024-03-26 15:09:03 +08:00
/*
* https://www.ietf.org/rfc/rfc0793.txt
*
* This space ranges from 0 to 2**32 - 1.
* Since the space is finite, all arithmetic dealing with sequence
* numbers must be performed modulo 2**32. This unsigned arithmetic
* preserves the relationship of sequence numbers as they cycle from
* 2**32 - 1 to 0 again. There are some subtleties to computer modulo
* arithmetic, so great care should be taken in programming the
* comparison of such values. The symbol "=<" means "less than or equal"
* (modulo 2**32).
*
* UINT32_MAX = 4294967295
* 2^32 = 4294967296
* 2^32 - 1 = 4294967295
* seq range: [0, 4294967295]
* seq range: [0, UINT32_MAX]
*/
2024-03-27 17:11:38 +08:00
uint64_t old_exp_seq = assy->exp_seq;
2024-03-25 17:30:48 +08:00
assy->exp_seq += len;
if (assy->exp_seq > UINT32_MAX)
{
2024-03-26 15:09:03 +08:00
assy->exp_seq = assy->exp_seq % 4294967296;
2024-03-25 17:30:48 +08:00
}
2024-03-27 17:11:38 +08:00
uint64_t new_exp_seq = assy->exp_seq;
2024-03-21 19:27:41 +08:00
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p consume [%lu, %lu], update expect seq %lu -> %lu", assy, old_exp_seq, old_exp_seq + len - 1, old_exp_seq, new_exp_seq);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
assy->stat.consume_segments++;
assy->stat.consume_bytes += len;
2024-03-27 17:11:38 +08:00
struct interval_tree_node *node = interval_tree_iter_first(&assy->tree_root, old_exp_seq, old_exp_seq + len - 1);
while (node)
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
if (before(node->last, new_exp_seq))
2024-03-21 19:27:41 +08:00
{
2024-03-27 17:11:38 +08:00
struct segment *seg = container_of(node, struct segment, tree_node);
uint32_t len = node->last - node->start + 1;
assy->stat.remove_segments++;
assy->stat.remove_bytes += len;
assy->stat.curr_segments--;
assy->stat.curr_bytes -= len;
TCP_REASSEMBLE_DEBUG("reassembler %p consume [%lu, %lu], delete segment %p [%lu, %lu]", assy, old_exp_seq, old_exp_seq + len - 1, node, node->start, node->last);
interval_tree_remove(node, &assy->tree_root);
list_del(&seg->list_node);
free(seg);
node = interval_tree_iter_first(&assy->tree_root, old_exp_seq, old_exp_seq + len - 1);
2024-03-21 19:27:41 +08:00
}
else
{
2024-03-27 17:11:38 +08:00
node = interval_tree_iter_next(node, old_exp_seq, old_exp_seq + len - 1);
2024-03-21 19:27:41 +08:00
}
}
}
2024-03-25 17:30:48 +08:00
struct tcp_reassembly_stat *tcp_reassembly_get_stat(struct tcp_reassembly *assy)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (!assy->opts.enable)
{
return NULL;
}
return &assy->stat;
}
void tcp_reassembly_print_stat(struct tcp_reassembly *assy)
{
if (!assy->opts.enable)
{
return;
}
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p current : segments %lu, bytes %lu", assy, assy->stat.curr_segments, assy->stat.curr_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p insert : segments %lu, bytes %lu", assy, assy->stat.insert_segments, assy->stat.insert_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p remove : segments %lu, bytes %lu", assy, assy->stat.remove_segments, assy->stat.remove_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p consume : segments %lu, bytes %lu", assy, assy->stat.consume_segments, assy->stat.consume_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p retrans bypass : segments %lu, bytes %lu", assy, assy->stat.retrans_bypass_segments, assy->stat.retrans_bypass_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p overload bypass : segments %lu, bytes %lu", assy, assy->stat.overload_bypass_segments, assy->stat.overload_bypass_bytes);
TCP_REASSEMBLE_DEBUG("reassembler %p timeout discard : segments %lu, bytes %lu", assy, assy->stat.timeout_discard_segments, assy->stat.timeout_discard_bytes);
2024-03-25 17:30:48 +08:00
}