From: Victor Julien Date: Wed, 22 Apr 2015 10:41:57 +0000 (+0200) Subject: debug: packet pool init/destroy validation X-Git-Tag: suricata-3.0RC1~190 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f871c0e1b80e0712bf5dd43cc1799561c5ad42c2;p=thirdparty%2Fsuricata.git debug: packet pool init/destroy validation Validate packet pool handling: - pools are initialized before use - pools are not used after destroy - pools are not double initialized/destroyed --- diff --git a/src/tmqh-packetpool.c b/src/tmqh-packetpool.c index 4c4bf40691..a1f19dca46 100644 --- a/src/tmqh-packetpool.c +++ b/src/tmqh-packetpool.c @@ -241,7 +241,10 @@ static void PacketPoolGetReturnedPackets(PktPool *pool) Packet *PacketPoolGetPacket(void) { PktPool *pool = GetThreadPacketPool(); - +#ifdef DEBUG_VALIDATION + BUG_ON(pool->initialized == 0); + BUG_ON(pool->destroyed == 1); +#endif /* DEBUG_VALIDATION */ if (pool->head) { /* Stack is not empty. */ Packet *p = pool->head; @@ -286,6 +289,12 @@ void PacketPoolReturnPacket(Packet *p) PacketFree(p); return; } +#ifdef DEBUG_VALIDATION + BUG_ON(pool->initialized == 0); + BUG_ON(pool->destroyed == 1); + BUG_ON(my_pool->initialized == 0); + BUG_ON(my_pool->destroyed == 1); +#endif /* DEBUG_VALIDATION */ if (pool == my_pool) { /* Push back onto this thread's own stack, so no locking. */ @@ -338,6 +347,12 @@ void PacketPoolInitEmpty(void) PktPool *my_pool = GetThreadPacketPool(); +#ifdef DEBUG_VALIDATION + BUG_ON(my_pool->initialized); + my_pool->initialized = 1; + my_pool->destroyed = 0; +#endif /* DEBUG_VALIDATION */ + SCMutexInit(&my_pool->return_stack.mutex, NULL); SCCondInit(&my_pool->return_stack.cond, NULL); SC_ATOMIC_INIT(my_pool->return_stack.sync_now); @@ -353,6 +368,12 @@ void PacketPoolInit(void) PktPool *my_pool = GetThreadPacketPool(); +#ifdef DEBUG_VALIDATION + BUG_ON(my_pool->initialized); + my_pool->initialized = 1; + my_pool->destroyed = 0; +#endif /* DEBUG_VALIDATION */ + SCMutexInit(&my_pool->return_stack.mutex, NULL); SCCondInit(&my_pool->return_stack.cond, NULL); SC_ATOMIC_INIT(my_pool->return_stack.sync_now); @@ -371,12 +392,18 @@ void PacketPoolInit(void) } SCLogInfo("preallocated %"PRIiMAX" packets. Total memory %"PRIuMAX"", max_pending_packets, (uintmax_t)(max_pending_packets*SIZE_OF_PACKET)); + } void PacketPoolDestroy(void) { Packet *p = NULL; PktPool *my_pool = GetThreadPacketPool(); + +#ifdef DEBUG_VALIDATION + BUG_ON(my_pool->destroyed); +#endif /* DEBUG_VALIDATION */ + if (my_pool && my_pool->pending_pool != NULL) { p = my_pool->pending_head; while (p) { @@ -385,7 +412,9 @@ void PacketPoolDestroy(void) p = next_p; my_pool->pending_count--; } +#ifdef DEBUG_VALIDATION BUG_ON(my_pool->pending_count); +#endif /* DEBUG_VALIDATION */ my_pool->pending_pool = NULL; my_pool->pending_head = NULL; my_pool->pending_tail = NULL; @@ -396,6 +425,11 @@ void PacketPoolDestroy(void) } SC_ATOMIC_DESTROY(my_pool->return_stack.sync_now); + +#ifdef DEBUG_VALIDATION + my_pool->initialized = 0; + my_pool->destroyed = 1; +#endif /* DEBUG_VALIDATION */ } Packet *TmqhInputPacketpool(ThreadVars *tv) diff --git a/src/tmqh-packetpool.h b/src/tmqh-packetpool.h index 12a3f5cabd..ab45184c65 100644 --- a/src/tmqh-packetpool.h +++ b/src/tmqh-packetpool.h @@ -38,7 +38,7 @@ typedef struct PktPoolLockedStack_{ } __attribute__((aligned(CLS))) PktPoolLockedStack; typedef struct PktPool_ { - /* link listed of free packets local to this thread. + /* link listed of free packets local to this thread. * No mutex is needed. */ Packet *head; @@ -51,8 +51,13 @@ typedef struct PktPool_ { Packet *pending_head; Packet *pending_tail; uint32_t pending_count; - - /* All members above this point are accessed locally by only one thread, so + +#ifdef DEBUG_VALIDATION + int initialized; + int destroyed; +#endif /* DEBUG_VALIDATION */ + + /* All members above this point are accessed locally by only one thread, so * these should live on their own cache line. */ @@ -60,7 +65,6 @@ typedef struct PktPool_ { * to this thread. */ PktPoolLockedStack return_stack; - } PktPool; Packet *TmqhInputPacketpool(ThreadVars *);