xref: /dpdk/lib/table/rte_swx_table.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2020 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_SWX_TABLE_H__
599a2dd95SBruce Richardson #define __INCLUDE_RTE_SWX_TABLE_H__
699a2dd95SBruce Richardson 
799a2dd95SBruce Richardson /**
899a2dd95SBruce Richardson  * @file
999a2dd95SBruce Richardson  * RTE SWX Table
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * Table interface.
1299a2dd95SBruce Richardson  */
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #include <stdint.h>
15f1f6ebc0SWilliam Tu 
16f1f6ebc0SWilliam Tu #include <rte_os.h>
1799a2dd95SBruce Richardson 
1882ff0701SCristian Dumitrescu #include "rte_swx_hash_func.h"
1982ff0701SCristian Dumitrescu 
2099a2dd95SBruce Richardson /** Match type. */
2199a2dd95SBruce Richardson enum rte_swx_table_match_type {
2299a2dd95SBruce Richardson 	/** Wildcard Match (WM). */
2399a2dd95SBruce Richardson 	RTE_SWX_TABLE_MATCH_WILDCARD,
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson 	/** Longest Prefix Match (LPM). */
2699a2dd95SBruce Richardson 	RTE_SWX_TABLE_MATCH_LPM,
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson 	/** Exact Match (EM). */
2999a2dd95SBruce Richardson 	RTE_SWX_TABLE_MATCH_EXACT,
3099a2dd95SBruce Richardson };
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson /** Table creation parameters. */
3399a2dd95SBruce Richardson struct rte_swx_table_params {
3499a2dd95SBruce Richardson 	/** Table match type. */
3599a2dd95SBruce Richardson 	enum rte_swx_table_match_type match_type;
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson 	/** Key size in bytes. */
3899a2dd95SBruce Richardson 	uint32_t key_size;
3999a2dd95SBruce Richardson 
4099a2dd95SBruce Richardson 	/** Offset of the first byte of the key within the key buffer. */
4199a2dd95SBruce Richardson 	uint32_t key_offset;
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson 	/** Mask of *key_size* bytes logically laid over the bytes at positions
4499a2dd95SBruce Richardson 	 * *key_offset* .. (*key_offset* + *key_size* - 1) of the key buffer in
4599a2dd95SBruce Richardson 	 * order to specify which bits from the key buffer are part of the key
4699a2dd95SBruce Richardson 	 * and which ones are not. A bit value of 1 in the *key_mask0* means the
4799a2dd95SBruce Richardson 	 * respective bit in the key buffer is part of the key, while a bit
4899a2dd95SBruce Richardson 	 * value of 0 means the opposite. A NULL value means that all the bits
4999a2dd95SBruce Richardson 	 * are part of the key, i.e. the *key_mask0* is an all-ones mask.
5099a2dd95SBruce Richardson 	 */
5199a2dd95SBruce Richardson 	uint8_t *key_mask0;
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson 	/** Maximum size (in bytes) of the action data. The data stored in the
5499a2dd95SBruce Richardson 	 * table for each entry is equal to *action_data_size* plus 8 bytes,
5599a2dd95SBruce Richardson 	 * which are used to store the action ID.
5699a2dd95SBruce Richardson 	 */
5799a2dd95SBruce Richardson 	uint32_t action_data_size;
5899a2dd95SBruce Richardson 
5982ff0701SCristian Dumitrescu 	/** Hash function. Ignored when not needed by the table implementation.
6082ff0701SCristian Dumitrescu 	 * When needed but set to NULL, the table implementation will select the
6182ff0701SCristian Dumitrescu 	 * hash function to use.
6282ff0701SCristian Dumitrescu 	 */
6382ff0701SCristian Dumitrescu 	rte_swx_hash_func_t hash_func;
6482ff0701SCristian Dumitrescu 
6599a2dd95SBruce Richardson 	/** Maximum number of keys to be stored in the table together with their
6699a2dd95SBruce Richardson 	 * associated data.
6799a2dd95SBruce Richardson 	 */
6899a2dd95SBruce Richardson 	uint32_t n_keys_max;
6999a2dd95SBruce Richardson };
7099a2dd95SBruce Richardson 
7199a2dd95SBruce Richardson /** Table entry. */
7299a2dd95SBruce Richardson struct rte_swx_table_entry {
7399a2dd95SBruce Richardson 	/** Used to facilitate the membership of this table entry to a
7499a2dd95SBruce Richardson 	 * linked list.
7599a2dd95SBruce Richardson 	 */
76f1f6ebc0SWilliam Tu 	RTE_TAILQ_ENTRY(rte_swx_table_entry) node;
7799a2dd95SBruce Richardson 
7899a2dd95SBruce Richardson 	/** Key value for the current entry. Array of *key_size* bytes or NULL
7999a2dd95SBruce Richardson 	 * if the *key_size* for the current table is 0.
8099a2dd95SBruce Richardson 	 */
8199a2dd95SBruce Richardson 	uint8_t *key;
8299a2dd95SBruce Richardson 
8399a2dd95SBruce Richardson 	/** Key mask for the current entry. Array of *key_size* bytes that is
8499a2dd95SBruce Richardson 	 * logically and'ed with *key_mask0* of the current table. A NULL value
8599a2dd95SBruce Richardson 	 * means that all the key bits already enabled by *key_mask0* are part
8699a2dd95SBruce Richardson 	 * of the key of the current entry.
8799a2dd95SBruce Richardson 	 */
8899a2dd95SBruce Richardson 	uint8_t *key_mask;
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson 	/** Placeholder for a possible compressed version of the *key* and
9199a2dd95SBruce Richardson 	 * *key_mask* of the current entry. Typically a hash signature, its main
9299a2dd95SBruce Richardson 	 * purpose is to the linked list search operation. Should be ignored by
9399a2dd95SBruce Richardson 	 * the API functions below.
9499a2dd95SBruce Richardson 	 */
9599a2dd95SBruce Richardson 	uint64_t key_signature;
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson 	/** Key priority for the current entry. Useful for wildcard match (as
9899a2dd95SBruce Richardson 	 * match rules are commonly overlapping with other rules), ignored for
9999a2dd95SBruce Richardson 	 * exact match (as match rules never overlap, hence all rules have the
10099a2dd95SBruce Richardson 	 * same match priority) and for LPM (match priority is driven by the
10199a2dd95SBruce Richardson 	 * prefix length, with non-overlapping prefixes essentially having the
10299a2dd95SBruce Richardson 	 * same match priority). Value 0 indicates the highest match priority.
10399a2dd95SBruce Richardson 	 */
10499a2dd95SBruce Richardson 	uint32_t key_priority;
10599a2dd95SBruce Richardson 
10699a2dd95SBruce Richardson 	/** Action ID for the current entry. */
10799a2dd95SBruce Richardson 	uint64_t action_id;
10899a2dd95SBruce Richardson 
10999a2dd95SBruce Richardson 	/** Action data for the current entry. Considering S as the action data
11099a2dd95SBruce Richardson 	 * size of the *action_id* action, which must be less than or equal to
11199a2dd95SBruce Richardson 	 * the table *action_data_size*, the *action_data* field must point to
11299a2dd95SBruce Richardson 	 * an array of S bytes when S is non-zero. The *action_data* field is
11399a2dd95SBruce Richardson 	 * ignored when S is zero.
11499a2dd95SBruce Richardson 	 */
11599a2dd95SBruce Richardson 	uint8_t *action_data;
11699a2dd95SBruce Richardson };
11799a2dd95SBruce Richardson 
11899a2dd95SBruce Richardson /** List of table entries. */
119f1f6ebc0SWilliam Tu RTE_TAILQ_HEAD(rte_swx_table_entry_list, rte_swx_table_entry);
12099a2dd95SBruce Richardson 
12199a2dd95SBruce Richardson /**
12299a2dd95SBruce Richardson  * Table memory footprint get
12399a2dd95SBruce Richardson  *
12499a2dd95SBruce Richardson  * @param[in] params
12599a2dd95SBruce Richardson  *   Table create parameters.
12699a2dd95SBruce Richardson  * @param[in] entries
12799a2dd95SBruce Richardson  *   Table entries.
12899a2dd95SBruce Richardson  * @param[in] args
12999a2dd95SBruce Richardson  *   Any additional table create arguments. It may be NULL.
13099a2dd95SBruce Richardson  * @return
13199a2dd95SBruce Richardson  *   Table memory footprint in bytes, if successful, or zero, on error.
13299a2dd95SBruce Richardson  */
13399a2dd95SBruce Richardson typedef uint64_t
13499a2dd95SBruce Richardson (*rte_swx_table_footprint_get_t)(struct rte_swx_table_params *params,
13599a2dd95SBruce Richardson 				 struct rte_swx_table_entry_list *entries,
13699a2dd95SBruce Richardson 				 const char *args);
13799a2dd95SBruce Richardson 
13899a2dd95SBruce Richardson /**
13999a2dd95SBruce Richardson  * Table mailbox size get
14099a2dd95SBruce Richardson  *
14199a2dd95SBruce Richardson  * The mailbox is used to store the context of a lookup operation that is in
14299a2dd95SBruce Richardson  * progress and it is passed as a parameter to the lookup operation. This allows
14399a2dd95SBruce Richardson  * for multiple concurrent lookup operations into the same table.
14499a2dd95SBruce Richardson  *
14599a2dd95SBruce Richardson  * @return
14699a2dd95SBruce Richardson  *   Table memory footprint in bytes, on success, or zero, on error.
14799a2dd95SBruce Richardson  */
14899a2dd95SBruce Richardson typedef uint64_t
14999a2dd95SBruce Richardson (*rte_swx_table_mailbox_size_get_t)(void);
15099a2dd95SBruce Richardson 
15199a2dd95SBruce Richardson /**
15299a2dd95SBruce Richardson  * Table create
15399a2dd95SBruce Richardson  *
15499a2dd95SBruce Richardson  * @param[in] params
15599a2dd95SBruce Richardson  *   Table creation parameters.
15699a2dd95SBruce Richardson  * @param[in] entries
15799a2dd95SBruce Richardson  *   Entries to be added to the table at creation time.
15899a2dd95SBruce Richardson  * @param[in] args
15999a2dd95SBruce Richardson  *   Any additional table create arguments. It may be NULL.
16099a2dd95SBruce Richardson  * @param[in] numa_node
16199a2dd95SBruce Richardson  *   Non-Uniform Memory Access (NUMA) node.
16299a2dd95SBruce Richardson  * @return
16399a2dd95SBruce Richardson  *   Table handle, on success, or NULL, on error.
16499a2dd95SBruce Richardson  */
16599a2dd95SBruce Richardson typedef void *
16699a2dd95SBruce Richardson (*rte_swx_table_create_t)(struct rte_swx_table_params *params,
16799a2dd95SBruce Richardson 			  struct rte_swx_table_entry_list *entries,
16899a2dd95SBruce Richardson 			  const char *args,
16999a2dd95SBruce Richardson 			  int numa_node);
17099a2dd95SBruce Richardson 
17199a2dd95SBruce Richardson /**
17299a2dd95SBruce Richardson  * Table entry add
17399a2dd95SBruce Richardson  *
17499a2dd95SBruce Richardson  * @param[in] table
17599a2dd95SBruce Richardson  *   Table handle.
17699a2dd95SBruce Richardson  * @param[in] entry
17799a2dd95SBruce Richardson  *   Entry to be added to the table.
17899a2dd95SBruce Richardson  * @return
17999a2dd95SBruce Richardson  *   0 on success or the following error codes otherwise:
18099a2dd95SBruce Richardson  *   -EINVAL: Invalid table handle, entry or entry field;
18199a2dd95SBruce Richardson  *   -ENOSPC: Table full.
18299a2dd95SBruce Richardson  */
18399a2dd95SBruce Richardson typedef int
18499a2dd95SBruce Richardson (*rte_swx_table_add_t)(void *table,
18599a2dd95SBruce Richardson 		       struct rte_swx_table_entry *entry);
18699a2dd95SBruce Richardson 
18799a2dd95SBruce Richardson /**
18899a2dd95SBruce Richardson  * Table entry delete
18999a2dd95SBruce Richardson  *
19099a2dd95SBruce Richardson  * @param[in] table
19199a2dd95SBruce Richardson  *   Table handle.
19299a2dd95SBruce Richardson  * @param[in] entry
19399a2dd95SBruce Richardson  *   Entry to be deleted from the table. The entry *action_id* and *action_data*
19499a2dd95SBruce Richardson  *   fields are ignored.
19599a2dd95SBruce Richardson  * @return
19699a2dd95SBruce Richardson  *   0 on success or the following error codes otherwise:
19799a2dd95SBruce Richardson  *   -EINVAL: Invalid table handle, entry or entry field;
19899a2dd95SBruce Richardson  *   -ENOSPC: Table full.
19999a2dd95SBruce Richardson  */
20099a2dd95SBruce Richardson typedef int
20199a2dd95SBruce Richardson (*rte_swx_table_delete_t)(void *table,
20299a2dd95SBruce Richardson 			  struct rte_swx_table_entry *entry);
20399a2dd95SBruce Richardson 
20499a2dd95SBruce Richardson /**
20599a2dd95SBruce Richardson  * Table lookup
20699a2dd95SBruce Richardson  *
20799a2dd95SBruce Richardson  * The table lookup operation searches a given key in the table and upon its
20899a2dd95SBruce Richardson  * completion it returns an indication of whether the key is found in the table
20999a2dd95SBruce Richardson  * (lookup hit) or not (lookup miss). In case of lookup hit, the action_id and
21099a2dd95SBruce Richardson  * the action_data associated with the key are also returned.
21199a2dd95SBruce Richardson  *
21299a2dd95SBruce Richardson  * Multiple invocations of this function may be required in order to complete a
21399a2dd95SBruce Richardson  * single table lookup operation for a given table and a given lookup key. The
21499a2dd95SBruce Richardson  * completion of the table lookup operation is flagged by a return value of 1;
21599a2dd95SBruce Richardson  * in case of a return value of 0, the function must be invoked again with
21699a2dd95SBruce Richardson  * exactly the same arguments.
21799a2dd95SBruce Richardson  *
21899a2dd95SBruce Richardson  * The mailbox argument is used to store the context of an on-going table lookup
21999a2dd95SBruce Richardson  * operation. The mailbox mechanism allows for multiple concurrent table lookup
22099a2dd95SBruce Richardson  * operations into the same table.
22199a2dd95SBruce Richardson  *
22299a2dd95SBruce Richardson  * The typical reason an implementation may choose to split the table lookup
2237be78d02SJosh Soref  * operation into multiple steps is to hide the latency of the inherent memory
22499a2dd95SBruce Richardson  * read operations: before a read operation with the source data likely not in
22599a2dd95SBruce Richardson  * the CPU cache, the source data prefetch is issued and the table lookup
22699a2dd95SBruce Richardson  * operation is postponed in favor of some other unrelated work, which the CPU
22799a2dd95SBruce Richardson  * executes in parallel with the source data being fetched into the CPU cache;
22899a2dd95SBruce Richardson  * later on, the table lookup operation is resumed, this time with the source
22999a2dd95SBruce Richardson  * data likely to be read from the CPU cache with no CPU pipeline stall, which
23099a2dd95SBruce Richardson  * significantly improves the table lookup performance.
23199a2dd95SBruce Richardson  *
232*42605e56SCristian Dumitrescu  * The table entry consists of the action ID and the action data. Each table
233*42605e56SCristian Dumitrescu  * entry is unique, although different table entries can have identical content,
234*42605e56SCristian Dumitrescu  * i.e. same values for the action ID and the action data. The table entry ID is
235*42605e56SCristian Dumitrescu  * also returned by the table lookup operation. It can be used to index into an
236*42605e56SCristian Dumitrescu  * external array of resources such as counters, registers or meters to identify
237*42605e56SCristian Dumitrescu  * the resource directly associated with the current table entry with no need to
238*42605e56SCristian Dumitrescu  * store the corresponding index into the table entry. The index of the external
239*42605e56SCristian Dumitrescu  * resource is thus auto-generated instead of being stored in the table entry.
240*42605e56SCristian Dumitrescu  *
24199a2dd95SBruce Richardson  * @param[in] table
24299a2dd95SBruce Richardson  *   Table handle.
24399a2dd95SBruce Richardson  * @param[in] mailbox
24499a2dd95SBruce Richardson  *   Mailbox for the current table lookup operation.
24599a2dd95SBruce Richardson  * @param[in] key
24699a2dd95SBruce Richardson  *   Lookup key. Its size mult be equal to the table *key_size*. If the latter
24799a2dd95SBruce Richardson  *   is zero, then the lookup key must be NULL.
24899a2dd95SBruce Richardson  * @param[out] action_id
24999a2dd95SBruce Richardson  *   ID of the action associated with the *key*. Must point to a valid 64-bit
25099a2dd95SBruce Richardson  *   variable. Only valid when the function returns 1 and *hit* is set to true.
25199a2dd95SBruce Richardson  * @param[out] action_data
25299a2dd95SBruce Richardson  *   Action data for the *action_id* action. Must point to a valid array of
25399a2dd95SBruce Richardson  *   table *action_data_size* bytes. Only valid when the function returns 1 and
25499a2dd95SBruce Richardson  *   *hit* is set to true.
255*42605e56SCristian Dumitrescu  * @param[out] entry_id
256*42605e56SCristian Dumitrescu  *   Table entry unique ID. Must point to a valid 32-bit variable. Only valid
257*42605e56SCristian Dumitrescu  *   when the function returns 1 and *hit* is set to true.
25899a2dd95SBruce Richardson  * @param[out] hit
25999a2dd95SBruce Richardson  *   Only valid when the function returns 1. Set to non-zero (true) on table
26099a2dd95SBruce Richardson  *   lookup hit and to zero (false) on table lookup miss.
26199a2dd95SBruce Richardson  * @return
26299a2dd95SBruce Richardson  *   0 when the table lookup operation is not yet completed, and 1 when the
26399a2dd95SBruce Richardson  *   table lookup operation is completed. No other return values are allowed.
26499a2dd95SBruce Richardson  */
26599a2dd95SBruce Richardson typedef int
26699a2dd95SBruce Richardson (*rte_swx_table_lookup_t)(void *table,
26799a2dd95SBruce Richardson 			  void *mailbox,
26899a2dd95SBruce Richardson 			  uint8_t **key,
26999a2dd95SBruce Richardson 			  uint64_t *action_id,
27099a2dd95SBruce Richardson 			  uint8_t **action_data,
271*42605e56SCristian Dumitrescu 			  size_t *entry_id,
27299a2dd95SBruce Richardson 			  int *hit);
27399a2dd95SBruce Richardson 
27499a2dd95SBruce Richardson /**
27599a2dd95SBruce Richardson  * Table free
27699a2dd95SBruce Richardson  *
27799a2dd95SBruce Richardson  * @param[in] table
27899a2dd95SBruce Richardson  *   Table handle.
27999a2dd95SBruce Richardson  */
28099a2dd95SBruce Richardson typedef void
28199a2dd95SBruce Richardson (*rte_swx_table_free_t)(void *table);
28299a2dd95SBruce Richardson 
28399a2dd95SBruce Richardson /** Table operations.  */
28499a2dd95SBruce Richardson struct rte_swx_table_ops {
28599a2dd95SBruce Richardson 	/** Table memory footprint get. Set to NULL when not supported. */
28699a2dd95SBruce Richardson 	rte_swx_table_footprint_get_t footprint_get;
28799a2dd95SBruce Richardson 
28899a2dd95SBruce Richardson 	/** Table mailbox size get. When NULL, the mailbox size is 0. */
28999a2dd95SBruce Richardson 	rte_swx_table_mailbox_size_get_t mailbox_size_get;
29099a2dd95SBruce Richardson 
29199a2dd95SBruce Richardson 	/** Table create. Must be non-NULL. */
29299a2dd95SBruce Richardson 	rte_swx_table_create_t create;
29399a2dd95SBruce Richardson 
29499a2dd95SBruce Richardson 	/** Incremental table entry add. Set to NULL when not supported, in
29599a2dd95SBruce Richardson 	 * which case the existing table has to be destroyed and a new table
29699a2dd95SBruce Richardson 	 * built from scratch with the new entry included.
29799a2dd95SBruce Richardson 	 */
29899a2dd95SBruce Richardson 	rte_swx_table_add_t add;
29999a2dd95SBruce Richardson 
30099a2dd95SBruce Richardson 	/** Incremental table entry delete. Set to NULL when not supported, in
30199a2dd95SBruce Richardson 	 * which case the existing table has to be destroyed and a new table
30299a2dd95SBruce Richardson 	 * built from scratch with the entry excluded.
30399a2dd95SBruce Richardson 	 */
30499a2dd95SBruce Richardson 	rte_swx_table_delete_t del;
30599a2dd95SBruce Richardson 
30699a2dd95SBruce Richardson 	/** Table lookup. Must be non-NULL. */
30799a2dd95SBruce Richardson 	rte_swx_table_lookup_t lkp;
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson 	/** Table free. Must be non-NULL. */
31099a2dd95SBruce Richardson 	rte_swx_table_free_t free;
31199a2dd95SBruce Richardson };
31299a2dd95SBruce Richardson 
31399a2dd95SBruce Richardson #endif
314