New upstream version 18.11-rc1
[deb_dpdk.git] / doc / guides / prog_guide / hash_lib.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2015 Intel Corporation.
3     Copyright(c) 2018 Arm Limited.
4
5 .. _Hash_Library:
6
7 Hash Library
8 ============
9
10 The DPDK provides a Hash Library for creating hash table for fast lookup.
11 The hash table is a data structure optimized for searching through a set of entries that are each identified by a unique key.
12 For increased performance the DPDK Hash requires that all the keys have the same number of bytes which is set at the hash creation time.
13
14 Hash API Overview
15 -----------------
16
17 The main configuration parameters for the hash are:
18
19 *   Total number of hash entries
20
21 *   Size of the key in bytes
22
23 *   An extra flag used to describe additional settings, for example the multithreading mode of operation (as will be described later)
24
25 The hash also allows the configuration of some low-level implementation related parameters such as:
26
27 *   Hash function to translate the key into a bucket index
28
29 The main methods exported by the hash are:
30
31 *   Add entry with key: The key is provided as input. If a new entry is successfully added to the hash for the specified key,
32     or there is already an entry in the hash for the specified key, then the position of the entry is returned.
33     If the operation was not successful, for example due to lack of free entries in the hash, then a negative value is returned;
34
35 *   Delete entry with key: The key is provided as input. If an entry with the specified key is found in the hash,
36     then the entry is removed from the hash and the position where the entry was found in the hash is returned.
37     If no entry with the specified key exists in the hash, then a negative value is returned
38
39 *   Lookup for entry with key: The key is provided as input. If an entry with the specified key is found in the hash (lookup hit),
40     then the position of the entry is returned, otherwise (lookup miss) a negative value is returned.
41
42 Apart from these methods explained above, the API provides the user with few more options:
43
44 *   Add / lookup / delete with key and precomputed hash: Both the key and its precomputed hash are provided as input. This allows
45     the user to perform these operations faster, as hash is already computed.
46
47 *   Add / lookup with key and data: A pair of key-value is provided as input. This allows the user to store
48     not only the key, but also data which may be either a 8-byte integer or a pointer to external data (if data size is more than 8 bytes).
49
50 *   Combination of the two options above: User can provide key, precomputed hash and data.
51
52 *   Ability to not free the position of the entry in the hash upon calling delete. This is useful for multi-threaded scenarios where
53     readers continue to use the position even after the entry is deleted.
54
55 Also, the API contains a method to allow the user to look up entries in bursts, achieving higher performance
56 than looking up individual entries, as the function prefetches next entries at the time it is operating
57 with the first ones, which reduces significantly the impact of the necessary memory accesses.
58
59
60 The actual data associated with each key can be either managed by the user using a separate table that
61 mirrors the hash in terms of number of entries and position of each entry,
62 as shown in the Flow Classification use case describes in the following sections,
63 or stored in the hash table itself.
64
65 The example hash tables in the L2/L3 Forwarding sample applications defines which port to forward a packet to based on a packet flow identified by the five-tuple lookup.
66 However, this table could also be used for more sophisticated features and provide many other functions and actions that could be performed on the packets and flows.
67
68 Multi-process support
69 ---------------------
70
71 The hash library can be used in a multi-process environment.
72 The only function that can only be used in single-process mode is rte_hash_set_cmp_func(), which sets up
73 a custom compare function, which is assigned to a function pointer (therefore, it is not supported in
74 multi-process mode).
75
76
77 Multi-thread support
78 ---------------------
79
80 The hash library supports multithreading, and the user specifies the needed mode of operation at the creation time of the hash table
81 by appropriately setting the flag. In all modes of operation lookups are thread-safe meaning lookups can be called from multiple
82 threads concurrently.
83
84 For concurrent writes, and concurrent reads and writes the following flag values define the corresponding modes of operation:
85
86 *  If the multi-writer flag (RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) is set, multiple threads writing to the table is allowed.
87    Key add, delete, and table reset are protected from other writer threads. With only this flag set, readers are not protected from ongoing writes.
88
89 *  If the read/write concurrency (RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) is set, multithread read/write operation is safe
90    (i.e., application does not need to stop the readers from accessing the hash table until writers finish their updates. Readers and writers can operate on the table concurrently).
91    The library uses a reader-writer lock to provide the concurrency.
92
93 *  In addition to these two flag values, if the transactional memory flag (RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT) is also set,
94    the reader-writer lock will use hardware transactional memory to guarantee thread safety as long as it is supported by the hardware (for example the IntelĀ® TSX support).
95    If the platform supports IntelĀ® TSX, it is advised to set the transactional memory flag, as this will speed up concurrent table operations.
96    Otherwise concurrent operations will be slower because of the overhead associated with the software locking mechanisms.
97
98 *  If lock free read/write concurrency (RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) is set, read/write concurrency is provided without using reader-writer lock.
99    For platforms (ex: current Arm based platforms), that do not support transactional memory, it is advised to set this flag to achieve greater scalability in performance.
100
101 *  If, do not free on delete (RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL) flag is set, the position of the entry in the hash is not freed upon calling delete. This flag is enabled
102    by default when lock free read/write concurrency is set. The application should free the position after all the readers have stopped referencing the position.
103    Where required, the application can make use of RCU mechanisms to determine when the readers have stopped referencing the position.
104
105 Implementation Details
106 ----------------------
107
108 The hash table has two main tables:
109
110 * First table is an array of entries which is further divided into buckets,
111   with the same number of consecutive array entries in each bucket. Each entry contains the computed primary
112   and secondary hashes of a given key (explained below), and an index to the second table.
113
114 * The second table is an array of all the keys stored in the hash table and its data associated to each key.
115
116 The hash library uses the cuckoo hash method to resolve collisions.
117 For any input key, there are two possible buckets (primary and secondary/alternative location)
118 where that key can be stored in the hash, therefore only the entries within those bucket need to be examined
119 when the key is looked up.
120 The lookup speed is achieved by reducing the number of entries to be scanned from the total
121 number of hash entries down to the number of entries in the two hash buckets,
122 as opposed to the basic method of linearly scanning all the entries in the array.
123 The hash uses a hash function (configurable) to translate the input key into a 4-byte key signature.
124 The bucket index is the key signature modulo the number of hash buckets.
125
126 Once the buckets are identified, the scope of the hash add,
127 delete and lookup operations is reduced to the entries in those buckets (it is very likely that entries are in the primary bucket).
128
129 To speed up the search logic within the bucket, each hash entry stores the 4-byte key signature together with the full key for each hash entry.
130 For large key sizes, comparing the input key against a key from the bucket can take significantly more time than
131 comparing the 4-byte signature of the input key against the signature of a key from the bucket.
132 Therefore, the signature comparison is done first and the full key comparison done only when the signatures matches.
133 The full key comparison is still necessary, as two input keys from the same bucket can still potentially have the same 4-byte hash signature,
134 although this event is relatively rare for hash functions providing good uniform distributions for the set of input keys.
135
136 Example of lookup:
137
138 First of all, the primary bucket is identified and entry is likely to be stored there.
139 If signature was stored there, we compare its key against the one provided and return the position
140 where it was stored and/or the data associated to that key if there is a match.
141 If signature is not in the primary bucket, the secondary bucket is looked up, where same procedure
142 is carried out. If there is no match there either, key is considered not to be in the table.
143
144 Example of addition:
145
146 Like lookup, the primary and secondary buckets are identified. If there is an empty slot in
147 the primary bucket, primary and secondary signatures are stored in that slot, key and data (if any) are added to
148 the second table and an index to the position in the second table is stored in the slot of the first table.
149 If there is no space in the primary bucket, one of the entries on that bucket is pushed to its alternative location,
150 and the key to be added is inserted in its position.
151 To know where the alternative bucket of the evicted entry is, the secondary signature is looked up and alternative bucket index
152 is calculated from doing the modulo, as seen above. If there is room in the alternative bucket, the evicted entry
153 is stored in it. If not, same process is repeated (one of the entries gets pushed) until a non full bucket is found.
154 Notice that despite all the entry movement in the first table, the second table is not touched, which would impact
155 greatly in performance.
156
157 In the very unlikely event that table enters in a loop where same entries are being evicted indefinitely,
158 key is considered not able to be stored.
159 With random keys, this method allows the user to get around 90% of the table utilization, without
160 having to drop any stored entry (LRU) or allocate more memory (extended buckets).
161
162
163 Example of deletion:
164
165 Similar to lookup, the key is searched in its primary and secondary buckets. If the key is found, the bucket
166 entry is marked as an empty slot. If the hash table was configured with 'no free on delete' or 'lock free read/write concurrency',
167 the position of the key is not freed. It is the responsibility of the user to free the position while making sure that
168 readers are not referencing the position anymore.
169
170 Entry distribution in hash table
171 --------------------------------
172
173 As mentioned above, Cuckoo hash implementation pushes elements out of their bucket,
174 if there is a new entry to be added which primary location coincides with their current bucket,
175 being pushed to their alternative location.
176 Therefore, as user adds more entries to the hash table, distribution of the hash values
177 in the buckets will change, being most of them in their primary location and a few in
178 their secondary location, which the later will increase, as table gets busier.
179 This information is quite useful, as performance may be lower as more entries
180 are evicted to their secondary location.
181
182 See the tables below showing example entry distribution as table utilization increases.
183
184 .. _table_hash_lib_1:
185
186 .. table:: Entry distribution measured with an example table with 1024 random entries using jhash algorithm
187
188    +--------------+-----------------------+-------------------------+
189    | % Table used | % In Primary location | % In Secondary location |
190    +==============+=======================+=========================+
191    |      25      |         100           |           0             |
192    +--------------+-----------------------+-------------------------+
193    |      50      |         96.1          |           3.9           |
194    +--------------+-----------------------+-------------------------+
195    |      75      |         88.2          |           11.8          |
196    +--------------+-----------------------+-------------------------+
197    |      80      |         86.3          |           13.7          |
198    +--------------+-----------------------+-------------------------+
199    |      85      |         83.1          |           16.9          |
200    +--------------+-----------------------+-------------------------+
201    |      90      |         77.3          |           22.7          |
202    +--------------+-----------------------+-------------------------+
203    |      95.8    |         64.5          |           35.5          |
204    +--------------+-----------------------+-------------------------+
205
206 |
207
208 .. _table_hash_lib_2:
209
210 .. table:: Entry distribution measured with an example table with 1 million random entries using jhash algorithm
211
212    +--------------+-----------------------+-------------------------+
213    | % Table used | % In Primary location | % In Secondary location |
214    +==============+=======================+=========================+
215    |      50      |         96            |           4             |
216    +--------------+-----------------------+-------------------------+
217    |      75      |         86.9          |           13.1          |
218    +--------------+-----------------------+-------------------------+
219    |      80      |         83.9          |           16.1          |
220    +--------------+-----------------------+-------------------------+
221    |      85      |         80.1          |           19.9          |
222    +--------------+-----------------------+-------------------------+
223    |      90      |         74.8          |           25.2          |
224    +--------------+-----------------------+-------------------------+
225    |      94.5    |         67.4          |           32.6          |
226    +--------------+-----------------------+-------------------------+
227
228 .. note::
229
230    Last values on the tables above are the average maximum table
231    utilization with random keys and using Jenkins hash function.
232
233 Use Case: Flow Classification
234 -----------------------------
235
236 Flow classification is used to map each input packet to the connection/flow it belongs to.
237 This operation is necessary as the processing of each input packet is usually done in the context of their connection,
238 so the same set of operations is applied to all the packets from the same flow.
239
240 Applications using flow classification typically have a flow table to manage, with each separate flow having an entry associated with it in this table.
241 The size of the flow table entry is application specific, with typical values of 4, 16, 32 or 64 bytes.
242
243 Each application using flow classification typically has a mechanism defined to uniquely identify a flow based on
244 a number of fields read from the input packet that make up the flow key.
245 One example is to use the DiffServ 5-tuple made up of the following fields of the IP and transport layer packet headers:
246 Source IP Address, Destination IP Address, Protocol, Source Port, Destination Port.
247
248 The DPDK hash provides a generic method to implement an application specific flow classification mechanism.
249 Given a flow table implemented as an array, the application should create a hash object with the same number of entries as the flow table and
250 with the hash key size set to the number of bytes in the selected flow key.
251
252 The flow table operations on the application side are described below:
253
254 *   Add flow: Add the flow key to hash.
255     If the returned position is valid, use it to access the flow entry in the flow table for adding a new flow or
256     updating the information associated with an existing flow.
257     Otherwise, the flow addition failed, for example due to lack of free entries for storing new flows.
258
259 *   Delete flow: Delete the flow key from the hash. If the returned position is valid,
260     use it to access the flow entry in the flow table to invalidate the information associated with the flow.
261
262 *   Free flow: Free flow key position. If 'no free on delete' or 'lock-free read/write concurrency' flags are set,
263     wait till the readers are not referencing the position returned during add/delete flow and then free the position.
264     RCU mechanisms can be used to find out when the readers are not referencing the position anymore.
265
266 *   Lookup flow: Lookup for the flow key in the hash.
267     If the returned position is valid (flow lookup hit), use the returned position to access the flow entry in the flow table.
268     Otherwise (flow lookup miss) there is no flow registered for the current packet.
269
270 References
271 ----------
272
273 *   Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching (2nd Edition), 1998, Addison-Wesley Professional