suricata
util-thash.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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  * thash -> thread hash. Hash table with locking handling.
24  */
25 
26 #ifndef __THASH_H__
27 #define __THASH_H__
28 
29 #include "decode.h"
30 #include "util-storage.h"
31 
32 /** Spinlocks or Mutex for the buckets. */
33 //#define HRLOCK_SPIN
34 #define HRLOCK_MUTEX
35 
36 #ifdef HRLOCK_SPIN
37  #ifdef HRLOCK_MUTEX
38  #error Cannot enable both HRLOCK_SPIN and HRLOCK_MUTEX
39  #endif
40 #endif
41 
42 #ifdef HRLOCK_SPIN
43  #define HRLOCK_TYPE SCSpinlock
44  #define HRLOCK_INIT(fb) SCSpinInit(&(fb)->lock, 0)
45  #define HRLOCK_DESTROY(fb) SCSpinDestroy(&(fb)->lock)
46  #define HRLOCK_LOCK(fb) SCSpinLock(&(fb)->lock)
47  #define HRLOCK_TRYLOCK(fb) SCSpinTrylock(&(fb)->lock)
48  #define HRLOCK_UNLOCK(fb) SCSpinUnlock(&(fb)->lock)
49 #elif defined HRLOCK_MUTEX
50  #define HRLOCK_TYPE SCMutex
51  #define HRLOCK_INIT(fb) SCMutexInit(&(fb)->lock, NULL)
52  #define HRLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->lock)
53  #define HRLOCK_LOCK(fb) SCMutexLock(&(fb)->lock)
54  #define HRLOCK_TRYLOCK(fb) SCMutexTrylock(&(fb)->lock)
55  #define HRLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->lock)
56 #else
57  #error Enable HRLOCK_SPIN or HRLOCK_MUTEX
58 #endif
59 
60 /** Spinlocks or Mutex for the queues. */
61 //#define HQLOCK_SPIN
62 #define HQLOCK_MUTEX
63 
64 #ifdef HQLOCK_SPIN
65  #ifdef HQLOCK_MUTEX
66  #error Cannot enable both HQLOCK_SPIN and HQLOCK_MUTEX
67  #endif
68 #endif
69 
70 #ifdef HQLOCK_SPIN
71  #define HQLOCK_INIT(q) SCSpinInit(&(q)->s, 0)
72  #define HQLOCK_DESTROY(q) SCSpinDestroy(&(q)->s)
73  #define HQLOCK_LOCK(q) SCSpinLock(&(q)->s)
74  #define HQLOCK_TRYLOCK(q) SCSpinTrylock(&(q)->s)
75  #define HQLOCK_UNLOCK(q) SCSpinUnlock(&(q)->s)
76 #elif defined HQLOCK_MUTEX
77  #define HQLOCK_INIT(q) SCMutexInit(&(q)->m, NULL)
78  #define HQLOCK_DESTROY(q) SCMutexDestroy(&(q)->m)
79  #define HQLOCK_LOCK(q) SCMutexLock(&(q)->m)
80  #define HQLOCK_TRYLOCK(q) SCMutexTrylock(&(q)->m)
81  #define HQLOCK_UNLOCK(q) SCMutexUnlock(&(q)->m)
82 #else
83  #error Enable HQLOCK_SPIN or HQLOCK_MUTEX
84 #endif
85 
86 typedef struct THashData_ {
87  /** ippair mutex */
89 
90  /** use cnt, reference counter */
91  SC_ATOMIC_DECLARE(unsigned int, use_cnt);
92 
93  void *data;
94 
95  struct THashData_ *next;
96  struct THashData_ *prev;
97 } THashData;
98 
99 typedef struct THashHashRow_ {
103 } __attribute__((aligned(CLS))) THashHashRow;
104 
105 typedef struct THashDataQueue_
106 {
109  uint32_t len;
110 #ifdef DBG_PERF
111  uint32_t dbg_maxlen;
112 #endif /* DBG_PERF */
113 #ifdef HQLOCK_MUTEX
115 #elif defined HQLOCK_SPIN
116  SCSpinlock s;
117 #else
118  #error Enable HQLOCK_SPIN or HQLOCK_MUTEX
119 #endif
121 
122 #define THASH_VERBOSE 0
123 #define THASH_QUIET 1
124 
125 typedef int (*THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint32_t data_len);
126 typedef int (*THashFormatFunc)(const void *in_data, char *output, size_t output_size);
127 
128 typedef struct THashDataConfig_ {
129  uint64_t memcap;
130  uint32_t hash_rand;
131  uint32_t hash_size;
132  uint32_t prealloc;
133 
134  uint32_t data_size;
135  int (*DataSet)(void *dst, void *src);
136  void (*DataFree)(void *);
137  uint32_t (*DataHash)(void *);
138  _Bool (*DataCompare)(void *, void *);
139 } THashConfig;
140 
141 #define THASH_DATA_SIZE(ctx) (sizeof(THashData) + (ctx)->config.data_size)
142 
143 typedef struct THashTableContext_ {
144  /* array of rows indexed by the hash value % hash size */
145  THashHashRow *array;
146 
147  SC_ATOMIC_DECLARE(uint64_t, memuse);
148  SC_ATOMIC_DECLARE(uint32_t, counter);
149  SC_ATOMIC_DECLARE(uint32_t, prune_idx);
150 
152 
154 
156 
157 /** \brief check if a memory alloc would fit in the memcap
158  *
159  * \param size memory allocation size to check
160  *
161  * \retval 1 it fits
162  * \retval 0 no fit
163  */
164 #define THASH_CHECK_MEMCAP(ctx, size) \
165  ((((uint64_t)SC_ATOMIC_GET((ctx)->memuse) + (uint64_t)(size)) <= (ctx)->config.memcap))
166 
167 #define THashIncrUsecnt(h) \
168  (void)SC_ATOMIC_ADD((h)->use_cnt, 1)
169 #define THashDecrUsecnt(h) \
170  (void)SC_ATOMIC_SUB((h)->use_cnt, 1)
171 
172 #define THashReference(dst_h_ptr, h) do { \
173  if ((h) != NULL) { \
174  THashIncrUsecnt((h)); \
175  *(dst_h_ptr) = h; \
176  } \
177  } while (0)
178 
179 #define THashDeReference(src_h_ptr) do { \
180  if (*(src_h_ptr) != NULL) { \
181  THashDecrUsecnt(*(src_h_ptr)); \
182  *(src_h_ptr) = NULL; \
183  } \
184  } while (0)
185 
186 THashTableContext* THashInit(const char *cnf_prefix, size_t data_size,
187  int (*DataSet)(void *dst, void *src),
188  void (*DataFree)(void *),
189  uint32_t (*DataHash)(void *),
190  _Bool (*DataCompare)(void *, void *));
191 
193 
194 static inline void THashDataLock(THashData *d)
195 {
196  SCMutexLock(&d->m);
197 }
198 
199 static inline void THashDataUnlock(THashData *d)
200 {
201  SCMutexUnlock(&d->m);
202 }
203 
206  bool is_new;
207 };
208 
214 
215 #endif /* __THASH_H__ */
#define SCMutex
struct THashTableContext_ THashTableContext
THashData * THashLookupFromHash(THashTableContext *ctx, void *data)
look up data in the hash
Definition: util-thash.c:620
uint32_t hash_size
Definition: util-thash.h:131
uint32_t data_size
Definition: util-thash.h:134
struct THashData_ * next
Definition: util-thash.h:95
uint16_t src
THashHashRow * array
Definition: util-thash.h:145
#define CLS
uint32_t prealloc
Definition: util-thash.h:132
THashData * data
Definition: util-thash.h:205
#define SCMutexLock(mut)
THashDataQueue * THashDataQueueNew(void)
Definition: util-thash.c:55
THashData * head
Definition: util-thash.h:101
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
THashData * tail
Definition: util-thash.h:102
uint16_t dst
#define SCMutexUnlock(mut)
struct THashDataGetResult THashGetFromHash(THashTableContext *ctx, void *data)
Definition: util-thash.c:512
struct THashData_ * prev
Definition: util-thash.h:96
#define HRLOCK_TYPE
Definition: util-thash.h:50
int(* THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint32_t data_len)
Definition: util-thash.h:125
struct THashDataQueue_ THashDataQueue
void THashCleanup(THashTableContext *ctx)
Cleanup the thash engine.
Definition: util-thash.c:402
THashData * bot
Definition: util-thash.h:108
void * data
Definition: util-thash.h:93
uint32_t len
Definition: util-thash.h:109
SCMutex m
Definition: util-thash.h:88
uint64_t memcap
Definition: util-thash.h:129
THashConfig config
Definition: util-thash.h:153
THashTableContext * THashInit(const char *cnf_prefix, size_t data_size, int(*DataSet)(void *dst, void *src), void(*DataFree)(void *), uint32_t(*DataHash)(void *), _Bool(*DataCompare)(void *, void *))
uint32_t hash_rand
Definition: util-thash.h:130
struct THashHashRow_ __attribute__((aligned(CLS))) THashHashRow
void THashShutdown(THashTableContext *ctx)
shutdown the flow engine
Definition: util-thash.c:327
struct THashData_ THashData
#define SCSpinlock
THashData * top
Definition: util-thash.h:107
int THashWalk(THashTableContext *, THashFormatFunc, THashOutputFunc, void *)
Walk the hash.
Definition: util-thash.c:367
HRLOCK_TYPE lock
Definition: util-thash.h:100
struct THashDataConfig_ THashConfig
THashDataQueue spare_q
Definition: util-thash.h:151
int(* THashFormatFunc)(const void *in_data, char *output, size_t output_size)
Definition: util-thash.h:126