suricata
ippair-timeout.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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 #include "suricata-common.h"
25 #include "ippair.h"
26 #include "ippair-bit.h"
27 #include "ippair-timeout.h"
28 
29 /** \internal
30  * \brief See if we can really discard this ippair. Check use_cnt reference.
31  *
32  * \param h ippair
33  * \param ts timestamp
34  *
35  * \retval 0 not timed out just yet
36  * \retval 1 fully timed out, lets kill it
37  */
38 static int IPPairTimedOut(IPPair *h, SCTime_t ts)
39 {
40  int vars = 0;
41  /** never prune a ippair that is used by a packet
42  * we are currently processing in one of the threads */
43  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
44  return 0;
45  }
46 
47  if (IPPairHasBits(h) && IPPairBitsTimedoutCheck(h, ts) == 0) {
48  vars = 1;
49  }
50  if (vars) {
51  return 0;
52  }
53 
54  SCLogDebug("ippair %p timed out", h);
55  return 1;
56 }
57 
58 /**
59  * \internal
60  *
61  * \brief check all ippairs in a hash row for timing out
62  *
63  * \param hb ippair hash row *LOCKED*
64  * \param h last ippair in the hash row
65  * \param ts timestamp
66  *
67  * \retval cnt timed out ippairs
68  */
69 static uint32_t IPPairHashRowTimeout(IPPairHashRow *hb, IPPair *h, SCTime_t ts)
70 {
71  uint32_t cnt = 0;
72 
73  do {
74  if (SCMutexTrylock(&h->m) != 0) {
75  h = h->hprev;
76  continue;
77  }
78 
79  IPPair *next_ippair = h->hprev;
80 
81  /* check if the ippair is fully timed out and
82  * ready to be discarded. */
83  if (IPPairTimedOut(h, ts) == 1) {
84  /* remove from the hash */
85  if (h->hprev != NULL)
86  h->hprev->hnext = h->hnext;
87  if (h->hnext != NULL)
88  h->hnext->hprev = h->hprev;
89  if (hb->head == h)
90  hb->head = h->hnext;
91  if (hb->tail == h)
92  hb->tail = h->hprev;
93 
94  h->hnext = NULL;
95  h->hprev = NULL;
96 
98 
99  /* no one is referring to this ippair, use_cnt 0, removed from hash
100  * so we can unlock it and move it back to the spare queue. */
101  SCMutexUnlock(&h->m);
102 
103  /* move to spare list */
105 
106  cnt++;
107  } else {
108  SCMutexUnlock(&h->m);
109  }
110 
111  h = next_ippair;
112  } while (h != NULL);
113 
114  return cnt;
115 }
116 
117 /**
118  * \brief time out ippairs from the hash
119  *
120  * \param ts timestamp
121  *
122  * \retval cnt number of timed out ippair
123  */
125 {
126  uint32_t idx = 0;
127  uint32_t cnt = 0;
128 
129  for (idx = 0; idx < ippair_config.hash_size; idx++) {
130  IPPairHashRow *hb = &ippair_hash[idx];
131 
132  if (HRLOCK_TRYLOCK(hb) != 0)
133  continue;
134 
135  /* ippair hash bucket is now locked */
136 
137  if (hb->tail == NULL) {
138  HRLOCK_UNLOCK(hb);
139  continue;
140  }
141 
142  /* we have a ippair, or more than one */
143  cnt += IPPairHashRowTimeout(hb, hb->tail, ts);
144  HRLOCK_UNLOCK(hb);
145  }
146 
147  return cnt;
148 }
ts
uint64_t ts
Definition: source-erf-file.c:55
ippair.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
IPPairTimeoutHash
uint32_t IPPairTimeoutHash(SCTime_t ts)
time out ippairs from the hash
Definition: ippair-timeout.c:124
ippair-bit.h
IPPairMoveToSpare
void IPPairMoveToSpare(IPPair *h)
Definition: ippair.c:98
IPPair_::m
SCMutex m
Definition: ippair.h:60
HRLOCK_UNLOCK
#define HRLOCK_UNLOCK(fb)
Definition: host.h:53
ippair_config
IPPairConfig ippair_config
Definition: ippair.c:52
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
SCTime_t
Definition: util-time.h:40
IPPairHasBits
int IPPairHasBits(IPPair *ippair)
Definition: ippair-bit.c:58
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
suricata-common.h
IPPair_
Definition: ippair.h:58
IPPairConfig_::hash_size
uint32_t hash_size
Definition: ippair.h:94
IPPairClearMemory
void IPPairClearMemory(IPPair *h)
Definition: ippair.c:150
IPPair_::hprev
struct IPPair_ * hprev
Definition: ippair.h:70
IPPair_::hnext
struct IPPair_ * hnext
Definition: ippair.h:69
HRLOCK_TRYLOCK
#define HRLOCK_TRYLOCK(fb)
Definition: host.h:52
ippair-timeout.h
IPPairBitsTimedoutCheck
int IPPairBitsTimedoutCheck(IPPair *h, SCTime_t ts)
Definition: ippair-bit.c:67
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
ippair_hash
IPPairHashRow * ippair_hash
Definition: ippair.c:49
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118