suricata
output-stats.c
Go to the documentation of this file.
1/* Copyright (C) 2014-2022 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Victor Julien <victor@inliniac.net>
22 *
23 * Stats Logger Output registration functions
24 */
25
26#include "suricata-common.h"
27#include "output.h"
28#include "output-stats.h"
29#include "util-validate.h"
30
31/** per thread data for this module, contains a list of per thread
32 * data for the packet loggers. */
36
37/* logger instance, a module + a output ctx,
38 * it's perfectly valid that have multiple instances of the same
39 * log module (e.g. http.log) with different output ctx'. */
48
49static OutputStatsLogger *list = NULL;
50
53{
54 OutputStatsLogger *op = SCCalloc(1, sizeof(*op));
55 if (op == NULL)
56 return -1;
57
58 op->LogFunc = LogFunc;
60 op->name = name;
63
64 if (list == NULL)
65 list = op;
66 else {
67 OutputStatsLogger *t = list;
68 while (t->next)
69 t = t->next;
70 t->next = op;
71 }
72
73 SCLogDebug("OutputRegisterStatsLogger happy");
74 return 0;
75}
76
78{
79 DEBUG_VALIDATE_BUG_ON(thread_data == NULL);
80 DEBUG_VALIDATE_BUG_ON(list == NULL);
81
82 OutputStatsLoggerThreadData *op_thread_data = (OutputStatsLoggerThreadData *)thread_data;
83 OutputStatsLogger *logger = list;
84 OutputLoggerThreadStore *store = op_thread_data->store;
85
86 DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
87 DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
88 DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
89
90 while (logger && store) {
91 DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL);
92
93 logger->LogFunc(tv, store->thread_data, st);
94
95 logger = logger->next;
96 store = store->next;
97
98 DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
99 DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
100 }
101
102 return TM_ECODE_OK;
103}
104
105/** \brief thread init for the tx logger
106 * This will run the thread init functions for the individual registered
107 * loggers */
108static TmEcode OutputStatsLogThreadInit(ThreadVars *tv, const void *initdata, void **data)
109{
110 OutputStatsLoggerThreadData *td = SCCalloc(1, sizeof(*td));
111 if (td == NULL)
112 return TM_ECODE_FAILED;
113
114 *data = (void *)td;
115
116 SCLogDebug("OutputStatsLogThreadInit happy (*data %p)", *data);
117
118 OutputStatsLogger *logger = list;
119 while (logger) {
120 if (logger->ThreadInit) {
121 void *retptr = NULL;
122 if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
123 OutputLoggerThreadStore *ts = SCCalloc(1, sizeof(*ts));
124 /* todo */ BUG_ON(ts == NULL);
125
126 /* store thread handle */
127 ts->thread_data = retptr;
128
129 if (td->store == NULL) {
130 td->store = ts;
131 } else {
133 while (tmp->next != NULL)
134 tmp = tmp->next;
135 tmp->next = ts;
136 }
137
138 SCLogDebug("%s is now set up", logger->name);
139 }
140 }
141
142 logger = logger->next;
143 }
144
145 SCLogDebug("OutputStatsLogThreadInit happy (*data %p)", *data);
146 return TM_ECODE_OK;
147}
148
149static TmEcode OutputStatsLogThreadDeinit(ThreadVars *tv, void *thread_data)
150{
151 OutputStatsLoggerThreadData *op_thread_data = (OutputStatsLoggerThreadData *)thread_data;
152 OutputLoggerThreadStore *store = op_thread_data->store;
153 OutputStatsLogger *logger = list;
154
155 while (logger && store) {
156 if (logger->ThreadDeinit) {
157 logger->ThreadDeinit(tv, store->thread_data);
158 }
159 OutputLoggerThreadStore *next_store = store->next;
160 SCFree(store);
161 store = next_store;
162 logger = logger->next;
163 }
164
165 SCFree(op_thread_data);
166 return TM_ECODE_OK;
167}
168
170{
171 tmm_modules[TMM_STATSLOGGER].name = "__stats_logger__";
172 tmm_modules[TMM_STATSLOGGER].ThreadInit = OutputStatsLogThreadInit;
173 tmm_modules[TMM_STATSLOGGER].ThreadDeinit = OutputStatsLogThreadDeinit;
175}
176
178{
179 if (list != NULL)
180 return 1;
181 return 0;
182}
183
185{
186 OutputStatsLogger *logger = list;
187 while (logger) {
188 OutputStatsLogger *next_logger = logger->next;
189 SCFree(logger);
190 logger = next_logger;
191 }
192 list = NULL;
193}
ThreadVars * tv
struct OutputStatsLogger_ OutputStatsLogger
int OutputRegisterStatsLogger(const char *name, StatsLogger LogFunc, OutputCtx *output_ctx, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
int OutputStatsLoggersRegistered(void)
struct OutputStatsLoggerThreadData_ OutputStatsLoggerThreadData
void OutputStatsShutdown(void)
void TmModuleStatsLoggerRegister(void)
TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
int(* StatsLogger)(ThreadVars *, void *thread_data, const StatsTable *)
uint64_t ts
struct OutputLoggerThreadStore_ * next
Definition output.h:35
OutputLoggerThreadStore * store
StatsLogger LogFunc
ThreadInitFunc ThreadInit
struct OutputStatsLogger_ * next
ThreadDeinitFunc ThreadDeinit
const char * name
OutputCtx * output_ctx
Per thread variable structure.
Definition threadvars.h:58
const char * name
Definition tm-modules.h:48
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition tm-modules.h:53
uint8_t cap_flags
Definition tm-modules.h:77
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition tm-modules.h:51
#define BUG_ON(x)
TmModule tmm_modules[TMM_SIZE]
Definition tm-modules.c:29
TmEcode(* ThreadDeinitFunc)(ThreadVars *, void *)
Definition tm-modules.h:44
TmEcode(* ThreadInitFunc)(ThreadVars *, const void *, void **)
Definition tm-modules.h:43
@ TMM_STATSLOGGER
@ TM_ECODE_FAILED
@ TM_ECODE_OK
const char * name
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define DEBUG_VALIDATE_BUG_ON(exp)