Linux Routing (1)

Source: Internet
Author: User
Tags goto

Routing, which plays a central and onerous role in network technology, is very important because of the frequent calls, plus it indicates the way forward for each packet. As we write, as long as we specify the destination address, the message will be delivered as much as possible, and if, when we assign the destination IP, a powerful mechanism is needed to ensure that it is delivered, and that the mechanism for that purpose is routing.
Let's start by explaining how a packet can get to the destination host step-by-step. Then the first thing to understand, in a LAN, the network card transmission packet is not required IP, only need to know the destination MAC address, that is, if the destination host in the same LAN, only need to find its IP corresponding to the Mac, you can send the packet to it. That is, to send a packet to a destination host, just the packet to its local area network on the line, and LAN and LAN connection between, often need to have an interface, we call it gateway. When to send a packet, here is often an IP packet, will give it a destination IP, the network stack will be based on its own route, determine whether the destination host is in its own LAN, that is, a subnet, if in, then very simple, directly according to the method mentioned above, if not, Then the local routing table should indicate which network card the packet should be sent to, who should be issued the next host, and the next host will continue to decide how to handle the packet. This allows the packet to go to the next subnet, and if the destination host is not on the next subnet, continue routing until it reaches the destination

, or not be able to make decisions and discard them.

In fact, the above mentioned a few important concepts of routing, subnets, the next hop address, from their own which network card bundle.
Linux is further expanded on this basis, resulting in a mechanism called Policy routing, that is, there are multiple routing tables, how to choose these routing tables, is determined by some strategies. That is, when sending a packet, according to some of the characteristics of the packet, such as source address, destination address, TOS and other information, to check whether and the established rules match, if the match is successful, according to the rules specified by the routing table will be used. Is the framework for Policy Routing:


We can see that when routing lookups, we first go through the rules list in order to match. We use code to understand the implementation of the policy routing under Linux, the author's Linux version number is 2.6.27.62.


As an example of IP V4, the specific data structure diagram, the routing strategy is to be connected to the fib4_rules_ops_template->rules_list of the single linked list. The policy for IP V4 is represented by Fib4_rules_ops_template, which specifies the location of the policy list, as well as the matching rules of the policy, as well as the actions after the successful match. We explain how the above structure can be applied to policy routing in the case of UDP packets.
The packet of UDP is completed in Udp_sendmsg, when the route is required, it will pass in the source address, source port, destination address, destination port, TOS and other information, then call, Ip_route_output_flow to find the route, find the route in order to speed up, will first find The contents of the cache, here we skip, looking directly at the process of finding a route not in the cache, which is more meaningful, it will call __ip_route_output_key, Ip_route_output_slow, thus through fib_ Lookup to find the routing table, here is the call to NET/IPV4/FIB_FULES.C in the fib_lookup. It will continue to call Fib_rules_lookup to traverse the policy table, which is fib4_rules_ops_template->rules_list. The code is as follows:

int fib_rules_lookup (struct fib_rules_ops *ops, struct flowi *FL,     int flags, struct Fib_lookup_arg *arg) {struct FIB_R Ule *rule;int Err;rcu_read_lock (); list_for_each_entry_rcu (rule, &ops->rules_list, list) {Jumped:if (!fib_rule _match (rule, OPS, FL, flags)) continue;if (rule->action = = Fr_act_goto) {struct Fib_rule *target;target = Rcu_dereferen CE (Rule->ctarget), if (target = = NULL) {continue;} else {rule = Target;goto jumped;}} else if (rule->action = = Fr_act_nop) Continue;elseerr = ops->action (rule, FL, flags, ARG); if (err! =-eagain) {Fib_ru Le_get (rule); arg->rule = Rule;goto out;}} Err =-esrch;out:rcu_read_unlock (); return err;}

This is the core of the policy in Policy routing, because the policy is added in order, so when the policy is matched, it is followed by the local policy, the custom policy, the master policy, and the default policy to search. The matching method is implemented by Fib4_rule_match, which determines the next action based on the action defined by the policy, and each policy specifies a routing table, which is the route table that we will use.
(not to be continued)

Linux Routing (1)

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.