|
|
|
/* Copyright (C) 2007-2013 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
|
|
|
|
* Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* version 2 along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \author Victor Julien <victor@inliniac.net>
|
|
|
|
*
|
|
|
|
* Flow Logger Output registration functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "suricata-common.h"
|
|
|
|
#include "tm-modules.h"
|
|
|
|
#include "output-flow.h"
|
|
|
|
#include "util-profiling.h"
|
|
|
|
#include "util-validate.h"
|
|
|
|
|
|
|
|
typedef struct OutputLoggerThreadStore_ {
|
|
|
|
void *thread_data;
|
|
|
|
struct OutputLoggerThreadStore_ *next;
|
|
|
|
} OutputLoggerThreadStore;
|
|
|
|
|
|
|
|
/** per thread data for this module, contains a list of per thread
|
|
|
|
* data for the packet loggers. */
|
|
|
|
typedef struct OutputLoggerThreadData_ {
|
|
|
|
OutputLoggerThreadStore *store;
|
|
|
|
} OutputLoggerThreadData;
|
|
|
|
|
|
|
|
/* logger instance, a module + a output ctx,
|
|
|
|
* it's perfectly valid that have multiple instances of the same
|
|
|
|
* log module (e.g. http.log) with different output ctx'. */
|
|
|
|
typedef struct OutputFlowLogger_ {
|
|
|
|
FlowLogger LogFunc;
|
|
|
|
OutputCtx *output_ctx;
|
|
|
|
struct OutputFlowLogger_ *next;
|
|
|
|
const char *name;
|
|
|
|
TmEcode (*ThreadInit)(ThreadVars *, const void *, void **);
|
|
|
|
TmEcode (*ThreadDeinit)(ThreadVars *, void *);
|
|
|
|
void (*ThreadExitPrintStats)(ThreadVars *, void *);
|
|
|
|
} OutputFlowLogger;
|
|
|
|
|
|
|
|
static OutputFlowLogger *list = NULL;
|
|
|
|
|
|
|
|
int OutputRegisterFlowLogger(const char *name, FlowLogger LogFunc,
|
|
|
|
OutputCtx *output_ctx, ThreadInitFunc ThreadInit,
|
|
|
|
ThreadDeinitFunc ThreadDeinit,
|
|
|
|
ThreadExitPrintStatsFunc ThreadExitPrintStats)
|
|
|
|
{
|
|
|
|
OutputFlowLogger *op = SCMalloc(sizeof(*op));
|
|
|
|
if (op == NULL)
|
|
|
|
return -1;
|
|
|
|
memset(op, 0x00, sizeof(*op));
|
|
|
|
|
|
|
|
op->LogFunc = LogFunc;
|
|
|
|
op->output_ctx = output_ctx;
|
|
|
|
op->name = name;
|
|
|
|
op->ThreadInit = ThreadInit;
|
|
|
|
op->ThreadDeinit = ThreadDeinit;
|
|
|
|
op->ThreadExitPrintStats = ThreadExitPrintStats;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
list = op;
|
|
|
|
else {
|
|
|
|
OutputFlowLogger *t = list;
|
|
|
|
while (t->next)
|
|
|
|
t = t->next;
|
|
|
|
t->next = op;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCLogDebug("OutputRegisterFlowLogger happy");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Run flow logger(s)
|
|
|
|
* \note flow is already write locked
|
|
|
|
*/
|
|
|
|
TmEcode OutputFlowLog(ThreadVars *tv, void *thread_data, Flow *f)
|
|
|
|
{
|
|
|
|
DEBUG_VALIDATE_BUG_ON(thread_data == NULL);
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
flow: redesign of flow timeout handling
Goals:
- reduce locking
- take advantage of 'hot' caches
- better locality
Locking reduction
New flow spare pool. The global pool is implmented as a list of blocks,
where each block has a 100 spare flows. Worker threads fetch a block at
a time, storing the block in the local thread storage.
Flow Recycler now returns flows to the pool is blocks as well.
Flow Recycler fetches all flows to be processed in one step instead of
one at a time.
Cache 'hot'ness
Worker threads now check the timeout of flows they evaluate during lookup.
The worker will have to read the flow into cache anyway, so the added
overhead of checking the timeout value is minimal. When a flow is considered
timed out, one of 2 things happens:
- if the flow is 'owned' by the thread it is handled locally. Handling means
checking if the flow needs 'timeout' work.
- otherwise, the flow is added to a special 'evicted' list in the flow
bucket where it will be picked up by the flow manager.
Flow Manager timing
By default the flow manager now tries to do passes of the flow hash in
smaller steps, where the goal is to do full pass in 8 x the lowest timeout
value it has to enforce. So if the lowest timeout value is 30s, a full pass
will take 4 minutes. The goal here is to reduce locking overhead and not
get in the way of the workers.
In emergency mode each pass is full, and lower timeouts are used.
Timing of the flow manager is also no longer relying on pthread condition
variables, as these generally cause waking up much quicker than the desired
timout. Instead a simple (u)sleep loop is used.
Both changes reduce the number of hash passes a lot.
Emergency behavior
In emergency mode there a number of changes to the workers. In this scenario
the flow memcap is fully used up and it is unavoidable that some flows won't
be tracked.
1. flow spare pool fetches are reduced to once a second. This avoids locking
overhead, while the chance of success was very low.
2. getting an active flow directly from the hash skips flows that had very
recent activity to avoid the scenario where all flows get only into the
NEW state before getting reused. Rather allow some to have a chance of
completing.
3. TCP packets that are not SYN packets will not get a used flow, unless
stream.midstream is enabled. The goal here is again to avoid evicting
active flows unnecessarily.
Better Localily
Flow Manager injects flows into the worker threads now, instead of one or
two packets. Advantage of this is that the worker threads can get packets
from their local packet pools, avoiding constant overhead of packets returning
to 'foreign' pools.
Counters
A lot of flow counters have been added and some have been renamed.
Overall the worker threads increment 'flow.wrk.*' counters, while the flow
manager increments 'flow.mgr.*'.
Additionally, none of the counters are snapshots anymore, they all increment
over time. The flow.memuse and flow.spare counters are exceptions.
Misc
FlowQueue has been split into a FlowQueuePrivate (unlocked) and FlowQueue.
Flow no longer has 'prev' pointers and used a unified 'next' pointer for
both hash and queue use.
6 years ago
|
|
|
FlowSetEndFlags(f);
|
|
|
|
|
|
|
|
OutputLoggerThreadData *op_thread_data = (OutputLoggerThreadData *)thread_data;
|
|
|
|
OutputFlowLogger *logger = list;
|
|
|
|
OutputLoggerThreadStore *store = op_thread_data->store;
|
|
|
|
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
|
|
|
|
|
|
|
|
logger = list;
|
|
|
|
store = op_thread_data->store;
|
|
|
|
while (logger && store) {
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL);
|
|
|
|
|
|
|
|
SCLogDebug("logger %p", logger);
|
|
|
|
//PACKET_PROFILING_LOGGER_START(p, logger->module_id);
|
|
|
|
logger->LogFunc(tv, store->thread_data, f);
|
|
|
|
//PACKET_PROFILING_LOGGER_END(p, logger->module_id);
|
|
|
|
|
|
|
|
logger = logger->next;
|
|
|
|
store = store->next;
|
|
|
|
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
|
|
|
|
DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief thread init for the flow logger
|
|
|
|
* This will run the thread init functions for the individual registered
|
|
|
|
* loggers */
|
|
|
|
TmEcode OutputFlowLogThreadInit(ThreadVars *tv, void *initdata, void **data)
|
|
|
|
{
|
|
|
|
OutputLoggerThreadData *td = SCMalloc(sizeof(*td));
|
|
|
|
if (td == NULL)
|
|
|
|
return TM_ECODE_FAILED;
|
|
|
|
memset(td, 0x00, sizeof(*td));
|
|
|
|
|
|
|
|
*data = (void *)td;
|
|
|
|
|
|
|
|
SCLogDebug("OutputFlowLogThreadInit happy (*data %p)", *data);
|
|
|
|
|
|
|
|
OutputFlowLogger *logger = list;
|
|
|
|
while (logger) {
|
|
|
|
if (logger->ThreadInit) {
|
|
|
|
void *retptr = NULL;
|
|
|
|
if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
|
|
|
|
OutputLoggerThreadStore *ts = SCMalloc(sizeof(*ts));
|
|
|
|
/* todo */ BUG_ON(ts == NULL);
|
|
|
|
memset(ts, 0x00, sizeof(*ts));
|
|
|
|
|
|
|
|
/* store thread handle */
|
|
|
|
ts->thread_data = retptr;
|
|
|
|
|
|
|
|
if (td->store == NULL) {
|
|
|
|
td->store = ts;
|
|
|
|
} else {
|
|
|
|
OutputLoggerThreadStore *tmp = td->store;
|
|
|
|
while (tmp->next != NULL)
|
|
|
|
tmp = tmp->next;
|
|
|
|
tmp->next = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCLogDebug("%s is now set up", logger->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = logger->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
TmEcode OutputFlowLogThreadDeinit(ThreadVars *tv, void *thread_data)
|
|
|
|
{
|
|
|
|
OutputLoggerThreadData *op_thread_data = (OutputLoggerThreadData *)thread_data;
|
|
|
|
if (op_thread_data == NULL)
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
|
|
|
OutputLoggerThreadStore *store = op_thread_data->store;
|
|
|
|
OutputFlowLogger *logger = list;
|
|
|
|
|
|
|
|
while (logger && store) {
|
|
|
|
if (logger->ThreadDeinit) {
|
|
|
|
logger->ThreadDeinit(tv, store->thread_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputLoggerThreadStore *next_store = store->next;
|
|
|
|
SCFree(store);
|
|
|
|
store = next_store;
|
|
|
|
logger = logger->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCFree(op_thread_data);
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputFlowLogExitPrintStats(ThreadVars *tv, void *thread_data)
|
|
|
|
{
|
|
|
|
OutputLoggerThreadData *op_thread_data = (OutputLoggerThreadData *)thread_data;
|
|
|
|
OutputLoggerThreadStore *store = op_thread_data->store;
|
|
|
|
OutputFlowLogger *logger = list;
|
|
|
|
|
|
|
|
while (logger && store) {
|
|
|
|
if (logger->ThreadExitPrintStats) {
|
|
|
|
logger->ThreadExitPrintStats(tv, store->thread_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = logger->next;
|
|
|
|
store = store->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputFlowShutdown(void)
|
|
|
|
{
|
|
|
|
OutputFlowLogger *logger = list;
|
|
|
|
while (logger) {
|
|
|
|
OutputFlowLogger *next_logger = logger->next;
|
|
|
|
SCFree(logger);
|
|
|
|
logger = next_logger;
|
|
|
|
}
|
|
|
|
list = NULL;
|
|
|
|
}
|