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"
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
64
65/** defrag tracker hash table */
67
68typedef 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
87SC_ATOMIC_EXTERN(uint64_t,defrag_memuse);
88SC_ATOMIC_EXTERN(unsigned int,defragtracker_counter);
89SC_ATOMIC_EXTERN(unsigned int,defragtracker_prune_idx);
90
91void DefragInitConfig(bool quiet);
92void DefragHashShutdown(void);
93
99
100int DefragTrackerSetMemcap(uint64_t);
101uint64_t DefragTrackerGetMemcap(void);
102uint64_t DefragTrackerGetMemuse(void);
104
105#endif /* SURICATA_DEFRAG_HASH_H */
DefragTracker * DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, Packet *)
DefragConfig defrag_config
Definition defrag-hash.c:32
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
Definition defrag-hash.c:63
#define DRLOCK_TYPE
Definition defrag-hash.h:50
void DefragTrackerClearMemory(DefragTracker *)
struct DefragTrackerHashRow_ DefragTrackerHashRow
void DefragTrackerRelease(DefragTracker *)
DefragTracker * DefragLookupTrackerFromHash(Packet *)
look up a tracker in the hash
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition defrag-hash.c:74
void DefragTrackerMoveToSpare(DefragTracker *)
Definition defrag-hash.c:85
enum ExceptionPolicy DefragGetMemcapExceptionPolicy(void)
Definition defrag-hash.c:80
struct DefragConfig_ DefragConfig
void DefragInitConfig(bool quiet)
initialize the configuration
DefragTrackerHashRow * defragtracker_hash
Definition defrag-hash.c:31
void DefragHashShutdown(void)
shutdown the flow engine
int DefragTrackerSetMemcap(uint64_t)
Update memcap value.
Definition defrag-hash.c:48
DecodeThreadVars * dtv
ThreadVars * tv
Structure to hold thread specific data for all decode modules.
Definition decode.h:963
uint32_t prealloc
Definition defrag-hash.h:72
uint32_t hash_size
Definition defrag-hash.h:71
enum ExceptionPolicy memcap_policy
Definition defrag-hash.h:73
uint32_t hash_rand
Definition defrag-hash.h:70
SC_ATOMIC_DECLARE(uint64_t, memcap)
DefragTracker * head
Definition defrag-hash.h:62
Per thread variable structure.
Definition threadvars.h:58
#define SC_ATOMIC_EXTERN(type, name)
wrapper for referencing an atomic variable declared on another file.