Linux Kernel Analysis-network [four complements]: routing table supplemental __linux

Source: Internet
Author: User
Tags goto


http://blog.csdn.net/qy532846454/article/details/6726171


Kernel version: 2.6.34
The previous routing table http://blog.csdn.net/qy532846454/article/details/6423496 describes the structure of the routing table and the creation of the routing table. The following are the details of the use of some routing tables for additional explanation.
Routes can be divided into two parts: routing caching (rt_hash_table) and routing tables ()
Routing caching, by definition, is an accelerated route lookup, and the insertion of the routing cache is controlled by the kernel rather than by human inserts, in contrast to the routing table being artificially inserted rather than being inserted by the kernel. In the kernel, routing caching is organized into rt_hash_table structures.

The following is a section of the IP layer protocol [NET/IPV4/ROUTE.C], where the protocol for the incoming IP layer looks in the routing cache first, if it already exists, Skb_dst_set (SKB, &RTH->U.DST) and returns Otherwise, query in the routing table.
[CPP] View Plain Copy Hash = rt_hash (Daddr, saddr, iif, rt_genid (net));       Rcu_read_lock ();   for  (rth = rcu_dereference (rt_hash_table[hash].chain);  rth;        rth = rcu_dereference (rth->u.dst.rt_next))  {        if  ((rth->fl.fl4_dst ^ daddr)  |              (RTH->FL.FL4_SRC ^ SADDR)  |             (RTH->FL.IIF ^ IIF)  |            rth->fl.oif |              (Rth->fl.fl4_tos ^ tos))  == 0 &&            rth->fl.mark == skb->mark & &           net_eq (Dev_net (Rth->u.dst.dev),  net)  &&            !rt_is_expired (rth))  {            dst_use (&rth->u.dst, jiffies);            rt_cache_stat_inc (in_hit);            rcu_read_unlock ();           skb_dst_set (skb,  &RTH->U.DST);           return 0;       }       rt_cache_stat_inc (in_hlist_search);  }    Rcu_read_unlock ();  

        in Ip_route_input () query for the completion of the multicast address by the cache, if it is a multicast address, then the following judgment will succeed: Ipv4_is_ Multicast (DADDR). The
then executes IP_ROUTE_INPUT_MC (), whose primary role is to generate the route cache entry Rth and insert the cache. The generation and initialization of Rth only gives the input function, other omitted, can see that the group broadcast text will continue to pass through Ip_local_deliver (). [CPP] view plain copy rth->u.dst.input= ip_local_deliver;   Hash = rt_ Hash (Daddr, saddr, dev->ifindex, rt_genid (dev_net (Dev)));   Return rt_intern_ Hash (hash, rth, null, skb, dev->ifindex);  

      routing tables can be divided into two: Rt_table_local and Rt_table_main
         rt_table_local Storage Destination address is the local routing table entry, which is the IP address configured for each network adapter;
        RT _table_main A routing table entry that is stored to another host;
      Obviously, the Rt_table_main routing table is only useful when the host is a router, and the general host is empty. Because the host does not have the ability to forward packets. Rt_table_local is sufficient for the host, the IP address configured for each NIC will be added to the rt_table_local, such as 1.2.3.4 address for eth1, rt_table_local Route entry will exist in 1.2.3.4. Only the local network card address will be added, such as Lo, eth1. When the IP module is initialized ip_init ()-> ip_rt_init ()-> Ip_fib_init () registers the notifier mechanism, which executes fib_netdev_notifier and FIB_ when configured for the network adapter address Inetaddr_notifier, so that the changes are reflected in the rt_table_local. [CPP] view plain copy register_netdevice_notifier (&fib_netdev_notifier);   Register_ Inetaddr_notifier (&fib_inetaddr_notifier);  

When a cache entry is not found in the routing cache, a routing table query is performed, or the code snippet in the IP layer protocol is taken as an example [net/ipv4/route.c],fib_lookup () is searched in main and local two tables. [CPP] view plain Copy if (err = fib_lookup (NET, &FL, &res))!= 0) {if (!       In_dev_forward (In_dev)) goto E_hostunreach;   Goto No_route; }

If the host is configured to support forwarding, either found in the routing table or not, will generate a cache of this query, including source IP, destination IP, received network card, insert the route cache: [CPP] view plain copy hash = Rt_hash (daddr   , Saddr, Fl.iif, Rt_genid (net)); Err = Rt_intern_hash (hash, Rth, NULL, SKB, FL.IIF);

      The difference is that if the query fails in the routing table, that is, if the packet is not sent to the native and cannot be forwarded by the native computer, the cache entry u.dst.input=ip_error that is inserted into the route cache is set. The u.dst.input is the function that is passed up after the IP layer is processed, and ip_error () discards the packet and is sent the corresponding ICMP error message. Routing items that are not in the routing table are also inserted into the routing cache, which can be considered a routing learning feature that can be found directly in the routing cache the next time. [CPP] view plain copy rth->u.dst.input= ip_error;   rth->u.dst.error= - err;   rth->rt_flags    &= ~rtcf_local;  

      But if the host does not support forwarding, that is, no routing cache entry is added until it is found, and no route cache entry is generated. This is because it is not found in the local table, indicating that the packet is not sent to the native, at which point the cached route item has no meaning for the host's packet transfer. It only needs to know what packets are sent to it, and the rest of it.        routing queries are integrated by Ip_route_input (), followed by routing caching and routing table queries, and updating the routing cache. The routing cache may be updated each time the packet arrives, but the routing table is different and can only be updated through the RTM mechanism, the local table is updated when the NIC is configured, and the main table is manually inserted (Inet_rtm_newroute).
       ip_route_input ()
        -   Routing cache query
        -  routing Table query: Ip_route_input_slow ()-> fib_ Lookup ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.