199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2017 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_TABLE_HASH_H__ 699a2dd95SBruce Richardson #define __INCLUDE_RTE_TABLE_HASH_H__ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson /** 999a2dd95SBruce Richardson * @file 1099a2dd95SBruce Richardson * RTE Table Hash 1199a2dd95SBruce Richardson * 1299a2dd95SBruce Richardson * These tables use the exact match criterion to uniquely associate data to 1399a2dd95SBruce Richardson * lookup keys. 1499a2dd95SBruce Richardson * 1599a2dd95SBruce Richardson * Hash table types: 1699a2dd95SBruce Richardson * 1. Entry add strategy on bucket full: 1799a2dd95SBruce Richardson * a. Least Recently Used (LRU): One of the existing keys in the bucket is 1899a2dd95SBruce Richardson * deleted and the new key is added in its place. The number of keys in 1999a2dd95SBruce Richardson * each bucket never grows bigger than 4. The logic to pick the key to 2099a2dd95SBruce Richardson * be dropped from the bucket is LRU. The hash table lookup operation 2199a2dd95SBruce Richardson * maintains the order in which the keys in the same bucket are hit, so 2299a2dd95SBruce Richardson * every time a key is hit, it becomes the new Most Recently Used (MRU) 2399a2dd95SBruce Richardson * key, i.e. the most unlikely candidate for drop. When a key is added 2499a2dd95SBruce Richardson * to the bucket, it also becomes the new MRU key. When a key needs to 2599a2dd95SBruce Richardson * be picked and dropped, the most likely candidate for drop, i.e. the 2699a2dd95SBruce Richardson * current LRU key, is always picked. The LRU logic requires maintaining 2799a2dd95SBruce Richardson * specific data structures per each bucket. Use-cases: flow cache, etc. 2899a2dd95SBruce Richardson * b. Extendable bucket (ext): The bucket is extended with space for 4 more 2999a2dd95SBruce Richardson * keys. This is done by allocating additional memory at table init time, 3099a2dd95SBruce Richardson * which is used to create a pool of free keys (the size of this pool is 3199a2dd95SBruce Richardson * configurable and always a multiple of 4). On key add operation, the 3299a2dd95SBruce Richardson * allocation of a group of 4 keys only happens successfully within the 3399a2dd95SBruce Richardson * limit of free keys, otherwise the key add operation fails. On key 3499a2dd95SBruce Richardson * delete operation, a group of 4 keys is freed back to the pool of free 3599a2dd95SBruce Richardson * keys when the key to be deleted is the only key that was used within 3699a2dd95SBruce Richardson * its group of 4 keys at that time. On key lookup operation, if the 3799a2dd95SBruce Richardson * current bucket is in extended state and a match is not found in the 3899a2dd95SBruce Richardson * first group of 4 keys, the search continues beyond the first group of 3999a2dd95SBruce Richardson * 4 keys, potentially until all keys in this bucket are examined. The 4099a2dd95SBruce Richardson * extendable bucket logic requires maintaining specific data structures 4199a2dd95SBruce Richardson * per table and per each bucket. Use-cases: flow table, etc. 4299a2dd95SBruce Richardson * 2. Key size: 4399a2dd95SBruce Richardson * a. Configurable key size 4499a2dd95SBruce Richardson * b. Single key size (8-byte, 16-byte or 32-byte key size) 453e4c5be9SThomas Monjalon */ 463e4c5be9SThomas Monjalon 4799a2dd95SBruce Richardson #include <stdint.h> 4899a2dd95SBruce Richardson 4999a2dd95SBruce Richardson #include "rte_table.h" 5099a2dd95SBruce Richardson 51*719834a6SMattias Rönnblom #ifdef __cplusplus 52*719834a6SMattias Rönnblom extern "C" { 53*719834a6SMattias Rönnblom #endif 54*719834a6SMattias Rönnblom 5599a2dd95SBruce Richardson /** Hash function */ 5699a2dd95SBruce Richardson typedef uint64_t (*rte_table_hash_op_hash)( 5799a2dd95SBruce Richardson void *key, 5899a2dd95SBruce Richardson void *key_mask, 5999a2dd95SBruce Richardson uint32_t key_size, 6099a2dd95SBruce Richardson uint64_t seed); 6199a2dd95SBruce Richardson 6299a2dd95SBruce Richardson /** Hash table parameters */ 6399a2dd95SBruce Richardson struct rte_table_hash_params { 6499a2dd95SBruce Richardson /** Name */ 6599a2dd95SBruce Richardson const char *name; 6699a2dd95SBruce Richardson 6799a2dd95SBruce Richardson /** Key size (number of bytes) */ 6899a2dd95SBruce Richardson uint32_t key_size; 6999a2dd95SBruce Richardson 7099a2dd95SBruce Richardson /** Byte offset within packet meta-data where the key is located */ 7199a2dd95SBruce Richardson uint32_t key_offset; 7299a2dd95SBruce Richardson 7399a2dd95SBruce Richardson /** Key mask */ 7499a2dd95SBruce Richardson uint8_t *key_mask; 7599a2dd95SBruce Richardson 7699a2dd95SBruce Richardson /** Number of keys */ 7799a2dd95SBruce Richardson uint32_t n_keys; 7899a2dd95SBruce Richardson 7999a2dd95SBruce Richardson /** Number of buckets */ 8099a2dd95SBruce Richardson uint32_t n_buckets; 8199a2dd95SBruce Richardson 8299a2dd95SBruce Richardson /** Hash function */ 8399a2dd95SBruce Richardson rte_table_hash_op_hash f_hash; 8499a2dd95SBruce Richardson 8599a2dd95SBruce Richardson /** Seed value for the hash function */ 8699a2dd95SBruce Richardson uint64_t seed; 8799a2dd95SBruce Richardson }; 8899a2dd95SBruce Richardson 8999a2dd95SBruce Richardson /** Extendable bucket hash table operations */ 9099a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_ext_ops; 9199a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key8_ext_ops; 9299a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key16_ext_ops; 9399a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key32_ext_ops; 9499a2dd95SBruce Richardson 9599a2dd95SBruce Richardson /** LRU hash table operations */ 9699a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_lru_ops; 9799a2dd95SBruce Richardson 9899a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key8_lru_ops; 9999a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key16_lru_ops; 10099a2dd95SBruce Richardson extern struct rte_table_ops rte_table_hash_key32_lru_ops; 10199a2dd95SBruce Richardson 10299a2dd95SBruce Richardson #ifdef __cplusplus 10399a2dd95SBruce Richardson } 10499a2dd95SBruce Richardson #endif 10599a2dd95SBruce Richardson 10699a2dd95SBruce Richardson #endif 107