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 #ifndef __FLOW_QUEUE_H__ 25 #define __FLOW_QUEUE_H__ 26 27 #include "suricata-common.h" 28 #include "flow.h" 29 30 /** Spinlocks or Mutex for the flow queues. */ 31 //#define FQLOCK_SPIN 32 #define FQLOCK_MUTEX 33 34 #ifdef FQLOCK_SPIN 35 #ifdef FQLOCK_MUTEX 36 #error Cannot enable both FQLOCK_SPIN and FQLOCK_MUTEX 37 #endif 38 #endif 39 40 typedef struct FlowQueuePrivate_ 41 { 42 Flow *top; 43 Flow *bot; 44 uint32_t len; 45 } FlowQueuePrivate; 46 47 /* Define a queue for storing flows */ 48 typedef struct FlowQueue_ 49 { 50 FlowQueuePrivate priv; 51 SC_ATOMIC_DECLARE(bool,non_empty); 52 #ifdef FQLOCK_MUTEX 53 SCMutex m; 54 #elif defined FQLOCK_SPIN 55 SCSpinlock s; 56 #else 57 #error Enable FQLOCK_SPIN or FQLOCK_MUTEX 58 #endif 59 } FlowQueue; 60 #define qtop priv.top 61 #define qbot priv.bot 62 #define qlen priv.len 63 64 #ifdef FQLOCK_SPIN 65 #define FQLOCK_INIT(q) SCSpinInit(&(q)->s, 0) 66 #define FQLOCK_DESTROY(q) SCSpinDestroy(&(q)->s) 67 #define FQLOCK_LOCK(q) SCSpinLock(&(q)->s) 68 #define FQLOCK_TRYLOCK(q) SCSpinTrylock(&(q)->s) 69 #define FQLOCK_UNLOCK(q) SCSpinUnlock(&(q)->s) 70 #elif defined FQLOCK_MUTEX 71 #define FQLOCK_INIT(q) SCMutexInit(&(q)->m, NULL) 72 #define FQLOCK_DESTROY(q) SCMutexDestroy(&(q)->m) 73 #define FQLOCK_LOCK(q) SCMutexLock(&(q)->m) 74 #define FQLOCK_TRYLOCK(q) SCMutexTrylock(&(q)->m) 75 #define FQLOCK_UNLOCK(q) SCMutexUnlock(&(q)->m) 76 #else 77 #error Enable FQLOCK_SPIN or FQLOCK_MUTEX 78 #endif 79 80 /* prototypes */ 81 FlowQueue *FlowQueueNew(void); 82 FlowQueue *FlowQueueInit(FlowQueue *); 83 void FlowQueueDestroy (FlowQueue *); 84 85 void FlowEnqueue (FlowQueue *, Flow *); 86 Flow *FlowDequeue (FlowQueue *); 87 void FlowQueueRemove(FlowQueue *fq, Flow *f); 88 void FlowQueueRemoveLock(FlowQueue *fq, Flow *f); 89 90 void FlowQueuePrivateAppendFlow(FlowQueuePrivate *fqc, Flow *f); 91 void FlowQueuePrivatePrependFlow(FlowQueuePrivate *fqc, Flow *f); 92 93 void FlowQueueAppendPrivate(FlowQueue *fq, FlowQueuePrivate *fqp); 94 void FlowQueuePrivateAppendPrivate(FlowQueuePrivate *dest, FlowQueuePrivate *src); 95 FlowQueuePrivate FlowQueueExtractPrivate(FlowQueue *fq); 96 Flow *FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqp); 97 98 #endif /* __FLOW_QUEUE_H__ */ 99 100