fib: promote API to stable
[dpdk.git] / lib / 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  * FIB (Forwarding information base) implementation
15  * for IPv4 Longest Prefix Match
16  */
17
18 #include <stdint.h>
19
20 #include <rte_compat.h>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 struct rte_fib;
27 struct rte_rib;
28
29 /** Maximum depth value possible for IPv4 FIB. */
30 #define RTE_FIB_MAXDEPTH        32
31
32 /** Type of FIB struct */
33 enum rte_fib_type {
34         RTE_FIB_DUMMY,          /**< RIB tree based FIB */
35         RTE_FIB_DIR24_8         /**< DIR24_8 based FIB */
36 };
37
38 /** Modify FIB function */
39 typedef int (*rte_fib_modify_fn_t)(struct rte_fib *fib, uint32_t ip,
40         uint8_t depth, uint64_t next_hop, int op);
41 /** FIB bulk lookup function */
42 typedef void (*rte_fib_lookup_fn_t)(void *fib, const uint32_t *ips,
43         uint64_t *next_hops, const unsigned int n);
44
45 enum rte_fib_op {
46         RTE_FIB_ADD,
47         RTE_FIB_DEL,
48 };
49
50 /** Size of nexthop (1 << nh_sz) bits for DIR24_8 based FIB */
51 enum rte_fib_dir24_8_nh_sz {
52         RTE_FIB_DIR24_8_1B,
53         RTE_FIB_DIR24_8_2B,
54         RTE_FIB_DIR24_8_4B,
55         RTE_FIB_DIR24_8_8B
56 };
57
58 /** Type of lookup function implementation */
59 enum rte_fib_lookup_type {
60         RTE_FIB_LOOKUP_DEFAULT,
61         /**< Selects the best implementation based on the max simd bitwidth */
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         RTE_FIB_LOOKUP_DIR24_8_VECTOR_AVX512
74         /**< Vector implementation using AVX512 */
75 };
76
77 /** FIB configuration structure */
78 struct rte_fib_conf {
79         enum rte_fib_type type; /**< Type of FIB struct */
80         /** Default value returned on lookup if there is no route */
81         uint64_t default_nh;
82         int     max_routes;
83         union {
84                 struct {
85                         enum rte_fib_dir24_8_nh_sz nh_sz;
86                         uint32_t        num_tbl8;
87                 } dir24_8;
88         };
89 };
90
91 /**
92  * Create FIB
93  *
94  * @param name
95  *  FIB name
96  * @param socket_id
97  *  NUMA socket ID for FIB table memory allocation
98  * @param conf
99  *  Structure containing the configuration
100  * @return
101  *  Handle to the FIB object on success
102  *  NULL otherwise with rte_errno set to an appropriate values.
103  */
104 struct rte_fib *
105 rte_fib_create(const char *name, int socket_id, struct rte_fib_conf *conf);
106
107 /**
108  * Find an existing FIB object and return a pointer to it.
109  *
110  * @param name
111  *  Name of the fib object as passed to rte_fib_create()
112  * @return
113  *  Pointer to fib object or NULL if object not found with rte_errno
114  *  set appropriately. Possible rte_errno values include:
115  *   - ENOENT - required entry not available to return.
116  */
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 void
129 rte_fib_free(struct rte_fib *fib);
130
131 /**
132  * Add a route to the FIB.
133  *
134  * @param fib
135  *   FIB object handle
136  * @param ip
137  *   IPv4 prefix address to be added to the FIB
138  * @param depth
139  *   Prefix length
140  * @param next_hop
141  *   Next hop to be added to the FIB
142  * @return
143  *   0 on success, negative value otherwise
144  */
145 int
146 rte_fib_add(struct rte_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop);
147
148 /**
149  * Delete a rule from the FIB.
150  *
151  * @param fib
152  *   FIB object handle
153  * @param ip
154  *   IPv4 prefix address to be deleted from the FIB
155  * @param depth
156  *   Prefix length
157  * @return
158  *   0 on success, negative value otherwise
159  */
160 int
161 rte_fib_delete(struct rte_fib *fib, uint32_t ip, uint8_t depth);
162
163 /**
164  * Lookup multiple IP addresses in the FIB.
165  *
166  * @param fib
167  *   FIB object handle
168  * @param ips
169  *   Array of IPs to be looked up in the FIB
170  * @param next_hops
171  *   Next hop of the most specific rule found for IP.
172  *   This is an array of eight byte values.
173  *   If the lookup for the given IP failed, then corresponding element would
174  *   contain default nexthop value configured for a FIB.
175  * @param n
176  *   Number of elements in ips (and next_hops) array to lookup.
177  *  @return
178  *   -EINVAL for incorrect arguments, otherwise 0
179  */
180 int
181 rte_fib_lookup_bulk(struct rte_fib *fib, uint32_t *ips,
182                 uint64_t *next_hops, int n);
183 /**
184  * Get pointer to the dataplane specific struct
185  *
186  * @param fib
187  *   FIB object handle
188  * @return
189  *   Pointer on the dataplane struct on success
190  *   NULL othervise
191  */
192 void *
193 rte_fib_get_dp(struct rte_fib *fib);
194
195 /**
196  * Get pointer to the RIB
197  *
198  * @param fib
199  *   FIB object handle
200  * @return
201  *   Pointer on the RIB on success
202  *   NULL othervise
203  */
204 struct rte_rib *
205 rte_fib_get_rib(struct rte_fib *fib);
206
207 /**
208  * Set lookup function based on type
209  *
210  * @param fib
211  *   FIB object handle
212  * @param type
213  *   type of lookup function
214  *
215  * @return
216  *   0 on success
217  *   -EINVAL on failure
218  */
219 int
220 rte_fib_select_lookup(struct rte_fib *fib, enum rte_fib_lookup_type type);
221
222 #ifdef __cplusplus
223 }
224 #endif
225
226 #endif /* _RTE_FIB_H_ */