Some relevant data of Linux routing table __linux

Source: Internet
Author: User
Some relevant data of Linux routing table

===============================================================================

Linux Routing Table maintenance view Linux kernel routing table

Use the following route command to view the Linux kernel routing table.

# route
Destination     Gateway         genmask         Flags Metric Ref use    iface
192.168.0.0     *               255.255.255.0   u     0      0        0 eth0
169.254.0.0     *               255.255.0.0     u     0      0        0 eth0
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

Output item description for the route command

Output Items Description
Destination Target network segment or host
Gateway Gateway address, "*" means that the destination is the network to which this host belongs, and does not require routing
Genmask Network mask
Flags Mark. Some of the possible tags are as follows:
U-routing is active
H-Target is a host
G-Route Pointing Gateway
R-Recovery table entries generated by dynamic routing
D-Dynamically installed by the routed background program
M-by-routing background Program modification
! -Reject Route
Metric Routing distance, the number of relays required to reach the specified network (not used in the Linux kernel)
Ref Route item reference times (not used in the Linux kernel)
Use The number of times this route item was found by the routing software
Iface The output interface corresponding to the routing table entry
3 Types of Routes Host Routing

Host routing is a routing record in the routing table that points to a single IP address or host name. The Flags field for host routing is H. For example, in the following example, a local host 192.168.1.1 a router with an IP address that reaches a host with an IP address of 10.0.0.10.

Destination    Gateway       genmask        Flags     Metric    Ref    use    iface
-----------    -------     -------            -----     ------    ---    ---    -----
10.0.0.10     192.168.1.1    255.255.255.255   UH       0    0      0    eth0
Network Routing

Network routing is a network that can be reached on behalf of a host. The flags field for the network route is N. For example, in the following example, the local host forwards packets sent to the network 192.19.12 to a router with an IP address of 192.168.1.1.

Destination    Gateway       genmask      Flags    Metric    Ref     use    iface
-----------    -------     -------         -----    -----------
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0
default route

When a host cannot find the IP address or network route of a destination host in the routing table, the packet is sent to the default route (the default gateway). The Flags field for the default route is G. For example, in the following example, the default route is a router with an IP address of 192.168.1.1.

Destination    Gateway       genmask    Flags     Metric    Ref    use    iface
-----------    -------     ------------      ------    ---    ---    -----
default       192.168.1.1     0.0.0.0    UG       0        0     0    eth0
Configuring static Routes Route Command

To set and view the routing table you can use the route command to set the command format for the kernel routing table:

# route  [Add|del] [-net|-host] target [netmask Nm] [GW GW] [[Dev] If]

Where: add: Adding a Routing rule del: Delete a routing rule-net: The destination address is a network-host: Destination address is a host target: Destination network or host netmask: Network mask for destination address GW: Gateway to route packets through Dev: An example of the route command used for routing-specific network interfaces

Routes added to the host

# route add-host 192.168.1.2 Dev eth0:0
# route add-host 10.20.30.148 GW 10.20.30.40

Routes added to the network

# route add-net 10.20.30.40 netmask 255.255.255.248 eth0
# route add-net 10.20.30.48 netmask 255.255.255.248 GW 10.2 0.30.41
# route add-net 192.168.1.0/24 eth1

Add default route

# route add default GW 192.168.1.1

Delete route

# route del-host 192.168.1.2 Dev eth0:0
# route del-host 10.20.30.148 GW 10.20.30.40
# route del-net 10.20.30.4 0 netmask 255.255.255.248 eth0
# route del-net 10.20.30.48 netmask 255.255.255.248 GW 10.20.30.41
# route Del-ne T 192.168.1.0/24 eth1
# route del default GW 192.168.1.1
Set Packet forwarding

The default kernel configuration in CentOS already contains routing features, but the default does not enable this feature at system startup. The routing function on Linux can be implemented by adjusting the network parameters of the kernel. To configure and adjust kernel parameters, you can use the Sysctl command. For example, to turn on the Linux kernel's packet forwarding function, you can use the following command.

