The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/radix_mpath.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 3415f8d0ad04d27a864bf47de6d6d4bf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.