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 __HOST_H__
25 #define __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  /** storage api handle */
73 
74  /** hash pointers, protected by hash row mutex/spin */
75  struct Host_ *hnext;
76  struct Host_ *hprev;
77 
78  /** list pointers, protected by host-queue mutex/spin */
79  struct Host_ *lnext;
80  struct Host_ *lprev;
81 } Host;
82 
83 typedef struct HostHashRow_ {
87 } __attribute__((aligned(CLS))) HostHashRow;
88 
89 /** host hash table */
90 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;
100 } HostConfig;
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 
132 SC_ATOMIC_DECLARE(uint64_t,host_memuse);
133 SC_ATOMIC_DECLARE(uint32_t,host_counter);
134 SC_ATOMIC_DECLARE(uint32_t,host_prune_idx);
135 
136 void HostInitConfig(char 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 /* __HOST_H__ */
161 
uint32_t prealloc
Definition: host.h:99
#define SCMutex
struct HostHashRow_ __attribute__((aligned(CLS))) HostHashRow
struct Host_ * hnext
Definition: host.h:75
uint64_t HostGetMemcap(void)
Return memcap value.
Definition: host.c:75
Host * HostAlloc(void)
Definition: host.c:103
uint64_t HostGetMemuse(void)
Return memuse value.
Definition: host.c:86
uint32_t hash_rand
Definition: host.h:97
uint32_t HostSpareQueueGetSize(void)
Definition: host.c:92
#define CLS
void HostUnlock(Host *h)
Definition: host.c:486
struct Host_ * hprev
Definition: host.h:76
void HostFree(Host *)
Definition: host.c:124
void HostInitConfig(char quiet)
initialize the configuration
Definition: host.c:168
struct Host_ * lnext
Definition: host.h:79
void HostPrintStats(void)
print some host stats
Definition: host.c:287
void HostRelease(Host *)
Definition: host.c:475
Storage * storage
Definition: host.h:72
HostHashRow * host_hash
Definition: host.h:90
SCMutex m
Definition: host.h:60
void * Storage
Definition: util-storage.h:39
HostConfig host_config
Definition: host.h:131
Address a
Definition: host.h:63
void * iprep
Definition: host.h:69
void HostRegisterUnittests(void)
Definition: host.c:731
Definition: host.h:58
#define HRLOCK_TYPE
Definition: host.h:48
struct Host_ Host
void HostCleanup(void)
Cleanup the host engine.
Definition: host.c:344
struct HostConfig_ HostConfig
void HostLock(Host *)
Definition: host.c:481
Host * HostGetHostFromHash(Address *)
Definition: host.c:500
Host * head
Definition: host.h:85
Host * tail
Definition: host.h:86
HRLOCK_TYPE lock
Definition: host.h:84
struct Host_ * lprev
Definition: host.h:80
void HostMoveToSpare(Host *)
Definition: host.c:97
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
uint32_t hash_size
Definition: host.h:98
void HostShutdown(void)
shutdown the flow engine
Definition: host.c:300
int HostSetMemcap(uint64_t)
Update memcap value.
Definition: host.c:60
void HostClearMemory(Host *)
Definition: host.c:151
Host * HostLookupHostFromHash(Address *)
look up a host in the hash
Definition: host.c:599