Route Selection completed by IP address is a routing mechanism. It searches the route table to determine which API to send the packet. It is different from the routing policy.
Is a set of rules used to determine which routes can be compiled into the routing table. The net/3 kernel implements the routing mechanism, and the routing daemon process, typically as shown in Figure
Routed or gated to implement the routing policy.
1. Route table structure
Is the route table of a host.
For the flags column, you must briefly describe it.
G. This route leads to a gateway (router), which is called an indirect route. If this flag is not set, the route destination is directly connected to the local machine,
It is called direct routing.
H. The route is routed to a host. That is, the destination address is a complete host address. If this flag is not set, the route is routed to a network and the destination address.
Is a network address: a network number, or a combination of a network number and a subnet number.
S. The route is static.
C. The route can be cloned to generate a new route. There are two routes in this route table with this flag: one is the route to the local Ethernet 140.252.13.32,
ARP creates a route to another specific host in the Ethernet network by cloning the route. The other is a route to multicast group 224. Cloning the route can create a route to a specific multicast
Group (such as 224.0.0.1) routing.
L this route contains the link layer address. This flag applies to unicast and multicast addresses. This flag is set for all host routes that are cloned by ARP from the Ethernet route.
The r Loop Drive (a common interface designed for a route with this flag) rejects all data packets that use this route.
NET/3 route table uses the Patricia tree structure to represent the host address and network address. The addresses to be searched and the addresses in the tree are regarded as bit sequences. In this way, the same
To find and maintain different types of data.
The purpose of finding a route table is to find a specific address that best matches a given target. We call this a given search key ). The so-called
The most matching address, that is, a host address that can be matched is better than a matching network address, and a matching network address is better
Default address.
Each route table entry has a corresponding network mask. Although there is no storage mask in the host route, it implies a full 1-bit mask. For the search key and
The mask of the route table entry is logical and operational. If the obtained value is the same as the destination address of the route table entry, the route table entry is matched. For a given
Search key. We will find multiple matching routes from the routing table.
The internal structure of the above route table is given.
The two shadow boxes marked with "end" are leaf nodes with special signs in the structure of the Book, which represent the tree endpoint. The one on the left has a full 0 Key, while
There is a full 1 key, and the two boxes marked with end and default on the left are stacked together. These two boxes have special meanings and are related to the duplicate key.
The square corner frame is called an internal node or a node for short, and the corner frame is called a leaf. Each internal node corresponds to a bit of the test search key, and each has its own
One branch. Each leaf corresponds to a host address or a network address. If there is a hexadecimal number under the leaf, then the leaf
Corresponds to a network address. The number of sixteen processes is the netmask of the leaf. If there is no hexadecimal mask under the leaf, the leaf is
Host address. The hidden mask is 0 xffffffff.
Some internal nodes also contain network masks, which are used during backtracking.
Bit comparison is applied to the plug-in address structure. Therefore, the given bit position starts from the start position in the plug-in address structure. Given
The bit position in the sockaddr_in structure.
Represents the bits of each IP address in the routing table.
The following examples describe how the route table is searched.
1. Example of matching host address
Assume that the search key is 140.252.13.35. 32 is, is, and 57 is, is 1. Search for the leaves in 140.252.13.35
Termination. The search key exactly matches the route table key.
2. network address matching example
Assume that the search key is 127.0.0.2. If 32 is 0, 33 is, and 63 is 0, the query ends at the leaf marked with 127.0.0.0. The search key and route table are not completely
Therefore, you need to see whether it is a network address that can be matched. Perform logic and operations on the search key and network mask 0xff000000.
The key of the route table is the same as that of the route table.
3. Example of matching with the default address
Assume that the search key is 10.1.2.3. 32 is 0, 33 is 0, therefore, search for the leaves marked with end and default with duplicate keys to terminate. Repeated in the two leaves
The route table key is 0.0.0.0. The lookup key does not exactly match the key value of the route table. Therefore, you need to check whether it is a matching network address. This matching
Each duplicate key with a network mask must be used for this operation. If the first key does not have a network mask, you can skip this step. The second key has a mask 0x00000000.
The logic of the search key and the mask is the same as that of the operation. The result is that the route table Key 0 matches. In this way, the default route table can be used for matching.
Routing.
3. Examples with backtracing, cloning, and matching with host addresses
Assume that the search key is 224.0.0.5. If 32 is and 35 is, is 1. Therefore, the search ends at the leaf marked with 224.0.0.1. Key Value and search keyword of the route table
Not equal, and the route table does not contain the network mask, so Backtracking is required.
Move back to the top layer to reach 63 corresponding nodes. The node contains the mask 0xff000000 (if there is no mask, continue back up). Therefore, the search key and the mask
Logic and operation to generate a new search key 224.0.0.0. Start a new search at this node. 63 is 0 in the new search key, so it is left branch
Reach the leaf marked with 224.0.0.0. The key and logic of this route table match the search key obtained by the Operation. Therefore, this route table item matches.
The route table is configured with a clone flag. Therefore, a new leaf is created with the address 224.0.0.5. The new route table entry is:
Starting from the node corresponding to bit 35, a new arrangement of the right part of the above route table tree is given. Whenever a new leaf is added to the tree, two nodes are required:
One serves as the leaf and the other serves as the internal node for testing a bit. The newly created table item is returned to the caller who finds 225.0.0.5.
Describes all involved data structures.
Key points in our explanation.
Rf_tables is a pointer array pointing to the radix_node_head structure. Each address family corresponds to an array. Rt_tables [af_inet] points to the Internet
The vertex of the route table tree.
The radix_node_head structure contains three radix_node structures. These three structures are created when the routing tree is initialized, and the center is the vertex of the tree. It
The node box of the route tree's bit32. The first of the three radix_node structures is the leftmost leaf in the routing tree (repeated with the default route sharing), and the third structure.
Is the rightmost leaf. An empty routing table contains only three radix_node structures.
The global variable mask_rnhead also points to a radix_node_head structure. It is the header structure of an independent tree that contains all masks. The preceding routing tree provides
According to the eight masks, one mask repeats four times, and the other two masks repeat once. By placing the mask in a separate tree, you can implement
You only need to maintain one of its backups.
The route table tree is created in the rtentry structure and has two rtentry structures. Each rtentry structure contains two radix_node structures, because each time a tree is inserted
Two nodes are required for a new route: one is an internal node, corresponding to a test bit, and the other is a leaf, corresponding to a host route or a network
Routing.
The Protocol control block PCB in each UDP and TCP plug-in contains a route structure pointing to the rtentry structure. Each time an IP data report is sent, UDP
And the TCP output function both pass a pointer to the route structure in the PCB as a parameter for calling ip_output. PCB with the same route points to the same route
Table item.
2. optional plug-ins
12 different types of optional messages are provided. The message type is in the rtm_type field in the rt_msghdr structure.
3. function call
Shows the relationship between the optional functions.
The rtalloc function is called by the Internet Protocol to find the route to the specified destination. The figure also shows five typical programs for creating plug-ins in the selected domain.
ARP processes the ARP high-speed cache, which is stored in the IP routing table of net/3.
Gated and routed are routing daemon processes. They communicate with other routers and operate the route table of the kernel when the selected route environment changes.
A route is usually a Program executed by a STARTUP script or a system administrator to add or delete a route.
Rwhod will call an optional sysctl to determine the connection interface at startup.
4. Radix node Data Structure
The header of each route table is a radix_node_head structure, and all nodes in the route number are radix_node structures. The radix_node_head structure is shown in figure
As shown in:
Rnh_treetop points to the radix_node structure at the top of the number of routes. Three radix_node structures are allocated at the end of the radix_node_head structure,
The vertex in the middle is initialized as the vertex of the tree.
From rnh_addaddr to rnh_tree are seven function pointers. the functions they direct to are called to complete Tree operations. In, rn_inithead only starts
And the four pointers are not used.
The radix_node structure of the nodes in the tree is given.
The first five members are members of both the internal node and the leaf node, followed by a Union: If the node is a leaf, it defines three members; if the node is an internal node
It defines three different members.
Rn_mklist is the header of the node mask linked list.
Rn_p points to the parent node of the node.
If the rn_ B value is greater than or equal to 0, the node is an internal node; otherwise, the node is a leaf. For internal nodes, rn_ B is the bit location to be tested. For leaves
For a node, rn_ B is negative, and its value is equal to-1 minus the netmask index. This index is the first zero bit position in the case of pressure. Given
The index of the mask.
The internal node rn_bmask is a single-byte mask, used to detect whether the corresponding bit is 0 or 1. In the leaves, its value is 0.
Three values of rn_flags are given.
For leaves, rn_key points to the plug-in address structure, and rn_mask points to the plug-in address structure that stores the mask. If rn_mask is null, its mask is implicit
Value 1 (that is, the route points to a host rather than a network ).
5. Routing Structure
The key to accessing kernel routing information is:
1. The rtalloc function is used to find the route to the destination.
2. route structure. Its value is filled in by the rtalloc function.
3. The rtentry structure pointed to by the route structure.
The Protocol control block (PCB) used in UDP and TCP contains a route structure.
Ro_dst is defined as a general plug-in address structure, but for Internet protocol, it is a sockaddr_in structure.
The rtentry structure is defined.
The structure contains two radix_node structures. Each time a new leaf is added to the routing tree, an internal node must be added. rt_nodes [0] is the leaf,
Rt_nodes [1] is an internal node.
Various constants stored in rt_flags and corresponding flags characters output by netstat are provided.
If the rtf_gateway flag is set, the plug-in address structure pointer in rt_gateway points to the network address. Similarly, rt_gwroute points
The rtentry of the gateway.
Rt_refcnt is a counter that stores the number of references that are containing this structure.
When the storage space of this structure is allocated, rt_use is initialized to 0. The value of rt_use increases progressively every time an IP data report is output using this route.
Rt_ifp and rt_ifa refer to the interface structure and interface address structure respectively.
The rt_llinfo pointer allows the link layer protocol to store the dedicated structure pointer of the Protocol in the routing table. This article describes how to use this pointer when introducing arp.
The rt_metrics structure contained in the rtentry structure is given.
The six members in the structure are used in TCP. In ARP, rmx_expire is used as the timer for each ARP route entry.
6. Initialization: route_init and rtable_init Functions
The fields related to the domain structure in each protocol family are given.
The pf_route domain is the only domain with an initialization function. Similarly, only the domains that require the route table have the dom_rtattach function, and this function is always rn_inithead.
Route tables are not required for the selected domain and Unix domain.
The dom_rtoffset member is the offset of the first bit detected during the path selection process (calculated from the beginning of the domain plug-in address structure ).
Dom_maxrtkey indicates the length of the byte of the structure.
Lists the steps involved in route table initialization.
During system initialization, the main function of the kernel calls the domaininit function once. The adddomain macro is used to create a Domain Structure linked list and call
The dom_init function of each domain.
7. Initialization: rn_init and rn_inithead Functions
The rn_init function is called only once by route_init to initialize some global variables used by the radix function.
Call rn_inithead to initialize the header of the address mask routing tree and point the global variable mask_rnhead to the radix_node_head structure.
The radix_node_head structure created for the Internet protocol.
These three radix_node structures form a tree: The Middle structure is the vertex of the tree, the leftmost leaf of the first structured tree, and the left structure is the most
On the right side of the leaf, the parent pointer of the three nodes points to the middle node.
The keys at the leftmost node are all 0 (rn_zeros), and keys at the rightmost node are all 1 (rn_ones ).
All three nodes have the rnf_root flag, which indicates that they are all the original nodes of the tree. They are also the only nodes with this sign.
8. Repeat key and mask list
The following describes two fields in the radix_node structure: one is rn_dupedkey, which forms an additional radix_node Structure linked list with duplicate keys; the other is
Is rn_mklist, which is the beginning of the radix_mask Structure linked list with a network mask. In the preceding routing tree, the left-most boxes are marked with end and default.
Duplicate key. The node with the rnf_root flag on the far left has a zero-bit key, but it is the same as the default route key.
Two nodes with full 0-bit duplicate keys are provided.
The top node in the figure is the vertex of the routing tree. The next two nodes are the leaves (their rn_ B is negative), and The rn_dupedkey of the first leaf
The member points to the second node.
The first leaf is the rnh_nodes [0] structure, which is the end node on the left of the tree and has the rnf_root flag. Its key is set to rn_inithead
Rn_zeros.
The second leaf is the default route table entry. Its rn_key points to the sockaddr_in structure of 0.0.0.0 and has a full 0 mask.
The last one is the radix_mask structure. The top node of the tree and the leaf corresponding to the default route point to this structure. In this list, the mask list of the top node of the tree is displayed in
When you look for a network mask, the backtracking algorithm uses it. The radix_mask structure list is used together with the internal nodes to determine the mask used for the Child tree starting from the node.
Note: keys with the same value cannot be shared with each other.
9. rn_match Function
In Internet protocol, it is called the rnh_matchaddr function. It will be called by the rtallocl function (and the rtallocl function will be called by the rtalloc function ).
The algorithm is as follows:
1. Search from the top of the tree until the leaf corresponding to the search key bit is reached. Check the leaf to get a precise match.
2. Check the leaf node to see if a matched network address can be obtained.
3. backtracking.
Details on TCP/IP 2: Implementation -- Radix tree route table