vpp 处理数据报文 hash table 代码
时间: 2023-11-28 22:04:42 浏览: 78
vpp 源代码
以下是使用 VPP 处理数据报文的哈希表(hash table)代码示例:
```
#include <vppinfra/hash.h>
typedef struct {
u32 key;
u32 value;
} my_hash_entry_t;
typedef struct {
u32 *buckets;
my_hash_entry_t *entries;
u32 num_buckets;
u32 num_entries;
} my_hash_table_t;
static void
my_hash_table_init (my_hash_table_t * ht, u32 num_buckets, u32 num_entries)
{
clib_memset (ht, 0, sizeof (*ht));
ht->num_buckets = num_buckets;
vec_validate (ht->buckets, num_buckets - 1);
ht->num_entries = num_entries;
vec_resize (ht->entries, num_entries);
}
static u32
my_hash_table_lookup (my_hash_table_t * ht, u32 key)
{
u32 bucket_index = key & (ht->num_buckets - 1);
u32 entry_index = ht->buckets[bucket_index];
while (entry_index != ~0)
{
if (ht->entries[entry_index].key == key)
return ht->entries[entry_index].value;
entry_index = ht->entries[entry_index].next_index;
}
return ~0;
}
static void
my_hash_table_add (my_hash_table_t * ht, u32 key, u32 value)
{
u32 bucket_index = key & (ht->num_buckets - 1);
u32 entry_index = ht->buckets[bucket_index];
while (entry_index != ~0)
{
if (ht->entries[entry_index].key == key)
{
ht->entries[entry_index].value = value;
return;
}
entry_index = ht->entries[entry_index].next_index;
}
entry_index = ht->entries - ht->entries;
ht->entries[entry_index].key = key;
ht->entries[entry_index].value = value;
ht->entries[entry_index].next_index = ht->buckets[bucket_index];
ht->buckets[bucket_index] = entry_index;
}
```
上述代码使用 VPP 的哈希表(hash table)实现了查找和添加操作。哈希表的初始化使用 my_hash_table_init 函数,接收哈希表、桶的数量和条目数量作为参数。哈希表查找使用 my_hash_table_lookup 函数,接收哈希表和要查找的键作为参数。哈希表添加使用 my_hash_table_add 函数,接收哈希表、键和值作为参数。
阅读全文