1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2021 Intel Corporation.
7 DPDK provides a Toeplitz Hash Library
8 to calculate the Toeplitz hash function and to use its properties.
9 The Toeplitz hash function is commonly used in a wide range of NICs
10 to calculate the RSS hash sum to spread the traffic among the queues.
12 .. _figure_rss_queue_assign:
14 .. figure:: img/rss_queue_assign.*
16 RSS queue assignment example
19 Toeplitz hash function API
20 --------------------------
22 There are two functions that provide calculation of the Toeplitz hash sum:
25 * ``rte_softrss_be()``
27 Both of these functions take the parameters:
29 * A pointer to the tuple, containing fields extracted from the packet.
30 * A length of this tuple counted in double words.
31 * A pointer to the RSS hash key corresponding to the one installed on the NIC.
33 Both functions expect the tuple to be in "host" byte order
34 and a multiple of 4 bytes in length.
35 The ``rte_softrss()`` function expects the ``rss_key``
36 to be exactly the same as the one installed on the NIC.
37 The ``rte_softrss_be`` function is a faster implementation,
38 but it expects ``rss_key`` to be converted to the host byte order.
44 In some usecases it is useful to have a way to find partial collisions of the
45 Toeplitz hash function. In figure :numref:`figure_rss_queue_assign` only a few
46 of the least significant bits (LSB) of the hash value are used to indicate an
47 entry in the RSS Redirection Table (ReTa) and thus the index of the queue. So,
48 in this case it would be useful to find another tuple whose hash has the same
49 LSB's as the hash from the original tuple.
53 - In the case of SNAT (Source Network Address Translation) it is possible to
54 find a special source port number on translation so that the hash of
55 returning packets, of the given connection, will have desired LSB's.
56 - In the case of MPLS (Multiprotocol Label Switching), if the MPLS tag is used
57 in the hash calculation, the Label Switching router can allocate a special
58 MPLS tag to bind an LSP (Label Switching Path) to a given queue. This method
59 can be used with the allocation of IPSec SPI, VXLan VNI, etc., to bind the
60 tunnel to the desired queue.
61 - In the case of a TCP stack, a special source port could be chosen for
62 outgoing connections, such that the response packets will be assigned to the
65 This functionality is provided by the API shown below.
66 The API consists of 3 parts:
68 * Create the thash context.
70 * Create the thash helper, associated with a context.
72 * Use the helper run time to calculate the adjustable bits of the tuple to
79 The function ``rte_thash_init_ctx()`` initializes the context struct
80 associated with a particular NIC or a set of NICs. It expects:
82 * The log2 value of the size of the RSS redirection table for the
83 corresponding NIC. It reflects the number of least significant bits of the
84 hash value to produce a collision for.
86 * A predefined RSS hash key. This is optional, if ``NULL`` then a random key
89 * The length of the RSS hash key. This value is usually hardware/driver
90 specific and can be found in the NIC datasheet.
92 * Optional flags, as shown below.
96 * ``RTE_THASH_IGNORE_PERIOD_OVERFLOW`` - By default, and for security reasons,
97 the library prohibits generating a repeatable sequence in the hash key. This
98 flag disables such checking. The flag is mainly used for testing in the lab
99 to generate an RSS hash key with a uniform hash distribution, if the input
100 traffic also has a uniform distribution.
102 * ``RTE_THASH_MINIMAL_SEQ`` - By default, the library generates a special bit
103 sequence in the hash key for all the bits of the subtuple. However, the
104 collision generation task requires only the ``log2(RETA_SZ)`` bits in the
105 subtuple. This flag forces the minimum bit sequence in the hash key to be
106 generated for the required ``log2(RETA_SZ)`` least significant bits of the
107 subtuple. The flag can be used in the case of a relatively large number of
108 helpers that may overlap with their corresponding bit sequences of RSS hash
115 The function ``rte_thash_add_helper()`` initializes the helper struct
116 associated with a given context and a part of a target tuple of interest which
117 could be altered to produce a hash collision. On success it writes a specially
118 calculated bit sequence into the RSS hash key which is stored in the context
119 and calculates a table with values to be XORed with a subtuple.
123 * A pointer to the Thash context to be associated with.
125 * A length of the subtuple to be modified. The length is counted in bits.
127 * An offset of the subtuple to be modified from the beginning of the tuple. It
128 is also counted in bits.
132 Adding a helper changes the key stored in the corresponding context. So the
133 updated RSS hash key must be uploaded into the NIC after creating all the
137 Calculation of the complementary bits to adjust the subtuple
138 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140 The ``rte_thash_get_complement()`` function returns a special bit sequence
141 with length ``N = log2(rss_reta_sz)`` (for the ``rss_reta_sz`` provided at
142 context initialization) to be xored with N least significant bits of the
147 * A corresponding helper created for a given subtuple of the tuple.
149 * A hash value of the tuple we want to alter.
151 * The desired LSB's of the hash value the user expects to have.
153 After the returned bit sequence has been XORed with the subtuple, the resulted
154 LSB's of the new hash value, calculated from the altered tuple, will be the
155 same as in ``desired_hash``.
161 The ``rte_thash_get_complement()`` function is a user-friendly wrapper around
162 a number of other functions. It alters a passed tuple to meet the above
163 mentioned requirements around the desired hash LSB's.
167 * A Thash context and helper.
169 * A pointer to the tuple to be changed.
171 * The length of the tuple.
173 * A callback function and its userdata to check the tuple after it has been
176 * The number of attempts to change the tuple. Basically, it makes sense if
177 there is a callback and a limit on the number of attempts to change the
178 tuple, if the callback function returns an error.
184 There could be a number of different usecases, such as NAT, TCP stack, MPLS
185 tag allocation, etc. In the following we will consider a SNAT application.
187 Packets of a single bidirectional flow belonging to different directions can
188 end up being assigned to different queues and thus processed by different
189 lcores, as shown in :numref:`figure_predictable_snat_1`:
191 .. _figure_predictable_snat_1:
193 .. figure:: img/predictable_snat_1.*
195 Bidirectional flow packets distribution in general
197 That leads to a situation where the same packet flow can be shared between two
198 cores. Such a situation is not ideal from a performance perspective and
199 requires extra synchronization efforts that might lead to various performance
200 penalties, for example:
202 * The connections table is global so locking/RCU on the flow insertion/removal
205 * Connection metadata must be protected to avoid race conditions.
207 * More cache pressure if a single connection metadata is kept in different
208 L1/L2 caches of a different CPU core.
210 * Cache pressure/less cache locality on packet handover to the different cores.
212 We can avoid all these penalties if it can be guaranteed that packets
213 belonging to one bidirectional flow will be assigned to the same queue, as
214 shown in :numref:`figure_predictable_snat_2`:
216 .. _figure_predictable_snat_2:
218 .. figure:: img/predictable_snat_2.*
220 Bidirectional flow packets distribution with predictable RSS
223 To achieve this in a SNAT scenario it is possible to choose a source port not
224 randomly, but using the predictable RSS library to produce a partial hash
225 collision. This is shown in the code below.
229 int key_len = 40; /* The default Niantic RSS key length. */
231 /** The default Niantic RSS reta size = 2^7 entries, LSBs of hash value are
232 * used as an indexes in RSS ReTa. */
235 struct rte_thash_ctx *ctx;
237 uint8_t initial_key[key_len] = {0}; /* Default empty key. */
239 /* Create and initialize a new thash context. */
240 ctx = rte_thash_init_ctx("SNAT", key_len, reta_sz, initial_key, 0);
242 /** Add a helper and specify the variable tuple part and its length. In the
243 * SNAT case we want to choose a new source port on SNAT translation in a
244 * way that the reverse tuple will have the same LSBs as the original
245 * direction tuple so that the selected source port will be the
246 * destination port on reply.
248 ret = rte_thash_add_helper(ctx, "snat", sizeof(uint16_t) * 8,
249 offsetof(union rte_thash_tuple, v4.dport) * 8);
254 /* Get handler of the required helper. */
255 struct rte_thash_subtuple_helper *h = rte_thash_get_helper(ctx, "snat");
257 /** After calling rte_thash_add_helper() the initial_key passed on ctx
258 * creation has been changed so we get the new one.
260 uint8_t *new_key = rte_thash_get_key(ctx);
262 union rte_thash_tuple tuple, rev_tuple;
264 /* A complete tuple from the packet. */
265 complete_tuple(mbuf, &tuple);
267 /* Calculate the RSS hash or get it from mbuf->hash.rss. */
268 uint32_t orig_hash = rte_softrss((uint32_t *)&tuple, RTE_THASH_V4_L4_LEN, new_key);
270 /** Complete the reverse tuple by translating the SRC address and swapping
271 * src and dst addresses and ports.
273 get_rev_tuple(&rev_tuple, &tuple, new_ip);
275 /* Calculate the expected rss hash for the reverse tuple. */
276 uint32_t rev_hash = rte_softrss((uint32_t *)&rev_tuple, RTE_THASH_V4_L4_LEN, new_key);
278 /* Get the adjustment bits for the src port to get a new port. */
279 uint32_t adj = rte_thash_get_compliment(h, rev_hash, orig_hash);
281 /* Adjust the source port bits. */
282 uint16_t new_sport = tuple.v4.sport ^ adj;
284 /* Make an actual packet translation. */
285 do_snat(mbuf, new_ip, new_sport);