suricata
ippair.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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_IPPAIR_H
25 #define SURICATA_IPPAIR_H
26 
27 #include "decode.h"
28 #include "util-storage.h"
29 
30 /** Spinlocks or Mutex for the flow buckets. */
31 //#define HRLOCK_SPIN
32 #define HRLOCK_MUTEX
33 
34 #ifdef HRLOCK_SPIN
35  #ifdef HRLOCK_MUTEX
36  #error Cannot enable both HRLOCK_SPIN and HRLOCK_MUTEX
37  #endif
38 #endif
39 
40 #ifdef HRLOCK_SPIN
41  #define HRLOCK_TYPE SCSpinlock
42  #define HRLOCK_INIT(fb) SCSpinInit(&(fb)->lock, 0)
43  #define HRLOCK_DESTROY(fb) SCSpinDestroy(&(fb)->lock)
44  #define HRLOCK_LOCK(fb) SCSpinLock(&(fb)->lock)
45  #define HRLOCK_TRYLOCK(fb) SCSpinTrylock(&(fb)->lock)
46  #define HRLOCK_UNLOCK(fb) SCSpinUnlock(&(fb)->lock)
47 #elif defined HRLOCK_MUTEX
48  #define HRLOCK_TYPE SCMutex
49  #define HRLOCK_INIT(fb) SCMutexInit(&(fb)->lock, NULL)
50  #define HRLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->lock)
51  #define HRLOCK_LOCK(fb) SCMutexLock(&(fb)->lock)
52  #define HRLOCK_TRYLOCK(fb) SCMutexTrylock(&(fb)->lock)
53  #define HRLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->lock)
54 #else
55  #error Enable HRLOCK_SPIN or HRLOCK_MUTEX
56 #endif
57 
58 typedef struct IPPair_ {
59  /** ippair mutex */
61 
62  /** ippair addresses -- ipv4 or ipv6 */
63  Address a[2];
64 
65  /** use cnt, reference counter */
66  SC_ATOMIC_DECLARE(unsigned int, use_cnt);
67 
68  /** hash pointers, protected by hash row mutex/spin */
69  struct IPPair_ *hnext;
70  struct IPPair_ *hprev;
71 
72  /** list pointers, protected by ippair-queue mutex/spin */
73  struct IPPair_ *lnext;
74  struct IPPair_ *lprev;
75 
76  /** storage api handle as a flex array member, so must stay last */
79 
80 typedef struct IPPairHashRow_ {
84 } __attribute__((aligned(CLS))) IPPairHashRow;
85 
86 /** ippair hash table */
87 extern IPPairHashRow *ippair_hash;
88 
89 #define IPPAIR_QUIET 1
90 
91 typedef struct IPPairConfig_ {
92  SC_ATOMIC_DECLARE(uint64_t, memcap);
93  uint32_t hash_rand;
94  uint32_t hash_size;
95  uint32_t prealloc;
97 
98 /** \brief check if a memory alloc would fit in the memcap
99  *
100  * \param size memory allocation size to check
101  *
102  * \retval 1 it fits
103  * \retval 0 no fit
104  */
105 #define IPPAIR_CHECK_MEMCAP(size) \
106  ((((uint64_t)SC_ATOMIC_GET(ippair_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(ippair_config.memcap)))
107 
108 #define IPPairIncrUsecnt(h) \
109  (void)SC_ATOMIC_ADD((h)->use_cnt, 1)
110 #define IPPairDecrUsecnt(h) \
111  (void)SC_ATOMIC_SUB((h)->use_cnt, 1)
112 
114 SC_ATOMIC_EXTERN(uint64_t,ippair_memuse);
115 SC_ATOMIC_EXTERN(uint32_t,ippair_counter);
116 SC_ATOMIC_EXTERN(uint32_t,ippair_prune_idx);
117 
118 void IPPairInitConfig(bool quiet);
119 void IPPairShutdown(void);
120 void IPPairCleanup(void);
121 
124 void IPPairRelease(IPPair *);
125 void IPPairClearMemory(IPPair *);
126 void IPPairMoveToSpare(IPPair *);
127 void IPPairPrintStats (void);
128 
129 void IPPairRegisterUnittests(void);
130 
131 IPPair *IPPairAlloc(void);
132 void IPPairFree(IPPair *);
133 
134 void IPPairUnlock(IPPair *);
135 
136 int IPPairSetMemcap(uint64_t size);
137 uint64_t IPPairGetMemcap(void);
138 uint64_t IPPairGetMemuse(void);
139 
140 #endif /* SURICATA_IPPAIR_H */
IPPair_::a
Address a[2]
Definition: ippair.h:63
IPPairSetMemcap
int IPPairSetMemcap(uint64_t size)
Update memcap value.
Definition: ippair.c:66
CLS
#define CLS
Definition: suricata-common.h:56
IPPairMoveToSpare
void IPPairMoveToSpare(IPPair *)
Definition: ippair.c:98
__attribute__
struct IPPairHashRow_ __attribute__((aligned(CLS))) IPPairHashRow
IPPairGetMemuse
uint64_t IPPairGetMemuse(void)
Return memuse value.
Definition: ippair.c:92
IPPairHashRow_
Definition: ippair.h:80
ippair_hash
IPPairHashRow * ippair_hash
Definition: ippair.c:49
IPPair_::lprev
struct IPPair_ * lprev
Definition: ippair.h:74
ippair_config
IPPairConfig ippair_config
Definition: ippair.c:52
IPPairClearMemory
void IPPairClearMemory(IPPair *)
Definition: ippair.c:150
Address_
Definition: decode.h:108
IPPair_::lnext
struct IPPair_ * lnext
Definition: ippair.h:73
IPPair_::m
SCMutex m
Definition: ippair.h:60
Storage
Definition: util-storage.h:40
IPPairConfig
struct IPPairConfig_ IPPairConfig
IPPairConfig_::prealloc
uint32_t prealloc
Definition: ippair.h:95
IPPairPrintStats
void IPPairPrintStats(void)
print some ippair stats
Definition: ippair.c:281
IPPairFree
void IPPairFree(IPPair *)
Definition: ippair.c:124
IPPairRegisterUnittests
void IPPairRegisterUnittests(void)
Definition: ippair.c:755
decode.h
IPPairUnlock
void IPPairUnlock(IPPair *)
Definition: ippair.c:511
IPPairConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)
IPPair_::storage
Storage storage[]
Definition: ippair.h:77
IPPairHashRow_::tail
IPPair * tail
Definition: ippair.h:83
IPPairHashRow_::head
IPPair * head
Definition: ippair.h:82
IPPairAlloc
IPPair * IPPairAlloc(void)
Definition: ippair.c:104
HRLOCK_TYPE
#define HRLOCK_TYPE
Definition: ippair.h:48
IPPairRelease
void IPPairRelease(IPPair *)
Definition: ippair.c:505
IPPairGetMemcap
uint64_t IPPairGetMemcap(void)
Return memcap value.
Definition: ippair.c:81
IPPair_
Definition: ippair.h:58
IPPairConfig_::hash_size
uint32_t hash_size
Definition: ippair.h:94
IPPair_::hprev
struct IPPair_ * hprev
Definition: ippair.h:70
IPPairConfig_
Definition: ippair.h:91
IPPairHashRow_::lock
HRLOCK_TYPE lock
Definition: ippair.h:81
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint64_t, ippair_memuse)
IPPair_::hnext
struct IPPair_ * hnext
Definition: ippair.h:69
IPPairCleanup
void IPPairCleanup(void)
Cleanup the ippair engine.
Definition: ippair.c:330
IPPairInitConfig
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition: ippair.c:162
IPPair_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
IPPair
struct IPPair_ IPPair
IPPairLookupIPPairFromHash
IPPair * IPPairLookupIPPairFromHash(Address *, Address *)
look up a ippair in the hash
Definition: ippair.c:623
IPPairConfig_::hash_rand
uint32_t hash_rand
Definition: ippair.h:93
IPPairGetIPPairFromHash
IPPair * IPPairGetIPPairFromHash(Address *, Address *)
Definition: ippair.c:524
IPPairShutdown
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:293
SCMutex
#define SCMutex
Definition: threads-debug.h:114
util-storage.h