Linux Routing (2)

Source: Internet
Author: User

In the previous article, we talked about how to match the strategy to get a routing table, in fact, in Rib_rule, is the index of the routing table used by this policy, here by the way to explain the FIB (Forward infomation Base), that is, the forwarding information database, which is equivalent to the routing table.
Given the index of the routing table used, it is possible to get a pointer to the routing table from the Current->nsproxy->net_ns.ipv4.fib_table_hash table, which is represented by the fib_table (see the diagram on the previous page). In the fib_table structure, it specifies the method to find the table, here is the fn_hash_lookup, and after that struct, a 33-element array is formed based on the length of the subnet mask, representing the [0-32] of the subnet mask, respectively, A route entry for the same subnet mask in a routing table is hashed to the hash table represented by the corresponding subnet mask, because often the subnet mask used in the routing table is often less, commonly used such as, 8, 16, 24, 32, so a linked list with the Fn_zone_list header will connect all the items used. When routing lookups, from the longest start of the subnet, sequentially lookup, the last subnet is 0 of the items, is the default route, because the subnet lookup, the longer the subnet mask, the more accurate the subnet is represented.

A route entry with the same subnet mask length, represented by Fn_zone, and hashed. Think about where it is now, determine the routing table, and then start matching the subnet mask with the longest subnet to determine which subnet the target host matches, so that it can be matched to a subnet based on the IP of the target host and the length of the mask at this time. Look at the code:

Static intfn_hash_lookup (struct fib_table *tb, const struct FLOWI *flp, struct fib_result *res) {int err;struct fn_zone *FZ  ; struct Fn_hash *t = (struct fn_hash*) tb->tb_data;read_lock (&fib_hash_lock); for (FZ = t->fn_zone_list; FZ; FZ = Fz->fz_next) {struct Hlist_head *head;struct hlist_node *node;struct fib_node *f;__be32 k = Fz_key (FLP->FL4_DST, FZ ) head = &fz->fz_hash[fn_hash (k, FZ)];hlist_for_each_entry (F, node, head, Fn_hash) {if (F->fn_key! = k) Continue;err = Fib_semantic_match (&f->fn_alias, FLP, Res, F->fn_key, Fz->fz_mask, Fz->fz_order); Err <= 0) goto out;}} Err = 1;out:read_unlock (&fib_hash_lock); return err;}

Fib_node structure represents a subnet, where the same subnet mask subnet to hash the algorithm is relatively simple, here do not say, when the target host matching subnet, that is Fib_node->fn_key = = DST & mask, as if the goal is to achieve, has found a representative of the target subnet of the route entry, take out the next and the network card exit, you can be contracted, the original is this, but the path of Linux to do more fine, look at the code:

int Fib_semantic_match (struct list_head *head, const struct FLOWI *flp, struct fib_result *res, __be32 zone, __be32 Mask,int prefixlen) {struct Fib_alias *fa;int Nh_sel = 0;list_for_each_entry_rcu (FA, Head, fa_list) {int err;if (FA-&GT;FA _tos && Fa->fa_tos! = Flp->fl4_tos) continue;if (Fa->fa_scope < Flp->fl4_scope) continue;fa-> Fa_state |= Fa_s_accessed;err = fib_props[fa->fa_type].error;if (err = = 0) {struct Fib_info *fi = fa->fa_info;if (fi ->fib_flags & Rtnh_f_dead) Continue;switch (fa->fa_type) {case Rtn_unicast:case rtn_local:case RTN_BROADCAST : Case rtn_anycast:case Rtn_multicast:for_nexthops (FI) {if (nh->nh_flags&rtnh_f_dead) continue;if (!flp-> OIF | | Flp->oif = = nh->nh_oif) break; #ifdef Config_ip_route_multipathif (Nhsel < FI-&GT;FIB_NHS) {Nh_sel = Nhsel;goto out_fill_res;} #elseif (Nhsel < 1) {goto out_fill_res;} #endifendfor_nexthops (FI); CONTINUE;DEFAULT:PRINTK (kern_warning "fib_semantic_match bad type% #x \ n", FA->fa_type); return-einval;}} return err;} return 1;out_fill_res:res->prefixlen = Prefixlen;res->nh_sel = Nh_sel;res->type = fa->fa_type;res-> Scope = Fa->fa_scope;res->fi = Fa->fa_info;atomic_inc (&res->fi->fib_clntref); return 0;}

When the subnet is determined, the route entry is represented by Fib_alias because of the same subnet, but the TOS, the scope of different routing items. Here Linux will also be more granular to judge, the TOS (Type of Service), represents a priority, by the setsockopt ip_tos option can be set up a socket TOS, and in the case of a route lookup, can be calculated according to the set TOS Scope, the specific code is Ip_route_output_slow,

U32 tos= Rt_fl_tos (OLDFLP); struct Flowi FL = {. Nl_u = {. Ip4_u = {. daddr = Oldflp->fl4_dst,.saddr =      OLDFLP->FL 4_src,.tos = tos & iptos_rt_mask,.scope = (TOS & rto_onlink)?  Rt_scope_link:  rt_scope_universe),      }},    . Mark = Oldflp->mark,    . IIf = net->loopback_dev-> IfIndex,    . OIF = oldflp->oif};

The scope here is provided by the TOS, which indicates what the socket wants to route within, if the socket only wants to route within the LINK range, but the scope of the route item is UNIVERSE, indicating that the subnet is too far away, that is Fa->fa_scope &lt ; Flp->fl4_scope, such a route is inappropriate, in the same way, if the route entry set the TOS, and the socket TOS does not match, then such a route entry is also inappropriate, thus can be based on these two to further determine the appropriate route.
When the appropriate route entry is determined, if the kernel is configured with Config_ip_route_multipath, what is the multipath of the route, it is explained here that a route entry may have multiple next-hop addresses, that is, multiple paths can reach the subnet represented by the route entry, called Multipath, It is clear that the two paths are available, which can be used for load balancing. After the route entry has been determined, the routing lookup is almost over.
Finally, according to a certain algorithm to decide which one to use the next hop address, there are exits.
These two articles on the Linux under the framework of strategic routing and some important aspects of the implementation, aside from the cache and other details can be learned in the code, know the entire context, combined with the previous article of the structure diagram, then the routing mechanism will be able to understand the smooth.

Linux Routing (2)

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.