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 void TmThreadDumpThreads(void);
119 
120 TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t);
121 TmEcode TmThreadSetThreadPriority(ThreadVars *, int);
122 TmEcode TmThreadSetCPU(ThreadVars *, uint8_t);
123 TmEcode TmThreadSetupOptions(ThreadVars *);
124 void TmThreadSetPrio(ThreadVars *);
125 int TmThreadGetNbThreads(uint8_t type);
126 
127 void TmThreadInitMC(ThreadVars *);
128 void TmThreadTestThreadUnPaused(ThreadVars *);
129 void TmThreadContinue(ThreadVars *);
130 void TmThreadContinueThreads(void);
131 void TmThreadPause(ThreadVars *);
132 void TmThreadPauseThreads(void);
133 void TmThreadCheckThreadState(void);
135 ThreadVars *TmThreadsGetCallingThread(void);
136 
137 int TmThreadsCheckFlag(ThreadVars *, uint16_t);
138 void TmThreadsSetFlag(ThreadVars *, uint16_t);
139 void TmThreadsUnsetFlag(ThreadVars *, uint16_t);
140 void TmThreadWaitForFlag(ThreadVars *, uint16_t);
141 
142 TmEcode TmThreadsSlotVarRun (ThreadVars *tv, Packet *p, TmSlot *slot);
143 
144 ThreadVars *TmThreadsGetTVContainingSlot(TmSlot *);
148 
149 uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags);
150 
151 /**
152  * \brief Process the rest of the functions (if any) and queue.
153  */
154 static inline TmEcode TmThreadsSlotProcessPkt(ThreadVars *tv, TmSlot *s, Packet *p)
155 {
156  TmEcode r = TM_ECODE_OK;
157 
158  if (s == NULL) {
159  tv->tmqh_out(tv, p);
160  return r;
161  }
162 
163  if (TmThreadsSlotVarRun(tv, p, s) == TM_ECODE_FAILED) {
164  TmqhOutputPacketpool(tv, p);
165  TmSlot *slot = s;
166  while (slot != NULL) {
170 
171  slot = slot->slot_next;
172  }
174  r = TM_ECODE_FAILED;
175 
176  } else {
177  tv->tmqh_out(tv, p);
178 
179  /* post process pq */
180  TmSlot *slot = s;
181  while (slot != NULL) {
182  if (slot->slot_post_pq.top != NULL) {
183  while (1) {
185  Packet *extra_p = PacketDequeue(&slot->slot_post_pq);
187 
188  if (extra_p == NULL)
189  break;
190 
191  if (slot->slot_next != NULL) {
192  r = TmThreadsSlotVarRun(tv, extra_p, slot->slot_next);
193  if (r == TM_ECODE_FAILED) {
197 
198  TmqhOutputPacketpool(tv, extra_p);
200  break;
201  }
202  }
203  tv->tmqh_out(tv, extra_p);
204  }
205  } /* if (slot->slot_post_pq.top != NULL) */
206  slot = slot->slot_next;
207  } /* while (slot != NULL) */
208  }
209 
210  return r;
211 }
212 
213 /**
214  * \brief Handle timeout from the capture layer. Checks
215  * post-pq which may have been filled by the flow
216  * manager.
217  */
218 static inline TmEcode TmThreadsSlotHandlePostPQs(ThreadVars *tv, TmSlot *s)
219 {
220  /* post process pq */
221  for (TmSlot *slot = s; slot != NULL; slot = slot->slot_next) {
222  if (slot->slot_post_pq.top != NULL) {
223  while (1) {
225  Packet *extra_p = PacketDequeue(&slot->slot_post_pq);
227 
228  if (extra_p == NULL)
229  break;
230 
231  if (slot->slot_next != NULL) {
232  TmEcode r = TmThreadsSlotVarRun(tv, extra_p, slot->slot_next);
233  if (r == TM_ECODE_FAILED) {
237 
238  TmqhOutputPacketpool(tv, extra_p);
240  return TM_ECODE_FAILED;
241  }
242  }
243  tv->tmqh_out(tv, extra_p);
244  }
245  }
246  }
247  return TM_ECODE_OK;
248 }
249 
250 /** \brief inject packet if THV_CAPTURE_INJECT_PKT is set
251  * Allow caller to supply their own packet
252  *
253  * Meant for detect reload process that interupts an sleeping capture thread
254  * to force a packet through the engine to complete a reload */
255 static inline void TmThreadsCaptureInjectPacket(ThreadVars *tv, TmSlot *slot, Packet *p)
256 {
258  if (p == NULL)
260  if (p != NULL) {
263  if (TmThreadsSlotProcessPkt(tv, slot, p) != TM_ECODE_OK) {
264  TmqhOutputPacketpool(tv, p);
265  }
266  }
267 }
268 
269 static inline void TmThreadsCaptureHandleTimeout(ThreadVars *tv, TmSlot *slot, Packet *p)
270 {
272  TmThreadsCaptureInjectPacket(tv, slot, p);
273  } else {
274  TmThreadsSlotHandlePostPQs(tv, slot);
275 
276  /* packet could have been passed to us that we won't use
277  * return it to the pool. */
278  if (p != NULL)
279  tv->tmqh_out(tv, p);
280  }
281 }
282 
283 void TmThreadsListThreads(void);
284 int TmThreadsRegisterThread(ThreadVars *tv, const int type);
285 void TmThreadsUnregisterThread(const int id);
286 int TmThreadsInjectPacketsById(Packet **, int id);
287 
288 void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts);
289 void TmreadsGetMinimalTimestamp(struct timeval *ts);
290 
291 #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:1271
TmEcode TmThreadSetThreadPriority(ThreadVars *, int)
Set the thread options (thread priority).
Definition: tm-threads.c:973
#define SCMutex
uint16_t flags
void TmThreadCheckThreadState(void)
Used to check the thread for certain conditions of failure.
Definition: tm-threads.c:2071
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:1332
void TmThreadKillThreadsFamily(int family)
Definition: tm-threads.c:1760
void TmThreadClearThreadsFamily(int family)
Definition: tm-threads.c:1850
int TmThreadsInjectPacketsById(Packet **, int id)
Definition: tm-threads.c:2438
void TmThreadSetFlags(ThreadVars *, uint8_t)
TmEcode TmThreadSpawn(ThreadVars *)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1875
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:1241
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:1299
void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *)
Appends a new entry to the slots.
Definition: tm-threads.c:832
ThreadVars * TmThreadsGetTVContainingSlot(TmSlot *)
Definition: tm-threads.c:802
void TmThreadAppend(ThreadVars *, int)
Appends this TV to tv_root based on its type.
Definition: tm-threads.c:1358
void TmThreadDisablePacketThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1671
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-threads.h:60
void TmThreadsSetFlag(ThreadVars *, uint16_t)
Set a thread flag.
Definition: tm-threads.c:97
#define THV_CAPTURE_INJECT_PKT
Definition: threadvars.h:53
void TmThreadDumpThreads(void)
Definition: tm-threads.c:2243
#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:2386
#define SCMutexLock(mut)
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1136
TmEcode TmThreadSetupOptions(ThreadVars *)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:1061
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:2038
#define SCMutexUnlock(mut)
void TmThreadsUnsetFlag(ThreadVars *, uint16_t)
Unset a thread flag.
Definition: tm-threads.c:105
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:1973
void TmThreadContinueThreads(void)
Unpauses all threads present in tv_root.
Definition: tm-threads.c:2015
TmEcode TmThreadWaitOnThreadInit(void)
Used to check if all threads have finished their initialization. On finding an un-initialized thread...
Definition: tm-threads.c:2099
uint8_t type
ThreadVars * TmThreadsGetCallingThread(void)
Returns the TV for the calling thread.
Definition: tm-threads.c:2176
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:2048
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:1924
uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags)
returns a count of all the threads that match the flag
Definition: tm-threads.c:2203
Packet * top
Definition: decode.h:622
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:2353
void TmThreadContinue(ThreadVars *)
Unpauses a thread.
Definition: tm-threads.c:2005
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1095
SCMutex tv_root_lock
Definition: tm-threads.c:81
PacketQueue slot_pre_pq
Definition: tm-threads.h:73
TmEcode TmThreadSetCPU(ThreadVars *, uint8_t)
Definition: tm-threads.c:1029
void TmThreadsListThreads(void)
Definition: tm-threads.c:2281
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:1396
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1832
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:1991
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:78
ThreadVars * tv
Definition: tm-threads.h:55
void TmreadsGetMinimalTimestamp(struct timeval *ts)
Definition: tm-threads.c:2402
TmEcode TmThreadsSlotVarRun(ThreadVars *tv, Packet *p, TmSlot *slot)
Separate run function so we can call it recursively.
Definition: tm-threads.c:115
int TmThreadsRegisterThread(ThreadVars *tv, const int type)
Definition: tm-threads.c:2310
#define SleepUsec(usec)
Definition: tm-threads.h:43
uint64_t ts
void TmThreadKillThreads(void)
Definition: tm-threads.c:1789
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:881
Packet * PacketDequeue(PacketQueue *q)
Definition: packet-queue.c:167
void TmThreadSetPrio(ThreadVars *)
Adjusting nice value for threads.
Definition: tm-threads.c:984
TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t)
Set the thread options (cpu affinity).
Definition: tm-threads.c:1020
Per thread variable structure.
Definition: threadvars.h:57
uint32_t flags
Definition: decode.h:444
int id
Definition: tm-threads.h:84
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:1045
int TmThreadsCheckFlag(ThreadVars *, uint16_t)
Check if a thread flag is set.
Definition: tm-threads.c:89
void TmThreadDisableReceiveThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1566
SCMutex mutex_q
Definition: decode.h:628
TmSlot * TmThreadGetFirstTmSlotForPartialPattern(const char *)
Definition: tm-threads.c:1727
void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
Release all the packets in the queue back to the packetpool. Mainly used by threads that have failed...