6e51fe686597579c1bf90800441f48d8f1e38564
[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 Intel 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 /**
63  * Lookup table create
64  *
65  * @param params
66  *   Parameters for lookup table creation. The underlying data structure is
67  *   different for each lookup table type.
68  * @param socket_id
69  *   CPU socket ID (e.g. for memory allocation purpose)
70  * @param entry_size
71  *   Data size of each lookup table entry (measured in bytes)
72  * @return
73  *   Handle to lookup table instance
74  */
75 typedef void* (*rte_table_op_create)(void *params, int socket_id,
76         uint32_t entry_size);
77
78 /**
79  * Lookup table free
80  *
81  * @param table
82  *   Handle to lookup table instance
83  * @return
84  *   0 on success, error code otherwise
85  */
86 typedef int (*rte_table_op_free)(void *table);
87
88 /**
89  * Lookup table entry add
90  *
91  * @param table
92  *   Handle to lookup table instance
93  * @param key
94  *   Lookup key
95  * @param entry
96  *   Data to be associated with the current key. This parameter has to point to
97  *   a valid memory buffer where the first entry_size bytes (table create
98  *   parameter) are populated with the data.
99  * @param key_found
100  *   After successful invocation, *key_found is set to a value different than 0
101  *   if the current key is already present in the table and to 0 if not. This
102  *   pointer has to be set to a valid memory location before the table entry add
103  *   function is called.
104  * @param entry_ptr
105  *   After successful invocation, *entry_ptr stores the handle to the table
106  *   entry containing the data associated with the current key. This handle can
107  *   be used to perform further read-write accesses to this entry. This handle
108  *   is valid until the key is deleted from the table or the same key is
109  *   re-added to the table, typically to associate it with different data. This
110  *   pointer has to be set to a valid memory location before the function is
111  *   called.
112  * @return
113  *   0 on success, error code otherwise
114  */
115 typedef int (*rte_table_op_entry_add)(
116         void *table,
117         void *key,
118         void *entry,
119         int *key_found,
120         void **entry_ptr);
121
122 /**
123  * Lookup table entry delete
124  *
125  * @param table
126  *   Handle to lookup table instance
127  * @param key
128  *   Lookup key
129  * @param key_found
130  *   After successful invocation, *key_found is set to a value different than 0
131  *   if the current key was present in the table before the delete operation
132  *   was performed and to 0 if not. This pointer has to be set to a valid
133  *   memory location before the table entry delete function is called.
134  * @param entry
135  *   After successful invocation, if the key is found in the table (*key found
136  *   is different than 0 after function call is completed) and entry points to
137  *   a valid buffer (entry is set to a value different than NULL before the
138  *   function is called), then the first entry_size bytes (table create
139  *   parameter) in *entry store a copy of table entry that contained the data
140  *   associated with the current key before the key was deleted.
141  * @return
142  *   0 on success, error code otherwise
143  */
144 typedef int (*rte_table_op_entry_delete)(
145         void *table,
146         void *key,
147         int *key_found,
148         void *entry);
149
150 /**
151  * Lookup table lookup
152  *
153  * @param table
154  *   Handle to lookup table instance
155  * @param pkts
156  *   Burst of input packets specified as array of up to 64 pointers to struct
157  *   rte_mbuf
158  * @param pkts_mask
159  *   64-bit bitmask specifying which packets in the input burst are valid. When
160  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
161  *   valid packet. Otherwise, element n of pkts array does not point to a valid
162  *   packet, therefore it will not be accessed.
163  * @param lookup_hit_mask
164  *   Once the table lookup operation is completed, this 64-bit bitmask
165  *   specifies which of the valid packets in the input burst resulted in lookup
166  *   hit. For each valid input packet (pkts_mask bit n is set), the following
167  *   are true on lookup hit: lookup_hit_mask bit n is set, element n of entries
168  *   array is valid and it points to the lookup table entry that was hit. For
169  *   each valid input packet (pkts_mask bit n is set), the following are true
170  *   on lookup miss: lookup_hit_mask bit n is not set and element n of entries
171  *   array is not valid.
172  * @param entries
173  *   Once the table lookup operation is completed, this array provides the
174  *   lookup table entries that were hit, as described above. It is required
175  *   that this array is always pre-allocated by the caller of this function
176  *   with exactly 64 elements. The implementation is allowed to speculatively
177  *   modify the elements of this array, so elements marked as invalid in
178  *   lookup_hit_mask once the table lookup operation is completed might have
179  *   been modified by this function.
180  * @return
181  *   0 on success, error code otherwise
182  */
183 typedef int (*rte_table_op_lookup)(
184         void *table,
185         struct rte_mbuf **pkts,
186         uint64_t pkts_mask,
187         uint64_t *lookup_hit_mask,
188         void **entries);
189
190 /** Lookup table interface defining the lookup table operation */
191 struct rte_table_ops {
192         rte_table_op_create f_create;       /**< Create */
193         rte_table_op_free f_free;           /**< Free */
194         rte_table_op_entry_add f_add;       /**< Entry add */
195         rte_table_op_entry_delete f_delete; /**< Entry delete */
196         rte_table_op_lookup f_lookup;       /**< Lookup */
197 };
198
199 #ifdef __cplusplus
200 }
201 #endif
202
203 #endif