From: Victor Julien Date: Tue, 13 Dec 2022 09:05:12 +0000 (+0100) Subject: stream: reduce streaming buffer internals use X-Git-Tag: suricata-7.0.0-rc1~152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61e47ad6f5156c10435e0ef1214e55bf33f0f703;p=thirdparty%2Fsuricata.git stream: reduce streaming buffer internals use --- diff --git a/src/stream-tcp-list.c b/src/stream-tcp-list.c index 2fae335b34..c488aa8aa7 100644 --- a/src/stream-tcp-list.c +++ b/src/stream-tcp-list.c @@ -816,7 +816,7 @@ static inline uint64_t GetLeftEdge(Flow *f, TcpSession *ssn, TcpStream *stream) left_edge = app_le; SCLogDebug("left_edge %" PRIu64 ", using only app:%" PRIu64, left_edge, app_le); } else { - left_edge = STREAM_BASE_OFFSET(stream) + stream->sb.buf_offset; + left_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->sb); SCLogDebug("no app & raw: left_edge %"PRIu64" (full stream)", left_edge); } diff --git a/src/stream-tcp-private.h b/src/stream-tcp-private.h index 2a7b124223..4f44fdcc8e 100644 --- a/src/stream-tcp-private.h +++ b/src/stream-tcp-private.h @@ -100,8 +100,8 @@ RB_PROTOTYPE(TCPSEG, TcpSegment, rb, TcpSegmentCompare); * Only use if STREAM_HAS_SEEN_DATA is true. */ #define STREAM_SEQ_RIGHT_EDGE(stream) (stream)->segs_right_edge #define STREAM_RIGHT_EDGE(stream) (STREAM_BASE_OFFSET((stream)) + (STREAM_SEQ_RIGHT_EDGE((stream)) - (stream)->base_seq)) -/* return true if we have seen data segments. */ -#define STREAM_HAS_SEEN_DATA(stream) (!RB_EMPTY(&(stream)->sb.sbb_tree) || (stream)->sb.stream_offset || (stream)->sb.buf_offset) +/* return true if we have seen data. */ +#define STREAM_HAS_SEEN_DATA(stream) StreamingBufferHasData(&(stream)->sb) typedef struct TcpStream_ { uint16_t flags:12; /**< Flag specific to the stream e.g. Timestamp */ diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index e50f44eac8..9db65f483e 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -413,7 +413,7 @@ uint64_t StreamTcpGetAcked(const TcpStream *stream) uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof) { - uint64_t right_edge = STREAM_BASE_OFFSET(stream) + stream->sb.buf_offset; + uint64_t right_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->sb); if (!eof && StreamTcpInlineMode() == FALSE) { right_edge = MIN(GetAbsLastAck(stream), right_edge); } @@ -899,7 +899,6 @@ uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction) dirstr = "server"; #endif } - int use_app = 1; int use_raw = 1; @@ -913,7 +912,7 @@ uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction) use_raw = 0; } - uint64_t right_edge = STREAM_BASE_OFFSET(stream) + stream->sb.buf_offset; + const uint64_t right_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->sb); SCLogDebug("%s: app %"PRIu64" (use: %s), raw %"PRIu64" (use: %s). Stream right edge: %"PRIu64, dirstr, @@ -1514,7 +1513,7 @@ void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, uint64_t prog /* app is dead */ } else if (progress == 0) { uint64_t tcp_window = stream->window; - uint64_t stream_right_edge = STREAM_BASE_OFFSET(stream) + stream->sb.buf_offset; + const uint64_t stream_right_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->sb); if (tcp_window < stream_right_edge) { uint64_t new_raw = stream_right_edge - tcp_window; if (new_raw > STREAM_RAW_PROGRESS(stream)) { diff --git a/src/util-file.c b/src/util-file.c index d395fc12a9..2476129fd7 100644 --- a/src/util-file.c +++ b/src/util-file.c @@ -323,9 +323,9 @@ static int FileMagicSize(void) uint64_t FileDataSize(const File *file) { if (file != NULL && file->sb != NULL) { - SCLogDebug("returning %"PRIu64, - file->sb->stream_offset + file->sb->buf_offset); - return file->sb->stream_offset + file->sb->buf_offset; + const uint64_t size = StreamingBufferGetConsecutiveDataRightEdge(file->sb); + SCLogDebug("returning %" PRIu64, size); + return size; } SCLogDebug("returning 0 (default)"); return 0; @@ -392,12 +392,13 @@ static int FilePruneFile(File *file) /* if file has inspect window and min size set, we * do some house keeping here */ if (file->inspect_window != 0 && file->inspect_min_size != 0) { + const uint64_t file_offset = StreamingBufferGetOffset(file->sb); uint32_t window = file->inspect_window; - if (file->sb->stream_offset == 0) + if (file_offset == 0) window = MAX(window, file->inspect_min_size); uint64_t file_size = FileDataSize(file); - uint64_t data_size = file_size - file->sb->stream_offset; + uint64_t data_size = file_size - file_offset; SCLogDebug("window %"PRIu32", file_size %"PRIu64", data_size %"PRIu64, window, file_size, data_size); diff --git a/src/util-streaming-buffer.h b/src/util-streaming-buffer.h index fd2a2e2449..17844cc243 100644 --- a/src/util-streaming-buffer.h +++ b/src/util-streaming-buffer.h @@ -106,6 +106,21 @@ typedef struct StreamingBuffer_ { #endif } StreamingBuffer; +static inline bool StreamingBufferHasData(const StreamingBuffer *sb) +{ + return (sb->stream_offset || sb->buf_offset || !RB_EMPTY(&sb->sbb_tree)); +} + +static inline uint64_t StreamingBufferGetConsecutiveDataRightEdge(const StreamingBuffer *sb) +{ + return sb->stream_offset + sb->buf_offset; +} + +static inline uint64_t StreamingBufferGetOffset(const StreamingBuffer *sb) +{ + return sb->stream_offset; +} + #ifndef DEBUG #define STREAMING_BUFFER_INITIALIZER(cfg) \ { \