FreeBSD/Linux Kernel Cross Reference
sys/net/radix_mpath.c
1 /* $KAME: radix_mpath.c,v 1.17 2004/11/08 10:29:39 itojun Exp $ */
2
3 /*
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (C) 2001 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * THE AUTHORS DO NOT GUARANTEE THAT THIS SOFTWARE DOES NOT INFRINGE
33 * ANY OTHERS' INTELLECTUAL PROPERTIES. IN NO EVENT SHALL THE AUTHORS
34 * BE LIABLE FOR ANY INFRINGEMENT OF ANY OTHERS' INTELLECTUAL
35 * PROPERTIES.
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: releng/12.0/sys/net/radix_mpath.c 335250 2018-06-16 08:26:23Z ae $");
40
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/socket.h>
50 #include <sys/domain.h>
51 #include <sys/syslog.h>
52 #include <net/radix.h>
53 #include <net/radix_mpath.h>
54 #include <sys/rmlock.h>
55 #include <net/route.h>
56 #include <net/route_var.h>
57 #include <net/if.h>
58 #include <net/if_var.h>
59
60 /*
61 * give some jitter to hash, to avoid synchronization between routers
62 */
63 static uint32_t hashjitter;
64
65 int
66 rt_mpath_capable(struct rib_head *rnh)
67 {
68
69 return rnh->rnh_multipath;
70 }
71
72 int
73 rn_mpath_capable(struct radix_head *rh)
74 {
75
76 return (rt_mpath_capable((struct rib_head *)rh));
77 }
78
79 struct radix_node *
80 rn_mpath_next(struct radix_node *rn)
81 {
82 struct radix_node *next;
83
84 if (!rn->rn_dupedkey)
85 return NULL;
86 next = rn->rn_dupedkey;
87 if (rn->rn_mask == next->rn_mask)
88 return next;
89 else
90 return NULL;
91 }
92
93 uint32_t
94 rn_mpath_count(struct radix_node *rn)
95 {
96 uint32_t i = 0;
97 struct rtentry *rt;
98
99 while (rn != NULL) {
100 rt = (struct rtentry *)rn;
101 i += rt->rt_weight;
102 rn = rn_mpath_next(rn);
103 }
104 return (i);
105 }
106
107 struct rtentry *
108 rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
109 {
110 struct radix_node *rn;
111
112 if (!gate || !rt->rt_gateway)
113 return NULL;
114
115 /* beyond here, we use rn as the master copy */
116 rn = (struct radix_node *)rt;
117 do {
118 rt = (struct rtentry *)rn;
119 /*
120 * we are removing an address alias that has
121 * the same prefix as another address
122 * we need to compare the interface address because
123 * rt_gateway is a special sockadd_dl structure
124 */
125 if (rt->rt_gateway->sa_family == AF_LINK) {
126 if (!memcmp(rt->rt_ifa->ifa_addr, gate, gate->sa_len))
127 break;
128 }
129
130 /*
131 * Check for other options:
132 * 1) Routes with 'real' IPv4/IPv6 gateway
133 * 2) Loopback host routes (another AF_LINK/sockadd_dl check)
134 * */
135 if (rt->rt_gateway->sa_len == gate->sa_len &&
136 !memcmp(rt->rt_gateway, gate, gate->sa_len))
137 break;
138 } while ((rn = rn_mpath_next(rn)) != NULL);
139
140 return (struct rtentry *)rn;
141 }
142
143 /*
144 * go through the chain and unlink "rt" from the list
145 * the caller will free "rt"
146 */
147 int
148 rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
149 {
150 struct radix_node *t, *tt;
151
152 if (!headrt || !rt)
153 return (0);
154 t = (struct radix_node *)headrt;
155 tt = rn_mpath_next(t);
156 while (tt) {
157 if (tt == (struct radix_node *)rt) {
158 t->rn_dupedkey = tt->rn_dupedkey;
159 tt->rn_dupedkey = NULL;
160 tt->rn_flags &= ~RNF_ACTIVE;
161 tt[1].rn_flags &= ~RNF_ACTIVE;
162 return (1);
163 }
164 t = tt;
165 tt = rn_mpath_next((struct radix_node *)t);
166 }
167 return (0);
168 }
169
170 /*
171 * check if we have the same key/mask/gateway on the table already.
172 * Assume @rt rt_key host bits are cleared according to @netmask
173 */
174 int
175 rt_mpath_conflict(struct rib_head *rnh, struct rtentry *rt,
176 struct sockaddr *netmask)
177 {
178 struct radix_node *rn, *rn1;
179 struct rtentry *rt1;
180
181 rn = (struct radix_node *)rt;
182 rn1 = rnh->rnh_lookup(rt_key(rt), netmask, &rnh->head);
183 if (!rn1 || rn1->rn_flags & RNF_ROOT)
184 return (0);
185
186 /* key/mask are the same. compare gateway for all multipaths */
187 do {
188 rt1 = (struct rtentry *)rn1;
189
190 /* sanity: no use in comparing the same thing */
191 if (rn1 == rn)
192 continue;
193
194 if (rt1->rt_gateway->sa_family == AF_LINK) {
195 if (rt1->rt_ifa->ifa_addr->sa_len != rt->rt_ifa->ifa_addr->sa_len ||
196 bcmp(rt1->rt_ifa->ifa_addr, rt->rt_ifa->ifa_addr,
197 rt1->rt_ifa->ifa_addr->sa_len))
198 continue;
199 } else {
200 if (rt1->rt_gateway->sa_len != rt->rt_gateway->sa_len ||
201 bcmp(rt1->rt_gateway, rt->rt_gateway,
202 rt1->rt_gateway->sa_len))
203 continue;
204 }
205
206 /* all key/mask/gateway are the same. conflicting entry. */
207 return (EEXIST);
208 } while ((rn1 = rn_mpath_next(rn1)) != NULL);
209
210 return (0);
211 }
212
213 static struct rtentry *
214 rt_mpath_selectrte(struct rtentry *rte, uint32_t hash)
215 {
216 struct radix_node *rn0, *rn;
217 uint32_t total_weight;
218 struct rtentry *rt;
219 int64_t weight;
220
221 /* beyond here, we use rn as the master copy */
222 rn0 = rn = (struct radix_node *)rte;
223 rt = rte;
224
225 /* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
226 total_weight = rn_mpath_count(rn0);
227 hash += hashjitter;
228 hash %= total_weight;
229 for (weight = abs((int32_t)hash);
230 rt != NULL && weight >= rt->rt_weight;
231 weight -= (rt == NULL) ? 0 : rt->rt_weight) {
232
233 /* stay within the multipath routes */
234 if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
235 break;
236 rn = rn->rn_dupedkey;
237 rt = (struct rtentry *)rn;
238 }
239
240 return (rt);
241 }
242
243 struct rtentry *
244 rt_mpath_select(struct rtentry *rte, uint32_t hash)
245 {
246 if (rn_mpath_next((struct radix_node *)rte) == NULL)
247 return (rte);
248
249 return (rt_mpath_selectrte(rte, hash));
250 }
251
252 void
253 rtalloc_mpath_fib(struct route *ro, uint32_t hash, u_int fibnum)
254 {
255 struct rtentry *rt;
256
257 /*
258 * XXX we don't attempt to lookup cached route again; what should
259 * be done for sendto(3) case?
260 */
261 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)
262 && RT_LINK_IS_UP(ro->ro_rt->rt_ifp))
263 return;
264 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, 0, fibnum);
265
266 /* if the route does not exist or it is not multipath, don't care */
267 if (ro->ro_rt == NULL)
268 return;
269 if (rn_mpath_next((struct radix_node *)ro->ro_rt) == NULL) {
270 RT_UNLOCK(ro->ro_rt);
271 return;
272 }
273
274 rt = rt_mpath_selectrte(ro->ro_rt, hash);
275 /* XXX try filling rt_gwroute and avoid unreachable gw */
276
277 /* gw selection has failed - there must be only zero weight routes */
278 if (!rt) {
279 RT_UNLOCK(ro->ro_rt);
280 ro->ro_rt = NULL;
281 return;
282 }
283 if (ro->ro_rt != rt) {
284 RTFREE_LOCKED(ro->ro_rt);
285 ro->ro_rt = rt;
286 RT_LOCK(ro->ro_rt);
287 RT_ADDREF(ro->ro_rt);
288
289 }
290 RT_UNLOCK(ro->ro_rt);
291 }
292
293 extern int in6_inithead(void **head, int off);
294 extern int in_inithead(void **head, int off);
295
296 #ifdef INET
297 int
298 rn4_mpath_inithead(void **head, int off)
299 {
300 struct rib_head *rnh;
301
302 hashjitter = arc4random();
303 if (in_inithead(head, off) == 1) {
304 rnh = (struct rib_head *)*head;
305 rnh->rnh_multipath = 1;
306 return 1;
307 } else
308 return 0;
309 }
310 #endif
311
312 #ifdef INET6
313 int
314 rn6_mpath_inithead(void **head, int off)
315 {
316 struct rib_head *rnh;
317
318 hashjitter = arc4random();
319 if (in6_inithead(head, off) == 1) {
320 rnh = (struct rib_head *)*head;
321 rnh->rnh_multipath = 1;
322 return 1;
323 } else
324 return 0;
325 }
326
327 #endif
Cache object: 2caed9b0b3fbfad2e7f5bbf86596002d
|