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'. */
40 typedef struct OutputStatsLogger_ {
44  const char *name;
48 
49 static OutputStatsLogger *list = NULL;
50 
53 {
54  OutputStatsLogger *op = SCCalloc(1, sizeof(*op));
55  if (op == NULL)
56  return -1;
57 
58  op->LogFunc = LogFunc;
59  op->output_ctx = output_ctx;
60  op->name = name;
61  op->ThreadInit = ThreadInit;
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 
77 TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
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 */
108 static 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 {
132  OutputLoggerThreadStore *tmp = td->store;
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 
149 static 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 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:73
ts
uint64_t ts
Definition: source-erf-file.c:55
OutputLoggerThreadStore_
Definition: output.h:33
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
TMM_STATSLOGGER
@ TMM_STATSLOGGER
Definition: tm-threads-common.h:61
OutputLoggerThreadStore_::next
struct OutputLoggerThreadStore_ * next
Definition: output.h:35
TmModuleStatsLoggerRegister
void TmModuleStatsLoggerRegister(void)
Definition: output-stats.c:169
OutputStatsShutdown
void OutputStatsShutdown(void)
Definition: output-stats.c:184
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
OutputRegisterStatsLogger
int OutputRegisterStatsLogger(const char *name, StatsLogger LogFunc, OutputCtx *output_ctx, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Definition: output-stats.c:51
OutputStatsLogger
struct OutputStatsLogger_ OutputStatsLogger
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:84
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
OutputStatsLogger_::ThreadDeinit
ThreadDeinitFunc ThreadDeinit
Definition: output-stats.c:46
OutputLoggerThreadStore_::thread_data
void * thread_data
Definition: output.h:34
OutputStatsLoggerThreadData_::store
OutputLoggerThreadStore * store
Definition: output-stats.c:34
OutputStatsLogger_::ThreadInit
ThreadInitFunc ThreadInit
Definition: output-stats.c:45
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
ThreadInitFunc
TmEcode(* ThreadInitFunc)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:39
OutputStatsLogger_
Definition: output-stats.c:40
OutputStatsLoggerThreadData_
Definition: output-stats.c:33
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
StatsTable_
Definition: output-stats.h:39
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
OutputStatsLogger_::name
const char * name
Definition: output-stats.c:44
OutputStatsLogger_::output_ctx
OutputCtx * output_ctx
Definition: output-stats.c:42
TmEcode
TmEcode
Definition: tm-threads-common.h:79
TmModule_::name
const char * name
Definition: tm-modules.h:44
OutputStatsLogger_::next
struct OutputStatsLogger_ * next
Definition: output-stats.c:43
suricata-common.h
OutputStatsLogger_::LogFunc
StatsLogger LogFunc
Definition: output-stats.c:41
OutputStatsLoggerThreadData
struct OutputStatsLoggerThreadData_ OutputStatsLoggerThreadData
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
util-validate.h
OutputStatsLoggersRegistered
int OutputStatsLoggersRegistered(void)
Definition: output-stats.c:177
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StatsLogger
int(* StatsLogger)(ThreadVars *, void *thread_data, const StatsTable *)
Definition: output-stats.h:50
output-stats.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
OutputStatsLog
TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
Definition: output-stats.c:77
output.h
ThreadDeinitFunc
TmEcode(* ThreadDeinitFunc)(ThreadVars *, void *)
Definition: tm-modules.h:40