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 *);
51 
52 typedef struct TmSlot_ {
53  /* function pointers */
54  union {
56  TmEcode (*PktAcqLoop)(ThreadVars *, void *, void *);
57  TmEcode (*Management)(ThreadVars *, void *);
58  };
59  /** linked list of slots, used when a pipeline has multiple slots
60  * in a single thread. */
61  struct TmSlot_ *slot_next;
62 
63  SC_ATOMIC_DECLARE(void *, slot_data);
64 
65  TmEcode (*SlotThreadInit)(ThreadVars *, const void *, void **);
68 
69  /* data storage */
70  const void *slot_initdata;
71  /* store the thread module id */
72  int tm_id;
73 
74 } TmSlot;
75 
76 extern ThreadVars *tv_root[TVT_MAX];
77 
78 extern SCMutex tv_root_lock;
79 
80 void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *);
82 
83 ThreadVars *TmThreadCreate(const char *, const char *, const char *, const char *, const char *, const char *,
84  void *(fn_p)(void *), int);
85 ThreadVars *TmThreadCreatePacketHandler(const char *, const char *, const char *, const char *, const char *,
86  const char *);
87 ThreadVars *TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int);
88 ThreadVars *TmThreadCreateMgmtThreadByName(const char *name, const char *module,
89  int mucond);
90 ThreadVars *TmThreadCreateCmdThreadByName(const char *name, const char *module,
91  int mucond);
93 void TmThreadSetFlags(ThreadVars *, uint8_t);
94 void TmThreadKillThreadsFamily(int family);
95 void TmThreadKillThreads(void);
96 void TmThreadClearThreadsFamily(int family);
97 void TmThreadAppend(ThreadVars *, int);
98 void TmThreadSetGroupName(ThreadVars *tv, const char *name);
99 void TmThreadDumpThreads(void);
100 
103 TmEcode TmThreadSetCPU(ThreadVars *, uint8_t);
106 int TmThreadGetNbThreads(uint8_t type);
107 
108 void TmThreadInitMC(ThreadVars *);
111 void TmThreadContinueThreads(void);
112 void TmThreadPause(ThreadVars *);
113 void TmThreadPauseThreads(void);
114 void TmThreadCheckThreadState(void);
117 
118 int TmThreadsCheckFlag(ThreadVars *, uint32_t);
119 void TmThreadsSetFlag(ThreadVars *, uint32_t);
120 void TmThreadsUnsetFlag(ThreadVars *, uint32_t);
121 void TmThreadWaitForFlag(ThreadVars *, uint32_t);
122 
124 
129 
130 uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags);
131 
132 static inline void TmThreadsCleanDecodePQ(PacketQueueNoLock *pq)
133 {
134  while (1) {
135  Packet *p = PacketDequeueNoLock(pq);
136  if (unlikely(p == NULL))
137  break;
138  TmqhOutputPacketpool(NULL, p);
139  }
140 }
141 
142 static inline void TmThreadsSlotProcessPktFail(ThreadVars *tv, TmSlot *s, Packet *p)
143 {
144  if (p != NULL) {
146  }
147  TmThreadsCleanDecodePQ(&tv->decode_pq);
148  if (tv->stream_pq_local) {
152  }
154 }
155 
156 /**
157  * \brief Handle timeout from the capture layer. Checks
158  * stream_pq which may have been filled by the flow
159  * manager.
160  * \param s pipeline to run on these packets.
161  */
162 static inline void TmThreadsHandleInjectedPackets(ThreadVars *tv)
163 {
165  if (pq && pq->len > 0) {
166  while (1) {
167  SCMutexLock(&pq->mutex_q);
168  Packet *extra_p = PacketDequeue(pq);
169  SCMutexUnlock(&pq->mutex_q);
170  if (extra_p == NULL)
171  break;
173  if (r == TM_ECODE_FAILED) {
174  TmThreadsSlotProcessPktFail(tv, tv->tm_flowworker, extra_p);
175  break;
176  }
177  tv->tmqh_out(tv, extra_p);
178  }
179  }
180 }
181 
182 /**
183  * \brief Process the rest of the functions (if any) and queue.
184  */
185 static inline TmEcode TmThreadsSlotProcessPkt(ThreadVars *tv, TmSlot *s, Packet *p)
186 {
187  if (s == NULL) {
188  tv->tmqh_out(tv, p);
189  return TM_ECODE_OK;
190  }
191 
192  TmEcode r = TmThreadsSlotVarRun(tv, p, s);
193  if (unlikely(r == TM_ECODE_FAILED)) {
194  TmThreadsSlotProcessPktFail(tv, s, p);
195  return TM_ECODE_FAILED;
196  }
197 
198  tv->tmqh_out(tv, p);
199 
200  TmThreadsHandleInjectedPackets(tv);
201 
202  return TM_ECODE_OK;
203 }
204 
205 /** \brief inject packet if THV_CAPTURE_INJECT_PKT is set
206  * Allow caller to supply their own packet
207  *
208  * Meant for detect reload process that interupts an sleeping capture thread
209  * to force a packet through the engine to complete a reload */
210 static inline void TmThreadsCaptureInjectPacket(ThreadVars *tv, Packet *p)
211 {
213  if (p == NULL)
215  if (p != NULL) {
218  if (TmThreadsSlotProcessPkt(tv, tv->tm_flowworker, p) != TM_ECODE_OK) {
220  }
221  }
222 }
223 
224 static inline void TmThreadsCaptureHandleTimeout(ThreadVars *tv, Packet *p)
225 {
227  TmThreadsCaptureInjectPacket(tv, p);
228  } else {
229  TmThreadsHandleInjectedPackets(tv);
230 
231  /* packet could have been passed to us that we won't use
232  * return it to the pool. */
233  if (p != NULL)
234  tv->tmqh_out(tv, p);
235  }
236 }
237 
238 void TmThreadsListThreads(void);
239 int TmThreadsRegisterThread(ThreadVars *tv, const int type);
240 void TmThreadsUnregisterThread(const int id);
241 int TmThreadsInjectPacketsById(Packet **, int id);
242 
243 void TmThreadsInitThreadsTimestamp(const struct timeval *ts);
244 void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts);
245 void TmThreadsGetMinimalTimestamp(struct timeval *ts);
246 bool TmThreadsTimeSubsysIsReady(void);
247 
248 #endif /* __TM_THREADS_H__ */
tv_root
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:78
TmSlot_::tm_id
int tm_id
Definition: tm-threads.h:72
ts
uint64_t ts
Definition: source-erf-file.c:2
TmThreadSetPrio
void TmThreadSetPrio(ThreadVars *)
Adjusting nice value for threads.
Definition: tm-threads.c:775
TmThreadContinue
void TmThreadContinue(ThreadVars *)
Unpauses a thread.
Definition: tm-threads.c:1744
TmThreadSetCPU
TmEcode TmThreadSetCPU(ThreadVars *, uint8_t)
Definition: tm-threads.c:820
TmThreadsListThreads
void TmThreadsListThreads(void)
Definition: tm-threads.c:1997
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
TmThreadPauseThreads
void TmThreadPauseThreads(void)
Pauses all threads present in tv_root.
Definition: tm-threads.c:1782
PacketQueue_
simple fifo queue for packets with mutex and cond Calling the mutex or triggering the cond is respons...
Definition: packet-queue.h:47
TmSlot_::SlotFunc
TmSlotFunc SlotFunc
Definition: tm-threads.h:55
PKT_SRC_CAPTURE_TIMEOUT
@ PKT_SRC_CAPTURE_TIMEOUT
Definition: decode.h:59
Packet_::flags
uint32_t flags
Definition: decode.h:444
TmThreadsSlotVarRun
TmEcode TmThreadsSlotVarRun(ThreadVars *tv, Packet *p, TmSlot *slot)
Separate run function so we can call it recursively.
Definition: tm-threads.c:113
tm-threads-common.h
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
TmThreadKillThreadsFamily
void TmThreadKillThreadsFamily(int family)
Definition: tm-threads.c:1509
ThreadVars_::stream_pq_local
struct PacketQueue_ * stream_pq_local
Definition: threadvars.h:114
TmThreadClearThreadsFamily
void TmThreadClearThreadsFamily(int family)
Definition: tm-threads.c:1605
tm-modules.h
TmThreadsGetCallingThread
ThreadVars * TmThreadsGetCallingThread(void)
Returns the TV for the calling thread.
Definition: tm-threads.c:1899
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1630
TmThreadsInitThreadsTimestamp
void TmThreadsInitThreadsTimestamp(const struct timeval *ts)
Definition: tm-threads.c:2131
SleepUsec
#define SleepUsec(usec)
Definition: tm-threads.h:43
TmqhOutputPacketpool
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
Definition: tmqh-packetpool.c:449
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
PacketQueueNoLock_
simple fifo queue for packets
Definition: packet-queue.h:32
tmqh-packetpool.h
TmqhReleasePacketsToPacketPool
void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
Release all the packets in the queue back to the packetpool. Mainly used by threads that have failed,...
Definition: tmqh-packetpool.c:554
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
TmThreadsUnregisterThread
void TmThreadsUnregisterThread(const int id)
Definition: tm-threads.c:2064
PacketQueue_::mutex_q
SCMutex mutex_q
Definition: packet-queue.h:54
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1118
TmThreadDisablePacketThreads
void TmThreadDisablePacketThreads(void)
Disable all packet threads.
Definition: tm-threads.c:1429
TmThreadGetFirstTmSlotForPartialPattern
TmSlot * TmThreadGetFirstTmSlotForPartialPattern(const char *)
Definition: tm-threads.c:1476
TmThreadsGetMinimalTimestamp
void TmThreadsGetMinimalTimestamp(struct timeval *ts)
Definition: tm-threads.c:2146
TmSlot_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-threads.h:56
type
uint8_t type
Definition: decode-icmpv4.h:2
PacketDequeueNoLock
Packet * PacketDequeueNoLock(PacketQueueNoLock *qnl)
Definition: packet-queue.c:206
TmSlot_::Management
TmEcode(* Management)(ThreadVars *, void *)
Definition: tm-threads.h:57
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1078
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
TmThreadDisableReceiveThreads
void TmThreadDisableReceiveThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1325
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *)
Appends a new entry to the slots.
Definition: tm-threads.c:623
TmThreadCreateCmdThreadByName
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:1132
TmThreadSetGroupName
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1587
Packet_
Definition: decode.h:408
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *, uint32_t)
Set a thread flag.
Definition: tm-threads.c:97
TmThreadWaitOnThreadInit
TmEcode TmThreadWaitOnThreadInit(void)
Used to check if all threads have finished their initialization. On finding an un-initialized thread,...
Definition: tm-threads.c:1824
THV_CAPTURE_INJECT_PKT
#define THV_CAPTURE_INJECT_PKT
Definition: threadvars.h:53
TmThreadCreate
ThreadVars * TmThreadCreate(const char *, const char *, const char *, const char *, const char *, const char *, void *(fn_p)(void *), int)
TmSlot_
Definition: tm-threads.h:52
TmEcode
TmEcode
Definition: tm-threads-common.h:77
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:836
TmThreadsInjectPacketsById
int TmThreadsInjectPacketsById(Packet **, int id)
Definition: tm-threads.c:2186
TmThreadsRegisterThread
int TmThreadsRegisterThread(ThreadVars *tv, const int type)
Definition: tm-threads.c:2021
TmModule_
Definition: tm-modules.h:43
TmThreadCreateMgmtThread
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:1071
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:1099
TmSlot
struct TmSlot_ TmSlot
TmThreadsGetTVContainingSlot
ThreadVars * TmThreadsGetTVContainingSlot(TmSlot *)
Definition: tm-threads.c:593
tv_root_lock
SCMutex tv_root_lock
Definition: tm-threads.c:81
TmThreadKillThreads
void TmThreadKillThreads(void)
Definition: tm-threads.c:1538
flags
uint8_t flags
Definition: decode-gre.h:2
TmThreadAppend
void TmThreadAppend(ThreadVars *, int)
Appends this TV to tv_root based on its type.
Definition: tm-threads.c:1158
TmThreadDumpThreads
void TmThreadDumpThreads(void)
Definition: tm-threads.c:1947
ThreadVars_::tmqh_out
void(* tmqh_out)(struct ThreadVars_ *, struct Packet_ *)
Definition: threadvars.h:105
THV_FAILED
#define THV_FAILED
Definition: threadvars.h:41
ThreadVars_::tm_flowworker
struct TmSlot_ * tm_flowworker
Definition: threadvars.h:100
TmThreadInitMC
void TmThreadInitMC(ThreadVars *)
Initializes the mutex and condition variables for this TV.
Definition: tm-threads.c:1663
PacketQueue_::len
uint32_t len
Definition: packet-queue.h:50
TmSlot_::SlotThreadInit
TmEcode(* SlotThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-threads.h:65
TmThreadWaitForFlag
void TmThreadWaitForFlag(ThreadVars *, uint32_t)
Waits till the specified flag(s) is(are) set. We don't bother if the kill flag has been set or not on...
Definition: tm-threads.c:1730
TmSlot_::slot_initdata
const void * slot_initdata
Definition: tm-threads.h:70
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
TmThreadPause
void TmThreadPause(ThreadVars *)
Pauses a thread.
Definition: tm-threads.c:1773
PacketDequeue
Packet * PacketDequeue(PacketQueue *q)
Definition: packet-queue.c:212
TmThreadSetCPUAffinity
TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t)
Set the thread options (cpu affinity).
Definition: tm-threads.c:811
TmThreadsSetThreadTimestamp
void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts)
Definition: tm-threads.c:2097
TmSlot_::SlotThreadDeinit
TmEcode(* SlotThreadDeinit)(ThreadVars *, void *)
Definition: tm-threads.h:67
TVT_MAX
@ TVT_MAX
Definition: tm-threads-common.h:88
TmSlot_::SlotThreadExitPrintStats
void(* SlotThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-threads.h:66
TmSlot_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(void *, slot_data)
ThreadVars_::decode_pq
PacketQueueNoLock decode_pq
Definition: threadvars.h:109
TmThreadCheckThreadState
void TmThreadCheckThreadState(void)
Used to check the thread for certain conditions of failure.
Definition: tm-threads.c:1800
TmThreadTestThreadUnPaused
void TmThreadTestThreadUnPaused(ThreadVars *)
Tests if the thread represented in the arg has been unpaused or not.
Definition: tm-threads.c:1712
TmSlot_::slot_next
struct TmSlot_ * slot_next
Definition: tm-threads.h:61
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *, uint32_t)
Check if a thread flag is set.
Definition: tm-threads.c:89
TmThreadsUnsetFlag
void TmThreadsUnsetFlag(ThreadVars *, uint32_t)
Unset a thread flag.
Definition: tm-threads.c:105
TmThreadSetThreadPriority
TmEcode TmThreadSetThreadPriority(ThreadVars *, int)
Set the thread options (thread priority).
Definition: tm-threads.c:764
TmSlotGetSlotForTM
TmSlot * TmSlotGetSlotForTM(int)
Returns the slot holding a TM with the particular tm_id.
Definition: tm-threads.c:672
TmSlotFunc
TmEcode(* TmSlotFunc)(ThreadVars *, Packet *, void *)
Definition: tm-threads.h:50
TmThreadContinueThreads
void TmThreadContinueThreads(void)
Unpauses all threads present in tv_root.
Definition: tm-threads.c:1754
TmThreadSetFlags
void TmThreadSetFlags(ThreadVars *, uint8_t)
SCMutex
#define SCMutex
Definition: threads-debug.h:114
PacketGetFromQueueOrAlloc
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:180
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:852
TmThreadsTimeSubsysIsReady
bool TmThreadsTimeSubsysIsReady(void)
Definition: tm-threads.c:2114
TmThreadCountThreadsByTmmFlags
uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags)
returns a count of all the threads that match the flag
Definition: tm-threads.c:1921
TmThreadCreatePacketHandler
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't support custo...
Definition: tm-threads.c:1041