1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* Copyright (C) 2007-2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*/
#include "suricata-common.h"
#include "suricata.h"
#include "stream-tcp-private.h"
#include "stream-tcp-cache.h"
#include "util-debug.h"
typedef struct TcpPoolCache {
bool cache_enabled; /**< cache should only be enabled for worker threads */
TcpSegment *segs_cache[64];
uint32_t segs_cache_idx;
uint32_t segs_returns_idx;
TcpSegment *segs_returns[64];
TcpSession *ssns_cache[64];
uint32_t ssns_cache_idx;
uint32_t ssns_returns_idx;
TcpSession *ssns_returns[64];
} TcpPoolCache;
static thread_local TcpPoolCache tcp_pool_cache;
extern PoolThread *ssn_pool;
extern PoolThread *segment_thread_pool;
/** \brief enable segment cache. Should only be done for worker threads */
void StreamTcpThreadCacheEnable(void)
{
tcp_pool_cache.cache_enabled = true;
}
void StreamTcpThreadCacheReturnSegment(TcpSegment *seg)
{
SCEnter();
#ifdef UNITTESTS
if (RunmodeIsUnittests()) {
PoolThreadReturn(segment_thread_pool, seg);
SCReturn;
}
#endif
/* cache can have segs from any pool id */
if (tcp_pool_cache.cache_enabled && tcp_pool_cache.segs_cache_idx < 64) {
tcp_pool_cache.segs_cache[tcp_pool_cache.segs_cache_idx++] = seg;
} else {
/* segs_returns should only have a single pool id. If ours is different,
* flush it. */
bool flush = false;
if (tcp_pool_cache.segs_returns_idx &&
tcp_pool_cache.segs_returns[0]->pool_id != seg->pool_id) {
flush = true;
}
if (tcp_pool_cache.segs_returns_idx == 64) {
flush = true;
}
if (flush) {
PoolThreadId pool_id = tcp_pool_cache.segs_returns[0]->pool_id;
PoolThreadLock(segment_thread_pool, pool_id);
for (uint32_t i = 0; i < tcp_pool_cache.segs_returns_idx; i++) {
TcpSegment *ret_seg = tcp_pool_cache.segs_returns[i];
PoolThreadReturnRaw(segment_thread_pool, pool_id, ret_seg);
}
PoolThreadUnlock(segment_thread_pool, pool_id);
tcp_pool_cache.segs_returns_idx = 0;
}
tcp_pool_cache.segs_returns[tcp_pool_cache.segs_returns_idx++] = seg;
}
}
void StreamTcpThreadCacheReturnSession(TcpSession *ssn)
{
SCEnter();
#ifdef UNITTESTS
if (RunmodeIsUnittests()) {
PoolThreadReturn(ssn_pool, ssn);
SCReturn;
}
#endif
/* cache can have ssns from any pool id */
if (tcp_pool_cache.cache_enabled && tcp_pool_cache.ssns_cache_idx < 64) {
tcp_pool_cache.ssns_cache[tcp_pool_cache.ssns_cache_idx++] = ssn;
} else {
/* ssns_returns should only have a single pool id. If ours is different,
* flush it. */
bool flush = false;
if (tcp_pool_cache.ssns_returns_idx &&
tcp_pool_cache.ssns_returns[0]->pool_id != ssn->pool_id) {
flush = true;
}
if (tcp_pool_cache.ssns_returns_idx == 64) {
flush = true;
}
if (flush) {
PoolThreadId pool_id = tcp_pool_cache.ssns_returns[0]->pool_id;
PoolThreadLock(ssn_pool, pool_id);
for (uint32_t i = 0; i < tcp_pool_cache.ssns_returns_idx; i++) {
TcpSession *ret_ssn = tcp_pool_cache.ssns_returns[i];
PoolThreadReturnRaw(ssn_pool, pool_id, ret_ssn);
}
PoolThreadUnlock(ssn_pool, pool_id);
tcp_pool_cache.ssns_returns_idx = 0;
}
tcp_pool_cache.ssns_returns[tcp_pool_cache.ssns_returns_idx++] = ssn;
}
SCReturn;
}
void StreamTcpThreadCacheCleanup(void)
{
SCEnter();
/* segments */
SCLogDebug("tcp_pool_cache.segs_cache_idx %u", tcp_pool_cache.segs_cache_idx);
for (uint32_t i = 0; i < tcp_pool_cache.segs_cache_idx; i++) {
PoolThreadReturn(segment_thread_pool, tcp_pool_cache.segs_cache[i]);
}
tcp_pool_cache.segs_cache_idx = 0;
SCLogDebug("tcp_pool_cache.segs_returns_idx %u", tcp_pool_cache.segs_returns_idx);
if (tcp_pool_cache.segs_returns_idx) {
PoolThreadId pool_id = tcp_pool_cache.segs_returns[0]->pool_id;
PoolThreadLock(segment_thread_pool, pool_id);
for (uint32_t i = 0; i < tcp_pool_cache.segs_returns_idx; i++) {
TcpSegment *ret_seg = tcp_pool_cache.segs_returns[i];
PoolThreadReturnRaw(segment_thread_pool, pool_id, ret_seg);
}
PoolThreadUnlock(segment_thread_pool, pool_id);
tcp_pool_cache.segs_returns_idx = 0;
}
/* sessions */
SCLogDebug("tcp_pool_cache.ssns_cache_idx %u", tcp_pool_cache.ssns_cache_idx);
for (uint32_t i = 0; i < tcp_pool_cache.ssns_cache_idx; i++) {
PoolThreadReturn(ssn_pool, tcp_pool_cache.ssns_cache[i]);
}
tcp_pool_cache.ssns_cache_idx = 0;
SCLogDebug("tcp_pool_cache.ssns_returns_idx %u", tcp_pool_cache.ssns_returns_idx);
if (tcp_pool_cache.ssns_returns_idx) {
PoolThreadId pool_id = tcp_pool_cache.ssns_returns[0]->pool_id;
PoolThreadLock(ssn_pool, pool_id);
for (uint32_t i = 0; i < tcp_pool_cache.ssns_returns_idx; i++) {
TcpSession *ret_ssn = tcp_pool_cache.ssns_returns[i];
PoolThreadReturnRaw(ssn_pool, pool_id, ret_ssn);
}
PoolThreadUnlock(ssn_pool, pool_id);
tcp_pool_cache.ssns_returns_idx = 0;
}
SCReturn;
}
TcpSegment *StreamTcpThreadCacheGetSegment(void)
{
if (tcp_pool_cache.segs_cache_idx) {
TcpSegment *seg = tcp_pool_cache.segs_cache[tcp_pool_cache.segs_cache_idx - 1];
tcp_pool_cache.segs_cache_idx--;
memset(&seg->sbseg, 0, sizeof(seg->sbseg));
return seg;
}
return NULL;
}
TcpSession *StreamTcpThreadCacheGetSession(void)
{
if (tcp_pool_cache.ssns_cache_idx) {
TcpSession *ssn = tcp_pool_cache.ssns_cache[tcp_pool_cache.ssns_cache_idx - 1];
tcp_pool_cache.ssns_cache_idx--;
return ssn;
}
return NULL;
}
|