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 __THREADVARS_H__
25 #define __THREADVARS_H__
26 
27 #include "util-affinity.h"
28 #include "tm-queues.h"
29 #include "counters.h"
30 #include "threads.h"
31 #include "packet-queue.h"
32 #include "util-atomic.h"
33 
34 struct TmSlot_;
35 
36 /** Thread flags set and read by threads to control the threads */
37 #define THV_USE                 BIT_U32(0)  /** thread is in use */
38 #define THV_INIT_DONE           BIT_U32(1)  /** thread initialization done */
39 #define THV_PAUSE               BIT_U32(2)  /** signal thread to pause itself */
40 #define THV_PAUSED              BIT_U32(3)  /** the thread is paused atm */
41 #define THV_KILL                BIT_U32(4)  /** thread has been asked to cleanup and exit */
42 #define THV_FAILED              BIT_U32(5)  /** thread has encountered an error and failed */
43 #define THV_CLOSED              BIT_U32(6)  /** thread done, should be joinable */
44 /* used to indicate the thread is going through de-init.  Introduced as more
45  * of a hack for solving stream-timeout-shutdown.  Is set by the main thread. */
46 #define THV_DEINIT              BIT_U32(7)
47 #define THV_RUNNING_DONE        BIT_U32(8)  /** thread has completed running and is entering
48                                          * the de-init phase */
49 #define THV_KILL_PKTACQ         BIT_U32(9)  /**< flag thread to stop packet acq */
50 #define THV_FLOW_LOOP           BIT_U32(10) /**< thread is in flow shutdown loop */
51 
52 /** signal thread's capture method to create a fake packet to force through
53  *  the engine. This is to force timely handling of maintenance taks like
54  *  rule reloads even if no packets are read by the capture method. */
55 #define THV_CAPTURE_INJECT_PKT  BIT_U32(11)
56 #define THV_DEAD                BIT_U32(12) /**< thread has been joined with pthread_join() */
57 
58 /** \brief Per thread variable structure */
59 typedef struct ThreadVars_ {
60     pthread_t t;
61     /** function pointer to the function that runs the packet pipeline for
62      *  this thread. It is passed directly to pthread_create(), hence the
63      *  void pointers in and out. */
64     void *(*tm_func)(void *);
65 
66     char name[16];
67     char *printable_name;
68     char *thread_group_name;
69 
70     uint8_t thread_setup_flags;
71 
72     /** the type of thread as defined in tm-threads.h (TVT_PPT, TVT_MGMT) */
73     uint8_t type;
74 
75     uint16_t cpu_affinity; /** cpu or core number to set affinity to */
76     int thread_priority; /** priority (real time) for this thread. Look at threads.h */
77 
78 
79     /** TmModule::flags for each module part of this thread */
80     uint8_t tmm_flags;
81 
82     uint8_t cap_flags; /**< Flags to indicate the capabilities of all the
83                             TmModules resgitered under this thread */
84     uint8_t inq_id;
85     uint8_t outq_id;
86 
87     /** local id */
88     int id;
89 
90     /** incoming queue and handler */
91     Tmq *inq;
92     struct Packet_ * (*tmqh_in)(struct ThreadVars_ *);
93 
94     SC_ATOMIC_DECLARE(uint32_t, flags);
95 
96     /** list of of TmSlot objects together forming the packet pipeline. */
97     struct TmSlot_ *tm_slots;
98 
99     /** pointer to the flowworker in the pipeline. Used as starting point
100      *  for injected packets. Can be NULL if the flowworker is not part
101      *  of this thread. */
102     struct TmSlot_ *tm_flowworker;
103 
104     /** outgoing queue and handler */
105     Tmq *outq;
106     void *outctx;
107     void (*tmqh_out)(struct ThreadVars_ *, struct Packet_ *);
108 
109     /** queue for decoders to temporarily store extra packets they
110      *  generate. */
111     PacketQueueNoLock decode_pq;
112 
113     /** Stream packet queue for flow time out injection. Either a pointer to the
114      *  workers input queue or to stream_pq_local */
115     struct PacketQueue_ *stream_pq;
116     struct PacketQueue_ *stream_pq_local;
117 
118     /* counters */
119 
120     /** private counter store: counter updates modify this */
121     StatsPrivateThreadContext perf_private_ctx;
122 
123     /** pointer to the next thread */
124     struct ThreadVars_ *next;
125 
126     /** public counter store: counter syncs update this */
127     StatsPublicThreadContext perf_public_ctx;
128 
129     /* mutex and condition used by management threads */
130 
131     SCCtrlMutex *ctrl_mutex;
132     SCCtrlCondT *ctrl_cond;
133 
134     struct FlowQueue_ *flow_queue;
135     bool break_loop;
136 
137 } ThreadVars;
138 
139 /** Thread setup flags: */
140 #define THREAD_SET_AFFINITY     0x01 /** CPU/Core affinity */
141 #define THREAD_SET_PRIORITY     0x02 /** Real time priority */
142 #define THREAD_SET_AFFTYPE      0x04 /** Priority and affinity */
143 
144 #endif /* __THREADVARS_H__ */
145