xref: /freebsd/sys/net/route/fib_algo.h (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
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 	FLM_BATCH,	/* Operation cannot be completed, algorithm asks to batch changes */
40 };
41 
42 struct rib_rtable_info {
43 	uint32_t num_prefixes;
44 	uint32_t num_nhops;
45 	uint32_t num_nhgrp;
46 };
47 
48 struct flm_lookup_key {
49 	union {
50 		const struct in6_addr *addr6;
51 		struct in_addr addr4;
52 	};
53 };
54 
55 struct fib_change_entry {
56 	union {
57 		struct in_addr	addr4;
58 		struct in6_addr	addr6;
59 	};
60 	uint32_t		scopeid;
61 	uint8_t			plen;
62 	struct nhop_object	*nh_old;
63 	struct nhop_object	*nh_new;
64 };
65 
66 struct fib_change_queue {
67 	uint32_t 		count;
68 	uint32_t		size;
69 	struct fib_change_entry	*entries;
70 };
71 
72 
73 typedef struct nhop_object *flm_lookup_t(void *algo_data,
74     const struct flm_lookup_key key, uint32_t scopeid);
75 typedef enum flm_op_result flm_init_t (uint32_t fibnum, struct fib_data *fd,
76     void *_old_data, void **new_data);
77 typedef void flm_destroy_t(void *data);
78 typedef enum flm_op_result flm_dump_t(struct rtentry *rt, void *data);
79 typedef enum flm_op_result flm_dump_end_t(void *data, struct fib_dp *dp);
80 typedef enum flm_op_result flm_change_t(struct rib_head *rnh,
81     struct rib_cmd_info *rc, void *data);
82 typedef enum flm_op_result flm_change_batch_t(struct rib_head *rnh,
83     struct fib_change_queue *q, void *data);
84 typedef uint8_t flm_get_pref_t(const struct rib_rtable_info *rinfo);
85 
86 struct fib_lookup_module {
87 	char		*flm_name;		/* algo name */
88 	int		flm_family;		/* address family this module supports */
89 	int		flm_refcount;		/* # of references */
90 	uint32_t	flm_flags;		/* flags */
91 	uint8_t		flm_index;		/* internal algo index */
92 	flm_init_t	*flm_init_cb;		/* instance init */
93 	flm_destroy_t	*flm_destroy_cb;	/* destroy instance */
94 	flm_change_t	*flm_change_rib_item_cb;/* routing table change hook */
95 	flm_dump_t	*flm_dump_rib_item_cb;	/* routing table dump cb */
96 	flm_dump_end_t	*flm_dump_end_cb;	/* end of dump */
97 	flm_lookup_t	*flm_lookup;		/* lookup function */
98 	flm_get_pref_t	*flm_get_pref;		/* get algo preference */
99 	flm_change_batch_t	*flm_change_rib_items_cb;/* routing table change hook */
100 	void		*spare[8];		/* Spare callbacks */
101 	TAILQ_ENTRY(fib_lookup_module)	entries;
102 };
103 
104 /* Datapath lookup data */
105 struct fib_dp {
106 	flm_lookup_t	*f;
107 	void		*arg;
108 };
109 
110 VNET_DECLARE(struct fib_dp *, inet_dp);
111 #define	V_inet_dp	VNET(inet_dp)
112 VNET_DECLARE(struct fib_dp *, inet6_dp);
113 #define	V_inet6_dp	VNET(inet6_dp)
114 
115 #define	FIB_PRINTF(_l, _fd, _fmt, ...)	fib_printf(_l, _fd, __func__, _fmt, ##__VA_ARGS__)
116 
117 void fib_printf(int level, struct fib_data *fd, const char *func, char *fmt, ...);
118 int fib_module_init(struct fib_lookup_module *flm, uint32_t fibnum,
119     int family);
120 int fib_module_clone(const struct fib_lookup_module *flm_orig,
121     struct fib_lookup_module *flm, bool waitok);
122 int fib_module_dumptree(struct fib_lookup_module *flm,
123     enum rib_subscription_type subscription_type);
124 int fib_module_register(struct fib_lookup_module *flm);
125 int fib_module_unregister(struct fib_lookup_module *flm);
126 
127 uint32_t fib_get_nhop_idx(struct fib_data *fd, struct nhop_object *nh);
128 struct nhop_object **fib_get_nhop_array(struct fib_data *fd);
129 void fib_get_rtable_info(struct rib_head *rh, struct rib_rtable_info *rinfo);
130 struct rib_head *fib_get_rh(struct fib_data *fd);
131 bool fib_set_datapath_ptr(struct fib_data *fd, struct fib_dp *dp);
132 void fib_set_algo_ptr(struct fib_data *fd, void *algo_data);
133 void fib_epoch_call(epoch_callback_t callback, epoch_context_t ctx);
134 
135