suricata
flow-bypass.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-2018 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 Eric Leblond <eleblond@stamus-networks.com>
22  */
23 
24 #include "suricata-common.h"
25 #include "tm-threads.h"
26 #include "flow.h"
27 #include "flow-bypass.h"
28 #include "flow-private.h"
29 #include "util-ebpf.h"
30 #include "runmodes.h"
31 
32 #ifdef CAPTURE_OFFLOAD_MANAGER
33 
34 #define FLOW_BYPASS_DELAY 10
35 
36 #ifndef TIMEVAL_TO_TIMESPEC
37 #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
38  (ts)->tv_sec = (tv)->tv_sec; \
39  (ts)->tv_nsec = (tv)->tv_usec * 1000; \
40 }
41 #endif
42 
43 typedef struct BypassedFlowManagerThreadData_ {
44  StatsCounterId flow_bypassed_cnt_clo;
45  StatsCounterId flow_bypassed_pkts;
46  StatsCounterId flow_bypassed_bytes;
47 } BypassedFlowManagerThreadData;
48 
49 #define BYPASSFUNCMAX 4
50 
51 typedef struct BypassedCheckFuncItem_ {
52  BypassedCheckFunc Func;
53  BypassedCheckFuncInit FuncInit;
54  void *data;
55 } BypassedCheckFuncItem;
56 
57 int g_bypassed_func_max_index = 0;
58 BypassedCheckFuncItem bypassedfunclist[BYPASSFUNCMAX];
59 
60 typedef struct BypassedUpdateFuncItem_ {
61  BypassedUpdateFunc Func;
62  void *data;
63 } BypassedUpdateFuncItem;
64 
65 int g_bypassed_update_max_index = 0;
66 BypassedUpdateFuncItem updatefunclist[BYPASSFUNCMAX];
67 
68 static TmEcode BypassedFlowManager(ThreadVars *th_v, void *thread_data)
69 {
70  int tcount = 0;
71  int i;
72  BypassedFlowManagerThreadData *ftd = thread_data;
73  struct timespec curtime = {0, 0};
74 
75  struct timeval tv;
76  gettimeofday(&tv, NULL);
77  TIMEVAL_TO_TIMESPEC(&tv, &curtime);
78 
79  for (i = 0; i < g_bypassed_func_max_index; i++) {
80  if (bypassedfunclist[i].FuncInit) {
81  bypassedfunclist[i].FuncInit(th_v, &curtime, bypassedfunclist[i].data);
82  }
83  }
84 
85  /* check if we have a periodic check function */
86  bool found = false;
87  for (i = 0; i < g_bypassed_func_max_index; i++) {
88  if (bypassedfunclist[i].FuncInit) {
89  found = true;
90  break;
91  }
92  }
93  if (!found)
94  return TM_ECODE_OK;
95 
97  bool run = TmThreadsWaitForUnpause(th_v);
98 
99  while (run) {
100  SCLogDebug("Dumping the table");
101  gettimeofday(&tv, NULL);
102  TIMEVAL_TO_TIMESPEC(&tv, &curtime);
103 
104  for (i = 0; i < g_bypassed_func_max_index; i++) {
105  struct flows_stats bypassstats = { 0, 0, 0};
106  if (bypassedfunclist[i].Func == NULL)
107  continue;
108  tcount = bypassedfunclist[i].Func(th_v, &bypassstats, &curtime, bypassedfunclist[i].data);
109  if (tcount) {
111  &th_v->stats, ftd->flow_bypassed_cnt_clo, (uint64_t)bypassstats.count);
112  }
114  &th_v->stats, ftd->flow_bypassed_pkts, (uint64_t)bypassstats.packets);
115  StatsCounterAddI64(&th_v->stats, ftd->flow_bypassed_bytes, (uint64_t)bypassstats.bytes);
116  }
117 
118  if (TmThreadsCheckFlag(th_v, THV_KILL)) {
119  StatsSyncCounters(&th_v->stats);
120  return TM_ECODE_OK;
121  }
122  for (i = 0; i < FLOW_BYPASS_DELAY * 100; i++) {
123  if (TmThreadsCheckFlag(th_v, THV_KILL)) {
124  StatsSyncCounters(&th_v->stats);
125  return TM_ECODE_OK;
126  }
128  SleepMsec(10);
129  }
130  }
131  return TM_ECODE_OK;
132 }
133 
134 static TmEcode BypassedFlowManagerThreadInit(ThreadVars *t, const void *initdata, void **data)
135 {
136  BypassedFlowManagerThreadData *ftd = SCCalloc(1, sizeof(BypassedFlowManagerThreadData));
137  if (ftd == NULL)
138  return TM_ECODE_FAILED;
139 
140  *data = ftd;
141 
142  ftd->flow_bypassed_cnt_clo = StatsRegisterCounter("flow_bypassed.closed", &t->stats);
143  ftd->flow_bypassed_pkts = StatsRegisterCounter("flow_bypassed.pkts", &t->stats);
144  ftd->flow_bypassed_bytes = StatsRegisterCounter("flow_bypassed.bytes", &t->stats);
145 
146  return TM_ECODE_OK;
147 }
148 
149 static TmEcode BypassedFlowManagerThreadDeinit(ThreadVars *t, void *data)
150 {
151  if (data)
152  SCFree(data);
153  return TM_ECODE_OK;
154 }
155 
157  BypassedCheckFuncInit CheckFuncInit,
158  void *data)
159 {
160  if (g_bypassed_func_max_index < BYPASSFUNCMAX) {
161  bypassedfunclist[g_bypassed_func_max_index].Func = CheckFunc;
162  bypassedfunclist[g_bypassed_func_max_index].FuncInit = CheckFuncInit;
163  bypassedfunclist[g_bypassed_func_max_index].data = data;
164  g_bypassed_func_max_index++;
165  } else {
166  return -1;
167  }
168  return 0;
169 }
170 
172  void *data)
173 {
174  if (!UpdateFunc) {
175  return -1;
176  }
177  if (g_bypassed_update_max_index < BYPASSFUNCMAX) {
178  updatefunclist[g_bypassed_update_max_index].Func = UpdateFunc;
179  updatefunclist[g_bypassed_update_max_index].data = data;
180  g_bypassed_update_max_index++;
181  } else {
182  return -1;
183  }
184  return 0;
185 }
186 #endif
187 
188 /** \brief spawn the flow bypass manager thread */
190 {
191 #ifdef CAPTURE_OFFLOAD_MANAGER
192 
193  ThreadVars *tv_flowmgr = NULL;
195  "BypassedFlowManager", 0);
196  BUG_ON(tv_flowmgr == NULL);
197 
198  if (tv_flowmgr == NULL) {
199  printf("ERROR: TmThreadsCreate failed\n");
200  exit(1);
201  }
202  if (TmThreadSpawn(tv_flowmgr) != TM_ECODE_OK) {
203  printf("ERROR: TmThreadSpawn failed\n");
204  exit(1);
205  }
206 #endif
207 }
208 
210 {
211 #ifdef CAPTURE_OFFLOAD_MANAGER
212  for (int i = 0; i < g_bypassed_update_max_index; i++) {
213  if (updatefunclist[i].Func(f, p, updatefunclist[i].data)) {
214  return;
215  }
216  }
217 #endif
218 }
219 
221 {
222 #ifdef CAPTURE_OFFLOAD_MANAGER
223  tmm_modules[TMM_BYPASSEDFLOWMANAGER].name = "BypassedFlowManager";
224  tmm_modules[TMM_BYPASSEDFLOWMANAGER].ThreadInit = BypassedFlowManagerThreadInit;
225  tmm_modules[TMM_BYPASSEDFLOWMANAGER].ThreadDeinit = BypassedFlowManagerThreadDeinit;
226  tmm_modules[TMM_BYPASSEDFLOWMANAGER].Management = BypassedFlowManager;
230 #endif
231 }
232 
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:77
tm-threads.h
flow-bypass.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1702
TmThreadCreateMgmtThreadByName
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...
Definition: tm-threads.c:1125
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *stats)
Definition: counters.c:483
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
StatsRegisterCounter
StatsCounterId StatsRegisterCounter(const char *name, StatsThreadContext *stats)
Registers a normal, unqualified counter.
Definition: counters.c:1001
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:101
BypassedFlowManagerThreadSpawn
void BypassedFlowManagerThreadSpawn(void)
spawn the flow bypass manager thread
Definition: flow-bypass.c:189
flows_stats::count
uint64_t count
Definition: flow-bypass.h:30
name
const char * name
Definition: detect-engine-proto.c:48
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:348
THV_RUNNING
#define THV_RUNNING
Definition: threadvars.h:55
BypassedFlowUpdate
void BypassedFlowUpdate(Flow *f, Packet *p)
Definition: flow-bypass.c:209
StatsCounterId
Definition: counters.h:30
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:53
TmModuleBypassedFlowManagerRegister
void TmModuleBypassedFlowManagerRegister(void)
Definition: flow-bypass.c:220
TMM_BYPASSEDFLOWMANAGER
@ TMM_BYPASSEDFLOWMANAGER
Definition: tm-threads-common.h:71
thread_name_flow_bypass
const char * thread_name_flow_bypass
Definition: runmodes.c:72
util-ebpf.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
BypassedCheckFunc
int(* BypassedCheckFunc)(ThreadVars *th_v, struct flows_stats *bypassstats, struct timespec *curtime, void *data)
Definition: flow-bypass.h:35
TmModule_::Management
TmEcode(* Management)(ThreadVars *, void *)
Definition: tm-modules.h:69
THV_KILL
#define THV_KILL
Definition: threadvars.h:40
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
Packet_
Definition: decode.h:501
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
TmEcode
TmEcode
Definition: tm-threads-common.h:80
TmModule_::name
const char * name
Definition: tm-modules.h:48
runmodes.h
flows_stats::bytes
uint64_t bytes
Definition: flow-bypass.h:32
SleepMsec
#define SleepMsec(msec)
Definition: tm-threads.h:45
suricata-common.h
TmThreadsWaitForUnpause
bool TmThreadsWaitForUnpause(ThreadVars *tv)
Wait for a thread to become unpaused.
Definition: tm-threads.c:363
StatsSyncCounters
void StatsSyncCounters(StatsThreadContext *stats)
Definition: counters.c:478
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:51
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
BypassedCheckFuncInit
int(* BypassedCheckFuncInit)(ThreadVars *th_v, struct timespec *curtime, void *data)
Definition: flow-bypass.h:38
BypassedFlowManagerRegisterUpdateFunc
int BypassedFlowManagerRegisterUpdateFunc(BypassedUpdateFunc UpdateFunc, void *data)
SCFree
#define SCFree(p)
Definition: util-mem.h:61
flow.h
BypassedUpdateFunc
int(* BypassedUpdateFunc)(Flow *f, Packet *p, void *data)
Definition: flow-bypass.h:40
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:93
BypassedFlowManagerRegisterCheckFunc
int BypassedFlowManagerRegisterCheckFunc(BypassedCheckFunc CheckFunc, BypassedCheckFuncInit CheckFuncInit, void *data)
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
flows_stats::packets
uint64_t packets
Definition: flow-bypass.h:31
StatsCounterAddI64
void StatsCounterAddI64(StatsThreadContext *stats, StatsCounterId id, int64_t x)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:146
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:80
TM_FLAG_MANAGEMENT_TM
#define TM_FLAG_MANAGEMENT_TM
Definition: tm-modules.h:36
flows_stats
Definition: flow-bypass.h:29