xref: /dpdk/lib/fib/rte_fib.h (revision df8b5bf744f9aa9a1fbb790dac14a32d9cc9f236)
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_rcu_qsbr.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 /** @internal Default RCU defer queue entries to reclaim in one go. */
33 #define RTE_FIB_RCU_DQ_RECLAIM_MAX	16
34 /** @internal Default RCU defer queue size. */
35 #define RTE_FIB_RCU_DQ_RECLAIM_SZ	128
36 
37 /** RCU reclamation modes */
38 enum rte_fib_qsbr_mode {
39 	/** Create defer queue for reclaim. */
40 	RTE_FIB_QSBR_MODE_DQ = 0,
41 	/** Use blocking mode reclaim. No defer queue created. */
42 	RTE_FIB_QSBR_MODE_SYNC
43 };
44 
45 /** Type of FIB struct */
46 enum rte_fib_type {
47 	RTE_FIB_DUMMY,		/**< RIB tree based FIB */
48 	RTE_FIB_DIR24_8		/**< DIR24_8 based FIB */
49 };
50 
51 /** Modify FIB function */
52 typedef int (*rte_fib_modify_fn_t)(struct rte_fib *fib, uint32_t ip,
53 	uint8_t depth, uint64_t next_hop, int op);
54 /** FIB bulk lookup function */
55 typedef void (*rte_fib_lookup_fn_t)(void *fib, const uint32_t *ips,
56 	uint64_t *next_hops, const unsigned int n);
57 
58 enum rte_fib_op {
59 	RTE_FIB_ADD,
60 	RTE_FIB_DEL,
61 };
62 
63 /** Size of nexthop (1 << nh_sz) bits for DIR24_8 based FIB */
64 enum rte_fib_dir24_8_nh_sz {
65 	RTE_FIB_DIR24_8_1B,
66 	RTE_FIB_DIR24_8_2B,
67 	RTE_FIB_DIR24_8_4B,
68 	RTE_FIB_DIR24_8_8B
69 };
70 
71 /** Type of lookup function implementation */
72 enum rte_fib_lookup_type {
73 	RTE_FIB_LOOKUP_DEFAULT,
74 	/**< Selects the best implementation based on the max simd bitwidth */
75 	RTE_FIB_LOOKUP_DIR24_8_SCALAR_MACRO,
76 	/**< Macro based lookup function */
77 	RTE_FIB_LOOKUP_DIR24_8_SCALAR_INLINE,
78 	/**<
79 	 * Lookup implementation using inlined functions
80 	 * for different next hop sizes
81 	 */
82 	RTE_FIB_LOOKUP_DIR24_8_SCALAR_UNI,
83 	/**<
84 	 * Unified lookup function for all next hop sizes
85 	 */
86 	RTE_FIB_LOOKUP_DIR24_8_VECTOR_AVX512
87 	/**< Vector implementation using AVX512 */
88 };
89 
90 /** If set, fib lookup is expecting IPv4 address in network byte order */
91 #define RTE_FIB_F_LOOKUP_NETWORK_ORDER 1
92 #define RTE_FIB_ALLOWED_FLAGS (RTE_FIB_F_LOOKUP_NETWORK_ORDER)
93 
94 /** FIB configuration structure */
95 struct rte_fib_conf {
96 	enum rte_fib_type type; /**< Type of FIB struct */
97 	/** Default value returned on lookup if there is no route */
98 	uint64_t default_nh;
99 	int	max_routes;
100 	/** Size of the node extension in the internal RIB struct */
101 	unsigned int rib_ext_sz;
102 	union {
103 		struct {
104 			enum rte_fib_dir24_8_nh_sz nh_sz;
105 			uint32_t	num_tbl8;
106 		} dir24_8;
107 	};
108 	unsigned int flags; /**< Optional feature flags from RTE_FIB_F_* **/
109 };
110 
111 /** FIB RCU QSBR configuration structure. */
112 struct rte_fib_rcu_config {
113 	/** RCU QSBR variable. */
114 	struct rte_rcu_qsbr *v;
115 	/** Mode of RCU QSBR. See RTE_FIB_QSBR_MODE_xxx.
116 	 * Default: RTE_FIB_QSBR_MODE_DQ, create defer queue for reclaim.
117 	 */
118 	enum rte_fib_qsbr_mode mode;
119 	/** RCU defer queue size.
120 	 * Default: RTE_FIB_RCU_DQ_RECLAIM_SZ.
121 	 */
122 	uint32_t dq_size;
123 	/** Threshold to trigger auto reclaim. */
124 	uint32_t reclaim_thd;
125 	/** Max entries to reclaim in one go.
126 	 * Default: RTE_FIB_RCU_DQ_RECLAIM_MAX.
127 	 */
128 	uint32_t reclaim_max;
129 };
130 
131 /**
132  * Create FIB
133  *
134  * @param name
135  *  FIB name
136  * @param socket_id
137  *  NUMA socket ID for FIB table memory allocation
138  * @param conf
139  *  Structure containing the configuration
140  * @return
141  *  Handle to the FIB object on success
142  *  NULL otherwise with rte_errno set to an appropriate values.
143  */
144 struct rte_fib *
145 rte_fib_create(const char *name, int socket_id, struct rte_fib_conf *conf);
146 
147 /**
148  * Find an existing FIB object and return a pointer to it.
149  *
150  * @param name
151  *  Name of the fib object as passed to rte_fib_create()
152  * @return
153  *  Pointer to fib object or NULL if object not found with rte_errno
154  *  set appropriately. Possible rte_errno values include:
155  *   - ENOENT - required entry not available to return.
156  */
157 struct rte_fib *
158 rte_fib_find_existing(const char *name);
159 
160 /**
161  * Free an FIB object.
162  *
163  * @param fib
164  *   FIB object handle created by rte_fib_create().
165  *   If fib is NULL, no operation is performed.
166  */
167 void
168 rte_fib_free(struct rte_fib *fib);
169 
170 /**
171  * Add a route to the FIB.
172  *
173  * @param fib
174  *   FIB object handle
175  * @param ip
176  *   IPv4 prefix address to be added to the FIB
177  * @param depth
178  *   Prefix length
179  * @param next_hop
180  *   Next hop to be added to the FIB
181  * @return
182  *   0 on success, negative value otherwise
183  */
184 int
185 rte_fib_add(struct rte_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop);
186 
187 /**
188  * Delete a rule from the FIB.
189  *
190  * @param fib
191  *   FIB object handle
192  * @param ip
193  *   IPv4 prefix address to be deleted from the FIB
194  * @param depth
195  *   Prefix length
196  * @return
197  *   0 on success, negative value otherwise
198  */
199 int
200 rte_fib_delete(struct rte_fib *fib, uint32_t ip, uint8_t depth);
201 
202 /**
203  * Lookup multiple IP addresses in the FIB.
204  *
205  * @param fib
206  *   FIB object handle
207  * @param ips
208  *   Array of IPs to be looked up in the FIB
209  * @param next_hops
210  *   Next hop of the most specific rule found for IP.
211  *   This is an array of eight byte values.
212  *   If the lookup for the given IP failed, then corresponding element would
213  *   contain default nexthop value configured for a FIB.
214  * @param n
215  *   Number of elements in ips (and next_hops) array to lookup.
216  *  @return
217  *   -EINVAL for incorrect arguments, otherwise 0
218  */
219 int
220 rte_fib_lookup_bulk(struct rte_fib *fib, uint32_t *ips,
221 		uint64_t *next_hops, int n);
222 /**
223  * Get pointer to the dataplane specific struct
224  *
225  * @param fib
226  *   FIB object handle
227  * @return
228  *   Pointer on the dataplane struct on success
229  *   NULL otherwise
230  */
231 void *
232 rte_fib_get_dp(struct rte_fib *fib);
233 
234 /**
235  * Get pointer to the RIB
236  *
237  * @param fib
238  *   FIB object handle
239  * @return
240  *   Pointer on the RIB on success
241  *   NULL otherwise
242  */
243 struct rte_rib *
244 rte_fib_get_rib(struct rte_fib *fib);
245 
246 /**
247  * Set lookup function based on type
248  *
249  * @param fib
250  *   FIB object handle
251  * @param type
252  *   type of lookup function
253  *
254  * @return
255  *   0 on success
256  *   -EINVAL on failure
257  */
258 int
259 rte_fib_select_lookup(struct rte_fib *fib, enum rte_fib_lookup_type type);
260 
261 /**
262  * Associate RCU QSBR variable with a FIB object.
263  *
264  * @param fib
265  *   FIB object handle
266  * @param cfg
267  *   RCU QSBR configuration
268  * @return
269  *   0 on success
270  *   Negative otherwise
271  *   Possible error codes are:
272  *   - -EINVAL - invalid parameters
273  *   - -EEXIST - already added QSBR
274  *   - -ENOMEM - memory allocation failure
275  *   - -ENOTSUP - not supported by configured dataplane algorithm
276  */
277 __rte_experimental
278 int
279 rte_fib_rcu_qsbr_add(struct rte_fib *fib, struct rte_fib_rcu_config *cfg);
280 
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif /* _RTE_FIB_H_ */
286