suricata
host.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_HOST_H
25 #define SURICATA_HOST_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 Host_ {
59  /** host mutex */
61 
62  /** host address -- ipv4 or ipv6 */
64 
65  /** use cnt, reference counter */
66  SC_ATOMIC_DECLARE(unsigned int, use_cnt);
67 
68  /** pointers to iprep storage */
69  void *iprep;
70 
71  /** hash pointers, protected by hash row mutex/spin */
72  struct Host_ *hnext;
73  struct Host_ *hprev;
74 
75  /** list pointers, protected by host-queue mutex/spin */
76  struct Host_ *lnext;
77  struct Host_ *lprev;
78 
79  /** storage api handle */
81 } Host;
82 
83 typedef struct HostHashRow_ {
87 } __attribute__((aligned(CLS))) HostHashRow;
88 
89 /** host hash table */
90 extern HostHashRow *host_hash;
91 
92 #define HOST_VERBOSE 0
93 #define HOST_QUIET 1
94 
95 typedef struct HostConfig_ {
96  SC_ATOMIC_DECLARE(uint64_t, memcap);
97  uint32_t hash_rand;
98  uint32_t hash_size;
99  uint32_t prealloc;
101 
102 /** \brief check if a memory alloc would fit in the memcap
103  *
104  * \param size memory allocation size to check
105  *
106  * \retval 1 it fits
107  * \retval 0 no fit
108  */
109 #define HOST_CHECK_MEMCAP(size) \
110  ((((uint64_t)SC_ATOMIC_GET(host_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(host_config.memcap)))
111 
112 #define HostIncrUsecnt(h) \
113  (void)SC_ATOMIC_ADD((h)->use_cnt, 1)
114 #define HostDecrUsecnt(h) \
115  (void)SC_ATOMIC_SUB((h)->use_cnt, 1)
116 
117 #define HostReference(dst_h_ptr, h) do { \
118  if ((h) != NULL) { \
119  HostIncrUsecnt((h)); \
120  *(dst_h_ptr) = h; \
121  } \
122  } while (0)
123 
124 #define HostDeReference(src_h_ptr) do { \
125  if (*(src_h_ptr) != NULL) { \
126  HostDecrUsecnt(*(src_h_ptr)); \
127  *(src_h_ptr) = NULL; \
128  } \
129  } while (0)
130 
131 extern HostConfig host_config;
132 SC_ATOMIC_EXTERN(uint64_t,host_memuse);
133 SC_ATOMIC_EXTERN(uint32_t,host_counter);
134 SC_ATOMIC_EXTERN(uint32_t,host_prune_idx);
135 
136 void HostInitConfig(bool quiet);
137 void HostShutdown(void);
138 void HostCleanup(void);
139 
142 void HostRelease(Host *);
143 void HostLock(Host *);
144 void HostClearMemory(Host *);
145 void HostMoveToSpare(Host *);
146 uint32_t HostSpareQueueGetSize(void);
147 void HostPrintStats (void);
148 
149 void HostRegisterUnittests(void);
150 
151 Host *HostAlloc(void);
152 void HostFree(Host *);
153 
154 void HostUnlock(Host *h);
155 
156 int HostSetMemcap(uint64_t);
157 uint64_t HostGetMemcap(void);
158 uint64_t HostGetMemuse(void);
159 
160 #endif /* SURICATA_HOST_H */
HostLookupHostFromHash
Host * HostLookupHostFromHash(Address *)
look up a host in the hash
Definition: host.c:596
HostHashRow_::tail
Host * tail
Definition: host.h:86
Host_::hnext
struct Host_ * hnext
Definition: host.h:72
Host_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
HostConfig
struct HostConfig_ HostConfig
CLS
#define CLS
Definition: suricata-common.h:56
HostConfig_::prealloc
uint32_t prealloc
Definition: host.h:99
HostConfig_
Definition: host.h:95
HostCleanup
void HostCleanup(void)
Cleanup the host engine.
Definition: host.c:341
HostConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)
Host_::storage
Storage storage[]
Definition: host.h:80
HostInitConfig
void HostInitConfig(bool quiet)
initialize the configuration
Definition: host.c:173
Host_::m
SCMutex m
Definition: host.h:60
Host_::a
Address a
Definition: host.h:63
host_hash
HostHashRow * host_hash
Definition: host.c:50
Address_
Definition: decode.h:115
Host_::lnext
struct Host_ * lnext
Definition: host.h:76
__attribute__
struct HostHashRow_ __attribute__((aligned(CLS))) HostHashRow
Host
struct Host_ Host
host_config
HostConfig host_config
Definition: host.c:53
HostPrintStats
void HostPrintStats(void)
print some host stats
Definition: host.c:291
Storage
Definition: util-storage.h:39
HostRegisterUnittests
void HostRegisterUnittests(void)
Definition: host.c:728
HostAlloc
Host * HostAlloc(void)
Definition: host.c:111
Host_::hprev
struct Host_ * hprev
Definition: host.h:73
decode.h
HostLock
void HostLock(Host *)
Definition: host.c:478
HostGetMemcap
uint64_t HostGetMemcap(void)
Return memcap value.
Definition: host.c:83
HostHashRow_::lock
HRLOCK_TYPE lock
Definition: host.h:84
Host_::lprev
struct Host_ * lprev
Definition: host.h:77
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint64_t, host_memuse)
HostUnlock
void HostUnlock(Host *h)
Definition: host.c:483
HostSetMemcap
int HostSetMemcap(uint64_t)
Update memcap value.
Definition: host.c:68
HostHashRow_
Definition: host.h:83
HostSpareQueueGetSize
uint32_t HostSpareQueueGetSize(void)
Definition: host.c:100
Host_::iprep
void * iprep
Definition: host.h:69
HostShutdown
void HostShutdown(void)
shutdown the flow engine
Definition: host.c:304
HostConfig_::hash_rand
uint32_t hash_rand
Definition: host.h:97
HostClearMemory
void HostClearMemory(Host *)
Definition: host.c:155
HostConfig_::hash_size
uint32_t hash_size
Definition: host.h:98
HostRelease
void HostRelease(Host *)
Definition: host.c:472
HostHashRow_::head
Host * head
Definition: host.h:85
HRLOCK_TYPE
#define HRLOCK_TYPE
Definition: host.h:48
HostFree
void HostFree(Host *)
Definition: host.c:130
Host_
Definition: host.h:58
HostGetHostFromHash
Host * HostGetHostFromHash(Address *)
Definition: host.c:497
SCMutex
#define SCMutex
Definition: threads-debug.h:114
util-storage.h
HostGetMemuse
uint64_t HostGetMemuse(void)
Return memuse value.
Definition: host.c:94
HostMoveToSpare
void HostMoveToSpare(Host *)
Definition: host.c:105