mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-10 17:58:09 +00:00
261 lines
6.9 KiB
C++
261 lines
6.9 KiB
C++
/*
|
|
*
|
|
* (C) 2013-19 - ntop.org
|
|
*
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the ho2pe 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
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*
|
|
*/
|
|
|
|
#include "ntop_includes.h"
|
|
|
|
/* ************************************ */
|
|
|
|
GenericHash::GenericHash(NetworkInterface *_iface, u_int _num_hashes,
|
|
u_int _max_hash_size, const char *_name) {
|
|
num_hashes = _num_hashes, max_hash_size = _max_hash_size, current_size = 0;
|
|
last_entry_id = 0;
|
|
purge_step = max_val(num_hashes / PURGE_FRACTION, 1);
|
|
name = strdup(_name ? _name : "???");
|
|
|
|
iface = _iface;
|
|
table = new GenericHashEntry*[num_hashes];
|
|
for(u_int i = 0; i < num_hashes; i++)
|
|
table[i] = NULL;
|
|
|
|
locks = new RwLock*[num_hashes];
|
|
for(u_int i = 0; i < num_hashes; i++) locks[i] = new RwLock();
|
|
|
|
last_purged_hash = _num_hashes - 1;
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
GenericHash::~GenericHash() {
|
|
cleanup();
|
|
|
|
delete[] table;
|
|
|
|
for(u_int i = 0; i < num_hashes; i++) delete(locks[i]);
|
|
delete[] locks;
|
|
free(name);
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
void GenericHash::cleanup() {
|
|
for(u_int i = 0; i < num_hashes; i++)
|
|
if(table[i] != NULL) {
|
|
GenericHashEntry *head = table[i];
|
|
|
|
while(head) {
|
|
GenericHashEntry *next = head->next();
|
|
|
|
delete(head);
|
|
head = next;
|
|
}
|
|
table[i] = NULL;
|
|
}
|
|
current_size = 0;
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
bool GenericHash::add(GenericHashEntry *h, bool do_lock) {
|
|
if(hasEmptyRoom()) {
|
|
u_int32_t hash = (h->key() % num_hashes);
|
|
|
|
if(do_lock)
|
|
locks[hash]->wrlock(__FILE__, __LINE__);
|
|
|
|
h->set_hash_entry_id(last_entry_id++);
|
|
h->set_next(table[hash]);
|
|
table[hash] = h;
|
|
current_size++;
|
|
|
|
if(do_lock)
|
|
locks[hash]->unlock(__FILE__, __LINE__);
|
|
|
|
return(true);
|
|
} else
|
|
return(false);
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
bool GenericHash::walk(u_int32_t *begin_slot,
|
|
bool walk_all,
|
|
bool (*walker)(GenericHashEntry *h, void *user_data, bool *entryMatched),
|
|
void *user_data, bool walk_idle) {
|
|
bool found = false;
|
|
u_int16_t tot_matched = 0;
|
|
|
|
for(u_int hash_id = *begin_slot; hash_id < num_hashes; hash_id++) {
|
|
if(table[hash_id] != NULL) {
|
|
GenericHashEntry *head;
|
|
|
|
#if WALK_DEBUG
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[walk] Locking %d [%p]", hash_id, locks[hash_id]);
|
|
#endif
|
|
|
|
locks[hash_id]->rdlock(__FILE__, __LINE__);
|
|
head = table[hash_id];
|
|
|
|
while(head) {
|
|
GenericHashEntry *next = head->next();
|
|
|
|
/* FIXX get_state() does not always match idle() as the latter can be
|
|
* overriden (e.g. Flow), leading to wolking entries that are actually
|
|
* idle even with walk_idle = false, what about using idle() here? */
|
|
|
|
if(!head->idle()
|
|
|| (walk_idle && (head->get_state() == hash_entry_state_idle))) {
|
|
bool matched = false;
|
|
bool rc = walker(head, user_data, &matched);
|
|
|
|
if(matched) tot_matched++;
|
|
|
|
if(rc) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
head = next;
|
|
} /* while */
|
|
|
|
locks[hash_id]->unlock(__FILE__, __LINE__);
|
|
// ntop->getTrace()->traceEvent(TRACE_NORMAL, "[walk] Unlocked %d", hash_id);
|
|
|
|
if((tot_matched >= MIN_NUM_HASH_WALK_ELEMS) /* At least a few entries have been returned */
|
|
&& (!walk_all)) {
|
|
u_int32_t next_slot = (hash_id == (num_hashes-1)) ? 0 /* start over */ : (hash_id+1);
|
|
|
|
*begin_slot = next_slot;
|
|
#if WALK_DEBUG
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[walk] Over [nextSlot: %u][hash_id: %u][tot_matched: %u]",
|
|
next_slot, hash_id, tot_matched);
|
|
#endif
|
|
|
|
return(found);
|
|
}
|
|
|
|
if(found)
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!found)
|
|
*begin_slot = 0 /* start over */;
|
|
|
|
#if WALK_DEBUG
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[walk] Over [tot_matched: %u]", tot_matched);
|
|
#endif
|
|
|
|
return(found);
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
/*
|
|
Bucket Lifecycle
|
|
|
|
Active -> Idle -> Ready to be Purged -> Purged
|
|
*/
|
|
|
|
u_int GenericHash::purgeIdle(bool force_idle) {
|
|
u_int i, num_purged = 0, buckets_checked = 0;
|
|
time_t now = time(NULL);
|
|
/* Visit all entries when force_idle is true */
|
|
u_int visit_fraction = !force_idle ? purge_step : num_hashes;
|
|
|
|
#if WALK_DEBUG
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[%s @ %s] Begin purgeIdle() [begin index: %u][purge step: %u][force_idle: %u]",
|
|
name, iface->get_name(), last_purged_hash, visit_fraction, force_idle ? 1 : 0);
|
|
#endif
|
|
|
|
for(u_int j = 0; j < visit_fraction; j++) {
|
|
if(++last_purged_hash == num_hashes) last_purged_hash = 0;
|
|
i = last_purged_hash;
|
|
|
|
if(table[i] != NULL) {
|
|
GenericHashEntry *head, *prev = NULL;
|
|
|
|
// ntop->getTrace()->traceEvent(TRACE_NORMAL, "[purge] Locking %d", i);
|
|
if(!locks[i]->trywrlock(__FILE__, __LINE__))
|
|
continue; /* Busy, will retry next round */
|
|
|
|
head = table[i];
|
|
|
|
while(head) {
|
|
HashEntryState head_state = head->get_state();
|
|
GenericHashEntry *next = head->next();
|
|
|
|
buckets_checked++;
|
|
|
|
switch(head_state) {
|
|
case hash_entry_state_ready_to_be_purged:
|
|
if(!prev)
|
|
table[i] = next;
|
|
else
|
|
prev->set_next(next);
|
|
|
|
num_purged++, current_size--;
|
|
delete(head);
|
|
head = next;
|
|
continue;
|
|
|
|
case hash_entry_state_allocated:
|
|
/* TCP flows with 3WH not yet completed (or collected with no TCP flags) fall here */
|
|
case hash_entry_state_flow_notyetdetected:
|
|
/* UDP flows or TCP flows for which the 3WH is completed but protocol hasn't been detected yet */
|
|
head->housekeep(now);
|
|
if(force_idle) head->set_hash_entry_state_idle();
|
|
break;
|
|
|
|
case hash_entry_state_flow_protocoldetected:
|
|
if(force_idle) head->set_hash_entry_state_idle();
|
|
break;
|
|
|
|
case hash_entry_state_idle:
|
|
/* Skip as this is handled by periodic activities thread */
|
|
break;
|
|
|
|
case hash_entry_state_active:
|
|
if(force_idle
|
|
|| (head->is_hash_entry_state_idle_transition_possible()
|
|
&& head->is_hash_entry_state_idle_transition_ready()))
|
|
head->set_hash_entry_state_idle();
|
|
break;
|
|
}
|
|
|
|
prev = head;
|
|
head = next;
|
|
} /* while */
|
|
|
|
locks[i]->unlock(__FILE__, __LINE__);
|
|
// ntop->getTrace()->traceEvent(TRACE_NORMAL, "[purge] Unlocked %d", i);
|
|
}
|
|
}
|
|
|
|
#if WALK_DEBUG
|
|
if(/* (num_purged > 0) && */ (!strcmp(name, "FlowHash")))
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL,
|
|
"[%s @ %s] purgeIdle() [num_purged: %u][num_checked: %u][end index: %u][current_size: %u]",
|
|
name, iface->get_name(), num_purged, buckets_checked, last_purged_hash, current_size);
|
|
#endif
|
|
|
|
return(num_purged);
|
|
}
|