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 
30 /** Spinlocks or Mutex for the buckets. */
31 //#define HRLOCK_SPIN
32 #define HRLOCK_MUTEX
33 
34 #ifdef HRLOCK_SPIN
35  #ifdef HRLOCK_MUTEX
36  #error Cannot enable both HRLOCK_SPIN and HRLOCK_MUTEX
37  #endif
38 #endif
39 
40 #ifdef HRLOCK_SPIN
41  #define HRLOCK_TYPE SCSpinlock
42  #define HRLOCK_INIT(fb) SCSpinInit(&(fb)->lock, 0)
43  #define HRLOCK_DESTROY(fb) SCSpinDestroy(&(fb)->lock)
44  #define HRLOCK_LOCK(fb) SCSpinLock(&(fb)->lock)
45  #define HRLOCK_TRYLOCK(fb) SCSpinTrylock(&(fb)->lock)
46  #define HRLOCK_UNLOCK(fb) SCSpinUnlock(&(fb)->lock)
47 #elif defined HRLOCK_MUTEX
48  #define HRLOCK_TYPE SCMutex
49  #define HRLOCK_INIT(fb) SCMutexInit(&(fb)->lock, NULL)
50  #define HRLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->lock)
51  #define HRLOCK_LOCK(fb) SCMutexLock(&(fb)->lock)
52  #define HRLOCK_TRYLOCK(fb) SCMutexTrylock(&(fb)->lock)
53  #define HRLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->lock)
54 #else
55  #error Enable HRLOCK_SPIN or HRLOCK_MUTEX
56 #endif
57 
58 /** Spinlocks or Mutex for the queues. */
59 //#define HQLOCK_SPIN
60 #define HQLOCK_MUTEX
61 
62 #ifdef HQLOCK_SPIN
63  #ifdef HQLOCK_MUTEX
64  #error Cannot enable both HQLOCK_SPIN and HQLOCK_MUTEX
65  #endif
66 #endif
67 
68 #ifdef HQLOCK_SPIN
69  #define HQLOCK_INIT(q) SCSpinInit(&(q)->s, 0)
70  #define HQLOCK_DESTROY(q) SCSpinDestroy(&(q)->s)
71  #define HQLOCK_LOCK(q) SCSpinLock(&(q)->s)
72  #define HQLOCK_TRYLOCK(q) SCSpinTrylock(&(q)->s)
73  #define HQLOCK_UNLOCK(q) SCSpinUnlock(&(q)->s)
74 #elif defined HQLOCK_MUTEX
75  #define HQLOCK_INIT(q) SCMutexInit(&(q)->m, NULL)
76  #define HQLOCK_DESTROY(q) SCMutexDestroy(&(q)->m)
77  #define HQLOCK_LOCK(q) SCMutexLock(&(q)->m)
78  #define HQLOCK_TRYLOCK(q) SCMutexTrylock(&(q)->m)
79  #define HQLOCK_UNLOCK(q) SCMutexUnlock(&(q)->m)
80 #else
81  #error Enable HQLOCK_SPIN or HQLOCK_MUTEX
82 #endif
83 
84 typedef struct THashData_ {
85  /** ippair mutex */
87 
88  /** use cnt, reference counter */
89  SC_ATOMIC_DECLARE(unsigned int, use_cnt);
90 
91  void *data;
92 
93  struct THashData_ *next;
94  struct THashData_ *prev;
96 
97 typedef struct THashHashRow_ {
101 } __attribute__((aligned(CLS))) THashHashRow;
102 
103 typedef struct THashDataQueue_
104 {
107  uint32_t len;
108 #ifdef DBG_PERF
109  uint32_t dbg_maxlen;
110 #endif /* DBG_PERF */
111 #ifdef HQLOCK_MUTEX
113 #elif defined HQLOCK_SPIN
114  SCSpinlock s;
115 #else
116  #error Enable HQLOCK_SPIN or HQLOCK_MUTEX
117 #endif
119 
120 #define THASH_VERBOSE 0
121 #define THASH_QUIET 1
122 
123 typedef int (*THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint32_t data_len);
124 typedef int (*THashFormatFunc)(const void *in_data, char *output, size_t output_size);
125 
126 typedef struct THashDataConfig_ {
127  uint64_t memcap;
128  uint32_t hash_rand;
129  uint32_t hash_size;
130  uint32_t prealloc;
131 
132  uint32_t data_size;
133  int (*DataSet)(void *dst, void *src);
134  void (*DataFree)(void *);
135  uint32_t (*DataHash)(void *);
136  bool (*DataCompare)(void *, void *);
138 
139 #define THASH_DATA_SIZE(ctx) (sizeof(THashData) + (ctx)->config.data_size)
140 
141 typedef struct THashTableContext_ {
142  /* array of rows indexed by the hash value % hash size */
143  THashHashRow *array;
144 
145  SC_ATOMIC_DECLARE(uint64_t, memuse);
146  SC_ATOMIC_DECLARE(uint32_t, counter);
147  SC_ATOMIC_DECLARE(uint32_t, prune_idx);
148 
150 
152 
153  /* flag set if memcap was reached at least once. */
154  SC_ATOMIC_DECLARE(bool, memcap_reached);
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), void (*DataFree)(void *),
188  uint32_t (*DataHash)(void *), bool (*DataCompare)(void *, void *), bool reset_memcap,
189  uint64_t memcap, uint32_t hashsize);
190 
192 
193 static inline void THashDataLock(THashData *d)
194 {
195  SCMutexLock(&d->m);
196 }
197 
198 static inline void THashDataUnlock(THashData *d)
199 {
200  SCMutexUnlock(&d->m);
201 }
202 
205  bool is_new;
206 };
207 
213 int THashRemoveFromHash (THashTableContext *ctx, void *data);
216 
217 #endif /* __THASH_H__ */
THashTableContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint32_t, counter)
THashDataQueueNew
THashDataQueue * THashDataQueueNew(void)
Definition: util-thash.c:56
THashData
struct THashData_ THashData
THashDataGetResult::data
THashData * data
Definition: util-thash.h:204
CLS
#define CLS
Definition: suricata-common.h:45
THashInit
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 *), bool reset_memcap, uint64_t memcap, uint32_t hashsize)
THashHashRow_::tail
THashData * tail
Definition: util-thash.h:100
THashOutputFunc
int(* THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint32_t data_len)
Definition: util-thash.h:123
THashShutdown
void THashShutdown(THashTableContext *ctx)
shutdown the flow engine
Definition: util-thash.c:346
THashData_::prev
struct THashData_ * prev
Definition: util-thash.h:94
THashDataConfig_::DataFree
void(* DataFree)(void *)
Definition: util-thash.h:134
THashHashRow_::lock
HRLOCK_TYPE lock
Definition: util-thash.h:98
THashConsolidateMemcap
void THashConsolidateMemcap(THashTableContext *ctx)
Definition: util-thash.c:338
THashHashRow_
Definition: util-thash.h:97
THashData_::next
struct THashData_ * next
Definition: util-thash.h:93
THashWalk
int THashWalk(THashTableContext *, THashFormatFunc, THashOutputFunc, void *)
Walk the hash.
Definition: util-thash.c:380
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
THashDataQueue_
Definition: util-thash.h:104
THashDataConfig_::DataHash
uint32_t(* DataHash)(void *)
Definition: util-thash.h:135
THashDataConfig_::DataCompare
bool(* DataCompare)(void *, void *)
Definition: util-thash.h:136
THashDataQueue
struct THashDataQueue_ THashDataQueue
hashsize
#define hashsize(n)
Definition: util-hash-lookup3.c:67
THashDataConfig_::DataSet
int(* DataSet)(void *dst, void *src)
Definition: util-thash.h:133
__attribute__
struct THashHashRow_ __attribute__((aligned(CLS))) THashHashRow
THashDataConfig_::prealloc
uint32_t prealloc
Definition: util-thash.h:130
THashDataConfig_::hash_size
uint32_t hash_size
Definition: util-thash.h:129
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
THashTableContext_
Definition: util-thash.h:141
THashRemoveFromHash
int THashRemoveFromHash(THashTableContext *ctx, void *data)
Definition: util-thash.c:774
THashDataQueue_::bot
THashData * bot
Definition: util-thash.h:106
THashData_::m
SCMutex m
Definition: util-thash.h:86
THashTableContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint32_t, prune_idx)
THashData_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
THashTableContext_::array
THashHashRow * array
Definition: util-thash.h:143
THashConfig
struct THashDataConfig_ THashConfig
THashDataGetResult
Definition: util-thash.h:203
THashDataConfig_::memcap
uint64_t memcap
Definition: util-thash.h:127
THashGetFromHash
struct THashDataGetResult THashGetFromHash(THashTableContext *ctx, void *data)
Definition: util-thash.c:529
THashTableContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(bool, memcap_reached)
THashTableContext_::spare_q
THashDataQueue spare_q
Definition: util-thash.h:149
THashHashRow_::head
THashData * head
Definition: util-thash.h:99
THashDataMoveToSpare
void THashDataMoveToSpare(THashTableContext *ctx, THashData *h)
Definition: util-thash.c:41
THashData_::data
void * data
Definition: util-thash.h:91
THashData_
Definition: util-thash.h:84
THashDataQueue_::len
uint32_t len
Definition: util-thash.h:107
THashLookupFromHash
THashData * THashLookupFromHash(THashTableContext *ctx, void *data)
look up data in the hash
Definition: util-thash.c:639
THashTableContext
struct THashTableContext_ THashTableContext
THashDataConfig_
Definition: util-thash.h:126
src
uint16_t src
Definition: app-layer-dnp3.h:5
THashFormatFunc
int(* THashFormatFunc)(const void *in_data, char *output, size_t output_size)
Definition: util-thash.h:124
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:234
THashDataGetResult::is_new
bool is_new
Definition: util-thash.h:205
THashDataConfig_::data_size
uint32_t data_size
Definition: util-thash.h:132
THashDataConfig_::hash_rand
uint32_t hash_rand
Definition: util-thash.h:128
THashCleanup
void THashCleanup(THashTableContext *ctx)
Cleanup the thash engine.
Definition: util-thash.c:415
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
THashTableContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memuse)
HRLOCK_TYPE
#define HRLOCK_TYPE
Definition: util-thash.h:48
SCMutex
#define SCMutex
Definition: threads-debug.h:114
THashDataQueue_::m
SCMutex m
Definition: util-thash.h:112
THashDataQueue_::top
THashData * top
Definition: util-thash.h:105
THashTableContext_::config
THashConfig config
Definition: util-thash.h:151