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 __IPPAIR_H__
25 #define __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  /** storage api handle */
70 
71  /** hash pointers, protected by hash row mutex/spin */
72  struct IPPair_ *hnext;
73  struct IPPair_ *hprev;
74 
75  /** list pointers, protected by ippair-queue mutex/spin */
76  struct IPPair_ *lnext;
77  struct IPPair_ *lprev;
78 } IPPair;
79 
80 typedef struct IPPairHashRow_ {
84 } __attribute__((aligned(CLS))) IPPairHashRow;
85 
86 /** ippair hash table */
87 IPPairHashRow *ippair_hash;
88 
89 #define IPPAIR_VERBOSE 0
90 #define IPPAIR_QUIET 1
91 
92 typedef struct IPPairConfig_ {
93  SC_ATOMIC_DECLARE(uint64_t, memcap);
94  uint32_t hash_rand;
95  uint32_t hash_size;
96  uint32_t prealloc;
97 } IPPairConfig;
98 
99 /** \brief check if a memory alloc would fit in the memcap
100  *
101  * \param size memory allocation size to check
102  *
103  * \retval 1 it fits
104  * \retval 0 no fit
105  */
106 #define IPPAIR_CHECK_MEMCAP(size) \
107  ((((uint64_t)SC_ATOMIC_GET(ippair_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(ippair_config.memcap)))
108 
109 #define IPPairIncrUsecnt(h) \
110  (void)SC_ATOMIC_ADD((h)->use_cnt, 1)
111 #define IPPairDecrUsecnt(h) \
112  (void)SC_ATOMIC_SUB((h)->use_cnt, 1)
113 
114 #define IPPairReference(dst_h_ptr, h) do { \
115  if ((h) != NULL) { \
116  IPPairIncrUsecnt((h)); \
117  *(dst_h_ptr) = h; \
118  } \
119  } while (0)
120 
121 #define IPPairDeReference(src_h_ptr) do { \
122  if (*(src_h_ptr) != NULL) { \
123  IPPairDecrUsecnt(*(src_h_ptr)); \
124  *(src_h_ptr) = NULL; \
125  } \
126  } while (0)
127 
129 SC_ATOMIC_DECLARE(uint64_t,ippair_memuse);
130 SC_ATOMIC_DECLARE(uint32_t,ippair_counter);
131 SC_ATOMIC_DECLARE(uint32_t,ippair_prune_idx);
132 
133 void IPPairInitConfig(char quiet);
134 void IPPairShutdown(void);
135 void IPPairCleanup(void);
136 
139 void IPPairRelease(IPPair *);
140 void IPPairLock(IPPair *);
141 void IPPairClearMemory(IPPair *);
142 void IPPairMoveToSpare(IPPair *);
143 uint32_t IPPairSpareQueueGetSize(void);
144 void IPPairPrintStats (void);
145 
146 void IPPairRegisterUnittests(void);
147 
148 IPPair *IPPairAlloc(void);
149 void IPPairFree(IPPair *);
150 
151 void IPPairLock(IPPair *);
152 void IPPairUnlock(IPPair *);
153 
154 int IPPairSetMemcap(uint64_t size);
155 uint64_t IPPairGetMemcap(void);
156 uint64_t IPPairGetMemuse(void);
157 
158 #endif /* __IPPAIR_H__ */
uint32_t hash_rand
Definition: ippair.h:94
void IPPairRegisterUnittests(void)
Definition: ippair.c:775
void IPPairClearMemory(IPPair *)
Definition: ippair.c:152
struct IPPair_ * hnext
Definition: ippair.h:72
void IPPairUnlock(IPPair *)
Definition: ippair.c:531
uint32_t IPPairSpareQueueGetSize(void)
Definition: ippair.c:91
struct IPPair_ * lprev
Definition: ippair.h:77
#define CLS
void IPPairCleanup(void)
Cleanup the ippair engine.
Definition: ippair.c:340
uint64_t IPPairGetMemcap(void)
Return memcap value.
Definition: ippair.c:74
IPPairConfig ippair_config
Definition: ippair.h:128
SCMutex m
Definition: ippair.h:60
uint32_t prealloc
Definition: ippair.h:96
struct IPPair_ IPPair
uint32_t hash_size
Definition: ippair.h:95
Storage * storage
Definition: ippair.h:69
int IPPairSetMemcap(uint64_t size)
Update memcap value.
Definition: ippair.c:59
Definition: ippair.h:58
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:296
IPPair * head
Definition: ippair.h:82
Address a[2]
Definition: ippair.h:63
void * Storage
Definition: util-storage.h:39
uint64_t IPPairGetMemuse(void)
Return memuse value.
Definition: ippair.c:85
HRLOCK_TYPE lock
Definition: ippair.h:81
IPPair * IPPairLookupIPPairFromHash(Address *, Address *)
look up a ippair in the hash
Definition: ippair.c:643
void IPPairMoveToSpare(IPPair *)
Definition: ippair.c:96
IPPair * tail
Definition: ippair.h:83
void IPPairInitConfig(char quiet)
initialize the configuration
Definition: ippair.c:164
#define SCMutex
IPPair * IPPairGetIPPairFromHash(Address *, Address *)
Definition: ippair.c:544
struct IPPair_ * lnext
Definition: ippair.h:76
void IPPairFree(IPPair *)
Definition: ippair.c:124
struct IPPairHashRow_ __attribute__((aligned(CLS))) IPPairHashRow
void IPPairRelease(IPPair *)
Definition: ippair.c:520
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
struct IPPairConfig_ IPPairConfig
void IPPairPrintStats(void)
print some ippair stats
Definition: ippair.c:283
struct IPPair_ * hprev
Definition: ippair.h:73
#define HRLOCK_TYPE
Definition: ippair.h:48
IPPair * IPPairAlloc(void)
Definition: ippair.c:102
void IPPairLock(IPPair *)
Definition: ippair.c:526
IPPairHashRow * ippair_hash
Definition: ippair.h:87