68static uint32_t flowmgr_number = 1;
73static uint32_t flowrec_number = 1;
79static SCCtrlCondT flow_manager_ctrl_cond = PTHREAD_COND_INITIALIZER;
80static SCCtrlMutex flow_manager_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
81static SCCtrlCondT flow_recycler_ctrl_cond = PTHREAD_COND_INITIALIZER;
82static SCCtrlMutex flow_recycler_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
145 struct timeval start_ts;
146 struct timeval cur_ts;
147 gettimeofday(&start_ts, NULL);
150 gettimeofday(&cur_ts, NULL);
151 if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
153 "threads to shutdown in time");
190static bool FlowManagerFlowTimeout(
Flow *f,
SCTime_t ts, uint32_t *next_ts,
const bool emerg)
202 if (*next_ts == 0 || (uint32_t)
SCTIME_SECS(timesout_at) < *next_ts)
223#ifdef CAPTURE_OFFLOAD
236 if (f->
flow_state != FLOW_STATE_CAPTURE_BYPASSED) {
249 SCLogDebug(
"Updated flow: %" PRIu64
"", FlowGetId(f));
256 pkts_tosrc + pkts_todst);
258 counters->bypassed_pkts += pkts_tosrc + pkts_todst;
259 counters->bypassed_bytes += bytes_tosrc + bytes_todst;
262 SCLogDebug(
"No new packet, dead flow %" PRIu64
"", FlowGetId(f));
270 counters->bypassed_count++;
305 if (f->
proto == IPPROTO_TCP &&
319 if (recycle.
len == 100) {
345 uint32_t checked = 0;
359 if (!FlowManagerFlowTimeout(f,
ts, next_ts, emergency)) {
361 counters->flows_notimeout++;
370#ifdef CAPTURE_OFFLOAD
373 if (!FlowBypassedTimeout(f,
ts, counters)) {
382 counters->flows_timeout++;
384 RemoveFromHash(f, prev_f);
392 counters->flows_checked += checked;
393 if (checked > counters->rows_maxlen)
394 counters->rows_maxlen = checked;
437 const uint32_t rows_checked = hash_max - hash_min;
438 uint32_t rows_skipped = 0;
439 uint32_t rows_empty = 0;
450 for (uint32_t idx = hash_min; idx < hash_max; idx+=
BITS) {
452 const uint32_t check =
MIN(
BITS, (hash_max - idx));
453 for (uint32_t i = 0; i < check; i++) {
462 for (uint32_t i = 0; i < check; i++) {
467 if (fb->evicted != NULL || fb->head != NULL) {
468 if (fb->evicted != NULL) {
474 if (fb->head != NULL) {
475 uint32_t next_ts = 0;
476 FlowManagerHashRowTimeout(td, fb->head,
ts, emergency, counters, &next_ts);
481 if (fb->evicted == NULL && fb->head == NULL) {
492 FlowManagerHashRowClearEvictedList(td,
evicted);
499 cnt += ProcessAsideQueue(td, counters);
503 counters->rows_checked += rows_checked;
504 counters->rows_skipped += rows_skipped;
505 counters->rows_empty += rows_empty;
508 cnt += ProcessAsideQueue(td, counters);
510 counters->flows_removed +=
cnt;
532 const uint32_t rows, uint32_t *pos,
const uint32_t instance)
537 uint32_t rows_left = rows;
541 if (start >= hash_max) {
544 end = start + rows_left;
545 if (end > hash_max) {
548 *pos = (end == hash_max) ? hash_min : end;
549 rows_left = rows_left - (end - start);
551 SCLogDebug(
"instance %u: %u:%u (hash_min %u, hash_max %u *pos %u)", instance, start, end,
552 hash_min, hash_max, *pos);
554 cnt += FlowTimeoutHash(td,
ts, start, end, counters);
583 RemoveFromHash(f, NULL);
585 FlowBucket *fb = f->
fb;
605#define RECYCLE_MAX_QUEUE_ITEMS 25
611static uint32_t FlowCleanupHash(
void)
621 if (fb->head != NULL) {
623 cnt += FlowManagerHashRowCleanup(fb->head, &local_queue, 0);
625 if (fb->evicted != NULL) {
627 cnt += FlowManagerHashRowCleanup(fb->evicted, &local_queue, 1);
703static void FlowCountersUpdate(
721static TmEcode FlowManagerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
738 if ((ftd->
instance + 1) == flowmgr_number) {
748 FlowCountersInit(t, &ftd->
cnt);
785static void GetWorkUnitSizing(
const uint32_t rows,
const uint32_t mp,
const bool emergency,
786 uint64_t *wu_sleep, uint32_t *wu_rows, uint32_t *rows_sec)
794 const uint32_t emp =
MAX(mp, 10);
795 const uint32_t rows_per_sec = (uint32_t)((
float)rows * (float)((
float)emp / (float)100));
798 const uint32_t work_per_unit =
MIN(rows_per_sec / 1000, 1000);
801 const uint32_t sleep_per_unit =
MAX(250, 1000 - work_per_unit);
802 SCLogDebug(
"mp %u emp %u rows %u rows_sec %u sleep %ums", mp, emp, rows, rows_per_sec,
805 *wu_sleep = sleep_per_unit;
806 *wu_rows = rows_per_sec;
807 *rows_sec = rows_per_sec;
819 const uint32_t rows = ftd->
max - ftd->
min;
822 uint32_t emerg_over_cnt = 0;
823 uint64_t next_run_ms = 0;
824 uint32_t pos = ftd->
min;
825 uint32_t rows_sec = 0;
826 uint32_t rows_per_wu = 0;
827 uint64_t sleep_per_wu = 0;
828 bool prev_emerg =
false;
829 uint32_t other_last_sec = 0;
836 GetWorkUnitSizing(rows, mp,
false, &sleep_per_wu, &rows_per_wu, &rows_sec);
857 const bool emerge_p = (emerg && !prev_emerg);
864 if (ts_ms >= next_run_ms) {
869 if (spare_perc < 90 || spare_perc > 110) {
876 FlowTimeoutCounters counters = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
884 SCLogDebug(
"hash %u:%u slice starting at %u with %u rows", ftd->
min, ftd->
max, pos,
887 const uint32_t ppos = pos;
888 FlowTimeoutHashInChunks(&ftd->
timeout,
ts, ftd->
min, ftd->
max, &counters,
898 FlowCountersUpdate(th_v, ftd, &counters);
901 SCLogDebug(
"flow_sparse_q.len = %" PRIu32
" prealloc: %" PRIu32
902 "flow_spare_q status: %" PRIu32
"%% flows at the queue",
915 if (emerg_over_cnt >= 30) {
922 SCLogNotice(
"Flow emergency mode over, back to normal... unsetting"
923 " FLOW_EMERGENCY bit (ts.tv_sec: %" PRIuMAX
", "
924 "ts.tv_usec:%" PRIuMAX
") flow_spare_q status(): %" PRIu32
925 "%% flows at the queue",
934 const uint32_t pmp = mp;
940 GetWorkUnitSizing(rows, mp, emerg, &sleep_per_wu, &rows_per_wu, &rows_sec);
945 next_run_ms = ts_ms + sleep_per_wu;
947 if (other_last_sec == 0 || other_last_sec < (uint32_t)
SCTIME_SECS(
ts)) {
967 if (emerg || !time_is_live) {
970 struct timeval cond_tv;
971 gettimeofday(&cond_tv, NULL);
972 struct timeval add_tv;
973 add_tv.tv_sec = sleep_per_wu / 1000;
974 add_tv.tv_usec = (sleep_per_wu % 1000) * 1000;
975 timeradd(&cond_tv, &add_tv, &cond_tv);
984 &flow_manager_ctrl_cond, &flow_manager_ctrl_mutex, &cond_time);
985 if (rc == ETIMEDOUT || rc < 0) {
1002 intmax_t setting = 1;
1005 if (setting < 1 || setting > 1024) {
1006 FatalError(
"invalid flow.managers setting %" PRIdMAX, setting);
1008 flowmgr_number = (uint32_t)setting;
1010 SCLogConfig(
"using %u flow manager threads", flowmgr_number);
1013 for (uint32_t u = 0; u < flowmgr_number; u++) {
1019 BUG_ON(tv_flowmgr == NULL);
1021 if (tv_flowmgr == NULL) {
1022 FatalError(
"flow manager thread creation failed");
1025 FatalError(
"flow manager thread spawn failed");
1042static TmEcode FlowRecyclerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
1048 SCLogError(
"initializing flow log API for thread failed");
1085 FlowEndCountersUpdate(
tv, &ftd->
fec, f);
1104 uint64_t recycled_cnt = 0;
1125 Recycler(th_v, ftd, f);
1134 if (ret_queue.
len > 0) {
1138 recycled_cnt +=
cnt;
1148 if (emerg || !time_is_live) {
1151 struct timeval cond_tv;
1152 gettimeofday(&cond_tv, NULL);
1153 cond_tv.tv_sec += 1;
1164 &flow_recycler_ctrl_cond, &flow_recycler_ctrl_mutex, &cond_time);
1165 if (rc == ETIMEDOUT || rc < 0) {
1177 SCLogPerf(
"%"PRIu64
" flows processed", recycled_cnt);
1181static bool FlowRecyclerReadyToShutdown(
void)
1191 return ((
len == 0));
1197 intmax_t setting = 1;
1200 if (setting < 1 || setting > 1024) {
1201 FatalError(
"invalid flow.recyclers setting %" PRIdMAX, setting);
1203 flowrec_number = (uint32_t)setting;
1205 SCLogConfig(
"using %u flow recycler threads", flowrec_number);
1207 for (uint32_t u = 0; u < flowrec_number; u++) {
1214 if (tv_flowrec == NULL) {
1215 FatalError(
"flow recycler thread creation failed");
1218 FatalError(
"flow recycler thread spawn failed");
1235 (void)FlowCleanupHash();
1237 uint32_t flows = FlowCleanupHash();
1245 }
while (!FlowRecyclerReadyToShutdown());
1258 struct timeval start_ts;
1259 struct timeval cur_ts;
1260 gettimeofday(&start_ts, NULL);
1263 gettimeofday(&cur_ts, NULL);
1264 if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
1265 FatalError(
"unable to get all flow recycler "
1266 "threads to shutdown in time");
uint32_t HttpRangeContainersTimeoutHash(const SCTime_t ts)
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
uint16_t StatsRegisterGlobalCounter(const char *name, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
void StatsDecr(ThreadVars *tv, uint16_t id)
Decrements the local counter.
void StatsSyncCounters(ThreadVars *tv)
uint16_t StatsRegisterMaxCounter(const char *name, struct ThreadVars_ *tv)
Registers a counter, whose value holds the maximum of all the values assigned to it.
uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
Registers a normal, unqualified counter.
void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Sets a value of type double to the local counter.
void StatsSyncCountersIfSignalled(ThreadVars *tv)
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
uint16_t StatsRegisterAvgCounter(const char *name, struct ThreadVars_ *tv)
Registers a counter, whose value holds the average of all the values assigned to it.
void StatsAddUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Adds a value of type uint64_t to the local counter.
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
uint32_t DefragTimeoutHash(SCTime_t ts)
time out tracker from the hash
void SCFlowRunFinishCallbacks(ThreadVars *tv, Flow *f)
#define FBLOCK_UNLOCK(fb)
void FlowTimeoutsEmergency(void)
void FlowManagerThreadSpawn(void)
spawn the flow manager thread
void FlowWakeupFlowManagerThread(void)
void TmModuleFlowRecyclerRegister(void)
void FlowRecyclerThreadSpawn(void)
spawn the flow recycler thread
void FlowTimeoutsInit(void)
void FlowWakeupFlowRecyclerThread(void)
#define RECYCLE_MAX_QUEUE_ITEMS
void FlowDisableFlowManagerThread(void)
Used to disable flow manager thread(s).
struct FlowManagerThreadData_ FlowManagerThreadData
struct FlowTimeoutCounters_ FlowTimeoutCounters
void TmModuleFlowManagerRegister(void)
void FlowDisableFlowRecyclerThread(void)
Used to disable flow recycler thread(s).
struct FlowRecyclerThreadData_ FlowRecyclerThreadData
struct FlowCounters_ FlowCounters
#define FlowTimeoutsReset()
FlowProtoTimeout flow_timeouts_normal[FLOW_PROTO_MAX]
FlowProtoTimeout flow_timeouts_emerg[FLOW_PROTO_MAX]
void FlowQueuePrivateAppendFlow(FlowQueuePrivate *fqc, Flow *f)
Flow * FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqc)
void FlowQueueAppendPrivate(FlowQueue *fq, FlowQueuePrivate *fqc)
FlowQueuePrivate FlowQueueExtractPrivate(FlowQueue *fq)
uint32_t FlowSpareGetPoolSize(void)
void FlowSparePoolUpdate(uint32_t size)
void FlowSparePoolReturnFlows(FlowQueuePrivate *fqp)
#define FLOW_SPARE_POOL_BLOCK_SIZE
void * FlowGetStorageById(const Flow *f, FlowStorageId id)
void FlowSendToLocalThread(Flow *f)
bool FlowNeedsReassembly(Flow *f)
Check if a flow needs forced reassembly, or any other processing.
void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec)
FlowStorageId GetFlowBypassInfoID(void)
int FlowClearMemory(Flow *f, uint8_t proto_map)
Function clear the flow memory before queueing it to spare flow queue.
uint64_t FlowGetMemuse(void)
#define FLOW_END_FLAG_TIMEOUT
#define FLOW_TIMEOUT_REASSEMBLY_DONE
#define FLOW_END_FLAG_SHUTDOWN
#define FLOWLOCK_UNLOCK(fb)
#define FLOWLOCK_WRLOCK(fb)
uint32_t ThresholdsExpire(const SCTime_t ts)
uint32_t HostTimeoutHash(SCTime_t ts)
time out hosts from the hash
uint32_t IPPairTimeoutHash(SCTime_t ts)
time out ippairs from the hash
TmEcode OutputFlowLogThreadDeinit(ThreadVars *tv, void *thread_data)
TmEcode OutputFlowLogThreadInit(ThreadVars *tv, void **data)
thread init for the flow logger This will run the thread init functions for the individual registered...
TmEcode OutputFlowLog(ThreadVars *tv, void *thread_data, Flow *f)
Run flow logger(s)
float MemcapsGetPressure(void)
const char * thread_name_flow_mgr
const char * thread_name_flow_rec
void StreamTcpThreadCacheCleanup(void)
bool(* BypassUpdate)(Flow *f, void *data, time_t tsec)
uint32_t emergency_recovery
uint16_t flow_mgr_full_pass
uint16_t flow_emerg_mode_enter
uint16_t flow_mgr_rows_sec
uint16_t flow_mgr_flows_checked
uint16_t flow_mgr_rows_maxlen
uint16_t flow_mgr_flows_aside
uint16_t flow_bypassed_pkts
uint16_t memcap_pressure_max
uint16_t flow_bypassed_bytes
uint16_t flow_mgr_flows_timeout
uint16_t flow_emerg_mode_over
uint16_t flow_mgr_flows_notimeout
uint16_t flow_mgr_flows_aside_needs_work
uint16_t flow_bypassed_cnt_clo
FlowManagerTimeoutThread timeout
uint16_t counter_defrag_timeout
uint16_t counter_defrag_memuse
FlowQueuePrivate aside_queue
void * output_thread_data
uint16_t counter_flow_active
uint16_t counter_queue_max
uint16_t counter_tcp_active_sessions
uint16_t counter_queue_avg
uint32_t flows_aside_needs_work
uint32_t flows_aside_needs_work
struct LiveDevice_ * livedev
FlowThreadId thread_id[2]
Per thread variable structure.
struct ThreadVars_ * next
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
TmEcode(* Management)(ThreadVars *, void *)
volatile uint8_t suricata_ctl_flags
#define SCCtrlCondTimedwait
#define SCCtrlMutexLock(mut)
#define SCMutexUnlock(mut)
#define SCCtrlMutexUnlock(mut)
TmModule tmm_modules[TMM_SIZE]
#define TM_FLAG_MANAGEMENT_TM
SCTime_t TmThreadsGetThreadTime(const int idx)
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
ThreadVars * TmThreadCreateMgmtThreadByName(const char *name, const char *module, int mucond)
Creates and returns the TV instance for a Management thread(MGMT). This function supports only custom...
ThreadVars * tv_root[TVT_MAX]
bool TmThreadsWaitForUnpause(ThreadVars *tv)
Wait for a thread to become unpaused.
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
#define TM_THREAD_NAME_MAX
void PacketPoolDestroy(void)
void PacketPoolInit(void)
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
#define SC_ATOMIC_LOAD_EXPLICIT(name, order)
#define SC_ATOMIC_AND(name, val)
Bitwise AND a value to our atomic variable.
#define SC_ATOMIC_INITPTR(name)
#define SC_ATOMIC_EXTERN(type, name)
wrapper for referencing an atomic variable declared on another file.
#define SC_ATOMIC_DECLARE(type, name)
wrapper for declaring atomic variables.
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
#define SC_ATOMIC_MEMORY_ORDER_RELAXED
#define SCLogNotice(...)
Macro used to log NOTICE messages.
#define SCLogError(...)
Macro used to log ERROR messages.
void LiveDevSubBypassStats(LiveDevice *dev, uint64_t cnt, int family)
bool TimeModeIsLive(void)
bool TimeModeIsReady(void)
#define timeradd(a, b, r)
#define SCTIME_CMP_LT(a, b)
#define SCTIME_ADD_SECS(ts, s)
#define FROM_TIMEVAL(timev)
initialize a 'struct timespec' from a 'struct timeval'.
#define DEBUG_VALIDATE_BUG_ON(exp)