diff --git a/src/decode.h b/src/decode.h index 111ab5fcf2..f685e2b268 100644 --- a/src/decode.h +++ b/src/decode.h @@ -382,10 +382,8 @@ typedef struct Packet_ uint64_t pcap_cnt; /* ready to set verdict counter, only set in root */ - uint8_t rtv_cnt; - /* tunnel packet ref count */ - uint8_t tpr_cnt; - SCMutex mutex_rtv_cnt; + SC_ATOMIC_DECLARE(unsigned short, tunnel_rtv_cnt); + SC_ATOMIC_DECLARE(unsigned short, tunnel_tpr_cnt); /* decoder events */ PacketDecoderEvents events; @@ -506,14 +504,16 @@ typedef struct DecodeThreadVars_ #ifndef __SC_CUDA_SUPPORT__ #define PACKET_INITIALIZE(p) { \ memset((p), 0x00, SIZE_OF_PACKET); \ - SCMutexInit(&(p)->mutex_rtv_cnt, NULL); \ + SC_ATOMIC_INIT(p->tunnel_rtv_cnt); \ + SC_ATOMIC_INIT(p->tunnel_tpr_cnt); \ PACKET_RESET_CHECKSUMS((p)); \ (p)->pkt = ((uint8_t *)(p)) + sizeof(Packet); \ } #else #define PACKET_INITIALIZE(p) { \ memset((p), 0x00, SIZE_OF_PACKET); \ - SCMutexInit(&(p)->mutex_rtv_cnt, NULL); \ + SC_ATOMIC_INIT(p->tunnel_rtv_cnt); \ + SC_ATOMIC_INIT(p->tunnel_tpr_cnt); \ PACKET_RESET_CHECKSUMS((p)); \ SCMutexInit(&(p)->cuda_mutex, NULL); \ SCCondInit(&(p)->cuda_cond, NULL); \ @@ -576,10 +576,8 @@ typedef struct DecodeThreadVars_ (p)->pktlen = 0; \ (p)->alerts.cnt = 0; \ (p)->pcap_cnt = 0; \ - (p)->rtv_cnt = 0; \ - (p)->tpr_cnt = 0; \ - SCMutexDestroy(&(p)->mutex_rtv_cnt); \ - SCMutexInit(&(p)->mutex_rtv_cnt, NULL); \ + SC_ATOMIC_RESET((p)->tunnel_rtv_cnt); \ + SC_ATOMIC_RESET((p)->tunnel_tpr_cnt); \ (p)->events.cnt = 0; \ (p)->next = NULL; \ (p)->prev = NULL; \ @@ -608,14 +606,16 @@ typedef struct DecodeThreadVars_ if ((p)->pktvar != NULL) { \ PktVarFree((p)->pktvar); \ } \ - SCMutexDestroy(&(p)->mutex_rtv_cnt); \ + SC_ATOMIC_DESTROY((p)->tunnel_rtv_cnt); \ + SC_ATOMIC_DESTROY((p)->tunnel_tpr_cnt); \ } while (0) #else #define PACKET_CLEANUP(p) do { \ if ((p)->pktvar != NULL) { \ PktVarFree((p)->pktvar); \ } \ - SCMutexDestroy(&(p)->mutex_rtv_cnt); \ + SC_ATOMIC_DESTROY((p)->tunnel_rtv_cnt); \ + SC_ATOMIC_DESTROY((p)->tunnel_tpr_cnt); \ SCMutexDestroy(&(p)->cuda_mutex); \ SCCondDestroy(&(p)->cuda_cond); \ } while(0) @@ -668,31 +668,27 @@ typedef struct DecodeThreadVars_ } while (0) #define TUNNEL_INCR_PKT_RTV(p) do { \ - SCMutexLock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ - ((p)->root ? (p)->root->rtv_cnt++ : (p)->rtv_cnt++); \ - SCMutexUnlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ + ((p)->root ? SC_ATOMIC_ADD((p)->root->tunnel_rtv_cnt, 1) : \ + SC_ATOMIC_ADD((p)->tunnel_rtv_cnt, 1)); \ } while (0) #define TUNNEL_INCR_PKT_TPR(p) do { \ - SCMutexLock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ - ((p)->root ? (p)->root->tpr_cnt++ : (p)->tpr_cnt++); \ - SCMutexUnlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ + ((p)->root ? SC_ATOMIC_ADD((p)->root->tunnel_tpr_cnt, 1) : \ + SC_ATOMIC_ADD((p)->tunnel_tpr_cnt, 1)); \ } while (0) #define TUNNEL_DECR_PKT_TPR(p) do { \ - SCMutexLock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ - ((p)->root ? (p)->root->tpr_cnt-- : (p)->tpr_cnt--); \ - SCMutexUnlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \ + ((p)->root ? SC_ATOMIC_SUB((p)->root->tunnel_tpr_cnt, 1) : \ + SC_ATOMIC_SUB((p)->tunnel_tpr_cnt, 1)); \ } while (0) -#define TUNNEL_DECR_PKT_TPR_NOLOCK(p) do { \ - ((p)->root ? (p)->root->tpr_cnt-- : (p)->tpr_cnt--); \ - } while (0) - -#define TUNNEL_PKT_RTV(p) ((p)->root ? (p)->root->rtv_cnt : (p)->rtv_cnt) -#define TUNNEL_PKT_TPR(p) ((p)->root ? (p)->root->tpr_cnt : (p)->tpr_cnt) +#define TUNNEL_PKT_RTV(p) ((p)->root ? \ + SC_ATOMIC_GET((p)->root->tunnel_rtv_cnt) : \ + SC_ATOMIC_GET((p)->tunnel_rtv_cnt)) +#define TUNNEL_PKT_TPR(p) ((p)->root ? \ + SC_ATOMIC_GET((p)->root->tunnel_tpr_cnt) : \ + SC_ATOMIC_GET((p)->tunnel_tpr_cnt)) -//#define IS_TUNNEL_ROOT_PKT(p) (((p)->root == NULL && (p)->tunnel_pkt == 1)) #define IS_TUNNEL_PKT(p) (((p)->flags & PKT_TUNNEL)) #define SET_TUNNEL_PKT(p) ((p)->flags |= PKT_TUNNEL) #define IS_TUNNEL_ROOT_PKT(p) (IS_TUNNEL_PKT(p) && (p)->root == NULL) diff --git a/src/source-ipfw.c b/src/source-ipfw.c index cc0af3481b..53c70976f0 100644 --- a/src/source-ipfw.c +++ b/src/source-ipfw.c @@ -549,16 +549,15 @@ TmEcode VerdictIPFW(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, Pack if (IS_TUNNEL_PKT(p)) { char verdict = 1; - pthread_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt; - SCMutexLock(m); /* if there are more tunnel packets than ready to verdict packets, * we won't verdict this one */ if (TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p)) { - SCLogDebug("VerdictIPFW: not ready to verdict yet: TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p) = %" PRId32 " > %" PRId32 "", TUNNEL_PKT_TPR(p), TUNNEL_PKT_RTV(p)); + SCLogDebug("VerdictIPFW: not ready to verdict yet: " + "TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p) = %" PRId32 + " > %" PRId32 "", TUNNEL_PKT_TPR(p), TUNNEL_PKT_RTV(p)); verdict = 0; } - SCMutexUnlock(m); /* don't verdict if we are not ready */ if (verdict == 1) { diff --git a/src/source-nfq.c b/src/source-nfq.c index 7f864e0d79..e87465f7b8 100644 --- a/src/source-nfq.c +++ b/src/source-nfq.c @@ -867,15 +867,14 @@ TmEcode VerdictNFQ(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, Packe char verdict = 1; //printf("VerdictNFQ: tunnel pkt: %p %s\n", p, p->root ? "upper layer" : "root"); - SCMutex *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt; - SCMutexLock(m); /* if there are more tunnel packets than ready to verdict packets, * we won't verdict this one */ if (TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p)) { - //printf("VerdictNFQ: not ready to verdict yet: TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p) = %" PRId32 " > %" PRId32 "\n", TUNNEL_PKT_TPR(p), TUNNEL_PKT_RTV(p)); + SCLogDebug("not ready to verdict yet: TUNNEL_PKT_TPR(p) > " + "TUNNEL_PKT_RTV(p) = %" PRId32 " > %" PRId32, + TUNNEL_PKT_TPR(p), TUNNEL_PKT_RTV(p)); verdict = 0; } - SCMutexUnlock(m); /* don't verdict if we are not ready */ if (verdict == 1) { diff --git a/src/tmqh-packetpool.c b/src/tmqh-packetpool.c index d9e169efae..b61f6ce09c 100644 --- a/src/tmqh-packetpool.c +++ b/src/tmqh-packetpool.c @@ -139,10 +139,6 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) SCLogDebug("Packet %p is a tunnel packet: %s", p,p->root ? "upper layer" : "tunnel root"); - /* get a lock */ - SCMutex *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt; - SCMutexLock(m); - if (IS_TUNNEL_ROOT_PKT(p)) { SCLogDebug("IS_TUNNEL_ROOT_PKT == TRUE"); if (TUNNEL_PKT_TPR(p) == 0) { @@ -158,7 +154,6 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) * by the tunnel packets, and we will enqueue it * when we handle them */ SET_TUNNEL_PKT_VERDICTED(p); - SCMutexUnlock(m); SCReturn; } } else { @@ -175,7 +170,7 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) SCLogDebug("p->root->tunnel_verdicted == 1 && TUNNEL_PKT_TPR(p) == 1"); /* the root is ready and we are the last tunnel packet, * lets enqueue them both. */ - TUNNEL_DECR_PKT_TPR_NOLOCK(p); + TUNNEL_DECR_PKT_TPR(p); /* handle the root */ SCLogDebug("calling PacketEnqueue for root pkt, p->root %p (tunnel packet %p)", p->root, p); @@ -186,12 +181,11 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) /* root not ready yet, so get rid of the tunnel pkt only */ SCLogDebug("NOT p->root->tunnel_verdicted == 1 && TUNNEL_PKT_TPR(p) == 1 (%" PRIu32 ")", TUNNEL_PKT_TPR(p)); - TUNNEL_DECR_PKT_TPR_NOLOCK(p); + TUNNEL_DECR_PKT_TPR(p); /* fall through */ } } - SCMutexUnlock(m); SCLogDebug("tunnel stuff done, move on (proot %d)", proot); }