mirror of https://github.com/OISF/suricata
Lockfree ringbuffer wip.
parent
7f29166aa8
commit
a48a767efc
@ -0,0 +1,65 @@
|
||||
/* 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>
|
||||
*
|
||||
* RingBuffer queue handler
|
||||
*/
|
||||
|
||||
#include "suricata.h"
|
||||
#include "packet-queue.h"
|
||||
#include "decode.h"
|
||||
#include "threads.h"
|
||||
#include "threadvars.h"
|
||||
|
||||
#include "tm-queuehandlers.h"
|
||||
|
||||
#include "util-ringbuffer.h"
|
||||
|
||||
static RingBufferMrSw *ringbuffers[256];
|
||||
|
||||
Packet *TmqhInputRingBuffer(ThreadVars *t);
|
||||
void TmqhOutputRingBuffer(ThreadVars *t, Packet *p);
|
||||
|
||||
void TmqhRingBufferRegister (void) {
|
||||
tmqh_table[TMQH_RINGBUFFER].name = "ringbuffer";
|
||||
tmqh_table[TMQH_RINGBUFFER].InHandler = TmqhInputRingBuffer;
|
||||
tmqh_table[TMQH_RINGBUFFER].OutHandler = TmqhOutputRingBuffer;
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < 256; i++) {
|
||||
ringbuffers[i] = RingBufferMrSwInit();
|
||||
}
|
||||
}
|
||||
|
||||
Packet *TmqhInputRingBuffer(ThreadVars *t)
|
||||
{
|
||||
RingBufferMrSw *rb = ringbuffers[t->inq->id];
|
||||
|
||||
Packet *p = (Packet *)RingBufferMrSwGet(rb);
|
||||
return p;
|
||||
}
|
||||
|
||||
void TmqhOutputRingBuffer(ThreadVars *t, Packet *p)
|
||||
{
|
||||
RingBufferMrSw *rb = ringbuffers[t->outq->id];
|
||||
RingBufferMrSwPut(rb, (void *)p);
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
/* 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>
|
||||
*/
|
||||
|
||||
#ifndef __TMQH_RINGBUFFER_H__
|
||||
#define __TMQH_RINGBUFFER_H__
|
||||
|
||||
void TmqhRingBufferRegister (void);
|
||||
|
||||
#endif /* __TMQH_RINGBUFFER_H__ */
|
@ -0,0 +1,107 @@
|
||||
#include "suricata-common.h"
|
||||
#include "util-ringbuffer.h"
|
||||
|
||||
/* Multi Reader, Single Writer */
|
||||
|
||||
RingBufferMrSw *RingBufferMrSwInit(void) {
|
||||
RingBufferMrSw *rb = SCMalloc(sizeof(RingBufferMrSw));
|
||||
if (rb == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(rb, 0x00, sizeof(RingBufferMrSw));
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
void RingBufferMrSwDestroy(RingBufferMrSw *rb) {
|
||||
if (rb == NULL) {
|
||||
SCFree(rb);
|
||||
}
|
||||
}
|
||||
|
||||
void *RingBufferMrSwGet(RingBufferMrSw *rb) {
|
||||
void *ptr;
|
||||
/* counter for data races. If __sync_bool_compare_and_swap (CAS) fails,
|
||||
* we increase cnt, get a new ptr and try to do CAS again. We init it to
|
||||
* -1 so it's 0 when first used the do { } while() loop. */
|
||||
unsigned short readp = -1;
|
||||
/* buffer is empty, wait... */
|
||||
retry:
|
||||
while (rb->read == rb->write) {
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
/* atomically update rb->read */
|
||||
readp += rb->read;
|
||||
do {
|
||||
/* with multiple readers we can get in the situation that we exitted
|
||||
* from the wait loop but the rb is empty again once we get here. */
|
||||
if (rb->read == rb->write)
|
||||
goto retry;
|
||||
|
||||
readp++;
|
||||
ptr = rb->array[readp];
|
||||
} while (!(__sync_bool_compare_and_swap(&rb->read, readp, (readp + 1))));
|
||||
|
||||
SCLogDebug("ptr %p", ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief put a ptr in the RingBuffer
|
||||
*/
|
||||
void RingBufferMrSwPut(RingBufferMrSw *rb, void *ptr) {
|
||||
SCLogDebug("ptr %p", ptr);
|
||||
|
||||
/* buffer is full, wait... */
|
||||
while ((rb->write + 1) == rb->read) {
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
rb->array[rb->write] = ptr;
|
||||
__sync_fetch_and_add(&rb->write, 1);
|
||||
}
|
||||
|
||||
|
||||
/* Single Reader, Single Writer */
|
||||
|
||||
RingBufferSrSw *RingBufferSrSwInit(void) {
|
||||
RingBufferSrSw *rb = SCMalloc(sizeof(RingBufferSrSw));
|
||||
if (rb == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(rb, 0x00, sizeof(RingBufferSrSw));
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
void RingBufferSrSwDestroy(RingBufferSrSw *rb) {
|
||||
if (rb == NULL) {
|
||||
SCFree(rb);
|
||||
}
|
||||
}
|
||||
|
||||
void *RingBufferSrSwGet(RingBufferSrSw *rb) {
|
||||
void *ptr = NULL;
|
||||
|
||||
/* buffer is empty, wait... */
|
||||
while (rb->read == rb->write) {
|
||||
}
|
||||
|
||||
ptr = rb->array[rb->read];
|
||||
__sync_fetch_and_add(&rb->read, 1);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void RingBufferSrSwPut(RingBufferSrSw *rb, void *ptr) {
|
||||
/* buffer is full, wait... */
|
||||
while ((rb->write + 1) == rb->read) {
|
||||
}
|
||||
|
||||
rb->array[rb->write] = ptr;
|
||||
__sync_fetch_and_add(&rb->write, 1);
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
#ifndef __UTIL_RINGBUFFER_H__
|
||||
|
||||
/** \brief ring buffer api
|
||||
*
|
||||
* Ring buffer api for a single writer and a single reader. It uses a
|
||||
* read and write pointer. Only the read ptr needs atomic updating.
|
||||
*/
|
||||
|
||||
#define RING_BUFFER_MRSW_SIZE 65536
|
||||
|
||||
/** Multiple Reader, Single Writer ring buffer, fixed at
|
||||
* 65536 items so we can use unsigned shorts that just
|
||||
* wrap around */
|
||||
typedef struct RingBufferMrSw_ {
|
||||
unsigned short write; /**< idx where we put data */
|
||||
unsigned short read; /**< idx where we read data */
|
||||
void *array[RING_BUFFER_MRSW_SIZE];
|
||||
} RingBufferMrSw;
|
||||
|
||||
void *RingBufferMrSwGet(RingBufferMrSw *);
|
||||
void RingBufferMrSwPut(RingBufferMrSw *, void *);
|
||||
RingBufferMrSw *RingBufferMrSwInit(void);
|
||||
void RingBufferMrSwDestroy(RingBufferMrSw *);
|
||||
|
||||
#define RING_BUFFER_SRSW_SIZE 65536
|
||||
|
||||
/** Single Reader, Single Writer ring buffer, fixed at
|
||||
* 65536 items so we can use unsigned shorts that just
|
||||
* wrap around */
|
||||
typedef struct RingBufferSrSw_ {
|
||||
unsigned short write; /**< idx where we put data */
|
||||
unsigned short read; /**< idx where we read data */
|
||||
void *array[RING_BUFFER_SRSW_SIZE];
|
||||
} RingBufferSrSw;
|
||||
|
||||
void *RingBufferSrSwGet(RingBufferSrSw *);
|
||||
void RingBufferSrSwPut(RingBufferSrSw *, void *);
|
||||
RingBufferSrSw *RingBufferSrSwInit(void);
|
||||
void RingBufferSrSwDestroy(RingBufferSrSw *);
|
||||
|
||||
#endif /* __UTIL_RINGBUFFER_H__ */
|
||||
|
Loading…
Reference in New Issue