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

452 lines
12 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>
#include "tcp_reassembly.h"
#include "itree.h"
struct segment
{
2024-03-25 17:30:48 +08:00
struct tcp_reassembly *assy;
2024-03-21 19:27:41 +08:00
struct segment *next;
struct segment *prev;
2024-03-25 17:30:48 +08:00
uint64_t id;
2024-03-21 19:27:41 +08:00
uint64_t time;
uint32_t offset;
uint32_t len;
char *payload; // Flexible array member
};
2024-03-25 17:30:48 +08:00
struct segment_list
{
struct segment *head; // del segment from head
struct segment *tail; // add segment to tail
};
struct tcp_reassembly
{
struct tcp_reassembly_options opts;
struct tcp_reassembly_stat stat;
struct segment_list list;
struct itree *itree;
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;
}
static inline void segment_list_add(struct segment_list *list, struct segment *seg)
{
if (list->head == NULL)
{
list->head = seg;
}
else
{
list->tail->next = seg;
seg->prev = list->tail;
}
list->tail = seg;
}
static inline void segment_list_del(struct segment_list *list, struct segment *seg)
{
if (list->head == seg)
{
list->head = seg->next;
}
if (list->tail == seg)
{
list->tail = seg->prev;
}
if (seg->prev)
{
seg->prev->next = seg->next;
}
if (seg->next)
{
seg->next->prev = seg->prev;
}
}
static inline struct segment *segment_list_get_oldest(struct segment_list *list)
{
return list->head;
}
static inline struct segment *segment_new(uint32_t len)
{
return (struct segment *)calloc(1, sizeof(struct segment) + len);
}
static inline void *segment_dup(void *p)
2024-03-21 19:27:41 +08:00
{
return p;
}
2024-03-25 17:30:48 +08:00
static inline void segment_free(void *p)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
struct segment *seg = NULL;
struct tcp_reassembly *assy = NULL;
if (p)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
seg = (struct segment *)p;
assy = seg->assy;
segment_list_del(&assy->list, seg);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
assy->stat.curr_bytes -= seg->len;
assy->stat.curr_segments--;
2024-03-21 19:27:41 +08:00
free(seg);
}
}
2024-03-25 17:30:48 +08:00
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));
if (!assy->opts.enable)
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
assy->itree = itree_new(segment_dup, segment_free);
if (assy->itree == NULL)
2024-03-21 19:27:41 +08:00
{
goto error_out;
}
2024-03-25 17:30:48 +08:00
return assy;
2024-03-21 19:27:41 +08:00
error_out:
2024-03-25 17:30:48 +08:00
tcp_reassembly_free(assy);
2024-03-21 19:27:41 +08:00
return NULL;
}
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-25 17:30:48 +08:00
if (assy)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (assy->itree)
{
itree_delete(assy->itree);
}
free(assy);
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-25 17:30:48 +08:00
uint64_t high;
interval_t expire;
struct segment *seg = NULL;
while ((seg = segment_list_get_oldest(&assy->list)) != NULL)
2024-03-21 19:27:41 +08:00
{
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-25 17:30:48 +08:00
high = (uint64_t)seg->offset + (uint64_t)seg->len - 1;
expire = {
2024-03-21 19:27:41 +08:00
.low = seg->offset,
2024-03-25 17:30:48 +08:00
.high = high,
.data = seg,
2024-03-21 19:27:41 +08:00
};
2024-03-25 17:30:48 +08:00
assy->stat.timeout_discard_segments++;
assy->stat.timeout_discard_bytes += seg->len;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p expire segment %p [%lu, %lu] (time: %lu, now: %lu)", assy, seg, seg->offset, high, seg->time, now);
2024-03-25 17:30:48 +08:00
itree_remove(assy->itree, &expire);
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
struct segment *seg = NULL;
interval_t insert;
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-25 17:30:48 +08:00
seg = segment_new(len);
2024-03-21 19:27:41 +08:00
if (seg == NULL)
{
return;
}
2024-03-25 17:30:48 +08:00
seg->assy = assy;
seg->id = assy->stat.insert_segments;
2024-03-21 19:27:41 +08:00
seg->time = now;
seg->offset = offset;
seg->len = len;
seg->payload = (char *)seg + sizeof(struct segment);
memcpy(seg->payload, payload, len);
2024-03-25 17:30:48 +08:00
insert = {
.low = low,
.high = high,
2024-03-21 19:27:41 +08:00
.data = seg,
};
2024-03-25 17:30:48 +08:00
if (itree_insert(assy->itree, &insert) == 0)
2024-03-21 19:27:41 +08:00
{
free(seg);
return;
}
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p insert segment %p [%lu, %lu]", assy, seg, insert.low, insert.high);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
segment_list_add(&assy->list, seg);
2024-03-21 19:27:41 +08:00
2024-03-25 17:30:48 +08:00
assy->stat.curr_segments++;
assy->stat.curr_bytes += seg->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-25 17:30:48 +08:00
int count = 0;
interval_t peek;
uint64_t overlap = 0;
uint64_t min_id = UINT64_MAX;
struct segment *seg = NULL;
ilist_t *list = NULL;
ilisttrav_t *trav = NULL;
interval_t *query = NULL;
interval_t *oldest = NULL;
peek = {
.low = assy->exp_seq,
.high = assy->exp_seq,
2024-03-21 19:27:41 +08:00
};
2024-03-25 17:30:48 +08:00
list = itree_findall(assy->itree, &peek);
if (list == NULL)
2024-03-21 19:27:41 +08:00
{
return NULL;
}
2024-03-25 17:30:48 +08:00
count = ilist_size(list);
trav = ilisttrav_new(list);
for (int i = 0; i < count; i++)
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
if (i == 0)
{
query = (interval_t *)ilisttrav_first(trav);
}
else
{
query = (interval_t *)ilisttrav_next(trav);
}
seg = (struct segment *)query->data;
if (seg->id < min_id)
{
min_id = seg->id;
oldest = query;
}
}
ilisttrav_delete(trav);
ilist_delete(list);
if (oldest == NULL)
{
return NULL;
}
seg = (struct segment *)oldest->data;
if (seg->offset < assy->exp_seq)
{
overlap = assy->exp_seq - seg->offset;
*len = seg->len - overlap;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p peek [%lu, +∞], found segment %p [%lu, %lu] (left overlap: %lu)", assy, assy->exp_seq, seg, oldest->low, oldest->high, overlap);
2024-03-25 17:30:48 +08:00
return seg->payload + overlap;
2024-03-21 19:27:41 +08:00
}
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p peek [%lu, +∞], found segment %p [%lu, %lu]", assy, assy->exp_seq, seg, oldest->low, oldest->high);
2024-03-21 19:27:41 +08:00
*len = seg->len;
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-25 17:30:48 +08:00
if (!assy->opts.enable)
2024-03-21 19:27:41 +08:00
{
return;
}
if (len == 0)
{
return;
}
2024-03-25 17:30:48 +08:00
int count;
uint64_t old_exp_seq;
uint64_t new_exp_seq;
interval_t consume;
ilist_t *list = NULL;
interval_t *del = NULL;
ilisttrav_t *trav = NULL;
struct segment *seg = NULL;
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-25 17:30:48 +08:00
old_exp_seq = assy->exp_seq;
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
}
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-25 17:30:48 +08:00
consume =
{
.low = old_exp_seq,
.high = old_exp_seq + len - 1,
};
list = itree_findall(assy->itree, &consume);
2024-03-21 19:27:41 +08:00
if (list == NULL)
{
return;
}
2024-03-25 17:30:48 +08:00
assy->stat.consume_segments++;
assy->stat.consume_bytes += len;
count = ilist_size(list);
trav = ilisttrav_new(list);
2024-03-21 19:27:41 +08:00
for (int i = 0; i < count; i++)
{
if (i == 0)
{
2024-03-25 17:30:48 +08:00
del = (interval_t *)ilisttrav_first(trav);
2024-03-21 19:27:41 +08:00
}
else
{
2024-03-25 17:30:48 +08:00
del = (interval_t *)ilisttrav_next(trav);
2024-03-21 19:27:41 +08:00
}
2024-03-25 17:30:48 +08:00
if (del && before(del->high, new_exp_seq))
2024-03-21 19:27:41 +08:00
{
2024-03-25 17:30:48 +08:00
seg = (struct segment *)del->data;
assy->stat.remove_segments++;
assy->stat.remove_bytes += seg->len;
2024-03-26 15:09:03 +08:00
TCP_REASSEMBLE_DEBUG("reassembler %p consume [%lu, %lu], delete segment %p [%lu, %lu]", assy, old_exp_seq, old_exp_seq + len - 1, seg, del->low, del->high);
2024-03-25 17:30:48 +08:00
itree_remove(assy->itree, del);
2024-03-21 19:27:41 +08:00
}
}
ilisttrav_delete(trav);
ilist_delete(list);
}
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
}