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_ {
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 
100 int DefragTrackerSetMemcap(uint64_t);
101 uint64_t DefragTrackerGetMemcap(void);
102 uint64_t DefragTrackerGetMemuse(void);
104 
105 #endif /* SURICATA_DEFRAG_HASH_H */
DefragLookupTrackerFromHash
DefragTracker * DefragLookupTrackerFromHash(Packet *)
look up a tracker in the hash
Definition: defrag-hash.c:624
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:296
DefragTrackerRelease
void DefragTrackerRelease(DefragTracker *)
Definition: defrag-hash.c:152
DefragTrackerMoveToSpare
void DefragTrackerMoveToSpare(DefragTracker *)
Definition: defrag-hash.c:85
DefragTrackerHashRow
struct DefragTrackerHashRow_ DefragTrackerHashRow
util-exception-policy-types.h
defrag_config
DefragConfig defrag_config
Definition: defrag-hash.c:32
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:62
decode.h
DefragTrackerGetMemuse
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition: defrag-hash.c:74
util-exception-policy.h
DefragTracker_
Definition: defrag.h:84
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DefragInitConfig
void DefragInitConfig(bool quiet)
initialize the configuration
Definition: defrag-hash.c:169
Packet_
Definition: decode.h:476
DefragGetMemcapExceptionPolicy
enum ExceptionPolicy DefragGetMemcapExceptionPolicy(void)
Definition: defrag-hash.c:80
DefragTrackerGetMemcap
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
Definition: defrag-hash.c:63
DefragTrackerSetMemcap
int DefragTrackerSetMemcap(uint64_t)
Update memcap value.
Definition: defrag-hash.c:48
defrag.h
DefragConfig_::memcap_policy
enum ExceptionPolicy memcap_policy
Definition: defrag-hash.h:73
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
DefragTrackerClearMemory
void DefragTrackerClearMemory(DefragTracker *)
Definition: defrag-hash.c:158
defragtracker_hash
DefragTrackerHashRow * defragtracker_hash
Definition: defrag-hash.c:31
DefragTrackerHashRow_::lock
DRLOCK_TYPE lock
Definition: defrag-hash.h:61
DefragConfig_
Definition: defrag-hash.h:68
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
DefragConfig_::hash_rand
uint32_t hash_rand
Definition: defrag-hash.h:70
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint64_t, defrag_memuse)
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:932
DefragGetTrackerFromHash
DefragTracker * DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, Packet *)
Definition: defrag-hash.c:529
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy-types.h:25
DefragConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)