]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/radix_mpath.c
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / 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$");
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/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/domain.h>
49 #include <sys/syslog.h>
50 #include <net/radix.h>
51 #include <net/radix_mpath.h>
52 #include <net/route.h>
53 #include <net/route_var.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56
57 /*
58  * give some jitter to hash, to avoid synchronization between routers
59  */
60 static uint32_t hashjitter;
61
62 int
63 rt_mpath_capable(struct rib_head *rnh)
64 {
65
66         return rnh->rnh_multipath;
67 }
68
69 int
70 rn_mpath_capable(struct radix_head *rh)
71 {
72
73         return (rt_mpath_capable((struct rib_head *)rh));
74 }
75
76 struct radix_node *
77 rn_mpath_next(struct radix_node *rn)
78 {
79         struct radix_node *next;
80
81         if (!rn->rn_dupedkey)
82                 return NULL;
83         next = rn->rn_dupedkey;
84         if (rn->rn_mask == next->rn_mask)
85                 return next;
86         else
87                 return NULL;
88 }
89
90 uint32_t
91 rn_mpath_count(struct radix_node *rn)
92 {
93         uint32_t i = 0;
94         struct rtentry *rt;
95         
96         while (rn != NULL) {
97                 rt = (struct rtentry *)rn;
98                 i += rt->rt_weight;
99                 rn = rn_mpath_next(rn);
100         }
101         return (i);
102 }
103
104 struct rtentry *
105 rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
106 {
107         struct radix_node *rn;
108
109         if (!gate || !rt->rt_gateway)
110                 return NULL;
111
112         /* beyond here, we use rn as the master copy */
113         rn = (struct radix_node *)rt;
114         do {
115                 rt = (struct rtentry *)rn;
116                 /*
117                  * we are removing an address alias that has 
118                  * the same prefix as another address
119                  * we need to compare the interface address because
120                  * rt_gateway is a special sockadd_dl structure
121                  */
122                 if (rt->rt_gateway->sa_family == AF_LINK) {
123                         if (!memcmp(rt->rt_ifa->ifa_addr, gate, gate->sa_len))
124                                 break;
125                 }
126
127                 /*
128                  * Check for other options:
129                  * 1) Routes with 'real' IPv4/IPv6 gateway
130                  * 2) Loopback host routes (another AF_LINK/sockadd_dl check)
131                  * */
132                 if (rt->rt_gateway->sa_len == gate->sa_len &&
133                     !memcmp(rt->rt_gateway, gate, gate->sa_len))
134                         break;
135         } while ((rn = rn_mpath_next(rn)) != NULL);
136
137         return (struct rtentry *)rn;
138 }
139
140 /* 
141  * go through the chain and unlink "rt" from the list
142  * the caller will free "rt"
143  */
144 int
145 rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
146 {
147         struct radix_node *t, *tt;
148
149         if (!headrt || !rt)
150             return (0);
151         t = (struct radix_node *)headrt;
152         tt = rn_mpath_next(t);
153         while (tt) {
154             if (tt == (struct radix_node *)rt) {
155                 t->rn_dupedkey = tt->rn_dupedkey;
156                 tt->rn_dupedkey = NULL;
157                 tt->rn_flags &= ~RNF_ACTIVE;
158                 tt[1].rn_flags &= ~RNF_ACTIVE;
159                 return (1);
160             }
161             t = tt;
162             tt = rn_mpath_next((struct radix_node *)t);
163         }
164         return (0);
165 }
166
167 /*
168  * check if we have the same key/mask/gateway on the table already.
169  * Assume @rt rt_key host bits are cleared according to @netmask
170  */
171 int
172 rt_mpath_conflict(struct rib_head *rnh, struct rtentry *rt,
173     struct sockaddr *netmask)
174 {
175         struct radix_node *rn, *rn1;
176         struct rtentry *rt1;
177
178         rn = (struct radix_node *)rt;
179         rn1 = rnh->rnh_lookup(rt_key(rt), netmask, &rnh->head);
180         if (!rn1 || rn1->rn_flags & RNF_ROOT)
181                 return (0);
182
183         /* key/mask are the same. compare gateway for all multipaths */
184         do {
185                 rt1 = (struct rtentry *)rn1;
186
187                 /* sanity: no use in comparing the same thing */
188                 if (rn1 == rn)
189                         continue;
190         
191                 if (rt1->rt_gateway->sa_family == AF_LINK) {
192                         if (rt1->rt_ifa->ifa_addr->sa_len != rt->rt_ifa->ifa_addr->sa_len ||
193                             bcmp(rt1->rt_ifa->ifa_addr, rt->rt_ifa->ifa_addr, 
194                             rt1->rt_ifa->ifa_addr->sa_len))
195                                 continue;
196                 } else {
197                         if (rt1->rt_gateway->sa_len != rt->rt_gateway->sa_len ||
198                             bcmp(rt1->rt_gateway, rt->rt_gateway,
199                             rt1->rt_gateway->sa_len))
200                                 continue;
201                 }
202
203                 /* all key/mask/gateway are the same.  conflicting entry. */
204                 return (EEXIST);
205         } while ((rn1 = rn_mpath_next(rn1)) != NULL);
206
207         return (0);
208 }
209
210 static struct rtentry *
211 rt_mpath_selectrte(struct rtentry *rte, uint32_t hash)
212 {
213         struct radix_node *rn0, *rn;
214         uint32_t total_weight;
215         struct rtentry *rt;
216         int64_t weight;
217
218         /* beyond here, we use rn as the master copy */
219         rn0 = rn = (struct radix_node *)rte;
220         rt = rte;
221
222         /* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
223         total_weight = rn_mpath_count(rn0);
224         hash += hashjitter;
225         hash %= total_weight;
226         for (weight = abs((int32_t)hash);
227              rt != NULL && weight >= rt->rt_weight; 
228              weight -= (rt == NULL) ? 0 : rt->rt_weight) {
229                 
230                 /* stay within the multipath routes */
231                 if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
232                         break;
233                 rn = rn->rn_dupedkey;
234                 rt = (struct rtentry *)rn;
235         }
236
237         return (rt);
238 }
239
240 struct rtentry *
241 rt_mpath_select(struct rtentry *rte, uint32_t hash)
242 {
243         if (rn_mpath_next((struct radix_node *)rte) == NULL)
244                 return (rte);
245
246         return (rt_mpath_selectrte(rte, hash));
247 }
248
249 void
250 rtalloc_mpath_fib(struct route *ro, uint32_t hash, u_int fibnum)
251 {
252         struct rtentry *rt;
253
254         /*
255          * XXX we don't attempt to lookup cached route again; what should
256          * be done for sendto(3) case?
257          */
258         if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)
259             && RT_LINK_IS_UP(ro->ro_rt->rt_ifp))
260                 return;                          
261         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, 0, fibnum);
262
263         /* if the route does not exist or it is not multipath, don't care */
264         if (ro->ro_rt == NULL)
265                 return;
266         if (rn_mpath_next((struct radix_node *)ro->ro_rt) == NULL) {
267                 RT_UNLOCK(ro->ro_rt);
268                 return;
269         }
270
271         rt = rt_mpath_selectrte(ro->ro_rt, hash);
272         /* XXX try filling rt_gwroute and avoid unreachable gw  */
273
274         /* gw selection has failed - there must be only zero weight routes */
275         if (!rt) {
276                 RT_UNLOCK(ro->ro_rt);
277                 ro->ro_rt = NULL;
278                 return;
279         }
280         if (ro->ro_rt != rt) {
281                 RTFREE_LOCKED(ro->ro_rt);
282                 ro->ro_rt = rt;
283                 RT_LOCK(ro->ro_rt);
284                 RT_ADDREF(ro->ro_rt);
285
286         } 
287         RT_UNLOCK(ro->ro_rt);
288 }
289
290 extern int      in6_inithead(void **head, int off);
291 extern int      in_inithead(void **head, int off);
292
293 #ifdef INET
294 int
295 rn4_mpath_inithead(void **head, int off)
296 {
297         struct rib_head *rnh;
298
299         hashjitter = arc4random();
300         if (in_inithead(head, off) == 1) {
301                 rnh = (struct rib_head *)*head;
302                 rnh->rnh_multipath = 1;
303                 return 1;
304         } else
305                 return 0;
306 }
307 #endif
308
309 #ifdef INET6
310 int
311 rn6_mpath_inithead(void **head, int off)
312 {
313         struct rib_head *rnh;
314
315         hashjitter = arc4random();
316         if (in6_inithead(head, off) == 1) {
317                 rnh = (struct rib_head *)*head;
318                 rnh->rnh_multipath = 1;
319                 return 1;
320         } else
321                 return 0;
322 }
323
324 #endif