stream-tcp: unify ssn clean up functions

There were 2 separate function doing ssn cleanup. To prevent issues
common with code duplication, unify them.
pull/2007/head
Victor Julien 10 years ago
parent 9b08cdae74
commit 93fa291922

@ -166,33 +166,25 @@ int StreamTcpCheckMemcap(uint64_t size)
} }
/** /**
* \brief Function to return the stream back to the pool. It returns the * \brief Session cleanup function. Does not free the ssn.
* segments in the stream to the segment pool. * \param ssn tcp session
*
* This function is called when the flow is destroyed, so it should free
* *everything* related to the tcp session. So including the app layer
* data. We are guaranteed to only get here when the flow's use_cnt is 0.
*
* \param ssn Void ptr to the ssn.
*/ */
void StreamTcpSessionClear(void *ssnptr) void StreamTcpSessionCleanup(TcpSession *ssn)
{ {
SCEnter(); SCEnter();
StreamMsg *smsg = NULL; StreamMsg *smsg = NULL;
TcpStateQueue *q, *q_next; TcpStateQueue *q, *q_next;
TcpSession *ssn = (TcpSession *)ssnptr;
if (ssn == NULL) if (ssn == NULL)
SCReturn; return;
StreamTcpReturnStreamSegments(&ssn->client);
StreamTcpReturnStreamSegments(&ssn->server);
//AppLayerParserCleanupState(ssn);
StreamTcpSackFreeList(&ssn->client); StreamTcpSackFreeList(&ssn->client);
StreamTcpSackFreeList(&ssn->server); StreamTcpSackFreeList(&ssn->server);
StreamTcpReturnStreamSegments(&ssn->client);
StreamTcpReturnStreamSegments(&ssn->server);
/* if we have (a) smsg(s), return to the pool */ /* if we have (a) smsg(s), return to the pool */
smsg = ssn->toserver_smsg_head; smsg = ssn->toserver_smsg_head;
while(smsg != NULL) { while(smsg != NULL) {
@ -226,6 +218,28 @@ void StreamTcpSessionClear(void *ssnptr)
ssn->queue = NULL; ssn->queue = NULL;
ssn->queue_len = 0; ssn->queue_len = 0;
SCReturn;
}
/**
* \brief Function to return the stream back to the pool. It returns the
* segments in the stream to the segment pool.
*
* This function is called when the flow is destroyed, so it should free
* *everything* related to the tcp session. So including the app layer
* data. We are guaranteed to only get here when the flow's use_cnt is 0.
*
* \param ssn Void ptr to the ssn.
*/
void StreamTcpSessionClear(void *ssnptr)
{
SCEnter();
TcpSession *ssn = (TcpSession *)ssnptr;
if (ssn == NULL)
return;
StreamTcpSessionCleanup(ssn);
memset(ssn, 0, sizeof(TcpSession)); memset(ssn, 0, sizeof(TcpSession));
PoolThreadReturn(ssn_pool, ssn); PoolThreadReturn(ssn_pool, ssn);
#ifdef DEBUG #ifdef DEBUG
@ -264,7 +278,7 @@ void StreamTcpSessionPktFree (Packet *p)
/** \brief Stream alloc function for the Pool /** \brief Stream alloc function for the Pool
* \retval ptr void ptr to TcpSession structure with all vars set to 0/NULL * \retval ptr void ptr to TcpSession structure with all vars set to 0/NULL
*/ */
void *StreamTcpSessionPoolAlloc() static void *StreamTcpSessionPoolAlloc()
{ {
void *ptr = NULL; void *ptr = NULL;
@ -278,7 +292,7 @@ void *StreamTcpSessionPoolAlloc()
return ptr; return ptr;
} }
int StreamTcpSessionPoolInit(void *data, void* initdata) static int StreamTcpSessionPoolInit(void *data, void* initdata)
{ {
memset(data, 0, sizeof(TcpSession)); memset(data, 0, sizeof(TcpSession));
StreamTcpIncrMemuse((uint64_t)sizeof(TcpSession)); StreamTcpIncrMemuse((uint64_t)sizeof(TcpSession));
@ -286,55 +300,15 @@ int StreamTcpSessionPoolInit(void *data, void* initdata)
return 1; return 1;
} }
/** \brief Pool free function /** \brief Pool cleanup function
* \param s Void ptr to TcpSession memory */ * \param s Void ptr to TcpSession memory */
void StreamTcpSessionPoolCleanup(void *s) static void StreamTcpSessionPoolCleanup(void *s)
{ {
StreamMsg *smsg = NULL; if (s != NULL) {
TcpStateQueue *q, *q_next; StreamTcpSessionCleanup(s);
/** \todo not very clean, as the memory is not freed here */
if (s == NULL) StreamTcpDecrMemuse((uint64_t)sizeof(TcpSession));
return;
TcpSession *ssn = (TcpSession *)s;
StreamTcpReturnStreamSegments(&ssn->client);
StreamTcpReturnStreamSegments(&ssn->server);
/* if we have (a) smsg(s), return to the pool */
smsg = ssn->toserver_smsg_head;
while(smsg != NULL) {
StreamMsg *smsg_next = smsg->next;
SCLogDebug("returning smsg %p to pool", smsg);
smsg->next = NULL;
smsg->prev = NULL;
StreamMsgReturnToPool(smsg);
smsg = smsg_next;
} }
ssn->toserver_smsg_head = NULL;
smsg = ssn->toclient_smsg_head;
while(smsg != NULL) {
StreamMsg *smsg_next = smsg->next;
SCLogDebug("returning smsg %p to pool", smsg);
smsg->next = NULL;
smsg->prev = NULL;
StreamMsgReturnToPool(smsg);
smsg = smsg_next;
}
ssn->toclient_smsg_head = NULL;
q = ssn->queue;
while (q != NULL) {
q_next = q->next;
SCFree(q);
q = q_next;
StreamTcpDecrMemuse((uint64_t)sizeof(TcpStateQueue));
}
ssn->queue = NULL;
ssn->queue_len = 0;
StreamTcpDecrMemuse((uint64_t)sizeof(TcpSession));
} }
/** \brief To initialize the stream global configuration data /** \brief To initialize the stream global configuration data

@ -225,7 +225,11 @@ TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **);
TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data); TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data);
int StreamTcpPacket (ThreadVars *tv, Packet *p, StreamTcpThread *stt, int StreamTcpPacket (ThreadVars *tv, Packet *p, StreamTcpThread *stt,
PacketQueue *pq); PacketQueue *pq);
/* clear ssn and return to pool */
void StreamTcpSessionClear(void *ssnptr); void StreamTcpSessionClear(void *ssnptr);
/* cleanup ssn, but don't free ssn */
void StreamTcpSessionCleanup(TcpSession *ssn);
uint32_t StreamTcpGetStreamSize(TcpStream *stream); uint32_t StreamTcpGetStreamSize(TcpStream *stream);
#endif /* __STREAM_TCP_H__ */ #endif /* __STREAM_TCP_H__ */

Loading…
Cancel
Save