xref: /dpdk/doc/guides/prog_guide/hash_lib.rst (revision fc1f2750a3ec6da919e3c86e59d56f34ec97154b)
1*fc1f2750SBernard Iremonger..  BSD LICENSE
2*fc1f2750SBernard Iremonger    Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3*fc1f2750SBernard Iremonger    All rights reserved.
4*fc1f2750SBernard Iremonger
5*fc1f2750SBernard Iremonger    Redistribution and use in source and binary forms, with or without
6*fc1f2750SBernard Iremonger    modification, are permitted provided that the following conditions
7*fc1f2750SBernard Iremonger    are met:
8*fc1f2750SBernard Iremonger
9*fc1f2750SBernard Iremonger    * Redistributions of source code must retain the above copyright
10*fc1f2750SBernard Iremonger    notice, this list of conditions and the following disclaimer.
11*fc1f2750SBernard Iremonger    * Redistributions in binary form must reproduce the above copyright
12*fc1f2750SBernard Iremonger    notice, this list of conditions and the following disclaimer in
13*fc1f2750SBernard Iremonger    the documentation and/or other materials provided with the
14*fc1f2750SBernard Iremonger    distribution.
15*fc1f2750SBernard Iremonger    * Neither the name of Intel Corporation nor the names of its
16*fc1f2750SBernard Iremonger    contributors may be used to endorse or promote products derived
17*fc1f2750SBernard Iremonger    from this software without specific prior written permission.
18*fc1f2750SBernard Iremonger
19*fc1f2750SBernard Iremonger    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*fc1f2750SBernard Iremonger    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*fc1f2750SBernard Iremonger    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*fc1f2750SBernard Iremonger    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*fc1f2750SBernard Iremonger    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*fc1f2750SBernard Iremonger    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*fc1f2750SBernard Iremonger    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*fc1f2750SBernard Iremonger    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*fc1f2750SBernard Iremonger    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*fc1f2750SBernard Iremonger    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*fc1f2750SBernard Iremonger    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*fc1f2750SBernard Iremonger
31*fc1f2750SBernard Iremonger.. _Hash_Library:
32*fc1f2750SBernard Iremonger
33*fc1f2750SBernard IremongerHash Library
34*fc1f2750SBernard Iremonger============
35*fc1f2750SBernard Iremonger
36*fc1f2750SBernard IremongerThe Intel® DPDK provides a Hash Library for creating hash table for fast lookup.
37*fc1f2750SBernard IremongerThe hash table is a data structure optimized for searching through a set of entries that are each identified by a unique key.
38*fc1f2750SBernard IremongerFor increased performance the Intel® DPDK Hash requires that all the keys have the same number of bytes which is set at the hash creation time.
39*fc1f2750SBernard Iremonger
40*fc1f2750SBernard IremongerHash API Overview
41*fc1f2750SBernard Iremonger-----------------
42*fc1f2750SBernard Iremonger
43*fc1f2750SBernard IremongerThe main configuration parameters for the hash are:
44*fc1f2750SBernard Iremonger
45*fc1f2750SBernard Iremonger*   Total number of hash entries
46*fc1f2750SBernard Iremonger
47*fc1f2750SBernard Iremonger*   Size of the key in bytes
48*fc1f2750SBernard Iremonger
49*fc1f2750SBernard IremongerThe hash also allows the configuration of some low-level implementation related parameters such as:
50*fc1f2750SBernard Iremonger
51*fc1f2750SBernard Iremonger*   Hash function to translate the key into a bucket index
52*fc1f2750SBernard Iremonger
53*fc1f2750SBernard Iremonger*   Number of entries per bucket
54*fc1f2750SBernard Iremonger
55*fc1f2750SBernard IremongerThe main methods exported by the hash are:
56*fc1f2750SBernard Iremonger
57*fc1f2750SBernard Iremonger*   Add entry with key: The key is provided as input. If a new entry is successfully added to the hash for the specified key,
58*fc1f2750SBernard Iremonger    or there is already an entry in the hash for the specified key, then the position of the entry is returned.
59*fc1f2750SBernard Iremonger    If the operation was not successful, for example due to lack of free entries in the hash, then a negative value is returned;
60*fc1f2750SBernard Iremonger
61*fc1f2750SBernard Iremonger*   Delete entry with key: The key is provided as input. If an entry with the specified key is found in the hash,
62*fc1f2750SBernard Iremonger    then the entry is removed from the hash and the position where the entry was found in the hash is returned.
63*fc1f2750SBernard Iremonger    If no entry with the specified key exists in the hash, then a negative value is returned
64*fc1f2750SBernard Iremonger
65*fc1f2750SBernard Iremonger*   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),
66*fc1f2750SBernard Iremonger    then the position of the entry is returned, otherwise (lookup miss) a negative value is returned.
67*fc1f2750SBernard Iremonger
68*fc1f2750SBernard IremongerThe current hash implementation handles the key management only.
69*fc1f2750SBernard IremongerThe actual data associated with each key has to be managed by the user using a separate table that
70*fc1f2750SBernard Iremongermirrors the hash in terms of number of entries and position of each entry,
71*fc1f2750SBernard Iremongeras shown in the Flow Classification use case describes in the following sections.
72*fc1f2750SBernard Iremonger
73*fc1f2750SBernard IremongerThe 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.
74*fc1f2750SBernard IremongerHowever, 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.
75*fc1f2750SBernard Iremonger
76*fc1f2750SBernard IremongerImplementation Details
77*fc1f2750SBernard Iremonger----------------------
78*fc1f2750SBernard Iremonger
79*fc1f2750SBernard IremongerThe hash table is implemented as an array of entries which is further divided into buckets,
80*fc1f2750SBernard Iremongerwith the same number of consecutive array entries in each bucket.
81*fc1f2750SBernard IremongerFor any input key, there is always a single bucket where that key can be stored in the hash,
82*fc1f2750SBernard Iremongertherefore only the entries within that bucket need to be examined when the key is looked up.
83*fc1f2750SBernard IremongerThe lookup speed is achieved by reducing the number of entries to be scanned from the total
84*fc1f2750SBernard Iremongernumber of hash entries down to the number of entries in a hash bucket,
85*fc1f2750SBernard Iremongeras opposed to the basic method of linearly scanning all the entries in the array.
86*fc1f2750SBernard IremongerThe hash uses a hash function (configurable) to translate the input key into a 4-byte key signature.
87*fc1f2750SBernard IremongerThe bucket index is the key signature modulo the number of hash buckets.
88*fc1f2750SBernard IremongerOnce the bucket is identified, the scope of the hash add,
89*fc1f2750SBernard Iremongerdelete and lookup operations is reduced to the entries in that bucket.
90*fc1f2750SBernard Iremonger
91*fc1f2750SBernard IremongerTo 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.
92*fc1f2750SBernard IremongerFor large key sizes, comparing the input key against a key from the bucket can take significantly more time than
93*fc1f2750SBernard Iremongercomparing the 4-byte signature of the input key against the signature of a key from the bucket.
94*fc1f2750SBernard IremongerTherefore, the signature comparison is done first and the full key comparison done only when the signatures matches.
95*fc1f2750SBernard IremongerThe full key comparison is still necessary, as two input keys from the same bucket can still potentially have the same 4-byte hash signature,
96*fc1f2750SBernard Iremongeralthough this event is relatively rare for hash functions providing good uniform distributions for the set of input keys.
97*fc1f2750SBernard Iremonger
98*fc1f2750SBernard IremongerUse Case: Flow Classification
99*fc1f2750SBernard Iremonger-----------------------------
100*fc1f2750SBernard Iremonger
101*fc1f2750SBernard IremongerFlow classification is used to map each input packet to the connection/flow it belongs to.
102*fc1f2750SBernard IremongerThis operation is necessary as the processing of each input packet is usually done in the context of their connection,
103*fc1f2750SBernard Iremongerso the same set of operations is applied to all the packets from the same flow.
104*fc1f2750SBernard Iremonger
105*fc1f2750SBernard IremongerApplications using flow classification typically have a flow table to manage, with each separate flow having an entry associated with it in this table.
106*fc1f2750SBernard IremongerThe size of the flow table entry is application specific, with typical values of 4, 16, 32 or 64 bytes.
107*fc1f2750SBernard Iremonger
108*fc1f2750SBernard IremongerEach application using flow classification typically has a mechanism defined to uniquely identify a flow based on
109*fc1f2750SBernard Iremongera number of fields read from the input packet that make up the flow key.
110*fc1f2750SBernard IremongerOne example is to use the DiffServ 5-tuple made up of the following fields of the IP and transport layer packet headers:
111*fc1f2750SBernard IremongerSource IP Address, Destination IP Address, Protocol, Source Port, Destination Port.
112*fc1f2750SBernard Iremonger
113*fc1f2750SBernard IremongerThe Intel® DPDK hash provides a generic method to implement an application specific flow classification mechanism.
114*fc1f2750SBernard IremongerGiven 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
115*fc1f2750SBernard Iremongerwith the hash key size set to the number of bytes in the selected flow key.
116*fc1f2750SBernard Iremonger
117*fc1f2750SBernard IremongerThe flow table operations on the application side are described below:
118*fc1f2750SBernard Iremonger
119*fc1f2750SBernard Iremonger*   Add flow: Add the flow key to hash.
120*fc1f2750SBernard Iremonger    If the returned position is valid, use it to access the flow entry in the flow table for adding a new flow or
121*fc1f2750SBernard Iremonger    updating the information associated with an existing flow.
122*fc1f2750SBernard Iremonger    Otherwise, the flow addition failed, for example due to lack of free entries for storing new flows.
123*fc1f2750SBernard Iremonger
124*fc1f2750SBernard Iremonger*   Delete flow: Delete the flow key from the hash. If the returned position is valid,
125*fc1f2750SBernard Iremonger    use it to access the flow entry in the flow table to invalidate the information associated with the flow.
126*fc1f2750SBernard Iremonger
127*fc1f2750SBernard Iremonger*   Lookup flow: Lookup for the flow key in the hash.
128*fc1f2750SBernard Iremonger    If the returned position is valid (flow lookup hit), use the returned position to access the flow entry in the flow table.
129*fc1f2750SBernard Iremonger    Otherwise (flow lookup miss) there is no flow registered for the current packet.
130*fc1f2750SBernard Iremonger
131*fc1f2750SBernard IremongerReferences
132*fc1f2750SBernard Iremonger----------
133*fc1f2750SBernard Iremonger
134*fc1f2750SBernard Iremonger*   Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching (2nd Edition), 1998, Addison-Wesley Professional
135