]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/radix_mpath.c
Fix si_addr value for breakpoints in a delay slot.
[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 #include "opt_mpath.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/socket.h>
51 #include <sys/domain.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <net/radix.h>
55 #include <net/radix_mpath.h>
56 #include <sys/rmlock.h>
57 #include <net/route.h>
58 #include <net/route/nhop.h>
59 #include <net/route/shared.h>
60 #include <net/route/route_var.h>
61 #include <net/route/nhop.h>
62 #include <net/if.h>
63 #include <net/if_var.h>
64
65 /*
66  * give some jitter to hash, to avoid synchronization between routers
67  */
68 static uint32_t hashjitter;
69
70 int
71 rt_mpath_capable(struct rib_head *rnh)
72 {
73
74         return rnh->rnh_multipath;
75 }
76
77 int
78 rn_mpath_capable(struct radix_head *rh)
79 {
80
81         return (rt_mpath_capable((struct rib_head *)rh));
82 }
83
84 struct radix_node *
85 rn_mpath_next(struct radix_node *rn)
86 {
87         struct radix_node *next;
88
89         if (!rn->rn_dupedkey)
90                 return NULL;
91         next = rn->rn_dupedkey;
92         if (rn->rn_mask == next->rn_mask)
93                 return next;
94         else
95                 return NULL;
96 }
97
98 uint32_t
99 rn_mpath_count(struct radix_node *rn)
100 {
101         uint32_t i = 0;
102         struct rtentry *rt;
103         
104         while (rn != NULL) {
105                 rt = (struct rtentry *)rn;
106                 i += rt->rt_weight;
107                 rn = rn_mpath_next(rn);
108         }
109         return (i);
110 }
111
112 struct rtentry *
113 rt_mpath_matchgate(struct rtentry *rt, struct sockaddr *gate)
114 {
115         struct radix_node *rn;
116         struct nhop_object *nh;
117
118         if (gate == NULL)
119                 return (NULL);
120
121         /* beyond here, we use rn as the master copy */
122         rn = (struct radix_node *)rt;
123         do {
124                 rt = (struct rtentry *)rn;
125                 nh = rt->rt_nhop;
126                 /*
127                  * we are removing an address alias that has
128                  * the same prefix as another address
129                  * we need to compare the interface address because
130                  * gateway is a special sockaddr_dl structure
131                  */
132                 if (nh->gw_sa.sa_family == AF_LINK) {
133                         if (!memcmp(nh->nh_ifa->ifa_addr, gate, gate->sa_len))
134                                 break;
135                 }
136
137                 /*
138                  * Check for other options:
139                  * 1) Routes with 'real' IPv4/IPv6 gateway
140                  * 2) Loopback host routes (another AF_LINK/sockadd_dl check)
141                  * */
142                 if (nh->gw_sa.sa_len == gate->sa_len &&
143                     !memcmp(&nh->gw_sa, gate, gate->sa_len))
144                         break;
145         } while ((rn = rn_mpath_next(rn)) != NULL);
146
147         return (struct rtentry *)rn;
148 }
149
150 /* 
151  * go through the chain and unlink "rt" from the list
152  * the caller will free "rt"
153  */
154 int
155 rt_mpath_deldup(struct rtentry *headrt, struct rtentry *rt)
156 {
157         struct radix_node *t, *tt;
158
159         if (!headrt || !rt)
160             return (0);
161         t = (struct radix_node *)headrt;
162         tt = rn_mpath_next(t);
163         while (tt) {
164             if (tt == (struct radix_node *)rt) {
165                 t->rn_dupedkey = tt->rn_dupedkey;
166                 tt->rn_dupedkey = NULL;
167                 tt->rn_flags &= ~RNF_ACTIVE;
168                 tt[1].rn_flags &= ~RNF_ACTIVE;
169                 return (1);
170             }
171             t = tt;
172             tt = rn_mpath_next((struct radix_node *)t);
173         }
174         return (0);
175 }
176
177 /*
178  * check if we have the same key/mask/gateway on the table already.
179  * Assume @rt rt_key host bits are cleared according to @netmask
180  */
181 int
182 rt_mpath_conflict(struct rib_head *rnh, struct rtentry *rt,
183     struct sockaddr *netmask)
184 {
185         struct radix_node *rn, *rn1;
186         struct nhop_object *nh, *nh1;
187         struct rtentry *rt1;
188
189         rn = (struct radix_node *)rt;
190         rn1 = rnh->rnh_lookup(rt_key(rt), netmask, &rnh->head);
191         if (!rn1 || rn1->rn_flags & RNF_ROOT)
192                 return (0);
193
194         /* key/mask are the same. compare gateway for all multipaths */
195         do {
196                 rt1 = (struct rtentry *)rn1;
197
198                 /* sanity: no use in comparing the same thing */
199                 if (rn1 == rn)
200                         continue;
201         
202                 nh = rt->rt_nhop;
203                 nh1 = rt1->rt_nhop;
204
205                 if (nh1->gw_sa.sa_family == AF_LINK) {
206                         if (nh1->nh_ifa->ifa_addr->sa_len != nh->nh_ifa->ifa_addr->sa_len ||
207                             bcmp(nh1->nh_ifa->ifa_addr, nh->nh_ifa->ifa_addr, 
208                             nh1->nh_ifa->ifa_addr->sa_len))
209                                 continue;
210                 } else {
211                         if (nh1->gw_sa.sa_len != nh->gw_sa.sa_len ||
212                             bcmp(&nh1->gw_sa, &nh->gw_sa, nh1->gw_sa.sa_len))
213                                 continue;
214                 }
215
216                 /* all key/mask/gateway are the same.  conflicting entry. */
217                 return (EEXIST);
218         } while ((rn1 = rn_mpath_next(rn1)) != NULL);
219
220         return (0);
221 }
222
223 struct rtentry *
224 rt_mpath_selectrte(struct rtentry *rte, uint32_t hash)
225 {
226         struct radix_node *rn0, *rn;
227         uint32_t total_weight;
228         struct rtentry *rt;
229         int64_t weight;
230
231         /* beyond here, we use rn as the master copy */
232         rn0 = rn = (struct radix_node *)rte;
233         rt = rte;
234
235         /* gw selection by Modulo-N Hash (RFC2991) XXX need improvement? */
236         total_weight = rn_mpath_count(rn0);
237         hash += hashjitter;
238         hash %= total_weight;
239         for (weight = abs((int32_t)hash);
240              rt != NULL && weight >= rt->rt_weight; 
241              weight -= (rt == NULL) ? 0 : rt->rt_weight) {
242                 
243                 /* stay within the multipath routes */
244                 if (rn->rn_dupedkey && rn->rn_mask != rn->rn_dupedkey->rn_mask)
245                         break;
246                 rn = rn->rn_dupedkey;
247                 rt = (struct rtentry *)rn;
248         }
249
250         return (rt);
251 }
252
253 struct rtentry *
254 rt_mpath_select(struct rtentry *rte, uint32_t hash)
255 {
256         if (rn_mpath_next((struct radix_node *)rte) == NULL)
257                 return (rte);
258
259         return (rt_mpath_selectrte(rte, hash));
260 }
261
262 void
263 rt_mpath_init_rnh(struct rib_head *rnh)
264 {
265
266         rnh->rnh_multipath = 1;
267 }
268
269 #ifdef RADIX_MPATH
270 static void
271 mpath_init(void)
272 {
273
274         hashjitter = arc4random();
275 }
276 SYSINIT(mpath_init, SI_SUB_LAST, SI_ORDER_ANY, mpath_init, NULL);
277 #endif
278