suricata
ippair.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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  * Information about ippairs.
24  */
25 
26 #include "suricata-common.h"
27 #include "conf.h"
28 
29 #include "util-debug.h"
30 #include "ippair.h"
31 #include "ippair-storage.h"
32 
33 #include "util-random.h"
34 #include "util-misc.h"
35 #include "util-byte.h"
36 
37 #include "ippair-queue.h"
38 
39 #include "detect-tag.h"
40 #include "detect-engine-tag.h"
42 
43 #include "util-hash-lookup3.h"
44 
45 static IPPair *IPPairGetUsedIPPair(void);
46 
47 /** ippair hash table */
48 IPPairHashRow *ippair_hash;
49 /** queue with spare ippairs */
50 static IPPairQueue ippair_spare_q;
52 SC_ATOMIC_DECLARE(uint64_t,ippair_memuse);
53 SC_ATOMIC_DECLARE(uint32_t,ippair_counter);
54 SC_ATOMIC_DECLARE(uint32_t,ippair_prune_idx);
55 
56 /** size of the ippair object. Maybe updated in IPPairInitConfig to include
57  * the storage APIs additions. */
58 static uint16_t g_ippair_size = sizeof(IPPair);
59 
60 /**
61  * \brief Update memcap value
62  *
63  * \param size new memcap value
64  */
65 int IPPairSetMemcap(uint64_t size)
66 {
67  if ((uint64_t)SC_ATOMIC_GET(ippair_memuse) < size) {
68  SC_ATOMIC_SET(ippair_config.memcap, size);
69  return 1;
70  }
71 
72  return 0;
73 }
74 
75 /**
76  * \brief Return memcap value
77  *
78  * \retval memcap value
79  */
80 uint64_t IPPairGetMemcap(void)
81 {
82  uint64_t memcapcopy = SC_ATOMIC_GET(ippair_config.memcap);
83  return memcapcopy;
84 }
85 
86 /**
87  * \brief Return memuse value
88  *
89  * \retval memuse value
90  */
91 uint64_t IPPairGetMemuse(void)
92 {
93  uint64_t memusecopy = SC_ATOMIC_GET(ippair_memuse);
94  return memusecopy;
95 }
96 
98 {
99  return IPPairQueueLen(&ippair_spare_q);
100 }
101 
103 {
104  IPPairEnqueue(&ippair_spare_q, h);
105  (void) SC_ATOMIC_SUB(ippair_counter, 1);
106 }
107 
109 {
110  if (!(IPPAIR_CHECK_MEMCAP(g_ippair_size))) {
111  return NULL;
112  }
113 
114  (void) SC_ATOMIC_ADD(ippair_memuse, g_ippair_size);
115 
116  IPPair *h = SCMalloc(g_ippair_size);
117  if (unlikely(h == NULL))
118  goto error;
119 
120  memset(h, 0x00, g_ippair_size);
121 
122  SCMutexInit(&h->m, NULL);
123  SC_ATOMIC_INIT(h->use_cnt);
124  return h;
125 
126 error:
127  return NULL;
128 }
129 
131 {
132  if (h != NULL) {
134  SCMutexDestroy(&h->m);
135  SCFree(h);
136  (void) SC_ATOMIC_SUB(ippair_memuse, g_ippair_size);
137  }
138 }
139 
140 static IPPair *IPPairNew(Address *a, Address *b)
141 {
142  IPPair *p = IPPairAlloc();
143  if (p == NULL)
144  goto error;
145 
146  /* copy addresses */
147  COPY_ADDRESS(a, &p->a[0]);
148  COPY_ADDRESS(b, &p->a[1]);
149 
150  return p;
151 
152 error:
153  return NULL;
154 }
155 
157 {
158  if (IPPairStorageSize() > 0)
160 }
161 
162 #define IPPAIR_DEFAULT_HASHSIZE 4096
163 #define IPPAIR_DEFAULT_MEMCAP 16777216
164 #define IPPAIR_DEFAULT_PREALLOC 1000
165 
166 /** \brief initialize the configuration
167  * \warning Not thread safe */
168 void IPPairInitConfig(char quiet)
169 {
170  SCLogDebug("initializing ippair engine...");
171  if (IPPairStorageSize() > 0)
172  g_ippair_size = sizeof(IPPair) + IPPairStorageSize();
173 
174  memset(&ippair_config, 0, sizeof(ippair_config));
175  //SC_ATOMIC_INIT(flow_flags);
176  SC_ATOMIC_INIT(ippair_counter);
177  SC_ATOMIC_INIT(ippair_memuse);
178  SC_ATOMIC_INIT(ippair_prune_idx);
180  IPPairQueueInit(&ippair_spare_q);
181 
182  /* set defaults */
183  ippair_config.hash_rand = (uint32_t)RandomGet();
187 
188  /* Check if we have memcap and hash_size defined at config */
189  const char *conf_val;
190  uint32_t configval = 0;
191 
192  /** set config values for memcap, prealloc and hash_size */
193  uint64_t ippair_memcap;
194  if ((ConfGet("ippair.memcap", &conf_val)) == 1)
195  {
196  if (ParseSizeStringU64(conf_val, &ippair_memcap) < 0) {
197  SCLogError(SC_ERR_SIZE_PARSE, "Error parsing ippair.memcap "
198  "from conf file - %s. Killing engine",
199  conf_val);
200  exit(EXIT_FAILURE);
201  } else {
202  SC_ATOMIC_SET(ippair_config.memcap, ippair_memcap);
203  }
204  }
205  if ((ConfGet("ippair.hash-size", &conf_val)) == 1)
206  {
207  if (StringParseUint32(&configval, 10, strlen(conf_val),
208  conf_val) > 0) {
209  ippair_config.hash_size = configval;
210  }
211  }
212 
213  if ((ConfGet("ippair.prealloc", &conf_val)) == 1)
214  {
215  if (StringParseUint32(&configval, 10, strlen(conf_val),
216  conf_val) > 0) {
217  ippair_config.prealloc = configval;
218  } else {
219  WarnInvalidConfEntry("ippair.prealloc", "%"PRIu32, ippair_config.prealloc);
220  }
221  }
222  SCLogDebug("IPPair config from suricata.yaml: memcap: %"PRIu64", hash-size: "
223  "%"PRIu32", prealloc: %"PRIu32, SC_ATOMIC_GET(ippair_config.memcap),
225 
226  /* alloc hash memory */
227  uint64_t hash_size = ippair_config.hash_size * sizeof(IPPairHashRow);
228  if (!(IPPAIR_CHECK_MEMCAP(hash_size))) {
229  SCLogError(SC_ERR_IPPAIR_INIT, "allocating ippair hash failed: "
230  "max ippair memcap is smaller than projected hash size. "
231  "Memcap: %"PRIu64", Hash table size %"PRIu64". Calculate "
232  "total hash size by multiplying \"ippair.hash-size\" with %"PRIuMAX", "
233  "which is the hash bucket size.", SC_ATOMIC_GET(ippair_config.memcap), hash_size,
234  (uintmax_t)sizeof(IPPairHashRow));
235  exit(EXIT_FAILURE);
236  }
237  ippair_hash = SCMallocAligned(ippair_config.hash_size * sizeof(IPPairHashRow), CLS);
238  if (unlikely(ippair_hash == NULL)) {
240  "Fatal error encountered in IPPairInitConfig. Exiting...");
241  }
242  memset(ippair_hash, 0, ippair_config.hash_size * sizeof(IPPairHashRow));
243 
244  uint32_t i = 0;
245  for (i = 0; i < ippair_config.hash_size; i++) {
247  }
248  (void) SC_ATOMIC_ADD(ippair_memuse, (ippair_config.hash_size * sizeof(IPPairHashRow)));
249 
250  if (quiet == FALSE) {
251  SCLogConfig("allocated %"PRIu64" bytes of memory for the ippair hash... "
252  "%" PRIu32 " buckets of size %" PRIuMAX "",
253  SC_ATOMIC_GET(ippair_memuse), ippair_config.hash_size,
254  (uintmax_t)sizeof(IPPairHashRow));
255  }
256 
257  /* pre allocate ippairs */
258  for (i = 0; i < ippair_config.prealloc; i++) {
259  if (!(IPPAIR_CHECK_MEMCAP(g_ippair_size))) {
260  SCLogError(SC_ERR_IPPAIR_INIT, "preallocating ippairs failed: "
261  "max ippair memcap reached. Memcap %"PRIu64", "
262  "Memuse %"PRIu64".", SC_ATOMIC_GET(ippair_config.memcap),
263  ((uint64_t)SC_ATOMIC_GET(ippair_memuse) + g_ippair_size));
264  exit(EXIT_FAILURE);
265  }
266 
267  IPPair *h = IPPairAlloc();
268  if (h == NULL) {
269  SCLogError(SC_ERR_IPPAIR_INIT, "preallocating ippair failed: %s", strerror(errno));
270  exit(EXIT_FAILURE);
271  }
272  IPPairEnqueue(&ippair_spare_q,h);
273  }
274 
275  if (quiet == FALSE) {
276  SCLogConfig("preallocated %" PRIu32 " ippairs of size %" PRIu16 "",
277  ippair_spare_q.len, g_ippair_size);
278  SCLogConfig("ippair memory usage: %"PRIu64" bytes, maximum: %"PRIu64,
279  SC_ATOMIC_GET(ippair_memuse), SC_ATOMIC_GET(ippair_config.memcap));
280  }
281 
282  return;
283 }
284 
285 /** \brief print some ippair stats
286  * \warning Not thread safe */
287 void IPPairPrintStats (void)
288 {
289 #ifdef IPPAIRBITS_STATS
290  SCLogPerf("ippairbits added: %" PRIu32 ", removed: %" PRIu32 ", max memory usage: %" PRIu32 "",
291  ippairbits_added, ippairbits_removed, ippairbits_memuse_max);
292 #endif /* IPPAIRBITS_STATS */
293  SCLogPerf("ippair memory usage: %"PRIu64" bytes, maximum: %"PRIu64,
294  SC_ATOMIC_GET(ippair_memuse), SC_ATOMIC_GET(ippair_config.memcap));
295  return;
296 }
297 
298 /** \brief shutdown the flow engine
299  * \warning Not thread safe */
300 void IPPairShutdown(void)
301 {
302  IPPair *h;
303  uint32_t u;
304 
306 
307  /* free spare queue */
308  while((h = IPPairDequeue(&ippair_spare_q))) {
309  BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
310  IPPairFree(h);
311  }
312 
313  /* clear and free the hash */
314  if (ippair_hash != NULL) {
315  for (u = 0; u < ippair_config.hash_size; u++) {
316  h = ippair_hash[u].head;
317  while (h) {
318  IPPair *n = h->hnext;
319  IPPairFree(h);
320  h = n;
321  }
322 
324  }
326  ippair_hash = NULL;
327  }
328  (void) SC_ATOMIC_SUB(ippair_memuse, ippair_config.hash_size * sizeof(IPPairHashRow));
329  IPPairQueueDestroy(&ippair_spare_q);
330  return;
331 }
332 
333 /** \brief Cleanup the ippair engine
334  *
335  * Cleanup the ippair engine from tag and threshold.
336  *
337  */
338 void IPPairCleanup(void)
339 {
340  IPPair *h;
341  uint32_t u;
342 
343  if (ippair_hash != NULL) {
344  for (u = 0; u < ippair_config.hash_size; u++) {
345  h = ippair_hash[u].head;
346  IPPairHashRow *hb = &ippair_hash[u];
347  HRLOCK_LOCK(hb);
348  while (h) {
349  if ((SC_ATOMIC_GET(h->use_cnt) > 0)) {
350  /* iprep is attached to ippair only clear local storage */
352  h = h->hnext;
353  } else {
354  IPPair *n = h->hnext;
355  /* remove from the hash */
356  if (h->hprev != NULL)
357  h->hprev->hnext = h->hnext;
358  if (h->hnext != NULL)
359  h->hnext->hprev = h->hprev;
360  if (hb->head == h)
361  hb->head = h->hnext;
362  if (hb->tail == h)
363  hb->tail = h->hprev;
364  h->hnext = NULL;
365  h->hprev = NULL;
368  h = n;
369  }
370  }
371  HRLOCK_UNLOCK(hb);
372  }
373  }
374 
375  return;
376 }
377 
378 /** \brief compare two raw ipv6 addrs
379  *
380  * \note we don't care about the real ipv6 ip's, this is just
381  * to consistently fill the FlowHashKey6 struct, without all
382  * the SCNtohl calls.
383  *
384  * \warning do not use elsewhere unless you know what you're doing.
385  * detect-engine-address-ipv6.c's AddressIPv6GtU32 is likely
386  * what you are looking for.
387  * Copied from FlowHashRawAddressIPv6GtU32
388  */
389 static inline int IPPairHashRawAddressIPv6GtU32(const uint32_t *a, const uint32_t *b)
390 {
391  int i;
392 
393  for (i = 0; i < 4; i++) {
394  if (a[i] > b[i])
395  return 1;
396  if (a[i] < b[i])
397  break;
398  }
399 
400  return 0;
401 }
402 
403 /* calculate the hash key for this packet
404  *
405  * we're using:
406  * hash_rand -- set at init time
407  * source address
408  */
409 static uint32_t IPPairGetKey(Address *a, Address *b)
410 {
411  uint32_t key;
412 
413  if (a->family == AF_INET) {
414  uint32_t addrs[2] = { MIN(a->addr_data32[0], b->addr_data32[0]),
415  MAX(a->addr_data32[0], b->addr_data32[0]) };
416  uint32_t hash = hashword(addrs, 2, ippair_config.hash_rand);
417  key = hash % ippair_config.hash_size;
418  } else if (a->family == AF_INET6) {
419  uint32_t addrs[8];
420  if (IPPairHashRawAddressIPv6GtU32(&a->addr_data32[0],&b->addr_data32[0])) {
421  addrs[0] = b->addr_data32[0];
422  addrs[1] = b->addr_data32[1];
423  addrs[2] = b->addr_data32[2];
424  addrs[3] = b->addr_data32[3];
425  addrs[4] = a->addr_data32[0];
426  addrs[5] = a->addr_data32[1];
427  addrs[6] = a->addr_data32[2];
428  addrs[7] = a->addr_data32[3];
429  } else {
430  addrs[0] = a->addr_data32[0];
431  addrs[1] = a->addr_data32[1];
432  addrs[2] = a->addr_data32[2];
433  addrs[3] = a->addr_data32[3];
434  addrs[4] = b->addr_data32[0];
435  addrs[5] = b->addr_data32[1];
436  addrs[6] = b->addr_data32[2];
437  addrs[7] = b->addr_data32[3];
438  }
439  uint32_t hash = hashword(addrs, 8, ippair_config.hash_rand);
440  key = hash % ippair_config.hash_size;
441  } else
442  key = 0;
443 
444  return key;
445 }
446 
447 /* Since two or more ippairs can have the same hash key, we need to compare
448  * the ippair with the current addresses. */
449 static inline int IPPairCompare(IPPair *p, Address *a, Address *b)
450 {
451  /* compare in both directions */
452  if ((CMP_ADDR(&p->a[0], a) && CMP_ADDR(&p->a[1], b)) ||
453  (CMP_ADDR(&p->a[0], b) && CMP_ADDR(&p->a[1], a)))
454  return 1;
455  return 0;
456 }
457 
458 /**
459  * \brief Get a new ippair
460  *
461  * Get a new ippair. We're checking memcap first and will try to make room
462  * if the memcap is reached.
463  *
464  * \retval h *LOCKED* ippair on succes, NULL on error.
465  */
466 static IPPair *IPPairGetNew(Address *a, Address *b)
467 {
468  IPPair *h = NULL;
469 
470  /* get a ippair from the spare queue */
471  h = IPPairDequeue(&ippair_spare_q);
472  if (h == NULL) {
473  /* If we reached the max memcap, we get a used ippair */
474  if (!(IPPAIR_CHECK_MEMCAP(g_ippair_size))) {
475  /* declare state of emergency */
476  //if (!(SC_ATOMIC_GET(ippair_flags) & IPPAIR_EMERGENCY)) {
477  // SC_ATOMIC_OR(ippair_flags, IPPAIR_EMERGENCY);
478 
479  /* under high load, waking up the flow mgr each time leads
480  * to high cpu usage. Flows are not timed out much faster if
481  * we check a 1000 times a second. */
482  // FlowWakeupFlowManagerThread();
483  //}
484 
485  h = IPPairGetUsedIPPair();
486  if (h == NULL) {
487  return NULL;
488  }
489 
490  /* freed a ippair, but it's unlocked */
491  } else {
492  /* now see if we can alloc a new ippair */
493  h = IPPairNew(a,b);
494  if (h == NULL) {
495  return NULL;
496  }
497 
498  /* ippair is initialized but *unlocked* */
499  }
500  } else {
501  /* ippair has been recycled before it went into the spare queue */
502 
503  /* ippair is initialized (recylced) but *unlocked* */
504  }
505 
506  (void) SC_ATOMIC_ADD(ippair_counter, 1);
507  SCMutexLock(&h->m);
508  return h;
509 }
510 
511 static void IPPairInit(IPPair *h, Address *a, Address *b)
512 {
513  COPY_ADDRESS(a, &h->a[0]);
514  COPY_ADDRESS(b, &h->a[1]);
515  (void) IPPairIncrUsecnt(h);
516 }
517 
519 {
520  (void) IPPairDecrUsecnt(h);
521  SCMutexUnlock(&h->m);
522 }
523 
525 {
526  SCMutexLock(&h->m);
527 }
528 
530 {
531  SCMutexUnlock(&h->m);
532 }
533 
534 /* IPPairGetIPPairFromHash
535  *
536  * Hash retrieval function for ippairs. Looks up the hash bucket containing the
537  * ippair pointer. Then compares the packet with the found ippair to see if it is
538  * the ippair we need. If it isn't, walk the list until the right ippair is found.
539  *
540  * returns a *LOCKED* ippair or NULL
541  */
543 {
544  IPPair *h = NULL;
545 
546  /* get the key to our bucket */
547  uint32_t key = IPPairGetKey(a, b);
548  /* get our hash bucket and lock it */
549  IPPairHashRow *hb = &ippair_hash[key];
550  HRLOCK_LOCK(hb);
551 
552  /* see if the bucket already has a ippair */
553  if (hb->head == NULL) {
554  h = IPPairGetNew(a,b);
555  if (h == NULL) {
556  HRLOCK_UNLOCK(hb);
557  return NULL;
558  }
559 
560  /* ippair is locked */
561  hb->head = h;
562  hb->tail = h;
563 
564  /* got one, now lock, initialize and return */
565  IPPairInit(h,a,b);
566 
567  HRLOCK_UNLOCK(hb);
568  return h;
569  }
570 
571  /* ok, we have a ippair in the bucket. Let's find out if it is our ippair */
572  h = hb->head;
573 
574  /* see if this is the ippair we are looking for */
575  if (IPPairCompare(h, a, b) == 0) {
576  IPPair *ph = NULL; /* previous ippair */
577 
578  while (h) {
579  ph = h;
580  h = h->hnext;
581 
582  if (h == NULL) {
583  h = ph->hnext = IPPairGetNew(a,b);
584  if (h == NULL) {
585  HRLOCK_UNLOCK(hb);
586  return NULL;
587  }
588  hb->tail = h;
589 
590  /* ippair is locked */
591 
592  h->hprev = ph;
593 
594  /* initialize and return */
595  IPPairInit(h,a,b);
596 
597  HRLOCK_UNLOCK(hb);
598  return h;
599  }
600 
601  if (IPPairCompare(h, a, b) != 0) {
602  /* we found our ippair, lets put it on top of the
603  * hash list -- this rewards active ippairs */
604  if (h->hnext) {
605  h->hnext->hprev = h->hprev;
606  }
607  if (h->hprev) {
608  h->hprev->hnext = h->hnext;
609  }
610  if (h == hb->tail) {
611  hb->tail = h->hprev;
612  }
613 
614  h->hnext = hb->head;
615  h->hprev = NULL;
616  hb->head->hprev = h;
617  hb->head = h;
618 
619  /* found our ippair, lock & return */
620  SCMutexLock(&h->m);
621  (void) IPPairIncrUsecnt(h);
622  HRLOCK_UNLOCK(hb);
623  return h;
624  }
625  }
626  }
627 
628  /* lock & return */
629  SCMutexLock(&h->m);
630  (void) IPPairIncrUsecnt(h);
631  HRLOCK_UNLOCK(hb);
632  return h;
633 }
634 
635 /** \brief look up a ippair in the hash
636  *
637  * \param a address to look up
638  *
639  * \retval h *LOCKED* ippair or NULL
640  */
642 {
643  IPPair *h = NULL;
644 
645  /* get the key to our bucket */
646  uint32_t key = IPPairGetKey(a, b);
647  /* get our hash bucket and lock it */
648  IPPairHashRow *hb = &ippair_hash[key];
649  HRLOCK_LOCK(hb);
650 
651  /* see if the bucket already has a ippair */
652  if (hb->head == NULL) {
653  HRLOCK_UNLOCK(hb);
654  return h;
655  }
656 
657  /* ok, we have a ippair in the bucket. Let's find out if it is our ippair */
658  h = hb->head;
659 
660  /* see if this is the ippair we are looking for */
661  if (IPPairCompare(h, a, b) == 0) {
662  while (h) {
663  h = h->hnext;
664 
665  if (h == NULL) {
666  HRLOCK_UNLOCK(hb);
667  return h;
668  }
669 
670  if (IPPairCompare(h, a, b) != 0) {
671  /* we found our ippair, lets put it on top of the
672  * hash list -- this rewards active ippairs */
673  if (h->hnext) {
674  h->hnext->hprev = h->hprev;
675  }
676  if (h->hprev) {
677  h->hprev->hnext = h->hnext;
678  }
679  if (h == hb->tail) {
680  hb->tail = h->hprev;
681  }
682 
683  h->hnext = hb->head;
684  h->hprev = NULL;
685  hb->head->hprev = h;
686  hb->head = h;
687 
688  /* found our ippair, lock & return */
689  SCMutexLock(&h->m);
690  (void) IPPairIncrUsecnt(h);
691  HRLOCK_UNLOCK(hb);
692  return h;
693  }
694  }
695  }
696 
697  /* lock & return */
698  SCMutexLock(&h->m);
699  (void) IPPairIncrUsecnt(h);
700  HRLOCK_UNLOCK(hb);
701  return h;
702 }
703 
704 /** \internal
705  * \brief Get a ippair from the hash directly.
706  *
707  * Called in conditions where the spare queue is empty and memcap is reached.
708  *
709  * Walks the hash until a ippair can be freed. "ippair_prune_idx" atomic int makes
710  * sure we don't start at the top each time since that would clear the top of
711  * the hash leading to longer and longer search times under high pressure (observed).
712  *
713  * \retval h ippair or NULL
714  */
715 static IPPair *IPPairGetUsedIPPair(void)
716 {
717  uint32_t idx = SC_ATOMIC_GET(ippair_prune_idx) % ippair_config.hash_size;
718  uint32_t cnt = ippair_config.hash_size;
719 
720  while (cnt--) {
721  if (++idx >= ippair_config.hash_size)
722  idx = 0;
723 
724  IPPairHashRow *hb = &ippair_hash[idx];
725 
726  if (HRLOCK_TRYLOCK(hb) != 0)
727  continue;
728 
729  IPPair *h = hb->tail;
730  if (h == NULL) {
731  HRLOCK_UNLOCK(hb);
732  continue;
733  }
734 
735  if (SCMutexTrylock(&h->m) != 0) {
736  HRLOCK_UNLOCK(hb);
737  continue;
738  }
739 
740  /** never prune a ippair that is used by a packets
741  * we are currently processing in one of the threads */
742  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
743  HRLOCK_UNLOCK(hb);
744  SCMutexUnlock(&h->m);
745  continue;
746  }
747 
748  /* remove from the hash */
749  if (h->hprev != NULL)
750  h->hprev->hnext = h->hnext;
751  if (h->hnext != NULL)
752  h->hnext->hprev = h->hprev;
753  if (hb->head == h)
754  hb->head = h->hnext;
755  if (hb->tail == h)
756  hb->tail = h->hprev;
757 
758  h->hnext = NULL;
759  h->hprev = NULL;
760  HRLOCK_UNLOCK(hb);
761 
762  IPPairClearMemory (h);
763 
764  SCMutexUnlock(&h->m);
765 
766  (void) SC_ATOMIC_ADD(ippair_prune_idx, (ippair_config.hash_size - cnt));
767  return h;
768  }
769 
770  return NULL;
771 }
772 
774 {
776 }
HRLOCK_DESTROY
#define HRLOCK_DESTROY(fb)
Definition: host.h:50
util-byte.h
SC_ERR_IPPAIR_INIT
@ SC_ERR_IPPAIR_INIT
Definition: util-error.h:298
ippair.h
IPPairQueue_::len
uint32_t len
Definition: ippair-queue.h:45
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:174
IPPAIR_DEFAULT_HASHSIZE
#define IPPAIR_DEFAULT_HASHSIZE
Definition: ippair.c:162
IPPairQueueLen
uint32_t IPPairQueueLen(IPPairQueue *q)
Definition: ippair-queue.c:136
IPPair_::a
Address a[2]
Definition: ippair.h:63
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:315
SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, ippair_memuse)
IPPairRelease
void IPPairRelease(IPPair *h)
Definition: ippair.c:518
CLS
#define CLS
Definition: suricata-common.h:45
IPPAIR_DEFAULT_PREALLOC
#define IPPAIR_DEFAULT_PREALLOC
Definition: ippair.c:164
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
IPPairIncrUsecnt
#define IPPairIncrUsecnt(h)
Definition: ippair.h:109
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
ParseSizeStringU64
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition: util-misc.c:202
IPPairQueueInit
IPPairQueue * IPPairQueueInit(IPPairQueue *q)
Definition: ippair-queue.c:34
IPPairRegisterUnittests
void IPPairRegisterUnittests(void)
Definition: ippair.c:773
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:333
IPPAIR_CHECK_MEMCAP
#define IPPAIR_CHECK_MEMCAP(size)
check if a memory alloc would fit in the memcap
Definition: ippair.h:106
IPPairDecrUsecnt
#define IPPairDecrUsecnt(h)
Definition: ippair.h:111
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
MIN
#define MIN(x, y)
Definition: suricata-common.h:377
Address_
Definition: decode.h:116
IPPairDequeue
IPPair * IPPairDequeue(IPPairQueue *q)
remove a ippair from the queue
Definition: ippair-queue.c:103
RandomGet
long int RandomGet(void)
Definition: util-random.c:129
IPPairAlloc
IPPair * IPPairAlloc(void)
Definition: ippair.c:108
detect-tag.h
IPPairMoveToSpare
void IPPairMoveToSpare(IPPair *h)
Definition: ippair.c:102
MAX
#define MAX(x, y)
Definition: suricata-common.h:381
IPPairStorageSize
unsigned int IPPairStorageSize(void)
Definition: ippair-storage.c:30
IPPair_::m
SCMutex m
Definition: ippair.h:60
HRLOCK_LOCK
#define HRLOCK_LOCK(fb)
Definition: host.h:51
IPPairFree
void IPPairFree(IPPair *h)
Definition: ippair.c:130
IPPairShutdown
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:300
IPPairConfig_::prealloc
uint32_t prealloc
Definition: ippair.h:96
SC_ERR_SIZE_PARSE
@ SC_ERR_SIZE_PARSE
Definition: util-error.h:230
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:330
HRLOCK_UNLOCK
#define HRLOCK_UNLOCK(fb)
Definition: host.h:53
util-debug.h
ippair_config
IPPairConfig ippair_config
Definition: ippair.c:51
IPPairCleanup
void IPPairCleanup(void)
Cleanup the ippair engine.
Definition: ippair.c:338
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
IPPairGetMemcap
uint64_t IPPairGetMemcap(void)
Return memcap value.
Definition: ippair.c:80
IPPairGetIPPairFromHash
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition: ippair.c:542
IPPairEnqueue
void IPPairEnqueue(IPPairQueue *q, IPPair *h)
add a ippair to a queue
Definition: ippair-queue.c:70
IPPairInitConfig
void IPPairInitConfig(char quiet)
initialize the configuration
Definition: ippair.c:168
IPPairLock
void IPPairLock(IPPair *h)
Definition: ippair.c:524
IPPairQueue_
Definition: ippair-queue.h:42
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
IPPAIR_DEFAULT_MEMCAP
#define IPPAIR_DEFAULT_MEMCAP
Definition: ippair.c:163
detect-engine-tag.h
IPPairUnlock
void IPPairUnlock(IPPair *h)
Definition: ippair.c:529
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:342
FALSE
#define FALSE
Definition: suricata-common.h:34
SCFreeAligned
#define SCFreeAligned(p)
Definition: util-mem.h:77
conf.h
HRLOCK_INIT
#define HRLOCK_INIT(fb)
Definition: host.h:49
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:37
IPPairSpareQueueGetSize
uint32_t IPPairSpareQueueGetSize(void)
Definition: ippair.c:97
StringParseUint32
int StringParseUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:313
CMP_ADDR
#define CMP_ADDR(a1, a2)
Definition: decode.h:245
suricata-common.h
RegisterIPPairStorageTests
void RegisterIPPairStorageTests(void)
Definition: ippair-storage.c:292
IPPair_
Definition: ippair.h:58
IPPairConfig_::hash_size
uint32_t hash_size
Definition: ippair.h:95
SCMallocAligned
#define SCMallocAligned(size, align)
Definition: util-mem.h:68
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:224
IPPairGetMemuse
uint64_t IPPairGetMemuse(void)
Return memuse value.
Definition: ippair.c:91
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
IPPairClearMemory
void IPPairClearMemory(IPPair *h)
Definition: ippair.c:156
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
util-hash-lookup3.h
IPPair_::hprev
struct IPPair_ * hprev
Definition: ippair.h:73
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
IPPairConfig_
Definition: ippair.h:92
IPPair_::hnext
struct IPPair_ * hnext
Definition: ippair.h:72
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ippair-queue.h
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
util-random.h
HRLOCK_TRYLOCK
#define HRLOCK_TRYLOCK(fb)
Definition: host.h:52
IPPairQueueDestroy
void IPPairQueueDestroy(IPPairQueue *q)
Destroy a ippair queue.
Definition: ippair-queue.c:59
IPPairSetMemcap
int IPPairSetMemcap(uint64_t size)
Update memcap value.
Definition: ippair.c:65
Address_::family
char family
Definition: decode.h:117
IPPairLookupIPPairFromHash
IPPair * IPPairLookupIPPairFromHash(Address *a, Address *b)
look up a ippair in the hash
Definition: ippair.c:641
IPPairFreeStorage
void IPPairFreeStorage(IPPair *h)
Definition: ippair-storage.c:55
IPPair
struct IPPair_ IPPair
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:376
util-misc.h
COPY_ADDRESS
#define COPY_ADDRESS(a, b)
Definition: decode.h:131
IPPairConfig_::hash_rand
uint32_t hash_rand
Definition: ippair.h:94
ippair-storage.h
ippair_hash
IPPairHashRow * ippair_hash
Definition: ippair.c:48
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:120
IPPairPrintStats
void IPPairPrintStats(void)
print some ippair stats
Definition: ippair.c:287
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118
detect-engine-threshold.h