xref: /dpdk/lib/table/rte_table.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_TABLE_H__
699a2dd95SBruce Richardson #define __INCLUDE_RTE_TABLE_H__
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  * RTE Table
1199a2dd95SBruce Richardson  *
1299a2dd95SBruce Richardson  * This tool is part of the DPDK Packet Framework tool suite and provides
1399a2dd95SBruce Richardson  * a standard interface to implement different types of lookup tables for data
1499a2dd95SBruce Richardson  * plane processing.
1599a2dd95SBruce Richardson  *
1699a2dd95SBruce Richardson  * Virtually any search algorithm that can uniquely associate data to a lookup
1799a2dd95SBruce Richardson  * key can be fitted under this lookup table abstraction. For the flow table
1899a2dd95SBruce Richardson  * use-case, the lookup key is an n-tuple of packet fields that uniquely
1999a2dd95SBruce Richardson  * identifies a traffic flow, while data represents actions and action
2099a2dd95SBruce Richardson  * meta-data associated with the same traffic flow.
21*3e4c5be9SThomas Monjalon  */
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson #include <stdint.h>
2499a2dd95SBruce Richardson #include <rte_port.h>
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson struct rte_mbuf;
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /** Lookup table statistics */
2999a2dd95SBruce Richardson struct rte_table_stats {
3099a2dd95SBruce Richardson 	uint64_t n_pkts_in;
3199a2dd95SBruce Richardson 	uint64_t n_pkts_lookup_miss;
3299a2dd95SBruce Richardson };
3399a2dd95SBruce Richardson 
3499a2dd95SBruce Richardson /**
3599a2dd95SBruce Richardson  * Lookup table create
3699a2dd95SBruce Richardson  *
3799a2dd95SBruce Richardson  * @param params
3899a2dd95SBruce Richardson  *   Parameters for lookup table creation. The underlying data structure is
3999a2dd95SBruce Richardson  *   different for each lookup table type.
4099a2dd95SBruce Richardson  * @param socket_id
4199a2dd95SBruce Richardson  *   CPU socket ID (e.g. for memory allocation purpose)
4299a2dd95SBruce Richardson  * @param entry_size
4399a2dd95SBruce Richardson  *   Data size of each lookup table entry (measured in bytes)
4499a2dd95SBruce Richardson  * @return
4599a2dd95SBruce Richardson  *   Handle to lookup table instance
4699a2dd95SBruce Richardson  */
4799a2dd95SBruce Richardson typedef void* (*rte_table_op_create)(void *params, int socket_id,
4899a2dd95SBruce Richardson 	uint32_t entry_size);
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson /**
5199a2dd95SBruce Richardson  * Lookup table free
5299a2dd95SBruce Richardson  *
5399a2dd95SBruce Richardson  * @param table
5499a2dd95SBruce Richardson  *   Handle to lookup table instance
5599a2dd95SBruce Richardson  * @return
5699a2dd95SBruce Richardson  *   0 on success, error code otherwise
5799a2dd95SBruce Richardson  */
5899a2dd95SBruce Richardson typedef int (*rte_table_op_free)(void *table);
5999a2dd95SBruce Richardson 
6099a2dd95SBruce Richardson /**
6199a2dd95SBruce Richardson  * Lookup table entry add
6299a2dd95SBruce Richardson  *
6399a2dd95SBruce Richardson  * @param table
6499a2dd95SBruce Richardson  *   Handle to lookup table instance
6599a2dd95SBruce Richardson  * @param key
6699a2dd95SBruce Richardson  *   Lookup key
6799a2dd95SBruce Richardson  * @param entry
6899a2dd95SBruce Richardson  *   Data to be associated with the current key. This parameter has to point to
6999a2dd95SBruce Richardson  *   a valid memory buffer where the first entry_size bytes (table create
7099a2dd95SBruce Richardson  *   parameter) are populated with the data.
7199a2dd95SBruce Richardson  * @param key_found
7299a2dd95SBruce Richardson  *   After successful invocation, *key_found is set to a value different than 0
7399a2dd95SBruce Richardson  *   if the current key is already present in the table and to 0 if not. This
7499a2dd95SBruce Richardson  *   pointer has to be set to a valid memory location before the table entry add
7599a2dd95SBruce Richardson  *   function is called.
7699a2dd95SBruce Richardson  * @param entry_ptr
7799a2dd95SBruce Richardson  *   After successful invocation, *entry_ptr stores the handle to the table
7899a2dd95SBruce Richardson  *   entry containing the data associated with the current key. This handle can
7999a2dd95SBruce Richardson  *   be used to perform further read-write accesses to this entry. This handle
8099a2dd95SBruce Richardson  *   is valid until the key is deleted from the table or the same key is
8199a2dd95SBruce Richardson  *   re-added to the table, typically to associate it with different data. This
8299a2dd95SBruce Richardson  *   pointer has to be set to a valid memory location before the function is
8399a2dd95SBruce Richardson  *   called.
8499a2dd95SBruce Richardson  * @return
8599a2dd95SBruce Richardson  *   0 on success, error code otherwise
8699a2dd95SBruce Richardson  */
8799a2dd95SBruce Richardson typedef int (*rte_table_op_entry_add)(
8899a2dd95SBruce Richardson 	void *table,
8999a2dd95SBruce Richardson 	void *key,
9099a2dd95SBruce Richardson 	void *entry,
9199a2dd95SBruce Richardson 	int *key_found,
9299a2dd95SBruce Richardson 	void **entry_ptr);
9399a2dd95SBruce Richardson 
9499a2dd95SBruce Richardson /**
9599a2dd95SBruce Richardson  * Lookup table entry delete
9699a2dd95SBruce Richardson  *
9799a2dd95SBruce Richardson  * @param table
9899a2dd95SBruce Richardson  *   Handle to lookup table instance
9999a2dd95SBruce Richardson  * @param key
10099a2dd95SBruce Richardson  *   Lookup key
10199a2dd95SBruce Richardson  * @param key_found
10299a2dd95SBruce Richardson  *   After successful invocation, *key_found is set to a value different than 0
10399a2dd95SBruce Richardson  *   if the current key was present in the table before the delete operation
10499a2dd95SBruce Richardson  *   was performed and to 0 if not. This pointer has to be set to a valid
10599a2dd95SBruce Richardson  *   memory location before the table entry delete function is called.
10699a2dd95SBruce Richardson  * @param entry
10799a2dd95SBruce Richardson  *   After successful invocation, if the key is found in the table (*key found
10899a2dd95SBruce Richardson  *   is different than 0 after function call is completed) and entry points to
10999a2dd95SBruce Richardson  *   a valid buffer (entry is set to a value different than NULL before the
11099a2dd95SBruce Richardson  *   function is called), then the first entry_size bytes (table create
11199a2dd95SBruce Richardson  *   parameter) in *entry store a copy of table entry that contained the data
11299a2dd95SBruce Richardson  *   associated with the current key before the key was deleted.
11399a2dd95SBruce Richardson  * @return
11499a2dd95SBruce Richardson  *   0 on success, error code otherwise
11599a2dd95SBruce Richardson  */
11699a2dd95SBruce Richardson typedef int (*rte_table_op_entry_delete)(
11799a2dd95SBruce Richardson 	void *table,
11899a2dd95SBruce Richardson 	void *key,
11999a2dd95SBruce Richardson 	int *key_found,
12099a2dd95SBruce Richardson 	void *entry);
12199a2dd95SBruce Richardson 
12299a2dd95SBruce Richardson /**
12399a2dd95SBruce Richardson  * Lookup table entry add bulk
12499a2dd95SBruce Richardson  *
12599a2dd95SBruce Richardson  * @param table
12699a2dd95SBruce Richardson  *   Handle to lookup table instance
12799a2dd95SBruce Richardson  * @param keys
12899a2dd95SBruce Richardson  *   Array containing lookup keys
12999a2dd95SBruce Richardson  * @param entries
13099a2dd95SBruce Richardson  *   Array containing data to be associated with each key. Every item in the
13199a2dd95SBruce Richardson  *   array has to point to a valid memory buffer where the first entry_size
13299a2dd95SBruce Richardson  *   bytes (table create parameter) are populated with the data.
13399a2dd95SBruce Richardson  * @param n_keys
13499a2dd95SBruce Richardson  *   Number of keys to add
13599a2dd95SBruce Richardson  * @param key_found
13699a2dd95SBruce Richardson  *   After successful invocation, key_found for every item in the array is set
13799a2dd95SBruce Richardson  *   to a value different than 0 if the current key is already present in the
13899a2dd95SBruce Richardson  *   table and to 0 if not. This pointer has to be set to a valid memory
13999a2dd95SBruce Richardson  *   location before the table entry add function is called.
14099a2dd95SBruce Richardson  * @param entries_ptr
14199a2dd95SBruce Richardson  *   After successful invocation, array *entries_ptr stores the handle to the
14299a2dd95SBruce Richardson  *   table entry containing the data associated with every key. This handle can
14399a2dd95SBruce Richardson  *   be used to perform further read-write accesses to this entry. This handle
14499a2dd95SBruce Richardson  *   is valid until the key is deleted from the table or the same key is
14599a2dd95SBruce Richardson  *   re-added to the table, typically to associate it with different data. This
14699a2dd95SBruce Richardson  *   pointer has to be set to a valid memory location before the function is
14799a2dd95SBruce Richardson  *   called.
14899a2dd95SBruce Richardson  * @return
14999a2dd95SBruce Richardson  *   0 on success, error code otherwise
15099a2dd95SBruce Richardson  */
15199a2dd95SBruce Richardson typedef int (*rte_table_op_entry_add_bulk)(
15299a2dd95SBruce Richardson 	void *table,
15399a2dd95SBruce Richardson 	void **keys,
15499a2dd95SBruce Richardson 	void **entries,
15599a2dd95SBruce Richardson 	uint32_t n_keys,
15699a2dd95SBruce Richardson 	int *key_found,
15799a2dd95SBruce Richardson 	void **entries_ptr);
15899a2dd95SBruce Richardson 
15999a2dd95SBruce Richardson /**
16099a2dd95SBruce Richardson  * Lookup table entry delete bulk
16199a2dd95SBruce Richardson  *
16299a2dd95SBruce Richardson  * @param table
16399a2dd95SBruce Richardson  *   Handle to lookup table instance
16499a2dd95SBruce Richardson  * @param keys
16599a2dd95SBruce Richardson  *   Array containing lookup keys
16699a2dd95SBruce Richardson  * @param n_keys
16799a2dd95SBruce Richardson  *   Number of keys to delete
16899a2dd95SBruce Richardson  * @param key_found
16999a2dd95SBruce Richardson  *   After successful invocation, key_found for every item in the array is set
17099a2dd95SBruce Richardson  *   to a value different than 0if the current key was present in the table
17199a2dd95SBruce Richardson  *   before the delete operation was performed and to 0 if not. This pointer
17299a2dd95SBruce Richardson  *   has to be set to a valid memory location before the table entry delete
17399a2dd95SBruce Richardson  *   function is called.
17499a2dd95SBruce Richardson  * @param entries
17599a2dd95SBruce Richardson  *   If entries pointer is NULL, this pointer is ignored for every entry found.
17699a2dd95SBruce Richardson  *   Else, after successful invocation, if specific key is found in the table
17799a2dd95SBruce Richardson  *   (key_found is different than 0 for this item after function call is
17899a2dd95SBruce Richardson  *   completed) and item of entry array points to a valid buffer (entry is set
17999a2dd95SBruce Richardson  *   to a value different than NULL before the function is called), then the
18099a2dd95SBruce Richardson  *   first entry_size bytes (table create parameter) in *entry store a copy of
18199a2dd95SBruce Richardson  *   table entry that contained the data associated with the current key before
18299a2dd95SBruce Richardson  *   the key was deleted.
18399a2dd95SBruce Richardson  * @return
18499a2dd95SBruce Richardson  *   0 on success, error code otherwise
18599a2dd95SBruce Richardson  */
18699a2dd95SBruce Richardson typedef int (*rte_table_op_entry_delete_bulk)(
18799a2dd95SBruce Richardson 	void *table,
18899a2dd95SBruce Richardson 	void **keys,
18999a2dd95SBruce Richardson 	uint32_t n_keys,
19099a2dd95SBruce Richardson 	int *key_found,
19199a2dd95SBruce Richardson 	void **entries);
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson /**
19499a2dd95SBruce Richardson  * Lookup table lookup
19599a2dd95SBruce Richardson  *
19699a2dd95SBruce Richardson  * @param table
19799a2dd95SBruce Richardson  *   Handle to lookup table instance
19899a2dd95SBruce Richardson  * @param pkts
19999a2dd95SBruce Richardson  *   Burst of input packets specified as array of up to 64 pointers to struct
20099a2dd95SBruce Richardson  *   rte_mbuf
20199a2dd95SBruce Richardson  * @param pkts_mask
20299a2dd95SBruce Richardson  *   64-bit bitmask specifying which packets in the input burst are valid. When
20399a2dd95SBruce Richardson  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
20499a2dd95SBruce Richardson  *   valid packet. Otherwise, element n of pkts array does not point to a valid
20599a2dd95SBruce Richardson  *   packet, therefore it will not be accessed.
20699a2dd95SBruce Richardson  * @param lookup_hit_mask
20799a2dd95SBruce Richardson  *   Once the table lookup operation is completed, this 64-bit bitmask
20899a2dd95SBruce Richardson  *   specifies which of the valid packets in the input burst resulted in lookup
20999a2dd95SBruce Richardson  *   hit. For each valid input packet (pkts_mask bit n is set), the following
21099a2dd95SBruce Richardson  *   are true on lookup hit: lookup_hit_mask bit n is set, element n of entries
21199a2dd95SBruce Richardson  *   array is valid and it points to the lookup table entry that was hit. For
21299a2dd95SBruce Richardson  *   each valid input packet (pkts_mask bit n is set), the following are true
21399a2dd95SBruce Richardson  *   on lookup miss: lookup_hit_mask bit n is not set and element n of entries
21499a2dd95SBruce Richardson  *   array is not valid.
21599a2dd95SBruce Richardson  * @param entries
21699a2dd95SBruce Richardson  *   Once the table lookup operation is completed, this array provides the
21799a2dd95SBruce Richardson  *   lookup table entries that were hit, as described above. It is required
21899a2dd95SBruce Richardson  *   that this array is always pre-allocated by the caller of this function
21999a2dd95SBruce Richardson  *   with exactly 64 elements. The implementation is allowed to speculatively
22099a2dd95SBruce Richardson  *   modify the elements of this array, so elements marked as invalid in
22199a2dd95SBruce Richardson  *   lookup_hit_mask once the table lookup operation is completed might have
22299a2dd95SBruce Richardson  *   been modified by this function.
22399a2dd95SBruce Richardson  * @return
22499a2dd95SBruce Richardson  *   0 on success, error code otherwise
22599a2dd95SBruce Richardson  */
22699a2dd95SBruce Richardson typedef int (*rte_table_op_lookup)(
22799a2dd95SBruce Richardson 	void *table,
22899a2dd95SBruce Richardson 	struct rte_mbuf **pkts,
22999a2dd95SBruce Richardson 	uint64_t pkts_mask,
23099a2dd95SBruce Richardson 	uint64_t *lookup_hit_mask,
23199a2dd95SBruce Richardson 	void **entries);
23299a2dd95SBruce Richardson 
23399a2dd95SBruce Richardson /**
23499a2dd95SBruce Richardson  * Lookup table stats read
23599a2dd95SBruce Richardson  *
23699a2dd95SBruce Richardson  * @param table
23799a2dd95SBruce Richardson  *   Handle to lookup table instance
23899a2dd95SBruce Richardson  * @param stats
23999a2dd95SBruce Richardson  *   Handle to table stats struct to copy data
24099a2dd95SBruce Richardson  * @param clear
24199a2dd95SBruce Richardson  *   Flag indicating that stats should be cleared after read
24299a2dd95SBruce Richardson  *
24399a2dd95SBruce Richardson  * @return
24499a2dd95SBruce Richardson  *   Error code or 0 on success.
24599a2dd95SBruce Richardson  */
24699a2dd95SBruce Richardson typedef int (*rte_table_op_stats_read)(
24799a2dd95SBruce Richardson 	void *table,
24899a2dd95SBruce Richardson 	struct rte_table_stats *stats,
24999a2dd95SBruce Richardson 	int clear);
25099a2dd95SBruce Richardson 
25199a2dd95SBruce Richardson /** Lookup table interface defining the lookup table operation */
25299a2dd95SBruce Richardson struct rte_table_ops {
25399a2dd95SBruce Richardson 	rte_table_op_create f_create;                 /**< Create */
25499a2dd95SBruce Richardson 	rte_table_op_free f_free;                     /**< Free */
25599a2dd95SBruce Richardson 	rte_table_op_entry_add f_add;                 /**< Entry add */
25699a2dd95SBruce Richardson 	rte_table_op_entry_delete f_delete;           /**< Entry delete */
25799a2dd95SBruce Richardson 	rte_table_op_entry_add_bulk f_add_bulk;       /**< Add entry bulk */
25899a2dd95SBruce Richardson 	rte_table_op_entry_delete_bulk f_delete_bulk; /**< Delete entry bulk */
25999a2dd95SBruce Richardson 	rte_table_op_lookup f_lookup;                 /**< Lookup */
26099a2dd95SBruce Richardson 	rte_table_op_stats_read f_stats;              /**< Stats */
26199a2dd95SBruce Richardson };
26299a2dd95SBruce Richardson 
26399a2dd95SBruce Richardson #endif
264