#include "util-hash.h"
+#define TC_ADDRESS 0
+#define TC_SID 1
+#define TC_GID 2
+#define TC_REV 3
+#define TC_TENANT 4
+
typedef struct ThresholdCacheItem {
int8_t track; // by_src/by_dst
int8_t ipv;
int8_t retval;
- uint32_t addr;
- uint32_t sid;
+ uint32_t key[5];
SCTime_t expires_at;
RB_ENTRY(ThresholdCacheItem) rb;
} ThresholdCacheItem;
static uint32_t ThresholdCacheHashFunc(HashTable *ht, void *data, uint16_t datalen)
{
- ThresholdCacheItem *tci = data;
- int hash = tci->ipv * tci->track + tci->addr + tci->sid;
+ ThresholdCacheItem *e = data;
+ uint32_t hash = hashword(e->key, sizeof(e->key) / sizeof(uint32_t), 0) * (e->ipv + e->track);
hash = hash % ht->array_size;
return hash;
}
{
ThresholdCacheItem *tci1 = data1;
ThresholdCacheItem *tci2 = data2;
- return tci1->ipv == tci2->ipv && tci1->track == tci2->track && tci1->addr == tci2->addr &&
- tci1->sid == tci2->sid;
+ return tci1->ipv == tci2->ipv && tci1->track == tci2->track &&
+ memcmp(tci1->key, tci2->key, sizeof(tci1->key)) == 0;
}
static void ThresholdCacheHashFreeFunc(void *data)
/// \brief Thread local cache
static int SetupCache(const Packet *p, const int8_t track, const int8_t retval, const uint32_t sid,
- SCTime_t expires)
+ const uint32_t gid, const uint32_t rev, SCTime_t expires)
{
if (!threshold_cache_ht) {
threshold_cache_ht = HashTableInit(256, ThresholdCacheHashFunc,
.track = track,
.ipv = 4,
.retval = retval,
- .addr = addr,
- .sid = sid,
+ .key[TC_ADDRESS] = addr,
+ .key[TC_SID] = sid,
+ .key[TC_GID] = gid,
+ .key[TC_REV] = rev,
+ .key[TC_TENANT] = p->tenant_id,
.expires_at = expires,
};
ThresholdCacheItem *found = HashTableLookup(threshold_cache_ht, &lookup, 0);
n->track = track;
n->ipv = 4;
n->retval = retval;
- n->addr = addr;
- n->sid = sid;
+ n->key[TC_ADDRESS] = addr;
+ n->key[TC_SID] = sid;
+ n->key[TC_GID] = gid;
+ n->key[TC_REV] = rev;
+ n->key[TC_TENANT] = p->tenant_id;
n->expires_at = expires;
if (HashTableAdd(threshold_cache_ht, n, 0) == 0) {
* \retval -4 error - unsupported tracker
* \retval ret cached return code
*/
-static int CheckCache(const Packet *p, const int8_t track, const uint32_t sid)
+static int CheckCache(const Packet *p, const int8_t track, const uint32_t sid, const uint32_t gid,
+ const uint32_t rev)
{
cache_lookup_cnt++;
ThresholdCacheItem lookup = {
.track = track,
.ipv = 4,
- .addr = addr,
- .sid = sid,
+ .key[TC_ADDRESS] = addr,
+ .key[TC_SID] = sid,
+ .key[TC_GID] = gid,
+ .key[TC_REV] = rev,
+ .key[TC_TENANT] = p->tenant_id,
};
ThresholdCacheItem *found = HashTableLookup(threshold_cache_ht, &lookup, 0);
if (found) {
static int ThresholdCheckUpdate(const DetectThresholdData *td, ThresholdEntry *te,
const Packet *p, // ts only? - cache too
- const uint32_t sid, PacketAlert *pa)
+ const uint32_t sid, const uint32_t gid, const uint32_t rev, PacketAlert *pa)
{
int ret = 0;
const SCTime_t packet_time = p->ts;
ret = 2;
if (PacketIsIPv4(p)) {
- SetupCache(p, td->track, (int8_t)ret, sid, entry);
+ SetupCache(p, td->track, (int8_t)ret, sid, gid, rev, entry);
}
}
} else {
ret = 2;
if (PacketIsIPv4(p)) {
- SetupCache(p, td->track, (int8_t)ret, sid, entry);
+ SetupCache(p, td->track, (int8_t)ret, sid, gid, rev, entry);
}
}
} else {
r = ThresholdSetup(td, te, p->ts, s->id, s->gid, s->rev, p->tenant_id);
} else {
// existing, check/update
- r = ThresholdCheckUpdate(td, te, p, s->id, pa);
+ r = ThresholdCheckUpdate(td, te, p, s->id, s->gid, s->rev, pa);
}
(void)THashDecrUsecnt(res.data);
}
} else {
// existing, check/update
- ret = ThresholdCheckUpdate(td, found, p, sid, pa);
+ ret = ThresholdCheckUpdate(td, found, p, sid, gid, rev, pa);
}
return ret;
}
ret = ThresholdHandlePacketSuppress(p,td,s->id,s->gid);
} else if (td->track == TRACK_SRC) {
if (PacketIsIPv4(p) && (td->type == TYPE_LIMIT || td->type == TYPE_BOTH)) {
- int cache_ret = CheckCache(p, td->track, s->id);
+ int cache_ret = CheckCache(p, td->track, s->id, s->gid, s->rev);
if (cache_ret >= 0) {
SCReturnInt(cache_ret);
}
ret = ThresholdGetFromHash(&ctx, p, s, td, pa);
} else if (td->track == TRACK_DST) {
if (PacketIsIPv4(p) && (td->type == TYPE_LIMIT || td->type == TYPE_BOTH)) {
- int cache_ret = CheckCache(p, td->track, s->id);
+ int cache_ret = CheckCache(p, td->track, s->id, s->gid, s->rev);
if (cache_ret >= 0) {
SCReturnInt(cache_ret);
}