suricata
defrag-hash.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2024 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 SURICATA_DEFRAG_HASH_H
25 #define SURICATA_DEFRAG_HASH_H
26 
27 #include "decode.h"
28 #include "defrag.h"
29 #include "util-exception-policy.h"
31 
32 /** Spinlocks or Mutex for the flow buckets. */
33 //#define DRLOCK_SPIN
34 #define DRLOCK_MUTEX
35 
36 #ifdef DRLOCK_SPIN
37  #ifdef DRLOCK_MUTEX
38  #error Cannot enable both DRLOCK_SPIN and DRLOCK_MUTEX
39  #endif
40 #endif
41 
42 #ifdef DRLOCK_SPIN
43  #define DRLOCK_TYPE SCSpinlock
44  #define DRLOCK_INIT(fb) SCSpinInit(&(fb)->lock, 0)
45  #define DRLOCK_DESTROY(fb) SCSpinDestroy(&(fb)->lock)
46  #define DRLOCK_LOCK(fb) SCSpinLock(&(fb)->lock)
47  #define DRLOCK_TRYLOCK(fb) SCSpinTrylock(&(fb)->lock)
48  #define DRLOCK_UNLOCK(fb) SCSpinUnlock(&(fb)->lock)
49 #elif defined DRLOCK_MUTEX
50  #define DRLOCK_TYPE SCMutex
51  #define DRLOCK_INIT(fb) SCMutexInit(&(fb)->lock, NULL)
52  #define DRLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->lock)
53  #define DRLOCK_LOCK(fb) SCMutexLock(&(fb)->lock)
54  #define DRLOCK_TRYLOCK(fb) SCMutexTrylock(&(fb)->lock)
55  #define DRLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->lock)
56 #else
57  #error Enable DRLOCK_SPIN or DRLOCK_MUTEX
58 #endif
59 
60 typedef struct DefragTrackerHashRow_ {
65 
66 /** defrag tracker hash table */
68 
69 typedef struct DefragConfig_ {
70  SC_ATOMIC_DECLARE(uint64_t, memcap);
71  uint32_t hash_rand;
72  uint32_t hash_size;
73  uint32_t prealloc;
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_EXTERN(uint64_t,defrag_memuse);
89 SC_ATOMIC_EXTERN(unsigned int,defragtracker_counter);
90 SC_ATOMIC_EXTERN(unsigned int,defragtracker_prune_idx);
91 
92 void DefragInitConfig(bool quiet);
93 void DefragHashShutdown(void);
94 
100 
101 int DefragTrackerSetMemcap(uint64_t);
102 uint64_t DefragTrackerGetMemcap(void);
103 uint64_t DefragTrackerGetMemuse(void);
105 
106 #endif /* SURICATA_DEFRAG_HASH_H */
DefragLookupTrackerFromHash
DefragTracker * DefragLookupTrackerFromHash(Packet *)
look up a tracker in the hash
Definition: defrag-hash.c:644
DRLOCK_TYPE
#define DRLOCK_TYPE
Definition: defrag-hash.h:50
DefragConfig
struct DefragConfig_ DefragConfig
DefragTrackerHashRow_
Definition: defrag-hash.h:60
DefragHashShutdown
void DefragHashShutdown(void)
shutdown the flow engine
Definition: defrag-hash.c:300
DefragTrackerRelease
void DefragTrackerRelease(DefragTracker *)
Definition: defrag-hash.c:147
DefragTrackerMoveToSpare
void DefragTrackerMoveToSpare(DefragTracker *)
Definition: defrag-hash.c:82
DefragTrackerHashRow
struct DefragTrackerHashRow_ DefragTrackerHashRow
util-exception-policy-types.h
defrag_config
DefragConfig defrag_config
Definition: defrag-hash.c:30
DefragConfig_::hash_size
uint32_t hash_size
Definition: defrag-hash.h:72
DefragConfig_::prealloc
uint32_t prealloc
Definition: defrag-hash.h:73
DefragTrackerHashRow_::head
DefragTracker * head
Definition: defrag-hash.h:62
decode.h
DefragTrackerGetMemuse
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition: defrag-hash.c:71
util-exception-policy.h
DefragTracker_
Definition: defrag.h:86
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DefragTrackerHashRow_::tail
DefragTracker * tail
Definition: defrag-hash.h:63
DefragInitConfig
void DefragInitConfig(bool quiet)
initialize the configuration
Definition: defrag-hash.c:164
Packet_
Definition: decode.h:437
DefragGetMemcapExceptionPolicy
enum ExceptionPolicy DefragGetMemcapExceptionPolicy(void)
Definition: defrag-hash.c:77
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:74
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
DefragTrackerClearMemory
void DefragTrackerClearMemory(DefragTracker *)
Definition: defrag-hash.c:153
defragtracker_hash
DefragTrackerHashRow * defragtracker_hash
Definition: defrag-hash.c:29
DefragTrackerHashRow_::lock
DRLOCK_TYPE lock
Definition: defrag-hash.h:61
DefragConfig_
Definition: defrag-hash.h:69
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
DefragConfig_::hash_rand
uint32_t hash_rand
Definition: defrag-hash.h:71
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint64_t, defrag_memuse)
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:685
DefragGetTrackerFromHash
DefragTracker * DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, Packet *)
Definition: defrag-hash.c:545
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy-types.h:25
DefragConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)