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 #define TM_QUEUE_NAME_MAX 16
33 #define TM_THREAD_NAME_MAX 16
34 
35 typedef TmEcode (*TmSlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *,
36  PacketQueue *);
37 
38 typedef struct TmSlot_ {
39  /* the TV holding this slot */
41 
42  /* function pointers */
43  SC_ATOMIC_DECLARE(TmSlotFunc, SlotFunc);
44 
45  TmEcode (*PktAcqLoop)(ThreadVars *, void *, void *);
46 
47  TmEcode (*SlotThreadInit)(ThreadVars *, const void *, void **);
50 
51  /* data storage */
52  const void *slot_initdata;
53  SC_ATOMIC_DECLARE(void *, slot_data);
54 
55  /* queue filled by the SlotFunc with packets that will
56  * be processed futher _before_ the current packet.
57  * The locks in the queue are NOT used */
59 
60  /* queue filled by the SlotFunc with packets that will
61  * be processed futher _after_ the current packet. The
62  * locks in the queue are NOT used */
64 
65  /* store the thread module id */
66  int tm_id;
67 
68  /* slot id, only used my TmVarSlot to know what the first slot is */
69  int id;
70 
71  /* linked list, only used when you have multiple slots(used by TmVarSlot) */
72  struct TmSlot_ *slot_next;
73 
74  /* just called once, so not perf critical */
75  TmEcode (*Management)(ThreadVars *, void *);
76 
77 } TmSlot;
78 
79 extern ThreadVars *tv_root[TVT_MAX];
80 
81 extern SCMutex tv_root_lock;
82 
83 void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *);
85 
86 ThreadVars *TmThreadCreate(const char *, const char *, const char *, const char *, const char *, const char *,
87  void *(fn_p)(void *), int);
88 ThreadVars *TmThreadCreatePacketHandler(const char *, const char *, const char *, const char *, const char *,
89  const char *);
90 ThreadVars *TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int);
91 ThreadVars *TmThreadCreateMgmtThreadByName(const char *name, const char *module,
92  int mucond);
93 ThreadVars *TmThreadCreateCmdThreadByName(const char *name, const char *module,
94  int mucond);
96 void TmThreadSetFlags(ThreadVars *, uint8_t);
97 void TmThreadKillThreadsFamily(int family);
98 void TmThreadKillThreads(void);
99 void TmThreadClearThreadsFamily(int family);
100 void TmThreadAppend(ThreadVars *, int);
101 void TmThreadRemove(ThreadVars *, int);
102 void TmThreadSetGroupName(ThreadVars *tv, const char *name);
103 
106 TmEcode TmThreadSetCPU(ThreadVars *, uint8_t);
109 int TmThreadGetNbThreads(uint8_t type);
110 
111 void TmThreadInitMC(ThreadVars *);
114 void TmThreadContinueThreads(void);
115 void TmThreadPause(ThreadVars *);
116 void TmThreadPauseThreads(void);
117 void TmThreadCheckThreadState(void);
120 
121 int TmThreadsCheckFlag(ThreadVars *, uint16_t);
122 void TmThreadsSetFlag(ThreadVars *, uint16_t);
123 void TmThreadsUnsetFlag(ThreadVars *, uint16_t);
124 void TmThreadWaitForFlag(ThreadVars *, uint16_t);
125 
127 
132 
133 uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags);
134 
135 /**
136  * \brief Process the rest of the functions (if any) and queue.
137  */
138 static inline TmEcode TmThreadsSlotProcessPkt(ThreadVars *tv, TmSlot *s, Packet *p)
139 {
140  TmEcode r = TM_ECODE_OK;
141 
142  if (s == NULL) {
143  tv->tmqh_out(tv, p);
144  return r;
145  }
146 
147  if (TmThreadsSlotVarRun(tv, p, s) == TM_ECODE_FAILED) {
148  TmqhOutputPacketpool(tv, p);
149  TmSlot *slot = s;
150  while (slot != NULL) {
154 
155  slot = slot->slot_next;
156  }
158  r = TM_ECODE_FAILED;
159 
160  } else {
161  tv->tmqh_out(tv, p);
162 
163  /* post process pq */
164  TmSlot *slot = s;
165  while (slot != NULL) {
166  if (slot->slot_post_pq.top != NULL) {
167  while (1) {
169  Packet *extra_p = PacketDequeue(&slot->slot_post_pq);
171 
172  if (extra_p == NULL)
173  break;
174 
175  if (slot->slot_next != NULL) {
176  r = TmThreadsSlotVarRun(tv, extra_p, slot->slot_next);
177  if (r == TM_ECODE_FAILED) {
181 
182  TmqhOutputPacketpool(tv, extra_p);
184  break;
185  }
186  }
187  tv->tmqh_out(tv, extra_p);
188  }
189  } /* if (slot->slot_post_pq.top != NULL) */
190  slot = slot->slot_next;
191  } /* while (slot != NULL) */
192  }
193 
194  return r;
195 }
196 
197 /** \brief inject packet if THV_CAPTURE_INJECT_PKT is set
198  * Allow caller to supply their own packet
199  *
200  * Meant for detect reload process that interupts an sleeping capture thread
201  * to force a packet through the engine to complete a reload */
202 static inline void TmThreadsCaptureInjectPacket(ThreadVars *tv, TmSlot *slot, Packet *p)
203 {
206  if (p == NULL)
208  if (p != NULL) {
210  if (TmThreadsSlotProcessPkt(tv, slot, p) != TM_ECODE_OK) {
211  TmqhOutputPacketpool(tv, p);
212  }
213  }
214  }
215 }
216 
217 void TmThreadsListThreads(void);
218 int TmThreadsRegisterThread(ThreadVars *tv, const int type);
219 void TmThreadsUnregisterThread(const int id);
220 int TmThreadsInjectPacketsById(Packet **, int id);
221 
222 void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts);
223 void TmreadsGetMinimalTimestamp(struct timeval *ts);
224 
225 #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:1282
TmEcode TmThreadSetThreadPriority(ThreadVars *, int)
Set the thread options (thread priority).
Definition: tm-threads.c:984
uint16_t flags
void TmThreadCheckThreadState(void)
Used to check the thread for certain conditions of failure.
Definition: tm-threads.c:2078
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:1343
void TmThreadKillThreadsFamily(int family)
Definition: tm-threads.c:1767
void TmThreadClearThreadsFamily(int family)
Definition: tm-threads.c:1857
int TmThreadsInjectPacketsById(Packet **, int id)
Definition: tm-threads.c:2397
void TmThreadSetFlags(ThreadVars *, uint8_t)
TmEcode TmThreadSpawn(ThreadVars *)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1882
TmEcode(* SlotThreadDeinit)(ThreadVars *, void *)
Definition: tm-threads.h:49
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:1252
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:1310
void TmSlotSetFuncAppend(ThreadVars *, TmModule *, const void *)
Appends a new entry to the slots.
Definition: tm-threads.c:836
ThreadVars * TmThreadsGetTVContainingSlot(TmSlot *)
Definition: tm-threads.c:799
void TmThreadAppend(ThreadVars *, int)
Appends this TV to tv_root based on its type.
Definition: tm-threads.c:1369
void TmThreadDisablePacketThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1663
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-threads.h:45
void TmThreadsSetFlag(ThreadVars *, uint16_t)
Set a thread flag.
Definition: tm-threads.c:113
#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:176
void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts)
Definition: tm-threads.c:2345
TmEcode TmThreadSetupOptions(ThreadVars *)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:1072
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:2045
void TmThreadsUnsetFlag(ThreadVars *, uint16_t)
Unset a thread flag.
Definition: tm-threads.c:121
#define SCMutexUnlock(mut)
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:1980
uint16_t type
void TmThreadContinueThreads(void)
Unpauses all threads present in tv_root.
Definition: tm-threads.c:2022
TmEcode TmThreadWaitOnThreadInit(void)
Used to check if all threads have finished their initialization. On finding an un-initialized thread...
Definition: tm-threads.c:2106
ThreadVars * TmThreadsGetCallingThread(void)
Returns the TV for the calling thread.
Definition: tm-threads.c:2183
const void * slot_initdata
Definition: tm-threads.h:52
void(* tmqh_out)(struct ThreadVars_ *, struct Packet_ *)
Definition: threadvars.h:80
PacketQueue slot_post_pq
Definition: tm-threads.h:63
void TmThreadPauseThreads(void)
Pauses all threads present in tv_root.
Definition: tm-threads.c:2055
TmEcode(* Management)(ThreadVars *, void *)
Definition: tm-threads.h:75
void TmThreadInitMC(ThreadVars *)
Sets the thread flags for a thread instance(tv)
Definition: tm-threads.c:1931
uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags)
returns a count of all the threads that match the flag
Definition: tm-threads.c:2210
Packet * top
Definition: decode.h:631
int tm_id
Definition: tm-threads.h:66
struct TmSlot_ * slot_next
Definition: tm-threads.h:72
void TmThreadsUnregisterThread(const int id)
Definition: tm-threads.c:2312
void TmThreadContinue(ThreadVars *)
Unpauses a thread.
Definition: tm-threads.c:2012
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1102
SCMutex tv_root_lock
Definition: tm-threads.c:97
PacketQueue slot_pre_pq
Definition: tm-threads.h:58
TmEcode TmThreadSetCPU(ThreadVars *, uint8_t)
Definition: tm-threads.c:1040
void TmThreadsListThreads(void)
Definition: tm-threads.c:2248
SC_ATOMIC_DECLARE(TmSlotFunc, SlotFunc)
TmEcode(* SlotThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-threads.h:47
void(* SlotThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-threads.h:48
void TmThreadRemove(ThreadVars *, int)
Removes this TV from tv_root based on its type.
Definition: tm-threads.c:1407
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1839
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:1998
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:94
ThreadVars * tv
Definition: tm-threads.h:40
void TmreadsGetMinimalTimestamp(struct timeval *ts)
Definition: tm-threads.c:2361
TmEcode TmThreadsSlotVarRun(ThreadVars *tv, Packet *p, TmSlot *slot)
Separate run function so we can call it recursively.
Definition: tm-threads.c:131
int TmThreadsRegisterThread(ThreadVars *tv, const int type)
Definition: tm-threads.c:2269
uint64_t ts
#define SCMutex
void TmThreadKillThreads(void)
Definition: tm-threads.c:1796
#define SCMutexLock(mut)
TmEcode(* TmSlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *)
Definition: tm-threads.h:35
struct TmSlot_ TmSlot
TmSlot * TmSlotGetSlotForTM(int)
Returns the slot holding a TM with the particular tm_id.
Definition: tm-threads.c:885
Packet * PacketDequeue(PacketQueue *q)
Definition: packet-queue.c:167
void TmThreadSetPrio(ThreadVars *)
Adjusting nice value for threads.
Definition: tm-threads.c:995
TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t)
Set the thread options (cpu affinity).
Definition: tm-threads.c:1031
Per thread variable structure.
Definition: threadvars.h:57
uint32_t flags
Definition: decode.h:442
int id
Definition: tm-threads.h:69
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:1056
int TmThreadsCheckFlag(ThreadVars *, uint16_t)
Check if a thread flag is set.
Definition: tm-threads.c:105
void TmThreadDisableReceiveThreads(void)
Disable all threads having the specified TMs.
Definition: tm-threads.c:1567
SCMutex mutex_q
Definition: decode.h:637
TmSlot * TmThreadGetFirstTmSlotForPartialPattern(const char *)
Definition: tm-threads.c:1734
void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
Release all the packets in the queue back to the packetpool. Mainly used by threads that have failed...