DPDK-HASH LIBRARY

来源:互联网 发布:gcf软件 编辑:程序博客网 时间:2024/06/08 13:40

0x01 缘由

     在产品开发过程中,有一个高效的hash表,影响产品的性能的提高。如流表的查找。

0x02 DPDK Hash算法

     1、介绍

     DPDK提供了一个Hash库为快速的查询创建hash表。这个哈希表为了高效搜索做了一些数据结构的优化,用一个唯一的KEY标识hash表中的每一个条目。为了提高DPDK Hash的性能,要求所有的keys的字节大小和创建hash表时设定的大小一样。

     2、Hash API介绍

     Hash表配置有两个参数:
          a.hash条目总数;
          b.key的字节大小;
     也支持更加底层的相关阐述设计,列如:
          a.将key转换成一个桶索引的Hash 函数;
     这个hash输出的主要方法有(增、删、查):
          a.用key添加一个条目:
 int32_t rte_hash_add_key(const struct rte_hash * h,const void * key)  非线程安全            Add a key to an existing hash table. This operation is not multi-thread safe and should only be called from one thread.          Parameters              h    Hash table to add the key to.              key    Key to add to the hash table.          Returns              -EINVAL if the parameters are invalid.              -ENOSPC if there is no space in the hash for this key.          A positive value that can be used by the caller as an offset into an array of user data. This value is unique for this key.

         b.用key删除一个条目: 
  int32_t rte_hash_del_key_with_hash(const struct rte_hash * h,const void * key,hash_sig_t sig)  非线程安全            Remove a key from an existing hash table. This operation is not multi-thread safe and should only be called from one thread.           Parameters              h    Hash table to remove the key from.              key    Key to remove from the hash table.              sig    Precomputed hash value for 'key'.          Returns              -EINVAL if the parameters are invalid.              -ENOENT if the key is not found.          A positive value that can be used by the caller as an offset into an array of user data. This value is unique for this key, and is the same value that was returned when the key was added.

          c.用key查找一个条目: 
         int32_t rte_hash_lookup(const struct rte_hash * h,const void *    key)多线程安全          Find a key in the hash table. This operation is multi-thread safe.          Parameters               h    Hash table to look in.               key    Key to find.          Returns-EINVAL if the parameters are invalid.          -ENOENT if the key is not found.          A positive value that can be used by the caller as an offset into an array of user data. This value is unique for this key, and is the same value that was returned when the key was added.

     多处理器支持:
          这个hash库能够用于多处理器环境,仅仅查找操作是线程安全的。唯一可以用在单进程模式下使用的功能是 rte_hash_set_cmp_func(),它设置一个自定义比较函数,它被分配给 一个函数指针(因此,它不支持多进程模式)。
     实现细节:
          这个hash表分为两个主表:
          第一个表是一组条目,进一步分为桶,每个桶中具有相同数量的连续数组条目。每个条目包含一个key计算出的主和备hashe值和第二张表的索引。
          第二张表存储所有的key和与key相关的数据。
          hash冲突解决方法: Cuckoo Hash(布谷鸟散列)。为了解决哈希冲突的问题而提出,利用较少的计算换取较大的空间。占用空间少,查询速度快。经常应用于Bloom Filter和内存管理中。之所以起这个名字是因为布谷鸟生性贪婪,不自己筑巢,而是在别的鸟巢里面鸟蛋孵化,先成长的幼鸟会将别的鸟蛋挤出,这样独享“母爱”,类似于哈希冲突处理过程。
    算法描述:
    使用hashA、hashB计算对应的key位置:
    1、两个位置均为空,则任选一个插入;
    2、两个位置中一个为空,则插入到空的那个位置
    3、两个位置均不为空,则踢出一个位置后插入,被踢出的<key,value>对调用该算法,再执行该算法找其另一个位置,循环直到插入成功。
    如果被踢出的次数达到一定的阈值,则认为hash表已满,并进行重新哈希rehash
优化(减少哈希碰撞):
    1、将一维改成多维,使用桶(bucket)的4路槽位(slot);
    2、一个key对应多个value;
    3、增加哈希函数,从两个增加到多个;
    4、增加哈希表,类似于第一种;
     
     对于每个输入Key,有两个可能的桶(主和备/相对位置)用于这个key存储在hash表中,因此当以一个key去查询一个条目时只需检查桶里面的唯一一个条目。他的查找速度性能提高是通过减少扫描条目。而不是线性查找所有条目。散列使用散列函数(可配置)将输入Key转换为4字节Key签名。这个桶索引是用key签名和hash表总数取摸。
     一旦这个桶被识别,hash表的添加、删除、查找操作范围在这些桶中减少(非常可能条目是在主桶中)。
     为了加快在桶内搜索逻辑,每个hash条目和4字节签名条目存储在一起。对于key值较多,对于较大的密钥大小,将输入密钥与存储桶中的密钥进行比较可能比比较4字节字节要花费更多的时间 输入密钥的性质抵抗来自桶的密钥的签名。 所以签名 首先进行比较,仅在签名匹配时完成全键比较。 仍然需要完整的关键比较,因为来自同一桶的两个输入键仍然可以 可能具有相同的4字节散列签名,尽管这个事件对于哈希来说比较少见 功能为该组输入键提供良好的均匀分布。

0x03 结论

    hash算法在网络安全开发过程中是一个比较重要的数据结构,尤其是在大流量处理的情况下,利用五元组做为hash key值,快速构建流表和查找流。
原创粉丝点击