suricata
defrag-hash.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 #include "suricata-common.h"
19 #include "conf.h"
20 #include "defrag-hash.h"
21 #include "defrag-queue.h"
22 #include "defrag-config.h"
23 #include "util-random.h"
24 #include "util-byte.h"
25 #include "util-misc.h"
26 #include "util-hash-lookup3.h"
27 
28 /** defrag tracker hash table */
31 SC_ATOMIC_DECLARE(uint64_t,defrag_memuse);
32 SC_ATOMIC_DECLARE(unsigned int,defragtracker_counter);
33 SC_ATOMIC_DECLARE(unsigned int,defragtracker_prune_idx);
34 
35 static DefragTracker *DefragTrackerGetUsedDefragTracker(void);
36 
37 /** queue with spare tracker */
38 static DefragTrackerQueue defragtracker_spare_q;
39 
40 /**
41  * \brief Update memcap value
42  *
43  * \param size new memcap value
44  */
45 int DefragTrackerSetMemcap(uint64_t size)
46 {
47  if ((uint64_t)SC_ATOMIC_GET(defrag_memuse) < size) {
48  SC_ATOMIC_SET(defrag_config.memcap, size);
49  return 1;
50  }
51 
52  return 0;
53 }
54 
55 /**
56  * \brief Return memcap value
57  *
58  * \retval memcap value
59  */
60 uint64_t DefragTrackerGetMemcap(void)
61 {
62  uint64_t memcapcopy = SC_ATOMIC_GET(defrag_config.memcap);
63  return memcapcopy;
64 }
65 
66 /**
67  * \brief Return memuse value
68  *
69  * \retval memuse value
70  */
71 uint64_t DefragTrackerGetMemuse(void)
72 {
73  uint64_t memusecopy = (uint64_t)SC_ATOMIC_GET(defrag_memuse);
74  return memusecopy;
75 }
76 
78 {
79  return DefragTrackerQueueLen(&defragtracker_spare_q);
80 }
81 
83 {
84  DefragTrackerEnqueue(&defragtracker_spare_q, h);
85  (void) SC_ATOMIC_SUB(defragtracker_counter, 1);
86 }
87 
88 static DefragTracker *DefragTrackerAlloc(void)
89 {
90  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
91  return NULL;
92  }
93 
94  (void) SC_ATOMIC_ADD(defrag_memuse, sizeof(DefragTracker));
95 
96  DefragTracker *dt = SCMalloc(sizeof(DefragTracker));
97  if (unlikely(dt == NULL))
98  goto error;
99 
100  memset(dt, 0x00, sizeof(DefragTracker));
101 
102  SCMutexInit(&dt->lock, NULL);
103  SC_ATOMIC_INIT(dt->use_cnt);
104  return dt;
105 
106 error:
107  return NULL;
108 }
109 
110 static void DefragTrackerFree(DefragTracker *dt)
111 {
112  if (dt != NULL) {
114 
115  SCMutexDestroy(&dt->lock);
116  SCFree(dt);
117  (void) SC_ATOMIC_SUB(defrag_memuse, sizeof(DefragTracker));
118  }
119 }
120 
121 #define DefragTrackerIncrUsecnt(dt) \
122  SC_ATOMIC_ADD((dt)->use_cnt, 1)
123 #define DefragTrackerDecrUsecnt(dt) \
124  SC_ATOMIC_SUB((dt)->use_cnt, 1)
125 
126 static void DefragTrackerInit(DefragTracker *dt, Packet *p)
127 {
128  /* copy address */
129  COPY_ADDRESS(&p->src, &dt->src_addr);
130  COPY_ADDRESS(&p->dst, &dt->dst_addr);
131 
132  if (PKT_IS_IPV4(p)) {
133  dt->id = (int32_t)IPV4_GET_IPID(p);
134  dt->af = AF_INET;
135  } else {
136  dt->id = (int32_t)IPV6_EXTHDR_GET_FH_ID(p);
137  dt->af = AF_INET6;
138  }
139  dt->proto = IP_GET_IPPROTO(p);
140  dt->vlan_id[0] = p->vlan_id[0];
141  dt->vlan_id[1] = p->vlan_id[1];
142  dt->policy = DefragGetOsPolicy(p);
144  dt->remove = 0;
145  dt->seen_last = 0;
146 
147  (void) DefragTrackerIncrUsecnt(dt);
148 }
149 
151 {
152  (void) DefragTrackerDecrUsecnt(t);
153  SCMutexUnlock(&t->lock);
154 }
155 
157 {
159  SC_ATOMIC_DESTROY(dt->use_cnt);
160 }
161 
162 #define DEFRAG_DEFAULT_HASHSIZE 4096
163 #define DEFRAG_DEFAULT_MEMCAP 16777216
164 #define DEFRAG_DEFAULT_PREALLOC 1000
165 
166 /** \brief initialize the configuration
167  * \warning Not thread safe */
168 void DefragInitConfig(char quiet)
169 {
170  SCLogDebug("initializing defrag engine...");
171 
172  memset(&defrag_config, 0, sizeof(defrag_config));
173  //SC_ATOMIC_INIT(flow_flags);
174  SC_ATOMIC_INIT(defragtracker_counter);
175  SC_ATOMIC_INIT(defrag_memuse);
176  SC_ATOMIC_INIT(defragtracker_prune_idx);
178  DefragTrackerQueueInit(&defragtracker_spare_q);
179 
180  /* set defaults */
181  defrag_config.hash_rand = (uint32_t)RandomGet();
185 
186  /* Check if we have memcap and hash_size defined at config */
187  const char *conf_val;
188  uint32_t configval = 0;
189 
190  uint64_t defrag_memcap;
191  /** set config values for memcap, prealloc and hash_size */
192  if ((ConfGet("defrag.memcap", &conf_val)) == 1)
193  {
194  if (ParseSizeStringU64(conf_val, &defrag_memcap) < 0) {
195  SCLogError(SC_ERR_SIZE_PARSE, "Error parsing defrag.memcap "
196  "from conf file - %s. Killing engine",
197  conf_val);
198  exit(EXIT_FAILURE);
199  } else {
200  SC_ATOMIC_SET(defrag_config.memcap, defrag_memcap);
201  }
202  }
203  if ((ConfGet("defrag.hash-size", &conf_val)) == 1)
204  {
205  if (StringParseUint32(&configval, 10, strlen(conf_val),
206  conf_val) > 0) {
207  defrag_config.hash_size = configval;
208  } else {
209  WarnInvalidConfEntry("defrag.hash-size", "%"PRIu32, defrag_config.hash_size);
210  }
211  }
212 
213 
214  if ((ConfGet("defrag.trackers", &conf_val)) == 1)
215  {
216  if (StringParseUint32(&configval, 10, strlen(conf_val),
217  conf_val) > 0) {
218  defrag_config.prealloc = configval;
219  } else {
220  WarnInvalidConfEntry("defrag.trackers", "%"PRIu32, defrag_config.prealloc);
221  }
222  }
223  SCLogDebug("DefragTracker config from suricata.yaml: memcap: %"PRIu64", hash-size: "
224  "%"PRIu32", prealloc: %"PRIu32, SC_ATOMIC_GET(defrag_config.memcap),
226 
227  /* alloc hash memory */
228  uint64_t hash_size = defrag_config.hash_size * sizeof(DefragTrackerHashRow);
229  if (!(DEFRAG_CHECK_MEMCAP(hash_size))) {
230  SCLogError(SC_ERR_DEFRAG_INIT, "allocating defrag hash failed: "
231  "max defrag memcap is smaller than projected hash size. "
232  "Memcap: %"PRIu64", Hash table size %"PRIu64". Calculate "
233  "total hash size by multiplying \"defrag.hash-size\" with %"PRIuMAX", "
234  "which is the hash bucket size.", SC_ATOMIC_GET(defrag_config.memcap), hash_size,
235  (uintmax_t)sizeof(DefragTrackerHashRow));
236  exit(EXIT_FAILURE);
237  }
239  if (unlikely(defragtracker_hash == NULL)) {
240  SCLogError(SC_ERR_FATAL, "Fatal error encountered in DefragTrackerInitConfig. Exiting...");
241  exit(EXIT_FAILURE);
242  }
244 
245  uint32_t i = 0;
246  for (i = 0; i < defrag_config.hash_size; i++) {
248  }
249  (void) SC_ATOMIC_ADD(defrag_memuse, (defrag_config.hash_size * sizeof(DefragTrackerHashRow)));
250 
251  if (quiet == FALSE) {
252  SCLogConfig("allocated %"PRIu64" bytes of memory for the defrag hash... "
253  "%" PRIu32 " buckets of size %" PRIuMAX "",
254  SC_ATOMIC_GET(defrag_memuse), defrag_config.hash_size,
255  (uintmax_t)sizeof(DefragTrackerHashRow));
256  }
257 
258  if ((ConfGet("defrag.prealloc", &conf_val)) == 1)
259  {
260  if (ConfValIsTrue(conf_val)) {
261  /* pre allocate defrag trackers */
262  for (i = 0; i < defrag_config.prealloc; i++) {
263  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
264  SCLogError(SC_ERR_DEFRAG_INIT, "preallocating defrag trackers failed: "
265  "max defrag memcap reached. Memcap %"PRIu64", "
266  "Memuse %"PRIu64".", SC_ATOMIC_GET(defrag_config.memcap),
267  ((uint64_t)SC_ATOMIC_GET(defrag_memuse) + (uint64_t)sizeof(DefragTracker)));
268  exit(EXIT_FAILURE);
269  }
270 
271  DefragTracker *h = DefragTrackerAlloc();
272  if (h == NULL) {
273  SCLogError(SC_ERR_DEFRAG_INIT, "preallocating defrag failed: %s", strerror(errno));
274  exit(EXIT_FAILURE);
275  }
276  DefragTrackerEnqueue(&defragtracker_spare_q,h);
277  }
278  if (quiet == FALSE) {
279  SCLogConfig("preallocated %" PRIu32 " defrag trackers of size %" PRIuMAX "",
280  defragtracker_spare_q.len, (uintmax_t)sizeof(DefragTracker));
281  }
282  }
283  }
284 
285  if (quiet == FALSE) {
286  SCLogConfig("defrag memory usage: %"PRIu64" bytes, maximum: %"PRIu64,
287  SC_ATOMIC_GET(defrag_memuse), SC_ATOMIC_GET(defrag_config.memcap));
288  }
289 
290  return;
291 }
292 
293 /** \brief print some defrag stats
294  * \warning Not thread safe */
295 static void DefragTrackerPrintStats (void)
296 {
297 }
298 
299 /** \brief shutdown the flow engine
300  * \warning Not thread safe */
302 {
303  DefragTracker *dt;
304  uint32_t u;
305 
306  DefragTrackerPrintStats();
307 
308  /* free spare queue */
309  while((dt = DefragTrackerDequeue(&defragtracker_spare_q))) {
310  BUG_ON(SC_ATOMIC_GET(dt->use_cnt) > 0);
311  DefragTrackerFree(dt);
312  }
313 
314  /* clear and free the hash */
315  if (defragtracker_hash != NULL) {
316  for (u = 0; u < defrag_config.hash_size; u++) {
317  dt = defragtracker_hash[u].head;
318  while (dt) {
319  DefragTracker *n = dt->hnext;
321  DefragTrackerFree(dt);
322  dt = n;
323  }
324 
326  }
328  defragtracker_hash = NULL;
329  }
330  (void) SC_ATOMIC_SUB(defrag_memuse, defrag_config.hash_size * sizeof(DefragTrackerHashRow));
331  DefragTrackerQueueDestroy(&defragtracker_spare_q);
332 
333  SC_ATOMIC_DESTROY(defragtracker_prune_idx);
334  SC_ATOMIC_DESTROY(defrag_memuse);
335  SC_ATOMIC_DESTROY(defragtracker_counter);
337  //SC_ATOMIC_DESTROY(flow_flags);
338  return;
339 }
340 
341 /** \brief compare two raw ipv6 addrs
342  *
343  * \note we don't care about the real ipv6 ip's, this is just
344  * to consistently fill the DefragHashKey6 struct, without all
345  * the SCNtohl calls.
346  *
347  * \warning do not use elsewhere unless you know what you're doing.
348  * detect-engine-address-ipv6.c's AddressIPv6GtU32 is likely
349  * what you are looking for.
350  */
351 static inline int DefragHashRawAddressIPv6GtU32(uint32_t *a, uint32_t *b)
352 {
353  int i;
354 
355  for (i = 0; i < 4; i++) {
356  if (a[i] > b[i])
357  return 1;
358  if (a[i] < b[i])
359  break;
360  }
361 
362  return 0;
363 }
364 
365 typedef struct DefragHashKey4_ {
366  union {
367  struct {
368  uint32_t src, dst;
369  uint32_t id;
370  uint16_t vlan_id[2];
371  };
372  uint32_t u32[4];
373  };
375 
376 typedef struct DefragHashKey6_ {
377  union {
378  struct {
379  uint32_t src[4], dst[4];
380  uint32_t id;
381  uint16_t vlan_id[2];
382  };
383  uint32_t u32[10];
384  };
386 
387 /* calculate the hash key for this packet
388  *
389  * we're using:
390  * hash_rand -- set at init time
391  * source address
392  * destination address
393  * id
394  * vlan_id
395  */
396 static inline uint32_t DefragHashGetKey(Packet *p)
397 {
398  uint32_t key;
399 
400  if (p->ip4h != NULL) {
401  DefragHashKey4 dhk;
402  if (p->src.addr_data32[0] > p->dst.addr_data32[0]) {
403  dhk.src = p->src.addr_data32[0];
404  dhk.dst = p->dst.addr_data32[0];
405  } else {
406  dhk.src = p->dst.addr_data32[0];
407  dhk.dst = p->src.addr_data32[0];
408  }
409  dhk.id = (uint32_t)IPV4_GET_IPID(p);
410  dhk.vlan_id[0] = p->vlan_id[0];
411  dhk.vlan_id[1] = p->vlan_id[1];
412 
413  uint32_t hash = hashword(dhk.u32, 4, defrag_config.hash_rand);
414  key = hash % defrag_config.hash_size;
415  } else if (p->ip6h != NULL) {
416  DefragHashKey6 dhk;
417  if (DefragHashRawAddressIPv6GtU32(p->src.addr_data32, p->dst.addr_data32)) {
418  dhk.src[0] = p->src.addr_data32[0];
419  dhk.src[1] = p->src.addr_data32[1];
420  dhk.src[2] = p->src.addr_data32[2];
421  dhk.src[3] = p->src.addr_data32[3];
422  dhk.dst[0] = p->dst.addr_data32[0];
423  dhk.dst[1] = p->dst.addr_data32[1];
424  dhk.dst[2] = p->dst.addr_data32[2];
425  dhk.dst[3] = p->dst.addr_data32[3];
426  } else {
427  dhk.src[0] = p->dst.addr_data32[0];
428  dhk.src[1] = p->dst.addr_data32[1];
429  dhk.src[2] = p->dst.addr_data32[2];
430  dhk.src[3] = p->dst.addr_data32[3];
431  dhk.dst[0] = p->src.addr_data32[0];
432  dhk.dst[1] = p->src.addr_data32[1];
433  dhk.dst[2] = p->src.addr_data32[2];
434  dhk.dst[3] = p->src.addr_data32[3];
435  }
436  dhk.id = IPV6_EXTHDR_GET_FH_ID(p);
437  dhk.vlan_id[0] = p->vlan_id[0];
438  dhk.vlan_id[1] = p->vlan_id[1];
439 
440  uint32_t hash = hashword(dhk.u32, 10, defrag_config.hash_rand);
441  key = hash % defrag_config.hash_size;
442  } else
443  key = 0;
444 
445  return key;
446 }
447 
448 /* Since two or more trackers can have the same hash key, we need to compare
449  * the tracker with the current tracker key. */
450 #define CMP_DEFRAGTRACKER(d1,d2,id) \
451  (((CMP_ADDR(&(d1)->src_addr, &(d2)->src) && \
452  CMP_ADDR(&(d1)->dst_addr, &(d2)->dst)) || \
453  (CMP_ADDR(&(d1)->src_addr, &(d2)->dst) && \
454  CMP_ADDR(&(d1)->dst_addr, &(d2)->src))) && \
455  (d1)->proto == IP_GET_IPPROTO(d2) && \
456  (d1)->id == (id) && \
457  (d1)->vlan_id[0] == (d2)->vlan_id[0] && \
458  (d1)->vlan_id[1] == (d2)->vlan_id[1])
459 
460 static inline int DefragTrackerCompare(DefragTracker *t, Packet *p)
461 {
462  uint32_t id;
463  if (PKT_IS_IPV4(p)) {
464  id = (uint32_t)IPV4_GET_IPID(p);
465  } else {
466  id = IPV6_EXTHDR_GET_FH_ID(p);
467  }
468 
469  return CMP_DEFRAGTRACKER(t, p, id);
470 }
471 
472 /**
473  * \brief Get a new defrag tracker
474  *
475  * Get a new defrag tracker. We're checking memcap first and will try to make room
476  * if the memcap is reached.
477  *
478  * \retval dt *LOCKED* tracker on succes, NULL on error.
479  */
480 static DefragTracker *DefragTrackerGetNew(Packet *p)
481 {
482  DefragTracker *dt = NULL;
483 
484  /* get a tracker from the spare queue */
485  dt = DefragTrackerDequeue(&defragtracker_spare_q);
486  if (dt == NULL) {
487  /* If we reached the max memcap, we get a used tracker */
488  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
489  /* declare state of emergency */
490  //if (!(SC_ATOMIC_GET(defragtracker_flags) & DEFRAG_EMERGENCY)) {
491  // SC_ATOMIC_OR(defragtracker_flags, DEFRAG_EMERGENCY);
492 
493  /* under high load, waking up the flow mgr each time leads
494  * to high cpu usage. Flows are not timed out much faster if
495  * we check a 1000 times a second. */
496  // FlowWakeupFlowManagerThread();
497  //}
498 
499  dt = DefragTrackerGetUsedDefragTracker();
500  if (dt == NULL) {
501  return NULL;
502  }
503 
504  /* freed a tracker, but it's unlocked */
505  } else {
506  /* now see if we can alloc a new tracker */
507  dt = DefragTrackerAlloc();
508  if (dt == NULL) {
509  return NULL;
510  }
511 
512  /* tracker is initialized but *unlocked* */
513  }
514  } else {
515  /* tracker has been recycled before it went into the spare queue */
516 
517  /* tracker is initialized (recylced) but *unlocked* */
518  }
519 
520  (void) SC_ATOMIC_ADD(defragtracker_counter, 1);
521  SCMutexLock(&dt->lock);
522  return dt;
523 }
524 
525 /* DefragGetTrackerFromHash
526  *
527  * Hash retrieval function for trackers. Looks up the hash bucket containing the
528  * tracker pointer. Then compares the packet with the found tracker to see if it is
529  * the tracker we need. If it isn't, walk the list until the right tracker is found.
530  *
531  * returns a *LOCKED* tracker or NULL
532  */
534 {
535  DefragTracker *dt = NULL;
536 
537  /* get the key to our bucket */
538  uint32_t key = DefragHashGetKey(p);
539  /* get our hash bucket and lock it */
541  DRLOCK_LOCK(hb);
542 
543  /* see if the bucket already has a tracker */
544  if (hb->head == NULL) {
545  dt = DefragTrackerGetNew(p);
546  if (dt == NULL) {
547  DRLOCK_UNLOCK(hb);
548  return NULL;
549  }
550 
551  /* tracker is locked */
552  hb->head = dt;
553  hb->tail = dt;
554 
555  /* got one, now lock, initialize and return */
556  DefragTrackerInit(dt,p);
557 
558  DRLOCK_UNLOCK(hb);
559  return dt;
560  }
561 
562  /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
563  dt = hb->head;
564 
565  /* see if this is the tracker we are looking for */
566  if (dt->remove || DefragTrackerCompare(dt, p) == 0) {
567  DefragTracker *pdt = NULL; /* previous tracker */
568 
569  while (dt) {
570  pdt = dt;
571  dt = dt->hnext;
572 
573  if (dt == NULL) {
574  dt = pdt->hnext = DefragTrackerGetNew(p);
575  if (dt == NULL) {
576  DRLOCK_UNLOCK(hb);
577  return NULL;
578  }
579  hb->tail = dt;
580 
581  /* tracker is locked */
582 
583  dt->hprev = pdt;
584 
585  /* initialize and return */
586  DefragTrackerInit(dt,p);
587 
588  DRLOCK_UNLOCK(hb);
589  return dt;
590  }
591 
592  if (DefragTrackerCompare(dt, p) != 0) {
593  /* we found our tracker, lets put it on top of the
594  * hash list -- this rewards active trackers */
595  if (dt->hnext) {
596  dt->hnext->hprev = dt->hprev;
597  }
598  if (dt->hprev) {
599  dt->hprev->hnext = dt->hnext;
600  }
601  if (dt == hb->tail) {
602  hb->tail = dt->hprev;
603  }
604 
605  dt->hnext = hb->head;
606  dt->hprev = NULL;
607  hb->head->hprev = dt;
608  hb->head = dt;
609 
610  /* found our tracker, lock & return */
611  SCMutexLock(&dt->lock);
612  (void) DefragTrackerIncrUsecnt(dt);
613  DRLOCK_UNLOCK(hb);
614  return dt;
615  }
616  }
617  }
618 
619  /* lock & return */
620  SCMutexLock(&dt->lock);
621  (void) DefragTrackerIncrUsecnt(dt);
622  DRLOCK_UNLOCK(hb);
623  return dt;
624 }
625 
626 /** \brief look up a tracker in the hash
627  *
628  * \param a address to look up
629  *
630  * \retval h *LOCKED* tracker or NULL
631  */
633 {
634  DefragTracker *dt = NULL;
635 
636  /* get the key to our bucket */
637  uint32_t key = DefragHashGetKey(p);
638  /* get our hash bucket and lock it */
640  DRLOCK_LOCK(hb);
641 
642  /* see if the bucket already has a tracker */
643  if (hb->head == NULL) {
644  DRLOCK_UNLOCK(hb);
645  return dt;
646  }
647 
648  /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
649  dt = hb->head;
650 
651  /* see if this is the tracker we are looking for */
652  if (DefragTrackerCompare(dt, p) == 0) {
653  while (dt) {
654  dt = dt->hnext;
655 
656  if (dt == NULL) {
657  DRLOCK_UNLOCK(hb);
658  return dt;
659  }
660 
661  if (DefragTrackerCompare(dt, p) != 0) {
662  /* we found our tracker, lets put it on top of the
663  * hash list -- this rewards active tracker */
664  if (dt->hnext) {
665  dt->hnext->hprev = dt->hprev;
666  }
667  if (dt->hprev) {
668  dt->hprev->hnext = dt->hnext;
669  }
670  if (dt == hb->tail) {
671  hb->tail = dt->hprev;
672  }
673 
674  dt->hnext = hb->head;
675  dt->hprev = NULL;
676  hb->head->hprev = dt;
677  hb->head = dt;
678 
679  /* found our tracker, lock & return */
680  SCMutexLock(&dt->lock);
681  (void) DefragTrackerIncrUsecnt(dt);
682  DRLOCK_UNLOCK(hb);
683  return dt;
684  }
685  }
686  }
687 
688  /* lock & return */
689  SCMutexLock(&dt->lock);
690  (void) DefragTrackerIncrUsecnt(dt);
691  DRLOCK_UNLOCK(hb);
692  return dt;
693 }
694 
695 /** \internal
696  * \brief Get a tracker from the hash directly.
697  *
698  * Called in conditions where the spare queue is empty and memcap is reached.
699  *
700  * Walks the hash until a tracker can be freed. "defragtracker_prune_idx" atomic int makes
701  * sure we don't start at the top each time since that would clear the top of
702  * the hash leading to longer and longer search times under high pressure (observed).
703  *
704  * \retval dt tracker or NULL
705  */
706 static DefragTracker *DefragTrackerGetUsedDefragTracker(void)
707 {
708  uint32_t idx = SC_ATOMIC_GET(defragtracker_prune_idx) % defrag_config.hash_size;
709  uint32_t cnt = defrag_config.hash_size;
710 
711  while (cnt--) {
712  if (++idx >= defrag_config.hash_size)
713  idx = 0;
714 
716 
717  if (DRLOCK_TRYLOCK(hb) != 0)
718  continue;
719 
720  DefragTracker *dt = hb->tail;
721  if (dt == NULL) {
722  DRLOCK_UNLOCK(hb);
723  continue;
724  }
725 
726  if (SCMutexTrylock(&dt->lock) != 0) {
727  DRLOCK_UNLOCK(hb);
728  continue;
729  }
730 
731  /** never prune a tracker that is used by a packets
732  * we are currently processing in one of the threads */
733  if (SC_ATOMIC_GET(dt->use_cnt) > 0) {
734  DRLOCK_UNLOCK(hb);
735  SCMutexUnlock(&dt->lock);
736  continue;
737  }
738 
739  /* remove from the hash */
740  if (dt->hprev != NULL)
741  dt->hprev->hnext = dt->hnext;
742  if (dt->hnext != NULL)
743  dt->hnext->hprev = dt->hprev;
744  if (hb->head == dt)
745  hb->head = dt->hnext;
746  if (hb->tail == dt)
747  hb->tail = dt->hprev;
748 
749  dt->hnext = NULL;
750  dt->hprev = NULL;
751  DRLOCK_UNLOCK(hb);
752 
754 
755  SCMutexUnlock(&dt->lock);
756 
757  (void) SC_ATOMIC_ADD(defragtracker_prune_idx, (defrag_config.hash_size - cnt));
758  return dt;
759  }
760 
761  return NULL;
762 }
763 
764 
util-byte.h
DefragTrackerFreeFrags
void DefragTrackerFreeFrags(DefragTracker *tracker)
Free all frags associated with a tracker.
Definition: defrag.c:152
DefragLookupTrackerFromHash
DefragTracker * DefragLookupTrackerFromHash(Packet *p)
look up a tracker in the hash
Definition: defrag-hash.c:632
DefragHashKey4
struct DefragHashKey4_ DefragHashKey4
IPV6_EXTHDR_GET_FH_ID
#define IPV6_EXTHDR_GET_FH_ID(p)
Definition: decode-ipv6.h:130
SC_ATOMIC_DESTROY
#define SC_ATOMIC_DESTROY(name)
Destroy the lock used to protect this variable.
Definition: util-atomic.h:97
DefragHashKey4_::src
uint32_t src
Definition: defrag-hash.c:368
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:174
DefragTracker_::hnext
struct DefragTracker_ * hnext
Definition: defrag.h:116
defragtracker_hash
DefragTrackerHashRow * defragtracker_hash
Definition: defrag-hash.c:29
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
Initialize the previously declared atomic variable and it's lock.
Definition: util-atomic.h:81
DefragTrackerIncrUsecnt
#define DefragTrackerIncrUsecnt(dt)
Definition: defrag-hash.c:121
Packet_::vlan_id
uint16_t vlan_id[2]
Definition: decode.h:436
SCFree
#define SCFree(a)
Definition: util-mem.h:322
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:207
DefragTrackerHashRow_
Definition: defrag-hash.h:58
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
ParseSizeStringU64
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition: util-misc.c:203
DefragTrackerSpareQueueGetSize
uint32_t DefragTrackerSpareQueueGetSize(void)
Definition: defrag-hash.c:77
DefragTrackerQueueDestroy
void DefragTrackerQueueDestroy(DefragTrackerQueue *q)
Destroy a tracker queue.
Definition: defrag-queue.c:59
DefragHashKey4_::id
uint32_t id
Definition: defrag-hash.c:369
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:107
DefragHashKey6_::vlan_id
uint16_t vlan_id[2]
Definition: defrag-hash.c:381
DefragTrackerHashRow
struct DefragTrackerHashRow_ DefragTrackerHashRow
DefragTrackerClearMemory
void DefragTrackerClearMemory(DefragTracker *dt)
Definition: defrag-hash.c:156
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
DefragTracker_::host_timeout
uint32_t host_timeout
Definition: defrag.h:108
DefragTracker_::vlan_id
uint16_t vlan_id[2]
Definition: defrag.h:88
RandomGet
long int RandomGet(void)
Definition: util-random.c:129
DefragHashKey6_::u32
uint32_t u32[10]
Definition: defrag-hash.c:383
DefragTrackerGetMemuse
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition: defrag-hash.c:71
defrag-config.h
DefragPolicyGetHostTimeout
int DefragPolicyGetHostTimeout(Packet *p)
Definition: defrag-config.c:91
DefragTrackerMoveToSpare
void DefragTrackerMoveToSpare(DefragTracker *h)
Definition: defrag-hash.c:82
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
DefragTracker_::hprev
struct DefragTracker_ * hprev
Definition: defrag.h:117
DefragTracker_::lock
SCMutex lock
Definition: defrag.h:85
DefragConfig_::hash_size
uint32_t hash_size
Definition: defrag-hash.h:73
DefragGetOsPolicy
uint8_t DefragGetOsPolicy(Packet *p)
Get the defrag policy based on the destination address of the packet.
Definition: defrag.c:943
SC_ERR_SIZE_PARSE
@ SC_ERR_SIZE_PARSE
Definition: util-error.h:230
DefragConfig_::prealloc
uint32_t prealloc
Definition: defrag-hash.h:74
DefragHashKey6_::src
uint32_t src[4]
Definition: defrag-hash.c:379
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:331
DefragTrackerHashRow_::head
DefragTracker * head
Definition: defrag-hash.h:60
DefragTracker_::remove
uint8_t remove
Definition: defrag.h:102
defrag_config
DefragConfig defrag_config
Definition: defrag-hash.c:30
DefragTrackerQueueLen
uint32_t DefragTrackerQueueLen(DefragTrackerQueue *q)
Definition: defrag-queue.c:136
DefragTracker_::seen_last
uint8_t seen_last
Definition: defrag.h:100
DefragTracker_::policy
uint8_t policy
Definition: defrag.h:95
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
DefragTracker_
Definition: defrag.h:84
DEFRAG_CHECK_MEMCAP
#define DEFRAG_CHECK_MEMCAP(size)
check if a memory alloc would fit in the memcap
Definition: defrag-hash.h:84
DefragTrackerHashRow_::tail
DefragTracker * tail
Definition: defrag-hash.h:61
DEFRAG_DEFAULT_HASHSIZE
#define DEFRAG_DEFAULT_HASHSIZE
Definition: defrag-hash.c:162
DefragHashKey6_::dst
uint32_t dst[4]
Definition: defrag-hash.c:379
defrag-queue.h
DefragTrackerGetMemcap
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
Definition: defrag-hash.c:60
DefragTracker_::dst_addr
Address dst_addr
Definition: defrag.h:105
DefragTrackerDequeue
DefragTracker * DefragTrackerDequeue(DefragTrackerQueue *q)
remove a tracker from the queue
Definition: defrag-queue.c:103
IPV4_GET_IPID
#define IPV4_GET_IPID(p)
Definition: decode-ipv4.h:129
DRLOCK_LOCK
#define DRLOCK_LOCK(fb)
Definition: defrag-hash.h:51
SCMalloc
#define SCMalloc(a)
Definition: util-mem.h:222
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:265
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:124
FALSE
#define FALSE
Definition: suricata-common.h:34
DefragHashKey6
struct DefragHashKey6_ DefragHashKey6
Packet_
Definition: decode.h:408
DefragTracker_::id
uint32_t id
Definition: defrag.h:90
DefragTracker_::src_addr
Address src_addr
Definition: defrag.h:104
SCCalloc
#define SCCalloc(nm, a)
Definition: util-mem.h:253
DRLOCK_UNLOCK
#define DRLOCK_UNLOCK(fb)
Definition: defrag-hash.h:53
conf.h
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:501
SC_ERR_DEFRAG_INIT
@ SC_ERR_DEFRAG_INIT
Definition: util-error.h:245
DefragTrackerQueue_
Definition: defrag-queue.h:41
DEFRAG_DEFAULT_MEMCAP
#define DEFRAG_DEFAULT_MEMCAP
Definition: defrag-hash.c:163
DefragTrackerRelease
void DefragTrackerRelease(DefragTracker *t)
Definition: defrag-hash.c:150
DefragTracker_::proto
uint8_t proto
Definition: defrag.h:93
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
DRLOCK_DESTROY
#define DRLOCK_DESTROY(fb)
Definition: defrag-hash.h:50
DefragTrackerQueue_::len
uint32_t len
Definition: defrag-queue.h:45
StringParseUint32
int StringParseUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:304
suricata-common.h
DefragGetTrackerFromHash
DefragTracker * DefragGetTrackerFromHash(Packet *p)
Definition: defrag-hash.c:533
DefragTrackerQueueInit
DefragTrackerQueue * DefragTrackerQueueInit(DefragTrackerQueue *q)
Definition: defrag-queue.c:34
DefragHashKey4_
Definition: defrag-hash.c:365
DefragHashKey6_
Definition: defrag-hash.c:376
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
DefragConfig_
Definition: defrag-hash.h:70
CMP_DEFRAGTRACKER
#define CMP_DEFRAGTRACKER(d1, d2, id)
Definition: defrag-hash.c:450
util-hash-lookup3.h
DRLOCK_TRYLOCK
#define DRLOCK_TRYLOCK(fb)
Definition: defrag-hash.h:52
DefragTrackerSetMemcap
int DefragTrackerSetMemcap(uint64_t size)
Update memcap value.
Definition: defrag-hash.c:45
DefragConfig_::hash_rand
uint32_t hash_rand
Definition: defrag-hash.h:72
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
DefragTrackerEnqueue
void DefragTrackerEnqueue(DefragTrackerQueue *q, DefragTracker *dt)
add a tracker to a queue
Definition: defrag-queue.c:70
DRLOCK_INIT
#define DRLOCK_INIT(fb)
Definition: defrag-hash.h:49
DefragTracker_::af
uint8_t af
Definition: defrag.h:97
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
DefragHashKey4_::vlan_id
uint16_t vlan_id[2]
Definition: defrag-hash.c:370
IP_GET_IPPROTO
#define IP_GET_IPPROTO(p)
Definition: decode.h:264
util-random.h
DEFRAG_DEFAULT_PREALLOC
#define DEFRAG_DEFAULT_PREALLOC
Definition: defrag-hash.c:164
DefragTrackerDecrUsecnt
#define DefragTrackerDecrUsecnt(dt)
Definition: defrag-hash.c:123
defrag-hash.h
Packet_::dst
Address dst
Definition: decode.h:414
DefragHashKey6_::id
uint32_t id
Definition: defrag-hash.c:380
DefragInitConfig
void DefragInitConfig(char quiet)
initialize the configuration
Definition: defrag-hash.c:168
DefragHashShutdown
void DefragHashShutdown(void)
shutdown the flow engine
Definition: defrag-hash.c:301
Packet_::ip6h
IPV6Hdr * ip6h
Definition: decode.h:503
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:192
util-misc.h
COPY_ADDRESS
#define COPY_ADDRESS(a, b)
Definition: decode.h:126
SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, defrag_memuse)
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:120
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:252
DefragHashKey4_::dst
uint32_t dst
Definition: defrag-hash.c:368
DefragHashKey4_::u32
uint32_t u32[4]
Definition: defrag-hash.c:372
Packet_::src
Address src
Definition: decode.h:413
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118