fib: add lookup runtime selection
[dpdk.git] / lib / librte_fib / rte_fib.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3  * Copyright(c) 2019 Intel Corporation
4  */
5
6 #ifndef _RTE_FIB_H_
7 #define _RTE_FIB_H_
8
9 /**
10  * @file
11  *
12  * RTE FIB library.
13  *
14  * @warning
15  * @b EXPERIMENTAL:
16  * All functions in this file may be changed or removed without prior notice.
17  *
18  * FIB (Forwarding information base) implementation
19  * for IPv4 Longest Prefix Match
20  */
21
22 #include <rte_compat.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 struct rte_fib;
29 struct rte_rib;
30
31 /** Maximum depth value possible for IPv4 FIB. */
32 #define RTE_FIB_MAXDEPTH        32
33
34 /** Type of FIB struct */
35 enum rte_fib_type {
36         RTE_FIB_DUMMY,          /**< RIB tree based FIB */
37         RTE_FIB_DIR24_8         /**< DIR24_8 based FIB */
38 };
39
40 /** Modify FIB function */
41 typedef int (*rte_fib_modify_fn_t)(struct rte_fib *fib, uint32_t ip,
42         uint8_t depth, uint64_t next_hop, int op);
43 /** FIB bulk lookup function */
44 typedef void (*rte_fib_lookup_fn_t)(void *fib, const uint32_t *ips,
45         uint64_t *next_hops, const unsigned int n);
46
47 enum rte_fib_op {
48         RTE_FIB_ADD,
49         RTE_FIB_DEL,
50 };
51
52 /** Size of nexthop (1 << nh_sz) bits for DIR24_8 based FIB */
53 enum rte_fib_dir24_8_nh_sz {
54         RTE_FIB_DIR24_8_1B,
55         RTE_FIB_DIR24_8_2B,
56         RTE_FIB_DIR24_8_4B,
57         RTE_FIB_DIR24_8_8B
58 };
59
60 /** Type of lookup function implementation */
61 enum rte_fib_lookup_type {
62         RTE_FIB_LOOKUP_DIR24_8_SCALAR_MACRO,
63         /**< Macro based lookup function */
64         RTE_FIB_LOOKUP_DIR24_8_SCALAR_INLINE,
65         /**<
66          * Lookup implementation using inlined functions
67          * for different next hop sizes
68          */
69         RTE_FIB_LOOKUP_DIR24_8_SCALAR_UNI,
70         /**<
71          * Unified lookup function for all next hop sizes
72          */
73 };
74
75 /** FIB configuration structure */
76 struct rte_fib_conf {
77         enum rte_fib_type type; /**< Type of FIB struct */
78         /** Default value returned on lookup if there is no route */
79         uint64_t default_nh;
80         int     max_routes;
81         union {
82                 struct {
83                         enum rte_fib_dir24_8_nh_sz nh_sz;
84                         uint32_t        num_tbl8;
85                 } dir24_8;
86         };
87 };
88
89 /**
90  * Create FIB
91  *
92  * @param name
93  *  FIB name
94  * @param socket_id
95  *  NUMA socket ID for FIB table memory allocation
96  * @param conf
97  *  Structure containing the configuration
98  * @return
99  *  Handle to the FIB object on success
100  *  NULL otherwise with rte_errno set to an appropriate values.
101  */
102 __rte_experimental
103 struct rte_fib *
104 rte_fib_create(const char *name, int socket_id, struct rte_fib_conf *conf);
105
106 /**
107  * Find an existing FIB object and return a pointer to it.
108  *
109  * @param name
110  *  Name of the fib object as passed to rte_fib_create()
111  * @return
112  *  Pointer to fib object or NULL if object not found with rte_errno
113  *  set appropriately. Possible rte_errno values include:
114  *   - ENOENT - required entry not available to return.
115  */
116 __rte_experimental
117 struct rte_fib *
118 rte_fib_find_existing(const char *name);
119
120 /**
121  * Free an FIB object.
122  *
123  * @param fib
124  *   FIB object handle
125  * @return
126  *   None
127  */
128 __rte_experimental
129 void
130 rte_fib_free(struct rte_fib *fib);
131
132 /**
133  * Add a route to the FIB.
134  *
135  * @param fib
136  *   FIB object handle
137  * @param ip
138  *   IPv4 prefix address to be added to the FIB
139  * @param depth
140  *   Prefix length
141  * @param next_hop
142  *   Next hop to be added to the FIB
143  * @return
144  *   0 on success, negative value otherwise
145  */
146 __rte_experimental
147 int
148 rte_fib_add(struct rte_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop);
149
150 /**
151  * Delete a rule from the FIB.
152  *
153  * @param fib
154  *   FIB object handle
155  * @param ip
156  *   IPv4 prefix address to be deleted from the FIB
157  * @param depth
158  *   Prefix length
159  * @return
160  *   0 on success, negative value otherwise
161  */
162 __rte_experimental
163 int
164 rte_fib_delete(struct rte_fib *fib, uint32_t ip, uint8_t depth);
165
166 /**
167  * Lookup multiple IP addresses in the FIB.
168  *
169  * @param fib
170  *   FIB object handle
171  * @param ips
172  *   Array of IPs to be looked up in the FIB
173  * @param next_hops
174  *   Next hop of the most specific rule found for IP.
175  *   This is an array of eight byte values.
176  *   If the lookup for the given IP failed, then corresponding element would
177  *   contain default nexthop value configured for a FIB.
178  * @param n
179  *   Number of elements in ips (and next_hops) array to lookup.
180  *  @return
181  *   -EINVAL for incorrect arguments, otherwise 0
182  */
183 __rte_experimental
184 int
185 rte_fib_lookup_bulk(struct rte_fib *fib, uint32_t *ips,
186                 uint64_t *next_hops, int n);
187 /**
188  * Get pointer to the dataplane specific struct
189  *
190  * @param fib
191  *   FIB object handle
192  * @return
193  *   Pointer on the dataplane struct on success
194  *   NULL othervise
195  */
196 __rte_experimental
197 void *
198 rte_fib_get_dp(struct rte_fib *fib);
199
200 /**
201  * Get pointer to the RIB
202  *
203  * @param fib
204  *   FIB object handle
205  * @return
206  *   Pointer on the RIB on success
207  *   NULL othervise
208  */
209 __rte_experimental
210 struct rte_rib *
211 rte_fib_get_rib(struct rte_fib *fib);
212
213 /**
214  * Set lookup function based on type
215  *
216  * @param fib
217  *   FIB object handle
218  * @param type
219  *   type of lookup function
220  *
221  * @return
222  *   0 on success
223  *   -EINVAL on failure
224  */
225 __rte_experimental
226 int
227 rte_fib_select_lookup(struct rte_fib *fib, enum rte_fib_lookup_type type);
228
229 #ifdef __cplusplus
230 }
231 #endif
232
233 #endif /* _RTE_FIB_H_ */