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

102 lines
3.3 KiB
C

#include "vips.h"
#include "packet-queue.h"
#include "decode.h"
#include "threads.h"
#include "threadvars.h"
#include "tm-queuehandlers.h"
Packet *TmqhInputPacketpool(ThreadVars *t);
void TmqhOutputPacketpool(ThreadVars *t, Packet *p);
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();
mutex_lock(&mutex_pending);
17 years ago
if (pending > MAX_PENDING) {
pthread_cond_wait(&cond_pending, &mutex_pending);
17 years ago
}
mutex_unlock(&mutex_pending);
return p;
}
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
{
PacketQueue *q = &packet_q;
17 years ago
char proot = 0;
if (IS_TUNNEL_PKT(p)) {
17 years ago
//printf("TmqhOutputPacketpool: tunnel packet: %p %s\n", p,p->root ? "upper layer":"root");
17 years ago
/* get a lock */
pthread_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
mutex_lock(m);
if (IS_TUNNEL_ROOT_PKT(p)) {
17 years ago
//printf("TmqhOutputPacketpool: IS_TUNNEL_ROOT_PKT\n");
17 years ago
if (TUNNEL_PKT_TPR(p) == 0) {
17 years ago
//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 {
17 years ago
//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;
mutex_unlock(m);
return;
}
} else {
17 years ago
//printf("TmqhOutputPacketpool: NOT IS_TUNNEL_ROOT_PKT\n");
17 years ago
if (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 */
17 years ago
//printf("TmqhOutputPacketpool: calling PacketEnqueue for root pkt\n");
17 years ago
proot = 1;
/* fall through */
} else {
17 years ago
//printf("TmqhOutputPacketpool: NOT p->root->tunnel_verdicted == 1 && TUNNEL_PKT_TPR(p) == 1 (%u)\n", TUNNEL_PKT_TPR(p));
17 years ago
TUNNEL_DECR_PKT_TPR_NOLOCK(p);
/* fall through */
}
}
mutex_unlock(m);
17 years ago
//printf("TmqhOutputPacketpool: tunnel stuff done, move on\n");
17 years ago
}
mutex_lock(&q->mutex_q);
17 years ago
if (proot) PacketEnqueue(q, p->root);
PacketEnqueue(q, p);
mutex_unlock(&q->mutex_q);
mutex_lock(&mutex_pending);
17 years ago
if (pending) {
pending--;
} else {
printf("TmqhOutputPacketpool: warning, trying to subtract from 0 pending counter.\n");
}
if (pending <= MAX_PENDING)
pthread_cond_signal(&cond_pending);
mutex_unlock(&mutex_pending);
}