Skip to content
137 changes: 73 additions & 64 deletions src/app-layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ typedef struct AppLayerCounters_ {
} AppLayerCounters;

/* counter names. Only used at init. */
AppLayerCounterNames applayer_counter_names[FLOW_PROTO_APPLAYER_MAX][ALPROTO_MAX];
AppLayerCounterNames (*applayer_counter_names)[FLOW_PROTO_APPLAYER_MAX];
/* counter id's. Used that runtime. */
AppLayerCounters applayer_counters[FLOW_PROTO_APPLAYER_MAX][ALPROTO_MAX];
AppLayerCounters (*applayer_counters)[FLOW_PROTO_APPLAYER_MAX];
/* Exception policy global counters ids */
ExceptionPolicyCounters eps_error_summary;

Expand Down Expand Up @@ -144,47 +144,47 @@ static inline int ProtoDetectDone(const Flow *f, const TcpSession *ssn, uint8_t
*/
static void AppLayerIncFlowCounter(ThreadVars *tv, Flow *f)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].counter_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].counter_id;
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
}

void AppLayerIncTxCounter(ThreadVars *tv, Flow *f, uint64_t step)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].counter_tx_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].counter_tx_id;
if (likely(tv && id > 0)) {
StatsAddUI64(tv, id, step);
}
}

void AppLayerIncGapErrorCounter(ThreadVars *tv, Flow *f)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].gap_error_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].gap_error_id;
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
}

void AppLayerIncAllocErrorCounter(ThreadVars *tv, Flow *f)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].alloc_error_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].alloc_error_id;
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
}

void AppLayerIncParserErrorCounter(ThreadVars *tv, Flow *f)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].parser_error_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].parser_error_id;
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
}