# sysctl-w Net.ipv4.ip_forward=1

After this setting, the current system can implement packet forwarding, but will fail the next time you start the computer. For the next time you start your computer, you need to write the following lines to the configuration file/etc/sysctl.conf.

# vi/etc/sysctl.conf
Net.ipv4.ip_forward = 1

Users can also use the following command to see whether the current system supports packet forwarding.

# sysctl  Net.ipv4.ip_forward


===============================================================================

Analysis of structure and algorithm of Linux routing table Huang-wen routing is the core of the network stack. The design of the routing table itself has a great influence on the performance of the routing, and the good design can reduce the consumption of the system resources, which is especially reflected in the lookup of the routing table. There are two kinds of search algorithms for kernel routing, one is the hash algorithm, the other is the Lc-trie algorithm, the former is the current kernel use of the default algorithm, and the latter is more suitable in the case of the large routing table, it improves the search efficiency in this case, greatly increasing the complexity of the algorithm itself and memory consumption.   In summary, the two algorithms have their own application, this paper analyzes the code based on the 2.6.18 kernel routing in the hash algorithm on the road by the implementation of the table, and at the end of the article gives a simple strategy routing application. The structure of the routing table in order to support policy Routing, Linux uses multiple routing tables instead of one, and even without the use of Policy Routing, Linux uses two routing tables, one for upload to the local upper layer protocol and the other for forwarding.          Linux uses multiple routing tables rather than one, so that the routing of different policies is stored in different tables, effectively exempted from the search for large routing tables, and improves the efficiency of searching in a certain degree. The routing table itself is not represented by a struct, but is composed of multiple structures. A routing table can be said to be a hierarchical combination of structures. On the first layer, it divides all routes into 33 parts (Structfn_zone) based on the length (0~32) of the subnet mask (netmask), then in the same subnet mask (the same layer), and then according to the different subnets (such as 10.1.1.0/24 and 10.1.2.0/24) , divided into the second layer (struct fib_node), in the same subnet, it is possible to use different routes due to the different properties such as TOS, which is the third layer (Structfib_alias), the third layer represents a routing table entry, and each routing table entry includes a corresponding parameter. such as protocol, next hop routing address and so on, this is the fourth layer (structfib_info). The benefits of tiering are obvious, which makes routing tables more optimized, logically clearer, and allows data to be shared (such as structfib_info), thereby reducing data redundancy.

struct fib_table *fib_tables[rt_table_max+1]; Rt_table_max is 255

Figure 1 is the overall structure of a routing table. From top to bottom from left to right, it is first an array of fib_table structure pointers, which are defined as:

