Replace session queue with kernel list

This commit is contained in:
luwenpeng
2024-03-29 17:45:41 +08:00
parent 6e422ecb8d
commit 8e527a0f4c
12 changed files with 60 additions and 241 deletions

View File

@@ -4,6 +4,7 @@
#define HASH_KEYCMP(a, b, len) HASH_KEYCMP_OVERWRITE(a, b, len)
#include "session_table.h"
#include "session_private.h"
#include "list.h"
struct session_table
{
@@ -12,8 +13,7 @@ struct session_table
void *arg;
uint64_t count;
struct session *head; // Least recently used
struct session *tail; // Most recently used
struct list_head lru_queue;
};
/******************************************************************************
@@ -45,56 +45,6 @@ static int HASH_KEYCMP_OVERWRITE(const void *key_a, const void *key_b, size_t le
return -1;
}
// add session to the tail of the list
static void add_session_to_list(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
sess->next_ptr = NULL;
sess->prev_ptr = NULL;
if (table->head == NULL && table->tail == NULL)
{
table->head = sess;
table->tail = sess;
}
else
{
table->tail->next_ptr = sess;
sess->prev_ptr = table->tail;
table->tail = sess;
}
}
static void del_session_from_list(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
if (sess->prev_ptr)
{
sess->prev_ptr->next_ptr = sess->next_ptr;
}
else
{
table->head = sess->next_ptr;
}
if (sess->next_ptr)
{
sess->next_ptr->prev_ptr = sess->prev_ptr;
}
else
{
table->tail = sess->prev_ptr;
}
}
/******************************************************************************
* Public API
******************************************************************************/
@@ -102,9 +52,13 @@ static void del_session_from_list(struct session_table *table, struct session *s
struct session_table *session_table_new()
{
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
if (table == NULL)
{
return NULL;
}
table->count = 0;
table->head = NULL;
table->tail = NULL;
INIT_LIST_HEAD(&table->lru_queue);
return table;
}
@@ -117,6 +71,7 @@ void session_table_free(struct session_table *table)
struct session *tmp = NULL;
HASH_ITER(hh, table->root, node, tmp)
{
list_del(&node->lru);
HASH_DELETE(hh, table->root, node);
if (table->free_cb && node)
{
@@ -161,8 +116,8 @@ int session_table_add(struct session_table *table, const struct tuple6 *tuple, s
return -1;
}
list_add_tail(&sess->lru, &table->lru_queue);
HASH_ADD(hh, table->root, tuple, sizeof(sess->tuple), sess);
add_session_to_list(table, sess);
table->count++;
return 0;
@@ -181,12 +136,12 @@ void session_table_del(struct session_table *table, const struct tuple6 *tuple)
return;
}
list_del(&sess->lru);
HASH_DELETE(hh, table->root, sess);
if (table->free_cb && sess)
{
table->free_cb(sess, table->arg);
}
del_session_from_list(table, sess);
table->count--;
}
@@ -209,8 +164,8 @@ struct session *session_table_find_tuple(struct session_table *table, const stru
if (sess)
{
del_session_from_list(table, sess);
add_session_to_list(table, sess);
list_del(&sess->lru);
list_add_tail(&sess->lru, &table->lru_queue);
}
return sess;
@@ -223,15 +178,12 @@ struct session *session_table_find_lru(struct session_table *table)
return NULL;
}
return table->head;
}
struct session *session_table_find_mru(struct session_table *table)
{
if (table == NULL)
if (list_empty(&table->lru_queue))
{
return NULL;
}
return table->tail;
else
{
return list_first_entry(&table->lru_queue, struct session, lru);
}
}