session table can search session by tuple6 or tuple4 or id

This commit is contained in:
luwenpeng
2024-04-24 11:39:15 +08:00
parent 74f0504d3d
commit 476c5bac56
5 changed files with 233 additions and 105 deletions

View File

@@ -6,7 +6,9 @@
struct session_table
{
struct session *root;
struct session *root_tuple6;
struct session *root_tuple4;
struct session *root_sessid;
session_free_cb free_cb;
void *arg;
uint64_t count;
@@ -18,29 +20,62 @@ struct session_table
* Private API
******************************************************************************/
static void HASH_FUNCTION_OVERWRITE(const struct tuple6 *tuple, unsigned int keylen, uint32_t *hashv)
static void HASH_FUNCTION_OVERWRITE(const void *keyptr, unsigned int keylen, uint32_t *hashv)
{
*hashv = tuple6_hash(tuple);
switch (keylen)
{
case sizeof(struct tuple6):
*hashv = tuple6_hash((const struct tuple6 *)keyptr);
break;
case sizeof(struct tuple4):
*hashv = tuple4_hash((const struct tuple4 *)keyptr);
break;
case sizeof(uint64_t):
HASH_JEN(keyptr, keylen, *hashv);
break;
default:
assert(0);
break;
}
}
static int HASH_KEYCMP_OVERWRITE(const void *key_a, const void *key_b, size_t len)
{
struct tuple6 *tuple_a = (struct tuple6 *)key_a;
struct tuple6 *tuple_b = (struct tuple6 *)key_b;
if (tuple6_cmp(tuple_a, tuple_b) == 0)
if (len == sizeof(struct tuple6))
{
return 0;
if (tuple6_cmp((const struct tuple6 *)key_a, (const struct tuple6 *)key_b) == -1)
{
struct tuple6 rev;
tuple6_reverse((const struct tuple6 *)key_b, &rev);
return tuple6_cmp((const struct tuple6 *)key_a, &rev);
}
else
{
return 0;
}
}
struct tuple6 reversed;
tuple6_reverse(tuple_b, &reversed);
if (tuple6_cmp(tuple_a, &reversed) == 0)
else if (len == sizeof(struct tuple4))
{
return 0;
if (tuple4_cmp((const struct tuple4 *)key_a, (const struct tuple4 *)key_b) == -1)
{
struct tuple4 rev;
tuple4_reverse((const struct tuple4 *)key_b, &rev);
return tuple4_cmp((const struct tuple4 *)key_a, &rev);
}
else
{
return 0;
}
}
else if (len == sizeof(uint64_t))
{
return memcmp(key_a, key_b, len);
}
else
{
assert(0);
return -1;
}
return -1;
}
/******************************************************************************
@@ -67,10 +102,12 @@ void session_table_free(struct session_table *table)
{
struct session *node = NULL;
struct session *tmp = NULL;
HASH_ITER(hh, table->root, node, tmp)
HASH_ITER(hh1, table->root_tuple6, node, tmp)
{
list_del(&node->lru);
HASH_DELETE(hh, table->root, node);
HASH_DELETE(hh1, table->root_tuple6, node);
HASH_DELETE(hh2, table->root_tuple4, node);
HASH_DELETE(hh3, table->root_sessid, node);
if (table->free_cb && node)
{
table->free_cb(node, table->arg);
@@ -109,13 +146,15 @@ int session_table_add(struct session_table *table, const struct tuple6 *tuple, s
return -1;
}
if (session_table_find_tuple(table, tuple))
if (session_table_find_tuple6(table, tuple))
{
return -1;
}
HASH_ADD(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess);
HASH_ADD(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess);
HASH_ADD(hh3, table->root_sessid, id, sizeof(uint64_t), sess);
list_add_tail(&sess->lru, &table->lru_queue);
HASH_ADD(hh, table->root, tuple, sizeof(sess->tuple), sess);
table->count++;
return 0;
@@ -128,14 +167,16 @@ void session_table_del(struct session_table *table, const struct tuple6 *tuple)
return;
}
struct session *sess = session_table_find_tuple(table, tuple);
struct session *sess = session_table_find_tuple6(table, tuple);
if (sess == NULL)
{
return;
}
list_del(&sess->lru);
HASH_DELETE(hh, table->root, sess);
HASH_DELETE(hh1, table->root_tuple6, sess);
HASH_DELETE(hh2, table->root_tuple4, sess);
HASH_DELETE(hh3, table->root_sessid, sess);
if (table->free_cb && sess)
{
table->free_cb(sess, table->arg);
@@ -143,7 +184,7 @@ void session_table_del(struct session_table *table, const struct tuple6 *tuple)
table->count--;
}
struct session *session_table_find_tuple(struct session_table *table, const struct tuple6 *tuple)
struct session *session_table_find_sessid(struct session_table *table, uint64_t id)
{
if (table == NULL)
{
@@ -151,15 +192,43 @@ struct session *session_table_find_tuple(struct session_table *table, const stru
}
struct session *sess = NULL;
HASH_FIND(hh, table->root, tuple, sizeof(struct tuple6), sess);
if (sess == NULL)
HASH_FIND(hh3, table->root_sessid, &id, sizeof(uint64_t), sess);
if (sess)
{
struct tuple6 out;
memset(&out, 0, sizeof(struct tuple6));
tuple6_reverse(tuple, &out);
HASH_FIND(hh, table->root, &out, sizeof(struct tuple6), sess);
list_del(&sess->lru);
list_add_tail(&sess->lru, &table->lru_queue);
}
return sess;
}
struct session *session_table_find_tuple6(struct session_table *table, const struct tuple6 *tuple)
{
if (table == NULL)
{
return NULL;
}
struct session *sess = NULL;
HASH_FIND(hh1, table->root_tuple6, tuple, sizeof(struct tuple6), sess);
if (sess)
{
list_del(&sess->lru);
list_add_tail(&sess->lru, &table->lru_queue);
}
return sess;
}
struct session *session_table_find_tuple4(struct session_table *table, const struct tuple4 *tuple)
{
if (table == NULL)
{
return NULL;
}
struct session *sess = NULL;
HASH_FIND(hh2, table->root_tuple4, tuple, sizeof(struct tuple4), sess);
if (sess)
{
list_del(&sess->lru);