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