69 static uint32_t flowmgr_number = 1;
74 static uint32_t flowrec_number = 1;
80 static SCCtrlCondT flow_manager_ctrl_cond = PTHREAD_COND_INITIALIZER;
81 static SCCtrlMutex flow_manager_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
82 static SCCtrlCondT flow_recycler_ctrl_cond = PTHREAD_COND_INITIALIZER;
83 static SCCtrlMutex flow_recycler_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
146 struct timeval start_ts;
147 struct timeval cur_ts;
148 gettimeofday(&start_ts, NULL);
151 gettimeofday(&cur_ts, NULL);
152 if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
154 "threads to shutdown in time");
191 static bool FlowManagerFlowTimeout(
Flow *f,
SCTime_t ts, uint32_t *next_ts,
const bool emerg)
203 if (*next_ts == 0 || (uint32_t)
SCTIME_SECS(timesout_at) < *next_ts)
224 #ifdef CAPTURE_OFFLOAD
237 if (f->
flow_state != FLOW_STATE_CAPTURE_BYPASSED) {
250 SCLogDebug(
"Updated flow: %" PRIu64
"", FlowGetId(f));
257 pkts_tosrc + pkts_todst);
259 counters->bypassed_pkts += pkts_tosrc + pkts_todst;
260 counters->bypassed_bytes += bytes_tosrc + bytes_todst;
263 SCLogDebug(
"No new packet, dead flow %" PRIu64
"", FlowGetId(f));
271 counters->bypassed_count++;
306 if (f->
proto == IPPROTO_TCP &&
320 if (recycle.
len == 100) {
346 uint32_t checked = 0;
360 if (!FlowManagerFlowTimeout(f,
ts, next_ts, emergency)) {
362 counters->flows_notimeout++;
371 #ifdef CAPTURE_OFFLOAD
374 if (!FlowBypassedTimeout(f,
ts, counters)) {
383 counters->flows_timeout++;
385 RemoveFromHash(f, prev_f);
393 counters->flows_checked += checked;
394 if (checked > counters->rows_maxlen)
395 counters->rows_maxlen = checked;
438 const uint32_t rows_checked = hash_max - hash_min;
439 uint32_t rows_skipped = 0;
440 uint32_t rows_empty = 0;
444 #define TYPE uint64_t
447 #define TYPE uint32_t
451 for (uint32_t idx = hash_min; idx < hash_max; idx+=
BITS) {
453 const uint32_t check =
MIN(
BITS, (hash_max - idx));
454 for (uint32_t i = 0; i < check; i++) {
463 for (uint32_t i = 0; i < check; i++) {
468 if (fb->evicted != NULL || fb->head != NULL) {
469 if (fb->evicted != NULL) {
475 if (fb->head != NULL) {
476 uint32_t next_ts = 0;
477 FlowManagerHashRowTimeout(td, fb->head,
ts, emergency, counters, &next_ts);
482 if (fb->evicted == NULL && fb->head == NULL) {
493 FlowManagerHashRowClearEvictedList(td,
evicted);
500 cnt += ProcessAsideQueue(td, counters);
504 counters->rows_checked += rows_checked;
505 counters->rows_skipped += rows_skipped;
506 counters->rows_empty += rows_empty;
509 cnt += ProcessAsideQueue(td, counters);
511 counters->flows_removed +=
cnt;
533 const uint32_t rows, uint32_t *pos,
const uint32_t instance)
538 uint32_t rows_left = rows;
542 if (start >= hash_max) {
545 end = start + rows_left;
546 if (end > hash_max) {
549 *pos = (end == hash_max) ? hash_min : end;
550 rows_left = rows_left - (end - start);
552 SCLogDebug(
"instance %u: %u:%u (hash_min %u, hash_max %u *pos %u)", instance, start, end,
553 hash_min, hash_max, *pos);
555 cnt += FlowTimeoutHash(td,
ts, start, end, counters);
584 RemoveFromHash(f, NULL);
586 FlowBucket *fb = f->
fb;
606 #define RECYCLE_MAX_QUEUE_ITEMS 25
612 static uint32_t FlowCleanupHash(
void)
622 if (fb->head != NULL) {
624 cnt += FlowManagerHashRowCleanup(fb->head, &local_queue, 0);
626 if (fb->evicted != NULL) {
628 cnt += FlowManagerHashRowCleanup(fb->evicted, &local_queue, 1);
704 static void FlowCountersUpdate(
722 static TmEcode FlowManagerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
739 if ((ftd->
instance + 1) == flowmgr_number) {
749 FlowCountersInit(t, &ftd->
cnt);
786 static void GetWorkUnitSizing(
const uint32_t rows,
const uint32_t mp,
const bool emergency,
787 uint64_t *wu_sleep, uint32_t *wu_rows, uint32_t *rows_sec)
795 const uint32_t emp =
MAX(mp, 10);
796 const uint32_t rows_per_sec = (uint32_t)((
float)rows * (float)((
float)emp / (float)100));
799 const uint32_t work_per_unit =
MIN(rows_per_sec / 1000, 1000);
802 const uint32_t sleep_per_unit =
MAX(250, 1000 - work_per_unit);
803 SCLogDebug(
"mp %u emp %u rows %u rows_sec %u sleep %ums", mp, emp, rows, rows_per_sec,
806 *wu_sleep = sleep_per_unit;
807 *wu_rows = rows_per_sec;
808 *rows_sec = rows_per_sec;
820 const uint32_t rows = ftd->
max - ftd->
min;
823 uint32_t emerg_over_cnt = 0;
824 uint64_t next_run_ms = 0;
825 uint32_t pos = ftd->
min;
826 uint32_t rows_sec = 0;
827 uint32_t rows_per_wu = 0;
828 uint64_t sleep_per_wu = 0;
829 bool prev_emerg =
false;
830 uint32_t other_last_sec = 0;
837 GetWorkUnitSizing(rows, mp,
false, &sleep_per_wu, &rows_per_wu, &rows_sec);
858 const bool emerge_p = (emerg && !prev_emerg);
865 if (ts_ms >= next_run_ms) {
870 if (spare_perc < 90 || spare_perc > 110) {
877 FlowTimeoutCounters counters = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
885 SCLogDebug(
"hash %u:%u slice starting at %u with %u rows", ftd->
min, ftd->
max, pos,
888 const uint32_t ppos = pos;
889 FlowTimeoutHashInChunks(&ftd->
timeout,
ts, ftd->
min, ftd->
max, &counters,
899 FlowCountersUpdate(th_v, ftd, &counters);
902 SCLogDebug(
"flow_sparse_q.len = %" PRIu32
" prealloc: %" PRIu32
903 "flow_spare_q status: %" PRIu32
"%% flows at the queue",
916 if (emerg_over_cnt >= 30) {
923 SCLogNotice(
"Flow emergency mode over, back to normal... unsetting"
924 " FLOW_EMERGENCY bit (ts.tv_sec: %" PRIuMAX
", "
925 "ts.tv_usec:%" PRIuMAX
") flow_spare_q status(): %" PRIu32
926 "%% flows at the queue",
935 const uint32_t pmp = mp;
941 GetWorkUnitSizing(rows, mp, emerg, &sleep_per_wu, &rows_per_wu, &rows_sec);
946 next_run_ms = ts_ms + sleep_per_wu;
948 if (other_last_sec == 0 || other_last_sec < (uint32_t)
SCTIME_SECS(
ts)) {
968 if (emerg || !time_is_live) {
971 struct timeval cond_tv;
972 gettimeofday(&cond_tv, NULL);
973 struct timeval add_tv;
974 add_tv.tv_sec = sleep_per_wu / 1000;
975 add_tv.tv_usec = (sleep_per_wu % 1000) * 1000;
976 timeradd(&cond_tv, &add_tv, &cond_tv);
985 &flow_manager_ctrl_cond, &flow_manager_ctrl_mutex, &cond_time);
986 if (rc == ETIMEDOUT || rc < 0) {
1003 intmax_t setting = 1;
1006 if (setting < 1 || setting > 1024) {
1007 FatalError(
"invalid flow.managers setting %" PRIdMAX, setting);
1009 flowmgr_number = (uint32_t)setting;
1011 SCLogConfig(
"using %u flow manager threads", flowmgr_number);
1014 for (uint32_t u = 0; u < flowmgr_number; u++) {
1020 BUG_ON(tv_flowmgr == NULL);
1022 if (tv_flowmgr == NULL) {
1023 FatalError(
"flow manager thread creation failed");
1026 FatalError(
"flow manager thread spawn failed");
1043 static TmEcode FlowRecyclerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
1049 SCLogError(
"initializing flow log API for thread failed");
1086 FlowEndCountersUpdate(
tv, &ftd->
fec, f);
1105 uint64_t recycled_cnt = 0;
1126 Recycler(th_v, ftd, f);
1135 if (ret_queue.
len > 0) {
1139 recycled_cnt +=
cnt;
1149 if (emerg || !time_is_live) {
1152 struct timeval cond_tv;
1153 gettimeofday(&cond_tv, NULL);
1154 cond_tv.tv_sec += 1;
1165 &flow_recycler_ctrl_cond, &flow_recycler_ctrl_mutex, &cond_time);
1166 if (rc == ETIMEDOUT || rc < 0) {
1178 SCLogPerf(
"%"PRIu64
" flows processed", recycled_cnt);
1182 static bool FlowRecyclerReadyToShutdown(
void)
1192 return ((
len == 0));
1198 intmax_t setting = 1;
1201 if (setting < 1 || setting > 1024) {
1202 FatalError(
"invalid flow.recyclers setting %" PRIdMAX, setting);
1204 flowrec_number = (uint32_t)setting;
1206 SCLogConfig(
"using %u flow recycler threads", flowrec_number);
1208 for (uint32_t u = 0; u < flowrec_number; u++) {
1215 if (tv_flowrec == NULL) {
1216 FatalError(
"flow recycler thread creation failed");
1219 FatalError(
"flow recycler thread spawn failed");
1236 (void)FlowCleanupHash();
1238 uint32_t flows = FlowCleanupHash();
1246 }
while (!FlowRecyclerReadyToShutdown());
1259 struct timeval start_ts;
1260 struct timeval cur_ts;
1261 gettimeofday(&start_ts, NULL);
1264 gettimeofday(&cur_ts, NULL);
1265 if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
1266 FatalError(
"unable to get all flow recycler "
1267 "threads to shutdown in time");