Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packet Pool fixes #1065

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/tm-threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ void *TmThreadsSlot1NoIn(void *td)
}
} /* while (run) */

PacketPoolDestroy();

TmThreadsSetFlag(tv, THV_RUNNING_DONE);
TmThreadWaitForFlag(tv, THV_DEINIT);

Expand Down Expand Up @@ -305,6 +307,8 @@ void *TmThreadsSlot1NoOut(void *td)
}
} /* while (run) */

PacketPoolDestroy();

TmThreadsSetFlag(tv, THV_RUNNING_DONE);
TmThreadWaitForFlag(tv, THV_DEINIT);

Expand Down Expand Up @@ -394,6 +398,8 @@ void *TmThreadsSlot1NoInOut(void *td)
}
} /* while (run) */

PacketPoolDestroy();

TmThreadsSetFlag(tv, THV_RUNNING_DONE);
TmThreadWaitForFlag(tv, THV_DEINIT);

Expand Down Expand Up @@ -523,6 +529,8 @@ void *TmThreadsSlot1(void *td)
}
} /* while (run) */

PacketPoolDestroy();

TmThreadsSetFlag(tv, THV_RUNNING_DONE);
TmThreadWaitForFlag(tv, THV_DEINIT);

Expand Down Expand Up @@ -722,6 +730,8 @@ void *TmThreadsSlotPktAcqLoop(void *td) {
}
SCPerfSyncCounters(tv);

PacketPoolDestroy();

TmThreadsSetFlag(tv, THV_RUNNING_DONE);
TmThreadWaitForFlag(tv, THV_DEINIT);

Expand Down
49 changes: 23 additions & 26 deletions src/tmqh-packetpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,12 @@ static inline PktPool *GetThreadPacketPool(void)
{
return &thread_pkt_pool;
}

static inline PktPool *ThreadPacketPoolCreate(void)
{
/* Nothing to do since __thread statically allocates the memory. */
return GetThreadPacketPool();
}
#else
/* __thread not supported. */
static pthread_key_t pkt_pool_thread_key;
static SCMutex pkt_pool_thread_key_mutex = SCMUTEX_INITIALIZER;
static int pkt_pool_thread_key_initialized = 0;

static inline PktPool *GetThreadPacketPool(void)
{
return (PktPool*)pthread_getspecific(pkt_pool_thread_key);
}

static void PktPoolThreadDestroy(void * buf)
{
free(buf);
Expand Down Expand Up @@ -102,15 +91,10 @@ static void TmqhPacketpoolInit(void)
SCMutexUnlock(&pkt_pool_thread_key_mutex);
}

static inline PktPool *ThreadPacketPoolCreate(void)
static PktPool *ThreadPacketPoolCreate(void)
{
TmqhPacketpoolInit();

/* Check that the pool is not already created */
PktPool *pool = GetThreadPacketPool();
if (pool)
return pool;

/* Create a new pool for this thread. */
pool = (PktPool*)SCMallocAligned(sizeof(PktPool), CLS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed undeclared 'pool' variable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I obviously didn’t check the non-TLS case. Sigh.

From: Victor Julien [mailto:notifications@github.com]
Sent: Wednesday, July 30, 2014 5:56 AM
To: inliniac/suricata
Cc: Kenneth Steele
Subject: Re: [suricata] Packet Pool fixes (#1065)

In src/tmqh-packetpool.c:

{

 TmqhPacketpoolInit();
  • /* Check that the pool is not already created */
  • PktPool *pool = GetThreadPacketPool();
  • if (pool)
  •    return pool;
    

 /* Create a new pool for this thread. */

 pool = (PktPool*)SCMallocAligned(sizeof(PktPool), CLS);

I fixed undeclared 'pool' variable here.


Reply to this email directly or view it on GitHubhttps://github.com//pull/1065/files#r15574878.

if (pool == NULL) {
Expand All @@ -125,6 +109,15 @@ static inline PktPool *ThreadPacketPoolCreate(void)

return pool;
}

static inline PktPool *GetThreadPacketPool(void)
{
PktPool* pool = (PktPool*)pthread_getspecific(pkt_pool_thread_key);
if (pool == NULL)
pool = ThreadPacketPoolCreate();

return pool;
}
#endif

/**
Expand Down Expand Up @@ -168,6 +161,15 @@ static void PacketPoolStorePacket(Packet *p)
PacketPoolReturnPacket(p);
}

static void PacketPoolGetReturnedPackets(PktPool *pool)
{
SCMutexLock(&pool->return_stack.mutex);
/* Move all the packets from the locked return stack to the local stack. */
pool->head = pool->return_stack.head;
pool->return_stack.head = NULL;
SCMutexUnlock(&pool->return_stack.mutex);
}

/** \brief Get a new packet from the packet pool
*
* Only allocates from the thread's local stack, or mallocs new packets.
Expand All @@ -189,11 +191,7 @@ Packet *PacketPoolGetPacket(void)

/* Local Stack is empty, so check the return stack, which requires
* locking. */
SCMutexLock(&pool->return_stack.mutex);
/* Move all the packets from the locked return stack to the local stack. */
pool->head = pool->return_stack.head;
pool->return_stack.head = NULL;
SCMutexUnlock(&pool->return_stack.mutex);
PacketPoolGetReturnedPackets(pool);

/* Try to allocate again. Need to check for not empty again, since the
* return stack might have been empty too.
Expand Down Expand Up @@ -266,7 +264,7 @@ void PacketPoolInit(void)
{
extern intmax_t max_pending_packets;

PktPool *my_pool = ThreadPacketPoolCreate();
PktPool *my_pool = GetThreadPacketPool();

SCMutexInit(&my_pool->return_stack.mutex, NULL);

Expand All @@ -286,14 +284,13 @@ void PacketPoolInit(void)
max_pending_packets, (uintmax_t)(max_pending_packets*SIZE_OF_PACKET));
}

void PacketPoolDestroy(void) {
#if 0
void PacketPoolDestroy(void)
{
Packet *p = NULL;
while ((p = PacketPoolGetPacket()) != NULL) {
PACKET_CLEANUP(p);
SCFree(p);
}
#endif
}

Packet *TmqhInputPacketpool(ThreadVars *tv)
Expand Down