suricata
tm-threads.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2011 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 Victor Julien <victor@inliniac.net>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  */
24 
25 #ifndef __TM_THREADS_H__
26 #define __TM_THREADS_H__
27 
28 #include "tmqh-packetpool.h"
29 #include "tm-threads-common.h"
30 #include "tm-modules.h"
31 
32 #ifdef OS_WIN32
33 static inline void SleepUsec(uint64_t usec)
34 {
35  uint64_t msec = 1;
36  if (usec > 1000) {
37  msec = usec / 1000;
38  }
39  Sleep(msec);
40 }
41 #define SleepMsec(msec) Sleep((msec))
42 #else
43 #define SleepUsec(usec) usleep((usec))
44 #define SleepMsec(msec) usleep((msec) * 1000)
45 #endif
46 
47 #define TM_QUEUE_NAME_MAX 16
48 #define TM_THREAD_NAME_MAX 16
49 
50 typedef TmEcode (*TmSlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *,
51  PacketQueue *);
52 
53 typedef struct TmSlot_ {
54  /* the TV holding this slot */
55  ThreadVars *tv;
56 
57  /* function pointers */
58  SC_ATOMIC_DECLARE(TmSlotFunc, SlotFunc);
59 
60  TmEcode (*PktAcqLoop)(ThreadVars *, void *, void *);
61 
62  TmEcode (*SlotThreadInit)(ThreadVars *, const void *, void **);
63  void (*SlotThreadExitPrintStats)(ThreadVars *, void *);
64  TmEcode (*SlotThreadDeinit)(ThreadVars *, void *);
65 
66  /* data storage */
67  const void *slot_initdata;
68  SC_ATOMIC_DECLARE(void *, slot_data);
69 
70  /* queue filled by the SlotFunc with packets that will
71  * be processed futher _before_ the current packet.
72  * The locks in the queue are NOT used */
73  PacketQueue slot_pre_pq;
74 
75  /* queue filled by the SlotFunc with packets that will
76  * be processed futher _after_ the current packet. The
77  * locks in the queue are NOT used */
78  PacketQueue slot_post_pq;
79 
80  /* store the thread module id */
81  int tm_id;
82 
83  /* slot id, only used my TmVarSlot to know what the first slot is */
84  int id;
85 
86  /* linked list, only used when you have multiple slots(used by TmVarSlot) */
87  struct TmSlot_ *slot_next;
88 
89  /* just called once, so not perf critical */
90  TmEcode (*Management)(ThreadVars *, void *);
91 
92 } TmSlot;
93 
94 extern ThreadVars *tv_root[TVT_MAX];
95 
96 extern SCMutex tv_root_lock;
97 
98 void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *);
100 
101 ThreadVars *TmThreadCreate(const char *, const char *, const char *, const char *, const char *, const char *,
102  void *(fn_p)(void *), int);
103 ThreadVars *TmThreadCreatePacketHandler(const char *, const char *, const char *, const char *, const char *,
104  const char *);
105 ThreadVars *TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int);
106 ThreadVars *TmThreadCreateMgmtThreadByName(const char *name, const char *module,
107  int mucond);
108 ThreadVars *TmThreadCreateCmdThreadByName(const char *name, const char *module,
109  int mucond);
110 TmEcode TmThreadSpawn(ThreadVars *);
111 void TmThreadSetFlags(ThreadVars *, uint8_t);
112 void TmThreadKillThreadsFamily(int family);
113 void TmThreadKillThreads(void);
114 void TmThreadClearThreadsFamily(int family);
115 void TmThreadAppend(ThreadVars *, int);
116 void TmThreadRemove(ThreadVars *, int);
117 void TmThreadSetGroupName(ThreadVars *tv, const char *name);
118 
119 TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t);
120 TmEcode TmThreadSetThreadPriority(ThreadVars *, int);
121 TmEcode TmThreadSetCPU(ThreadVars *, uint8_t);
122 TmEcode TmThreadSetupOptions(ThreadVars *);
123 void TmThreadSetPrio(ThreadVars *);
124 int TmThreadGetNbThreads(uint8_t type);
125 
126 void TmThreadInitMC(ThreadVars *);
127 void TmThreadTestThreadUnPaused(ThreadVars *);
128 void TmThreadContinue(ThreadVars *);
129 void TmThreadContinueThreads(void);
130 void TmThreadPause(ThreadVars *);
131 void TmThreadPauseThreads(void);
132 void TmThreadCheckThreadState(void);
134 ThreadVars *TmThreadsGetCallingThread(void);
135 
136 int TmThreadsCheckFlag(ThreadVars *, uint16_t);
137 void TmThreadsSetFlag(ThreadVars *, uint16_t);
138 void TmThreadsUnsetFlag(ThreadVars *, uint16_t);
139 void TmThreadWaitForFlag(ThreadVars *, uint16_t);
140 
141 TmEcode TmThreadsSlotVarRun (ThreadVars *tv, Packet *p, TmSlot *slot);
142 
143 ThreadVars *TmThreadsGetTVContainingSlot(TmSlot *);
147 
148 uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags);
149 
150 /**
151  * \brief Process the rest of the functions (if any) and queue.
152  */
153 static inline TmEcode TmThreadsSlotProcessPkt(ThreadVars *tv, TmSlot *s, Packet *p)
154 {
155  TmEcode r = TM_ECODE_OK;
156 
157  if (s == NULL) {
158  tv->tmqh_out(tv, p);
159  return r;
160  }
161 
162  if (TmThreadsSlotVarRun(tv, p, s) == TM_ECODE_FAILED) {
163  TmqhOutputPacketpool(tv, p);
164  TmSlot *slot = s;
165  while (slot != NULL) {
169 
170  slot = slot->slot_next;
171  }
173  r = TM_ECODE_FAILED;
174 
175  } else {
176  tv->tmqh_out(tv, p);
177 
178  /* post process pq */
179  TmSlot *slot = s;
180  while (slot != NULL) {
181  if (slot->slot_post_pq.top != NULL) {
182  while (1) {
184  Packet *extra_p = PacketDequeue(&slot->slot_post_pq);
186 
187  if (extra_p == NULL)
188  break;
189 
190  if (slot->slot_next != NULL) {
191  r = TmThreadsSlotVarRun(tv, extra_p, slot->slot_next);
192  if (r == TM_ECODE_FAILED) {
196 
197  TmqhOutputPacketpool(tv, extra_p);
199  break;
200  }
201  }
202  tv->tmqh_out(tv, extra_p);
203  }
204  } /* if (slot->slot_post_pq.top != NULL) */
205  slot = slot->slot_next;
206  } /* while (slot != NULL) */
207  }
208 
209  return r;
210 }
211 
212 /**
213  * \brief Handle timeout from the capture layer. Checks
214  * post-pq which may have been filled by the flow
215  * manager.
216  */
217 static inline TmEcode TmThreadsSlotHandlePostPQs(ThreadVars *tv, TmSlot *s)
218 {
219  /* post process pq */
220  for (TmSlot *slot = s; slot != NULL; slot = slot->slot_next) {
221  if (slot->slot_post_pq.top != NULL) {
222  while (1) {
224  Packet *extra_p = PacketDequeue(&slot->slot_post_pq);
226 
227  if (extra_p == NULL)
228  break;
229 
230  if (slot->slot_next != NULL) {
231  TmEcode r = TmThreadsSlotVarRun(tv, extra_p, slot->slot_next);
232  if (r == TM_ECODE_FAILED) {
236 
237  TmqhOutputPacketpool(tv, extra_p);
239  return TM_ECODE_FAILED;
240  }
241  }
242  tv->tmqh_out(tv, extra_p);
243  }
244  }
245  }
246  return TM_ECODE_OK;
247 }
248 
249 /** \brief inject packet if THV_CAPTURE_INJECT_PKT is set
250  * Allow caller to supply their own packet
251  *
252  * Meant for detect reload process that interupts an sleeping capture thread
253  * to force a packet through the engine to complete a reload */
254 static inline void TmThreadsCaptureInjectPacket(ThreadVars *tv, TmSlot *slot, Packet *p)
255 {
257  if (p == NULL)
259  if (p != NULL) {
261  if (TmThreadsSlotProcessPkt(tv, slot, p) != TM_ECODE_OK) {
262  TmqhOutputPacketpool(tv, p);
263  }
264  }
265 }
266 
267 static inline void TmThreadsCaptureHandleTimeout(ThreadVars *tv, TmSlot *slot, Packet *p)
268 {
270  TmThreadsCaptureInjectPacket(tv, slot, p);
271  } else {
272  TmThreadsSlotHandlePostPQs(tv, slot);
273 
274  /* packet could have been passed to us that we won't use
275  * return it to the pool. */
276  if (p != NULL)
277  tv->tmqh_out(tv, p);
278  }
279 }
280 
281 void TmThreadsListThreads(void);
282 int TmThreadsRegisterThread(ThreadVars *tv, const int type);
283 void TmThreadsUnregisterThread(const int id);
284 int TmThreadsInjectPacketsById(Packet **, int id);
285 
286 void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts);
287 void TmreadsGetMinimalTimestamp(struct timeval *ts);
288 
289 #endif /* __TM_THREADS_H__ */
ThreadVars * TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int)
Creates and returns the TV instance for a Management thread(MGMT). This function supports only custom...
Definition: tm-threads.c:1255
TmEcode TmThreadSetThreadPriority(ThreadVars *, int)
Set the thread options (thread priority).
Definition: tm-threads.c:957
#define SCMutex
uint16_t flags
void TmThreadCheckThreadState(void)
Used to check the thread for certain conditions of failure.
Definition: tm-threads.c:2051
ThreadVars * TmThreadCreateCmdThreadByName(const char *name, const char *module, int mucond)
Creates and returns the TV instance for a Command thread (CMD). This function supports only custom sl...
Definition: tm-threads.c:1316
void TmThreadKillThreadsFamily(int family)
Definition: tm-threads.c:1740
void TmThreadClearThreadsFamily(int family)
Definition: tm-threads.c:1830
int TmThreadsInjectPacketsById(Packet **, int id)
Definition: tm-threads.c:2370
void TmThreadSetFlags(ThreadVars *, uint8_t)
TmEcode TmThreadSpawn(ThreadVars *)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1855
TmEcode(* SlotThreadDeinit)(ThreadVars *, void *)
Definition: tm-threads.h:64
ThreadVars * TmThreadCreatePacketHandler(const char *, const char *, const char *, const char *, const char *, const char *)
Creates and returns a TV instance for a Packet Processing Thread. This function doesn&#39;t support custo...
Definition: tm-threads.c:1225
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:1283
void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *)
Appends a new entry to the slots.
Definition: tm-threads.c:816
ThreadVars * TmThreadsGetTVContainingSlot(TmSlot *)
Definition: tm-threads.c:786
void TmThreadAppend(ThreadVars *, int)
Appends this TV to tv_root based on its type.
Definition: tm-threads.c:1342
void TmThreadDisablePacketThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1636
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-threads.h:60
void TmThreadsSetFlag(ThreadVars *, uint16_t)
Set a thread flag.
Definition: tm-threads.c:98
#define THV_CAPTURE_INJECT_PKT
Definition: threadvars.h:53
#define THV_FAILED
Definition: threadvars.h:40
Packet * PacketGetFromQueueOrAlloc(void)
Get a packet. We try to get a packet from the packetpool first, but if that is empty we alloc a packe...
Definition: decode.c:177
void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts)
Definition: tm-threads.c:2318
#define SCMutexLock(mut)
TmEcode TmThreadSetupOptions(ThreadVars *)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:1045
ThreadVars * TmThreadCreate(const char *, const char *, const char *, const char *, const char *, const char *, void *(fn_p)(void *), int)
void TmThreadPause(ThreadVars *)
Pauses a thread.
Definition: tm-threads.c:2018
#define SCMutexUnlock(mut)
void TmThreadsUnsetFlag(ThreadVars *, uint16_t)
Unset a thread flag.
Definition: tm-threads.c:106
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
void TmThreadTestThreadUnPaused(ThreadVars *)
Tests if the thread represented in the arg has been unpaused or not.
Definition: tm-threads.c:1953
void TmThreadContinueThreads(void)
Unpauses all threads present in tv_root.
Definition: tm-threads.c:1995
TmEcode TmThreadWaitOnThreadInit(void)
Used to check if all threads have finished their initialization. On finding an un-initialized thread...
Definition: tm-threads.c:2079
uint8_t type
ThreadVars * TmThreadsGetCallingThread(void)
Returns the TV for the calling thread.
Definition: tm-threads.c:2156
const void * slot_initdata
Definition: tm-threads.h:67
void(* tmqh_out)(struct ThreadVars_ *, struct Packet_ *)
Definition: threadvars.h:80
PacketQueue slot_post_pq
Definition: tm-threads.h:78
void TmThreadPauseThreads(void)
Pauses all threads present in tv_root.
Definition: tm-threads.c:2028
TmEcode(* Management)(ThreadVars *, void *)
Definition: tm-threads.h:90
void TmThreadInitMC(ThreadVars *)
Sets the thread flags for a thread instance(tv)
Definition: tm-threads.c:1904
uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags)
returns a count of all the threads that match the flag
Definition: tm-threads.c:2183
Packet * top
Definition: decode.h:621
int tm_id
Definition: tm-threads.h:81
struct TmSlot_ * slot_next
Definition: tm-threads.h:87
void TmThreadsUnregisterThread(const int id)
Definition: tm-threads.c:2285
void TmThreadContinue(ThreadVars *)
Unpauses a thread.
Definition: tm-threads.c:1985
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1091
SCMutex tv_root_lock
Definition: tm-threads.c:82
PacketQueue slot_pre_pq
Definition: tm-threads.h:73
TmEcode TmThreadSetCPU(ThreadVars *, uint8_t)
Definition: tm-threads.c:1013
void TmThreadsListThreads(void)
Definition: tm-threads.c:2221
SC_ATOMIC_DECLARE(TmSlotFunc, SlotFunc)
TmEcode(* SlotThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-threads.h:62
void(* SlotThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-threads.h:63
void TmThreadRemove(ThreadVars *, int)
Removes this TV from tv_root based on its type.
Definition: tm-threads.c:1380
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1812
void TmThreadWaitForFlag(ThreadVars *, uint16_t)
Waits till the specified flag(s) is(are) set. We don&#39;t bother if the kill flag has been set or not on...
Definition: tm-threads.c:1971
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:79
ThreadVars * tv
Definition: tm-threads.h:55
void TmreadsGetMinimalTimestamp(struct timeval *ts)
Definition: tm-threads.c:2334
TmEcode TmThreadsSlotVarRun(ThreadVars *tv, Packet *p, TmSlot *slot)
Separate run function so we can call it recursively.
Definition: tm-threads.c:116
int TmThreadsRegisterThread(ThreadVars *tv, const int type)
Definition: tm-threads.c:2242
#define SleepUsec(usec)
Definition: tm-threads.h:43
uint64_t ts
void TmThreadKillThreads(void)
Definition: tm-threads.c:1769
TmEcode(* TmSlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *)
Definition: tm-threads.h:50
struct TmSlot_ TmSlot
TmSlot * TmSlotGetSlotForTM(int)
Returns the slot holding a TM with the particular tm_id.
Definition: tm-threads.c:865
Packet * PacketDequeue(PacketQueue *q)
Definition: packet-queue.c:167
void TmThreadSetPrio(ThreadVars *)
Adjusting nice value for threads.
Definition: tm-threads.c:968
TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t)
Set the thread options (cpu affinity).
Definition: tm-threads.c:1004
Per thread variable structure.
Definition: threadvars.h:57
uint32_t flags
Definition: decode.h:443
int id
Definition: tm-threads.h:84
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:1029
int TmThreadsCheckFlag(ThreadVars *, uint16_t)
Check if a thread flag is set.
Definition: tm-threads.c:90
void TmThreadDisableReceiveThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1540
SCMutex mutex_q
Definition: decode.h:627
TmSlot * TmThreadGetFirstTmSlotForPartialPattern(const char *)
Definition: tm-threads.c:1707
void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
Release all the packets in the queue back to the packetpool. Mainly used by threads that have failed...