Skip to content

Commit

Permalink
stream/tcp: add ssnmemcap exception policy counter
Browse files Browse the repository at this point in the history
Add stats counters for exception policies applied in case a stream
session memcap is hit.

Task OISF#5816
  • Loading branch information
jufajardini authored and victorjulien committed Apr 11, 2024
1 parent a71ace8 commit 2dee377
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5397,6 +5397,11 @@
"ssn_memcap_drop": {
"type": "integer"
},
"ssn_memcap_exception_policy": {
"description":
"How many times session memcap exception policy was applied, and which one",
"$ref": "#/$defs/exceptionPolicy"
},
"stream_depth_reached": {
"type": "integer"
},
Expand Down
49 changes: 49 additions & 0 deletions src/stream-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@
#define STREAMTCP_DEFAULT_MAX_SYN_QUEUED 10
#define STREAMTCP_DEFAULT_MAX_SYNACK_QUEUED 5

/* Settings order as in the enum */
// clang-format off
ExceptionPolicyStatsSetts stream_memcap_eps_stats = {
.valid_settings_ids = {
/* EXCEPTION_POLICY_NOT_SET */ false,
/* EXCEPTION_POLICY_AUTO */ false,
/* EXCEPTION_POLICY_PASS_PACKET */ true,
/* EXCEPTION_POLICY_PASS_FLOW */ true,
/* EXCEPTION_POLICY_BYPASS_FLOW */ true,
/* EXCEPTION_POLICY_DROP_PACKET */ false,
/* EXCEPTION_POLICY_DROP_FLOW */ false,
/* EXCEPTION_POLICY_REJECT */ true,
},
.valid_settings_ips = {
/* EXCEPTION_POLICY_NOT_SET */ false,
/* EXCEPTION_POLICY_AUTO */ false,
/* EXCEPTION_POLICY_PASS_PACKET */ true,
/* EXCEPTION_POLICY_PASS_FLOW */ true,
/* EXCEPTION_POLICY_BYPASS_FLOW */ true,
/* EXCEPTION_POLICY_DROP_PACKET */ true,
/* EXCEPTION_POLICY_DROP_FLOW */ true,
/* EXCEPTION_POLICY_REJECT */ true,
},
};
// clang-format on

static int StreamTcpHandleFin(ThreadVars *tv, StreamTcpThread *, TcpSession *, Packet *);
void StreamTcpReturnStreamSegments (TcpStream *);
void StreamTcpInitConfig(bool);
Expand Down Expand Up @@ -702,6 +728,23 @@ void StreamTcpFreeConfig(bool quiet)
SCLogDebug("ssn_pool_cnt %"PRIu64"", ssn_pool_cnt);
}

static bool IsStreamTcpSessionMemcapExceptionPolicyStatsValid(enum ExceptionPolicy policy)
{
if (EngineModeIsIPS()) {
return stream_memcap_eps_stats.valid_settings_ips[policy];
}
return stream_memcap_eps_stats.valid_settings_ids[policy];
}

static void StreamTcpSsnMemcapExceptionPolicyStatsIncr(
ThreadVars *tv, StreamTcpThread *stt, enum ExceptionPolicy policy)
{
const uint16_t id = stt->counter_tcp_ssn_memcap_eps.eps_id[policy];
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
}

/** \internal
* \brief The function is used to fetch a TCP session from the
* ssn_pool, when a TCP SYN is received.
Expand Down Expand Up @@ -741,13 +784,15 @@ static TcpSession *StreamTcpNewSession(ThreadVars *tv, StreamTcpThread *stt, Pac
g_eps_stream_ssn_memcap == t_pcapcnt))) {
SCLogNotice("simulating memcap reached condition for packet %" PRIu64, t_pcapcnt);
ExceptionPolicyApply(p, stream_config.ssn_memcap_policy, PKT_DROP_REASON_STREAM_MEMCAP);
StreamTcpSsnMemcapExceptionPolicyStatsIncr(tv, stt, stream_config.ssn_memcap_policy);
return NULL;
}
#endif
ssn = (TcpSession *)p->flow->protoctx;
if (ssn == NULL) {
SCLogDebug("ssn_pool is empty");
ExceptionPolicyApply(p, stream_config.ssn_memcap_policy, PKT_DROP_REASON_STREAM_MEMCAP);
StreamTcpSsnMemcapExceptionPolicyStatsIncr(tv, stt, stream_config.ssn_memcap_policy);
return NULL;
}

Expand Down Expand Up @@ -5763,6 +5808,10 @@ TmEcode StreamTcpThreadInit(ThreadVars *tv, void *initdata, void **data)
stt->counter_tcp_ssn_memcap = StatsRegisterCounter("tcp.ssn_memcap_drop", tv);
stt->counter_tcp_ssn_from_cache = StatsRegisterCounter("tcp.ssn_from_cache", tv);
stt->counter_tcp_ssn_from_pool = StatsRegisterCounter("tcp.ssn_from_pool", tv);
ExceptionPolicySetStatsCounters(tv, &stt->counter_tcp_ssn_memcap_eps, &stream_memcap_eps_stats,
stream_config.ssn_memcap_policy, "tcp.ssn_memcap_exception_policy.",
IsStreamTcpSessionMemcapExceptionPolicyStatsValid);

stt->counter_tcp_pseudo = StatsRegisterCounter("tcp.pseudo", tv);
stt->counter_tcp_pseudo_failed = StatsRegisterCounter("tcp.pseudo_failed", tv);
stt->counter_tcp_invalid_checksum = StatsRegisterCounter("tcp.invalid_checksum", tv);
Expand Down
5 changes: 4 additions & 1 deletion src/stream-tcp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2022 Open Information Security Foundation
/* Copyright (C) 2007-2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -30,6 +30,7 @@
#include "stream.h"
#include "stream-tcp-reassemble.h"
#include "suricata.h"
#include "util-exception-policy-types.h"

#define STREAM_VERBOSE false
/* Flag to indicate that the checksum validation for the stream engine
Expand Down Expand Up @@ -85,6 +86,8 @@ typedef struct StreamTcpThread_ {
uint16_t counter_tcp_ssn_memcap;
uint16_t counter_tcp_ssn_from_cache;
uint16_t counter_tcp_ssn_from_pool;
/** exception policy */
ExceptionPolicyCounters counter_tcp_ssn_memcap_eps;
/** pseudo packets processed */
uint16_t counter_tcp_pseudo;
/** pseudo packets failed to setup */
Expand Down

0 comments on commit 2dee377

Please sign in to comment.