xref: /dpdk/doc/guides/prog_guide/packet_framework.rst (revision 41dd9a6bc2d9c6e20e139ad713cc9d172572dd43)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2010-2014 Intel Corporation.
3
4Packet Framework Library
5========================
6
7Design Objectives
8-----------------
9
10The main design objectives for the DPDK Packet Framework are:
11
12*   Provide standard methodology to build complex packet processing pipelines.
13    Provide reusable and extensible templates for the commonly used pipeline functional blocks;
14
15*   Provide capability to switch between pure software and hardware-accelerated implementations for the same pipeline functional block;
16
17*   Provide the best trade-off between flexibility and performance.
18    Hardcoded pipelines usually provide the best performance, but are not flexible,
19    while developing flexible frameworks is never a problem, but performance is usually low;
20
21*   Provide a framework that is logically similar to Open Flow.
22
23Overview
24--------
25
26Packet processing applications are frequently structured as pipelines of multiple stages,
27with the logic of each stage glued around a lookup table.
28For each incoming packet, the table defines the set of actions to be applied to the packet,
29as well as the next stage to send the packet to.
30
31The DPDK Packet Framework minimizes the development effort required to build packet processing pipelines
32by defining a standard methodology for pipeline development,
33as well as providing libraries of reusable templates for the commonly used pipeline blocks.
34
35The pipeline is constructed by connecting the set of input ports with the set of output ports
36through the set of tables in a tree-like topology.
37As result of lookup operation for the current packet in the current table,
38one of the table entries (on lookup hit) or the default table entry (on lookup miss)
39provides the set of actions to be applied on the current packet,
40as well as the next hop for the packet, which can be either another table, an output port or packet drop.
41
42An example of packet processing pipeline is presented in :numref:`figure_figure32`:
43
44.. _figure_figure32:
45
46.. figure:: img/figure32.*
47
48   Example of Packet Processing Pipeline where Input Ports 0 and 1
49   are Connected with Output Ports 0, 1 and 2 through Tables 0 and 1
50
51
52Port Library Design
53-------------------
54
55Port Types
56~~~~~~~~~~
57
58:numref:`table_qos_19` is a non-exhaustive list of ports that can be implemented with the Packet Framework.
59
60.. _table_qos_19:
61
62.. table:: Port Types
63
64   +---+------------------+---------------------------------------------------------------------------------------+
65   | # | Port type        | Description                                                                           |
66   |   |                  |                                                                                       |
67   +===+==================+=======================================================================================+
68   | 1 | SW ring          | SW circular buffer used for message passing between the application threads. Uses     |
69   |   |                  | the DPDK rte_ring primitive. Expected to be the most commonly used type of            |
70   |   |                  | port.                                                                                 |
71   |   |                  |                                                                                       |
72   +---+------------------+---------------------------------------------------------------------------------------+
73   | 2 | HW ring          | Queue of buffer descriptors used to interact with NIC, switch or accelerator ports.   |
74   |   |                  | For NIC ports, it uses the DPDK rte_eth_rx_queue or rte_eth_tx_queue                  |
75   |   |                  | primitives.                                                                           |
76   |   |                  |                                                                                       |
77   +---+------------------+---------------------------------------------------------------------------------------+
78   | 3 | IP reassembly    | Input packets are either IP fragments or complete IP datagrams. Output packets are    |
79   |   |                  | complete IP datagrams.                                                                |
80   |   |                  |                                                                                       |
81   +---+------------------+---------------------------------------------------------------------------------------+
82   | 4 | IP fragmentation | Input packets are jumbo (IP datagrams with length bigger than MTU) or non-jumbo       |
83   |   |                  | packets. Output packets are non-jumbo packets.                                        |
84   |   |                  |                                                                                       |
85   +---+------------------+---------------------------------------------------------------------------------------+
86   | 5 | Traffic manager  | Traffic manager attached to a specific NIC output port, performing congestion         |
87   |   |                  | management and hierarchical scheduling according to pre-defined SLAs.                 |
88   |   |                  |                                                                                       |
89   +---+------------------+---------------------------------------------------------------------------------------+
90   | 6 | Source           | Input port used as packet generator. Similar to Linux kernel /dev/zero character      |
91   |   |                  | device.                                                                               |
92   |   |                  |                                                                                       |
93   +---+------------------+---------------------------------------------------------------------------------------+
94   | 7 | Sink             | Output port used to drop all input packets. Similar to Linux kernel /dev/null         |
95   |   |                  | character device.                                                                     |
96   |   |                  |                                                                                       |
97   +---+------------------+---------------------------------------------------------------------------------------+
98   | 8 | Sym_crypto       | Output port used to extract DPDK Cryptodev operations from a fixed offset of the      |
99   |   |                  | packet and then enqueue to the Cryptodev PMD. Input port used to dequeue the          |
100   |   |                  | Cryptodev operations from the Cryptodev PMD and then retrieve the packets from them.  |
101   +---+------------------+---------------------------------------------------------------------------------------+
102
103Port Interface
104~~~~~~~~~~~~~~
105
106Each port is unidirectional, i.e. either input port or output port.
107Each input/output port is required to implement an abstract interface that
108defines the initialization and run-time operation of the port.
109The port abstract interface is described in.
110
111.. _table_qos_20:
112
113.. table:: 20 Port Abstract Interface
114
115   +---+----------------+-----------------------------------------------------------------------------------------+
116   | # | Port Operation | Description                                                                             |
117   |   |                |                                                                                         |
118   +===+================+=========================================================================================+
119   | 1 | Create         | Create the low-level port object (e.g. queue). Can internally allocate memory.          |
120   |   |                |                                                                                         |
121   +---+----------------+-----------------------------------------------------------------------------------------+
122   | 2 | Free           | Free the resources (e.g. memory) used by the low-level port object.                     |
123   |   |                |                                                                                         |
124   +---+----------------+-----------------------------------------------------------------------------------------+
125   | 3 | RX             | Read a burst of input packets. Non-blocking operation. Only defined for input ports.    |
126   |   |                |                                                                                         |
127   +---+----------------+-----------------------------------------------------------------------------------------+
128   | 4 | TX             | Write a burst of input packets. Non-blocking operation. Only defined for output ports.  |
129   |   |                |                                                                                         |
130   +---+----------------+-----------------------------------------------------------------------------------------+
131   | 5 | Flush          | Flush the output buffer. Only defined for output ports.                                 |
132   |   |                |                                                                                         |
133   +---+----------------+-----------------------------------------------------------------------------------------+
134
135Table Library Design
136--------------------
137
138Table Types
139~~~~~~~~~~~
140
141:numref:`table_qos_21` is a non-exhaustive list of types of tables that can be implemented with the Packet Framework.
142
143.. _table_qos_21:
144
145.. table:: Table Types
146
147   +---+----------------------------+-----------------------------------------------------------------------------+
148   | # | Table Type                 | Description                                                                 |
149   |   |                            |                                                                             |
150   +===+============================+=============================================================================+
151   | 1 | Hash table                 | Lookup key is n-tuple based.                                                |
152   |   |                            |                                                                             |
153   |   |                            | Typically, the lookup key is hashed to produce a signature that is used to  |
154   |   |                            | identify a bucket of entries where the lookup key is searched next.         |
155   |   |                            |                                                                             |
156   |   |                            | The signature associated with the lookup key of each input packet is either |
157   |   |                            | read from the packet descriptor (pre-computed signature) or computed at     |
158   |   |                            | table lookup time.                                                          |
159   |   |                            |                                                                             |
160   |   |                            | The table lookup, add entry and delete entry operations, as well as any     |
161   |   |                            | other pipeline block that pre-computes the signature all have to use the    |
162   |   |                            | same hashing algorithm to generate the signature.                           |
163   |   |                            |                                                                             |
164   |   |                            | Typically used to implement flow classification tables, ARP caches, routing |
165   |   |                            | table for tunnelling protocols, etc.                                        |
166   |   |                            |                                                                             |
167   +---+----------------------------+-----------------------------------------------------------------------------+
168   | 2 | Longest Prefix Match (LPM) | Lookup key is the IP address.                                               |
169   |   |                            |                                                                             |
170   |   |                            | Each table entries has an associated IP prefix (IP and depth).              |
171   |   |                            |                                                                             |
172   |   |                            | The table lookup operation selects the IP prefix that is matched by the     |
173   |   |                            | lookup key; in case of multiple matches, the entry with the longest prefix  |
174   |   |                            | depth wins.                                                                 |
175   |   |                            |                                                                             |
176   |   |                            | Typically used to implement IP routing tables.                              |
177   |   |                            |                                                                             |
178   +---+----------------------------+-----------------------------------------------------------------------------+
179   | 3 | Access Control List (ACLs) | Lookup key is 7-tuple of two VLAN/MPLS labels, IP destination address,      |
180   |   |                            | IP source addresses, L4 protocol, L4 destination port, L4 source port.      |
181   |   |                            |                                                                             |
182   |   |                            | Each table entry has an associated ACL and priority. The ACL contains bit   |
183   |   |                            | masks for the VLAN/MPLS labels, IP prefix for IP destination address, IP    |
184   |   |                            | prefix for IP source addresses, L4 protocol and bitmask, L4 destination     |
185   |   |                            | port and bit mask, L4 source port and bit mask.                             |
186   |   |                            |                                                                             |
187   |   |                            | The table lookup operation selects the ACL that is matched by the lookup    |
188   |   |                            | key; in case of multiple matches, the entry with the highest priority wins. |
189   |   |                            |                                                                             |
190   |   |                            | Typically used to implement rule databases for firewalls, etc.              |
191   |   |                            |                                                                             |
192   +---+----------------------------+-----------------------------------------------------------------------------+
193   | 4 | Pattern matching search    | Lookup key is the packet payload.                                           |
194   |   |                            |                                                                             |
195   |   |                            | Table is a database of patterns, with each pattern having a priority        |
196   |   |                            | assigned.                                                                   |
197   |   |                            |                                                                             |
198   |   |                            | The table lookup operation selects the patterns that is matched by the      |
199   |   |                            | input packet; in case of multiple matches, the matching pattern with the    |
200   |   |                            | highest priority wins.                                                      |
201   |   |                            |                                                                             |
202   +---+----------------------------+-----------------------------------------------------------------------------+
203   | 5 | Array                      | Lookup key is the table entry index itself.                                 |
204   |   |                            |                                                                             |
205   +---+----------------------------+-----------------------------------------------------------------------------+
206
207Table Interface
208~~~~~~~~~~~~~~~
209
210Each table is required to implement an abstract interface that defines the initialization
211and run-time operation of the table.
212The table abstract interface is described in :numref:`table_qos_29_1`.
213
214.. _table_qos_29_1:
215
216.. table:: Table Abstract Interface
217
218   +---+-----------------+----------------------------------------------------------------------------------------+
219   | # | Table operation | Description                                                                            |
220   |   |                 |                                                                                        |
221   +===+=================+========================================================================================+
222   | 1 | Create          | Create the low-level data structures of the lookup table. Can internally allocate      |
223   |   |                 | memory.                                                                                |
224   |   |                 |                                                                                        |
225   +---+-----------------+----------------------------------------------------------------------------------------+
226   | 2 | Free            | Free up all the resources used by the lookup table.                                    |
227   |   |                 |                                                                                        |
228   +---+-----------------+----------------------------------------------------------------------------------------+
229   | 3 | Add entry       | Add new entry to the lookup table.                                                     |
230   |   |                 |                                                                                        |
231   +---+-----------------+----------------------------------------------------------------------------------------+
232   | 4 | Delete entry    | Delete specific entry from the lookup table.                                           |
233   |   |                 |                                                                                        |
234   +---+-----------------+----------------------------------------------------------------------------------------+
235   | 5 | Lookup          | Look up a burst of input packets and return a bit mask specifying the result of the    |
236   |   |                 | lookup operation for each packet: a set bit signifies lookup hit for the corresponding |
237   |   |                 | packet, while a cleared bit a lookup miss.                                             |
238   |   |                 |                                                                                        |
239   |   |                 | For each lookup hit packet, the lookup operation also returns a pointer to the table   |
240   |   |                 | entry that was hit, which contains the actions to be applied on the packet and any     |
241   |   |                 | associated metadata.                                                                   |
242   |   |                 |                                                                                        |
243   |   |                 | For each lookup miss packet, the actions to be applied on the packet and any           |
244   |   |                 | associated metadata are specified by the default table entry preconfigured for lookup  |
245   |   |                 | miss.                                                                                  |
246   |   |                 |                                                                                        |
247   +---+-----------------+----------------------------------------------------------------------------------------+
248
249
250Hash Table Design
251~~~~~~~~~~~~~~~~~
252
253Hash Table Overview
254^^^^^^^^^^^^^^^^^^^
255
256Hash tables are important because the key lookup operation is optimized for speed:
257instead of having to linearly search the lookup key through all the keys in the table,
258the search is limited to only the keys stored in a single table bucket.
259
260**Associative Arrays**
261
262An associative array is a function that can be specified as a set of (key, value) pairs,
263with each key from the possible set of input keys present at most once.
264For a given associative array, the possible operations are:
265
266#.  *add (key, value)*: When no value is currently associated with *key*, then the (key, *value* ) association is created.
267    When *key* is already associated value *value0*, then the association (*key*, *value0*) is removed
268    and association *(key, value)* is created;
269
270#.  *delete key*: When no value is currently associated with *key*, this operation has no effect.
271    When *key* is already associated  *value*, then association  *(key, value)* is removed;
272
273#.  *lookup key*: When no value is currently associated with  *key*, then this operation returns void value (lookup miss).
274    When *key* is associated with *value*, then this operation returns *value*.
275    The *(key, value)* association is not changed.
276
277The matching criterion used to compare the input key against the keys in the associative array is *exact match*,
278as the key size (number of bytes) and the key value (array of bytes) have to match exactly for the two keys under comparison.
279
280**Hash Function**
281
282A hash function deterministically maps data of variable length (key) to data of fixed size (hash value or key signature).
283Typically, the size of the key is bigger than the size of the key signature.
284The hash function basically compresses a long key into a short signature.
285Several keys can share the same signature (collisions).
286
287High quality hash functions have uniform distribution.
288For large number of keys, when dividing the space of signature values into a fixed number of equal intervals (buckets),
289it is desirable to have the key signatures evenly distributed across these intervals (uniform distribution),
290as opposed to most of the signatures going into only a few of the intervals
291and the rest of the intervals being largely unused (non-uniform distribution).
292
293**Hash Table**
294
295A hash table is an associative array that uses a hash function for its operation.
296The reason for using a hash function is to optimize the performance of the lookup operation
297by minimizing the number of table keys that have to be compared against the input key.
298
299Instead of storing the (key, value) pairs in a single list, the hash table maintains multiple lists (buckets).
300For any given key, there is a single bucket where that key might exist, and this bucket is uniquely identified based on the key signature.
301Once the key signature is computed and the hash table bucket identified,
302the key is either located in this bucket or it is not present in the hash table at all,
303so the key search can be narrowed down from the full set of keys currently in the table
304to just the set of keys currently in the identified table bucket.
305
306The performance of the hash table lookup operation is greatly improved,
307provided that the table keys are evenly distributed among the hash table buckets,
308which can be achieved by using a hash function with uniform distribution.
309The rule to map a key to its bucket can simply be to use the key signature (modulo the number of table buckets) as the table bucket ID:
310
311    *bucket_id = f_hash(key) % n_buckets;*
312
313By selecting the number of buckets to be a power of two, the modulo operator can be replaced by a bitwise AND logical operation:
314
315    *bucket_id = f_hash(key) & (n_buckets - 1);*
316
317considering *n_bits* as the number of bits set in *bucket_mask = n_buckets - 1*,
318this means that all the keys that end up in the same hash table bucket have the lower *n_bits* of their signature identical.
319In order to reduce the number of keys in the same bucket (collisions), the number of hash table buckets needs to be increased.
320
321In packet processing context, the sequence of operations involved in hash table operations is described in :numref:`figure_figure33`:
322
323.. _figure_figure33:
324
325.. figure:: img/figure33.*
326
327   Sequence of Steps for Hash Table Operations in a Packet Processing Context
328
329
330
331Hash Table Use Cases
332^^^^^^^^^^^^^^^^^^^^
333
334**Flow Classification**
335
336*Description:* The flow classification is executed at least once for each input packet.
337This operation maps each incoming packet against one of the known traffic flows in the flow database that typically contains millions of flows.
338
339*Hash table name:* Flow classification table
340
341*Number of keys:* Millions
342
343*Key format:* n-tuple of packet fields that uniquely identify a traffic flow/connection.
344Example: DiffServ 5-tuple of (Source IP address, Destination IP address, L4 protocol, L4 protocol source port, L4 protocol destination port).
345For IPv4 protocol and L4 protocols like TCP, UDP or SCTP, the size of the DiffServ 5-tuple is 13 bytes, while for IPv6 it is 37 bytes.
346
347*Key value (key data):* actions and action meta-data describing what processing to be applied for the packets of the current flow.
348The size of the data associated with each traffic flow can vary from 8 bytes to kilobytes.
349
350**Address Resolution Protocol (ARP)**
351
352*Description:* Once a route has been identified for an IP packet (so the output interface and the IP address of the next hop station are known),
353the MAC address of the next hop station is needed in order to send this packet onto the next leg of the journey
354towards its destination (as identified by its destination IP address).
355The MAC address of the next hop station becomes the destination MAC address of the outgoing Ethernet frame.
356
357*Hash table name:* ARP table
358
359*Number of keys:* Thousands
360
361*Key format:* The pair of (Output interface, Next Hop IP address), which is typically 5 bytes for IPv4 and 17 bytes for IPv6.
362
363*Key value (key data):* MAC address of the next hop station (6 bytes).
364
365Hash Table Types
366^^^^^^^^^^^^^^^^
367
368:numref:`table_qos_22` lists the hash table configuration parameters shared by all different hash table types.
369
370.. _table_qos_22:
371
372.. table:: Configuration Parameters Common for All Hash Table Types
373
374   +---+---------------------------+------------------------------------------------------------------------------+
375   | # | Parameter                 | Details                                                                      |
376   |   |                           |                                                                              |
377   +===+===========================+==============================================================================+
378   | 1 | Key size                  | Measured as number of bytes. All keys have the same size.                    |
379   |   |                           |                                                                              |
380   +---+---------------------------+------------------------------------------------------------------------------+
381   | 2 | Key value (key data) size | Measured as number of bytes.                                                 |
382   |   |                           |                                                                              |
383   +---+---------------------------+------------------------------------------------------------------------------+
384   | 3 | Number of buckets         | Needs to be a power of two.                                                  |
385   |   |                           |                                                                              |
386   +---+---------------------------+------------------------------------------------------------------------------+
387   | 4 | Maximum number of keys    | Needs to be a power of two.                                                  |
388   |   |                           |                                                                              |
389   +---+---------------------------+------------------------------------------------------------------------------+
390   | 5 | Hash function             | Examples: jhash, CRC hash, etc.                                              |
391   |   |                           |                                                                              |
392   +---+---------------------------+------------------------------------------------------------------------------+
393   | 6 | Hash function seed        | Parameter to be passed to the hash function.                                 |
394   |   |                           |                                                                              |
395   +---+---------------------------+------------------------------------------------------------------------------+
396   | 7 | Key offset                | Offset of the lookup key byte array within the packet meta-data stored in    |
397   |   |                           | the packet buffer.                                                           |
398   |   |                           |                                                                              |
399   +---+---------------------------+------------------------------------------------------------------------------+
400
401Bucket Full Problem
402"""""""""""""""""""
403
404On initialization, each hash table bucket is allocated space for exactly 4 keys.
405As keys are added to the table, it can happen that a given bucket already has 4 keys when a new key has to be added to this bucket.
406The possible options are:
407
408#.  **Least Recently Used (LRU) Hash Table.**
409    One of the existing keys in the bucket is deleted and the new key is added in its place.
410    The number of keys in each bucket never grows bigger than 4. The logic to pick the key to be dropped from the bucket is LRU.
411    The hash table lookup operation maintains the order in which the keys in the same bucket are hit, so every time a key is hit,
412    it becomes the new Most Recently Used (MRU) key, i.e. the last candidate for drop.
413    When a key is added to the bucket, it also becomes the new MRU key.
414    When a key needs to be picked and dropped, the first candidate for drop, i.e. the current LRU key, is always picked.
415    The LRU logic requires maintaining specific data structures per each bucket.
416
417#.  **Extendable Bucket Hash Table.**
418    The bucket is extended with space for 4 more keys.
419    This is done by allocating additional memory at table initialization time,
420    which is used to create a pool of free keys (the size of this pool is configurable and always a multiple of 4).
421    On key add operation, the allocation of a group of 4 keys only happens successfully within the limit of free keys,
422    otherwise the key add operation fails.
423    On key delete operation, a group of 4 keys is freed back to the pool of free keys
424    when the key to be deleted is the only key that was used within its group of 4 keys at that time.
425    On key lookup operation, if the current bucket is in extended state and a match is not found in the first group of 4 keys,
426    the search continues beyond the first group of 4 keys, potentially until all keys in this bucket are examined.
427    The extendable bucket logic requires maintaining specific data structures per table and per each bucket.
428
429.. _table_qos_23:
430
431.. table:: Configuration Parameters Specific to Extendable Bucket Hash Table
432
433   +---+---------------------------+--------------------------------------------------+
434   | # | Parameter                 | Details                                          |
435   |   |                           |                                                  |
436   +===+===========================+==================================================+
437   | 1 | Number of additional keys | Needs to be a power of two, at least equal to 4. |
438   |   |                           |                                                  |
439   +---+---------------------------+--------------------------------------------------+
440
441
442Signature Computation
443"""""""""""""""""""""
444
445The possible options for key signature computation are:
446
447#.  **Pre-computed key signature.**
448    The key lookup operation is split between two CPU cores.
449    The first CPU core (typically the CPU core that performs packet RX) extracts the key from the input packet,
450    computes the key signature and saves both the key and the key signature in the packet buffer as packet meta-data.
451    The second CPU core reads both the key and the key signature from the packet meta-data
452    and performs the bucket search step of the key lookup operation.
453
454#.  **Key signature computed on lookup ("do-sig" version).**
455    The same CPU core reads the key from the packet meta-data, uses it to compute the key signature
456    and also performs the bucket search step of the key lookup operation.
457
458.. _table_qos_24:
459
460.. table:: Configuration Parameters Specific to Pre-computed Key Signature Hash Table
461
462   +---+------------------+-----------------------------------------------------------------------+
463   | # | Parameter        | Details                                                               |
464   |   |                  |                                                                       |
465   +===+==================+=======================================================================+
466   | 1 | Signature offset | Offset of the pre-computed key signature within the packet meta-data. |
467   |   |                  |                                                                       |
468   +---+------------------+-----------------------------------------------------------------------+
469
470Key Size Optimized Hash Tables
471""""""""""""""""""""""""""""""
472
473For specific key sizes, the data structures and algorithm of key lookup operation can be specially handcrafted for further performance improvements,
474so following options are possible:
475
476#.  **Implementation supporting configurable key size.**
477
478#.  **Implementation supporting a single key size.**
479    Typical key sizes are 8 bytes and 16 bytes.
480
481Bucket Search Logic for Configurable Key Size Hash Tables
482^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
483
484The performance of the bucket search logic is one of the main factors influencing the performance of the key lookup operation.
485The data structures and algorithm are designed to make the best use of Intel CPU architecture resources like:
486cache memory space, cache memory bandwidth, external memory bandwidth, multiple execution units working in parallel,
487out of order instruction execution, special CPU instructions, etc.
488
489The bucket search logic handles multiple input packets in parallel.
490It is built as a pipeline of several stages (3 or 4), with each pipeline stage handling two different packets from the burst of input packets.
491On each pipeline iteration, the packets are pushed to the next pipeline stage: for the 4-stage pipeline,
492two packets (that just completed stage 3) exit the pipeline,
493two packets (that just completed stage 2) are now executing stage 3, two packets (that just completed stage 1) are now executing stage 2,
494two packets (that just completed stage 0) are now executing stage 1 and two packets (next two packets to read from the burst of input packets)
495are entering the pipeline to execute stage 0.
496The pipeline iterations continue until all packets from the burst of input packets execute the last stage of the pipeline.
497
498The bucket search logic is broken into pipeline stages at the boundary of the next memory access.
499Each pipeline stage uses data structures that are stored (with high probability) into the L1 or L2 cache memory of the current CPU core and
500breaks just before the next memory access required by the algorithm.
501The current pipeline stage finalizes by prefetching the data structures required by the next pipeline stage,
502so given enough time for the prefetch to complete,
503when the next pipeline stage eventually gets executed for the same packets,
504it will read the data structures it needs from L1 or L2 cache memory and thus avoid the significant penalty incurred by L2 or L3 cache memory miss.
505
506By prefetching the data structures required by the next pipeline stage in advance (before they are used)
507and switching to executing another pipeline stage for different packets,
508the number of L2 or L3 cache memory misses is greatly reduced, hence one of the main reasons for improved performance.
509This is because the cost of L2/L3 cache memory miss on memory read accesses is high, as usually due to data dependency between instructions,
510the CPU execution units have to stall until the read operation is completed from L3 cache memory or external DRAM memory.
511By using prefetch instructions, the latency of memory read accesses is hidden,
512provided that it is performed early enough before the respective data structure is actually used.
513
514By splitting the processing into several stages that are executed on different packets (the packets from the input burst are interlaced),
515enough work is created to allow the prefetch instructions to complete successfully (before the prefetched data structures are actually accessed) and
516also the data dependency between instructions is loosened.
517For example, for the 4-stage pipeline, stage 0 is executed on packets 0 and 1 and then,
518before same packets 0 and 1 are used (i.e. before stage 1 is executed on packets 0 and 1),
519different packets are used: packets 2 and 3 (executing stage 1), packets 4 and 5 (executing stage 2) and packets 6 and 7 (executing stage 3).
520By executing useful work while the data structures are brought into the L1 or L2 cache memory, the latency of the read memory accesses is hidden.
521By increasing the gap between two consecutive accesses to the same data structure, the data dependency between instructions is loosened;
522this allows making the best use of the super-scalar and out-of-order execution CPU architecture,
523as the number of CPU core execution units that are active (rather than idle or stalled due to data dependency constraints between instructions) is maximized.
524
525The bucket search logic is also implemented without using any branch instructions.
526This avoids the important cost associated with flushing the CPU core execution pipeline on every instance of branch misprediction.
527
528Configurable Key Size Hash Table
529""""""""""""""""""""""""""""""""
530
531:numref:`figure_figure34`, :numref:`table_qos_25` and :numref:`table_qos_26` detail the main data structures used to implement configurable key size hash tables (either LRU or extendable bucket,
532either with pre-computed signature or "do-sig").
533
534.. _figure_figure34:
535
536.. figure:: img/figure34.*
537
538   Data Structures for Configurable Key Size Hash Tables
539
540
541.. _table_qos_25:
542
543.. table:: Main Large Data Structures (Arrays) used for Configurable Key Size Hash Tables
544
545   +---+-------------------------+------------------------------+---------------------------+-------------------------------+
546   | # | Array name              | Number of entries            | Entry size (bytes)        | Description                   |
547   |   |                         |                              |                           |                               |
548   +===+=========================+==============================+===========================+===============================+
549   | 1 | Bucket array            | n_buckets (configurable)     | 32                        | Buckets of the hash table.    |
550   |   |                         |                              |                           |                               |
551   +---+-------------------------+------------------------------+---------------------------+-------------------------------+
552   | 2 | Bucket extensions array | n_buckets_ext (configurable) | 32                        | This array is only created    |
553   |   |                         |                              |                           | for extendable bucket tables. |
554   |   |                         |                              |                           |                               |
555   +---+-------------------------+------------------------------+---------------------------+-------------------------------+
556   | 3 | Key array               | n_keys                       | key_size (configurable)   | Keys added to the hash table. |
557   |   |                         |                              |                           |                               |
558   +---+-------------------------+------------------------------+---------------------------+-------------------------------+
559   | 4 | Data array              | n_keys                       | entry_size (configurable) | Key values (key data)         |
560   |   |                         |                              |                           | associated with the hash      |
561   |   |                         |                              |                           | table keys.                   |
562   |   |                         |                              |                           |                               |
563   +---+-------------------------+------------------------------+---------------------------+-------------------------------+
564
565.. _table_qos_26:
566
567.. table:: Field Description for Bucket Array Entry (Configurable Key Size Hash Tables)
568
569   +---+------------------+--------------------+------------------------------------------------------------------+
570   | # | Field name       | Field size (bytes) | Description                                                      |
571   |   |                  |                    |                                                                  |
572   +===+==================+====================+==================================================================+
573   | 1 | Next Ptr/LRU     | 8                  | For LRU tables, this fields represents the LRU list for the      |
574   |   |                  |                    | current bucket stored as array of 4 entries of 2 bytes each.     |
575   |   |                  |                    | Entry 0 stores the index (0 .. 3) of the MRU key, while entry 3  |
576   |   |                  |                    | stores the index of the LRU key.                                 |
577   |   |                  |                    |                                                                  |
578   |   |                  |                    | For extendable bucket tables, this field represents the next     |
579   |   |                  |                    | pointer (i.e. the pointer to the next group of 4 keys linked to  |
580   |   |                  |                    | the current bucket). The next pointer is not NULL if the bucket  |
581   |   |                  |                    | is currently extended or NULL otherwise.                         |
582   |   |                  |                    | To help the branchless implementation, bit 0 (least significant  |
583   |   |                  |                    | bit) of this field is set to 1 if the next pointer is not NULL   |
584   |   |                  |                    | and to 0 otherwise.                                              |
585   |   |                  |                    |                                                                  |
586   +---+------------------+--------------------+------------------------------------------------------------------+
587   | 2 | Sig[0 .. 3]      | 4 x 2              | If key X (X = 0 .. 3) is valid, then sig X bits 15 .. 1 store    |
588   |   |                  |                    | the most significant 15 bits of key X signature and sig X bit 0  |
589   |   |                  |                    | is set to 1.                                                     |
590   |   |                  |                    |                                                                  |
591   |   |                  |                    | If key X is not valid, then sig X is set to zero.                |
592   |   |                  |                    |                                                                  |
593   +---+------------------+--------------------+------------------------------------------------------------------+
594   | 3 | Key Pos [0 .. 3] | 4 x 4              | If key X is valid (X = 0 .. 3), then Key Pos X represents the    |
595   |   |                  |                    | index into the key array where key X is stored, as well as the   |
596   |   |                  |                    | index into the data array where the value associated with key X  |
597   |   |                  |                    | is stored.                                                       |
598   |   |                  |                    |                                                                  |
599   |   |                  |                    | If key X is not valid, then the value of Key Pos X is undefined. |
600   |   |                  |                    |                                                                  |
601   +---+------------------+--------------------+------------------------------------------------------------------+
602
603
604:numref:`figure_figure35` and :numref:`table_qos_27` detail the bucket search pipeline stages (either LRU or extendable bucket,
605either with pre-computed signature or "do-sig").
606For each pipeline stage, the described operations are applied to each of the two packets handled by that stage.
607
608.. _figure_figure35:
609
610.. figure:: img/figure35.*
611
612   Bucket Search Pipeline for Key Lookup Operation (Configurable Key Size Hash
613   Tables)
614
615
616.. _table_qos_27:
617
618.. table:: Description of the Bucket Search Pipeline Stages (Configurable Key Size Hash Tables)
619
620   +---+---------------------------+------------------------------------------------------------------------------+
621   | # | Stage name                | Description                                                                  |
622   |   |                           |                                                                              |
623   +===+===========================+==============================================================================+
624   | 0 | Prefetch packet meta-data | Select next two packets from the burst of input packets.                     |
625   |   |                           |                                                                              |
626   |   |                           | Prefetch packet meta-data containing the key and key signature.              |
627   |   |                           |                                                                              |
628   +---+---------------------------+------------------------------------------------------------------------------+
629   | 1 | Prefetch table bucket     | Read the key signature from the packet meta-data (for extendable bucket hash |
630   |   |                           | tables) or read the key from the packet meta-data and compute key signature  |
631   |   |                           | (for LRU tables).                                                            |
632   |   |                           |                                                                              |
633   |   |                           | Identify the bucket ID using the key signature.                              |
634   |   |                           |                                                                              |
635   |   |                           | Set bit 0 of the signature to 1 (to match only signatures of valid keys from |
636   |   |                           | the table).                                                                  |
637   |   |                           |                                                                              |
638   |   |                           | Prefetch the bucket.                                                         |
639   |   |                           |                                                                              |
640   +---+---------------------------+------------------------------------------------------------------------------+
641   | 2 | Prefetch table key        | Read the key signatures from the bucket.                                     |
642   |   |                           |                                                                              |
643   |   |                           | Compare the signature of the input key against the 4 key signatures from the |
644   |   |                           | packet. As result, the following is obtained:                                |
645   |   |                           |                                                                              |
646   |   |                           | *match*                                                                      |
647   |   |                           | = equal to TRUE if there was at least one signature match and to FALSE in    |
648   |   |                           | the case of no signature match;                                              |
649   |   |                           |                                                                              |
650   |   |                           | *match_many*                                                                 |
651   |   |                           | = equal to TRUE is there were more than one signature matches (can be up to  |
652   |   |                           | 4 signature matches in the worst case scenario) and to FALSE otherwise;      |
653   |   |                           |                                                                              |
654   |   |                           | *match_pos*                                                                  |
655   |   |                           | = the index of the first key that produced signature match (only valid if    |
656   |   |                           | match is true).                                                              |
657   |   |                           |                                                                              |
658   |   |                           | For extendable bucket hash tables only, set                                  |
659   |   |                           | *match_many*                                                                 |
660   |   |                           | to TRUE if next pointer is valid.                                            |
661   |   |                           |                                                                              |
662   |   |                           | Prefetch the bucket key indicated by                                         |
663   |   |                           | *match_pos*                                                                  |
664   |   |                           | (even if                                                                     |
665   |   |                           | *match_pos*                                                                  |
666   |   |                           | does not point to valid key valid).                                          |
667   |   |                           |                                                                              |
668   +---+---------------------------+------------------------------------------------------------------------------+
669   | 3 | Prefetch table data       | Read the bucket key indicated by                                             |
670   |   |                           | *match_pos*.                                                                 |
671   |   |                           |                                                                              |
672   |   |                           | Compare the bucket key against the input key. As result, the following is    |
673   |   |                           | obtained:                                                                    |
674   |   |                           | *match_key*                                                                  |
675   |   |                           | = equal to TRUE if the two keys match and to FALSE otherwise.                |
676   |   |                           |                                                                              |
677   |   |                           | Report input key as lookup hit only when both                                |
678   |   |                           | *match*                                                                      |
679   |   |                           | and                                                                          |
680   |   |                           | *match_key*                                                                  |
681   |   |                           | are equal to TRUE and as lookup miss otherwise.                              |
682   |   |                           |                                                                              |
683   |   |                           | For LRU tables only, use branchless logic to update the bucket LRU list      |
684   |   |                           | (the current key becomes the new MRU) only on lookup hit.                    |
685   |   |                           |                                                                              |
686   |   |                           | Prefetch the key value (key data) associated with the current key (to avoid  |
687   |   |                           | branches, this is done on both lookup hit and miss).                         |
688   |   |                           |                                                                              |
689   +---+---------------------------+------------------------------------------------------------------------------+
690
691
692Additional notes:
693
694#.  The pipelined version of the bucket search algorithm is executed only if there are at least 7 packets in the burst of input packets.
695    If there are less than 7 packets in the burst of input packets,
696    a non-optimized implementation of the bucket search algorithm is executed.
697
698#.  Once the pipelined version of the bucket search algorithm has been executed for all the packets in the burst of input packets,
699    the non-optimized implementation of the bucket search algorithm is also executed for any packets that did not produce a lookup hit,
700    but have the *match_many* flag set.
701    As result of executing the non-optimized version, some of these packets may produce a lookup hit or lookup miss.
702    This does not impact the performance of the key lookup operation,
703    as the probability of matching more than one signature in the same group of 4 keys or of having the bucket in extended state
704    (for extendable bucket hash tables only) is relatively small.
705
706**Key Signature Comparison Logic**
707
708The key signature comparison logic is described in :numref:`table_qos_28`.
709
710.. _table_qos_28:
711
712.. table:: Lookup Tables for Match, Match_Many and Match_Pos
713
714   +----+------+---------------+--------------------+--------------------+
715   | #  | mask | match (1 bit) | match_many (1 bit) | match_pos (2 bits) |
716   |    |      |               |                    |                    |
717   +----+------+---------------+--------------------+--------------------+
718   | 0  | 0000 | 0             | 0                  | 00                 |
719   |    |      |               |                    |                    |
720   +----+------+---------------+--------------------+--------------------+
721   | 1  | 0001 | 1             | 0                  | 00                 |
722   |    |      |               |                    |                    |
723   +----+------+---------------+--------------------+--------------------+
724   | 2  | 0010 | 1             | 0                  | 01                 |
725   |    |      |               |                    |                    |
726   +----+------+---------------+--------------------+--------------------+
727   | 3  | 0011 | 1             | 1                  | 00                 |
728   |    |      |               |                    |                    |
729   +----+------+---------------+--------------------+--------------------+
730   | 4  | 0100 | 1             | 0                  | 10                 |
731   |    |      |               |                    |                    |
732   +----+------+---------------+--------------------+--------------------+
733   | 5  | 0101 | 1             | 1                  | 00                 |
734   |    |      |               |                    |                    |
735   +----+------+---------------+--------------------+--------------------+
736   | 6  | 0110 | 1             | 1                  | 01                 |
737   |    |      |               |                    |                    |
738   +----+------+---------------+--------------------+--------------------+
739   | 7  | 0111 | 1             | 1                  | 00                 |
740   |    |      |               |                    |                    |
741   +----+------+---------------+--------------------+--------------------+
742   | 8  | 1000 | 1             | 0                  | 11                 |
743   |    |      |               |                    |                    |
744   +----+------+---------------+--------------------+--------------------+
745   | 9  | 1001 | 1             | 1                  | 00                 |
746   |    |      |               |                    |                    |
747   +----+------+---------------+--------------------+--------------------+
748   | 10 | 1010 | 1             | 1                  | 01                 |
749   |    |      |               |                    |                    |
750   +----+------+---------------+--------------------+--------------------+
751   | 11 | 1011 | 1             | 1                  | 00                 |
752   |    |      |               |                    |                    |
753   +----+------+---------------+--------------------+--------------------+
754   | 12 | 1100 | 1             | 1                  | 10                 |
755   |    |      |               |                    |                    |
756   +----+------+---------------+--------------------+--------------------+
757   | 13 | 1101 | 1             | 1                  | 00                 |
758   |    |      |               |                    |                    |
759   +----+------+---------------+--------------------+--------------------+
760   | 14 | 1110 | 1             | 1                  | 01                 |
761   |    |      |               |                    |                    |
762   +----+------+---------------+--------------------+--------------------+
763   | 15 | 1111 | 1             | 1                  | 00                 |
764   |    |      |               |                    |                    |
765   +----+------+---------------+--------------------+--------------------+
766
767The input *mask* hash bit X (X = 0 .. 3) set to 1 if input signature is equal to bucket signature X and set to 0 otherwise.
768The outputs *match*, *match_many* and *match_pos* are 1 bit, 1 bit and 2 bits in size respectively and their meaning has been explained above.
769
770As displayed in :numref:`table_qos_29`, the lookup tables for *match* and *match_many* can be collapsed into a single 32-bit value and the lookup table for
771*match_pos* can be collapsed into a 64-bit value.
772Given the input *mask*, the values for *match*, *match_many* and *match_pos* can be obtained by indexing their respective bit array to extract 1 bit,
7731 bit and 2 bits respectively with branchless logic.
774
775.. _table_qos_29:
776
777.. table:: Collapsed Lookup Tables for Match, Match_Many and Match_Pos
778
779   +------------+------------------------------------------+-------------------+
780   |            | Bit array                                | Hexadecimal value |
781   |            |                                          |                   |
782   +------------+------------------------------------------+-------------------+
783   | match      | 1111_1111_1111_1110                      | 0xFFFELLU         |
784   |            |                                          |                   |
785   +------------+------------------------------------------+-------------------+
786   | match_many | 1111_1110_1110_1000                      | 0xFEE8LLU         |
787   |            |                                          |                   |
788   +------------+------------------------------------------+-------------------+
789   | match_pos  | 0001_0010_0001_0011__0001_0010_0001_0000 | 0x12131210LLU     |
790   |            |                                          |                   |
791   +------------+------------------------------------------+-------------------+
792
793
794The pseudo-code for match, match_many and match_pos is::
795
796    match = (0xFFFELLU >> mask) & 1;
797
798    match_many = (0xFEE8LLU >> mask) & 1;
799
800    match_pos = (0x12131210LLU >> (mask << 1)) & 3;
801
802Single Key Size Hash Tables
803"""""""""""""""""""""""""""
804
805:numref:`figure_figure37`, :numref:`figure_figure38`, :numref:`table_qos_30` and :numref:`table_qos_31` detail the main data structures used to implement 8-byte and 16-byte key hash tables
806(either LRU or extendable bucket, either with pre-computed signature or "do-sig").
807
808.. _figure_figure37:
809
810.. figure:: img/figure37.*
811
812   Data Structures for 8-byte Key Hash Tables
813
814
815.. _figure_figure38:
816
817.. figure:: img/figure38.*
818
819   Data Structures for 16-byte Key Hash Tables
820
821
822.. _table_qos_30:
823
824.. table:: Main Large Data Structures (Arrays) used for 8-byte and 16-byte Key Size Hash Tables
825
826   +---+-------------------------+------------------------------+----------------------+------------------------------------+
827   | # | Array name              | Number of entries            | Entry size (bytes)   | Description                        |
828   |   |                         |                              |                      |                                    |
829   +===+=========================+==============================+======================+====================================+
830   | 1 | Bucket array            | n_buckets (configurable)     | *8-byte key size:*   | Buckets of the hash table.         |
831   |   |                         |                              |                      |                                    |
832   |   |                         |                              | 64 + 4 x entry_size  |                                    |
833   |   |                         |                              |                      |                                    |
834   |   |                         |                              |                      |                                    |
835   |   |                         |                              | *16-byte key size:*  |                                    |
836   |   |                         |                              |                      |                                    |
837   |   |                         |                              | 128 + 4 x entry_size |                                    |
838   |   |                         |                              |                      |                                    |
839   +---+-------------------------+------------------------------+----------------------+------------------------------------+
840   | 2 | Bucket extensions array | n_buckets_ext (configurable) | *8-byte key size:*   | This array is only created for     |
841   |   |                         |                              |                      | extendable bucket tables.          |
842   |   |                         |                              |                      |                                    |
843   |   |                         |                              | 64 + 4 x entry_size  |                                    |
844   |   |                         |                              |                      |                                    |
845   |   |                         |                              |                      |                                    |
846   |   |                         |                              | *16-byte key size:*  |                                    |
847   |   |                         |                              |                      |                                    |
848   |   |                         |                              | 128 + 4 x entry_size |                                    |
849   |   |                         |                              |                      |                                    |
850   +---+-------------------------+------------------------------+----------------------+------------------------------------+
851
852.. _table_qos_31:
853
854.. table:: Field Description for Bucket Array Entry (8-byte and 16-byte Key Hash Tables)
855
856   +---+---------------+--------------------+-------------------------------------------------------------------------------+
857   | # | Field name    | Field size (bytes) | Description                                                                   |
858   |   |               |                    |                                                                               |
859   +===+===============+====================+===============================================================================+
860   | 1 | Valid         | 8                  | Bit X (X = 0 .. 3) is set to 1 if key X is valid or to 0 otherwise.           |
861   |   |               |                    |                                                                               |
862   |   |               |                    | Bit 4 is only used for extendable bucket tables to help with the              |
863   |   |               |                    | implementation of the branchless logic. In this case, bit 4 is set to 1 if    |
864   |   |               |                    | next pointer is valid (not NULL) or to 0 otherwise.                           |
865   |   |               |                    |                                                                               |
866   +---+---------------+--------------------+-------------------------------------------------------------------------------+
867   | 2 | Next Ptr/LRU  | 8                  | For LRU tables, this fields represents the LRU list for the current bucket    |
868   |   |               |                    | stored as array of 4 entries of 2 bytes each. Entry 0 stores the index        |
869   |   |               |                    | (0 .. 3) of the MRU key, while entry 3 stores the index of the LRU key.       |
870   |   |               |                    |                                                                               |
871   |   |               |                    | For extendable bucket tables, this field represents the next pointer (i.e.    |
872   |   |               |                    | the pointer to the next group of 4 keys linked to the current bucket). The    |
873   |   |               |                    | next pointer is not NULL if the bucket is currently extended or NULL          |
874   |   |               |                    | otherwise.                                                                    |
875   |   |               |                    |                                                                               |
876   +---+---------------+--------------------+-------------------------------------------------------------------------------+
877   | 3 | Key [0 .. 3]  | 4 x key_size       | Full keys.                                                                    |
878   |   |               |                    |                                                                               |
879   +---+---------------+--------------------+-------------------------------------------------------------------------------+
880   | 4 | Data [0 .. 3] | 4 x entry_size     | Full key values (key data) associated with keys 0 .. 3.                       |
881   |   |               |                    |                                                                               |
882   +---+---------------+--------------------+-------------------------------------------------------------------------------+
883
884and detail the bucket search pipeline used to implement 8-byte and 16-byte key hash tables (either LRU or extendable bucket,
885either with pre-computed signature or "do-sig").
886For each pipeline stage, the described operations are applied to each of the two packets handled by that stage.
887
888.. _figure_figure39:
889
890.. figure:: img/figure39.*
891
892   Bucket Search Pipeline for Key Lookup Operation (Single Key Size Hash
893   Tables)
894
895
896.. _table_qos_32:
897
898.. table:: Description of the Bucket Search Pipeline Stages (8-byte and 16-byte Key Hash Tables)
899
900   +---+---------------------------+-----------------------------------------------------------------------------+
901   | # | Stage name                | Description                                                                 |
902   |   |                           |                                                                             |
903   +===+===========================+=============================================================================+
904   | 0 | Prefetch packet meta-data | #.  Select next two packets from the burst of input packets.                |
905   |   |                           |                                                                             |
906   |   |                           | #.  Prefetch packet meta-data containing the key and key signature.         |
907   |   |                           |                                                                             |
908   +---+---------------------------+-----------------------------------------------------------------------------+
909   | 1 | Prefetch table bucket     | #.  Read the key signature from the packet meta-data (for extendable bucket |
910   |   |                           |     hash tables) or read the key from the packet meta-data and compute key  |
911   |   |                           |     signature (for LRU tables).                                             |
912   |   |                           |                                                                             |
913   |   |                           | #.  Identify the bucket ID using the key signature.                         |
914   |   |                           |                                                                             |
915   |   |                           | #.  Prefetch the bucket.                                                    |
916   |   |                           |                                                                             |
917   +---+---------------------------+-----------------------------------------------------------------------------+
918   | 2 | Prefetch table data       | #.  Read the bucket.                                                        |
919   |   |                           |                                                                             |
920   |   |                           | #.  Compare all 4 bucket keys against the input key.                        |
921   |   |                           |                                                                             |
922   |   |                           | #.  Report input key as lookup hit only when a match is identified (more    |
923   |   |                           |     than one key match is not possible)                                     |
924   |   |                           |                                                                             |
925   |   |                           | #.  For LRU tables only, use branchless logic to update the bucket LRU list |
926   |   |                           |     (the current key becomes the new MRU) only on lookup hit.               |
927   |   |                           |                                                                             |
928   |   |                           | #.  Prefetch the key value (key data) associated with the matched key (to   |
929   |   |                           |     avoid branches, this is done on both lookup hit and miss).              |
930   |   |                           |                                                                             |
931   +---+---------------------------+-----------------------------------------------------------------------------+
932
933Additional notes:
934
935#.  The pipelined version of the bucket search algorithm is executed only if there are at least 5 packets in the burst of input packets.
936    If there are less than 5 packets in the burst of input packets, a non-optimized implementation of the bucket search algorithm is executed.
937
938#.  For extendable bucket hash tables only,
939    once the pipelined version of the bucket search algorithm has been executed for all the packets in the burst of input packets,
940    the non-optimized implementation of the bucket search algorithm is also executed for any packets that did not produce a lookup hit,
941    but have the bucket in extended state.
942    As result of executing the non-optimized version, some of these packets may produce a lookup hit or lookup miss.
943    This does not impact the performance of the key lookup operation,
944    as the probability of having the bucket in extended state is relatively small.
945
946Pipeline Library Design
947-----------------------
948
949A pipeline is defined by:
950
951#.  The set of input ports;
952
953#.  The set of output ports;
954
955#.  The set of tables;
956
957#.  The set of actions.
958
959The input ports are connected with the output ports through tree-like topologies of interconnected tables.
960The table entries contain the actions defining the operations to be executed on the input packets and the packet flow within the pipeline.
961
962Connectivity of Ports and Tables
963~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
964
965To avoid any dependencies on the order in which pipeline elements are created,
966the connectivity of pipeline elements is defined after all the pipeline input ports,
967output ports and tables have been created.
968
969General connectivity rules:
970
971#.  Each input port is connected to a single table. No input port should be left unconnected;
972
973#.  The table connectivity to other tables or to output ports is regulated by the next hop actions of each table entry and the default table entry.
974    The table connectivity is fluid, as the table entries and the default table entry can be updated during run-time.
975
976    *   A table can have multiple entries (including the default entry) connected to the same output port.
977        A table can have different entries connected to different output ports.
978        Different tables can have entries (including default table entry) connected to the same output port.
979
980    *   A table can have multiple entries (including the default entry) connected to another table,
981        in which case all these entries have to point to the same table.
982        This constraint is enforced by the API and prevents tree-like topologies from being created (allowing table chaining only),
983        with the purpose of simplifying the implementation of the pipeline run-time execution engine.
984
985Port Actions
986~~~~~~~~~~~~
987
988Port Action Handler
989^^^^^^^^^^^^^^^^^^^
990
991An action handler can be assigned to each input/output port to define actions to be executed on each input packet that is received by the port.
992Defining the action handler for a specific input/output port is optional (i.e. the action handler can be disabled).
993
994For input ports, the action handler is executed after RX function. For output ports, the action handler is executed before the TX function.
995
996The action handler can decide to drop packets.
997
998Table Actions
999~~~~~~~~~~~~~
1000
1001Table Action Handler
1002^^^^^^^^^^^^^^^^^^^^
1003
1004An action handler to be executed on each input packet can be assigned to each table.
1005Defining the action handler for a specific table is optional (i.e. the action handler can be disabled).
1006
1007The action handler is executed after the table lookup operation is performed and the table entry associated with each input packet is identified.
1008The action handler can only handle the user-defined actions, while the reserved actions (e.g. the next hop actions) are handled by the Packet Framework.
1009The action handler can decide to drop the input packet.
1010
1011Reserved Actions
1012^^^^^^^^^^^^^^^^
1013
1014The reserved actions are handled directly by the Packet Framework without the user being able to change their meaning
1015through the table action handler configuration.
1016A special category of the reserved actions is represented by the next hop actions, which regulate the packet flow between input ports,
1017tables and output ports through the pipeline.
1018:numref:`table_qos_33` lists the next hop actions.
1019
1020.. _table_qos_33:
1021
1022.. table:: Next Hop Actions (Reserved)
1023
1024   +---+---------------------+-----------------------------------------------------------------------------------+
1025   | # | Next hop action     | Description                                                                       |
1026   |   |                     |                                                                                   |
1027   +===+=====================+===================================================================================+
1028   | 1 | Drop                | Drop the current packet.                                                          |
1029   |   |                     |                                                                                   |
1030   +---+---------------------+-----------------------------------------------------------------------------------+
1031   | 2 | Send to output port | Send the current packet to specified output port. The output port ID is metadata  |
1032   |   |                     | stored in the same table entry.                                                   |
1033   |   |                     |                                                                                   |
1034   +---+---------------------+-----------------------------------------------------------------------------------+
1035   | 3 | Send to table       | Send the current packet to specified table. The table ID is metadata stored in    |
1036   |   |                     | the same table entry.                                                             |
1037   |   |                     |                                                                                   |
1038   +---+---------------------+-----------------------------------------------------------------------------------+
1039
1040User Actions
1041^^^^^^^^^^^^
1042
1043For each table, the meaning of user actions is defined through the configuration of the table action handler.
1044Different tables can be configured with different action handlers, therefore the meaning of the user actions
1045and their associated meta-data is private to each table.
1046Within the same table, all the table entries (including the table default entry) share the same definition
1047for the user actions and their associated meta-data,
1048with each table entry having its own set of enabled user actions and its own copy of the action meta-data.
1049:numref:`table_qos_34` contains a non-exhaustive list of user action examples.
1050
1051.. _table_qos_34:
1052
1053.. table:: User Action Examples
1054
1055   +---+-----------------------------------+---------------------------------------------------------------------+
1056   | # | User action                       | Description                                                         |
1057   |   |                                   |                                                                     |
1058   +===+===================================+=====================================================================+
1059   | 1 | Metering                          | Per flow traffic metering using the srTCM and trTCM algorithms.     |
1060   |   |                                   |                                                                     |
1061   +---+-----------------------------------+---------------------------------------------------------------------+
1062   | 2 | Statistics                        | Update the statistics counters maintained per flow.                 |
1063   |   |                                   |                                                                     |
1064   +---+-----------------------------------+---------------------------------------------------------------------+
1065   | 3 | App ID                            | Per flow state machine fed by variable length sequence of packets   |
1066   |   |                                   | at the flow initialization with the purpose of identifying the      |
1067   |   |                                   | traffic type and application.                                       |
1068   |   |                                   |                                                                     |
1069   +---+-----------------------------------+---------------------------------------------------------------------+
1070   | 4 | Push/pop labels                   | Push/pop VLAN/MPLS labels to/from the current packet.               |
1071   |   |                                   |                                                                     |
1072   +---+-----------------------------------+---------------------------------------------------------------------+
1073   | 5 | Network Address Translation (NAT) | Translate between the internal (LAN) and external (WAN) IP          |
1074   |   |                                   | destination/source address and/or L4 protocol destination/source    |
1075   |   |                                   | port.                                                               |
1076   |   |                                   |                                                                     |
1077   +---+-----------------------------------+---------------------------------------------------------------------+
1078   | 6 | TTL update                        | Decrement IP TTL and, in case of IPv4 packets, update the IP        |
1079   |   |                                   | checksum.                                                           |
1080   |   |                                   |                                                                     |
1081   +---+-----------------------------------+---------------------------------------------------------------------+
1082   | 7 | Sym Crypto                        | Generate Cryptodev session based on the user-specified algorithm    |
1083   |   |                                   | and key(s), and assemble the cryptodev operation based on the       |
1084   |   |                                   | predefined offsets.                                                 |
1085   |   |                                   |                                                                     |
1086   +---+-----------------------------------+---------------------------------------------------------------------+
1087
1088Multicore Scaling
1089-----------------
1090
1091A complex application is typically split across multiple cores, with cores communicating through SW queues.
1092There is usually a performance limit on the number of table lookups
1093and actions that can be fitted on the same CPU core due to HW constraints like:
1094available CPU cycles, cache memory size, cache transfer BW, memory transfer BW, etc.
1095
1096As the application is split across multiple CPU cores, the Packet Framework facilitates the creation of several pipelines,
1097the assignment of each such pipeline to a different CPU core
1098and the interconnection of all CPU core-level pipelines into a single application-level complex pipeline.
1099For example, if CPU core A is assigned to run pipeline P1 and CPU core B pipeline P2,
1100then the interconnection of P1 with P2 could be achieved by having the same set of SW queues act like output ports
1101for P1 and input ports for P2.
1102
1103This approach enables the application development using the pipeline, run-to-completion (clustered) or hybrid (mixed) models.
1104
1105It is allowed for the same core to run several pipelines, but it is not allowed for several cores to run the same pipeline.
1106
1107Shared Data Structures
1108~~~~~~~~~~~~~~~~~~~~~~
1109
1110The threads performing table lookup are actually table writers rather than just readers.
1111Even if the specific table lookup algorithm is thread-safe for multiple readers
1112(e. g. read-only access of the search algorithm data structures is enough to conduct the lookup operation),
1113once the table entry for the current packet is identified, the thread is typically expected to update the action meta-data stored in the table entry
1114(e.g. increment the counter tracking the number of packets that hit this table entry), and thus modify the table entry.
1115During the time this thread is accessing this table entry (either writing or reading; duration is application specific),
1116for data consistency reasons, no other threads (threads performing table lookup or entry add/delete operations) are allowed to modify this table entry.
1117
1118Mechanisms to share the same table between multiple threads:
1119
1120#.  **Multiple writer threads.**
1121    Threads need to use synchronization primitives like semaphores (distinct semaphore per table entry) or atomic instructions.
1122    The cost of semaphores is usually high, even when the semaphore is free.
1123    The cost of atomic instructions is normally higher than the cost of regular instructions.
1124
1125#.  **Multiple writer threads, with single thread performing table lookup operations and multiple threads performing table entry add/delete operations.**
1126    The threads performing table entry add/delete operations send table update requests to the reader (typically through message passing queues),
1127    which does the actual table updates and then sends the response back to the request initiator.
1128
1129#.  **Single writer thread performing table entry add/delete operations and multiple reader threads that perform table lookup operations with read-only access to the table entries.**
1130    The reader threads use the main table copy while the writer is updating the mirror copy.
1131    Once the writer update is done, the writer can signal to the readers and busy wait until all readers swaps between the mirror copy (which now becomes the main copy) and
1132    the mirror copy (which now becomes the main copy).
1133
1134Interfacing with Accelerators
1135-----------------------------
1136
1137The presence of accelerators is usually detected during the initialization phase by inspecting the HW devices that are part of the system (e.g. by PCI bus enumeration).
1138Typical devices with acceleration capabilities are:
1139
1140*   Inline accelerators: NICs, switches, FPGAs, etc;
1141
1142*   Look-aside accelerators: chipsets, FPGAs, Intel QuickAssist, etc.
1143
1144Usually, to support a specific functional block, specific implementation of Packet Framework tables and/or ports and/or actions has to be provided for each accelerator,
1145with all the implementations sharing the same API: pure SW implementation (no acceleration), implementation using accelerator A, implementation using accelerator B, etc.
1146The selection between these implementations could be done at build time or at run-time (recommended), based on which accelerators are present in the system,
1147with no application changes required.
1148
1149The Software Switch (SWX) Pipeline
1150----------------------------------
1151
1152The Software Switch (SWX) pipeline is designed to combine the DPDK performance with the flexibility of the P4-16 language [1]. It can be used either by itself
1153to code a complete software switch or data plane application, or in combination with the open-source P4 compiler P4C [2], acting as a P4C back-end that allows
1154the P4 programs to be translated to the DPDK SWX API and run on multi-core CPUs.
1155
1156The main features of the SWX pipeline are:
1157
1158*   Nothing is hard-wired, everything is dynamically defined: The packet headers (i.e. the network protocols), the packet meta-data, the actions, the tables
1159    and the pipeline itself are dynamically defined instead of selected from a predefined set.
1160
1161*   Instructions: The actions and the life of the packet through the pipeline are defined with instructions that manipulate the pipeline objects mentioned
1162    above. The pipeline is the main function of the packet program, with actions as subroutines triggered by the tables.
1163
1164*   Call external plugins: Extern objects and functions can be defined to call functionality that cannot be efficiently implemented with the existing
1165    pipeline-oriented instruction set, such as: error detecting/correcting codes, cryptographic operations, meters, statistics counter arrays, heuristics, etc.
1166
1167*   Better control plane interaction: Transaction-oriented table update mechanism that supports multi-table atomic updates. Multiple tables can be updated in a
1168    single step with only the before-update and the after-update table entries visible to the packets. Alignment with the P4Runtime [3] protocol.
1169
1170*   Performance: Multiple packets are in-flight within the pipeline at any moment. Each packet is owned by a different time-sharing thread in
1171    run-to-completion, with the thread pausing before memory access operations such as packet I/O and table lookup to allow the memory prefetch to complete.
1172    The instructions are verified and translated at initialization time with no run-time impact. The instructions are also optimized to detect and "fuse"
1173    frequently used patterns into vector-like instructions transparently to the user.
1174
1175The main SWX pipeline components are:
1176
1177*   Input and output ports: Each port instantiates a port type that defines the port operations, e.g. Ethernet device port, PCAP port, etc. The RX interface
1178    of the input ports and the TX interface of the output ports are single packet based, with packet batching typically implemented internally by each port for
1179    performance reasons.
1180
1181*   Structure types: Each structure type is used to define the logical layout of a memory block, such as: packet headers, packet meta-data, action data stored
1182    in a table entry, mailboxes of extern objects and functions. Similar to C language structs, each structure type is a well defined sequence of fields, with
1183    each field having a unique name and a constant size.
1184
1185*   Packet headers: Each packet typically has one or multiple headers. The headers are extracted from the input packet as part of the packet parsing operation,
1186    which is likely executed immediately after the packet reception. As result of the extract operation, each header is logically removed from the packet, so
1187    once the packet parsing operation is completed, the input packet is reduced to opaque payload. Just before transmission, one or several headers are pushed
1188    in front of each output packet through the emit operation; these headers can be part of the set of headers that were previously extracted from the input
1189    packet (and potentially modified afterwards) or some new headers whose contents is generated by the pipeline (e.g. by reading them from tables). The format
1190    of each packet header is defined by instantiating a structure type.
1191
1192*   Packet meta-data: The packet meta-data is filled in by the pipeline (e.g. by reading it from tables) or computed by the pipeline. It is not sent out unless
1193    some of the meta-data fields are explicitly written into the headers emitted into the output packet. The format of the packet meta-data is defined by
1194    instantiating a structure type.
1195
1196*   Extern objects and functions: Used to plug into the pipeline any functionality that cannot be efficiently implemented with the existing pipeline instruction
1197    set. Each extern object and extern function has its own mailbox, which is used to pass the input arguments to and retrieve the output arguments from the
1198    extern object member functions or the extern function.  The mailbox format is defined by instantiating a structure type.
1199
1200*   Instructions: The pipeline and its actions are defined with instructions from a predefined instruction set. The instructions are used to receive and
1201    transmit the current packet, extract and emit headers from/into the packet, read/write the packet headers, packet meta-data and mailboxes, start table
1202    lookup operations, read the action arguments from the table entry, call extern object member functions or extern functions. See the rte_swx_pipeline.h file
1203    for the complete list of instructions.
1204
1205*   Actions: The pipeline actions are dynamically defined through instructions as opposed to predefined. Essentially, the actions are subroutines of the
1206    pipeline program and their execution is triggered by the table lookup. The input arguments of each action are read from the table entry (in case of table
1207    lookup hit) or the default table action (in case of table lookup miss) and are read-only; their format is defined by instantiating a structure type. The
1208    actions have read-write access to the packet headers and meta-data.
1209
1210*   Table: Each pipeline typically has one or more lookup tables. The match fields of each table are flexibly selected from the packet headers and meta-data
1211    defined for the current pipeline. The set of table actions is flexibly selected for each table from the set of actions defined for the current pipeline. The
1212    tables can be looked at as special pipeline operators that result in one of the table actions being called, depending on the result of the table lookup
1213    operation.
1214
1215*   Pipeline: The pipeline represents the main program that defines the life of the packet, with subroutines (actions) executed on table lookup. As packets
1216    go through the pipeline, the packet headers and meta-data are transformed along the way.
1217
1218References:
1219
1220[1] P4-16 specification: https://p4.org/specs/
1221
1222[2] P4-16 compiler: https://github.com/p4lang/p4c
1223
1224[3] P4Runtime specification: https://p4.org/specs/
1225