Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_table / rte_table.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __INCLUDE_RTE_TABLE_H__
35 #define __INCLUDE_RTE_TABLE_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * @file
43  * RTE Table
44  *
45  * This tool is part of the DPDK Packet Framework tool suite and provides
46  * a standard interface to implement different types of lookup tables for data
47  * plane processing.
48  *
49  * Virtually any search algorithm that can uniquely associate data to a lookup
50  * key can be fitted under this lookup table abstraction. For the flow table
51  * use-case, the lookup key is an n-tuple of packet fields that uniquely
52  * identifies a traffic flow, while data represents actions and action
53  * meta-data associated with the same traffic flow.
54  *
55  ***/
56
57 #include <stdint.h>
58 #include <rte_port.h>
59
60 struct rte_mbuf;
61
62 /** Lookup table statistics */
63 struct rte_table_stats {
64         uint64_t n_pkts_in;
65         uint64_t n_pkts_lookup_miss;
66 };
67
68 /**
69  * Lookup table create
70  *
71  * @param params
72  *   Parameters for lookup table creation. The underlying data structure is
73  *   different for each lookup table type.
74  * @param socket_id
75  *   CPU socket ID (e.g. for memory allocation purpose)
76  * @param entry_size
77  *   Data size of each lookup table entry (measured in bytes)
78  * @return
79  *   Handle to lookup table instance
80  */
81 typedef void* (*rte_table_op_create)(void *params, int socket_id,
82         uint32_t entry_size);
83
84 /**
85  * Lookup table free
86  *
87  * @param table
88  *   Handle to lookup table instance
89  * @return
90  *   0 on success, error code otherwise
91  */
92 typedef int (*rte_table_op_free)(void *table);
93
94 /**
95  * Lookup table entry add
96  *
97  * @param table
98  *   Handle to lookup table instance
99  * @param key
100  *   Lookup key
101  * @param entry
102  *   Data to be associated with the current key. This parameter has to point to
103  *   a valid memory buffer where the first entry_size bytes (table create
104  *   parameter) are populated with the data.
105  * @param key_found
106  *   After successful invocation, *key_found is set to a value different than 0
107  *   if the current key is already present in the table and to 0 if not. This
108  *   pointer has to be set to a valid memory location before the table entry add
109  *   function is called.
110  * @param entry_ptr
111  *   After successful invocation, *entry_ptr stores the handle to the table
112  *   entry containing the data associated with the current key. This handle can
113  *   be used to perform further read-write accesses to this entry. This handle
114  *   is valid until the key is deleted from the table or the same key is
115  *   re-added to the table, typically to associate it with different data. This
116  *   pointer has to be set to a valid memory location before the function is
117  *   called.
118  * @return
119  *   0 on success, error code otherwise
120  */
121 typedef int (*rte_table_op_entry_add)(
122         void *table,
123         void *key,
124         void *entry,
125         int *key_found,
126         void **entry_ptr);
127
128 /**
129  * Lookup table entry delete
130  *
131  * @param table
132  *   Handle to lookup table instance
133  * @param key
134  *   Lookup key
135  * @param key_found
136  *   After successful invocation, *key_found is set to a value different than 0
137  *   if the current key was present in the table before the delete operation
138  *   was performed and to 0 if not. This pointer has to be set to a valid
139  *   memory location before the table entry delete function is called.
140  * @param entry
141  *   After successful invocation, if the key is found in the table (*key found
142  *   is different than 0 after function call is completed) and entry points to
143  *   a valid buffer (entry is set to a value different than NULL before the
144  *   function is called), then the first entry_size bytes (table create
145  *   parameter) in *entry store a copy of table entry that contained the data
146  *   associated with the current key before the key was deleted.
147  * @return
148  *   0 on success, error code otherwise
149  */
150 typedef int (*rte_table_op_entry_delete)(
151         void *table,
152         void *key,
153         int *key_found,
154         void *entry);
155
156 /**
157  * Lookup table entry add bulk
158  *
159  * @param table
160  *   Handle to lookup table instance
161  * @param key
162  *   Array containing lookup keys
163  * @param entries
164  *   Array containing data to be associated with each key. Every item in the
165  *   array has to point to a valid memory buffer where the first entry_size
166  *   bytes (table create parameter) are populated with the data.
167  * @param n_keys
168  *   Number of keys to add
169  * @param key_found
170  *   After successful invocation, key_found for every item in the array is set
171  *   to a value different than 0 if the current key is already present in the
172  *   table and to 0 if not. This pointer has to be set to a valid memory
173  *   location before the table entry add function is called.
174  * @param entries_ptr
175  *   After successful invocation, array *entries_ptr stores the handle to the
176  *   table entry containing the data associated with every key. This handle can
177  *   be used to perform further read-write accesses to this entry. This handle
178  *   is valid until the key is deleted from the table or the same key is
179  *   re-added to the table, typically to associate it with different data. This
180  *   pointer has to be set to a valid memory location before the function is
181  *   called.
182  * @return
183  *   0 on success, error code otherwise
184  */
185 typedef int (*rte_table_op_entry_add_bulk)(
186         void *table,
187         void **keys,
188         void **entries,
189         uint32_t n_keys,
190         int *key_found,
191         void **entries_ptr);
192
193 /**
194  * Lookup table entry delete bulk
195  *
196  * @param table
197  *   Handle to lookup table instance
198  * @param key
199  *   Array containing lookup keys
200  * @param n_keys
201  *   Number of keys to delete
202  * @param key_found
203  *   After successful invocation, key_found for every item in the array is set
204  *   to a value different than 0if the current key was present in the table
205  *   before the delete operation was performed and to 0 if not. This pointer
206  *   has to be set to a valid memory location before the table entry delete
207  *   function is called.
208  * @param entries
209  *   If entries pointer is NULL, this pointer is ignored for every entry found.
210  *   Else, after successful invocation, if specific key is found in the table
211  *   (key_found is different than 0 for this item after function call is
212  *   completed) and item of entry array points to a valid buffer (entry is set
213  *   to a value different than NULL before the function is called), then the
214  *   first entry_size bytes (table create parameter) in *entry store a copy of
215  *   table entry that contained the data associated with the current key before
216  *   the key was deleted.
217  * @return
218  *   0 on success, error code otherwise
219  */
220 typedef int (*rte_table_op_entry_delete_bulk)(
221         void *table,
222         void **keys,
223         uint32_t n_keys,
224         int *key_found,
225         void **entries);
226
227 /**
228  * Lookup table lookup
229  *
230  * @param table
231  *   Handle to lookup table instance
232  * @param pkts
233  *   Burst of input packets specified as array of up to 64 pointers to struct
234  *   rte_mbuf
235  * @param pkts_mask
236  *   64-bit bitmask specifying which packets in the input burst are valid. When
237  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
238  *   valid packet. Otherwise, element n of pkts array does not point to a valid
239  *   packet, therefore it will not be accessed.
240  * @param lookup_hit_mask
241  *   Once the table lookup operation is completed, this 64-bit bitmask
242  *   specifies which of the valid packets in the input burst resulted in lookup
243  *   hit. For each valid input packet (pkts_mask bit n is set), the following
244  *   are true on lookup hit: lookup_hit_mask bit n is set, element n of entries
245  *   array is valid and it points to the lookup table entry that was hit. For
246  *   each valid input packet (pkts_mask bit n is set), the following are true
247  *   on lookup miss: lookup_hit_mask bit n is not set and element n of entries
248  *   array is not valid.
249  * @param entries
250  *   Once the table lookup operation is completed, this array provides the
251  *   lookup table entries that were hit, as described above. It is required
252  *   that this array is always pre-allocated by the caller of this function
253  *   with exactly 64 elements. The implementation is allowed to speculatively
254  *   modify the elements of this array, so elements marked as invalid in
255  *   lookup_hit_mask once the table lookup operation is completed might have
256  *   been modified by this function.
257  * @return
258  *   0 on success, error code otherwise
259  */
260 typedef int (*rte_table_op_lookup)(
261         void *table,
262         struct rte_mbuf **pkts,
263         uint64_t pkts_mask,
264         uint64_t *lookup_hit_mask,
265         void **entries);
266
267 /**
268  * Lookup table stats read
269  *
270  * @param table
271  *   Handle to lookup table instance
272  * @param stats
273  *   Handle to table stats struct to copy data
274  * @param clear
275  *   Flag indicating that stats should be cleared after read
276  *
277  * @return
278  *   Error code or 0 on success.
279  */
280 typedef int (*rte_table_op_stats_read)(
281         void *table,
282         struct rte_table_stats *stats,
283         int clear);
284
285 /** Lookup table interface defining the lookup table operation */
286 struct rte_table_ops {
287         rte_table_op_create f_create;                 /**< Create */
288         rte_table_op_free f_free;                     /**< Free */
289         rte_table_op_entry_add f_add;                 /**< Entry add */
290         rte_table_op_entry_delete f_delete;           /**< Entry delete */
291         rte_table_op_entry_add_bulk f_add_bulk;       /**< Add entry bulk */
292         rte_table_op_entry_delete_bulk f_delete_bulk; /**< Delete entry bulk */
293         rte_table_op_lookup f_lookup;                 /**< Lookup */
294         rte_table_op_stats_read f_stats;              /**< Stats */
295 };
296
297 #ifdef __cplusplus
298 }
299 #endif
300
301 #endif