suricata
defrag-hash.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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 __DEFRAG_HASH_H__
25 #define __DEFRAG_HASH_H__
26 
27 #include "decode.h"
28 #include "defrag.h"
29 
30 /** Spinlocks or Mutex for the flow buckets. */
31 //#define DRLOCK_SPIN
32 #define DRLOCK_MUTEX
33 
34 #ifdef DRLOCK_SPIN
35  #ifdef DRLOCK_MUTEX
36  #error Cannot enable both DRLOCK_SPIN and DRLOCK_MUTEX
37  #endif
38 #endif
39 
40 #ifdef DRLOCK_SPIN
41  #define DRLOCK_TYPE SCSpinlock
42  #define DRLOCK_INIT(fb) SCSpinInit(&(fb)->lock, 0)
43  #define DRLOCK_DESTROY(fb) SCSpinDestroy(&(fb)->lock)
44  #define DRLOCK_LOCK(fb) SCSpinLock(&(fb)->lock)
45  #define DRLOCK_TRYLOCK(fb) SCSpinTrylock(&(fb)->lock)
46  #define DRLOCK_UNLOCK(fb) SCSpinUnlock(&(fb)->lock)
47 #elif defined DRLOCK_MUTEX
48  #define DRLOCK_TYPE SCMutex
49  #define DRLOCK_INIT(fb) SCMutexInit(&(fb)->lock, NULL)
50  #define DRLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->lock)
51  #define DRLOCK_LOCK(fb) SCMutexLock(&(fb)->lock)
52  #define DRLOCK_TRYLOCK(fb) SCMutexTrylock(&(fb)->lock)
53  #define DRLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->lock)
54 #else
55  #error Enable DRLOCK_SPIN or DRLOCK_MUTEX
56 #endif
57 
58 typedef struct DefragTrackerHashRow_ {
63 
64 /** defrag tracker hash table */
66 
67 #define DEFRAG_VERBOSE 0
68 #define DEFRAG_QUIET 1
69 
70 typedef struct DefragConfig_ {
71  SC_ATOMIC_DECLARE(uint64_t, memcap);
72  uint32_t hash_rand;
73  uint32_t hash_size;
74  uint32_t prealloc;
75 } DefragConfig;
76 
77 /** \brief check if a memory alloc would fit in the memcap
78  *
79  * \param size memory allocation size to check
80  *
81  * \retval 1 it fits
82  * \retval 0 no fit
83  */
84 #define DEFRAG_CHECK_MEMCAP(size) \
85  ((((uint64_t)SC_ATOMIC_GET(defrag_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(defrag_config.memcap)))
86 
88 SC_ATOMIC_DECLARE(uint64_t,defrag_memuse);
89 SC_ATOMIC_DECLARE(unsigned int,defragtracker_counter);
90 SC_ATOMIC_DECLARE(unsigned int,defragtracker_prune_idx);
91 
92 void DefragInitConfig(char quiet);
93 void DefragHashShutdown(void);
94 
100 uint32_t DefragTrackerSpareQueueGetSize(void);
101 
102 int DefragTrackerSetMemcap(uint64_t);
103 uint64_t DefragTrackerGetMemcap(void);
104 uint64_t DefragTrackerGetMemuse(void);
105 
106 #endif /* __DEFRAG_HASH_H__ */
107 
uint32_t hash_size
Definition: defrag-hash.h:73
void DefragTrackerRelease(DefragTracker *)
Definition: defrag-hash.c:143
void DefragHashShutdown(void)
shutdown the flow engine
Definition: defrag-hash.c:294
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
Definition: defrag-hash.c:53
SC_ATOMIC_DECLARE(uint64_t, defrag_memuse)
DefragTracker * tail
Definition: defrag-hash.h:61
DefragTracker * DefragLookupTrackerFromHash(Packet *)
look up a tracker in the hash
Definition: defrag-hash.c:625
uint32_t prealloc
Definition: defrag-hash.h:74
DefragConfig defrag_config
Definition: defrag-hash.h:87
DefragTrackerHashRow * defragtracker_hash
Definition: defrag-hash.h:65
struct DefragConfig_ DefragConfig
DefragTracker * head
Definition: defrag-hash.h:60
int DefragTrackerSetMemcap(uint64_t)
Update memcap value.
Definition: defrag-hash.c:38
uint32_t DefragTrackerSpareQueueGetSize(void)
Definition: defrag-hash.c:70
#define DRLOCK_TYPE
Definition: defrag-hash.h:48
void DefragTrackerMoveToSpare(DefragTracker *)
Definition: defrag-hash.c:75
struct DefragTrackerHashRow_ DefragTrackerHashRow
DefragTracker * DefragGetTrackerFromHash(Packet *)
Definition: defrag-hash.c:526
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition: defrag-hash.c:64
void DefragTrackerClearMemory(DefragTracker *)
Definition: defrag-hash.c:149
uint32_t hash_rand
Definition: defrag-hash.h:72
void DefragInitConfig(char quiet)
initialize the configuration
Definition: defrag-hash.c:161