You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/tmqh-packetpool.c

195 lines
5.9 KiB
C

/* Copyright (C) 2007-2010 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Packetpool queue handlers
*/
#include "suricata.h"
#include "packet-queue.h"
#include "decode.h"
#include "detect.h"
#include "detect-uricontent.h"
#include "threads.h"
#include "threadvars.h"
#include "flow.h"
#include "tm-queuehandlers.h"
#include "pkt-var.h"
#include "tmqh-packetpool.h"
extern int max_pending_packets;
void TmqhPacketpoolRegister (void) {
tmqh_table[TMQH_PACKETPOOL].name = "packetpool";
tmqh_table[TMQH_PACKETPOOL].InHandler = TmqhInputPacketpool;
tmqh_table[TMQH_PACKETPOOL].OutHandler = TmqhOutputPacketpool;
}
Packet *TmqhInputPacketpool(ThreadVars *t)
{
/* XXX */
Packet *p = SetupPkt();
#if 0
SCMutexLock(&mutex_pending);
pending++;
//printf("PcapFileCallback: pending %" PRIu32 "\n", pending);
#ifdef DBG_PERF
if (pending > dbg_maxpending)
dbg_maxpending = pending;
#endif /* DBG_PERF */
SCMutexUnlock(&mutex_pending);
#endif
17 years ago
/*
* Disabled because it can enter a 'wait' state, while
* keeping the nfq queue locked thus making it impossble
* to free packets, the exact condition we are waiting
* for. VJ 09-01-16
*
SCMutexLock(&mutex_pending);
17 years ago
if (pending > MAX_PENDING) {
SCondWait(&cond_pending, &mutex_pending);
17 years ago
}
SCMutexUnlock(&mutex_pending);
17 years ago
*/
return p;
}
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
{
PacketQueue *q = &packet_q;
17 years ago
char proot = 0;
if (p == NULL)
return;
17 years ago
if (IS_TUNNEL_PKT(p)) {
//printf("TmqhOutputPacketpool: tunnel packet: %p %s\n", p,p->root ? "upper layer":"root");
17 years ago
/* get a lock */
SCMutex *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
SCMutexLock(m);
17 years ago
if (IS_TUNNEL_ROOT_PKT(p)) {
//printf("TmqhOutputPacketpool: IS_TUNNEL_ROOT_PKT\n");
17 years ago
if (TUNNEL_PKT_TPR(p) == 0) {
//printf("TmqhOutputPacketpool: TUNNEL_PKT_TPR(p) == 0\n");
17 years ago
/* if this packet is the root and there are no
* more tunnel packets, enqueue it */
/* fall through */
} else {
//printf("TmqhOutputPacketpool: TUNNEL_PKT_TPR(p) > 0\n");
17 years ago
/* if this is the root and there are more tunnel
* packets, don't add this. It's still referenced
* by the tunnel packets, and we will enqueue it
* when we handle them */
p->tunnel_verdicted = 1;
SCMutexUnlock(m);
17 years ago
return;
}
} else {
//printf("TmqhOutputPacketpool: NOT IS_TUNNEL_ROOT_PKT\n");
/* the p->root != NULL here seems unnecessary: IS_TUNNEL_PKT checks
* that p->tunnel_pkt == 1, IS_TUNNEL_ROOT_PKT checks that +
* p->root == NULL. So when we are here p->root can only be
* non-NULL, right? CLANG thinks differently. May be a FP, but
* better safe than sorry. VJ */
if (p->root != NULL && p->root->tunnel_verdicted == 1 &&
TUNNEL_PKT_TPR(p) == 1)
{
17 years ago
//printf("TmqhOutputPacketpool: p->root->tunnel_verdicted == 1 && TUNNEL_PKT_TPR(p) == 1\n");
17 years ago
/* the root is ready and we are the last tunnel packet,
* lets enqueue them both. */
TUNNEL_DECR_PKT_TPR_NOLOCK(p);
/* handle the root */
//printf("TmqhOutputPacketpool: calling PacketEnqueue for root pkt, p->root %p (%p)\n", p->root, p);
17 years ago
proot = 1;
/* fall through */
} else {
//printf("TmqhOutputPacketpool: NOT p->root->tunnel_verdicted == 1 && TUNNEL_PKT_TPR(p) == 1 (%" PRIu32 ")\n", TUNNEL_PKT_TPR(p));
17 years ago
TUNNEL_DECR_PKT_TPR_NOLOCK(p);
/* fall through */
}
}
SCMutexUnlock(m);
//printf("TmqhOutputPacketpool: tunnel stuff done, move on\n");
17 years ago
}
FlowDecrUsecnt(t,p);
if (proot && p->root != NULL) {
if (p->root->flags & PKT_ALLOC) {
SCMutexDestroy(&p->root->mutex_rtv_cnt);
CLEAR_PACKET(p->root);
SCFree(p->root);
} else {
SCMutexLock(&q->mutex_q);
PacketEnqueue(q, p->root);
SCCondSignal(&q->cond_q);
SCMutexUnlock(&q->mutex_q);
}
}
if (p->flags & PKT_ALLOC) {
SCMutexDestroy(&p->mutex_rtv_cnt);
CLEAR_PACKET(p);
SCFree(p);
} else {
CLEAR_PACKET(p);
SCMutexLock(&q->mutex_q);
PacketEnqueue(q, p);
SCCondSignal(&q->cond_q);
SCMutexUnlock(&q->mutex_q);
}
}
/**
* \brief Release all the packets in the queue back to the packetpool. Mainly
* used by threads that have failed, and wants to return the packets back
* to the packetpool.
*
* \param pq Pointer to the packetqueue from which the packets have to be
* returned back to the packetpool
*/
void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
{
Packet *p = NULL;
if (pq == NULL)
return;
SCMutexLock(&pq->mutex_q);
while ( (p = PacketDequeue(pq)) != NULL)
TmqhOutputPacketpool(NULL, p);
SCMutexUnlock(&pq->mutex_q);
return;
}