suricata
flow-queue.c
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  * Flow queue handler functions
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "debug.h"
29 #include "flow-private.h"
30 #include "flow-queue.h"
31 #include "flow-util.h"
32 #include "util-error.h"
33 #include "util-debug.h"
34 #include "util-print.h"
35 
37 {
38  FlowQueue *q = (FlowQueue *)SCMalloc(sizeof(FlowQueue));
39  if (q == NULL) {
40  SCLogError(SC_ERR_FATAL, "Fatal error encountered in FlowQueueNew. Exiting...");
41  exit(EXIT_SUCCESS);
42  }
43  q = FlowQueueInit(q);
44  return q;
45 }
46 
48 {
49  if (q != NULL) {
50  memset(q, 0, sizeof(FlowQueue));
51  FQLOCK_INIT(q);
52  }
53  return q;
54 }
55 
56 /**
57  * \brief Destroy a flow queue
58  *
59  * \param q the flow queue to destroy
60  */
62 {
63  FQLOCK_DESTROY(q);
64 }
65 
66 /**
67  * \brief add a flow to a queue
68  *
69  * \param q queue
70  * \param f flow
71  */
72 void FlowEnqueue (FlowQueue *q, Flow *f)
73 {
74 #ifdef DEBUG
75  BUG_ON(q == NULL || f == NULL);
76 #endif
77 
78  FQLOCK_LOCK(q);
79 
80  /* more flows in queue */
81  if (q->top != NULL) {
82  f->lnext = q->top;
83  q->top->lprev = f;
84  q->top = f;
85  /* only flow */
86  } else {
87  q->top = f;
88  q->bot = f;
89  }
90  q->len++;
91 #ifdef DBG_PERF
92  if (q->len > q->dbg_maxlen)
93  q->dbg_maxlen = q->len;
94 #endif /* DBG_PERF */
95  FQLOCK_UNLOCK(q);
96 }
97 
98 /**
99  * \brief remove a flow from the queue
100  *
101  * \param q queue
102  *
103  * \retval f flow or NULL if empty list.
104  */
106 {
107  FQLOCK_LOCK(q);
108 
109  Flow *f = q->bot;
110  if (f == NULL) {
111  FQLOCK_UNLOCK(q);
112  return NULL;
113  }
114 
115  /* more packets in queue */
116  if (q->bot->lprev != NULL) {
117  q->bot = q->bot->lprev;
118  q->bot->lnext = NULL;
119  /* just the one we remove, so now empty */
120  } else {
121  q->top = NULL;
122  q->bot = NULL;
123  }
124 
125 #ifdef DEBUG
126  BUG_ON(q->len == 0);
127 #endif
128  if (q->len > 0)
129  q->len--;
130 
131  f->lnext = NULL;
132  f->lprev = NULL;
133 
134  FQLOCK_UNLOCK(q);
135  return f;
136 }
137 
138 /**
139  * \brief Transfer a flow from a queue to the spare queue
140  *
141  * \param f the flow to be transfered
142  * \param q the source queue, where the flow will be removed. This queue is locked.
143  *
144  * \note spare queue needs locking
145  */
147 {
148  /* now put it in spare */
150 
151  /* add to new queue (append) */
152  f->lprev = flow_spare_q.bot;
153  if (f->lprev != NULL)
154  f->lprev->lnext = f;
155  f->lnext = NULL;
156  flow_spare_q.bot = f;
157  if (flow_spare_q.top == NULL)
158  flow_spare_q.top = f;
159 
160  flow_spare_q.len++;
161 #ifdef DBG_PERF
162  if (flow_spare_q.len > flow_spare_q.dbg_maxlen)
163  flow_spare_q.dbg_maxlen = flow_spare_q.len;
164 #endif /* DBG_PERF */
165 
167 }
168 
#define BUG_ON(x)
#define FQLOCK_LOCK(q)
Definition: flow-queue.h:67
void FlowEnqueue(FlowQueue *q, Flow *f)
add a flow to a queue
Definition: flow-queue.c:72
#define FQLOCK_INIT(q)
Definition: flow-queue.h:65
void FlowMoveToSpare(Flow *f)
Transfer a flow from a queue to the spare queue.
Definition: flow-queue.c:146
void FlowQueueDestroy(FlowQueue *q)
Destroy a flow queue.
Definition: flow-queue.c:61
FlowQueue * FlowQueueInit(FlowQueue *q)
Definition: flow-queue.c:47
struct Flow_ * lprev
Definition: flow.h:452
Flow * bot
Definition: flow-queue.h:44
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define FQLOCK_DESTROY(q)
Definition: flow-queue.h:66
Flow * top
Definition: flow-queue.h:43
#define SCMalloc(a)
Definition: util-mem.h:166
struct Flow_ * lnext
Definition: flow.h:451
Flow * FlowDequeue(FlowQueue *q)
remove a flow from the queue
Definition: flow-queue.c:105
FlowQueue * FlowQueueNew()
Definition: flow-queue.c:36
uint32_t len
Definition: flow-queue.h:45
Flow data structure.
Definition: flow.h:324
#define FQLOCK_UNLOCK(q)
Definition: flow-queue.h:69
FlowQueue flow_spare_q
Definition: flow-private.h:91