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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* Copyright (C) 2007-2019 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>
*
* Packet Queue portion of the engine.
*/
#include "suricata-common.h"
#include "decode.h"
#include "packet-queue.h"
#include "threads.h"
#include "suricata.h"
#include "util-var.h"
#include "pkt-var.h"
#include "util-validate.h"
#ifdef DEBUG
void PacketQueueValidateDebug(PacketQueue *q);
void PacketQueueValidate(PacketQueue *q);
void PacketQueueValidateDebug(PacketQueue *q)
{
SCLogDebug("q->len %u, q->top %p, q->bot %p", q->len, q->top, q->bot);
if (q->len == 0) {
BUG_ON(q->top != NULL);
BUG_ON(q->bot != NULL);
} else if(q->len == 1) {
SCLogDebug("q->top->next %p, q->top->prev %p", q->top->next, q->top->prev);
SCLogDebug("q->bot->next %p, q->bot->prev %p", q->bot->next, q->bot->prev);
BUG_ON(q->top != q->bot);
BUG_ON(q->top->next != NULL);
BUG_ON(q->bot->next != NULL);
BUG_ON(q->top->prev != NULL);
BUG_ON(q->bot->prev != NULL);
} else if (q->len == 2) {
SCLogDebug("q->top->next %p, q->top->prev %p", q->top->next, q->top->prev);
SCLogDebug("q->bot->next %p, q->bot->prev %p", q->bot->next, q->bot->prev);
BUG_ON(q->top == NULL);
BUG_ON(q->bot == NULL);
BUG_ON(q->top == q->bot);
BUG_ON(q->top->prev != NULL);
BUG_ON(q->top->next != q->bot);
BUG_ON(q->bot->prev != q->top);
BUG_ON(q->bot->next != NULL);
} else {
BUG_ON(q->top == NULL);
BUG_ON(q->bot == NULL);
SCLogDebug("q->top->next %p, q->top->prev %p", q->top->next, q->top->prev);
SCLogDebug("q->bot->next %p, q->bot->prev %p", q->bot->next, q->bot->prev);
BUG_ON(q->top == q->bot);
BUG_ON(q->top->prev != NULL);
BUG_ON(q->bot->next != NULL);
BUG_ON(q->top->next == q->bot);
BUG_ON(q->bot->prev == q->top);
Packet *p, *pp;
for (p = q->top, pp = p->prev; p != NULL; pp = p, p = p->next) {
SCLogDebug("p %p, pp %p, p->next %p, p->prev %p", p, pp, p->next, p->prev);
BUG_ON(pp != p->prev);
}
}
}
#define BUGGER_ON(cond) { \
if ((cond)) { \
PacketQueueValidateDebug(q); \
} \
}
void PacketQueueValidate(PacketQueue *q)
{
if (q->len == 0) {
BUGGER_ON(q->top != NULL);
BUGGER_ON(q->bot != NULL);
} else if(q->len == 1) {
BUGGER_ON(q->top != q->bot);
BUGGER_ON(q->top->next != NULL);
BUGGER_ON(q->bot->next != NULL);
BUGGER_ON(q->top->prev != NULL);
BUGGER_ON(q->bot->prev != NULL);
} else if (q->len == 2) {
BUGGER_ON(q->top == NULL);
BUGGER_ON(q->bot == NULL);
BUGGER_ON(q->top == q->bot);
BUGGER_ON(q->top->prev != NULL);
BUGGER_ON(q->top->next != q->bot);
BUGGER_ON(q->bot->prev != q->top);
BUGGER_ON(q->bot->next != NULL);
} else {
BUGGER_ON(q->top == NULL);
BUGGER_ON(q->bot == NULL);
BUGGER_ON(q->top == q->bot);
BUGGER_ON(q->top->prev != NULL);
BUGGER_ON(q->bot->next != NULL);
BUGGER_ON(q->top->next == q->bot);
BUGGER_ON(q->bot->prev == q->top);
Packet *p, *pp;
for (p = q->top, pp = p->prev; p != NULL; pp = p, p = p->next) {
BUGGER_ON(pp != p->prev);
}
}
}
#endif /* DEBUG */
static inline void PacketEnqueueDo(PacketQueue *q, Packet *p)
{
//PacketQueueValidateDebug(q);
if (p == NULL)
return;
/* more packets in queue */
if (q->top != NULL) {
p->prev = NULL;
p->next = q->top;
q->top->prev = p;
q->top = p;
/* only packet */
} else {
p->prev = NULL;
p->next = NULL;
q->top = p;
q->bot = p;
}
q->len++;
#ifdef DBG_PERF
if (q->len > q->dbg_maxlen)
q->dbg_maxlen = q->len;
#endif /* DBG_PERF */
//PacketQueueValidateDebug(q);
}
void PacketEnqueueNoLock(PacketQueueNoLock *qnl, Packet *p)
{
DEBUG_VALIDATE_BUG_ON(p->pkt_src == 0);
PacketQueue *q = (PacketQueue *)qnl;
PacketEnqueueDo(q, p);
}
void PacketEnqueue (PacketQueue *q, Packet *p)
{
PacketEnqueueDo(q, p);
}
static inline Packet *PacketDequeueDo (PacketQueue *q)
{
//PacketQueueValidateDebug(q);
/* if the queue is empty there are no packets left. */
if (q->len == 0) {
return NULL;
}
q->len--;
/* pull the bottom packet from the queue */
Packet *p = q->bot;
/* more packets in queue */
if (q->bot->prev != NULL) {
q->bot = q->bot->prev;
q->bot->next = NULL;
/* just the one we remove, so now empty */
} else {
q->top = NULL;
q->bot = NULL;
}
//PacketQueueValidateDebug(q);
p->next = NULL;
p->prev = NULL;
return p;
}
Packet *PacketDequeueNoLock (PacketQueueNoLock *qnl)
{
PacketQueue *q = (PacketQueue *)qnl;
Packet *p = PacketDequeueDo(q);
DEBUG_VALIDATE_BUG_ON(p != NULL && p->pkt_src == 0);
return p;
}
Packet *PacketDequeue (PacketQueue *q)
{
return PacketDequeueDo(q);
}
PacketQueue *PacketQueueAlloc(void)
{
PacketQueue *pq = SCCalloc(1, sizeof(*pq));
if (pq == NULL)
return NULL;
SCMutexInit(&pq->mutex_q, NULL);
SCCondInit(&pq->cond_q, NULL);
return pq;
}
void PacketQueueFree(PacketQueue *pq)
{
SCCondDestroy(&pq->cond_q);
SCMutexDestroy(&pq->mutex_q);
SCFree(pq);
}
|