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
58typedef 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 */
82
83typedef struct HostHashRow_ {
87} __attribute__((aligned(CLS))) HostHashRow;
88
89/** host hash table */
90extern HostHashRow *host_hash;
91
92#define HOST_VERBOSE 0
93#define HOST_QUIET 1
94
95typedef 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
132SC_ATOMIC_EXTERN(uint64_t,host_memuse);
133SC_ATOMIC_EXTERN(uint32_t,host_counter);
134SC_ATOMIC_EXTERN(uint32_t,host_prune_idx);
135
136void HostInitConfig(bool quiet);
137void HostShutdown(void);
138void HostCleanup(void);
139
142void HostRelease(Host *);
143void HostLock(Host *);
144void HostClearMemory(Host *);
145void HostMoveToSpare(Host *);
146void HostPrintStats (void);
147
148void HostRegisterUnittests(void);
149
150Host *HostAlloc(void);
151void HostFree(Host *);
152
153void HostUnlock(Host *h);
154
155int HostSetMemcap(uint64_t);
156uint64_t HostGetMemcap(void);
157uint64_t HostGetMemuse(void);
158
159#endif /* SURICATA_HOST_H */
struct PrefilterEngineFlowbits __attribute__
DNP3 application header.
void HostMoveToSpare(Host *)
Definition host.c:100
void HostFree(Host *)
Definition host.c:125
void HostLock(Host *)
Definition host.c:467
Host * HostGetHostFromHash(Address *)
Definition host.c:486
uint64_t HostGetMemcap(void)
Return memcap value.
Definition host.c:83
void HostRelease(Host *)
Definition host.c:461
Host * HostLookupHostFromHash(Address *)
look up a host in the hash
Definition host.c:585
#define HRLOCK_TYPE
Definition host.h:48
void HostCleanup(void)
Cleanup the host engine.
Definition host.c:332
int HostSetMemcap(uint64_t)
Update memcap value.
Definition host.c:68
void HostShutdown(void)
shutdown the flow engine
Definition host.c:296
uint64_t HostGetMemuse(void)
Return memuse value.
Definition host.c:94
struct HostConfig_ HostConfig
void HostPrintStats(void)
print some host stats
Definition host.c:284
void HostInitConfig(bool quiet)
initialize the configuration
Definition host.c:168
Host * HostAlloc(void)
Definition host.c:106
struct Host_ Host
void HostClearMemory(Host *)
Definition host.c:150
HostConfig host_config
Definition host.c:53
HostHashRow * host_hash
Definition host.c:50
void HostUnlock(Host *h)
Definition host.c:472
void HostRegisterUnittests(void)
Definition host.c:717
SC_ATOMIC_DECLARE(uint64_t, memcap)
uint32_t prealloc
Definition host.h:99
uint32_t hash_size
Definition host.h:98
uint32_t hash_rand
Definition host.h:97
HRLOCK_TYPE lock
Definition host.h:84
Host * tail
Definition host.h:86
Host * head
Definition host.h:85
Definition host.h:58
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
struct Host_ * lprev
Definition host.h:77
struct Host_ * hprev
Definition host.h:73
Address a
Definition host.h:63
Storage storage[]
Definition host.h:80
void * iprep
Definition host.h:69
SCMutex m
Definition host.h:60
struct Host_ * hnext
Definition host.h:72
struct Host_ * lnext
Definition host.h:76
#define CLS
#define SCMutex
#define SC_ATOMIC_EXTERN(type, name)
wrapper for referencing an atomic variable declared on another file.