TSG-13347 Steering Service开发流表

This commit is contained in:
luwenpeng
2023-01-10 16:30:34 +08:00
parent 4639d9ceaa
commit fcc99918fd
13 changed files with 466 additions and 63 deletions

View File

@@ -1,30 +0,0 @@
#include "conntable.h"
#include "uthash.h"
#include "log.h"
struct conntable *conntable_create()
{
return NULL;
}
void conntable_destory(struct conntable *table)
{
}
void conntable_add(struct conntable *table, uint64_t streamid, void *val)
{
}
void conntable_del(struct conntable *table, uint64_t streamid)
{
}
void *conntable_search_by_streamid(struct conntable *table, uint64_t streamid)
{
return NULL;
}
void *conntable_search_by_tuple4(struct conntable *table, struct tuple4 *tuplekey)
{
return NULL;
}

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <arpa/inet.h>
#include "stream_addr.h"
char *stream_addr_to_str(const struct stream_addr *addr)
{
char *str_ret = NULL;
if (addr->addr_type == STREAM_ADDR_TYPE_V4)
{
char src_addr[INET_ADDRSTRLEN] = {0};
char dst_addr[INET_ADDRSTRLEN] = {0};
uint16_t src_port = ntohs((uint16_t)addr->addr_v4.src_port);
uint16_t dst_port = ntohs((uint16_t)addr->addr_v4.dst_port);
inet_ntop(AF_INET, &addr->addr_v4.src_addr, src_addr, sizeof(src_addr));
inet_ntop(AF_INET, &addr->addr_v4.dst_addr, dst_addr, sizeof(dst_addr));
asprintf(&str_ret, "%s %u %s %u", src_addr, src_port, dst_addr, dst_port);
}
if (addr->addr_type == STREAM_ADDR_TYPE_V6)
{
char src_addr[INET6_ADDRSTRLEN] = {0};
char dst_addr[INET6_ADDRSTRLEN] = {0};
uint16_t src_port = ntohs((uint16_t)addr->addr_v6.src_port);
uint16_t dst_port = ntohs((uint16_t)addr->addr_v6.dst_port);
inet_ntop(AF_INET6, &addr->addr_v6.src_addr, src_addr, sizeof(src_addr));
inet_ntop(AF_INET6, &addr->addr_v6.dst_addr, dst_addr, sizeof(dst_addr));
asprintf(&str_ret, "%s %u %s %u", src_addr, src_port, dst_addr, dst_port);
}
return str_ret;
}

161
common/src/stream_table.cpp Normal file
View File

@@ -0,0 +1,161 @@
#include <assert.h>
#include "stream_table.h"
#include "log.h"
struct stream_table
{
struct stream_node *streamid_root;
struct stream_node *streamaddr_root;
};
// Note: key_streamaddr must be initialized by memset(0) before use !!!
struct stream_table *stream_table_create()
{
struct stream_table *table = (struct stream_table *)calloc(1, sizeof(struct stream_table));
assert(table);
return table;
}
void stream_table_destory(struct stream_table *table)
{
if (table)
{
struct stream_node *temp = NULL;
struct stream_node *node = NULL;
HASH_ITER(hh1, table->streamid_root, node, temp)
{
HASH_DELETE(hh1, table->streamid_root, node);
HASH_DELETE(hh2, table->streamaddr_root, node);
if (node->val_freecb && node->val_data)
{
node->val_freecb(node->val_data);
}
free(node);
node = NULL;
}
free(table);
table = NULL;
}
}
// key_streamaddr : deep copy
// val_data : shallow copy (malloc by user, free by val_freecb)
int stream_table_insert(struct stream_table *table, uint64_t key_streamid, const struct stream_addr *key_streamaddr, void *val_data, const fn_free_cb *val_freecb)
{
struct stream_node *temp = NULL;
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
if (temp)
{
LOG_DEBUG("stream table insert: key %lu exists", key_streamid);
return -1;
}
temp = (struct stream_node *)calloc(1, sizeof(struct stream_node));
assert(temp);
temp->key_streamid = key_streamid;
memcpy(&temp->key_streamaddr, key_streamaddr, sizeof(struct stream_addr));
temp->val_data = val_data;
temp->val_freecb = val_freecb;
HASH_ADD(hh1, table->streamid_root, key_streamid, sizeof(temp->key_streamid), temp);
HASH_ADD(hh2, table->streamaddr_root, key_streamaddr, sizeof(temp->key_streamaddr), temp);
LOG_DEBUG("stream table insert: key %lu success", key_streamid);
return 0;
}
int stream_table_delete_by_streamid(struct stream_table *table, uint64_t key_streamid)
{
struct stream_node *temp = NULL;
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
if (!temp)
{
LOG_DEBUG("stream table delete: key %lu not exists", key_streamid);
return -1;
}
HASH_DELETE(hh1, table->streamid_root, temp);
HASH_DELETE(hh2, table->streamaddr_root, temp);
if (temp->val_freecb && temp->val_data)
{
temp->val_freecb(temp->val_data);
}
free(temp);
temp = NULL;
LOG_DEBUG("stream table delete: key %lu success", key_streamid);
return 0;
}
int stream_table_delete_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr)
{
struct stream_node *temp = NULL;
char *addr_str = stream_addr_to_str(key_streamaddr);
HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp);
if (!temp)
{
LOG_DEBUG("stream table delete: key %s not exists", addr_str);
free(addr_str);
return -1;
}
HASH_DELETE(hh1, table->streamid_root, temp);
HASH_DELETE(hh2, table->streamaddr_root, temp);
if (temp->val_freecb && temp->val_data)
{
temp->val_freecb(temp->val_data);
}
free(temp);
temp = NULL;
LOG_DEBUG("stream table delete: key %s success", addr_str);
free(addr_str);
addr_str = NULL;
return 0;
}
struct stream_node *stream_table_search_by_streamid(struct stream_table *table, uint64_t key_streamid)
{
struct stream_node *temp = NULL;
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
if (!temp)
{
LOG_DEBUG("stream table search: key %lu not exists", key_streamid);
return NULL;
}
LOG_DEBUG("stream table search: key %lu success", key_streamid);
return temp;
}
struct stream_node *stream_table_search_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr)
{
struct stream_node *temp = NULL;
char *addr_str = stream_addr_to_str(key_streamaddr);
HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp);
if (!temp)
{
LOG_DEBUG("stream table search: key %s not exists", addr_str);
free(addr_str);
addr_str = NULL;
return NULL;
}
LOG_DEBUG("stream table search: key %s success", addr_str);
free(addr_str);
addr_str = NULL;
return temp;
}