mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-10 17:58:09 +00:00
263 lines
6.5 KiB
C++
263 lines
6.5 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;
|
|
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 Mutex*[num_hashes];
|
|
for(u_int i = 0; i < num_hashes; i++) locks[i] = new Mutex();
|
|
|
|
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(false) {
|
|
char buf[256];
|
|
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "%s(): adding %s/%u",
|
|
__FUNCTION__, h->get_string_key(buf, sizeof(buf)), h->key());
|
|
}
|
|
|
|
if(do_lock)
|
|
locks[hash]->lock(__FILE__, __LINE__);
|
|
|
|
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]->lock(__FILE__, __LINE__);
|
|
head = table[hash_id];
|
|
|
|
while(head) {
|
|
HashEntryState head_state = head->get_state();
|
|
GenericHashEntry *next = head->next();
|
|
|
|
if(head_state == hash_entry_state_active
|
|
|| (walk_idle && head_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() {
|
|
u_int i, num_purged = 0, buckets_checked = 0;
|
|
time_t now = time(NULL);
|
|
|
|
if(ntop->getGlobals()->isShutdown())
|
|
return(0);
|
|
|
|
#if WALK_DEBUG
|
|
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[%s @ %s] Begin purgeIdle() [begin index: %u][purge step: %u]",
|
|
name, iface->get_name(), last_purged_hash, purge_step);
|
|
#endif
|
|
|
|
for(u_int j = 0; j < purge_step; 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);
|
|
locks[i]->lock(__FILE__, __LINE__);
|
|
head = table[i];
|
|
|
|
while(head) {
|
|
HashEntryState head_state = head->get_state();
|
|
GenericHashEntry *next = head->next();
|
|
|
|
buckets_checked++;
|
|
if(head_state == hash_entry_state_ready_to_be_purged) {
|
|
if(prev == NULL) {
|
|
table[i] = next;
|
|
} else {
|
|
prev->set_next(next);
|
|
}
|
|
|
|
num_purged++, current_size--;
|
|
delete(head);
|
|
head = next;
|
|
} else {
|
|
/* Do the chores */
|
|
head->housekeep(now);
|
|
|
|
if(head_state == hash_entry_state_active && head->idle())
|
|
head->set_hash_entry_state_idle();
|
|
|
|
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);
|
|
}
|
|
|
|
/* ************************************ */
|
|
|
|
GenericHashEntry* GenericHash::findByKey(u_int32_t key) {
|
|
u_int32_t hash = key % num_hashes;
|
|
GenericHashEntry *head = table[hash];
|
|
|
|
if(head == NULL) return(NULL);
|
|
|
|
locks[hash]->lock(__FILE__, __LINE__);
|
|
while(head) {
|
|
if(!head->idle() && head->key() == key)
|
|
break;
|
|
else
|
|
head = head->next();
|
|
}
|
|
|
|
locks[hash]->unlock(__FILE__, __LINE__);
|
|
|
|
return(head);
|
|
}
|