68 static uint32_t flowmgr_number = 1;
73 static uint32_t flowrec_number = 1;
79 static SCCtrlCondT flow_manager_ctrl_cond = PTHREAD_COND_INITIALIZER;
80 static SCCtrlMutex flow_manager_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
81 static SCCtrlCondT flow_recycler_ctrl_cond = PTHREAD_COND_INITIALIZER;
82 static 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");
184 static bool FlowManagerFlowTimeout(
Flow *f,
SCTime_t ts, uint32_t *next_ts,
const bool emerg)
191 if (*next_ts == 0 || flow_times_out_at < *next_ts)
192 *next_ts = flow_times_out_at;
202 #ifdef CAPTURE_OFFLOAD
215 if (f->
flow_state != FLOW_STATE_CAPTURE_BYPASSED) {
228 SCLogDebug(
"Updated flow: %"PRId64
"", FlowGetId(f));
235 pkts_tosrc + pkts_todst);
237 counters->bypassed_pkts += pkts_tosrc + pkts_todst;
238 counters->bypassed_bytes += bytes_tosrc + bytes_todst;
241 SCLogDebug(
"No new packet, dead flow %" PRId64
"", FlowGetId(f));
249 counters->bypassed_count++;
284 if (f->
proto == IPPROTO_TCP &&
298 if (recycle.
len == 100) {
324 uint32_t checked = 0;
336 if (FlowManagerFlowTimeout(f,
ts, next_ts, emergency) ==
false) {
337 counters->flows_notimeout++;
348 #ifdef CAPTURE_OFFLOAD
351 if (!FlowBypassedTimeout(f,
ts, counters)) {
360 counters->flows_timeout++;
362 RemoveFromHash(f, prev_f);
370 counters->flows_checked += checked;
371 if (checked > counters->rows_maxlen)
372 counters->rows_maxlen = checked;
415 const uint32_t rows_checked = hash_max - hash_min;
416 uint32_t rows_skipped = 0;
417 uint32_t rows_empty = 0;
421 #define TYPE uint64_t
424 #define TYPE uint32_t
428 for (uint32_t idx = hash_min; idx < hash_max; idx+=
BITS) {
430 const uint32_t check =
MIN(
BITS, (hash_max - idx));
431 for (uint32_t i = 0; i < check; i++) {
440 for (uint32_t i = 0; i < check; i++) {
445 if (fb->evicted != NULL || fb->head != NULL) {
446 if (fb->evicted != NULL) {
452 if (fb->head != NULL) {
453 uint32_t next_ts = 0;
454 FlowManagerHashRowTimeout(td, fb->head,
ts, emergency, counters, &next_ts);
459 if (fb->evicted == NULL && fb->head == NULL) {
470 FlowManagerHashRowClearEvictedList(td,
evicted);
477 cnt += ProcessAsideQueue(td, counters);
481 counters->rows_checked += rows_checked;
482 counters->rows_skipped += rows_skipped;
483 counters->rows_empty += rows_empty;
486 cnt += ProcessAsideQueue(td, counters);
488 counters->flows_removed +=
cnt;
509 const uint32_t rows, uint32_t *pos)
514 uint32_t rows_left = rows;
517 start = hash_min + (*pos);
518 if (start >= hash_max) {
521 end = start + rows_left;
522 if (end > hash_max) {
525 *pos = (end == hash_max) ? hash_min : end;
526 rows_left = rows_left - (end - start);
528 cnt += FlowTimeoutHash(td,
ts, start, end, counters);
557 RemoveFromHash(f, NULL);
559 FlowBucket *fb = f->
fb;
579 #define RECYCLE_MAX_QUEUE_ITEMS 25
585 static uint32_t FlowCleanupHash(
void)
595 if (fb->head != NULL) {
597 cnt += FlowManagerHashRowCleanup(fb->head, &local_queue, 0);
599 if (fb->evicted != NULL) {
601 cnt += FlowManagerHashRowCleanup(fb->evicted, &local_queue, 1);
677 static void FlowCountersUpdate(
695 static TmEcode FlowManagerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
712 if ((ftd->
instance + 1) == flowmgr_number) {
722 FlowCountersInit(t, &ftd->
cnt);
759 static void GetWorkUnitSizing(
const uint32_t rows,
const uint32_t mp,
const bool emergency,
760 uint64_t *wu_sleep, uint32_t *wu_rows, uint32_t *rows_sec)
768 const uint32_t emp =
MAX(mp, 10);
769 const uint32_t rows_per_sec = (uint32_t)((
float)rows * (float)((
float)emp / (float)100));
772 const uint32_t work_per_unit =
MIN(rows_per_sec / 1000, 1000);
775 const uint32_t sleep_per_unit =
MAX(250, 1000 - work_per_unit);
776 SCLogDebug(
"mp %u emp %u rows %u rows_sec %u sleep %ums", mp, emp, rows, rows_per_sec,
779 *wu_sleep = sleep_per_unit;
780 *wu_rows = rows_per_sec;
781 *rows_sec = rows_per_sec;
793 const uint32_t rows = ftd->
max - ftd->
min;
796 uint32_t emerg_over_cnt = 0;
797 uint64_t next_run_ms = 0;
799 uint32_t rows_sec = 0;
800 uint32_t rows_per_wu = 0;
801 uint64_t sleep_per_wu = 0;
802 bool prev_emerg =
false;
803 uint32_t other_last_sec = 0;
818 GetWorkUnitSizing(rows, mp,
false, &sleep_per_wu, &rows_per_wu, &rows_sec);
831 const bool emerge_p = (emerg && !prev_emerg);
838 if (ts_ms >= next_run_ms) {
843 if (spare_perc < 90 || spare_perc > 110) {
850 FlowTimeoutCounters counters = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
858 SCLogDebug(
"hash %u:%u slice starting at %u with %u rows", ftd->
min, ftd->
max, pos,
861 const uint32_t ppos = pos;
862 FlowTimeoutHashInChunks(
872 FlowCountersUpdate(th_v, ftd, &counters);
875 SCLogDebug(
"flow_sparse_q.len = %" PRIu32
" prealloc: %" PRIu32
876 "flow_spare_q status: %" PRIu32
"%% flows at the queue",
889 if (emerg_over_cnt >= 30) {
896 SCLogNotice(
"Flow emergency mode over, back to normal... unsetting"
897 " FLOW_EMERGENCY bit (ts.tv_sec: %" PRIuMAX
", "
898 "ts.tv_usec:%" PRIuMAX
") flow_spare_q status(): %" PRIu32
899 "%% flows at the queue",
908 const uint32_t pmp = mp;
914 GetWorkUnitSizing(rows, mp, emerg, &sleep_per_wu, &rows_per_wu, &rows_sec);
919 next_run_ms = ts_ms + sleep_per_wu;
921 if (other_last_sec == 0 || other_last_sec < (uint32_t)
SCTIME_SECS(
ts)) {
941 if (emerg || !time_is_live) {
944 struct timeval cond_tv;
945 gettimeofday(&cond_tv, NULL);
946 struct timeval add_tv;
947 add_tv.tv_sec = sleep_per_wu / 1000;
948 add_tv.tv_usec = (sleep_per_wu % 1000) * 1000;
949 timeradd(&cond_tv, &add_tv, &cond_tv);
955 &flow_manager_ctrl_cond, &flow_manager_ctrl_mutex, &cond_time);
956 if (rc == ETIMEDOUT || rc < 0)
975 intmax_t setting = 1;
978 if (setting < 1 || setting > 1024) {
979 FatalError(
"invalid flow.managers setting %" PRIdMAX, setting);
981 flowmgr_number = (uint32_t)setting;
983 SCLogConfig(
"using %u flow manager threads", flowmgr_number);
986 for (uint32_t u = 0; u < flowmgr_number; u++) {
992 BUG_ON(tv_flowmgr == NULL);
994 if (tv_flowmgr == NULL) {
995 FatalError(
"flow manager thread creation failed");
998 FatalError(
"flow manager thread spawn failed");
1015 static TmEcode FlowRecyclerThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
1021 SCLogError(
"initializing flow log API for thread failed");
1058 FlowEndCountersUpdate(
tv, &ftd->
fec, f);
1077 uint64_t recycled_cnt = 0;
1098 Recycler(th_v, ftd, f);
1107 if (ret_queue.
len > 0) {
1111 recycled_cnt +=
cnt;
1121 if (emerg || !time_is_live) {
1124 struct timeval cond_tv;
1125 gettimeofday(&cond_tv, NULL);
1126 cond_tv.tv_sec += 1;
1131 &flow_recycler_ctrl_cond, &flow_recycler_ctrl_mutex, &cond_time);
1132 if (rc == ETIMEDOUT || rc < 0) {
1150 SCLogPerf(
"%"PRIu64
" flows processed", recycled_cnt);
1154 static bool FlowRecyclerReadyToShutdown(
void)
1164 return ((
len == 0));
1170 intmax_t setting = 1;
1171 (void)
ConfGetInt(
"flow.recyclers", &setting);
1173 if (setting < 1 || setting > 1024) {
1174 FatalError(
"invalid flow.recyclers setting %" PRIdMAX, setting);
1176 flowrec_number = (uint32_t)setting;
1178 SCLogConfig(
"using %u flow recycler threads", flowrec_number);
1180 for (uint32_t u = 0; u < flowrec_number; u++) {
1187 if (tv_flowrec == NULL) {
1188 FatalError(
"flow recycler thread creation failed");
1191 FatalError(
"flow recycler thread spawn failed");
1208 (void)FlowCleanupHash();
1210 uint32_t flows = FlowCleanupHash();
1218 }
while (FlowRecyclerReadyToShutdown() ==
false);
1231 struct timeval start_ts;
1232 struct timeval cur_ts;
1233 gettimeofday(&start_ts, NULL);
1236 gettimeofday(&cur_ts, NULL);
1237 if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
1238 FatalError(
"unable to get all flow recycler "
1239 "threads to shutdown in time");