struct Fib_table {       unsigned char tb_id;        unsigned tb_stamp;        int           (*tb_lookup ) (struct fib_table *tb, const struct FLOWI *flp, struct fib_result);        int           (*tb_insert ) (struct fib_table *table, struct rtmsg *r,                       ...        void         (*tb_select_default) (struct fib_table *table,                                           const struct FLOWI *flp, struct FIB_RESULt *res);          unsigned char tb_data[0]; };

Each fib_table structure represents a routing table in the kernel: + Figure 1 (quote [1]) This structure includes the ID of this table, as well as some of the main function pointers for manipulating the routing table, where we only care about the last domain ――tb_data[0], which is a 0-long array, which in the kernel also More common, it says

struct Fn_hash {struct fn_zone *fn_zones[33]; struct fn_zone *fn_zone_list;};

Point to the end of this structure. As you can see from Figure 1, the end of this structure is followed by a struct Fn_hash structure that is distributed along with the fib_table structure, so Fib_table->tb_data is Fn_hash.

struct Fn_zone {       struct fn_zone           *fz_next; /* Next not empty zone */       struct hlist_head     *fz_hash ;      /* Hash Table pointer      * *         int                                 fz_nent;   /* Number of entries      *          int                                  fz_divisor;      /* Hash divisor              * *        u32                              Fz_ hashmask; /* (FZ_DIVISOR-1)   /* #define FZ_HASHMASK (FZ)           ((FZ)->fz_hashmask)          int                                  Fz_order; /* Zone order         * *        u32                               Fz_mask; #define FZ_MASK (FZ)           ((FZ)->fz_mask)   };

This fn_zone domain is our advance structure, which separates the route from the length of the subnet mask into 33 parts, where fn_zones[0] is used for the default gateway. The fn_zone_list domain is the fn_zone chain that is being used as a linked list. Then go deep into the struct Fn_zone structure: There are two domains in this structure that are more important, one for the Fz_hash field, and it points to the header of a hash table, the length of which is fz_divisor.   And the length of this hash table is variable, when the table length reaches a limit, the hash table will be rebuilt, avoid the occurrence of hash conflict table too long cause the search efficiency is reduced. In order to improve the efficiency of the lookup, the kernel uses a large number of hash tables, and the routing table is an example. As you can see in Figure 1, routes such as the eldest subnet mask are stored in the same fn_zone and are hashed into the corresponding list according to the routing key value (Fn_key) to the different subnets (Fib_node).

struct Fib_node {struct Hlist_node fn_hash;        struct List_head fn_alias; U32 Fn_key; };
This key value is actually this subnet value (such as 10.1.1.0/24, then the subnet value is 10.1.1), get this key value through the n =fn_hash () function hash is this subnet corresponding to the hash value, and then can be inserted into the corresponding fz_hash[n] list. The Fib_node of the conflict is a chain of fn_hash domains, and Fn_alias is a route to the subnet.

struct Fib_alias {       struct LIST_HEAD  &NBS p;    fa_list;        struct rcu_head      rcu;        struct fib_info        *fa_info;        u8                   Fa_tos;        u8                   Fa_type;        u8                   Fa_scope;        u8                   fa_state; };
When routes to this subnet can exist for multiple routes due to differences in attributes such as TOS, they link these routing tables into a list through the fa_list domain in Fib_alias. Another domain in this structure fa_info points to a fib_info structure, which is the structure that holds the truly important routing information.

struct Fib_info {       struct hlist_node     fib_hash;         struct hlist_node     fib_lhash;        ...        int                   Fib_dead;        unsigned         fib_flags;        int                   Fib_protocol;        u32                 fib_prefsrc;        u32                 fib_priority;        ... int                          FIB_NHS;        struct fib_nh          fib_nh[0 ]; #define fib_dev             Fib_nh[0].nh_dev};
  This structure is a symbol and attribute for routing, one of the most important domain is fib_nh[0], where we see the application of 0 long array, it is through 0 long to achieve the function of the variable length structure. Because, we need a fixed-length fib_info structure, but at the end of this structure, the number of FIB_NH structures we need is indeterminate and is determined at runtime. In this way, we can make use of this structure to allocate space at runtime for Fib_info, at the same time allocate the desired number of FIB_NH array at the end, and this structure array can be accessed through Fib_info->fib_nh[n], completing fib_ The FIB_NHS field is placed as the length of the array after the allocation of info.   On the other hand, Fib_info is also an application of the hash table, there are two domains in the structure, namely Fib_hash and Fib_lhash, which are all used in the hash list. After the assignment is completed, this structure will be chained into the Fib_info_hash table with the Fib_hash domain, and if the route has a preferred source address, the Fib_info will be fib_lhash linked to the Fib_info_laddrhash table. In this way, you can achieve a quick lookup based on different purposes.   STRUCTFIB_NH is also an important structure. It holds the address of the next hop route (NH_GW). As has just been mentioned, a route (Fib_alias) may have multiple FIB_NH structures that indicate that the route has multiple next-hop addresses, that is, it is multipath (multipath). The next hop address choice also has a variety of algorithms, these algorithms are based on the nh_weight,nh_power domain. The Nh_hash domain is used to link the nh_hash into the hash table.

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.