简介
EFD(Elastic Flow Distributor)是利用完美散列(perfect hashing)实现的一个流分发库,具有以下优点:
- 不需要存储key,查找性能不受key大小影响。
- 目标/值(target/value)可以任意值。
- 支持数百万量级的key。
- 支持多核CPU性能扩展。
因此,一般可以用于实现在数据中心进行负载均衡流量分发的场景;具体可参考示例examples/server_node_efd
示例
API
源码
#include <rte_byteorder.h>
#include <rte_efd.h>
static void efd_test(void)
{
unsigned int i;
unsigned int num_flows = 4;
unsigned int num_nodes = 2;
int32_t ret;
uint32_t ip_dst;
uint8_t socket_id = rte_socket_id();
uint64_t node_id;
struct rte_efd_table *efd_table;
void *key_ptrs;
uint32_t ipv4_dst_ip;
/* create table */
efd_table = rte_efd_create("flow table", 4, sizeof(uint32_t), 1 << socket_id, socket_id);
if (efd_table == NULL)
rte_exit(EXIT_FAILURE, "Problem creating the flow table\n");
/* add flows in table */
for (i = 0; i < num_flows; i++) {
node_id = i % num_nodes + 1;
ip_dst = rte_cpu_to_be_32(i);
ret = rte_efd_update(efd_table, socket_id,
(void *)&ip_dst, (efd_value_t)node_id);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Unable to add entry %u in "
"EFD table\n", i);
}
printf("EFD table: Adding 0x%x keys\n", num_flows);
/* lookup flows in table */
for (i = 0; i < num_flows+1; i++) {
ipv4_dst_ip = rte_cpu_to_be_32(i);
key_ptrs = &ipv4_dst_ip;
ret = rte_efd_lookup(efd_table, socket_id, key_ptrs);
if (ret == 0) {
printf("EFD table: lookup keys %d not found\n", i);
} else {
printf("EFD table: lookup keys %d to node:%d\n", i, ret);
}
}
}
运行结果
EFD table: Adding 0x4 keys
EFD table: lookup keys 0 to node:1
EFD table: lookup keys 1 to node:2
EFD table: lookup keys 2 to node:1
EFD table: lookup keys 3 to node:2
EFD table: lookup keys 4 not found