void AppLayerIncInternalErrorCounter(ThreadVars *tv, Flow *f)
{
const uint16_t id = applayer_counters[f->protomap][f->alproto].internal_error_id;
const uint16_t id = applayer_counters[f->alproto][f->protomap].internal_error_id;
if (likely(tv && id > 0)) {
StatsIncr(tv, id);
}
Expand All @@ -197,7 +197,7 @@ static void AppLayerIncrErrorExcPolicyCounter(ThreadVars *tv, Flow *f, enum Exce
return;
}
#endif
uint16_t id = applayer_counters[f->protomap][f->alproto].eps_error.eps_id[policy];
uint16_t id = applayer_counters[f->alproto][f->protomap].eps_error.eps_id[policy];
/* for the summary values */
uint16_t g_id = eps_error_summary.eps_id[policy];

Expand Down Expand Up @@ -1132,8 +1132,8 @@ static void AppLayerSetupExceptionPolicyPerProtoCounters(
g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1; i < EXCEPTION_POLICY_MAX; i++) {
if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
snprintf(applayer_counter_names[ipproto_map][alproto].eps_name[i],
sizeof(applayer_counter_names[ipproto_map][alproto].eps_name[i]),
snprintf(applayer_counter_names[alproto][ipproto_map].eps_name[i],
sizeof(applayer_counter_names[alproto][ipproto_map].eps_name[i]),
"app_layer.error.%s%s.exception_policy.%s", alproto_str, ipproto_suffix,
ExceptionPolicyEnumToString(i, true));
}
Expand All @@ -1148,6 +1148,15 @@ void AppLayerSetupCounters(void)
const char *str = "app_layer.flow.";
const char *estr = "app_layer.error.";

applayer_counter_names =
SCCalloc(ALPROTO_MAX, sizeof(AppLayerCounterNames[FLOW_PROTO_APPLAYER_MAX]));
if (unlikely(applayer_counter_names == NULL)) {
FatalError("Unable to alloc applayer_counter_names.");
}
applayer_counters = SCCalloc(ALPROTO_MAX, sizeof(AppLayerCounters[FLOW_PROTO_APPLAYER_MAX]));
if (unlikely(applayer_counters == NULL)) {
FatalError("Unable to alloc applayer_counters.");
}
/* We don't log stats counters if exception policy is `ignore`/`not set` */
if (g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
/* Register global counters for app layer error exception policy summary */
Expand Down Expand Up @@ -1178,62 +1187,62 @@ void AppLayerSetupCounters(void)
AppLayerProtoDetectSupportedIpprotos(alproto, ipprotos_all);
if ((ipprotos_all[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) &&
(ipprotos_all[IPPROTO_UDP / 8] & (1 << (IPPROTO_UDP % 8)))) {
snprintf(applayer_counter_names[ipproto_map][alproto].name,
sizeof(applayer_counter_names[ipproto_map][alproto].name),
"%s%s%s", str, alproto_str, ipproto_suffix);
snprintf(applayer_counter_names[ipproto_map][alproto].tx_name,
sizeof(applayer_counter_names[ipproto_map][alproto].tx_name),
"%s%s%s", tx_str, alproto_str, ipproto_suffix);
snprintf(applayer_counter_names[alproto][ipproto_map].name,
sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s",
str, alproto_str, ipproto_suffix);
snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s%s",
tx_str, alproto_str, ipproto_suffix);

if (ipproto == IPPROTO_TCP) {
snprintf(applayer_counter_names[ipproto_map][alproto].gap_error,
sizeof(applayer_counter_names[ipproto_map][alproto].gap_error),
snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
"%s%s%s.gap", estr, alproto_str, ipproto_suffix);
}
snprintf(applayer_counter_names[ipproto_map][alproto].alloc_error,
sizeof(applayer_counter_names[ipproto_map][alproto].alloc_error),
snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
"%s%s%s.alloc", estr, alproto_str, ipproto_suffix);
snprintf(applayer_counter_names[ipproto_map][alproto].parser_error,
sizeof(applayer_counter_names[ipproto_map][alproto].parser_error),
snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
"%s%s%s.parser", estr, alproto_str, ipproto_suffix);
snprintf(applayer_counter_names[ipproto_map][alproto].internal_error,
sizeof(applayer_counter_names[ipproto_map][alproto].internal_error),
snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
"%s%s%s.internal", estr, alproto_str, ipproto_suffix);

AppLayerSetupExceptionPolicyPerProtoCounters(
ipproto_map, alproto, alproto_str, ipproto_suffix);
} else {
snprintf(applayer_counter_names[ipproto_map][alproto].name,
sizeof(applayer_counter_names[ipproto_map][alproto].name),
"%s%s", str, alproto_str);
snprintf(applayer_counter_names[ipproto_map][alproto].tx_name,
sizeof(applayer_counter_names[ipproto_map][alproto].tx_name),
"%s%s", tx_str, alproto_str);
snprintf(applayer_counter_names[alproto][ipproto_map].name,
sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s", str,
alproto_str);
snprintf(applayer_counter_names[alproto][ipproto_map].tx_name,
sizeof(applayer_counter_names[alproto][ipproto_map].tx_name), "%s%s",
tx_str, alproto_str);

if (ipproto == IPPROTO_TCP) {
snprintf(applayer_counter_names[ipproto_map][alproto].gap_error,
sizeof(applayer_counter_names[ipproto_map][alproto].gap_error),
snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
"%s%s.gap", estr, alproto_str);
}
snprintf(applayer_counter_names[ipproto_map][alproto].alloc_error,
sizeof(applayer_counter_names[ipproto_map][alproto].alloc_error),
snprintf(applayer_counter_names[alproto][ipproto_map].alloc_error,
sizeof(applayer_counter_names[alproto][ipproto_map].alloc_error),
"%s%s.alloc", estr, alproto_str);
snprintf(applayer_counter_names[ipproto_map][alproto].parser_error,
sizeof(applayer_counter_names[ipproto_map][alproto].parser_error),
snprintf(applayer_counter_names[alproto][ipproto_map].parser_error,
sizeof(applayer_counter_names[alproto][ipproto_map].parser_error),
"%s%s.parser", estr, alproto_str);
snprintf(applayer_counter_names[ipproto_map][alproto].internal_error,
sizeof(applayer_counter_names[ipproto_map][alproto].internal_error),
snprintf(applayer_counter_names[alproto][ipproto_map].internal_error,
sizeof(applayer_counter_names[alproto][ipproto_map].internal_error),
"%s%s.internal", estr, alproto_str);
AppLayerSetupExceptionPolicyPerProtoCounters(
ipproto_map, alproto, alproto_str, "");
}
} else if (alproto == ALPROTO_FAILED) {
snprintf(applayer_counter_names[ipproto_map][alproto].name,
sizeof(applayer_counter_names[ipproto_map][alproto].name),
"%s%s%s", str, "failed", ipproto_suffix);
snprintf(applayer_counter_names[alproto][ipproto_map].name,
sizeof(applayer_counter_names[alproto][ipproto_map].name), "%s%s%s", str,
"failed", ipproto_suffix);
if (ipproto == IPPROTO_TCP) {
snprintf(applayer_counter_names[ipproto_map][alproto].gap_error,
sizeof(applayer_counter_names[ipproto_map][alproto].gap_error),
snprintf(applayer_counter_names[alproto][ipproto_map].gap_error,
sizeof(applayer_counter_names[alproto][ipproto_map].gap_error),
"%sfailed%s.gap", estr, ipproto_suffix);
}
}
Expand Down Expand Up @@ -1264,41 +1273,41 @@ void AppLayerRegisterThreadCounters(ThreadVars *tv)

for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
if (alprotos[alproto] == 1) {
applayer_counters[ipproto_map][alproto].counter_id =
StatsRegisterCounter(applayer_counter_names[ipproto_map][alproto].name, tv);
applayer_counters[alproto][ipproto_map].counter_id =
StatsRegisterCounter(applayer_counter_names[alproto][ipproto_map].name, tv);

applayer_counters[ipproto_map][alproto].counter_tx_id =
StatsRegisterCounter(applayer_counter_names[ipproto_map][alproto].tx_name, tv);
applayer_counters[alproto][ipproto_map].counter_tx_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].tx_name, tv);

if (ipproto == IPPROTO_TCP) {
applayer_counters[ipproto_map][alproto].gap_error_id = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].gap_error, tv);
applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].gap_error, tv);
}
applayer_counters[ipproto_map][alproto].alloc_error_id = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].alloc_error, tv);
applayer_counters[ipproto_map][alproto].parser_error_id = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].parser_error, tv);
applayer_counters[ipproto_map][alproto].internal_error_id = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].internal_error, tv);
applayer_counters[alproto][ipproto_map].alloc_error_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].alloc_error, tv);
applayer_counters[alproto][ipproto_map].parser_error_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].parser_error, tv);
applayer_counters[alproto][ipproto_map].internal_error_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].internal_error, tv);
/* We don't log stats counters if exception policy is `ignore`/`not set` */
if (g_stats_eps_per_app_proto_errors &&
g_applayerparser_error_policy != EXCEPTION_POLICY_NOT_SET) {
for (enum ExceptionPolicy i = EXCEPTION_POLICY_NOT_SET + 1;
i < EXCEPTION_POLICY_MAX; i++) {
if (IsAppLayerErrorExceptionPolicyStatsValid(i)) {
applayer_counters[ipproto_map][alproto]
applayer_counters[alproto][ipproto_map]
.eps_error.eps_id[i] = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].eps_name[i], tv);
applayer_counter_names[alproto][ipproto_map].eps_name[i], tv);
}
}
}
} else if (alproto == ALPROTO_FAILED) {
applayer_counters[ipproto_map][alproto].counter_id =
StatsRegisterCounter(applayer_counter_names[ipproto_map][alproto].name, tv);
applayer_counters[alproto][ipproto_map].counter_id =
StatsRegisterCounter(applayer_counter_names[alproto][ipproto_map].name, tv);

if (ipproto == IPPROTO_TCP) {
applayer_counters[ipproto_map][alproto].gap_error_id = StatsRegisterCounter(
applayer_counter_names[ipproto_map][alproto].gap_error, tv);
applayer_counters[alproto][ipproto_map].gap_error_id = StatsRegisterCounter(
applayer_counter_names[alproto][ipproto_map].gap_error, tv);
}
}
}
Expand All @@ -1307,8 +1316,8 @@ void AppLayerRegisterThreadCounters(ThreadVars *tv)

void AppLayerDeSetupCounters(void)
{
memset(applayer_counter_names, 0, sizeof(applayer_counter_names));
memset(applayer_counters, 0, sizeof(applayer_counters));
SCFree(applayer_counter_names);
SCFree(applayer_counters);
}

/***** Unittests *****/
Expand Down
2 changes: 1 addition & 1 deletion src/flow-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum {
FLOW_PROTO_MAX,
};
/* max used in app-layer (counters) */
#define FLOW_PROTO_APPLAYER_MAX FLOW_PROTO_UDP + 1
#define FLOW_PROTO_APPLAYER_MAX (FLOW_PROTO_UDP + 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: shouldn't this one be part of c32bc61 :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are possible.
As a matter of facts, I did c32bc61 after finding this parenthesis addition was needed for 897b6606e4ae116337ce9899d34c7ab9c6a14fda


/*
* Variables
Expand Down