suricata
util-thash.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "conf.h"
27 
28 #include "util-debug.h"
29 #include "util-thash.h"
30 
31 #include "util-random.h"
32 #include "util-misc.h"
33 #include "util-byte.h"
34 
35 #include "util-hash-lookup3.h"
36 #include "util-validate.h"
37 
38 static THashData *THashGetUsed(THashTableContext *ctx);
39 static void THashDataEnqueue (THashDataQueue *q, THashData *h);
40 
42 {
43  THashDataEnqueue(&ctx->spare_q, h);
44  (void) SC_ATOMIC_SUB(ctx->counter, 1);
45 }
46 
47 static THashDataQueue *THashDataQueueInit (THashDataQueue *q)
48 {
49  if (q != NULL) {
50  memset(q, 0, sizeof(THashDataQueue));
51  HQLOCK_INIT(q);
52  }
53  return q;
54 }
55 
57 {
59  if (q == NULL) {
60  SCLogError("Fatal error encountered in THashDataQueueNew. Exiting...");
61  exit(EXIT_SUCCESS);
62  }
63  q = THashDataQueueInit(q);
64  return q;
65 }
66 
67 /**
68  * \brief Destroy a queue
69  *
70  * \param q the queue to destroy
71  */
72 static void THashDataQueueDestroy (THashDataQueue *q)
73 {
74  HQLOCK_DESTROY(q);
75 }
76 
77 /**
78  * \brief add to queue
79  *
80  * \param q queue
81  * \param h data
82  */
83 static void THashDataEnqueue (THashDataQueue *q, THashData *h)
84 {
85 #ifdef DEBUG
86  BUG_ON(q == NULL || h == NULL);
87 #endif
88 
89  HQLOCK_LOCK(q);
90 
91  /* more data in queue */
92  if (q->top != NULL) {
93  h->next = q->top;
94  q->top->prev = h;
95  q->top = h;
96  /* only data */
97  } else {
98  q->top = h;
99  q->bot = h;
100  }
101  q->len++;
102 #ifdef DBG_PERF
103  if (q->len > q->dbg_maxlen)
104  q->dbg_maxlen = q->len;
105 #endif /* DBG_PERF */
106  HQLOCK_UNLOCK(q);
107 }
108 
109 /**
110  * \brief remove data from the queue
111  *
112  * \param q queue
113  *
114  * \retval h data or NULL if empty list.
115  */
116 static THashData *THashDataDequeue (THashDataQueue *q)
117 {
118  HQLOCK_LOCK(q);
119 
120  THashData *h = q->bot;
121  if (h == NULL) {
122  HQLOCK_UNLOCK(q);
123  return NULL;
124  }
125 
126  /* more packets in queue */
127  if (q->bot->prev != NULL) {
128  q->bot = q->bot->prev;
129  q->bot->next = NULL;
130  /* just the one we remove, so now empty */
131  } else {
132  q->top = NULL;
133  q->bot = NULL;
134  }
135 
136 #ifdef DEBUG
137  BUG_ON(q->len == 0);
138 #endif
139  if (q->len > 0)
140  q->len--;
141 
142  h->next = NULL;
143  h->prev = NULL;
144 
145  HQLOCK_UNLOCK(q);
146  return h;
147 }
148 
149 #if 0
150 static uint32_t THashDataQueueLen(THashDataQueue *q)
151 {
152  uint32_t len;
153  HQLOCK_LOCK(q);
154  len = q->len;
155  HQLOCK_UNLOCK(q);
156  return len;
157 }
158 #endif
159 
160 static THashData *THashDataAlloc(THashTableContext *ctx)
161 {
162  const size_t data_size = THASH_DATA_SIZE(ctx);
163 
164  if (!(THASH_CHECK_MEMCAP(ctx, data_size))) {
165  return NULL;
166  }
167 
168  (void) SC_ATOMIC_ADD(ctx->memuse, data_size);
169 
170  THashData *h = SCCalloc(1, data_size);
171  if (unlikely(h == NULL))
172  goto error;
173 
174  /* points to data right after THashData block */
175  h->data = (uint8_t *)h + sizeof(THashData);
176 
177 // memset(h, 0x00, data_size);
178 
179  SCMutexInit(&h->m, NULL);
180  SC_ATOMIC_INIT(h->use_cnt);
181  return h;
182 
183 error:
184  return NULL;
185 }
186 
187 static void THashDataFree(THashTableContext *ctx, THashData *h)
188 {
189  if (h != NULL) {
190  DEBUG_VALIDATE_BUG_ON(SC_ATOMIC_GET(h->use_cnt) != 0);
191 
192  if (h->data != NULL) {
193  ctx->config.DataFree(h->data);
194  }
195  SCMutexDestroy(&h->m);
196  SCFree(h);
197  (void) SC_ATOMIC_SUB(ctx->memuse, THASH_DATA_SIZE(ctx));
198  }
199 }
200 
201 #define THASH_DEFAULT_HASHSIZE 4096
202 #define THASH_DEFAULT_MEMCAP 16777216
203 #define THASH_DEFAULT_PREALLOC 1000
204 
205 #define GET_VAR(prefix,name) \
206  snprintf(varname, sizeof(varname), "%s.%s", (prefix), (name))
207 
208 /** \brief initialize the configuration
209  * \warning Not thread safe */
210 static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
211 {
212  char varname[256];
213 
214  SCLogDebug("initializing thash engine...");
215 
216  /* Check if we have memcap and hash_size defined at config */
217  const char *conf_val;
218  uint32_t configval = 0;
219 
220  /** set config values for memcap, prealloc and hash_size */
221  GET_VAR(cnf_prefix, "memcap");
222  if ((ConfGet(varname, &conf_val)) == 1)
223  {
224  if (ParseSizeStringU64(conf_val, &ctx->config.memcap) < 0) {
225  SCLogError("Error parsing %s "
226  "from conf file - %s. Killing engine",
227  varname, conf_val);
228  return -1;
229  }
230  }
231  GET_VAR(cnf_prefix, "hash-size");
232  if ((ConfGet(varname, &conf_val)) == 1)
233  {
234  if (StringParseUint32(&configval, 10, (uint16_t)strlen(conf_val), conf_val) > 0) {
235  ctx->config.hash_size = configval;
236  }
237  }
238 
239  GET_VAR(cnf_prefix, "prealloc");
240  if ((ConfGet(varname, &conf_val)) == 1)
241  {
242  if (StringParseUint32(&configval, 10, (uint16_t)strlen(conf_val), conf_val) > 0) {
243  ctx->config.prealloc = configval;
244  } else {
245  WarnInvalidConfEntry(varname, "%"PRIu32, ctx->config.prealloc);
246  }
247  }
248 
249  /* alloc hash memory */
250  uint64_t hash_size = ctx->config.hash_size * sizeof(THashHashRow);
251  if (!(THASH_CHECK_MEMCAP(ctx, hash_size))) {
252  SCLogError("allocating hash failed: "
253  "max hash memcap is smaller than projected hash size. "
254  "Memcap: %" PRIu64 ", Hash table size %" PRIu64 ". Calculate "
255  "total hash size by multiplying \"hash-size\" with %" PRIuMAX ", "
256  "which is the hash bucket size.",
257  ctx->config.memcap, hash_size, (uintmax_t)sizeof(THashHashRow));
258  return -1;
259  }
260  ctx->array = SCMallocAligned(ctx->config.hash_size * sizeof(THashHashRow), CLS);
261  if (unlikely(ctx->array == NULL)) {
262  SCLogError("Fatal error encountered in THashInitConfig. Exiting...");
263  return -1;
264  }
265  memset(ctx->array, 0, ctx->config.hash_size * sizeof(THashHashRow));
266 
267  uint32_t i = 0;
268  for (i = 0; i < ctx->config.hash_size; i++) {
269  HRLOCK_INIT(&ctx->array[i]);
270  }
271  (void) SC_ATOMIC_ADD(ctx->memuse, (ctx->config.hash_size * sizeof(THashHashRow)));
272 
273  /* pre allocate prealloc */
274  for (i = 0; i < ctx->config.prealloc; i++) {
275  if (!(THASH_CHECK_MEMCAP(ctx, THASH_DATA_SIZE(ctx)))) {
276  SCLogError("preallocating data failed: "
277  "max thash memcap reached. Memcap %" PRIu64 ", "
278  "Memuse %" PRIu64 ".",
279  ctx->config.memcap,
280  ((uint64_t)SC_ATOMIC_GET(ctx->memuse) + THASH_DATA_SIZE(ctx)));
281  return -1;
282  }
283 
284  THashData *h = THashDataAlloc(ctx);
285  if (h == NULL) {
286  SCLogError("preallocating data failed: %s", strerror(errno));
287  return -1;
288  }
289  THashDataEnqueue(&ctx->spare_q,h);
290  }
291 
292  return 0;
293 }
294 
295 THashTableContext *THashInit(const char *cnf_prefix, size_t data_size,
296  int (*DataSet)(void *, void *), void (*DataFree)(void *), uint32_t (*DataHash)(void *),
297  bool (*DataCompare)(void *, void *), bool reset_memcap, uint64_t memcap, uint32_t hashsize)
298 {
299  THashTableContext *ctx = SCCalloc(1, sizeof(*ctx));
300  BUG_ON(!ctx);
301 
302  ctx->config.data_size = data_size;
303  ctx->config.DataSet = DataSet;
304  ctx->config.DataFree = DataFree;
305  ctx->config.DataHash = DataHash;
306  ctx->config.DataCompare = DataCompare;
307 
308  /* set defaults */
309  ctx->config.hash_rand = (uint32_t)RandomGet();
311  /* Reset memcap in case of loading from file to the highest possible value
312  unless defined by the rule keyword */
313 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
314  // limit memcap size to default when fuzzing
316 #else
317  if (memcap > 0) {
318  ctx->config.memcap = memcap;
319  } else {
320  ctx->config.memcap = reset_memcap ? UINT64_MAX : THASH_DEFAULT_MEMCAP;
321  }
322 #endif
324 
325  SC_ATOMIC_INIT(ctx->counter);
326  SC_ATOMIC_INIT(ctx->memuse);
327  SC_ATOMIC_INIT(ctx->prune_idx);
328  THashDataQueueInit(&ctx->spare_q);
329 
330  if (THashInitConfig(ctx, cnf_prefix) < 0) {
331  THashShutdown(ctx);
332  ctx = NULL;
333  }
334  return ctx;
335 }
336 
337 /* \brief Set memcap to current memuse
338  * */
340 {
341  ctx->config.memcap = MAX(SC_ATOMIC_GET(ctx->memuse), ctx->config.memcap);
342  SCLogDebug("memcap after load set to: %" PRIu64, ctx->config.memcap);
343 }
344 
345 /** \brief shutdown the flow engine
346  * \warning Not thread safe */
348 {
349  THashData *h;
350 
351  /* free spare queue */
352  while ((h = THashDataDequeue(&ctx->spare_q))) {
353  BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
354  THashDataFree(ctx, h);
355  }
356 
357  /* clear and free the hash */
358  if (ctx->array != NULL) {
359  for (uint32_t u = 0; u < ctx->config.hash_size; u++) {
360  h = ctx->array[u].head;
361  while (h) {
362  THashData *n = h->next;
363  THashDataFree(ctx, h);
364  h = n;
365  }
366 
367  HRLOCK_DESTROY(&ctx->array[u]);
368  }
369  SCFreeAligned(ctx->array);
370  ctx->array = NULL;
371  }
372  (void) SC_ATOMIC_SUB(ctx->memuse, ctx->config.hash_size * sizeof(THashHashRow));
373  THashDataQueueDestroy(&ctx->spare_q);
374  SCFree(ctx);
375  return;
376 }
377 
378 /** \brief Walk the hash
379  *
380  */
381 int THashWalk(THashTableContext *ctx, THashFormatFunc FormatterFunc, THashOutputFunc OutputterFunc, void *output_ctx)
382 {
383  uint32_t u;
384 
385  if (ctx->array == NULL)
386  return -1;
387 
388  bool err = false;
389  for (u = 0; u < ctx->config.hash_size; u++) {
390  THashHashRow *hb = &ctx->array[u];
391  HRLOCK_LOCK(hb);
392  THashData *h = hb->head;
393  while (h) {
394  char output_string[1024] = "";
395  int size = FormatterFunc(h->data, output_string, sizeof(output_string));
396  if (size > 0) {
397  if (OutputterFunc(output_ctx, (const uint8_t *)output_string, size) < 0) {
398  err = true;
399  break;
400  }
401  }
402  h = h->next;
403  }
404  HRLOCK_UNLOCK(hb);
405  if (err == true)
406  return -1;
407  }
408  return 0;
409 }
410 
411 /** \brief Cleanup the thash engine
412  *
413  * Cleanup the thash engine from tag and threshold.
414  *
415  */
417 {
418  uint32_t u;
419 
420  if (ctx->array == NULL)
421  return;
422 
423  for (u = 0; u < ctx->config.hash_size; u++) {
424  THashHashRow *hb = &ctx->array[u];
425  HRLOCK_LOCK(hb);
426  THashData *h = hb->head;
427  while (h) {
428  if ((SC_ATOMIC_GET(h->use_cnt) > 0)) {
429  h = h->next;
430  } else {
431  THashData *n = h->next;
432  /* remove from the hash */
433  if (h->prev != NULL)
434  h->prev->next = h->next;
435  if (h->next != NULL)
436  h->next->prev = h->prev;
437  if (hb->head == h)
438  hb->head = h->next;
439  if (hb->tail == h)
440  hb->tail = h->prev;
441  h->next = NULL;
442  h->prev = NULL;
443  THashDataMoveToSpare(ctx, h);
444  h = n;
445  }
446  }
447  HRLOCK_UNLOCK(hb);
448  }
449  return;
450 }
451 
452 /* calculate the hash key for this packet
453  *
454  * we're using:
455  * hash_rand -- set at init time
456  * source address
457  */
458 static uint32_t THashGetKey(const THashConfig *cnf, void *data)
459 {
460  uint32_t key;
461 
462  key = cnf->DataHash(data);
463  key %= cnf->hash_size;
464 
465  return key;
466 }
467 
468 static inline int THashCompare(const THashConfig *cnf, void *a, void *b)
469 {
470  if (cnf->DataCompare(a, b))
471  return 1;
472  return 0;
473 }
474 
475 /**
476  * \brief Get new data
477  *
478  * Get new data. We're checking memcap first and will try to make room
479  * if the memcap is reached.
480  *
481  * \retval h *LOCKED* data on succes, NULL on error.
482  */
483 static THashData *THashDataGetNew(THashTableContext *ctx, void *data)
484 {
485  THashData *h = NULL;
486 
487  /* get data from the spare queue */
488  h = THashDataDequeue(&ctx->spare_q);
489  if (h == NULL) {
490  /* If we reached the max memcap, we get used data */
491  if (!(THASH_CHECK_MEMCAP(ctx, THASH_DATA_SIZE(ctx)))) {
492  h = THashGetUsed(ctx);
493  if (h == NULL) {
494  return NULL;
495  }
496 
497  if (!SC_ATOMIC_GET(ctx->memcap_reached)) {
498  SC_ATOMIC_SET(ctx->memcap_reached, true);
499  }
500 
501  /* freed data, but it's unlocked */
502  } else {
503  /* now see if we can alloc a new data */
504  h = THashDataAlloc(ctx);
505  if (h == NULL) {
506  return NULL;
507  }
508 
509  /* data is initialized but *unlocked* */
510  }
511  } else {
512  /* data has been recycled before it went into the spare queue */
513 
514  /* data is initialized (recycled) but *unlocked* */
515  }
516 
517  // setup the data
518  BUG_ON(ctx->config.DataSet(h->data, data) != 0);
519 
520  (void) SC_ATOMIC_ADD(ctx->counter, 1);
521  SCMutexLock(&h->m);
522  return h;
523 }
524 
525 /*
526  * returns a *LOCKED* data or NULL
527  */
528 
529 struct THashDataGetResult
531 {
532  struct THashDataGetResult res = { .data = NULL, .is_new = false, };
533  THashData *h = NULL;
534 
535  /* get the key to our bucket */
536  uint32_t key = THashGetKey(&ctx->config, data);
537  /* get our hash bucket and lock it */
538  THashHashRow *hb = &ctx->array[key];
539  HRLOCK_LOCK(hb);
540 
541  /* see if the bucket already has data */
542  if (hb->head == NULL) {
543  h = THashDataGetNew(ctx, data);
544  if (h == NULL) {
545  HRLOCK_UNLOCK(hb);
546  return res;
547  }
548 
549  /* data is locked */
550  hb->head = h;
551  hb->tail = h;
552 
553  /* initialize and return */
554  (void) THashIncrUsecnt(h);
555 
556  HRLOCK_UNLOCK(hb);
557  res.data = h;
558  res.is_new = true;
559  return res;
560  }
561 
562  /* ok, we have data in the bucket. Let's find out if it is our data */
563  h = hb->head;
564 
565  /* see if this is the data we are looking for */
566  if (THashCompare(&ctx->config, h->data, data) == 0) {
567  THashData *ph = NULL; /* previous data */
568 
569  while (h) {
570  ph = h;
571  h = h->next;
572 
573  if (h == NULL) {
574  h = ph->next = THashDataGetNew(ctx, data);
575  if (h == NULL) {
576  HRLOCK_UNLOCK(hb);
577  return res;
578  }
579  hb->tail = h;
580 
581  /* data is locked */
582 
583  h->prev = ph;
584 
585  /* initialize and return */
586  (void) THashIncrUsecnt(h);
587 
588  HRLOCK_UNLOCK(hb);
589  res.data = h;
590  res.is_new = true;
591  return res;
592  }
593 
594  if (THashCompare(&ctx->config, h->data, data) != 0) {
595  /* we found our data, lets put it on top of the
596  * hash list -- this rewards active data */
597  if (h->next) {
598  h->next->prev = h->prev;
599  }
600  if (h->prev) {
601  h->prev->next = h->next;
602  }
603  if (h == hb->tail) {
604  hb->tail = h->prev;
605  }
606 
607  h->next = hb->head;
608  h->prev = NULL;
609  hb->head->prev = h;
610  hb->head = h;
611 
612  /* found our data, lock & return */
613  SCMutexLock(&h->m);
614  (void) THashIncrUsecnt(h);
615  HRLOCK_UNLOCK(hb);
616  res.data = h;
617  res.is_new = false;
618  /* coverity[missing_unlock : FALSE] */
619  return res;
620  }
621  }
622  }
623 
624  /* lock & return */
625  SCMutexLock(&h->m);
626  (void) THashIncrUsecnt(h);
627  HRLOCK_UNLOCK(hb);
628  res.data = h;
629  res.is_new = false;
630  /* coverity[missing_unlock : FALSE] */
631  return res;
632 }
633 
634 /** \brief look up data in the hash
635  *
636  * \param data data to look up
637  *
638  * \retval h *LOCKED* data or NULL
639  */
641 {
642  THashData *h = NULL;
643 
644  /* get the key to our bucket */
645  uint32_t key = THashGetKey(&ctx->config, data);
646  /* get our hash bucket and lock it */
647  THashHashRow *hb = &ctx->array[key];
648  HRLOCK_LOCK(hb);
649 
650  if (hb->head == NULL) {
651  HRLOCK_UNLOCK(hb);
652  return h;
653  }
654 
655  /* ok, we have data in the bucket. Let's find out if it is our data */
656  h = hb->head;
657 
658  /* see if this is the data we are looking for */
659  if (THashCompare(&ctx->config, h->data, data) == 0) {
660  while (h) {
661  h = h->next;
662  if (h == NULL) {
663  HRLOCK_UNLOCK(hb);
664  return h;
665  }
666 
667  if (THashCompare(&ctx->config, h->data, data) != 0) {
668  /* we found our data, lets put it on top of the
669  * hash list -- this rewards active data */
670  if (h->next) {
671  h->next->prev = h->prev;
672  }
673  if (h->prev) {
674  h->prev->next = h->next;
675  }
676  if (h == hb->tail) {
677  hb->tail = h->prev;
678  }
679 
680  h->next = hb->head;
681  h->prev = NULL;
682  hb->head->prev = h;
683  hb->head = h;
684 
685  /* found our data, lock & return */
686  SCMutexLock(&h->m);
687  (void) THashIncrUsecnt(h);
688  HRLOCK_UNLOCK(hb);
689  return h;
690  }
691  }
692  }
693 
694  /* lock & return */
695  SCMutexLock(&h->m);
696  (void) THashIncrUsecnt(h);
697  HRLOCK_UNLOCK(hb);
698  return h;
699 }
700 
701 /** \internal
702  * \brief Get data from the hash directly.
703  *
704  * Called in conditions where the spare queue is empty and memcap is
705  * reached.
706  *
707  * Walks the hash until data can be freed. "prune_idx" atomic int makes
708  * sure we don't start at the top each time since that would clear the top
709  * of the hash leading to longer and longer search times under high
710  * pressure (observed).
711  *
712  * \retval h data or NULL
713  */
714 static THashData *THashGetUsed(THashTableContext *ctx)
715 {
716  uint32_t idx = SC_ATOMIC_GET(ctx->prune_idx) % ctx->config.hash_size;
717  uint32_t cnt = ctx->config.hash_size;
718 
719  while (cnt--) {
720  if (++idx >= ctx->config.hash_size)
721  idx = 0;
722 
723  THashHashRow *hb = &ctx->array[idx];
724 
725  if (HRLOCK_TRYLOCK(hb) != 0)
726  continue;
727 
728  THashData *h = hb->tail;
729  if (h == NULL) {
730  HRLOCK_UNLOCK(hb);
731  continue;
732  }
733 
734  if (SCMutexTrylock(&h->m) != 0) {
735  HRLOCK_UNLOCK(hb);
736  continue;
737  }
738 
739  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
740  HRLOCK_UNLOCK(hb);
741  SCMutexUnlock(&h->m);
742  continue;
743  }
744 
745  /* remove from the hash */
746  if (h->prev != NULL)
747  h->prev->next = h->next;
748  if (h->next != NULL)
749  h->next->prev = h->prev;
750  if (hb->head == h)
751  hb->head = h->next;
752  if (hb->tail == h)
753  hb->tail = h->prev;
754 
755  h->next = NULL;
756  h->prev = NULL;
757  HRLOCK_UNLOCK(hb);
758 
759  if (h->data != NULL) {
760  ctx->config.DataFree(h->data);
761  }
762  SCMutexUnlock(&h->m);
763 
764  (void) SC_ATOMIC_ADD(ctx->prune_idx, (ctx->config.hash_size - cnt));
765  return h;
766  }
767 
768  return NULL;
769 }
770 
771 /**
772  * \retval int -1 not found
773  * \retval int 0 found, but it was busy (ref cnt)
774  * \retval int 1 found and removed */
776 {
777  /* get the key to our bucket */
778  uint32_t key = THashGetKey(&ctx->config, data);
779  /* get our hash bucket and lock it */
780  THashHashRow *hb = &ctx->array[key];
781 
782  HRLOCK_LOCK(hb);
783  THashData *h = hb->head;
784  while (h != NULL) {
785  /* see if this is the data we are looking for */
786  if (THashCompare(&ctx->config, h->data, data) == 0) {
787  h = h->next;
788  continue;
789  }
790 
791  SCMutexLock(&h->m);
792  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
793  SCMutexUnlock(&h->m);
794  HRLOCK_UNLOCK(hb);
795  return 0;
796  }
797 
798  /* remove from the hash */
799  if (h->prev != NULL)
800  h->prev->next = h->next;
801  if (h->next != NULL)
802  h->next->prev = h->prev;
803  if (hb->head == h)
804  hb->head = h->next;
805  if (hb->tail == h)
806  hb->tail = h->prev;
807 
808  h->next = NULL;
809  h->prev = NULL;
810  SCMutexUnlock(&h->m);
811  HRLOCK_UNLOCK(hb);
812  THashDataFree(ctx, h);
813  SCLogDebug("found and removed");
814  return 1;
815  }
816 
817  HRLOCK_UNLOCK(hb);
818  SCLogDebug("data not found");
819  return -1;
820 }
HRLOCK_DESTROY
#define HRLOCK_DESTROY(fb)
Definition: host.h:50
util-byte.h
HQLOCK_LOCK
#define HQLOCK_LOCK(q)
Definition: host-queue.h:67
len
uint8_t len
Definition: app-layer-dnp3.h:2
THashCleanup
void THashCleanup(THashTableContext *ctx)
Cleanup the thash engine.
Definition: util-thash.c:416
THashDataGetResult::data
THashData * data
Definition: util-thash.h:188
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:315
CLS
#define CLS
Definition: suricata-common.h:59
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:387
HQLOCK_DESTROY
#define HQLOCK_DESTROY(q)
Definition: host-queue.h:66
THashOutputFunc
int(* THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint32_t data_len)
Definition: util-thash.h:121
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
ParseSizeStringU64
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition: util-misc.c:198
THashData_::prev
struct THashData_ * prev
Definition: util-thash.h:95
THashDataConfig_::DataFree
void(* DataFree)(void *)
Definition: util-thash.h:132
THashRemoveFromHash
int THashRemoveFromHash(THashTableContext *ctx, void *data)
Definition: util-thash.c:775
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:333
THashData_::next
struct THashData_ * next
Definition: util-thash.h:94
THashConsolidateMemcap
void THashConsolidateMemcap(THashTableContext *ctx)
Definition: util-thash.c:339
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
THashDataQueue_
Definition: util-thash.h:105
THashDataConfig_::DataHash
uint32_t(* DataHash)(void *)
Definition: util-thash.h:133
RandomGet
long int RandomGet(void)
Definition: util-random.c:130
THashDataConfig_::DataCompare
bool(* DataCompare)(void *, void *)
Definition: util-thash.h:134
MAX
#define MAX(x, y)
Definition: suricata-common.h:390
hashsize
#define hashsize(n)
Definition: util-hash-lookup3.c:67
THashDataConfig_::DataSet
int(* DataSet)(void *dst, void *src)
Definition: util-thash.h:131
HRLOCK_LOCK
#define HRLOCK_LOCK(fb)
Definition: host.h:51
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
HRLOCK_UNLOCK
#define HRLOCK_UNLOCK(fb)
Definition: host.h:53
THashDataConfig_::prealloc
uint32_t prealloc
Definition: util-thash.h:128
THashDataConfig_::hash_size
uint32_t hash_size
Definition: util-thash.h:127
util-debug.h
THASH_DEFAULT_HASHSIZE
#define THASH_DEFAULT_HASHSIZE
Definition: util-thash.c:201
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
THashTableContext_
Definition: util-thash.h:139
THASH_DATA_SIZE
#define THASH_DATA_SIZE(ctx)
Definition: util-thash.h:137
THashDataQueue_::bot
THashData * bot
Definition: util-thash.h:107
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:313
THashData_::m
SCMutex m
Definition: util-thash.h:87
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:295
THashInit
THashTableContext * THashInit(const char *cnf_prefix, size_t data_size, int(*DataSet)(void *, void *), void(*DataFree)(void *), uint32_t(*DataHash)(void *), bool(*DataCompare)(void *, void *), bool reset_memcap, uint64_t memcap, uint32_t hashsize)
Definition: util-thash.c:295
THashTableContext_::array
THashHashRow * array
Definition: util-thash.h:141
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:342
THashDataGetResult
Definition: util-thash.h:187
THashDataConfig_::memcap
uint64_t memcap
Definition: util-thash.h:125
SCFreeAligned
#define SCFreeAligned(p)
Definition: util-mem.h:77
GET_VAR
#define GET_VAR(prefix, name)
Definition: util-thash.c:205
conf.h
HRLOCK_INIT
#define HRLOCK_INIT(fb)
Definition: host.h:49
THashDataMoveToSpare
void THashDataMoveToSpare(THashTableContext *ctx, THashData *h)
Definition: util-thash.c:41
THashTableContext_::spare_q
THashDataQueue spare_q
Definition: util-thash.h:147
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
WarnInvalidConfEntry
#define WarnInvalidConfEntry(param_name, format, value)
Generic API that can be used by all to log an invalid conf entry.
Definition: util-misc.h:36
THashShutdown
void THashShutdown(THashTableContext *ctx)
shutdown the flow engine
Definition: util-thash.c:347
HQLOCK_UNLOCK
#define HQLOCK_UNLOCK(q)
Definition: host-queue.h:69
THashData_::data
void * data
Definition: util-thash.h:92
THashData_
Definition: util-thash.h:85
THashDataQueue_::len
uint32_t len
Definition: util-thash.h:108
suricata-common.h
THASH_DEFAULT_MEMCAP
#define THASH_DEFAULT_MEMCAP
Definition: util-thash.c:202
SCMallocAligned
#define SCMallocAligned(size, align)
Definition: util-mem.h:68
HQLOCK_INIT
#define HQLOCK_INIT(q)
Definition: host-queue.h:65
THashGetFromHash
struct THashDataGetResult THashGetFromHash(THashTableContext *ctx, void *data)
Definition: util-thash.c:530
THashLookupFromHash
THashData * THashLookupFromHash(THashTableContext *ctx, void *data)
look up data in the hash
Definition: util-thash.c:640
util-hash-lookup3.h
THASH_DEFAULT_PREALLOC
#define THASH_DEFAULT_PREALLOC
Definition: util-thash.c:203
util-validate.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
THashDataConfig_
Definition: util-thash.h:124
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
THashWalk
int THashWalk(THashTableContext *ctx, THashFormatFunc FormatterFunc, THashOutputFunc OutputterFunc, void *output_ctx)
Walk the hash.
Definition: util-thash.c:381
SCFree
#define SCFree(p)
Definition: util-mem.h:61
THashFormatFunc
int(* THashFormatFunc)(const void *in_data, char *output, size_t output_size)
Definition: util-thash.h:122
util-random.h
HRLOCK_TRYLOCK
#define HRLOCK_TRYLOCK(fb)
Definition: host.h:52
THashDataGetResult::is_new
bool is_new
Definition: util-thash.h:189
THashDataConfig_::data_size
uint32_t data_size
Definition: util-thash.h:130
THashDataConfig_::hash_rand
uint32_t hash_rand
Definition: util-thash.h:126
THashIncrUsecnt
#define THashIncrUsecnt(h)
Definition: util-thash.h:165
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:376
util-misc.h
util-thash.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:120
THASH_CHECK_MEMCAP
#define THASH_CHECK_MEMCAP(ctx, size)
check if a memory alloc would fit in the memcap
Definition: util-thash.h:162
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:104
THashDataQueueNew
THashDataQueue * THashDataQueueNew(void)
Definition: util-thash.c:56
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118
THashDataQueue_::top
THashData * top
Definition: util-thash.h:106
THashTableContext_::config
THashConfig config
Definition: util-thash.h:149