xref: /freebsd/sys/net/route/fib_algo.h (revision 7791ecf04b48a0c365b003447f479ec890115dfc)
1 /*-
2  * Copyright (c) 2020
3  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 
33 struct fib_data;
34 struct fib_dp;
35 enum flm_op_result {
36 	FLM_SUCCESS,	/* No errors, operation successful */
37 	FLM_REBUILD,	/* Operation cannot be completed, schedule algorithm rebuild */
38 	FLM_ERROR,	/* Operation failed, this algo cannot be used */
39 };
40 
41 struct rib_rtable_info {
42 	uint32_t num_prefixes;
43 	uint32_t num_nhops;
44 	uint32_t num_nhgrp;
45 };
46 
47 struct flm_lookup_key {
48 	union {
49 		const struct in6_addr *addr6;
50 		struct in_addr addr4;
51 	};
52 };
53 
54 typedef struct nhop_object *flm_lookup_t(void *algo_data,
55     const struct flm_lookup_key key, uint32_t scopeid);
56 typedef enum flm_op_result flm_init_t (uint32_t fibnum, struct fib_data *fd,
57     void *_old_data, void **new_data);
58 typedef void flm_destroy_t(void *data);
59 typedef enum flm_op_result flm_dump_t(struct rtentry *rt, void *data);
60 typedef enum flm_op_result flm_dump_end_t(void *data, struct fib_dp *dp);
61 typedef enum flm_op_result flm_change_t(struct rib_head *rnh,
62     struct rib_cmd_info *rc, void *data);
63 typedef uint8_t flm_get_pref_t(const struct rib_rtable_info *rinfo);
64 
65 struct fib_lookup_module {
66 	char		*flm_name;		/* algo name */
67 	int		flm_family;		/* address family this module supports */
68 	int		flm_refcount;		/* # of references */
69 	uint32_t	flm_flags;		/* flags */
70 	uint8_t		flm_index;		/* internal algo index */
71 	flm_init_t	*flm_init_cb;		/* instance init */
72 	flm_destroy_t	*flm_destroy_cb;		/* destroy instance */
73 	flm_change_t	*flm_change_rib_item_cb;/* routing table change hook */
74 	flm_dump_t	*flm_dump_rib_item_cb;	/* routing table dump cb */
75 	flm_dump_end_t	*flm_dump_end_cb;	/* end of dump */
76 	flm_lookup_t	*flm_lookup;		/* lookup function */
77 	flm_get_pref_t	*flm_get_pref;		/* get algo preference */
78 	TAILQ_ENTRY(fib_lookup_module)	entries;
79 };
80 
81 /* Datapath lookup data */
82 struct fib_dp {
83 	flm_lookup_t	*f;
84 	void		*arg;
85 };
86 
87 VNET_DECLARE(struct fib_dp *, inet_dp);
88 #define	V_inet_dp	VNET(inet_dp)
89 VNET_DECLARE(struct fib_dp *, inet6_dp);
90 #define	V_inet6_dp	VNET(inet6_dp)
91 
92 #define	FIB_PRINTF(_l, _fd, _fmt, ...)	fib_printf(_l, _fd, __func__, _fmt, ##__VA_ARGS__)
93 
94 void fib_printf(int level, struct fib_data *fd, const char *func, char *fmt, ...);
95 int fib_module_init(struct fib_lookup_module *flm, uint32_t fibnum,
96     int family);
97 int fib_module_clone(const struct fib_lookup_module *flm_orig,
98     struct fib_lookup_module *flm, bool waitok);
99 int fib_module_dumptree(struct fib_lookup_module *flm,
100     enum rib_subscription_type subscription_type);
101 int fib_module_register(struct fib_lookup_module *flm);
102 int fib_module_unregister(struct fib_lookup_module *flm);
103 
104 uint32_t fib_get_nhop_idx(struct fib_data *fd, struct nhop_object *nh);
105 struct nhop_object **fib_get_nhop_array(struct fib_data *fd);
106 void fib_get_rtable_info(struct rib_head *rh, struct rib_rtable_info *rinfo);
107 struct rib_head *fib_get_rh(struct fib_data *fd);
108 
109 
110