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 */
60     SCMutex m;
61 
62     /** host address -- ipv4 or ipv6 */
63     Address a;
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 */
72     Storage *storage;
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_ {
84     HRLOCK_TYPE lock;
85     Host *head;
86     Host *tail;
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;
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 
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(char quiet);
137 void HostShutdown(void);
138 void HostCleanup(void);
139 
140 Host *HostLookupHostFromHash (Address *);
141 Host *HostGetHostFromHash (Address *);
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 
162