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