suricata
threadvars.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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  */
23 
24 #ifndef SURICATA_THREADVARS_H
25 #define SURICATA_THREADVARS_H
26 
27 #include "tm-queues.h"
28 #include "counters.h"
29 #include "packet-queue.h"
30 #include "util-storage.h"
31 
32 struct TmSlot_;
33 
34 /** Thread flags set and read by threads to control the threads */
35 // bit 0 vacant
36 #define THV_INIT_DONE BIT_U32(1) /** thread initialization done */
37 #define THV_PAUSE BIT_U32(2) /** signal thread to pause itself */
38 #define THV_PAUSED BIT_U32(3) /** the thread is paused atm */
39 #define THV_KILL BIT_U32(4) /** thread has been asked to cleanup and exit */
40 #define THV_FAILED BIT_U32(5) /** thread has encountered an error and failed */
41 #define THV_CLOSED BIT_U32(6) /** thread done, should be joinable */
42 /* used to indicate the thread is going through de-init. Introduced as more
43  * of a hack for solving stream-timeout-shutdown. Is set by the main thread. */
44 #define THV_DEINIT BIT_U32(7)
45 #define THV_RUNNING_DONE BIT_U32(8) /** thread has completed running and is entering
46  * the de-init phase */
47 #define THV_REQ_FLOW_LOOP BIT_U32(9) /**< request thread to enter flow timeout loop */
48 #define THV_FLOW_LOOP BIT_U32(10) /**< thread is in flow shutdown loop */
49 
50 /** signal thread's capture method to create a fake packet to force through
51  * the engine. This is to force timely handling of maintenance tasks like
52  * rule reloads even if no packets are read by the capture method. */
53 #define THV_CAPTURE_INJECT_PKT BIT_U32(11)
54 #define THV_DEAD BIT_U32(12) /**< thread has been joined with pthread_join() */
55 #define THV_RUNNING BIT_U32(13) /**< thread is running */
56 
57 /** \brief Per thread variable structure */
58 typedef struct ThreadVars_ {
59  pthread_t t;
60  /** function pointer to the function that runs the packet pipeline for
61  * this thread. It is passed directly to pthread_create(), hence the
62  * void pointers in and out. */
63  void *(*tm_func)(void *);
64 
65  char name[16];
66  char *printable_name;
67 
68  uint8_t thread_setup_flags;
69 
70  /** the type of thread as defined in tm-threads.h (TVT_PPT, TVT_MGMT) */
71  uint8_t type;
72 
73  uint16_t cpu_affinity; /** cpu or core number to set affinity to */
74  int thread_priority; /** priority (real time) for this thread. Look at threads.h */
75 
76 
77  /** TmModule::flags for each module part of this thread */
78  uint8_t tmm_flags;
79 
80  uint8_t cap_flags; /**< Flags to indicate the capabilities of all the
81  TmModules registered under this thread */
82  uint8_t inq_id;
83  uint8_t outq_id;
84 
85  /** local id */
86  int id;
87 
88  /** incoming queue and handler */
89  Tmq *inq;
90  struct Packet_ * (*tmqh_in)(struct ThreadVars_ *);
91 
92  SC_ATOMIC_DECLARE(uint32_t, flags);
93 
94  /** list of TmSlot objects together forming the packet pipeline. */
95  struct TmSlot_ *tm_slots;
96 
97  /** pointer to the flowworker in the pipeline. Used as starting point
98  * for injected packets. Can be NULL if the flowworker is not part
99  * of this thread. */
100  struct TmSlot_ *tm_flowworker;
101 
102  /** outgoing queue and handler */
104  void *outctx;
105  void (*tmqh_out)(struct ThreadVars_ *, struct Packet_ *);
106 
107  /** Queue for decoders to temporarily store extra packets they
108  * generate. These packets are generated as part of the tunnel
109  * handling, and are processed directly after the "real" packet
110  * from the current position in the pipeline. */
112 
113  /** Stream packet queue for flow time out injection. Either a pointer to the
114  * workers input queue or to stream_pq_local */
117 
118  /* counters */
119 
120  /** private counter store: counter updates modify this */
122 
123  /** pointer to the next thread */
124  struct ThreadVars_ *next;
125 
126  /* mutex and condition used by management threads */
127 
130 
132  bool break_loop;
133 
134  /** Interface-specific thread affinity */
135  char *iface_name;
136 
138 } ThreadVars;
139 
140 /** Thread setup flags: */
141 #define THREAD_SET_AFFINITY 0x01 /** CPU/Core affinity */
142 #define THREAD_SET_PRIORITY 0x02 /** Real time priority */
143 #define THREAD_SET_AFFTYPE 0x04 /** Priority and affinity */
144 
145 /**
146  * \brief Allocate a new ThreadVars structure.
147  *
148  * \retval NULL if allocation failed.
149  * \retval Pointer to newly allocated ThreadVars structure.
150  */
152 
153 /**
154  * \brief Free a ThreadVars structure.
155  *
156  * \param tv Pointer to ThreadVars structure to be freed.
157  */
159 
160 #endif /* SURICATA_THREADVARS_H */
ThreadVars_::flow_queue
struct FlowQueue_ * flow_queue
Definition: threadvars.h:130
ThreadVars_::storage
Storage storage[]
Definition: threadvars.h:136
ThreadVars_::iface_name
char * iface_name
Definition: threadvars.h:134
ThreadVars_::name
char name[16]
Definition: threadvars.h:64
ThreadVars_::outctx
void * outctx
Definition: threadvars.h:103
PacketQueue_
simple fifo queue for packets with mutex and cond Calling the mutex or triggering the cond is respons...
Definition: packet-queue.h:49
ThreadVars_::t
pthread_t t
Definition: threadvars.h:58
packet-queue.h
ThreadVars_::outq
Tmq * outq
Definition: threadvars.h:102
ThreadVars_::stream_pq_local
struct PacketQueue_ * stream_pq_local
Definition: threadvars.h:115
ThreadVars_::cpu_affinity
uint16_t cpu_affinity
Definition: threadvars.h:72
PacketQueueNoLock_
simple fifo queue for packets
Definition: packet-queue.h:34
Storage
Definition: util-storage.h:40
ThreadVars_::cap_flags
uint8_t cap_flags
Definition: threadvars.h:79
ThreadVars_::tmm_flags
uint8_t tmm_flags
Definition: threadvars.h:77
counters.h
ThreadVars_::tm_slots
struct TmSlot_ * tm_slots
Definition: threadvars.h:94
ThreadVarsFree
void ThreadVarsFree(ThreadVars *tv)
Free a ThreadVars structure.
Definition: threadvars.c:42
SCCtrlCondT
#define SCCtrlCondT
Definition: threads-debug.h:383
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
ThreadVars_::next
struct ThreadVars_ * next
Definition: threadvars.h:123
ThreadVars_::id
int id
Definition: threadvars.h:85
ThreadVars_::type
uint8_t type
Definition: threadvars.h:70
Packet_
Definition: decode.h:516
ThreadVars_::ctrl_cond
SCCtrlCondT * ctrl_cond
Definition: threadvars.h:128
TmSlot_
Definition: tm-threads.h:53
ThreadVars
struct ThreadVars_ ThreadVars
Per thread variable structure.
ThreadVars_::thread_setup_flags
uint8_t thread_setup_flags
Definition: threadvars.h:67
ThreadVarsAlloc
ThreadVars * ThreadVarsAlloc(void)
Allocate a new ThreadVars structure.
Definition: threadvars.c:28
ThreadVars_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint32_t, flags)
FlowQueue_
Definition: flow-queue.h:48
ThreadVars_::stream_pq
struct PacketQueue_ * stream_pq
Definition: threadvars.h:114
ThreadVars_::inq
Tmq * inq
Definition: threadvars.h:88
ThreadVars_::thread_priority
int thread_priority
Definition: threadvars.h:73
flags
uint8_t flags
Definition: decode-gre.h:0
SCCtrlMutex
#define SCCtrlMutex
Definition: threads-debug.h:374
tm-queues.h
ThreadVars_::tmqh_out
void(* tmqh_out)(struct ThreadVars_ *, struct Packet_ *)
Definition: threadvars.h:104
ThreadVars_::break_loop
bool break_loop
Definition: threadvars.h:131
ThreadVars_::tm_flowworker
struct TmSlot_ * tm_flowworker
Definition: threadvars.h:99
StatsThreadContext_
Definition: counters.h:131
ThreadVars_::printable_name
char * printable_name
Definition: threadvars.h:65
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
ThreadVars_::outq_id
uint8_t outq_id
Definition: threadvars.h:82
ThreadVars_::decode_pq
PacketQueueNoLock decode_pq
Definition: threadvars.h:110
Tmq_
Definition: tm-queues.h:30
ThreadVars_::ctrl_mutex
SCCtrlMutex * ctrl_mutex
Definition: threadvars.h:127
ThreadVars_::inq_id
uint8_t inq_id
Definition: threadvars.h:81
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
util-storage.h