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 SURICATA_IPPAIR_H
25#define SURICATA_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
58typedef struct IPPair_ {
59 /** ippair mutex */
61
62 /** ippair addresses -- ipv4 or ipv6 */
64
65 /** use cnt, reference counter */
66 SC_ATOMIC_DECLARE(unsigned int, use_cnt);
67
68 /** hash pointers, protected by hash row mutex/spin */
69 struct IPPair_ *hnext;
70 struct IPPair_ *hprev;
71
72 /** list pointers, protected by ippair-queue mutex/spin */
73 struct IPPair_ *lnext;
74 struct IPPair_ *lprev;
75
76 /** storage api handle as a flex array member, so must stay last */
79
80typedef struct IPPairHashRow_ {
84} __attribute__((aligned(CLS))) IPPairHashRow;
85
86/** ippair hash table */
87extern IPPairHashRow *ippair_hash;
88
89#define IPPAIR_QUIET 1
90
91typedef struct IPPairConfig_ {
92 SC_ATOMIC_DECLARE(uint64_t, memcap);
93 uint32_t hash_rand;
94 uint32_t hash_size;
95 uint32_t prealloc;
97
98/** \brief check if a memory alloc would fit in the memcap
99 *
100 * \param size memory allocation size to check
101 *
102 * \retval 1 it fits
103 * \retval 0 no fit
104 */
105#define IPPAIR_CHECK_MEMCAP(size) \
106 ((((uint64_t)SC_ATOMIC_GET(ippair_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(ippair_config.memcap)))
107
108#define IPPairIncrUsecnt(h) \
109 (void)SC_ATOMIC_ADD((h)->use_cnt, 1)
110#define IPPairDecrUsecnt(h) \
111 (void)SC_ATOMIC_SUB((h)->use_cnt, 1)
112
114SC_ATOMIC_EXTERN(uint64_t,ippair_memuse);
115SC_ATOMIC_EXTERN(uint32_t,ippair_counter);
116SC_ATOMIC_EXTERN(uint32_t,ippair_prune_idx);
117
118void IPPairInitConfig(bool quiet);
119void IPPairShutdown(void);
120void IPPairCleanup(void);
121
124void IPPairRelease(IPPair *);
127void IPPairPrintStats (void);
128
129void IPPairRegisterUnittests(void);
130
131IPPair *IPPairAlloc(void);
132void IPPairFree(IPPair *);
133
134void IPPairUnlock(IPPair *);
135
136int IPPairSetMemcap(uint64_t size);
137uint64_t IPPairGetMemcap(void);
138uint64_t IPPairGetMemuse(void);
139
140#endif /* SURICATA_IPPAIR_H */
struct PrefilterEngineFlowbits __attribute__
DNP3 application header.
struct IPPairConfig_ IPPairConfig
void IPPairClearMemory(IPPair *)
Definition ippair.c:150
void IPPairUnlock(IPPair *)
Definition ippair.c:508
IPPairHashRow * ippair_hash
Definition ippair.c:49
int IPPairSetMemcap(uint64_t size)
Update memcap value.
Definition ippair.c:66
IPPair * IPPairLookupIPPairFromHash(Address *, Address *)
look up a ippair in the hash
Definition ippair.c:620
struct IPPair_ IPPair
#define HRLOCK_TYPE
Definition ippair.h:48
void IPPairCleanup(void)
Cleanup the ippair engine.
Definition ippair.c:327
void IPPairMoveToSpare(IPPair *)
Definition ippair.c:98
void IPPairRegisterUnittests(void)
Definition ippair.c:752
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition ippair.c:162
IPPair * IPPairGetIPPairFromHash(Address *, Address *)
Definition ippair.c:521
void IPPairRelease(IPPair *)
Definition ippair.c:502
void IPPairFree(IPPair *)
Definition ippair.c:124
IPPairConfig ippair_config
Definition ippair.c:52
void IPPairShutdown(void)
shutdown the flow engine
Definition ippair.c:290
uint64_t IPPairGetMemuse(void)
Return memuse value.
Definition ippair.c:92
void IPPairPrintStats(void)
print some ippair stats
Definition ippair.c:278
IPPair * IPPairAlloc(void)
Definition ippair.c:104
uint64_t IPPairGetMemcap(void)
Return memcap value.
Definition ippair.c:81
uint32_t hash_size
Definition ippair.h:94
uint32_t prealloc
Definition ippair.h:95
uint32_t hash_rand
Definition ippair.h:93
SC_ATOMIC_DECLARE(uint64_t, memcap)
IPPair * head
Definition ippair.h:82
HRLOCK_TYPE lock
Definition ippair.h:81
IPPair * tail
Definition ippair.h:83
struct IPPair_ * lnext
Definition ippair.h:73
Address a[2]
Definition ippair.h:63
struct IPPair_ * hprev
Definition ippair.h:70
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
SCMutex m
Definition ippair.h:60
struct IPPair_ * hnext
Definition ippair.h:69
Storage storage[]
Definition ippair.h:77
struct IPPair_ * lprev
Definition ippair.h:74
#define CLS
#define SCMutex
#define SC_ATOMIC_EXTERN(type, name)
wrapper for referencing an atomic variable declared on another file.